msxml3: Store output stream for processor.
[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 static inline BOOL set_security_flag(HttpProtocol *This, DWORD new_flag)
84 {
85     DWORD flags, size = sizeof(flags);
86     BOOL res;
87
88     res = InternetQueryOptionW(This->base.request, INTERNET_OPTION_SECURITY_FLAGS, &flags, &size);
89     if(res) {
90         flags |= new_flag;
91         res = InternetSetOptionW(This->base.request, INTERNET_OPTION_SECURITY_FLAGS, &flags, size);
92     }
93     if(!res)
94         ERR("Failed to set security flag(s): %x\n", new_flag);
95
96     return res;
97 }
98
99 static inline HRESULT internet_error_to_hres(DWORD error)
100 {
101     switch(error)
102     {
103     case ERROR_INTERNET_SEC_CERT_DATE_INVALID:
104     case ERROR_INTERNET_SEC_CERT_CN_INVALID:
105     case ERROR_INTERNET_INVALID_CA:
106     case ERROR_INTERNET_CLIENT_AUTH_CERT_NEEDED:
107         return INET_E_INVALID_CERTIFICATE;
108     case ERROR_INTERNET_HTTP_TO_HTTPS_ON_REDIR:
109     case ERROR_INTERNET_HTTPS_TO_HTTP_ON_REDIR:
110     case ERROR_HTTP_REDIRECT_NEEDS_CONFIRMATION:
111         return INET_E_REDIRECT_FAILED;
112     default:
113         return INET_E_DOWNLOAD_FAILURE;
114     }
115 }
116
117 static HRESULT handle_http_error(HttpProtocol *This, DWORD error)
118 {
119     IServiceProvider *serv_prov;
120     IWindowForBindingUI *wfb_ui;
121     IHttpSecurity *http_security;
122     BOOL security_problem;
123     HRESULT hres;
124
125     switch(error) {
126     case ERROR_INTERNET_SEC_CERT_DATE_INVALID:
127     case ERROR_INTERNET_SEC_CERT_CN_INVALID:
128     case ERROR_INTERNET_HTTP_TO_HTTPS_ON_REDIR:
129     case ERROR_INTERNET_HTTPS_TO_HTTP_ON_REDIR:
130     case ERROR_HTTP_REDIRECT_NEEDS_CONFIRMATION:
131     case ERROR_INTERNET_INVALID_CA:
132     case ERROR_INTERNET_CLIENT_AUTH_CERT_NEEDED:
133         security_problem = TRUE;
134         break;
135     default:
136         security_problem = FALSE;
137     }
138
139     hres = IInternetProtocolSink_QueryInterface(This->base.protocol_sink, &IID_IServiceProvider,
140                                                 (void**)&serv_prov);
141     if(FAILED(hres)) {
142         ERR("Failed to get IServiceProvider.\n");
143         return E_ABORT;
144     }
145
146     if(security_problem) {
147         hres = IServiceProvider_QueryService(serv_prov, &IID_IHttpSecurity, &IID_IHttpSecurity,
148                                              (void**)&http_security);
149         if(SUCCEEDED(hres)) {
150             hres = IHttpSecurity_OnSecurityProblem(http_security, error);
151             IHttpSecurity_Release(http_security);
152
153             if(hres != S_FALSE)
154             {
155                 BOOL res = FALSE;
156
157                 IServiceProvider_Release(serv_prov);
158
159                 if(hres == S_OK) {
160                     if(error == ERROR_INTERNET_SEC_CERT_DATE_INVALID)
161                         res = set_security_flag(This, SECURITY_FLAG_IGNORE_CERT_DATE_INVALID);
162                     else if(error == ERROR_INTERNET_SEC_CERT_CN_INVALID)
163                         res = set_security_flag(This, SECURITY_FLAG_IGNORE_CERT_CN_INVALID);
164                     else if(error == ERROR_INTERNET_INVALID_CA)
165                         res = set_security_flag(This, SECURITY_FLAG_IGNORE_UNKNOWN_CA);
166
167                     if(res)
168                         return RPC_E_RETRY;
169
170                     FIXME("Don't know how to ignore error %d\n", error);
171                     return E_ABORT;
172                 }
173
174                 if(hres == E_ABORT)
175                     return E_ABORT;
176                 if(hres == RPC_E_RETRY)
177                     return RPC_E_RETRY;
178
179                 return internet_error_to_hres(error);
180             }
181         }
182     }
183
184     hres = IServiceProvider_QueryService(serv_prov, &IID_IWindowForBindingUI, &IID_IWindowForBindingUI,
185                                          (void**)&wfb_ui);
186     if(SUCCEEDED(hres)) {
187         HWND hwnd;
188         const IID *iid_reason;
189
190         if(security_problem)
191             iid_reason = &IID_IHttpSecurity;
192         else if(error == ERROR_INTERNET_INCORRECT_PASSWORD)
193             iid_reason = &IID_IAuthenticate;
194         else
195             iid_reason = &IID_IWindowForBindingUI;
196
197         hres = IWindowForBindingUI_GetWindow(wfb_ui, iid_reason, &hwnd);
198         if(SUCCEEDED(hres) && hwnd)
199         {
200             DWORD res;
201
202             res = InternetErrorDlg(hwnd, This->base.request, error,
203                                    FLAGS_ERROR_UI_FLAGS_CHANGE_OPTIONS | FLAGS_ERROR_UI_FLAGS_GENERATE_DATA,
204                                    NULL);
205
206             if(res == ERROR_INTERNET_FORCE_RETRY || res == ERROR_SUCCESS)
207                 hres = RPC_E_RETRY;
208             else
209                 hres = E_FAIL;
210         }
211         IWindowForBindingUI_Release(wfb_ui);
212     }
213
214     IServiceProvider_Release(serv_prov);
215
216     if(hres == RPC_E_RETRY)
217         return hres;
218
219     return internet_error_to_hres(error);
220 }
221
222 static ULONG send_http_request(HttpProtocol *This)
223 {
224     INTERNET_BUFFERSW send_buffer = {sizeof(INTERNET_BUFFERSW)};
225     BOOL res;
226
227     send_buffer.lpcszHeader = This->full_header;
228     send_buffer.dwHeadersLength = send_buffer.dwHeadersTotal = strlenW(This->full_header);
229
230     if(This->base.bind_info.dwBindVerb != BINDVERB_GET) {
231         switch(This->base.bind_info.stgmedData.tymed) {
232         case TYMED_HGLOBAL:
233             /* Native does not use GlobalLock/GlobalUnlock, so we won't either */
234             send_buffer.lpvBuffer = This->base.bind_info.stgmedData.u.hGlobal;
235             send_buffer.dwBufferLength = send_buffer.dwBufferTotal = This->base.bind_info.cbstgmedData;
236             break;
237         case TYMED_ISTREAM: {
238             LARGE_INTEGER offset;
239
240             send_buffer.dwBufferTotal = This->base.bind_info.cbstgmedData;
241             if(!This->base.post_stream) {
242                 This->base.post_stream = This->base.bind_info.stgmedData.u.pstm;
243                 IStream_AddRef(This->base.post_stream);
244             }
245
246             offset.QuadPart = 0;
247             IStream_Seek(This->base.post_stream, offset, STREAM_SEEK_SET, NULL);
248             break;
249         }
250         default:
251             FIXME("Unsupported This->base.bind_info.stgmedData.tymed %d\n", This->base.bind_info.stgmedData.tymed);
252         }
253     }
254
255     if(This->base.post_stream)
256         res = HttpSendRequestExW(This->base.request, &send_buffer, NULL, 0, 0);
257     else
258         res = HttpSendRequestW(This->base.request, send_buffer.lpcszHeader, send_buffer.dwHeadersLength,
259                 send_buffer.lpvBuffer, send_buffer.dwBufferLength);
260
261     return res ? 0 : GetLastError();
262 }
263
264 static inline HttpProtocol *impl_from_Protocol(Protocol *prot)
265 {
266     return CONTAINING_RECORD(prot, HttpProtocol, base);
267 }
268
269 static HRESULT HttpProtocol_open_request(Protocol *prot, IUri *uri, DWORD request_flags,
270         HINTERNET internet_session, IInternetBindInfo *bind_info)
271 {
272     HttpProtocol *This = impl_from_Protocol(prot);
273     LPWSTR addl_header = NULL, post_cookie = NULL;
274     IServiceProvider *service_provider = NULL;
275     IHttpNegotiate2 *http_negotiate2 = NULL;
276     BSTR url, host, user, pass, path;
277     LPOLESTR accept_mimes[257];
278     const WCHAR **accept_types;
279     BYTE security_id[512];
280     DWORD len = 0, port;
281     ULONG num, error;
282     BOOL res, b;
283     HRESULT hres;
284
285     static const WCHAR wszBindVerb[BINDVERB_CUSTOM][5] =
286         {{'G','E','T',0},
287          {'P','O','S','T',0},
288          {'P','U','T',0}};
289
290     hres = IUri_GetPort(uri, &port);
291     if(FAILED(hres))
292         return hres;
293
294     hres = IUri_GetHost(uri, &host);
295     if(FAILED(hres))
296         return hres;
297
298     hres = IUri_GetUserName(uri, &user);
299     if(SUCCEEDED(hres)) {
300         hres = IUri_GetPassword(uri, &pass);
301
302         if(SUCCEEDED(hres)) {
303             This->base.connection = InternetConnectW(internet_session, host, port, user, pass,
304                     INTERNET_SERVICE_HTTP, This->https ? INTERNET_FLAG_SECURE : 0, (DWORD_PTR)&This->base);
305             SysFreeString(pass);
306         }
307         SysFreeString(user);
308     }
309     SysFreeString(host);
310     if(FAILED(hres))
311         return hres;
312     if(!This->base.connection) {
313         WARN("InternetConnect failed: %d\n", GetLastError());
314         return INET_E_CANNOT_CONNECT;
315     }
316
317     num = sizeof(accept_mimes)/sizeof(accept_mimes[0])-1;
318     hres = IInternetBindInfo_GetBindString(bind_info, BINDSTRING_ACCEPT_MIMES, accept_mimes, num, &num);
319     if(hres == INET_E_USE_DEFAULT_SETTING) {
320         static const WCHAR default_accept_mimeW[] = {'*','/','*',0};
321         static const WCHAR *default_accept_mimes[] = {default_accept_mimeW, NULL};
322
323         accept_types = default_accept_mimes;
324         num = 0;
325     }else if(hres == S_OK) {
326         accept_types = (const WCHAR**)accept_mimes;
327     }else {
328         WARN("GetBindString BINDSTRING_ACCEPT_MIMES failed: %08x\n", hres);
329         return INET_E_NO_VALID_MEDIA;
330     }
331     accept_mimes[num] = 0;
332
333     if(This->https)
334         request_flags |= INTERNET_FLAG_SECURE;
335
336     hres = IUri_GetPathAndQuery(uri, &path);
337     if(SUCCEEDED(hres)) {
338         This->base.request = HttpOpenRequestW(This->base.connection,
339                 This->base.bind_info.dwBindVerb < BINDVERB_CUSTOM
340                     ? wszBindVerb[This->base.bind_info.dwBindVerb] : This->base.bind_info.szCustomVerb,
341                 path, NULL, NULL, accept_types, request_flags, (DWORD_PTR)&This->base);
342         SysFreeString(path);
343     }
344     while(num--)
345         CoTaskMemFree(accept_mimes[num]);
346     if(FAILED(hres))
347         return hres;
348     if (!This->base.request) {
349         WARN("HttpOpenRequest failed: %d\n", GetLastError());
350         return INET_E_RESOURCE_NOT_FOUND;
351     }
352
353     hres = IInternetProtocolSink_QueryInterface(This->base.protocol_sink, &IID_IServiceProvider,
354             (void **)&service_provider);
355     if (hres != S_OK) {
356         WARN("IInternetProtocolSink_QueryInterface IID_IServiceProvider failed: %08x\n", hres);
357         return hres;
358     }
359
360     hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate,
361             &IID_IHttpNegotiate, (void **)&This->http_negotiate);
362     if (hres != S_OK) {
363         WARN("IServiceProvider_QueryService IID_IHttpNegotiate failed: %08x\n", hres);
364         IServiceProvider_Release(service_provider);
365         return hres;
366     }
367
368     hres = IUri_GetAbsoluteUri(uri, &url);
369     if(FAILED(hres)) {
370         IServiceProvider_Release(service_provider);
371         return hres;
372     }
373
374     hres = IHttpNegotiate_BeginningTransaction(This->http_negotiate, url, wszHeaders,
375             0, &addl_header);
376     SysFreeString(url);
377     if(hres != S_OK) {
378         WARN("IHttpNegotiate_BeginningTransaction failed: %08x\n", hres);
379         IServiceProvider_Release(service_provider);
380         return hres;
381     }
382
383     if(addl_header) {
384         int len_addl_header = strlenW(addl_header);
385
386         This->full_header = heap_alloc(len_addl_header*sizeof(WCHAR)+sizeof(wszHeaders));
387
388         lstrcpyW(This->full_header, addl_header);
389         lstrcpyW(&This->full_header[len_addl_header], wszHeaders);
390         CoTaskMemFree(addl_header);
391     }else {
392         This->full_header = (LPWSTR)wszHeaders;
393     }
394
395     hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate2,
396             &IID_IHttpNegotiate2, (void **)&http_negotiate2);
397     IServiceProvider_Release(service_provider);
398     if(hres != S_OK) {
399         WARN("IServiceProvider_QueryService IID_IHttpNegotiate2 failed: %08x\n", hres);
400         /* No goto done as per native */
401     }else {
402         len = sizeof(security_id)/sizeof(security_id[0]);
403         hres = IHttpNegotiate2_GetRootSecurityId(http_negotiate2, security_id, &len, 0);
404         IHttpNegotiate2_Release(http_negotiate2);
405         if (hres != S_OK)
406             WARN("IHttpNegotiate2_GetRootSecurityId failed: %08x\n", hres);
407     }
408
409     /* FIXME: Handle security_id. Native calls undocumented function IsHostInProxyBypassList. */
410
411     if(This->base.bind_info.dwBindVerb == BINDVERB_POST) {
412         num = 0;
413         hres = IInternetBindInfo_GetBindString(bind_info, BINDSTRING_POST_COOKIE, &post_cookie, 1, &num);
414         if(hres == S_OK && num) {
415             if(!InternetSetOptionW(This->base.request, INTERNET_OPTION_SECONDARY_CACHE_KEY,
416                                    post_cookie, lstrlenW(post_cookie)))
417                 WARN("InternetSetOption INTERNET_OPTION_SECONDARY_CACHE_KEY failed: %d\n", GetLastError());
418             CoTaskMemFree(post_cookie);
419         }
420     }
421
422     b = TRUE;
423     res = InternetSetOptionW(This->base.request, INTERNET_OPTION_HTTP_DECODING, &b, sizeof(b));
424     if(!res)
425         WARN("InternetSetOption(INTERNET_OPTION_HTTP_DECODING) failed: %08x\n", GetLastError());
426
427     do {
428         error = send_http_request(This);
429
430         if(error == ERROR_IO_PENDING || error == ERROR_SUCCESS)
431             return S_OK;
432
433         hres = handle_http_error(This, error);
434
435     } while(hres == RPC_E_RETRY);
436
437     WARN("HttpSendRequest failed: %d\n", error);
438     return hres;
439 }
440
441 static HRESULT HttpProtocol_end_request(Protocol *protocol)
442 {
443     BOOL res;
444
445     res = HttpEndRequestW(protocol->request, NULL, 0, 0);
446     if(!res && GetLastError() != ERROR_IO_PENDING) {
447         FIXME("HttpEndRequest failed: %u\n", GetLastError());
448         return E_FAIL;
449     }
450
451     return S_OK;
452 }
453
454 static HRESULT HttpProtocol_start_downloading(Protocol *prot)
455 {
456     HttpProtocol *This = impl_from_Protocol(prot);
457     LPWSTR content_type, content_length, ranges;
458     DWORD len = sizeof(DWORD);
459     DWORD status_code;
460     BOOL res;
461     HRESULT hres;
462
463     static const WCHAR wszDefaultContentType[] =
464         {'t','e','x','t','/','h','t','m','l',0};
465
466     if(!This->http_negotiate) {
467         WARN("Expected IHttpNegotiate pointer to be non-NULL\n");
468         return S_OK;
469     }
470
471     res = HttpQueryInfoW(This->base.request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER,
472             &status_code, &len, NULL);
473     if(res) {
474         LPWSTR response_headers = query_http_info(This, HTTP_QUERY_RAW_HEADERS_CRLF);
475         if(response_headers) {
476             hres = IHttpNegotiate_OnResponse(This->http_negotiate, status_code, response_headers,
477                     NULL, NULL);
478             heap_free(response_headers);
479             if (hres != S_OK) {
480                 WARN("IHttpNegotiate_OnResponse failed: %08x\n", hres);
481                 return S_OK;
482             }
483         }
484     }else {
485         WARN("HttpQueryInfo failed: %d\n", GetLastError());
486     }
487
488     ranges = query_http_info(This, HTTP_QUERY_ACCEPT_RANGES);
489     if(ranges) {
490         IInternetProtocolSink_ReportProgress(This->base.protocol_sink, BINDSTATUS_ACCEPTRANGES, NULL);
491         heap_free(ranges);
492     }
493
494     content_type = query_http_info(This, HTTP_QUERY_CONTENT_TYPE);
495     if(content_type) {
496         /* remove the charset, if present */
497         LPWSTR p = strchrW(content_type, ';');
498         if (p) *p = '\0';
499
500         IInternetProtocolSink_ReportProgress(This->base.protocol_sink,
501                 (This->base.bindf & BINDF_FROMURLMON)
502                  ? BINDSTATUS_MIMETYPEAVAILABLE : BINDSTATUS_RAWMIMETYPE,
503                  content_type);
504         heap_free(content_type);
505     }else {
506         WARN("HttpQueryInfo failed: %d\n", GetLastError());
507         IInternetProtocolSink_ReportProgress(This->base.protocol_sink,
508                  (This->base.bindf & BINDF_FROMURLMON)
509                   ? BINDSTATUS_MIMETYPEAVAILABLE : BINDSTATUS_RAWMIMETYPE,
510                   wszDefaultContentType);
511     }
512
513     content_length = query_http_info(This, HTTP_QUERY_CONTENT_LENGTH);
514     if(content_length) {
515         This->base.content_length = atoiW(content_length);
516         heap_free(content_length);
517     }
518
519     return S_OK;
520 }
521
522 static void HttpProtocol_close_connection(Protocol *prot)
523 {
524     HttpProtocol *This = impl_from_Protocol(prot);
525
526     if(This->http_negotiate) {
527         IHttpNegotiate_Release(This->http_negotiate);
528         This->http_negotiate = 0;
529     }
530
531     if(This->full_header) {
532         if(This->full_header != wszHeaders)
533             heap_free(This->full_header);
534         This->full_header = 0;
535     }
536 }
537
538 static void HttpProtocol_on_error(Protocol *prot, DWORD error)
539 {
540     HttpProtocol *This = impl_from_Protocol(prot);
541     HRESULT hres;
542
543     TRACE("(%p) %d\n", prot, error);
544
545     if(prot->flags & FLAG_FIRST_CONTINUE_COMPLETE) {
546         FIXME("Not handling error %d\n", error);
547         return;
548     }
549
550     while((hres = handle_http_error(This, error)) == RPC_E_RETRY) {
551         error = send_http_request(This);
552
553         if(error == ERROR_IO_PENDING || error == ERROR_SUCCESS)
554             return;
555     }
556
557     protocol_abort(prot, hres);
558     protocol_close_connection(prot);
559     return;
560 }
561
562 static const ProtocolVtbl AsyncProtocolVtbl = {
563     HttpProtocol_open_request,
564     HttpProtocol_end_request,
565     HttpProtocol_start_downloading,
566     HttpProtocol_close_connection,
567     HttpProtocol_on_error
568 };
569
570 static HRESULT WINAPI HttpProtocol_QueryInterface(IInternetProtocolEx *iface, REFIID riid, void **ppv)
571 {
572     HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
573
574     *ppv = NULL;
575     if(IsEqualGUID(&IID_IUnknown, riid)) {
576         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
577         *ppv = &This->IInternetProtocolEx_iface;
578     }else if(IsEqualGUID(&IID_IInternetProtocolRoot, riid)) {
579         TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", This, ppv);
580         *ppv = &This->IInternetProtocolEx_iface;
581     }else if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
582         TRACE("(%p)->(IID_IInternetProtocol %p)\n", This, ppv);
583         *ppv = &This->IInternetProtocolEx_iface;
584     }else if(IsEqualGUID(&IID_IInternetProtocolEx, riid)) {
585         TRACE("(%p)->(IID_IInternetProtocolEx %p)\n", This, ppv);
586         *ppv = &This->IInternetProtocolEx_iface;
587     }else if(IsEqualGUID(&IID_IInternetPriority, riid)) {
588         TRACE("(%p)->(IID_IInternetPriority %p)\n", This, ppv);
589         *ppv = &This->IInternetPriority_iface;
590     }else if(IsEqualGUID(&IID_IWinInetInfo, riid)) {
591         TRACE("(%p)->(IID_IWinInetInfo %p)\n", This, ppv);
592         *ppv = &This->IWinInetHttpInfo_iface;
593     }else if(IsEqualGUID(&IID_IWinInetHttpInfo, riid)) {
594         TRACE("(%p)->(IID_IWinInetHttpInfo %p)\n", This, ppv);
595         *ppv = &This->IWinInetHttpInfo_iface;
596     }
597
598     if(*ppv) {
599         IInternetProtocol_AddRef(iface);
600         return S_OK;
601     }
602
603     WARN("not supported interface %s\n", debugstr_guid(riid));
604     return E_NOINTERFACE;
605 }
606
607 static ULONG WINAPI HttpProtocol_AddRef(IInternetProtocolEx *iface)
608 {
609     HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
610     LONG ref = InterlockedIncrement(&This->ref);
611     TRACE("(%p) ref=%d\n", This, ref);
612     return ref;
613 }
614
615 static ULONG WINAPI HttpProtocol_Release(IInternetProtocolEx *iface)
616 {
617     HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
618     LONG ref = InterlockedDecrement(&This->ref);
619
620     TRACE("(%p) ref=%d\n", This, ref);
621
622     if(!ref) {
623         protocol_close_connection(&This->base);
624         heap_free(This);
625
626         URLMON_UnlockModule();
627     }
628
629     return ref;
630 }
631
632 static HRESULT WINAPI HttpProtocol_Start(IInternetProtocolEx *iface, LPCWSTR szUrl,
633         IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
634         DWORD grfPI, HANDLE_PTR dwReserved)
635 {
636     HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
637     IUri *uri;
638     HRESULT hres;
639
640     TRACE("(%p)->(%s %p %p %08x %lx)\n", This, debugstr_w(szUrl), pOIProtSink,
641             pOIBindInfo, grfPI, dwReserved);
642
643     hres = CreateUri(szUrl, 0, 0, &uri);
644     if(FAILED(hres))
645         return hres;
646
647     hres = IInternetProtocolEx_StartEx(&This->IInternetProtocolEx_iface, uri, pOIProtSink,
648             pOIBindInfo, grfPI, (HANDLE*)dwReserved);
649
650     IUri_Release(uri);
651     return hres;
652 }
653
654 static HRESULT WINAPI HttpProtocol_Continue(IInternetProtocolEx *iface, PROTOCOLDATA *pProtocolData)
655 {
656     HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
657
658     TRACE("(%p)->(%p)\n", This, pProtocolData);
659
660     return protocol_continue(&This->base, pProtocolData);
661 }
662
663 static HRESULT WINAPI HttpProtocol_Abort(IInternetProtocolEx *iface, HRESULT hrReason,
664         DWORD dwOptions)
665 {
666     HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
667
668     TRACE("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);
669
670     return protocol_abort(&This->base, hrReason);
671 }
672
673 static HRESULT WINAPI HttpProtocol_Terminate(IInternetProtocolEx *iface, DWORD dwOptions)
674 {
675     HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
676
677     TRACE("(%p)->(%08x)\n", This, dwOptions);
678
679     protocol_close_connection(&This->base);
680     return S_OK;
681 }
682
683 static HRESULT WINAPI HttpProtocol_Suspend(IInternetProtocolEx *iface)
684 {
685     HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
686     FIXME("(%p)\n", This);
687     return E_NOTIMPL;
688 }
689
690 static HRESULT WINAPI HttpProtocol_Resume(IInternetProtocolEx *iface)
691 {
692     HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
693     FIXME("(%p)\n", This);
694     return E_NOTIMPL;
695 }
696
697 static HRESULT WINAPI HttpProtocol_Read(IInternetProtocolEx *iface, void *pv,
698         ULONG cb, ULONG *pcbRead)
699 {
700     HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
701
702     TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
703
704     return protocol_read(&This->base, pv, cb, pcbRead);
705 }
706
707 static HRESULT WINAPI HttpProtocol_Seek(IInternetProtocolEx *iface, LARGE_INTEGER dlibMove,
708         DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
709 {
710     HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
711     FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
712     return E_NOTIMPL;
713 }
714
715 static HRESULT WINAPI HttpProtocol_LockRequest(IInternetProtocolEx *iface, DWORD dwOptions)
716 {
717     HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
718
719     TRACE("(%p)->(%08x)\n", This, dwOptions);
720
721     return protocol_lock_request(&This->base);
722 }
723
724 static HRESULT WINAPI HttpProtocol_UnlockRequest(IInternetProtocolEx *iface)
725 {
726     HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
727
728     TRACE("(%p)\n", This);
729
730     return protocol_unlock_request(&This->base);
731 }
732
733 static HRESULT WINAPI HttpProtocol_StartEx(IInternetProtocolEx *iface, IUri *pUri,
734         IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
735         DWORD grfPI, HANDLE *dwReserved)
736 {
737     HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
738     DWORD scheme = 0;
739     HRESULT hres;
740
741     TRACE("(%p)->(%p %p %p %08x %p)\n", This, pUri, pOIProtSink,
742             pOIBindInfo, grfPI, dwReserved);
743
744     hres = IUri_GetScheme(pUri, &scheme);
745     if(FAILED(hres))
746         return hres;
747     if(scheme != (This->https ? URL_SCHEME_HTTPS : URL_SCHEME_HTTP))
748         return MK_E_SYNTAX;
749
750     return protocol_start(&This->base, (IInternetProtocol*)&This->IInternetProtocolEx_iface, pUri,
751                           pOIProtSink, pOIBindInfo);
752 }
753
754 static const IInternetProtocolExVtbl HttpProtocolVtbl = {
755     HttpProtocol_QueryInterface,
756     HttpProtocol_AddRef,
757     HttpProtocol_Release,
758     HttpProtocol_Start,
759     HttpProtocol_Continue,
760     HttpProtocol_Abort,
761     HttpProtocol_Terminate,
762     HttpProtocol_Suspend,
763     HttpProtocol_Resume,
764     HttpProtocol_Read,
765     HttpProtocol_Seek,
766     HttpProtocol_LockRequest,
767     HttpProtocol_UnlockRequest,
768     HttpProtocol_StartEx
769 };
770
771 static HRESULT WINAPI HttpPriority_QueryInterface(IInternetPriority *iface, REFIID riid, void **ppv)
772 {
773     HttpProtocol *This = impl_from_IInternetPriority(iface);
774     return IInternetProtocolEx_QueryInterface(&This->IInternetProtocolEx_iface, riid, ppv);
775 }
776
777 static ULONG WINAPI HttpPriority_AddRef(IInternetPriority *iface)
778 {
779     HttpProtocol *This = impl_from_IInternetPriority(iface);
780     return IInternetProtocolEx_AddRef(&This->IInternetProtocolEx_iface);
781 }
782
783 static ULONG WINAPI HttpPriority_Release(IInternetPriority *iface)
784 {
785     HttpProtocol *This = impl_from_IInternetPriority(iface);
786     return IInternetProtocolEx_Release(&This->IInternetProtocolEx_iface);
787 }
788
789 static HRESULT WINAPI HttpPriority_SetPriority(IInternetPriority *iface, LONG nPriority)
790 {
791     HttpProtocol *This = impl_from_IInternetPriority(iface);
792
793     TRACE("(%p)->(%d)\n", This, nPriority);
794
795     This->base.priority = nPriority;
796     return S_OK;
797 }
798
799 static HRESULT WINAPI HttpPriority_GetPriority(IInternetPriority *iface, LONG *pnPriority)
800 {
801     HttpProtocol *This = impl_from_IInternetPriority(iface);
802
803     TRACE("(%p)->(%p)\n", This, pnPriority);
804
805     *pnPriority = This->base.priority;
806     return S_OK;
807 }
808
809 static const IInternetPriorityVtbl HttpPriorityVtbl = {
810     HttpPriority_QueryInterface,
811     HttpPriority_AddRef,
812     HttpPriority_Release,
813     HttpPriority_SetPriority,
814     HttpPriority_GetPriority
815 };
816
817 static HRESULT WINAPI HttpInfo_QueryInterface(IWinInetHttpInfo *iface, REFIID riid, void **ppv)
818 {
819     HttpProtocol *This = impl_from_IWinInetHttpInfo(iface);
820     return IInternetProtocolEx_QueryInterface(&This->IInternetProtocolEx_iface, riid, ppv);
821 }
822
823 static ULONG WINAPI HttpInfo_AddRef(IWinInetHttpInfo *iface)
824 {
825     HttpProtocol *This = impl_from_IWinInetHttpInfo(iface);
826     return IInternetProtocolEx_AddRef(&This->IInternetProtocolEx_iface);
827 }
828
829 static ULONG WINAPI HttpInfo_Release(IWinInetHttpInfo *iface)
830 {
831     HttpProtocol *This = impl_from_IWinInetHttpInfo(iface);
832     return IInternetProtocolEx_Release(&This->IInternetProtocolEx_iface);
833 }
834
835 static HRESULT WINAPI HttpInfo_QueryOption(IWinInetHttpInfo *iface, DWORD dwOption,
836         void *pBuffer, DWORD *pcbBuffer)
837 {
838     HttpProtocol *This = impl_from_IWinInetHttpInfo(iface);
839     FIXME("(%p)->(%x %p %p)\n", This, dwOption, pBuffer, pcbBuffer);
840     return E_NOTIMPL;
841 }
842
843 static HRESULT WINAPI HttpInfo_QueryInfo(IWinInetHttpInfo *iface, DWORD dwOption,
844         void *pBuffer, DWORD *pcbBuffer, DWORD *pdwFlags, DWORD *pdwReserved)
845 {
846     HttpProtocol *This = impl_from_IWinInetHttpInfo(iface);
847     FIXME("(%p)->(%x %p %p %p %p)\n", This, dwOption, pBuffer, pcbBuffer, pdwFlags, pdwReserved);
848     return E_NOTIMPL;
849 }
850
851 static const IWinInetHttpInfoVtbl WinInetHttpInfoVtbl = {
852     HttpInfo_QueryInterface,
853     HttpInfo_AddRef,
854     HttpInfo_Release,
855     HttpInfo_QueryOption,
856     HttpInfo_QueryInfo
857 };
858
859 static HRESULT create_http_protocol(BOOL https, void **ppobj)
860 {
861     HttpProtocol *ret;
862
863     ret = heap_alloc_zero(sizeof(HttpProtocol));
864     if(!ret)
865         return E_OUTOFMEMORY;
866
867     ret->base.vtbl = &AsyncProtocolVtbl;
868     ret->IInternetProtocolEx_iface.lpVtbl = &HttpProtocolVtbl;
869     ret->IInternetPriority_iface.lpVtbl   = &HttpPriorityVtbl;
870     ret->IWinInetHttpInfo_iface.lpVtbl    = &WinInetHttpInfoVtbl;
871
872     ret->https = https;
873     ret->ref = 1;
874
875     *ppobj = &ret->IInternetProtocolEx_iface;
876
877     URLMON_LockModule();
878     return S_OK;
879 }
880
881 HRESULT HttpProtocol_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
882 {
883     TRACE("(%p %p)\n", pUnkOuter, ppobj);
884
885     return create_http_protocol(FALSE, ppobj);
886 }
887
888 HRESULT HttpSProtocol_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
889 {
890     TRACE("(%p %p)\n", pUnkOuter, ppobj);
891
892     return create_http_protocol(TRUE, ppobj);
893 }