msvcrt: Added UTF16 support to read function.
[wine] / dlls / urlmon / session.c
1 /*
2  * Copyright 2005-2006 Jacek Caban for CodeWeavers
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #include "urlmon_main.h"
20 #include "winreg.h"
21
22 #include "wine/debug.h"
23
24 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
25
26 typedef struct {
27     LPWSTR protocol;
28     IClassFactory *cf;
29     CLSID clsid;
30     BOOL urlmon;
31
32     struct list entry;
33 } name_space;
34
35 typedef struct {
36     IClassFactory *cf;
37     CLSID clsid;
38     LPWSTR mime;
39
40     struct list entry;
41 } mime_filter;
42
43 static struct list name_space_list = LIST_INIT(name_space_list);
44 static struct list mime_filter_list = LIST_INIT(mime_filter_list);
45
46 static CRITICAL_SECTION session_cs;
47 static CRITICAL_SECTION_DEBUG session_cs_dbg =
48 {
49     0, 0, &session_cs,
50     { &session_cs_dbg.ProcessLocksList, &session_cs_dbg.ProcessLocksList },
51       0, 0, { (DWORD_PTR)(__FILE__ ": session") }
52 };
53 static CRITICAL_SECTION session_cs = { &session_cs_dbg, -1, 0, 0, 0, 0 };
54
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};
61
62 static name_space *find_name_space(LPCWSTR protocol)
63 {
64     name_space *iter;
65
66     LIST_FOR_EACH_ENTRY(iter, &name_space_list, name_space, entry) {
67         if(!strcmpiW(iter->protocol, protocol))
68             return iter;
69     }
70
71     return NULL;
72 }
73
74 static HRESULT get_protocol_cf(LPCWSTR schema, DWORD schema_len, CLSID *pclsid, IClassFactory **ret)
75 {
76     WCHAR str_clsid[64];
77     HKEY hkey = NULL;
78     DWORD res, type, size;
79     CLSID clsid;
80     LPWSTR wszKey;
81     HRESULT hres;
82
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};
86
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));
90
91     res = RegOpenKeyW(HKEY_CLASSES_ROOT, wszKey, &hkey);
92     heap_free(wszKey);
93     if(res != ERROR_SUCCESS) {
94         TRACE("Could not open protocol handler key\n");
95         return MK_E_SYNTAX;
96     }
97     
98     size = sizeof(str_clsid);
99     res = RegQueryValueExW(hkey, wszCLSID, NULL, &type, (LPBYTE)str_clsid, &size);
100     RegCloseKey(hkey);
101     if(res != ERROR_SUCCESS || type != REG_SZ) {
102         WARN("Could not get protocol CLSID res=%d\n", res);
103         return MK_E_SYNTAX;
104     }
105
106     hres = CLSIDFromString(str_clsid, &clsid);
107     if(FAILED(hres)) {
108         WARN("CLSIDFromString failed: %08x\n", hres);
109         return hres;
110     }
111
112     if(pclsid)
113         *pclsid = clsid;
114
115     if(!ret)
116         return S_OK;
117
118     hres = CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL, &IID_IClassFactory, (void**)ret);
119     return SUCCEEDED(hres) ? S_OK : MK_E_SYNTAX;
120 }
121
122 HRESULT register_namespace(IClassFactory *cf, REFIID clsid, LPCWSTR protocol, BOOL urlmon_protocol)
123 {
124     name_space *new_name_space;
125
126     new_name_space = heap_alloc(sizeof(name_space));
127
128     if(!urlmon_protocol)
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);
134
135     EnterCriticalSection(&session_cs);
136
137     list_add_head(&name_space_list, &new_name_space->entry);
138
139     LeaveCriticalSection(&session_cs);
140
141     return S_OK;
142 }
143
144 static HRESULT unregister_namespace(IClassFactory *cf, LPCWSTR protocol)
145 {
146     name_space *iter;
147
148     EnterCriticalSection(&session_cs);
149
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);
153
154             LeaveCriticalSection(&session_cs);
155
156             if(!iter->urlmon)
157                 IClassFactory_Release(iter->cf);
158             heap_free(iter->protocol);
159             heap_free(iter);
160             return S_OK;
161         }
162     }
163
164     LeaveCriticalSection(&session_cs);
165     return S_OK;
166 }
167
168 BOOL is_registered_protocol(LPCWSTR url)
169 {
170     DWORD schema_len;
171     WCHAR schema[64];
172     HRESULT hres;
173
174     hres = CoInternetParseUrl(url, PARSE_SCHEMA, 0, schema, sizeof(schema)/sizeof(schema[0]),
175             &schema_len, 0);
176     if(FAILED(hres))
177         return FALSE;
178
179     return get_protocol_cf(schema, schema_len, NULL, NULL) == S_OK;
180 }
181
182 IInternetProtocolInfo *get_protocol_info(LPCWSTR url)
183 {
184     IInternetProtocolInfo *ret = NULL;
185     IClassFactory *cf;
186     name_space *ns;
187     WCHAR schema[64];
188     DWORD schema_len;
189     HRESULT hres;
190
191     hres = CoInternetParseUrl(url, PARSE_SCHEMA, 0, schema, sizeof(schema)/sizeof(schema[0]),
192             &schema_len, 0);
193     if(FAILED(hres) || !schema_len)
194         return NULL;
195
196     EnterCriticalSection(&session_cs);
197
198     ns = find_name_space(schema);
199     if(ns && !ns->urlmon) {
200         hres = IClassFactory_QueryInterface(ns->cf, &IID_IInternetProtocolInfo, (void**)&ret);
201         if(FAILED(hres))
202             hres = IClassFactory_CreateInstance(ns->cf, NULL, &IID_IInternetProtocolInfo, (void**)&ret);
203     }
204
205     LeaveCriticalSection(&session_cs);
206
207     if(ns && SUCCEEDED(hres))
208         return ret;
209
210     hres = get_protocol_cf(schema, schema_len, NULL, &cf);
211     if(FAILED(hres))
212         return NULL;
213
214     hres = IClassFactory_QueryInterface(cf, &IID_IInternetProtocolInfo, (void**)&ret);
215     if(FAILED(hres))
216         IClassFactory_CreateInstance(cf, NULL, &IID_IInternetProtocolInfo, (void**)&ret);
217     IClassFactory_Release(cf);
218
219     return ret;
220 }
221
222 HRESULT get_protocol_handler(IUri *uri, CLSID *clsid, BOOL *urlmon_protocol, IClassFactory **ret)
223 {
224     name_space *ns;
225     BSTR scheme;
226     HRESULT hres;
227
228     *ret = NULL;
229
230     /* FIXME: Avoid GetSchemeName call for known schemes */
231     hres = IUri_GetSchemeName(uri, &scheme);
232     if(FAILED(hres))
233         return hres;
234
235     EnterCriticalSection(&session_cs);
236
237     ns = find_name_space(scheme);
238     if(ns) {
239         *ret = ns->cf;
240         IClassFactory_AddRef(*ret);
241         if(clsid)
242             *clsid = ns->clsid;
243         if(urlmon_protocol)
244             *urlmon_protocol = ns->urlmon;
245     }
246
247     LeaveCriticalSection(&session_cs);
248
249     if(*ret) {
250         hres = S_OK;
251     }else {
252         if(urlmon_protocol)
253             *urlmon_protocol = FALSE;
254         hres = get_protocol_cf(scheme, SysStringLen(scheme), clsid, ret);
255     }
256
257     SysFreeString(scheme);
258     return hres;
259 }
260
261 IInternetProtocol *get_mime_filter(LPCWSTR mime)
262 {
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};
266
267     IClassFactory *cf = NULL;
268     IInternetProtocol *ret;
269     mime_filter *iter;
270     HKEY hlist, hfilter;
271     WCHAR clsidw[64];
272     CLSID clsid;
273     DWORD res, type, size;
274     HRESULT hres;
275
276     EnterCriticalSection(&session_cs);
277
278     LIST_FOR_EACH_ENTRY(iter, &mime_filter_list, mime_filter, entry) {
279         if(!strcmpW(iter->mime, mime)) {
280             cf = iter->cf;
281             break;
282         }
283     }
284
285     LeaveCriticalSection(&session_cs);
286
287     if(cf) {
288         hres = IClassFactory_CreateInstance(cf, NULL, &IID_IInternetProtocol, (void**)&ret);
289         if(FAILED(hres)) {
290             WARN("CreateInstance failed: %08x\n", hres);
291             return NULL;
292         }
293
294         return ret;
295     }
296
297     res = RegOpenKeyW(HKEY_CLASSES_ROOT, filtersW, &hlist);
298     if(res != ERROR_SUCCESS) {
299         TRACE("Could not open MIME filters key\n");
300         return NULL;
301     }
302
303     res = RegOpenKeyW(hlist, mime, &hfilter);
304     CloseHandle(hlist);
305     if(res != ERROR_SUCCESS)
306         return NULL;
307
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));
313         return NULL;
314     }
315
316     hres = CLSIDFromString(clsidw, &clsid);
317     if(FAILED(hres)) {
318         WARN("CLSIDFromString failed for %s (%x)\n", debugstr_w(mime), hres);
319         return NULL;
320     }
321
322     hres = CoCreateInstance(&clsid, NULL, CLSCTX_INPROC_SERVER, &IID_IInternetProtocol, (void**)&ret);
323     if(FAILED(hres)) {
324         WARN("CoCreateInstance failed: %08x\n", hres);
325         return NULL;
326     }
327
328     return ret;
329 }
330
331 static HRESULT WINAPI InternetSession_QueryInterface(IInternetSession *iface,
332         REFIID riid, void **ppv)
333 {
334     TRACE("(%s %p)\n", debugstr_guid(riid), ppv);
335
336     if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IInternetSession, riid)) {
337         *ppv = iface;
338         IInternetSession_AddRef(iface);
339         return S_OK;
340     }
341
342     *ppv = NULL;
343     return E_NOINTERFACE;
344 }
345
346 static ULONG WINAPI InternetSession_AddRef(IInternetSession *iface)
347 {
348     TRACE("()\n");
349     URLMON_LockModule();
350     return 2;
351 }
352
353 static ULONG WINAPI InternetSession_Release(IInternetSession *iface)
354 {
355     TRACE("()\n");
356     URLMON_UnlockModule();
357     return 1;
358 }
359
360 static HRESULT WINAPI InternetSession_RegisterNameSpace(IInternetSession *iface,
361         IClassFactory *pCF, REFCLSID rclsid, LPCWSTR pwzProtocol, ULONG cPatterns,
362         const LPCWSTR *ppwzPatterns, DWORD dwReserved)
363 {
364     TRACE("(%p %s %s %d %p %d)\n", pCF, debugstr_guid(rclsid), debugstr_w(pwzProtocol),
365           cPatterns, ppwzPatterns, dwReserved);
366
367     if(cPatterns || ppwzPatterns)
368         FIXME("patterns not supported\n");
369     if(dwReserved)
370         WARN("dwReserved = %d\n", dwReserved);
371
372     if(!pCF || !pwzProtocol)
373         return E_INVALIDARG;
374
375     return register_namespace(pCF, rclsid, pwzProtocol, FALSE);
376 }
377
378 static HRESULT WINAPI InternetSession_UnregisterNameSpace(IInternetSession *iface,
379         IClassFactory *pCF, LPCWSTR pszProtocol)
380 {
381     TRACE("(%p %s)\n", pCF, debugstr_w(pszProtocol));
382
383     if(!pCF || !pszProtocol)
384         return E_INVALIDARG;
385
386     return unregister_namespace(pCF, pszProtocol);
387 }
388
389 static HRESULT WINAPI InternetSession_RegisterMimeFilter(IInternetSession *iface,
390         IClassFactory *pCF, REFCLSID rclsid, LPCWSTR pwzType)
391 {
392     mime_filter *filter;
393
394     TRACE("(%p %s %s)\n", pCF, debugstr_guid(rclsid), debugstr_w(pwzType));
395
396     filter = heap_alloc(sizeof(mime_filter));
397
398     IClassFactory_AddRef(pCF);
399     filter->cf = pCF;
400     filter->clsid = *rclsid;
401     filter->mime = heap_strdupW(pwzType);
402
403     EnterCriticalSection(&session_cs);
404
405     list_add_head(&mime_filter_list, &filter->entry);
406
407     LeaveCriticalSection(&session_cs);
408
409     return S_OK;
410 }
411
412 static HRESULT WINAPI InternetSession_UnregisterMimeFilter(IInternetSession *iface,
413         IClassFactory *pCF, LPCWSTR pwzType)
414 {
415     mime_filter *iter;
416
417     TRACE("(%p %s)\n", pCF, debugstr_w(pwzType));
418
419     EnterCriticalSection(&session_cs);
420
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);
424
425             LeaveCriticalSection(&session_cs);
426
427             IClassFactory_Release(iter->cf);
428             heap_free(iter->mime);
429             heap_free(iter);
430             return S_OK;
431         }
432     }
433
434     LeaveCriticalSection(&session_cs);
435     return S_OK;
436 }
437
438 static HRESULT WINAPI InternetSession_CreateBinding(IInternetSession *iface,
439         LPBC pBC, LPCWSTR szUrl, IUnknown *pUnkOuter, IUnknown **ppUnk,
440         IInternetProtocol **ppOInetProt, DWORD dwOption)
441 {
442     BindProtocol *protocol;
443     HRESULT hres;
444
445     TRACE("(%p %s %p %p %p %08x)\n", pBC, debugstr_w(szUrl), pUnkOuter, ppUnk,
446             ppOInetProt, dwOption);
447
448     if(pBC || pUnkOuter || ppUnk || dwOption)
449         FIXME("Unsupported arguments\n");
450
451     hres = create_binding_protocol(FALSE, &protocol);
452     if(FAILED(hres))
453         return hres;
454
455     *ppOInetProt = (IInternetProtocol*)&protocol->IInternetProtocolEx_iface;
456     return S_OK;
457 }
458
459 static HRESULT WINAPI InternetSession_SetSessionOption(IInternetSession *iface,
460         DWORD dwOption, LPVOID pBuffer, DWORD dwBufferLength, DWORD dwReserved)
461 {
462     FIXME("(%08x %p %d %d)\n", dwOption, pBuffer, dwBufferLength, dwReserved);
463     return E_NOTIMPL;
464 }
465
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
476 };
477
478 static IInternetSession InternetSession = { &InternetSessionVtbl };
479
480 /***********************************************************************
481  *           CoInternetGetSession (URLMON.@)
482  *
483  * Create a new internet session and return an IInternetSession interface
484  * representing it.
485  *
486  * PARAMS
487  *    dwSessionMode      [I] Mode for the internet session
488  *    ppIInternetSession [O] Destination for creates IInternetSession object
489  *    dwReserved         [I] Reserved, must be 0.
490  *
491  * RETURNS
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.
495  */
496 HRESULT WINAPI CoInternetGetSession(DWORD dwSessionMode, IInternetSession **ppIInternetSession,
497         DWORD dwReserved)
498 {
499     TRACE("(%d %p %d)\n", dwSessionMode, ppIInternetSession, dwReserved);
500
501     if(dwSessionMode)
502         ERR("dwSessionMode=%d\n", dwSessionMode);
503     if(dwReserved)
504         ERR("dwReserved=%d\n", dwReserved);
505
506     IInternetSession_AddRef(&InternetSession);
507     *ppIInternetSession = &InternetSession;
508     return S_OK;
509 }
510
511 /**************************************************************************
512  *                 UrlMkGetSessionOption (URLMON.@)
513  */
514 static BOOL get_url_encoding(HKEY root, DWORD *encoding)
515 {
516     DWORD size = sizeof(DWORD), res, type;
517     HKEY hkey;
518
519     static const WCHAR wszUrlEncoding[] = {'U','r','l','E','n','c','o','d','i','n','g',0};
520
521     res = RegOpenKeyW(root, internet_settings_keyW, &hkey);
522     if(res != ERROR_SUCCESS)
523         return FALSE;
524
525     res = RegQueryValueExW(hkey, wszUrlEncoding, NULL, &type, (LPBYTE)encoding, &size);
526     RegCloseKey(hkey);
527
528     return res == ERROR_SUCCESS;
529 }
530
531 static LPWSTR user_agent;
532
533 static void ensure_useragent(void)
534 {
535     OSVERSIONINFOW info = {sizeof(info)};
536     const WCHAR *os_type, *is_nt;
537     WCHAR buf[512];
538     BOOL is_wow;
539
540     static const WCHAR formatW[] =
541         {'M','o','z','i','l','l','a','/','4','.','0',
542          ' ','(','c','o','m','p','a','t','i','b','l','e',';',
543          ' ','M','S','I','E',' ','8','.','0',';',
544          ' ','W','i','n','d','o','w','s',' ','%','s','%','d','.','%','d',';',
545          ' ','%','s',';',' ','T','r','i','d','e','n','t','/','5','.','0',')',0};
546     static const WCHAR ntW[] = {'N','T',' ',0};
547     static const WCHAR win32W[] = {'W','i','n','3','2',0};
548     static const WCHAR win64W[] = {'W','i','n','6','4',0};
549     static const WCHAR wow64W[] = {'W','O','W','6','4',0};
550     static const WCHAR emptyW[] = {0};
551
552     if(user_agent)
553         return;
554
555     GetVersionExW(&info);
556     is_nt = info.dwPlatformId == VER_PLATFORM_WIN32_NT ? ntW : emptyW;
557
558     if(sizeof(void*) == 8)
559         os_type = win64W;
560     else if(IsWow64Process(GetCurrentProcess(), &is_wow) && is_wow)
561         os_type = wow64W;
562     else
563         os_type = win32W;
564
565     sprintfW(buf, formatW, is_nt, info.dwMajorVersion, info.dwMinorVersion, os_type);
566     user_agent = heap_strdupW(buf);
567 }
568
569 LPWSTR get_useragent(void)
570 {
571     LPWSTR ret;
572
573     ensure_useragent();
574
575     EnterCriticalSection(&session_cs);
576     ret = heap_strdupW(user_agent);
577     LeaveCriticalSection(&session_cs);
578
579     return ret;
580 }
581
582 HRESULT WINAPI UrlMkGetSessionOption(DWORD dwOption, LPVOID pBuffer, DWORD dwBufferLength,
583                                      DWORD* pdwBufferLength, DWORD dwReserved)
584 {
585     TRACE("(%x, %p, %d, %p)\n", dwOption, pBuffer, dwBufferLength, pdwBufferLength);
586
587     if(dwReserved)
588         WARN("dwReserved = %d\n", dwReserved);
589
590     switch(dwOption) {
591     case URLMON_OPTION_USERAGENT: {
592         HRESULT hres = E_OUTOFMEMORY;
593         DWORD size;
594
595         if(!pdwBufferLength)
596             return E_INVALIDARG;
597
598         EnterCriticalSection(&session_cs);
599
600         ensure_useragent();
601         if(user_agent) {
602             size = WideCharToMultiByte(CP_ACP, 0, user_agent, -1, NULL, 0, NULL, NULL);
603             *pdwBufferLength = size;
604             if(size <= dwBufferLength) {
605                 if(pBuffer)
606                     WideCharToMultiByte(CP_ACP, 0, user_agent, -1, pBuffer, size, NULL, NULL);
607                 else
608                     hres = E_INVALIDARG;
609             }
610         }
611
612         LeaveCriticalSection(&session_cs);
613
614         /* Tests prove that we have to return E_OUTOFMEMORY on success. */
615         return hres;
616     }
617     case URLMON_OPTION_URL_ENCODING: {
618         DWORD encoding = 0;
619
620         if(!pBuffer || dwBufferLength < sizeof(DWORD) || !pdwBufferLength)
621             return E_INVALIDARG;
622
623         if(!get_url_encoding(HKEY_CURRENT_USER, &encoding))
624             get_url_encoding(HKEY_LOCAL_MACHINE, &encoding);
625
626         *pdwBufferLength = sizeof(DWORD);
627         *(DWORD*)pBuffer = encoding ? URL_ENCODING_DISABLE_UTF8 : URL_ENCODING_ENABLE_UTF8;
628         return S_OK;
629     }
630     default:
631         FIXME("unsupported option %x\n", dwOption);
632     }
633
634     return E_INVALIDARG;
635 }
636
637 /**************************************************************************
638  *                 UrlMkSetSessionOption (URLMON.@)
639  */
640 HRESULT WINAPI UrlMkSetSessionOption(DWORD dwOption, LPVOID pBuffer, DWORD dwBufferLength,
641         DWORD Reserved)
642 {
643     TRACE("(%x %p %x)\n", dwOption, pBuffer, dwBufferLength);
644
645     switch(dwOption) {
646     case URLMON_OPTION_USERAGENT: {
647         LPWSTR new_user_agent;
648         char *buf = pBuffer;
649         DWORD len, size;
650
651         if(!pBuffer || !dwBufferLength)
652             return E_INVALIDARG;
653
654         for(len=0; len<dwBufferLength && buf[len]; len++);
655
656         TRACE("Setting user agent %s\n", debugstr_an(buf, len));
657
658         size = MultiByteToWideChar(CP_ACP, 0, buf, len, NULL, 0);
659         new_user_agent = heap_alloc((size+1)*sizeof(WCHAR));
660         if(!new_user_agent)
661             return E_OUTOFMEMORY;
662         MultiByteToWideChar(CP_ACP, 0, buf, len, new_user_agent, size);
663         new_user_agent[size] = 0;
664
665         EnterCriticalSection(&session_cs);
666
667         heap_free(user_agent);
668         user_agent = new_user_agent;
669
670         LeaveCriticalSection(&session_cs);
671         break;
672     }
673     default:
674         FIXME("Unknown option %x\n", dwOption);
675         return E_INVALIDARG;
676     }
677
678     return S_OK;
679 }
680
681 /**************************************************************************
682  *                 ObtainUserAgentString (URLMON.@)
683  */
684 HRESULT WINAPI ObtainUserAgentString(DWORD dwOption, LPSTR pcszUAOut, DWORD *cbSize)
685 {
686     DWORD size;
687     HRESULT hres = E_FAIL;
688
689     TRACE("(%d %p %p)\n", dwOption, pcszUAOut, cbSize);
690
691     if(!pcszUAOut || !cbSize)
692         return E_INVALIDARG;
693
694     EnterCriticalSection(&session_cs);
695
696     ensure_useragent();
697     if(user_agent) {
698         size = WideCharToMultiByte(CP_ACP, 0, user_agent, -1, NULL, 0, NULL, NULL);
699
700         if(size <= *cbSize) {
701             WideCharToMultiByte(CP_ACP, 0, user_agent, -1, pcszUAOut, *cbSize, NULL, NULL);
702             hres = S_OK;
703         }else {
704             hres = E_OUTOFMEMORY;
705         }
706
707         *cbSize = size;
708     }
709
710     LeaveCriticalSection(&session_cs);
711     return hres;
712 }
713
714 void free_session(void)
715 {
716     name_space *ns_iter, *ns_last;
717     mime_filter *mf_iter, *mf_last;
718
719     LIST_FOR_EACH_ENTRY_SAFE(ns_iter, ns_last, &name_space_list, name_space, entry) {
720             if(!ns_iter->urlmon)
721                 IClassFactory_Release(ns_iter->cf);
722             heap_free(ns_iter->protocol);
723             heap_free(ns_iter);
724     }
725
726     LIST_FOR_EACH_ENTRY_SAFE(mf_iter, mf_last, &mime_filter_list, mime_filter, entry) {
727             IClassFactory_Release(mf_iter->cf);
728             heap_free(mf_iter->mime);
729             heap_free(mf_iter);
730     }
731
732     heap_free(user_agent);
733 }