winex11: Fix ctrl-<symbol> to generate codes below 0x20 where necessary.
[wine/multimedia.git] / dlls / urlmon / http.c
blobb0c7d2acf0175afddaa1b25143e7ff5612ca1010
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 IInternetProtocolEx IInternetProtocolEx_iface;
36 IInternetPriority IInternetPriority_iface;
37 IWinInetHttpInfo IWinInetHttpInfo_iface;
39 BOOL https;
40 IHttpNegotiate *http_negotiate;
41 WCHAR *full_header;
43 LONG ref;
44 } HttpProtocol;
46 static inline HttpProtocol *impl_from_IInternetProtocolEx(IInternetProtocolEx *iface)
48 return CONTAINING_RECORD(iface, HttpProtocol, IInternetProtocolEx_iface);
51 static inline HttpProtocol *impl_from_IInternetPriority(IInternetPriority *iface)
53 return CONTAINING_RECORD(iface, HttpProtocol, IInternetPriority_iface);
56 static inline HttpProtocol *impl_from_IWinInetHttpInfo(IWinInetHttpInfo *iface)
58 return CONTAINING_RECORD(iface, HttpProtocol, IWinInetHttpInfo_iface);
61 static const WCHAR default_headersW[] = {
62 '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};
64 static LPWSTR query_http_info(HttpProtocol *This, DWORD option)
66 LPWSTR ret = NULL;
67 DWORD len = 0;
68 BOOL res;
70 res = HttpQueryInfoW(This->base.request, option, NULL, &len, NULL);
71 if (!res && GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
72 ret = heap_alloc(len);
73 res = HttpQueryInfoW(This->base.request, option, ret, &len, NULL);
75 if(!res) {
76 TRACE("HttpQueryInfoW(%d) failed: %08x\n", option, GetLastError());
77 heap_free(ret);
78 return NULL;
81 return ret;
84 static inline BOOL set_security_flag(HttpProtocol *This, DWORD flags)
86 BOOL res;
88 res = InternetSetOptionW(This->base.request, INTERNET_OPTION_SECURITY_FLAGS, &flags, sizeof(flags));
89 if(!res)
90 ERR("Failed to set security flags: %x\n", flags);
92 return res;
95 static inline HRESULT internet_error_to_hres(DWORD error)
97 switch(error)
99 case ERROR_INTERNET_SEC_CERT_DATE_INVALID:
100 case ERROR_INTERNET_SEC_CERT_CN_INVALID:
101 case ERROR_INTERNET_INVALID_CA:
102 case ERROR_INTERNET_CLIENT_AUTH_CERT_NEEDED:
103 case ERROR_INTERNET_SEC_INVALID_CERT:
104 case ERROR_INTERNET_SEC_CERT_ERRORS:
105 case ERROR_INTERNET_SEC_CERT_REV_FAILED:
106 case ERROR_INTERNET_SEC_CERT_NO_REV:
107 case ERROR_INTERNET_SEC_CERT_REVOKED:
108 return INET_E_INVALID_CERTIFICATE;
109 case ERROR_INTERNET_HTTP_TO_HTTPS_ON_REDIR:
110 case ERROR_INTERNET_HTTPS_TO_HTTP_ON_REDIR:
111 case ERROR_HTTP_REDIRECT_NEEDS_CONFIRMATION:
112 return INET_E_REDIRECT_FAILED;
113 default:
114 return INET_E_DOWNLOAD_FAILURE;
118 static HRESULT handle_http_error(HttpProtocol *This, DWORD error)
120 IServiceProvider *serv_prov;
121 IWindowForBindingUI *wfb_ui;
122 IHttpSecurity *http_security;
123 BOOL security_problem;
124 DWORD dlg_flags;
125 HWND hwnd;
126 DWORD res;
127 HRESULT hres;
129 TRACE("(%p %u)\n", This, error);
131 switch(error) {
132 case ERROR_INTERNET_SEC_CERT_DATE_INVALID:
133 case ERROR_INTERNET_SEC_CERT_CN_INVALID:
134 case ERROR_INTERNET_HTTP_TO_HTTPS_ON_REDIR:
135 case ERROR_INTERNET_HTTPS_TO_HTTP_ON_REDIR:
136 case ERROR_INTERNET_INVALID_CA:
137 case ERROR_INTERNET_CLIENT_AUTH_CERT_NEEDED:
138 case ERROR_INTERNET_SEC_INVALID_CERT:
139 case ERROR_INTERNET_SEC_CERT_ERRORS:
140 case ERROR_INTERNET_SEC_CERT_REV_FAILED:
141 case ERROR_INTERNET_SEC_CERT_NO_REV:
142 case ERROR_INTERNET_SEC_CERT_REVOKED:
143 case ERROR_HTTP_REDIRECT_NEEDS_CONFIRMATION:
144 security_problem = TRUE;
145 break;
146 default:
147 security_problem = FALSE;
150 hres = IInternetProtocolSink_QueryInterface(This->base.protocol_sink, &IID_IServiceProvider,
151 (void**)&serv_prov);
152 if(FAILED(hres)) {
153 ERR("Failed to get IServiceProvider.\n");
154 return E_ABORT;
157 if(security_problem) {
158 hres = IServiceProvider_QueryService(serv_prov, &IID_IHttpSecurity, &IID_IHttpSecurity,
159 (void**)&http_security);
160 if(SUCCEEDED(hres)) {
161 hres = IHttpSecurity_OnSecurityProblem(http_security, error);
162 IHttpSecurity_Release(http_security);
164 TRACE("OnSecurityProblem returned %08x\n", hres);
166 if(hres != S_FALSE)
168 BOOL res = FALSE;
170 IServiceProvider_Release(serv_prov);
172 if(hres == S_OK) {
173 if(error == ERROR_INTERNET_SEC_CERT_DATE_INVALID)
174 res = set_security_flag(This, SECURITY_FLAG_IGNORE_CERT_DATE_INVALID);
175 else if(error == ERROR_INTERNET_SEC_CERT_CN_INVALID)
176 res = set_security_flag(This, SECURITY_FLAG_IGNORE_CERT_CN_INVALID);
177 else if(error == ERROR_INTERNET_INVALID_CA)
178 res = set_security_flag(This, SECURITY_FLAG_IGNORE_UNKNOWN_CA);
180 if(res)
181 return RPC_E_RETRY;
183 FIXME("Don't know how to ignore error %d\n", error);
184 return E_ABORT;
187 if(hres == E_ABORT)
188 return E_ABORT;
189 if(hres == RPC_E_RETRY)
190 return RPC_E_RETRY;
192 return internet_error_to_hres(error);
197 switch(error) {
198 case ERROR_INTERNET_SEC_CERT_REV_FAILED:
199 if(hres != S_FALSE) {
200 /* Silently ignore the error. We will get more detailed error from wininet anyway. */
201 set_security_flag(This, SECURITY_FLAG_IGNORE_REVOCATION);
202 hres = RPC_E_RETRY;
203 break;
205 /* fallthrough */
206 default:
207 hres = IServiceProvider_QueryService(serv_prov, &IID_IWindowForBindingUI, &IID_IWindowForBindingUI, (void**)&wfb_ui);
208 if(SUCCEEDED(hres)) {
209 const IID *iid_reason;
211 if(security_problem)
212 iid_reason = &IID_IHttpSecurity;
213 else if(error == ERROR_INTERNET_INCORRECT_PASSWORD)
214 iid_reason = &IID_IAuthenticate;
215 else
216 iid_reason = &IID_IWindowForBindingUI;
218 hres = IWindowForBindingUI_GetWindow(wfb_ui, iid_reason, &hwnd);
219 IWindowForBindingUI_Release(wfb_ui);
222 if(FAILED(hres)) hwnd = NULL;
224 dlg_flags = FLAGS_ERROR_UI_FLAGS_CHANGE_OPTIONS | FLAGS_ERROR_UI_FLAGS_GENERATE_DATA;
225 if(This->base.bindf & BINDF_NO_UI)
226 dlg_flags |= FLAGS_ERROR_UI_FLAGS_NO_UI;
228 res = InternetErrorDlg(hwnd, This->base.request, error, dlg_flags, NULL);
229 hres = res == ERROR_INTERNET_FORCE_RETRY || res == ERROR_SUCCESS ? RPC_E_RETRY : internet_error_to_hres(error);
232 IServiceProvider_Release(serv_prov);
233 return hres;
236 static ULONG send_http_request(HttpProtocol *This)
238 INTERNET_BUFFERSW send_buffer = {sizeof(INTERNET_BUFFERSW)};
239 BOOL res;
241 send_buffer.lpcszHeader = This->full_header;
242 send_buffer.dwHeadersLength = send_buffer.dwHeadersTotal = strlenW(This->full_header);
244 if(This->base.bind_info.dwBindVerb != BINDVERB_GET) {
245 switch(This->base.bind_info.stgmedData.tymed) {
246 case TYMED_HGLOBAL:
247 /* Native does not use GlobalLock/GlobalUnlock, so we won't either */
248 send_buffer.lpvBuffer = This->base.bind_info.stgmedData.u.hGlobal;
249 send_buffer.dwBufferLength = send_buffer.dwBufferTotal = This->base.bind_info.cbstgmedData;
250 break;
251 case TYMED_ISTREAM: {
252 LARGE_INTEGER offset;
254 send_buffer.dwBufferTotal = This->base.bind_info.cbstgmedData;
255 if(!This->base.post_stream) {
256 This->base.post_stream = This->base.bind_info.stgmedData.u.pstm;
257 IStream_AddRef(This->base.post_stream);
260 offset.QuadPart = 0;
261 IStream_Seek(This->base.post_stream, offset, STREAM_SEEK_SET, NULL);
262 break;
264 default:
265 FIXME("Unsupported This->base.bind_info.stgmedData.tymed %d\n", This->base.bind_info.stgmedData.tymed);
269 if(This->base.post_stream)
270 res = HttpSendRequestExW(This->base.request, &send_buffer, NULL, 0, 0);
271 else
272 res = HttpSendRequestW(This->base.request, send_buffer.lpcszHeader, send_buffer.dwHeadersLength,
273 send_buffer.lpvBuffer, send_buffer.dwBufferLength);
275 return res ? 0 : GetLastError();
278 static inline HttpProtocol *impl_from_Protocol(Protocol *prot)
280 return CONTAINING_RECORD(prot, HttpProtocol, base);
283 static HRESULT HttpProtocol_open_request(Protocol *prot, IUri *uri, DWORD request_flags,
284 HINTERNET internet_session, IInternetBindInfo *bind_info)
286 HttpProtocol *This = impl_from_Protocol(prot);
287 WCHAR *addl_header = NULL, *post_cookie = NULL, *rootdoc_url = NULL;
288 IServiceProvider *service_provider = NULL;
289 IHttpNegotiate2 *http_negotiate2 = NULL;
290 BSTR url, host, user, pass, path;
291 LPOLESTR accept_mimes[257];
292 const WCHAR **accept_types;
293 BYTE security_id[512];
294 DWORD len, port, flags;
295 ULONG num, error;
296 BOOL res, b;
297 HRESULT hres;
299 static const WCHAR wszBindVerb[BINDVERB_CUSTOM][5] =
300 {{'G','E','T',0},
301 {'P','O','S','T',0},
302 {'P','U','T',0}};
304 hres = IUri_GetPort(uri, &port);
305 if(FAILED(hres))
306 return hres;
308 hres = IUri_GetHost(uri, &host);
309 if(FAILED(hres))
310 return hres;
312 hres = IUri_GetUserName(uri, &user);
313 if(SUCCEEDED(hres)) {
314 hres = IUri_GetPassword(uri, &pass);
316 if(SUCCEEDED(hres)) {
317 This->base.connection = InternetConnectW(internet_session, host, port, user, pass,
318 INTERNET_SERVICE_HTTP, This->https ? INTERNET_FLAG_SECURE : 0, (DWORD_PTR)&This->base);
319 SysFreeString(pass);
321 SysFreeString(user);
323 SysFreeString(host);
324 if(FAILED(hres))
325 return hres;
326 if(!This->base.connection) {
327 WARN("InternetConnect failed: %d\n", GetLastError());
328 return INET_E_CANNOT_CONNECT;
331 num = 0;
332 hres = IInternetBindInfo_GetBindString(bind_info, BINDSTRING_ROOTDOC_URL, &rootdoc_url, 1, &num);
333 if(hres == S_OK && num) {
334 FIXME("Use root doc URL %s\n", debugstr_w(rootdoc_url));
335 CoTaskMemFree(rootdoc_url);
338 num = sizeof(accept_mimes)/sizeof(accept_mimes[0])-1;
339 hres = IInternetBindInfo_GetBindString(bind_info, BINDSTRING_ACCEPT_MIMES, accept_mimes, num, &num);
340 if(hres == INET_E_USE_DEFAULT_SETTING) {
341 static const WCHAR default_accept_mimeW[] = {'*','/','*',0};
342 static const WCHAR *default_accept_mimes[] = {default_accept_mimeW, NULL};
344 accept_types = default_accept_mimes;
345 num = 0;
346 }else if(hres == S_OK) {
347 accept_types = (const WCHAR**)accept_mimes;
348 }else {
349 WARN("GetBindString BINDSTRING_ACCEPT_MIMES failed: %08x\n", hres);
350 return INET_E_NO_VALID_MEDIA;
352 accept_mimes[num] = 0;
354 if(This->https)
355 request_flags |= INTERNET_FLAG_SECURE;
357 hres = IUri_GetPathAndQuery(uri, &path);
358 if(SUCCEEDED(hres)) {
359 This->base.request = HttpOpenRequestW(This->base.connection,
360 This->base.bind_info.dwBindVerb < BINDVERB_CUSTOM
361 ? wszBindVerb[This->base.bind_info.dwBindVerb] : This->base.bind_info.szCustomVerb,
362 path, NULL, NULL, accept_types, request_flags, (DWORD_PTR)&This->base);
363 SysFreeString(path);
365 while(num--)
366 CoTaskMemFree(accept_mimes[num]);
367 if(FAILED(hres))
368 return hres;
369 if (!This->base.request) {
370 WARN("HttpOpenRequest failed: %d\n", GetLastError());
371 return INET_E_RESOURCE_NOT_FOUND;
374 hres = IInternetProtocolSink_QueryInterface(This->base.protocol_sink, &IID_IServiceProvider,
375 (void **)&service_provider);
376 if (hres != S_OK) {
377 WARN("IInternetProtocolSink_QueryInterface IID_IServiceProvider failed: %08x\n", hres);
378 return hres;
381 hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate,
382 &IID_IHttpNegotiate, (void **)&This->http_negotiate);
383 if (hres != S_OK) {
384 WARN("IServiceProvider_QueryService IID_IHttpNegotiate failed: %08x\n", hres);
385 IServiceProvider_Release(service_provider);
386 return hres;
389 hres = IUri_GetAbsoluteUri(uri, &url);
390 if(FAILED(hres)) {
391 IServiceProvider_Release(service_provider);
392 return hres;
395 hres = IHttpNegotiate_BeginningTransaction(This->http_negotiate, url, default_headersW,
396 0, &addl_header);
397 SysFreeString(url);
398 if(hres != S_OK) {
399 WARN("IHttpNegotiate_BeginningTransaction failed: %08x\n", hres);
400 IServiceProvider_Release(service_provider);
401 return hres;
404 len = addl_header ? strlenW(addl_header) : 0;
406 This->full_header = heap_alloc(len*sizeof(WCHAR)+sizeof(default_headersW));
407 if(!This->full_header) {
408 IServiceProvider_Release(service_provider);
409 return E_OUTOFMEMORY;
412 if(len)
413 memcpy(This->full_header, addl_header, len*sizeof(WCHAR));
414 CoTaskMemFree(addl_header);
415 memcpy(This->full_header+len, default_headersW, sizeof(default_headersW));
417 hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate2,
418 &IID_IHttpNegotiate2, (void **)&http_negotiate2);
419 IServiceProvider_Release(service_provider);
420 if(hres != S_OK) {
421 WARN("IServiceProvider_QueryService IID_IHttpNegotiate2 failed: %08x\n", hres);
422 /* No goto done as per native */
423 }else {
424 len = sizeof(security_id)/sizeof(security_id[0]);
425 hres = IHttpNegotiate2_GetRootSecurityId(http_negotiate2, security_id, &len, 0);
426 IHttpNegotiate2_Release(http_negotiate2);
427 if (hres != S_OK)
428 WARN("IHttpNegotiate2_GetRootSecurityId failed: %08x\n", hres);
431 /* FIXME: Handle security_id. Native calls undocumented function IsHostInProxyBypassList. */
433 if(This->base.bind_info.dwBindVerb == BINDVERB_POST) {
434 num = 0;
435 hres = IInternetBindInfo_GetBindString(bind_info, BINDSTRING_POST_COOKIE, &post_cookie, 1, &num);
436 if(hres == S_OK && num) {
437 if(!InternetSetOptionW(This->base.request, INTERNET_OPTION_SECONDARY_CACHE_KEY,
438 post_cookie, lstrlenW(post_cookie)))
439 WARN("InternetSetOption INTERNET_OPTION_SECONDARY_CACHE_KEY failed: %d\n", GetLastError());
440 CoTaskMemFree(post_cookie);
444 flags = INTERNET_ERROR_MASK_COMBINED_SEC_CERT;
445 res = InternetSetOptionW(This->base.request, INTERNET_OPTION_ERROR_MASK, &flags, sizeof(flags));
446 if(!res)
447 WARN("InternetSetOption(INTERNET_OPTION_ERROR_MASK) failed: %u\n", GetLastError());
449 b = TRUE;
450 res = InternetSetOptionW(This->base.request, INTERNET_OPTION_HTTP_DECODING, &b, sizeof(b));
451 if(!res)
452 WARN("InternetSetOption(INTERNET_OPTION_HTTP_DECODING) failed: %u\n", GetLastError());
454 do {
455 error = send_http_request(This);
457 switch(error) {
458 case ERROR_IO_PENDING:
459 return S_OK;
460 case ERROR_SUCCESS:
462 * If sending response ended synchronously, it means that we have the whole data
463 * available locally (most likely in cache).
465 return protocol_syncbinding(&This->base);
466 default:
467 hres = handle_http_error(This, error);
469 } while(hres == RPC_E_RETRY);
471 WARN("HttpSendRequest failed: %d\n", error);
472 return hres;
475 static HRESULT HttpProtocol_end_request(Protocol *protocol)
477 BOOL res;
479 res = HttpEndRequestW(protocol->request, NULL, 0, 0);
480 if(!res && GetLastError() != ERROR_IO_PENDING) {
481 FIXME("HttpEndRequest failed: %u\n", GetLastError());
482 return E_FAIL;
485 return S_OK;
488 static HRESULT HttpProtocol_start_downloading(Protocol *prot)
490 HttpProtocol *This = impl_from_Protocol(prot);
491 LPWSTR content_type, content_length, ranges;
492 DWORD len = sizeof(DWORD);
493 DWORD status_code;
494 BOOL res;
495 HRESULT hres;
497 static const WCHAR wszDefaultContentType[] =
498 {'t','e','x','t','/','h','t','m','l',0};
500 if(!This->http_negotiate) {
501 WARN("Expected IHttpNegotiate pointer to be non-NULL\n");
502 return S_OK;
505 res = HttpQueryInfoW(This->base.request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER,
506 &status_code, &len, NULL);
507 if(res) {
508 LPWSTR response_headers = query_http_info(This, HTTP_QUERY_RAW_HEADERS_CRLF);
509 if(response_headers) {
510 hres = IHttpNegotiate_OnResponse(This->http_negotiate, status_code, response_headers,
511 NULL, NULL);
512 heap_free(response_headers);
513 if (hres != S_OK) {
514 WARN("IHttpNegotiate_OnResponse failed: %08x\n", hres);
515 return S_OK;
518 }else {
519 WARN("HttpQueryInfo failed: %d\n", GetLastError());
522 ranges = query_http_info(This, HTTP_QUERY_ACCEPT_RANGES);
523 if(ranges) {
524 IInternetProtocolSink_ReportProgress(This->base.protocol_sink, BINDSTATUS_ACCEPTRANGES, NULL);
525 heap_free(ranges);
528 content_type = query_http_info(This, HTTP_QUERY_CONTENT_TYPE);
529 if(content_type) {
530 /* remove the charset, if present */
531 LPWSTR p = strchrW(content_type, ';');
532 if (p) *p = '\0';
534 IInternetProtocolSink_ReportProgress(This->base.protocol_sink,
535 (This->base.bindf & BINDF_FROMURLMON)
536 ? BINDSTATUS_MIMETYPEAVAILABLE : BINDSTATUS_RAWMIMETYPE,
537 content_type);
538 heap_free(content_type);
539 }else {
540 WARN("HttpQueryInfo failed: %d\n", GetLastError());
541 IInternetProtocolSink_ReportProgress(This->base.protocol_sink,
542 (This->base.bindf & BINDF_FROMURLMON)
543 ? BINDSTATUS_MIMETYPEAVAILABLE : BINDSTATUS_RAWMIMETYPE,
544 wszDefaultContentType);
547 content_length = query_http_info(This, HTTP_QUERY_CONTENT_LENGTH);
548 if(content_length) {
549 This->base.content_length = atoiW(content_length);
550 heap_free(content_length);
553 return S_OK;
556 static void HttpProtocol_close_connection(Protocol *prot)
558 HttpProtocol *This = impl_from_Protocol(prot);
560 if(This->http_negotiate) {
561 IHttpNegotiate_Release(This->http_negotiate);
562 This->http_negotiate = NULL;
565 heap_free(This->full_header);
566 This->full_header = NULL;
569 static void HttpProtocol_on_error(Protocol *prot, DWORD error)
571 HttpProtocol *This = impl_from_Protocol(prot);
572 HRESULT hres;
574 TRACE("(%p) %d\n", prot, error);
576 if(prot->flags & FLAG_FIRST_CONTINUE_COMPLETE) {
577 FIXME("Not handling error %d\n", error);
578 return;
581 while((hres = handle_http_error(This, error)) == RPC_E_RETRY) {
582 error = send_http_request(This);
584 if(error == ERROR_IO_PENDING || error == ERROR_SUCCESS)
585 return;
588 protocol_abort(prot, hres);
589 protocol_close_connection(prot);
590 return;
593 static const ProtocolVtbl AsyncProtocolVtbl = {
594 HttpProtocol_open_request,
595 HttpProtocol_end_request,
596 HttpProtocol_start_downloading,
597 HttpProtocol_close_connection,
598 HttpProtocol_on_error
601 static HRESULT WINAPI HttpProtocol_QueryInterface(IInternetProtocolEx *iface, REFIID riid, void **ppv)
603 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
605 *ppv = NULL;
606 if(IsEqualGUID(&IID_IUnknown, riid)) {
607 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
608 *ppv = &This->IInternetProtocolEx_iface;
609 }else if(IsEqualGUID(&IID_IInternetProtocolRoot, riid)) {
610 TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", This, ppv);
611 *ppv = &This->IInternetProtocolEx_iface;
612 }else if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
613 TRACE("(%p)->(IID_IInternetProtocol %p)\n", This, ppv);
614 *ppv = &This->IInternetProtocolEx_iface;
615 }else if(IsEqualGUID(&IID_IInternetProtocolEx, riid)) {
616 TRACE("(%p)->(IID_IInternetProtocolEx %p)\n", This, ppv);
617 *ppv = &This->IInternetProtocolEx_iface;
618 }else if(IsEqualGUID(&IID_IInternetPriority, riid)) {
619 TRACE("(%p)->(IID_IInternetPriority %p)\n", This, ppv);
620 *ppv = &This->IInternetPriority_iface;
621 }else if(IsEqualGUID(&IID_IWinInetInfo, riid)) {
622 TRACE("(%p)->(IID_IWinInetInfo %p)\n", This, ppv);
623 *ppv = &This->IWinInetHttpInfo_iface;
624 }else if(IsEqualGUID(&IID_IWinInetHttpInfo, riid)) {
625 TRACE("(%p)->(IID_IWinInetHttpInfo %p)\n", This, ppv);
626 *ppv = &This->IWinInetHttpInfo_iface;
629 if(*ppv) {
630 IInternetProtocolEx_AddRef(iface);
631 return S_OK;
634 WARN("not supported interface %s\n", debugstr_guid(riid));
635 return E_NOINTERFACE;
638 static ULONG WINAPI HttpProtocol_AddRef(IInternetProtocolEx *iface)
640 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
641 LONG ref = InterlockedIncrement(&This->ref);
642 TRACE("(%p) ref=%d\n", This, ref);
643 return ref;
646 static ULONG WINAPI HttpProtocol_Release(IInternetProtocolEx *iface)
648 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
649 LONG ref = InterlockedDecrement(&This->ref);
651 TRACE("(%p) ref=%d\n", This, ref);
653 if(!ref) {
654 protocol_close_connection(&This->base);
655 heap_free(This);
657 URLMON_UnlockModule();
660 return ref;
663 static HRESULT WINAPI HttpProtocol_Start(IInternetProtocolEx *iface, LPCWSTR szUrl,
664 IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
665 DWORD grfPI, HANDLE_PTR dwReserved)
667 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
668 IUri *uri;
669 HRESULT hres;
671 TRACE("(%p)->(%s %p %p %08x %lx)\n", This, debugstr_w(szUrl), pOIProtSink,
672 pOIBindInfo, grfPI, dwReserved);
674 hres = CreateUri(szUrl, 0, 0, &uri);
675 if(FAILED(hres))
676 return hres;
678 hres = IInternetProtocolEx_StartEx(&This->IInternetProtocolEx_iface, uri, pOIProtSink,
679 pOIBindInfo, grfPI, (HANDLE*)dwReserved);
681 IUri_Release(uri);
682 return hres;
685 static HRESULT WINAPI HttpProtocol_Continue(IInternetProtocolEx *iface, PROTOCOLDATA *pProtocolData)
687 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
689 TRACE("(%p)->(%p)\n", This, pProtocolData);
691 return protocol_continue(&This->base, pProtocolData);
694 static HRESULT WINAPI HttpProtocol_Abort(IInternetProtocolEx *iface, HRESULT hrReason,
695 DWORD dwOptions)
697 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
699 TRACE("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);
701 return protocol_abort(&This->base, hrReason);
704 static HRESULT WINAPI HttpProtocol_Terminate(IInternetProtocolEx *iface, DWORD dwOptions)
706 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
708 TRACE("(%p)->(%08x)\n", This, dwOptions);
710 protocol_close_connection(&This->base);
711 return S_OK;
714 static HRESULT WINAPI HttpProtocol_Suspend(IInternetProtocolEx *iface)
716 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
717 FIXME("(%p)\n", This);
718 return E_NOTIMPL;
721 static HRESULT WINAPI HttpProtocol_Resume(IInternetProtocolEx *iface)
723 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
724 FIXME("(%p)\n", This);
725 return E_NOTIMPL;
728 static HRESULT WINAPI HttpProtocol_Read(IInternetProtocolEx *iface, void *pv,
729 ULONG cb, ULONG *pcbRead)
731 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
733 TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
735 return protocol_read(&This->base, pv, cb, pcbRead);
738 static HRESULT WINAPI HttpProtocol_Seek(IInternetProtocolEx *iface, LARGE_INTEGER dlibMove,
739 DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
741 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
742 FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
743 return E_NOTIMPL;
746 static HRESULT WINAPI HttpProtocol_LockRequest(IInternetProtocolEx *iface, DWORD dwOptions)
748 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
750 TRACE("(%p)->(%08x)\n", This, dwOptions);
752 return protocol_lock_request(&This->base);
755 static HRESULT WINAPI HttpProtocol_UnlockRequest(IInternetProtocolEx *iface)
757 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
759 TRACE("(%p)\n", This);
761 return protocol_unlock_request(&This->base);
764 static HRESULT WINAPI HttpProtocol_StartEx(IInternetProtocolEx *iface, IUri *pUri,
765 IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
766 DWORD grfPI, HANDLE *dwReserved)
768 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
769 DWORD scheme = 0;
770 HRESULT hres;
772 TRACE("(%p)->(%p %p %p %08x %p)\n", This, pUri, pOIProtSink,
773 pOIBindInfo, grfPI, dwReserved);
775 hres = IUri_GetScheme(pUri, &scheme);
776 if(FAILED(hres))
777 return hres;
778 if(scheme != (This->https ? URL_SCHEME_HTTPS : URL_SCHEME_HTTP))
779 return MK_E_SYNTAX;
781 return protocol_start(&This->base, (IInternetProtocol*)&This->IInternetProtocolEx_iface, pUri,
782 pOIProtSink, pOIBindInfo);
785 static const IInternetProtocolExVtbl HttpProtocolVtbl = {
786 HttpProtocol_QueryInterface,
787 HttpProtocol_AddRef,
788 HttpProtocol_Release,
789 HttpProtocol_Start,
790 HttpProtocol_Continue,
791 HttpProtocol_Abort,
792 HttpProtocol_Terminate,
793 HttpProtocol_Suspend,
794 HttpProtocol_Resume,
795 HttpProtocol_Read,
796 HttpProtocol_Seek,
797 HttpProtocol_LockRequest,
798 HttpProtocol_UnlockRequest,
799 HttpProtocol_StartEx
802 static HRESULT WINAPI HttpPriority_QueryInterface(IInternetPriority *iface, REFIID riid, void **ppv)
804 HttpProtocol *This = impl_from_IInternetPriority(iface);
805 return IInternetProtocolEx_QueryInterface(&This->IInternetProtocolEx_iface, riid, ppv);
808 static ULONG WINAPI HttpPriority_AddRef(IInternetPriority *iface)
810 HttpProtocol *This = impl_from_IInternetPriority(iface);
811 return IInternetProtocolEx_AddRef(&This->IInternetProtocolEx_iface);
814 static ULONG WINAPI HttpPriority_Release(IInternetPriority *iface)
816 HttpProtocol *This = impl_from_IInternetPriority(iface);
817 return IInternetProtocolEx_Release(&This->IInternetProtocolEx_iface);
820 static HRESULT WINAPI HttpPriority_SetPriority(IInternetPriority *iface, LONG nPriority)
822 HttpProtocol *This = impl_from_IInternetPriority(iface);
824 TRACE("(%p)->(%d)\n", This, nPriority);
826 This->base.priority = nPriority;
827 return S_OK;
830 static HRESULT WINAPI HttpPriority_GetPriority(IInternetPriority *iface, LONG *pnPriority)
832 HttpProtocol *This = impl_from_IInternetPriority(iface);
834 TRACE("(%p)->(%p)\n", This, pnPriority);
836 *pnPriority = This->base.priority;
837 return S_OK;
840 static const IInternetPriorityVtbl HttpPriorityVtbl = {
841 HttpPriority_QueryInterface,
842 HttpPriority_AddRef,
843 HttpPriority_Release,
844 HttpPriority_SetPriority,
845 HttpPriority_GetPriority
848 static HRESULT WINAPI HttpInfo_QueryInterface(IWinInetHttpInfo *iface, REFIID riid, void **ppv)
850 HttpProtocol *This = impl_from_IWinInetHttpInfo(iface);
851 return IInternetProtocolEx_QueryInterface(&This->IInternetProtocolEx_iface, riid, ppv);
854 static ULONG WINAPI HttpInfo_AddRef(IWinInetHttpInfo *iface)
856 HttpProtocol *This = impl_from_IWinInetHttpInfo(iface);
857 return IInternetProtocolEx_AddRef(&This->IInternetProtocolEx_iface);
860 static ULONG WINAPI HttpInfo_Release(IWinInetHttpInfo *iface)
862 HttpProtocol *This = impl_from_IWinInetHttpInfo(iface);
863 return IInternetProtocolEx_Release(&This->IInternetProtocolEx_iface);
866 static HRESULT WINAPI HttpInfo_QueryOption(IWinInetHttpInfo *iface, DWORD dwOption,
867 void *pBuffer, DWORD *pcbBuffer)
869 HttpProtocol *This = impl_from_IWinInetHttpInfo(iface);
870 TRACE("(%p)->(%x %p %p)\n", This, dwOption, pBuffer, pcbBuffer);
872 if(!This->base.request)
873 return E_FAIL;
875 if(!InternetQueryOptionW(This->base.request, dwOption, pBuffer, pcbBuffer))
876 return S_FALSE;
877 return S_OK;
880 static HRESULT WINAPI HttpInfo_QueryInfo(IWinInetHttpInfo *iface, DWORD dwOption,
881 void *pBuffer, DWORD *pcbBuffer, DWORD *pdwFlags, DWORD *pdwReserved)
883 HttpProtocol *This = impl_from_IWinInetHttpInfo(iface);
884 TRACE("(%p)->(%x %p %p %p %p)\n", This, dwOption, pBuffer, pcbBuffer, pdwFlags, pdwReserved);
886 if(!This->base.request)
887 return E_FAIL;
889 if(!HttpQueryInfoW(This->base.request, dwOption, pBuffer, pcbBuffer, pdwFlags)) {
890 if(pBuffer)
891 memset(pBuffer, 0, *pcbBuffer);
892 return S_OK;
894 return S_OK;
897 static const IWinInetHttpInfoVtbl WinInetHttpInfoVtbl = {
898 HttpInfo_QueryInterface,
899 HttpInfo_AddRef,
900 HttpInfo_Release,
901 HttpInfo_QueryOption,
902 HttpInfo_QueryInfo
905 static HRESULT create_http_protocol(BOOL https, void **ppobj)
907 HttpProtocol *ret;
909 ret = heap_alloc_zero(sizeof(HttpProtocol));
910 if(!ret)
911 return E_OUTOFMEMORY;
913 ret->base.vtbl = &AsyncProtocolVtbl;
914 ret->IInternetProtocolEx_iface.lpVtbl = &HttpProtocolVtbl;
915 ret->IInternetPriority_iface.lpVtbl = &HttpPriorityVtbl;
916 ret->IWinInetHttpInfo_iface.lpVtbl = &WinInetHttpInfoVtbl;
918 ret->https = https;
919 ret->ref = 1;
921 *ppobj = &ret->IInternetProtocolEx_iface;
923 URLMON_LockModule();
924 return S_OK;
927 HRESULT HttpProtocol_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
929 TRACE("(%p %p)\n", pUnkOuter, ppobj);
931 return create_http_protocol(FALSE, ppobj);
934 HRESULT HttpSProtocol_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
936 TRACE("(%p %p)\n", pUnkOuter, ppobj);
938 return create_http_protocol(TRUE, ppobj);