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