msvcr100: Add critical_section class stub.
[wine.git] / dlls / urlmon / http.c
blob16e5fad2f8a0acdb89a4e16aa20d1bb980cf91e4
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 WCHAR *addl_header = NULL, *post_cookie = NULL, *rootdoc_url = 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 = 0;
330 hres = IInternetBindInfo_GetBindString(bind_info, BINDSTRING_ROOTDOC_URL, &rootdoc_url, 1, &num);
331 if(hres == S_OK && num) {
332 FIXME("Use root doc URL %s\n", debugstr_w(rootdoc_url));
333 CoTaskMemFree(rootdoc_url);
336 num = sizeof(accept_mimes)/sizeof(accept_mimes[0])-1;
337 hres = IInternetBindInfo_GetBindString(bind_info, BINDSTRING_ACCEPT_MIMES, accept_mimes, num, &num);
338 if(hres == INET_E_USE_DEFAULT_SETTING) {
339 static const WCHAR default_accept_mimeW[] = {'*','/','*',0};
340 static const WCHAR *default_accept_mimes[] = {default_accept_mimeW, NULL};
342 accept_types = default_accept_mimes;
343 num = 0;
344 }else if(hres == S_OK) {
345 accept_types = (const WCHAR**)accept_mimes;
346 }else {
347 WARN("GetBindString BINDSTRING_ACCEPT_MIMES failed: %08x\n", hres);
348 return INET_E_NO_VALID_MEDIA;
350 accept_mimes[num] = 0;
352 if(This->https)
353 request_flags |= INTERNET_FLAG_SECURE;
355 hres = IUri_GetPathAndQuery(uri, &path);
356 if(SUCCEEDED(hres)) {
357 This->base.request = HttpOpenRequestW(This->base.connection,
358 This->base.bind_info.dwBindVerb < BINDVERB_CUSTOM
359 ? wszBindVerb[This->base.bind_info.dwBindVerb] : This->base.bind_info.szCustomVerb,
360 path, NULL, NULL, accept_types, request_flags, (DWORD_PTR)&This->base);
361 SysFreeString(path);
363 while(num--)
364 CoTaskMemFree(accept_mimes[num]);
365 if(FAILED(hres))
366 return hres;
367 if (!This->base.request) {
368 WARN("HttpOpenRequest failed: %d\n", GetLastError());
369 return INET_E_RESOURCE_NOT_FOUND;
372 hres = IInternetProtocolSink_QueryInterface(This->base.protocol_sink, &IID_IServiceProvider,
373 (void **)&service_provider);
374 if (hres != S_OK) {
375 WARN("IInternetProtocolSink_QueryInterface IID_IServiceProvider failed: %08x\n", hres);
376 return hres;
379 hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate,
380 &IID_IHttpNegotiate, (void **)&This->http_negotiate);
381 if (hres != S_OK) {
382 WARN("IServiceProvider_QueryService IID_IHttpNegotiate failed: %08x\n", hres);
383 IServiceProvider_Release(service_provider);
384 return hres;
387 hres = IUri_GetAbsoluteUri(uri, &url);
388 if(FAILED(hres)) {
389 IServiceProvider_Release(service_provider);
390 return hres;
393 hres = IHttpNegotiate_BeginningTransaction(This->http_negotiate, url, default_headersW,
394 0, &addl_header);
395 SysFreeString(url);
396 if(hres != S_OK) {
397 WARN("IHttpNegotiate_BeginningTransaction failed: %08x\n", hres);
398 IServiceProvider_Release(service_provider);
399 return hres;
402 len = addl_header ? strlenW(addl_header) : 0;
404 This->full_header = heap_alloc(len*sizeof(WCHAR)+sizeof(default_headersW));
405 if(!This->full_header) {
406 IServiceProvider_Release(service_provider);
407 return E_OUTOFMEMORY;
410 if(len)
411 memcpy(This->full_header, addl_header, len*sizeof(WCHAR));
412 CoTaskMemFree(addl_header);
413 memcpy(This->full_header+len, default_headersW, sizeof(default_headersW));
415 hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate2,
416 &IID_IHttpNegotiate2, (void **)&http_negotiate2);
417 IServiceProvider_Release(service_provider);
418 if(hres != S_OK) {
419 WARN("IServiceProvider_QueryService IID_IHttpNegotiate2 failed: %08x\n", hres);
420 /* No goto done as per native */
421 }else {
422 len = sizeof(security_id)/sizeof(security_id[0]);
423 hres = IHttpNegotiate2_GetRootSecurityId(http_negotiate2, security_id, &len, 0);
424 IHttpNegotiate2_Release(http_negotiate2);
425 if (hres != S_OK)
426 WARN("IHttpNegotiate2_GetRootSecurityId failed: %08x\n", hres);
429 /* FIXME: Handle security_id. Native calls undocumented function IsHostInProxyBypassList. */
431 if(This->base.bind_info.dwBindVerb == BINDVERB_POST) {
432 num = 0;
433 hres = IInternetBindInfo_GetBindString(bind_info, BINDSTRING_POST_COOKIE, &post_cookie, 1, &num);
434 if(hres == S_OK && num) {
435 if(!InternetSetOptionW(This->base.request, INTERNET_OPTION_SECONDARY_CACHE_KEY,
436 post_cookie, lstrlenW(post_cookie)))
437 WARN("InternetSetOption INTERNET_OPTION_SECONDARY_CACHE_KEY failed: %d\n", GetLastError());
438 CoTaskMemFree(post_cookie);
442 flags = INTERNET_ERROR_MASK_COMBINED_SEC_CERT;
443 res = InternetSetOptionW(This->base.request, INTERNET_OPTION_ERROR_MASK, &flags, sizeof(flags));
444 if(!res)
445 WARN("InternetSetOption(INTERNET_OPTION_ERROR_MASK) failed: %u\n", GetLastError());
447 b = TRUE;
448 res = InternetSetOptionW(This->base.request, INTERNET_OPTION_HTTP_DECODING, &b, sizeof(b));
449 if(!res)
450 WARN("InternetSetOption(INTERNET_OPTION_HTTP_DECODING) failed: %u\n", GetLastError());
452 do {
453 error = send_http_request(This);
455 switch(error) {
456 case ERROR_IO_PENDING:
457 return S_OK;
458 case ERROR_SUCCESS:
460 * If sending response ended synchronously, it means that we have the whole data
461 * available locally (most likely in cache).
463 return protocol_syncbinding(&This->base);
464 default:
465 hres = handle_http_error(This, error);
467 } while(hres == RPC_E_RETRY);
469 WARN("HttpSendRequest failed: %d\n", error);
470 return hres;
473 static HRESULT HttpProtocol_end_request(Protocol *protocol)
475 BOOL res;
477 res = HttpEndRequestW(protocol->request, NULL, 0, 0);
478 if(!res && GetLastError() != ERROR_IO_PENDING) {
479 FIXME("HttpEndRequest failed: %u\n", GetLastError());
480 return E_FAIL;
483 return S_OK;
486 static HRESULT HttpProtocol_start_downloading(Protocol *prot)
488 HttpProtocol *This = impl_from_Protocol(prot);
489 LPWSTR content_type, content_length, ranges;
490 DWORD len = sizeof(DWORD);
491 DWORD status_code;
492 BOOL res;
493 HRESULT hres;
495 static const WCHAR wszDefaultContentType[] =
496 {'t','e','x','t','/','h','t','m','l',0};
498 if(!This->http_negotiate) {
499 WARN("Expected IHttpNegotiate pointer to be non-NULL\n");
500 return S_OK;
503 res = HttpQueryInfoW(This->base.request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER,
504 &status_code, &len, NULL);
505 if(res) {
506 LPWSTR response_headers = query_http_info(This, HTTP_QUERY_RAW_HEADERS_CRLF);
507 if(response_headers) {
508 hres = IHttpNegotiate_OnResponse(This->http_negotiate, status_code, response_headers,
509 NULL, NULL);
510 heap_free(response_headers);
511 if (hres != S_OK) {
512 WARN("IHttpNegotiate_OnResponse failed: %08x\n", hres);
513 return S_OK;
516 }else {
517 WARN("HttpQueryInfo failed: %d\n", GetLastError());
520 ranges = query_http_info(This, HTTP_QUERY_ACCEPT_RANGES);
521 if(ranges) {
522 IInternetProtocolSink_ReportProgress(This->base.protocol_sink, BINDSTATUS_ACCEPTRANGES, NULL);
523 heap_free(ranges);
526 content_type = query_http_info(This, HTTP_QUERY_CONTENT_TYPE);
527 if(content_type) {
528 /* remove the charset, if present */
529 LPWSTR p = strchrW(content_type, ';');
530 if (p) *p = '\0';
532 IInternetProtocolSink_ReportProgress(This->base.protocol_sink,
533 (This->base.bindf & BINDF_FROMURLMON)
534 ? BINDSTATUS_MIMETYPEAVAILABLE : BINDSTATUS_RAWMIMETYPE,
535 content_type);
536 heap_free(content_type);
537 }else {
538 WARN("HttpQueryInfo failed: %d\n", GetLastError());
539 IInternetProtocolSink_ReportProgress(This->base.protocol_sink,
540 (This->base.bindf & BINDF_FROMURLMON)
541 ? BINDSTATUS_MIMETYPEAVAILABLE : BINDSTATUS_RAWMIMETYPE,
542 wszDefaultContentType);
545 content_length = query_http_info(This, HTTP_QUERY_CONTENT_LENGTH);
546 if(content_length) {
547 This->base.content_length = atoiW(content_length);
548 heap_free(content_length);
551 return S_OK;
554 static void HttpProtocol_close_connection(Protocol *prot)
556 HttpProtocol *This = impl_from_Protocol(prot);
558 if(This->http_negotiate) {
559 IHttpNegotiate_Release(This->http_negotiate);
560 This->http_negotiate = NULL;
563 heap_free(This->full_header);
564 This->full_header = NULL;
567 static void HttpProtocol_on_error(Protocol *prot, DWORD error)
569 HttpProtocol *This = impl_from_Protocol(prot);
570 HRESULT hres;
572 TRACE("(%p) %d\n", prot, error);
574 if(prot->flags & FLAG_FIRST_CONTINUE_COMPLETE) {
575 FIXME("Not handling error %d\n", error);
576 return;
579 while((hres = handle_http_error(This, error)) == RPC_E_RETRY) {
580 error = send_http_request(This);
582 if(error == ERROR_IO_PENDING || error == ERROR_SUCCESS)
583 return;
586 protocol_abort(prot, hres);
587 protocol_close_connection(prot);
588 return;
591 static const ProtocolVtbl AsyncProtocolVtbl = {
592 HttpProtocol_open_request,
593 HttpProtocol_end_request,
594 HttpProtocol_start_downloading,
595 HttpProtocol_close_connection,
596 HttpProtocol_on_error
599 static HRESULT WINAPI HttpProtocol_QueryInterface(IInternetProtocolEx *iface, REFIID riid, void **ppv)
601 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
603 *ppv = NULL;
604 if(IsEqualGUID(&IID_IUnknown, riid)) {
605 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
606 *ppv = &This->IInternetProtocolEx_iface;
607 }else if(IsEqualGUID(&IID_IInternetProtocolRoot, riid)) {
608 TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", This, ppv);
609 *ppv = &This->IInternetProtocolEx_iface;
610 }else if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
611 TRACE("(%p)->(IID_IInternetProtocol %p)\n", This, ppv);
612 *ppv = &This->IInternetProtocolEx_iface;
613 }else if(IsEqualGUID(&IID_IInternetProtocolEx, riid)) {
614 TRACE("(%p)->(IID_IInternetProtocolEx %p)\n", This, ppv);
615 *ppv = &This->IInternetProtocolEx_iface;
616 }else if(IsEqualGUID(&IID_IInternetPriority, riid)) {
617 TRACE("(%p)->(IID_IInternetPriority %p)\n", This, ppv);
618 *ppv = &This->IInternetPriority_iface;
619 }else if(IsEqualGUID(&IID_IWinInetInfo, riid)) {
620 TRACE("(%p)->(IID_IWinInetInfo %p)\n", This, ppv);
621 *ppv = &This->IWinInetHttpInfo_iface;
622 }else if(IsEqualGUID(&IID_IWinInetHttpInfo, riid)) {
623 TRACE("(%p)->(IID_IWinInetHttpInfo %p)\n", This, ppv);
624 *ppv = &This->IWinInetHttpInfo_iface;
627 if(*ppv) {
628 IInternetProtocolEx_AddRef(iface);
629 return S_OK;
632 WARN("not supported interface %s\n", debugstr_guid(riid));
633 return E_NOINTERFACE;
636 static ULONG WINAPI HttpProtocol_AddRef(IInternetProtocolEx *iface)
638 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
639 LONG ref = InterlockedIncrement(&This->ref);
640 TRACE("(%p) ref=%d\n", This, ref);
641 return ref;
644 static ULONG WINAPI HttpProtocol_Release(IInternetProtocolEx *iface)
646 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
647 LONG ref = InterlockedDecrement(&This->ref);
649 TRACE("(%p) ref=%d\n", This, ref);
651 if(!ref) {
652 protocol_close_connection(&This->base);
653 heap_free(This);
655 URLMON_UnlockModule();
658 return ref;
661 static HRESULT WINAPI HttpProtocol_Start(IInternetProtocolEx *iface, LPCWSTR szUrl,
662 IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
663 DWORD grfPI, HANDLE_PTR dwReserved)
665 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
666 IUri *uri;
667 HRESULT hres;
669 TRACE("(%p)->(%s %p %p %08x %lx)\n", This, debugstr_w(szUrl), pOIProtSink,
670 pOIBindInfo, grfPI, dwReserved);
672 hres = CreateUri(szUrl, 0, 0, &uri);
673 if(FAILED(hres))
674 return hres;
676 hres = IInternetProtocolEx_StartEx(&This->IInternetProtocolEx_iface, uri, pOIProtSink,
677 pOIBindInfo, grfPI, (HANDLE*)dwReserved);
679 IUri_Release(uri);
680 return hres;
683 static HRESULT WINAPI HttpProtocol_Continue(IInternetProtocolEx *iface, PROTOCOLDATA *pProtocolData)
685 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
687 TRACE("(%p)->(%p)\n", This, pProtocolData);
689 return protocol_continue(&This->base, pProtocolData);
692 static HRESULT WINAPI HttpProtocol_Abort(IInternetProtocolEx *iface, HRESULT hrReason,
693 DWORD dwOptions)
695 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
697 TRACE("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);
699 return protocol_abort(&This->base, hrReason);
702 static HRESULT WINAPI HttpProtocol_Terminate(IInternetProtocolEx *iface, DWORD dwOptions)
704 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
706 TRACE("(%p)->(%08x)\n", This, dwOptions);
708 protocol_close_connection(&This->base);
709 return S_OK;
712 static HRESULT WINAPI HttpProtocol_Suspend(IInternetProtocolEx *iface)
714 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
715 FIXME("(%p)\n", This);
716 return E_NOTIMPL;
719 static HRESULT WINAPI HttpProtocol_Resume(IInternetProtocolEx *iface)
721 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
722 FIXME("(%p)\n", This);
723 return E_NOTIMPL;
726 static HRESULT WINAPI HttpProtocol_Read(IInternetProtocolEx *iface, void *pv,
727 ULONG cb, ULONG *pcbRead)
729 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
731 TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
733 return protocol_read(&This->base, pv, cb, pcbRead);
736 static HRESULT WINAPI HttpProtocol_Seek(IInternetProtocolEx *iface, LARGE_INTEGER dlibMove,
737 DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
739 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
740 FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
741 return E_NOTIMPL;
744 static HRESULT WINAPI HttpProtocol_LockRequest(IInternetProtocolEx *iface, DWORD dwOptions)
746 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
748 TRACE("(%p)->(%08x)\n", This, dwOptions);
750 return protocol_lock_request(&This->base);
753 static HRESULT WINAPI HttpProtocol_UnlockRequest(IInternetProtocolEx *iface)
755 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
757 TRACE("(%p)\n", This);
759 return protocol_unlock_request(&This->base);
762 static HRESULT WINAPI HttpProtocol_StartEx(IInternetProtocolEx *iface, IUri *pUri,
763 IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
764 DWORD grfPI, HANDLE *dwReserved)
766 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
767 DWORD scheme = 0;
768 HRESULT hres;
770 TRACE("(%p)->(%p %p %p %08x %p)\n", This, pUri, pOIProtSink,
771 pOIBindInfo, grfPI, dwReserved);
773 hres = IUri_GetScheme(pUri, &scheme);
774 if(FAILED(hres))
775 return hres;
776 if(scheme != (This->https ? URL_SCHEME_HTTPS : URL_SCHEME_HTTP))
777 return MK_E_SYNTAX;
779 return protocol_start(&This->base, (IInternetProtocol*)&This->IInternetProtocolEx_iface, pUri,
780 pOIProtSink, pOIBindInfo);
783 static const IInternetProtocolExVtbl HttpProtocolVtbl = {
784 HttpProtocol_QueryInterface,
785 HttpProtocol_AddRef,
786 HttpProtocol_Release,
787 HttpProtocol_Start,
788 HttpProtocol_Continue,
789 HttpProtocol_Abort,
790 HttpProtocol_Terminate,
791 HttpProtocol_Suspend,
792 HttpProtocol_Resume,
793 HttpProtocol_Read,
794 HttpProtocol_Seek,
795 HttpProtocol_LockRequest,
796 HttpProtocol_UnlockRequest,
797 HttpProtocol_StartEx
800 static HRESULT WINAPI HttpPriority_QueryInterface(IInternetPriority *iface, REFIID riid, void **ppv)
802 HttpProtocol *This = impl_from_IInternetPriority(iface);
803 return IInternetProtocolEx_QueryInterface(&This->IInternetProtocolEx_iface, riid, ppv);
806 static ULONG WINAPI HttpPriority_AddRef(IInternetPriority *iface)
808 HttpProtocol *This = impl_from_IInternetPriority(iface);
809 return IInternetProtocolEx_AddRef(&This->IInternetProtocolEx_iface);
812 static ULONG WINAPI HttpPriority_Release(IInternetPriority *iface)
814 HttpProtocol *This = impl_from_IInternetPriority(iface);
815 return IInternetProtocolEx_Release(&This->IInternetProtocolEx_iface);
818 static HRESULT WINAPI HttpPriority_SetPriority(IInternetPriority *iface, LONG nPriority)
820 HttpProtocol *This = impl_from_IInternetPriority(iface);
822 TRACE("(%p)->(%d)\n", This, nPriority);
824 This->base.priority = nPriority;
825 return S_OK;
828 static HRESULT WINAPI HttpPriority_GetPriority(IInternetPriority *iface, LONG *pnPriority)
830 HttpProtocol *This = impl_from_IInternetPriority(iface);
832 TRACE("(%p)->(%p)\n", This, pnPriority);
834 *pnPriority = This->base.priority;
835 return S_OK;
838 static const IInternetPriorityVtbl HttpPriorityVtbl = {
839 HttpPriority_QueryInterface,
840 HttpPriority_AddRef,
841 HttpPriority_Release,
842 HttpPriority_SetPriority,
843 HttpPriority_GetPriority
846 static HRESULT WINAPI HttpInfo_QueryInterface(IWinInetHttpInfo *iface, REFIID riid, void **ppv)
848 HttpProtocol *This = impl_from_IWinInetHttpInfo(iface);
849 return IInternetProtocolEx_QueryInterface(&This->IInternetProtocolEx_iface, riid, ppv);
852 static ULONG WINAPI HttpInfo_AddRef(IWinInetHttpInfo *iface)
854 HttpProtocol *This = impl_from_IWinInetHttpInfo(iface);
855 return IInternetProtocolEx_AddRef(&This->IInternetProtocolEx_iface);
858 static ULONG WINAPI HttpInfo_Release(IWinInetHttpInfo *iface)
860 HttpProtocol *This = impl_from_IWinInetHttpInfo(iface);
861 return IInternetProtocolEx_Release(&This->IInternetProtocolEx_iface);
864 static HRESULT WINAPI HttpInfo_QueryOption(IWinInetHttpInfo *iface, DWORD dwOption,
865 void *pBuffer, DWORD *pcbBuffer)
867 HttpProtocol *This = impl_from_IWinInetHttpInfo(iface);
868 TRACE("(%p)->(%x %p %p)\n", This, dwOption, pBuffer, pcbBuffer);
870 if(!This->base.request)
871 return E_FAIL;
873 if(!InternetQueryOptionW(This->base.request, dwOption, pBuffer, pcbBuffer))
874 return S_FALSE;
875 return S_OK;
878 static HRESULT WINAPI HttpInfo_QueryInfo(IWinInetHttpInfo *iface, DWORD dwOption,
879 void *pBuffer, DWORD *pcbBuffer, DWORD *pdwFlags, DWORD *pdwReserved)
881 HttpProtocol *This = impl_from_IWinInetHttpInfo(iface);
882 TRACE("(%p)->(%x %p %p %p %p)\n", This, dwOption, pBuffer, pcbBuffer, pdwFlags, pdwReserved);
884 if(!This->base.request)
885 return E_FAIL;
887 if(!HttpQueryInfoW(This->base.request, dwOption, pBuffer, pcbBuffer, pdwFlags)) {
888 if(pBuffer)
889 memset(pBuffer, 0, *pcbBuffer);
890 return S_OK;
892 return S_OK;
895 static const IWinInetHttpInfoVtbl WinInetHttpInfoVtbl = {
896 HttpInfo_QueryInterface,
897 HttpInfo_AddRef,
898 HttpInfo_Release,
899 HttpInfo_QueryOption,
900 HttpInfo_QueryInfo
903 static HRESULT create_http_protocol(BOOL https, void **ppobj)
905 HttpProtocol *ret;
907 ret = heap_alloc_zero(sizeof(HttpProtocol));
908 if(!ret)
909 return E_OUTOFMEMORY;
911 ret->base.vtbl = &AsyncProtocolVtbl;
912 ret->IInternetProtocolEx_iface.lpVtbl = &HttpProtocolVtbl;
913 ret->IInternetPriority_iface.lpVtbl = &HttpPriorityVtbl;
914 ret->IWinInetHttpInfo_iface.lpVtbl = &WinInetHttpInfoVtbl;
916 ret->https = https;
917 ret->ref = 1;
919 *ppobj = &ret->IInternetProtocolEx_iface;
921 URLMON_LockModule();
922 return S_OK;
925 HRESULT HttpProtocol_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
927 TRACE("(%p %p)\n", pUnkOuter, ppobj);
929 return create_http_protocol(FALSE, ppobj);
932 HRESULT HttpSProtocol_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
934 TRACE("(%p %p)\n", pUnkOuter, ppobj);
936 return create_http_protocol(TRUE, ppobj);