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_CLOSING
:
208 if (This
->protocol_sink
)
210 IInternetProtocolSink_Release(This
->protocol_sink
);
211 This
->protocol_sink
= 0;
213 if (This
->bind_info
.cbSize
)
215 ReleaseBindInfo(&This
->bind_info
);
216 memset(&This
->bind_info
, 0, sizeof(This
->bind_info
));
220 WARN("Unhandled Internet status callback %d\n", dwInternetStatus
);
224 IInternetProtocolSink_ReportProgress(This
->protocol_sink
, ulStatusCode
, (LPWSTR
)lpvStatusInformation
);
227 static inline LPWSTR
strndupW(LPWSTR string
, int len
)
231 (ret
= HeapAlloc(GetProcessHeap(), 0, (len
+1)*sizeof(WCHAR
))) != NULL
)
233 memcpy(ret
, string
, len
*sizeof(WCHAR
));
240 * Interface implementations
243 #define PROTOCOL(x) ((IInternetProtocol*) &(x)->lpInternetProtocolVtbl)
244 #define PRIORITY(x) ((IInternetPriority*) &(x)->lpInternetPriorityVtbl)
246 #define PROTOCOL_THIS(iface) DEFINE_THIS(HttpProtocol, InternetProtocol, iface)
248 static HRESULT WINAPI
HttpProtocol_QueryInterface(IInternetProtocol
*iface
, REFIID riid
, void **ppv
)
250 HttpProtocol
*This
= PROTOCOL_THIS(iface
);
253 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
254 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
255 *ppv
= PROTOCOL(This
);
256 }else if(IsEqualGUID(&IID_IInternetProtocolRoot
, riid
)) {
257 TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", This
, ppv
);
258 *ppv
= PROTOCOL(This
);
259 }else if(IsEqualGUID(&IID_IInternetProtocol
, riid
)) {
260 TRACE("(%p)->(IID_IInternetProtocol %p)\n", This
, ppv
);
261 *ppv
= PROTOCOL(This
);
262 }else if(IsEqualGUID(&IID_IInternetPriority
, riid
)) {
263 TRACE("(%p)->(IID_IInternetPriority %p)\n", This
, ppv
);
264 *ppv
= PRIORITY(This
);
268 IInternetProtocol_AddRef(iface
);
272 WARN("not supported interface %s\n", debugstr_guid(riid
));
273 return E_NOINTERFACE
;
276 static ULONG WINAPI
HttpProtocol_AddRef(IInternetProtocol
*iface
)
278 HttpProtocol
*This
= PROTOCOL_THIS(iface
);
279 LONG ref
= InterlockedIncrement(&This
->ref
);
280 TRACE("(%p) ref=%d\n", This
, ref
);
284 static ULONG WINAPI
HttpProtocol_Release(IInternetProtocol
*iface
)
286 HttpProtocol
*This
= PROTOCOL_THIS(iface
);
287 LONG ref
= InterlockedDecrement(&This
->ref
);
289 TRACE("(%p) ref=%d\n", This
, ref
);
292 HTTPPROTOCOL_Close(This
);
293 HeapFree(GetProcessHeap(), 0, This
);
295 URLMON_UnlockModule();
301 static HRESULT WINAPI
HttpProtocol_Start(IInternetProtocol
*iface
, LPCWSTR szUrl
,
302 IInternetProtocolSink
*pOIProtSink
, IInternetBindInfo
*pOIBindInfo
,
303 DWORD grfPI
, DWORD dwReserved
)
305 HttpProtocol
*This
= PROTOCOL_THIS(iface
);
307 DWORD len
= 0, request_flags
= INTERNET_FLAG_KEEP_CONNECTION
;
309 IServiceProvider
*service_provider
= 0;
310 IHttpNegotiate2
*http_negotiate2
= 0;
311 LPWSTR host
= 0, path
= 0, user
= 0, pass
= 0, addl_header
= 0,
312 post_cookie
= 0, optional
= 0;
313 BYTE security_id
[512];
314 LPOLESTR user_agent
, accept_mimes
[257];
317 static const WCHAR wszHttp
[] = {'h','t','t','p',':'};
318 static const WCHAR wszBindVerb
[BINDVERB_CUSTOM
][5] =
323 TRACE("(%p)->(%s %p %p %08x %d)\n", This
, debugstr_w(szUrl
), pOIProtSink
,
324 pOIBindInfo
, grfPI
, dwReserved
);
326 IInternetProtocolSink_AddRef(pOIProtSink
);
327 This
->protocol_sink
= pOIProtSink
;
329 memset(&This
->bind_info
, 0, sizeof(This
->bind_info
));
330 This
->bind_info
.cbSize
= sizeof(BINDINFO
);
331 hres
= IInternetBindInfo_GetBindInfo(pOIBindInfo
, &This
->grfBINDF
, &This
->bind_info
);
334 WARN("GetBindInfo failed: %08x\n", hres
);
338 if (lstrlenW(szUrl
) < sizeof(wszHttp
)/sizeof(WCHAR
)
339 || memcmp(szUrl
, wszHttp
, sizeof(wszHttp
)))
345 memset(&url
, 0, sizeof(url
));
346 url
.dwStructSize
= sizeof(url
);
347 url
.dwSchemeLength
= url
.dwHostNameLength
= url
.dwUrlPathLength
= url
.dwUserNameLength
=
348 url
.dwPasswordLength
= 1;
349 if (!InternetCrackUrlW(szUrl
, 0, 0, &url
))
354 host
= strndupW(url
.lpszHostName
, url
.dwHostNameLength
);
355 path
= strndupW(url
.lpszUrlPath
, url
.dwUrlPathLength
);
356 user
= strndupW(url
.lpszUserName
, url
.dwUserNameLength
);
357 pass
= strndupW(url
.lpszPassword
, url
.dwPasswordLength
);
359 url
.nPort
= INTERNET_DEFAULT_HTTP_PORT
;
361 if(!(This
->grfBINDF
& BINDF_FROMURLMON
))
362 IInternetProtocolSink_ReportProgress(This
->protocol_sink
, BINDSTATUS_DIRECTBIND
, NULL
);
364 hres
= IInternetBindInfo_GetBindString(pOIBindInfo
, BINDSTRING_USER_AGENT
, &user_agent
,
366 if (hres
!= S_OK
|| !num
)
369 LPSTR user_agenta
= NULL
;
371 if ((hres
= ObtainUserAgentString(0, &null_char
, &len
)) != E_OUTOFMEMORY
)
373 WARN("ObtainUserAgentString failed: %08x\n", hres
);
375 else if (!(user_agenta
= HeapAlloc(GetProcessHeap(), 0, len
*sizeof(CHAR
))))
377 WARN("Out of memory\n");
379 else if ((hres
= ObtainUserAgentString(0, user_agenta
, &len
)) != S_OK
)
381 WARN("ObtainUserAgentString failed: %08x\n", hres
);
385 if (!(user_agent
= CoTaskMemAlloc((len
)*sizeof(WCHAR
))))
386 WARN("Out of memory\n");
388 MultiByteToWideChar(CP_ACP
, 0, user_agenta
, -1, user_agent
, len
*sizeof(WCHAR
));
390 HeapFree(GetProcessHeap(), 0, user_agenta
);
393 This
->internet
= InternetOpenW(user_agent
, 0, NULL
, NULL
, INTERNET_FLAG_ASYNC
);
396 WARN("InternetOpen failed: %d\n", GetLastError());
397 hres
= INET_E_NO_SESSION
;
401 /* Native does not check for success of next call, so we won't either */
402 InternetSetStatusCallbackW(This
->internet
, HTTPPROTOCOL_InternetStatusCallback
);
404 This
->connect
= InternetConnectW(This
->internet
, host
, url
.nPort
, user
,
405 pass
, INTERNET_SERVICE_HTTP
, 0, (DWORD
)This
);
408 WARN("InternetConnect failed: %d\n", GetLastError());
409 hres
= INET_E_CANNOT_CONNECT
;
413 num
= sizeof(accept_mimes
)/sizeof(accept_mimes
[0])-1;
414 hres
= IInternetBindInfo_GetBindString(pOIBindInfo
, BINDSTRING_ACCEPT_MIMES
,
419 WARN("GetBindString BINDSTRING_ACCEPT_MIMES failed: %08x\n", hres
);
420 hres
= INET_E_NO_VALID_MEDIA
;
423 accept_mimes
[num
] = 0;
425 if (This
->grfBINDF
& BINDF_NOWRITECACHE
)
426 request_flags
|= INTERNET_FLAG_NO_CACHE_WRITE
;
427 This
->request
= HttpOpenRequestW(This
->connect
, This
->bind_info
.dwBindVerb
< BINDVERB_CUSTOM
?
428 wszBindVerb
[This
->bind_info
.dwBindVerb
] :
429 This
->bind_info
.szCustomVerb
,
430 path
, NULL
, NULL
, (LPCWSTR
*)accept_mimes
,
431 request_flags
, (DWORD
)This
);
434 WARN("HttpOpenRequest failed: %d\n", GetLastError());
435 hres
= INET_E_RESOURCE_NOT_FOUND
;
439 hres
= IInternetProtocolSink_QueryInterface(This
->protocol_sink
, &IID_IServiceProvider
,
440 (void **)&service_provider
);
443 WARN("IInternetProtocolSink_QueryInterface IID_IServiceProvider failed: %08x\n", hres
);
447 hres
= IServiceProvider_QueryService(service_provider
, &IID_IHttpNegotiate
,
448 &IID_IHttpNegotiate
, (void **)&This
->http_negotiate
);
451 WARN("IServiceProvider_QueryService IID_IHttpNegotiate failed: %08x\n", hres
);
455 hres
= IHttpNegotiate_BeginningTransaction(This
->http_negotiate
, szUrl
, wszHeaders
,
459 WARN("IHttpNegotiate_BeginningTransaction failed: %08x\n", hres
);
462 else if (addl_header
== NULL
)
464 This
->full_header
= (LPWSTR
)wszHeaders
;
468 int len_addl_header
= lstrlenW(addl_header
);
469 This
->full_header
= HeapAlloc(GetProcessHeap(), 0,
470 len_addl_header
*sizeof(WCHAR
)+sizeof(wszHeaders
));
471 if (!This
->full_header
)
473 WARN("Out of memory\n");
474 hres
= E_OUTOFMEMORY
;
477 lstrcpyW(This
->full_header
, addl_header
);
478 lstrcpyW(&This
->full_header
[len_addl_header
], wszHeaders
);
481 hres
= IServiceProvider_QueryService(service_provider
, &IID_IHttpNegotiate2
,
482 &IID_IHttpNegotiate2
, (void **)&http_negotiate2
);
485 WARN("IServiceProvider_QueryService IID_IHttpNegotiate2 failed: %08x\n", hres
);
486 /* No goto done as per native */
490 len
= sizeof(security_id
)/sizeof(security_id
[0]);
491 hres
= IHttpNegotiate2_GetRootSecurityId(http_negotiate2
, security_id
, &len
, 0);
494 WARN("IHttpNegotiate2_GetRootSecurityId failed: %08x\n", hres
);
495 /* No goto done as per native */
499 /* FIXME: Handle security_id. Native calls undocumented function IsHostInProxyBypassList. */
501 if (This
->bind_info
.dwBindVerb
== BINDVERB_POST
)
504 hres
= IInternetBindInfo_GetBindString(pOIBindInfo
, BINDSTRING_POST_COOKIE
, &post_cookie
,
506 if (hres
== S_OK
&& num
&&
507 !InternetSetOptionW(This
->request
, INTERNET_OPTION_SECONDARY_CACHE_KEY
,
508 post_cookie
, lstrlenW(post_cookie
)))
510 WARN("InternetSetOption INTERNET_OPTION_SECONDARY_CACHE_KEY failed: %d\n",
515 if (This
->bind_info
.dwBindVerb
!= BINDVERB_GET
)
517 /* Native does not use GlobalLock/GlobalUnlock, so we won't either */
518 if (This
->bind_info
.stgmedData
.tymed
!= TYMED_HGLOBAL
)
519 WARN("Expected This->bind_info.stgmedData.tymed to be TYMED_HGLOBAL, not %d\n",
520 This
->bind_info
.stgmedData
.tymed
);
522 optional
= (LPWSTR
)This
->bind_info
.stgmedData
.u
.hGlobal
;
524 if (!HttpSendRequestW(This
->request
, This
->full_header
, lstrlenW(This
->full_header
),
526 optional
? This
->bind_info
.cbstgmedData
: 0) &&
527 GetLastError() != ERROR_IO_PENDING
)
529 WARN("HttpSendRequest failed: %d\n", GetLastError());
530 hres
= INET_E_DOWNLOAD_FAILURE
;
538 IInternetProtocolSink_ReportResult(This
->protocol_sink
, hres
, 0, NULL
);
539 HTTPPROTOCOL_Close(This
);
542 CoTaskMemFree(post_cookie
);
543 CoTaskMemFree(addl_header
);
545 IHttpNegotiate2_Release(http_negotiate2
);
546 if (service_provider
)
547 IServiceProvider_Release(service_provider
);
549 while (num
<sizeof(accept_mimes
)/sizeof(accept_mimes
[0]) &&
551 CoTaskMemFree(accept_mimes
[num
++]);
552 CoTaskMemFree(user_agent
);
554 HeapFree(GetProcessHeap(), 0, pass
);
555 HeapFree(GetProcessHeap(), 0, user
);
556 HeapFree(GetProcessHeap(), 0, path
);
557 HeapFree(GetProcessHeap(), 0, host
);
562 static HRESULT WINAPI
HttpProtocol_Continue(IInternetProtocol
*iface
, PROTOCOLDATA
*pProtocolData
)
564 HttpProtocol
*This
= PROTOCOL_THIS(iface
);
565 DWORD len
= sizeof(DWORD
), status_code
;
566 LPWSTR response_headers
= 0, content_type
= 0, content_length
= 0;
568 static const WCHAR wszDefaultContentType
[] =
569 {'t','e','x','t','/','h','t','m','l',0};
571 TRACE("(%p)->(%p)\n", This
, pProtocolData
);
575 WARN("Expected pProtocolData to be non-NULL\n");
578 else if (!This
->request
)
580 WARN("Expected request to be non-NULL\n");
583 else if (!This
->http_negotiate
)
585 WARN("Expected IHttpNegotiate pointer to be non-NULL\n");
588 else if (!This
->protocol_sink
)
590 WARN("Expected IInternetProtocolSink pointer to be non-NULL\n");
594 if (pProtocolData
->pData
== (LPVOID
)BINDSTATUS_DOWNLOADINGDATA
)
596 if (!HttpQueryInfoW(This
->request
, HTTP_QUERY_STATUS_CODE
| HTTP_QUERY_FLAG_NUMBER
,
597 &status_code
, &len
, NULL
))
599 WARN("HttpQueryInfo failed: %d\n", GetLastError());
604 if ((!HttpQueryInfoW(This
->request
, HTTP_QUERY_RAW_HEADERS_CRLF
, response_headers
, &len
,
606 GetLastError() != ERROR_INSUFFICIENT_BUFFER
) ||
607 !(response_headers
= HeapAlloc(GetProcessHeap(), 0, len
)) ||
608 !HttpQueryInfoW(This
->request
, HTTP_QUERY_RAW_HEADERS_CRLF
, response_headers
, &len
,
611 WARN("HttpQueryInfo failed: %d\n", GetLastError());
615 HRESULT hres
= IHttpNegotiate_OnResponse(This
->http_negotiate
, status_code
,
616 response_headers
, NULL
, NULL
);
619 WARN("IHttpNegotiate_OnResponse failed: %08x\n", hres
);
626 if ((!HttpQueryInfoW(This
->request
, HTTP_QUERY_CONTENT_TYPE
, content_type
, &len
, NULL
) &&
627 GetLastError() != ERROR_INSUFFICIENT_BUFFER
) ||
628 !(content_type
= HeapAlloc(GetProcessHeap(), 0, len
)) ||
629 !HttpQueryInfoW(This
->request
, HTTP_QUERY_CONTENT_TYPE
, content_type
, &len
, NULL
))
631 WARN("HttpQueryInfo failed: %d\n", GetLastError());
632 IInternetProtocolSink_ReportProgress(This
->protocol_sink
,
633 (This
->grfBINDF
& BINDF_FROMURLMON
) ?
634 BINDSTATUS_MIMETYPEAVAILABLE
:
635 BINDSTATUS_RAWMIMETYPE
,
636 wszDefaultContentType
);
640 IInternetProtocolSink_ReportProgress(This
->protocol_sink
,
641 (This
->grfBINDF
& BINDF_FROMURLMON
) ?
642 BINDSTATUS_MIMETYPEAVAILABLE
:
643 BINDSTATUS_RAWMIMETYPE
,
648 if ((!HttpQueryInfoW(This
->request
, HTTP_QUERY_CONTENT_LENGTH
, content_length
, &len
, NULL
) &&
649 GetLastError() != ERROR_INSUFFICIENT_BUFFER
) ||
650 !(content_length
= HeapAlloc(GetProcessHeap(), 0, len
)) ||
651 !HttpQueryInfoW(This
->request
, HTTP_QUERY_CONTENT_LENGTH
, content_length
, &len
, NULL
))
653 WARN("HttpQueryInfo failed: %d\n", GetLastError());
654 This
->content_length
= 0;
658 This
->content_length
= atoiW(content_length
);
661 This
->flags
|= FLAG_FIRST_CONTINUE_COMPLETE
;
664 if (pProtocolData
->pData
>= (LPVOID
)BINDSTATUS_DOWNLOADINGDATA
)
666 /* InternetQueryDataAvailable may immediately fork and perform its asynchronous
667 * read, so clear the flag _before_ calling so it does not incorrectly get cleared
668 * after the status callback is called */
669 This
->flags
&= ~FLAG_REQUEST_COMPLETE
;
670 if (!InternetQueryDataAvailable(This
->request
, &This
->available_bytes
, 0, 0))
672 if (GetLastError() != ERROR_IO_PENDING
)
674 This
->flags
|= FLAG_REQUEST_COMPLETE
;
675 WARN("InternetQueryDataAvailable failed: %d\n", GetLastError());
676 HTTPPROTOCOL_ReportResult(This
, INET_E_DATA_NOT_AVAILABLE
);
681 This
->flags
|= FLAG_REQUEST_COMPLETE
;
682 HTTPPROTOCOL_ReportData(This
);
687 HeapFree(GetProcessHeap(), 0, response_headers
);
688 HeapFree(GetProcessHeap(), 0, content_type
);
689 HeapFree(GetProcessHeap(), 0, content_length
);
691 /* Returns S_OK on native */
695 static HRESULT WINAPI
HttpProtocol_Abort(IInternetProtocol
*iface
, HRESULT hrReason
,
698 HttpProtocol
*This
= PROTOCOL_THIS(iface
);
699 FIXME("(%p)->(%08x %08x)\n", This
, hrReason
, dwOptions
);
703 static HRESULT WINAPI
HttpProtocol_Terminate(IInternetProtocol
*iface
, DWORD dwOptions
)
705 HttpProtocol
*This
= PROTOCOL_THIS(iface
);
707 TRACE("(%p)->(%08x)\n", This
, dwOptions
);
708 HTTPPROTOCOL_Close(This
);
713 static HRESULT WINAPI
HttpProtocol_Suspend(IInternetProtocol
*iface
)
715 HttpProtocol
*This
= PROTOCOL_THIS(iface
);
716 FIXME("(%p)\n", This
);
720 static HRESULT WINAPI
HttpProtocol_Resume(IInternetProtocol
*iface
)
722 HttpProtocol
*This
= PROTOCOL_THIS(iface
);
723 FIXME("(%p)\n", This
);
727 static HRESULT WINAPI
HttpProtocol_Read(IInternetProtocol
*iface
, void *pv
,
728 ULONG cb
, ULONG
*pcbRead
)
730 HttpProtocol
*This
= PROTOCOL_THIS(iface
);
731 ULONG read
= 0, len
= 0;
732 HRESULT hres
= S_FALSE
;
734 TRACE("(%p)->(%p %u %p)\n", This
, pv
, cb
, pcbRead
);
736 if (!(This
->flags
& FLAG_REQUEST_COMPLETE
))
740 else while (!(This
->flags
& FLAG_ALL_DATA_READ
) &&
743 if (This
->available_bytes
== 0)
745 /* InternetQueryDataAvailable may immediately fork and perform its asynchronous
746 * read, so clear the flag _before_ calling so it does not incorrectly get cleared
747 * after the status callback is called */
748 This
->flags
&= ~FLAG_REQUEST_COMPLETE
;
749 if (!InternetQueryDataAvailable(This
->request
, &This
->available_bytes
, 0, 0))
751 if (GetLastError() == ERROR_IO_PENDING
)
757 WARN("InternetQueryDataAvailable failed: %d\n", GetLastError());
758 hres
= INET_E_DATA_NOT_AVAILABLE
;
759 HTTPPROTOCOL_ReportResult(This
, hres
);
763 else if (This
->available_bytes
== 0)
765 HTTPPROTOCOL_AllDataRead(This
);
770 if (!InternetReadFile(This
->request
, ((BYTE
*)pv
)+read
,
771 This
->available_bytes
> cb
-read
?
772 cb
-read
: This
->available_bytes
, &len
))
774 WARN("InternetReadFile failed: %d\n", GetLastError());
775 hres
= INET_E_DOWNLOAD_FAILURE
;
776 HTTPPROTOCOL_ReportResult(This
, hres
);
781 HTTPPROTOCOL_AllDataRead(This
);
786 This
->current_position
+= len
;
787 This
->available_bytes
-= len
;
792 /* Per MSDN this should be if (read == cb), but native returns S_OK
793 * if any bytes were read, so we will too */
801 if (hres
!= E_PENDING
)
802 This
->flags
|= FLAG_REQUEST_COMPLETE
;
807 static HRESULT WINAPI
HttpProtocol_Seek(IInternetProtocol
*iface
, LARGE_INTEGER dlibMove
,
808 DWORD dwOrigin
, ULARGE_INTEGER
*plibNewPosition
)
810 HttpProtocol
*This
= PROTOCOL_THIS(iface
);
811 FIXME("(%p)->(%d %d %p)\n", This
, dlibMove
.u
.LowPart
, dwOrigin
, plibNewPosition
);
815 static HRESULT WINAPI
HttpProtocol_LockRequest(IInternetProtocol
*iface
, DWORD dwOptions
)
817 HttpProtocol
*This
= PROTOCOL_THIS(iface
);
819 TRACE("(%p)->(%08x)\n", This
, dwOptions
);
821 if (!InternetLockRequestFile(This
->request
, &This
->lock
))
822 WARN("InternetLockRequest failed: %d\n", GetLastError());
827 static HRESULT WINAPI
HttpProtocol_UnlockRequest(IInternetProtocol
*iface
)
829 HttpProtocol
*This
= PROTOCOL_THIS(iface
);
831 TRACE("(%p)\n", This
);
835 if (!InternetUnlockRequestFile(This
->lock
))
836 WARN("InternetUnlockRequest failed: %d\n", GetLastError());
845 #define PRIORITY_THIS(iface) DEFINE_THIS(HttpProtocol, InternetPriority, iface)
847 static HRESULT WINAPI
HttpPriority_QueryInterface(IInternetPriority
*iface
, REFIID riid
, void **ppv
)
849 HttpProtocol
*This
= PRIORITY_THIS(iface
);
850 return IInternetProtocol_QueryInterface(PROTOCOL(This
), riid
, ppv
);
853 static ULONG WINAPI
HttpPriority_AddRef(IInternetPriority
*iface
)
855 HttpProtocol
*This
= PRIORITY_THIS(iface
);
856 return IInternetProtocol_AddRef(PROTOCOL(This
));
859 static ULONG WINAPI
HttpPriority_Release(IInternetPriority
*iface
)
861 HttpProtocol
*This
= PRIORITY_THIS(iface
);
862 return IInternetProtocol_Release(PROTOCOL(This
));
865 static HRESULT WINAPI
HttpPriority_SetPriority(IInternetPriority
*iface
, LONG nPriority
)
867 HttpProtocol
*This
= PRIORITY_THIS(iface
);
869 TRACE("(%p)->(%d)\n", This
, nPriority
);
871 This
->priority
= nPriority
;
875 static HRESULT WINAPI
HttpPriority_GetPriority(IInternetPriority
*iface
, LONG
*pnPriority
)
877 HttpProtocol
*This
= PRIORITY_THIS(iface
);
879 TRACE("(%p)->(%p)\n", This
, pnPriority
);
881 *pnPriority
= This
->priority
;
887 static const IInternetPriorityVtbl HttpPriorityVtbl
= {
888 HttpPriority_QueryInterface
,
890 HttpPriority_Release
,
891 HttpPriority_SetPriority
,
892 HttpPriority_GetPriority
895 static const IInternetProtocolVtbl HttpProtocolVtbl
= {
896 HttpProtocol_QueryInterface
,
898 HttpProtocol_Release
,
900 HttpProtocol_Continue
,
902 HttpProtocol_Terminate
,
903 HttpProtocol_Suspend
,
907 HttpProtocol_LockRequest
,
908 HttpProtocol_UnlockRequest
911 HRESULT
HttpProtocol_Construct(IUnknown
*pUnkOuter
, LPVOID
*ppobj
)
915 TRACE("(%p %p)\n", pUnkOuter
, ppobj
);
919 ret
= HeapAlloc(GetProcessHeap(), 0, sizeof(HttpProtocol
));
921 ret
->lpInternetProtocolVtbl
= &HttpProtocolVtbl
;
922 ret
->lpInternetPriorityVtbl
= &HttpPriorityVtbl
;
923 ret
->flags
= ret
->grfBINDF
= 0;
924 memset(&ret
->bind_info
, 0, sizeof(ret
->bind_info
));
925 ret
->protocol_sink
= 0;
926 ret
->http_negotiate
= 0;
927 ret
->internet
= ret
->connect
= ret
->request
= 0;
928 ret
->full_header
= 0;
930 ret
->current_position
= ret
->content_length
= ret
->available_bytes
= 0;
934 *ppobj
= PROTOCOL(ret
);