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
20 #include "urlmon_main.h"
23 #include "wine/debug.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
30 const IInternetProtocolVtbl *lpIInternetProtocolVtbl;
31 const IInternetPriorityVtbl *lpInternetPriorityVtbl;
32 const IWinInetHttpInfoVtbl *lpWinInetHttpInfoVtbl;
35 IHttpNegotiate *http_negotiate;
41 #define PRIORITY(x) ((IInternetPriority*) &(x)->lpInternetPriorityVtbl)
42 #define INETHTTPINFO(x) ((IWinInetHttpInfo*) &(x)->lpWinInetHttpInfoVtbl)
44 /* Default headers from native */
45 static const WCHAR wszHeaders[] = {'A','c','c','e','p','t','-','E','n','c','o','d','i','n','g',
46 ':',' ','g','z','i','p',',',' ','d','e','f','l','a','t','e',0};
48 static LPWSTR query_http_info(HttpProtocol *This, DWORD option)
54 res = HttpQueryInfoW(This->base.request, option, NULL, &len, NULL);
55 if (!res && GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
56 ret = heap_alloc(len);
57 res = HttpQueryInfoW(This->base.request, option, ret, &len, NULL);
60 TRACE("HttpQueryInfoW(%d) failed: %08x\n", option, GetLastError());
68 #define ASYNCPROTOCOL_THIS(iface) DEFINE_THIS2(HttpProtocol, base, iface)
70 static HRESULT HttpProtocol_open_request(Protocol *prot, IUri *uri, DWORD request_flags,
71 HINTERNET internet_session, IInternetBindInfo *bind_info)
73 HttpProtocol *This = ASYNCPROTOCOL_THIS(prot);
74 LPWSTR addl_header = NULL, post_cookie = NULL, optional = NULL;
75 IServiceProvider *service_provider = NULL;
76 IHttpNegotiate2 *http_negotiate2 = NULL;
77 BSTR url, host, user, pass, path;
78 LPOLESTR accept_mimes[257];
79 const WCHAR **accept_types;
80 BYTE security_id[512];
86 static const WCHAR wszBindVerb[BINDVERB_CUSTOM][5] =
91 hres = IUri_GetPort(uri, &port);
95 hres = IUri_GetHost(uri, &host);
99 hres = IUri_GetUserName(uri, &user);
100 if(SUCCEEDED(hres)) {
101 hres = IUri_GetPassword(uri, &pass);
103 if(SUCCEEDED(hres)) {
104 This->base.connection = InternetConnectW(internet_session, host, port, user, pass,
105 INTERNET_SERVICE_HTTP, This->https ? INTERNET_FLAG_SECURE : 0, (DWORD_PTR)&This->base);
113 if(!This->base.connection) {
114 WARN("InternetConnect failed: %d\n", GetLastError());
115 return INET_E_CANNOT_CONNECT;
118 num = sizeof(accept_mimes)/sizeof(accept_mimes[0])-1;
119 hres = IInternetBindInfo_GetBindString(bind_info, BINDSTRING_ACCEPT_MIMES, accept_mimes, num, &num);
120 if(hres == INET_E_USE_DEFAULT_SETTING) {
121 static const WCHAR default_accept_mimeW[] = {'*','/','*',0};
122 static const WCHAR *default_accept_mimes[] = {default_accept_mimeW, NULL};
124 accept_types = default_accept_mimes;
126 }else if(hres == S_OK) {
127 accept_types = (const WCHAR**)accept_mimes;
129 WARN("GetBindString BINDSTRING_ACCEPT_MIMES failed: %08x\n", hres);
130 return INET_E_NO_VALID_MEDIA;
132 accept_mimes[num] = 0;
135 request_flags |= INTERNET_FLAG_SECURE;
137 hres = IUri_GetPathAndQuery(uri, &path);
138 if(SUCCEEDED(hres)) {
139 This->base.request = HttpOpenRequestW(This->base.connection,
140 This->base.bind_info.dwBindVerb < BINDVERB_CUSTOM
141 ? wszBindVerb[This->base.bind_info.dwBindVerb] : This->base.bind_info.szCustomVerb,
142 path, NULL, NULL, accept_types, request_flags, (DWORD_PTR)&This->base);
146 CoTaskMemFree(accept_mimes[num]);
149 if (!This->base.request) {
150 WARN("HttpOpenRequest failed: %d\n", GetLastError());
151 return INET_E_RESOURCE_NOT_FOUND;
154 hres = IInternetProtocolSink_QueryInterface(This->base.protocol_sink, &IID_IServiceProvider,
155 (void **)&service_provider);
157 WARN("IInternetProtocolSink_QueryInterface IID_IServiceProvider failed: %08x\n", hres);
161 hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate,
162 &IID_IHttpNegotiate, (void **)&This->http_negotiate);
164 WARN("IServiceProvider_QueryService IID_IHttpNegotiate failed: %08x\n", hres);
168 hres = IUri_GetAbsoluteUri(uri, &url);
170 IServiceProvider_Release(service_provider);
174 hres = IHttpNegotiate_BeginningTransaction(This->http_negotiate, url, wszHeaders,
178 WARN("IHttpNegotiate_BeginningTransaction failed: %08x\n", hres);
179 IServiceProvider_Release(service_provider);
184 int len_addl_header = strlenW(addl_header);
186 This->full_header = heap_alloc(len_addl_header*sizeof(WCHAR)+sizeof(wszHeaders));
188 lstrcpyW(This->full_header, addl_header);
189 lstrcpyW(&This->full_header[len_addl_header], wszHeaders);
190 CoTaskMemFree(addl_header);
192 This->full_header = (LPWSTR)wszHeaders;
195 hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate2,
196 &IID_IHttpNegotiate2, (void **)&http_negotiate2);
197 IServiceProvider_Release(service_provider);
199 WARN("IServiceProvider_QueryService IID_IHttpNegotiate2 failed: %08x\n", hres);
200 /* No goto done as per native */
202 len = sizeof(security_id)/sizeof(security_id[0]);
203 hres = IHttpNegotiate2_GetRootSecurityId(http_negotiate2, security_id, &len, 0);
204 IHttpNegotiate2_Release(http_negotiate2);
206 WARN("IHttpNegotiate2_GetRootSecurityId failed: %08x\n", hres);
209 /* FIXME: Handle security_id. Native calls undocumented function IsHostInProxyBypassList. */
211 if(This->base.bind_info.dwBindVerb == BINDVERB_POST) {
213 hres = IInternetBindInfo_GetBindString(bind_info, BINDSTRING_POST_COOKIE, &post_cookie, 1, &num);
214 if(hres == S_OK && num) {
215 if(!InternetSetOptionW(This->base.request, INTERNET_OPTION_SECONDARY_CACHE_KEY,
216 post_cookie, lstrlenW(post_cookie)))
217 WARN("InternetSetOption INTERNET_OPTION_SECONDARY_CACHE_KEY failed: %d\n", GetLastError());
218 CoTaskMemFree(post_cookie);
222 if(This->base.bind_info.dwBindVerb != BINDVERB_GET) {
223 /* Native does not use GlobalLock/GlobalUnlock, so we won't either */
224 if (This->base.bind_info.stgmedData.tymed != TYMED_HGLOBAL)
225 WARN("Expected This->base.bind_info.stgmedData.tymed to be TYMED_HGLOBAL, not %d\n",
226 This->base.bind_info.stgmedData.tymed);
228 optional = (LPWSTR)This->base.bind_info.stgmedData.u.hGlobal;
232 res = InternetSetOptionW(This->base.request, INTERNET_OPTION_HTTP_DECODING, &b, sizeof(b));
234 WARN("InternetSetOption(INTERNET_OPTION_HTTP_DECODING) failed: %08x\n", GetLastError());
236 res = HttpSendRequestW(This->base.request, This->full_header, lstrlenW(This->full_header),
237 optional, optional ? This->base.bind_info.cbstgmedData : 0);
238 if(!res && GetLastError() != ERROR_IO_PENDING) {
239 WARN("HttpSendRequest failed: %d\n", GetLastError());
240 return INET_E_DOWNLOAD_FAILURE;
246 static HRESULT HttpProtocol_start_downloading(Protocol *prot)
248 HttpProtocol *This = ASYNCPROTOCOL_THIS(prot);
249 LPWSTR content_type, content_length, ranges;
250 DWORD len = sizeof(DWORD);
255 static const WCHAR wszDefaultContentType[] =
256 {'t','e','x','t','/','h','t','m','l',0};
258 if(!This->http_negotiate) {
259 WARN("Expected IHttpNegotiate pointer to be non-NULL\n");
263 res = HttpQueryInfoW(This->base.request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER,
264 &status_code, &len, NULL);
266 LPWSTR response_headers = query_http_info(This, HTTP_QUERY_RAW_HEADERS_CRLF);
267 if(response_headers) {
268 hres = IHttpNegotiate_OnResponse(This->http_negotiate, status_code, response_headers,
270 heap_free(response_headers);
272 WARN("IHttpNegotiate_OnResponse failed: %08x\n", hres);
277 WARN("HttpQueryInfo failed: %d\n", GetLastError());
280 ranges = query_http_info(This, HTTP_QUERY_ACCEPT_RANGES);
282 IInternetProtocolSink_ReportProgress(This->base.protocol_sink, BINDSTATUS_ACCEPTRANGES, NULL);
286 content_type = query_http_info(This, HTTP_QUERY_CONTENT_TYPE);
288 /* remove the charset, if present */
289 LPWSTR p = strchrW(content_type, ';');
292 IInternetProtocolSink_ReportProgress(This->base.protocol_sink,
293 (This->base.bindf & BINDF_FROMURLMON)
294 ? BINDSTATUS_MIMETYPEAVAILABLE : BINDSTATUS_RAWMIMETYPE,
296 heap_free(content_type);
298 WARN("HttpQueryInfo failed: %d\n", GetLastError());
299 IInternetProtocolSink_ReportProgress(This->base.protocol_sink,
300 (This->base.bindf & BINDF_FROMURLMON)
301 ? BINDSTATUS_MIMETYPEAVAILABLE : BINDSTATUS_RAWMIMETYPE,
302 wszDefaultContentType);
305 content_length = query_http_info(This, HTTP_QUERY_CONTENT_LENGTH);
307 This->base.content_length = atoiW(content_length);
308 heap_free(content_length);
314 static void HttpProtocol_close_connection(Protocol *prot)
316 HttpProtocol *This = ASYNCPROTOCOL_THIS(prot);
318 if(This->http_negotiate) {
319 IHttpNegotiate_Release(This->http_negotiate);
320 This->http_negotiate = 0;
323 if(This->full_header) {
324 if(This->full_header != wszHeaders)
325 heap_free(This->full_header);
326 This->full_header = 0;
330 #undef ASYNCPROTOCOL_THIS
332 static const ProtocolVtbl AsyncProtocolVtbl = {
333 HttpProtocol_open_request,
334 HttpProtocol_start_downloading,
335 HttpProtocol_close_connection
338 #define PROTOCOL_THIS(iface) DEFINE_THIS(HttpProtocol, IInternetProtocol, iface)
340 static HRESULT WINAPI HttpProtocol_QueryInterface(IInternetProtocol *iface, REFIID riid, void **ppv)
342 HttpProtocol *This = PROTOCOL_THIS(iface);
345 if(IsEqualGUID(&IID_IUnknown, riid)) {
346 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
347 *ppv = PROTOCOL(This);
348 }else if(IsEqualGUID(&IID_IInternetProtocolRoot, riid)) {
349 TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", This, ppv);
350 *ppv = PROTOCOL(This);
351 }else if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
352 TRACE("(%p)->(IID_IInternetProtocol %p)\n", This, ppv);
353 *ppv = PROTOCOL(This);
354 }else if(IsEqualGUID(&IID_IInternetPriority, riid)) {
355 TRACE("(%p)->(IID_IInternetPriority %p)\n", This, ppv);
356 *ppv = PRIORITY(This);
357 }else if(IsEqualGUID(&IID_IWinInetInfo, riid)) {
358 TRACE("(%p)->(IID_IWinInetInfo %p)\n", This, ppv);
359 *ppv = INETHTTPINFO(This);
360 }else if(IsEqualGUID(&IID_IWinInetHttpInfo, riid)) {
361 TRACE("(%p)->(IID_IWinInetHttpInfo %p)\n", This, ppv);
362 *ppv = INETHTTPINFO(This);
366 IInternetProtocol_AddRef(iface);
370 WARN("not supported interface %s\n", debugstr_guid(riid));
371 return E_NOINTERFACE;
374 static ULONG WINAPI HttpProtocol_AddRef(IInternetProtocol *iface)
376 HttpProtocol *This = PROTOCOL_THIS(iface);
377 LONG ref = InterlockedIncrement(&This->ref);
378 TRACE("(%p) ref=%d\n", This, ref);
382 static ULONG WINAPI HttpProtocol_Release(IInternetProtocol *iface)
384 HttpProtocol *This = PROTOCOL_THIS(iface);
385 LONG ref = InterlockedDecrement(&This->ref);
387 TRACE("(%p) ref=%d\n", This, ref);
390 protocol_close_connection(&This->base);
393 URLMON_UnlockModule();
399 static HRESULT WINAPI HttpProtocol_Start(IInternetProtocol *iface, LPCWSTR szUrl,
400 IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
401 DWORD grfPI, HANDLE_PTR dwReserved)
403 HttpProtocol *This = PROTOCOL_THIS(iface);
407 static const WCHAR httpW[] = {'h','t','t','p',':'};
408 static const WCHAR httpsW[] = {'h','t','t','p','s',':'};
410 TRACE("(%p)->(%s %p %p %08x %lx)\n", This, debugstr_w(szUrl), pOIProtSink,
411 pOIBindInfo, grfPI, dwReserved);
414 ? strncmpW(szUrl, httpsW, sizeof(httpsW)/sizeof(WCHAR))
415 : strncmpW(szUrl, httpW, sizeof(httpW)/sizeof(WCHAR)))
418 hres = CreateUri(szUrl, 0, 0, &uri);
422 hres = protocol_start(&This->base, PROTOCOL(This), uri, pOIProtSink, pOIBindInfo);
428 static HRESULT WINAPI HttpProtocol_Continue(IInternetProtocol *iface, PROTOCOLDATA *pProtocolData)
430 HttpProtocol *This = PROTOCOL_THIS(iface);
432 TRACE("(%p)->(%p)\n", This, pProtocolData);
434 return protocol_continue(&This->base, pProtocolData);
437 static HRESULT WINAPI HttpProtocol_Abort(IInternetProtocol *iface, HRESULT hrReason,
440 HttpProtocol *This = PROTOCOL_THIS(iface);
441 FIXME("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);
445 static HRESULT WINAPI HttpProtocol_Terminate(IInternetProtocol *iface, DWORD dwOptions)
447 HttpProtocol *This = PROTOCOL_THIS(iface);
449 TRACE("(%p)->(%08x)\n", This, dwOptions);
451 protocol_close_connection(&This->base);
455 static HRESULT WINAPI HttpProtocol_Suspend(IInternetProtocol *iface)
457 HttpProtocol *This = PROTOCOL_THIS(iface);
458 FIXME("(%p)\n", This);
462 static HRESULT WINAPI HttpProtocol_Resume(IInternetProtocol *iface)
464 HttpProtocol *This = PROTOCOL_THIS(iface);
465 FIXME("(%p)\n", This);
469 static HRESULT WINAPI HttpProtocol_Read(IInternetProtocol *iface, void *pv,
470 ULONG cb, ULONG *pcbRead)
472 HttpProtocol *This = PROTOCOL_THIS(iface);
474 TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
476 return protocol_read(&This->base, pv, cb, pcbRead);
479 static HRESULT WINAPI HttpProtocol_Seek(IInternetProtocol *iface, LARGE_INTEGER dlibMove,
480 DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
482 HttpProtocol *This = PROTOCOL_THIS(iface);
483 FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
487 static HRESULT WINAPI HttpProtocol_LockRequest(IInternetProtocol *iface, DWORD dwOptions)
489 HttpProtocol *This = PROTOCOL_THIS(iface);
491 TRACE("(%p)->(%08x)\n", This, dwOptions);
493 return protocol_lock_request(&This->base);
496 static HRESULT WINAPI HttpProtocol_UnlockRequest(IInternetProtocol *iface)
498 HttpProtocol *This = PROTOCOL_THIS(iface);
500 TRACE("(%p)\n", This);
502 return protocol_unlock_request(&This->base);
507 static const IInternetProtocolVtbl HttpProtocolVtbl = {
508 HttpProtocol_QueryInterface,
510 HttpProtocol_Release,
512 HttpProtocol_Continue,
514 HttpProtocol_Terminate,
515 HttpProtocol_Suspend,
519 HttpProtocol_LockRequest,
520 HttpProtocol_UnlockRequest
523 #define PRIORITY_THIS(iface) DEFINE_THIS(HttpProtocol, InternetPriority, iface)
525 static HRESULT WINAPI HttpPriority_QueryInterface(IInternetPriority *iface, REFIID riid, void **ppv)
527 HttpProtocol *This = PRIORITY_THIS(iface);
528 return IInternetProtocol_QueryInterface(PROTOCOL(This), riid, ppv);
531 static ULONG WINAPI HttpPriority_AddRef(IInternetPriority *iface)
533 HttpProtocol *This = PRIORITY_THIS(iface);
534 return IInternetProtocol_AddRef(PROTOCOL(This));
537 static ULONG WINAPI HttpPriority_Release(IInternetPriority *iface)
539 HttpProtocol *This = PRIORITY_THIS(iface);
540 return IInternetProtocol_Release(PROTOCOL(This));
543 static HRESULT WINAPI HttpPriority_SetPriority(IInternetPriority *iface, LONG nPriority)
545 HttpProtocol *This = PRIORITY_THIS(iface);
547 TRACE("(%p)->(%d)\n", This, nPriority);
549 This->base.priority = nPriority;
553 static HRESULT WINAPI HttpPriority_GetPriority(IInternetPriority *iface, LONG *pnPriority)
555 HttpProtocol *This = PRIORITY_THIS(iface);
557 TRACE("(%p)->(%p)\n", This, pnPriority);
559 *pnPriority = This->base.priority;
565 static const IInternetPriorityVtbl HttpPriorityVtbl = {
566 HttpPriority_QueryInterface,
568 HttpPriority_Release,
569 HttpPriority_SetPriority,
570 HttpPriority_GetPriority
573 #define INETINFO_THIS(iface) DEFINE_THIS(HttpProtocol, WinInetHttpInfo, iface)
575 static HRESULT WINAPI HttpInfo_QueryInterface(IWinInetHttpInfo *iface, REFIID riid, void **ppv)
577 HttpProtocol *This = INETINFO_THIS(iface);
578 return IBinding_QueryInterface(PROTOCOL(This), riid, ppv);
581 static ULONG WINAPI HttpInfo_AddRef(IWinInetHttpInfo *iface)
583 HttpProtocol *This = INETINFO_THIS(iface);
584 return IBinding_AddRef(PROTOCOL(This));
587 static ULONG WINAPI HttpInfo_Release(IWinInetHttpInfo *iface)
589 HttpProtocol *This = INETINFO_THIS(iface);
590 return IBinding_Release(PROTOCOL(This));
593 static HRESULT WINAPI HttpInfo_QueryOption(IWinInetHttpInfo *iface, DWORD dwOption,
594 void *pBuffer, DWORD *pcbBuffer)
596 HttpProtocol *This = INETINFO_THIS(iface);
597 FIXME("(%p)->(%x %p %p)\n", This, dwOption, pBuffer, pcbBuffer);
601 static HRESULT WINAPI HttpInfo_QueryInfo(IWinInetHttpInfo *iface, DWORD dwOption,
602 void *pBuffer, DWORD *pcbBuffer, DWORD *pdwFlags, DWORD *pdwReserved)
604 HttpProtocol *This = INETINFO_THIS(iface);
605 FIXME("(%p)->(%x %p %p %p %p)\n", This, dwOption, pBuffer, pcbBuffer, pdwFlags, pdwReserved);
611 static const IWinInetHttpInfoVtbl WinInetHttpInfoVtbl = {
612 HttpInfo_QueryInterface,
615 HttpInfo_QueryOption,
619 static HRESULT create_http_protocol(BOOL https, void **ppobj)
623 ret = heap_alloc_zero(sizeof(HttpProtocol));
625 return E_OUTOFMEMORY;
627 ret->base.vtbl = &AsyncProtocolVtbl;
628 ret->lpIInternetProtocolVtbl = &HttpProtocolVtbl;
629 ret->lpInternetPriorityVtbl = &HttpPriorityVtbl;
630 ret->lpWinInetHttpInfoVtbl = &WinInetHttpInfoVtbl;
635 *ppobj = PROTOCOL(ret);
641 HRESULT HttpProtocol_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
643 TRACE("(%p %p)\n", pUnkOuter, ppobj);
645 return create_http_protocol(FALSE, ppobj);
648 HRESULT HttpSProtocol_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
650 TRACE("(%p %p)\n", pUnkOuter, ppobj);
652 return create_http_protocol(TRUE, ppobj);