urlmon: Register protocol handlers in DllMain.
[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 static void init_session(BOOL);
48
49 /***********************************************************************
50  *              DllMain (URLMON.init)
51  */
52 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID fImpLoad)
53 {
54     TRACE("%p 0x%lx %p\n", hinstDLL, fdwReason, fImpLoad);
55
56     switch(fdwReason) {
57     case DLL_PROCESS_ATTACH:
58         DisableThreadLibraryCalls(hinstDLL);
59         URLMON_hInstance = hinstDLL;
60         init_session(TRUE);
61         break;
62
63     case DLL_PROCESS_DETACH:
64         if(urlmon_tls)
65             TlsFree(urlmon_tls);
66         init_session(FALSE);
67         URLMON_hInstance = 0;
68         break;
69     }
70     return TRUE;
71 }
72
73
74 /***********************************************************************
75  *              DllInstall (URLMON.@)
76  */
77 HRESULT WINAPI DllInstall(BOOL bInstall, LPCWSTR cmdline)
78 {
79   FIXME("(%s, %s): stub\n", bInstall?"TRUE":"FALSE",
80         debugstr_w(cmdline));
81
82   return S_OK;
83 }
84
85 /***********************************************************************
86  *              DllCanUnloadNow (URLMON.@)
87  */
88 HRESULT WINAPI DllCanUnloadNow(void)
89 {
90     return URLMON_refCount != 0 ? S_FALSE : S_OK;
91 }
92
93
94
95 /******************************************************************************
96  * Urlmon ClassFactory
97  */
98 typedef struct {
99     const IClassFactoryVtbl *lpClassFactoryVtbl;
100
101     HRESULT (*pfnCreateInstance)(IUnknown *pUnkOuter, LPVOID *ppObj);
102 } ClassFactory;
103
104 #define CLASSFACTORY(x)  ((IClassFactory*)  &(x)->lpClassFactoryVtbl)
105
106 static HRESULT WINAPI CF_QueryInterface(IClassFactory *iface, REFIID riid, LPVOID *ppv)
107 {
108     *ppv = NULL;
109
110     if(IsEqualGUID(riid, &IID_IUnknown)) {
111         TRACE("(%p)->(IID_IUnknown %p)\n", iface, ppv);
112         *ppv = iface;
113     }else if(IsEqualGUID(riid, &IID_IClassFactory)) {
114         TRACE("(%p)->(IID_IClassFactory %p)\n", iface, ppv);
115         *ppv = iface;
116     }
117
118     if(*ppv) {
119         IUnknown_AddRef((IUnknown*)*ppv);
120         return S_OK;
121     }
122
123     WARN("(%p)->(%s,%p),not found\n", iface, debugstr_guid(riid), ppv);
124     return E_NOINTERFACE;
125 }
126
127 static ULONG WINAPI CF_AddRef(IClassFactory *iface)
128 {
129     URLMON_LockModule();
130     return 2;
131 }
132
133 static ULONG WINAPI CF_Release(IClassFactory *iface)
134 {
135     URLMON_UnlockModule();
136     return 1;
137 }
138
139
140 static HRESULT WINAPI CF_CreateInstance(IClassFactory *iface, IUnknown *pOuter,
141                                         REFIID riid, LPVOID *ppobj)
142 {
143     ClassFactory *This = (ClassFactory*)iface;
144     HRESULT hres;
145     LPUNKNOWN punk;
146     
147     TRACE("(%p)->(%p,%s,%p)\n",This,pOuter,debugstr_guid(riid),ppobj);
148
149     *ppobj = NULL;
150     if(SUCCEEDED(hres = This->pfnCreateInstance(pOuter, (LPVOID *) &punk))) {
151         hres = IUnknown_QueryInterface(punk, riid, ppobj);
152         IUnknown_Release(punk);
153     }
154     return hres;
155 }
156
157 static HRESULT WINAPI CF_LockServer(LPCLASSFACTORY iface,BOOL dolock)
158 {
159     TRACE("(%d)\n", dolock);
160
161     if (dolock)
162            URLMON_LockModule();
163     else
164            URLMON_UnlockModule();
165
166     return S_OK;
167 }
168
169 static const IClassFactoryVtbl ClassFactoryVtbl =
170 {
171     CF_QueryInterface,
172     CF_AddRef,
173     CF_Release,
174     CF_CreateInstance,
175     CF_LockServer
176 };
177
178 static const ClassFactory FileProtocolCF =
179     { &ClassFactoryVtbl, FileProtocol_Construct};
180 static const ClassFactory FtpProtocolCF =
181     { &ClassFactoryVtbl, FtpProtocol_Construct};
182 static const ClassFactory HttpProtocolCF =
183     { &ClassFactoryVtbl, HttpProtocol_Construct};
184 static const ClassFactory SecurityManagerCF =
185     { &ClassFactoryVtbl, SecManagerImpl_Construct};
186 static const ClassFactory ZoneManagerCF =
187     { &ClassFactoryVtbl, ZoneMgrImpl_Construct};
188  
189 struct object_creation_info
190 {
191     const CLSID *clsid;
192     IClassFactory *cf;
193     LPCWSTR protocol;
194 };
195
196 static const WCHAR wszFile[] = {'f','i','l','e',0};
197 static const WCHAR wszFtp[]  = {'f','t','p',0};
198 static const WCHAR wszHttp[] = {'h','t','t','p',0};
199
200 static const struct object_creation_info object_creation[] =
201 {
202     { &CLSID_FileProtocol,            CLASSFACTORY(&FileProtocolCF),    wszFile },
203     { &CLSID_FtpProtocol,             CLASSFACTORY(&FtpProtocolCF),     wszFtp  },
204     { &CLSID_HttpProtocol,            CLASSFACTORY(&HttpProtocolCF),    wszHttp },
205     { &CLSID_InternetSecurityManager, CLASSFACTORY(&SecurityManagerCF), NULL    },
206     { &CLSID_InternetZoneManager,     CLASSFACTORY(&ZoneManagerCF),     NULL    }
207 };
208
209 static void init_session(BOOL init)
210 {
211     IInternetSession *session;
212     int i;
213
214     CoInternetGetSession(0, &session, 0);
215
216     for(i=0; i < sizeof(object_creation)/sizeof(object_creation[0]); i++) {
217         if(object_creation[i].protocol) {
218             if(init)
219                 IInternetSession_RegisterNameSpace(session, object_creation[i].cf,
220                         object_creation[i].clsid, object_creation[i].protocol, 0, NULL, 0);
221             else
222                 IInternetSession_UnregisterNameSpace(session, object_creation[i].cf,
223                         object_creation[i].protocol);
224         }
225     }
226
227     IInternetSession_Release(session);
228 }
229
230 /*******************************************************************************
231  * DllGetClassObject [URLMON.@]
232  * Retrieves class object from a DLL object
233  *
234  * NOTES
235  *    Docs say returns STDAPI
236  *
237  * PARAMS
238  *    rclsid [I] CLSID for the class object
239  *    riid   [I] Reference to identifier of interface for class object
240  *    ppv    [O] Address of variable to receive interface pointer for riid
241  *
242  * RETURNS
243  *    Success: S_OK
244  *    Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
245  *             E_UNEXPECTED
246  */
247
248 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
249 {
250     int i;
251     
252     TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
253     
254     for (i=0; i < sizeof(object_creation)/sizeof(object_creation[0]); i++)
255     {
256         if (IsEqualGUID(object_creation[i].clsid, rclsid))
257             return IClassFactory_QueryInterface(object_creation[i].cf, riid, ppv);
258     }
259
260     FIXME("%s: no class found.\n", debugstr_guid(rclsid));
261     return CLASS_E_CLASSNOTAVAILABLE;
262 }
263
264
265 /***********************************************************************
266  *              DllRegisterServerEx (URLMON.@)
267  */
268 HRESULT WINAPI DllRegisterServerEx(void)
269 {
270     FIXME("(void): stub\n");
271
272     return E_FAIL;
273 }
274
275 /**************************************************************************
276  *                 UrlMkSetSessionOption (URLMON.@)
277  */
278 HRESULT WINAPI UrlMkSetSessionOption(DWORD dwOption, LPVOID pBuffer, DWORD dwBufferLength,
279                                         DWORD Reserved)
280 {
281     FIXME("(%#lx, %p, %#lx): stub\n", dwOption, pBuffer, dwBufferLength);
282
283     return S_OK;
284 }
285
286 /**************************************************************************
287  *                 UrlMkGetSessionOption (URLMON.@)
288  */
289 HRESULT WINAPI UrlMkGetSessionOption(DWORD dwOption, LPVOID pBuffer, DWORD dwBufferLength,
290                                         DWORD* pdwBufferLength, DWORD dwReserved)
291 {
292     FIXME("(%#lx, %p, %#lx, %p): stub\n", dwOption, pBuffer, dwBufferLength, pdwBufferLength);
293
294     return S_OK;
295 }
296
297 static const CHAR Agent[] = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)";
298
299 /**************************************************************************
300  *                 ObtainUserAgentString (URLMON.@)
301  */
302 HRESULT WINAPI ObtainUserAgentString(DWORD dwOption, LPSTR pcszUAOut, DWORD *cbSize)
303 {
304     FIXME("(%ld, %p, %p): stub\n", dwOption, pcszUAOut, cbSize);
305
306     if(dwOption) {
307       ERR("dwOption: %ld, must be zero\n", dwOption);
308     }
309
310     if (sizeof(Agent) < *cbSize)
311         *cbSize = sizeof(Agent);
312     lstrcpynA(pcszUAOut, Agent, *cbSize); 
313
314     return S_OK;
315 }
316
317 HRESULT WINAPI CoInternetCompareUrl(LPCWSTR pwzUrl1, LPCWSTR pwzUrl2, DWORD dwCompareFlags)
318 {
319     TRACE("(%s,%s,%08lx)\n", debugstr_w(pwzUrl1), debugstr_w(pwzUrl2), dwCompareFlags);
320     return UrlCompareW(pwzUrl1, pwzUrl2, dwCompareFlags)==0?S_OK:S_FALSE;
321 }
322
323 /**************************************************************************
324  *                 IsValidURL (URLMON.@)
325  * 
326  * Determines if a specified string is a valid URL.
327  *
328  * PARAMS
329  *  pBC        [I] ignored, must be NULL.
330  *  szURL      [I] string that represents the URL in question.
331  *  dwReserved [I] reserved and must be zero.
332  *
333  * RETURNS
334  *  Success: S_OK.
335  *  Failure: S_FALSE.
336  *  returns E_INVALIDARG if one or more of the args is invalid.
337  *
338  * TODO:
339  *  test functionality against windows to see what a valid URL is.
340  */
341 HRESULT WINAPI IsValidURL(LPBC pBC, LPCWSTR szURL, DWORD dwReserved)
342 {
343     FIXME("(%p, %s, %ld): stub\n", pBC, debugstr_w(szURL), dwReserved);
344     
345     if (pBC != NULL || dwReserved != 0)
346         return E_INVALIDARG;
347     
348     return S_OK;
349 }
350
351 /**************************************************************************
352  *                 FaultInIEFeature (URLMON.@)
353  *
354  *  Undocumented.  Appears to be used by native shdocvw.dll.
355  */
356 HRESULT WINAPI FaultInIEFeature( HWND hwnd, uCLSSPEC * pClassSpec,
357                                  QUERYCONTEXT *pQuery, DWORD flags )
358 {
359     FIXME("%p %p %p %08lx\n", hwnd, pClassSpec, pQuery, flags);
360     return E_NOTIMPL;
361 }
362
363 /**************************************************************************
364  *                 CoGetClassObjectFromURL (URLMON.@)
365  */
366 HRESULT WINAPI CoGetClassObjectFromURL( REFCLSID rclsid, LPCWSTR szCodeURL, DWORD dwFileVersionMS,
367                                         DWORD dwFileVersionLS, LPCWSTR szContentType,
368                                         LPBINDCTX pBindCtx, DWORD dwClsContext, LPVOID pvReserved,
369                                         REFIID riid, LPVOID *ppv )
370 {
371     FIXME("(%s %s %ld %ld %s %p %ld %p %s %p) Stub!\n", debugstr_guid(rclsid), debugstr_w(szCodeURL),
372         dwFileVersionMS, dwFileVersionLS, debugstr_w(szContentType), pBindCtx, dwClsContext, pvReserved,
373         debugstr_guid(riid), ppv);
374     return E_NOINTERFACE;
375 }
376
377 /***********************************************************************
378  *           ReleaseBindInfo (URLMON.@)
379  *
380  * Release the resources used by the specified BINDINFO structure.
381  *
382  * PARAMS
383  *  pbindinfo [I] BINDINFO to release.
384  *
385  * RETURNS
386  *  Nothing.
387  */
388 void WINAPI ReleaseBindInfo(BINDINFO* pbindinfo)
389 {
390     DWORD size;
391
392     TRACE("(%p)\n", pbindinfo);
393
394     if(!pbindinfo || !(size = pbindinfo->cbSize))
395         return;
396
397     CoTaskMemFree(pbindinfo->szExtraInfo);
398     ReleaseStgMedium(&pbindinfo->stgmedData);
399
400     if(offsetof(BINDINFO, szExtraInfo) < size)
401         CoTaskMemFree(pbindinfo->szCustomVerb);
402
403
404     if(pbindinfo->pUnk && offsetof(BINDINFO, pUnk) < size)
405         IUnknown_Release(pbindinfo->pUnk);
406
407     memset(pbindinfo, 0, size);
408     pbindinfo->cbSize = size;
409 }
410
411 /***********************************************************************
412  *           FindMimeFromData (URLMON.@)
413  *
414  * Determines the Multipurpose Internet Mail Extensions (MIME) type from the data provided.
415  */
416 static BOOL text_html_filter(LPVOID buf, DWORD size)
417 {
418     const char *b = buf;
419     int i;
420
421     if(size < 5)
422         return FALSE;
423
424     for(i=0; i < size-5; i++) {
425         if(b[i] == '<'
426            && (b[i+1] == 'h' || b[i+1] == 'H')
427            && (b[i+2] == 't' || b[i+2] == 'T')
428            && (b[i+3] == 'm' || b[i+3] == 'M')
429            && (b[i+4] == 'l' || b[i+4] == 'L'))
430             return TRUE;
431     }
432
433     return FALSE;
434 }
435
436 static BOOL image_gif_filter(LPVOID buf, DWORD size)
437 {
438     const BYTE const *b = buf;
439
440     return size >= 6
441         && (b[0] == 'G' || b[0] == 'g')
442         && (b[1] == 'I' || b[1] == 'i')
443         && (b[2] == 'F' || b[2] == 'f')
444         &&  b[3] == '8'
445         && (b[4] == '7' || b[4] == '9')
446         && (b[5] == 'A' || b[5] == 'a');
447 }
448
449 static BOOL image_pjpeg_filter(LPVOID buf, DWORD size)
450 {
451     return size > 2 && *(BYTE*)buf == 0xff && *((BYTE*)buf+1) == 0xd8;
452 }
453
454 static BOOL image_xpng_filter(LPVOID buf, DWORD size)
455 {
456     static const BYTE xpng_header[] = {0x89,'P','N','G',0x0d,0x0a,0x1a,0x0a};
457     return size > sizeof(xpng_header) && !memcmp(buf, xpng_header, sizeof(xpng_header));
458 }
459
460 static BOOL image_bmp_filter(LPVOID buf, DWORD size)
461 {
462     return size >= 14
463         && *(BYTE*)buf == 0x42
464         && *((BYTE*)buf+1) == 0x4d
465         && *(DWORD*)((BYTE*)buf+6) == 0;
466 }
467
468 static BOOL text_plain_filter(LPVOID buf, DWORD size)
469 {
470     UCHAR *ptr;
471
472     for(ptr = buf; ptr < (UCHAR*)buf+size-1; ptr++) {
473         if(*ptr < 0x20 && *ptr != '\n' && *ptr != '\r' && *ptr != '\t')
474             return FALSE;
475     }
476
477     return TRUE;
478 }
479
480 static BOOL application_octet_stream_filter(LPVOID buf, DWORD size)
481 {
482     return TRUE;
483 }
484
485 HRESULT WINAPI FindMimeFromData(LPBC pBC, LPCWSTR pwzUrl, LPVOID pBuffer,
486         DWORD cbSize, LPCWSTR pwzMimeProposed, DWORD dwMimeFlags,
487         LPWSTR* ppwzMimeOut, DWORD dwReserved)
488 {
489     TRACE("(%p,%s,%p,%ld,%s,0x%lx,%p,0x%lx)\n", pBC, debugstr_w(pwzUrl), pBuffer, cbSize,
490             debugstr_w(pwzMimeProposed), dwMimeFlags, ppwzMimeOut, dwReserved);
491
492     if(dwMimeFlags)
493         WARN("dwMimeFlags=%08lx\n", dwMimeFlags);
494     if(dwReserved)
495         WARN("dwReserved=%ld\n", dwReserved);
496
497     /* pBC seams to not be used */
498
499     if(!ppwzMimeOut || (!pwzUrl && !pBuffer))
500         return E_INVALIDARG;
501
502     if(pwzMimeProposed && (!pBuffer || (pBuffer && !cbSize))) {
503         DWORD len;
504
505         if(!pwzMimeProposed)
506             return E_FAIL;
507
508         len = strlenW(pwzMimeProposed)+1;
509         *ppwzMimeOut = CoTaskMemAlloc(len*sizeof(WCHAR));
510         memcpy(*ppwzMimeOut, pwzMimeProposed, len*sizeof(WCHAR));
511         return S_OK;
512     }
513
514     if(pBuffer) {
515         DWORD len;
516         LPCWSTR ret = NULL;
517         int i;
518
519         static const WCHAR wszTextHtml[] = {'t','e','x','t','/','h','t','m','l',0};
520         static const WCHAR wszImageGif[] = {'i','m','a','g','e','/','g','i','f',0};
521         static const WCHAR wszImagePjpeg[] = {'i','m','a','g','e','/','p','j','p','e','g',0};
522         static const WCHAR wszImageXPng[] = {'i','m','a','g','e','/','x','-','p','n','g',0};
523         static const WCHAR wszImageBmp[] = {'i','m','a','g','e','/','b','m','p',0};
524         static const WCHAR wszTextPlain[] = {'t','e','x','t','/','p','l','a','i','n','\0'};
525         static const WCHAR wszAppOctetStream[] = {'a','p','p','l','i','c','a','t','i','o','n','/',
526             'o','c','t','e','t','-','s','t','r','e','a','m','\0'};
527
528         static const struct {
529             LPCWSTR mime;
530             BOOL (*filter)(LPVOID,DWORD);
531         } mime_filters[] = {
532             {wszTextHtml,       text_html_filter},
533             {wszImageGif,       image_gif_filter},
534             {wszImagePjpeg,     image_pjpeg_filter},
535             {wszImageXPng,      image_xpng_filter},
536             {wszImageBmp,       image_bmp_filter},
537             {wszTextPlain,      text_plain_filter},
538             {wszAppOctetStream, application_octet_stream_filter}
539         };
540
541         if(!cbSize)
542             return E_FAIL;
543
544         if(pwzMimeProposed && strcmpW(pwzMimeProposed, wszAppOctetStream)) {
545             for(i=0; i < sizeof(mime_filters)/sizeof(*mime_filters); i++) {
546                 if(!strcmpW(pwzMimeProposed, mime_filters[i].mime))
547                     break;
548             }
549
550             if(i == sizeof(mime_filters)/sizeof(*mime_filters)
551                     || mime_filters[i].filter(pBuffer, cbSize)) {
552                 len = strlenW(pwzMimeProposed)+1;
553                 *ppwzMimeOut = CoTaskMemAlloc(len*sizeof(WCHAR));
554                 memcpy(*ppwzMimeOut, pwzMimeProposed, len*sizeof(WCHAR));
555                 return S_OK;
556             }
557         }
558
559         i=0;
560         while(!ret) {
561             if(mime_filters[i].filter(pBuffer, cbSize))
562                 ret = mime_filters[i].mime;
563             i++;
564         }
565
566         if(pwzMimeProposed) {
567             if(i == sizeof(mime_filters)/sizeof(*mime_filters))
568                 ret = pwzMimeProposed;
569
570             /* text/html is a special case */
571            if(!strcmpW(pwzMimeProposed, wszTextHtml) && !strcmpW(ret, wszTextPlain))
572                 ret = wszTextHtml;
573         }
574
575         len = strlenW(ret)+1;
576         *ppwzMimeOut = CoTaskMemAlloc(len*sizeof(WCHAR));
577         memcpy(*ppwzMimeOut, ret, len*sizeof(WCHAR));
578         return S_OK;
579     }
580
581     if(pwzUrl) {
582         HKEY hkey;
583         DWORD res, size;
584         LPCWSTR ptr;
585         WCHAR mime[64];
586
587         static const WCHAR wszContentType[] =
588                 {'C','o','n','t','e','n','t',' ','T','y','p','e','\0'};
589
590         ptr = strrchrW(pwzUrl, '.');
591         if(!ptr)
592             return E_FAIL;
593
594         res = RegOpenKeyW(HKEY_CLASSES_ROOT, ptr, &hkey);
595         if(res != ERROR_SUCCESS)
596             return E_FAIL;
597
598         size = sizeof(mime);
599         res = RegQueryValueExW(hkey, wszContentType, NULL, NULL, (LPBYTE)mime, &size);
600         RegCloseKey(hkey);
601         if(res != ERROR_SUCCESS)
602             return E_FAIL;
603
604         *ppwzMimeOut = CoTaskMemAlloc(size);
605         memcpy(*ppwzMimeOut, mime, size);
606         return S_OK;
607     }
608
609     return E_FAIL;
610 }