msvcp60: Manually define virtual destructors in vtables.
[wine/multimedia.git] / dlls / urlmon / http.c
blob18a99ae3a2860fd182b2970f04d345bf2a0723ef
1 /*
2 * Copyright 2005 Jacek Caban
3 * Copyright 2007 Misha Koshelev
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.
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.
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
20 #include "urlmon_main.h"
21 #include "wininet.h"
23 #define NO_SHLWAPI_REG
24 #include "shlwapi.h"
26 #include "wine/debug.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
30 typedef struct {
31 Protocol base;
33 IInternetProtocolEx IInternetProtocolEx_iface;
34 IInternetPriority IInternetPriority_iface;
35 IWinInetHttpInfo IWinInetHttpInfo_iface;
37 BOOL https;
38 IHttpNegotiate *http_negotiate;
39 WCHAR *full_header;
41 LONG ref;
42 } HttpProtocol;
44 static inline HttpProtocol *impl_from_IInternetProtocolEx(IInternetProtocolEx *iface)
46 return CONTAINING_RECORD(iface, HttpProtocol, IInternetProtocolEx_iface);
49 static inline HttpProtocol *impl_from_IInternetPriority(IInternetPriority *iface)
51 return CONTAINING_RECORD(iface, HttpProtocol, IInternetPriority_iface);
54 static inline HttpProtocol *impl_from_IWinInetHttpInfo(IWinInetHttpInfo *iface)
56 return CONTAINING_RECORD(iface, HttpProtocol, IWinInetHttpInfo_iface);
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};
62 static LPWSTR query_http_info(HttpProtocol *This, DWORD option)
64 LPWSTR ret = NULL;
65 DWORD len = 0;
66 BOOL res;
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);
73 if(!res) {
74 TRACE("HttpQueryInfoW(%d) failed: %08x\n", option, GetLastError());
75 heap_free(ret);
76 return NULL;
79 return ret;
82 static inline BOOL set_security_flag(HttpProtocol *This, DWORD flags)
84 BOOL res;
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);
90 return res;
93 static inline HRESULT internet_error_to_hres(DWORD error)
95 switch(error)
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;
116 static HRESULT handle_http_error(HttpProtocol *This, DWORD error)
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;
127 TRACE("(%p %u)\n", This, error);
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;
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;
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);
162 TRACE("OnSecurityProblem returned %08x\n", hres);
164 if(hres != S_FALSE)
166 BOOL res = FALSE;
168 IServiceProvider_Release(serv_prov);
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);
178 if(res)
179 return RPC_E_RETRY;
181 FIXME("Don't know how to ignore error %d\n", error);
182 return E_ABORT;
185 if(hres == E_ABORT)
186 return E_ABORT;
187 if(hres == RPC_E_RETRY)
188 return RPC_E_RETRY;
190 return internet_error_to_hres(error);
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;
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;
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;
216 hres = IWindowForBindingUI_GetWindow(wfb_ui, iid_reason, &hwnd);
217 IWindowForBindingUI_Release(wfb_ui);
218 if(FAILED(hres))
219 hwnd = NULL;
223 dlg_flags = FLAGS_ERROR_UI_FLAGS_CHANGE_OPTIONS | FLAGS_ERROR_UI_FLAGS_GENERATE_DATA;
224 if(This->base.bindf & BINDF_NO_UI)
225 dlg_flags |= FLAGS_ERROR_UI_FLAGS_NO_UI;
227 res = InternetErrorDlg(hwnd, This->base.request, error, dlg_flags, NULL);
228 hres = res == ERROR_INTERNET_FORCE_RETRY || res == ERROR_SUCCESS ? RPC_E_RETRY : internet_error_to_hres(error);
231 IServiceProvider_Release(serv_prov);
232 return hres;
235 static ULONG send_http_request(HttpProtocol *This)
237 INTERNET_BUFFERSW send_buffer = {sizeof(INTERNET_BUFFERSW)};
238 BOOL res;
240 send_buffer.lpcszHeader = This->full_header;
241 send_buffer.dwHeadersLength = send_buffer.dwHeadersTotal = strlenW(This->full_header);
243 if(This->base.bind_info.dwBindVerb != BINDVERB_GET) {
244 switch(This->base.bind_info.stgmedData.tymed) {
245 case TYMED_HGLOBAL:
246 /* Native does not use GlobalLock/GlobalUnlock, so we won't either */
247 send_buffer.lpvBuffer = This->base.bind_info.stgmedData.u.hGlobal;
248 send_buffer.dwBufferLength = send_buffer.dwBufferTotal = This->base.bind_info.cbstgmedData;
249 break;
250 case TYMED_ISTREAM: {
251 LARGE_INTEGER offset;
253 send_buffer.dwBufferTotal = This->base.bind_info.cbstgmedData;
254 if(!This->base.post_stream) {
255 This->base.post_stream = This->base.bind_info.stgmedData.u.pstm;
256 IStream_AddRef(This->base.post_stream);
259 offset.QuadPart = 0;
260 IStream_Seek(This->base.post_stream, offset, STREAM_SEEK_SET, NULL);
261 break;
263 default:
264 FIXME("Unsupported This->base.bind_info.stgmedData.tymed %d\n", This->base.bind_info.stgmedData.tymed);
268 if(This->base.post_stream)
269 res = HttpSendRequestExW(This->base.request, &send_buffer, NULL, 0, 0);
270 else
271 res = HttpSendRequestW(This->base.request, send_buffer.lpcszHeader, send_buffer.dwHeadersLength,
272 send_buffer.lpvBuffer, send_buffer.dwBufferLength);
274 return res ? 0 : GetLastError();
277 static inline HttpProtocol *impl_from_Protocol(Protocol *prot)
279 return CONTAINING_RECORD(prot, HttpProtocol, base);
282 static HRESULT HttpProtocol_open_request(Protocol *prot, IUri *uri, DWORD request_flags,
283 HINTERNET internet_session, IInternetBindInfo *bind_info)
285 HttpProtocol *This = impl_from_Protocol(prot);
286 LPWSTR addl_header = NULL, post_cookie = NULL;
287 IServiceProvider *service_provider = NULL;
288 IHttpNegotiate2 *http_negotiate2 = NULL;
289 BSTR url, host, user, pass, path;
290 LPOLESTR accept_mimes[257];
291 const WCHAR **accept_types;
292 BYTE security_id[512];
293 DWORD len, port, flags;
294 ULONG num, error;
295 BOOL res, b;
296 HRESULT hres;
298 static const WCHAR wszBindVerb[BINDVERB_CUSTOM][5] =
299 {{'G','E','T',0},
300 {'P','O','S','T',0},
301 {'P','U','T',0}};
303 hres = IUri_GetPort(uri, &port);
304 if(FAILED(hres))
305 return hres;
307 hres = IUri_GetHost(uri, &host);
308 if(FAILED(hres))
309 return hres;
311 hres = IUri_GetUserName(uri, &user);
312 if(SUCCEEDED(hres)) {
313 hres = IUri_GetPassword(uri, &pass);
315 if(SUCCEEDED(hres)) {
316 This->base.connection = InternetConnectW(internet_session, host, port, user, pass,
317 INTERNET_SERVICE_HTTP, This->https ? INTERNET_FLAG_SECURE : 0, (DWORD_PTR)&This->base);
318 SysFreeString(pass);
320 SysFreeString(user);
322 SysFreeString(host);
323 if(FAILED(hres))
324 return hres;
325 if(!This->base.connection) {
326 WARN("InternetConnect failed: %d\n", GetLastError());
327 return INET_E_CANNOT_CONNECT;
330 num = sizeof(accept_mimes)/sizeof(accept_mimes[0])-1;
331 hres = IInternetBindInfo_GetBindString(bind_info, BINDSTRING_ACCEPT_MIMES, accept_mimes, num, &num);
332 if(hres == INET_E_USE_DEFAULT_SETTING) {
333 static const WCHAR default_accept_mimeW[] = {'*','/','*',0};
334 static const WCHAR *default_accept_mimes[] = {default_accept_mimeW, NULL};
336 accept_types = default_accept_mimes;
337 num = 0;
338 }else if(hres == S_OK) {
339 accept_types = (const WCHAR**)accept_mimes;
340 }else {
341 WARN("GetBindString BINDSTRING_ACCEPT_MIMES failed: %08x\n", hres);
342 return INET_E_NO_VALID_MEDIA;
344 accept_mimes[num] = 0;
346 if(This->https)
347 request_flags |= INTERNET_FLAG_SECURE;
349 hres = IUri_GetPathAndQuery(uri, &path);
350 if(SUCCEEDED(hres)) {
351 This->base.request = HttpOpenRequestW(This->base.connection,
352 This->base.bind_info.dwBindVerb < BINDVERB_CUSTOM
353 ? wszBindVerb[This->base.bind_info.dwBindVerb] : This->base.bind_info.szCustomVerb,
354 path, NULL, NULL, accept_types, request_flags, (DWORD_PTR)&This->base);
355 SysFreeString(path);
357 while(num--)
358 CoTaskMemFree(accept_mimes[num]);
359 if(FAILED(hres))
360 return hres;
361 if (!This->base.request) {
362 WARN("HttpOpenRequest failed: %d\n", GetLastError());
363 return INET_E_RESOURCE_NOT_FOUND;
366 hres = IInternetProtocolSink_QueryInterface(This->base.protocol_sink, &IID_IServiceProvider,
367 (void **)&service_provider);
368 if (hres != S_OK) {
369 WARN("IInternetProtocolSink_QueryInterface IID_IServiceProvider failed: %08x\n", hres);
370 return hres;
373 hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate,
374 &IID_IHttpNegotiate, (void **)&This->http_negotiate);
375 if (hres != S_OK) {
376 WARN("IServiceProvider_QueryService IID_IHttpNegotiate failed: %08x\n", hres);
377 IServiceProvider_Release(service_provider);
378 return hres;
381 hres = IUri_GetAbsoluteUri(uri, &url);
382 if(FAILED(hres)) {
383 IServiceProvider_Release(service_provider);
384 return hres;
387 hres = IHttpNegotiate_BeginningTransaction(This->http_negotiate, url, default_headersW,
388 0, &addl_header);
389 SysFreeString(url);
390 if(hres != S_OK) {
391 WARN("IHttpNegotiate_BeginningTransaction failed: %08x\n", hres);
392 IServiceProvider_Release(service_provider);
393 return hres;
396 len = addl_header ? strlenW(addl_header) : 0;
398 This->full_header = heap_alloc(len*sizeof(WCHAR)+sizeof(default_headersW));
399 if(!This->full_header) {
400 IServiceProvider_Release(service_provider);
401 return E_OUTOFMEMORY;
404 if(len)
405 memcpy(This->full_header, addl_header, len*sizeof(WCHAR));
406 CoTaskMemFree(addl_header);
407 memcpy(This->full_header+len, default_headersW, sizeof(default_headersW));
409 hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate2,
410 &IID_IHttpNegotiate2, (void **)&http_negotiate2);
411 IServiceProvider_Release(service_provider);
412 if(hres != S_OK) {
413 WARN("IServiceProvider_QueryService IID_IHttpNegotiate2 failed: %08x\n", hres);
414 /* No goto done as per native */
415 }else {
416 len = sizeof(security_id)/sizeof(security_id[0]);
417 hres = IHttpNegotiate2_GetRootSecurityId(http_negotiate2, security_id, &len, 0);
418 IHttpNegotiate2_Release(http_negotiate2);
419 if (hres != S_OK)
420 WARN("IHttpNegotiate2_GetRootSecurityId failed: %08x\n", hres);
423 /* FIXME: Handle security_id. Native calls undocumented function IsHostInProxyBypassList. */
425 if(This->base.bind_info.dwBindVerb == BINDVERB_POST) {
426 num = 0;
427 hres = IInternetBindInfo_GetBindString(bind_info, BINDSTRING_POST_COOKIE, &post_cookie, 1, &num);
428 if(hres == S_OK && num) {
429 if(!InternetSetOptionW(This->base.request, INTERNET_OPTION_SECONDARY_CACHE_KEY,
430 post_cookie, lstrlenW(post_cookie)))
431 WARN("InternetSetOption INTERNET_OPTION_SECONDARY_CACHE_KEY failed: %d\n", GetLastError());
432 CoTaskMemFree(post_cookie);
436 flags = INTERNET_ERROR_MASK_COMBINED_SEC_CERT;
437 res = InternetSetOptionW(This->base.request, INTERNET_OPTION_ERROR_MASK, &flags, sizeof(flags));
438 if(!res)
439 WARN("InternetSetOption(INTERNET_OPTION_ERROR_MASK) failed: %u\n", GetLastError());
441 b = TRUE;
442 res = InternetSetOptionW(This->base.request, INTERNET_OPTION_HTTP_DECODING, &b, sizeof(b));
443 if(!res)
444 WARN("InternetSetOption(INTERNET_OPTION_HTTP_DECODING) failed: %u\n", GetLastError());
446 do {
447 error = send_http_request(This);
449 if(error == ERROR_IO_PENDING || error == ERROR_SUCCESS)
450 return S_OK;
452 hres = handle_http_error(This, error);
454 } while(hres == RPC_E_RETRY);
456 WARN("HttpSendRequest failed: %d\n", error);
457 return hres;
460 static HRESULT HttpProtocol_end_request(Protocol *protocol)
462 BOOL res;
464 res = HttpEndRequestW(protocol->request, NULL, 0, 0);
465 if(!res && GetLastError() != ERROR_IO_PENDING) {
466 FIXME("HttpEndRequest failed: %u\n", GetLastError());
467 return E_FAIL;
470 return S_OK;
473 static HRESULT HttpProtocol_start_downloading(Protocol *prot)
475 HttpProtocol *This = impl_from_Protocol(prot);
476 LPWSTR content_type, content_length, ranges;
477 DWORD len = sizeof(DWORD);
478 DWORD status_code;
479 BOOL res;
480 HRESULT hres;
482 static const WCHAR wszDefaultContentType[] =
483 {'t','e','x','t','/','h','t','m','l',0};
485 if(!This->http_negotiate) {
486 WARN("Expected IHttpNegotiate pointer to be non-NULL\n");
487 return S_OK;
490 res = HttpQueryInfoW(This->base.request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER,
491 &status_code, &len, NULL);
492 if(res) {
493 LPWSTR response_headers = query_http_info(This, HTTP_QUERY_RAW_HEADERS_CRLF);
494 if(response_headers) {
495 hres = IHttpNegotiate_OnResponse(This->http_negotiate, status_code, response_headers,
496 NULL, NULL);
497 heap_free(response_headers);
498 if (hres != S_OK) {
499 WARN("IHttpNegotiate_OnResponse failed: %08x\n", hres);
500 return S_OK;
503 }else {
504 WARN("HttpQueryInfo failed: %d\n", GetLastError());
507 ranges = query_http_info(This, HTTP_QUERY_ACCEPT_RANGES);
508 if(ranges) {
509 IInternetProtocolSink_ReportProgress(This->base.protocol_sink, BINDSTATUS_ACCEPTRANGES, NULL);
510 heap_free(ranges);
513 content_type = query_http_info(This, HTTP_QUERY_CONTENT_TYPE);
514 if(content_type) {
515 /* remove the charset, if present */
516 LPWSTR p = strchrW(content_type, ';');
517 if (p) *p = '\0';
519 IInternetProtocolSink_ReportProgress(This->base.protocol_sink,
520 (This->base.bindf & BINDF_FROMURLMON)
521 ? BINDSTATUS_MIMETYPEAVAILABLE : BINDSTATUS_RAWMIMETYPE,
522 content_type);
523 heap_free(content_type);
524 }else {
525 WARN("HttpQueryInfo failed: %d\n", GetLastError());
526 IInternetProtocolSink_ReportProgress(This->base.protocol_sink,
527 (This->base.bindf & BINDF_FROMURLMON)
528 ? BINDSTATUS_MIMETYPEAVAILABLE : BINDSTATUS_RAWMIMETYPE,
529 wszDefaultContentType);
532 content_length = query_http_info(This, HTTP_QUERY_CONTENT_LENGTH);
533 if(content_length) {
534 This->base.content_length = atoiW(content_length);
535 heap_free(content_length);
538 return S_OK;
541 static void HttpProtocol_close_connection(Protocol *prot)
543 HttpProtocol *This = impl_from_Protocol(prot);
545 if(This->http_negotiate) {
546 IHttpNegotiate_Release(This->http_negotiate);
547 This->http_negotiate = NULL;
550 heap_free(This->full_header);
551 This->full_header = NULL;
554 static void HttpProtocol_on_error(Protocol *prot, DWORD error)
556 HttpProtocol *This = impl_from_Protocol(prot);
557 HRESULT hres;
559 TRACE("(%p) %d\n", prot, error);
561 if(prot->flags & FLAG_FIRST_CONTINUE_COMPLETE) {
562 FIXME("Not handling error %d\n", error);
563 return;
566 while((hres = handle_http_error(This, error)) == RPC_E_RETRY) {
567 error = send_http_request(This);
569 if(error == ERROR_IO_PENDING || error == ERROR_SUCCESS)
570 return;
573 protocol_abort(prot, hres);
574 protocol_close_connection(prot);
575 return;
578 static const ProtocolVtbl AsyncProtocolVtbl = {
579 HttpProtocol_open_request,
580 HttpProtocol_end_request,
581 HttpProtocol_start_downloading,
582 HttpProtocol_close_connection,
583 HttpProtocol_on_error
586 static HRESULT WINAPI HttpProtocol_QueryInterface(IInternetProtocolEx *iface, REFIID riid, void **ppv)
588 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
590 *ppv = NULL;
591 if(IsEqualGUID(&IID_IUnknown, riid)) {
592 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
593 *ppv = &This->IInternetProtocolEx_iface;
594 }else if(IsEqualGUID(&IID_IInternetProtocolRoot, riid)) {
595 TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", This, ppv);
596 *ppv = &This->IInternetProtocolEx_iface;
597 }else if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
598 TRACE("(%p)->(IID_IInternetProtocol %p)\n", This, ppv);
599 *ppv = &This->IInternetProtocolEx_iface;
600 }else if(IsEqualGUID(&IID_IInternetProtocolEx, riid)) {
601 TRACE("(%p)->(IID_IInternetProtocolEx %p)\n", This, ppv);
602 *ppv = &This->IInternetProtocolEx_iface;
603 }else if(IsEqualGUID(&IID_IInternetPriority, riid)) {
604 TRACE("(%p)->(IID_IInternetPriority %p)\n", This, ppv);
605 *ppv = &This->IInternetPriority_iface;
606 }else if(IsEqualGUID(&IID_IWinInetInfo, riid)) {
607 TRACE("(%p)->(IID_IWinInetInfo %p)\n", This, ppv);
608 *ppv = &This->IWinInetHttpInfo_iface;
609 }else if(IsEqualGUID(&IID_IWinInetHttpInfo, riid)) {
610 TRACE("(%p)->(IID_IWinInetHttpInfo %p)\n", This, ppv);
611 *ppv = &This->IWinInetHttpInfo_iface;
614 if(*ppv) {
615 IInternetProtocolEx_AddRef(iface);
616 return S_OK;
619 WARN("not supported interface %s\n", debugstr_guid(riid));
620 return E_NOINTERFACE;
623 static ULONG WINAPI HttpProtocol_AddRef(IInternetProtocolEx *iface)
625 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
626 LONG ref = InterlockedIncrement(&This->ref);
627 TRACE("(%p) ref=%d\n", This, ref);
628 return ref;
631 static ULONG WINAPI HttpProtocol_Release(IInternetProtocolEx *iface)
633 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
634 LONG ref = InterlockedDecrement(&This->ref);
636 TRACE("(%p) ref=%d\n", This, ref);
638 if(!ref) {
639 protocol_close_connection(&This->base);
640 heap_free(This);
642 URLMON_UnlockModule();
645 return ref;
648 static HRESULT WINAPI HttpProtocol_Start(IInternetProtocolEx *iface, LPCWSTR szUrl,
649 IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
650 DWORD grfPI, HANDLE_PTR dwReserved)
652 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
653 IUri *uri;
654 HRESULT hres;
656 TRACE("(%p)->(%s %p %p %08x %lx)\n", This, debugstr_w(szUrl), pOIProtSink,
657 pOIBindInfo, grfPI, dwReserved);
659 hres = CreateUri(szUrl, 0, 0, &uri);
660 if(FAILED(hres))
661 return hres;
663 hres = IInternetProtocolEx_StartEx(&This->IInternetProtocolEx_iface, uri, pOIProtSink,
664 pOIBindInfo, grfPI, (HANDLE*)dwReserved);
666 IUri_Release(uri);
667 return hres;
670 static HRESULT WINAPI HttpProtocol_Continue(IInternetProtocolEx *iface, PROTOCOLDATA *pProtocolData)
672 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
674 TRACE("(%p)->(%p)\n", This, pProtocolData);
676 return protocol_continue(&This->base, pProtocolData);
679 static HRESULT WINAPI HttpProtocol_Abort(IInternetProtocolEx *iface, HRESULT hrReason,
680 DWORD dwOptions)
682 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
684 TRACE("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);
686 return protocol_abort(&This->base, hrReason);
689 static HRESULT WINAPI HttpProtocol_Terminate(IInternetProtocolEx *iface, DWORD dwOptions)
691 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
693 TRACE("(%p)->(%08x)\n", This, dwOptions);
695 protocol_close_connection(&This->base);
696 return S_OK;
699 static HRESULT WINAPI HttpProtocol_Suspend(IInternetProtocolEx *iface)
701 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
702 FIXME("(%p)\n", This);
703 return E_NOTIMPL;
706 static HRESULT WINAPI HttpProtocol_Resume(IInternetProtocolEx *iface)
708 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
709 FIXME("(%p)\n", This);
710 return E_NOTIMPL;
713 static HRESULT WINAPI HttpProtocol_Read(IInternetProtocolEx *iface, void *pv,
714 ULONG cb, ULONG *pcbRead)
716 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
718 TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
720 return protocol_read(&This->base, pv, cb, pcbRead);
723 static HRESULT WINAPI HttpProtocol_Seek(IInternetProtocolEx *iface, LARGE_INTEGER dlibMove,
724 DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
726 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
727 FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
728 return E_NOTIMPL;
731 static HRESULT WINAPI HttpProtocol_LockRequest(IInternetProtocolEx *iface, DWORD dwOptions)
733 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
735 TRACE("(%p)->(%08x)\n", This, dwOptions);
737 return protocol_lock_request(&This->base);
740 static HRESULT WINAPI HttpProtocol_UnlockRequest(IInternetProtocolEx *iface)
742 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
744 TRACE("(%p)\n", This);
746 return protocol_unlock_request(&This->base);
749 static HRESULT WINAPI HttpProtocol_StartEx(IInternetProtocolEx *iface, IUri *pUri,
750 IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
751 DWORD grfPI, HANDLE *dwReserved)
753 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
754 DWORD scheme = 0;
755 HRESULT hres;
757 TRACE("(%p)->(%p %p %p %08x %p)\n", This, pUri, pOIProtSink,
758 pOIBindInfo, grfPI, dwReserved);
760 hres = IUri_GetScheme(pUri, &scheme);
761 if(FAILED(hres))
762 return hres;
763 if(scheme != (This->https ? URL_SCHEME_HTTPS : URL_SCHEME_HTTP))
764 return MK_E_SYNTAX;
766 return protocol_start(&This->base, (IInternetProtocol*)&This->IInternetProtocolEx_iface, pUri,
767 pOIProtSink, pOIBindInfo);
770 static const IInternetProtocolExVtbl HttpProtocolVtbl = {
771 HttpProtocol_QueryInterface,
772 HttpProtocol_AddRef,
773 HttpProtocol_Release,
774 HttpProtocol_Start,
775 HttpProtocol_Continue,
776 HttpProtocol_Abort,
777 HttpProtocol_Terminate,
778 HttpProtocol_Suspend,
779 HttpProtocol_Resume,
780 HttpProtocol_Read,
781 HttpProtocol_Seek,
782 HttpProtocol_LockRequest,
783 HttpProtocol_UnlockRequest,
784 HttpProtocol_StartEx
787 static HRESULT WINAPI HttpPriority_QueryInterface(IInternetPriority *iface, REFIID riid, void **ppv)
789 HttpProtocol *This = impl_from_IInternetPriority(iface);
790 return IInternetProtocolEx_QueryInterface(&This->IInternetProtocolEx_iface, riid, ppv);
793 static ULONG WINAPI HttpPriority_AddRef(IInternetPriority *iface)
795 HttpProtocol *This = impl_from_IInternetPriority(iface);
796 return IInternetProtocolEx_AddRef(&This->IInternetProtocolEx_iface);
799 static ULONG WINAPI HttpPriority_Release(IInternetPriority *iface)
801 HttpProtocol *This = impl_from_IInternetPriority(iface);
802 return IInternetProtocolEx_Release(&This->IInternetProtocolEx_iface);
805 static HRESULT WINAPI HttpPriority_SetPriority(IInternetPriority *iface, LONG nPriority)
807 HttpProtocol *This = impl_from_IInternetPriority(iface);
809 TRACE("(%p)->(%d)\n", This, nPriority);
811 This->base.priority = nPriority;
812 return S_OK;
815 static HRESULT WINAPI HttpPriority_GetPriority(IInternetPriority *iface, LONG *pnPriority)
817 HttpProtocol *This = impl_from_IInternetPriority(iface);
819 TRACE("(%p)->(%p)\n", This, pnPriority);
821 *pnPriority = This->base.priority;
822 return S_OK;
825 static const IInternetPriorityVtbl HttpPriorityVtbl = {
826 HttpPriority_QueryInterface,
827 HttpPriority_AddRef,
828 HttpPriority_Release,
829 HttpPriority_SetPriority,
830 HttpPriority_GetPriority
833 static HRESULT WINAPI HttpInfo_QueryInterface(IWinInetHttpInfo *iface, REFIID riid, void **ppv)
835 HttpProtocol *This = impl_from_IWinInetHttpInfo(iface);
836 return IInternetProtocolEx_QueryInterface(&This->IInternetProtocolEx_iface, riid, ppv);
839 static ULONG WINAPI HttpInfo_AddRef(IWinInetHttpInfo *iface)
841 HttpProtocol *This = impl_from_IWinInetHttpInfo(iface);
842 return IInternetProtocolEx_AddRef(&This->IInternetProtocolEx_iface);
845 static ULONG WINAPI HttpInfo_Release(IWinInetHttpInfo *iface)
847 HttpProtocol *This = impl_from_IWinInetHttpInfo(iface);
848 return IInternetProtocolEx_Release(&This->IInternetProtocolEx_iface);
851 static HRESULT WINAPI HttpInfo_QueryOption(IWinInetHttpInfo *iface, DWORD dwOption,
852 void *pBuffer, DWORD *pcbBuffer)
854 HttpProtocol *This = impl_from_IWinInetHttpInfo(iface);
855 TRACE("(%p)->(%x %p %p)\n", This, dwOption, pBuffer, pcbBuffer);
857 if(!This->base.request)
858 return E_FAIL;
860 if(!InternetQueryOptionW(This->base.request, dwOption, pBuffer, pcbBuffer))
861 return S_FALSE;
862 return S_OK;
865 static HRESULT WINAPI HttpInfo_QueryInfo(IWinInetHttpInfo *iface, DWORD dwOption,
866 void *pBuffer, DWORD *pcbBuffer, DWORD *pdwFlags, DWORD *pdwReserved)
868 HttpProtocol *This = impl_from_IWinInetHttpInfo(iface);
869 TRACE("(%p)->(%x %p %p %p %p)\n", This, dwOption, pBuffer, pcbBuffer, pdwFlags, pdwReserved);
871 if(!This->base.request)
872 return E_FAIL;
874 if(!HttpQueryInfoW(This->base.request, dwOption, pBuffer, pcbBuffer, pdwFlags)) {
875 if(pBuffer)
876 memset(pBuffer, 0, *pcbBuffer);
877 return S_OK;
879 return S_OK;
882 static const IWinInetHttpInfoVtbl WinInetHttpInfoVtbl = {
883 HttpInfo_QueryInterface,
884 HttpInfo_AddRef,
885 HttpInfo_Release,
886 HttpInfo_QueryOption,
887 HttpInfo_QueryInfo
890 static HRESULT create_http_protocol(BOOL https, void **ppobj)
892 HttpProtocol *ret;
894 ret = heap_alloc_zero(sizeof(HttpProtocol));
895 if(!ret)
896 return E_OUTOFMEMORY;
898 ret->base.vtbl = &AsyncProtocolVtbl;
899 ret->IInternetProtocolEx_iface.lpVtbl = &HttpProtocolVtbl;
900 ret->IInternetPriority_iface.lpVtbl = &HttpPriorityVtbl;
901 ret->IWinInetHttpInfo_iface.lpVtbl = &WinInetHttpInfoVtbl;
903 ret->https = https;
904 ret->ref = 1;
906 *ppobj = &ret->IInternetProtocolEx_iface;
908 URLMON_LockModule();
909 return S_OK;
912 HRESULT HttpProtocol_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
914 TRACE("(%p %p)\n", pUnkOuter, ppobj);
916 return create_http_protocol(FALSE, ppobj);
919 HRESULT HttpSProtocol_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
921 TRACE("(%p %p)\n", pUnkOuter, ppobj);
923 return create_http_protocol(TRUE, ppobj);