2 * Copyright 2007 Misha Koshelev
3 * Copyright 2009 Jacek Caban for CodeWeavers
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
20 #include "urlmon_main.h"
22 #include "wine/debug.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
26 /* Flags are needed for, among other things, return HRESULTs from the Read function
27 * to conform to native. For example, Read returns:
29 * 1. E_PENDING if called before the request has completed,
31 * 2. S_FALSE after all data has been read and S_OK has been reported,
32 * (flags = FLAG_REQUEST_COMPLETE | FLAG_ALL_DATA_READ | FLAG_RESULT_REPORTED)
33 * 3. INET_E_DATA_NOT_AVAILABLE if InternetQueryDataAvailable fails. The first time
34 * this occurs, INET_E_DATA_NOT_AVAILABLE will also be reported to the sink,
35 * (flags = FLAG_REQUEST_COMPLETE)
36 * but upon subsequent calls to Read no reporting will take place, yet
37 * InternetQueryDataAvailable will still be called, and, on failure,
38 * INET_E_DATA_NOT_AVAILABLE will still be returned.
39 * (flags = FLAG_REQUEST_COMPLETE | FLAG_RESULT_REPORTED)
41 * FLAG_FIRST_DATA_REPORTED and FLAG_LAST_DATA_REPORTED are needed for proper
42 * ReportData reporting. For example, if OnResponse returns S_OK, Continue will
43 * report BSCF_FIRSTDATANOTIFICATION, and when all data has been read Read will
44 * report BSCF_INTERMEDIATEDATANOTIFICATION|BSCF_LASTDATANOTIFICATION. However,
45 * if OnResponse does not return S_OK, Continue will not report data, and Read
46 * will report BSCF_FIRSTDATANOTIFICATION|BSCF_LASTDATANOTIFICATION when all
49 #define FLAG_REQUEST_COMPLETE 0x0001
50 #define FLAG_FIRST_CONTINUE_COMPLETE 0x0002
51 #define FLAG_FIRST_DATA_REPORTED 0x0004
52 #define FLAG_ALL_DATA_READ 0x0008
53 #define FLAG_LAST_DATA_REPORTED 0x0010
54 #define FLAG_RESULT_REPORTED 0x0020
56 static inline HRESULT report_progress(Protocol *protocol, ULONG status_code, LPCWSTR status_text)
58 return IInternetProtocolSink_ReportProgress(protocol->protocol_sink, status_code, status_text);
61 static inline HRESULT report_result(Protocol *protocol, HRESULT hres)
63 if (!(protocol->flags & FLAG_RESULT_REPORTED) && protocol->protocol_sink) {
64 protocol->flags |= FLAG_RESULT_REPORTED;
65 IInternetProtocolSink_ReportResult(protocol->protocol_sink, hres, 0, NULL);
71 static void report_data(Protocol *protocol)
75 if((protocol->flags & FLAG_LAST_DATA_REPORTED) || !protocol->protocol_sink)
78 if(protocol->flags & FLAG_FIRST_DATA_REPORTED) {
79 bscf = BSCF_INTERMEDIATEDATANOTIFICATION;
81 protocol->flags |= FLAG_FIRST_DATA_REPORTED;
82 bscf = BSCF_FIRSTDATANOTIFICATION;
85 if(protocol->flags & FLAG_ALL_DATA_READ && !(protocol->flags & FLAG_LAST_DATA_REPORTED)) {
86 protocol->flags |= FLAG_LAST_DATA_REPORTED;
87 bscf |= BSCF_LASTDATANOTIFICATION;
90 IInternetProtocolSink_ReportData(protocol->protocol_sink, bscf,
91 protocol->current_position+protocol->available_bytes,
92 protocol->content_length);
95 static void all_data_read(Protocol *protocol)
97 protocol->flags |= FLAG_ALL_DATA_READ;
99 report_data(protocol);
100 report_result(protocol, S_OK);
103 static void request_complete(Protocol *protocol, INTERNET_ASYNC_RESULT *ar)
107 TRACE("(%p)->(%p)\n", protocol, ar);
110 WARN("request failed: %d\n", ar->dwError);
114 protocol->flags |= FLAG_REQUEST_COMPLETE;
116 if(!protocol->request) {
117 TRACE("setting request handle %p\n", (HINTERNET)ar->dwResult);
118 protocol->request = (HINTERNET)ar->dwResult;
121 /* PROTOCOLDATA same as native */
122 memset(&data, 0, sizeof(data));
123 data.dwState = 0xf1000000;
124 if(protocol->flags & FLAG_FIRST_CONTINUE_COMPLETE)
125 data.pData = (LPVOID)BINDSTATUS_ENDDOWNLOADCOMPONENTS;
127 data.pData = (LPVOID)BINDSTATUS_DOWNLOADINGDATA;
129 if (protocol->bindf & BINDF_FROMURLMON)
130 IInternetProtocolSink_Switch(protocol->protocol_sink, &data);
132 protocol_continue(protocol, &data);
135 static void WINAPI internet_status_callback(HINTERNET internet, DWORD_PTR context,
136 DWORD internet_status, LPVOID status_info, DWORD status_info_len)
138 Protocol *protocol = (Protocol*)context;
140 switch(internet_status) {
141 case INTERNET_STATUS_RESOLVING_NAME:
142 TRACE("%p INTERNET_STATUS_RESOLVING_NAME\n", protocol);
143 report_progress(protocol, BINDSTATUS_FINDINGRESOURCE, (LPWSTR)status_info);
146 case INTERNET_STATUS_CONNECTING_TO_SERVER:
147 TRACE("%p INTERNET_STATUS_CONNECTING_TO_SERVER\n", protocol);
148 report_progress(protocol, BINDSTATUS_CONNECTING, (LPWSTR)status_info);
151 case INTERNET_STATUS_SENDING_REQUEST:
152 TRACE("%p INTERNET_STATUS_SENDING_REQUEST\n", protocol);
153 report_progress(protocol, BINDSTATUS_SENDINGREQUEST, (LPWSTR)status_info);
156 case INTERNET_STATUS_REDIRECT:
157 TRACE("%p INTERNET_STATUS_REDIRECT\n", protocol);
158 report_progress(protocol, BINDSTATUS_REDIRECTING, (LPWSTR)status_info);
161 case INTERNET_STATUS_REQUEST_COMPLETE:
162 request_complete(protocol, status_info);
165 case INTERNET_STATUS_HANDLE_CREATED:
166 TRACE("%p INTERNET_STATUS_HANDLE_CREATED\n", protocol);
167 IInternetProtocol_AddRef(protocol->protocol);
170 case INTERNET_STATUS_HANDLE_CLOSING:
171 TRACE("%p INTERNET_STATUS_HANDLE_CLOSING\n", protocol);
173 if(*(HINTERNET *)status_info == protocol->request) {
174 protocol->request = NULL;
175 if(protocol->protocol_sink) {
176 IInternetProtocolSink_Release(protocol->protocol_sink);
177 protocol->protocol_sink = NULL;
180 if(protocol->bind_info.cbSize) {
181 ReleaseBindInfo(&protocol->bind_info);
182 memset(&protocol->bind_info, 0, sizeof(protocol->bind_info));
184 }else if(*(HINTERNET *)status_info == protocol->connection) {
185 protocol->connection = NULL;
188 IInternetProtocol_Release(protocol->protocol);
192 WARN("Unhandled Internet status callback %d\n", internet_status);
196 static HRESULT write_post_stream(Protocol *protocol)
204 protocol->flags &= ~FLAG_REQUEST_COMPLETE;
208 hres = IStream_Read(protocol->post_stream, buf, sizeof(buf), &size);
209 if(FAILED(hres) || !size)
211 res = InternetWriteFile(protocol->request, buf, size, &written);
213 FIXME("InternetWriteFile failed: %u\n", GetLastError());
219 if(SUCCEEDED(hres)) {
220 IStream_Release(protocol->post_stream);
221 protocol->post_stream = NULL;
223 hres = protocol->vtbl->end_request(protocol);
227 return report_result(protocol, hres);
232 static HINTERNET create_internet_session(IInternetBindInfo *bind_info)
234 LPWSTR global_user_agent = NULL;
235 LPOLESTR user_agent = NULL;
240 hres = IInternetBindInfo_GetBindString(bind_info, BINDSTRING_USER_AGENT, &user_agent, 1, &size);
241 if(hres != S_OK || !size)
242 global_user_agent = get_useragent();
244 ret = InternetOpenW(user_agent ? user_agent : global_user_agent, 0, NULL, NULL, INTERNET_FLAG_ASYNC);
245 heap_free(global_user_agent);
246 CoTaskMemFree(user_agent);
248 WARN("InternetOpen failed: %d\n", GetLastError());
252 InternetSetStatusCallbackW(ret, internet_status_callback);
256 static HINTERNET internet_session;
258 HINTERNET get_internet_session(IInternetBindInfo *bind_info)
260 HINTERNET new_session;
263 return internet_session;
268 new_session = create_internet_session(bind_info);
269 if(new_session && InterlockedCompareExchangePointer((void**)&internet_session, new_session, NULL))
270 InternetCloseHandle(new_session);
272 return internet_session;
275 HRESULT protocol_start(Protocol *protocol, IInternetProtocol *prot, IUri *uri,
276 IInternetProtocolSink *protocol_sink, IInternetBindInfo *bind_info)
281 protocol->protocol = prot;
283 IInternetProtocolSink_AddRef(protocol_sink);
284 protocol->protocol_sink = protocol_sink;
286 memset(&protocol->bind_info, 0, sizeof(protocol->bind_info));
287 protocol->bind_info.cbSize = sizeof(BINDINFO);
288 hres = IInternetBindInfo_GetBindInfo(bind_info, &protocol->bindf, &protocol->bind_info);
290 WARN("GetBindInfo failed: %08x\n", hres);
291 return report_result(protocol, hres);
294 if(!(protocol->bindf & BINDF_FROMURLMON))
295 report_progress(protocol, BINDSTATUS_DIRECTBIND, NULL);
297 if(!get_internet_session(bind_info))
298 return report_result(protocol, INET_E_NO_SESSION);
300 request_flags = INTERNET_FLAG_KEEP_CONNECTION;
301 if(protocol->bindf & BINDF_NOWRITECACHE)
302 request_flags |= INTERNET_FLAG_NO_CACHE_WRITE;
303 if(protocol->bindf & BINDF_NEEDFILE)
304 request_flags |= INTERNET_FLAG_NEED_FILE;
306 hres = protocol->vtbl->open_request(protocol, uri, request_flags, internet_session, bind_info);
308 protocol_close_connection(protocol);
309 return report_result(protocol, hres);
315 HRESULT protocol_continue(Protocol *protocol, PROTOCOLDATA *data)
320 WARN("Expected pProtocolData to be non-NULL\n");
324 if(!protocol->request) {
325 WARN("Expected request to be non-NULL\n");
329 if(!protocol->protocol_sink) {
330 WARN("Expected IInternetProtocolSink pointer to be non-NULL\n");
334 if(protocol->post_stream)
335 return write_post_stream(protocol);
337 if(data->pData == (LPVOID)BINDSTATUS_DOWNLOADINGDATA) {
338 hres = protocol->vtbl->start_downloading(protocol);
340 protocol_close_connection(protocol);
341 report_result(protocol, hres);
345 if(protocol->bindf & BINDF_NEEDFILE) {
346 WCHAR cache_file[MAX_PATH];
347 DWORD buflen = sizeof(cache_file);
349 if(InternetQueryOptionW(protocol->request, INTERNET_OPTION_DATAFILE_NAME,
350 cache_file, &buflen)) {
351 report_progress(protocol, BINDSTATUS_CACHEFILENAMEAVAILABLE, cache_file);
353 FIXME("Could not get cache file\n");
357 protocol->flags |= FLAG_FIRST_CONTINUE_COMPLETE;
360 if(data->pData >= (LPVOID)BINDSTATUS_DOWNLOADINGDATA) {
363 /* InternetQueryDataAvailable may immediately fork and perform its asynchronous
364 * read, so clear the flag _before_ calling so it does not incorrectly get cleared
365 * after the status callback is called */
366 protocol->flags &= ~FLAG_REQUEST_COMPLETE;
367 res = InternetQueryDataAvailable(protocol->request, &protocol->available_bytes, 0, 0);
369 protocol->flags |= FLAG_REQUEST_COMPLETE;
370 report_data(protocol);
371 }else if(GetLastError() != ERROR_IO_PENDING) {
372 protocol->flags |= FLAG_REQUEST_COMPLETE;
373 WARN("InternetQueryDataAvailable failed: %d\n", GetLastError());
374 report_result(protocol, INET_E_DATA_NOT_AVAILABLE);
381 HRESULT protocol_read(Protocol *protocol, void *buf, ULONG size, ULONG *read_ret)
385 HRESULT hres = S_FALSE;
387 if(protocol->flags & FLAG_ALL_DATA_READ) {
392 if(!(protocol->flags & FLAG_REQUEST_COMPLETE)) {
398 if(protocol->available_bytes) {
401 res = InternetReadFile(protocol->request, ((BYTE *)buf)+read,
402 protocol->available_bytes > size-read ? size-read : protocol->available_bytes, &len);
404 WARN("InternetReadFile failed: %d\n", GetLastError());
405 hres = INET_E_DOWNLOAD_FAILURE;
406 report_result(protocol, hres);
411 all_data_read(protocol);
416 protocol->current_position += len;
417 protocol->available_bytes -= len;
419 /* InternetQueryDataAvailable may immediately fork and perform its asynchronous
420 * read, so clear the flag _before_ calling so it does not incorrectly get cleared
421 * after the status callback is called */
422 protocol->flags &= ~FLAG_REQUEST_COMPLETE;
423 res = InternetQueryDataAvailable(protocol->request, &protocol->available_bytes, 0, 0);
425 if (GetLastError() == ERROR_IO_PENDING) {
428 WARN("InternetQueryDataAvailable failed: %d\n", GetLastError());
429 hres = INET_E_DATA_NOT_AVAILABLE;
430 report_result(protocol, hres);
435 if(!protocol->available_bytes) {
436 all_data_read(protocol);
444 if (hres != E_PENDING)
445 protocol->flags |= FLAG_REQUEST_COMPLETE;
449 return read ? S_OK : S_FALSE;
452 HRESULT protocol_lock_request(Protocol *protocol)
454 if (!InternetLockRequestFile(protocol->request, &protocol->lock))
455 WARN("InternetLockRequest failed: %d\n", GetLastError());
460 HRESULT protocol_unlock_request(Protocol *protocol)
465 if(!InternetUnlockRequestFile(protocol->lock))
466 WARN("InternetUnlockRequest failed: %d\n", GetLastError());
472 HRESULT protocol_abort(Protocol *protocol, HRESULT reason)
474 if(!protocol->protocol_sink)
477 if(protocol->flags & FLAG_RESULT_REPORTED)
478 return INET_E_RESULT_DISPATCHED;
480 report_result(protocol, reason);
484 void protocol_close_connection(Protocol *protocol)
486 protocol->vtbl->close_connection(protocol);
488 if(protocol->request)
489 InternetCloseHandle(protocol->request);
491 if(protocol->connection)
492 InternetCloseHandle(protocol->connection);
494 if(protocol->post_stream) {
495 IStream_Release(protocol->post_stream);
496 protocol->post_stream = NULL;