Sync with trunk r62754.
[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 LPWSTR addl_header = NULL, post_cookie = 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 = sizeof(accept_mimes)/sizeof(accept_mimes[0])-1;
322 hres = IInternetBindInfo_GetBindString(bind_info, BINDSTRING_ACCEPT_MIMES, accept_mimes, num, &num);
323 if(hres == INET_E_USE_DEFAULT_SETTING) {
324 static const WCHAR default_accept_mimeW[] = {'*','/','*',0};
325 static const WCHAR *default_accept_mimes[] = {default_accept_mimeW, NULL};
326
327 accept_types = default_accept_mimes;
328 num = 0;
329 }else if(hres == S_OK) {
330 accept_types = (const WCHAR**)accept_mimes;
331 }else {
332 WARN("GetBindString BINDSTRING_ACCEPT_MIMES failed: %08x\n", hres);
333 return INET_E_NO_VALID_MEDIA;
334 }
335 accept_mimes[num] = 0;
336
337 if(This->https)
338 request_flags |= INTERNET_FLAG_SECURE;
339
340 hres = IUri_GetPathAndQuery(uri, &path);
341 if(SUCCEEDED(hres)) {
342 This->base.request = HttpOpenRequestW(This->base.connection,
343 This->base.bind_info.dwBindVerb < BINDVERB_CUSTOM
344 ? wszBindVerb[This->base.bind_info.dwBindVerb] : This->base.bind_info.szCustomVerb,
345 path, NULL, NULL, accept_types, request_flags, (DWORD_PTR)&This->base);
346 SysFreeString(path);
347 }
348 while(num--)
349 CoTaskMemFree(accept_mimes[num]);
350 if(FAILED(hres))
351 return hres;
352 if (!This->base.request) {
353 WARN("HttpOpenRequest failed: %d\n", GetLastError());
354 return INET_E_RESOURCE_NOT_FOUND;
355 }
356
357 hres = IInternetProtocolSink_QueryInterface(This->base.protocol_sink, &IID_IServiceProvider,
358 (void **)&service_provider);
359 if (hres != S_OK) {
360 WARN("IInternetProtocolSink_QueryInterface IID_IServiceProvider failed: %08x\n", hres);
361 return hres;
362 }
363
364 hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate,
365 &IID_IHttpNegotiate, (void **)&This->http_negotiate);
366 if (hres != S_OK) {
367 WARN("IServiceProvider_QueryService IID_IHttpNegotiate failed: %08x\n", hres);
368 IServiceProvider_Release(service_provider);
369 return hres;
370 }
371
372 hres = IUri_GetAbsoluteUri(uri, &url);
373 if(FAILED(hres)) {
374 IServiceProvider_Release(service_provider);
375 return hres;
376 }
377
378 hres = IHttpNegotiate_BeginningTransaction(This->http_negotiate, url, default_headersW,
379 0, &addl_header);
380 SysFreeString(url);
381 if(hres != S_OK) {
382 WARN("IHttpNegotiate_BeginningTransaction failed: %08x\n", hres);
383 IServiceProvider_Release(service_provider);
384 return hres;
385 }
386
387 len = addl_header ? strlenW(addl_header) : 0;
388
389 This->full_header = heap_alloc(len*sizeof(WCHAR)+sizeof(default_headersW));
390 if(!This->full_header) {
391 IServiceProvider_Release(service_provider);
392 return E_OUTOFMEMORY;
393 }
394
395 if(len)
396 memcpy(This->full_header, addl_header, len*sizeof(WCHAR));
397 CoTaskMemFree(addl_header);
398 memcpy(This->full_header+len, default_headersW, sizeof(default_headersW));
399
400 hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate2,
401 &IID_IHttpNegotiate2, (void **)&http_negotiate2);
402 IServiceProvider_Release(service_provider);
403 if(hres != S_OK) {
404 WARN("IServiceProvider_QueryService IID_IHttpNegotiate2 failed: %08x\n", hres);
405 /* No goto done as per native */
406 }else {
407 len = sizeof(security_id)/sizeof(security_id[0]);
408 hres = IHttpNegotiate2_GetRootSecurityId(http_negotiate2, security_id, &len, 0);
409 IHttpNegotiate2_Release(http_negotiate2);
410 if (hres != S_OK)
411 WARN("IHttpNegotiate2_GetRootSecurityId failed: %08x\n", hres);
412 }
413
414 /* FIXME: Handle security_id. Native calls undocumented function IsHostInProxyBypassList. */
415
416 if(This->base.bind_info.dwBindVerb == BINDVERB_POST) {
417 num = 0;
418 hres = IInternetBindInfo_GetBindString(bind_info, BINDSTRING_POST_COOKIE, &post_cookie, 1, &num);
419 if(hres == S_OK && num) {
420 if(!InternetSetOptionW(This->base.request, INTERNET_OPTION_SECONDARY_CACHE_KEY,
421 post_cookie, lstrlenW(post_cookie)))
422 WARN("InternetSetOption INTERNET_OPTION_SECONDARY_CACHE_KEY failed: %d\n", GetLastError());
423 CoTaskMemFree(post_cookie);
424 }
425 }
426
427 flags = INTERNET_ERROR_MASK_COMBINED_SEC_CERT;
428 res = InternetSetOptionW(This->base.request, INTERNET_OPTION_ERROR_MASK, &flags, sizeof(flags));
429 if(!res)
430 WARN("InternetSetOption(INTERNET_OPTION_ERROR_MASK) failed: %u\n", GetLastError());
431
432 b = TRUE;
433 res = InternetSetOptionW(This->base.request, INTERNET_OPTION_HTTP_DECODING, &b, sizeof(b));
434 if(!res)
435 WARN("InternetSetOption(INTERNET_OPTION_HTTP_DECODING) failed: %u\n", GetLastError());
436
437 do {
438 error = send_http_request(This);
439
440 switch(error) {
441 case ERROR_IO_PENDING:
442 return S_OK;
443 case ERROR_SUCCESS:
444 /*
445 * If sending response ended synchronously, it means that we have the whole data
446 * available locally (most likely in cache).
447 */
448 return protocol_syncbinding(&This->base);
449 default:
450 hres = handle_http_error(This, error);
451 }
452 } while(hres == RPC_E_RETRY);
453
454 WARN("HttpSendRequest failed: %d\n", error);
455 return hres;
456 }
457
458 static HRESULT HttpProtocol_end_request(Protocol *protocol)
459 {
460 BOOL res;
461
462 res = HttpEndRequestW(protocol->request, NULL, 0, 0);
463 if(!res && GetLastError() != ERROR_IO_PENDING) {
464 FIXME("HttpEndRequest failed: %u\n", GetLastError());
465 return E_FAIL;
466 }
467
468 return S_OK;
469 }
470
471 static HRESULT HttpProtocol_start_downloading(Protocol *prot)
472 {
473 HttpProtocol *This = impl_from_Protocol(prot);
474 LPWSTR content_type, content_length, ranges;
475 DWORD len = sizeof(DWORD);
476 DWORD status_code;
477 BOOL res;
478 HRESULT hres;
479
480 static const WCHAR wszDefaultContentType[] =
481 {'t','e','x','t','/','h','t','m','l',0};
482
483 if(!This->http_negotiate) {
484 WARN("Expected IHttpNegotiate pointer to be non-NULL\n");
485 return S_OK;
486 }
487
488 res = HttpQueryInfoW(This->base.request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER,
489 &status_code, &len, NULL);
490 if(res) {
491 LPWSTR response_headers = query_http_info(This, HTTP_QUERY_RAW_HEADERS_CRLF);
492 if(response_headers) {
493 hres = IHttpNegotiate_OnResponse(This->http_negotiate, status_code, response_headers,
494 NULL, NULL);
495 heap_free(response_headers);
496 if (hres != S_OK) {
497 WARN("IHttpNegotiate_OnResponse failed: %08x\n", hres);
498 return S_OK;
499 }
500 }
501 }else {
502 WARN("HttpQueryInfo failed: %d\n", GetLastError());
503 }
504
505 ranges = query_http_info(This, HTTP_QUERY_ACCEPT_RANGES);
506 if(ranges) {
507 IInternetProtocolSink_ReportProgress(This->base.protocol_sink, BINDSTATUS_ACCEPTRANGES, NULL);
508 heap_free(ranges);
509 }
510
511 content_type = query_http_info(This, HTTP_QUERY_CONTENT_TYPE);
512 if(content_type) {
513 /* remove the charset, if present */
514 LPWSTR p = strchrW(content_type, ';');
515 if (p) *p = '\0';
516
517 IInternetProtocolSink_ReportProgress(This->base.protocol_sink,
518 (This->base.bindf & BINDF_FROMURLMON)
519 ? BINDSTATUS_MIMETYPEAVAILABLE : BINDSTATUS_RAWMIMETYPE,
520 content_type);
521 heap_free(content_type);
522 }else {
523 WARN("HttpQueryInfo failed: %d\n", GetLastError());
524 IInternetProtocolSink_ReportProgress(This->base.protocol_sink,
525 (This->base.bindf & BINDF_FROMURLMON)
526 ? BINDSTATUS_MIMETYPEAVAILABLE : BINDSTATUS_RAWMIMETYPE,
527 wszDefaultContentType);
528 }
529
530 content_length = query_http_info(This, HTTP_QUERY_CONTENT_LENGTH);
531 if(content_length) {
532 This->base.content_length = atoiW(content_length);
533 heap_free(content_length);
534 }
535
536 return S_OK;
537 }
538
539 static void HttpProtocol_close_connection(Protocol *prot)
540 {
541 HttpProtocol *This = impl_from_Protocol(prot);
542
543 if(This->http_negotiate) {
544 IHttpNegotiate_Release(This->http_negotiate);
545 This->http_negotiate = NULL;
546 }
547
548 heap_free(This->full_header);
549 This->full_header = NULL;
550 }
551
552 static void HttpProtocol_on_error(Protocol *prot, DWORD error)
553 {
554 HttpProtocol *This = impl_from_Protocol(prot);
555 HRESULT hres;
556
557 TRACE("(%p) %d\n", prot, error);
558
559 if(prot->flags & FLAG_FIRST_CONTINUE_COMPLETE) {
560 FIXME("Not handling error %d\n", error);
561 return;
562 }
563
564 while((hres = handle_http_error(This, error)) == RPC_E_RETRY) {
565 error = send_http_request(This);
566
567 if(error == ERROR_IO_PENDING || error == ERROR_SUCCESS)
568 return;
569 }
570
571 protocol_abort(prot, hres);
572 protocol_close_connection(prot);
573 return;
574 }
575
576 static const ProtocolVtbl AsyncProtocolVtbl = {
577 HttpProtocol_open_request,
578 HttpProtocol_end_request,
579 HttpProtocol_start_downloading,
580 HttpProtocol_close_connection,
581 HttpProtocol_on_error
582 };
583
584 static HRESULT WINAPI HttpProtocol_QueryInterface(IInternetProtocolEx *iface, REFIID riid, void **ppv)
585 {
586 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
587
588 *ppv = NULL;
589 if(IsEqualGUID(&IID_IUnknown, riid)) {
590 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
591 *ppv = &This->IInternetProtocolEx_iface;
592 }else if(IsEqualGUID(&IID_IInternetProtocolRoot, riid)) {
593 TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", This, ppv);
594 *ppv = &This->IInternetProtocolEx_iface;
595 }else if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
596 TRACE("(%p)->(IID_IInternetProtocol %p)\n", This, ppv);
597 *ppv = &This->IInternetProtocolEx_iface;
598 }else if(IsEqualGUID(&IID_IInternetProtocolEx, riid)) {
599 TRACE("(%p)->(IID_IInternetProtocolEx %p)\n", This, ppv);
600 *ppv = &This->IInternetProtocolEx_iface;
601 }else if(IsEqualGUID(&IID_IInternetPriority, riid)) {
602 TRACE("(%p)->(IID_IInternetPriority %p)\n", This, ppv);
603 *ppv = &This->IInternetPriority_iface;
604 }else if(IsEqualGUID(&IID_IWinInetInfo, riid)) {
605 TRACE("(%p)->(IID_IWinInetInfo %p)\n", This, ppv);
606 *ppv = &This->IWinInetHttpInfo_iface;
607 }else if(IsEqualGUID(&IID_IWinInetHttpInfo, riid)) {
608 TRACE("(%p)->(IID_IWinInetHttpInfo %p)\n", This, ppv);
609 *ppv = &This->IWinInetHttpInfo_iface;
610 }
611
612 if(*ppv) {
613 IInternetProtocolEx_AddRef(iface);
614 return S_OK;
615 }
616
617 WARN("not supported interface %s\n", debugstr_guid(riid));
618 return E_NOINTERFACE;
619 }
620
621 static ULONG WINAPI HttpProtocol_AddRef(IInternetProtocolEx *iface)
622 {
623 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
624 LONG ref = InterlockedIncrement(&This->ref);
625 TRACE("(%p) ref=%d\n", This, ref);
626 return ref;
627 }
628
629 static ULONG WINAPI HttpProtocol_Release(IInternetProtocolEx *iface)
630 {
631 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
632 LONG ref = InterlockedDecrement(&This->ref);
633
634 TRACE("(%p) ref=%d\n", This, ref);
635
636 if(!ref) {
637 protocol_close_connection(&This->base);
638 heap_free(This);
639
640 URLMON_UnlockModule();
641 }
642
643 return ref;
644 }
645
646 static HRESULT WINAPI HttpProtocol_Start(IInternetProtocolEx *iface, LPCWSTR szUrl,
647 IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
648 DWORD grfPI, HANDLE_PTR dwReserved)
649 {
650 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
651 IUri *uri;
652 HRESULT hres;
653
654 TRACE("(%p)->(%s %p %p %08x %lx)\n", This, debugstr_w(szUrl), pOIProtSink,
655 pOIBindInfo, grfPI, dwReserved);
656
657 hres = CreateUri(szUrl, 0, 0, &uri);
658 if(FAILED(hres))
659 return hres;
660
661 hres = IInternetProtocolEx_StartEx(&This->IInternetProtocolEx_iface, uri, pOIProtSink,
662 pOIBindInfo, grfPI, (HANDLE*)dwReserved);
663
664 IUri_Release(uri);
665 return hres;
666 }
667
668 static HRESULT WINAPI HttpProtocol_Continue(IInternetProtocolEx *iface, PROTOCOLDATA *pProtocolData)
669 {
670 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
671
672 TRACE("(%p)->(%p)\n", This, pProtocolData);
673
674 return protocol_continue(&This->base, pProtocolData);
675 }
676
677 static HRESULT WINAPI HttpProtocol_Abort(IInternetProtocolEx *iface, HRESULT hrReason,
678 DWORD dwOptions)
679 {
680 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
681
682 TRACE("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);
683
684 return protocol_abort(&This->base, hrReason);
685 }
686
687 static HRESULT WINAPI HttpProtocol_Terminate(IInternetProtocolEx *iface, DWORD dwOptions)
688 {
689 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
690
691 TRACE("(%p)->(%08x)\n", This, dwOptions);
692
693 protocol_close_connection(&This->base);
694 return S_OK;
695 }
696
697 static HRESULT WINAPI HttpProtocol_Suspend(IInternetProtocolEx *iface)
698 {
699 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
700 FIXME("(%p)\n", This);
701 return E_NOTIMPL;
702 }
703
704 static HRESULT WINAPI HttpProtocol_Resume(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_Read(IInternetProtocolEx *iface, void *pv,
712 ULONG cb, ULONG *pcbRead)
713 {
714 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
715
716 TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
717
718 return protocol_read(&This->base, pv, cb, pcbRead);
719 }
720
721 static HRESULT WINAPI HttpProtocol_Seek(IInternetProtocolEx *iface, LARGE_INTEGER dlibMove,
722 DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
723 {
724 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
725 FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
726 return E_NOTIMPL;
727 }
728
729 static HRESULT WINAPI HttpProtocol_LockRequest(IInternetProtocolEx *iface, DWORD dwOptions)
730 {
731 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
732
733 TRACE("(%p)->(%08x)\n", This, dwOptions);
734
735 return protocol_lock_request(&This->base);
736 }
737
738 static HRESULT WINAPI HttpProtocol_UnlockRequest(IInternetProtocolEx *iface)
739 {
740 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
741
742 TRACE("(%p)\n", This);
743
744 return protocol_unlock_request(&This->base);
745 }
746
747 static HRESULT WINAPI HttpProtocol_StartEx(IInternetProtocolEx *iface, IUri *pUri,
748 IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
749 DWORD grfPI, HANDLE *dwReserved)
750 {
751 HttpProtocol *This = impl_from_IInternetProtocolEx(iface);
752 DWORD scheme = 0;
753 HRESULT hres;
754
755 TRACE("(%p)->(%p %p %p %08x %p)\n", This, pUri, pOIProtSink,
756 pOIBindInfo, grfPI, dwReserved);
757
758 hres = IUri_GetScheme(pUri, &scheme);
759 if(FAILED(hres))
760 return hres;
761 if(scheme != (This->https ? URL_SCHEME_HTTPS : URL_SCHEME_HTTP))
762 return MK_E_SYNTAX;
763
764 return protocol_start(&This->base, (IInternetProtocol*)&This->IInternetProtocolEx_iface, pUri,
765 pOIProtSink, pOIBindInfo);
766 }
767
768 static const IInternetProtocolExVtbl HttpProtocolVtbl = {
769 HttpProtocol_QueryInterface,
770 HttpProtocol_AddRef,
771 HttpProtocol_Release,
772 HttpProtocol_Start,
773 HttpProtocol_Continue,
774 HttpProtocol_Abort,
775 HttpProtocol_Terminate,
776 HttpProtocol_Suspend,
777 HttpProtocol_Resume,
778 HttpProtocol_Read,
779 HttpProtocol_Seek,
780 HttpProtocol_LockRequest,
781 HttpProtocol_UnlockRequest,
782 HttpProtocol_StartEx
783 };
784
785 static HRESULT WINAPI HttpPriority_QueryInterface(IInternetPriority *iface, REFIID riid, void **ppv)
786 {
787 HttpProtocol *This = impl_from_IInternetPriority(iface);
788 return IInternetProtocolEx_QueryInterface(&This->IInternetProtocolEx_iface, riid, ppv);
789 }
790
791 static ULONG WINAPI HttpPriority_AddRef(IInternetPriority *iface)
792 {
793 HttpProtocol *This = impl_from_IInternetPriority(iface);
794 return IInternetProtocolEx_AddRef(&This->IInternetProtocolEx_iface);
795 }
796
797 static ULONG WINAPI HttpPriority_Release(IInternetPriority *iface)
798 {
799 HttpProtocol *This = impl_from_IInternetPriority(iface);
800 return IInternetProtocolEx_Release(&This->IInternetProtocolEx_iface);
801 }
802
803 static HRESULT WINAPI HttpPriority_SetPriority(IInternetPriority *iface, LONG nPriority)
804 {
805 HttpProtocol *This = impl_from_IInternetPriority(iface);
806
807 TRACE("(%p)->(%d)\n", This, nPriority);
808
809 This->base.priority = nPriority;
810 return S_OK;
811 }
812
813 static HRESULT WINAPI HttpPriority_GetPriority(IInternetPriority *iface, LONG *pnPriority)
814 {
815 HttpProtocol *This = impl_from_IInternetPriority(iface);
816
817 TRACE("(%p)->(%p)\n", This, pnPriority);
818
819 *pnPriority = This->base.priority;
820 return S_OK;
821 }
822
823 static const IInternetPriorityVtbl HttpPriorityVtbl = {
824 HttpPriority_QueryInterface,
825 HttpPriority_AddRef,
826 HttpPriority_Release,
827 HttpPriority_SetPriority,
828 HttpPriority_GetPriority
829 };
830
831 static HRESULT WINAPI HttpInfo_QueryInterface(IWinInetHttpInfo *iface, REFIID riid, void **ppv)
832 {
833 HttpProtocol *This = impl_from_IWinInetHttpInfo(iface);
834 return IInternetProtocolEx_QueryInterface(&This->IInternetProtocolEx_iface, riid, ppv);
835 }
836
837 static ULONG WINAPI HttpInfo_AddRef(IWinInetHttpInfo *iface)
838 {
839 HttpProtocol *This = impl_from_IWinInetHttpInfo(iface);
840 return IInternetProtocolEx_AddRef(&This->IInternetProtocolEx_iface);
841 }
842
843 static ULONG WINAPI HttpInfo_Release(IWinInetHttpInfo *iface)
844 {
845 HttpProtocol *This = impl_from_IWinInetHttpInfo(iface);
846 return IInternetProtocolEx_Release(&This->IInternetProtocolEx_iface);
847 }
848
849 static HRESULT WINAPI HttpInfo_QueryOption(IWinInetHttpInfo *iface, DWORD dwOption,
850 void *pBuffer, DWORD *pcbBuffer)
851 {
852 HttpProtocol *This = impl_from_IWinInetHttpInfo(iface);
853 TRACE("(%p)->(%x %p %p)\n", This, dwOption, pBuffer, pcbBuffer);
854
855 if(!This->base.request)
856 return E_FAIL;
857
858 if(!InternetQueryOptionW(This->base.request, dwOption, pBuffer, pcbBuffer))
859 return S_FALSE;
860 return S_OK;
861 }
862
863 static HRESULT WINAPI HttpInfo_QueryInfo(IWinInetHttpInfo *iface, DWORD dwOption,
864 void *pBuffer, DWORD *pcbBuffer, DWORD *pdwFlags, DWORD *pdwReserved)
865 {
866 HttpProtocol *This = impl_from_IWinInetHttpInfo(iface);
867 TRACE("(%p)->(%x %p %p %p %p)\n", This, dwOption, pBuffer, pcbBuffer, pdwFlags, pdwReserved);
868
869 if(!This->base.request)
870 return E_FAIL;
871
872 if(!HttpQueryInfoW(This->base.request, dwOption, pBuffer, pcbBuffer, pdwFlags)) {
873 if(pBuffer)
874 memset(pBuffer, 0, *pcbBuffer);
875 return S_OK;
876 }
877 return S_OK;
878 }
879
880 static const IWinInetHttpInfoVtbl WinInetHttpInfoVtbl = {
881 HttpInfo_QueryInterface,
882 HttpInfo_AddRef,
883 HttpInfo_Release,
884 HttpInfo_QueryOption,
885 HttpInfo_QueryInfo
886 };
887
888 static HRESULT create_http_protocol(BOOL https, void **ppobj)
889 {
890 HttpProtocol *ret;
891
892 ret = heap_alloc_zero(sizeof(HttpProtocol));
893 if(!ret)
894 return E_OUTOFMEMORY;
895
896 ret->base.vtbl = &AsyncProtocolVtbl;
897 ret->IInternetProtocolEx_iface.lpVtbl = &HttpProtocolVtbl;
898 ret->IInternetPriority_iface.lpVtbl = &HttpPriorityVtbl;
899 ret->IWinInetHttpInfo_iface.lpVtbl = &WinInetHttpInfoVtbl;
900
901 ret->https = https;
902 ret->ref = 1;
903
904 *ppobj = &ret->IInternetProtocolEx_iface;
905
906 URLMON_LockModule();
907 return S_OK;
908 }
909
910 HRESULT HttpProtocol_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
911 {
912 TRACE("(%p %p)\n", pUnkOuter, ppobj);
913
914 return create_http_protocol(FALSE, ppobj);
915 }
916
917 HRESULT HttpSProtocol_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
918 {
919 TRACE("(%p %p)\n", pUnkOuter, ppobj);
920
921 return create_http_protocol(TRUE, ppobj);
922 }