dmband: Set DMUS_PATCH_PMSG bank LSB/MSB from instrument patch.
[wine.git] / dlls / urlmon / http.c
bloba24006ba08cd98ac88b8d72dad833e4a7d2d2cd4
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 IUnknown IUnknown_inner;
34 IInternetProtocolEx IInternetProtocolEx_iface;
35 IInternetPriority IInternetPriority_iface;
36 IWinInetHttpInfo IWinInetHttpInfo_iface;
38 BOOL https;
39 IHttpNegotiate *http_negotiate;
40 WCHAR *full_header;
42 LONG ref;
43 IUnknown *outer;
44 } HttpProtocol;
46 static inline HttpProtocol *impl_from_IUnknown(IUnknown *iface)
48 return CONTAINING_RECORD(iface, HttpProtocol, IUnknown_inner);
51 static inline HttpProtocol *impl_from_IInternetProtocolEx(IInternetProtocolEx *iface)
53 return CONTAINING_RECORD(iface, HttpProtocol, IInternetProtocolEx_iface);
56 static inline HttpProtocol *impl_from_IInternetPriority(IInternetPriority *iface)
58 return CONTAINING_RECORD(iface, HttpProtocol, IInternetPriority_iface);
61 static inline HttpProtocol *impl_from_IWinInetHttpInfo(IWinInetHttpInfo *iface)
63 return CONTAINING_RECORD(iface, HttpProtocol, IWinInetHttpInfo_iface);
66 static const WCHAR default_headersW[] = L"Accept-Encoding: gzip, deflate";
68 static LPWSTR query_http_info(HttpProtocol *This, DWORD option)
70 LPWSTR ret = NULL;
71 DWORD len = 0;
72 BOOL res;
74 res = HttpQueryInfoW(This->base.request, option, NULL, &len, NULL);
75 if (!res && GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
76 ret = malloc(len);
77 res = HttpQueryInfoW(This->base.request, option, ret, &len, NULL);
79 if(!res) {
80 TRACE("HttpQueryInfoW(%ld) failed: %08lx\n", option, GetLastError());
81 free(ret);
82 return NULL;
85 return ret;
88 static inline BOOL set_security_flag(HttpProtocol *This, DWORD flags)
90 BOOL res;
92 res = InternetSetOptionW(This->base.request, INTERNET_OPTION_SECURITY_FLAGS, &flags, sizeof(flags));
93 if(!res)
94 ERR("Failed to set security flags: %lx\n", flags);
96 return res;
99 static inline HRESULT internet_error_to_hres(DWORD error)
101 switch(error)
103 case ERROR_INTERNET_SEC_CERT_DATE_INVALID:
104 case ERROR_INTERNET_SEC_CERT_CN_INVALID:
105 case ERROR_INTERNET_INVALID_CA:
106 case ERROR_INTERNET_CLIENT_AUTH_CERT_NEEDED:
107 case ERROR_INTERNET_SEC_INVALID_CERT:
108 case ERROR_INTERNET_SEC_CERT_ERRORS:
109 case ERROR_INTERNET_SEC_CERT_REV_FAILED:
110 case ERROR_INTERNET_SEC_CERT_NO_REV:
111 case ERROR_INTERNET_SEC_CERT_REVOKED:
112 return INET_E_INVALID_CERTIFICATE;
113 case ERROR_INTERNET_HTTP_TO_HTTPS_ON_REDIR:
114 case ERROR_INTERNET_HTTPS_TO_HTTP_ON_REDIR:
115 case ERROR_HTTP_REDIRECT_NEEDS_CONFIRMATION:
116 return INET_E_REDIRECT_FAILED;
117 default:
118 return INET_E_DOWNLOAD_FAILURE;
122 static HRESULT handle_http_error(HttpProtocol *This, DWORD error)
124 IServiceProvider *serv_prov;
125 IWindowForBindingUI *wfb_ui;
126 IHttpSecurity *http_security;
127 BOOL security_problem;
128 DWORD dlg_flags;
129 HWND hwnd;
130 DWORD res;
131 HRESULT hres;
133 TRACE("(%p %lu)\n", This, error);
135 switch(error) {
136 case ERROR_INTERNET_SEC_CERT_DATE_INVALID:
137 case ERROR_INTERNET_SEC_CERT_CN_INVALID:
138 case ERROR_INTERNET_HTTP_TO_HTTPS_ON_REDIR:
139 case ERROR_INTERNET_HTTPS_TO_HTTP_ON_REDIR:
140 case ERROR_INTERNET_INVALID_CA:
141 case ERROR_INTERNET_CLIENT_AUTH_CERT_NEEDED:
142 case ERROR_INTERNET_SEC_INVALID_CERT:
143 case ERROR_INTERNET_SEC_CERT_ERRORS:
144 case ERROR_INTERNET_SEC_CERT_REV_FAILED:
145 case ERROR_INTERNET_SEC_CERT_NO_REV:
146 case ERROR_INTERNET_SEC_CERT_REVOKED:
147 case ERROR_HTTP_REDIRECT_NEEDS_CONFIRMATION:
148 security_problem = TRUE;
149 break;
150 default:
151 security_problem = FALSE;
154 hres = IInternetProtocolSink_QueryInterface(This->base.protocol_sink, &IID_IServiceProvider,
155 (void**)&serv_prov);
156 if(FAILED(hres)) {
157 ERR("Failed to get IServiceProvider.\n");
158 return E_ABORT;
161 if(security_problem) {
162 hres = IServiceProvider_QueryService(serv_prov, &IID_IHttpSecurity, &IID_IHttpSecurity,
163 (void**)&http_security);
164 if(SUCCEEDED(hres)) {
165 hres = IHttpSecurity_OnSecurityProblem(http_security, error);
166 IHttpSecurity_Release(http_security);
168 TRACE("OnSecurityProblem returned %08lx\n", hres);
170 if(hres != S_FALSE)
172 BOOL res = FALSE;
174 IServiceProvider_Release(serv_prov);
176 if(hres == S_OK) {
177 if(error == ERROR_INTERNET_SEC_CERT_DATE_INVALID)
178 res = set_security_flag(This, SECURITY_FLAG_IGNORE_CERT_DATE_INVALID);
179 else if(error == ERROR_INTERNET_SEC_CERT_CN_INVALID)
180 res = set_security_flag(This, SECURITY_FLAG_IGNORE_CERT_CN_INVALID);
181 else if(error == ERROR_INTERNET_INVALID_CA)
182 res = set_security_flag(This, SECURITY_FLAG_IGNORE_UNKNOWN_CA);
184 if(res)
185 return RPC_E_RETRY;
187 FIXME("Don't know how to ignore error %ld\n", error);
188 return E_ABORT;
191 if(hres == E_ABORT)
192 return E_ABORT;
193 if(hres == RPC_E_RETRY)
194 return RPC_E_RETRY;
196 return internet_error_to_hres(error);
201 switch(error) {
202 case ERROR_INTERNET_SEC_CERT_REV_FAILED:
203 if(hres != S_FALSE) {
204 /* Silently ignore the error. We will get more detailed error from wininet anyway. */
205 set_security_flag(This, SECURITY_FLAG_IGNORE_REVOCATION);
206 hres = RPC_E_RETRY;
207 break;
209 /* fallthrough */
210 default:
211 hres = IServiceProvider_QueryService(serv_prov, &IID_IWindowForBindingUI, &IID_IWindowForBindingUI, (void**)&wfb_ui);
212 if(SUCCEEDED(hres)) {
213 const IID *iid_reason;
215 if(security_problem)
216 iid_reason = &IID_IHttpSecurity;
217 else if(error == ERROR_INTERNET_INCORRECT_PASSWORD)
218 iid_reason = &IID_IAuthenticate;
219 else
220 iid_reason = &IID_IWindowForBindingUI;
222 hres = IWindowForBindingUI_GetWindow(wfb_ui, iid_reason, &hwnd);
223 IWindowForBindingUI_Release(wfb_ui);
226 if(FAILED(hres)) hwnd = NULL;
228 dlg_flags = FLAGS_ERROR_UI_FLAGS_CHANGE_OPTIONS | FLAGS_ERROR_UI_FLAGS_GENERATE_DATA;
229 if(This->base.bindf & BINDF_NO_UI)
230 dlg_flags |= FLAGS_ERROR_UI_FLAGS_NO_UI;
232 res = InternetErrorDlg(hwnd, This->base.request, error, dlg_flags, NULL);
233 hres = res == ERROR_INTERNET_FORCE_RETRY || res == ERROR_SUCCESS ? RPC_E_RETRY : internet_error_to_hres(error);
236 IServiceProvider_Release(serv_prov);
237 return hres;
240 static ULONG send_http_request(HttpProtocol *This)
242 INTERNET_BUFFERSW send_buffer = {sizeof(INTERNET_BUFFERSW)};
243 BOOL res;
245 send_buffer.lpcszHeader = This->full_header;
246 send_buffer.dwHeadersLength = send_buffer.dwHeadersTotal = lstrlenW(This->full_header);
248 if(This->base.bind_info.dwBindVerb != BINDVERB_GET) {
249 switch(This->base.bind_info.stgmedData.tymed) {
250 case TYMED_HGLOBAL:
251 /* Native does not use GlobalLock/GlobalUnlock, so we won't either */
252 send_buffer.lpvBuffer = This->base.bind_info.stgmedData.hGlobal;
253 send_buffer.dwBufferLength = send_buffer.dwBufferTotal = This->base.bind_info.cbstgmedData;
254 break;
255 case TYMED_ISTREAM: {
256 LARGE_INTEGER offset;
258 send_buffer.dwBufferTotal = This->base.bind_info.cbstgmedData;
259 if(!This->base.post_stream) {
260 This->base.post_stream = This->base.bind_info.stgmedData.pstm;
261 IStream_AddRef(This->base.post_stream);
264 offset.QuadPart = 0;
265 IStream_Seek(This->base.post_stream, offset, STREAM_SEEK_SET, NULL);
266 break;
268 default:
269 FIXME("Unsupported This->base.bind_info.stgmedData.tymed %ld\n", This->base.bind_info.stgmedData.tymed);
273 if(This->base.post_stream)
274 res = HttpSendRequestExW(This->base.request, &send_buffer, NULL, 0, 0);
275 else
276 res = HttpSendRequestW(This->base.request, send_buffer.lpcszHeader, send_buffer.dwHeadersLength,
277 send_buffer.lpvBuffer, send_buffer.dwBufferLength);
279 return res ? 0 : GetLastError();
282 static inline HttpProtocol *impl_from_Protocol(Protocol *prot)
284 return CONTAINING_RECORD(prot, HttpProtocol, base);
287 static HRESULT HttpProtocol_open_request(Protocol *prot, IUri *uri, DWORD request_flags,
288 HINTERNET internet_session, IInternetBindInfo *bind_info)
290 HttpProtocol *This = impl_from_Protocol(prot);
291 WCHAR *addl_header = NULL, *post_cookie = NULL, *rootdoc_url = NULL;
292 IServiceProvider *service_provider = NULL;
293 IHttpNegotiate2 *http_negotiate2 = NULL;
294 BSTR url, host, user, pass, path;
295 LPOLESTR accept_mimes[257];
296 const WCHAR **accept_types;
297 BYTE security_id[512];
298 DWORD len, port, flags;
299 ULONG num, error;
300 BOOL res, b;
301 HRESULT hres;
303 static const WCHAR wszBindVerb[BINDVERB_CUSTOM][5] =
304 {L"GET",
305 L"POST",
306 L"PUT"};
308 hres = IUri_GetPort(uri, &port);
309 if(FAILED(hres))
310 return hres;
312 hres = IUri_GetHost(uri, &host);
313 if(FAILED(hres))
314 return hres;
316 hres = IUri_GetUserName(uri, &user);
317 if(SUCCEEDED(hres)) {
318 hres = IUri_GetPassword(uri, &pass);
320 if(SUCCEEDED(hres)) {
321 This->base.connection = InternetConnectW(internet_session, host, port, user, pass,
322 INTERNET_SERVICE_HTTP, This->https ? INTERNET_FLAG_SECURE : 0, (DWORD_PTR)&This->base);
323 SysFreeString(pass);
325 SysFreeString(user);
327 SysFreeString(host);
328 if(FAILED(hres))
329 return hres;
330 if(!This->base.connection) {
331 WARN("InternetConnect failed: %ld\n", GetLastError());
332 return INET_E_CANNOT_CONNECT;
335 num = 0;
336 hres = IInternetBindInfo_GetBindString(bind_info, BINDSTRING_ROOTDOC_URL, &rootdoc_url, 1, &num);
337 if(hres == S_OK && num) {
338 FIXME("Use root doc URL %s\n", debugstr_w(rootdoc_url));
339 CoTaskMemFree(rootdoc_url);
342 num = ARRAY_SIZE(accept_mimes) - 1;
343 hres = IInternetBindInfo_GetBindString(bind_info, BINDSTRING_ACCEPT_MIMES, accept_mimes, num, &num);
344 if(hres == INET_E_USE_DEFAULT_SETTING) {
345 static const WCHAR *default_accept_mimes[] = {L"*/*", NULL};
347 accept_types = default_accept_mimes;
348 num = 0;
349 }else if(hres == S_OK) {
350 accept_types = (const WCHAR**)accept_mimes;
351 }else {
352 WARN("GetBindString BINDSTRING_ACCEPT_MIMES failed: %08lx\n", hres);
353 return INET_E_NO_VALID_MEDIA;
355 accept_mimes[num] = 0;
357 if(This->https)
358 request_flags |= INTERNET_FLAG_SECURE;
360 hres = IUri_GetPathAndQuery(uri, &path);
361 if(SUCCEEDED(hres)) {
362 This->base.request = HttpOpenRequestW(This->base.connection,
363 This->base.bind_info.dwBindVerb < BINDVERB_CUSTOM
364 ? wszBindVerb[This->base.bind_info.dwBindVerb] : This->base.bind_info.szCustomVerb,
365 path, NULL, NULL, accept_types, request_flags, (DWORD_PTR)&This->base);
366 SysFreeString(path);
368 while(num--)
369 CoTaskMemFree(accept_mimes[num]);
370 if(FAILED(hres))
371 return hres;
372 if (!This->base.request) {
373 WARN("HttpOpenRequest failed: %ld\n", GetLastError());
374 return INET_E_RESOURCE_NOT_FOUND;
377 hres = IInternetProtocolSink_QueryInterface(This->base.protocol_sink, &IID_IServiceProvider,
378 (void **)&service_provider);
379 if (hres != S_OK) {
380 WARN("IInternetProtocolSink_QueryInterface IID_IServiceProvider failed: %08lx\n", hres);
381 return hres;
384 hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate,
385 &IID_IHttpNegotiate, (void **)&This->http_negotiate);
386 if (hres != S_OK) {
387 WARN("IServiceProvider_QueryService IID_IHttpNegotiate failed: %08lx\n", hres);
388 IServiceProvider_Release(service_provider);
389 return hres;
392 hres = IUri_GetAbsoluteUri(uri, &url);
393 if(FAILED(hres)) {
394 IServiceProvider_Release(service_provider);
395 return hres;
398 hres = IHttpNegotiate_BeginningTransaction(This->http_negotiate, url, default_headersW,
399 0, &addl_header);
400 SysFreeString(url);
401 if(hres != S_OK) {
402 WARN("IHttpNegotiate_BeginningTransaction failed: %08lx\n", hres);
403 IServiceProvider_Release(service_provider);
404 return hres;
407 len = addl_header ? lstrlenW(addl_header) : 0;
409 This->full_header = malloc(len * sizeof(WCHAR) + sizeof(default_headersW));
410 if(!This->full_header) {
411 IServiceProvider_Release(service_provider);
412 return E_OUTOFMEMORY;
415 if(len)
416 memcpy(This->full_header, addl_header, len*sizeof(WCHAR));
417 CoTaskMemFree(addl_header);
418 memcpy(This->full_header+len, default_headersW, sizeof(default_headersW));
420 hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate2,
421 &IID_IHttpNegotiate2, (void **)&http_negotiate2);
422 IServiceProvider_Release(service_provider);
423 if(hres != S_OK) {
424 WARN("IServiceProvider_QueryService IID_IHttpNegotiate2 failed: %08lx\n", hres);
425 /* No goto done as per native */
426 }else {
427 len = ARRAY_SIZE(security_id);
428 hres = IHttpNegotiate2_GetRootSecurityId(http_negotiate2, security_id, &len, 0);
429 IHttpNegotiate2_Release(http_negotiate2);
430 if (hres != S_OK)
431 WARN("IHttpNegotiate2_GetRootSecurityId failed: %08lx\n", hres);
434 /* FIXME: Handle security_id. Native calls undocumented function IsHostInProxyBypassList. */
436 if(This->base.bind_info.dwBindVerb == BINDVERB_POST) {
437 num = 0;
438 hres = IInternetBindInfo_GetBindString(bind_info, BINDSTRING_POST_COOKIE, &post_cookie, 1, &num);
439 if(hres == S_OK && num) {
440 if(!InternetSetOptionW(This->base.request, INTERNET_OPTION_SECONDARY_CACHE_KEY,
441 post_cookie, lstrlenW(post_cookie)))
442 WARN("InternetSetOption INTERNET_OPTION_SECONDARY_CACHE_KEY failed: %ld\n", GetLastError());
443 CoTaskMemFree(post_cookie);
447 flags = INTERNET_ERROR_MASK_COMBINED_SEC_CERT;
448 res = InternetSetOptionW(This->base.request, INTERNET_OPTION_ERROR_MASK, &flags, sizeof(flags));
449 if(!res)
450 WARN("InternetSetOption(INTERNET_OPTION_ERROR_MASK) failed: %lu\n", GetLastError());
452 b = TRUE;
453 res = InternetSetOptionW(This->base.request, INTERNET_OPTION_HTTP_DECODING, &b, sizeof(b));
454 if(!res)
455 WARN("InternetSetOption(INTERNET_OPTION_HTTP_DECODING) failed: %lu\n", GetLastError());
457 do {
458 error = send_http_request(This);
460 switch(error) {
461 case ERROR_IO_PENDING:
462 return S_OK;
463 case ERROR_SUCCESS:
465 * If sending response ended synchronously, it means that we have the whole data
466 * available locally (most likely in cache).
468 return protocol_syncbinding(&This->base);
469 default:
470 hres = handle_http_error(This, error);
472 } while(hres == RPC_E_RETRY);
474 WARN("HttpSendRequest failed: %ld\n", error);
475 return hres;
478 static HRESULT HttpProtocol_end_request(Protocol *protocol)
480 BOOL res;
482 res = HttpEndRequestW(protocol->request, NULL, 0, 0);
483 if(!res && GetLastError() != ERROR_IO_PENDING) {
484 FIXME("HttpEndRequest failed: %lu\n", GetLastError());
485 return E_FAIL;
488 return S_OK;
491 static BOOL is_redirect_response(DWORD status_code)
493 switch(status_code) {
494 case HTTP_STATUS_REDIRECT:
495 case HTTP_STATUS_MOVED:
496 case HTTP_STATUS_REDIRECT_KEEP_VERB:
497 case HTTP_STATUS_REDIRECT_METHOD:
498 return TRUE;
500 return FALSE;
503 static HRESULT HttpProtocol_start_downloading(Protocol *prot)
505 HttpProtocol *This = impl_from_Protocol(prot);
506 LPWSTR content_type, content_length, ranges;
507 DWORD len = sizeof(DWORD);
508 DWORD status_code;
509 BOOL res;
510 HRESULT hres;
512 if(!This->http_negotiate) {
513 WARN("Expected IHttpNegotiate pointer to be non-NULL\n");
514 return S_OK;
517 res = HttpQueryInfoW(This->base.request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER,
518 &status_code, &len, NULL);
519 if(res) {
520 WCHAR *response_headers;
522 if((This->base.bind_info.dwOptions & BINDINFO_OPTIONS_DISABLEAUTOREDIRECTS) && is_redirect_response(status_code)) {
523 WCHAR *location;
525 TRACE("Got redirect with disabled auto redirects\n");
527 location = query_http_info(This, HTTP_QUERY_LOCATION);
528 This->base.flags |= FLAG_RESULT_REPORTED | FLAG_LAST_DATA_REPORTED;
529 IInternetProtocolSink_ReportResult(This->base.protocol_sink, INET_E_REDIRECT_FAILED, 0, location);
530 free(location);
531 return INET_E_REDIRECT_FAILED;
534 response_headers = query_http_info(This, HTTP_QUERY_RAW_HEADERS_CRLF);
535 if(response_headers) {
536 hres = IHttpNegotiate_OnResponse(This->http_negotiate, status_code, response_headers,
537 NULL, NULL);
538 free(response_headers);
539 if (hres != S_OK) {
540 WARN("IHttpNegotiate_OnResponse failed: %08lx\n", hres);
541 return S_OK;
544 }else {
545 WARN("HttpQueryInfo failed: %ld\n", GetLastError());
548 ranges = query_http_info(This, HTTP_QUERY_ACCEPT_RANGES);
549 if(ranges) {
550 IInternetProtocolSink_ReportProgress(This->base.protocol_sink, BINDSTATUS_ACCEPTRANGES, NULL);
551 free(ranges);
554 content_type = query_http_info(This, HTTP_QUERY_CONTENT_TYPE);
555 if(content_type) {
556 /* remove the charset, if present */
557 LPWSTR p = wcschr(content_type, ';');
558 if (p) *p = '\0';
560 IInternetProtocolSink_ReportProgress(This->base.protocol_sink,
561 (This->base.bindf & BINDF_FROMURLMON)
562 ? BINDSTATUS_MIMETYPEAVAILABLE : BINDSTATUS_RAWMIMETYPE,
563 content_type);
564 free(content_type);
565 }else {
566 WARN("HttpQueryInfo failed: %ld\n", GetLastError());
567 IInternetProtocolSink_ReportProgress(This->base.protocol_sink,
568 (This->base.bindf & BINDF_FROMURLMON)
569 ? BINDSTATUS_MIMETYPEAVAILABLE : BINDSTATUS_RAWMIMETYPE, L"text/html");
572 content_length = query_http_info(This, HTTP_QUERY_CONTENT_LENGTH);
573 if(content_length) {
574 This->base.content_length = wcstol(content_length, NULL, 10);
575 free(content_length);
578 return S_OK;
581 static void HttpProtocol_close_connection(Protocol *prot)
583 HttpProtocol *This = impl_from_Protocol(prot);
585 if(This->http_negotiate) {
586 IHttpNegotiate_Release(This->http_negotiate);
587 This->http_negotiate = NULL;
590 free(This->full_header);
591 This->full_header = NULL;
594 static void HttpProtocol_on_error(Protocol *prot, DWORD error)
596 HttpProtocol *This = impl_from_Protocol(prot);
597 HRESULT hres;
599 TRACE("(%p) %ld\n", prot, error);
601 if(prot->flags & FLAG_FIRST_CONTINUE_COMPLETE) {
602 FIXME("Not handling error %ld\n", error);
603 return;
606 while((hres = handle_http_error(This, error)) == RPC_E_RETRY) {
607 error = send_http_request(This);
609 if(error == ERROR_IO_PENDING || error == ERROR_SUCCESS)
610 return;
613 protocol_abort(prot, hres);
614 protocol_close_connection(prot);
615 return;
618 static const ProtocolVtbl AsyncProtocolVtbl = {
619 HttpProtocol_open_request,
620 HttpProtocol_end_request,
621 HttpProtocol_start_downloading,
622 HttpProtocol_close_connection,
623 HttpProtocol_on_error
626 static HRESULT WINAPI HttpProtocolUnk_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
628 HttpProtocol *This = impl_from_IUnknown(iface);
630 if(IsEqualGUID(&IID_IUnknown, riid)) {
631 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
632 *ppv = &This->IUnknown_inner;
633 }else if(IsEqualGUID(&IID_IInternetProtocolRoot, riid)) {
634 TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", This, ppv);
635 *ppv = &This->IInternetProtocolEx_iface;
636 }else if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
637 TRACE("(%p)->(IID_IInternetProtocol %p)\n", This, ppv);
638 *ppv = &This->IInternetProtocolEx_iface;
639 }else if(IsEqualGUID(&IID_IInternetProtocolEx, riid)) {
640 TRACE("(%p)->(IID_IInternetProtocolEx %p)\n", This, ppv);
641 *ppv = &This->IInternetProtocolEx_iface;
642 }else if(IsEqualGUID(&IID_IInternetPriority, riid)) {
643 TRACE("(%p)->(IID_IInternetPriority %p)\n", This, ppv);
644 *ppv = &This->IInternetPriority_iface;
645 }else if(IsEqualGUID(&IID_IWinInetInfo, riid)) {
646 TRACE("(%p)->(IID_IWinInetInfo %p)\n", This, ppv);
647 *ppv = &This->IWinInetHttpInfo_iface;
648 }else if(IsEqualGUID(&IID_IWinInetHttpInfo, riid)) {
649 TRACE("(%p)->(IID_IWinInetHttpInfo %p)\n", This, ppv);
650 *ppv = &This->IWinInetHttpInfo_iface;
651 }else {
652 *ppv = NULL;
653 WARN("not supported interface %s\n", debugstr_guid(riid));
654 return E_NOINTERFACE;
657 IUnknown_AddRef((IUnknown*)*ppv);
658 return S_OK;
661 static ULONG WINAPI HttpProtocolUnk_AddRef(IUnknown *iface)
663 HttpProtocol *This = impl_from_IUnknown(iface);
664 LONG ref = InterlockedIncrement(&This->ref);
665 TRACE("(%p) ref=%ld\n", This, ref);
666 return ref;
669 static ULONG WINAPI HttpProtocolUnk_Release(IUnknown *iface)
671 HttpProtocol *This = impl_from_IUnknown(iface);
672 LONG ref = InterlockedDecrement(&This->ref);
674 TRACE("(%p) ref=%ld\n", This, ref);
676 if(!ref) {
677 protocol_close_connection(&This->base);
678 free(This);
680 URLMON_UnlockModule();
683 return ref;
686 static const IUnknownVtbl HttpProtocolUnkVtbl = {
687 HttpProtocolUnk_QueryInterface,
688 HttpProtocolUnk_AddRef,
689 HttpProtocolUnk_Release
692 static HRESULT WINAPI HttpProtocol_QueryInterface(IInternetProtocolEx *iface, REFIID riid, void **ppv)
694 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
695 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
696 return IUnknown_QueryInterface(This->outer, riid, ppv);
699 static ULONG WINAPI HttpProtocol_AddRef(IInternetProtocolEx *iface)
701 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
702 TRACE("(%p)\n", This);
703 return IUnknown_AddRef(This->outer);
706 static ULONG WINAPI HttpProtocol_Release(IInternetProtocolEx *iface)
708 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
709 TRACE("(%p)\n", This);
710 return IUnknown_Release(This->outer);
713 static HRESULT WINAPI HttpProtocol_Start(IInternetProtocolEx *iface, LPCWSTR szUrl,
714 IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
715 DWORD grfPI, HANDLE_PTR dwReserved)
717 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
718 IUri *uri;
719 HRESULT hres;
721 TRACE("(%p)->(%s %p %p %08lx %Ix)\n", This, debugstr_w(szUrl), pOIProtSink,
722 pOIBindInfo, grfPI, dwReserved);
724 hres = CreateUri(szUrl, 0, 0, &uri);
725 if(FAILED(hres))
726 return hres;
728 hres = IInternetProtocolEx_StartEx(&This->IInternetProtocolEx_iface, uri, pOIProtSink,
729 pOIBindInfo, grfPI, (HANDLE*)dwReserved);
731 IUri_Release(uri);
732 return hres;
735 static HRESULT WINAPI HttpProtocol_Continue(IInternetProtocolEx *iface, PROTOCOLDATA *pProtocolData)
737 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
739 TRACE("(%p)->(%p)\n", This, pProtocolData);
741 return protocol_continue(&This->base, pProtocolData);
744 static HRESULT WINAPI HttpProtocol_Abort(IInternetProtocolEx *iface, HRESULT hrReason,
745 DWORD dwOptions)
747 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
749 TRACE("(%p)->(%08lx %08lx)\n", This, hrReason, dwOptions);
751 return protocol_abort(&This->base, hrReason);
754 static HRESULT WINAPI HttpProtocol_Terminate(IInternetProtocolEx *iface, DWORD dwOptions)
756 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
758 TRACE("(%p)->(%08lx)\n", This, dwOptions);
760 protocol_close_connection(&This->base);
761 return S_OK;
764 static HRESULT WINAPI HttpProtocol_Suspend(IInternetProtocolEx *iface)
766 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
767 FIXME("(%p)\n", This);
768 return E_NOTIMPL;
771 static HRESULT WINAPI HttpProtocol_Resume(IInternetProtocolEx *iface)
773 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
774 FIXME("(%p)\n", This);
775 return E_NOTIMPL;
778 static HRESULT WINAPI HttpProtocol_Read(IInternetProtocolEx *iface, void *pv,
779 ULONG cb, ULONG *pcbRead)
781 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
783 TRACE("(%p)->(%p %lu %p)\n", This, pv, cb, pcbRead);
785 return protocol_read(&This->base, pv, cb, pcbRead);
788 static HRESULT WINAPI HttpProtocol_Seek(IInternetProtocolEx *iface, LARGE_INTEGER dlibMove,
789 DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
791 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
792 FIXME("(%p)->(%ld %ld %p)\n", This, dlibMove.LowPart, dwOrigin, plibNewPosition);
793 return E_NOTIMPL;
796 static HRESULT WINAPI HttpProtocol_LockRequest(IInternetProtocolEx *iface, DWORD dwOptions)
798 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
800 TRACE("(%p)->(%08lx)\n", This, dwOptions);
802 return protocol_lock_request(&This->base);
805 static HRESULT WINAPI HttpProtocol_UnlockRequest(IInternetProtocolEx *iface)
807 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
809 TRACE("(%p)\n", This);
811 return protocol_unlock_request(&This->base);
814 static HRESULT WINAPI HttpProtocol_StartEx(IInternetProtocolEx *iface, IUri *pUri,
815 IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
816 DWORD grfPI, HANDLE *dwReserved)
818 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
819 DWORD scheme = 0;
820 HRESULT hres;
822 TRACE("(%p)->(%p %p %p %08lx %p)\n", This, pUri, pOIProtSink,
823 pOIBindInfo, grfPI, dwReserved);
825 hres = IUri_GetScheme(pUri, &scheme);
826 if(FAILED(hres))
827 return hres;
828 if(scheme != (This->https ? URL_SCHEME_HTTPS : URL_SCHEME_HTTP))
829 return MK_E_SYNTAX;
831 return protocol_start(&This->base, (IInternetProtocol*)&This->IInternetProtocolEx_iface, pUri,
832 pOIProtSink, pOIBindInfo);
835 static const IInternetProtocolExVtbl HttpProtocolVtbl = {
836 HttpProtocol_QueryInterface,
837 HttpProtocol_AddRef,
838 HttpProtocol_Release,
839 HttpProtocol_Start,
840 HttpProtocol_Continue,
841 HttpProtocol_Abort,
842 HttpProtocol_Terminate,
843 HttpProtocol_Suspend,
844 HttpProtocol_Resume,
845 HttpProtocol_Read,
846 HttpProtocol_Seek,
847 HttpProtocol_LockRequest,
848 HttpProtocol_UnlockRequest,
849 HttpProtocol_StartEx
852 static HRESULT WINAPI HttpPriority_QueryInterface(IInternetPriority *iface, REFIID riid, void **ppv)
854 HttpProtocol *This = impl_from_IInternetPriority(iface);
855 return IInternetProtocolEx_QueryInterface(&This->IInternetProtocolEx_iface, riid, ppv);
858 static ULONG WINAPI HttpPriority_AddRef(IInternetPriority *iface)
860 HttpProtocol *This = impl_from_IInternetPriority(iface);
861 return IInternetProtocolEx_AddRef(&This->IInternetProtocolEx_iface);
864 static ULONG WINAPI HttpPriority_Release(IInternetPriority *iface)
866 HttpProtocol *This = impl_from_IInternetPriority(iface);
867 return IInternetProtocolEx_Release(&This->IInternetProtocolEx_iface);
870 static HRESULT WINAPI HttpPriority_SetPriority(IInternetPriority *iface, LONG nPriority)
872 HttpProtocol *This = impl_from_IInternetPriority(iface);
874 TRACE("(%p)->(%ld)\n", This, nPriority);
876 This->base.priority = nPriority;
877 return S_OK;
880 static HRESULT WINAPI HttpPriority_GetPriority(IInternetPriority *iface, LONG *pnPriority)
882 HttpProtocol *This = impl_from_IInternetPriority(iface);
884 TRACE("(%p)->(%p)\n", This, pnPriority);
886 *pnPriority = This->base.priority;
887 return S_OK;
890 static const IInternetPriorityVtbl HttpPriorityVtbl = {
891 HttpPriority_QueryInterface,
892 HttpPriority_AddRef,
893 HttpPriority_Release,
894 HttpPriority_SetPriority,
895 HttpPriority_GetPriority
898 static HRESULT WINAPI HttpInfo_QueryInterface(IWinInetHttpInfo *iface, REFIID riid, void **ppv)
900 HttpProtocol *This = impl_from_IWinInetHttpInfo(iface);
901 return IInternetProtocolEx_QueryInterface(&This->IInternetProtocolEx_iface, riid, ppv);
904 static ULONG WINAPI HttpInfo_AddRef(IWinInetHttpInfo *iface)
906 HttpProtocol *This = impl_from_IWinInetHttpInfo(iface);
907 return IInternetProtocolEx_AddRef(&This->IInternetProtocolEx_iface);
910 static ULONG WINAPI HttpInfo_Release(IWinInetHttpInfo *iface)
912 HttpProtocol *This = impl_from_IWinInetHttpInfo(iface);
913 return IInternetProtocolEx_Release(&This->IInternetProtocolEx_iface);
916 static HRESULT WINAPI HttpInfo_QueryOption(IWinInetHttpInfo *iface, DWORD dwOption,
917 void *pBuffer, DWORD *pcbBuffer)
919 HttpProtocol *This = impl_from_IWinInetHttpInfo(iface);
920 TRACE("(%p)->(%lx %p %p)\n", This, dwOption, pBuffer, pcbBuffer);
922 if(!This->base.request)
923 return E_FAIL;
925 if(!InternetQueryOptionW(This->base.request, dwOption, pBuffer, pcbBuffer))
926 return S_FALSE;
927 return S_OK;
930 static HRESULT WINAPI HttpInfo_QueryInfo(IWinInetHttpInfo *iface, DWORD dwOption,
931 void *pBuffer, DWORD *pcbBuffer, DWORD *pdwFlags, DWORD *pdwReserved)
933 HttpProtocol *This = impl_from_IWinInetHttpInfo(iface);
934 TRACE("(%p)->(%lx %p %p %p %p)\n", This, dwOption, pBuffer, pcbBuffer, pdwFlags, pdwReserved);
936 if(!This->base.request)
937 return E_FAIL;
939 if(!HttpQueryInfoA(This->base.request, dwOption, pBuffer, pcbBuffer, pdwFlags))
940 return S_FALSE;
942 return S_OK;
945 static const IWinInetHttpInfoVtbl WinInetHttpInfoVtbl = {
946 HttpInfo_QueryInterface,
947 HttpInfo_AddRef,
948 HttpInfo_Release,
949 HttpInfo_QueryOption,
950 HttpInfo_QueryInfo
953 static HRESULT create_http_protocol(BOOL https, IUnknown *outer, void **ppobj)
955 HttpProtocol *ret;
957 ret = calloc(1, sizeof(HttpProtocol));
958 if(!ret)
959 return E_OUTOFMEMORY;
961 ret->base.vtbl = &AsyncProtocolVtbl;
962 ret->IUnknown_inner.lpVtbl = &HttpProtocolUnkVtbl;
963 ret->IInternetProtocolEx_iface.lpVtbl = &HttpProtocolVtbl;
964 ret->IInternetPriority_iface.lpVtbl = &HttpPriorityVtbl;
965 ret->IWinInetHttpInfo_iface.lpVtbl = &WinInetHttpInfoVtbl;
967 ret->https = https;
968 ret->ref = 1;
969 ret->outer = outer ? outer : &ret->IUnknown_inner;
971 *ppobj = &ret->IUnknown_inner;
973 URLMON_LockModule();
974 return S_OK;
977 HRESULT HttpProtocol_Construct(IUnknown *outer, void **ppv)
979 TRACE("(%p %p)\n", outer, ppv);
981 return create_http_protocol(FALSE, outer, ppv);
984 HRESULT HttpSProtocol_Construct(IUnknown *outer, void **ppv)
986 TRACE("(%p %p)\n", outer, ppv);
988 return create_http_protocol(TRUE, outer, ppv);