wbemprox: Add a partial implementation of Win32_DiskPartition.
[wine.git] / dlls / urlmon / http.c
blobb2c3d10fff500fb44044c5bdb43f0a1c42b65689
1 /*
2 * Copyright 2005 Jacek Caban
3 * Copyright 2007 Misha Koshelev
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "urlmon_main.h"
21 #include "wininet.h"
23 #define NO_SHLWAPI_REG
24 #include "shlwapi.h"
26 #include "wine/debug.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
30 typedef struct {
31 Protocol base;
33 IInternetProtocolEx IInternetProtocolEx_iface;
34 IInternetPriority IInternetPriority_iface;
35 IWinInetHttpInfo IWinInetHttpInfo_iface;
37 BOOL https;
38 IHttpNegotiate *http_negotiate;
39 WCHAR *full_header;
41 LONG ref;
42 } HttpProtocol;
44 static inline HttpProtocol *impl_from_IInternetProtocolEx(IInternetProtocolEx *iface)
46 return CONTAINING_RECORD(iface, HttpProtocol, IInternetProtocolEx_iface);
49 static inline HttpProtocol *impl_from_IInternetPriority(IInternetPriority *iface)
51 return CONTAINING_RECORD(iface, HttpProtocol, IInternetPriority_iface);
54 static inline HttpProtocol *impl_from_IWinInetHttpInfo(IWinInetHttpInfo *iface)
56 return CONTAINING_RECORD(iface, HttpProtocol, IWinInetHttpInfo_iface);
59 static const WCHAR default_headersW[] = {
60 'A','c','c','e','p','t','-','E','n','c','o','d','i','n','g',':',' ','g','z','i','p',',',' ','d','e','f','l','a','t','e',0};
62 static LPWSTR query_http_info(HttpProtocol *This, DWORD option)
64 LPWSTR ret = NULL;
65 DWORD len = 0;
66 BOOL res;
68 res = HttpQueryInfoW(This->base.request, option, NULL, &len, NULL);
69 if (!res && GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
70 ret = heap_alloc(len);
71 res = HttpQueryInfoW(This->base.request, option, ret, &len, NULL);
73 if(!res) {
74 TRACE("HttpQueryInfoW(%d) failed: %08x\n", option, GetLastError());
75 heap_free(ret);
76 return NULL;
79 return ret;
82 static inline BOOL set_security_flag(HttpProtocol *This, DWORD flags)
84 BOOL res;
86 res = InternetSetOptionW(This->base.request, INTERNET_OPTION_SECURITY_FLAGS, &flags, sizeof(flags));
87 if(!res)
88 ERR("Failed to set security flags: %x\n", flags);
90 return res;
93 static inline HRESULT internet_error_to_hres(DWORD error)
95 switch(error)
97 case ERROR_INTERNET_SEC_CERT_DATE_INVALID:
98 case ERROR_INTERNET_SEC_CERT_CN_INVALID:
99 case ERROR_INTERNET_INVALID_CA:
100 case ERROR_INTERNET_CLIENT_AUTH_CERT_NEEDED:
101 case ERROR_INTERNET_SEC_INVALID_CERT:
102 case ERROR_INTERNET_SEC_CERT_ERRORS:
103 case ERROR_INTERNET_SEC_CERT_REV_FAILED:
104 case ERROR_INTERNET_SEC_CERT_NO_REV:
105 case ERROR_INTERNET_SEC_CERT_REVOKED:
106 return INET_E_INVALID_CERTIFICATE;
107 case ERROR_INTERNET_HTTP_TO_HTTPS_ON_REDIR:
108 case ERROR_INTERNET_HTTPS_TO_HTTP_ON_REDIR:
109 case ERROR_HTTP_REDIRECT_NEEDS_CONFIRMATION:
110 return INET_E_REDIRECT_FAILED;
111 default:
112 return INET_E_DOWNLOAD_FAILURE;
116 static HRESULT handle_http_error(HttpProtocol *This, DWORD error)
118 IServiceProvider *serv_prov;
119 IWindowForBindingUI *wfb_ui;
120 IHttpSecurity *http_security;
121 BOOL security_problem;
122 DWORD dlg_flags;
123 HWND hwnd;
124 DWORD res;
125 HRESULT hres;
127 TRACE("(%p %u)\n", This, error);
129 switch(error) {
130 case ERROR_INTERNET_SEC_CERT_DATE_INVALID:
131 case ERROR_INTERNET_SEC_CERT_CN_INVALID:
132 case ERROR_INTERNET_HTTP_TO_HTTPS_ON_REDIR:
133 case ERROR_INTERNET_HTTPS_TO_HTTP_ON_REDIR:
134 case ERROR_INTERNET_INVALID_CA:
135 case ERROR_INTERNET_CLIENT_AUTH_CERT_NEEDED:
136 case ERROR_INTERNET_SEC_INVALID_CERT:
137 case ERROR_INTERNET_SEC_CERT_ERRORS:
138 case ERROR_INTERNET_SEC_CERT_REV_FAILED:
139 case ERROR_INTERNET_SEC_CERT_NO_REV:
140 case ERROR_INTERNET_SEC_CERT_REVOKED:
141 case ERROR_HTTP_REDIRECT_NEEDS_CONFIRMATION:
142 security_problem = TRUE;
143 break;
144 default:
145 security_problem = FALSE;
148 hres = IInternetProtocolSink_QueryInterface(This->base.protocol_sink, &IID_IServiceProvider,
149 (void**)&serv_prov);
150 if(FAILED(hres)) {
151 ERR("Failed to get IServiceProvider.\n");
152 return E_ABORT;
155 if(security_problem) {
156 hres = IServiceProvider_QueryService(serv_prov, &IID_IHttpSecurity, &IID_IHttpSecurity,
157 (void**)&http_security);
158 if(SUCCEEDED(hres)) {
159 hres = IHttpSecurity_OnSecurityProblem(http_security, error);
160 IHttpSecurity_Release(http_security);
162 TRACE("OnSecurityProblem returned %08x\n", hres);
164 if(hres != S_FALSE)
166 BOOL res = FALSE;
168 IServiceProvider_Release(serv_prov);
170 if(hres == S_OK) {
171 if(error == ERROR_INTERNET_SEC_CERT_DATE_INVALID)
172 res = set_security_flag(This, SECURITY_FLAG_IGNORE_CERT_DATE_INVALID);
173 else if(error == ERROR_INTERNET_SEC_CERT_CN_INVALID)
174 res = set_security_flag(This, SECURITY_FLAG_IGNORE_CERT_CN_INVALID);
175 else if(error == ERROR_INTERNET_INVALID_CA)
176 res = set_security_flag(This, SECURITY_FLAG_IGNORE_UNKNOWN_CA);
178 if(res)
179 return RPC_E_RETRY;
181 FIXME("Don't know how to ignore error %d\n", error);
182 return E_ABORT;
185 if(hres == E_ABORT)
186 return E_ABORT;
187 if(hres == RPC_E_RETRY)
188 return RPC_E_RETRY;
190 return internet_error_to_hres(error);
195 switch(error) {
196 case ERROR_INTERNET_SEC_CERT_REV_FAILED:
197 if(hres != S_FALSE) {
198 /* Silently ignore the error. We will get more detailed error from wininet anyway. */
199 set_security_flag(This, SECURITY_FLAG_IGNORE_REVOCATION);
200 hres = RPC_E_RETRY;
201 break;
203 /* fallthrough */
204 default:
205 hres = IServiceProvider_QueryService(serv_prov, &IID_IWindowForBindingUI, &IID_IWindowForBindingUI, (void**)&wfb_ui);
206 if(SUCCEEDED(hres)) {
207 const IID *iid_reason;
209 if(security_problem)
210 iid_reason = &IID_IHttpSecurity;
211 else if(error == ERROR_INTERNET_INCORRECT_PASSWORD)
212 iid_reason = &IID_IAuthenticate;
213 else
214 iid_reason = &IID_IWindowForBindingUI;
216 hres = IWindowForBindingUI_GetWindow(wfb_ui, iid_reason, &hwnd);
217 IWindowForBindingUI_Release(wfb_ui);
220 if(FAILED(hres)) hwnd = NULL;
222 dlg_flags = FLAGS_ERROR_UI_FLAGS_CHANGE_OPTIONS | FLAGS_ERROR_UI_FLAGS_GENERATE_DATA;
223 if(This->base.bindf & BINDF_NO_UI)
224 dlg_flags |= FLAGS_ERROR_UI_FLAGS_NO_UI;
226 res = InternetErrorDlg(hwnd, This->base.request, error, dlg_flags, NULL);
227 hres = res == ERROR_INTERNET_FORCE_RETRY || res == ERROR_SUCCESS ? RPC_E_RETRY : internet_error_to_hres(error);
230 IServiceProvider_Release(serv_prov);
231 return hres;
234 static ULONG send_http_request(HttpProtocol *This)
236 INTERNET_BUFFERSW send_buffer = {sizeof(INTERNET_BUFFERSW)};
237 BOOL res;
239 send_buffer.lpcszHeader = This->full_header;
240 send_buffer.dwHeadersLength = send_buffer.dwHeadersTotal = strlenW(This->full_header);
242 if(This->base.bind_info.dwBindVerb != BINDVERB_GET) {
243 switch(This->base.bind_info.stgmedData.tymed) {
244 case TYMED_HGLOBAL:
245 /* Native does not use GlobalLock/GlobalUnlock, so we won't either */
246 send_buffer.lpvBuffer = This->base.bind_info.stgmedData.u.hGlobal;
247 send_buffer.dwBufferLength = send_buffer.dwBufferTotal = This->base.bind_info.cbstgmedData;
248 break;
249 case TYMED_ISTREAM: {
250 LARGE_INTEGER offset;
252 send_buffer.dwBufferTotal = This->base.bind_info.cbstgmedData;
253 if(!This->base.post_stream) {
254 This->base.post_stream = This->base.bind_info.stgmedData.u.pstm;
255 IStream_AddRef(This->base.post_stream);
258 offset.QuadPart = 0;
259 IStream_Seek(This->base.post_stream, offset, STREAM_SEEK_SET, NULL);
260 break;
262 default:
263 FIXME("Unsupported This->base.bind_info.stgmedData.tymed %d\n", This->base.bind_info.stgmedData.tymed);
267 if(This->base.post_stream)
268 res = HttpSendRequestExW(This->base.request, &send_buffer, NULL, 0, 0);
269 else
270 res = HttpSendRequestW(This->base.request, send_buffer.lpcszHeader, send_buffer.dwHeadersLength,
271 send_buffer.lpvBuffer, send_buffer.dwBufferLength);
273 return res ? 0 : GetLastError();
276 static inline HttpProtocol *impl_from_Protocol(Protocol *prot)
278 return CONTAINING_RECORD(prot, HttpProtocol, base);
281 static HRESULT HttpProtocol_open_request(Protocol *prot, IUri *uri, DWORD request_flags,
282 HINTERNET internet_session, IInternetBindInfo *bind_info)
284 HttpProtocol *This = impl_from_Protocol(prot);
285 LPWSTR addl_header = NULL, post_cookie = NULL;
286 IServiceProvider *service_provider = NULL;
287 IHttpNegotiate2 *http_negotiate2 = NULL;
288 BSTR url, host, user, pass, path;
289 LPOLESTR accept_mimes[257];
290 const WCHAR **accept_types;
291 BYTE security_id[512];
292 DWORD len, port, flags;
293 ULONG num, error;
294 BOOL res, b;
295 HRESULT hres;
297 static const WCHAR wszBindVerb[BINDVERB_CUSTOM][5] =
298 {{'G','E','T',0},
299 {'P','O','S','T',0},
300 {'P','U','T',0}};
302 hres = IUri_GetPort(uri, &port);
303 if(FAILED(hres))
304 return hres;
306 hres = IUri_GetHost(uri, &host);
307 if(FAILED(hres))
308 return hres;
310 hres = IUri_GetUserName(uri, &user);
311 if(SUCCEEDED(hres)) {
312 hres = IUri_GetPassword(uri, &pass);
314 if(SUCCEEDED(hres)) {
315 This->base.connection = InternetConnectW(internet_session, host, port, user, pass,
316 INTERNET_SERVICE_HTTP, This->https ? INTERNET_FLAG_SECURE : 0, (DWORD_PTR)&This->base);
317 SysFreeString(pass);
319 SysFreeString(user);
321 SysFreeString(host);
322 if(FAILED(hres))
323 return hres;
324 if(!This->base.connection) {
325 WARN("InternetConnect failed: %d\n", GetLastError());
326 return INET_E_CANNOT_CONNECT;
329 num = sizeof(accept_mimes)/sizeof(accept_mimes[0])-1;
330 hres = IInternetBindInfo_GetBindString(bind_info, BINDSTRING_ACCEPT_MIMES, accept_mimes, num, &num);
331 if(hres == INET_E_USE_DEFAULT_SETTING) {
332 static const WCHAR default_accept_mimeW[] = {'*','/','*',0};
333 static const WCHAR *default_accept_mimes[] = {default_accept_mimeW, NULL};
335 accept_types = default_accept_mimes;
336 num = 0;
337 }else if(hres == S_OK) {
338 accept_types = (const WCHAR**)accept_mimes;
339 }else {
340 WARN("GetBindString BINDSTRING_ACCEPT_MIMES failed: %08x\n", hres);
341 return INET_E_NO_VALID_MEDIA;
343 accept_mimes[num] = 0;
345 if(This->https)
346 request_flags |= INTERNET_FLAG_SECURE;
348 hres = IUri_GetPathAndQuery(uri, &path);
349 if(SUCCEEDED(hres)) {
350 This->base.request = HttpOpenRequestW(This->base.connection,
351 This->base.bind_info.dwBindVerb < BINDVERB_CUSTOM
352 ? wszBindVerb[This->base.bind_info.dwBindVerb] : This->base.bind_info.szCustomVerb,
353 path, NULL, NULL, accept_types, request_flags, (DWORD_PTR)&This->base);
354 SysFreeString(path);
356 while(num--)
357 CoTaskMemFree(accept_mimes[num]);
358 if(FAILED(hres))
359 return hres;
360 if (!This->base.request) {
361 WARN("HttpOpenRequest failed: %d\n", GetLastError());
362 return INET_E_RESOURCE_NOT_FOUND;
365 hres = IInternetProtocolSink_QueryInterface(This->base.protocol_sink, &IID_IServiceProvider,
366 (void **)&service_provider);
367 if (hres != S_OK) {
368 WARN("IInternetProtocolSink_QueryInterface IID_IServiceProvider failed: %08x\n", hres);
369 return hres;
372 hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate,
373 &IID_IHttpNegotiate, (void **)&This->http_negotiate);
374 if (hres != S_OK) {
375 WARN("IServiceProvider_QueryService IID_IHttpNegotiate failed: %08x\n", hres);
376 IServiceProvider_Release(service_provider);
377 return hres;
380 hres = IUri_GetAbsoluteUri(uri, &url);
381 if(FAILED(hres)) {
382 IServiceProvider_Release(service_provider);
383 return hres;
386 hres = IHttpNegotiate_BeginningTransaction(This->http_negotiate, url, default_headersW,
387 0, &addl_header);
388 SysFreeString(url);
389 if(hres != S_OK) {
390 WARN("IHttpNegotiate_BeginningTransaction failed: %08x\n", hres);
391 IServiceProvider_Release(service_provider);
392 return hres;
395 len = addl_header ? strlenW(addl_header) : 0;
397 This->full_header = heap_alloc(len*sizeof(WCHAR)+sizeof(default_headersW));
398 if(!This->full_header) {
399 IServiceProvider_Release(service_provider);
400 return E_OUTOFMEMORY;
403 if(len)
404 memcpy(This->full_header, addl_header, len*sizeof(WCHAR));
405 CoTaskMemFree(addl_header);
406 memcpy(This->full_header+len, default_headersW, sizeof(default_headersW));
408 hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate2,
409 &IID_IHttpNegotiate2, (void **)&http_negotiate2);
410 IServiceProvider_Release(service_provider);
411 if(hres != S_OK) {
412 WARN("IServiceProvider_QueryService IID_IHttpNegotiate2 failed: %08x\n", hres);
413 /* No goto done as per native */
414 }else {
415 len = sizeof(security_id)/sizeof(security_id[0]);
416 hres = IHttpNegotiate2_GetRootSecurityId(http_negotiate2, security_id, &len, 0);
417 IHttpNegotiate2_Release(http_negotiate2);
418 if (hres != S_OK)
419 WARN("IHttpNegotiate2_GetRootSecurityId failed: %08x\n", hres);
422 /* FIXME: Handle security_id. Native calls undocumented function IsHostInProxyBypassList. */
424 if(This->base.bind_info.dwBindVerb == BINDVERB_POST) {
425 num = 0;
426 hres = IInternetBindInfo_GetBindString(bind_info, BINDSTRING_POST_COOKIE, &post_cookie, 1, &num);
427 if(hres == S_OK && num) {
428 if(!InternetSetOptionW(This->base.request, INTERNET_OPTION_SECONDARY_CACHE_KEY,
429 post_cookie, lstrlenW(post_cookie)))
430 WARN("InternetSetOption INTERNET_OPTION_SECONDARY_CACHE_KEY failed: %d\n", GetLastError());
431 CoTaskMemFree(post_cookie);
435 flags = INTERNET_ERROR_MASK_COMBINED_SEC_CERT;
436 res = InternetSetOptionW(This->base.request, INTERNET_OPTION_ERROR_MASK, &flags, sizeof(flags));
437 if(!res)
438 WARN("InternetSetOption(INTERNET_OPTION_ERROR_MASK) failed: %u\n", GetLastError());
440 b = TRUE;
441 res = InternetSetOptionW(This->base.request, INTERNET_OPTION_HTTP_DECODING, &b, sizeof(b));
442 if(!res)
443 WARN("InternetSetOption(INTERNET_OPTION_HTTP_DECODING) failed: %u\n", GetLastError());
445 do {
446 error = send_http_request(This);
448 if(error == ERROR_IO_PENDING || error == ERROR_SUCCESS)
449 return S_OK;
451 hres = handle_http_error(This, error);
453 } while(hres == RPC_E_RETRY);
455 WARN("HttpSendRequest failed: %d\n", error);
456 return hres;
459 static HRESULT HttpProtocol_end_request(Protocol *protocol)
461 BOOL res;
463 res = HttpEndRequestW(protocol->request, NULL, 0, 0);
464 if(!res && GetLastError() != ERROR_IO_PENDING) {
465 FIXME("HttpEndRequest failed: %u\n", GetLastError());
466 return E_FAIL;
469 return S_OK;
472 static HRESULT HttpProtocol_start_downloading(Protocol *prot)
474 HttpProtocol *This = impl_from_Protocol(prot);
475 LPWSTR content_type, content_length, ranges;
476 DWORD len = sizeof(DWORD);
477 DWORD status_code;
478 BOOL res;
479 HRESULT hres;
481 static const WCHAR wszDefaultContentType[] =
482 {'t','e','x','t','/','h','t','m','l',0};
484 if(!This->http_negotiate) {
485 WARN("Expected IHttpNegotiate pointer to be non-NULL\n");
486 return S_OK;
489 res = HttpQueryInfoW(This->base.request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER,
490 &status_code, &len, NULL);
491 if(res) {
492 LPWSTR response_headers = query_http_info(This, HTTP_QUERY_RAW_HEADERS_CRLF);
493 if(response_headers) {
494 hres = IHttpNegotiate_OnResponse(This->http_negotiate, status_code, response_headers,
495 NULL, NULL);
496 heap_free(response_headers);
497 if (hres != S_OK) {
498 WARN("IHttpNegotiate_OnResponse failed: %08x\n", hres);
499 return S_OK;
502 }else {
503 WARN("HttpQueryInfo failed: %d\n", GetLastError());
506 ranges = query_http_info(This, HTTP_QUERY_ACCEPT_RANGES);
507 if(ranges) {
508 IInternetProtocolSink_ReportProgress(This->base.protocol_sink, BINDSTATUS_ACCEPTRANGES, NULL);
509 heap_free(ranges);
512 content_type = query_http_info(This, HTTP_QUERY_CONTENT_TYPE);
513 if(content_type) {
514 /* remove the charset, if present */
515 LPWSTR p = strchrW(content_type, ';');
516 if (p) *p = '\0';
518 IInternetProtocolSink_ReportProgress(This->base.protocol_sink,
519 (This->base.bindf & BINDF_FROMURLMON)
520 ? BINDSTATUS_MIMETYPEAVAILABLE : BINDSTATUS_RAWMIMETYPE,
521 content_type);
522 heap_free(content_type);
523 }else {
524 WARN("HttpQueryInfo failed: %d\n", GetLastError());
525 IInternetProtocolSink_ReportProgress(This->base.protocol_sink,
526 (This->base.bindf & BINDF_FROMURLMON)
527 ? BINDSTATUS_MIMETYPEAVAILABLE : BINDSTATUS_RAWMIMETYPE,
528 wszDefaultContentType);
531 content_length = query_http_info(This, HTTP_QUERY_CONTENT_LENGTH);
532 if(content_length) {
533 This->base.content_length = atoiW(content_length);
534 heap_free(content_length);
537 return S_OK;
540 static void HttpProtocol_close_connection(Protocol *prot)
542 HttpProtocol *This = impl_from_Protocol(prot);
544 if(This->http_negotiate) {
545 IHttpNegotiate_Release(This->http_negotiate);
546 This->http_negotiate = NULL;
549 heap_free(This->full_header);
550 This->full_header = NULL;
553 static void HttpProtocol_on_error(Protocol *prot, DWORD error)
555 HttpProtocol *This = impl_from_Protocol(prot);
556 HRESULT hres;
558 TRACE("(%p) %d\n", prot, error);
560 if(prot->flags & FLAG_FIRST_CONTINUE_COMPLETE) {
561 FIXME("Not handling error %d\n", error);
562 return;
565 while((hres = handle_http_error(This, error)) == RPC_E_RETRY) {
566 error = send_http_request(This);
568 if(error == ERROR_IO_PENDING || error == ERROR_SUCCESS)
569 return;
572 protocol_abort(prot, hres);
573 protocol_close_connection(prot);
574 return;
577 static const ProtocolVtbl AsyncProtocolVtbl = {
578 HttpProtocol_open_request,
579 HttpProtocol_end_request,
580 HttpProtocol_start_downloading,
581 HttpProtocol_close_connection,
582 HttpProtocol_on_error
585 static HRESULT WINAPI HttpProtocol_QueryInterface(IInternetProtocolEx *iface, REFIID riid, void **ppv)
587 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
589 *ppv = NULL;
590 if(IsEqualGUID(&IID_IUnknown, riid)) {
591 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
592 *ppv = &This->IInternetProtocolEx_iface;
593 }else if(IsEqualGUID(&IID_IInternetProtocolRoot, riid)) {
594 TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", This, ppv);
595 *ppv = &This->IInternetProtocolEx_iface;
596 }else if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
597 TRACE("(%p)->(IID_IInternetProtocol %p)\n", This, ppv);
598 *ppv = &This->IInternetProtocolEx_iface;
599 }else if(IsEqualGUID(&IID_IInternetProtocolEx, riid)) {
600 TRACE("(%p)->(IID_IInternetProtocolEx %p)\n", This, ppv);
601 *ppv = &This->IInternetProtocolEx_iface;
602 }else if(IsEqualGUID(&IID_IInternetPriority, riid)) {
603 TRACE("(%p)->(IID_IInternetPriority %p)\n", This, ppv);
604 *ppv = &This->IInternetPriority_iface;
605 }else if(IsEqualGUID(&IID_IWinInetInfo, riid)) {
606 TRACE("(%p)->(IID_IWinInetInfo %p)\n", This, ppv);
607 *ppv = &This->IWinInetHttpInfo_iface;
608 }else if(IsEqualGUID(&IID_IWinInetHttpInfo, riid)) {
609 TRACE("(%p)->(IID_IWinInetHttpInfo %p)\n", This, ppv);
610 *ppv = &This->IWinInetHttpInfo_iface;
613 if(*ppv) {
614 IInternetProtocolEx_AddRef(iface);
615 return S_OK;
618 WARN("not supported interface %s\n", debugstr_guid(riid));
619 return E_NOINTERFACE;
622 static ULONG WINAPI HttpProtocol_AddRef(IInternetProtocolEx *iface)
624 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
625 LONG ref = InterlockedIncrement(&This->ref);
626 TRACE("(%p) ref=%d\n", This, ref);
627 return ref;
630 static ULONG WINAPI HttpProtocol_Release(IInternetProtocolEx *iface)
632 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
633 LONG ref = InterlockedDecrement(&This->ref);
635 TRACE("(%p) ref=%d\n", This, ref);
637 if(!ref) {
638 protocol_close_connection(&This->base);
639 heap_free(This);
641 URLMON_UnlockModule();
644 return ref;
647 static HRESULT WINAPI HttpProtocol_Start(IInternetProtocolEx *iface, LPCWSTR szUrl,
648 IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
649 DWORD grfPI, HANDLE_PTR dwReserved)
651 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
652 IUri *uri;
653 HRESULT hres;
655 TRACE("(%p)->(%s %p %p %08x %lx)\n", This, debugstr_w(szUrl), pOIProtSink,
656 pOIBindInfo, grfPI, dwReserved);
658 hres = CreateUri(szUrl, 0, 0, &uri);
659 if(FAILED(hres))
660 return hres;
662 hres = IInternetProtocolEx_StartEx(&This->IInternetProtocolEx_iface, uri, pOIProtSink,
663 pOIBindInfo, grfPI, (HANDLE*)dwReserved);
665 IUri_Release(uri);
666 return hres;
669 static HRESULT WINAPI HttpProtocol_Continue(IInternetProtocolEx *iface, PROTOCOLDATA *pProtocolData)
671 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
673 TRACE("(%p)->(%p)\n", This, pProtocolData);
675 return protocol_continue(&This->base, pProtocolData);
678 static HRESULT WINAPI HttpProtocol_Abort(IInternetProtocolEx *iface, HRESULT hrReason,
679 DWORD dwOptions)
681 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
683 TRACE("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);
685 return protocol_abort(&This->base, hrReason);
688 static HRESULT WINAPI HttpProtocol_Terminate(IInternetProtocolEx *iface, DWORD dwOptions)
690 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
692 TRACE("(%p)->(%08x)\n", This, dwOptions);
694 protocol_close_connection(&This->base);
695 return S_OK;
698 static HRESULT WINAPI HttpProtocol_Suspend(IInternetProtocolEx *iface)
700 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
701 FIXME("(%p)\n", This);
702 return E_NOTIMPL;
705 static HRESULT WINAPI HttpProtocol_Resume(IInternetProtocolEx *iface)
707 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
708 FIXME("(%p)\n", This);
709 return E_NOTIMPL;
712 static HRESULT WINAPI HttpProtocol_Read(IInternetProtocolEx *iface, void *pv,
713 ULONG cb, ULONG *pcbRead)
715 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
717 TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
719 return protocol_read(&This->base, pv, cb, pcbRead);
722 static HRESULT WINAPI HttpProtocol_Seek(IInternetProtocolEx *iface, LARGE_INTEGER dlibMove,
723 DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
725 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
726 FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
727 return E_NOTIMPL;
730 static HRESULT WINAPI HttpProtocol_LockRequest(IInternetProtocolEx *iface, DWORD dwOptions)
732 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
734 TRACE("(%p)->(%08x)\n", This, dwOptions);
736 return protocol_lock_request(&This->base);
739 static HRESULT WINAPI HttpProtocol_UnlockRequest(IInternetProtocolEx *iface)
741 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
743 TRACE("(%p)\n", This);
745 return protocol_unlock_request(&This->base);
748 static HRESULT WINAPI HttpProtocol_StartEx(IInternetProtocolEx *iface, IUri *pUri,
749 IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
750 DWORD grfPI, HANDLE *dwReserved)
752 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
753 DWORD scheme = 0;
754 HRESULT hres;
756 TRACE("(%p)->(%p %p %p %08x %p)\n", This, pUri, pOIProtSink,
757 pOIBindInfo, grfPI, dwReserved);
759 hres = IUri_GetScheme(pUri, &scheme);
760 if(FAILED(hres))
761 return hres;
762 if(scheme != (This->https ? URL_SCHEME_HTTPS : URL_SCHEME_HTTP))
763 return MK_E_SYNTAX;
765 return protocol_start(&This->base, (IInternetProtocol*)&This->IInternetProtocolEx_iface, pUri,
766 pOIProtSink, pOIBindInfo);
769 static const IInternetProtocolExVtbl HttpProtocolVtbl = {
770 HttpProtocol_QueryInterface,
771 HttpProtocol_AddRef,
772 HttpProtocol_Release,
773 HttpProtocol_Start,
774 HttpProtocol_Continue,
775 HttpProtocol_Abort,
776 HttpProtocol_Terminate,
777 HttpProtocol_Suspend,
778 HttpProtocol_Resume,
779 HttpProtocol_Read,
780 HttpProtocol_Seek,
781 HttpProtocol_LockRequest,
782 HttpProtocol_UnlockRequest,
783 HttpProtocol_StartEx
786 static HRESULT WINAPI HttpPriority_QueryInterface(IInternetPriority *iface, REFIID riid, void **ppv)
788 HttpProtocol *This = impl_from_IInternetPriority(iface);
789 return IInternetProtocolEx_QueryInterface(&This->IInternetProtocolEx_iface, riid, ppv);
792 static ULONG WINAPI HttpPriority_AddRef(IInternetPriority *iface)
794 HttpProtocol *This = impl_from_IInternetPriority(iface);
795 return IInternetProtocolEx_AddRef(&This->IInternetProtocolEx_iface);
798 static ULONG WINAPI HttpPriority_Release(IInternetPriority *iface)
800 HttpProtocol *This = impl_from_IInternetPriority(iface);
801 return IInternetProtocolEx_Release(&This->IInternetProtocolEx_iface);
804 static HRESULT WINAPI HttpPriority_SetPriority(IInternetPriority *iface, LONG nPriority)
806 HttpProtocol *This = impl_from_IInternetPriority(iface);
808 TRACE("(%p)->(%d)\n", This, nPriority);
810 This->base.priority = nPriority;
811 return S_OK;
814 static HRESULT WINAPI HttpPriority_GetPriority(IInternetPriority *iface, LONG *pnPriority)
816 HttpProtocol *This = impl_from_IInternetPriority(iface);
818 TRACE("(%p)->(%p)\n", This, pnPriority);
820 *pnPriority = This->base.priority;
821 return S_OK;
824 static const IInternetPriorityVtbl HttpPriorityVtbl = {
825 HttpPriority_QueryInterface,
826 HttpPriority_AddRef,
827 HttpPriority_Release,
828 HttpPriority_SetPriority,
829 HttpPriority_GetPriority
832 static HRESULT WINAPI HttpInfo_QueryInterface(IWinInetHttpInfo *iface, REFIID riid, void **ppv)
834 HttpProtocol *This = impl_from_IWinInetHttpInfo(iface);
835 return IInternetProtocolEx_QueryInterface(&This->IInternetProtocolEx_iface, riid, ppv);
838 static ULONG WINAPI HttpInfo_AddRef(IWinInetHttpInfo *iface)
840 HttpProtocol *This = impl_from_IWinInetHttpInfo(iface);
841 return IInternetProtocolEx_AddRef(&This->IInternetProtocolEx_iface);
844 static ULONG WINAPI HttpInfo_Release(IWinInetHttpInfo *iface)
846 HttpProtocol *This = impl_from_IWinInetHttpInfo(iface);
847 return IInternetProtocolEx_Release(&This->IInternetProtocolEx_iface);
850 static HRESULT WINAPI HttpInfo_QueryOption(IWinInetHttpInfo *iface, DWORD dwOption,
851 void *pBuffer, DWORD *pcbBuffer)
853 HttpProtocol *This = impl_from_IWinInetHttpInfo(iface);
854 TRACE("(%p)->(%x %p %p)\n", This, dwOption, pBuffer, pcbBuffer);
856 if(!This->base.request)
857 return E_FAIL;
859 if(!InternetQueryOptionW(This->base.request, dwOption, pBuffer, pcbBuffer))
860 return S_FALSE;
861 return S_OK;
864 static HRESULT WINAPI HttpInfo_QueryInfo(IWinInetHttpInfo *iface, DWORD dwOption,
865 void *pBuffer, DWORD *pcbBuffer, DWORD *pdwFlags, DWORD *pdwReserved)
867 HttpProtocol *This = impl_from_IWinInetHttpInfo(iface);
868 TRACE("(%p)->(%x %p %p %p %p)\n", This, dwOption, pBuffer, pcbBuffer, pdwFlags, pdwReserved);
870 if(!This->base.request)
871 return E_FAIL;
873 if(!HttpQueryInfoW(This->base.request, dwOption, pBuffer, pcbBuffer, pdwFlags)) {
874 if(pBuffer)
875 memset(pBuffer, 0, *pcbBuffer);
876 return S_OK;
878 return S_OK;
881 static const IWinInetHttpInfoVtbl WinInetHttpInfoVtbl = {
882 HttpInfo_QueryInterface,
883 HttpInfo_AddRef,
884 HttpInfo_Release,
885 HttpInfo_QueryOption,
886 HttpInfo_QueryInfo
889 static HRESULT create_http_protocol(BOOL https, void **ppobj)
891 HttpProtocol *ret;
893 ret = heap_alloc_zero(sizeof(HttpProtocol));
894 if(!ret)
895 return E_OUTOFMEMORY;
897 ret->base.vtbl = &AsyncProtocolVtbl;
898 ret->IInternetProtocolEx_iface.lpVtbl = &HttpProtocolVtbl;
899 ret->IInternetPriority_iface.lpVtbl = &HttpPriorityVtbl;
900 ret->IWinInetHttpInfo_iface.lpVtbl = &WinInetHttpInfoVtbl;
902 ret->https = https;
903 ret->ref = 1;
905 *ppobj = &ret->IInternetProtocolEx_iface;
907 URLMON_LockModule();
908 return S_OK;
911 HRESULT HttpProtocol_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
913 TRACE("(%p %p)\n", pUnkOuter, ppobj);
915 return create_http_protocol(FALSE, ppobj);
918 HRESULT HttpSProtocol_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
920 TRACE("(%p %p)\n", pUnkOuter, ppobj);
922 return create_http_protocol(TRUE, ppobj);