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;
152 InternetCloseHandle(This->request);
157 InternetCloseHandle(This->connect);
162 InternetCloseHandle(This->internet);
165 if (This->full_header)
167 if (This->full_header != wszHeaders)
168 HeapFree(GetProcessHeap(), 0, This->full_header);
169 This->full_header = 0;
174 static void CALLBACK HTTPPROTOCOL_InternetStatusCallback(
175 HINTERNET hInternet, DWORD_PTR dwContext, DWORD dwInternetStatus,
176 LPVOID lpvStatusInformation, DWORD dwStatusInformationLength)
178 HttpProtocol *This = (HttpProtocol *)dwContext;
182 switch (dwInternetStatus)
184 case INTERNET_STATUS_RESOLVING_NAME:
185 ulStatusCode = BINDSTATUS_FINDINGRESOURCE;
187 case INTERNET_STATUS_CONNECTING_TO_SERVER:
188 ulStatusCode = BINDSTATUS_CONNECTING;
190 case INTERNET_STATUS_SENDING_REQUEST:
191 ulStatusCode = BINDSTATUS_SENDINGREQUEST;
193 case INTERNET_STATUS_REQUEST_COMPLETE:
194 This->flags |= FLAG_REQUEST_COMPLETE;
195 /* PROTOCOLDATA same as native */
196 memset(&data, 0, sizeof(data));
197 data.dwState = 0xf1000000;
198 if (This->flags & FLAG_FIRST_CONTINUE_COMPLETE)
199 data.pData = (LPVOID)BINDSTATUS_ENDDOWNLOADCOMPONENTS;
201 data.pData = (LPVOID)BINDSTATUS_DOWNLOADINGDATA;
202 if (This->grfBINDF & BINDF_FROMURLMON)
203 IInternetProtocolSink_Switch(This->protocol_sink, &data);
205 IInternetProtocol_Continue((IInternetProtocol *)This, &data);
207 case INTERNET_STATUS_HANDLE_CREATED:
208 IInternetProtocol_AddRef((IInternetProtocol *)This);
210 case INTERNET_STATUS_HANDLE_CLOSING:
211 if (This->protocol_sink)
213 IInternetProtocolSink_Release(This->protocol_sink);
214 This->protocol_sink = 0;
216 if (This->bind_info.cbSize)
218 ReleaseBindInfo(&This->bind_info);
219 memset(&This->bind_info, 0, sizeof(This->bind_info));
221 IInternetProtocol_Release((IInternetProtocol *)This);
224 WARN("Unhandled Internet status callback %d\n", dwInternetStatus);
228 IInternetProtocolSink_ReportProgress(This->protocol_sink, ulStatusCode, (LPWSTR)lpvStatusInformation);
231 static inline LPWSTR strndupW(LPWSTR string, int len)
235 (ret = HeapAlloc(GetProcessHeap(), 0, (len+1)*sizeof(WCHAR))) != NULL)
237 memcpy(ret, string, len*sizeof(WCHAR));
244 * Interface implementations
247 #define PROTOCOL(x) ((IInternetProtocol*) &(x)->lpInternetProtocolVtbl)
248 #define PRIORITY(x) ((IInternetPriority*) &(x)->lpInternetPriorityVtbl)
250 #define PROTOCOL_THIS(iface) DEFINE_THIS(HttpProtocol, InternetProtocol, iface)
252 static HRESULT WINAPI HttpProtocol_QueryInterface(IInternetProtocol *iface, REFIID riid, void **ppv)
254 HttpProtocol *This = PROTOCOL_THIS(iface);
257 if(IsEqualGUID(&IID_IUnknown, riid)) {
258 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
259 *ppv = PROTOCOL(This);
260 }else if(IsEqualGUID(&IID_IInternetProtocolRoot, riid)) {
261 TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", This, ppv);
262 *ppv = PROTOCOL(This);
263 }else if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
264 TRACE("(%p)->(IID_IInternetProtocol %p)\n", This, ppv);
265 *ppv = PROTOCOL(This);
266 }else if(IsEqualGUID(&IID_IInternetPriority, riid)) {
267 TRACE("(%p)->(IID_IInternetPriority %p)\n", This, ppv);
268 *ppv = PRIORITY(This);
272 IInternetProtocol_AddRef(iface);
276 WARN("not supported interface %s\n", debugstr_guid(riid));
277 return E_NOINTERFACE;
280 static ULONG WINAPI HttpProtocol_AddRef(IInternetProtocol *iface)
282 HttpProtocol *This = PROTOCOL_THIS(iface);
283 LONG ref = InterlockedIncrement(&This->ref);
284 TRACE("(%p) ref=%d\n", This, ref);
288 static ULONG WINAPI HttpProtocol_Release(IInternetProtocol *iface)
290 HttpProtocol *This = PROTOCOL_THIS(iface);
291 LONG ref = InterlockedDecrement(&This->ref);
293 TRACE("(%p) ref=%d\n", This, ref);
296 HTTPPROTOCOL_Close(This);
297 HeapFree(GetProcessHeap(), 0, This);
299 URLMON_UnlockModule();
305 static HRESULT WINAPI HttpProtocol_Start(IInternetProtocol *iface, LPCWSTR szUrl,
306 IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
307 DWORD grfPI, DWORD dwReserved)
309 HttpProtocol *This = PROTOCOL_THIS(iface);
311 DWORD len = 0, request_flags = INTERNET_FLAG_KEEP_CONNECTION;
313 IServiceProvider *service_provider = 0;
314 IHttpNegotiate2 *http_negotiate2 = 0;
315 LPWSTR host = 0, path = 0, user = 0, pass = 0, addl_header = 0,
316 post_cookie = 0, optional = 0;
317 BYTE security_id[512];
318 LPOLESTR user_agent, accept_mimes[257];
321 static const WCHAR wszHttp[] = {'h','t','t','p',':'};
322 static const WCHAR wszBindVerb[BINDVERB_CUSTOM][5] =
327 TRACE("(%p)->(%s %p %p %08x %d)\n", This, debugstr_w(szUrl), pOIProtSink,
328 pOIBindInfo, grfPI, dwReserved);
330 IInternetProtocolSink_AddRef(pOIProtSink);
331 This->protocol_sink = pOIProtSink;
333 memset(&This->bind_info, 0, sizeof(This->bind_info));
334 This->bind_info.cbSize = sizeof(BINDINFO);
335 hres = IInternetBindInfo_GetBindInfo(pOIBindInfo, &This->grfBINDF, &This->bind_info);
338 WARN("GetBindInfo failed: %08x\n", hres);
342 if (lstrlenW(szUrl) < sizeof(wszHttp)/sizeof(WCHAR)
343 || memcmp(szUrl, wszHttp, sizeof(wszHttp)))
349 memset(&url, 0, sizeof(url));
350 url.dwStructSize = sizeof(url);
351 url.dwSchemeLength = url.dwHostNameLength = url.dwUrlPathLength = url.dwUserNameLength =
352 url.dwPasswordLength = 1;
353 if (!InternetCrackUrlW(szUrl, 0, 0, &url))
358 host = strndupW(url.lpszHostName, url.dwHostNameLength);
359 path = strndupW(url.lpszUrlPath, url.dwUrlPathLength);
360 user = strndupW(url.lpszUserName, url.dwUserNameLength);
361 pass = strndupW(url.lpszPassword, url.dwPasswordLength);
363 url.nPort = INTERNET_DEFAULT_HTTP_PORT;
365 if(!(This->grfBINDF & BINDF_FROMURLMON))
366 IInternetProtocolSink_ReportProgress(This->protocol_sink, BINDSTATUS_DIRECTBIND, NULL);
368 hres = IInternetBindInfo_GetBindString(pOIBindInfo, BINDSTRING_USER_AGENT, &user_agent,
370 if (hres != S_OK || !num)
373 LPSTR user_agenta = NULL;
375 if ((hres = ObtainUserAgentString(0, &null_char, &len)) != E_OUTOFMEMORY)
377 WARN("ObtainUserAgentString failed: %08x\n", hres);
379 else if (!(user_agenta = HeapAlloc(GetProcessHeap(), 0, len*sizeof(CHAR))))
381 WARN("Out of memory\n");
383 else if ((hres = ObtainUserAgentString(0, user_agenta, &len)) != S_OK)
385 WARN("ObtainUserAgentString failed: %08x\n", hres);
389 if (!(user_agent = CoTaskMemAlloc((len)*sizeof(WCHAR))))
390 WARN("Out of memory\n");
392 MultiByteToWideChar(CP_ACP, 0, user_agenta, -1, user_agent, len*sizeof(WCHAR));
394 HeapFree(GetProcessHeap(), 0, user_agenta);
397 This->internet = InternetOpenW(user_agent, 0, NULL, NULL, INTERNET_FLAG_ASYNC);
400 WARN("InternetOpen failed: %d\n", GetLastError());
401 hres = INET_E_NO_SESSION;
405 /* Native does not check for success of next call, so we won't either */
406 InternetSetStatusCallbackW(This->internet, HTTPPROTOCOL_InternetStatusCallback);
408 This->connect = InternetConnectW(This->internet, host, url.nPort, user,
409 pass, INTERNET_SERVICE_HTTP, 0, (DWORD)This);
412 WARN("InternetConnect failed: %d\n", GetLastError());
413 hres = INET_E_CANNOT_CONNECT;
417 num = sizeof(accept_mimes)/sizeof(accept_mimes[0])-1;
418 hres = IInternetBindInfo_GetBindString(pOIBindInfo, BINDSTRING_ACCEPT_MIMES,
423 WARN("GetBindString BINDSTRING_ACCEPT_MIMES failed: %08x\n", hres);
424 hres = INET_E_NO_VALID_MEDIA;
427 accept_mimes[num] = 0;
429 if (This->grfBINDF & BINDF_NOWRITECACHE)
430 request_flags |= INTERNET_FLAG_NO_CACHE_WRITE;
431 This->request = HttpOpenRequestW(This->connect, This->bind_info.dwBindVerb < BINDVERB_CUSTOM ?
432 wszBindVerb[This->bind_info.dwBindVerb] :
433 This->bind_info.szCustomVerb,
434 path, NULL, NULL, (LPCWSTR *)accept_mimes,
435 request_flags, (DWORD)This);
438 WARN("HttpOpenRequest failed: %d\n", GetLastError());
439 hres = INET_E_RESOURCE_NOT_FOUND;
443 hres = IInternetProtocolSink_QueryInterface(This->protocol_sink, &IID_IServiceProvider,
444 (void **)&service_provider);
447 WARN("IInternetProtocolSink_QueryInterface IID_IServiceProvider failed: %08x\n", hres);
451 hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate,
452 &IID_IHttpNegotiate, (void **)&This->http_negotiate);
455 WARN("IServiceProvider_QueryService IID_IHttpNegotiate failed: %08x\n", hres);
459 hres = IHttpNegotiate_BeginningTransaction(This->http_negotiate, szUrl, wszHeaders,
463 WARN("IHttpNegotiate_BeginningTransaction failed: %08x\n", hres);
466 else if (addl_header == NULL)
468 This->full_header = (LPWSTR)wszHeaders;
472 int len_addl_header = lstrlenW(addl_header);
473 This->full_header = HeapAlloc(GetProcessHeap(), 0,
474 len_addl_header*sizeof(WCHAR)+sizeof(wszHeaders));
475 if (!This->full_header)
477 WARN("Out of memory\n");
478 hres = E_OUTOFMEMORY;
481 lstrcpyW(This->full_header, addl_header);
482 lstrcpyW(&This->full_header[len_addl_header], wszHeaders);
485 hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate2,
486 &IID_IHttpNegotiate2, (void **)&http_negotiate2);
489 WARN("IServiceProvider_QueryService IID_IHttpNegotiate2 failed: %08x\n", hres);
490 /* No goto done as per native */
494 len = sizeof(security_id)/sizeof(security_id[0]);
495 hres = IHttpNegotiate2_GetRootSecurityId(http_negotiate2, security_id, &len, 0);
498 WARN("IHttpNegotiate2_GetRootSecurityId failed: %08x\n", hres);
499 /* No goto done as per native */
503 /* FIXME: Handle security_id. Native calls undocumented function IsHostInProxyBypassList. */
505 if (This->bind_info.dwBindVerb == BINDVERB_POST)
508 hres = IInternetBindInfo_GetBindString(pOIBindInfo, BINDSTRING_POST_COOKIE, &post_cookie,
510 if (hres == S_OK && num &&
511 !InternetSetOptionW(This->request, INTERNET_OPTION_SECONDARY_CACHE_KEY,
512 post_cookie, lstrlenW(post_cookie)))
514 WARN("InternetSetOption INTERNET_OPTION_SECONDARY_CACHE_KEY failed: %d\n",
519 if (This->bind_info.dwBindVerb != BINDVERB_GET)
521 /* Native does not use GlobalLock/GlobalUnlock, so we won't either */
522 if (This->bind_info.stgmedData.tymed != TYMED_HGLOBAL)
523 WARN("Expected This->bind_info.stgmedData.tymed to be TYMED_HGLOBAL, not %d\n",
524 This->bind_info.stgmedData.tymed);
526 optional = (LPWSTR)This->bind_info.stgmedData.u.hGlobal;
528 if (!HttpSendRequestW(This->request, This->full_header, lstrlenW(This->full_header),
530 optional ? This->bind_info.cbstgmedData : 0) &&
531 GetLastError() != ERROR_IO_PENDING)
533 WARN("HttpSendRequest failed: %d\n", GetLastError());
534 hres = INET_E_DOWNLOAD_FAILURE;
542 IInternetProtocolSink_ReportResult(This->protocol_sink, hres, 0, NULL);
543 HTTPPROTOCOL_Close(This);
546 CoTaskMemFree(post_cookie);
547 CoTaskMemFree(addl_header);
549 IHttpNegotiate2_Release(http_negotiate2);
550 if (service_provider)
551 IServiceProvider_Release(service_provider);
553 while (num<sizeof(accept_mimes)/sizeof(accept_mimes[0]) &&
555 CoTaskMemFree(accept_mimes[num++]);
556 CoTaskMemFree(user_agent);
558 HeapFree(GetProcessHeap(), 0, pass);
559 HeapFree(GetProcessHeap(), 0, user);
560 HeapFree(GetProcessHeap(), 0, path);
561 HeapFree(GetProcessHeap(), 0, host);
566 static HRESULT WINAPI HttpProtocol_Continue(IInternetProtocol *iface, PROTOCOLDATA *pProtocolData)
568 HttpProtocol *This = PROTOCOL_THIS(iface);
569 DWORD len = sizeof(DWORD), status_code;
570 LPWSTR response_headers = 0, content_type = 0, content_length = 0;
572 static const WCHAR wszDefaultContentType[] =
573 {'t','e','x','t','/','h','t','m','l',0};
575 TRACE("(%p)->(%p)\n", This, pProtocolData);
579 WARN("Expected pProtocolData to be non-NULL\n");
582 else if (!This->request)
584 WARN("Expected request to be non-NULL\n");
587 else if (!This->http_negotiate)
589 WARN("Expected IHttpNegotiate pointer to be non-NULL\n");
592 else if (!This->protocol_sink)
594 WARN("Expected IInternetProtocolSink pointer to be non-NULL\n");
598 if (pProtocolData->pData == (LPVOID)BINDSTATUS_DOWNLOADINGDATA)
600 if (!HttpQueryInfoW(This->request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER,
601 &status_code, &len, NULL))
603 WARN("HttpQueryInfo failed: %d\n", GetLastError());
608 if ((!HttpQueryInfoW(This->request, HTTP_QUERY_RAW_HEADERS_CRLF, response_headers, &len,
610 GetLastError() != ERROR_INSUFFICIENT_BUFFER) ||
611 !(response_headers = HeapAlloc(GetProcessHeap(), 0, len)) ||
612 !HttpQueryInfoW(This->request, HTTP_QUERY_RAW_HEADERS_CRLF, response_headers, &len,
615 WARN("HttpQueryInfo failed: %d\n", GetLastError());
619 HRESULT hres = IHttpNegotiate_OnResponse(This->http_negotiate, status_code,
620 response_headers, NULL, NULL);
623 WARN("IHttpNegotiate_OnResponse failed: %08x\n", hres);
630 if ((!HttpQueryInfoW(This->request, HTTP_QUERY_CONTENT_TYPE, content_type, &len, NULL) &&
631 GetLastError() != ERROR_INSUFFICIENT_BUFFER) ||
632 !(content_type = HeapAlloc(GetProcessHeap(), 0, len)) ||
633 !HttpQueryInfoW(This->request, HTTP_QUERY_CONTENT_TYPE, content_type, &len, NULL))
635 WARN("HttpQueryInfo failed: %d\n", GetLastError());
636 IInternetProtocolSink_ReportProgress(This->protocol_sink,
637 (This->grfBINDF & BINDF_FROMURLMON) ?
638 BINDSTATUS_MIMETYPEAVAILABLE :
639 BINDSTATUS_RAWMIMETYPE,
640 wszDefaultContentType);
644 IInternetProtocolSink_ReportProgress(This->protocol_sink,
645 (This->grfBINDF & BINDF_FROMURLMON) ?
646 BINDSTATUS_MIMETYPEAVAILABLE :
647 BINDSTATUS_RAWMIMETYPE,
652 if ((!HttpQueryInfoW(This->request, HTTP_QUERY_CONTENT_LENGTH, content_length, &len, NULL) &&
653 GetLastError() != ERROR_INSUFFICIENT_BUFFER) ||
654 !(content_length = HeapAlloc(GetProcessHeap(), 0, len)) ||
655 !HttpQueryInfoW(This->request, HTTP_QUERY_CONTENT_LENGTH, content_length, &len, NULL))
657 WARN("HttpQueryInfo failed: %d\n", GetLastError());
658 This->content_length = 0;
662 This->content_length = atoiW(content_length);
665 This->flags |= FLAG_FIRST_CONTINUE_COMPLETE;
668 if (pProtocolData->pData >= (LPVOID)BINDSTATUS_DOWNLOADINGDATA)
670 /* InternetQueryDataAvailable may immediately fork and perform its asynchronous
671 * read, so clear the flag _before_ calling so it does not incorrectly get cleared
672 * after the status callback is called */
673 This->flags &= ~FLAG_REQUEST_COMPLETE;
674 if (!InternetQueryDataAvailable(This->request, &This->available_bytes, 0, 0))
676 if (GetLastError() != ERROR_IO_PENDING)
678 This->flags |= FLAG_REQUEST_COMPLETE;
679 WARN("InternetQueryDataAvailable failed: %d\n", GetLastError());
680 HTTPPROTOCOL_ReportResult(This, INET_E_DATA_NOT_AVAILABLE);
685 This->flags |= FLAG_REQUEST_COMPLETE;
686 HTTPPROTOCOL_ReportData(This);
691 HeapFree(GetProcessHeap(), 0, response_headers);
692 HeapFree(GetProcessHeap(), 0, content_type);
693 HeapFree(GetProcessHeap(), 0, content_length);
695 /* Returns S_OK on native */
699 static HRESULT WINAPI HttpProtocol_Abort(IInternetProtocol *iface, HRESULT hrReason,
702 HttpProtocol *This = PROTOCOL_THIS(iface);
703 FIXME("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);
707 static HRESULT WINAPI HttpProtocol_Terminate(IInternetProtocol *iface, DWORD dwOptions)
709 HttpProtocol *This = PROTOCOL_THIS(iface);
711 TRACE("(%p)->(%08x)\n", This, dwOptions);
712 HTTPPROTOCOL_Close(This);
717 static HRESULT WINAPI HttpProtocol_Suspend(IInternetProtocol *iface)
719 HttpProtocol *This = PROTOCOL_THIS(iface);
720 FIXME("(%p)\n", This);
724 static HRESULT WINAPI HttpProtocol_Resume(IInternetProtocol *iface)
726 HttpProtocol *This = PROTOCOL_THIS(iface);
727 FIXME("(%p)\n", This);
731 static HRESULT WINAPI HttpProtocol_Read(IInternetProtocol *iface, void *pv,
732 ULONG cb, ULONG *pcbRead)
734 HttpProtocol *This = PROTOCOL_THIS(iface);
735 ULONG read = 0, len = 0;
736 HRESULT hres = S_FALSE;
738 TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
740 if (!(This->flags & FLAG_REQUEST_COMPLETE))
744 else while (!(This->flags & FLAG_ALL_DATA_READ) &&
747 if (This->available_bytes == 0)
749 /* InternetQueryDataAvailable may immediately fork and perform its asynchronous
750 * read, so clear the flag _before_ calling so it does not incorrectly get cleared
751 * after the status callback is called */
752 This->flags &= ~FLAG_REQUEST_COMPLETE;
753 if (!InternetQueryDataAvailable(This->request, &This->available_bytes, 0, 0))
755 if (GetLastError() == ERROR_IO_PENDING)
761 WARN("InternetQueryDataAvailable failed: %d\n", GetLastError());
762 hres = INET_E_DATA_NOT_AVAILABLE;
763 HTTPPROTOCOL_ReportResult(This, hres);
767 else if (This->available_bytes == 0)
769 HTTPPROTOCOL_AllDataRead(This);
774 if (!InternetReadFile(This->request, ((BYTE *)pv)+read,
775 This->available_bytes > cb-read ?
776 cb-read : This->available_bytes, &len))
778 WARN("InternetReadFile failed: %d\n", GetLastError());
779 hres = INET_E_DOWNLOAD_FAILURE;
780 HTTPPROTOCOL_ReportResult(This, hres);
785 HTTPPROTOCOL_AllDataRead(This);
790 This->current_position += len;
791 This->available_bytes -= len;
796 /* Per MSDN this should be if (read == cb), but native returns S_OK
797 * if any bytes were read, so we will too */
805 if (hres != E_PENDING)
806 This->flags |= FLAG_REQUEST_COMPLETE;
811 static HRESULT WINAPI HttpProtocol_Seek(IInternetProtocol *iface, LARGE_INTEGER dlibMove,
812 DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
814 HttpProtocol *This = PROTOCOL_THIS(iface);
815 FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
819 static HRESULT WINAPI HttpProtocol_LockRequest(IInternetProtocol *iface, DWORD dwOptions)
821 HttpProtocol *This = PROTOCOL_THIS(iface);
823 TRACE("(%p)->(%08x)\n", This, dwOptions);
825 if (!InternetLockRequestFile(This->request, &This->lock))
826 WARN("InternetLockRequest failed: %d\n", GetLastError());
831 static HRESULT WINAPI HttpProtocol_UnlockRequest(IInternetProtocol *iface)
833 HttpProtocol *This = PROTOCOL_THIS(iface);
835 TRACE("(%p)\n", This);
839 if (!InternetUnlockRequestFile(This->lock))
840 WARN("InternetUnlockRequest failed: %d\n", GetLastError());
849 #define PRIORITY_THIS(iface) DEFINE_THIS(HttpProtocol, InternetPriority, iface)
851 static HRESULT WINAPI HttpPriority_QueryInterface(IInternetPriority *iface, REFIID riid, void **ppv)
853 HttpProtocol *This = PRIORITY_THIS(iface);
854 return IInternetProtocol_QueryInterface(PROTOCOL(This), riid, ppv);
857 static ULONG WINAPI HttpPriority_AddRef(IInternetPriority *iface)
859 HttpProtocol *This = PRIORITY_THIS(iface);
860 return IInternetProtocol_AddRef(PROTOCOL(This));
863 static ULONG WINAPI HttpPriority_Release(IInternetPriority *iface)
865 HttpProtocol *This = PRIORITY_THIS(iface);
866 return IInternetProtocol_Release(PROTOCOL(This));
869 static HRESULT WINAPI HttpPriority_SetPriority(IInternetPriority *iface, LONG nPriority)
871 HttpProtocol *This = PRIORITY_THIS(iface);
873 TRACE("(%p)->(%d)\n", This, nPriority);
875 This->priority = nPriority;
879 static HRESULT WINAPI HttpPriority_GetPriority(IInternetPriority *iface, LONG *pnPriority)
881 HttpProtocol *This = PRIORITY_THIS(iface);
883 TRACE("(%p)->(%p)\n", This, pnPriority);
885 *pnPriority = This->priority;
891 static const IInternetPriorityVtbl HttpPriorityVtbl = {
892 HttpPriority_QueryInterface,
894 HttpPriority_Release,
895 HttpPriority_SetPriority,
896 HttpPriority_GetPriority
899 static const IInternetProtocolVtbl HttpProtocolVtbl = {
900 HttpProtocol_QueryInterface,
902 HttpProtocol_Release,
904 HttpProtocol_Continue,
906 HttpProtocol_Terminate,
907 HttpProtocol_Suspend,
911 HttpProtocol_LockRequest,
912 HttpProtocol_UnlockRequest
915 HRESULT HttpProtocol_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
919 TRACE("(%p %p)\n", pUnkOuter, ppobj);
923 ret = HeapAlloc(GetProcessHeap(), 0, sizeof(HttpProtocol));
925 ret->lpInternetProtocolVtbl = &HttpProtocolVtbl;
926 ret->lpInternetPriorityVtbl = &HttpPriorityVtbl;
927 ret->flags = ret->grfBINDF = 0;
928 memset(&ret->bind_info, 0, sizeof(ret->bind_info));
929 ret->protocol_sink = 0;
930 ret->http_negotiate = 0;
931 ret->internet = ret->connect = ret->request = 0;
932 ret->full_header = 0;
934 ret->current_position = ret->content_length = ret->available_bytes = 0;
938 *ppobj = PROTOCOL(ret);