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(!strcmpiW(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
+ sizeof(wszProtocolsKey
)/sizeof(WCHAR
), 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
&& !strcmpiW(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
, sizeof(schema
)/sizeof(schema
[0]),
179 return get_protocol_cf(schema
, schema_len
, NULL
, NULL
) == S_OK
;
182 IInternetProtocolInfo
*get_protocol_info(LPCWSTR url
)
184 IInternetProtocolInfo
*ret
= NULL
;
191 hres
= CoInternetParseUrl(url
, PARSE_SCHEMA
, 0, schema
, sizeof(schema
)/sizeof(schema
[0]),
193 if(FAILED(hres
) || !schema_len
)
196 EnterCriticalSection(&session_cs
);
198 ns
= find_name_space(schema
);
199 if(ns
&& !ns
->urlmon
) {
200 hres
= IClassFactory_QueryInterface(ns
->cf
, &IID_IInternetProtocolInfo
, (void**)&ret
);
202 hres
= IClassFactory_CreateInstance(ns
->cf
, NULL
, &IID_IInternetProtocolInfo
, (void**)&ret
);
205 LeaveCriticalSection(&session_cs
);
207 if(ns
&& SUCCEEDED(hres
))
210 hres
= get_protocol_cf(schema
, schema_len
, NULL
, &cf
);
214 hres
= IClassFactory_QueryInterface(cf
, &IID_IInternetProtocolInfo
, (void**)&ret
);
216 IClassFactory_CreateInstance(cf
, NULL
, &IID_IInternetProtocolInfo
, (void**)&ret
);
217 IClassFactory_Release(cf
);
222 HRESULT
get_protocol_handler(IUri
*uri
, CLSID
*clsid
, BOOL
*urlmon_protocol
, IClassFactory
**ret
)
230 /* FIXME: Avoid GetSchemeName call for known schemes */
231 hres
= IUri_GetSchemeName(uri
, &scheme
);
235 EnterCriticalSection(&session_cs
);
237 ns
= find_name_space(scheme
);
240 IClassFactory_AddRef(*ret
);
244 *urlmon_protocol
= ns
->urlmon
;
247 LeaveCriticalSection(&session_cs
);
253 *urlmon_protocol
= FALSE
;
254 hres
= get_protocol_cf(scheme
, SysStringLen(scheme
), clsid
, ret
);
257 SysFreeString(scheme
);
261 IInternetProtocol
*get_mime_filter(LPCWSTR mime
)
263 static const WCHAR filtersW
[] = {'P','r','o','t','o','c','o','l','s',
264 '\\','F','i','l','t','e','r',0 };
265 static const WCHAR CLSIDW
[] = {'C','L','S','I','D',0};
267 IClassFactory
*cf
= NULL
;
268 IInternetProtocol
*ret
;
273 DWORD res
, type
, size
;
276 EnterCriticalSection(&session_cs
);
278 LIST_FOR_EACH_ENTRY(iter
, &mime_filter_list
, mime_filter
, entry
) {
279 if(!strcmpW(iter
->mime
, mime
)) {
285 LeaveCriticalSection(&session_cs
);
288 hres
= IClassFactory_CreateInstance(cf
, NULL
, &IID_IInternetProtocol
, (void**)&ret
);
290 WARN("CreateInstance failed: %08x\n", hres
);
297 res
= RegOpenKeyW(HKEY_CLASSES_ROOT
, filtersW
, &hlist
);
298 if(res
!= ERROR_SUCCESS
) {
299 TRACE("Could not open MIME filters key\n");
303 res
= RegOpenKeyW(hlist
, mime
, &hfilter
);
305 if(res
!= ERROR_SUCCESS
)
308 size
= sizeof(clsidw
);
309 res
= RegQueryValueExW(hfilter
, CLSIDW
, NULL
, &type
, (LPBYTE
)clsidw
, &size
);
310 CloseHandle(hfilter
);
311 if(res
!=ERROR_SUCCESS
|| type
!=REG_SZ
) {
312 WARN("Could not get filter CLSID for %s\n", debugstr_w(mime
));
316 hres
= CLSIDFromString(clsidw
, &clsid
);
318 WARN("CLSIDFromString failed for %s (%x)\n", debugstr_w(mime
), hres
);
322 hres
= CoCreateInstance(&clsid
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IInternetProtocol
, (void**)&ret
);
324 WARN("CoCreateInstance failed: %08x\n", hres
);
331 static HRESULT WINAPI
InternetSession_QueryInterface(IInternetSession
*iface
,
332 REFIID riid
, void **ppv
)
334 TRACE("(%s %p)\n", debugstr_guid(riid
), ppv
);
336 if(IsEqualGUID(&IID_IUnknown
, riid
) || IsEqualGUID(&IID_IInternetSession
, riid
)) {
338 IInternetSession_AddRef(iface
);
343 return E_NOINTERFACE
;
346 static ULONG WINAPI
InternetSession_AddRef(IInternetSession
*iface
)
353 static ULONG WINAPI
InternetSession_Release(IInternetSession
*iface
)
356 URLMON_UnlockModule();
360 static HRESULT WINAPI
InternetSession_RegisterNameSpace(IInternetSession
*iface
,
361 IClassFactory
*pCF
, REFCLSID rclsid
, LPCWSTR pwzProtocol
, ULONG cPatterns
,
362 const LPCWSTR
*ppwzPatterns
, DWORD dwReserved
)
364 TRACE("(%p %s %s %d %p %d)\n", pCF
, debugstr_guid(rclsid
), debugstr_w(pwzProtocol
),
365 cPatterns
, ppwzPatterns
, dwReserved
);
367 if(cPatterns
|| ppwzPatterns
)
368 FIXME("patterns not supported\n");
370 WARN("dwReserved = %d\n", dwReserved
);
372 if(!pCF
|| !pwzProtocol
)
375 return register_namespace(pCF
, rclsid
, pwzProtocol
, FALSE
);
378 static HRESULT WINAPI
InternetSession_UnregisterNameSpace(IInternetSession
*iface
,
379 IClassFactory
*pCF
, LPCWSTR pszProtocol
)
381 TRACE("(%p %s)\n", pCF
, debugstr_w(pszProtocol
));
383 if(!pCF
|| !pszProtocol
)
386 return unregister_namespace(pCF
, pszProtocol
);
389 static HRESULT WINAPI
InternetSession_RegisterMimeFilter(IInternetSession
*iface
,
390 IClassFactory
*pCF
, REFCLSID rclsid
, LPCWSTR pwzType
)
394 TRACE("(%p %s %s)\n", pCF
, debugstr_guid(rclsid
), debugstr_w(pwzType
));
396 filter
= heap_alloc(sizeof(mime_filter
));
398 IClassFactory_AddRef(pCF
);
400 filter
->clsid
= *rclsid
;
401 filter
->mime
= heap_strdupW(pwzType
);
403 EnterCriticalSection(&session_cs
);
405 list_add_head(&mime_filter_list
, &filter
->entry
);
407 LeaveCriticalSection(&session_cs
);
412 static HRESULT WINAPI
InternetSession_UnregisterMimeFilter(IInternetSession
*iface
,
413 IClassFactory
*pCF
, LPCWSTR pwzType
)
417 TRACE("(%p %s)\n", pCF
, debugstr_w(pwzType
));
419 EnterCriticalSection(&session_cs
);
421 LIST_FOR_EACH_ENTRY(iter
, &mime_filter_list
, mime_filter
, entry
) {
422 if(iter
->cf
== pCF
&& !strcmpW(iter
->mime
, pwzType
)) {
423 list_remove(&iter
->entry
);
425 LeaveCriticalSection(&session_cs
);
427 IClassFactory_Release(iter
->cf
);
428 heap_free(iter
->mime
);
434 LeaveCriticalSection(&session_cs
);
438 static HRESULT WINAPI
InternetSession_CreateBinding(IInternetSession
*iface
,
439 LPBC pBC
, LPCWSTR szUrl
, IUnknown
*pUnkOuter
, IUnknown
**ppUnk
,
440 IInternetProtocol
**ppOInetProt
, DWORD dwOption
)
442 BindProtocol
*protocol
;
445 TRACE("(%p %s %p %p %p %08x)\n", pBC
, debugstr_w(szUrl
), pUnkOuter
, ppUnk
,
446 ppOInetProt
, dwOption
);
448 if(pBC
|| pUnkOuter
|| ppUnk
|| dwOption
)
449 FIXME("Unsupported arguments\n");
451 hres
= create_binding_protocol(FALSE
, &protocol
);
455 *ppOInetProt
= (IInternetProtocol
*)&protocol
->IInternetProtocolEx_iface
;
459 static HRESULT WINAPI
InternetSession_SetSessionOption(IInternetSession
*iface
,
460 DWORD dwOption
, LPVOID pBuffer
, DWORD dwBufferLength
, DWORD dwReserved
)
462 FIXME("(%08x %p %d %d)\n", dwOption
, pBuffer
, dwBufferLength
, dwReserved
);
466 static const IInternetSessionVtbl InternetSessionVtbl
= {
467 InternetSession_QueryInterface
,
468 InternetSession_AddRef
,
469 InternetSession_Release
,
470 InternetSession_RegisterNameSpace
,
471 InternetSession_UnregisterNameSpace
,
472 InternetSession_RegisterMimeFilter
,
473 InternetSession_UnregisterMimeFilter
,
474 InternetSession_CreateBinding
,
475 InternetSession_SetSessionOption
478 static IInternetSession InternetSession
= { &InternetSessionVtbl
};
480 /***********************************************************************
481 * CoInternetGetSession (URLMON.@)
483 * Create a new internet session and return an IInternetSession interface
487 * dwSessionMode [I] Mode for the internet session
488 * ppIInternetSession [O] Destination for creates IInternetSession object
489 * dwReserved [I] Reserved, must be 0.
492 * Success: S_OK. ppIInternetSession contains the IInternetSession interface.
493 * Failure: E_INVALIDARG, if any argument is invalid, or
494 * E_OUTOFMEMORY if memory allocation fails.
496 HRESULT WINAPI
CoInternetGetSession(DWORD dwSessionMode
, IInternetSession
**ppIInternetSession
,
499 TRACE("(%d %p %d)\n", dwSessionMode
, ppIInternetSession
, dwReserved
);
502 ERR("dwSessionMode=%d\n", dwSessionMode
);
504 ERR("dwReserved=%d\n", dwReserved
);
506 IInternetSession_AddRef(&InternetSession
);
507 *ppIInternetSession
= &InternetSession
;
511 /**************************************************************************
512 * UrlMkGetSessionOption (URLMON.@)
514 static BOOL
get_url_encoding(HKEY root
, DWORD
*encoding
)
516 DWORD size
= sizeof(DWORD
), res
, type
;
519 static const WCHAR wszUrlEncoding
[] = {'U','r','l','E','n','c','o','d','i','n','g',0};
521 res
= RegOpenKeyW(root
, internet_settings_keyW
, &hkey
);
522 if(res
!= ERROR_SUCCESS
)
525 res
= RegQueryValueExW(hkey
, wszUrlEncoding
, NULL
, &type
, (LPBYTE
)encoding
, &size
);
528 return res
== ERROR_SUCCESS
;
531 static LPWSTR user_agent
;
533 static void ensure_useragent(void)
535 OSVERSIONINFOW info
= {sizeof(info
)};
536 const WCHAR
*os_type
, *is_nt
;
537 WCHAR buf
[512], *ret
, *tmp
;
543 static const WCHAR formatW
[] =
544 {'M','o','z','i','l','l','a','/','4','.','0',
545 ' ','(','c','o','m','p','a','t','i','b','l','e',';',
546 ' ','M','S','I','E',' ','8','.','0',';',
547 ' ','W','i','n','d','o','w','s',' ','%','s','%','d','.','%','d',';',
548 ' ','%','s',';',' ','T','r','i','d','e','n','t','/','5','.','0',0};
549 static const WCHAR post_platform_keyW
[] =
550 {'S','O','F','T','W','A','R','E',
551 '\\','M','i','c','r','o','s','o','f','t',
552 '\\','W','i','n','d','o','w','s',
553 '\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n',
554 '\\','I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s',
555 '\\','5','.','0','\\','U','s','e','r',' ','A','g','e','n','t',
556 '\\','P','o','s','t',' ','P','l','a','t','f','o','r','m',0};
557 static const WCHAR ntW
[] = {'N','T',' ',0};
558 static const WCHAR win32W
[] = {'W','i','n','3','2',0};
559 static const WCHAR win64W
[] = {'W','i','n','6','4',0};
560 static const WCHAR wow64W
[] = {'W','O','W','6','4',0};
561 static const WCHAR emptyW
[] = {0};
566 GetVersionExW(&info
);
567 is_nt
= info
.dwPlatformId
== VER_PLATFORM_WIN32_NT
? ntW
: emptyW
;
569 if(sizeof(void*) == 8)
571 else if(IsWow64Process(GetCurrentProcess(), &is_wow
) && is_wow
)
576 sprintfW(buf
, formatW
, is_nt
, info
.dwMajorVersion
, info
.dwMinorVersion
, os_type
);
580 ret
= heap_alloc(size
* sizeof(WCHAR
));
584 memcpy(ret
, buf
, len
*sizeof(WCHAR
));
586 res
= RegOpenKeyW(HKEY_LOCAL_MACHINE
, post_platform_keyW
, &key
);
587 if(res
== ERROR_SUCCESS
) {
591 value_len
= sizeof(buf
)/sizeof(WCHAR
);
592 res
= RegEnumValueW(key
, idx
, buf
, &value_len
, NULL
, NULL
, NULL
, NULL
);
593 if(res
!= ERROR_SUCCESS
)
597 if(len
+ value_len
+ 2 /* strlen("; ") */ + 1 /* trailing ')' */ >= size
) {
598 tmp
= heap_realloc(ret
, (size
*2+value_len
)*sizeof(WCHAR
));
602 size
= size
*2+value_len
;
607 memcpy(ret
+len
, buf
, value_len
*sizeof(WCHAR
));
618 TRACE("Using user agent %s\n", debugstr_w(user_agent
));
621 LPWSTR
get_useragent(void)
627 EnterCriticalSection(&session_cs
);
628 ret
= heap_strdupW(user_agent
);
629 LeaveCriticalSection(&session_cs
);
634 HRESULT WINAPI
UrlMkGetSessionOption(DWORD dwOption
, LPVOID pBuffer
, DWORD dwBufferLength
,
635 DWORD
* pdwBufferLength
, DWORD dwReserved
)
637 TRACE("(%x, %p, %d, %p)\n", dwOption
, pBuffer
, dwBufferLength
, pdwBufferLength
);
640 WARN("dwReserved = %d\n", dwReserved
);
643 case URLMON_OPTION_USERAGENT
: {
644 HRESULT hres
= E_OUTOFMEMORY
;
650 EnterCriticalSection(&session_cs
);
654 size
= WideCharToMultiByte(CP_ACP
, 0, user_agent
, -1, NULL
, 0, NULL
, NULL
);
655 *pdwBufferLength
= size
;
656 if(size
<= dwBufferLength
) {
658 WideCharToMultiByte(CP_ACP
, 0, user_agent
, -1, pBuffer
, size
, NULL
, NULL
);
664 LeaveCriticalSection(&session_cs
);
666 /* Tests prove that we have to return E_OUTOFMEMORY on success. */
669 case URLMON_OPTION_URL_ENCODING
: {
672 if(!pBuffer
|| dwBufferLength
< sizeof(DWORD
) || !pdwBufferLength
)
675 if(!get_url_encoding(HKEY_CURRENT_USER
, &encoding
))
676 get_url_encoding(HKEY_LOCAL_MACHINE
, &encoding
);
678 *pdwBufferLength
= sizeof(DWORD
);
679 *(DWORD
*)pBuffer
= encoding
? URL_ENCODING_DISABLE_UTF8
: URL_ENCODING_ENABLE_UTF8
;
683 FIXME("unsupported option %x\n", dwOption
);
689 /**************************************************************************
690 * UrlMkSetSessionOption (URLMON.@)
692 HRESULT WINAPI
UrlMkSetSessionOption(DWORD dwOption
, LPVOID pBuffer
, DWORD dwBufferLength
,
695 TRACE("(%x %p %x)\n", dwOption
, pBuffer
, dwBufferLength
);
698 case URLMON_OPTION_USERAGENT
: {
699 LPWSTR new_user_agent
;
703 if(!pBuffer
|| !dwBufferLength
)
706 for(len
=0; len
<dwBufferLength
&& buf
[len
]; len
++);
708 TRACE("Setting user agent %s\n", debugstr_an(buf
, len
));
710 size
= MultiByteToWideChar(CP_ACP
, 0, buf
, len
, NULL
, 0);
711 new_user_agent
= heap_alloc((size
+1)*sizeof(WCHAR
));
713 return E_OUTOFMEMORY
;
714 MultiByteToWideChar(CP_ACP
, 0, buf
, len
, new_user_agent
, size
);
715 new_user_agent
[size
] = 0;
717 EnterCriticalSection(&session_cs
);
719 heap_free(user_agent
);
720 user_agent
= new_user_agent
;
722 LeaveCriticalSection(&session_cs
);
726 FIXME("Unknown option %x\n", dwOption
);
733 /**************************************************************************
734 * ObtainUserAgentString (URLMON.@)
736 HRESULT WINAPI
ObtainUserAgentString(DWORD dwOption
, LPSTR pcszUAOut
, DWORD
*cbSize
)
739 HRESULT hres
= E_FAIL
;
741 TRACE("(%d %p %p)\n", dwOption
, pcszUAOut
, cbSize
);
743 if(!pcszUAOut
|| !cbSize
)
746 EnterCriticalSection(&session_cs
);
750 size
= WideCharToMultiByte(CP_ACP
, 0, user_agent
, -1, NULL
, 0, NULL
, NULL
);
752 if(size
<= *cbSize
) {
753 WideCharToMultiByte(CP_ACP
, 0, user_agent
, -1, pcszUAOut
, *cbSize
, NULL
, NULL
);
756 hres
= E_OUTOFMEMORY
;
762 LeaveCriticalSection(&session_cs
);
766 void free_session(void)
768 name_space
*ns_iter
, *ns_last
;
769 mime_filter
*mf_iter
, *mf_last
;
771 LIST_FOR_EACH_ENTRY_SAFE(ns_iter
, ns_last
, &name_space_list
, name_space
, entry
) {
773 IClassFactory_Release(ns_iter
->cf
);
774 heap_free(ns_iter
->protocol
);
778 LIST_FOR_EACH_ENTRY_SAFE(mf_iter
, mf_last
, &mime_filter_list
, mime_filter
, entry
) {
779 IClassFactory_Release(mf_iter
->cf
);
780 heap_free(mf_iter
->mime
);
784 heap_free(user_agent
);