Implemented IsAsyncMoniker.
[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     IUnknown *am;
1139     
1140     TRACE("(%p)\n", pmk);
1141     if(!pmk)
1142         return E_INVALIDARG;
1143     if(SUCCEEDED(IMoniker_QueryInterface(pmk, &IID_IAsyncMoniker, (void**)&am))) {
1144         IUnknown_Release(am);
1145         return S_OK;
1146     }
1147     return S_FALSE;
1148 }
1149
1150 /***********************************************************************
1151  *           RegisterBindStatusCallback (URLMON.@)
1152  *
1153  * Register a bind status callback.
1154  *
1155  * PARAMS
1156  *  pbc           [I] Binding context
1157  *  pbsc          [I] Callback to register
1158  *  ppbscPrevious [O] Destination for previous callback
1159  *  dwReserved    [I] Reserved, must be 0.
1160  *
1161  * RETURNS
1162  *    Success: S_OK.
1163  *    Failure: E_INVALIDARG, if any argument is invalid, or
1164  *             E_OUTOFMEMORY if memory allocation fails.
1165  */
1166 HRESULT WINAPI RegisterBindStatusCallback(
1167     IBindCtx *pbc,
1168     IBindStatusCallback *pbsc,
1169     IBindStatusCallback **ppbscPrevious,
1170     DWORD dwReserved)
1171 {
1172     IBindStatusCallback *prev;
1173
1174         TRACE("(%p,%p,%p,%lu)\n", pbc, pbsc, ppbscPrevious, dwReserved);
1175
1176     if (pbc == NULL || pbsc == NULL)
1177         return E_INVALIDARG;
1178
1179     if (SUCCEEDED(IBindCtx_GetObjectParam(pbc, (LPOLESTR)BSCBHolder, (IUnknown **)&prev)))
1180     {
1181         IBindCtx_RevokeObjectParam(pbc, (LPOLESTR)BSCBHolder);
1182         if (ppbscPrevious)
1183             *ppbscPrevious = prev;
1184         else
1185             IBindStatusCallback_Release(prev);
1186     }
1187
1188         return IBindCtx_RegisterObjectParam(pbc, (LPOLESTR)BSCBHolder, (IUnknown *)pbsc);
1189 }
1190
1191 /***********************************************************************
1192  *           RevokeBindStatusCallback (URLMON.@)
1193  *
1194  * Unregister a bind status callback.
1195  *
1196  *  pbc           [I] Binding context
1197  *  pbsc          [I] Callback to unregister
1198  *
1199  * RETURNS
1200  *    Success: S_OK.
1201  *    Failure: E_INVALIDARG, if any argument is invalid, or
1202  *             E_FAIL if pbsc wasn't registered with pbc.
1203  */
1204 HRESULT WINAPI RevokeBindStatusCallback(
1205     IBindCtx *pbc,
1206     IBindStatusCallback *pbsc)
1207 {
1208     IBindStatusCallback *callback;
1209     HRESULT hr = E_FAIL;
1210
1211         TRACE("(%p,%p)\n", pbc, pbsc);
1212
1213     if (pbc == NULL || pbsc == NULL)
1214         return E_INVALIDARG;
1215
1216     if (SUCCEEDED(IBindCtx_GetObjectParam(pbc, (LPOLESTR)BSCBHolder, (IUnknown **)&callback)))
1217     {
1218         if (callback == pbsc)
1219         {
1220             IBindCtx_RevokeObjectParam(pbc, (LPOLESTR)BSCBHolder);
1221             hr = S_OK;
1222         }
1223         IBindStatusCallback_Release(pbsc);
1224     }
1225
1226     return hr;
1227 }
1228
1229 /***********************************************************************
1230  *           ReleaseBindInfo (URLMON.@)
1231  *
1232  * Release the resources used by the specified BINDINFO structure.
1233  *
1234  * PARAMS
1235  *  pbindinfo [I] BINDINFO to release.
1236  *
1237  * RETURNS
1238  *  Nothing.
1239  */
1240 void WINAPI ReleaseBindInfo(BINDINFO* pbindinfo)
1241 {
1242     FIXME("(%p)stub!\n", pbindinfo);
1243 }
1244
1245 /***********************************************************************
1246  *           URLDownloadToFileA (URLMON.@)
1247  *
1248  * Downloads URL szURL to rile szFileName and call lpfnCB callback to
1249  * report progress.
1250  *
1251  * PARAMS
1252  *  pCaller    [I] controlling IUnknown interface.
1253  *  szURL      [I] URL of the file to download
1254  *  szFileName [I] file name to store the content of the URL
1255  *  dwReserved [I] reserved - set to 0
1256  *  lpfnCB     [I] callback for progress report
1257  *
1258  * RETURNS
1259  *  S_OK on success
1260  *  E_OUTOFMEMORY when going out of memory
1261  */
1262 HRESULT WINAPI URLDownloadToFileA(LPUNKNOWN pCaller,
1263                                   LPCSTR szURL,
1264                                   LPCSTR szFileName,
1265                                   DWORD dwReserved,
1266                                   LPBINDSTATUSCALLBACK lpfnCB)
1267 {
1268     UNICODE_STRING szURL_w, szFileName_w;
1269
1270     if ((szURL == NULL) || (szFileName == NULL)) {
1271         FIXME("(%p,%s,%s,%08lx,%p) cannot accept NULL strings !\n", pCaller, debugstr_a(szURL), debugstr_a(szFileName), dwReserved, lpfnCB);
1272         return E_INVALIDARG; /* The error code is not specified in this case... */
1273     }
1274     
1275     if (RtlCreateUnicodeStringFromAsciiz(&szURL_w, szURL)) {
1276         if (RtlCreateUnicodeStringFromAsciiz(&szFileName_w, szFileName)) {
1277             HRESULT ret = URLDownloadToFileW(pCaller, szURL_w.Buffer, szFileName_w.Buffer, dwReserved, lpfnCB);
1278
1279             RtlFreeUnicodeString(&szURL_w);
1280             RtlFreeUnicodeString(&szFileName_w);
1281             
1282             return ret;
1283         } else {
1284             RtlFreeUnicodeString(&szURL_w);
1285         }
1286     }
1287     
1288     FIXME("(%p,%s,%s,%08lx,%p) could not allocate W strings !\n", pCaller, szURL, szFileName, dwReserved, lpfnCB);
1289     return E_OUTOFMEMORY;
1290 }
1291
1292 /***********************************************************************
1293  *           URLDownloadToFileW (URLMON.@)
1294  *
1295  * Downloads URL szURL to rile szFileName and call lpfnCB callback to
1296  * report progress.
1297  *
1298  * PARAMS
1299  *  pCaller    [I] controlling IUnknown interface.
1300  *  szURL      [I] URL of the file to download
1301  *  szFileName [I] file name to store the content of the URL
1302  *  dwReserved [I] reserved - set to 0
1303  *  lpfnCB     [I] callback for progress report
1304  *
1305  * RETURNS
1306  *  S_OK on success
1307  *  E_OUTOFMEMORY when going out of memory
1308  */
1309 HRESULT WINAPI URLDownloadToFileW(LPUNKNOWN pCaller,
1310                                   LPCWSTR szURL,
1311                                   LPCWSTR szFileName,
1312                                   DWORD dwReserved,
1313                                   LPBINDSTATUSCALLBACK lpfnCB)
1314 {
1315     HINTERNET hinternet, hcon, hreq;
1316     BOOL r;
1317     CHAR buffer[0x1000];
1318     DWORD sz, total, written;
1319     DWORD total_size = 0xFFFFFFFF, arg_size = sizeof(total_size);
1320     URL_COMPONENTSW url;
1321     WCHAR host[0x80], path[0x100];
1322     HANDLE hfile;
1323     static const WCHAR wszAppName[]={'u','r','l','m','o','n','.','d','l','l',0};
1324
1325     /* Note: all error codes would need to be checked agains real Windows behaviour... */
1326     TRACE("(%p,%s,%s,%08lx,%p) stub!\n", pCaller, debugstr_w(szURL), debugstr_w(szFileName), dwReserved, lpfnCB);
1327
1328     if ((szURL == NULL) || (szFileName == NULL)) {
1329         FIXME(" cannot accept NULL strings !\n");
1330         return E_INVALIDARG;
1331     }
1332
1333     /* Would be better to use the application name here rather than 'urlmon' :-/ */
1334     hinternet = InternetOpenW(wszAppName, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
1335     if (hinternet == NULL) {
1336         return E_OUTOFMEMORY;
1337     }                                                                                                                             
1338
1339     memset(&url, 0, sizeof(url));
1340     url.dwStructSize = sizeof(url);
1341     url.lpszHostName = host;
1342     url.dwHostNameLength = sizeof(host);
1343     url.lpszUrlPath = path;
1344     url.dwUrlPathLength = sizeof(path);
1345
1346     if (!InternetCrackUrlW(szURL, 0, 0, &url)) {
1347         InternetCloseHandle(hinternet);
1348         return E_OUTOFMEMORY;
1349     }
1350
1351     if (lpfnCB) {
1352         if (IBindStatusCallback_OnProgress(lpfnCB, 0, 0, BINDSTATUS_CONNECTING, url.lpszHostName) == E_ABORT) {
1353             InternetCloseHandle(hinternet);
1354             return S_OK;
1355         }
1356     }
1357     
1358     hcon = InternetConnectW(hinternet, url.lpszHostName, url.nPort,
1359                             url.lpszUserName, url.lpszPassword,
1360                             INTERNET_SERVICE_HTTP, 0, 0);
1361     if (!hcon) {
1362         InternetCloseHandle(hinternet);
1363         return E_OUTOFMEMORY;
1364     }
1365     
1366     hreq = HttpOpenRequestW(hcon, NULL, url.lpszUrlPath, NULL, NULL, NULL, 0, 0);
1367     if (!hreq) {
1368         InternetCloseHandle(hinternet);
1369         InternetCloseHandle(hcon);
1370         return E_OUTOFMEMORY;
1371     }                                                                                                                             
1372
1373     if (!HttpSendRequestW(hreq, NULL, 0, NULL, 0)) {
1374         InternetCloseHandle(hinternet);
1375         InternetCloseHandle(hcon);
1376         InternetCloseHandle(hreq);
1377         return E_OUTOFMEMORY;
1378     }
1379     
1380     if (HttpQueryInfoW(hreq, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER,
1381                        &total_size, &arg_size, NULL)) {
1382         TRACE(" total size : %ld\n", total_size);
1383     }
1384     
1385     hfile = CreateFileW(szFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
1386                         FILE_ATTRIBUTE_NORMAL, NULL );
1387     if (hfile == INVALID_HANDLE_VALUE) {
1388         return E_ACCESSDENIED;
1389     }
1390     
1391     if (lpfnCB) {
1392         if (IBindStatusCallback_OnProgress(lpfnCB, 0, total_size != 0xFFFFFFFF ? total_size : 0,
1393                                            BINDSTATUS_BEGINDOWNLOADDATA, szURL) == E_ABORT) {
1394             InternetCloseHandle(hreq);
1395             InternetCloseHandle(hcon);
1396             InternetCloseHandle(hinternet);
1397             CloseHandle(hfile);
1398             return S_OK;
1399         }
1400     }
1401     
1402     total = 0;
1403     while (1) {
1404         r = InternetReadFile(hreq, buffer, sizeof(buffer), &sz);
1405         if (!r) {
1406             InternetCloseHandle(hreq);
1407             InternetCloseHandle(hcon);
1408             InternetCloseHandle(hinternet);
1409             
1410             CloseHandle(hfile);
1411             return E_OUTOFMEMORY;           
1412         }
1413         if (!sz)
1414             break;
1415         
1416         total += sz;
1417
1418         if (lpfnCB) {
1419             if (IBindStatusCallback_OnProgress(lpfnCB, total, total_size != 0xFFFFFFFF ? total_size : 0,
1420                                                BINDSTATUS_DOWNLOADINGDATA, szURL) == E_ABORT) {
1421                 InternetCloseHandle(hreq);
1422                 InternetCloseHandle(hcon);
1423                 InternetCloseHandle(hinternet);
1424                 CloseHandle(hfile);
1425                 return S_OK;
1426             }
1427         }
1428         
1429         if (!WriteFile(hfile, buffer, sz, &written, NULL)) {
1430             InternetCloseHandle(hreq);
1431             InternetCloseHandle(hcon);
1432             InternetCloseHandle(hinternet);
1433             
1434             CloseHandle(hfile);
1435             return E_OUTOFMEMORY;
1436         }
1437     }
1438
1439     if (lpfnCB) {
1440         if (IBindStatusCallback_OnProgress(lpfnCB, total, total_size != 0xFFFFFFFF ? total_size : 0,
1441                                            BINDSTATUS_ENDDOWNLOADDATA, szURL) == E_ABORT) {
1442             InternetCloseHandle(hreq);
1443             InternetCloseHandle(hcon);
1444             InternetCloseHandle(hinternet);
1445             CloseHandle(hfile);
1446             return S_OK;
1447         }
1448     }
1449     
1450     InternetCloseHandle(hreq);
1451     InternetCloseHandle(hcon);
1452     InternetCloseHandle(hinternet);
1453     
1454     CloseHandle(hfile);
1455
1456     return S_OK;
1457 }