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