wpp: Free strings on define deletion (Valgrind).
[wine/multimedia.git] / dlls / urlmon / http.c
blob23f6e6935a11f53c4f29f62071c5506dac99e714
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 LPWSTR 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 /* Default headers from native */
60 static const WCHAR wszHeaders[] = {'A','c','c','e','p','t','-','E','n','c','o','d','i','n','g',
61 ':',' ','g','z','i','p',',',' ','d','e','f','l','a','t','e',0};
63 static LPWSTR query_http_info(HttpProtocol *This, DWORD option)
65 LPWSTR ret = NULL;
66 DWORD len = 0;
67 BOOL res;
69 res = HttpQueryInfoW(This->base.request, option, NULL, &len, NULL);
70 if (!res && GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
71 ret = heap_alloc(len);
72 res = HttpQueryInfoW(This->base.request, option, ret, &len, NULL);
74 if(!res) {
75 TRACE("HttpQueryInfoW(%d) failed: %08x\n", option, GetLastError());
76 heap_free(ret);
77 return NULL;
80 return ret;
83 static inline BOOL set_security_flag(HttpProtocol *This, DWORD new_flag)
85 DWORD flags, size = sizeof(flags);
86 BOOL res;
88 res = InternetQueryOptionW(This->base.request, INTERNET_OPTION_SECURITY_FLAGS, &flags, &size);
89 if(res) {
90 flags |= new_flag;
91 res = InternetSetOptionW(This->base.request, INTERNET_OPTION_SECURITY_FLAGS, &flags, size);
93 if(!res)
94 ERR("Failed to set security flag(s): %x\n", new_flag);
96 return res;
99 static inline HRESULT internet_error_to_hres(DWORD error)
101 switch(error)
103 case ERROR_INTERNET_SEC_CERT_DATE_INVALID:
104 case ERROR_INTERNET_SEC_CERT_CN_INVALID:
105 case ERROR_INTERNET_INVALID_CA:
106 case ERROR_INTERNET_CLIENT_AUTH_CERT_NEEDED:
107 return INET_E_INVALID_CERTIFICATE;
108 case ERROR_INTERNET_HTTP_TO_HTTPS_ON_REDIR:
109 case ERROR_INTERNET_HTTPS_TO_HTTP_ON_REDIR:
110 case ERROR_HTTP_REDIRECT_NEEDS_CONFIRMATION:
111 return INET_E_REDIRECT_FAILED;
112 default:
113 return INET_E_DOWNLOAD_FAILURE;
117 static HRESULT handle_http_error(HttpProtocol *This, DWORD error)
119 IServiceProvider *serv_prov;
120 IWindowForBindingUI *wfb_ui;
121 IHttpSecurity *http_security;
122 BOOL security_problem;
123 HRESULT hres;
125 switch(error) {
126 case ERROR_INTERNET_SEC_CERT_DATE_INVALID:
127 case ERROR_INTERNET_SEC_CERT_CN_INVALID:
128 case ERROR_INTERNET_HTTP_TO_HTTPS_ON_REDIR:
129 case ERROR_INTERNET_HTTPS_TO_HTTP_ON_REDIR:
130 case ERROR_HTTP_REDIRECT_NEEDS_CONFIRMATION:
131 case ERROR_INTERNET_INVALID_CA:
132 case ERROR_INTERNET_CLIENT_AUTH_CERT_NEEDED:
133 security_problem = TRUE;
134 break;
135 default:
136 security_problem = FALSE;
139 hres = IInternetProtocolSink_QueryInterface(This->base.protocol_sink, &IID_IServiceProvider,
140 (void**)&serv_prov);
141 if(FAILED(hres)) {
142 ERR("Failed to get IServiceProvider.\n");
143 return E_ABORT;
146 if(security_problem) {
147 hres = IServiceProvider_QueryService(serv_prov, &IID_IHttpSecurity, &IID_IHttpSecurity,
148 (void**)&http_security);
149 if(SUCCEEDED(hres)) {
150 hres = IHttpSecurity_OnSecurityProblem(http_security, error);
151 IHttpSecurity_Release(http_security);
153 if(hres != S_FALSE)
155 BOOL res = FALSE;
157 IServiceProvider_Release(serv_prov);
159 if(hres == S_OK) {
160 if(error == ERROR_INTERNET_SEC_CERT_DATE_INVALID)
161 res = set_security_flag(This, SECURITY_FLAG_IGNORE_CERT_DATE_INVALID);
162 else if(error == ERROR_INTERNET_SEC_CERT_CN_INVALID)
163 res = set_security_flag(This, SECURITY_FLAG_IGNORE_CERT_CN_INVALID);
164 else if(error == ERROR_INTERNET_INVALID_CA)
165 res = set_security_flag(This, SECURITY_FLAG_IGNORE_UNKNOWN_CA);
167 if(res)
168 return RPC_E_RETRY;
170 FIXME("Don't know how to ignore error %d\n", error);
171 return E_ABORT;
174 if(hres == E_ABORT)
175 return E_ABORT;
176 if(hres == RPC_E_RETRY)
177 return RPC_E_RETRY;
179 return internet_error_to_hres(error);
184 hres = IServiceProvider_QueryService(serv_prov, &IID_IWindowForBindingUI, &IID_IWindowForBindingUI,
185 (void**)&wfb_ui);
186 if(SUCCEEDED(hres)) {
187 HWND hwnd;
188 const IID *iid_reason;
190 if(security_problem)
191 iid_reason = &IID_IHttpSecurity;
192 else if(error == ERROR_INTERNET_INCORRECT_PASSWORD)
193 iid_reason = &IID_IAuthenticate;
194 else
195 iid_reason = &IID_IWindowForBindingUI;
197 hres = IWindowForBindingUI_GetWindow(wfb_ui, iid_reason, &hwnd);
198 if(SUCCEEDED(hres) && hwnd)
200 DWORD res;
202 res = InternetErrorDlg(hwnd, This->base.request, error,
203 FLAGS_ERROR_UI_FLAGS_CHANGE_OPTIONS | FLAGS_ERROR_UI_FLAGS_GENERATE_DATA,
204 NULL);
206 if(res == ERROR_INTERNET_FORCE_RETRY || res == ERROR_SUCCESS)
207 hres = RPC_E_RETRY;
208 else
209 hres = E_FAIL;
211 IWindowForBindingUI_Release(wfb_ui);
214 IServiceProvider_Release(serv_prov);
216 if(hres == RPC_E_RETRY)
217 return hres;
219 return internet_error_to_hres(error);
222 static ULONG send_http_request(HttpProtocol *This)
224 INTERNET_BUFFERSW send_buffer = {sizeof(INTERNET_BUFFERSW)};
225 BOOL res;
227 send_buffer.lpcszHeader = This->full_header;
228 send_buffer.dwHeadersLength = send_buffer.dwHeadersTotal = strlenW(This->full_header);
230 if(This->base.bind_info.dwBindVerb != BINDVERB_GET) {
231 switch(This->base.bind_info.stgmedData.tymed) {
232 case TYMED_HGLOBAL:
233 /* Native does not use GlobalLock/GlobalUnlock, so we won't either */
234 send_buffer.lpvBuffer = This->base.bind_info.stgmedData.u.hGlobal;
235 send_buffer.dwBufferLength = send_buffer.dwBufferTotal = This->base.bind_info.cbstgmedData;
236 break;
237 case TYMED_ISTREAM: {
238 LARGE_INTEGER offset;
240 send_buffer.dwBufferTotal = This->base.bind_info.cbstgmedData;
241 if(!This->base.post_stream) {
242 This->base.post_stream = This->base.bind_info.stgmedData.u.pstm;
243 IStream_AddRef(This->base.post_stream);
246 offset.QuadPart = 0;
247 IStream_Seek(This->base.post_stream, offset, STREAM_SEEK_SET, NULL);
248 break;
250 default:
251 FIXME("Unsupported This->base.bind_info.stgmedData.tymed %d\n", This->base.bind_info.stgmedData.tymed);
255 if(This->base.post_stream)
256 res = HttpSendRequestExW(This->base.request, &send_buffer, NULL, 0, 0);
257 else
258 res = HttpSendRequestW(This->base.request, send_buffer.lpcszHeader, send_buffer.dwHeadersLength,
259 send_buffer.lpvBuffer, send_buffer.dwBufferLength);
261 return res ? 0 : GetLastError();
264 static inline HttpProtocol *impl_from_Protocol(Protocol *prot)
266 return CONTAINING_RECORD(prot, HttpProtocol, base);
269 static HRESULT HttpProtocol_open_request(Protocol *prot, IUri *uri, DWORD request_flags,
270 HINTERNET internet_session, IInternetBindInfo *bind_info)
272 HttpProtocol *This = impl_from_Protocol(prot);
273 LPWSTR addl_header = NULL, post_cookie = NULL;
274 IServiceProvider *service_provider = NULL;
275 IHttpNegotiate2 *http_negotiate2 = NULL;
276 BSTR url, host, user, pass, path;
277 LPOLESTR accept_mimes[257];
278 const WCHAR **accept_types;
279 BYTE security_id[512];
280 DWORD len = 0, port;
281 ULONG num, error;
282 BOOL res, b;
283 HRESULT hres;
285 static const WCHAR wszBindVerb[BINDVERB_CUSTOM][5] =
286 {{'G','E','T',0},
287 {'P','O','S','T',0},
288 {'P','U','T',0}};
290 hres = IUri_GetPort(uri, &port);
291 if(FAILED(hres))
292 return hres;
294 hres = IUri_GetHost(uri, &host);
295 if(FAILED(hres))
296 return hres;
298 hres = IUri_GetUserName(uri, &user);
299 if(SUCCEEDED(hres)) {
300 hres = IUri_GetPassword(uri, &pass);
302 if(SUCCEEDED(hres)) {
303 This->base.connection = InternetConnectW(internet_session, host, port, user, pass,
304 INTERNET_SERVICE_HTTP, This->https ? INTERNET_FLAG_SECURE : 0, (DWORD_PTR)&This->base);
305 SysFreeString(pass);
307 SysFreeString(user);
309 SysFreeString(host);
310 if(FAILED(hres))
311 return hres;
312 if(!This->base.connection) {
313 WARN("InternetConnect failed: %d\n", GetLastError());
314 return INET_E_CANNOT_CONNECT;
317 num = sizeof(accept_mimes)/sizeof(accept_mimes[0])-1;
318 hres = IInternetBindInfo_GetBindString(bind_info, BINDSTRING_ACCEPT_MIMES, accept_mimes, num, &num);
319 if(hres == INET_E_USE_DEFAULT_SETTING) {
320 static const WCHAR default_accept_mimeW[] = {'*','/','*',0};
321 static const WCHAR *default_accept_mimes[] = {default_accept_mimeW, NULL};
323 accept_types = default_accept_mimes;
324 num = 0;
325 }else if(hres == S_OK) {
326 accept_types = (const WCHAR**)accept_mimes;
327 }else {
328 WARN("GetBindString BINDSTRING_ACCEPT_MIMES failed: %08x\n", hres);
329 return INET_E_NO_VALID_MEDIA;
331 accept_mimes[num] = 0;
333 if(This->https)
334 request_flags |= INTERNET_FLAG_SECURE;
336 hres = IUri_GetPathAndQuery(uri, &path);
337 if(SUCCEEDED(hres)) {
338 This->base.request = HttpOpenRequestW(This->base.connection,
339 This->base.bind_info.dwBindVerb < BINDVERB_CUSTOM
340 ? wszBindVerb[This->base.bind_info.dwBindVerb] : This->base.bind_info.szCustomVerb,
341 path, NULL, NULL, accept_types, request_flags, (DWORD_PTR)&This->base);
342 SysFreeString(path);
344 while(num--)
345 CoTaskMemFree(accept_mimes[num]);
346 if(FAILED(hres))
347 return hres;
348 if (!This->base.request) {
349 WARN("HttpOpenRequest failed: %d\n", GetLastError());
350 return INET_E_RESOURCE_NOT_FOUND;
353 hres = IInternetProtocolSink_QueryInterface(This->base.protocol_sink, &IID_IServiceProvider,
354 (void **)&service_provider);
355 if (hres != S_OK) {
356 WARN("IInternetProtocolSink_QueryInterface IID_IServiceProvider failed: %08x\n", hres);
357 return hres;
360 hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate,
361 &IID_IHttpNegotiate, (void **)&This->http_negotiate);
362 if (hres != S_OK) {
363 WARN("IServiceProvider_QueryService IID_IHttpNegotiate failed: %08x\n", hres);
364 IServiceProvider_Release(service_provider);
365 return hres;
368 hres = IUri_GetAbsoluteUri(uri, &url);
369 if(FAILED(hres)) {
370 IServiceProvider_Release(service_provider);
371 return hres;
374 hres = IHttpNegotiate_BeginningTransaction(This->http_negotiate, url, wszHeaders,
375 0, &addl_header);
376 SysFreeString(url);
377 if(hres != S_OK) {
378 WARN("IHttpNegotiate_BeginningTransaction failed: %08x\n", hres);
379 IServiceProvider_Release(service_provider);
380 return hres;
383 if(addl_header) {
384 int len_addl_header = strlenW(addl_header);
386 This->full_header = heap_alloc(len_addl_header*sizeof(WCHAR)+sizeof(wszHeaders));
388 lstrcpyW(This->full_header, addl_header);
389 lstrcpyW(&This->full_header[len_addl_header], wszHeaders);
390 CoTaskMemFree(addl_header);
391 }else {
392 This->full_header = (LPWSTR)wszHeaders;
395 hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate2,
396 &IID_IHttpNegotiate2, (void **)&http_negotiate2);
397 IServiceProvider_Release(service_provider);
398 if(hres != S_OK) {
399 WARN("IServiceProvider_QueryService IID_IHttpNegotiate2 failed: %08x\n", hres);
400 /* No goto done as per native */
401 }else {
402 len = sizeof(security_id)/sizeof(security_id[0]);
403 hres = IHttpNegotiate2_GetRootSecurityId(http_negotiate2, security_id, &len, 0);
404 IHttpNegotiate2_Release(http_negotiate2);
405 if (hres != S_OK)
406 WARN("IHttpNegotiate2_GetRootSecurityId failed: %08x\n", hres);
409 /* FIXME: Handle security_id. Native calls undocumented function IsHostInProxyBypassList. */
411 if(This->base.bind_info.dwBindVerb == BINDVERB_POST) {
412 num = 0;
413 hres = IInternetBindInfo_GetBindString(bind_info, BINDSTRING_POST_COOKIE, &post_cookie, 1, &num);
414 if(hres == S_OK && num) {
415 if(!InternetSetOptionW(This->base.request, INTERNET_OPTION_SECONDARY_CACHE_KEY,
416 post_cookie, lstrlenW(post_cookie)))
417 WARN("InternetSetOption INTERNET_OPTION_SECONDARY_CACHE_KEY failed: %d\n", GetLastError());
418 CoTaskMemFree(post_cookie);
422 b = TRUE;
423 res = InternetSetOptionW(This->base.request, INTERNET_OPTION_HTTP_DECODING, &b, sizeof(b));
424 if(!res)
425 WARN("InternetSetOption(INTERNET_OPTION_HTTP_DECODING) failed: %08x\n", GetLastError());
427 do {
428 error = send_http_request(This);
430 if(error == ERROR_IO_PENDING || error == ERROR_SUCCESS)
431 return S_OK;
433 hres = handle_http_error(This, error);
435 } while(hres == RPC_E_RETRY);
437 WARN("HttpSendRequest failed: %d\n", error);
438 return hres;
441 static HRESULT HttpProtocol_end_request(Protocol *protocol)
443 BOOL res;
445 res = HttpEndRequestW(protocol->request, NULL, 0, 0);
446 if(!res && GetLastError() != ERROR_IO_PENDING) {
447 FIXME("HttpEndRequest failed: %u\n", GetLastError());
448 return E_FAIL;
451 return S_OK;
454 static HRESULT HttpProtocol_start_downloading(Protocol *prot)
456 HttpProtocol *This = impl_from_Protocol(prot);
457 LPWSTR content_type, content_length, ranges;
458 DWORD len = sizeof(DWORD);
459 DWORD status_code;
460 BOOL res;
461 HRESULT hres;
463 static const WCHAR wszDefaultContentType[] =
464 {'t','e','x','t','/','h','t','m','l',0};
466 if(!This->http_negotiate) {
467 WARN("Expected IHttpNegotiate pointer to be non-NULL\n");
468 return S_OK;
471 res = HttpQueryInfoW(This->base.request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER,
472 &status_code, &len, NULL);
473 if(res) {
474 LPWSTR response_headers = query_http_info(This, HTTP_QUERY_RAW_HEADERS_CRLF);
475 if(response_headers) {
476 hres = IHttpNegotiate_OnResponse(This->http_negotiate, status_code, response_headers,
477 NULL, NULL);
478 heap_free(response_headers);
479 if (hres != S_OK) {
480 WARN("IHttpNegotiate_OnResponse failed: %08x\n", hres);
481 return S_OK;
484 }else {
485 WARN("HttpQueryInfo failed: %d\n", GetLastError());
488 ranges = query_http_info(This, HTTP_QUERY_ACCEPT_RANGES);
489 if(ranges) {
490 IInternetProtocolSink_ReportProgress(This->base.protocol_sink, BINDSTATUS_ACCEPTRANGES, NULL);
491 heap_free(ranges);
494 content_type = query_http_info(This, HTTP_QUERY_CONTENT_TYPE);
495 if(content_type) {
496 /* remove the charset, if present */
497 LPWSTR p = strchrW(content_type, ';');
498 if (p) *p = '\0';
500 IInternetProtocolSink_ReportProgress(This->base.protocol_sink,
501 (This->base.bindf & BINDF_FROMURLMON)
502 ? BINDSTATUS_MIMETYPEAVAILABLE : BINDSTATUS_RAWMIMETYPE,
503 content_type);
504 heap_free(content_type);
505 }else {
506 WARN("HttpQueryInfo failed: %d\n", GetLastError());
507 IInternetProtocolSink_ReportProgress(This->base.protocol_sink,
508 (This->base.bindf & BINDF_FROMURLMON)
509 ? BINDSTATUS_MIMETYPEAVAILABLE : BINDSTATUS_RAWMIMETYPE,
510 wszDefaultContentType);
513 content_length = query_http_info(This, HTTP_QUERY_CONTENT_LENGTH);
514 if(content_length) {
515 This->base.content_length = atoiW(content_length);
516 heap_free(content_length);
519 return S_OK;
522 static void HttpProtocol_close_connection(Protocol *prot)
524 HttpProtocol *This = impl_from_Protocol(prot);
526 if(This->http_negotiate) {
527 IHttpNegotiate_Release(This->http_negotiate);
528 This->http_negotiate = 0;
531 if(This->full_header) {
532 if(This->full_header != wszHeaders)
533 heap_free(This->full_header);
534 This->full_header = 0;
538 static void HttpProtocol_on_error(Protocol *prot, DWORD error)
540 HttpProtocol *This = impl_from_Protocol(prot);
541 HRESULT hres;
543 TRACE("(%p) %d\n", prot, error);
545 if(prot->flags & FLAG_FIRST_CONTINUE_COMPLETE) {
546 FIXME("Not handling error %d\n", error);
547 return;
550 while((hres = handle_http_error(This, error)) == RPC_E_RETRY) {
551 error = send_http_request(This);
553 if(error == ERROR_IO_PENDING || error == ERROR_SUCCESS)
554 return;
557 protocol_abort(prot, hres);
558 protocol_close_connection(prot);
559 return;
562 static const ProtocolVtbl AsyncProtocolVtbl = {
563 HttpProtocol_open_request,
564 HttpProtocol_end_request,
565 HttpProtocol_start_downloading,
566 HttpProtocol_close_connection,
567 HttpProtocol_on_error
570 static HRESULT WINAPI HttpProtocol_QueryInterface(IInternetProtocolEx *iface, REFIID riid, void **ppv)
572 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
574 *ppv = NULL;
575 if(IsEqualGUID(&IID_IUnknown, riid)) {
576 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
577 *ppv = &This->IInternetProtocolEx_iface;
578 }else if(IsEqualGUID(&IID_IInternetProtocolRoot, riid)) {
579 TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", This, ppv);
580 *ppv = &This->IInternetProtocolEx_iface;
581 }else if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
582 TRACE("(%p)->(IID_IInternetProtocol %p)\n", This, ppv);
583 *ppv = &This->IInternetProtocolEx_iface;
584 }else if(IsEqualGUID(&IID_IInternetProtocolEx, riid)) {
585 TRACE("(%p)->(IID_IInternetProtocolEx %p)\n", This, ppv);
586 *ppv = &This->IInternetProtocolEx_iface;
587 }else if(IsEqualGUID(&IID_IInternetPriority, riid)) {
588 TRACE("(%p)->(IID_IInternetPriority %p)\n", This, ppv);
589 *ppv = &This->IInternetPriority_iface;
590 }else if(IsEqualGUID(&IID_IWinInetInfo, riid)) {
591 TRACE("(%p)->(IID_IWinInetInfo %p)\n", This, ppv);
592 *ppv = &This->IWinInetHttpInfo_iface;
593 }else if(IsEqualGUID(&IID_IWinInetHttpInfo, riid)) {
594 TRACE("(%p)->(IID_IWinInetHttpInfo %p)\n", This, ppv);
595 *ppv = &This->IWinInetHttpInfo_iface;
598 if(*ppv) {
599 IInternetProtocol_AddRef(iface);
600 return S_OK;
603 WARN("not supported interface %s\n", debugstr_guid(riid));
604 return E_NOINTERFACE;
607 static ULONG WINAPI HttpProtocol_AddRef(IInternetProtocolEx *iface)
609 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
610 LONG ref = InterlockedIncrement(&This->ref);
611 TRACE("(%p) ref=%d\n", This, ref);
612 return ref;
615 static ULONG WINAPI HttpProtocol_Release(IInternetProtocolEx *iface)
617 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
618 LONG ref = InterlockedDecrement(&This->ref);
620 TRACE("(%p) ref=%d\n", This, ref);
622 if(!ref) {
623 protocol_close_connection(&This->base);
624 heap_free(This);
626 URLMON_UnlockModule();
629 return ref;
632 static HRESULT WINAPI HttpProtocol_Start(IInternetProtocolEx *iface, LPCWSTR szUrl,
633 IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
634 DWORD grfPI, HANDLE_PTR dwReserved)
636 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
637 IUri *uri;
638 HRESULT hres;
640 TRACE("(%p)->(%s %p %p %08x %lx)\n", This, debugstr_w(szUrl), pOIProtSink,
641 pOIBindInfo, grfPI, dwReserved);
643 hres = CreateUri(szUrl, 0, 0, &uri);
644 if(FAILED(hres))
645 return hres;
647 hres = IInternetProtocolEx_StartEx(&This->IInternetProtocolEx_iface, uri, pOIProtSink,
648 pOIBindInfo, grfPI, (HANDLE*)dwReserved);
650 IUri_Release(uri);
651 return hres;
654 static HRESULT WINAPI HttpProtocol_Continue(IInternetProtocolEx *iface, PROTOCOLDATA *pProtocolData)
656 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
658 TRACE("(%p)->(%p)\n", This, pProtocolData);
660 return protocol_continue(&This->base, pProtocolData);
663 static HRESULT WINAPI HttpProtocol_Abort(IInternetProtocolEx *iface, HRESULT hrReason,
664 DWORD dwOptions)
666 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
668 TRACE("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);
670 return protocol_abort(&This->base, hrReason);
673 static HRESULT WINAPI HttpProtocol_Terminate(IInternetProtocolEx *iface, DWORD dwOptions)
675 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
677 TRACE("(%p)->(%08x)\n", This, dwOptions);
679 protocol_close_connection(&This->base);
680 return S_OK;
683 static HRESULT WINAPI HttpProtocol_Suspend(IInternetProtocolEx *iface)
685 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
686 FIXME("(%p)\n", This);
687 return E_NOTIMPL;
690 static HRESULT WINAPI HttpProtocol_Resume(IInternetProtocolEx *iface)
692 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
693 FIXME("(%p)\n", This);
694 return E_NOTIMPL;
697 static HRESULT WINAPI HttpProtocol_Read(IInternetProtocolEx *iface, void *pv,
698 ULONG cb, ULONG *pcbRead)
700 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
702 TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
704 return protocol_read(&This->base, pv, cb, pcbRead);
707 static HRESULT WINAPI HttpProtocol_Seek(IInternetProtocolEx *iface, LARGE_INTEGER dlibMove,
708 DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
710 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
711 FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
712 return E_NOTIMPL;
715 static HRESULT WINAPI HttpProtocol_LockRequest(IInternetProtocolEx *iface, DWORD dwOptions)
717 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
719 TRACE("(%p)->(%08x)\n", This, dwOptions);
721 return protocol_lock_request(&This->base);
724 static HRESULT WINAPI HttpProtocol_UnlockRequest(IInternetProtocolEx *iface)
726 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
728 TRACE("(%p)\n", This);
730 return protocol_unlock_request(&This->base);
733 static HRESULT WINAPI HttpProtocol_StartEx(IInternetProtocolEx *iface, IUri *pUri,
734 IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
735 DWORD grfPI, HANDLE *dwReserved)
737 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
738 DWORD scheme = 0;
739 HRESULT hres;
741 TRACE("(%p)->(%p %p %p %08x %p)\n", This, pUri, pOIProtSink,
742 pOIBindInfo, grfPI, dwReserved);
744 hres = IUri_GetScheme(pUri, &scheme);
745 if(FAILED(hres))
746 return hres;
747 if(scheme != (This->https ? URL_SCHEME_HTTPS : URL_SCHEME_HTTP))
748 return MK_E_SYNTAX;
750 return protocol_start(&This->base, (IInternetProtocol*)&This->IInternetProtocolEx_iface, pUri,
751 pOIProtSink, pOIBindInfo);
754 static const IInternetProtocolExVtbl HttpProtocolVtbl = {
755 HttpProtocol_QueryInterface,
756 HttpProtocol_AddRef,
757 HttpProtocol_Release,
758 HttpProtocol_Start,
759 HttpProtocol_Continue,
760 HttpProtocol_Abort,
761 HttpProtocol_Terminate,
762 HttpProtocol_Suspend,
763 HttpProtocol_Resume,
764 HttpProtocol_Read,
765 HttpProtocol_Seek,
766 HttpProtocol_LockRequest,
767 HttpProtocol_UnlockRequest,
768 HttpProtocol_StartEx
771 static HRESULT WINAPI HttpPriority_QueryInterface(IInternetPriority *iface, REFIID riid, void **ppv)
773 HttpProtocol *This = impl_from_IInternetPriority(iface);
774 return IInternetProtocolEx_QueryInterface(&This->IInternetProtocolEx_iface, riid, ppv);
777 static ULONG WINAPI HttpPriority_AddRef(IInternetPriority *iface)
779 HttpProtocol *This = impl_from_IInternetPriority(iface);
780 return IInternetProtocolEx_AddRef(&This->IInternetProtocolEx_iface);
783 static ULONG WINAPI HttpPriority_Release(IInternetPriority *iface)
785 HttpProtocol *This = impl_from_IInternetPriority(iface);
786 return IInternetProtocolEx_Release(&This->IInternetProtocolEx_iface);
789 static HRESULT WINAPI HttpPriority_SetPriority(IInternetPriority *iface, LONG nPriority)
791 HttpProtocol *This = impl_from_IInternetPriority(iface);
793 TRACE("(%p)->(%d)\n", This, nPriority);
795 This->base.priority = nPriority;
796 return S_OK;
799 static HRESULT WINAPI HttpPriority_GetPriority(IInternetPriority *iface, LONG *pnPriority)
801 HttpProtocol *This = impl_from_IInternetPriority(iface);
803 TRACE("(%p)->(%p)\n", This, pnPriority);
805 *pnPriority = This->base.priority;
806 return S_OK;
809 static const IInternetPriorityVtbl HttpPriorityVtbl = {
810 HttpPriority_QueryInterface,
811 HttpPriority_AddRef,
812 HttpPriority_Release,
813 HttpPriority_SetPriority,
814 HttpPriority_GetPriority
817 static HRESULT WINAPI HttpInfo_QueryInterface(IWinInetHttpInfo *iface, REFIID riid, void **ppv)
819 HttpProtocol *This = impl_from_IWinInetHttpInfo(iface);
820 return IInternetProtocolEx_QueryInterface(&This->IInternetProtocolEx_iface, riid, ppv);
823 static ULONG WINAPI HttpInfo_AddRef(IWinInetHttpInfo *iface)
825 HttpProtocol *This = impl_from_IWinInetHttpInfo(iface);
826 return IInternetProtocolEx_AddRef(&This->IInternetProtocolEx_iface);
829 static ULONG WINAPI HttpInfo_Release(IWinInetHttpInfo *iface)
831 HttpProtocol *This = impl_from_IWinInetHttpInfo(iface);
832 return IInternetProtocolEx_Release(&This->IInternetProtocolEx_iface);
835 static HRESULT WINAPI HttpInfo_QueryOption(IWinInetHttpInfo *iface, DWORD dwOption,
836 void *pBuffer, DWORD *pcbBuffer)
838 HttpProtocol *This = impl_from_IWinInetHttpInfo(iface);
839 FIXME("(%p)->(%x %p %p)\n", This, dwOption, pBuffer, pcbBuffer);
840 return E_NOTIMPL;
843 static HRESULT WINAPI HttpInfo_QueryInfo(IWinInetHttpInfo *iface, DWORD dwOption,
844 void *pBuffer, DWORD *pcbBuffer, DWORD *pdwFlags, DWORD *pdwReserved)
846 HttpProtocol *This = impl_from_IWinInetHttpInfo(iface);
847 FIXME("(%p)->(%x %p %p %p %p)\n", This, dwOption, pBuffer, pcbBuffer, pdwFlags, pdwReserved);
848 return E_NOTIMPL;
851 static const IWinInetHttpInfoVtbl WinInetHttpInfoVtbl = {
852 HttpInfo_QueryInterface,
853 HttpInfo_AddRef,
854 HttpInfo_Release,
855 HttpInfo_QueryOption,
856 HttpInfo_QueryInfo
859 static HRESULT create_http_protocol(BOOL https, void **ppobj)
861 HttpProtocol *ret;
863 ret = heap_alloc_zero(sizeof(HttpProtocol));
864 if(!ret)
865 return E_OUTOFMEMORY;
867 ret->base.vtbl = &AsyncProtocolVtbl;
868 ret->IInternetProtocolEx_iface.lpVtbl = &HttpProtocolVtbl;
869 ret->IInternetPriority_iface.lpVtbl = &HttpPriorityVtbl;
870 ret->IWinInetHttpInfo_iface.lpVtbl = &WinInetHttpInfoVtbl;
872 ret->https = https;
873 ret->ref = 1;
875 *ppobj = &ret->IInternetProtocolEx_iface;
877 URLMON_LockModule();
878 return S_OK;
881 HRESULT HttpProtocol_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
883 TRACE("(%p %p)\n", pUnkOuter, ppobj);
885 return create_http_protocol(FALSE, ppobj);
888 HRESULT HttpSProtocol_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
890 TRACE("(%p %p)\n", pUnkOuter, ppobj);
892 return create_http_protocol(TRUE, ppobj);