winecoreaudio: Protect pthread.h inclusion.
[wine] / dlls / urlmon / urlmon_main.c
1 /*
2  * UrlMon
3  *
4  * Copyright (c) 2000 Patrik Stridvall
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <stdarg.h>
22
23 #define COBJMACROS
24
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winreg.h"
28
29 #define NO_SHLWAPI_REG
30 #include "shlwapi.h"
31 #include "wine/debug.h"
32 #include "wine/unicode.h"
33
34 #include "winuser.h"
35 #include "urlmon.h"
36 #include "urlmon_main.h"
37 #include "ole2.h"
38
39 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
40
41 LONG URLMON_refCount = 0;
42
43 HINSTANCE URLMON_hInstance = 0;
44
45 DWORD urlmon_tls = 0;
46
47 /***********************************************************************
48  *              DllMain (URLMON.init)
49  */
50 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID fImpLoad)
51 {
52     TRACE("%p 0x%lx %p\n", hinstDLL, fdwReason, fImpLoad);
53
54     switch(fdwReason) {
55     case DLL_PROCESS_ATTACH:
56         DisableThreadLibraryCalls(hinstDLL);
57         URLMON_hInstance = hinstDLL;
58         break;
59
60     case DLL_PROCESS_DETACH:
61         if(urlmon_tls)
62             TlsFree(urlmon_tls);
63         URLMON_hInstance = 0;
64         break;
65     }
66     return TRUE;
67 }
68
69
70 /***********************************************************************
71  *              DllInstall (URLMON.@)
72  */
73 HRESULT WINAPI DllInstall(BOOL bInstall, LPCWSTR cmdline)
74 {
75   FIXME("(%s, %s): stub\n", bInstall?"TRUE":"FALSE",
76         debugstr_w(cmdline));
77
78   return S_OK;
79 }
80
81 /***********************************************************************
82  *              DllCanUnloadNow (URLMON.@)
83  */
84 HRESULT WINAPI DllCanUnloadNow(void)
85 {
86     return URLMON_refCount != 0 ? S_FALSE : S_OK;
87 }
88
89
90
91 /******************************************************************************
92  * Urlmon ClassFactory
93  */
94 typedef struct {
95     IClassFactory ITF_IClassFactory;
96
97     LONG ref;
98     HRESULT (*pfnCreateInstance)(IUnknown *pUnkOuter, LPVOID *ppObj);
99 } IClassFactoryImpl;
100
101 struct object_creation_info
102 {
103     const CLSID *clsid;
104     HRESULT (*pfnCreateInstance)(IUnknown *pUnkOuter, LPVOID *ppObj);
105 };
106  
107 static const struct object_creation_info object_creation[] =
108 {
109     { &CLSID_FileProtocol, FileProtocol_Construct },
110     { &CLSID_FtpProtocol, FtpProtocol_Construct },
111     { &CLSID_HttpProtocol, HttpProtocol_Construct },
112     { &CLSID_InternetSecurityManager, &SecManagerImpl_Construct },
113     { &CLSID_InternetZoneManager, ZoneMgrImpl_Construct }
114 };
115
116 static HRESULT WINAPI
117 CF_QueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj)
118 {
119     IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
120
121     if (IsEqualGUID(riid, &IID_IUnknown)
122         || IsEqualGUID(riid, &IID_IClassFactory))
123     {
124         IClassFactory_AddRef(iface);
125         *ppobj = This;
126         return S_OK;
127     }
128
129     WARN("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppobj);
130     return E_NOINTERFACE;
131 }
132
133 static ULONG WINAPI CF_AddRef(LPCLASSFACTORY iface)
134 {
135     IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
136     return InterlockedIncrement(&This->ref);
137 }
138
139 static ULONG WINAPI CF_Release(LPCLASSFACTORY iface)
140 {
141     IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
142
143     ULONG ref = InterlockedDecrement(&This->ref);
144
145     if (ref == 0) {
146         HeapFree(GetProcessHeap(), 0, This);
147         URLMON_UnlockModule();
148     }
149
150     return ref;
151 }
152
153
154 static HRESULT WINAPI CF_CreateInstance(LPCLASSFACTORY iface, LPUNKNOWN pOuter,
155                                         REFIID riid, LPVOID *ppobj)
156 {
157     IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
158     HRESULT hres;
159     LPUNKNOWN punk;
160     
161     TRACE("(%p)->(%p,%s,%p)\n",This,pOuter,debugstr_guid(riid),ppobj);
162
163     *ppobj = NULL;
164     if(SUCCEEDED(hres = This->pfnCreateInstance(pOuter, (LPVOID *) &punk))) {
165         hres = IUnknown_QueryInterface(punk, riid, ppobj);
166         IUnknown_Release(punk);
167     }
168     return hres;
169 }
170
171 static HRESULT WINAPI CF_LockServer(LPCLASSFACTORY iface,BOOL dolock)
172 {
173     TRACE("(%d)\n", dolock);
174
175     if (dolock)
176            URLMON_LockModule();
177     else
178            URLMON_UnlockModule();
179
180     return S_OK;
181 }
182
183 static const IClassFactoryVtbl CF_Vtbl =
184 {
185     CF_QueryInterface,
186     CF_AddRef,
187     CF_Release,
188     CF_CreateInstance,
189     CF_LockServer
190 };
191
192 /*******************************************************************************
193  * DllGetClassObject [URLMON.@]
194  * Retrieves class object from a DLL object
195  *
196  * NOTES
197  *    Docs say returns STDAPI
198  *
199  * PARAMS
200  *    rclsid [I] CLSID for the class object
201  *    riid   [I] Reference to identifier of interface for class object
202  *    ppv    [O] Address of variable to receive interface pointer for riid
203  *
204  * RETURNS
205  *    Success: S_OK
206  *    Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
207  *             E_UNEXPECTED
208  */
209
210 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
211 {
212     int i;
213     IClassFactoryImpl *factory;
214     
215     TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
216     
217     if ( !IsEqualGUID( &IID_IClassFactory, riid )
218          && ! IsEqualGUID( &IID_IUnknown, riid) )
219         return E_NOINTERFACE;
220
221     for (i=0; i < sizeof(object_creation)/sizeof(object_creation[0]); i++)
222     {
223         if (IsEqualGUID(object_creation[i].clsid, rclsid))
224             break;
225     }
226
227     if (i == sizeof(object_creation)/sizeof(object_creation[0]))
228     {
229         FIXME("%s: no class found.\n", debugstr_guid(rclsid));
230         return CLASS_E_CLASSNOTAVAILABLE;
231     }
232
233     factory = HeapAlloc(GetProcessHeap(), 0, sizeof(*factory));
234     if (factory == NULL) return E_OUTOFMEMORY;
235
236     factory->ITF_IClassFactory.lpVtbl = &CF_Vtbl;
237     factory->ref = 1;
238     factory->pfnCreateInstance = object_creation[i].pfnCreateInstance;
239
240     *ppv = &(factory->ITF_IClassFactory);
241
242     URLMON_LockModule();
243
244     return S_OK;
245 }
246
247
248 /***********************************************************************
249  *              DllRegisterServerEx (URLMON.@)
250  */
251 HRESULT WINAPI DllRegisterServerEx(void)
252 {
253     FIXME("(void): stub\n");
254
255     return E_FAIL;
256 }
257
258 /**************************************************************************
259  *                 UrlMkSetSessionOption (URLMON.@)
260  */
261 HRESULT WINAPI UrlMkSetSessionOption(DWORD dwOption, LPVOID pBuffer, DWORD dwBufferLength,
262                                         DWORD Reserved)
263 {
264     FIXME("(%#lx, %p, %#lx): stub\n", dwOption, pBuffer, dwBufferLength);
265
266     return S_OK;
267 }
268
269 /**************************************************************************
270  *                 UrlMkGetSessionOption (URLMON.@)
271  */
272 HRESULT WINAPI UrlMkGetSessionOption(DWORD dwOption, LPVOID pBuffer, DWORD dwBufferLength,
273                                         DWORD* pdwBufferLength, DWORD dwReserved)
274 {
275     FIXME("(%#lx, %p, %#lx, %p): stub\n", dwOption, pBuffer, dwBufferLength, pdwBufferLength);
276
277     return S_OK;
278 }
279
280 static const CHAR Agent[] = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)";
281
282 /**************************************************************************
283  *                 ObtainUserAgentString (URLMON.@)
284  */
285 HRESULT WINAPI ObtainUserAgentString(DWORD dwOption, LPSTR pcszUAOut, DWORD *cbSize)
286 {
287     FIXME("(%ld, %p, %p): stub\n", dwOption, pcszUAOut, cbSize);
288
289     if(dwOption) {
290       ERR("dwOption: %ld, must be zero\n", dwOption);
291     }
292
293     if (sizeof(Agent) < *cbSize)
294         *cbSize = sizeof(Agent);
295     lstrcpynA(pcszUAOut, Agent, *cbSize); 
296
297     return S_OK;
298 }
299
300 HRESULT WINAPI CoInternetCompareUrl(LPCWSTR pwzUrl1, LPCWSTR pwzUrl2, DWORD dwCompareFlags)
301 {
302     TRACE("(%s,%s,%08lx)\n", debugstr_w(pwzUrl1), debugstr_w(pwzUrl2), dwCompareFlags);
303     return UrlCompareW(pwzUrl1, pwzUrl2, dwCompareFlags)==0?S_OK:S_FALSE;
304 }
305
306 /**************************************************************************
307  *                 IsValidURL (URLMON.@)
308  * 
309  * Determines if a specified string is a valid URL.
310  *
311  * PARAMS
312  *  pBC        [I] ignored, must be NULL.
313  *  szURL      [I] string that represents the URL in question.
314  *  dwReserved [I] reserved and must be zero.
315  *
316  * RETURNS
317  *  Success: S_OK.
318  *  Failure: S_FALSE.
319  *  returns E_INVALIDARG if one or more of the args is invalid.
320  *
321  * TODO:
322  *  test functionality against windows to see what a valid URL is.
323  */
324 HRESULT WINAPI IsValidURL(LPBC pBC, LPCWSTR szURL, DWORD dwReserved)
325 {
326     FIXME("(%p, %s, %ld): stub\n", pBC, debugstr_w(szURL), dwReserved);
327     
328     if (pBC != NULL || dwReserved != 0)
329         return E_INVALIDARG;
330     
331     return S_OK;
332 }
333
334 /**************************************************************************
335  *                 FaultInIEFeature (URLMON.@)
336  *
337  *  Undocumented.  Appears to be used by native shdocvw.dll.
338  */
339 HRESULT WINAPI FaultInIEFeature( HWND hwnd, uCLSSPEC * pClassSpec,
340                                  QUERYCONTEXT *pQuery, DWORD flags )
341 {
342     FIXME("%p %p %p %08lx\n", hwnd, pClassSpec, pQuery, flags);
343     return E_NOTIMPL;
344 }
345
346 /**************************************************************************
347  *                 CoGetClassObjectFromURL (URLMON.@)
348  */
349 HRESULT WINAPI CoGetClassObjectFromURL( REFCLSID rclsid, LPCWSTR szCodeURL, DWORD dwFileVersionMS,
350                                         DWORD dwFileVersionLS, LPCWSTR szContentType,
351                                         LPBINDCTX pBindCtx, DWORD dwClsContext, LPVOID pvReserved,
352                                         REFIID riid, LPVOID *ppv )
353 {
354     FIXME("(%s %s %ld %ld %s %p %ld %p %s %p) Stub!\n", debugstr_guid(rclsid), debugstr_w(szCodeURL),
355         dwFileVersionMS, dwFileVersionLS, debugstr_w(szContentType), pBindCtx, dwClsContext, pvReserved,
356         debugstr_guid(riid), ppv);
357     return E_NOINTERFACE;
358 }
359
360 /***********************************************************************
361  *           ReleaseBindInfo (URLMON.@)
362  *
363  * Release the resources used by the specified BINDINFO structure.
364  *
365  * PARAMS
366  *  pbindinfo [I] BINDINFO to release.
367  *
368  * RETURNS
369  *  Nothing.
370  */
371 void WINAPI ReleaseBindInfo(BINDINFO* pbindinfo)
372 {
373     DWORD size;
374
375     TRACE("(%p)\n", pbindinfo);
376
377     if(!pbindinfo || !(size = pbindinfo->cbSize))
378         return;
379
380     CoTaskMemFree(pbindinfo->szExtraInfo);
381     ReleaseStgMedium(&pbindinfo->stgmedData);
382
383     if(offsetof(BINDINFO, szExtraInfo) < size)
384         CoTaskMemFree(pbindinfo->szCustomVerb);
385
386
387     if(pbindinfo->pUnk && offsetof(BINDINFO, pUnk) < size)
388         IUnknown_Release(pbindinfo->pUnk);
389
390     memset(pbindinfo, 0, size);
391     pbindinfo->cbSize = size;
392 }
393
394 /***********************************************************************
395  *           FindMimeFromData (URLMON.@)
396  *
397  * Determines the Multipurpose Internet Mail Extensions (MIME) type from the data provided.
398  */
399 static BOOL text_html_filter(LPVOID buf, DWORD size)
400 {
401     const char *b = buf;
402     int i;
403
404     if(size < 5)
405         return FALSE;
406
407     for(i=0; i < size-5; i++) {
408         if(b[i] == '<'
409            && (b[i+1] == 'h' || b[i+1] == 'H')
410            && (b[i+2] == 't' || b[i+2] == 'T')
411            && (b[i+3] == 'm' || b[i+3] == 'M')
412            && (b[i+4] == 'l' || b[i+4] == 'L'))
413             return TRUE;
414     }
415
416     return FALSE;
417 }
418
419 static BOOL text_plain_filter(LPVOID buf, DWORD size)
420 {
421     UCHAR *ptr;
422
423     for(ptr = buf; ptr < (UCHAR*)buf+size-1; ptr++) {
424         if(*ptr < 0x20 && *ptr != '\n' && *ptr != '\r' && *ptr != '\t')
425             return FALSE;
426     }
427
428     return TRUE;
429 }
430
431 static BOOL application_octet_stream_filter(LPVOID buf, DWORD size)
432 {
433     return TRUE;
434 }
435
436 HRESULT WINAPI FindMimeFromData(LPBC pBC, LPCWSTR pwzUrl, LPVOID pBuffer,
437         DWORD cbSize, LPCWSTR pwzMimeProposed, DWORD dwMimeFlags,
438         LPWSTR* ppwzMimeOut, DWORD dwReserved)
439 {
440     TRACE("(%p,%s,%p,%ld,%s,0x%lx,%p,0x%lx)\n", pBC, debugstr_w(pwzUrl), pBuffer, cbSize,
441             debugstr_w(pwzMimeProposed), dwMimeFlags, ppwzMimeOut, dwReserved);
442
443     if(dwMimeFlags)
444         WARN("dwMimeFlags=%08lx\n", dwMimeFlags);
445     if(dwReserved)
446         WARN("dwReserved=%ld\n", dwReserved);
447
448     /* pBC seams to not be used */
449
450     if(!ppwzMimeOut || (!pwzUrl && !pBuffer))
451         return E_INVALIDARG;
452
453     if(pwzMimeProposed && (!pwzUrl || !pBuffer || (pBuffer && !cbSize))) {
454         DWORD len;
455
456         if(!pwzMimeProposed)
457             return E_FAIL;
458
459         len = strlenW(pwzMimeProposed)+1;
460         *ppwzMimeOut = CoTaskMemAlloc(len*sizeof(WCHAR));
461         memcpy(*ppwzMimeOut, pwzMimeProposed, len*sizeof(WCHAR));
462         return S_OK;
463     }
464
465     if(pBuffer) {
466         DWORD len;
467         LPCWSTR ret = NULL;
468         int i = 0;
469
470         static const WCHAR wszTextHtml[] = {'t','e','x','t','/','h','t','m','l',0};
471         static const WCHAR wszTextPlain[] = {'t','e','x','t','/','p','l','a','i','n','\0'};
472         static const WCHAR wszAppOctetStream[] = {'a','p','p','l','i','c','a','t','i','o','n','/',
473             'o','c','t','e','t','-','s','t','r','e','a','m','\0'};
474
475         static const struct {
476             LPCWSTR mime;
477             BOOL (*filter)(LPVOID,DWORD);
478         } mime_filters[] = {
479             {wszTextHtml,       text_html_filter},
480             {wszTextPlain,      text_plain_filter},
481             {wszAppOctetStream, application_octet_stream_filter}
482         };
483
484         if(!cbSize)
485             return E_FAIL;
486
487         while(!ret) {
488             if(mime_filters[i].filter(pBuffer, cbSize))
489                 ret = mime_filters[i].mime;
490             i++;
491         }
492
493         len = strlenW(ret)+1;
494         *ppwzMimeOut = CoTaskMemAlloc(len*sizeof(WCHAR));
495         memcpy(*ppwzMimeOut, ret, len*sizeof(WCHAR));
496         return S_OK;
497     }
498
499     if(pwzUrl) {
500         HKEY hkey;
501         DWORD res, size;
502         LPCWSTR ptr;
503         WCHAR mime[64];
504
505         static const WCHAR wszContentType[] =
506                 {'C','o','n','t','e','n','t',' ','T','y','p','e','\0'};
507
508         ptr = strrchrW(pwzUrl, '.');
509         if(!ptr)
510             return E_FAIL;
511
512         res = RegOpenKeyW(HKEY_CLASSES_ROOT, ptr, &hkey);
513         if(res != ERROR_SUCCESS)
514             return E_FAIL;
515
516         size = sizeof(mime);
517         res = RegQueryValueExW(hkey, wszContentType, NULL, NULL, (LPBYTE)mime, &size);
518         RegCloseKey(hkey);
519         if(res != ERROR_SUCCESS)
520             return E_FAIL;
521
522         *ppwzMimeOut = CoTaskMemAlloc(size);
523         memcpy(*ppwzMimeOut, mime, size);
524         return S_OK;
525     }
526
527     return E_FAIL;
528 }