urlmon: RegisterNameSpace clean up.
[wine/hacks.git] / dlls / urlmon / http.c
blob9617aedf2f3a6e404c8229579d83eda1cda9e69b
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 const IInternetProtocolVtbl *lpInternetProtocolVtbl;
64 const IInternetPriorityVtbl *lpInternetPriorityVtbl;
66 DWORD flags, grfBINDF;
67 BINDINFO bind_info;
68 IInternetProtocolSink *protocol_sink;
69 IHttpNegotiate *http_negotiate;
70 HINTERNET internet, connect, request;
71 LPWSTR full_header;
72 HANDLE lock;
73 ULONG current_position, content_length, available_bytes;
74 LONG priority;
76 LONG ref;
77 } HttpProtocol;
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->flags & FLAG_RESULT_REPORTED) &&
90 This->protocol_sink)
92 This->flags |= FLAG_RESULT_REPORTED;
93 IInternetProtocolSink_ReportResult(This->protocol_sink, hres, 0, NULL);
97 static void HTTPPROTOCOL_ReportData(HttpProtocol *This)
99 DWORD bscf;
100 if (!(This->flags & FLAG_LAST_DATA_REPORTED) &&
101 This->protocol_sink)
103 if (This->flags & FLAG_FIRST_DATA_REPORTED)
105 bscf = BSCF_INTERMEDIATEDATANOTIFICATION;
107 else
109 This->flags |= FLAG_FIRST_DATA_REPORTED;
110 bscf = BSCF_FIRSTDATANOTIFICATION;
112 if (This->flags & FLAG_ALL_DATA_READ &&
113 !(This->flags & FLAG_LAST_DATA_REPORTED))
115 This->flags |= FLAG_LAST_DATA_REPORTED;
116 bscf |= BSCF_LASTDATANOTIFICATION;
118 IInternetProtocolSink_ReportData(This->protocol_sink, bscf,
119 This->current_position+This->available_bytes,
120 This->content_length);
124 static void HTTPPROTOCOL_AllDataRead(HttpProtocol *This)
126 if (!(This->flags & FLAG_ALL_DATA_READ))
127 This->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->request)
140 InternetCloseHandle(This->request);
141 if (This->connect)
142 InternetCloseHandle(This->connect);
143 if (This->internet)
145 InternetCloseHandle(This->internet);
146 This->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->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->flags |= FLAG_REQUEST_COMPLETE;
178 /* PROTOCOLDATA same as native */
179 memset(&data, 0, sizeof(data));
180 data.dwState = 0xf1000000;
181 if (This->flags & FLAG_FIRST_CONTINUE_COMPLETE)
182 data.pData = (LPVOID)BINDSTATUS_ENDDOWNLOADCOMPONENTS;
183 else
184 data.pData = (LPVOID)BINDSTATUS_DOWNLOADINGDATA;
185 if (This->grfBINDF & BINDF_FROMURLMON)
186 IInternetProtocolSink_Switch(This->protocol_sink, &data);
187 else
188 IInternetProtocol_Continue((IInternetProtocol *)This, &data);
189 return;
190 case INTERNET_STATUS_HANDLE_CREATED:
191 IInternetProtocol_AddRef((IInternetProtocol *)This);
192 return;
193 case INTERNET_STATUS_HANDLE_CLOSING:
194 if (*(HINTERNET *)lpvStatusInformation == This->connect)
196 This->connect = 0;
198 else if (*(HINTERNET *)lpvStatusInformation == This->request)
200 This->request = 0;
201 if (This->protocol_sink)
203 IInternetProtocolSink_Release(This->protocol_sink);
204 This->protocol_sink = 0;
206 if (This->bind_info.cbSize)
208 ReleaseBindInfo(&This->bind_info);
209 memset(&This->bind_info, 0, sizeof(This->bind_info));
212 IInternetProtocol_Release((IInternetProtocol *)This);
213 return;
214 default:
215 WARN("Unhandled Internet status callback %d\n", dwInternetStatus);
216 return;
219 IInternetProtocolSink_ReportProgress(This->protocol_sink, ulStatusCode, (LPWSTR)lpvStatusInformation);
222 static inline LPWSTR strndupW(LPCWSTR string, int len)
224 LPWSTR ret = NULL;
225 if (string &&
226 (ret = heap_alloc((len+1)*sizeof(WCHAR))) != NULL)
228 memcpy(ret, string, len*sizeof(WCHAR));
229 ret[len] = 0;
231 return ret;
235 * Interface implementations
238 #define PROTOCOL(x) ((IInternetProtocol*) &(x)->lpInternetProtocolVtbl)
239 #define PRIORITY(x) ((IInternetPriority*) &(x)->lpInternetPriorityVtbl)
241 #define PROTOCOL_THIS(iface) DEFINE_THIS(HttpProtocol, InternetProtocol, iface)
243 static HRESULT WINAPI HttpProtocol_QueryInterface(IInternetProtocol *iface, REFIID riid, void **ppv)
245 HttpProtocol *This = PROTOCOL_THIS(iface);
247 *ppv = NULL;
248 if(IsEqualGUID(&IID_IUnknown, riid)) {
249 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
250 *ppv = PROTOCOL(This);
251 }else if(IsEqualGUID(&IID_IInternetProtocolRoot, riid)) {
252 TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", This, ppv);
253 *ppv = PROTOCOL(This);
254 }else if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
255 TRACE("(%p)->(IID_IInternetProtocol %p)\n", This, ppv);
256 *ppv = PROTOCOL(This);
257 }else if(IsEqualGUID(&IID_IInternetPriority, riid)) {
258 TRACE("(%p)->(IID_IInternetPriority %p)\n", This, ppv);
259 *ppv = PRIORITY(This);
262 if(*ppv) {
263 IInternetProtocol_AddRef(iface);
264 return S_OK;
267 WARN("not supported interface %s\n", debugstr_guid(riid));
268 return E_NOINTERFACE;
271 static ULONG WINAPI HttpProtocol_AddRef(IInternetProtocol *iface)
273 HttpProtocol *This = PROTOCOL_THIS(iface);
274 LONG ref = InterlockedIncrement(&This->ref);
275 TRACE("(%p) ref=%d\n", This, ref);
276 return ref;
279 static ULONG WINAPI HttpProtocol_Release(IInternetProtocol *iface)
281 HttpProtocol *This = PROTOCOL_THIS(iface);
282 LONG ref = InterlockedDecrement(&This->ref);
284 TRACE("(%p) ref=%d\n", This, ref);
286 if(!ref) {
287 HTTPPROTOCOL_Close(This);
288 heap_free(This);
290 URLMON_UnlockModule();
293 return ref;
296 static HRESULT WINAPI HttpProtocol_Start(IInternetProtocol *iface, LPCWSTR szUrl,
297 IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
298 DWORD grfPI, DWORD dwReserved)
300 HttpProtocol *This = PROTOCOL_THIS(iface);
301 URL_COMPONENTSW url;
302 DWORD len = 0, request_flags = INTERNET_FLAG_KEEP_CONNECTION;
303 ULONG num = 0;
304 IServiceProvider *service_provider = 0;
305 IHttpNegotiate2 *http_negotiate2 = 0;
306 LPWSTR host = 0, path = 0, user = 0, pass = 0, addl_header = 0,
307 post_cookie = 0, optional = 0;
308 BYTE security_id[512];
309 LPOLESTR user_agent, accept_mimes[257];
310 HRESULT hres;
312 static const WCHAR wszHttp[] = {'h','t','t','p',':'};
313 static const WCHAR wszBindVerb[BINDVERB_CUSTOM][5] =
314 {{'G','E','T',0},
315 {'P','O','S','T',0},
316 {'P','U','T',0}};
318 TRACE("(%p)->(%s %p %p %08x %d)\n", This, debugstr_w(szUrl), pOIProtSink,
319 pOIBindInfo, grfPI, dwReserved);
321 IInternetProtocolSink_AddRef(pOIProtSink);
322 This->protocol_sink = pOIProtSink;
324 memset(&This->bind_info, 0, sizeof(This->bind_info));
325 This->bind_info.cbSize = sizeof(BINDINFO);
326 hres = IInternetBindInfo_GetBindInfo(pOIBindInfo, &This->grfBINDF, &This->bind_info);
327 if (hres != S_OK)
329 WARN("GetBindInfo failed: %08x\n", hres);
330 goto done;
333 if (lstrlenW(szUrl) < sizeof(wszHttp)/sizeof(WCHAR)
334 || memcmp(szUrl, wszHttp, sizeof(wszHttp)))
336 hres = MK_E_SYNTAX;
337 goto done;
340 memset(&url, 0, sizeof(url));
341 url.dwStructSize = sizeof(url);
342 url.dwSchemeLength = url.dwHostNameLength = url.dwUrlPathLength = url.dwUserNameLength =
343 url.dwPasswordLength = 1;
344 if (!InternetCrackUrlW(szUrl, 0, 0, &url))
346 hres = MK_E_SYNTAX;
347 goto done;
349 host = strndupW(url.lpszHostName, url.dwHostNameLength);
350 path = strndupW(url.lpszUrlPath, url.dwUrlPathLength);
351 user = strndupW(url.lpszUserName, url.dwUserNameLength);
352 pass = strndupW(url.lpszPassword, url.dwPasswordLength);
353 if (!url.nPort)
354 url.nPort = INTERNET_DEFAULT_HTTP_PORT;
356 if(!(This->grfBINDF & BINDF_FROMURLMON))
357 IInternetProtocolSink_ReportProgress(This->protocol_sink, BINDSTATUS_DIRECTBIND, NULL);
359 hres = IInternetBindInfo_GetBindString(pOIBindInfo, BINDSTRING_USER_AGENT, &user_agent,
360 1, &num);
361 if (hres != S_OK || !num)
363 CHAR null_char = 0;
364 LPSTR user_agenta = NULL;
365 len = 0;
366 if ((hres = ObtainUserAgentString(0, &null_char, &len)) != E_OUTOFMEMORY)
368 WARN("ObtainUserAgentString failed: %08x\n", hres);
370 else if (!(user_agenta = heap_alloc(len*sizeof(CHAR))))
372 WARN("Out of memory\n");
374 else if ((hres = ObtainUserAgentString(0, user_agenta, &len)) != S_OK)
376 WARN("ObtainUserAgentString failed: %08x\n", hres);
378 else
380 if (!(user_agent = CoTaskMemAlloc((len)*sizeof(WCHAR))))
381 WARN("Out of memory\n");
382 else
383 MultiByteToWideChar(CP_ACP, 0, user_agenta, -1, user_agent, len*sizeof(WCHAR));
385 heap_free(user_agenta);
388 This->internet = InternetOpenW(user_agent, 0, NULL, NULL, INTERNET_FLAG_ASYNC);
389 if (!This->internet)
391 WARN("InternetOpen failed: %d\n", GetLastError());
392 hres = INET_E_NO_SESSION;
393 goto done;
396 /* Native does not check for success of next call, so we won't either */
397 InternetSetStatusCallbackW(This->internet, HTTPPROTOCOL_InternetStatusCallback);
399 This->connect = InternetConnectW(This->internet, host, url.nPort, user,
400 pass, INTERNET_SERVICE_HTTP, 0, (DWORD)This);
401 if (!This->connect)
403 WARN("InternetConnect failed: %d\n", GetLastError());
404 hres = INET_E_CANNOT_CONNECT;
405 goto done;
408 num = sizeof(accept_mimes)/sizeof(accept_mimes[0])-1;
409 hres = IInternetBindInfo_GetBindString(pOIBindInfo, BINDSTRING_ACCEPT_MIMES,
410 accept_mimes,
411 num, &num);
412 if (hres != S_OK)
414 WARN("GetBindString BINDSTRING_ACCEPT_MIMES failed: %08x\n", hres);
415 hres = INET_E_NO_VALID_MEDIA;
416 goto done;
418 accept_mimes[num] = 0;
420 if (This->grfBINDF & BINDF_NOWRITECACHE)
421 request_flags |= INTERNET_FLAG_NO_CACHE_WRITE;
422 This->request = HttpOpenRequestW(This->connect, This->bind_info.dwBindVerb < BINDVERB_CUSTOM ?
423 wszBindVerb[This->bind_info.dwBindVerb] :
424 This->bind_info.szCustomVerb,
425 path, NULL, NULL, (LPCWSTR *)accept_mimes,
426 request_flags, (DWORD)This);
427 if (!This->request)
429 WARN("HttpOpenRequest failed: %d\n", GetLastError());
430 hres = INET_E_RESOURCE_NOT_FOUND;
431 goto done;
434 hres = IInternetProtocolSink_QueryInterface(This->protocol_sink, &IID_IServiceProvider,
435 (void **)&service_provider);
436 if (hres != S_OK)
438 WARN("IInternetProtocolSink_QueryInterface IID_IServiceProvider failed: %08x\n", hres);
439 goto done;
442 hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate,
443 &IID_IHttpNegotiate, (void **)&This->http_negotiate);
444 if (hres != S_OK)
446 WARN("IServiceProvider_QueryService IID_IHttpNegotiate failed: %08x\n", hres);
447 goto done;
450 hres = IHttpNegotiate_BeginningTransaction(This->http_negotiate, szUrl, wszHeaders,
451 0, &addl_header);
452 if (hres != S_OK)
454 WARN("IHttpNegotiate_BeginningTransaction failed: %08x\n", hres);
455 goto done;
457 else if (addl_header == NULL)
459 This->full_header = (LPWSTR)wszHeaders;
461 else
463 int len_addl_header = lstrlenW(addl_header);
464 This->full_header = heap_alloc(len_addl_header*sizeof(WCHAR)+sizeof(wszHeaders));
465 if (!This->full_header)
467 WARN("Out of memory\n");
468 hres = E_OUTOFMEMORY;
469 goto done;
471 lstrcpyW(This->full_header, addl_header);
472 lstrcpyW(&This->full_header[len_addl_header], wszHeaders);
475 hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate2,
476 &IID_IHttpNegotiate2, (void **)&http_negotiate2);
477 if (hres != S_OK)
479 WARN("IServiceProvider_QueryService IID_IHttpNegotiate2 failed: %08x\n", hres);
480 /* No goto done as per native */
482 else
484 len = sizeof(security_id)/sizeof(security_id[0]);
485 hres = IHttpNegotiate2_GetRootSecurityId(http_negotiate2, security_id, &len, 0);
486 if (hres != S_OK)
488 WARN("IHttpNegotiate2_GetRootSecurityId failed: %08x\n", hres);
489 /* No goto done as per native */
493 /* FIXME: Handle security_id. Native calls undocumented function IsHostInProxyBypassList. */
495 if (This->bind_info.dwBindVerb == BINDVERB_POST)
497 num = 0;
498 hres = IInternetBindInfo_GetBindString(pOIBindInfo, BINDSTRING_POST_COOKIE, &post_cookie,
499 1, &num);
500 if (hres == S_OK && num &&
501 !InternetSetOptionW(This->request, INTERNET_OPTION_SECONDARY_CACHE_KEY,
502 post_cookie, lstrlenW(post_cookie)))
504 WARN("InternetSetOption INTERNET_OPTION_SECONDARY_CACHE_KEY failed: %d\n",
505 GetLastError());
509 if (This->bind_info.dwBindVerb != BINDVERB_GET)
511 /* Native does not use GlobalLock/GlobalUnlock, so we won't either */
512 if (This->bind_info.stgmedData.tymed != TYMED_HGLOBAL)
513 WARN("Expected This->bind_info.stgmedData.tymed to be TYMED_HGLOBAL, not %d\n",
514 This->bind_info.stgmedData.tymed);
515 else
516 optional = (LPWSTR)This->bind_info.stgmedData.u.hGlobal;
518 if (!HttpSendRequestW(This->request, This->full_header, lstrlenW(This->full_header),
519 optional,
520 optional ? This->bind_info.cbstgmedData : 0) &&
521 GetLastError() != ERROR_IO_PENDING)
523 WARN("HttpSendRequest failed: %d\n", GetLastError());
524 hres = INET_E_DOWNLOAD_FAILURE;
525 goto done;
528 hres = S_OK;
529 done:
530 if (hres != S_OK)
532 IInternetProtocolSink_ReportResult(This->protocol_sink, hres, 0, NULL);
533 HTTPPROTOCOL_Close(This);
536 CoTaskMemFree(post_cookie);
537 CoTaskMemFree(addl_header);
538 if (http_negotiate2)
539 IHttpNegotiate2_Release(http_negotiate2);
540 if (service_provider)
541 IServiceProvider_Release(service_provider);
543 while (num<sizeof(accept_mimes)/sizeof(accept_mimes[0]) &&
544 accept_mimes[num])
545 CoTaskMemFree(accept_mimes[num++]);
546 CoTaskMemFree(user_agent);
548 heap_free(pass);
549 heap_free(user);
550 heap_free(path);
551 heap_free(host);
553 return hres;
556 static HRESULT WINAPI HttpProtocol_Continue(IInternetProtocol *iface, PROTOCOLDATA *pProtocolData)
558 HttpProtocol *This = PROTOCOL_THIS(iface);
559 DWORD len = sizeof(DWORD), status_code;
560 LPWSTR response_headers = 0, content_type = 0, content_length = 0;
562 static const WCHAR wszDefaultContentType[] =
563 {'t','e','x','t','/','h','t','m','l',0};
565 TRACE("(%p)->(%p)\n", This, pProtocolData);
567 if (!pProtocolData)
569 WARN("Expected pProtocolData to be non-NULL\n");
570 return S_OK;
572 else if (!This->request)
574 WARN("Expected request to be non-NULL\n");
575 return S_OK;
577 else if (!This->http_negotiate)
579 WARN("Expected IHttpNegotiate pointer to be non-NULL\n");
580 return S_OK;
582 else if (!This->protocol_sink)
584 WARN("Expected IInternetProtocolSink pointer to be non-NULL\n");
585 return S_OK;
588 if (pProtocolData->pData == (LPVOID)BINDSTATUS_DOWNLOADINGDATA)
590 if (!HttpQueryInfoW(This->request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER,
591 &status_code, &len, NULL))
593 WARN("HttpQueryInfo failed: %d\n", GetLastError());
595 else
597 len = 0;
598 if ((!HttpQueryInfoW(This->request, HTTP_QUERY_RAW_HEADERS_CRLF, response_headers, &len,
599 NULL) &&
600 GetLastError() != ERROR_INSUFFICIENT_BUFFER) ||
601 !(response_headers = heap_alloc(len)) ||
602 !HttpQueryInfoW(This->request, HTTP_QUERY_RAW_HEADERS_CRLF, response_headers, &len,
603 NULL))
605 WARN("HttpQueryInfo failed: %d\n", GetLastError());
607 else
609 HRESULT hres = IHttpNegotiate_OnResponse(This->http_negotiate, status_code,
610 response_headers, NULL, NULL);
611 if (hres != S_OK)
613 WARN("IHttpNegotiate_OnResponse failed: %08x\n", hres);
614 goto done;
619 len = 0;
620 if ((!HttpQueryInfoW(This->request, HTTP_QUERY_CONTENT_TYPE, content_type, &len, NULL) &&
621 GetLastError() != ERROR_INSUFFICIENT_BUFFER) ||
622 !(content_type = heap_alloc(len)) ||
623 !HttpQueryInfoW(This->request, HTTP_QUERY_CONTENT_TYPE, content_type, &len, NULL))
625 WARN("HttpQueryInfo failed: %d\n", GetLastError());
626 IInternetProtocolSink_ReportProgress(This->protocol_sink,
627 (This->grfBINDF & BINDF_FROMURLMON) ?
628 BINDSTATUS_MIMETYPEAVAILABLE :
629 BINDSTATUS_RAWMIMETYPE,
630 wszDefaultContentType);
632 else
634 /* remove the charset, if present */
635 LPWSTR p = strchrW(content_type, ';');
636 if (p) *p = '\0';
638 IInternetProtocolSink_ReportProgress(This->protocol_sink,
639 (This->grfBINDF & BINDF_FROMURLMON) ?
640 BINDSTATUS_MIMETYPEAVAILABLE :
641 BINDSTATUS_RAWMIMETYPE,
642 content_type);
645 len = 0;
646 if ((!HttpQueryInfoW(This->request, HTTP_QUERY_CONTENT_LENGTH, content_length, &len, NULL) &&
647 GetLastError() != ERROR_INSUFFICIENT_BUFFER) ||
648 !(content_length = heap_alloc(len)) ||
649 !HttpQueryInfoW(This->request, HTTP_QUERY_CONTENT_LENGTH, content_length, &len, NULL))
651 WARN("HttpQueryInfo failed: %d\n", GetLastError());
652 This->content_length = 0;
654 else
656 This->content_length = atoiW(content_length);
659 This->flags |= FLAG_FIRST_CONTINUE_COMPLETE;
662 if (pProtocolData->pData >= (LPVOID)BINDSTATUS_DOWNLOADINGDATA)
664 /* InternetQueryDataAvailable may immediately fork and perform its asynchronous
665 * read, so clear the flag _before_ calling so it does not incorrectly get cleared
666 * after the status callback is called */
667 This->flags &= ~FLAG_REQUEST_COMPLETE;
668 if (!InternetQueryDataAvailable(This->request, &This->available_bytes, 0, 0))
670 if (GetLastError() != ERROR_IO_PENDING)
672 This->flags |= FLAG_REQUEST_COMPLETE;
673 WARN("InternetQueryDataAvailable failed: %d\n", GetLastError());
674 HTTPPROTOCOL_ReportResult(This, INET_E_DATA_NOT_AVAILABLE);
677 else
679 This->flags |= FLAG_REQUEST_COMPLETE;
680 HTTPPROTOCOL_ReportData(This);
684 done:
685 heap_free(response_headers);
686 heap_free(content_type);
687 heap_free(content_length);
689 /* Returns S_OK on native */
690 return S_OK;
693 static HRESULT WINAPI HttpProtocol_Abort(IInternetProtocol *iface, HRESULT hrReason,
694 DWORD dwOptions)
696 HttpProtocol *This = PROTOCOL_THIS(iface);
697 FIXME("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);
698 return E_NOTIMPL;
701 static HRESULT WINAPI HttpProtocol_Terminate(IInternetProtocol *iface, DWORD dwOptions)
703 HttpProtocol *This = PROTOCOL_THIS(iface);
705 TRACE("(%p)->(%08x)\n", This, dwOptions);
706 HTTPPROTOCOL_Close(This);
708 return S_OK;
711 static HRESULT WINAPI HttpProtocol_Suspend(IInternetProtocol *iface)
713 HttpProtocol *This = PROTOCOL_THIS(iface);
714 FIXME("(%p)\n", This);
715 return E_NOTIMPL;
718 static HRESULT WINAPI HttpProtocol_Resume(IInternetProtocol *iface)
720 HttpProtocol *This = PROTOCOL_THIS(iface);
721 FIXME("(%p)\n", This);
722 return E_NOTIMPL;
725 static HRESULT WINAPI HttpProtocol_Read(IInternetProtocol *iface, void *pv,
726 ULONG cb, ULONG *pcbRead)
728 HttpProtocol *This = PROTOCOL_THIS(iface);
729 ULONG read = 0, len = 0;
730 HRESULT hres = S_FALSE;
732 TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
734 if (!(This->flags & FLAG_REQUEST_COMPLETE))
736 hres = E_PENDING;
738 else while (!(This->flags & FLAG_ALL_DATA_READ) &&
739 read < cb)
741 if (This->available_bytes == 0)
743 /* InternetQueryDataAvailable may immediately fork and perform its asynchronous
744 * read, so clear the flag _before_ calling so it does not incorrectly get cleared
745 * after the status callback is called */
746 This->flags &= ~FLAG_REQUEST_COMPLETE;
747 if (!InternetQueryDataAvailable(This->request, &This->available_bytes, 0, 0))
749 if (GetLastError() == ERROR_IO_PENDING)
751 hres = E_PENDING;
753 else
755 WARN("InternetQueryDataAvailable failed: %d\n", GetLastError());
756 hres = INET_E_DATA_NOT_AVAILABLE;
757 HTTPPROTOCOL_ReportResult(This, hres);
759 goto done;
761 else if (This->available_bytes == 0)
763 HTTPPROTOCOL_AllDataRead(This);
766 else
768 if (!InternetReadFile(This->request, ((BYTE *)pv)+read,
769 This->available_bytes > cb-read ?
770 cb-read : This->available_bytes, &len))
772 WARN("InternetReadFile failed: %d\n", GetLastError());
773 hres = INET_E_DOWNLOAD_FAILURE;
774 HTTPPROTOCOL_ReportResult(This, hres);
775 goto done;
777 else if (len == 0)
779 HTTPPROTOCOL_AllDataRead(This);
781 else
783 read += len;
784 This->current_position += len;
785 This->available_bytes -= len;
790 /* Per MSDN this should be if (read == cb), but native returns S_OK
791 * if any bytes were read, so we will too */
792 if (read)
793 hres = S_OK;
795 done:
796 if (pcbRead)
797 *pcbRead = read;
799 if (hres != E_PENDING)
800 This->flags |= FLAG_REQUEST_COMPLETE;
802 return hres;
805 static HRESULT WINAPI HttpProtocol_Seek(IInternetProtocol *iface, LARGE_INTEGER dlibMove,
806 DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
808 HttpProtocol *This = PROTOCOL_THIS(iface);
809 FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
810 return E_NOTIMPL;
813 static HRESULT WINAPI HttpProtocol_LockRequest(IInternetProtocol *iface, DWORD dwOptions)
815 HttpProtocol *This = PROTOCOL_THIS(iface);
817 TRACE("(%p)->(%08x)\n", This, dwOptions);
819 if (!InternetLockRequestFile(This->request, &This->lock))
820 WARN("InternetLockRequest failed: %d\n", GetLastError());
822 return S_OK;
825 static HRESULT WINAPI HttpProtocol_UnlockRequest(IInternetProtocol *iface)
827 HttpProtocol *This = PROTOCOL_THIS(iface);
829 TRACE("(%p)\n", This);
831 if (This->lock)
833 if (!InternetUnlockRequestFile(This->lock))
834 WARN("InternetUnlockRequest failed: %d\n", GetLastError());
835 This->lock = 0;
838 return S_OK;
841 #undef PROTOCOL_THIS
843 #define PRIORITY_THIS(iface) DEFINE_THIS(HttpProtocol, InternetPriority, iface)
845 static HRESULT WINAPI HttpPriority_QueryInterface(IInternetPriority *iface, REFIID riid, void **ppv)
847 HttpProtocol *This = PRIORITY_THIS(iface);
848 return IInternetProtocol_QueryInterface(PROTOCOL(This), riid, ppv);
851 static ULONG WINAPI HttpPriority_AddRef(IInternetPriority *iface)
853 HttpProtocol *This = PRIORITY_THIS(iface);
854 return IInternetProtocol_AddRef(PROTOCOL(This));
857 static ULONG WINAPI HttpPriority_Release(IInternetPriority *iface)
859 HttpProtocol *This = PRIORITY_THIS(iface);
860 return IInternetProtocol_Release(PROTOCOL(This));
863 static HRESULT WINAPI HttpPriority_SetPriority(IInternetPriority *iface, LONG nPriority)
865 HttpProtocol *This = PRIORITY_THIS(iface);
867 TRACE("(%p)->(%d)\n", This, nPriority);
869 This->priority = nPriority;
870 return S_OK;
873 static HRESULT WINAPI HttpPriority_GetPriority(IInternetPriority *iface, LONG *pnPriority)
875 HttpProtocol *This = PRIORITY_THIS(iface);
877 TRACE("(%p)->(%p)\n", This, pnPriority);
879 *pnPriority = This->priority;
880 return S_OK;
883 #undef PRIORITY_THIS
885 static const IInternetPriorityVtbl HttpPriorityVtbl = {
886 HttpPriority_QueryInterface,
887 HttpPriority_AddRef,
888 HttpPriority_Release,
889 HttpPriority_SetPriority,
890 HttpPriority_GetPriority
893 static const IInternetProtocolVtbl HttpProtocolVtbl = {
894 HttpProtocol_QueryInterface,
895 HttpProtocol_AddRef,
896 HttpProtocol_Release,
897 HttpProtocol_Start,
898 HttpProtocol_Continue,
899 HttpProtocol_Abort,
900 HttpProtocol_Terminate,
901 HttpProtocol_Suspend,
902 HttpProtocol_Resume,
903 HttpProtocol_Read,
904 HttpProtocol_Seek,
905 HttpProtocol_LockRequest,
906 HttpProtocol_UnlockRequest
909 HRESULT HttpProtocol_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
911 HttpProtocol *ret;
913 TRACE("(%p %p)\n", pUnkOuter, ppobj);
915 URLMON_LockModule();
917 ret = heap_alloc(sizeof(HttpProtocol));
919 ret->lpInternetProtocolVtbl = &HttpProtocolVtbl;
920 ret->lpInternetPriorityVtbl = &HttpPriorityVtbl;
921 ret->flags = ret->grfBINDF = 0;
922 memset(&ret->bind_info, 0, sizeof(ret->bind_info));
923 ret->protocol_sink = 0;
924 ret->http_negotiate = 0;
925 ret->internet = ret->connect = ret->request = 0;
926 ret->full_header = 0;
927 ret->lock = 0;
928 ret->current_position = ret->content_length = ret->available_bytes = 0;
929 ret->priority = 0;
930 ret->ref = 1;
932 *ppobj = PROTOCOL(ret);
934 return S_OK;
937 HRESULT HttpSProtocol_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
939 FIXME("(%p %p)\n", pUnkOuter, ppobj);
940 return E_NOINTERFACE;