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