winefile: Use SendMessageW instead of SNDMSG.
[wine.git] / dlls / urlmon / http.c
blob8dc343264d9d5a26d763120e02bc12bb3f5d852f
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 <stdarg.h>
27 #define COBJMACROS
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winuser.h"
32 #include "ole2.h"
33 #include "urlmon.h"
34 #include "wininet.h"
35 #include "urlmon_main.h"
37 #include "wine/debug.h"
38 #include "wine/unicode.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
42 /* Flags are needed for, among other things, return HRESULTs from the Read function
43 * to conform to native. For example, Read returns:
45 * 1. E_PENDING if called before the request has completed,
46 * (flags = 0)
47 * 2. S_FALSE after all data has been read and S_OK has been reported,
48 * (flags = FLAG_REQUEST_COMPLETE | FLAG_ALL_DATA_READ | FLAG_RESULT_REPORTED)
49 * 3. INET_E_DATA_NOT_AVAILABLE if InternetQueryDataAvailable fails. The first time
50 * this occurs, INET_E_DATA_NOT_AVAILABLE will also be reported to the sink,
51 * (flags = FLAG_REQUEST_COMPLETE)
52 * but upon subsequent calls to Read no reporting will take place, yet
53 * InternetQueryDataAvailable will still be called, and, on failure,
54 * INET_E_DATA_NOT_AVAILABLE will still be returned.
55 * (flags = FLAG_REQUEST_COMPLETE | FLAG_RESULT_REPORTED)
57 * FLAG_FIRST_DATA_REPORTED and FLAG_LAST_DATA_REPORTED are needed for proper
58 * ReportData reporting. For example, if OnResponse returns S_OK, Continue will
59 * report BSCF_FIRSTDATANOTIFICATION, and when all data has been read Read will
60 * report BSCF_INTERMEDIATEDATANOTIFICATION|BSCF_LASTDATANOTIFICATION. However,
61 * if OnResponse does not return S_OK, Continue will not report data, and Read
62 * will report BSCF_FIRSTDATANOTIFICATION|BSCF_LASTDATANOTIFICATION when all
63 * data has been read.
65 #define FLAG_REQUEST_COMPLETE 0x1
66 #define FLAG_FIRST_CONTINUE_COMPLETE 0x2
67 #define FLAG_FIRST_DATA_REPORTED 0x4
68 #define FLAG_ALL_DATA_READ 0x8
69 #define FLAG_LAST_DATA_REPORTED 0x10
70 #define FLAG_RESULT_REPORTED 0x20
72 typedef struct {
73 const IInternetProtocolVtbl *lpInternetProtocolVtbl;
74 const IInternetPriorityVtbl *lpInternetPriorityVtbl;
76 DWORD flags, grfBINDF;
77 BINDINFO bind_info;
78 IInternetProtocolSink *protocol_sink;
79 IHttpNegotiate *http_negotiate;
80 HINTERNET internet, connect, request;
81 LPWSTR full_header;
82 HANDLE lock;
83 ULONG current_position, content_length, available_bytes;
84 LONG priority;
86 LONG ref;
87 } HttpProtocol;
89 /* Default headers from native */
90 static const WCHAR wszHeaders[] = {'A','c','c','e','p','t','-','E','n','c','o','d','i','n','g',
91 ':',' ','g','z','i','p',',',' ','d','e','f','l','a','t','e',0};
94 * Helpers
97 static void HTTPPROTOCOL_ReportResult(HttpProtocol *This, HRESULT hres)
99 if (!(This->flags & FLAG_RESULT_REPORTED) &&
100 This->protocol_sink)
102 This->flags |= FLAG_RESULT_REPORTED;
103 IInternetProtocolSink_ReportResult(This->protocol_sink, hres, 0, NULL);
107 static void HTTPPROTOCOL_ReportData(HttpProtocol *This)
109 DWORD bscf;
110 if (!(This->flags & FLAG_LAST_DATA_REPORTED) &&
111 This->protocol_sink)
113 if (This->flags & FLAG_FIRST_DATA_REPORTED)
115 bscf = BSCF_INTERMEDIATEDATANOTIFICATION;
117 else
119 This->flags |= FLAG_FIRST_DATA_REPORTED;
120 bscf = BSCF_FIRSTDATANOTIFICATION;
122 if (This->flags & FLAG_ALL_DATA_READ &&
123 !(This->flags & FLAG_LAST_DATA_REPORTED))
125 This->flags |= FLAG_LAST_DATA_REPORTED;
126 bscf |= BSCF_LASTDATANOTIFICATION;
128 IInternetProtocolSink_ReportData(This->protocol_sink, bscf,
129 This->current_position+This->available_bytes,
130 This->content_length);
134 static void HTTPPROTOCOL_AllDataRead(HttpProtocol *This)
136 if (!(This->flags & FLAG_ALL_DATA_READ))
137 This->flags |= FLAG_ALL_DATA_READ;
138 HTTPPROTOCOL_ReportData(This);
139 HTTPPROTOCOL_ReportResult(This, S_OK);
142 static void HTTPPROTOCOL_Close(HttpProtocol *This)
144 if (This->protocol_sink)
146 IInternetProtocolSink_Release(This->protocol_sink);
147 This->protocol_sink = 0;
149 if (This->http_negotiate)
151 IHttpNegotiate_Release(This->http_negotiate);
152 This->http_negotiate = 0;
154 if (This->request)
156 InternetCloseHandle(This->request);
157 This->request = 0;
159 if (This->connect)
161 InternetCloseHandle(This->connect);
162 This->connect = 0;
164 if (This->internet)
166 InternetCloseHandle(This->internet);
167 This->internet = 0;
169 if (This->full_header)
171 if (This->full_header != wszHeaders)
172 HeapFree(GetProcessHeap(), 0, This->full_header);
173 This->full_header = 0;
175 if (This->bind_info.cbSize)
177 ReleaseBindInfo(&This->bind_info);
178 memset(&This->bind_info, 0, sizeof(This->bind_info));
180 This->flags = 0;
183 static void CALLBACK HTTPPROTOCOL_InternetStatusCallback(
184 HINTERNET hInternet, DWORD_PTR dwContext, DWORD dwInternetStatus,
185 LPVOID lpvStatusInformation, DWORD dwStatusInformationLength)
187 HttpProtocol *This = (HttpProtocol *)dwContext;
188 PROTOCOLDATA data;
189 ULONG ulStatusCode;
191 switch (dwInternetStatus)
193 case INTERNET_STATUS_RESOLVING_NAME:
194 ulStatusCode = BINDSTATUS_FINDINGRESOURCE;
195 break;
196 case INTERNET_STATUS_CONNECTING_TO_SERVER:
197 ulStatusCode = BINDSTATUS_CONNECTING;
198 break;
199 case INTERNET_STATUS_SENDING_REQUEST:
200 ulStatusCode = BINDSTATUS_SENDINGREQUEST;
201 break;
202 case INTERNET_STATUS_REQUEST_COMPLETE:
203 This->flags |= FLAG_REQUEST_COMPLETE;
204 /* PROTOCOLDATA same as native */
205 memset(&data, 0, sizeof(data));
206 data.dwState = 0xf1000000;
207 if (This->flags & FLAG_FIRST_CONTINUE_COMPLETE)
208 data.pData = (LPVOID)BINDSTATUS_ENDDOWNLOADCOMPONENTS;
209 else
210 data.pData = (LPVOID)BINDSTATUS_DOWNLOADINGDATA;
211 if (This->grfBINDF & BINDF_FROMURLMON)
212 IInternetProtocolSink_Switch(This->protocol_sink, &data);
213 else
214 IInternetProtocol_Continue((IInternetProtocol *)This, &data);
215 return;
216 default:
217 WARN("Unhandled Internet status callback %d\n", dwInternetStatus);
218 return;
221 IInternetProtocolSink_ReportProgress(This->protocol_sink, ulStatusCode, (LPWSTR)lpvStatusInformation);
224 static inline LPWSTR strndupW(LPWSTR string, int len)
226 LPWSTR ret = NULL;
227 if (string &&
228 (ret = HeapAlloc(GetProcessHeap(), 0, (len+1)*sizeof(WCHAR))) != NULL)
230 memcpy(ret, string, len*sizeof(WCHAR));
231 ret[len] = 0;
233 return ret;
237 * Interface implementations
240 #define PROTOCOL(x) ((IInternetProtocol*) &(x)->lpInternetProtocolVtbl)
241 #define PRIORITY(x) ((IInternetPriority*) &(x)->lpInternetPriorityVtbl)
243 #define PROTOCOL_THIS(iface) DEFINE_THIS(HttpProtocol, InternetProtocol, iface)
245 static HRESULT WINAPI HttpProtocol_QueryInterface(IInternetProtocol *iface, REFIID riid, void **ppv)
247 HttpProtocol *This = PROTOCOL_THIS(iface);
249 *ppv = NULL;
250 if(IsEqualGUID(&IID_IUnknown, riid)) {
251 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
252 *ppv = PROTOCOL(This);
253 }else if(IsEqualGUID(&IID_IInternetProtocolRoot, riid)) {
254 TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", This, ppv);
255 *ppv = PROTOCOL(This);
256 }else if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
257 TRACE("(%p)->(IID_IInternetProtocol %p)\n", This, ppv);
258 *ppv = PROTOCOL(This);
259 }else if(IsEqualGUID(&IID_IInternetPriority, riid)) {
260 TRACE("(%p)->(IID_IInternetPriority %p)\n", This, ppv);
261 *ppv = PRIORITY(This);
264 if(*ppv) {
265 IInternetProtocol_AddRef(iface);
266 return S_OK;
269 WARN("not supported interface %s\n", debugstr_guid(riid));
270 return E_NOINTERFACE;
273 static ULONG WINAPI HttpProtocol_AddRef(IInternetProtocol *iface)
275 HttpProtocol *This = PROTOCOL_THIS(iface);
276 LONG ref = InterlockedIncrement(&This->ref);
277 TRACE("(%p) ref=%d\n", This, ref);
278 return ref;
281 static ULONG WINAPI HttpProtocol_Release(IInternetProtocol *iface)
283 HttpProtocol *This = PROTOCOL_THIS(iface);
284 LONG ref = InterlockedDecrement(&This->ref);
286 TRACE("(%p) ref=%d\n", This, ref);
288 if(!ref) {
289 HTTPPROTOCOL_Close(This);
290 HeapFree(GetProcessHeap(), 0, This);
292 URLMON_UnlockModule();
295 return ref;
298 static HRESULT WINAPI HttpProtocol_Start(IInternetProtocol *iface, LPCWSTR szUrl,
299 IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
300 DWORD grfPI, DWORD dwReserved)
302 HttpProtocol *This = PROTOCOL_THIS(iface);
303 URL_COMPONENTSW url;
304 DWORD len = 0, request_flags = INTERNET_FLAG_KEEP_CONNECTION;
305 ULONG num = 0;
306 IServiceProvider *service_provider = 0;
307 IHttpNegotiate2 *http_negotiate2 = 0;
308 LPWSTR host = 0, path = 0, user = 0, pass = 0, addl_header = 0,
309 post_cookie = 0, optional = 0;
310 BYTE security_id[512];
311 LPOLESTR user_agent, accept_mimes[257];
312 HRESULT hres;
314 static const WCHAR wszHttp[] = {'h','t','t','p',':'};
315 static const WCHAR wszBindVerb[BINDVERB_CUSTOM][5] =
316 {{'G','E','T',0},
317 {'P','O','S','T',0},
318 {'P','U','T',0}};
320 TRACE("(%p)->(%s %p %p %08x %d)\n", This, debugstr_w(szUrl), pOIProtSink,
321 pOIBindInfo, grfPI, dwReserved);
323 memset(&This->bind_info, 0, sizeof(This->bind_info));
324 This->bind_info.cbSize = sizeof(BINDINFO);
325 hres = IInternetBindInfo_GetBindInfo(pOIBindInfo, &This->grfBINDF, &This->bind_info);
326 if (hres != S_OK)
328 WARN("GetBindInfo failed: %08x\n", hres);
329 goto done;
332 if (lstrlenW(szUrl) < sizeof(wszHttp)/sizeof(WCHAR)
333 || memcmp(szUrl, wszHttp, sizeof(wszHttp)))
335 hres = MK_E_SYNTAX;
336 goto done;
339 memset(&url, 0, sizeof(url));
340 url.dwStructSize = sizeof(url);
341 url.dwSchemeLength = url.dwHostNameLength = url.dwUrlPathLength = url.dwUserNameLength =
342 url.dwPasswordLength = 1;
343 if (!InternetCrackUrlW(szUrl, 0, 0, &url))
345 hres = MK_E_SYNTAX;
346 goto done;
348 host = strndupW(url.lpszHostName, url.dwHostNameLength);
349 path = strndupW(url.lpszUrlPath, url.dwUrlPathLength);
350 user = strndupW(url.lpszUserName, url.dwUserNameLength);
351 pass = strndupW(url.lpszPassword, url.dwPasswordLength);
352 if (!url.nPort)
353 url.nPort = INTERNET_DEFAULT_HTTP_PORT;
355 if(!(This->grfBINDF & BINDF_FROMURLMON))
356 IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_DIRECTBIND, NULL);
358 hres = IInternetBindInfo_GetBindString(pOIBindInfo, BINDSTRING_USER_AGENT, &user_agent,
359 1, &num);
360 if (hres != S_OK || !num)
362 CHAR null_char = 0;
363 LPSTR user_agenta = NULL;
364 len = 0;
365 if ((hres = ObtainUserAgentString(0, &null_char, &len)) != E_OUTOFMEMORY)
367 WARN("ObtainUserAgentString failed: %08x\n", hres);
369 else if (!(user_agenta = HeapAlloc(GetProcessHeap(), 0, len*sizeof(CHAR))))
371 WARN("Out of memory\n");
373 else if ((hres = ObtainUserAgentString(0, user_agenta, &len)) != S_OK)
375 WARN("ObtainUserAgentString failed: %08x\n", hres);
377 else
379 if (!(user_agent = CoTaskMemAlloc((len)*sizeof(WCHAR))))
380 WARN("Out of memory\n");
381 else
382 MultiByteToWideChar(CP_ACP, 0, user_agenta, -1, user_agent, len*sizeof(WCHAR));
384 HeapFree(GetProcessHeap(), 0, user_agenta);
387 This->internet = InternetOpenW(user_agent, 0, NULL, NULL, INTERNET_FLAG_ASYNC);
388 if (!This->internet)
390 WARN("InternetOpen failed: %d\n", GetLastError());
391 hres = INET_E_NO_SESSION;
392 goto done;
395 IInternetProtocolSink_AddRef(pOIProtSink);
396 This->protocol_sink = pOIProtSink;
398 /* Native does not check for success of next call, so we won't either */
399 InternetSetStatusCallbackW(This->internet, HTTPPROTOCOL_InternetStatusCallback);
401 This->connect = InternetConnectW(This->internet, host, url.nPort, user,
402 pass, INTERNET_SERVICE_HTTP, 0, (DWORD)This);
403 if (!This->connect)
405 WARN("InternetConnect failed: %d\n", GetLastError());
406 hres = INET_E_CANNOT_CONNECT;
407 goto done;
410 num = sizeof(accept_mimes)/sizeof(accept_mimes[0])-1;
411 hres = IInternetBindInfo_GetBindString(pOIBindInfo, BINDSTRING_ACCEPT_MIMES,
412 accept_mimes,
413 num, &num);
414 if (hres != S_OK)
416 WARN("GetBindString BINDSTRING_ACCEPT_MIMES failed: %08x\n", hres);
417 hres = INET_E_NO_VALID_MEDIA;
418 goto done;
420 accept_mimes[num] = 0;
422 if (This->grfBINDF & BINDF_NOWRITECACHE)
423 request_flags |= INTERNET_FLAG_NO_CACHE_WRITE;
424 This->request = HttpOpenRequestW(This->connect, This->bind_info.dwBindVerb < BINDVERB_CUSTOM ?
425 wszBindVerb[This->bind_info.dwBindVerb] :
426 This->bind_info.szCustomVerb,
427 path, NULL, NULL, (LPCWSTR *)accept_mimes,
428 request_flags, (DWORD)This);
429 if (!This->request)
431 WARN("HttpOpenRequest failed: %d\n", GetLastError());
432 hres = INET_E_RESOURCE_NOT_FOUND;
433 goto done;
436 hres = IInternetProtocolSink_QueryInterface(pOIProtSink, &IID_IServiceProvider,
437 (void **)&service_provider);
438 if (hres != S_OK)
440 WARN("IInternetProtocolSink_QueryInterface IID_IServiceProvider failed: %08x\n", hres);
441 goto done;
444 hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate,
445 &IID_IHttpNegotiate, (void **)&This->http_negotiate);
446 if (hres != S_OK)
448 WARN("IServiceProvider_QueryService IID_IHttpNegotiate failed: %08x\n", hres);
449 goto done;
452 hres = IHttpNegotiate_BeginningTransaction(This->http_negotiate, szUrl, wszHeaders,
453 0, &addl_header);
454 if (hres != S_OK)
456 WARN("IHttpNegotiate_BeginningTransaction failed: %08x\n", hres);
457 goto done;
459 else if (addl_header == NULL)
461 This->full_header = (LPWSTR)wszHeaders;
463 else
465 int len_addl_header = lstrlenW(addl_header);
466 This->full_header = HeapAlloc(GetProcessHeap(), 0,
467 len_addl_header*sizeof(WCHAR)+sizeof(wszHeaders));
468 if (!This->full_header)
470 WARN("Out of memory\n");
471 hres = E_OUTOFMEMORY;
472 goto done;
474 lstrcpyW(This->full_header, addl_header);
475 lstrcpyW(&This->full_header[len_addl_header], wszHeaders);
478 hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate2,
479 &IID_IHttpNegotiate2, (void **)&http_negotiate2);
480 if (hres != S_OK)
482 WARN("IServiceProvider_QueryService IID_IHttpNegotiate2 failed: %08x\n", hres);
483 /* No goto done as per native */
485 else
487 len = sizeof(security_id)/sizeof(security_id[0]);
488 hres = IHttpNegotiate2_GetRootSecurityId(http_negotiate2, security_id, &len, 0);
489 if (hres != S_OK)
491 WARN("IHttpNegotiate2_GetRootSecurityId failed: %08x\n", hres);
492 /* No goto done as per native */
496 /* FIXME: Handle security_id. Native calls undocumented function IsHostInProxyBypassList. */
498 if (This->bind_info.dwBindVerb == BINDVERB_POST)
500 num = 0;
501 hres = IInternetBindInfo_GetBindString(pOIBindInfo, BINDSTRING_POST_COOKIE, &post_cookie,
502 1, &num);
503 if (hres == S_OK && num &&
504 !InternetSetOptionW(This->request, INTERNET_OPTION_SECONDARY_CACHE_KEY,
505 post_cookie, lstrlenW(post_cookie)))
507 WARN("InternetSetOption INTERNET_OPTION_SECONDARY_CACHE_KEY failed: %d\n",
508 GetLastError());
512 if (This->bind_info.dwBindVerb != BINDVERB_GET)
514 /* Native does not use GlobalLock/GlobalUnlock, so we won't either */
515 if (This->bind_info.stgmedData.tymed != TYMED_HGLOBAL)
516 WARN("Expected This->bind_info.stgmedData.tymed to be TYMED_HGLOBAL, not %d\n",
517 This->bind_info.stgmedData.tymed);
518 else
519 optional = (LPWSTR)This->bind_info.stgmedData.hGlobal;
521 if (!HttpSendRequestW(This->request, This->full_header, lstrlenW(This->full_header),
522 optional,
523 optional ? This->bind_info.cbstgmedData : 0) &&
524 GetLastError() != ERROR_IO_PENDING)
526 WARN("HttpSendRequest failed: %d\n", GetLastError());
527 hres = INET_E_DOWNLOAD_FAILURE;
528 goto done;
531 hres = S_OK;
532 done:
533 if (hres != S_OK)
535 IInternetProtocolSink_ReportResult(pOIProtSink, hres, 0, NULL);
536 HTTPPROTOCOL_Close(This);
539 CoTaskMemFree(post_cookie);
540 CoTaskMemFree(addl_header);
541 if (http_negotiate2)
542 IHttpNegotiate2_Release(http_negotiate2);
543 if (service_provider)
544 IServiceProvider_Release(service_provider);
546 while (num<sizeof(accept_mimes)/sizeof(accept_mimes[0]) &&
547 accept_mimes[num])
548 CoTaskMemFree(accept_mimes[num++]);
549 CoTaskMemFree(user_agent);
551 HeapFree(GetProcessHeap(), 0, pass);
552 HeapFree(GetProcessHeap(), 0, user);
553 HeapFree(GetProcessHeap(), 0, path);
554 HeapFree(GetProcessHeap(), 0, host);
556 return hres;
559 static HRESULT WINAPI HttpProtocol_Continue(IInternetProtocol *iface, PROTOCOLDATA *pProtocolData)
561 HttpProtocol *This = PROTOCOL_THIS(iface);
562 DWORD len = sizeof(DWORD), status_code;
563 LPWSTR response_headers = 0, content_type = 0, content_length = 0;
565 static const WCHAR wszDefaultContentType[] =
566 {'t','e','x','t','/','h','t','m','l',0};
568 TRACE("(%p)->(%p)\n", This, pProtocolData);
570 if (!pProtocolData)
572 WARN("Expected pProtocolData to be non-NULL\n");
573 return S_OK;
575 else if (!This->request)
577 WARN("Expected request to be non-NULL\n");
578 return S_OK;
580 else if (!This->http_negotiate)
582 WARN("Expected IHttpNegotiate pointer to be non-NULL\n");
583 return S_OK;
585 else if (!This->protocol_sink)
587 WARN("Expected IInternetProtocolSink pointer to be non-NULL\n");
588 return S_OK;
591 if (pProtocolData->pData == (LPVOID)BINDSTATUS_DOWNLOADINGDATA)
593 if (!HttpQueryInfoW(This->request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER,
594 &status_code, &len, NULL))
596 WARN("HttpQueryInfo failed: %d\n", GetLastError());
598 else
600 len = 0;
601 if ((!HttpQueryInfoW(This->request, HTTP_QUERY_RAW_HEADERS_CRLF, response_headers, &len,
602 NULL) &&
603 GetLastError() != ERROR_INSUFFICIENT_BUFFER) ||
604 !(response_headers = HeapAlloc(GetProcessHeap(), 0, len)) ||
605 !HttpQueryInfoW(This->request, HTTP_QUERY_RAW_HEADERS_CRLF, response_headers, &len,
606 NULL))
608 WARN("HttpQueryInfo failed: %d\n", GetLastError());
610 else
612 HRESULT hres = IHttpNegotiate_OnResponse(This->http_negotiate, status_code,
613 response_headers, NULL, NULL);
614 if (hres != S_OK)
616 WARN("IHttpNegotiate_OnResponse failed: %08x\n", hres);
617 goto done;
622 len = 0;
623 if ((!HttpQueryInfoW(This->request, HTTP_QUERY_CONTENT_TYPE, content_type, &len, NULL) &&
624 GetLastError() != ERROR_INSUFFICIENT_BUFFER) ||
625 !(content_type = HeapAlloc(GetProcessHeap(), 0, len)) ||
626 !HttpQueryInfoW(This->request, HTTP_QUERY_CONTENT_TYPE, content_type, &len, NULL))
628 WARN("HttpQueryInfo failed: %d\n", GetLastError());
629 IInternetProtocolSink_ReportProgress(This->protocol_sink,
630 (This->grfBINDF & BINDF_FROMURLMON) ?
631 BINDSTATUS_MIMETYPEAVAILABLE :
632 BINDSTATUS_RAWMIMETYPE,
633 wszDefaultContentType);
635 else
637 IInternetProtocolSink_ReportProgress(This->protocol_sink,
638 (This->grfBINDF & BINDF_FROMURLMON) ?
639 BINDSTATUS_MIMETYPEAVAILABLE :
640 BINDSTATUS_RAWMIMETYPE,
641 content_type);
644 len = 0;
645 if ((!HttpQueryInfoW(This->request, HTTP_QUERY_CONTENT_LENGTH, content_length, &len, NULL) &&
646 GetLastError() != ERROR_INSUFFICIENT_BUFFER) ||
647 !(content_length = HeapAlloc(GetProcessHeap(), 0, len)) ||
648 !HttpQueryInfoW(This->request, HTTP_QUERY_CONTENT_LENGTH, content_length, &len, NULL))
650 WARN("HttpQueryInfo failed: %d\n", GetLastError());
651 This->content_length = 0;
653 else
655 This->content_length = atoiW(content_length);
658 This->flags |= FLAG_FIRST_CONTINUE_COMPLETE;
661 if (pProtocolData->pData >= (LPVOID)BINDSTATUS_DOWNLOADINGDATA)
663 if (!InternetQueryDataAvailable(This->request, &This->available_bytes, 0, 0))
665 if (GetLastError() == ERROR_IO_PENDING)
667 This->flags &= ~FLAG_REQUEST_COMPLETE;
669 else
671 WARN("InternetQueryDataAvailable failed: %d\n", GetLastError());
672 HTTPPROTOCOL_ReportResult(This, INET_E_DATA_NOT_AVAILABLE);
675 else
677 HTTPPROTOCOL_ReportData(This);
681 done:
682 HeapFree(GetProcessHeap(), 0, response_headers);
683 HeapFree(GetProcessHeap(), 0, content_type);
684 HeapFree(GetProcessHeap(), 0, content_length);
686 /* Returns S_OK on native */
687 return S_OK;
690 static HRESULT WINAPI HttpProtocol_Abort(IInternetProtocol *iface, HRESULT hrReason,
691 DWORD dwOptions)
693 HttpProtocol *This = PROTOCOL_THIS(iface);
694 FIXME("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);
695 return E_NOTIMPL;
698 static HRESULT WINAPI HttpProtocol_Terminate(IInternetProtocol *iface, DWORD dwOptions)
700 HttpProtocol *This = PROTOCOL_THIS(iface);
702 TRACE("(%p)->(%08x)\n", This, dwOptions);
703 HTTPPROTOCOL_Close(This);
705 return S_OK;
708 static HRESULT WINAPI HttpProtocol_Suspend(IInternetProtocol *iface)
710 HttpProtocol *This = PROTOCOL_THIS(iface);
711 FIXME("(%p)\n", This);
712 return E_NOTIMPL;
715 static HRESULT WINAPI HttpProtocol_Resume(IInternetProtocol *iface)
717 HttpProtocol *This = PROTOCOL_THIS(iface);
718 FIXME("(%p)\n", This);
719 return E_NOTIMPL;
722 static HRESULT WINAPI HttpProtocol_Read(IInternetProtocol *iface, void *pv,
723 ULONG cb, ULONG *pcbRead)
725 HttpProtocol *This = PROTOCOL_THIS(iface);
726 ULONG read = 0, len = 0;
727 HRESULT hres = S_FALSE;
729 TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
731 if (!(This->flags & FLAG_REQUEST_COMPLETE))
733 hres = E_PENDING;
735 else while (!(This->flags & FLAG_ALL_DATA_READ) &&
736 read < cb)
738 if (This->available_bytes == 0)
740 /* InternetQueryDataAvailable may immediately fork and perform its asynchronous
741 * read, so clear the flag _before_ calling so it does not incorrectly get cleared
742 * after the status callback is called */
743 This->flags &= ~FLAG_REQUEST_COMPLETE;
744 if (!InternetQueryDataAvailable(This->request, &This->available_bytes, 0, 0))
746 if (GetLastError() == ERROR_IO_PENDING)
748 hres = E_PENDING;
750 else
752 WARN("InternetQueryDataAvailable failed: %d\n", GetLastError());
753 hres = INET_E_DATA_NOT_AVAILABLE;
754 HTTPPROTOCOL_ReportResult(This, hres);
756 goto done;
758 else if (This->available_bytes == 0)
760 HTTPPROTOCOL_AllDataRead(This);
763 else
765 if (!InternetReadFile(This->request, ((BYTE *)pv)+read,
766 This->available_bytes > cb-read ?
767 cb-read : This->available_bytes, &len))
769 WARN("InternetReadFile failed: %d\n", GetLastError());
770 hres = INET_E_DOWNLOAD_FAILURE;
771 HTTPPROTOCOL_ReportResult(This, hres);
772 goto done;
774 else if (len == 0)
776 HTTPPROTOCOL_AllDataRead(This);
778 else
780 read += len;
781 This->current_position += len;
782 This->available_bytes -= len;
787 /* Per MSDN this should be if (read == cb), but native returns S_OK
788 * if any bytes were read, so we will too */
789 if (read)
790 hres = S_OK;
792 done:
793 if (pcbRead)
794 *pcbRead = read;
796 if (hres != E_PENDING)
797 This->flags |= FLAG_REQUEST_COMPLETE;
799 return hres;
802 static HRESULT WINAPI HttpProtocol_Seek(IInternetProtocol *iface, LARGE_INTEGER dlibMove,
803 DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
805 HttpProtocol *This = PROTOCOL_THIS(iface);
806 FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
807 return E_NOTIMPL;
810 static HRESULT WINAPI HttpProtocol_LockRequest(IInternetProtocol *iface, DWORD dwOptions)
812 HttpProtocol *This = PROTOCOL_THIS(iface);
814 TRACE("(%p)->(%08x)\n", This, dwOptions);
816 if (!InternetLockRequestFile(This->request, &This->lock))
817 WARN("InternetLockRequest failed: %d\n", GetLastError());
819 return S_OK;
822 static HRESULT WINAPI HttpProtocol_UnlockRequest(IInternetProtocol *iface)
824 HttpProtocol *This = PROTOCOL_THIS(iface);
826 TRACE("(%p)\n", This);
828 if (This->lock)
830 if (!InternetUnlockRequestFile(This->lock))
831 WARN("InternetUnlockRequest failed: %d\n", GetLastError());
832 This->lock = 0;
835 return S_OK;
838 #undef PROTOCOL_THIS
840 #define PRIORITY_THIS(iface) DEFINE_THIS(HttpProtocol, InternetPriority, iface)
842 static HRESULT WINAPI HttpPriority_QueryInterface(IInternetPriority *iface, REFIID riid, void **ppv)
844 HttpProtocol *This = PRIORITY_THIS(iface);
845 return IInternetProtocol_QueryInterface(PROTOCOL(This), riid, ppv);
848 static ULONG WINAPI HttpPriority_AddRef(IInternetPriority *iface)
850 HttpProtocol *This = PRIORITY_THIS(iface);
851 return IInternetProtocol_AddRef(PROTOCOL(This));
854 static ULONG WINAPI HttpPriority_Release(IInternetPriority *iface)
856 HttpProtocol *This = PRIORITY_THIS(iface);
857 return IInternetProtocol_Release(PROTOCOL(This));
860 static HRESULT WINAPI HttpPriority_SetPriority(IInternetPriority *iface, LONG nPriority)
862 HttpProtocol *This = PRIORITY_THIS(iface);
864 TRACE("(%p)->(%d)\n", This, nPriority);
866 This->priority = nPriority;
867 return S_OK;
870 static HRESULT WINAPI HttpPriority_GetPriority(IInternetPriority *iface, LONG *pnPriority)
872 HttpProtocol *This = PRIORITY_THIS(iface);
874 TRACE("(%p)->(%p)\n", This, pnPriority);
876 *pnPriority = This->priority;
877 return S_OK;
880 #undef PRIORITY_THIS
882 static const IInternetPriorityVtbl HttpPriorityVtbl = {
883 HttpPriority_QueryInterface,
884 HttpPriority_AddRef,
885 HttpPriority_Release,
886 HttpPriority_SetPriority,
887 HttpPriority_GetPriority
890 static const IInternetProtocolVtbl HttpProtocolVtbl = {
891 HttpProtocol_QueryInterface,
892 HttpProtocol_AddRef,
893 HttpProtocol_Release,
894 HttpProtocol_Start,
895 HttpProtocol_Continue,
896 HttpProtocol_Abort,
897 HttpProtocol_Terminate,
898 HttpProtocol_Suspend,
899 HttpProtocol_Resume,
900 HttpProtocol_Read,
901 HttpProtocol_Seek,
902 HttpProtocol_LockRequest,
903 HttpProtocol_UnlockRequest
906 HRESULT HttpProtocol_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
908 HttpProtocol *ret;
910 TRACE("(%p %p)\n", pUnkOuter, ppobj);
912 URLMON_LockModule();
914 ret = HeapAlloc(GetProcessHeap(), 0, sizeof(HttpProtocol));
916 ret->lpInternetProtocolVtbl = &HttpProtocolVtbl;
917 ret->lpInternetPriorityVtbl = &HttpPriorityVtbl;
918 ret->flags = ret->grfBINDF = 0;
919 memset(&ret->bind_info, 0, sizeof(ret->bind_info));
920 ret->protocol_sink = 0;
921 ret->http_negotiate = 0;
922 ret->internet = ret->connect = ret->request = 0;
923 ret->full_header = 0;
924 ret->lock = 0;
925 ret->current_position = ret->content_length = ret->available_bytes = 0;
926 ret->priority = 0;
927 ret->ref = 1;
929 *ppobj = PROTOCOL(ret);
931 return S_OK;