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