urlmon: Move strndupW implementation to header file.
[wine/multimedia.git] / dlls / urlmon / http.c
blob87d2036167a7ffc22646e79c64ffb5fd92f12173
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
21 * TODO:
22 * - Handle redirects as native.
25 #include "urlmon_main.h"
26 #include "wininet.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
32 /* Flags are needed for, among other things, return HRESULTs from the Read function
33 * to conform to native. For example, Read returns:
35 * 1. E_PENDING if called before the request has completed,
36 * (flags = 0)
37 * 2. S_FALSE after all data has been read and S_OK has been reported,
38 * (flags = FLAG_REQUEST_COMPLETE | FLAG_ALL_DATA_READ | FLAG_RESULT_REPORTED)
39 * 3. INET_E_DATA_NOT_AVAILABLE if InternetQueryDataAvailable fails. The first time
40 * this occurs, INET_E_DATA_NOT_AVAILABLE will also be reported to the sink,
41 * (flags = FLAG_REQUEST_COMPLETE)
42 * but upon subsequent calls to Read no reporting will take place, yet
43 * InternetQueryDataAvailable will still be called, and, on failure,
44 * INET_E_DATA_NOT_AVAILABLE will still be returned.
45 * (flags = FLAG_REQUEST_COMPLETE | FLAG_RESULT_REPORTED)
47 * FLAG_FIRST_DATA_REPORTED and FLAG_LAST_DATA_REPORTED are needed for proper
48 * ReportData reporting. For example, if OnResponse returns S_OK, Continue will
49 * report BSCF_FIRSTDATANOTIFICATION, and when all data has been read Read will
50 * report BSCF_INTERMEDIATEDATANOTIFICATION|BSCF_LASTDATANOTIFICATION. However,
51 * if OnResponse does not return S_OK, Continue will not report data, and Read
52 * will report BSCF_FIRSTDATANOTIFICATION|BSCF_LASTDATANOTIFICATION when all
53 * data has been read.
55 #define FLAG_REQUEST_COMPLETE 0x1
56 #define FLAG_FIRST_CONTINUE_COMPLETE 0x2
57 #define FLAG_FIRST_DATA_REPORTED 0x4
58 #define FLAG_ALL_DATA_READ 0x8
59 #define FLAG_LAST_DATA_REPORTED 0x10
60 #define FLAG_RESULT_REPORTED 0x20
62 typedef struct {
63 Protocol base;
65 const IInternetProtocolVtbl *lpInternetProtocolVtbl;
66 const IInternetPriorityVtbl *lpInternetPriorityVtbl;
68 LONG ref;
70 BOOL https;
71 IHttpNegotiate *http_negotiate;
72 LPWSTR full_header;
73 HINTERNET connection;
74 } HttpProtocol;
76 #define PROTOCOL(x) ((IInternetProtocol*) &(x)->lpInternetProtocolVtbl)
77 #define PRIORITY(x) ((IInternetPriority*) &(x)->lpInternetPriorityVtbl)
79 /* Default headers from native */
80 static const WCHAR wszHeaders[] = {'A','c','c','e','p','t','-','E','n','c','o','d','i','n','g',
81 ':',' ','g','z','i','p',',',' ','d','e','f','l','a','t','e',0};
84 * Helpers
87 static void HTTPPROTOCOL_ReportResult(HttpProtocol *This, HRESULT hres)
89 if (!(This->base.flags & FLAG_RESULT_REPORTED) &&
90 This->base.protocol_sink)
92 This->base.flags |= FLAG_RESULT_REPORTED;
93 IInternetProtocolSink_ReportResult(This->base.protocol_sink, hres, 0, NULL);
97 static void HTTPPROTOCOL_ReportData(HttpProtocol *This)
99 DWORD bscf;
100 if (!(This->base.flags & FLAG_LAST_DATA_REPORTED) &&
101 This->base.protocol_sink)
103 if (This->base.flags & FLAG_FIRST_DATA_REPORTED)
105 bscf = BSCF_INTERMEDIATEDATANOTIFICATION;
107 else
109 This->base.flags |= FLAG_FIRST_DATA_REPORTED;
110 bscf = BSCF_FIRSTDATANOTIFICATION;
112 if (This->base.flags & FLAG_ALL_DATA_READ &&
113 !(This->base.flags & FLAG_LAST_DATA_REPORTED))
115 This->base.flags |= FLAG_LAST_DATA_REPORTED;
116 bscf |= BSCF_LASTDATANOTIFICATION;
118 IInternetProtocolSink_ReportData(This->base.protocol_sink, bscf,
119 This->base.current_position+This->base.available_bytes,
120 This->base.content_length);
124 static void HTTPPROTOCOL_AllDataRead(HttpProtocol *This)
126 if (!(This->base.flags & FLAG_ALL_DATA_READ))
127 This->base.flags |= FLAG_ALL_DATA_READ;
128 HTTPPROTOCOL_ReportData(This);
129 HTTPPROTOCOL_ReportResult(This, S_OK);
132 static void HTTPPROTOCOL_Close(HttpProtocol *This)
134 if (This->http_negotiate)
136 IHttpNegotiate_Release(This->http_negotiate);
137 This->http_negotiate = 0;
139 if (This->base.request)
140 InternetCloseHandle(This->base.request);
141 if (This->connection)
142 InternetCloseHandle(This->connection);
143 if (This->base.internet)
145 InternetCloseHandle(This->base.internet);
146 This->base.internet = 0;
148 if (This->full_header)
150 if (This->full_header != wszHeaders)
151 heap_free(This->full_header);
152 This->full_header = 0;
154 This->base.flags = 0;
157 static void CALLBACK HTTPPROTOCOL_InternetStatusCallback(
158 HINTERNET hInternet, DWORD_PTR dwContext, DWORD dwInternetStatus,
159 LPVOID lpvStatusInformation, DWORD dwStatusInformationLength)
161 HttpProtocol *This = (HttpProtocol *)dwContext;
162 PROTOCOLDATA data;
163 ULONG ulStatusCode;
165 switch (dwInternetStatus)
167 case INTERNET_STATUS_RESOLVING_NAME:
168 ulStatusCode = BINDSTATUS_FINDINGRESOURCE;
169 break;
170 case INTERNET_STATUS_CONNECTING_TO_SERVER:
171 ulStatusCode = BINDSTATUS_CONNECTING;
172 break;
173 case INTERNET_STATUS_SENDING_REQUEST:
174 ulStatusCode = BINDSTATUS_SENDINGREQUEST;
175 break;
176 case INTERNET_STATUS_REQUEST_COMPLETE:
177 This->base.flags |= FLAG_REQUEST_COMPLETE;
178 /* PROTOCOLDATA same as native */
179 memset(&data, 0, sizeof(data));
180 data.dwState = 0xf1000000;
181 if (This->base.flags & FLAG_FIRST_CONTINUE_COMPLETE)
182 data.pData = (LPVOID)BINDSTATUS_ENDDOWNLOADCOMPONENTS;
183 else
184 data.pData = (LPVOID)BINDSTATUS_DOWNLOADINGDATA;
185 if (This->base.bindf & BINDF_FROMURLMON)
186 IInternetProtocolSink_Switch(This->base.protocol_sink, &data);
187 else
188 IInternetProtocol_Continue(PROTOCOL(This), &data);
189 return;
190 case INTERNET_STATUS_HANDLE_CREATED:
191 IInternetProtocol_AddRef(PROTOCOL(This));
192 return;
193 case INTERNET_STATUS_HANDLE_CLOSING:
194 if (*(HINTERNET *)lpvStatusInformation == This->connection)
196 This->connection = 0;
198 else if (*(HINTERNET *)lpvStatusInformation == This->base.request)
200 This->base.request = 0;
201 if (This->base.protocol_sink)
203 IInternetProtocolSink_Release(This->base.protocol_sink);
204 This->base.protocol_sink = 0;
206 if (This->base.bind_info.cbSize)
208 ReleaseBindInfo(&This->base.bind_info);
209 memset(&This->base.bind_info, 0, sizeof(This->base.bind_info));
212 IInternetProtocol_Release(PROTOCOL(This));
213 return;
214 default:
215 WARN("Unhandled Internet status callback %d\n", dwInternetStatus);
216 return;
219 IInternetProtocolSink_ReportProgress(This->base.protocol_sink, ulStatusCode, (LPWSTR)lpvStatusInformation);
223 * Interface implementations
226 #define PROTOCOL_THIS(iface) DEFINE_THIS(HttpProtocol, InternetProtocol, iface)
228 static HRESULT WINAPI HttpProtocol_QueryInterface(IInternetProtocol *iface, REFIID riid, void **ppv)
230 HttpProtocol *This = PROTOCOL_THIS(iface);
232 *ppv = NULL;
233 if(IsEqualGUID(&IID_IUnknown, riid)) {
234 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
235 *ppv = PROTOCOL(This);
236 }else if(IsEqualGUID(&IID_IInternetProtocolRoot, riid)) {
237 TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", This, ppv);
238 *ppv = PROTOCOL(This);
239 }else if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
240 TRACE("(%p)->(IID_IInternetProtocol %p)\n", This, ppv);
241 *ppv = PROTOCOL(This);
242 }else if(IsEqualGUID(&IID_IInternetPriority, riid)) {
243 TRACE("(%p)->(IID_IInternetPriority %p)\n", This, ppv);
244 *ppv = PRIORITY(This);
247 if(*ppv) {
248 IInternetProtocol_AddRef(iface);
249 return S_OK;
252 WARN("not supported interface %s\n", debugstr_guid(riid));
253 return E_NOINTERFACE;
256 static ULONG WINAPI HttpProtocol_AddRef(IInternetProtocol *iface)
258 HttpProtocol *This = PROTOCOL_THIS(iface);
259 LONG ref = InterlockedIncrement(&This->ref);
260 TRACE("(%p) ref=%d\n", This, ref);
261 return ref;
264 static ULONG WINAPI HttpProtocol_Release(IInternetProtocol *iface)
266 HttpProtocol *This = PROTOCOL_THIS(iface);
267 LONG ref = InterlockedDecrement(&This->ref);
269 TRACE("(%p) ref=%d\n", This, ref);
271 if(!ref) {
272 HTTPPROTOCOL_Close(This);
273 heap_free(This);
275 URLMON_UnlockModule();
278 return ref;
281 static HRESULT WINAPI HttpProtocol_Start(IInternetProtocol *iface, LPCWSTR szUrl,
282 IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
283 DWORD grfPI, DWORD dwReserved)
285 HttpProtocol *This = PROTOCOL_THIS(iface);
286 URL_COMPONENTSW url;
287 DWORD len = 0, request_flags = INTERNET_FLAG_KEEP_CONNECTION;
288 ULONG num = 0;
289 IServiceProvider *service_provider = 0;
290 IHttpNegotiate2 *http_negotiate2 = 0;
291 LPWSTR host = 0, path = 0, user = 0, pass = 0, addl_header = 0,
292 post_cookie = 0, optional = 0;
293 BYTE security_id[512];
294 LPOLESTR user_agent = NULL, accept_mimes[257];
295 HRESULT hres;
297 static const WCHAR httpW[] = {'h','t','t','p',':'};
298 static const WCHAR httpsW[] = {'h','t','t','p','s',':'};
299 static const WCHAR wszBindVerb[BINDVERB_CUSTOM][5] =
300 {{'G','E','T',0},
301 {'P','O','S','T',0},
302 {'P','U','T',0}};
304 TRACE("(%p)->(%s %p %p %08x %d)\n", This, debugstr_w(szUrl), pOIProtSink,
305 pOIBindInfo, grfPI, dwReserved);
307 IInternetProtocolSink_AddRef(pOIProtSink);
308 This->base.protocol_sink = pOIProtSink;
310 memset(&This->base.bind_info, 0, sizeof(This->base.bind_info));
311 This->base.bind_info.cbSize = sizeof(BINDINFO);
312 hres = IInternetBindInfo_GetBindInfo(pOIBindInfo, &This->base.bindf, &This->base.bind_info);
313 if (hres != S_OK)
315 WARN("GetBindInfo failed: %08x\n", hres);
316 goto done;
319 if(This->https
320 ? strncmpW(szUrl, httpsW, sizeof(httpsW)/sizeof(WCHAR))
321 : strncmpW(szUrl, httpW, sizeof(httpW)/sizeof(WCHAR)))
323 hres = MK_E_SYNTAX;
324 goto done;
327 memset(&url, 0, sizeof(url));
328 url.dwStructSize = sizeof(url);
329 url.dwSchemeLength = url.dwHostNameLength = url.dwUrlPathLength = url.dwUserNameLength =
330 url.dwPasswordLength = 1;
331 if (!InternetCrackUrlW(szUrl, 0, 0, &url))
333 hres = MK_E_SYNTAX;
334 goto done;
336 host = heap_strndupW(url.lpszHostName, url.dwHostNameLength);
337 path = heap_strndupW(url.lpszUrlPath, url.dwUrlPathLength);
338 user = heap_strndupW(url.lpszUserName, url.dwUserNameLength);
339 pass = heap_strndupW(url.lpszPassword, url.dwPasswordLength);
340 if (!url.nPort)
341 url.nPort = This->https ? INTERNET_DEFAULT_HTTPS_PORT : INTERNET_DEFAULT_HTTP_PORT;
343 if(!(This->base.bindf & BINDF_FROMURLMON))
344 IInternetProtocolSink_ReportProgress(This->base.protocol_sink, BINDSTATUS_DIRECTBIND, NULL);
346 hres = IInternetBindInfo_GetBindString(pOIBindInfo, BINDSTRING_USER_AGENT, &user_agent,
347 1, &num);
348 if (hres != S_OK || !num)
350 CHAR null_char = 0;
351 LPSTR user_agenta = NULL;
352 len = 0;
353 if ((hres = ObtainUserAgentString(0, &null_char, &len)) != E_OUTOFMEMORY)
355 WARN("ObtainUserAgentString failed: %08x\n", hres);
357 else if (!(user_agenta = heap_alloc(len*sizeof(CHAR))))
359 WARN("Out of memory\n");
361 else if ((hres = ObtainUserAgentString(0, user_agenta, &len)) != S_OK)
363 WARN("ObtainUserAgentString failed: %08x\n", hres);
365 else
367 if (!(user_agent = CoTaskMemAlloc((len)*sizeof(WCHAR))))
368 WARN("Out of memory\n");
369 else
370 MultiByteToWideChar(CP_ACP, 0, user_agenta, -1, user_agent, len);
372 heap_free(user_agenta);
375 This->base.internet = InternetOpenW(user_agent, 0, NULL, NULL, INTERNET_FLAG_ASYNC);
376 if (!This->base.internet)
378 WARN("InternetOpen failed: %d\n", GetLastError());
379 hres = INET_E_NO_SESSION;
380 goto done;
383 /* Native does not check for success of next call, so we won't either */
384 InternetSetStatusCallbackW(This->base.internet, HTTPPROTOCOL_InternetStatusCallback);
386 This->connection = InternetConnectW(This->base.internet, host, url.nPort, user,
387 pass, INTERNET_SERVICE_HTTP,
388 This->https ? INTERNET_FLAG_SECURE : 0,
389 (DWORD_PTR)This);
390 if (!This->connection)
392 WARN("InternetConnect failed: %d\n", GetLastError());
393 hres = INET_E_CANNOT_CONNECT;
394 goto done;
397 num = sizeof(accept_mimes)/sizeof(accept_mimes[0])-1;
398 hres = IInternetBindInfo_GetBindString(pOIBindInfo, BINDSTRING_ACCEPT_MIMES,
399 accept_mimes,
400 num, &num);
401 if (hres != S_OK)
403 WARN("GetBindString BINDSTRING_ACCEPT_MIMES failed: %08x\n", hres);
404 hres = INET_E_NO_VALID_MEDIA;
405 goto done;
407 accept_mimes[num] = 0;
409 if (This->base.bindf & BINDF_NOWRITECACHE)
410 request_flags |= INTERNET_FLAG_NO_CACHE_WRITE;
411 if (This->base.bindf & BINDF_NEEDFILE)
412 request_flags |= INTERNET_FLAG_NEED_FILE;
413 if (This->https)
414 request_flags |= INTERNET_FLAG_SECURE;
415 This->base.request = HttpOpenRequestW(This->connection, This->base.bind_info.dwBindVerb < BINDVERB_CUSTOM ?
416 wszBindVerb[This->base.bind_info.dwBindVerb] :
417 This->base.bind_info.szCustomVerb,
418 path, NULL, NULL, (LPCWSTR *)accept_mimes,
419 request_flags, (DWORD_PTR)This);
420 if (!This->base.request)
422 WARN("HttpOpenRequest failed: %d\n", GetLastError());
423 hres = INET_E_RESOURCE_NOT_FOUND;
424 goto done;
427 hres = IInternetProtocolSink_QueryInterface(This->base.protocol_sink, &IID_IServiceProvider,
428 (void **)&service_provider);
429 if (hres != S_OK)
431 WARN("IInternetProtocolSink_QueryInterface IID_IServiceProvider failed: %08x\n", hres);
432 goto done;
435 hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate,
436 &IID_IHttpNegotiate, (void **)&This->http_negotiate);
437 if (hres != S_OK)
439 WARN("IServiceProvider_QueryService IID_IHttpNegotiate failed: %08x\n", hres);
440 goto done;
443 hres = IHttpNegotiate_BeginningTransaction(This->http_negotiate, szUrl, wszHeaders,
444 0, &addl_header);
445 if (hres != S_OK)
447 WARN("IHttpNegotiate_BeginningTransaction failed: %08x\n", hres);
448 goto done;
450 else if (addl_header == NULL)
452 This->full_header = (LPWSTR)wszHeaders;
454 else
456 int len_addl_header = lstrlenW(addl_header);
457 This->full_header = heap_alloc(len_addl_header*sizeof(WCHAR)+sizeof(wszHeaders));
458 if (!This->full_header)
460 WARN("Out of memory\n");
461 hres = E_OUTOFMEMORY;
462 goto done;
464 lstrcpyW(This->full_header, addl_header);
465 lstrcpyW(&This->full_header[len_addl_header], wszHeaders);
468 hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate2,
469 &IID_IHttpNegotiate2, (void **)&http_negotiate2);
470 if (hres != S_OK)
472 WARN("IServiceProvider_QueryService IID_IHttpNegotiate2 failed: %08x\n", hres);
473 /* No goto done as per native */
475 else
477 len = sizeof(security_id)/sizeof(security_id[0]);
478 hres = IHttpNegotiate2_GetRootSecurityId(http_negotiate2, security_id, &len, 0);
479 if (hres != S_OK)
481 WARN("IHttpNegotiate2_GetRootSecurityId failed: %08x\n", hres);
482 /* No goto done as per native */
486 /* FIXME: Handle security_id. Native calls undocumented function IsHostInProxyBypassList. */
488 if (This->base.bind_info.dwBindVerb == BINDVERB_POST)
490 num = 0;
491 hres = IInternetBindInfo_GetBindString(pOIBindInfo, BINDSTRING_POST_COOKIE, &post_cookie,
492 1, &num);
493 if (hres == S_OK && num &&
494 !InternetSetOptionW(This->base.request, INTERNET_OPTION_SECONDARY_CACHE_KEY,
495 post_cookie, lstrlenW(post_cookie)))
497 WARN("InternetSetOption INTERNET_OPTION_SECONDARY_CACHE_KEY failed: %d\n",
498 GetLastError());
502 if (This->base.bind_info.dwBindVerb != BINDVERB_GET)
504 /* Native does not use GlobalLock/GlobalUnlock, so we won't either */
505 if (This->base.bind_info.stgmedData.tymed != TYMED_HGLOBAL)
506 WARN("Expected This->bind_info.stgmedData.tymed to be TYMED_HGLOBAL, not %d\n",
507 This->base.bind_info.stgmedData.tymed);
508 else
509 optional = (LPWSTR)This->base.bind_info.stgmedData.u.hGlobal;
511 if (!HttpSendRequestW(This->base.request, This->full_header, lstrlenW(This->full_header),
512 optional,
513 optional ? This->base.bind_info.cbstgmedData : 0) &&
514 GetLastError() != ERROR_IO_PENDING)
516 WARN("HttpSendRequest failed: %d\n", GetLastError());
517 hres = INET_E_DOWNLOAD_FAILURE;
518 goto done;
521 hres = S_OK;
522 done:
523 if (hres != S_OK)
525 IInternetProtocolSink_ReportResult(This->base.protocol_sink, hres, 0, NULL);
526 HTTPPROTOCOL_Close(This);
529 CoTaskMemFree(post_cookie);
530 CoTaskMemFree(addl_header);
531 if (http_negotiate2)
532 IHttpNegotiate2_Release(http_negotiate2);
533 if (service_provider)
534 IServiceProvider_Release(service_provider);
536 while (num<sizeof(accept_mimes)/sizeof(accept_mimes[0]) &&
537 accept_mimes[num])
538 CoTaskMemFree(accept_mimes[num++]);
539 CoTaskMemFree(user_agent);
541 heap_free(pass);
542 heap_free(user);
543 heap_free(path);
544 heap_free(host);
546 return hres;
549 static HRESULT WINAPI HttpProtocol_Continue(IInternetProtocol *iface, PROTOCOLDATA *pProtocolData)
551 HttpProtocol *This = PROTOCOL_THIS(iface);
552 DWORD len = sizeof(DWORD), status_code;
553 LPWSTR response_headers = 0, content_type = 0, content_length = 0;
555 static const WCHAR wszDefaultContentType[] =
556 {'t','e','x','t','/','h','t','m','l',0};
558 TRACE("(%p)->(%p)\n", This, pProtocolData);
560 if (!pProtocolData)
562 WARN("Expected pProtocolData to be non-NULL\n");
563 return S_OK;
565 else if (!This->base.request)
567 WARN("Expected request to be non-NULL\n");
568 return S_OK;
570 else if (!This->http_negotiate)
572 WARN("Expected IHttpNegotiate pointer to be non-NULL\n");
573 return S_OK;
575 else if (!This->base.protocol_sink)
577 WARN("Expected IInternetProtocolSink pointer to be non-NULL\n");
578 return S_OK;
581 if (pProtocolData->pData == (LPVOID)BINDSTATUS_DOWNLOADINGDATA)
583 if (!HttpQueryInfoW(This->base.request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER,
584 &status_code, &len, NULL))
586 WARN("HttpQueryInfo failed: %d\n", GetLastError());
588 else
590 len = 0;
591 if ((!HttpQueryInfoW(This->base.request, HTTP_QUERY_RAW_HEADERS_CRLF, response_headers, &len,
592 NULL) &&
593 GetLastError() != ERROR_INSUFFICIENT_BUFFER) ||
594 !(response_headers = heap_alloc(len)) ||
595 !HttpQueryInfoW(This->base.request, HTTP_QUERY_RAW_HEADERS_CRLF, response_headers, &len,
596 NULL))
598 WARN("HttpQueryInfo failed: %d\n", GetLastError());
600 else
602 HRESULT hres = IHttpNegotiate_OnResponse(This->http_negotiate, status_code,
603 response_headers, NULL, NULL);
604 if (hres != S_OK)
606 WARN("IHttpNegotiate_OnResponse failed: %08x\n", hres);
607 goto done;
612 if(This->https)
613 IInternetProtocolSink_ReportProgress(This->base.protocol_sink, BINDSTATUS_ACCEPTRANGES, NULL);
615 len = 0;
616 if ((!HttpQueryInfoW(This->base.request, HTTP_QUERY_CONTENT_TYPE, content_type, &len, NULL) &&
617 GetLastError() != ERROR_INSUFFICIENT_BUFFER) ||
618 !(content_type = heap_alloc(len)) ||
619 !HttpQueryInfoW(This->base.request, HTTP_QUERY_CONTENT_TYPE, content_type, &len, NULL))
621 WARN("HttpQueryInfo failed: %d\n", GetLastError());
622 IInternetProtocolSink_ReportProgress(This->base.protocol_sink,
623 (This->base.bindf & BINDF_FROMURLMON) ?
624 BINDSTATUS_MIMETYPEAVAILABLE :
625 BINDSTATUS_RAWMIMETYPE,
626 wszDefaultContentType);
628 else
630 /* remove the charset, if present */
631 LPWSTR p = strchrW(content_type, ';');
632 if (p) *p = '\0';
634 IInternetProtocolSink_ReportProgress(This->base.protocol_sink,
635 (This->base.bindf & BINDF_FROMURLMON) ?
636 BINDSTATUS_MIMETYPEAVAILABLE :
637 BINDSTATUS_RAWMIMETYPE,
638 content_type);
641 len = 0;
642 if ((!HttpQueryInfoW(This->base.request, HTTP_QUERY_CONTENT_LENGTH, content_length, &len, NULL) &&
643 GetLastError() != ERROR_INSUFFICIENT_BUFFER) ||
644 !(content_length = heap_alloc(len)) ||
645 !HttpQueryInfoW(This->base.request, HTTP_QUERY_CONTENT_LENGTH, content_length, &len, NULL))
647 WARN("HttpQueryInfo failed: %d\n", GetLastError());
648 This->base.content_length = 0;
650 else
652 This->base.content_length = atoiW(content_length);
655 if(This->base.bindf & BINDF_NEEDFILE) {
656 WCHAR cache_file[MAX_PATH];
657 DWORD buflen = sizeof(cache_file);
659 if(InternetQueryOptionW(This->base.request, INTERNET_OPTION_DATAFILE_NAME,
660 cache_file, &buflen))
662 IInternetProtocolSink_ReportProgress(This->base.protocol_sink,
663 BINDSTATUS_CACHEFILENAMEAVAILABLE,
664 cache_file);
665 }else {
666 FIXME("Could not get cache file\n");
670 This->base.flags |= FLAG_FIRST_CONTINUE_COMPLETE;
673 if (pProtocolData->pData >= (LPVOID)BINDSTATUS_DOWNLOADINGDATA)
675 /* InternetQueryDataAvailable may immediately fork and perform its asynchronous
676 * read, so clear the flag _before_ calling so it does not incorrectly get cleared
677 * after the status callback is called */
678 This->base.flags &= ~FLAG_REQUEST_COMPLETE;
679 if (!InternetQueryDataAvailable(This->base.request, &This->base.available_bytes, 0, 0))
681 if (GetLastError() != ERROR_IO_PENDING)
683 This->base.flags |= FLAG_REQUEST_COMPLETE;
684 WARN("InternetQueryDataAvailable failed: %d\n", GetLastError());
685 HTTPPROTOCOL_ReportResult(This, INET_E_DATA_NOT_AVAILABLE);
688 else
690 This->base.flags |= FLAG_REQUEST_COMPLETE;
691 HTTPPROTOCOL_ReportData(This);
695 done:
696 heap_free(response_headers);
697 heap_free(content_type);
698 heap_free(content_length);
700 /* Returns S_OK on native */
701 return S_OK;
704 static HRESULT WINAPI HttpProtocol_Abort(IInternetProtocol *iface, HRESULT hrReason,
705 DWORD dwOptions)
707 HttpProtocol *This = PROTOCOL_THIS(iface);
708 FIXME("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);
709 return E_NOTIMPL;
712 static HRESULT WINAPI HttpProtocol_Terminate(IInternetProtocol *iface, DWORD dwOptions)
714 HttpProtocol *This = PROTOCOL_THIS(iface);
716 TRACE("(%p)->(%08x)\n", This, dwOptions);
717 HTTPPROTOCOL_Close(This);
719 return S_OK;
722 static HRESULT WINAPI HttpProtocol_Suspend(IInternetProtocol *iface)
724 HttpProtocol *This = PROTOCOL_THIS(iface);
725 FIXME("(%p)\n", This);
726 return E_NOTIMPL;
729 static HRESULT WINAPI HttpProtocol_Resume(IInternetProtocol *iface)
731 HttpProtocol *This = PROTOCOL_THIS(iface);
732 FIXME("(%p)\n", This);
733 return E_NOTIMPL;
736 static HRESULT WINAPI HttpProtocol_Read(IInternetProtocol *iface, void *pv,
737 ULONG cb, ULONG *pcbRead)
739 HttpProtocol *This = PROTOCOL_THIS(iface);
740 ULONG read = 0, len = 0;
741 HRESULT hres = S_FALSE;
743 TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
745 if (!(This->base.flags & FLAG_REQUEST_COMPLETE))
747 hres = E_PENDING;
749 else while (!(This->base.flags & FLAG_ALL_DATA_READ) &&
750 read < cb)
752 if (This->base.available_bytes == 0)
754 /* InternetQueryDataAvailable may immediately fork and perform its asynchronous
755 * read, so clear the flag _before_ calling so it does not incorrectly get cleared
756 * after the status callback is called */
757 This->base.flags &= ~FLAG_REQUEST_COMPLETE;
758 if (!InternetQueryDataAvailable(This->base.request, &This->base.available_bytes, 0, 0))
760 if (GetLastError() == ERROR_IO_PENDING)
762 hres = E_PENDING;
764 else
766 WARN("InternetQueryDataAvailable failed: %d\n", GetLastError());
767 hres = INET_E_DATA_NOT_AVAILABLE;
768 HTTPPROTOCOL_ReportResult(This, hres);
770 goto done;
772 else if (This->base.available_bytes == 0)
774 HTTPPROTOCOL_AllDataRead(This);
777 else
779 if (!InternetReadFile(This->base.request, ((BYTE *)pv)+read,
780 This->base.available_bytes > cb-read ?
781 cb-read : This->base.available_bytes, &len))
783 WARN("InternetReadFile failed: %d\n", GetLastError());
784 hres = INET_E_DOWNLOAD_FAILURE;
785 HTTPPROTOCOL_ReportResult(This, hres);
786 goto done;
788 else if (len == 0)
790 HTTPPROTOCOL_AllDataRead(This);
792 else
794 read += len;
795 This->base.current_position += len;
796 This->base.available_bytes -= len;
801 /* Per MSDN this should be if (read == cb), but native returns S_OK
802 * if any bytes were read, so we will too */
803 if (read)
804 hres = S_OK;
806 done:
807 if (pcbRead)
808 *pcbRead = read;
810 if (hres != E_PENDING)
811 This->base.flags |= FLAG_REQUEST_COMPLETE;
813 return hres;
816 static HRESULT WINAPI HttpProtocol_Seek(IInternetProtocol *iface, LARGE_INTEGER dlibMove,
817 DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
819 HttpProtocol *This = PROTOCOL_THIS(iface);
820 FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
821 return E_NOTIMPL;
824 static HRESULT WINAPI HttpProtocol_LockRequest(IInternetProtocol *iface, DWORD dwOptions)
826 HttpProtocol *This = PROTOCOL_THIS(iface);
828 TRACE("(%p)->(%08x)\n", This, dwOptions);
830 if (!InternetLockRequestFile(This->base.request, &This->base.lock))
831 WARN("InternetLockRequest failed: %d\n", GetLastError());
833 return S_OK;
836 static HRESULT WINAPI HttpProtocol_UnlockRequest(IInternetProtocol *iface)
838 HttpProtocol *This = PROTOCOL_THIS(iface);
840 TRACE("(%p)\n", This);
842 if (This->base.lock)
844 if (!InternetUnlockRequestFile(This->base.lock))
845 WARN("InternetUnlockRequest failed: %d\n", GetLastError());
846 This->base.lock = 0;
849 return S_OK;
852 #undef PROTOCOL_THIS
854 #define PRIORITY_THIS(iface) DEFINE_THIS(HttpProtocol, InternetPriority, iface)
856 static HRESULT WINAPI HttpPriority_QueryInterface(IInternetPriority *iface, REFIID riid, void **ppv)
858 HttpProtocol *This = PRIORITY_THIS(iface);
859 return IInternetProtocol_QueryInterface(PROTOCOL(This), riid, ppv);
862 static ULONG WINAPI HttpPriority_AddRef(IInternetPriority *iface)
864 HttpProtocol *This = PRIORITY_THIS(iface);
865 return IInternetProtocol_AddRef(PROTOCOL(This));
868 static ULONG WINAPI HttpPriority_Release(IInternetPriority *iface)
870 HttpProtocol *This = PRIORITY_THIS(iface);
871 return IInternetProtocol_Release(PROTOCOL(This));
874 static HRESULT WINAPI HttpPriority_SetPriority(IInternetPriority *iface, LONG nPriority)
876 HttpProtocol *This = PRIORITY_THIS(iface);
878 TRACE("(%p)->(%d)\n", This, nPriority);
880 This->base.priority = nPriority;
881 return S_OK;
884 static HRESULT WINAPI HttpPriority_GetPriority(IInternetPriority *iface, LONG *pnPriority)
886 HttpProtocol *This = PRIORITY_THIS(iface);
888 TRACE("(%p)->(%p)\n", This, pnPriority);
890 *pnPriority = This->base.priority;
891 return S_OK;
894 #undef PRIORITY_THIS
896 static const IInternetPriorityVtbl HttpPriorityVtbl = {
897 HttpPriority_QueryInterface,
898 HttpPriority_AddRef,
899 HttpPriority_Release,
900 HttpPriority_SetPriority,
901 HttpPriority_GetPriority
904 static const IInternetProtocolVtbl HttpProtocolVtbl = {
905 HttpProtocol_QueryInterface,
906 HttpProtocol_AddRef,
907 HttpProtocol_Release,
908 HttpProtocol_Start,
909 HttpProtocol_Continue,
910 HttpProtocol_Abort,
911 HttpProtocol_Terminate,
912 HttpProtocol_Suspend,
913 HttpProtocol_Resume,
914 HttpProtocol_Read,
915 HttpProtocol_Seek,
916 HttpProtocol_LockRequest,
917 HttpProtocol_UnlockRequest
920 static HRESULT create_http_protocol(BOOL https, void **ppobj)
922 HttpProtocol *ret;
924 ret = heap_alloc_zero(sizeof(HttpProtocol));
925 if(!ret)
926 return E_OUTOFMEMORY;
928 ret->lpInternetProtocolVtbl = &HttpProtocolVtbl;
929 ret->lpInternetPriorityVtbl = &HttpPriorityVtbl;
931 ret->https = https;
932 ret->ref = 1;
934 *ppobj = PROTOCOL(ret);
936 URLMON_LockModule();
937 return S_OK;
940 HRESULT HttpProtocol_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
942 TRACE("(%p %p)\n", pUnkOuter, ppobj);
944 return create_http_protocol(FALSE, ppobj);
947 HRESULT HttpSProtocol_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
949 TRACE("(%p %p)\n", pUnkOuter, ppobj);
951 return create_http_protocol(TRUE, ppobj);