* Sync up to trunk HEAD (r62975).
[reactos.git] / dll / win32 / urlmon / http.c
1 /*
2 * Copyright 2005 Jacek Caban
3 * Copyright 2007 Misha Koshelev
4 *
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.
9 *
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.
14 *
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
18 */
19
20 #include "urlmon_main.h"
21
22 typedef struct {
23 Protocol base;
24
25 IInternetProtocolEx IInternetProtocolEx_iface;
26 IInternetPriority IInternetPriority_iface;
27 IWinInetHttpInfo IWinInetHttpInfo_iface;
28
29 BOOL https;
30 IHttpNegotiate *http_negotiate;
31 WCHAR *full_header;
32
33 LONG ref;
34 } HttpProtocol;
35
36 static inline HttpProtocol *impl_from_IInternetProtocolEx(IInternetProtocolEx *iface)
37 {
38 return CONTAINING_RECORD(iface, HttpProtocol, IInternetProtocolEx_iface);
39 }
40
41 static inline HttpProtocol *impl_from_IInternetPriority(IInternetPriority *iface)
42 {
43 return CONTAINING_RECORD(iface, HttpProtocol, IInternetPriority_iface);
44 }
45
46 static inline HttpProtocol *impl_from_IWinInetHttpInfo(IWinInetHttpInfo *iface)
47 {
48 return CONTAINING_RECORD(iface, HttpProtocol, IWinInetHttpInfo_iface);
49 }
50
51 static const WCHAR default_headersW[] = {
52 '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};
53
54 static LPWSTR query_http_info(HttpProtocol *This, DWORD option)
55 {
56 LPWSTR ret = NULL;
57 DWORD len = 0;
58 BOOL res;
59
60 res = HttpQueryInfoW(This->base.request, option, NULL, &len, NULL);
61 if (!res && GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
62 ret = heap_alloc(len);
63 res = HttpQueryInfoW(This->base.request, option, ret, &len, NULL);
64 }
65 if(!res) {
66 TRACE("HttpQueryInfoW(%d) failed: %08x\n", option, GetLastError());
67 heap_free(ret);
68 return NULL;
69 }
70
71 return ret;
72 }
73
74 static inline BOOL set_security_flag(HttpProtocol *This, DWORD flags)
75 {
76 BOOL res;
77
78 res = InternetSetOptionW(This->base.request, INTERNET_OPTION_SECURITY_FLAGS, &flags, sizeof(flags));
79 if(!res)
80 ERR("Failed to set security flags: %x\n", flags);
81
82 return res;
83 }
84
85 static inline HRESULT internet_error_to_hres(DWORD error)
86 {
87 switch(error)
88 {
89 case ERROR_INTERNET_SEC_CERT_DATE_INVALID:
90 case ERROR_INTERNET_SEC_CERT_CN_INVALID:
91 case ERROR_INTERNET_INVALID_CA:
92 case ERROR_INTERNET_CLIENT_AUTH_CERT_NEEDED:
93 case ERROR_INTERNET_SEC_INVALID_CERT:
94 case ERROR_INTERNET_SEC_CERT_ERRORS:
95 case ERROR_INTERNET_SEC_CERT_REV_FAILED:
96 case ERROR_INTERNET_SEC_CERT_NO_REV:
97 case ERROR_INTERNET_SEC_CERT_REVOKED:
98 return INET_E_INVALID_CERTIFICATE;
99 case ERROR_INTERNET_HTTP_TO_HTTPS_ON_REDIR:
100 case ERROR_INTERNET_HTTPS_TO_HTTP_ON_REDIR:
101 case ERROR_HTTP_REDIRECT_NEEDS_CONFIRMATION:
102 return INET_E_REDIRECT_FAILED;
103 default:
104 return INET_E_DOWNLOAD_FAILURE;
105 }
106 }
107
108 static HRESULT handle_http_error(HttpProtocol *This, DWORD error)
109 {
110 IServiceProvider *serv_prov;
111 IWindowForBindingUI *wfb_ui;
112 IHttpSecurity *http_security;
113 BOOL security_problem;
114 DWORD dlg_flags;
115 HWND hwnd;
116 DWORD res;
117 HRESULT hres;
118
119 TRACE("(%p %u)\n", This, error);
120
121 switch(error) {
122 case ERROR_INTERNET_SEC_CERT_DATE_INVALID:
123 case ERROR_INTERNET_SEC_CERT_CN_INVALID:
124 case ERROR_INTERNET_HTTP_TO_HTTPS_ON_REDIR:
125 case ERROR_INTERNET_HTTPS_TO_HTTP_ON_REDIR:
126 case ERROR_INTERNET_INVALID_CA:
127 case ERROR_INTERNET_CLIENT_AUTH_CERT_NEEDED:
128 case ERROR_INTERNET_SEC_INVALID_CERT:
129 case ERROR_INTERNET_SEC_CERT_ERRORS:
130 case ERROR_INTERNET_SEC_CERT_REV_FAILED:
131 case ERROR_INTERNET_SEC_CERT_NO_REV:
132 case ERROR_INTERNET_SEC_CERT_REVOKED:
133 case ERROR_HTTP_REDIRECT_NEEDS_CONFIRMATION:
134 security_problem = TRUE;
135 break;
136 default:
137 security_problem = FALSE;
138 }
139
140 hres = IInternetProtocolSink_QueryInterface(This->base.protocol_sink, &IID_IServiceProvider,
141 (void**)&serv_prov);
142 if(FAILED(hres)) {
143 ERR("Failed to get IServiceProvider.\n");
144 return E_ABORT;
145 }
146
147 if(security_problem) {
148 hres = IServiceProvider_QueryService(serv_prov, &IID_IHttpSecurity, &IID_IHttpSecurity,
149 (void**)&http_security);
150 if(SUCCEEDED(hres)) {
151 hres = IHttpSecurity_OnSecurityProblem(http_security, error);
152 IHttpSecurity_Release(http_security);
153
154 TRACE("OnSecurityProblem returned %08x\n", hres);
155
156 if(hres != S_FALSE)
157 {
158 BOOL res = FALSE;
159
160 IServiceProvider_Release(serv_prov);
161
162 if(hres == S_OK) {
163 if(error == ERROR_INTERNET_SEC_CERT_DATE_INVALID)
164 res = set_security_flag(This, SECURITY_FLAG_IGNORE_CERT_DATE_INVALID);
165 else if(error == ERROR_INTERNET_SEC_CERT_CN_INVALID)
166 res = set_security_flag(This, SECURITY_FLAG_IGNORE_CERT_CN_INVALID);
167 else if(error == ERROR_INTERNET_INVALID_CA)
168 res = set_security_flag(This, SECURITY_FLAG_IGNORE_UNKNOWN_CA);
169
170 if(res)
171 return RPC_E_RETRY;
172
173 FIXME("Don't know how to ignore error %d\n", error);
174 return E_ABORT;
175 }
176
177 if(hres == E_ABORT)
178 return E_ABORT;
179 if(hres == RPC_E_RETRY)
180 return RPC_E_RETRY;
181
182 return internet_error_to_hres(error);
183 }
184 }
185 }
186
187 switch(error) {
188 case ERROR_INTERNET_SEC_CERT_REV_FAILED:
189 if(hres != S_FALSE) {
190 /* Silently ignore the error. We will get more detailed error from wininet anyway. */
191 set_security_flag(This, SECURITY_FLAG_IGNORE_REVOCATION);
192 hres = RPC_E_RETRY;
193 break;
194 }
195 /* fallthrough */
196 default:
197 hres = IServiceProvider_QueryService(serv_prov, &IID_IWindowForBindingUI, &IID_IWindowForBindingUI, (void**)&wfb_ui);
198 if(SUCCEEDED(hres)) {
199 const IID *iid_reason;
200
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;
207
208 hres = IWindowForBindingUI_GetWindow(wfb_ui, iid_reason, &hwnd);
209 IWindowForBindingUI_Release(wfb_ui);
210 }
211
212 if(FAILED(hres)) hwnd = NULL;
213
214 dlg_flags = FLAGS_ERROR_UI_FLAGS_CHANGE_OPTIONS | FLAGS_ERROR_UI_FLAGS_GENERATE_DATA;
215 if(This->base.bindf & BINDF_NO_UI)
216 dlg_flags |= FLAGS_ERROR_UI_FLAGS_NO_UI;
217
218 res = InternetErrorDlg(hwnd, This->base.request, error, dlg_flags, NULL);
219 hres = res == ERROR_INTERNET_FORCE_RETRY || res == ERROR_SUCCESS ? RPC_E_RETRY : internet_error_to_hres(error);
220 }
221
222 IServiceProvider_Release(serv_prov);
223 return hres;
224 }
225
226 static ULONG send_http_request(HttpProtocol *This)
227 {
228 INTERNET_BUFFERSW send_buffer = {sizeof(INTERNET_BUFFERSW)};
229 BOOL res;
230
231 send_buffer.lpcszHeader = This->full_header;
232 send_buffer.dwHeadersLength = send_buffer.dwHeadersTotal = strlenW(This->full_header);
233
234 if(This->base.bind_info.dwBindVerb != BINDVERB_GET) {
235 switch(This->base.bind_info.stgmedData.tymed) {
236 case TYMED_HGLOBAL:
237 /* Native does not use GlobalLock/GlobalUnlock, so we won't either */
238 send_buffer.lpvBuffer = This->base.bind_info.stgmedData.u.hGlobal;
239 send_buffer.dwBufferLength = send_buffer.dwBufferTotal = This->base.bind_info.cbstgmedData;
240 break;
241 case TYMED_ISTREAM: {
242 LARGE_INTEGER offset;
243
244 send_buffer.dwBufferTotal = This->base.bind_info.cbstgmedData;
245 if(!This->base.post_stream) {
246 This->base.post_stream = This->base.bind_info.stgmedData.u.pstm;
247 IStream_AddRef(This->base.post_stream);
248 }
249
250 offset.QuadPart = 0;
251 IStream_Seek(This->base.post_stream, offset, STREAM_SEEK_SET, NULL);
252 break;
253 }
254 default:
255 FIXME("Unsupported This->base.bind_info.stgmedData.tymed %d\n", This->base.bind_info.stgmedData.tymed);
256 }
257 }
258
259 if(This->base.post_stream)
260 res = HttpSendRequestExW(This->base.request, &send_buffer, NULL, 0, 0);
261 else
262 res = HttpSendRequestW(This->base.request, send_buffer.lpcszHeader, send_buffer.dwHeadersLength,
263 send_buffer.lpvBuffer, send_buffer.dwBufferLength);
264
265 return res ? 0 : GetLastError();
266 }
267
268 static inline HttpProtocol *impl_from_Protocol(Protocol *prot)
269 {
270 return CONTAINING_RECORD(prot, HttpProtocol, base);
271 }
272
273 static HRESULT HttpProtocol_open_request(Protocol *prot, IUri *uri, DWORD request_flags,
274 HINTERNET internet_session, IInternetBindInfo *bind_info)
275 {
276 HttpProtocol *This = impl_from_Protocol(prot);
277 WCHAR *addl_header = NULL, *post_cookie = NULL, *rootdoc_url = NULL;
278 IServiceProvider *service_provider = NULL;
279 IHttpNegotiate2 *http_negotiate2 = NULL;
280 BSTR url, host, user, pass, path;
281 LPOLESTR accept_mimes[257];
282 const WCHAR **accept_types;
283 BYTE security_id[512];
284 DWORD len, port, flags;
285 ULONG num, error;
286 BOOL res, b;
287 HRESULT hres;
288
289 static const WCHAR wszBindVerb[BINDVERB_CUSTOM][5] =
290 {{'G','E','T',0},
291 {'P','O','S','T',0},
292 {'P','U','T',0}};
293
294 hres = IUri_GetPort(uri, &port);
295 if(FAILED(hres))
296 return hres;
297
298 hres = IUri_GetHost(uri, &host);
299 if(FAILED(hres))
300 return hres;
301
302 hres = IUri_GetUserName(uri, &user);
303 if(SUCCEEDED(hres)) {
304 hres = IUri_GetPassword(uri, &pass);
305
306 if(SUCCEEDED(hres)) {
307 This->base.connection = InternetConnectW(internet_session, host, port, user, pass,
308 INTERNET_SERVICE_HTTP, This->https ? INTERNET_FLAG_SECURE : 0, (DWORD_PTR)&This->base);
309 SysFreeString(pass);
310 }
311 SysFreeString(user);
312 }
313 SysFreeString(host);
314 if(FAILED(hres))
315 return hres;
316 if(!This->base.connection) {
317 WARN("InternetConnect failed: %d\n", GetLastError());
318 return INET_E_CANNOT_CONNECT;
319 }
320
321 num = 0;
322 hres = IInternetBindInfo_GetBindString(bind_info, BINDSTRING_ROOTDOC_URL, &rootdoc_url, 1, &num);
323 if(hres == S_OK && num) {
324 FIXME("Use root doc URL %s\n", debugstr_w(rootdoc_url));
325 CoTaskMemFree(rootdoc_url);
326 }
327
328 num = sizeof(accept_mimes)/sizeof(accept_mimes[0])-1;
329 hres = IInternetBindInfo_GetBindString(bind_info, BINDSTRING_ACCEPT_MIMES, accept_mimes, num, &num);
330 if(hres == INET_E_USE_DEFAULT_SETTING) {
331 static const WCHAR default_accept_mimeW[] = {'*','/','*',0};
332 static const WCHAR *default_accept_mimes[] = {default_accept_mimeW, NULL};
333
334 accept_types = default_accept_mimes;
335 num = 0;
336 }else if(hres == S_OK) {
337 accept_types = (const WCHAR**)accept_mimes;
338 }else {
339 WARN("GetBindString BINDSTRING_ACCEPT_MIMES failed: %08x\n", hres);
340 return INET_E_NO_VALID_MEDIA;
341 }
342 accept_mimes[num] = 0;
343
344 if(This->https)
345 request_flags |= INTERNET_FLAG_SECURE;
346
347 hres = IUri_GetPathAndQuery(uri, &path);
348 if(SUCCEEDED(hres)) {
349 This->base.request = HttpOpenRequestW(This->base.connection,
350 This->base.bind_info.dwBindVerb < BINDVERB_CUSTOM
351 ? wszBindVerb[This->base.bind_info.dwBindVerb] : This->base.bind_info.szCustomVerb,
352 path, NULL, NULL, accept_types, request_flags, (DWORD_PTR)&This->base);
353 SysFreeString(path);
354 }
355 while(num--)
356 CoTaskMemFree(accept_mimes[num]);
357 if(FAILED(hres))
358 return hres;
359 if (!This->base.request) {
360 WARN("HttpOpenRequest failed: %d\n", GetLastError());
361 return INET_E_RESOURCE_NOT_FOUND;
362 }
363
364 hres = IInternetProtocolSink_QueryInterface(This->base.protocol_sink, &IID_IServiceProvider,
365 (void **)&service_provider);
366 if (hres != S_OK) {
367 WARN("IInternetProtocolSink_QueryInterface IID_IServiceProvider failed: %08x\n", hres);
368 return hres;
369 }
370
371 hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate,
372 &IID_IHttpNegotiate, (void **)&This->http_negotiate);
373 if (hres != S_OK) {
374 WARN("IServiceProvider_QueryService IID_IHttpNegotiate failed: %08x\n", hres);
375 IServiceProvider_Release(service_provider);
376 return hres;
377 }
378
379 hres = IUri_GetAbsoluteUri(uri, &url);
380 if(FAILED(hres)) {
381 IServiceProvider_Release(service_provider);
382 return hres;
383 }
384
385 hres = IHttpNegotiate_BeginningTransaction(This->http_negotiate, url, default_headersW,
386 0, &addl_header);
387 SysFreeString(url);
388 if(hres != S_OK) {
389 WARN("IHttpNegotiate_BeginningTransaction failed: %08x\n", hres);
390 IServiceProvider_Release(service_provider);
391 return hres;
392 }
393
394 len = addl_header ? strlenW(addl_header) : 0;
395
396 This->full_header = heap_alloc(len*sizeof(WCHAR)+sizeof(default_headersW));
397 if(!This->full_header) {
398 IServiceProvider_Release(service_provider);
399 return E_OUTOFMEMORY;
400 }
401
402 if(len)
403 memcpy(This->full_header, addl_header, len*sizeof(WCHAR));
404 CoTaskMemFree(addl_header);
405 memcpy(This->full_header+len, default_headersW, sizeof(default_headersW));
406
407 hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate2,
408 &IID_IHttpNegotiate2, (void **)&http_negotiate2);
409 IServiceProvider_Release(service_provider);
410 if(hres != S_OK) {
411 WARN("IServiceProvider_QueryService IID_IHttpNegotiate2 failed: %08x\n", hres);
412 /* No goto done as per native */
413 }else {
414 len = sizeof(security_id)/sizeof(security_id[0]);
415 hres = IHttpNegotiate2_GetRootSecurityId(http_negotiate2, security_id, &len, 0);
416 IHttpNegotiate2_Release(http_negotiate2);
417 if (hres != S_OK)
418 WARN("IHttpNegotiate2_GetRootSecurityId failed: %08x\n", hres);
419 }
420
421 /* FIXME: Handle security_id. Native calls undocumented function IsHostInProxyBypassList. */
422
423 if(This->base.bind_info.dwBindVerb == BINDVERB_POST) {
424 num = 0;
425 hres = IInternetBindInfo_GetBindString(bind_info, BINDSTRING_POST_COOKIE, &post_cookie, 1, &num);
426 if(hres == S_OK && num) {
427 if(!InternetSetOptionW(This->base.request, INTERNET_OPTION_SECONDARY_CACHE_KEY,
428 post_cookie, lstrlenW(post_cookie)))
429 WARN("InternetSetOption INTERNET_OPTION_SECONDARY_CACHE_KEY failed: %d\n", GetLastError());
430 CoTaskMemFree(post_cookie);
431 }
432 }
433
434 flags = INTERNET_ERROR_MASK_COMBINED_SEC_CERT;
435 res = InternetSetOptionW(This->base.request, INTERNET_OPTION_ERROR_MASK, &flags, sizeof(flags));
436 if(!res)
437 WARN("InternetSetOption(INTERNET_OPTION_ERROR_MASK) failed: %u\n", GetLastError());
438
439 b = TRUE;
440 res = InternetSetOptionW(This->base.request, INTERNET_OPTION_HTTP_DECODING, &b, sizeof(b));
441 if(!res)
442 WARN("InternetSetOption(INTERNET_OPTION_HTTP_DECODING) failed: %u\n", GetLastError());
443
444 do {
445 error = send_http_request(This);
446
447 switch(error) {
448 case ERROR_IO_PENDING:
449 return S_OK;
450 case ERROR_SUCCESS:
451 /*
452 * If sending response ended synchronously, it means that we have the whole data
453 * available locally (most likely in cache).
454 */
455 return protocol_syncbinding(&This->base);
456 default:
457 hres = handle_http_error(This, error);
458 }
459 } while(hres == RPC_E_RETRY);
460
461 WARN("HttpSendRequest failed: %d\n", error);
462 return hres;
463 }
464
465 static HRESULT HttpProtocol_end_request(Protocol *protocol)
466 {
467 BOOL res;
468
469 res = HttpEndRequestW(protocol->request, NULL, 0, 0);
470 if(!res && GetLastError() != ERROR_IO_PENDING) {
471 FIXME("HttpEndRequest failed: %u\n", GetLastError());
472 return E_FAIL;
473 }
474
475 return S_OK;
476 }
477
478 static HRESULT HttpProtocol_start_downloading(Protocol *prot)
479 {
480 HttpProtocol *This = impl_from_Protocol(prot);
481 LPWSTR content_type, content_length, ranges;
482 DWORD len = sizeof(DWORD);
483 DWORD status_code;
484 BOOL res;
485 HRESULT hres;
486
487 static const WCHAR wszDefaultContentType[] =
488 {'t','e','x','t','/','h','t','m','l',0};
489
490 if(!This->http_negotiate) {
491 WARN("Expected IHttpNegotiate pointer to be non-NULL\n");
492 return S_OK;
493 }
494
495 res = HttpQueryInfoW(This->base.request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER,
496 &status_code, &len, NULL);
497 if(res) {
498 LPWSTR response_headers = query_http_info(This, HTTP_QUERY_RAW_HEADERS_CRLF);
499 if(response_headers) {
500 hres = IHttpNegotiate_OnResponse(This->http_negotiate, status_code, response_headers,
501 NULL, NULL);
502 heap_free(response_headers);
503 if (hres != S_OK) {
504 WARN("IHttpNegotiate_OnResponse failed: %08x\n", hres);
505 return S_OK;
506 }
507 }
508 }else {
509 WARN("HttpQueryInfo failed: %d\n", GetLastError());
510 }
511
512 ranges = query_http_info(This, HTTP_QUERY_ACCEPT_RANGES);
513 if(ranges) {
514 IInternetProtocolSink_ReportProgress(This->base.protocol_sink, BINDSTATUS_ACCEPTRANGES, NULL);
515 heap_free(ranges);
516 }
517
518 content_type = query_http_info(This, HTTP_QUERY_CONTENT_TYPE);
519 if(content_type) {
520 /* remove the charset, if present */
521 LPWSTR p = strchrW(content_type, ';');
522 if (p) *p = '\0';
523
524 IInternetProtocolSink_ReportProgress(This->base.protocol_sink,
525 (This->base.bindf & BINDF_FROMURLMON)
526 ? BINDSTATUS_MIMETYPEAVAILABLE : BINDSTATUS_RAWMIMETYPE,
527 content_type);
528 heap_free(content_type);
529 }else {
530 WARN("HttpQueryInfo failed: %d\n", GetLastError());
531 IInternetProtocolSink_ReportProgress(This->base.protocol_sink,
532 (This->base.bindf & BINDF_FROMURLMON)
533 ? BINDSTATUS_MIMETYPEAVAILABLE : BINDSTATUS_RAWMIMETYPE,
534 wszDefaultContentType);
535 }
536
537 content_length = query_http_info(This, HTTP_QUERY_CONTENT_LENGTH);
538 if(content_length) {
539 This->base.content_length = atoiW(content_length);
540 heap_free(content_length);
541 }
542
543 return S_OK;
544 }
545
546 static void HttpProtocol_close_connection(Protocol *prot)
547 {
548 HttpProtocol *This = impl_from_Protocol(prot);
549
550 if(This->http_negotiate) {
551 IHttpNegotiate_Release(This->http_negotiate);
552 This->http_negotiate = NULL;
553 }
554
555 heap_free(This->full_header);
556 This->full_header = NULL;
557 }
558
559 static void HttpProtocol_on_error(Protocol *prot, DWORD error)
560 {
561 HttpProtocol *This = impl_from_Protocol(prot);
562 HRESULT hres;
563
564 TRACE("(%p) %d\n", prot, error);
565
566 if(prot->flags & FLAG_FIRST_CONTINUE_COMPLETE) {
567 FIXME("Not handling error %d\n", error);
568 return;
569 }
570
571 while((hres = handle_http_error(This, error)) == RPC_E_RETRY) {
572 error = send_http_request(This);
573
574 if(error == ERROR_IO_PENDING || error == ERROR_SUCCESS)
575 return;
576 }
577
578 protocol_abort(prot, hres);
579 protocol_close_connection(prot);
580 return;
581 }
582
583 static const ProtocolVtbl AsyncProtocolVtbl = {
584 HttpProtocol_open_request,
585 HttpProtocol_end_request,
586 HttpProtocol_start_downloading,
587 HttpProtocol_close_connection,
588 HttpProtocol_on_error
589 };
590
591 static HRESULT WINAPI HttpProtocol_QueryInterface(IInternetProtocolEx *iface, REFIID riid, void **ppv)
592 {
593 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
594
595 *ppv = NULL;
596 if(IsEqualGUID(&IID_IUnknown, riid)) {
597 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
598 *ppv = &This->IInternetProtocolEx_iface;
599 }else if(IsEqualGUID(&IID_IInternetProtocolRoot, riid)) {
600 TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", This, ppv);
601 *ppv = &This->IInternetProtocolEx_iface;
602 }else if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
603 TRACE("(%p)->(IID_IInternetProtocol %p)\n", This, ppv);
604 *ppv = &This->IInternetProtocolEx_iface;
605 }else if(IsEqualGUID(&IID_IInternetProtocolEx, riid)) {
606 TRACE("(%p)->(IID_IInternetProtocolEx %p)\n", This, ppv);
607 *ppv = &This->IInternetProtocolEx_iface;
608 }else if(IsEqualGUID(&IID_IInternetPriority, riid)) {
609 TRACE("(%p)->(IID_IInternetPriority %p)\n", This, ppv);
610 *ppv = &This->IInternetPriority_iface;
611 }else if(IsEqualGUID(&IID_IWinInetInfo, riid)) {
612 TRACE("(%p)->(IID_IWinInetInfo %p)\n", This, ppv);
613 *ppv = &This->IWinInetHttpInfo_iface;
614 }else if(IsEqualGUID(&IID_IWinInetHttpInfo, riid)) {
615 TRACE("(%p)->(IID_IWinInetHttpInfo %p)\n", This, ppv);
616 *ppv = &This->IWinInetHttpInfo_iface;
617 }
618
619 if(*ppv) {
620 IInternetProtocolEx_AddRef(iface);
621 return S_OK;
622 }
623
624 WARN("not supported interface %s\n", debugstr_guid(riid));
625 return E_NOINTERFACE;
626 }
627
628 static ULONG WINAPI HttpProtocol_AddRef(IInternetProtocolEx *iface)
629 {
630 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
631 LONG ref = InterlockedIncrement(&This->ref);
632 TRACE("(%p) ref=%d\n", This, ref);
633 return ref;
634 }
635
636 static ULONG WINAPI HttpProtocol_Release(IInternetProtocolEx *iface)
637 {
638 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
639 LONG ref = InterlockedDecrement(&This->ref);
640
641 TRACE("(%p) ref=%d\n", This, ref);
642
643 if(!ref) {
644 protocol_close_connection(&This->base);
645 heap_free(This);
646
647 URLMON_UnlockModule();
648 }
649
650 return ref;
651 }
652
653 static HRESULT WINAPI HttpProtocol_Start(IInternetProtocolEx *iface, LPCWSTR szUrl,
654 IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
655 DWORD grfPI, HANDLE_PTR dwReserved)
656 {
657 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
658 IUri *uri;
659 HRESULT hres;
660
661 TRACE("(%p)->(%s %p %p %08x %lx)\n", This, debugstr_w(szUrl), pOIProtSink,
662 pOIBindInfo, grfPI, dwReserved);
663
664 hres = CreateUri(szUrl, 0, 0, &uri);
665 if(FAILED(hres))
666 return hres;
667
668 hres = IInternetProtocolEx_StartEx(&This->IInternetProtocolEx_iface, uri, pOIProtSink,
669 pOIBindInfo, grfPI, (HANDLE*)dwReserved);
670
671 IUri_Release(uri);
672 return hres;
673 }
674
675 static HRESULT WINAPI HttpProtocol_Continue(IInternetProtocolEx *iface, PROTOCOLDATA *pProtocolData)
676 {
677 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
678
679 TRACE("(%p)->(%p)\n", This, pProtocolData);
680
681 return protocol_continue(&This->base, pProtocolData);
682 }
683
684 static HRESULT WINAPI HttpProtocol_Abort(IInternetProtocolEx *iface, HRESULT hrReason,
685 DWORD dwOptions)
686 {
687 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
688
689 TRACE("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);
690
691 return protocol_abort(&This->base, hrReason);
692 }
693
694 static HRESULT WINAPI HttpProtocol_Terminate(IInternetProtocolEx *iface, DWORD dwOptions)
695 {
696 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
697
698 TRACE("(%p)->(%08x)\n", This, dwOptions);
699
700 protocol_close_connection(&This->base);
701 return S_OK;
702 }
703
704 static HRESULT WINAPI HttpProtocol_Suspend(IInternetProtocolEx *iface)
705 {
706 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
707 FIXME("(%p)\n", This);
708 return E_NOTIMPL;
709 }
710
711 static HRESULT WINAPI HttpProtocol_Resume(IInternetProtocolEx *iface)
712 {
713 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
714 FIXME("(%p)\n", This);
715 return E_NOTIMPL;
716 }
717
718 static HRESULT WINAPI HttpProtocol_Read(IInternetProtocolEx *iface, void *pv,
719 ULONG cb, ULONG *pcbRead)
720 {
721 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
722
723 TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
724
725 return protocol_read(&This->base, pv, cb, pcbRead);
726 }
727
728 static HRESULT WINAPI HttpProtocol_Seek(IInternetProtocolEx *iface, LARGE_INTEGER dlibMove,
729 DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
730 {
731 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
732 FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
733 return E_NOTIMPL;
734 }
735
736 static HRESULT WINAPI HttpProtocol_LockRequest(IInternetProtocolEx *iface, DWORD dwOptions)
737 {
738 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
739
740 TRACE("(%p)->(%08x)\n", This, dwOptions);
741
742 return protocol_lock_request(&This->base);
743 }
744
745 static HRESULT WINAPI HttpProtocol_UnlockRequest(IInternetProtocolEx *iface)
746 {
747 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
748
749 TRACE("(%p)\n", This);
750
751 return protocol_unlock_request(&This->base);
752 }
753
754 static HRESULT WINAPI HttpProtocol_StartEx(IInternetProtocolEx *iface, IUri *pUri,
755 IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
756 DWORD grfPI, HANDLE *dwReserved)
757 {
758 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
759 DWORD scheme = 0;
760 HRESULT hres;
761
762 TRACE("(%p)->(%p %p %p %08x %p)\n", This, pUri, pOIProtSink,
763 pOIBindInfo, grfPI, dwReserved);
764
765 hres = IUri_GetScheme(pUri, &scheme);
766 if(FAILED(hres))
767 return hres;
768 if(scheme != (This->https ? URL_SCHEME_HTTPS : URL_SCHEME_HTTP))
769 return MK_E_SYNTAX;
770
771 return protocol_start(&This->base, (IInternetProtocol*)&This->IInternetProtocolEx_iface, pUri,
772 pOIProtSink, pOIBindInfo);
773 }
774
775 static const IInternetProtocolExVtbl HttpProtocolVtbl = {
776 HttpProtocol_QueryInterface,
777 HttpProtocol_AddRef,
778 HttpProtocol_Release,
779 HttpProtocol_Start,
780 HttpProtocol_Continue,
781 HttpProtocol_Abort,
782 HttpProtocol_Terminate,
783 HttpProtocol_Suspend,
784 HttpProtocol_Resume,
785 HttpProtocol_Read,
786 HttpProtocol_Seek,
787 HttpProtocol_LockRequest,
788 HttpProtocol_UnlockRequest,
789 HttpProtocol_StartEx
790 };
791
792 static HRESULT WINAPI HttpPriority_QueryInterface(IInternetPriority *iface, REFIID riid, void **ppv)
793 {
794 HttpProtocol *This = impl_from_IInternetPriority(iface);
795 return IInternetProtocolEx_QueryInterface(&This->IInternetProtocolEx_iface, riid, ppv);
796 }
797
798 static ULONG WINAPI HttpPriority_AddRef(IInternetPriority *iface)
799 {
800 HttpProtocol *This = impl_from_IInternetPriority(iface);
801 return IInternetProtocolEx_AddRef(&This->IInternetProtocolEx_iface);
802 }
803
804 static ULONG WINAPI HttpPriority_Release(IInternetPriority *iface)
805 {
806 HttpProtocol *This = impl_from_IInternetPriority(iface);
807 return IInternetProtocolEx_Release(&This->IInternetProtocolEx_iface);
808 }
809
810 static HRESULT WINAPI HttpPriority_SetPriority(IInternetPriority *iface, LONG nPriority)
811 {
812 HttpProtocol *This = impl_from_IInternetPriority(iface);
813
814 TRACE("(%p)->(%d)\n", This, nPriority);
815
816 This->base.priority = nPriority;
817 return S_OK;
818 }
819
820 static HRESULT WINAPI HttpPriority_GetPriority(IInternetPriority *iface, LONG *pnPriority)
821 {
822 HttpProtocol *This = impl_from_IInternetPriority(iface);
823
824 TRACE("(%p)->(%p)\n", This, pnPriority);
825
826 *pnPriority = This->base.priority;
827 return S_OK;
828 }
829
830 static const IInternetPriorityVtbl HttpPriorityVtbl = {
831 HttpPriority_QueryInterface,
832 HttpPriority_AddRef,
833 HttpPriority_Release,
834 HttpPriority_SetPriority,
835 HttpPriority_GetPriority
836 };
837
838 static HRESULT WINAPI HttpInfo_QueryInterface(IWinInetHttpInfo *iface, REFIID riid, void **ppv)
839 {
840 HttpProtocol *This = impl_from_IWinInetHttpInfo(iface);
841 return IInternetProtocolEx_QueryInterface(&This->IInternetProtocolEx_iface, riid, ppv);
842 }
843
844 static ULONG WINAPI HttpInfo_AddRef(IWinInetHttpInfo *iface)
845 {
846 HttpProtocol *This = impl_from_IWinInetHttpInfo(iface);
847 return IInternetProtocolEx_AddRef(&This->IInternetProtocolEx_iface);
848 }
849
850 static ULONG WINAPI HttpInfo_Release(IWinInetHttpInfo *iface)
851 {
852 HttpProtocol *This = impl_from_IWinInetHttpInfo(iface);
853 return IInternetProtocolEx_Release(&This->IInternetProtocolEx_iface);
854 }
855
856 static HRESULT WINAPI HttpInfo_QueryOption(IWinInetHttpInfo *iface, DWORD dwOption,
857 void *pBuffer, DWORD *pcbBuffer)
858 {
859 HttpProtocol *This = impl_from_IWinInetHttpInfo(iface);
860 TRACE("(%p)->(%x %p %p)\n", This, dwOption, pBuffer, pcbBuffer);
861
862 if(!This->base.request)
863 return E_FAIL;
864
865 if(!InternetQueryOptionW(This->base.request, dwOption, pBuffer, pcbBuffer))
866 return S_FALSE;
867 return S_OK;
868 }
869
870 static HRESULT WINAPI HttpInfo_QueryInfo(IWinInetHttpInfo *iface, DWORD dwOption,
871 void *pBuffer, DWORD *pcbBuffer, DWORD *pdwFlags, DWORD *pdwReserved)
872 {
873 HttpProtocol *This = impl_from_IWinInetHttpInfo(iface);
874 TRACE("(%p)->(%x %p %p %p %p)\n", This, dwOption, pBuffer, pcbBuffer, pdwFlags, pdwReserved);
875
876 if(!This->base.request)
877 return E_FAIL;
878
879 if(!HttpQueryInfoW(This->base.request, dwOption, pBuffer, pcbBuffer, pdwFlags)) {
880 if(pBuffer)
881 memset(pBuffer, 0, *pcbBuffer);
882 return S_OK;
883 }
884 return S_OK;
885 }
886
887 static const IWinInetHttpInfoVtbl WinInetHttpInfoVtbl = {
888 HttpInfo_QueryInterface,
889 HttpInfo_AddRef,
890 HttpInfo_Release,
891 HttpInfo_QueryOption,
892 HttpInfo_QueryInfo
893 };
894
895 static HRESULT create_http_protocol(BOOL https, void **ppobj)
896 {
897 HttpProtocol *ret;
898
899 ret = heap_alloc_zero(sizeof(HttpProtocol));
900 if(!ret)
901 return E_OUTOFMEMORY;
902
903 ret->base.vtbl = &AsyncProtocolVtbl;
904 ret->IInternetProtocolEx_iface.lpVtbl = &HttpProtocolVtbl;
905 ret->IInternetPriority_iface.lpVtbl = &HttpPriorityVtbl;
906 ret->IWinInetHttpInfo_iface.lpVtbl = &WinInetHttpInfoVtbl;
907
908 ret->https = https;
909 ret->ref = 1;
910
911 *ppobj = &ret->IInternetProtocolEx_iface;
912
913 URLMON_LockModule();
914 return S_OK;
915 }
916
917 HRESULT HttpProtocol_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
918 {
919 TRACE("(%p %p)\n", pUnkOuter, ppobj);
920
921 return create_http_protocol(FALSE, ppobj);
922 }
923
924 HRESULT HttpSProtocol_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
925 {
926 TRACE("(%p %p)\n", pUnkOuter, ppobj);
927
928 return create_http_protocol(TRUE, ppobj);
929 }