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