wined3d: Get rid of the "render_to_fbo" field from the wined3d_swapchain structure.
[wine.git] / dlls / urlmon / http.c
blobbee226bd4743a282e1892fd080501ce1dc7a9a0a
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 #define NONAMELESSUNION
22 #include "urlmon_main.h"
23 #include "wininet.h"
25 #define NO_SHLWAPI_REG
26 #include "shlwapi.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
32 typedef struct {
33 Protocol base;
35 IUnknown IUnknown_inner;
36 IInternetProtocolEx IInternetProtocolEx_iface;
37 IInternetPriority IInternetPriority_iface;
38 IWinInetHttpInfo IWinInetHttpInfo_iface;
40 BOOL https;
41 IHttpNegotiate *http_negotiate;
42 WCHAR *full_header;
44 LONG ref;
45 IUnknown *outer;
46 } HttpProtocol;
48 static inline HttpProtocol *impl_from_IUnknown(IUnknown *iface)
50 return CONTAINING_RECORD(iface, HttpProtocol, IUnknown_inner);
53 static inline HttpProtocol *impl_from_IInternetProtocolEx(IInternetProtocolEx *iface)
55 return CONTAINING_RECORD(iface, HttpProtocol, IInternetProtocolEx_iface);
58 static inline HttpProtocol *impl_from_IInternetPriority(IInternetPriority *iface)
60 return CONTAINING_RECORD(iface, HttpProtocol, IInternetPriority_iface);
63 static inline HttpProtocol *impl_from_IWinInetHttpInfo(IWinInetHttpInfo *iface)
65 return CONTAINING_RECORD(iface, HttpProtocol, IWinInetHttpInfo_iface);
68 static const WCHAR default_headersW[] = L"Accept-Encoding: gzip, deflate";
70 static LPWSTR query_http_info(HttpProtocol *This, DWORD option)
72 LPWSTR ret = NULL;
73 DWORD len = 0;
74 BOOL res;
76 res = HttpQueryInfoW(This->base.request, option, NULL, &len, NULL);
77 if (!res && GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
78 ret = heap_alloc(len);
79 res = HttpQueryInfoW(This->base.request, option, ret, &len, NULL);
81 if(!res) {
82 TRACE("HttpQueryInfoW(%d) failed: %08x\n", option, GetLastError());
83 heap_free(ret);
84 return NULL;
87 return ret;
90 static inline BOOL set_security_flag(HttpProtocol *This, DWORD flags)
92 BOOL res;
94 res = InternetSetOptionW(This->base.request, INTERNET_OPTION_SECURITY_FLAGS, &flags, sizeof(flags));
95 if(!res)
96 ERR("Failed to set security flags: %x\n", flags);
98 return res;
101 static inline HRESULT internet_error_to_hres(DWORD error)
103 switch(error)
105 case ERROR_INTERNET_SEC_CERT_DATE_INVALID:
106 case ERROR_INTERNET_SEC_CERT_CN_INVALID:
107 case ERROR_INTERNET_INVALID_CA:
108 case ERROR_INTERNET_CLIENT_AUTH_CERT_NEEDED:
109 case ERROR_INTERNET_SEC_INVALID_CERT:
110 case ERROR_INTERNET_SEC_CERT_ERRORS:
111 case ERROR_INTERNET_SEC_CERT_REV_FAILED:
112 case ERROR_INTERNET_SEC_CERT_NO_REV:
113 case ERROR_INTERNET_SEC_CERT_REVOKED:
114 return INET_E_INVALID_CERTIFICATE;
115 case ERROR_INTERNET_HTTP_TO_HTTPS_ON_REDIR:
116 case ERROR_INTERNET_HTTPS_TO_HTTP_ON_REDIR:
117 case ERROR_HTTP_REDIRECT_NEEDS_CONFIRMATION:
118 return INET_E_REDIRECT_FAILED;
119 default:
120 return INET_E_DOWNLOAD_FAILURE;
124 static HRESULT handle_http_error(HttpProtocol *This, DWORD error)
126 IServiceProvider *serv_prov;
127 IWindowForBindingUI *wfb_ui;
128 IHttpSecurity *http_security;
129 BOOL security_problem;
130 DWORD dlg_flags;
131 HWND hwnd;
132 DWORD res;
133 HRESULT hres;
135 TRACE("(%p %u)\n", This, error);
137 switch(error) {
138 case ERROR_INTERNET_SEC_CERT_DATE_INVALID:
139 case ERROR_INTERNET_SEC_CERT_CN_INVALID:
140 case ERROR_INTERNET_HTTP_TO_HTTPS_ON_REDIR:
141 case ERROR_INTERNET_HTTPS_TO_HTTP_ON_REDIR:
142 case ERROR_INTERNET_INVALID_CA:
143 case ERROR_INTERNET_CLIENT_AUTH_CERT_NEEDED:
144 case ERROR_INTERNET_SEC_INVALID_CERT:
145 case ERROR_INTERNET_SEC_CERT_ERRORS:
146 case ERROR_INTERNET_SEC_CERT_REV_FAILED:
147 case ERROR_INTERNET_SEC_CERT_NO_REV:
148 case ERROR_INTERNET_SEC_CERT_REVOKED:
149 case ERROR_HTTP_REDIRECT_NEEDS_CONFIRMATION:
150 security_problem = TRUE;
151 break;
152 default:
153 security_problem = FALSE;
156 hres = IInternetProtocolSink_QueryInterface(This->base.protocol_sink, &IID_IServiceProvider,
157 (void**)&serv_prov);
158 if(FAILED(hres)) {
159 ERR("Failed to get IServiceProvider.\n");
160 return E_ABORT;
163 if(security_problem) {
164 hres = IServiceProvider_QueryService(serv_prov, &IID_IHttpSecurity, &IID_IHttpSecurity,
165 (void**)&http_security);
166 if(SUCCEEDED(hres)) {
167 hres = IHttpSecurity_OnSecurityProblem(http_security, error);
168 IHttpSecurity_Release(http_security);
170 TRACE("OnSecurityProblem returned %08x\n", hres);
172 if(hres != S_FALSE)
174 BOOL res = FALSE;
176 IServiceProvider_Release(serv_prov);
178 if(hres == S_OK) {
179 if(error == ERROR_INTERNET_SEC_CERT_DATE_INVALID)
180 res = set_security_flag(This, SECURITY_FLAG_IGNORE_CERT_DATE_INVALID);
181 else if(error == ERROR_INTERNET_SEC_CERT_CN_INVALID)
182 res = set_security_flag(This, SECURITY_FLAG_IGNORE_CERT_CN_INVALID);
183 else if(error == ERROR_INTERNET_INVALID_CA)
184 res = set_security_flag(This, SECURITY_FLAG_IGNORE_UNKNOWN_CA);
186 if(res)
187 return RPC_E_RETRY;
189 FIXME("Don't know how to ignore error %d\n", error);
190 return E_ABORT;
193 if(hres == E_ABORT)
194 return E_ABORT;
195 if(hres == RPC_E_RETRY)
196 return RPC_E_RETRY;
198 return internet_error_to_hres(error);
203 switch(error) {
204 case ERROR_INTERNET_SEC_CERT_REV_FAILED:
205 if(hres != S_FALSE) {
206 /* Silently ignore the error. We will get more detailed error from wininet anyway. */
207 set_security_flag(This, SECURITY_FLAG_IGNORE_REVOCATION);
208 hres = RPC_E_RETRY;
209 break;
211 /* fallthrough */
212 default:
213 hres = IServiceProvider_QueryService(serv_prov, &IID_IWindowForBindingUI, &IID_IWindowForBindingUI, (void**)&wfb_ui);
214 if(SUCCEEDED(hres)) {
215 const IID *iid_reason;
217 if(security_problem)
218 iid_reason = &IID_IHttpSecurity;
219 else if(error == ERROR_INTERNET_INCORRECT_PASSWORD)
220 iid_reason = &IID_IAuthenticate;
221 else
222 iid_reason = &IID_IWindowForBindingUI;
224 hres = IWindowForBindingUI_GetWindow(wfb_ui, iid_reason, &hwnd);
225 IWindowForBindingUI_Release(wfb_ui);
228 if(FAILED(hres)) hwnd = NULL;
230 dlg_flags = FLAGS_ERROR_UI_FLAGS_CHANGE_OPTIONS | FLAGS_ERROR_UI_FLAGS_GENERATE_DATA;
231 if(This->base.bindf & BINDF_NO_UI)
232 dlg_flags |= FLAGS_ERROR_UI_FLAGS_NO_UI;
234 res = InternetErrorDlg(hwnd, This->base.request, error, dlg_flags, NULL);
235 hres = res == ERROR_INTERNET_FORCE_RETRY || res == ERROR_SUCCESS ? RPC_E_RETRY : internet_error_to_hres(error);
238 IServiceProvider_Release(serv_prov);
239 return hres;
242 static ULONG send_http_request(HttpProtocol *This)
244 INTERNET_BUFFERSW send_buffer = {sizeof(INTERNET_BUFFERSW)};
245 BOOL res;
247 send_buffer.lpcszHeader = This->full_header;
248 send_buffer.dwHeadersLength = send_buffer.dwHeadersTotal = lstrlenW(This->full_header);
250 if(This->base.bind_info.dwBindVerb != BINDVERB_GET) {
251 switch(This->base.bind_info.stgmedData.tymed) {
252 case TYMED_HGLOBAL:
253 /* Native does not use GlobalLock/GlobalUnlock, so we won't either */
254 send_buffer.lpvBuffer = This->base.bind_info.stgmedData.u.hGlobal;
255 send_buffer.dwBufferLength = send_buffer.dwBufferTotal = This->base.bind_info.cbstgmedData;
256 break;
257 case TYMED_ISTREAM: {
258 LARGE_INTEGER offset;
260 send_buffer.dwBufferTotal = This->base.bind_info.cbstgmedData;
261 if(!This->base.post_stream) {
262 This->base.post_stream = This->base.bind_info.stgmedData.u.pstm;
263 IStream_AddRef(This->base.post_stream);
266 offset.QuadPart = 0;
267 IStream_Seek(This->base.post_stream, offset, STREAM_SEEK_SET, NULL);
268 break;
270 default:
271 FIXME("Unsupported This->base.bind_info.stgmedData.tymed %d\n", This->base.bind_info.stgmedData.tymed);
275 if(This->base.post_stream)
276 res = HttpSendRequestExW(This->base.request, &send_buffer, NULL, 0, 0);
277 else
278 res = HttpSendRequestW(This->base.request, send_buffer.lpcszHeader, send_buffer.dwHeadersLength,
279 send_buffer.lpvBuffer, send_buffer.dwBufferLength);
281 return res ? 0 : GetLastError();
284 static inline HttpProtocol *impl_from_Protocol(Protocol *prot)
286 return CONTAINING_RECORD(prot, HttpProtocol, base);
289 static HRESULT HttpProtocol_open_request(Protocol *prot, IUri *uri, DWORD request_flags,
290 HINTERNET internet_session, IInternetBindInfo *bind_info)
292 HttpProtocol *This = impl_from_Protocol(prot);
293 WCHAR *addl_header = NULL, *post_cookie = NULL, *rootdoc_url = NULL;
294 IServiceProvider *service_provider = NULL;
295 IHttpNegotiate2 *http_negotiate2 = NULL;
296 BSTR url, host, user, pass, path;
297 LPOLESTR accept_mimes[257];
298 const WCHAR **accept_types;
299 BYTE security_id[512];
300 DWORD len, port, flags;
301 ULONG num, error;
302 BOOL res, b;
303 HRESULT hres;
305 static const WCHAR wszBindVerb[BINDVERB_CUSTOM][5] =
306 {L"GET",
307 L"POST",
308 L"PUT"};
310 hres = IUri_GetPort(uri, &port);
311 if(FAILED(hres))
312 return hres;
314 hres = IUri_GetHost(uri, &host);
315 if(FAILED(hres))
316 return hres;
318 hres = IUri_GetUserName(uri, &user);
319 if(SUCCEEDED(hres)) {
320 hres = IUri_GetPassword(uri, &pass);
322 if(SUCCEEDED(hres)) {
323 This->base.connection = InternetConnectW(internet_session, host, port, user, pass,
324 INTERNET_SERVICE_HTTP, This->https ? INTERNET_FLAG_SECURE : 0, (DWORD_PTR)&This->base);
325 SysFreeString(pass);
327 SysFreeString(user);
329 SysFreeString(host);
330 if(FAILED(hres))
331 return hres;
332 if(!This->base.connection) {
333 WARN("InternetConnect failed: %d\n", GetLastError());
334 return INET_E_CANNOT_CONNECT;
337 num = 0;
338 hres = IInternetBindInfo_GetBindString(bind_info, BINDSTRING_ROOTDOC_URL, &rootdoc_url, 1, &num);
339 if(hres == S_OK && num) {
340 FIXME("Use root doc URL %s\n", debugstr_w(rootdoc_url));
341 CoTaskMemFree(rootdoc_url);
344 num = ARRAY_SIZE(accept_mimes) - 1;
345 hres = IInternetBindInfo_GetBindString(bind_info, BINDSTRING_ACCEPT_MIMES, accept_mimes, num, &num);
346 if(hres == INET_E_USE_DEFAULT_SETTING) {
347 static const WCHAR *default_accept_mimes[] = {L"*/*", NULL};
349 accept_types = default_accept_mimes;
350 num = 0;
351 }else if(hres == S_OK) {
352 accept_types = (const WCHAR**)accept_mimes;
353 }else {
354 WARN("GetBindString BINDSTRING_ACCEPT_MIMES failed: %08x\n", hres);
355 return INET_E_NO_VALID_MEDIA;
357 accept_mimes[num] = 0;
359 if(This->https)
360 request_flags |= INTERNET_FLAG_SECURE;
362 hres = IUri_GetPathAndQuery(uri, &path);
363 if(SUCCEEDED(hres)) {
364 This->base.request = HttpOpenRequestW(This->base.connection,
365 This->base.bind_info.dwBindVerb < BINDVERB_CUSTOM
366 ? wszBindVerb[This->base.bind_info.dwBindVerb] : This->base.bind_info.szCustomVerb,
367 path, NULL, NULL, accept_types, request_flags, (DWORD_PTR)&This->base);
368 SysFreeString(path);
370 while(num--)
371 CoTaskMemFree(accept_mimes[num]);
372 if(FAILED(hres))
373 return hres;
374 if (!This->base.request) {
375 WARN("HttpOpenRequest failed: %d\n", GetLastError());
376 return INET_E_RESOURCE_NOT_FOUND;
379 hres = IInternetProtocolSink_QueryInterface(This->base.protocol_sink, &IID_IServiceProvider,
380 (void **)&service_provider);
381 if (hres != S_OK) {
382 WARN("IInternetProtocolSink_QueryInterface IID_IServiceProvider failed: %08x\n", hres);
383 return hres;
386 hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate,
387 &IID_IHttpNegotiate, (void **)&This->http_negotiate);
388 if (hres != S_OK) {
389 WARN("IServiceProvider_QueryService IID_IHttpNegotiate failed: %08x\n", hres);
390 IServiceProvider_Release(service_provider);
391 return hres;
394 hres = IUri_GetAbsoluteUri(uri, &url);
395 if(FAILED(hres)) {
396 IServiceProvider_Release(service_provider);
397 return hres;
400 hres = IHttpNegotiate_BeginningTransaction(This->http_negotiate, url, default_headersW,
401 0, &addl_header);
402 SysFreeString(url);
403 if(hres != S_OK) {
404 WARN("IHttpNegotiate_BeginningTransaction failed: %08x\n", hres);
405 IServiceProvider_Release(service_provider);
406 return hres;
409 len = addl_header ? lstrlenW(addl_header) : 0;
411 This->full_header = heap_alloc(len*sizeof(WCHAR)+sizeof(default_headersW));
412 if(!This->full_header) {
413 IServiceProvider_Release(service_provider);
414 return E_OUTOFMEMORY;
417 if(len)
418 memcpy(This->full_header, addl_header, len*sizeof(WCHAR));
419 CoTaskMemFree(addl_header);
420 memcpy(This->full_header+len, default_headersW, sizeof(default_headersW));
422 hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate2,
423 &IID_IHttpNegotiate2, (void **)&http_negotiate2);
424 IServiceProvider_Release(service_provider);
425 if(hres != S_OK) {
426 WARN("IServiceProvider_QueryService IID_IHttpNegotiate2 failed: %08x\n", hres);
427 /* No goto done as per native */
428 }else {
429 len = ARRAY_SIZE(security_id);
430 hres = IHttpNegotiate2_GetRootSecurityId(http_negotiate2, security_id, &len, 0);
431 IHttpNegotiate2_Release(http_negotiate2);
432 if (hres != S_OK)
433 WARN("IHttpNegotiate2_GetRootSecurityId failed: %08x\n", hres);
436 /* FIXME: Handle security_id. Native calls undocumented function IsHostInProxyBypassList. */
438 if(This->base.bind_info.dwBindVerb == BINDVERB_POST) {
439 num = 0;
440 hres = IInternetBindInfo_GetBindString(bind_info, BINDSTRING_POST_COOKIE, &post_cookie, 1, &num);
441 if(hres == S_OK && num) {
442 if(!InternetSetOptionW(This->base.request, INTERNET_OPTION_SECONDARY_CACHE_KEY,
443 post_cookie, lstrlenW(post_cookie)))
444 WARN("InternetSetOption INTERNET_OPTION_SECONDARY_CACHE_KEY failed: %d\n", GetLastError());
445 CoTaskMemFree(post_cookie);
449 flags = INTERNET_ERROR_MASK_COMBINED_SEC_CERT;
450 res = InternetSetOptionW(This->base.request, INTERNET_OPTION_ERROR_MASK, &flags, sizeof(flags));
451 if(!res)
452 WARN("InternetSetOption(INTERNET_OPTION_ERROR_MASK) failed: %u\n", GetLastError());
454 b = TRUE;
455 res = InternetSetOptionW(This->base.request, INTERNET_OPTION_HTTP_DECODING, &b, sizeof(b));
456 if(!res)
457 WARN("InternetSetOption(INTERNET_OPTION_HTTP_DECODING) failed: %u\n", GetLastError());
459 do {
460 error = send_http_request(This);
462 switch(error) {
463 case ERROR_IO_PENDING:
464 return S_OK;
465 case ERROR_SUCCESS:
467 * If sending response ended synchronously, it means that we have the whole data
468 * available locally (most likely in cache).
470 return protocol_syncbinding(&This->base);
471 default:
472 hres = handle_http_error(This, error);
474 } while(hres == RPC_E_RETRY);
476 WARN("HttpSendRequest failed: %d\n", error);
477 return hres;
480 static HRESULT HttpProtocol_end_request(Protocol *protocol)
482 BOOL res;
484 res = HttpEndRequestW(protocol->request, NULL, 0, 0);
485 if(!res && GetLastError() != ERROR_IO_PENDING) {
486 FIXME("HttpEndRequest failed: %u\n", GetLastError());
487 return E_FAIL;
490 return S_OK;
493 static BOOL is_redirect_response(DWORD status_code)
495 switch(status_code) {
496 case HTTP_STATUS_REDIRECT:
497 case HTTP_STATUS_MOVED:
498 case HTTP_STATUS_REDIRECT_KEEP_VERB:
499 case HTTP_STATUS_REDIRECT_METHOD:
500 return TRUE;
502 return FALSE;
505 static HRESULT HttpProtocol_start_downloading(Protocol *prot)
507 HttpProtocol *This = impl_from_Protocol(prot);
508 LPWSTR content_type, content_length, ranges;
509 DWORD len = sizeof(DWORD);
510 DWORD status_code;
511 BOOL res;
512 HRESULT hres;
514 if(!This->http_negotiate) {
515 WARN("Expected IHttpNegotiate pointer to be non-NULL\n");
516 return S_OK;
519 res = HttpQueryInfoW(This->base.request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER,
520 &status_code, &len, NULL);
521 if(res) {
522 WCHAR *response_headers;
524 if((This->base.bind_info.dwOptions & BINDINFO_OPTIONS_DISABLEAUTOREDIRECTS) && is_redirect_response(status_code)) {
525 WCHAR *location;
527 TRACE("Got redirect with disabled auto redirects\n");
529 location = query_http_info(This, HTTP_QUERY_LOCATION);
530 This->base.flags |= FLAG_RESULT_REPORTED | FLAG_LAST_DATA_REPORTED;
531 IInternetProtocolSink_ReportResult(This->base.protocol_sink, INET_E_REDIRECT_FAILED, 0, location);
532 heap_free(location);
533 return INET_E_REDIRECT_FAILED;
536 response_headers = query_http_info(This, HTTP_QUERY_RAW_HEADERS_CRLF);
537 if(response_headers) {
538 hres = IHttpNegotiate_OnResponse(This->http_negotiate, status_code, response_headers,
539 NULL, NULL);
540 heap_free(response_headers);
541 if (hres != S_OK) {
542 WARN("IHttpNegotiate_OnResponse failed: %08x\n", hres);
543 return S_OK;
546 }else {
547 WARN("HttpQueryInfo failed: %d\n", GetLastError());
550 ranges = query_http_info(This, HTTP_QUERY_ACCEPT_RANGES);
551 if(ranges) {
552 IInternetProtocolSink_ReportProgress(This->base.protocol_sink, BINDSTATUS_ACCEPTRANGES, NULL);
553 heap_free(ranges);
556 content_type = query_http_info(This, HTTP_QUERY_CONTENT_TYPE);
557 if(content_type) {
558 /* remove the charset, if present */
559 LPWSTR p = wcschr(content_type, ';');
560 if (p) *p = '\0';
562 IInternetProtocolSink_ReportProgress(This->base.protocol_sink,
563 (This->base.bindf & BINDF_FROMURLMON)
564 ? BINDSTATUS_MIMETYPEAVAILABLE : BINDSTATUS_RAWMIMETYPE,
565 content_type);
566 heap_free(content_type);
567 }else {
568 WARN("HttpQueryInfo failed: %d\n", GetLastError());
569 IInternetProtocolSink_ReportProgress(This->base.protocol_sink,
570 (This->base.bindf & BINDF_FROMURLMON)
571 ? BINDSTATUS_MIMETYPEAVAILABLE : BINDSTATUS_RAWMIMETYPE, L"text/html");
574 content_length = query_http_info(This, HTTP_QUERY_CONTENT_LENGTH);
575 if(content_length) {
576 This->base.content_length = wcstol(content_length, NULL, 10);
577 heap_free(content_length);
580 return S_OK;
583 static void HttpProtocol_close_connection(Protocol *prot)
585 HttpProtocol *This = impl_from_Protocol(prot);
587 if(This->http_negotiate) {
588 IHttpNegotiate_Release(This->http_negotiate);
589 This->http_negotiate = NULL;
592 heap_free(This->full_header);
593 This->full_header = NULL;
596 static void HttpProtocol_on_error(Protocol *prot, DWORD error)
598 HttpProtocol *This = impl_from_Protocol(prot);
599 HRESULT hres;
601 TRACE("(%p) %d\n", prot, error);
603 if(prot->flags & FLAG_FIRST_CONTINUE_COMPLETE) {
604 FIXME("Not handling error %d\n", error);
605 return;
608 while((hres = handle_http_error(This, error)) == RPC_E_RETRY) {
609 error = send_http_request(This);
611 if(error == ERROR_IO_PENDING || error == ERROR_SUCCESS)
612 return;
615 protocol_abort(prot, hres);
616 protocol_close_connection(prot);
617 return;
620 static const ProtocolVtbl AsyncProtocolVtbl = {
621 HttpProtocol_open_request,
622 HttpProtocol_end_request,
623 HttpProtocol_start_downloading,
624 HttpProtocol_close_connection,
625 HttpProtocol_on_error
628 static HRESULT WINAPI HttpProtocolUnk_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
630 HttpProtocol *This = impl_from_IUnknown(iface);
632 if(IsEqualGUID(&IID_IUnknown, riid)) {
633 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
634 *ppv = &This->IUnknown_inner;
635 }else if(IsEqualGUID(&IID_IInternetProtocolRoot, riid)) {
636 TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", This, ppv);
637 *ppv = &This->IInternetProtocolEx_iface;
638 }else if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
639 TRACE("(%p)->(IID_IInternetProtocol %p)\n", This, ppv);
640 *ppv = &This->IInternetProtocolEx_iface;
641 }else if(IsEqualGUID(&IID_IInternetProtocolEx, riid)) {
642 TRACE("(%p)->(IID_IInternetProtocolEx %p)\n", This, ppv);
643 *ppv = &This->IInternetProtocolEx_iface;
644 }else if(IsEqualGUID(&IID_IInternetPriority, riid)) {
645 TRACE("(%p)->(IID_IInternetPriority %p)\n", This, ppv);
646 *ppv = &This->IInternetPriority_iface;
647 }else if(IsEqualGUID(&IID_IWinInetInfo, riid)) {
648 TRACE("(%p)->(IID_IWinInetInfo %p)\n", This, ppv);
649 *ppv = &This->IWinInetHttpInfo_iface;
650 }else if(IsEqualGUID(&IID_IWinInetHttpInfo, riid)) {
651 TRACE("(%p)->(IID_IWinInetHttpInfo %p)\n", This, ppv);
652 *ppv = &This->IWinInetHttpInfo_iface;
653 }else {
654 *ppv = NULL;
655 WARN("not supported interface %s\n", debugstr_guid(riid));
656 return E_NOINTERFACE;
659 IUnknown_AddRef((IUnknown*)*ppv);
660 return S_OK;
663 static ULONG WINAPI HttpProtocolUnk_AddRef(IUnknown *iface)
665 HttpProtocol *This = impl_from_IUnknown(iface);
666 LONG ref = InterlockedIncrement(&This->ref);
667 TRACE("(%p) ref=%d\n", This, ref);
668 return ref;
671 static ULONG WINAPI HttpProtocolUnk_Release(IUnknown *iface)
673 HttpProtocol *This = impl_from_IUnknown(iface);
674 LONG ref = InterlockedDecrement(&This->ref);
676 TRACE("(%p) ref=%d\n", This, ref);
678 if(!ref) {
679 protocol_close_connection(&This->base);
680 heap_free(This);
682 URLMON_UnlockModule();
685 return ref;
688 static const IUnknownVtbl HttpProtocolUnkVtbl = {
689 HttpProtocolUnk_QueryInterface,
690 HttpProtocolUnk_AddRef,
691 HttpProtocolUnk_Release
694 static HRESULT WINAPI HttpProtocol_QueryInterface(IInternetProtocolEx *iface, REFIID riid, void **ppv)
696 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
697 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
698 return IUnknown_QueryInterface(This->outer, riid, ppv);
701 static ULONG WINAPI HttpProtocol_AddRef(IInternetProtocolEx *iface)
703 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
704 TRACE("(%p)\n", This);
705 return IUnknown_AddRef(This->outer);
708 static ULONG WINAPI HttpProtocol_Release(IInternetProtocolEx *iface)
710 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
711 TRACE("(%p)\n", This);
712 return IUnknown_Release(This->outer);
715 static HRESULT WINAPI HttpProtocol_Start(IInternetProtocolEx *iface, LPCWSTR szUrl,
716 IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
717 DWORD grfPI, HANDLE_PTR dwReserved)
719 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
720 IUri *uri;
721 HRESULT hres;
723 TRACE("(%p)->(%s %p %p %08x %lx)\n", This, debugstr_w(szUrl), pOIProtSink,
724 pOIBindInfo, grfPI, dwReserved);
726 hres = CreateUri(szUrl, 0, 0, &uri);
727 if(FAILED(hres))
728 return hres;
730 hres = IInternetProtocolEx_StartEx(&This->IInternetProtocolEx_iface, uri, pOIProtSink,
731 pOIBindInfo, grfPI, (HANDLE*)dwReserved);
733 IUri_Release(uri);
734 return hres;
737 static HRESULT WINAPI HttpProtocol_Continue(IInternetProtocolEx *iface, PROTOCOLDATA *pProtocolData)
739 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
741 TRACE("(%p)->(%p)\n", This, pProtocolData);
743 return protocol_continue(&This->base, pProtocolData);
746 static HRESULT WINAPI HttpProtocol_Abort(IInternetProtocolEx *iface, HRESULT hrReason,
747 DWORD dwOptions)
749 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
751 TRACE("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);
753 return protocol_abort(&This->base, hrReason);
756 static HRESULT WINAPI HttpProtocol_Terminate(IInternetProtocolEx *iface, DWORD dwOptions)
758 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
760 TRACE("(%p)->(%08x)\n", This, dwOptions);
762 protocol_close_connection(&This->base);
763 return S_OK;
766 static HRESULT WINAPI HttpProtocol_Suspend(IInternetProtocolEx *iface)
768 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
769 FIXME("(%p)\n", This);
770 return E_NOTIMPL;
773 static HRESULT WINAPI HttpProtocol_Resume(IInternetProtocolEx *iface)
775 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
776 FIXME("(%p)\n", This);
777 return E_NOTIMPL;
780 static HRESULT WINAPI HttpProtocol_Read(IInternetProtocolEx *iface, void *pv,
781 ULONG cb, ULONG *pcbRead)
783 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
785 TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
787 return protocol_read(&This->base, pv, cb, pcbRead);
790 static HRESULT WINAPI HttpProtocol_Seek(IInternetProtocolEx *iface, LARGE_INTEGER dlibMove,
791 DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
793 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
794 FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
795 return E_NOTIMPL;
798 static HRESULT WINAPI HttpProtocol_LockRequest(IInternetProtocolEx *iface, DWORD dwOptions)
800 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
802 TRACE("(%p)->(%08x)\n", This, dwOptions);
804 return protocol_lock_request(&This->base);
807 static HRESULT WINAPI HttpProtocol_UnlockRequest(IInternetProtocolEx *iface)
809 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
811 TRACE("(%p)\n", This);
813 return protocol_unlock_request(&This->base);
816 static HRESULT WINAPI HttpProtocol_StartEx(IInternetProtocolEx *iface, IUri *pUri,
817 IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
818 DWORD grfPI, HANDLE *dwReserved)
820 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
821 DWORD scheme = 0;
822 HRESULT hres;
824 TRACE("(%p)->(%p %p %p %08x %p)\n", This, pUri, pOIProtSink,
825 pOIBindInfo, grfPI, dwReserved);
827 hres = IUri_GetScheme(pUri, &scheme);
828 if(FAILED(hres))
829 return hres;
830 if(scheme != (This->https ? URL_SCHEME_HTTPS : URL_SCHEME_HTTP))
831 return MK_E_SYNTAX;
833 return protocol_start(&This->base, (IInternetProtocol*)&This->IInternetProtocolEx_iface, pUri,
834 pOIProtSink, pOIBindInfo);
837 static const IInternetProtocolExVtbl HttpProtocolVtbl = {
838 HttpProtocol_QueryInterface,
839 HttpProtocol_AddRef,
840 HttpProtocol_Release,
841 HttpProtocol_Start,
842 HttpProtocol_Continue,
843 HttpProtocol_Abort,
844 HttpProtocol_Terminate,
845 HttpProtocol_Suspend,
846 HttpProtocol_Resume,
847 HttpProtocol_Read,
848 HttpProtocol_Seek,
849 HttpProtocol_LockRequest,
850 HttpProtocol_UnlockRequest,
851 HttpProtocol_StartEx
854 static HRESULT WINAPI HttpPriority_QueryInterface(IInternetPriority *iface, REFIID riid, void **ppv)
856 HttpProtocol *This = impl_from_IInternetPriority(iface);
857 return IInternetProtocolEx_QueryInterface(&This->IInternetProtocolEx_iface, riid, ppv);
860 static ULONG WINAPI HttpPriority_AddRef(IInternetPriority *iface)
862 HttpProtocol *This = impl_from_IInternetPriority(iface);
863 return IInternetProtocolEx_AddRef(&This->IInternetProtocolEx_iface);
866 static ULONG WINAPI HttpPriority_Release(IInternetPriority *iface)
868 HttpProtocol *This = impl_from_IInternetPriority(iface);
869 return IInternetProtocolEx_Release(&This->IInternetProtocolEx_iface);
872 static HRESULT WINAPI HttpPriority_SetPriority(IInternetPriority *iface, LONG nPriority)
874 HttpProtocol *This = impl_from_IInternetPriority(iface);
876 TRACE("(%p)->(%d)\n", This, nPriority);
878 This->base.priority = nPriority;
879 return S_OK;
882 static HRESULT WINAPI HttpPriority_GetPriority(IInternetPriority *iface, LONG *pnPriority)
884 HttpProtocol *This = impl_from_IInternetPriority(iface);
886 TRACE("(%p)->(%p)\n", This, pnPriority);
888 *pnPriority = This->base.priority;
889 return S_OK;
892 static const IInternetPriorityVtbl HttpPriorityVtbl = {
893 HttpPriority_QueryInterface,
894 HttpPriority_AddRef,
895 HttpPriority_Release,
896 HttpPriority_SetPriority,
897 HttpPriority_GetPriority
900 static HRESULT WINAPI HttpInfo_QueryInterface(IWinInetHttpInfo *iface, REFIID riid, void **ppv)
902 HttpProtocol *This = impl_from_IWinInetHttpInfo(iface);
903 return IInternetProtocolEx_QueryInterface(&This->IInternetProtocolEx_iface, riid, ppv);
906 static ULONG WINAPI HttpInfo_AddRef(IWinInetHttpInfo *iface)
908 HttpProtocol *This = impl_from_IWinInetHttpInfo(iface);
909 return IInternetProtocolEx_AddRef(&This->IInternetProtocolEx_iface);
912 static ULONG WINAPI HttpInfo_Release(IWinInetHttpInfo *iface)
914 HttpProtocol *This = impl_from_IWinInetHttpInfo(iface);
915 return IInternetProtocolEx_Release(&This->IInternetProtocolEx_iface);
918 static HRESULT WINAPI HttpInfo_QueryOption(IWinInetHttpInfo *iface, DWORD dwOption,
919 void *pBuffer, DWORD *pcbBuffer)
921 HttpProtocol *This = impl_from_IWinInetHttpInfo(iface);
922 TRACE("(%p)->(%x %p %p)\n", This, dwOption, pBuffer, pcbBuffer);
924 if(!This->base.request)
925 return E_FAIL;
927 if(!InternetQueryOptionW(This->base.request, dwOption, pBuffer, pcbBuffer))
928 return S_FALSE;
929 return S_OK;
932 static HRESULT WINAPI HttpInfo_QueryInfo(IWinInetHttpInfo *iface, DWORD dwOption,
933 void *pBuffer, DWORD *pcbBuffer, DWORD *pdwFlags, DWORD *pdwReserved)
935 HttpProtocol *This = impl_from_IWinInetHttpInfo(iface);
936 TRACE("(%p)->(%x %p %p %p %p)\n", This, dwOption, pBuffer, pcbBuffer, pdwFlags, pdwReserved);
938 if(!This->base.request)
939 return E_FAIL;
941 if(!HttpQueryInfoA(This->base.request, dwOption, pBuffer, pcbBuffer, pdwFlags))
942 return S_FALSE;
944 return S_OK;
947 static const IWinInetHttpInfoVtbl WinInetHttpInfoVtbl = {
948 HttpInfo_QueryInterface,
949 HttpInfo_AddRef,
950 HttpInfo_Release,
951 HttpInfo_QueryOption,
952 HttpInfo_QueryInfo
955 static HRESULT create_http_protocol(BOOL https, IUnknown *outer, void **ppobj)
957 HttpProtocol *ret;
959 ret = heap_alloc_zero(sizeof(HttpProtocol));
960 if(!ret)
961 return E_OUTOFMEMORY;
963 ret->base.vtbl = &AsyncProtocolVtbl;
964 ret->IUnknown_inner.lpVtbl = &HttpProtocolUnkVtbl;
965 ret->IInternetProtocolEx_iface.lpVtbl = &HttpProtocolVtbl;
966 ret->IInternetPriority_iface.lpVtbl = &HttpPriorityVtbl;
967 ret->IWinInetHttpInfo_iface.lpVtbl = &WinInetHttpInfoVtbl;
969 ret->https = https;
970 ret->ref = 1;
971 ret->outer = outer ? outer : &ret->IUnknown_inner;
973 *ppobj = &ret->IUnknown_inner;
975 URLMON_LockModule();
976 return S_OK;
979 HRESULT HttpProtocol_Construct(IUnknown *outer, void **ppv)
981 TRACE("(%p %p)\n", outer, ppv);
983 return create_http_protocol(FALSE, outer, ppv);
986 HRESULT HttpSProtocol_Construct(IUnknown *outer, void **ppv)
988 TRACE("(%p %p)\n", outer, ppv);
990 return create_http_protocol(TRUE, outer, ppv);