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
22 * - Handle redirects as native.
28 #define NONAMELESSUNION
36 #include "urlmon_main.h"
38 #include "wine/debug.h"
39 #include "wine/unicode.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
43 /* Flags are needed for, among other things, return HRESULTs from the Read function
44 * to conform to native. For example, Read returns:
46 * 1. E_PENDING if called before the request has completed,
48 * 2. S_FALSE after all data has been read and S_OK has been reported,
49 * (flags = FLAG_REQUEST_COMPLETE | FLAG_ALL_DATA_READ | FLAG_RESULT_REPORTED)
50 * 3. INET_E_DATA_NOT_AVAILABLE if InternetQueryDataAvailable fails. The first time
51 * this occurs, INET_E_DATA_NOT_AVAILABLE will also be reported to the sink,
52 * (flags = FLAG_REQUEST_COMPLETE)
53 * but upon subsequent calls to Read no reporting will take place, yet
54 * InternetQueryDataAvailable will still be called, and, on failure,
55 * INET_E_DATA_NOT_AVAILABLE will still be returned.
56 * (flags = FLAG_REQUEST_COMPLETE | FLAG_RESULT_REPORTED)
58 * FLAG_FIRST_DATA_REPORTED and FLAG_LAST_DATA_REPORTED are needed for proper
59 * ReportData reporting. For example, if OnResponse returns S_OK, Continue will
60 * report BSCF_FIRSTDATANOTIFICATION, and when all data has been read Read will
61 * report BSCF_INTERMEDIATEDATANOTIFICATION|BSCF_LASTDATANOTIFICATION. However,
62 * if OnResponse does not return S_OK, Continue will not report data, and Read
63 * will report BSCF_FIRSTDATANOTIFICATION|BSCF_LASTDATANOTIFICATION when all
66 #define FLAG_REQUEST_COMPLETE 0x1
67 #define FLAG_FIRST_CONTINUE_COMPLETE 0x2
68 #define FLAG_FIRST_DATA_REPORTED 0x4
69 #define FLAG_ALL_DATA_READ 0x8
70 #define FLAG_LAST_DATA_REPORTED 0x10
71 #define FLAG_RESULT_REPORTED 0x20
74 const IInternetProtocolVtbl *lpInternetProtocolVtbl;
75 const IInternetPriorityVtbl *lpInternetPriorityVtbl;
77 DWORD flags, grfBINDF;
79 IInternetProtocolSink *protocol_sink;
80 IHttpNegotiate *http_negotiate;
81 HINTERNET internet, connect, request;
84 ULONG current_position, content_length, available_bytes;
90 /* Default headers from native */
91 static const WCHAR wszHeaders[] = {'A','c','c','e','p','t','-','E','n','c','o','d','i','n','g',
92 ':',' ','g','z','i','p',',',' ','d','e','f','l','a','t','e',0};
98 static void HTTPPROTOCOL_ReportResult(HttpProtocol *This, HRESULT hres)
100 if (!(This->flags & FLAG_RESULT_REPORTED) &&
103 This->flags |= FLAG_RESULT_REPORTED;
104 IInternetProtocolSink_ReportResult(This->protocol_sink, hres, 0, NULL);
108 static void HTTPPROTOCOL_ReportData(HttpProtocol *This)
111 if (!(This->flags & FLAG_LAST_DATA_REPORTED) &&
114 if (This->flags & FLAG_FIRST_DATA_REPORTED)
116 bscf = BSCF_INTERMEDIATEDATANOTIFICATION;
120 This->flags |= FLAG_FIRST_DATA_REPORTED;
121 bscf = BSCF_FIRSTDATANOTIFICATION;
123 if (This->flags & FLAG_ALL_DATA_READ &&
124 !(This->flags & FLAG_LAST_DATA_REPORTED))
126 This->flags |= FLAG_LAST_DATA_REPORTED;
127 bscf |= BSCF_LASTDATANOTIFICATION;
129 IInternetProtocolSink_ReportData(This->protocol_sink, bscf,
130 This->current_position+This->available_bytes,
131 This->content_length);
135 static void HTTPPROTOCOL_AllDataRead(HttpProtocol *This)
137 if (!(This->flags & FLAG_ALL_DATA_READ))
138 This->flags |= FLAG_ALL_DATA_READ;
139 HTTPPROTOCOL_ReportData(This);
140 HTTPPROTOCOL_ReportResult(This, S_OK);
143 static void HTTPPROTOCOL_Close(HttpProtocol *This)
145 if (This->http_negotiate)
147 IHttpNegotiate_Release(This->http_negotiate);
148 This->http_negotiate = 0;
151 InternetCloseHandle(This->request);
153 InternetCloseHandle(This->connect);
156 InternetCloseHandle(This->internet);
159 if (This->full_header)
161 if (This->full_header != wszHeaders)
162 HeapFree(GetProcessHeap(), 0, This->full_header);
163 This->full_header = 0;
168 static void CALLBACK HTTPPROTOCOL_InternetStatusCallback(
169 HINTERNET hInternet, DWORD_PTR dwContext, DWORD dwInternetStatus,
170 LPVOID lpvStatusInformation, DWORD dwStatusInformationLength)
172 HttpProtocol *This = (HttpProtocol *)dwContext;
176 switch (dwInternetStatus)
178 case INTERNET_STATUS_RESOLVING_NAME:
179 ulStatusCode = BINDSTATUS_FINDINGRESOURCE;
181 case INTERNET_STATUS_CONNECTING_TO_SERVER:
182 ulStatusCode = BINDSTATUS_CONNECTING;
184 case INTERNET_STATUS_SENDING_REQUEST:
185 ulStatusCode = BINDSTATUS_SENDINGREQUEST;
187 case INTERNET_STATUS_REQUEST_COMPLETE:
188 This->flags |= FLAG_REQUEST_COMPLETE;
189 /* PROTOCOLDATA same as native */
190 memset(&data, 0, sizeof(data));
191 data.dwState = 0xf1000000;
192 if (This->flags & FLAG_FIRST_CONTINUE_COMPLETE)
193 data.pData = (LPVOID)BINDSTATUS_ENDDOWNLOADCOMPONENTS;
195 data.pData = (LPVOID)BINDSTATUS_DOWNLOADINGDATA;
196 if (This->grfBINDF & BINDF_FROMURLMON)
197 IInternetProtocolSink_Switch(This->protocol_sink, &data);
199 IInternetProtocol_Continue((IInternetProtocol *)This, &data);
201 case INTERNET_STATUS_HANDLE_CREATED:
202 IInternetProtocol_AddRef((IInternetProtocol *)This);
204 case INTERNET_STATUS_HANDLE_CLOSING:
205 if (*(HINTERNET *)lpvStatusInformation == This->connect)
209 else if (*(HINTERNET *)lpvStatusInformation == This->request)
212 if (This->protocol_sink)
214 IInternetProtocolSink_Release(This->protocol_sink);
215 This->protocol_sink = 0;
217 if (This->bind_info.cbSize)
219 ReleaseBindInfo(&This->bind_info);
220 memset(&This->bind_info, 0, sizeof(This->bind_info));
223 IInternetProtocol_Release((IInternetProtocol *)This);
226 WARN("Unhandled Internet status callback %d\n", dwInternetStatus);
230 IInternetProtocolSink_ReportProgress(This->protocol_sink, ulStatusCode, (LPWSTR)lpvStatusInformation);
233 static inline LPWSTR strndupW(LPCWSTR string, int len)
237 (ret = HeapAlloc(GetProcessHeap(), 0, (len+1)*sizeof(WCHAR))) != NULL)
239 memcpy(ret, string, len*sizeof(WCHAR));
246 * Interface implementations
249 #define PROTOCOL(x) ((IInternetProtocol*) &(x)->lpInternetProtocolVtbl)
250 #define PRIORITY(x) ((IInternetPriority*) &(x)->lpInternetPriorityVtbl)
252 #define PROTOCOL_THIS(iface) DEFINE_THIS(HttpProtocol, InternetProtocol, iface)
254 static HRESULT WINAPI HttpProtocol_QueryInterface(IInternetProtocol *iface, REFIID riid, void **ppv)
256 HttpProtocol *This = PROTOCOL_THIS(iface);
259 if(IsEqualGUID(&IID_IUnknown, riid)) {
260 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
261 *ppv = PROTOCOL(This);
262 }else if(IsEqualGUID(&IID_IInternetProtocolRoot, riid)) {
263 TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", This, ppv);
264 *ppv = PROTOCOL(This);
265 }else if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
266 TRACE("(%p)->(IID_IInternetProtocol %p)\n", This, ppv);
267 *ppv = PROTOCOL(This);
268 }else if(IsEqualGUID(&IID_IInternetPriority, riid)) {
269 TRACE("(%p)->(IID_IInternetPriority %p)\n", This, ppv);
270 *ppv = PRIORITY(This);
274 IInternetProtocol_AddRef(iface);
278 WARN("not supported interface %s\n", debugstr_guid(riid));
279 return E_NOINTERFACE;
282 static ULONG WINAPI HttpProtocol_AddRef(IInternetProtocol *iface)
284 HttpProtocol *This = PROTOCOL_THIS(iface);
285 LONG ref = InterlockedIncrement(&This->ref);
286 TRACE("(%p) ref=%d\n", This, ref);
290 static ULONG WINAPI HttpProtocol_Release(IInternetProtocol *iface)
292 HttpProtocol *This = PROTOCOL_THIS(iface);
293 LONG ref = InterlockedDecrement(&This->ref);
295 TRACE("(%p) ref=%d\n", This, ref);
298 HTTPPROTOCOL_Close(This);
299 HeapFree(GetProcessHeap(), 0, This);
301 URLMON_UnlockModule();
307 static HRESULT WINAPI HttpProtocol_Start(IInternetProtocol *iface, LPCWSTR szUrl,
308 IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
309 DWORD grfPI, DWORD dwReserved)
311 HttpProtocol *This = PROTOCOL_THIS(iface);
313 DWORD len = 0, request_flags = INTERNET_FLAG_KEEP_CONNECTION;
315 IServiceProvider *service_provider = 0;
316 IHttpNegotiate2 *http_negotiate2 = 0;
317 LPWSTR host = 0, path = 0, user = 0, pass = 0, addl_header = 0,
318 post_cookie = 0, optional = 0;
319 BYTE security_id[512];
320 LPOLESTR user_agent, accept_mimes[257];
323 static const WCHAR wszHttp[] = {'h','t','t','p',':'};
324 static const WCHAR wszBindVerb[BINDVERB_CUSTOM][5] =
329 TRACE("(%p)->(%s %p %p %08x %d)\n", This, debugstr_w(szUrl), pOIProtSink,
330 pOIBindInfo, grfPI, dwReserved);
332 IInternetProtocolSink_AddRef(pOIProtSink);
333 This->protocol_sink = pOIProtSink;
335 memset(&This->bind_info, 0, sizeof(This->bind_info));
336 This->bind_info.cbSize = sizeof(BINDINFO);
337 hres = IInternetBindInfo_GetBindInfo(pOIBindInfo, &This->grfBINDF, &This->bind_info);
340 WARN("GetBindInfo failed: %08x\n", hres);
344 if (lstrlenW(szUrl) < sizeof(wszHttp)/sizeof(WCHAR)
345 || memcmp(szUrl, wszHttp, sizeof(wszHttp)))
351 memset(&url, 0, sizeof(url));
352 url.dwStructSize = sizeof(url);
353 url.dwSchemeLength = url.dwHostNameLength = url.dwUrlPathLength = url.dwUserNameLength =
354 url.dwPasswordLength = 1;
355 if (!InternetCrackUrlW(szUrl, 0, 0, &url))
360 host = strndupW(url.lpszHostName, url.dwHostNameLength);
361 path = strndupW(url.lpszUrlPath, url.dwUrlPathLength);
362 user = strndupW(url.lpszUserName, url.dwUserNameLength);
363 pass = strndupW(url.lpszPassword, url.dwPasswordLength);
365 url.nPort = INTERNET_DEFAULT_HTTP_PORT;
367 if(!(This->grfBINDF & BINDF_FROMURLMON))
368 IInternetProtocolSink_ReportProgress(This->protocol_sink, BINDSTATUS_DIRECTBIND, NULL);
370 hres = IInternetBindInfo_GetBindString(pOIBindInfo, BINDSTRING_USER_AGENT, &user_agent,
372 if (hres != S_OK || !num)
375 LPSTR user_agenta = NULL;
377 if ((hres = ObtainUserAgentString(0, &null_char, &len)) != E_OUTOFMEMORY)
379 WARN("ObtainUserAgentString failed: %08x\n", hres);
381 else if (!(user_agenta = HeapAlloc(GetProcessHeap(), 0, len*sizeof(CHAR))))
383 WARN("Out of memory\n");
385 else if ((hres = ObtainUserAgentString(0, user_agenta, &len)) != S_OK)
387 WARN("ObtainUserAgentString failed: %08x\n", hres);
391 if (!(user_agent = CoTaskMemAlloc((len)*sizeof(WCHAR))))
392 WARN("Out of memory\n");
394 MultiByteToWideChar(CP_ACP, 0, user_agenta, -1, user_agent, len*sizeof(WCHAR));
396 HeapFree(GetProcessHeap(), 0, user_agenta);
399 This->internet = InternetOpenW(user_agent, 0, NULL, NULL, INTERNET_FLAG_ASYNC);
402 WARN("InternetOpen failed: %d\n", GetLastError());
403 hres = INET_E_NO_SESSION;
407 /* Native does not check for success of next call, so we won't either */
408 InternetSetStatusCallbackW(This->internet, HTTPPROTOCOL_InternetStatusCallback);
410 This->connect = InternetConnectW(This->internet, host, url.nPort, user,
411 pass, INTERNET_SERVICE_HTTP, 0, (DWORD)This);
414 WARN("InternetConnect failed: %d\n", GetLastError());
415 hres = INET_E_CANNOT_CONNECT;
419 num = sizeof(accept_mimes)/sizeof(accept_mimes[0])-1;
420 hres = IInternetBindInfo_GetBindString(pOIBindInfo, BINDSTRING_ACCEPT_MIMES,
425 WARN("GetBindString BINDSTRING_ACCEPT_MIMES failed: %08x\n", hres);
426 hres = INET_E_NO_VALID_MEDIA;
429 accept_mimes[num] = 0;
431 if (This->grfBINDF & BINDF_NOWRITECACHE)
432 request_flags |= INTERNET_FLAG_NO_CACHE_WRITE;
433 This->request = HttpOpenRequestW(This->connect, This->bind_info.dwBindVerb < BINDVERB_CUSTOM ?
434 wszBindVerb[This->bind_info.dwBindVerb] :
435 This->bind_info.szCustomVerb,
436 path, NULL, NULL, (LPCWSTR *)accept_mimes,
437 request_flags, (DWORD)This);
440 WARN("HttpOpenRequest failed: %d\n", GetLastError());
441 hres = INET_E_RESOURCE_NOT_FOUND;
445 hres = IInternetProtocolSink_QueryInterface(This->protocol_sink, &IID_IServiceProvider,
446 (void **)&service_provider);
449 WARN("IInternetProtocolSink_QueryInterface IID_IServiceProvider failed: %08x\n", hres);
453 hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate,
454 &IID_IHttpNegotiate, (void **)&This->http_negotiate);
457 WARN("IServiceProvider_QueryService IID_IHttpNegotiate failed: %08x\n", hres);
461 hres = IHttpNegotiate_BeginningTransaction(This->http_negotiate, szUrl, wszHeaders,
465 WARN("IHttpNegotiate_BeginningTransaction failed: %08x\n", hres);
468 else if (addl_header == NULL)
470 This->full_header = (LPWSTR)wszHeaders;
474 int len_addl_header = lstrlenW(addl_header);
475 This->full_header = HeapAlloc(GetProcessHeap(), 0,
476 len_addl_header*sizeof(WCHAR)+sizeof(wszHeaders));
477 if (!This->full_header)
479 WARN("Out of memory\n");
480 hres = E_OUTOFMEMORY;
483 lstrcpyW(This->full_header, addl_header);
484 lstrcpyW(&This->full_header[len_addl_header], wszHeaders);
487 hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate2,
488 &IID_IHttpNegotiate2, (void **)&http_negotiate2);
491 WARN("IServiceProvider_QueryService IID_IHttpNegotiate2 failed: %08x\n", hres);
492 /* No goto done as per native */
496 len = sizeof(security_id)/sizeof(security_id[0]);
497 hres = IHttpNegotiate2_GetRootSecurityId(http_negotiate2, security_id, &len, 0);
500 WARN("IHttpNegotiate2_GetRootSecurityId failed: %08x\n", hres);
501 /* No goto done as per native */
505 /* FIXME: Handle security_id. Native calls undocumented function IsHostInProxyBypassList. */
507 if (This->bind_info.dwBindVerb == BINDVERB_POST)
510 hres = IInternetBindInfo_GetBindString(pOIBindInfo, BINDSTRING_POST_COOKIE, &post_cookie,
512 if (hres == S_OK && num &&
513 !InternetSetOptionW(This->request, INTERNET_OPTION_SECONDARY_CACHE_KEY,
514 post_cookie, lstrlenW(post_cookie)))
516 WARN("InternetSetOption INTERNET_OPTION_SECONDARY_CACHE_KEY failed: %d\n",
521 if (This->bind_info.dwBindVerb != BINDVERB_GET)
523 /* Native does not use GlobalLock/GlobalUnlock, so we won't either */
524 if (This->bind_info.stgmedData.tymed != TYMED_HGLOBAL)
525 WARN("Expected This->bind_info.stgmedData.tymed to be TYMED_HGLOBAL, not %d\n",
526 This->bind_info.stgmedData.tymed);
528 optional = (LPWSTR)This->bind_info.stgmedData.u.hGlobal;
530 if (!HttpSendRequestW(This->request, This->full_header, lstrlenW(This->full_header),
532 optional ? This->bind_info.cbstgmedData : 0) &&
533 GetLastError() != ERROR_IO_PENDING)
535 WARN("HttpSendRequest failed: %d\n", GetLastError());
536 hres = INET_E_DOWNLOAD_FAILURE;
544 IInternetProtocolSink_ReportResult(This->protocol_sink, hres, 0, NULL);
545 HTTPPROTOCOL_Close(This);
548 CoTaskMemFree(post_cookie);
549 CoTaskMemFree(addl_header);
551 IHttpNegotiate2_Release(http_negotiate2);
552 if (service_provider)
553 IServiceProvider_Release(service_provider);
555 while (num<sizeof(accept_mimes)/sizeof(accept_mimes[0]) &&
557 CoTaskMemFree(accept_mimes[num++]);
558 CoTaskMemFree(user_agent);
560 HeapFree(GetProcessHeap(), 0, pass);
561 HeapFree(GetProcessHeap(), 0, user);
562 HeapFree(GetProcessHeap(), 0, path);
563 HeapFree(GetProcessHeap(), 0, host);
568 static HRESULT WINAPI HttpProtocol_Continue(IInternetProtocol *iface, PROTOCOLDATA *pProtocolData)
570 HttpProtocol *This = PROTOCOL_THIS(iface);
571 DWORD len = sizeof(DWORD), status_code;
572 LPWSTR response_headers = 0, content_type = 0, content_length = 0;
574 static const WCHAR wszDefaultContentType[] =
575 {'t','e','x','t','/','h','t','m','l',0};
577 TRACE("(%p)->(%p)\n", This, pProtocolData);
581 WARN("Expected pProtocolData to be non-NULL\n");
584 else if (!This->request)
586 WARN("Expected request to be non-NULL\n");
589 else if (!This->http_negotiate)
591 WARN("Expected IHttpNegotiate pointer to be non-NULL\n");
594 else if (!This->protocol_sink)
596 WARN("Expected IInternetProtocolSink pointer to be non-NULL\n");
600 if (pProtocolData->pData == (LPVOID)BINDSTATUS_DOWNLOADINGDATA)
602 if (!HttpQueryInfoW(This->request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER,
603 &status_code, &len, NULL))
605 WARN("HttpQueryInfo failed: %d\n", GetLastError());
610 if ((!HttpQueryInfoW(This->request, HTTP_QUERY_RAW_HEADERS_CRLF, response_headers, &len,
612 GetLastError() != ERROR_INSUFFICIENT_BUFFER) ||
613 !(response_headers = HeapAlloc(GetProcessHeap(), 0, len)) ||
614 !HttpQueryInfoW(This->request, HTTP_QUERY_RAW_HEADERS_CRLF, response_headers, &len,
617 WARN("HttpQueryInfo failed: %d\n", GetLastError());
621 HRESULT hres = IHttpNegotiate_OnResponse(This->http_negotiate, status_code,
622 response_headers, NULL, NULL);
625 WARN("IHttpNegotiate_OnResponse failed: %08x\n", hres);
632 if ((!HttpQueryInfoW(This->request, HTTP_QUERY_CONTENT_TYPE, content_type, &len, NULL) &&
633 GetLastError() != ERROR_INSUFFICIENT_BUFFER) ||
634 !(content_type = HeapAlloc(GetProcessHeap(), 0, len)) ||
635 !HttpQueryInfoW(This->request, HTTP_QUERY_CONTENT_TYPE, content_type, &len, NULL))
637 WARN("HttpQueryInfo failed: %d\n", GetLastError());
638 IInternetProtocolSink_ReportProgress(This->protocol_sink,
639 (This->grfBINDF & BINDF_FROMURLMON) ?
640 BINDSTATUS_MIMETYPEAVAILABLE :
641 BINDSTATUS_RAWMIMETYPE,
642 wszDefaultContentType);
646 /* remove the charset, if present */
647 LPWSTR p = strchrW(content_type, ';');
650 IInternetProtocolSink_ReportProgress(This->protocol_sink,
651 (This->grfBINDF & BINDF_FROMURLMON) ?
652 BINDSTATUS_MIMETYPEAVAILABLE :
653 BINDSTATUS_RAWMIMETYPE,
658 if ((!HttpQueryInfoW(This->request, HTTP_QUERY_CONTENT_LENGTH, content_length, &len, NULL) &&
659 GetLastError() != ERROR_INSUFFICIENT_BUFFER) ||
660 !(content_length = HeapAlloc(GetProcessHeap(), 0, len)) ||
661 !HttpQueryInfoW(This->request, HTTP_QUERY_CONTENT_LENGTH, content_length, &len, NULL))
663 WARN("HttpQueryInfo failed: %d\n", GetLastError());
664 This->content_length = 0;
668 This->content_length = atoiW(content_length);
671 This->flags |= FLAG_FIRST_CONTINUE_COMPLETE;
674 if (pProtocolData->pData >= (LPVOID)BINDSTATUS_DOWNLOADINGDATA)
676 /* InternetQueryDataAvailable may immediately fork and perform its asynchronous
677 * read, so clear the flag _before_ calling so it does not incorrectly get cleared
678 * after the status callback is called */
679 This->flags &= ~FLAG_REQUEST_COMPLETE;
680 if (!InternetQueryDataAvailable(This->request, &This->available_bytes, 0, 0))
682 if (GetLastError() != ERROR_IO_PENDING)
684 This->flags |= FLAG_REQUEST_COMPLETE;
685 WARN("InternetQueryDataAvailable failed: %d\n", GetLastError());
686 HTTPPROTOCOL_ReportResult(This, INET_E_DATA_NOT_AVAILABLE);
691 This->flags |= FLAG_REQUEST_COMPLETE;
692 HTTPPROTOCOL_ReportData(This);
697 HeapFree(GetProcessHeap(), 0, response_headers);
698 HeapFree(GetProcessHeap(), 0, content_type);
699 HeapFree(GetProcessHeap(), 0, content_length);
701 /* Returns S_OK on native */
705 static HRESULT WINAPI HttpProtocol_Abort(IInternetProtocol *iface, HRESULT hrReason,
708 HttpProtocol *This = PROTOCOL_THIS(iface);
709 FIXME("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);
713 static HRESULT WINAPI HttpProtocol_Terminate(IInternetProtocol *iface, DWORD dwOptions)
715 HttpProtocol *This = PROTOCOL_THIS(iface);
717 TRACE("(%p)->(%08x)\n", This, dwOptions);
718 HTTPPROTOCOL_Close(This);
723 static HRESULT WINAPI HttpProtocol_Suspend(IInternetProtocol *iface)
725 HttpProtocol *This = PROTOCOL_THIS(iface);
726 FIXME("(%p)\n", This);
730 static HRESULT WINAPI HttpProtocol_Resume(IInternetProtocol *iface)
732 HttpProtocol *This = PROTOCOL_THIS(iface);
733 FIXME("(%p)\n", This);
737 static HRESULT WINAPI HttpProtocol_Read(IInternetProtocol *iface, void *pv,
738 ULONG cb, ULONG *pcbRead)
740 HttpProtocol *This = PROTOCOL_THIS(iface);
741 ULONG read = 0, len = 0;
742 HRESULT hres = S_FALSE;
744 TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
746 if (!(This->flags & FLAG_REQUEST_COMPLETE))
750 else while (!(This->flags & FLAG_ALL_DATA_READ) &&
753 if (This->available_bytes == 0)
755 /* InternetQueryDataAvailable may immediately fork and perform its asynchronous
756 * read, so clear the flag _before_ calling so it does not incorrectly get cleared
757 * after the status callback is called */
758 This->flags &= ~FLAG_REQUEST_COMPLETE;
759 if (!InternetQueryDataAvailable(This->request, &This->available_bytes, 0, 0))
761 if (GetLastError() == ERROR_IO_PENDING)
767 WARN("InternetQueryDataAvailable failed: %d\n", GetLastError());
768 hres = INET_E_DATA_NOT_AVAILABLE;
769 HTTPPROTOCOL_ReportResult(This, hres);
773 else if (This->available_bytes == 0)
775 HTTPPROTOCOL_AllDataRead(This);
780 if (!InternetReadFile(This->request, ((BYTE *)pv)+read,
781 This->available_bytes > cb-read ?
782 cb-read : This->available_bytes, &len))
784 WARN("InternetReadFile failed: %d\n", GetLastError());
785 hres = INET_E_DOWNLOAD_FAILURE;
786 HTTPPROTOCOL_ReportResult(This, hres);
791 HTTPPROTOCOL_AllDataRead(This);
796 This->current_position += len;
797 This->available_bytes -= len;
802 /* Per MSDN this should be if (read == cb), but native returns S_OK
803 * if any bytes were read, so we will too */
811 if (hres != E_PENDING)
812 This->flags |= FLAG_REQUEST_COMPLETE;
817 static HRESULT WINAPI HttpProtocol_Seek(IInternetProtocol *iface, LARGE_INTEGER dlibMove,
818 DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
820 HttpProtocol *This = PROTOCOL_THIS(iface);
821 FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
825 static HRESULT WINAPI HttpProtocol_LockRequest(IInternetProtocol *iface, DWORD dwOptions)
827 HttpProtocol *This = PROTOCOL_THIS(iface);
829 TRACE("(%p)->(%08x)\n", This, dwOptions);
831 if (!InternetLockRequestFile(This->request, &This->lock))
832 WARN("InternetLockRequest failed: %d\n", GetLastError());
837 static HRESULT WINAPI HttpProtocol_UnlockRequest(IInternetProtocol *iface)
839 HttpProtocol *This = PROTOCOL_THIS(iface);
841 TRACE("(%p)\n", This);
845 if (!InternetUnlockRequestFile(This->lock))
846 WARN("InternetUnlockRequest failed: %d\n", GetLastError());
855 #define PRIORITY_THIS(iface) DEFINE_THIS(HttpProtocol, InternetPriority, iface)
857 static HRESULT WINAPI HttpPriority_QueryInterface(IInternetPriority *iface, REFIID riid, void **ppv)
859 HttpProtocol *This = PRIORITY_THIS(iface);
860 return IInternetProtocol_QueryInterface(PROTOCOL(This), riid, ppv);
863 static ULONG WINAPI HttpPriority_AddRef(IInternetPriority *iface)
865 HttpProtocol *This = PRIORITY_THIS(iface);
866 return IInternetProtocol_AddRef(PROTOCOL(This));
869 static ULONG WINAPI HttpPriority_Release(IInternetPriority *iface)
871 HttpProtocol *This = PRIORITY_THIS(iface);
872 return IInternetProtocol_Release(PROTOCOL(This));
875 static HRESULT WINAPI HttpPriority_SetPriority(IInternetPriority *iface, LONG nPriority)
877 HttpProtocol *This = PRIORITY_THIS(iface);
879 TRACE("(%p)->(%d)\n", This, nPriority);
881 This->priority = nPriority;
885 static HRESULT WINAPI HttpPriority_GetPriority(IInternetPriority *iface, LONG *pnPriority)
887 HttpProtocol *This = PRIORITY_THIS(iface);
889 TRACE("(%p)->(%p)\n", This, pnPriority);
891 *pnPriority = This->priority;
897 static const IInternetPriorityVtbl HttpPriorityVtbl = {
898 HttpPriority_QueryInterface,
900 HttpPriority_Release,
901 HttpPriority_SetPriority,
902 HttpPriority_GetPriority
905 static const IInternetProtocolVtbl HttpProtocolVtbl = {
906 HttpProtocol_QueryInterface,
908 HttpProtocol_Release,
910 HttpProtocol_Continue,
912 HttpProtocol_Terminate,
913 HttpProtocol_Suspend,
917 HttpProtocol_LockRequest,
918 HttpProtocol_UnlockRequest
921 HRESULT HttpProtocol_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
925 TRACE("(%p %p)\n", pUnkOuter, ppobj);
929 ret = HeapAlloc(GetProcessHeap(), 0, sizeof(HttpProtocol));
931 ret->lpInternetProtocolVtbl = &HttpProtocolVtbl;
932 ret->lpInternetPriorityVtbl = &HttpPriorityVtbl;
933 ret->flags = ret->grfBINDF = 0;
934 memset(&ret->bind_info, 0, sizeof(ret->bind_info));
935 ret->protocol_sink = 0;
936 ret->http_negotiate = 0;
937 ret->internet = ret->connect = ret->request = 0;
938 ret->full_header = 0;
940 ret->current_position = ret->content_length = ret->available_bytes = 0;
944 *ppobj = PROTOCOL(ret);
949 HRESULT HttpSProtocol_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
951 FIXME("(%p %p)\n", pUnkOuter, ppobj);
952 return E_NOINTERFACE;