winepulse: Add IAudioRenderClient and IAudioCaptureClient
[wine/multimedia.git] / dlls / urlmon / http.c
bloba030d0be6c1b0e6ec7e48297a629751482a9ea44
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 new_flag)
84 DWORD flags, size = sizeof(flags);
85 BOOL res;
87 res = InternetQueryOptionW(This->base.request, INTERNET_OPTION_SECURITY_FLAGS, &flags, &size);
88 if(res) {
89 flags |= new_flag;
90 res = InternetSetOptionW(This->base.request, INTERNET_OPTION_SECURITY_FLAGS, &flags, size);
92 if(!res)
93 ERR("Failed to set security flag(s): %x\n", new_flag);
95 return res;
98 static inline HRESULT internet_error_to_hres(DWORD error)
100 switch(error)
102 case ERROR_INTERNET_SEC_CERT_DATE_INVALID:
103 case ERROR_INTERNET_SEC_CERT_CN_INVALID:
104 case ERROR_INTERNET_INVALID_CA:
105 case ERROR_INTERNET_CLIENT_AUTH_CERT_NEEDED:
106 case ERROR_INTERNET_SEC_INVALID_CERT:
107 case ERROR_INTERNET_SEC_CERT_ERRORS:
108 case ERROR_INTERNET_SEC_CERT_REV_FAILED:
109 case ERROR_INTERNET_SEC_CERT_NO_REV:
110 case ERROR_INTERNET_SEC_CERT_REVOKED:
111 return INET_E_INVALID_CERTIFICATE;
112 case ERROR_INTERNET_HTTP_TO_HTTPS_ON_REDIR:
113 case ERROR_INTERNET_HTTPS_TO_HTTP_ON_REDIR:
114 case ERROR_HTTP_REDIRECT_NEEDS_CONFIRMATION:
115 return INET_E_REDIRECT_FAILED;
116 default:
117 return INET_E_DOWNLOAD_FAILURE;
121 static HRESULT handle_http_error(HttpProtocol *This, DWORD error)
123 IServiceProvider *serv_prov;
124 IWindowForBindingUI *wfb_ui;
125 IHttpSecurity *http_security;
126 BOOL security_problem;
127 DWORD dlg_flags;
128 HWND hwnd;
129 DWORD res;
130 HRESULT hres;
132 switch(error) {
133 case ERROR_INTERNET_SEC_CERT_DATE_INVALID:
134 case ERROR_INTERNET_SEC_CERT_CN_INVALID:
135 case ERROR_INTERNET_HTTP_TO_HTTPS_ON_REDIR:
136 case ERROR_INTERNET_HTTPS_TO_HTTP_ON_REDIR:
137 case ERROR_INTERNET_INVALID_CA:
138 case ERROR_INTERNET_CLIENT_AUTH_CERT_NEEDED:
139 case ERROR_INTERNET_SEC_INVALID_CERT:
140 case ERROR_INTERNET_SEC_CERT_ERRORS:
141 case ERROR_INTERNET_SEC_CERT_REV_FAILED:
142 case ERROR_INTERNET_SEC_CERT_NO_REV:
143 case ERROR_INTERNET_SEC_CERT_REVOKED:
144 case ERROR_HTTP_REDIRECT_NEEDS_CONFIRMATION:
145 security_problem = TRUE;
146 break;
147 default:
148 security_problem = FALSE;
151 hres = IInternetProtocolSink_QueryInterface(This->base.protocol_sink, &IID_IServiceProvider,
152 (void**)&serv_prov);
153 if(FAILED(hres)) {
154 ERR("Failed to get IServiceProvider.\n");
155 return E_ABORT;
158 if(security_problem) {
159 hres = IServiceProvider_QueryService(serv_prov, &IID_IHttpSecurity, &IID_IHttpSecurity,
160 (void**)&http_security);
161 if(SUCCEEDED(hres)) {
162 hres = IHttpSecurity_OnSecurityProblem(http_security, error);
163 IHttpSecurity_Release(http_security);
165 if(hres != S_FALSE)
167 BOOL res = FALSE;
169 IServiceProvider_Release(serv_prov);
171 if(hres == S_OK) {
172 if(error == ERROR_INTERNET_SEC_CERT_DATE_INVALID)
173 res = set_security_flag(This, SECURITY_FLAG_IGNORE_CERT_DATE_INVALID);
174 else if(error == ERROR_INTERNET_SEC_CERT_CN_INVALID)
175 res = set_security_flag(This, SECURITY_FLAG_IGNORE_CERT_CN_INVALID);
176 else if(error == ERROR_INTERNET_INVALID_CA)
177 res = set_security_flag(This, SECURITY_FLAG_IGNORE_UNKNOWN_CA);
179 if(res)
180 return RPC_E_RETRY;
182 FIXME("Don't know how to ignore error %d\n", error);
183 return E_ABORT;
186 if(hres == E_ABORT)
187 return E_ABORT;
188 if(hres == RPC_E_RETRY)
189 return RPC_E_RETRY;
191 return internet_error_to_hres(error);
196 hres = IServiceProvider_QueryService(serv_prov, &IID_IWindowForBindingUI, &IID_IWindowForBindingUI,
197 (void**)&wfb_ui);
198 if(SUCCEEDED(hres)) {
199 const IID *iid_reason;
201 if(security_problem)
202 iid_reason = &IID_IHttpSecurity;
203 else if(error == ERROR_INTERNET_INCORRECT_PASSWORD)
204 iid_reason = &IID_IAuthenticate;
205 else
206 iid_reason = &IID_IWindowForBindingUI;
208 hres = IWindowForBindingUI_GetWindow(wfb_ui, iid_reason, &hwnd);
209 IWindowForBindingUI_Release(wfb_ui);
210 if(FAILED(hres))
211 hwnd = NULL;
214 IServiceProvider_Release(serv_prov);
216 dlg_flags = FLAGS_ERROR_UI_FLAGS_CHANGE_OPTIONS | FLAGS_ERROR_UI_FLAGS_GENERATE_DATA;
217 if(This->base.bindf & BINDF_NO_UI)
218 dlg_flags |= FLAGS_ERROR_UI_FLAGS_NO_UI;
220 res = InternetErrorDlg(hwnd, This->base.request, error, dlg_flags, NULL);
221 if(res == ERROR_INTERNET_FORCE_RETRY || res == ERROR_SUCCESS)
222 return RPC_E_RETRY;
224 return internet_error_to_hres(error);
227 static ULONG send_http_request(HttpProtocol *This)
229 INTERNET_BUFFERSW send_buffer = {sizeof(INTERNET_BUFFERSW)};
230 BOOL res;
232 send_buffer.lpcszHeader = This->full_header;
233 send_buffer.dwHeadersLength = send_buffer.dwHeadersTotal = strlenW(This->full_header);
235 if(This->base.bind_info.dwBindVerb != BINDVERB_GET) {
236 switch(This->base.bind_info.stgmedData.tymed) {
237 case TYMED_HGLOBAL:
238 /* Native does not use GlobalLock/GlobalUnlock, so we won't either */
239 send_buffer.lpvBuffer = This->base.bind_info.stgmedData.u.hGlobal;
240 send_buffer.dwBufferLength = send_buffer.dwBufferTotal = This->base.bind_info.cbstgmedData;
241 break;
242 case TYMED_ISTREAM: {
243 LARGE_INTEGER offset;
245 send_buffer.dwBufferTotal = This->base.bind_info.cbstgmedData;
246 if(!This->base.post_stream) {
247 This->base.post_stream = This->base.bind_info.stgmedData.u.pstm;
248 IStream_AddRef(This->base.post_stream);
251 offset.QuadPart = 0;
252 IStream_Seek(This->base.post_stream, offset, STREAM_SEEK_SET, NULL);
253 break;
255 default:
256 FIXME("Unsupported This->base.bind_info.stgmedData.tymed %d\n", This->base.bind_info.stgmedData.tymed);
260 if(This->base.post_stream)
261 res = HttpSendRequestExW(This->base.request, &send_buffer, NULL, 0, 0);
262 else
263 res = HttpSendRequestW(This->base.request, send_buffer.lpcszHeader, send_buffer.dwHeadersLength,
264 send_buffer.lpvBuffer, send_buffer.dwBufferLength);
266 return res ? 0 : GetLastError();
269 static inline HttpProtocol *impl_from_Protocol(Protocol *prot)
271 return CONTAINING_RECORD(prot, HttpProtocol, base);
274 static HRESULT HttpProtocol_open_request(Protocol *prot, IUri *uri, DWORD request_flags,
275 HINTERNET internet_session, IInternetBindInfo *bind_info)
277 HttpProtocol *This = impl_from_Protocol(prot);
278 LPWSTR addl_header = NULL, post_cookie = NULL;
279 IServiceProvider *service_provider = NULL;
280 IHttpNegotiate2 *http_negotiate2 = NULL;
281 BSTR url, host, user, pass, path;
282 LPOLESTR accept_mimes[257];
283 const WCHAR **accept_types;
284 BYTE security_id[512];
285 DWORD len, port, flags;
286 ULONG num, error;
287 BOOL res, b;
288 HRESULT hres;
290 static const WCHAR wszBindVerb[BINDVERB_CUSTOM][5] =
291 {{'G','E','T',0},
292 {'P','O','S','T',0},
293 {'P','U','T',0}};
295 hres = IUri_GetPort(uri, &port);
296 if(FAILED(hres))
297 return hres;
299 hres = IUri_GetHost(uri, &host);
300 if(FAILED(hres))
301 return hres;
303 hres = IUri_GetUserName(uri, &user);
304 if(SUCCEEDED(hres)) {
305 hres = IUri_GetPassword(uri, &pass);
307 if(SUCCEEDED(hres)) {
308 This->base.connection = InternetConnectW(internet_session, host, port, user, pass,
309 INTERNET_SERVICE_HTTP, This->https ? INTERNET_FLAG_SECURE : 0, (DWORD_PTR)&This->base);
310 SysFreeString(pass);
312 SysFreeString(user);
314 SysFreeString(host);
315 if(FAILED(hres))
316 return hres;
317 if(!This->base.connection) {
318 WARN("InternetConnect failed: %d\n", GetLastError());
319 return INET_E_CANNOT_CONNECT;
322 num = sizeof(accept_mimes)/sizeof(accept_mimes[0])-1;
323 hres = IInternetBindInfo_GetBindString(bind_info, BINDSTRING_ACCEPT_MIMES, accept_mimes, num, &num);
324 if(hres == INET_E_USE_DEFAULT_SETTING) {
325 static const WCHAR default_accept_mimeW[] = {'*','/','*',0};
326 static const WCHAR *default_accept_mimes[] = {default_accept_mimeW, NULL};
328 accept_types = default_accept_mimes;
329 num = 0;
330 }else if(hres == S_OK) {
331 accept_types = (const WCHAR**)accept_mimes;
332 }else {
333 WARN("GetBindString BINDSTRING_ACCEPT_MIMES failed: %08x\n", hres);
334 return INET_E_NO_VALID_MEDIA;
336 accept_mimes[num] = 0;
338 if(This->https)
339 request_flags |= INTERNET_FLAG_SECURE;
341 hres = IUri_GetPathAndQuery(uri, &path);
342 if(SUCCEEDED(hres)) {
343 This->base.request = HttpOpenRequestW(This->base.connection,
344 This->base.bind_info.dwBindVerb < BINDVERB_CUSTOM
345 ? wszBindVerb[This->base.bind_info.dwBindVerb] : This->base.bind_info.szCustomVerb,
346 path, NULL, NULL, accept_types, request_flags, (DWORD_PTR)&This->base);
347 SysFreeString(path);
349 while(num--)
350 CoTaskMemFree(accept_mimes[num]);
351 if(FAILED(hres))
352 return hres;
353 if (!This->base.request) {
354 WARN("HttpOpenRequest failed: %d\n", GetLastError());
355 return INET_E_RESOURCE_NOT_FOUND;
358 hres = IInternetProtocolSink_QueryInterface(This->base.protocol_sink, &IID_IServiceProvider,
359 (void **)&service_provider);
360 if (hres != S_OK) {
361 WARN("IInternetProtocolSink_QueryInterface IID_IServiceProvider failed: %08x\n", hres);
362 return hres;
365 hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate,
366 &IID_IHttpNegotiate, (void **)&This->http_negotiate);
367 if (hres != S_OK) {
368 WARN("IServiceProvider_QueryService IID_IHttpNegotiate failed: %08x\n", hres);
369 IServiceProvider_Release(service_provider);
370 return hres;
373 hres = IUri_GetAbsoluteUri(uri, &url);
374 if(FAILED(hres)) {
375 IServiceProvider_Release(service_provider);
376 return hres;
379 hres = IHttpNegotiate_BeginningTransaction(This->http_negotiate, url, default_headersW,
380 0, &addl_header);
381 SysFreeString(url);
382 if(hres != S_OK) {
383 WARN("IHttpNegotiate_BeginningTransaction failed: %08x\n", hres);
384 IServiceProvider_Release(service_provider);
385 return hres;
388 len = addl_header ? strlenW(addl_header) : 0;
390 This->full_header = heap_alloc(len*sizeof(WCHAR)+sizeof(default_headersW));
391 if(!This->full_header) {
392 IServiceProvider_Release(service_provider);
393 return E_OUTOFMEMORY;
396 if(len)
397 memcpy(This->full_header, addl_header, len*sizeof(WCHAR));
398 CoTaskMemFree(addl_header);
399 memcpy(This->full_header+len, default_headersW, sizeof(default_headersW));
401 hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate2,
402 &IID_IHttpNegotiate2, (void **)&http_negotiate2);
403 IServiceProvider_Release(service_provider);
404 if(hres != S_OK) {
405 WARN("IServiceProvider_QueryService IID_IHttpNegotiate2 failed: %08x\n", hres);
406 /* No goto done as per native */
407 }else {
408 len = sizeof(security_id)/sizeof(security_id[0]);
409 hres = IHttpNegotiate2_GetRootSecurityId(http_negotiate2, security_id, &len, 0);
410 IHttpNegotiate2_Release(http_negotiate2);
411 if (hres != S_OK)
412 WARN("IHttpNegotiate2_GetRootSecurityId failed: %08x\n", hres);
415 /* FIXME: Handle security_id. Native calls undocumented function IsHostInProxyBypassList. */
417 if(This->base.bind_info.dwBindVerb == BINDVERB_POST) {
418 num = 0;
419 hres = IInternetBindInfo_GetBindString(bind_info, BINDSTRING_POST_COOKIE, &post_cookie, 1, &num);
420 if(hres == S_OK && num) {
421 if(!InternetSetOptionW(This->base.request, INTERNET_OPTION_SECONDARY_CACHE_KEY,
422 post_cookie, lstrlenW(post_cookie)))
423 WARN("InternetSetOption INTERNET_OPTION_SECONDARY_CACHE_KEY failed: %d\n", GetLastError());
424 CoTaskMemFree(post_cookie);
428 flags = INTERNET_ERROR_MASK_COMBINED_SEC_CERT;
429 res = InternetSetOptionW(This->base.request, INTERNET_OPTION_ERROR_MASK, &flags, sizeof(flags));
430 if(!res)
431 WARN("InternetSetOption(INTERNET_OPTION_ERROR_MASK) failed: %u\n", GetLastError());
433 b = TRUE;
434 res = InternetSetOptionW(This->base.request, INTERNET_OPTION_HTTP_DECODING, &b, sizeof(b));
435 if(!res)
436 WARN("InternetSetOption(INTERNET_OPTION_HTTP_DECODING) failed: %u\n", GetLastError());
438 do {
439 error = send_http_request(This);
441 if(error == ERROR_IO_PENDING || error == ERROR_SUCCESS)
442 return S_OK;
444 hres = handle_http_error(This, error);
446 } while(hres == RPC_E_RETRY);
448 WARN("HttpSendRequest failed: %d\n", error);
449 return hres;
452 static HRESULT HttpProtocol_end_request(Protocol *protocol)
454 BOOL res;
456 res = HttpEndRequestW(protocol->request, NULL, 0, 0);
457 if(!res && GetLastError() != ERROR_IO_PENDING) {
458 FIXME("HttpEndRequest failed: %u\n", GetLastError());
459 return E_FAIL;
462 return S_OK;
465 static HRESULT HttpProtocol_start_downloading(Protocol *prot)
467 HttpProtocol *This = impl_from_Protocol(prot);
468 LPWSTR content_type, content_length, ranges;
469 DWORD len = sizeof(DWORD);
470 DWORD status_code;
471 BOOL res;
472 HRESULT hres;
474 static const WCHAR wszDefaultContentType[] =
475 {'t','e','x','t','/','h','t','m','l',0};
477 if(!This->http_negotiate) {
478 WARN("Expected IHttpNegotiate pointer to be non-NULL\n");
479 return S_OK;
482 res = HttpQueryInfoW(This->base.request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER,
483 &status_code, &len, NULL);
484 if(res) {
485 LPWSTR response_headers = query_http_info(This, HTTP_QUERY_RAW_HEADERS_CRLF);
486 if(response_headers) {
487 hres = IHttpNegotiate_OnResponse(This->http_negotiate, status_code, response_headers,
488 NULL, NULL);
489 heap_free(response_headers);
490 if (hres != S_OK) {
491 WARN("IHttpNegotiate_OnResponse failed: %08x\n", hres);
492 return S_OK;
495 }else {
496 WARN("HttpQueryInfo failed: %d\n", GetLastError());
499 ranges = query_http_info(This, HTTP_QUERY_ACCEPT_RANGES);
500 if(ranges) {
501 IInternetProtocolSink_ReportProgress(This->base.protocol_sink, BINDSTATUS_ACCEPTRANGES, NULL);
502 heap_free(ranges);
505 content_type = query_http_info(This, HTTP_QUERY_CONTENT_TYPE);
506 if(content_type) {
507 /* remove the charset, if present */
508 LPWSTR p = strchrW(content_type, ';');
509 if (p) *p = '\0';
511 IInternetProtocolSink_ReportProgress(This->base.protocol_sink,
512 (This->base.bindf & BINDF_FROMURLMON)
513 ? BINDSTATUS_MIMETYPEAVAILABLE : BINDSTATUS_RAWMIMETYPE,
514 content_type);
515 heap_free(content_type);
516 }else {
517 WARN("HttpQueryInfo failed: %d\n", GetLastError());
518 IInternetProtocolSink_ReportProgress(This->base.protocol_sink,
519 (This->base.bindf & BINDF_FROMURLMON)
520 ? BINDSTATUS_MIMETYPEAVAILABLE : BINDSTATUS_RAWMIMETYPE,
521 wszDefaultContentType);
524 content_length = query_http_info(This, HTTP_QUERY_CONTENT_LENGTH);
525 if(content_length) {
526 This->base.content_length = atoiW(content_length);
527 heap_free(content_length);
530 return S_OK;
533 static void HttpProtocol_close_connection(Protocol *prot)
535 HttpProtocol *This = impl_from_Protocol(prot);
537 if(This->http_negotiate) {
538 IHttpNegotiate_Release(This->http_negotiate);
539 This->http_negotiate = NULL;
542 heap_free(This->full_header);
543 This->full_header = NULL;
546 static void HttpProtocol_on_error(Protocol *prot, DWORD error)
548 HttpProtocol *This = impl_from_Protocol(prot);
549 HRESULT hres;
551 TRACE("(%p) %d\n", prot, error);
553 if(prot->flags & FLAG_FIRST_CONTINUE_COMPLETE) {
554 FIXME("Not handling error %d\n", error);
555 return;
558 while((hres = handle_http_error(This, error)) == RPC_E_RETRY) {
559 error = send_http_request(This);
561 if(error == ERROR_IO_PENDING || error == ERROR_SUCCESS)
562 return;
565 protocol_abort(prot, hres);
566 protocol_close_connection(prot);
567 return;
570 static const ProtocolVtbl AsyncProtocolVtbl = {
571 HttpProtocol_open_request,
572 HttpProtocol_end_request,
573 HttpProtocol_start_downloading,
574 HttpProtocol_close_connection,
575 HttpProtocol_on_error
578 static HRESULT WINAPI HttpProtocol_QueryInterface(IInternetProtocolEx *iface, REFIID riid, void **ppv)
580 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
582 *ppv = NULL;
583 if(IsEqualGUID(&IID_IUnknown, riid)) {
584 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
585 *ppv = &This->IInternetProtocolEx_iface;
586 }else if(IsEqualGUID(&IID_IInternetProtocolRoot, riid)) {
587 TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", This, ppv);
588 *ppv = &This->IInternetProtocolEx_iface;
589 }else if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
590 TRACE("(%p)->(IID_IInternetProtocol %p)\n", This, ppv);
591 *ppv = &This->IInternetProtocolEx_iface;
592 }else if(IsEqualGUID(&IID_IInternetProtocolEx, riid)) {
593 TRACE("(%p)->(IID_IInternetProtocolEx %p)\n", This, ppv);
594 *ppv = &This->IInternetProtocolEx_iface;
595 }else if(IsEqualGUID(&IID_IInternetPriority, riid)) {
596 TRACE("(%p)->(IID_IInternetPriority %p)\n", This, ppv);
597 *ppv = &This->IInternetPriority_iface;
598 }else if(IsEqualGUID(&IID_IWinInetInfo, riid)) {
599 TRACE("(%p)->(IID_IWinInetInfo %p)\n", This, ppv);
600 *ppv = &This->IWinInetHttpInfo_iface;
601 }else if(IsEqualGUID(&IID_IWinInetHttpInfo, riid)) {
602 TRACE("(%p)->(IID_IWinInetHttpInfo %p)\n", This, ppv);
603 *ppv = &This->IWinInetHttpInfo_iface;
606 if(*ppv) {
607 IInternetProtocol_AddRef(iface);
608 return S_OK;
611 WARN("not supported interface %s\n", debugstr_guid(riid));
612 return E_NOINTERFACE;
615 static ULONG WINAPI HttpProtocol_AddRef(IInternetProtocolEx *iface)
617 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
618 LONG ref = InterlockedIncrement(&This->ref);
619 TRACE("(%p) ref=%d\n", This, ref);
620 return ref;
623 static ULONG WINAPI HttpProtocol_Release(IInternetProtocolEx *iface)
625 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
626 LONG ref = InterlockedDecrement(&This->ref);
628 TRACE("(%p) ref=%d\n", This, ref);
630 if(!ref) {
631 protocol_close_connection(&This->base);
632 heap_free(This);
634 URLMON_UnlockModule();
637 return ref;
640 static HRESULT WINAPI HttpProtocol_Start(IInternetProtocolEx *iface, LPCWSTR szUrl,
641 IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
642 DWORD grfPI, HANDLE_PTR dwReserved)
644 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
645 IUri *uri;
646 HRESULT hres;
648 TRACE("(%p)->(%s %p %p %08x %lx)\n", This, debugstr_w(szUrl), pOIProtSink,
649 pOIBindInfo, grfPI, dwReserved);
651 hres = CreateUri(szUrl, 0, 0, &uri);
652 if(FAILED(hres))
653 return hres;
655 hres = IInternetProtocolEx_StartEx(&This->IInternetProtocolEx_iface, uri, pOIProtSink,
656 pOIBindInfo, grfPI, (HANDLE*)dwReserved);
658 IUri_Release(uri);
659 return hres;
662 static HRESULT WINAPI HttpProtocol_Continue(IInternetProtocolEx *iface, PROTOCOLDATA *pProtocolData)
664 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
666 TRACE("(%p)->(%p)\n", This, pProtocolData);
668 return protocol_continue(&This->base, pProtocolData);
671 static HRESULT WINAPI HttpProtocol_Abort(IInternetProtocolEx *iface, HRESULT hrReason,
672 DWORD dwOptions)
674 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
676 TRACE("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);
678 return protocol_abort(&This->base, hrReason);
681 static HRESULT WINAPI HttpProtocol_Terminate(IInternetProtocolEx *iface, DWORD dwOptions)
683 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
685 TRACE("(%p)->(%08x)\n", This, dwOptions);
687 protocol_close_connection(&This->base);
688 return S_OK;
691 static HRESULT WINAPI HttpProtocol_Suspend(IInternetProtocolEx *iface)
693 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
694 FIXME("(%p)\n", This);
695 return E_NOTIMPL;
698 static HRESULT WINAPI HttpProtocol_Resume(IInternetProtocolEx *iface)
700 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
701 FIXME("(%p)\n", This);
702 return E_NOTIMPL;
705 static HRESULT WINAPI HttpProtocol_Read(IInternetProtocolEx *iface, void *pv,
706 ULONG cb, ULONG *pcbRead)
708 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
710 TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
712 return protocol_read(&This->base, pv, cb, pcbRead);
715 static HRESULT WINAPI HttpProtocol_Seek(IInternetProtocolEx *iface, LARGE_INTEGER dlibMove,
716 DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
718 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
719 FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
720 return E_NOTIMPL;
723 static HRESULT WINAPI HttpProtocol_LockRequest(IInternetProtocolEx *iface, DWORD dwOptions)
725 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
727 TRACE("(%p)->(%08x)\n", This, dwOptions);
729 return protocol_lock_request(&This->base);
732 static HRESULT WINAPI HttpProtocol_UnlockRequest(IInternetProtocolEx *iface)
734 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
736 TRACE("(%p)\n", This);
738 return protocol_unlock_request(&This->base);
741 static HRESULT WINAPI HttpProtocol_StartEx(IInternetProtocolEx *iface, IUri *pUri,
742 IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
743 DWORD grfPI, HANDLE *dwReserved)
745 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
746 DWORD scheme = 0;
747 HRESULT hres;
749 TRACE("(%p)->(%p %p %p %08x %p)\n", This, pUri, pOIProtSink,
750 pOIBindInfo, grfPI, dwReserved);
752 hres = IUri_GetScheme(pUri, &scheme);
753 if(FAILED(hres))
754 return hres;
755 if(scheme != (This->https ? URL_SCHEME_HTTPS : URL_SCHEME_HTTP))
756 return MK_E_SYNTAX;
758 return protocol_start(&This->base, (IInternetProtocol*)&This->IInternetProtocolEx_iface, pUri,
759 pOIProtSink, pOIBindInfo);
762 static const IInternetProtocolExVtbl HttpProtocolVtbl = {
763 HttpProtocol_QueryInterface,
764 HttpProtocol_AddRef,
765 HttpProtocol_Release,
766 HttpProtocol_Start,
767 HttpProtocol_Continue,
768 HttpProtocol_Abort,
769 HttpProtocol_Terminate,
770 HttpProtocol_Suspend,
771 HttpProtocol_Resume,
772 HttpProtocol_Read,
773 HttpProtocol_Seek,
774 HttpProtocol_LockRequest,
775 HttpProtocol_UnlockRequest,
776 HttpProtocol_StartEx
779 static HRESULT WINAPI HttpPriority_QueryInterface(IInternetPriority *iface, REFIID riid, void **ppv)
781 HttpProtocol *This = impl_from_IInternetPriority(iface);
782 return IInternetProtocolEx_QueryInterface(&This->IInternetProtocolEx_iface, riid, ppv);
785 static ULONG WINAPI HttpPriority_AddRef(IInternetPriority *iface)
787 HttpProtocol *This = impl_from_IInternetPriority(iface);
788 return IInternetProtocolEx_AddRef(&This->IInternetProtocolEx_iface);
791 static ULONG WINAPI HttpPriority_Release(IInternetPriority *iface)
793 HttpProtocol *This = impl_from_IInternetPriority(iface);
794 return IInternetProtocolEx_Release(&This->IInternetProtocolEx_iface);
797 static HRESULT WINAPI HttpPriority_SetPriority(IInternetPriority *iface, LONG nPriority)
799 HttpProtocol *This = impl_from_IInternetPriority(iface);
801 TRACE("(%p)->(%d)\n", This, nPriority);
803 This->base.priority = nPriority;
804 return S_OK;
807 static HRESULT WINAPI HttpPriority_GetPriority(IInternetPriority *iface, LONG *pnPriority)
809 HttpProtocol *This = impl_from_IInternetPriority(iface);
811 TRACE("(%p)->(%p)\n", This, pnPriority);
813 *pnPriority = This->base.priority;
814 return S_OK;
817 static const IInternetPriorityVtbl HttpPriorityVtbl = {
818 HttpPriority_QueryInterface,
819 HttpPriority_AddRef,
820 HttpPriority_Release,
821 HttpPriority_SetPriority,
822 HttpPriority_GetPriority
825 static HRESULT WINAPI HttpInfo_QueryInterface(IWinInetHttpInfo *iface, REFIID riid, void **ppv)
827 HttpProtocol *This = impl_from_IWinInetHttpInfo(iface);
828 return IInternetProtocolEx_QueryInterface(&This->IInternetProtocolEx_iface, riid, ppv);
831 static ULONG WINAPI HttpInfo_AddRef(IWinInetHttpInfo *iface)
833 HttpProtocol *This = impl_from_IWinInetHttpInfo(iface);
834 return IInternetProtocolEx_AddRef(&This->IInternetProtocolEx_iface);
837 static ULONG WINAPI HttpInfo_Release(IWinInetHttpInfo *iface)
839 HttpProtocol *This = impl_from_IWinInetHttpInfo(iface);
840 return IInternetProtocolEx_Release(&This->IInternetProtocolEx_iface);
843 static HRESULT WINAPI HttpInfo_QueryOption(IWinInetHttpInfo *iface, DWORD dwOption,
844 void *pBuffer, DWORD *pcbBuffer)
846 HttpProtocol *This = impl_from_IWinInetHttpInfo(iface);
847 TRACE("(%p)->(%x %p %p)\n", This, dwOption, pBuffer, pcbBuffer);
849 if(!This->base.request)
850 return E_FAIL;
852 if(!InternetQueryOptionW(This->base.request, dwOption, pBuffer, pcbBuffer))
853 return S_FALSE;
854 return S_OK;
857 static HRESULT WINAPI HttpInfo_QueryInfo(IWinInetHttpInfo *iface, DWORD dwOption,
858 void *pBuffer, DWORD *pcbBuffer, DWORD *pdwFlags, DWORD *pdwReserved)
860 HttpProtocol *This = impl_from_IWinInetHttpInfo(iface);
861 TRACE("(%p)->(%x %p %p %p %p)\n", This, dwOption, pBuffer, pcbBuffer, pdwFlags, pdwReserved);
863 if(!This->base.request)
864 return E_FAIL;
866 if(!HttpQueryInfoW(This->base.request, dwOption, pBuffer, pcbBuffer, pdwFlags)) {
867 if(pBuffer)
868 memset(pBuffer, 0, *pcbBuffer);
869 return S_OK;
871 return S_OK;
874 static const IWinInetHttpInfoVtbl WinInetHttpInfoVtbl = {
875 HttpInfo_QueryInterface,
876 HttpInfo_AddRef,
877 HttpInfo_Release,
878 HttpInfo_QueryOption,
879 HttpInfo_QueryInfo
882 static HRESULT create_http_protocol(BOOL https, void **ppobj)
884 HttpProtocol *ret;
886 ret = heap_alloc_zero(sizeof(HttpProtocol));
887 if(!ret)
888 return E_OUTOFMEMORY;
890 ret->base.vtbl = &AsyncProtocolVtbl;
891 ret->IInternetProtocolEx_iface.lpVtbl = &HttpProtocolVtbl;
892 ret->IInternetPriority_iface.lpVtbl = &HttpPriorityVtbl;
893 ret->IWinInetHttpInfo_iface.lpVtbl = &WinInetHttpInfoVtbl;
895 ret->https = https;
896 ret->ref = 1;
898 *ppobj = &ret->IInternetProtocolEx_iface;
900 URLMON_LockModule();
901 return S_OK;
904 HRESULT HttpProtocol_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
906 TRACE("(%p %p)\n", pUnkOuter, ppobj);
908 return create_http_protocol(FALSE, ppobj);
911 HRESULT HttpSProtocol_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
913 TRACE("(%p %p)\n", pUnkOuter, ppobj);
915 return create_http_protocol(TRUE, ppobj);