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