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