2 * Copyright 2005-2006 Jacek Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include "urlmon_main.h"
22 #include "wine/debug.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(urlmon
);
43 static struct list name_space_list
= LIST_INIT(name_space_list
);
44 static struct list mime_filter_list
= LIST_INIT(mime_filter_list
);
46 static CRITICAL_SECTION session_cs
;
47 static CRITICAL_SECTION_DEBUG session_cs_dbg
=
50 { &session_cs_dbg
.ProcessLocksList
, &session_cs_dbg
.ProcessLocksList
},
51 0, 0, { (DWORD_PTR
)(__FILE__
": session") }
53 static CRITICAL_SECTION session_cs
= { &session_cs_dbg
, -1, 0, 0, 0, 0 };
55 static const WCHAR internet_settings_keyW
[] =
56 {'S','O','F','T','W','A','R','E',
57 '\\','M','i','c','r','o','s','o','f','t',
58 '\\','W','i','n','d','o','w','s',
59 '\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n',
60 '\\','I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s',0};
62 static name_space
*find_name_space(LPCWSTR protocol
)
66 LIST_FOR_EACH_ENTRY(iter
, &name_space_list
, name_space
, entry
) {
67 if(!wcsicmp(iter
->protocol
, protocol
))
74 static HRESULT
get_protocol_cf(LPCWSTR schema
, DWORD schema_len
, CLSID
*pclsid
, IClassFactory
**ret
)
78 DWORD res
, type
, size
;
83 static const WCHAR wszProtocolsKey
[] =
84 {'P','R','O','T','O','C','O','L','S','\\','H','a','n','d','l','e','r','\\'};
85 static const WCHAR wszCLSID
[] = {'C','L','S','I','D',0};
87 wszKey
= heap_alloc(sizeof(wszProtocolsKey
)+(schema_len
+1)*sizeof(WCHAR
));
88 memcpy(wszKey
, wszProtocolsKey
, sizeof(wszProtocolsKey
));
89 memcpy(wszKey
+ ARRAY_SIZE(wszProtocolsKey
), schema
, (schema_len
+1)*sizeof(WCHAR
));
91 res
= RegOpenKeyW(HKEY_CLASSES_ROOT
, wszKey
, &hkey
);
93 if(res
!= ERROR_SUCCESS
) {
94 TRACE("Could not open protocol handler key\n");
98 size
= sizeof(str_clsid
);
99 res
= RegQueryValueExW(hkey
, wszCLSID
, NULL
, &type
, (LPBYTE
)str_clsid
, &size
);
101 if(res
!= ERROR_SUCCESS
|| type
!= REG_SZ
) {
102 WARN("Could not get protocol CLSID res=%d\n", res
);
106 hres
= CLSIDFromString(str_clsid
, &clsid
);
108 WARN("CLSIDFromString failed: %08x\n", hres
);
118 hres
= CoGetClassObject(&clsid
, CLSCTX_INPROC_SERVER
, NULL
, &IID_IClassFactory
, (void**)ret
);
119 return SUCCEEDED(hres
) ? S_OK
: MK_E_SYNTAX
;
122 HRESULT
register_namespace(IClassFactory
*cf
, REFIID clsid
, LPCWSTR protocol
, BOOL urlmon_protocol
)
124 name_space
*new_name_space
;
126 new_name_space
= heap_alloc(sizeof(name_space
));
129 IClassFactory_AddRef(cf
);
130 new_name_space
->cf
= cf
;
131 new_name_space
->clsid
= *clsid
;
132 new_name_space
->urlmon
= urlmon_protocol
;
133 new_name_space
->protocol
= heap_strdupW(protocol
);
135 EnterCriticalSection(&session_cs
);
137 list_add_head(&name_space_list
, &new_name_space
->entry
);
139 LeaveCriticalSection(&session_cs
);
144 static HRESULT
unregister_namespace(IClassFactory
*cf
, LPCWSTR protocol
)
148 EnterCriticalSection(&session_cs
);
150 LIST_FOR_EACH_ENTRY(iter
, &name_space_list
, name_space
, entry
) {
151 if(iter
->cf
== cf
&& !wcsicmp(iter
->protocol
, protocol
)) {
152 list_remove(&iter
->entry
);
154 LeaveCriticalSection(&session_cs
);
157 IClassFactory_Release(iter
->cf
);
158 heap_free(iter
->protocol
);
164 LeaveCriticalSection(&session_cs
);
168 BOOL
is_registered_protocol(LPCWSTR url
)
174 hres
= CoInternetParseUrl(url
, PARSE_SCHEMA
, 0, schema
, ARRAY_SIZE(schema
), &schema_len
, 0);
178 return get_protocol_cf(schema
, schema_len
, NULL
, NULL
) == S_OK
;
181 IInternetProtocolInfo
*get_protocol_info(LPCWSTR url
)
183 IInternetProtocolInfo
*ret
= NULL
;
190 hres
= CoInternetParseUrl(url
, PARSE_SCHEMA
, 0, schema
, ARRAY_SIZE(schema
), &schema_len
, 0);
191 if(FAILED(hres
) || !schema_len
)
194 EnterCriticalSection(&session_cs
);
196 ns
= find_name_space(schema
);
197 if(ns
&& !ns
->urlmon
) {
198 hres
= IClassFactory_QueryInterface(ns
->cf
, &IID_IInternetProtocolInfo
, (void**)&ret
);
200 hres
= IClassFactory_CreateInstance(ns
->cf
, NULL
, &IID_IInternetProtocolInfo
, (void**)&ret
);
203 LeaveCriticalSection(&session_cs
);
205 if(ns
&& SUCCEEDED(hres
))
208 hres
= get_protocol_cf(schema
, schema_len
, NULL
, &cf
);
212 hres
= IClassFactory_QueryInterface(cf
, &IID_IInternetProtocolInfo
, (void**)&ret
);
214 IClassFactory_CreateInstance(cf
, NULL
, &IID_IInternetProtocolInfo
, (void**)&ret
);
215 IClassFactory_Release(cf
);
220 HRESULT
get_protocol_handler(IUri
*uri
, CLSID
*clsid
, IClassFactory
**ret
)
228 /* FIXME: Avoid GetSchemeName call for known schemes */
229 hres
= IUri_GetSchemeName(uri
, &scheme
);
233 EnterCriticalSection(&session_cs
);
235 ns
= find_name_space(scheme
);
238 IClassFactory_AddRef(*ret
);
243 LeaveCriticalSection(&session_cs
);
245 hres
= *ret
? S_OK
: get_protocol_cf(scheme
, SysStringLen(scheme
), clsid
, ret
);
246 SysFreeString(scheme
);
250 IInternetProtocol
*get_mime_filter(LPCWSTR mime
)
252 static const WCHAR filtersW
[] = {'P','r','o','t','o','c','o','l','s',
253 '\\','F','i','l','t','e','r',0 };
254 static const WCHAR CLSIDW
[] = {'C','L','S','I','D',0};
256 IClassFactory
*cf
= NULL
;
257 IInternetProtocol
*ret
;
262 DWORD res
, type
, size
;
265 EnterCriticalSection(&session_cs
);
267 LIST_FOR_EACH_ENTRY(iter
, &mime_filter_list
, mime_filter
, entry
) {
268 if(!wcscmp(iter
->mime
, mime
)) {
274 LeaveCriticalSection(&session_cs
);
277 hres
= IClassFactory_CreateInstance(cf
, NULL
, &IID_IInternetProtocol
, (void**)&ret
);
279 WARN("CreateInstance failed: %08x\n", hres
);
286 res
= RegOpenKeyW(HKEY_CLASSES_ROOT
, filtersW
, &hlist
);
287 if(res
!= ERROR_SUCCESS
) {
288 TRACE("Could not open MIME filters key\n");
292 res
= RegOpenKeyW(hlist
, mime
, &hfilter
);
294 if(res
!= ERROR_SUCCESS
)
297 size
= sizeof(clsidw
);
298 res
= RegQueryValueExW(hfilter
, CLSIDW
, NULL
, &type
, (LPBYTE
)clsidw
, &size
);
299 CloseHandle(hfilter
);
300 if(res
!=ERROR_SUCCESS
|| type
!=REG_SZ
) {
301 WARN("Could not get filter CLSID for %s\n", debugstr_w(mime
));
305 hres
= CLSIDFromString(clsidw
, &clsid
);
307 WARN("CLSIDFromString failed for %s (%x)\n", debugstr_w(mime
), hres
);
311 hres
= CoCreateInstance(&clsid
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IInternetProtocol
, (void**)&ret
);
313 WARN("CoCreateInstance failed: %08x\n", hres
);
320 static HRESULT WINAPI
InternetSession_QueryInterface(IInternetSession
*iface
,
321 REFIID riid
, void **ppv
)
323 TRACE("(%s %p)\n", debugstr_guid(riid
), ppv
);
325 if(IsEqualGUID(&IID_IUnknown
, riid
) || IsEqualGUID(&IID_IInternetSession
, riid
)) {
327 IInternetSession_AddRef(iface
);
332 return E_NOINTERFACE
;
335 static ULONG WINAPI
InternetSession_AddRef(IInternetSession
*iface
)
342 static ULONG WINAPI
InternetSession_Release(IInternetSession
*iface
)
345 URLMON_UnlockModule();
349 static HRESULT WINAPI
InternetSession_RegisterNameSpace(IInternetSession
*iface
,
350 IClassFactory
*pCF
, REFCLSID rclsid
, LPCWSTR pwzProtocol
, ULONG cPatterns
,
351 const LPCWSTR
*ppwzPatterns
, DWORD dwReserved
)
353 TRACE("(%p %s %s %d %p %d)\n", pCF
, debugstr_guid(rclsid
), debugstr_w(pwzProtocol
),
354 cPatterns
, ppwzPatterns
, dwReserved
);
356 if(cPatterns
|| ppwzPatterns
)
357 FIXME("patterns not supported\n");
359 WARN("dwReserved = %d\n", dwReserved
);
361 if(!pCF
|| !pwzProtocol
)
364 return register_namespace(pCF
, rclsid
, pwzProtocol
, FALSE
);
367 static HRESULT WINAPI
InternetSession_UnregisterNameSpace(IInternetSession
*iface
,
368 IClassFactory
*pCF
, LPCWSTR pszProtocol
)
370 TRACE("(%p %s)\n", pCF
, debugstr_w(pszProtocol
));
372 if(!pCF
|| !pszProtocol
)
375 return unregister_namespace(pCF
, pszProtocol
);
378 static HRESULT WINAPI
InternetSession_RegisterMimeFilter(IInternetSession
*iface
,
379 IClassFactory
*pCF
, REFCLSID rclsid
, LPCWSTR pwzType
)
383 TRACE("(%p %s %s)\n", pCF
, debugstr_guid(rclsid
), debugstr_w(pwzType
));
385 filter
= heap_alloc(sizeof(mime_filter
));
387 IClassFactory_AddRef(pCF
);
389 filter
->clsid
= *rclsid
;
390 filter
->mime
= heap_strdupW(pwzType
);
392 EnterCriticalSection(&session_cs
);
394 list_add_head(&mime_filter_list
, &filter
->entry
);
396 LeaveCriticalSection(&session_cs
);
401 static HRESULT WINAPI
InternetSession_UnregisterMimeFilter(IInternetSession
*iface
,
402 IClassFactory
*pCF
, LPCWSTR pwzType
)
406 TRACE("(%p %s)\n", pCF
, debugstr_w(pwzType
));
408 EnterCriticalSection(&session_cs
);
410 LIST_FOR_EACH_ENTRY(iter
, &mime_filter_list
, mime_filter
, entry
) {
411 if(iter
->cf
== pCF
&& !wcscmp(iter
->mime
, pwzType
)) {
412 list_remove(&iter
->entry
);
414 LeaveCriticalSection(&session_cs
);
416 IClassFactory_Release(iter
->cf
);
417 heap_free(iter
->mime
);
423 LeaveCriticalSection(&session_cs
);
427 static HRESULT WINAPI
InternetSession_CreateBinding(IInternetSession
*iface
,
428 LPBC pBC
, LPCWSTR szUrl
, IUnknown
*pUnkOuter
, IUnknown
**ppUnk
,
429 IInternetProtocol
**ppOInetProt
, DWORD dwOption
)
431 BindProtocol
*protocol
;
434 TRACE("(%p %s %p %p %p %08x)\n", pBC
, debugstr_w(szUrl
), pUnkOuter
, ppUnk
,
435 ppOInetProt
, dwOption
);
437 if(pBC
|| pUnkOuter
|| ppUnk
|| dwOption
)
438 FIXME("Unsupported arguments\n");
440 hres
= create_binding_protocol(&protocol
);
444 *ppOInetProt
= (IInternetProtocol
*)&protocol
->IInternetProtocolEx_iface
;
448 static HRESULT WINAPI
InternetSession_SetSessionOption(IInternetSession
*iface
,
449 DWORD dwOption
, LPVOID pBuffer
, DWORD dwBufferLength
, DWORD dwReserved
)
451 FIXME("(%08x %p %d %d)\n", dwOption
, pBuffer
, dwBufferLength
, dwReserved
);
455 static const IInternetSessionVtbl InternetSessionVtbl
= {
456 InternetSession_QueryInterface
,
457 InternetSession_AddRef
,
458 InternetSession_Release
,
459 InternetSession_RegisterNameSpace
,
460 InternetSession_UnregisterNameSpace
,
461 InternetSession_RegisterMimeFilter
,
462 InternetSession_UnregisterMimeFilter
,
463 InternetSession_CreateBinding
,
464 InternetSession_SetSessionOption
467 static IInternetSession InternetSession
= { &InternetSessionVtbl
};
469 /***********************************************************************
470 * CoInternetGetSession (URLMON.@)
472 * Create a new internet session and return an IInternetSession interface
476 * dwSessionMode [I] Mode for the internet session
477 * ppIInternetSession [O] Destination for creates IInternetSession object
478 * dwReserved [I] Reserved, must be 0.
481 * Success: S_OK. ppIInternetSession contains the IInternetSession interface.
482 * Failure: E_INVALIDARG, if any argument is invalid, or
483 * E_OUTOFMEMORY if memory allocation fails.
485 HRESULT WINAPI
CoInternetGetSession(DWORD dwSessionMode
, IInternetSession
**ppIInternetSession
,
488 TRACE("(%d %p %d)\n", dwSessionMode
, ppIInternetSession
, dwReserved
);
491 ERR("dwSessionMode=%d\n", dwSessionMode
);
493 ERR("dwReserved=%d\n", dwReserved
);
495 IInternetSession_AddRef(&InternetSession
);
496 *ppIInternetSession
= &InternetSession
;
500 /**************************************************************************
501 * UrlMkGetSessionOption (URLMON.@)
503 static BOOL
get_url_encoding(HKEY root
, DWORD
*encoding
)
505 DWORD size
= sizeof(DWORD
), res
, type
;
508 static const WCHAR wszUrlEncoding
[] = {'U','r','l','E','n','c','o','d','i','n','g',0};
510 res
= RegOpenKeyW(root
, internet_settings_keyW
, &hkey
);
511 if(res
!= ERROR_SUCCESS
)
514 res
= RegQueryValueExW(hkey
, wszUrlEncoding
, NULL
, &type
, (LPBYTE
)encoding
, &size
);
517 return res
== ERROR_SUCCESS
;
520 static LPWSTR user_agent
;
522 static void ensure_useragent(void)
524 OSVERSIONINFOW info
= {sizeof(info
)};
525 const WCHAR
*os_type
, *is_nt
;
526 WCHAR buf
[512], *ret
, *tmp
;
532 static const WCHAR formatW
[] =
533 {'M','o','z','i','l','l','a','/','4','.','0',
534 ' ','(','c','o','m','p','a','t','i','b','l','e',';',
535 ' ','M','S','I','E',' ','8','.','0',';',
536 ' ','W','i','n','d','o','w','s',' ','%','s','%','d','.','%','d',';',
537 ' ','%','s','T','r','i','d','e','n','t','/','5','.','0',0};
538 static const WCHAR post_platform_keyW
[] =
539 {'S','O','F','T','W','A','R','E',
540 '\\','M','i','c','r','o','s','o','f','t',
541 '\\','W','i','n','d','o','w','s',
542 '\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n',
543 '\\','I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s',
544 '\\','5','.','0','\\','U','s','e','r',' ','A','g','e','n','t',
545 '\\','P','o','s','t',' ','P','l','a','t','f','o','r','m',0};
546 static const WCHAR ntW
[] = {'N','T',' ',0};
547 static const WCHAR win64W
[] = {'W','i','n','6','4',';',' ','x','6','4',';',' ',0};
548 static const WCHAR wow64W
[] = {'W','O','W','6','4',';',' ',0};
549 static const WCHAR emptyW
[] = {0};
554 GetVersionExW(&info
);
555 is_nt
= info
.dwPlatformId
== VER_PLATFORM_WIN32_NT
? ntW
: emptyW
;
557 if(sizeof(void*) == 8)
559 else if(IsWow64Process(GetCurrentProcess(), &is_wow
) && is_wow
)
564 swprintf(buf
, ARRAY_SIZE(buf
), formatW
, is_nt
, info
.dwMajorVersion
, info
.dwMinorVersion
, os_type
);
568 ret
= heap_alloc(size
* sizeof(WCHAR
));
572 memcpy(ret
, buf
, len
*sizeof(WCHAR
));
574 res
= RegOpenKeyW(HKEY_LOCAL_MACHINE
, post_platform_keyW
, &key
);
575 if(res
== ERROR_SUCCESS
) {
579 value_len
= ARRAY_SIZE(buf
);
580 res
= RegEnumValueW(key
, idx
, buf
, &value_len
, NULL
, NULL
, NULL
, NULL
);
581 if(res
!= ERROR_SUCCESS
)
585 if(len
+ value_len
+ 2 /* strlen("; ") */ + 1 /* trailing ')' */ >= size
) {
586 tmp
= heap_realloc(ret
, (size
*2+value_len
)*sizeof(WCHAR
));
590 size
= size
*2+value_len
;
595 memcpy(ret
+len
, buf
, value_len
*sizeof(WCHAR
));
606 TRACE("Using user agent %s\n", debugstr_w(user_agent
));
609 LPWSTR
get_useragent(void)
615 EnterCriticalSection(&session_cs
);
616 ret
= heap_strdupW(user_agent
);
617 LeaveCriticalSection(&session_cs
);
622 HRESULT WINAPI
UrlMkGetSessionOption(DWORD dwOption
, LPVOID pBuffer
, DWORD dwBufferLength
,
623 DWORD
* pdwBufferLength
, DWORD dwReserved
)
625 TRACE("(%x, %p, %d, %p)\n", dwOption
, pBuffer
, dwBufferLength
, pdwBufferLength
);
628 WARN("dwReserved = %d\n", dwReserved
);
631 case URLMON_OPTION_USERAGENT
: {
632 HRESULT hres
= E_OUTOFMEMORY
;
638 EnterCriticalSection(&session_cs
);
642 size
= WideCharToMultiByte(CP_ACP
, 0, user_agent
, -1, NULL
, 0, NULL
, NULL
);
643 *pdwBufferLength
= size
;
644 if(size
<= dwBufferLength
) {
646 WideCharToMultiByte(CP_ACP
, 0, user_agent
, -1, pBuffer
, size
, NULL
, NULL
);
652 LeaveCriticalSection(&session_cs
);
654 /* Tests prove that we have to return E_OUTOFMEMORY on success. */
657 case URLMON_OPTION_URL_ENCODING
: {
660 if(!pBuffer
|| dwBufferLength
< sizeof(DWORD
) || !pdwBufferLength
)
663 if(!get_url_encoding(HKEY_CURRENT_USER
, &encoding
))
664 get_url_encoding(HKEY_LOCAL_MACHINE
, &encoding
);
666 *pdwBufferLength
= sizeof(DWORD
);
667 *(DWORD
*)pBuffer
= encoding
? URL_ENCODING_DISABLE_UTF8
: URL_ENCODING_ENABLE_UTF8
;
671 FIXME("unsupported option %x\n", dwOption
);
677 /**************************************************************************
678 * UrlMkSetSessionOption (URLMON.@)
680 HRESULT WINAPI
UrlMkSetSessionOption(DWORD dwOption
, LPVOID pBuffer
, DWORD dwBufferLength
,
683 TRACE("(%x %p %x)\n", dwOption
, pBuffer
, dwBufferLength
);
686 case URLMON_OPTION_USERAGENT
: {
687 LPWSTR new_user_agent
;
691 if(!pBuffer
|| !dwBufferLength
)
694 for(len
=0; len
<dwBufferLength
&& buf
[len
]; len
++);
696 TRACE("Setting user agent %s\n", debugstr_an(buf
, len
));
698 size
= MultiByteToWideChar(CP_ACP
, 0, buf
, len
, NULL
, 0);
699 new_user_agent
= heap_alloc((size
+1)*sizeof(WCHAR
));
701 return E_OUTOFMEMORY
;
702 MultiByteToWideChar(CP_ACP
, 0, buf
, len
, new_user_agent
, size
);
703 new_user_agent
[size
] = 0;
705 EnterCriticalSection(&session_cs
);
707 heap_free(user_agent
);
708 user_agent
= new_user_agent
;
709 update_user_agent(user_agent
);
711 LeaveCriticalSection(&session_cs
);
715 FIXME("Unknown option %x\n", dwOption
);
722 /**************************************************************************
723 * ObtainUserAgentString (URLMON.@)
725 HRESULT WINAPI
ObtainUserAgentString(DWORD dwOption
, LPSTR pcszUAOut
, DWORD
*cbSize
)
728 HRESULT hres
= E_FAIL
;
730 TRACE("(%d %p %p)\n", dwOption
, pcszUAOut
, cbSize
);
732 if(!pcszUAOut
|| !cbSize
)
735 EnterCriticalSection(&session_cs
);
739 size
= WideCharToMultiByte(CP_ACP
, 0, user_agent
, -1, NULL
, 0, NULL
, NULL
);
741 if(size
<= *cbSize
) {
742 WideCharToMultiByte(CP_ACP
, 0, user_agent
, -1, pcszUAOut
, *cbSize
, NULL
, NULL
);
745 hres
= E_OUTOFMEMORY
;
751 LeaveCriticalSection(&session_cs
);
755 void free_session(void)
757 name_space
*ns_iter
, *ns_last
;
758 mime_filter
*mf_iter
, *mf_last
;
760 LIST_FOR_EACH_ENTRY_SAFE(ns_iter
, ns_last
, &name_space_list
, name_space
, entry
) {
762 IClassFactory_Release(ns_iter
->cf
);
763 heap_free(ns_iter
->protocol
);
767 LIST_FOR_EACH_ENTRY_SAFE(mf_iter
, mf_last
, &mime_filter_list
, mime_filter
, entry
) {
768 IClassFactory_Release(mf_iter
->cf
);
769 heap_free(mf_iter
->mime
);
773 heap_free(user_agent
);