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