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.
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,
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
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
73 const IInternetProtocolVtbl
*lpInternetProtocolVtbl
;
74 const IInternetPriorityVtbl
*lpInternetPriorityVtbl
;
76 DWORD flags
, grfBINDF
;
78 IInternetProtocolSink
*protocol_sink
;
79 IHttpNegotiate
*http_negotiate
;
80 HINTERNET internet
, connect
, request
;
83 ULONG current_position
, content_length
, available_bytes
;
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};
97 static void HTTPPROTOCOL_ReportResult(HttpProtocol
*This
, HRESULT hres
)
99 if (!(This
->flags
& FLAG_RESULT_REPORTED
) &&
102 This
->flags
|= FLAG_RESULT_REPORTED
;
103 IInternetProtocolSink_ReportResult(This
->protocol_sink
, hres
, 0, NULL
);
107 static void HTTPPROTOCOL_ReportData(HttpProtocol
*This
)
110 if (!(This
->flags
& FLAG_LAST_DATA_REPORTED
) &&
113 if (This
->flags
& FLAG_FIRST_DATA_REPORTED
)
115 bscf
= BSCF_INTERMEDIATEDATANOTIFICATION
;
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;
156 InternetCloseHandle(This
->request
);
161 InternetCloseHandle(This
->connect
);
166 InternetCloseHandle(This
->internet
);
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
));
183 static void CALLBACK
HTTPPROTOCOL_InternetStatusCallback(
184 HINTERNET hInternet
, DWORD_PTR dwContext
, DWORD dwInternetStatus
,
185 LPVOID lpvStatusInformation
, DWORD dwStatusInformationLength
)
187 HttpProtocol
*This
= (HttpProtocol
*)dwContext
;
191 switch (dwInternetStatus
)
193 case INTERNET_STATUS_RESOLVING_NAME
:
194 ulStatusCode
= BINDSTATUS_FINDINGRESOURCE
;
196 case INTERNET_STATUS_CONNECTING_TO_SERVER
:
197 ulStatusCode
= BINDSTATUS_CONNECTING
;
199 case INTERNET_STATUS_SENDING_REQUEST
:
200 ulStatusCode
= BINDSTATUS_SENDINGREQUEST
;
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
;
210 data
.pData
= (LPVOID
)BINDSTATUS_DOWNLOADINGDATA
;
211 if (This
->grfBINDF
& BINDF_FROMURLMON
)
212 IInternetProtocolSink_Switch(This
->protocol_sink
, &data
);
214 IInternetProtocol_Continue((IInternetProtocol
*)This
, &data
);
217 WARN("Unhandled Internet status callback %d\n", dwInternetStatus
);
221 IInternetProtocolSink_ReportProgress(This
->protocol_sink
, ulStatusCode
, (LPWSTR
)lpvStatusInformation
);
224 static inline LPWSTR
strndupW(LPWSTR string
, int len
)
228 (ret
= HeapAlloc(GetProcessHeap(), 0, (len
+1)*sizeof(WCHAR
))) != NULL
)
230 memcpy(ret
, string
, len
*sizeof(WCHAR
));
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
);
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
);
265 IInternetProtocol_AddRef(iface
);
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
);
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
);
289 HTTPPROTOCOL_Close(This
);
290 HeapFree(GetProcessHeap(), 0, This
);
292 URLMON_UnlockModule();
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
);
304 DWORD len
= 0, request_flags
= INTERNET_FLAG_KEEP_CONNECTION
;
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];
314 static const WCHAR wszHttp
[] = {'h','t','t','p',':'};
315 static const WCHAR wszBindVerb
[BINDVERB_CUSTOM
][5] =
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
);
328 WARN("GetBindInfo failed: %08x\n", hres
);
332 if (lstrlenW(szUrl
) < sizeof(wszHttp
)/sizeof(WCHAR
)
333 || memcmp(szUrl
, wszHttp
, sizeof(wszHttp
)))
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
))
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
);
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
,
360 if (hres
!= S_OK
|| !num
)
363 LPSTR user_agenta
= NULL
;
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
);
379 if (!(user_agent
= CoTaskMemAlloc((len
)*sizeof(WCHAR
))))
380 WARN("Out of memory\n");
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
);
390 WARN("InternetOpen failed: %d\n", GetLastError());
391 hres
= INET_E_NO_SESSION
;
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
);
405 WARN("InternetConnect failed: %d\n", GetLastError());
406 hres
= INET_E_CANNOT_CONNECT
;
410 num
= sizeof(accept_mimes
)/sizeof(accept_mimes
[0])-1;
411 hres
= IInternetBindInfo_GetBindString(pOIBindInfo
, BINDSTRING_ACCEPT_MIMES
,
416 WARN("GetBindString BINDSTRING_ACCEPT_MIMES failed: %08x\n", hres
);
417 hres
= INET_E_NO_VALID_MEDIA
;
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
);
431 WARN("HttpOpenRequest failed: %d\n", GetLastError());
432 hres
= INET_E_RESOURCE_NOT_FOUND
;
436 hres
= IInternetProtocolSink_QueryInterface(pOIProtSink
, &IID_IServiceProvider
,
437 (void **)&service_provider
);
440 WARN("IInternetProtocolSink_QueryInterface IID_IServiceProvider failed: %08x\n", hres
);
444 hres
= IServiceProvider_QueryService(service_provider
, &IID_IHttpNegotiate
,
445 &IID_IHttpNegotiate
, (void **)&This
->http_negotiate
);
448 WARN("IServiceProvider_QueryService IID_IHttpNegotiate failed: %08x\n", hres
);
452 hres
= IHttpNegotiate_BeginningTransaction(This
->http_negotiate
, szUrl
, wszHeaders
,
456 WARN("IHttpNegotiate_BeginningTransaction failed: %08x\n", hres
);
459 else if (addl_header
== NULL
)
461 This
->full_header
= (LPWSTR
)wszHeaders
;
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
;
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
);
482 WARN("IServiceProvider_QueryService IID_IHttpNegotiate2 failed: %08x\n", hres
);
483 /* No goto done as per native */
487 len
= sizeof(security_id
)/sizeof(security_id
[0]);
488 hres
= IHttpNegotiate2_GetRootSecurityId(http_negotiate2
, security_id
, &len
, 0);
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
)
501 hres
= IInternetBindInfo_GetBindString(pOIBindInfo
, BINDSTRING_POST_COOKIE
, &post_cookie
,
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",
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
);
519 optional
= (LPWSTR
)This
->bind_info
.stgmedData
.hGlobal
;
521 if (!HttpSendRequestW(This
->request
, This
->full_header
, lstrlenW(This
->full_header
),
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
;
535 IInternetProtocolSink_ReportResult(pOIProtSink
, hres
, 0, NULL
);
536 HTTPPROTOCOL_Close(This
);
539 CoTaskMemFree(post_cookie
);
540 CoTaskMemFree(addl_header
);
542 IHttpNegotiate2_Release(http_negotiate2
);
543 if (service_provider
)
544 IServiceProvider_Release(service_provider
);
546 while (num
<sizeof(accept_mimes
)/sizeof(accept_mimes
[0]) &&
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
);
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
);
572 WARN("Expected pProtocolData to be non-NULL\n");
575 else if (!This
->request
)
577 WARN("Expected request to be non-NULL\n");
580 else if (!This
->http_negotiate
)
582 WARN("Expected IHttpNegotiate pointer to be non-NULL\n");
585 else if (!This
->protocol_sink
)
587 WARN("Expected IInternetProtocolSink pointer to be non-NULL\n");
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());
601 if ((!HttpQueryInfoW(This
->request
, HTTP_QUERY_RAW_HEADERS_CRLF
, response_headers
, &len
,
603 GetLastError() != ERROR_INSUFFICIENT_BUFFER
) ||
604 !(response_headers
= HeapAlloc(GetProcessHeap(), 0, len
)) ||
605 !HttpQueryInfoW(This
->request
, HTTP_QUERY_RAW_HEADERS_CRLF
, response_headers
, &len
,
608 WARN("HttpQueryInfo failed: %d\n", GetLastError());
612 HRESULT hres
= IHttpNegotiate_OnResponse(This
->http_negotiate
, status_code
,
613 response_headers
, NULL
, NULL
);
616 WARN("IHttpNegotiate_OnResponse failed: %08x\n", hres
);
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
);
637 IInternetProtocolSink_ReportProgress(This
->protocol_sink
,
638 (This
->grfBINDF
& BINDF_FROMURLMON
) ?
639 BINDSTATUS_MIMETYPEAVAILABLE
:
640 BINDSTATUS_RAWMIMETYPE
,
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;
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
;
671 WARN("InternetQueryDataAvailable failed: %d\n", GetLastError());
672 HTTPPROTOCOL_ReportResult(This
, INET_E_DATA_NOT_AVAILABLE
);
677 HTTPPROTOCOL_ReportData(This
);
682 HeapFree(GetProcessHeap(), 0, response_headers
);
683 HeapFree(GetProcessHeap(), 0, content_type
);
684 HeapFree(GetProcessHeap(), 0, content_length
);
686 /* Returns S_OK on native */
690 static HRESULT WINAPI
HttpProtocol_Abort(IInternetProtocol
*iface
, HRESULT hrReason
,
693 HttpProtocol
*This
= PROTOCOL_THIS(iface
);
694 FIXME("(%p)->(%08x %08x)\n", This
, hrReason
, dwOptions
);
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
);
708 static HRESULT WINAPI
HttpProtocol_Suspend(IInternetProtocol
*iface
)
710 HttpProtocol
*This
= PROTOCOL_THIS(iface
);
711 FIXME("(%p)\n", This
);
715 static HRESULT WINAPI
HttpProtocol_Resume(IInternetProtocol
*iface
)
717 HttpProtocol
*This
= PROTOCOL_THIS(iface
);
718 FIXME("(%p)\n", This
);
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
))
735 else while (!(This
->flags
& FLAG_ALL_DATA_READ
) &&
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
)
752 WARN("InternetQueryDataAvailable failed: %d\n", GetLastError());
753 hres
= INET_E_DATA_NOT_AVAILABLE
;
754 HTTPPROTOCOL_ReportResult(This
, hres
);
758 else if (This
->available_bytes
== 0)
760 HTTPPROTOCOL_AllDataRead(This
);
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
);
776 HTTPPROTOCOL_AllDataRead(This
);
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 */
796 if (hres
!= E_PENDING
)
797 This
->flags
|= FLAG_REQUEST_COMPLETE
;
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
);
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());
822 static HRESULT WINAPI
HttpProtocol_UnlockRequest(IInternetProtocol
*iface
)
824 HttpProtocol
*This
= PROTOCOL_THIS(iface
);
826 TRACE("(%p)\n", This
);
830 if (!InternetUnlockRequestFile(This
->lock
))
831 WARN("InternetUnlockRequest failed: %d\n", GetLastError());
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
;
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
;
882 static const IInternetPriorityVtbl HttpPriorityVtbl
= {
883 HttpPriority_QueryInterface
,
885 HttpPriority_Release
,
886 HttpPriority_SetPriority
,
887 HttpPriority_GetPriority
890 static const IInternetProtocolVtbl HttpProtocolVtbl
= {
891 HttpProtocol_QueryInterface
,
893 HttpProtocol_Release
,
895 HttpProtocol_Continue
,
897 HttpProtocol_Terminate
,
898 HttpProtocol_Suspend
,
902 HttpProtocol_LockRequest
,
903 HttpProtocol_UnlockRequest
906 HRESULT
HttpProtocol_Construct(IUnknown
*pUnkOuter
, LPVOID
*ppobj
)
910 TRACE("(%p %p)\n", pUnkOuter
, ppobj
);
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;
925 ret
->current_position
= ret
->content_length
= ret
->available_bytes
= 0;
929 *ppobj
= PROTOCOL(ret
);