Make our ascii strings static const.
[wine] / dlls / urlmon / umon.c
1 /*
2  * UrlMon
3  *
4  * Copyright 1999 Ulrich Czekalla for Corel Corporation
5  * Copyright 2002 Huw D M Davies for CodeWeavers
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #define COM_NO_WINDOWS_H
23 #include <stdarg.h>
24 #include <stdio.h>
25
26 #define NONAMELESSUNION
27 #define NONAMELESSSTRUCT
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winreg.h"
31 #include "winternl.h"
32 #include "winuser.h"
33 #include "objbase.h"
34 #include "wine/debug.h"
35 #include "wine/unicode.h"
36 #include "ole2.h"
37 #include "urlmon.h"
38 #include "wininet.h"
39 #include "shlwapi.h"
40 #include "urlmon_main.h"
41
42 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
43
44 /* native urlmon.dll uses this key, too */
45 static const WCHAR BSCBHolder[] = { '_','B','S','C','B','_','H','o','l','d','e','r','_',0 };
46
47 /*static BOOL registered_wndclass = FALSE;*/
48
49 /* filemoniker data structure */
50 typedef struct URLMonikerImpl{
51
52     ICOM_VTABLE(IMoniker)*  lpvtbl1;  /* VTable relative to the IMoniker interface.*/
53     ICOM_VTABLE(IBinding)*  lpvtbl2;  /* VTable to IBinding interface */
54
55     ULONG ref; /* reference counter for this object */
56
57     LPOLESTR URLName; /* URL string identified by this URLmoniker */
58
59     HWND hwndCallback;
60     IBindCtx *pBC;
61     HINTERNET hinternet, hconnect, hrequest;
62 } URLMonikerImpl;
63
64 /********************************************************************************/
65 /* URLMoniker prototype functions :                                            */
66
67 /* IUnknown prototype functions */
68 static HRESULT WINAPI URLMonikerImpl_QueryInterface(IMoniker* iface,REFIID riid,void** ppvObject);
69 static ULONG   WINAPI URLMonikerImpl_AddRef(IMoniker* iface);
70 static ULONG   WINAPI URLMonikerImpl_Release(IMoniker* iface);
71
72 /* IPersist prototype functions */
73 static HRESULT WINAPI URLMonikerImpl_GetClassID(IMoniker* iface, CLSID *pClassID);
74
75 /* IPersistStream prototype functions */
76 static HRESULT WINAPI URLMonikerImpl_IsDirty(IMoniker* iface);
77 static HRESULT WINAPI URLMonikerImpl_Load(IMoniker* iface, IStream* pStm);
78 static HRESULT WINAPI URLMonikerImpl_Save(IMoniker* iface, IStream* pStm, BOOL fClearDirty);
79 static HRESULT WINAPI URLMonikerImpl_GetSizeMax(IMoniker* iface, ULARGE_INTEGER* pcbSize);
80
81 /* IMoniker prototype functions */
82 static HRESULT WINAPI URLMonikerImpl_BindToObject(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, REFIID riid, VOID** ppvResult);
83 static HRESULT WINAPI URLMonikerImpl_BindToStorage(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, REFIID riid, VOID** ppvResult);
84 static HRESULT WINAPI URLMonikerImpl_Reduce(IMoniker* iface,IBindCtx* pbc, DWORD dwReduceHowFar,IMoniker** ppmkToLeft, IMoniker** ppmkReduced);
85 static HRESULT WINAPI URLMonikerImpl_ComposeWith(IMoniker* iface,IMoniker* pmkRight,BOOL fOnlyIfNotGeneric, IMoniker** ppmkComposite);
86 static HRESULT WINAPI URLMonikerImpl_Enum(IMoniker* iface,BOOL fForward, IEnumMoniker** ppenumMoniker);
87 static HRESULT WINAPI URLMonikerImpl_IsEqual(IMoniker* iface,IMoniker* pmkOtherMoniker);
88 static HRESULT WINAPI URLMonikerImpl_Hash(IMoniker* iface,DWORD* pdwHash);
89 static HRESULT WINAPI URLMonikerImpl_IsRunning(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, IMoniker* pmkNewlyRunning);
90 static HRESULT WINAPI URLMonikerImpl_GetTimeOfLastChange(IMoniker* iface, IBindCtx* pbc, IMoniker* pmkToLeft, FILETIME* pFileTime);
91 static HRESULT WINAPI URLMonikerImpl_Inverse(IMoniker* iface,IMoniker** ppmk);
92 static HRESULT WINAPI URLMonikerImpl_CommonPrefixWith(IMoniker* iface,IMoniker* pmkOther, IMoniker** ppmkPrefix);
93 static HRESULT WINAPI URLMonikerImpl_RelativePathTo(IMoniker* iface,IMoniker* pmOther, IMoniker** ppmkRelPath);
94 static HRESULT WINAPI URLMonikerImpl_GetDisplayName(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, LPOLESTR *ppszDisplayName);
95 static HRESULT WINAPI URLMonikerImpl_ParseDisplayName(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, LPOLESTR pszDisplayName, ULONG* pchEaten, IMoniker** ppmkOut);
96 static HRESULT WINAPI URLMonikerImpl_IsSystemMoniker(IMoniker* iface,DWORD* pwdMksys);
97
98 /* IBinding interface to moniker */
99 static HRESULT WINAPI URLMonikerImpl_IBinding_QueryInterface(IBinding* iface,REFIID riid,void** ppvObject);
100 static ULONG WINAPI URLMonikerImpl_IBinding_AddRef(IBinding* iface);
101 static ULONG WINAPI URLMonikerImpl_IBinding_Release(IBinding* iface);
102 static HRESULT WINAPI URLMonikerImpl_IBinding_Abort(IBinding* iface);
103 static HRESULT WINAPI URLMonikerImpl_IBinding_Suspend(IBinding* iface);
104 static HRESULT WINAPI URLMonikerImpl_IBinding_Resume(IBinding* iface);
105 static HRESULT WINAPI URLMonikerImpl_IBinding_SetPriority(IBinding* iface, LONG nPriority);
106 static HRESULT WINAPI URLMonikerImpl_IBinding_GetPriority(IBinding* iface, LONG* pnPriority);
107 static HRESULT WINAPI URLMonikerImpl_IBinding_GetBindResult(IBinding* iface, CLSID* pclsidProtocol, DWORD* pdwResult, LPOLESTR* pszResult, DWORD* pdwReserved);
108
109 /* Local function used by urlmoniker implementation */
110 static HRESULT URLMonikerImpl_Construct(URLMonikerImpl* iface, LPCOLESTR lpszLeftURL, LPCOLESTR lpszURL);
111 static HRESULT URLMonikerImpl_Destroy(URLMonikerImpl* iface);
112
113 /********************************************************************************/
114 /* Virtual function table for the URLMonikerImpl class which  include IPersist,*/
115 /* IPersistStream and IMoniker functions.                                       */
116 static ICOM_VTABLE(IMoniker) VT_URLMonikerImpl =
117 {
118     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
119     URLMonikerImpl_QueryInterface,
120     URLMonikerImpl_AddRef,
121     URLMonikerImpl_Release,
122     URLMonikerImpl_GetClassID,
123     URLMonikerImpl_IsDirty,
124     URLMonikerImpl_Load,
125     URLMonikerImpl_Save,
126     URLMonikerImpl_GetSizeMax,
127     URLMonikerImpl_BindToObject,
128     URLMonikerImpl_BindToStorage,
129     URLMonikerImpl_Reduce,
130     URLMonikerImpl_ComposeWith,
131     URLMonikerImpl_Enum,
132     URLMonikerImpl_IsEqual,
133     URLMonikerImpl_Hash,
134     URLMonikerImpl_IsRunning,
135     URLMonikerImpl_GetTimeOfLastChange,
136     URLMonikerImpl_Inverse,
137     URLMonikerImpl_CommonPrefixWith,
138     URLMonikerImpl_RelativePathTo,
139     URLMonikerImpl_GetDisplayName,
140     URLMonikerImpl_ParseDisplayName,
141     URLMonikerImpl_IsSystemMoniker
142 };
143
144 static ICOM_VTABLE(IBinding) VTBinding_URLMonikerImpl =
145 {
146     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
147     URLMonikerImpl_IBinding_QueryInterface,
148     URLMonikerImpl_IBinding_AddRef,
149     URLMonikerImpl_IBinding_Release,
150     URLMonikerImpl_IBinding_Abort,
151     URLMonikerImpl_IBinding_Suspend,
152     URLMonikerImpl_IBinding_Resume,
153     URLMonikerImpl_IBinding_SetPriority,
154     URLMonikerImpl_IBinding_GetPriority,
155     URLMonikerImpl_IBinding_GetBindResult
156 };
157
158
159 /*******************************************************************************
160  *        URLMoniker_QueryInterface
161  *******************************************************************************/
162 static HRESULT WINAPI URLMonikerImpl_QueryInterface(IMoniker* iface,REFIID riid,void** ppvObject)
163 {
164     ICOM_THIS(URLMonikerImpl,iface);
165
166     TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppvObject);
167
168     /* Perform a sanity check on the parameters.*/
169     if ( (This==0) || (ppvObject==0) )
170         return E_INVALIDARG;
171
172     /* Initialize the return parameter */
173     *ppvObject = 0;
174
175     /* Compare the riid with the interface IDs implemented by this object.*/
176     if (IsEqualIID(&IID_IUnknown, riid)      ||
177         IsEqualIID(&IID_IPersist, riid)      ||
178         IsEqualIID(&IID_IPersistStream,riid) ||
179         IsEqualIID(&IID_IMoniker, riid)
180        )
181         *ppvObject = iface;
182
183     /* Check that we obtained an interface.*/
184     if ((*ppvObject)==0)
185         return E_NOINTERFACE;
186
187     /* Query Interface always increases the reference count by one when it is successful */
188     URLMonikerImpl_AddRef(iface);
189
190     return S_OK;
191 }
192
193 /******************************************************************************
194  *        URLMoniker_AddRef
195  ******************************************************************************/
196 static ULONG WINAPI URLMonikerImpl_AddRef(IMoniker* iface)
197 {
198     ICOM_THIS(URLMonikerImpl,iface);
199
200     TRACE("(%p)\n",This);
201
202     return ++(This->ref);
203 }
204
205 /******************************************************************************
206  *        URLMoniker_Release
207  ******************************************************************************/
208 static ULONG WINAPI URLMonikerImpl_Release(IMoniker* iface)
209 {
210     ICOM_THIS(URLMonikerImpl,iface);
211
212     TRACE("(%p)\n",This);
213
214     This->ref--;
215
216     /* destroy the object if there's no more reference on it */
217     if (This->ref==0){
218
219         URLMonikerImpl_Destroy(This);
220
221         return 0;
222     }
223     return This->ref;
224 }
225
226 /******************************************************************************
227  *        URLMoniker_GetClassID
228  ******************************************************************************/
229 static HRESULT WINAPI URLMonikerImpl_GetClassID(IMoniker* iface,
230                                                 CLSID *pClassID)/* Pointer to CLSID of object */
231 {
232     ICOM_THIS(URLMonikerImpl,iface);
233
234     TRACE("(%p,%p)\n",This,pClassID);
235
236     if (pClassID==NULL)
237         return E_POINTER;
238     /* Windows always returns CLSID_StdURLMoniker */
239     *pClassID = CLSID_StdURLMoniker;
240     return S_OK;
241 }
242
243 /******************************************************************************
244  *        URLMoniker_IsDirty
245  ******************************************************************************/
246 static HRESULT WINAPI URLMonikerImpl_IsDirty(IMoniker* iface)
247 {
248     ICOM_THIS(URLMonikerImpl,iface);
249     /* Note that the OLE-provided implementations of the IPersistStream::IsDirty
250        method in the OLE-provided moniker interfaces always return S_FALSE because
251        their internal state never changes. */
252
253     TRACE("(%p)\n",This);
254
255     return S_FALSE;
256 }
257
258 /******************************************************************************
259  *        URLMoniker_Load
260  *
261  * NOTE
262  *  Writes a ULONG containing length of unicode string, followed
263  *  by that many unicode characters
264  ******************************************************************************/
265 static HRESULT WINAPI URLMonikerImpl_Load(IMoniker* iface,IStream* pStm)
266 {
267     ICOM_THIS(URLMonikerImpl,iface);
268     
269     HRESULT res;
270     ULONG len;
271     ULONG got;
272     TRACE("(%p,%p)\n",This,pStm);
273
274     if(!pStm)
275         return E_INVALIDARG;
276
277     res = IStream_Read(pStm, &len, sizeof(ULONG), &got);
278     if(SUCCEEDED(res)) {
279         if(got == sizeof(ULONG)) {
280             if(This->URLName)
281                 HeapFree(GetProcessHeap(), 0, This->URLName);
282             This->URLName=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(len+1));
283             if(!This->URLName)
284                 res = E_OUTOFMEMORY;
285             else {
286                 res = IStream_Read(pStm, This->URLName, len, NULL);
287                 This->URLName[len] = 0;
288             }
289         }
290         else
291             res = E_FAIL;
292     }
293     return res;
294 }
295
296 /******************************************************************************
297  *        URLMoniker_Save
298  ******************************************************************************/
299 static HRESULT WINAPI URLMonikerImpl_Save(IMoniker* iface,
300                                           IStream* pStm,/* pointer to the stream where the object is to be saved */
301                                           BOOL fClearDirty)/* Specifies whether to clear the dirty flag */
302 {
303     ICOM_THIS(URLMonikerImpl,iface);
304
305     HRESULT res;
306     ULONG len;
307     TRACE("(%p,%p,%d)\n",This,pStm,fClearDirty);
308
309     if(!pStm)
310         return E_INVALIDARG;
311
312     len = strlenW(This->URLName);
313     res=IStream_Write(pStm,&len,sizeof(ULONG),NULL);
314     if(SUCCEEDED(res))
315         res=IStream_Write(pStm,&This->URLName,len*sizeof(WCHAR),NULL);
316     return res;
317
318 }
319
320 /******************************************************************************
321  *        URLMoniker_GetSizeMax
322  ******************************************************************************/
323 static HRESULT WINAPI URLMonikerImpl_GetSizeMax(IMoniker* iface,
324                                                 ULARGE_INTEGER* pcbSize)/* Pointer to size of stream needed to save object */
325 {
326     ICOM_THIS(URLMonikerImpl,iface);
327
328     TRACE("(%p,%p)\n",This,pcbSize);
329
330     if(!pcbSize)
331         return E_INVALIDARG;
332
333     pcbSize->u.LowPart = sizeof(ULONG) + (strlenW(This->URLName) * sizeof(WCHAR));
334     pcbSize->u.HighPart = 0;
335     return S_OK;
336 }
337
338 /******************************************************************************
339  *         URLMoniker_Construct (local function)
340  *******************************************************************************/
341 static HRESULT URLMonikerImpl_Construct(URLMonikerImpl* This, LPCOLESTR lpszLeftURLName, LPCOLESTR lpszURLName)
342 {
343     HRESULT hres;
344     DWORD sizeStr;
345
346     TRACE("(%p,%s,%s)\n",This,debugstr_w(lpszLeftURLName),debugstr_w(lpszURLName));
347     memset(This, 0, sizeof(*This));
348
349     /* Initialize the virtual function table. */
350     This->lpvtbl1      = &VT_URLMonikerImpl;
351     This->lpvtbl2      = &VTBinding_URLMonikerImpl;
352     This->ref          = 0;
353
354     if(lpszLeftURLName) {
355         hres = UrlCombineW(lpszLeftURLName, lpszURLName, NULL, &sizeStr, 0);
356         if(FAILED(hres)) {
357             return hres;
358         }
359         sizeStr++;
360     }
361     else
362         sizeStr = lstrlenW(lpszURLName)+1;
363
364     This->URLName=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(sizeStr));
365
366     if (This->URLName==NULL)
367         return E_OUTOFMEMORY;
368
369     if(lpszLeftURLName) {
370         hres = UrlCombineW(lpszLeftURLName, lpszURLName, This->URLName, &sizeStr, 0);
371         if(FAILED(hres)) {
372             HeapFree(GetProcessHeap(), 0, This->URLName);
373             return hres;
374         }
375     }
376     else
377         strcpyW(This->URLName,lpszURLName);
378
379     return S_OK;
380 }
381
382
383
384 /******************************************************************************
385  *        URLMoniker_Destroy (local function)
386  *******************************************************************************/
387 static HRESULT URLMonikerImpl_Destroy(URLMonikerImpl* This)
388 {
389     TRACE("(%p)\n",This);
390
391     if (This->URLName!=NULL)
392             HeapFree(GetProcessHeap(),0,This->URLName);
393
394     HeapFree(GetProcessHeap(),0,This);
395
396     return S_OK;
397 }
398
399 /******************************************************************************
400  *                  URLMoniker_BindToObject
401  ******************************************************************************/
402 static HRESULT WINAPI URLMonikerImpl_BindToObject(IMoniker* iface,
403                                                   IBindCtx* pbc,
404                                                   IMoniker* pmkToLeft,
405                                                   REFIID riid,
406                                                   VOID** ppvResult)
407 {
408     ICOM_THIS(URLMonikerImpl,iface);
409
410     *ppvResult=0;
411
412     FIXME("(%p)->(%p,%p,%s,%p): stub\n",This,pbc,pmkToLeft,debugstr_guid(riid),
413           ppvResult);
414
415     return E_NOTIMPL;
416 }
417
418 typedef struct {
419     enum {OnProgress, OnDataAvailable} callback;
420 } URLMON_CallbackData;
421
422
423 #if 0
424 static LRESULT CALLBACK URLMON_WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
425 {
426     return DefWindowProcA(hwnd, msg, wparam, lparam);
427 }
428
429 static void PostOnProgress(URLMonikerImpl *This, UINT progress, UINT maxprogress, DWORD status, LPCWSTR *str)
430 {
431 }
432
433 static void CALLBACK URLMON_InternetCallback(HINTERNET hinet, /*DWORD_PTR*/ DWORD context, DWORD status,
434                                              void *status_info, DWORD status_info_len)
435 {
436     URLMonikerImpl *This = (URLMonikerImpl *)context;
437     TRACE("handle %p this %p status %08lx\n", hinet, This, status);
438
439     if(This->filesize == -1) {
440         switch(status) {
441         case INTERNET_STATUS_RESOLVING_NAME:
442             PostOnProgess(This, 0, 0, BINDSTATUS_FINDINGRESOURCE, status_info);
443             break;
444         case INTERNET_STATUS_CONNECTING_TO_SERVER:
445             PostOnProgress(This, 0, 0, BINDSTATUS_CONNECTING, NULL);
446             break;
447         case INTERNET_STATUS_SENDING_REQUEST:
448             PostOnProgress(This, 0, 0, BINDSTATUS_SENDINGREQUEST, NULL);
449             break;
450         case INTERNET_REQUEST_COMPLETE:
451           {
452               DWORD len, lensz = sizeof(len);
453
454             HttpQueryInfoW(hrequest, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER, &len, &lensz, NULL);
455             TRACE("res = %ld gle = %08lx url len = %ld\n", hres, GetLastError(), len);
456             This->filesize = len;
457             break;
458           }
459         }
460     }
461
462     return;
463 }
464 #endif
465 /******************************************************************************
466  *        URLMoniker_BindToStorage
467  ******************************************************************************/
468 static HRESULT WINAPI URLMonikerImpl_BindToStorage(IMoniker* iface,
469                                                    IBindCtx* pbc,
470                                                    IMoniker* pmkToLeft,
471                                                    REFIID riid,
472                                                    VOID** ppvObject)
473 {
474     ICOM_THIS(URLMonikerImpl,iface);
475     HRESULT hres;
476     IBindStatusCallback *pbscb;
477     BINDINFO bi;
478     DWORD bindf;
479
480     FIXME("(%p)->(%p,%p,%s,%p): stub\n",This,pbc,pmkToLeft,debugstr_guid(riid),ppvObject);
481     if(pmkToLeft) {
482         FIXME("pmkToLeft != NULL\n");
483         return E_NOTIMPL;
484     }
485     if(!IsEqualIID(&IID_IStream, riid)) {
486         FIXME("unsupported iid\n");
487         return E_NOTIMPL;
488     }
489
490     /* FIXME This is a bad hack (tm).  We should clearly download to a temporary file.
491        We also need to implement IStream ourselves so that IStream_Read can return
492        E_PENDING */
493
494     hres = CreateStreamOnHGlobal(0, TRUE, (IStream**)ppvObject);
495
496
497     if(SUCCEEDED(hres)) {
498         TRACE("Created dummy stream...\n");
499
500         hres = IBindCtx_GetObjectParam(pbc, (LPOLESTR)BSCBHolder, (IUnknown**)&pbscb);
501         if(SUCCEEDED(hres)) {
502             TRACE("Got IBindStatusCallback...\n");
503
504             memset(&bi, 0, sizeof(bi));
505             bi.cbSize = sizeof(bi);
506             bindf = 0;
507             hres = IBindStatusCallback_GetBindInfo(pbscb, &bindf, &bi);
508             if(SUCCEEDED(hres)) {
509                 URL_COMPONENTSW url;
510                 WCHAR *host, *path;
511                 DWORD len, lensz = sizeof(len), total_read = 0;
512                 LARGE_INTEGER last_read_pos;
513                 FORMATETC fmt;
514                 STGMEDIUM stg;
515
516                 TRACE("got bindinfo. bindf = %08lx extrainfo = %s bindinfof = %08lx bindverb = %08lx iid %s\n",
517                       bindf, debugstr_w(bi.szExtraInfo), bi.grfBindInfoF, bi.dwBindVerb, debugstr_guid(&bi.iid));
518                 hres = IBindStatusCallback_OnStartBinding(pbscb, 0, (IBinding*)&This->lpvtbl2);
519                 TRACE("OnStartBinding rets %08lx\n", hres);
520
521 #if 0
522                 if(!registered_wndclass) {
523                     WNDCLASSA urlmon_wndclass = {0, URLMON_WndProc,0, 0, URLMON_hInstance, 0, 0, 0, NULL, "URLMON_Callback_Window_Class"};
524                     RegisterClassA(&urlmon_wndclass);
525                     registered_wndclass = TRUE;
526                 }
527
528                 This->hwndCallback = CreateWindowA("URLMON_Callback_Window_Class", NULL, 0, 0, 0, 0, 0, 0, 0,
529                                                    URLMON_hInstance, NULL);
530
531 #endif
532                 memset(&url, 0, sizeof(url));
533                 url.dwStructSize = sizeof(url);
534                 url.dwSchemeLength = url.dwHostNameLength = url.dwUrlPathLength = 1;
535                 InternetCrackUrlW(This->URLName, 0, 0, &url);
536                 host = HeapAlloc(GetProcessHeap(), 0, (url.dwHostNameLength + 1) * sizeof(WCHAR));
537                 memcpy(host, url.lpszHostName, url.dwHostNameLength * sizeof(WCHAR));
538                 host[url.dwHostNameLength] = '\0';
539                 path = HeapAlloc(GetProcessHeap(), 0, (url.dwUrlPathLength + 1) * sizeof(WCHAR));
540                 memcpy(path, url.lpszUrlPath, url.dwUrlPathLength * sizeof(WCHAR));
541                 path[url.dwUrlPathLength] = '\0';
542
543                 This->hinternet = InternetOpenA("User Agent", 0, NULL, NULL, 0 /*INTERNET_FLAG_ASYNC*/);
544 /*              InternetSetStatusCallback(This->hinternet, URLMON_InternetCallback);*/
545
546                 This->hconnect = InternetConnectW(This->hinternet, host, INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
547                                             INTERNET_SERVICE_HTTP, 0, (DWORD)This);
548                 This->hrequest = HttpOpenRequestW(This->hconnect, NULL, path, NULL, NULL, NULL, 0, (DWORD)This);
549
550                 hres = IBindStatusCallback_OnProgress(pbscb, 0, 0, 0x22, NULL);
551                 hres = IBindStatusCallback_OnProgress(pbscb, 0, 0, BINDSTATUS_FINDINGRESOURCE, NULL);
552                 hres = IBindStatusCallback_OnProgress(pbscb, 0, 0, BINDSTATUS_CONNECTING, NULL);
553                 hres = IBindStatusCallback_OnProgress(pbscb, 0, 0, BINDSTATUS_SENDINGREQUEST, NULL);
554                 hres = E_OUTOFMEMORY; /* FIXME */
555                 if(HttpSendRequestW(This->hrequest, NULL, 0, NULL, 0)) {
556
557                     len = 0;
558                     HttpQueryInfoW(This->hrequest, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER, &len, &lensz, NULL);
559
560                     TRACE("res = %ld gle = %08lx url len = %ld\n", hres, GetLastError(), len);
561
562                     last_read_pos.u.LowPart = last_read_pos.u.HighPart = 0;
563                     fmt.cfFormat = 0;
564                     fmt.ptd = NULL;
565                     fmt.dwAspect = 0;
566                     fmt.lindex = -1;
567                     fmt.tymed = TYMED_ISTREAM;
568                     stg.tymed = TYMED_ISTREAM;
569                     stg.u.pstm = *(IStream**)ppvObject;
570                     stg.pUnkForRelease = NULL;
571
572                     while(1) {
573                         char buf[4096];
574                         DWORD bufread;
575                         DWORD written;
576                         if(InternetReadFile(This->hrequest, buf, sizeof(buf), &bufread)) {
577                             TRACE("read %ld bytes %s...\n", bufread, debugstr_an(buf, 10));
578                             if(bufread == 0) break;
579                             IStream_Write(*(IStream**)ppvObject, buf, bufread, &written);
580                             total_read += bufread;
581                             IStream_Seek(*(IStream**)ppvObject, last_read_pos, STREAM_SEEK_SET, NULL);
582                             hres = IBindStatusCallback_OnProgress(pbscb, total_read, len, (total_read == bufread) ?
583                                                                   BINDSTATUS_BEGINDOWNLOADDATA :
584                                                                   BINDSTATUS_DOWNLOADINGDATA, NULL);
585                             hres = IBindStatusCallback_OnDataAvailable(pbscb,
586                                                                        (total_read == bufread) ? BSCF_FIRSTDATANOTIFICATION :
587                                                                        BSCF_INTERMEDIATEDATANOTIFICATION,
588                                                                        total_read, &fmt, &stg);
589                             last_read_pos.u.LowPart += bufread; /* FIXME */
590                         } else
591                             break;
592                     }
593                     hres = IBindStatusCallback_OnProgress(pbscb, total_read, len, BINDSTATUS_ENDDOWNLOADDATA, NULL);
594                     hres = IBindStatusCallback_OnDataAvailable(pbscb, BSCF_LASTDATANOTIFICATION, total_read, &fmt, &stg);
595                     TRACE("OnDataAvail rets %08lx\n", hres);
596                     hres = IBindStatusCallback_OnStopBinding(pbscb, S_OK, NULL);
597                     TRACE("OnStop rets %08lx\n", hres);
598                     hres = S_OK;
599                 }
600                 InternetCloseHandle(This->hrequest);
601                 InternetCloseHandle(This->hconnect);
602                 InternetCloseHandle(This->hinternet);
603             }
604         }
605     }
606     return hres;
607 }
608
609 /******************************************************************************
610  *        URLMoniker_Reduce
611  ******************************************************************************/
612 static HRESULT WINAPI URLMonikerImpl_Reduce(IMoniker* iface,
613                                             IBindCtx* pbc,
614                                             DWORD dwReduceHowFar,
615                                             IMoniker** ppmkToLeft,
616                                             IMoniker** ppmkReduced)
617 {
618     ICOM_THIS(URLMonikerImpl,iface);
619     
620     TRACE("(%p,%p,%ld,%p,%p)\n",This,pbc,dwReduceHowFar,ppmkToLeft,ppmkReduced);
621
622     if(!ppmkReduced)
623         return E_INVALIDARG;
624
625     URLMonikerImpl_AddRef(iface);
626     *ppmkReduced = iface;
627     return MK_S_REDUCED_TO_SELF;
628 }
629
630 /******************************************************************************
631  *        URLMoniker_ComposeWith
632  ******************************************************************************/
633 static HRESULT WINAPI URLMonikerImpl_ComposeWith(IMoniker* iface,
634                                                  IMoniker* pmkRight,
635                                                  BOOL fOnlyIfNotGeneric,
636                                                  IMoniker** ppmkComposite)
637 {
638     ICOM_THIS(URLMonikerImpl,iface);
639     FIXME("(%p)->(%p,%d,%p): stub\n",This,pmkRight,fOnlyIfNotGeneric,ppmkComposite);
640
641     return E_NOTIMPL;
642 }
643
644 /******************************************************************************
645  *        URLMoniker_Enum
646  ******************************************************************************/
647 static HRESULT WINAPI URLMonikerImpl_Enum(IMoniker* iface,BOOL fForward, IEnumMoniker** ppenumMoniker)
648 {
649     ICOM_THIS(URLMonikerImpl,iface);
650     TRACE("(%p,%d,%p)\n",This,fForward,ppenumMoniker);
651
652     if(!ppenumMoniker)
653         return E_INVALIDARG;
654
655     /* Does not support sub-monikers */
656     *ppenumMoniker = NULL;
657     return S_OK;
658 }
659
660 /******************************************************************************
661  *        URLMoniker_IsEqual
662  ******************************************************************************/
663 static HRESULT WINAPI URLMonikerImpl_IsEqual(IMoniker* iface,IMoniker* pmkOtherMoniker)
664 {
665     ICOM_THIS(URLMonikerImpl,iface);
666     CLSID clsid;
667     LPOLESTR urlPath;
668     IBindCtx* bind;
669     HRESULT res;
670
671     TRACE("(%p,%p)\n",This,pmkOtherMoniker);
672
673     if(pmkOtherMoniker==NULL)
674         return E_INVALIDARG;
675
676     IMoniker_GetClassID(pmkOtherMoniker,&clsid);
677
678     if(!IsEqualCLSID(&clsid,&CLSID_StdURLMoniker))
679         return S_FALSE;
680
681     res = CreateBindCtx(0,&bind);
682     if(FAILED(res))
683         return res;
684
685     res = S_FALSE;
686     if(SUCCEEDED(IMoniker_GetDisplayName(pmkOtherMoniker,bind,NULL,&urlPath))) {
687         int result = lstrcmpiW(urlPath, This->URLName);
688         CoTaskMemFree(urlPath);
689         if(result == 0)
690             res = S_OK;
691     }
692     IUnknown_Release(bind);
693     return res;
694 }
695
696
697 /******************************************************************************
698  *        URLMoniker_Hash
699  ******************************************************************************/
700 static HRESULT WINAPI URLMonikerImpl_Hash(IMoniker* iface,DWORD* pdwHash)
701 {
702     ICOM_THIS(URLMonikerImpl,iface);
703     
704     int  h = 0,i,skip,len;
705     int  off = 0;
706     LPOLESTR val;
707
708     TRACE("(%p,%p)\n",This,pdwHash);
709
710     if(!pdwHash)
711         return E_INVALIDARG;
712
713     val = This->URLName;
714     len = lstrlenW(val);
715
716     if(len < 16) {
717         for(i = len ; i > 0; i--) {
718             h = (h * 37) + val[off++];
719         }
720     }
721     else {
722         /* only sample some characters */
723         skip = len / 8;
724         for(i = len; i > 0; i -= skip, off += skip) {
725             h = (h * 39) + val[off];
726         }
727     }
728     *pdwHash = h;
729     return S_OK;
730 }
731
732 /******************************************************************************
733  *        URLMoniker_IsRunning
734  ******************************************************************************/
735 static HRESULT WINAPI URLMonikerImpl_IsRunning(IMoniker* iface,
736                                                IBindCtx* pbc,
737                                                IMoniker* pmkToLeft,
738                                                IMoniker* pmkNewlyRunning)
739 {
740     ICOM_THIS(URLMonikerImpl,iface);
741     FIXME("(%p)->(%p,%p,%p): stub\n",This,pbc,pmkToLeft,pmkNewlyRunning);
742
743     return E_NOTIMPL;
744 }
745
746 /******************************************************************************
747  *        URLMoniker_GetTimeOfLastChange
748  ******************************************************************************/
749 static HRESULT WINAPI URLMonikerImpl_GetTimeOfLastChange(IMoniker* iface,
750                                                          IBindCtx* pbc,
751                                                          IMoniker* pmkToLeft,
752                                                          FILETIME* pFileTime)
753 {
754     ICOM_THIS(URLMonikerImpl,iface);
755     FIXME("(%p)->(%p,%p,%p): stub\n",This,pbc,pmkToLeft,pFileTime);
756
757     return E_NOTIMPL;
758 }
759
760 /******************************************************************************
761  *        URLMoniker_Inverse
762  ******************************************************************************/
763 static HRESULT WINAPI URLMonikerImpl_Inverse(IMoniker* iface,IMoniker** ppmk)
764 {
765     ICOM_THIS(URLMonikerImpl,iface);
766     TRACE("(%p,%p)\n",This,ppmk);
767
768     return MK_E_NOINVERSE;
769 }
770
771 /******************************************************************************
772  *        URLMoniker_CommonPrefixWith
773  ******************************************************************************/
774 static HRESULT WINAPI URLMonikerImpl_CommonPrefixWith(IMoniker* iface,IMoniker* pmkOther,IMoniker** ppmkPrefix)
775 {
776     ICOM_THIS(URLMonikerImpl,iface);
777     FIXME("(%p)->(%p,%p): stub\n",This,pmkOther,ppmkPrefix);
778
779     return E_NOTIMPL;
780 }
781
782 /******************************************************************************
783  *        URLMoniker_RelativePathTo
784  ******************************************************************************/
785 static HRESULT WINAPI URLMonikerImpl_RelativePathTo(IMoniker* iface,IMoniker* pmOther, IMoniker** ppmkRelPath)
786 {
787     ICOM_THIS(URLMonikerImpl,iface);
788     FIXME("(%p)->(%p,%p): stub\n",This,pmOther,ppmkRelPath);
789
790     return E_NOTIMPL;
791 }
792
793 /******************************************************************************
794  *        URLMoniker_GetDisplayName
795  ******************************************************************************/
796 static HRESULT WINAPI URLMonikerImpl_GetDisplayName(IMoniker* iface,
797                                                     IBindCtx* pbc,
798                                                     IMoniker* pmkToLeft,
799                                                     LPOLESTR *ppszDisplayName)
800 {
801     ICOM_THIS(URLMonikerImpl,iface);
802     
803     int len;
804     
805     TRACE("(%p,%p,%p,%p)\n",This,pbc,pmkToLeft,ppszDisplayName);
806     
807     if(!ppszDisplayName)
808         return E_INVALIDARG;
809     
810     /* FIXME: If this is a partial URL, try and get a URL moniker from SZ_URLCONTEXT in the bind context,
811         then look at pmkToLeft to try and complete the URL
812     */
813     len = lstrlenW(This->URLName)+1;
814     *ppszDisplayName = CoTaskMemAlloc(len*sizeof(WCHAR));
815     if(!*ppszDisplayName)
816         return E_OUTOFMEMORY;
817     lstrcpyW(*ppszDisplayName, This->URLName);
818     return S_OK;
819 }
820
821 /******************************************************************************
822  *        URLMoniker_ParseDisplayName
823  ******************************************************************************/
824 static HRESULT WINAPI URLMonikerImpl_ParseDisplayName(IMoniker* iface,
825                                                       IBindCtx* pbc,
826                                                       IMoniker* pmkToLeft,
827                                                       LPOLESTR pszDisplayName,
828                                                       ULONG* pchEaten,
829                                                       IMoniker** ppmkOut)
830 {
831     ICOM_THIS(URLMonikerImpl,iface);
832     FIXME("(%p)->(%p,%p,%p,%p,%p): stub\n",This,pbc,pmkToLeft,pszDisplayName,pchEaten,ppmkOut);
833
834     return E_NOTIMPL;
835 }
836
837 /******************************************************************************
838  *        URLMoniker_IsSystemMoniker
839  ******************************************************************************/
840 static HRESULT WINAPI URLMonikerImpl_IsSystemMoniker(IMoniker* iface,DWORD* pwdMksys)
841 {
842     ICOM_THIS(URLMonikerImpl,iface);
843     TRACE("(%p,%p)\n",This,pwdMksys);
844
845     if(!pwdMksys)
846         return E_INVALIDARG;
847
848     *pwdMksys = MKSYS_URLMONIKER;
849     return S_OK;
850 }
851
852 static HRESULT WINAPI URLMonikerImpl_IBinding_QueryInterface(IBinding* iface,REFIID riid,void** ppvObject)
853 {
854     ICOM_THIS_MULTI(URLMonikerImpl, lpvtbl2, iface);
855
856     TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppvObject);
857
858     /* Perform a sanity check on the parameters.*/
859     if ( (This==0) || (ppvObject==0) )
860         return E_INVALIDARG;
861
862     /* Initialize the return parameter */
863     *ppvObject = 0;
864
865     /* Compare the riid with the interface IDs implemented by this object.*/
866     if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IBinding, riid))
867         *ppvObject = iface;
868
869     /* Check that we obtained an interface.*/
870     if ((*ppvObject)==0)
871         return E_NOINTERFACE;
872
873     /* Query Interface always increases the reference count by one when it is successful */
874     URLMonikerImpl_IBinding_AddRef(iface);
875
876     return S_OK;
877
878 }
879
880 static ULONG WINAPI URLMonikerImpl_IBinding_AddRef(IBinding* iface)
881 {
882     ICOM_THIS_MULTI(URLMonikerImpl, lpvtbl2, iface);
883     TRACE("(%p)\n",This);
884
885     return URLMonikerImpl_AddRef((IMoniker*)This);
886 }
887
888 static ULONG WINAPI URLMonikerImpl_IBinding_Release(IBinding* iface)
889 {
890     ICOM_THIS_MULTI(URLMonikerImpl, lpvtbl2, iface);
891     TRACE("(%p)\n",This);
892
893     return URLMonikerImpl_Release((IMoniker*)This);
894 }
895
896 static HRESULT WINAPI URLMonikerImpl_IBinding_Abort(IBinding* iface)
897 {
898     ICOM_THIS_MULTI(URLMonikerImpl, lpvtbl2, iface);
899     FIXME("(%p): stub\n", This);
900
901     return E_NOTIMPL;
902 }
903
904 static HRESULT WINAPI URLMonikerImpl_IBinding_GetBindResult(IBinding* iface, CLSID* pclsidProtocol, DWORD* pdwResult, LPOLESTR* pszResult, DWORD* pdwReserved)
905 {
906     ICOM_THIS_MULTI(URLMonikerImpl, lpvtbl2, iface);
907     FIXME("(%p)->(%s, %p, %p, %p): stub\n", This, debugstr_guid(pclsidProtocol), pdwResult, pszResult, pdwReserved);
908
909     return E_NOTIMPL;
910 }
911
912 static HRESULT WINAPI URLMonikerImpl_IBinding_GetPriority(IBinding* iface, LONG* pnPriority)
913 {
914     ICOM_THIS_MULTI(URLMonikerImpl, lpvtbl2, iface);
915     FIXME("(%p)->(%p): stub\n", This, pnPriority);
916
917     return E_NOTIMPL;
918 }
919
920 static HRESULT WINAPI URLMonikerImpl_IBinding_Resume(IBinding* iface)
921 {
922     ICOM_THIS_MULTI(URLMonikerImpl, lpvtbl2, iface);
923     FIXME("(%p): stub\n", This);
924
925     return E_NOTIMPL;
926 }
927
928 static HRESULT WINAPI URLMonikerImpl_IBinding_SetPriority(IBinding* iface, LONG nPriority)
929 {
930     ICOM_THIS_MULTI(URLMonikerImpl, lpvtbl2, iface);
931     FIXME("(%p)->(%ld): stub\n", This, nPriority);
932
933     return E_NOTIMPL;
934 }
935
936 static HRESULT WINAPI URLMonikerImpl_IBinding_Suspend(IBinding* iface)
937 {
938     ICOM_THIS_MULTI(URLMonikerImpl, lpvtbl2, iface);
939     FIXME("(%p): stub\n", This);
940
941     return E_NOTIMPL;
942 }
943
944 /***********************************************************************
945  *           CreateAsyncBindCtx (URLMON.@)
946  */
947 HRESULT WINAPI CreateAsyncBindCtx(DWORD reserved, IBindStatusCallback *callback,
948     IEnumFORMATETC *format, IBindCtx **pbind)
949 {
950     FIXME("stub.\n");
951     return E_INVALIDARG;
952 }
953 /***********************************************************************
954  *           CreateAsyncBindCtxEx (URLMON.@)
955  *
956  * Create an asynchronous bind context.
957  *
958  * FIXME
959  *   Not implemented.
960  */
961 HRESULT WINAPI CreateAsyncBindCtxEx(IBindCtx *ibind, DWORD options,
962     IBindStatusCallback *callback, IEnumFORMATETC *format, IBindCtx** pbind,
963     DWORD reserved)
964 {
965      FIXME("stub, returns failure\n");
966      return E_INVALIDARG;
967 }
968
969
970 /***********************************************************************
971  *           CreateURLMoniker (URLMON.@)
972  *
973  * Create a url moniker.
974  *
975  * PARAMS
976  *    pmkContext [I] Context
977  *    szURL      [I] Url to create the moniker for
978  *    ppmk       [O] Destination for created moniker.
979  *
980  * RETURNS
981  *    Success: S_OK. ppmk contains the created IMoniker object.
982  *    Failure: MK_E_SYNTAX if szURL is not a valid url, or
983  *             E_OUTOFMEMORY if memory allocation fails.
984  */
985 HRESULT WINAPI CreateURLMoniker(IMoniker *pmkContext, LPCWSTR szURL, IMoniker **ppmk)
986 {
987     URLMonikerImpl *obj;
988     HRESULT hres;
989     IID iid = IID_IMoniker;
990     LPOLESTR lefturl = NULL;
991
992     TRACE("(%p, %s, %p)\n", pmkContext, debugstr_w(szURL), ppmk);
993
994     if(!(obj = HeapAlloc(GetProcessHeap(), 0, sizeof(*obj))))
995         return E_OUTOFMEMORY;
996
997     if(pmkContext) {
998         CLSID clsid;
999         IBindCtx* bind;
1000         IMoniker_GetClassID(pmkContext, &clsid);
1001         if(IsEqualCLSID(&clsid, &CLSID_StdURLMoniker) && SUCCEEDED(CreateBindCtx(0, &bind))) {
1002             URLMonikerImpl_GetDisplayName(pmkContext, bind, NULL, &lefturl);
1003             IBindCtx_Release(bind);
1004         }
1005     }
1006         
1007     hres = URLMonikerImpl_Construct(obj, lefturl, szURL);
1008     CoTaskMemFree(lefturl);
1009     if(SUCCEEDED(hres))
1010         hres = URLMonikerImpl_QueryInterface((IMoniker*)obj, &iid, (void**)ppmk);
1011     else
1012         HeapFree(GetProcessHeap(), 0, obj);
1013     return hres;
1014 }
1015
1016
1017 /***********************************************************************
1018  *           CoInternetGetSession (URLMON.@)
1019  *
1020  * Create a new internet session and return an IInternetSession interface
1021  * representing it.
1022  *
1023  * PARAMS
1024  *    dwSessionMode      [I] Mode for the internet session
1025  *    ppIInternetSession [O] Destination for creates IInternetSession object
1026  *    dwReserved         [I] Reserved, must be 0.
1027  *
1028  * RETURNS
1029  *    Success: S_OK. ppIInternetSession contains the IInternetSession interface.
1030  *    Failure: E_INVALIDARG, if any argument is invalid, or
1031  *             E_OUTOFMEMORY if memory allocation fails.
1032  */
1033 HRESULT WINAPI CoInternetGetSession(DWORD dwSessionMode, /*IInternetSession*/void **ppIInternetSession, DWORD dwReserved)
1034 {
1035     FIXME("(%ld, %p, %ld): stub\n", dwSessionMode, ppIInternetSession, dwReserved);
1036
1037     if(dwSessionMode) {
1038       ERR("dwSessionMode: %ld, must be zero\n", dwSessionMode);
1039     }
1040
1041     if(dwReserved) {
1042       ERR("dwReserved: %ld, must be zero\n", dwReserved);
1043     }
1044
1045     *ppIInternetSession=NULL;
1046     return E_OUTOFMEMORY;
1047 }
1048
1049 /***********************************************************************
1050  *           CoInternetQueryInfo (URLMON.@)
1051  *
1052  * Retrieves information relevant to a specified URL
1053  *
1054  * RETURNS
1055  *    S_OK                      success
1056  *    S_FALSE                   buffer too small
1057  *    INET_E_QUERYOPTIONUNKNOWN invalid option
1058  *
1059  */
1060 HRESULT WINAPI CoInternetQueryInfo(LPCWSTR pwzUrl, QUERYOPTION QueryOption,
1061   DWORD dwQueryFlags, LPVOID pvBuffer, DWORD cbBuffer, DWORD * pcbBuffer,
1062   DWORD dwReserved)
1063 {
1064   FIXME("(%s, %x, %lx, %p, %lx, %p, %lx): stub\n", debugstr_w(pwzUrl),
1065     QueryOption, dwQueryFlags, pvBuffer, cbBuffer, pcbBuffer, dwReserved);
1066   return S_OK;
1067 }
1068
1069 static BOOL URLMON_IsBinary(LPVOID pBuffer, DWORD cbSize)
1070 {
1071     int binarycount = 0;
1072     int i;
1073     unsigned char *buff = pBuffer;
1074     for(i=0; i<cbSize; i++) {
1075         if(buff[i] < 32)
1076             binarycount++;
1077     }
1078     return binarycount > (cbSize-binarycount);
1079 }
1080
1081 /***********************************************************************
1082  *           FindMimeFromData (URLMON.@)
1083  *
1084  * Determines the Multipurpose Internet Mail Extensions (MIME) type from the data provided.
1085  *
1086  * NOTE
1087  *  See http://msdn.microsoft.com/workshop/networking/moniker/overview/appendix_a.asp
1088  */
1089 HRESULT WINAPI FindMimeFromData(LPBC pBC, LPCWSTR pwzUrl, LPVOID pBuffer,
1090    DWORD cbSize, LPCWSTR pwzMimeProposed, DWORD dwMimeFlags,
1091    LPWSTR* ppwzMimeOut, DWORD dwReserved)
1092 {
1093     static const WCHAR szBinaryMime[] = {'a','p','p','l','i','c','a','t','i','o','n','/','o','c','t','e','t','-','s','t','r','e','a','m','\0'};
1094     static const WCHAR szTextMime[] = {'t','e','x','t','/','p','l','a','i','n','\0'};
1095     static const WCHAR szContentType[] = {'C','o','n','t','e','n','t',' ','T','y','p','e','\0'};
1096     WCHAR szTmpMime[256];
1097     LPCWSTR mimeType = NULL;
1098     HKEY hKey = NULL;
1099
1100     TRACE("(%p,%s,%p,%ld,%s,0x%lx,%p,0x%lx)\n", pBC, debugstr_w(pwzUrl), pBuffer, cbSize,
1101           debugstr_w(pwzMimeProposed), dwMimeFlags, ppwzMimeOut, dwReserved);
1102
1103     if((!pwzUrl && (!pBuffer || cbSize <= 0)) || !ppwzMimeOut)
1104         return E_INVALIDARG;
1105
1106     if(pwzMimeProposed)
1107         mimeType = pwzMimeProposed;
1108     else {
1109         /* Try and find the mime type in the registry */
1110         if(pwzUrl) {
1111             LPWSTR ext = strrchrW(pwzUrl, '.');
1112             if(ext) {
1113                 DWORD dwSize;
1114                 if(!RegOpenKeyExW(HKEY_CLASSES_ROOT, ext, 0, 0, &hKey)) {
1115                     if(!RegQueryValueExW(hKey, szContentType, NULL, NULL, (LPBYTE)szTmpMime, &dwSize)) {
1116                         mimeType = szTmpMime;
1117                     }
1118                     RegCloseKey(hKey);
1119                 }
1120             }
1121         }
1122     }
1123     if(!mimeType && pBuffer && cbSize > 0)
1124         mimeType = URLMON_IsBinary(pBuffer, cbSize)?szBinaryMime:szTextMime;
1125
1126     TRACE("Using %s\n", debugstr_w(mimeType));
1127     *ppwzMimeOut = CoTaskMemAlloc((lstrlenW(mimeType)+1)*sizeof(WCHAR));
1128     if(!*ppwzMimeOut) return E_OUTOFMEMORY;
1129     lstrcpyW(*ppwzMimeOut, mimeType);
1130     return S_OK;
1131 }
1132
1133 /***********************************************************************
1134  *           IsAsyncMoniker (URLMON.@)
1135  */
1136 HRESULT WINAPI IsAsyncMoniker(IMoniker *pmk)
1137 {
1138     FIXME("stub\n");
1139     return S_FALSE;
1140 }
1141
1142 /***********************************************************************
1143  *           RegisterBindStatusCallback (URLMON.@)
1144  *
1145  * Register a bind status callback.
1146  *
1147  * PARAMS
1148  *  pbc           [I] Binding context
1149  *  pbsc          [I] Callback to register
1150  *  ppbscPrevious [O] Destination for previous callback
1151  *  dwReserved    [I] Reserved, must be 0.
1152  *
1153  * RETURNS
1154  *    Success: S_OK.
1155  *    Failure: E_INVALIDARG, if any argument is invalid, or
1156  *             E_OUTOFMEMORY if memory allocation fails.
1157  */
1158 HRESULT WINAPI RegisterBindStatusCallback(
1159     IBindCtx *pbc,
1160     IBindStatusCallback *pbsc,
1161     IBindStatusCallback **ppbscPrevious,
1162     DWORD dwReserved)
1163 {
1164     IBindStatusCallback *prev;
1165
1166         TRACE("(%p,%p,%p,%lu)\n", pbc, pbsc, ppbscPrevious, dwReserved);
1167
1168     if (pbc == NULL || pbsc == NULL)
1169         return E_INVALIDARG;
1170
1171     if (SUCCEEDED(IBindCtx_GetObjectParam(pbc, (LPOLESTR)BSCBHolder, (IUnknown **)&prev)))
1172     {
1173         IBindCtx_RevokeObjectParam(pbc, (LPOLESTR)BSCBHolder);
1174         if (ppbscPrevious)
1175             *ppbscPrevious = prev;
1176         else
1177             IBindStatusCallback_Release(prev);
1178     }
1179
1180         return IBindCtx_RegisterObjectParam(pbc, (LPOLESTR)BSCBHolder, (IUnknown *)pbsc);
1181 }
1182
1183 /***********************************************************************
1184  *           RevokeBindStatusCallback (URLMON.@)
1185  *
1186  * Unregister a bind status callback.
1187  *
1188  *  pbc           [I] Binding context
1189  *  pbsc          [I] Callback to unregister
1190  *
1191  * RETURNS
1192  *    Success: S_OK.
1193  *    Failure: E_INVALIDARG, if any argument is invalid, or
1194  *             E_FAIL if pbsc wasn't registered with pbc.
1195  */
1196 HRESULT WINAPI RevokeBindStatusCallback(
1197     IBindCtx *pbc,
1198     IBindStatusCallback *pbsc)
1199 {
1200     IBindStatusCallback *callback;
1201     HRESULT hr = E_FAIL;
1202
1203         TRACE("(%p,%p)\n", pbc, pbsc);
1204
1205     if (pbc == NULL || pbsc == NULL)
1206         return E_INVALIDARG;
1207
1208     if (SUCCEEDED(IBindCtx_GetObjectParam(pbc, (LPOLESTR)BSCBHolder, (IUnknown **)&callback)))
1209     {
1210         if (callback == pbsc)
1211         {
1212             IBindCtx_RevokeObjectParam(pbc, (LPOLESTR)BSCBHolder);
1213             hr = S_OK;
1214         }
1215         IBindStatusCallback_Release(pbsc);
1216     }
1217
1218     return hr;
1219 }
1220
1221 /***********************************************************************
1222  *           ReleaseBindInfo (URLMON.@)
1223  *
1224  * Release the resources used by the specified BINDINFO structure.
1225  *
1226  * PARAMS
1227  *  pbindinfo [I] BINDINFO to release.
1228  *
1229  * RETURNS
1230  *  Nothing.
1231  */
1232 void WINAPI ReleaseBindInfo(BINDINFO* pbindinfo)
1233 {
1234     FIXME("(%p)stub!\n", pbindinfo);
1235 }
1236
1237 /***********************************************************************
1238  *           URLDownloadToFileA (URLMON.@)
1239  *
1240  * Downloads URL szURL to rile szFileName and call lpfnCB callback to
1241  * report progress.
1242  *
1243  * PARAMS
1244  *  pCaller    [I] controlling IUnknown interface.
1245  *  szURL      [I] URL of the file to download
1246  *  szFileName [I] file name to store the content of the URL
1247  *  dwReserved [I] reserved - set to 0
1248  *  lpfnCB     [I] callback for progress report
1249  *
1250  * RETURNS
1251  *  S_OK on success
1252  *  E_OUTOFMEMORY when going out of memory
1253  */
1254 HRESULT WINAPI URLDownloadToFileA(LPUNKNOWN pCaller,
1255                                   LPCSTR szURL,
1256                                   LPCSTR szFileName,
1257                                   DWORD dwReserved,
1258                                   LPBINDSTATUSCALLBACK lpfnCB)
1259 {
1260     UNICODE_STRING szURL_w, szFileName_w;
1261
1262     if ((szURL == NULL) || (szFileName == NULL)) {
1263         FIXME("(%p,%s,%s,%08lx,%p) cannot accept NULL strings !\n", pCaller, debugstr_a(szURL), debugstr_a(szFileName), dwReserved, lpfnCB);
1264         return E_INVALIDARG; /* The error code is not specified in this case... */
1265     }
1266     
1267     if (RtlCreateUnicodeStringFromAsciiz(&szURL_w, szURL)) {
1268         if (RtlCreateUnicodeStringFromAsciiz(&szFileName_w, szFileName)) {
1269             HRESULT ret = URLDownloadToFileW(pCaller, szURL_w.Buffer, szFileName_w.Buffer, dwReserved, lpfnCB);
1270
1271             RtlFreeUnicodeString(&szURL_w);
1272             RtlFreeUnicodeString(&szFileName_w);
1273             
1274             return ret;
1275         } else {
1276             RtlFreeUnicodeString(&szURL_w);
1277         }
1278     }
1279     
1280     FIXME("(%p,%s,%s,%08lx,%p) could not allocate W strings !\n", pCaller, szURL, szFileName, dwReserved, lpfnCB);
1281     return E_OUTOFMEMORY;
1282 }
1283
1284 /***********************************************************************
1285  *           URLDownloadToFileW (URLMON.@)
1286  *
1287  * Downloads URL szURL to rile szFileName and call lpfnCB callback to
1288  * report progress.
1289  *
1290  * PARAMS
1291  *  pCaller    [I] controlling IUnknown interface.
1292  *  szURL      [I] URL of the file to download
1293  *  szFileName [I] file name to store the content of the URL
1294  *  dwReserved [I] reserved - set to 0
1295  *  lpfnCB     [I] callback for progress report
1296  *
1297  * RETURNS
1298  *  S_OK on success
1299  *  E_OUTOFMEMORY when going out of memory
1300  */
1301 HRESULT WINAPI URLDownloadToFileW(LPUNKNOWN pCaller,
1302                                   LPCWSTR szURL,
1303                                   LPCWSTR szFileName,
1304                                   DWORD dwReserved,
1305                                   LPBINDSTATUSCALLBACK lpfnCB)
1306 {
1307     HINTERNET hinternet, hcon, hreq;
1308     BOOL r;
1309     CHAR buffer[0x1000];
1310     DWORD sz, total, written;
1311     DWORD total_size = 0xFFFFFFFF, arg_size = sizeof(total_size);
1312     URL_COMPONENTSW url;
1313     WCHAR host[0x80], path[0x100];
1314     HANDLE hfile;
1315     static const WCHAR wszAppName[]={'u','r','l','m','o','n','.','d','l','l',0};
1316
1317     /* Note: all error codes would need to be checked agains real Windows behaviour... */
1318     TRACE("(%p,%s,%s,%08lx,%p) stub!\n", pCaller, debugstr_w(szURL), debugstr_w(szFileName), dwReserved, lpfnCB);
1319
1320     if ((szURL == NULL) || (szFileName == NULL)) {
1321         FIXME(" cannot accept NULL strings !\n");
1322         return E_INVALIDARG;
1323     }
1324
1325     /* Would be better to use the application name here rather than 'urlmon' :-/ */
1326     hinternet = InternetOpenW(wszAppName, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
1327     if (hinternet == NULL) {
1328         return E_OUTOFMEMORY;
1329     }                                                                                                                             
1330
1331     memset(&url, 0, sizeof(url));
1332     url.dwStructSize = sizeof(url);
1333     url.lpszHostName = host;
1334     url.dwHostNameLength = sizeof(host);
1335     url.lpszUrlPath = path;
1336     url.dwUrlPathLength = sizeof(path);
1337
1338     if (!InternetCrackUrlW(szURL, 0, 0, &url)) {
1339         InternetCloseHandle(hinternet);
1340         return E_OUTOFMEMORY;
1341     }
1342
1343     if (lpfnCB) {
1344         if (IBindStatusCallback_OnProgress(lpfnCB, 0, 0, BINDSTATUS_CONNECTING, url.lpszHostName) == E_ABORT) {
1345             InternetCloseHandle(hinternet);
1346             return S_OK;
1347         }
1348     }
1349     
1350     hcon = InternetConnectW(hinternet, url.lpszHostName, url.nPort,
1351                             url.lpszUserName, url.lpszPassword,
1352                             INTERNET_SERVICE_HTTP, 0, 0);
1353     if (!hcon) {
1354         InternetCloseHandle(hinternet);
1355         return E_OUTOFMEMORY;
1356     }
1357     
1358     hreq = HttpOpenRequestW(hcon, NULL, url.lpszUrlPath, NULL, NULL, NULL, 0, 0);
1359     if (!hreq) {
1360         InternetCloseHandle(hinternet);
1361         InternetCloseHandle(hcon);
1362         return E_OUTOFMEMORY;
1363     }                                                                                                                             
1364
1365     if (!HttpSendRequestW(hreq, NULL, 0, NULL, 0)) {
1366         InternetCloseHandle(hinternet);
1367         InternetCloseHandle(hcon);
1368         InternetCloseHandle(hreq);
1369         return E_OUTOFMEMORY;
1370     }
1371     
1372     if (HttpQueryInfoW(hreq, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER,
1373                        &total_size, &arg_size, NULL)) {
1374         TRACE(" total size : %ld\n", total_size);
1375     }
1376     
1377     hfile = CreateFileW(szFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
1378                         FILE_ATTRIBUTE_NORMAL, NULL );
1379     if (hfile == INVALID_HANDLE_VALUE) {
1380         return E_ACCESSDENIED;
1381     }
1382     
1383     if (lpfnCB) {
1384         if (IBindStatusCallback_OnProgress(lpfnCB, 0, total_size != 0xFFFFFFFF ? total_size : 0,
1385                                            BINDSTATUS_BEGINDOWNLOADDATA, szURL) == E_ABORT) {
1386             InternetCloseHandle(hreq);
1387             InternetCloseHandle(hcon);
1388             InternetCloseHandle(hinternet);
1389             CloseHandle(hfile);
1390             return S_OK;
1391         }
1392     }
1393     
1394     total = 0;
1395     while (1) {
1396         r = InternetReadFile(hreq, buffer, sizeof(buffer), &sz);
1397         if (!r) {
1398             InternetCloseHandle(hreq);
1399             InternetCloseHandle(hcon);
1400             InternetCloseHandle(hinternet);
1401             
1402             CloseHandle(hfile);
1403             return E_OUTOFMEMORY;           
1404         }
1405         if (!sz)
1406             break;
1407         
1408         total += sz;
1409
1410         if (lpfnCB) {
1411             if (IBindStatusCallback_OnProgress(lpfnCB, total, total_size != 0xFFFFFFFF ? total_size : 0,
1412                                                BINDSTATUS_DOWNLOADINGDATA, szURL) == E_ABORT) {
1413                 InternetCloseHandle(hreq);
1414                 InternetCloseHandle(hcon);
1415                 InternetCloseHandle(hinternet);
1416                 CloseHandle(hfile);
1417                 return S_OK;
1418             }
1419         }
1420         
1421         if (!WriteFile(hfile, buffer, sz, &written, NULL)) {
1422             InternetCloseHandle(hreq);
1423             InternetCloseHandle(hcon);
1424             InternetCloseHandle(hinternet);
1425             
1426             CloseHandle(hfile);
1427             return E_OUTOFMEMORY;
1428         }
1429     }
1430
1431     if (lpfnCB) {
1432         if (IBindStatusCallback_OnProgress(lpfnCB, total, total_size != 0xFFFFFFFF ? total_size : 0,
1433                                            BINDSTATUS_ENDDOWNLOADDATA, szURL) == E_ABORT) {
1434             InternetCloseHandle(hreq);
1435             InternetCloseHandle(hcon);
1436             InternetCloseHandle(hinternet);
1437             CloseHandle(hfile);
1438             return S_OK;
1439         }
1440     }
1441     
1442     InternetCloseHandle(hreq);
1443     InternetCloseHandle(hcon);
1444     InternetCloseHandle(hinternet);
1445     
1446     CloseHandle(hfile);
1447
1448     return S_OK;
1449 }