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