jscript: Added Date.setYear implementation.
[wine] / dlls / urlmon / http.c
1 /*
2  * Copyright 2005 Jacek Caban
3  * Copyright 2007 Misha Koshelev
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18  */
19
20 #include "urlmon_main.h"
21 #include "wininet.h"
22
23 #define NO_SHLWAPI_REG
24 #include "shlwapi.h"
25
26 #include "wine/debug.h"
27
28 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
29
30 typedef struct {
31     Protocol base;
32
33     IInternetProtocolEx IInternetProtocolEx_iface;
34     IInternetPriority   IInternetPriority_iface;
35     IWinInetHttpInfo    IWinInetHttpInfo_iface;
36
37     BOOL https;
38     IHttpNegotiate *http_negotiate;
39     LPWSTR full_header;
40
41     LONG ref;
42 } HttpProtocol;
43
44 static inline HttpProtocol *impl_from_IInternetProtocolEx(IInternetProtocolEx *iface)
45 {
46     return CONTAINING_RECORD(iface, HttpProtocol, IInternetProtocolEx_iface);
47 }
48
49 static inline HttpProtocol *impl_from_IInternetPriority(IInternetPriority *iface)
50 {
51     return CONTAINING_RECORD(iface, HttpProtocol, IInternetPriority_iface);
52 }
53
54 static inline HttpProtocol *impl_from_IWinInetHttpInfo(IWinInetHttpInfo *iface)
55 {
56     return CONTAINING_RECORD(iface, HttpProtocol, IWinInetHttpInfo_iface);
57 }
58
59 /* Default headers from native */
60 static const WCHAR wszHeaders[] = {'A','c','c','e','p','t','-','E','n','c','o','d','i','n','g',
61                                    ':',' ','g','z','i','p',',',' ','d','e','f','l','a','t','e',0};
62
63 static LPWSTR query_http_info(HttpProtocol *This, DWORD option)
64 {
65     LPWSTR ret = NULL;
66     DWORD len = 0;
67     BOOL res;
68
69     res = HttpQueryInfoW(This->base.request, option, NULL, &len, NULL);
70     if (!res && GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
71         ret = heap_alloc(len);
72         res = HttpQueryInfoW(This->base.request, option, ret, &len, NULL);
73     }
74     if(!res) {
75         TRACE("HttpQueryInfoW(%d) failed: %08x\n", option, GetLastError());
76         heap_free(ret);
77         return NULL;
78     }
79
80     return ret;
81 }
82
83 #define ASYNCPROTOCOL_THIS(iface) DEFINE_THIS2(HttpProtocol, base, iface)
84
85 static HRESULT HttpProtocol_open_request(Protocol *prot, IUri *uri, DWORD request_flags,
86         HINTERNET internet_session, IInternetBindInfo *bind_info)
87 {
88     HttpProtocol *This = ASYNCPROTOCOL_THIS(prot);
89     INTERNET_BUFFERSW send_buffer = {sizeof(INTERNET_BUFFERSW)};
90     LPWSTR addl_header = NULL, post_cookie = NULL;
91     IServiceProvider *service_provider = NULL;
92     IHttpNegotiate2 *http_negotiate2 = NULL;
93     BSTR url, host, user, pass, path;
94     LPOLESTR accept_mimes[257];
95     const WCHAR **accept_types;
96     BYTE security_id[512];
97     DWORD len = 0, port;
98     ULONG num;
99     BOOL res, b;
100     HRESULT hres;
101
102     static const WCHAR wszBindVerb[BINDVERB_CUSTOM][5] =
103         {{'G','E','T',0},
104          {'P','O','S','T',0},
105          {'P','U','T',0}};
106
107     hres = IUri_GetPort(uri, &port);
108     if(FAILED(hres))
109         return hres;
110
111     hres = IUri_GetHost(uri, &host);
112     if(FAILED(hres))
113         return hres;
114
115     hres = IUri_GetUserName(uri, &user);
116     if(SUCCEEDED(hres)) {
117         hres = IUri_GetPassword(uri, &pass);
118
119         if(SUCCEEDED(hres)) {
120             This->base.connection = InternetConnectW(internet_session, host, port, user, pass,
121                     INTERNET_SERVICE_HTTP, This->https ? INTERNET_FLAG_SECURE : 0, (DWORD_PTR)&This->base);
122             SysFreeString(pass);
123         }
124         SysFreeString(user);
125     }
126     SysFreeString(host);
127     if(FAILED(hres))
128         return hres;
129     if(!This->base.connection) {
130         WARN("InternetConnect failed: %d\n", GetLastError());
131         return INET_E_CANNOT_CONNECT;
132     }
133
134     num = sizeof(accept_mimes)/sizeof(accept_mimes[0])-1;
135     hres = IInternetBindInfo_GetBindString(bind_info, BINDSTRING_ACCEPT_MIMES, accept_mimes, num, &num);
136     if(hres == INET_E_USE_DEFAULT_SETTING) {
137         static const WCHAR default_accept_mimeW[] = {'*','/','*',0};
138         static const WCHAR *default_accept_mimes[] = {default_accept_mimeW, NULL};
139
140         accept_types = default_accept_mimes;
141         num = 0;
142     }else if(hres == S_OK) {
143         accept_types = (const WCHAR**)accept_mimes;
144     }else {
145         WARN("GetBindString BINDSTRING_ACCEPT_MIMES failed: %08x\n", hres);
146         return INET_E_NO_VALID_MEDIA;
147     }
148     accept_mimes[num] = 0;
149
150     if(This->https)
151         request_flags |= INTERNET_FLAG_SECURE;
152
153     hres = IUri_GetPathAndQuery(uri, &path);
154     if(SUCCEEDED(hres)) {
155         This->base.request = HttpOpenRequestW(This->base.connection,
156                 This->base.bind_info.dwBindVerb < BINDVERB_CUSTOM
157                     ? wszBindVerb[This->base.bind_info.dwBindVerb] : This->base.bind_info.szCustomVerb,
158                 path, NULL, NULL, accept_types, request_flags, (DWORD_PTR)&This->base);
159         SysFreeString(path);
160     }
161     while(num--)
162         CoTaskMemFree(accept_mimes[num]);
163     if(FAILED(hres))
164         return hres;
165     if (!This->base.request) {
166         WARN("HttpOpenRequest failed: %d\n", GetLastError());
167         return INET_E_RESOURCE_NOT_FOUND;
168     }
169
170     hres = IInternetProtocolSink_QueryInterface(This->base.protocol_sink, &IID_IServiceProvider,
171             (void **)&service_provider);
172     if (hres != S_OK) {
173         WARN("IInternetProtocolSink_QueryInterface IID_IServiceProvider failed: %08x\n", hres);
174         return hres;
175     }
176
177     hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate,
178             &IID_IHttpNegotiate, (void **)&This->http_negotiate);
179     if (hres != S_OK) {
180         WARN("IServiceProvider_QueryService IID_IHttpNegotiate failed: %08x\n", hres);
181         IServiceProvider_Release(service_provider);
182         return hres;
183     }
184
185     hres = IUri_GetAbsoluteUri(uri, &url);
186     if(FAILED(hres)) {
187         IServiceProvider_Release(service_provider);
188         return hres;
189     }
190
191     hres = IHttpNegotiate_BeginningTransaction(This->http_negotiate, url, wszHeaders,
192             0, &addl_header);
193     SysFreeString(url);
194     if(hres != S_OK) {
195         WARN("IHttpNegotiate_BeginningTransaction failed: %08x\n", hres);
196         IServiceProvider_Release(service_provider);
197         return hres;
198     }
199
200     if(addl_header) {
201         int len_addl_header = strlenW(addl_header);
202
203         This->full_header = heap_alloc(len_addl_header*sizeof(WCHAR)+sizeof(wszHeaders));
204
205         lstrcpyW(This->full_header, addl_header);
206         lstrcpyW(&This->full_header[len_addl_header], wszHeaders);
207         CoTaskMemFree(addl_header);
208     }else {
209         This->full_header = (LPWSTR)wszHeaders;
210     }
211
212     hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate2,
213             &IID_IHttpNegotiate2, (void **)&http_negotiate2);
214     IServiceProvider_Release(service_provider);
215     if(hres != S_OK) {
216         WARN("IServiceProvider_QueryService IID_IHttpNegotiate2 failed: %08x\n", hres);
217         /* No goto done as per native */
218     }else {
219         len = sizeof(security_id)/sizeof(security_id[0]);
220         hres = IHttpNegotiate2_GetRootSecurityId(http_negotiate2, security_id, &len, 0);
221         IHttpNegotiate2_Release(http_negotiate2);
222         if (hres != S_OK)
223             WARN("IHttpNegotiate2_GetRootSecurityId failed: %08x\n", hres);
224     }
225
226     /* FIXME: Handle security_id. Native calls undocumented function IsHostInProxyBypassList. */
227
228     if(This->base.bind_info.dwBindVerb == BINDVERB_POST) {
229         num = 0;
230         hres = IInternetBindInfo_GetBindString(bind_info, BINDSTRING_POST_COOKIE, &post_cookie, 1, &num);
231         if(hres == S_OK && num) {
232             if(!InternetSetOptionW(This->base.request, INTERNET_OPTION_SECONDARY_CACHE_KEY,
233                                    post_cookie, lstrlenW(post_cookie)))
234                 WARN("InternetSetOption INTERNET_OPTION_SECONDARY_CACHE_KEY failed: %d\n", GetLastError());
235             CoTaskMemFree(post_cookie);
236         }
237     }
238
239     send_buffer.lpcszHeader = This->full_header;
240     send_buffer.dwHeadersLength = send_buffer.dwHeadersTotal = strlenW(This->full_header);
241
242     if(This->base.bind_info.dwBindVerb != BINDVERB_GET) {
243         switch(This->base.bind_info.stgmedData.tymed) {
244         case TYMED_HGLOBAL:
245             /* Native does not use GlobalLock/GlobalUnlock, so we won't either */
246             send_buffer.lpvBuffer = This->base.bind_info.stgmedData.u.hGlobal;
247             send_buffer.dwBufferLength = send_buffer.dwBufferTotal = This->base.bind_info.cbstgmedData;
248             break;
249         case TYMED_ISTREAM: {
250             LARGE_INTEGER offset;
251
252             send_buffer.dwBufferTotal = This->base.bind_info.cbstgmedData;
253             This->base.post_stream = This->base.bind_info.stgmedData.u.pstm;
254             IStream_AddRef(This->base.post_stream);
255
256             offset.QuadPart = 0;
257             IStream_Seek(This->base.post_stream, offset, STREAM_SEEK_SET, NULL);
258             break;
259         }
260         default:
261             FIXME("Unsupported This->base.bind_info.stgmedData.tymed %d\n", This->base.bind_info.stgmedData.tymed);
262         }
263     }
264
265     b = TRUE;
266     res = InternetSetOptionW(This->base.request, INTERNET_OPTION_HTTP_DECODING, &b, sizeof(b));
267     if(!res)
268         WARN("InternetSetOption(INTERNET_OPTION_HTTP_DECODING) failed: %08x\n", GetLastError());
269
270     if(This->base.post_stream)
271         res = HttpSendRequestExW(This->base.request, &send_buffer, NULL, 0, 0);
272     else
273         res = HttpSendRequestW(This->base.request, send_buffer.lpcszHeader, send_buffer.dwHeadersLength,
274                 send_buffer.lpvBuffer, send_buffer.dwBufferLength);
275     if(!res && GetLastError() != ERROR_IO_PENDING) {
276         WARN("HttpSendRequest failed: %d\n", GetLastError());
277         return INET_E_DOWNLOAD_FAILURE;
278     }
279
280     return S_OK;
281 }
282
283 static HRESULT HttpProtocol_end_request(Protocol *protocol)
284 {
285     BOOL res;
286
287     res = HttpEndRequestW(protocol->request, NULL, 0, 0);
288     if(!res && GetLastError() != ERROR_IO_PENDING) {
289         FIXME("HttpEndRequest failed: %u\n", GetLastError());
290         return E_FAIL;
291     }
292
293     return S_OK;
294 }
295
296 static HRESULT HttpProtocol_start_downloading(Protocol *prot)
297 {
298     HttpProtocol *This = ASYNCPROTOCOL_THIS(prot);
299     LPWSTR content_type, content_length, ranges;
300     DWORD len = sizeof(DWORD);
301     DWORD status_code;
302     BOOL res;
303     HRESULT hres;
304
305     static const WCHAR wszDefaultContentType[] =
306         {'t','e','x','t','/','h','t','m','l',0};
307
308     if(!This->http_negotiate) {
309         WARN("Expected IHttpNegotiate pointer to be non-NULL\n");
310         return S_OK;
311     }
312
313     res = HttpQueryInfoW(This->base.request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER,
314             &status_code, &len, NULL);
315     if(res) {
316         LPWSTR response_headers = query_http_info(This, HTTP_QUERY_RAW_HEADERS_CRLF);
317         if(response_headers) {
318             hres = IHttpNegotiate_OnResponse(This->http_negotiate, status_code, response_headers,
319                     NULL, NULL);
320             heap_free(response_headers);
321             if (hres != S_OK) {
322                 WARN("IHttpNegotiate_OnResponse failed: %08x\n", hres);
323                 return S_OK;
324             }
325         }
326     }else {
327         WARN("HttpQueryInfo failed: %d\n", GetLastError());
328     }
329
330     ranges = query_http_info(This, HTTP_QUERY_ACCEPT_RANGES);
331     if(ranges) {
332         IInternetProtocolSink_ReportProgress(This->base.protocol_sink, BINDSTATUS_ACCEPTRANGES, NULL);
333         heap_free(ranges);
334     }
335
336     content_type = query_http_info(This, HTTP_QUERY_CONTENT_TYPE);
337     if(content_type) {
338         /* remove the charset, if present */
339         LPWSTR p = strchrW(content_type, ';');
340         if (p) *p = '\0';
341
342         IInternetProtocolSink_ReportProgress(This->base.protocol_sink,
343                 (This->base.bindf & BINDF_FROMURLMON)
344                  ? BINDSTATUS_MIMETYPEAVAILABLE : BINDSTATUS_RAWMIMETYPE,
345                  content_type);
346         heap_free(content_type);
347     }else {
348         WARN("HttpQueryInfo failed: %d\n", GetLastError());
349         IInternetProtocolSink_ReportProgress(This->base.protocol_sink,
350                  (This->base.bindf & BINDF_FROMURLMON)
351                   ? BINDSTATUS_MIMETYPEAVAILABLE : BINDSTATUS_RAWMIMETYPE,
352                   wszDefaultContentType);
353     }
354
355     content_length = query_http_info(This, HTTP_QUERY_CONTENT_LENGTH);
356     if(content_length) {
357         This->base.content_length = atoiW(content_length);
358         heap_free(content_length);
359     }
360
361     return S_OK;
362 }
363
364 static void HttpProtocol_close_connection(Protocol *prot)
365 {
366     HttpProtocol *This = ASYNCPROTOCOL_THIS(prot);
367
368     if(This->http_negotiate) {
369         IHttpNegotiate_Release(This->http_negotiate);
370         This->http_negotiate = 0;
371     }
372
373     if(This->full_header) {
374         if(This->full_header != wszHeaders)
375             heap_free(This->full_header);
376         This->full_header = 0;
377     }
378 }
379
380 #undef ASYNCPROTOCOL_THIS
381
382 static const ProtocolVtbl AsyncProtocolVtbl = {
383     HttpProtocol_open_request,
384     HttpProtocol_end_request,
385     HttpProtocol_start_downloading,
386     HttpProtocol_close_connection
387 };
388
389 static HRESULT WINAPI HttpProtocol_QueryInterface(IInternetProtocolEx *iface, REFIID riid, void **ppv)
390 {
391     HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
392
393     *ppv = NULL;
394     if(IsEqualGUID(&IID_IUnknown, riid)) {
395         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
396         *ppv = &This->IInternetProtocolEx_iface;
397     }else if(IsEqualGUID(&IID_IInternetProtocolRoot, riid)) {
398         TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", This, ppv);
399         *ppv = &This->IInternetProtocolEx_iface;
400     }else if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
401         TRACE("(%p)->(IID_IInternetProtocol %p)\n", This, ppv);
402         *ppv = &This->IInternetProtocolEx_iface;
403     }else if(IsEqualGUID(&IID_IInternetProtocolEx, riid)) {
404         TRACE("(%p)->(IID_IInternetProtocolEx %p)\n", This, ppv);
405         *ppv = &This->IInternetProtocolEx_iface;
406     }else if(IsEqualGUID(&IID_IInternetPriority, riid)) {
407         TRACE("(%p)->(IID_IInternetPriority %p)\n", This, ppv);
408         *ppv = &This->IInternetPriority_iface;
409     }else if(IsEqualGUID(&IID_IWinInetInfo, riid)) {
410         TRACE("(%p)->(IID_IWinInetInfo %p)\n", This, ppv);
411         *ppv = &This->IWinInetHttpInfo_iface;
412     }else if(IsEqualGUID(&IID_IWinInetHttpInfo, riid)) {
413         TRACE("(%p)->(IID_IWinInetHttpInfo %p)\n", This, ppv);
414         *ppv = &This->IWinInetHttpInfo_iface;
415     }
416
417     if(*ppv) {
418         IInternetProtocol_AddRef(iface);
419         return S_OK;
420     }
421
422     WARN("not supported interface %s\n", debugstr_guid(riid));
423     return E_NOINTERFACE;
424 }
425
426 static ULONG WINAPI HttpProtocol_AddRef(IInternetProtocolEx *iface)
427 {
428     HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
429     LONG ref = InterlockedIncrement(&This->ref);
430     TRACE("(%p) ref=%d\n", This, ref);
431     return ref;
432 }
433
434 static ULONG WINAPI HttpProtocol_Release(IInternetProtocolEx *iface)
435 {
436     HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
437     LONG ref = InterlockedDecrement(&This->ref);
438
439     TRACE("(%p) ref=%d\n", This, ref);
440
441     if(!ref) {
442         protocol_close_connection(&This->base);
443         heap_free(This);
444
445         URLMON_UnlockModule();
446     }
447
448     return ref;
449 }
450
451 static HRESULT WINAPI HttpProtocol_Start(IInternetProtocolEx *iface, LPCWSTR szUrl,
452         IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
453         DWORD grfPI, HANDLE_PTR dwReserved)
454 {
455     HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
456     IUri *uri;
457     HRESULT hres;
458
459     TRACE("(%p)->(%s %p %p %08x %lx)\n", This, debugstr_w(szUrl), pOIProtSink,
460             pOIBindInfo, grfPI, dwReserved);
461
462     hres = CreateUri(szUrl, 0, 0, &uri);
463     if(FAILED(hres))
464         return hres;
465
466     hres = IInternetProtocolEx_StartEx(&This->IInternetProtocolEx_iface, uri, pOIProtSink,
467             pOIBindInfo, grfPI, (HANDLE*)dwReserved);
468
469     IUri_Release(uri);
470     return hres;
471 }
472
473 static HRESULT WINAPI HttpProtocol_Continue(IInternetProtocolEx *iface, PROTOCOLDATA *pProtocolData)
474 {
475     HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
476
477     TRACE("(%p)->(%p)\n", This, pProtocolData);
478
479     return protocol_continue(&This->base, pProtocolData);
480 }
481
482 static HRESULT WINAPI HttpProtocol_Abort(IInternetProtocolEx *iface, HRESULT hrReason,
483         DWORD dwOptions)
484 {
485     HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
486
487     TRACE("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);
488
489     return protocol_abort(&This->base, hrReason);
490 }
491
492 static HRESULT WINAPI HttpProtocol_Terminate(IInternetProtocolEx *iface, DWORD dwOptions)
493 {
494     HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
495
496     TRACE("(%p)->(%08x)\n", This, dwOptions);
497
498     protocol_close_connection(&This->base);
499     return S_OK;
500 }
501
502 static HRESULT WINAPI HttpProtocol_Suspend(IInternetProtocolEx *iface)
503 {
504     HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
505     FIXME("(%p)\n", This);
506     return E_NOTIMPL;
507 }
508
509 static HRESULT WINAPI HttpProtocol_Resume(IInternetProtocolEx *iface)
510 {
511     HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
512     FIXME("(%p)\n", This);
513     return E_NOTIMPL;
514 }
515
516 static HRESULT WINAPI HttpProtocol_Read(IInternetProtocolEx *iface, void *pv,
517         ULONG cb, ULONG *pcbRead)
518 {
519     HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
520
521     TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
522
523     return protocol_read(&This->base, pv, cb, pcbRead);
524 }
525
526 static HRESULT WINAPI HttpProtocol_Seek(IInternetProtocolEx *iface, LARGE_INTEGER dlibMove,
527         DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
528 {
529     HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
530     FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
531     return E_NOTIMPL;
532 }
533
534 static HRESULT WINAPI HttpProtocol_LockRequest(IInternetProtocolEx *iface, DWORD dwOptions)
535 {
536     HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
537
538     TRACE("(%p)->(%08x)\n", This, dwOptions);
539
540     return protocol_lock_request(&This->base);
541 }
542
543 static HRESULT WINAPI HttpProtocol_UnlockRequest(IInternetProtocolEx *iface)
544 {
545     HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
546
547     TRACE("(%p)\n", This);
548
549     return protocol_unlock_request(&This->base);
550 }
551
552 static HRESULT WINAPI HttpProtocol_StartEx(IInternetProtocolEx *iface, IUri *pUri,
553         IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
554         DWORD grfPI, HANDLE *dwReserved)
555 {
556     HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
557     DWORD scheme = 0;
558     HRESULT hres;
559
560     TRACE("(%p)->(%p %p %p %08x %p)\n", This, pUri, pOIProtSink,
561             pOIBindInfo, grfPI, dwReserved);
562
563     hres = IUri_GetScheme(pUri, &scheme);
564     if(FAILED(hres))
565         return hres;
566     if(scheme != (This->https ? URL_SCHEME_HTTPS : URL_SCHEME_HTTP))
567         return MK_E_SYNTAX;
568
569     return protocol_start(&This->base, (IInternetProtocol*)&This->IInternetProtocolEx_iface, pUri,
570                           pOIProtSink, pOIBindInfo);
571 }
572
573 static const IInternetProtocolExVtbl HttpProtocolVtbl = {
574     HttpProtocol_QueryInterface,
575     HttpProtocol_AddRef,
576     HttpProtocol_Release,
577     HttpProtocol_Start,
578     HttpProtocol_Continue,
579     HttpProtocol_Abort,
580     HttpProtocol_Terminate,
581     HttpProtocol_Suspend,
582     HttpProtocol_Resume,
583     HttpProtocol_Read,
584     HttpProtocol_Seek,
585     HttpProtocol_LockRequest,
586     HttpProtocol_UnlockRequest,
587     HttpProtocol_StartEx
588 };
589
590 static HRESULT WINAPI HttpPriority_QueryInterface(IInternetPriority *iface, REFIID riid, void **ppv)
591 {
592     HttpProtocol *This = impl_from_IInternetPriority(iface);
593     return IInternetProtocolEx_QueryInterface(&This->IInternetProtocolEx_iface, riid, ppv);
594 }
595
596 static ULONG WINAPI HttpPriority_AddRef(IInternetPriority *iface)
597 {
598     HttpProtocol *This = impl_from_IInternetPriority(iface);
599     return IInternetProtocolEx_AddRef(&This->IInternetProtocolEx_iface);
600 }
601
602 static ULONG WINAPI HttpPriority_Release(IInternetPriority *iface)
603 {
604     HttpProtocol *This = impl_from_IInternetPriority(iface);
605     return IInternetProtocolEx_Release(&This->IInternetProtocolEx_iface);
606 }
607
608 static HRESULT WINAPI HttpPriority_SetPriority(IInternetPriority *iface, LONG nPriority)
609 {
610     HttpProtocol *This = impl_from_IInternetPriority(iface);
611
612     TRACE("(%p)->(%d)\n", This, nPriority);
613
614     This->base.priority = nPriority;
615     return S_OK;
616 }
617
618 static HRESULT WINAPI HttpPriority_GetPriority(IInternetPriority *iface, LONG *pnPriority)
619 {
620     HttpProtocol *This = impl_from_IInternetPriority(iface);
621
622     TRACE("(%p)->(%p)\n", This, pnPriority);
623
624     *pnPriority = This->base.priority;
625     return S_OK;
626 }
627
628 static const IInternetPriorityVtbl HttpPriorityVtbl = {
629     HttpPriority_QueryInterface,
630     HttpPriority_AddRef,
631     HttpPriority_Release,
632     HttpPriority_SetPriority,
633     HttpPriority_GetPriority
634 };
635
636 static HRESULT WINAPI HttpInfo_QueryInterface(IWinInetHttpInfo *iface, REFIID riid, void **ppv)
637 {
638     HttpProtocol *This = impl_from_IWinInetHttpInfo(iface);
639     return IInternetProtocolEx_QueryInterface(&This->IInternetProtocolEx_iface, riid, ppv);
640 }
641
642 static ULONG WINAPI HttpInfo_AddRef(IWinInetHttpInfo *iface)
643 {
644     HttpProtocol *This = impl_from_IWinInetHttpInfo(iface);
645     return IInternetProtocolEx_AddRef(&This->IInternetProtocolEx_iface);
646 }
647
648 static ULONG WINAPI HttpInfo_Release(IWinInetHttpInfo *iface)
649 {
650     HttpProtocol *This = impl_from_IWinInetHttpInfo(iface);
651     return IInternetProtocolEx_Release(&This->IInternetProtocolEx_iface);
652 }
653
654 static HRESULT WINAPI HttpInfo_QueryOption(IWinInetHttpInfo *iface, DWORD dwOption,
655         void *pBuffer, DWORD *pcbBuffer)
656 {
657     HttpProtocol *This = impl_from_IWinInetHttpInfo(iface);
658     FIXME("(%p)->(%x %p %p)\n", This, dwOption, pBuffer, pcbBuffer);
659     return E_NOTIMPL;
660 }
661
662 static HRESULT WINAPI HttpInfo_QueryInfo(IWinInetHttpInfo *iface, DWORD dwOption,
663         void *pBuffer, DWORD *pcbBuffer, DWORD *pdwFlags, DWORD *pdwReserved)
664 {
665     HttpProtocol *This = impl_from_IWinInetHttpInfo(iface);
666     FIXME("(%p)->(%x %p %p %p %p)\n", This, dwOption, pBuffer, pcbBuffer, pdwFlags, pdwReserved);
667     return E_NOTIMPL;
668 }
669
670 static const IWinInetHttpInfoVtbl WinInetHttpInfoVtbl = {
671     HttpInfo_QueryInterface,
672     HttpInfo_AddRef,
673     HttpInfo_Release,
674     HttpInfo_QueryOption,
675     HttpInfo_QueryInfo
676 };
677
678 static HRESULT create_http_protocol(BOOL https, void **ppobj)
679 {
680     HttpProtocol *ret;
681
682     ret = heap_alloc_zero(sizeof(HttpProtocol));
683     if(!ret)
684         return E_OUTOFMEMORY;
685
686     ret->base.vtbl = &AsyncProtocolVtbl;
687     ret->IInternetProtocolEx_iface.lpVtbl = &HttpProtocolVtbl;
688     ret->IInternetPriority_iface.lpVtbl   = &HttpPriorityVtbl;
689     ret->IWinInetHttpInfo_iface.lpVtbl    = &WinInetHttpInfoVtbl;
690
691     ret->https = https;
692     ret->ref = 1;
693
694     *ppobj = &ret->IInternetProtocolEx_iface;
695
696     URLMON_LockModule();
697     return S_OK;
698 }
699
700 HRESULT HttpProtocol_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
701 {
702     TRACE("(%p %p)\n", pUnkOuter, ppobj);
703
704     return create_http_protocol(FALSE, ppobj);
705 }
706
707 HRESULT HttpSProtocol_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
708 {
709     TRACE("(%p %p)\n", pUnkOuter, ppobj);
710
711     return create_http_protocol(TRUE, ppobj);
712 }