reshuffling of dlls
[reactos.git] / reactos / dll / win32 / 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 * Copyright 2005 Jacek Caban for CodeWeavers
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23 #define COM_NO_WINDOWS_H
24 #include <stdarg.h>
25 #include <stdio.h>
26
27 #define COBJMACROS
28 #define NONAMELESSUNION
29 #define NONAMELESSSTRUCT
30
31 #include "windef.h"
32 #include "winbase.h"
33 #include "winreg.h"
34 #include "winternl.h"
35 #include "winuser.h"
36 #include "objbase.h"
37 #include "wine/debug.h"
38 #include "wine/unicode.h"
39 #include "ole2.h"
40 #include "urlmon.h"
41 #include "wininet.h"
42 #include "shlwapi.h"
43 #include "urlmon_main.h"
44
45 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
46
47 /* native urlmon.dll uses this key, too */
48 static const WCHAR BSCBHolder[] = { '_','B','S','C','B','_','H','o','l','d','e','r','_',0 };
49
50 /*static BOOL registered_wndclass = FALSE;*/
51
52 typedef struct {
53 const IBindingVtbl *lpVtbl;
54
55 LONG ref;
56
57 LPWSTR URLName;
58
59 HWND hwndCallback;
60 IBindCtx *pBC;
61 HINTERNET hinternet, hconnect, hrequest;
62 HANDLE hCacheFile;
63 IUMCacheStream *pstrCache;
64 IBindStatusCallback *pbscb;
65 DWORD total_read, expected_size;
66 } Binding;
67
68 static HRESULT WINAPI Binding_QueryInterface(IBinding* iface, REFIID riid, void **ppvObject)
69 {
70 Binding *This = (Binding*)iface;
71
72 TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(riid), ppvObject);
73
74 if((This == NULL) || (ppvObject == NULL))
75 return E_INVALIDARG;
76
77 if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IBinding, riid)) {
78 *ppvObject = iface;
79 IBinding_AddRef(iface);
80 return S_OK;
81 }
82
83 *ppvObject = NULL;
84 return E_NOINTERFACE;
85 }
86
87 static ULONG WINAPI Binding_AddRef(IBinding* iface)
88 {
89 Binding *This = (Binding*)iface;
90 ULONG ref = InterlockedIncrement(&This->ref);
91
92 TRACE("(%p) ref=%ld\n", This, ref);
93
94 return ref;
95 }
96
97 static ULONG WINAPI Binding_Release(IBinding* iface)
98 {
99 Binding *This = (Binding*)iface;
100 ULONG ref = InterlockedDecrement(&This->ref);
101
102 TRACE("(%p) ref=%ld\n",This, ref);
103
104 if(!ref) {
105 HeapFree(GetProcessHeap(), 0, This->URLName);
106 if (This->hCacheFile)
107 CloseHandle(This->hCacheFile);
108 if (This->pstrCache)
109 {
110 UMCloseCacheFileStream(This->pstrCache);
111 IStream_Release((IStream *)This->pstrCache);
112 }
113 if (This->pbscb)
114 IBindStatusCallback_Release(This->pbscb);
115
116 HeapFree(GetProcessHeap(), 0, This);
117
118 URLMON_UnlockModule();
119 }
120
121 return ref;
122 }
123
124 static HRESULT WINAPI Binding_Abort(IBinding* iface)
125 {
126 Binding *This = (Binding*)iface;
127
128 FIXME("(%p): stub\n", This);
129
130 return E_NOTIMPL;
131 }
132
133 static HRESULT WINAPI Binding_GetBindResult(IBinding* iface, CLSID* pclsidProtocol, DWORD* pdwResult, LPOLESTR* pszResult, DWORD* pdwReserved)
134 {
135 Binding *This = (Binding*)iface;
136
137 FIXME("(%p)->(%p, %p, %p, %p): stub\n", This, pclsidProtocol, pdwResult, pszResult, pdwReserved);
138
139 return E_NOTIMPL;
140 }
141
142 static HRESULT WINAPI Binding_GetPriority(IBinding* iface, LONG* pnPriority)
143 {
144 Binding *This = (Binding*)iface;
145
146 FIXME("(%p)->(%p): stub\n", This, pnPriority);
147
148 return E_NOTIMPL;
149 }
150
151 static HRESULT WINAPI Binding_Resume(IBinding* iface)
152 {
153 Binding *This = (Binding*)iface;
154
155 FIXME("(%p): stub\n", This);
156
157 return E_NOTIMPL;
158 }
159
160 static HRESULT WINAPI Binding_SetPriority(IBinding* iface, LONG nPriority)
161 {
162 Binding *This = (Binding*)iface;
163
164 FIXME("(%p)->(%ld): stub\n", This, nPriority);
165
166 return E_NOTIMPL;
167 }
168
169 static HRESULT WINAPI Binding_Suspend(IBinding* iface)
170 {
171 Binding *This = (Binding*)iface;
172
173 FIXME("(%p): stub\n", This);
174
175 return E_NOTIMPL;
176 }
177
178 static void Binding_CloseCacheDownload(Binding *This)
179 {
180 CloseHandle(This->hCacheFile);
181 This->hCacheFile = 0;
182 UMCloseCacheFileStream(This->pstrCache);
183 IStream_Release((IStream *)This->pstrCache);
184 This->pstrCache = 0;
185 }
186
187 static HRESULT Binding_MoreCacheData(Binding *This, char *buf, DWORD dwBytes)
188 {
189 DWORD written;
190
191 if (WriteFile(This->hCacheFile, buf, dwBytes, &written, NULL) && written == dwBytes)
192 {
193 HRESULT hr;
194
195 This->total_read += written;
196 hr = IBindStatusCallback_OnProgress(This->pbscb,
197 This->total_read + written,
198 This->expected_size,
199 (This->total_read == written) ?
200 BINDSTATUS_BEGINDOWNLOADDATA :
201 BINDSTATUS_DOWNLOADINGDATA,
202 This->URLName);
203 if (!hr)
204 {
205 STGMEDIUM stg;
206 FORMATETC fmt;
207
208 fmt.cfFormat = 0;
209 fmt.ptd = NULL;
210 fmt.dwAspect = 0;
211 fmt.lindex = -1;
212 fmt.tymed = TYMED_ISTREAM;
213
214 stg.tymed = TYMED_ISTREAM;
215 stg.u.pstm = (IStream *)This->pstrCache;
216 stg.pUnkForRelease = NULL;
217
218 hr = IBindStatusCallback_OnDataAvailable(This->pbscb,
219 (This->total_read == written) ?
220 BSCF_FIRSTDATANOTIFICATION :
221 BSCF_INTERMEDIATEDATANOTIFICATION,
222 This->total_read + written,
223 &fmt,
224 &stg);
225 }
226 if (written < dwBytes)
227 return STG_E_MEDIUMFULL;
228 else
229 return hr;
230 }
231 return HRESULT_FROM_WIN32(GetLastError());
232 }
233
234 static void Binding_FinishedDownload(Binding *This, HRESULT hr)
235 {
236 STGMEDIUM stg;
237 FORMATETC fmt;
238
239 fmt.ptd = NULL;
240 fmt.dwAspect = 0;
241 fmt.lindex = -1;
242 fmt.tymed = TYMED_ISTREAM;
243
244 stg.tymed = TYMED_ISTREAM;
245 stg.u.pstm = (IStream *)This->pstrCache;
246 stg.pUnkForRelease = NULL;
247
248 IBindStatusCallback_OnProgress(This->pbscb, This->total_read, This->expected_size,
249 BINDSTATUS_ENDDOWNLOADDATA, This->URLName);
250 IBindStatusCallback_OnDataAvailable(This->pbscb, BSCF_LASTDATANOTIFICATION, This->total_read, &fmt, &stg);
251 if (hr)
252 {
253 WCHAR *pwchError = 0;
254
255 FormatMessageW (FORMAT_MESSAGE_FROM_SYSTEM |
256 FORMAT_MESSAGE_ALLOCATE_BUFFER,
257 NULL, (DWORD) hr,
258 0, (LPWSTR) &pwchError,
259 0, NULL);
260 if (!pwchError)
261 {
262 static WCHAR achFormat[] = { '%', '0', '8', 'x', 0 };
263
264 pwchError =(WCHAR *) LocalAlloc(LMEM_FIXED, sizeof(WCHAR) * 9);
265 wsprintfW(pwchError, achFormat, hr);
266 }
267 IBindStatusCallback_OnStopBinding(This->pbscb, hr, pwchError);
268 LocalFree(pwchError);
269 }
270 else
271 {
272 IBindStatusCallback_OnStopBinding(This->pbscb, hr, NULL);
273 }
274 IBindStatusCallback_Release(This->pbscb);
275 This->pbscb = 0;
276 }
277
278 static const IBindingVtbl BindingVtbl =
279 {
280 Binding_QueryInterface,
281 Binding_AddRef,
282 Binding_Release,
283 Binding_Abort,
284 Binding_Suspend,
285 Binding_Resume,
286 Binding_SetPriority,
287 Binding_GetPriority,
288 Binding_GetBindResult
289 };
290
291 /* filemoniker data structure */
292 typedef struct {
293
294 const IMonikerVtbl* lpvtbl; /* VTable relative to the IMoniker interface.*/
295
296 LONG ref; /* reference counter for this object */
297
298 LPOLESTR URLName; /* URL string identified by this URLmoniker */
299 } URLMonikerImpl;
300
301 /*******************************************************************************
302 * URLMoniker_QueryInterface
303 *******************************************************************************/
304 static HRESULT WINAPI URLMonikerImpl_QueryInterface(IMoniker* iface,REFIID riid,void** ppvObject)
305 {
306 URLMonikerImpl *This = (URLMonikerImpl *)iface;
307
308 TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppvObject);
309
310 /* Perform a sanity check on the parameters.*/
311 if ( (This==0) || (ppvObject==0) )
312 return E_INVALIDARG;
313
314 /* Initialize the return parameter */
315 *ppvObject = 0;
316
317 /* Compare the riid with the interface IDs implemented by this object.*/
318 if (IsEqualIID(&IID_IUnknown, riid) ||
319 IsEqualIID(&IID_IPersist, riid) ||
320 IsEqualIID(&IID_IPersistStream,riid) ||
321 IsEqualIID(&IID_IMoniker, riid)
322 )
323 *ppvObject = iface;
324
325 /* Check that we obtained an interface.*/
326 if ((*ppvObject)==0)
327 return E_NOINTERFACE;
328
329 /* Query Interface always increases the reference count by one when it is successful */
330 IMoniker_AddRef(iface);
331
332 return S_OK;
333 }
334
335 /******************************************************************************
336 * URLMoniker_AddRef
337 ******************************************************************************/
338 static ULONG WINAPI URLMonikerImpl_AddRef(IMoniker* iface)
339 {
340 URLMonikerImpl *This = (URLMonikerImpl *)iface;
341 ULONG refCount = InterlockedIncrement(&This->ref);
342
343 TRACE("(%p)->(ref before=%lu)\n",This, refCount - 1);
344
345 return refCount;
346 }
347
348 /******************************************************************************
349 * URLMoniker_Release
350 ******************************************************************************/
351 static ULONG WINAPI URLMonikerImpl_Release(IMoniker* iface)
352 {
353 URLMonikerImpl *This = (URLMonikerImpl *)iface;
354 ULONG refCount = InterlockedDecrement(&This->ref);
355
356 TRACE("(%p)->(ref before=%lu)\n",This, refCount + 1);
357
358 /* destroy the object if there's no more reference on it */
359 if (!refCount) {
360 HeapFree(GetProcessHeap(),0,This->URLName);
361 HeapFree(GetProcessHeap(),0,This);
362
363 URLMON_UnlockModule();
364 }
365
366 return refCount;
367 }
368
369
370 /******************************************************************************
371 * URLMoniker_GetClassID
372 ******************************************************************************/
373 static HRESULT WINAPI URLMonikerImpl_GetClassID(IMoniker* iface,
374 CLSID *pClassID)/* Pointer to CLSID of object */
375 {
376 URLMonikerImpl *This = (URLMonikerImpl *)iface;
377
378 TRACE("(%p,%p)\n",This,pClassID);
379
380 if (pClassID==NULL)
381 return E_POINTER;
382 /* Windows always returns CLSID_StdURLMoniker */
383 *pClassID = CLSID_StdURLMoniker;
384 return S_OK;
385 }
386
387 /******************************************************************************
388 * URLMoniker_IsDirty
389 ******************************************************************************/
390 static HRESULT WINAPI URLMonikerImpl_IsDirty(IMoniker* iface)
391 {
392 URLMonikerImpl *This = (URLMonikerImpl *)iface;
393 /* Note that the OLE-provided implementations of the IPersistStream::IsDirty
394 method in the OLE-provided moniker interfaces always return S_FALSE because
395 their internal state never changes. */
396
397 TRACE("(%p)\n",This);
398
399 return S_FALSE;
400 }
401
402 /******************************************************************************
403 * URLMoniker_Load
404 *
405 * NOTE
406 * Writes a ULONG containing length of unicode string, followed
407 * by that many unicode characters
408 ******************************************************************************/
409 static HRESULT WINAPI URLMonikerImpl_Load(IMoniker* iface,IStream* pStm)
410 {
411 URLMonikerImpl *This = (URLMonikerImpl *)iface;
412
413 HRESULT res;
414 ULONG len;
415 ULONG got;
416 TRACE("(%p,%p)\n",This,pStm);
417
418 if(!pStm)
419 return E_INVALIDARG;
420
421 res = IStream_Read(pStm, &len, sizeof(ULONG), &got);
422 if(SUCCEEDED(res)) {
423 if(got == sizeof(ULONG)) {
424 HeapFree(GetProcessHeap(), 0, This->URLName);
425 This->URLName=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(len+1));
426 if(!This->URLName)
427 res = E_OUTOFMEMORY;
428 else {
429 res = IStream_Read(pStm, This->URLName, len, NULL);
430 This->URLName[len] = 0;
431 }
432 }
433 else
434 res = E_FAIL;
435 }
436 return res;
437 }
438
439 /******************************************************************************
440 * URLMoniker_Save
441 ******************************************************************************/
442 static HRESULT WINAPI URLMonikerImpl_Save(IMoniker* iface,
443 IStream* pStm,/* pointer to the stream where the object is to be saved */
444 BOOL fClearDirty)/* Specifies whether to clear the dirty flag */
445 {
446 URLMonikerImpl *This = (URLMonikerImpl *)iface;
447
448 HRESULT res;
449 ULONG len;
450 TRACE("(%p,%p,%d)\n",This,pStm,fClearDirty);
451
452 if(!pStm)
453 return E_INVALIDARG;
454
455 len = strlenW(This->URLName);
456 res=IStream_Write(pStm,&len,sizeof(ULONG),NULL);
457 if(SUCCEEDED(res))
458 res=IStream_Write(pStm,&This->URLName,len*sizeof(WCHAR),NULL);
459 return res;
460
461 }
462
463 /******************************************************************************
464 * URLMoniker_GetSizeMax
465 ******************************************************************************/
466 static HRESULT WINAPI URLMonikerImpl_GetSizeMax(IMoniker* iface,
467 ULARGE_INTEGER* pcbSize)/* Pointer to size of stream needed to save object */
468 {
469 URLMonikerImpl *This = (URLMonikerImpl *)iface;
470
471 TRACE("(%p,%p)\n",This,pcbSize);
472
473 if(!pcbSize)
474 return E_INVALIDARG;
475
476 pcbSize->u.LowPart = sizeof(ULONG) + (strlenW(This->URLName) * sizeof(WCHAR));
477 pcbSize->u.HighPart = 0;
478 return S_OK;
479 }
480
481 /******************************************************************************
482 * URLMoniker_BindToObject
483 ******************************************************************************/
484 static HRESULT WINAPI URLMonikerImpl_BindToObject(IMoniker* iface,
485 IBindCtx* pbc,
486 IMoniker* pmkToLeft,
487 REFIID riid,
488 VOID** ppvResult)
489 {
490 URLMonikerImpl *This = (URLMonikerImpl *)iface;
491
492 *ppvResult=0;
493
494 FIXME("(%p)->(%p,%p,%s,%p): stub\n",This,pbc,pmkToLeft,debugstr_guid(riid),
495 ppvResult);
496
497 return E_NOTIMPL;
498 }
499
500 /******************************************************************************
501 * URLMoniker_BindToStorage
502 ******************************************************************************/
503 static HRESULT URLMonikerImpl_BindToStorage_hack(LPCWSTR URLName,
504 IBindCtx* pbc,
505 IMoniker* pmkToLeft,
506 REFIID riid,
507 VOID** ppvObject)
508 {
509 HRESULT hres;
510 BINDINFO bi;
511 DWORD bindf;
512 WCHAR szFileName[MAX_PATH + 1];
513 Binding *bind;
514 int len;
515
516 WARN("(%s %p %p %s %p)\n", debugstr_w(URLName), pbc, pmkToLeft, debugstr_guid(riid),
517 ppvObject);
518
519 if(pmkToLeft) {
520 FIXME("pmkToLeft != NULL\n");
521 return E_NOTIMPL;
522 }
523 if(!IsEqualIID(&IID_IStream, riid)) {
524 FIXME("unsupported iid\n");
525 return E_NOTIMPL;
526 }
527
528 bind = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(Binding));
529 bind->lpVtbl = &BindingVtbl;
530 bind->ref = 1;
531 URLMON_LockModule();
532
533 len = lstrlenW(URLName)+1;
534 bind->URLName = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
535 memcpy(bind->URLName, URLName, len*sizeof(WCHAR));
536
537 hres = UMCreateStreamOnCacheFile(bind->URLName, 0, szFileName, &bind->hCacheFile, &bind->pstrCache);
538
539 if(SUCCEEDED(hres)) {
540 TRACE("Created stream...\n");
541
542 *ppvObject = (void *) bind->pstrCache;
543 IStream_AddRef((IStream *) bind->pstrCache);
544
545 hres = IBindCtx_GetObjectParam(pbc, (LPOLESTR)BSCBHolder, (IUnknown**)&bind->pbscb);
546 if(SUCCEEDED(hres)) {
547 TRACE("Got IBindStatusCallback...\n");
548
549 memset(&bi, 0, sizeof(bi));
550 bi.cbSize = sizeof(bi);
551 bindf = 0;
552 hres = IBindStatusCallback_GetBindInfo(bind->pbscb, &bindf, &bi);
553 if(SUCCEEDED(hres)) {
554 WCHAR *urlcopy, *tmpwc;
555 URL_COMPONENTSW url;
556 WCHAR *host, *path, *user, *pass;
557 DWORD lensz = sizeof(bind->expected_size);
558 DWORD dwService = 0;
559 BOOL bSuccess;
560
561 TRACE("got bindinfo. bindf = %08lx extrainfo = %s bindinfof = %08lx bindverb = %08lx iid %s\n",
562 bindf, debugstr_w(bi.szExtraInfo), bi.grfBindInfoF, bi.dwBindVerb, debugstr_guid(&bi.iid));
563 hres = IBindStatusCallback_OnStartBinding(bind->pbscb, 0, (IBinding*)bind);
564 TRACE("OnStartBinding rets %08lx\n", hres);
565
566 /* This class will accept URLs with the backslash in them. But InternetCrackURL will not - it
567 * requires forward slashes (this is the behaviour of Microsoft's INETAPI). So we need to make
568 * a copy of the URL here and change the backslash to a forward slash everywhere it appears -
569 * but only before any '#' or '?', after which backslash should be left alone.
570 */
571 urlcopy = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR) * (lstrlenW(bind->URLName) + 1));
572 lstrcpyW(urlcopy, bind->URLName);
573 for (tmpwc = urlcopy; *tmpwc && *tmpwc != '#' && *tmpwc != '?'; ++tmpwc)
574 if (*tmpwc == '\\')
575 *tmpwc = '/';
576
577 bind->expected_size = 0;
578 bind->total_read = 0;
579
580 memset(&url, 0, sizeof(url));
581 url.dwStructSize = sizeof(url);
582 url.dwSchemeLength = url.dwHostNameLength = url.dwUrlPathLength = url.dwUserNameLength = url.dwPasswordLength = 1;
583 InternetCrackUrlW(urlcopy, 0, ICU_ESCAPE, &url);
584 host = HeapAlloc(GetProcessHeap(), 0, (url.dwHostNameLength + 1) * sizeof(WCHAR));
585 memcpy(host, url.lpszHostName, url.dwHostNameLength * sizeof(WCHAR));
586 host[url.dwHostNameLength] = '\0';
587 path = HeapAlloc(GetProcessHeap(), 0, (url.dwUrlPathLength + 1) * sizeof(WCHAR));
588 memcpy(path, url.lpszUrlPath, url.dwUrlPathLength * sizeof(WCHAR));
589 path[url.dwUrlPathLength] = '\0';
590 if (url.dwUserNameLength)
591 {
592 user = HeapAlloc(GetProcessHeap(), 0, ((url.dwUserNameLength + 1) * sizeof(WCHAR)));
593 memcpy(user, url.lpszUserName, url.dwUserNameLength * sizeof(WCHAR));
594 user[url.dwUserNameLength] = 0;
595 }
596 else
597 {
598 user = 0;
599 }
600 if (url.dwPasswordLength)
601 {
602 pass = HeapAlloc(GetProcessHeap(), 0, ((url.dwPasswordLength + 1) * sizeof(WCHAR)));
603 memcpy(pass, url.lpszPassword, url.dwPasswordLength * sizeof(WCHAR));
604 pass[url.dwPasswordLength] = 0;
605 }
606 else
607 {
608 pass = 0;
609 }
610
611
612 do {
613 bind->hinternet = InternetOpenA("User Agent", 0, NULL, NULL, 0);
614 if (!bind->hinternet)
615 {
616 hres = HRESULT_FROM_WIN32(GetLastError());
617 break;
618 }
619
620 switch ((DWORD) url.nScheme)
621 {
622 case INTERNET_SCHEME_FTP:
623 if (!url.nPort)
624 url.nPort = INTERNET_DEFAULT_FTP_PORT;
625 dwService = INTERNET_SERVICE_FTP;
626 break;
627
628 case INTERNET_SCHEME_GOPHER:
629 if (!url.nPort)
630 url.nPort = INTERNET_DEFAULT_GOPHER_PORT;
631 dwService = INTERNET_SERVICE_GOPHER;
632 break;
633
634 case INTERNET_SCHEME_HTTP:
635 if (!url.nPort)
636 url.nPort = INTERNET_DEFAULT_HTTP_PORT;
637 dwService = INTERNET_SERVICE_HTTP;
638 break;
639
640 case INTERNET_SCHEME_HTTPS:
641 if (!url.nPort)
642 url.nPort = INTERNET_DEFAULT_HTTPS_PORT;
643 dwService = INTERNET_SERVICE_HTTP;
644 break;
645 }
646
647 bind->hconnect = InternetConnectW(bind->hinternet, host, url.nPort, user, pass,
648 dwService, 0, (DWORD)bind);
649 if (!bind->hconnect)
650 {
651 hres = HRESULT_FROM_WIN32(GetLastError());
652 CloseHandle(bind->hinternet);
653 break;
654 }
655
656 hres = IBindStatusCallback_OnProgress(bind->pbscb, 0, 0, 0x22, NULL);
657 hres = IBindStatusCallback_OnProgress(bind->pbscb, 0, 0, BINDSTATUS_FINDINGRESOURCE, NULL);
658 hres = IBindStatusCallback_OnProgress(bind->pbscb, 0, 0, BINDSTATUS_CONNECTING, NULL);
659 hres = IBindStatusCallback_OnProgress(bind->pbscb, 0, 0, BINDSTATUS_SENDINGREQUEST, NULL);
660
661 bSuccess = FALSE;
662
663 switch (dwService)
664 {
665 case INTERNET_SERVICE_GOPHER:
666 bind->hrequest = GopherOpenFileW(bind->hconnect,
667 path,
668 0,
669 INTERNET_FLAG_RELOAD,
670 0);
671 if (bind->hrequest)
672 bSuccess = TRUE;
673 else
674 hres = HRESULT_FROM_WIN32(GetLastError());
675 break;
676
677 case INTERNET_SERVICE_FTP:
678 bind->hrequest = FtpOpenFileW(bind->hconnect,
679 path,
680 GENERIC_READ,
681 FTP_TRANSFER_TYPE_BINARY |
682 INTERNET_FLAG_TRANSFER_BINARY |
683 INTERNET_FLAG_RELOAD,
684 0);
685 if (bind->hrequest)
686 bSuccess = TRUE;
687 else
688 hres = HRESULT_FROM_WIN32(GetLastError());
689 break;
690
691 case INTERNET_SERVICE_HTTP:
692 bind->hrequest = HttpOpenRequestW(bind->hconnect, NULL, path, NULL, NULL, NULL, 0, (DWORD)bind);
693 if (!bind->hrequest)
694 {
695 hres = HRESULT_FROM_WIN32(GetLastError());
696 }
697 else if (!HttpSendRequestW(bind->hrequest, NULL, 0, NULL, 0))
698 {
699 hres = HRESULT_FROM_WIN32(GetLastError());
700 InternetCloseHandle(bind->hrequest);
701 }
702 else
703 {
704 HttpQueryInfoW(bind->hrequest,
705 HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER,
706 &bind->expected_size,
707 &lensz,
708 NULL);
709 bSuccess = TRUE;
710 }
711 break;
712 }
713 if(bSuccess)
714 {
715 TRACE("res = %ld gle = %08lx url len = %ld\n", hres, GetLastError(), bind->expected_size);
716
717 IBindStatusCallback_OnProgress(bind->pbscb, 0, 0, BINDSTATUS_CACHEFILENAMEAVAILABLE, szFileName);
718
719 while(1) {
720 char buf[4096];
721 DWORD bufread;
722 if(InternetReadFile(bind->hrequest, buf, sizeof(buf), &bufread)) {
723 TRACE("read %ld bytes %s...\n", bufread, debugstr_an(buf, 10));
724 if(bufread == 0) break;
725 hres = Binding_MoreCacheData(bind, buf, bufread);
726 } else
727 break;
728 }
729 InternetCloseHandle(bind->hrequest);
730 hres = S_OK;
731 }
732
733 InternetCloseHandle(bind->hconnect);
734 InternetCloseHandle(bind->hinternet);
735 } while(0);
736
737 Binding_CloseCacheDownload(bind);
738 Binding_FinishedDownload(bind, hres);
739
740 if (user)
741 HeapFree(GetProcessHeap(), 0, user);
742 if (pass)
743 HeapFree(GetProcessHeap(), 0, pass);
744 HeapFree(GetProcessHeap(), 0, path);
745 HeapFree(GetProcessHeap(), 0, host);
746 HeapFree(GetProcessHeap(), 0, urlcopy);
747 }
748 }
749 }
750
751 IBinding_Release((IBinding*)bind);
752
753 return hres;
754 }
755
756 static HRESULT WINAPI URLMonikerImpl_BindToStorage(IMoniker* iface,
757 IBindCtx* pbc,
758 IMoniker* pmkToLeft,
759 REFIID riid,
760 VOID** ppvObject)
761 {
762 URLMonikerImpl *This = (URLMonikerImpl*)iface;
763 WCHAR schema[64];
764 BOOL bret;
765
766 URL_COMPONENTSW url = {sizeof(URL_COMPONENTSW), schema,
767 sizeof(schema)/sizeof(WCHAR), 0, NULL, 0, 0, NULL, 0, NULL, 0, NULL, 0, NULL, 0};
768
769 bret = InternetCrackUrlW(This->URLName, 0, ICU_ESCAPE, &url);
770 if(!bret) {
771 ERR("InternetCrackUrl failed: %ld\n", GetLastError());
772 return E_FAIL;
773 }
774
775 if(url.nScheme == INTERNET_SCHEME_HTTP
776 || url.nScheme== INTERNET_SCHEME_HTTPS
777 || url.nScheme== INTERNET_SCHEME_FTP
778 || url.nScheme == INTERNET_SCHEME_GOPHER)
779 return URLMonikerImpl_BindToStorage_hack(This->URLName, pbc, pmkToLeft, riid, ppvObject);
780
781 TRACE("(%p)->(%p %p %s %p)\n", This, pbc, pmkToLeft, debugstr_guid(riid), ppvObject);
782
783 return start_binding(This->URLName, pbc, riid, ppvObject);
784 }
785
786 /******************************************************************************
787 * URLMoniker_Reduce
788 ******************************************************************************/
789 static HRESULT WINAPI URLMonikerImpl_Reduce(IMoniker* iface,
790 IBindCtx* pbc,
791 DWORD dwReduceHowFar,
792 IMoniker** ppmkToLeft,
793 IMoniker** ppmkReduced)
794 {
795 URLMonikerImpl *This = (URLMonikerImpl *)iface;
796
797 TRACE("(%p,%p,%ld,%p,%p)\n",This,pbc,dwReduceHowFar,ppmkToLeft,ppmkReduced);
798
799 if(!ppmkReduced)
800 return E_INVALIDARG;
801
802 URLMonikerImpl_AddRef(iface);
803 *ppmkReduced = iface;
804 return MK_S_REDUCED_TO_SELF;
805 }
806
807 /******************************************************************************
808 * URLMoniker_ComposeWith
809 ******************************************************************************/
810 static HRESULT WINAPI URLMonikerImpl_ComposeWith(IMoniker* iface,
811 IMoniker* pmkRight,
812 BOOL fOnlyIfNotGeneric,
813 IMoniker** ppmkComposite)
814 {
815 URLMonikerImpl *This = (URLMonikerImpl *)iface;
816 FIXME("(%p)->(%p,%d,%p): stub\n",This,pmkRight,fOnlyIfNotGeneric,ppmkComposite);
817
818 return E_NOTIMPL;
819 }
820
821 /******************************************************************************
822 * URLMoniker_Enum
823 ******************************************************************************/
824 static HRESULT WINAPI URLMonikerImpl_Enum(IMoniker* iface,BOOL fForward, IEnumMoniker** ppenumMoniker)
825 {
826 URLMonikerImpl *This = (URLMonikerImpl *)iface;
827 TRACE("(%p,%d,%p)\n",This,fForward,ppenumMoniker);
828
829 if(!ppenumMoniker)
830 return E_INVALIDARG;
831
832 /* Does not support sub-monikers */
833 *ppenumMoniker = NULL;
834 return S_OK;
835 }
836
837 /******************************************************************************
838 * URLMoniker_IsEqual
839 ******************************************************************************/
840 static HRESULT WINAPI URLMonikerImpl_IsEqual(IMoniker* iface,IMoniker* pmkOtherMoniker)
841 {
842 URLMonikerImpl *This = (URLMonikerImpl *)iface;
843 CLSID clsid;
844 LPOLESTR urlPath;
845 IBindCtx* bind;
846 HRESULT res;
847
848 TRACE("(%p,%p)\n",This,pmkOtherMoniker);
849
850 if(pmkOtherMoniker==NULL)
851 return E_INVALIDARG;
852
853 IMoniker_GetClassID(pmkOtherMoniker,&clsid);
854
855 if(!IsEqualCLSID(&clsid,&CLSID_StdURLMoniker))
856 return S_FALSE;
857
858 res = CreateBindCtx(0,&bind);
859 if(FAILED(res))
860 return res;
861
862 res = S_FALSE;
863 if(SUCCEEDED(IMoniker_GetDisplayName(pmkOtherMoniker,bind,NULL,&urlPath))) {
864 int result = lstrcmpiW(urlPath, This->URLName);
865 CoTaskMemFree(urlPath);
866 if(result == 0)
867 res = S_OK;
868 }
869 IUnknown_Release(bind);
870 return res;
871 }
872
873
874 /******************************************************************************
875 * URLMoniker_Hash
876 ******************************************************************************/
877 static HRESULT WINAPI URLMonikerImpl_Hash(IMoniker* iface,DWORD* pdwHash)
878 {
879 URLMonikerImpl *This = (URLMonikerImpl *)iface;
880
881 int h = 0,i,skip,len;
882 int off = 0;
883 LPOLESTR val;
884
885 TRACE("(%p,%p)\n",This,pdwHash);
886
887 if(!pdwHash)
888 return E_INVALIDARG;
889
890 val = This->URLName;
891 len = lstrlenW(val);
892
893 if(len < 16) {
894 for(i = len ; i > 0; i--) {
895 h = (h * 37) + val[off++];
896 }
897 }
898 else {
899 /* only sample some characters */
900 skip = len / 8;
901 for(i = len; i > 0; i -= skip, off += skip) {
902 h = (h * 39) + val[off];
903 }
904 }
905 *pdwHash = h;
906 return S_OK;
907 }
908
909 /******************************************************************************
910 * URLMoniker_IsRunning
911 ******************************************************************************/
912 static HRESULT WINAPI URLMonikerImpl_IsRunning(IMoniker* iface,
913 IBindCtx* pbc,
914 IMoniker* pmkToLeft,
915 IMoniker* pmkNewlyRunning)
916 {
917 URLMonikerImpl *This = (URLMonikerImpl *)iface;
918 FIXME("(%p)->(%p,%p,%p): stub\n",This,pbc,pmkToLeft,pmkNewlyRunning);
919
920 return E_NOTIMPL;
921 }
922
923 /******************************************************************************
924 * URLMoniker_GetTimeOfLastChange
925 ******************************************************************************/
926 static HRESULT WINAPI URLMonikerImpl_GetTimeOfLastChange(IMoniker* iface,
927 IBindCtx* pbc,
928 IMoniker* pmkToLeft,
929 FILETIME* pFileTime)
930 {
931 URLMonikerImpl *This = (URLMonikerImpl *)iface;
932 FIXME("(%p)->(%p,%p,%p): stub\n",This,pbc,pmkToLeft,pFileTime);
933
934 return E_NOTIMPL;
935 }
936
937 /******************************************************************************
938 * URLMoniker_Inverse
939 ******************************************************************************/
940 static HRESULT WINAPI URLMonikerImpl_Inverse(IMoniker* iface,IMoniker** ppmk)
941 {
942 URLMonikerImpl *This = (URLMonikerImpl *)iface;
943 TRACE("(%p,%p)\n",This,ppmk);
944
945 return MK_E_NOINVERSE;
946 }
947
948 /******************************************************************************
949 * URLMoniker_CommonPrefixWith
950 ******************************************************************************/
951 static HRESULT WINAPI URLMonikerImpl_CommonPrefixWith(IMoniker* iface,IMoniker* pmkOther,IMoniker** ppmkPrefix)
952 {
953 URLMonikerImpl *This = (URLMonikerImpl *)iface;
954 FIXME("(%p)->(%p,%p): stub\n",This,pmkOther,ppmkPrefix);
955
956 return E_NOTIMPL;
957 }
958
959 /******************************************************************************
960 * URLMoniker_RelativePathTo
961 ******************************************************************************/
962 static HRESULT WINAPI URLMonikerImpl_RelativePathTo(IMoniker* iface,IMoniker* pmOther, IMoniker** ppmkRelPath)
963 {
964 URLMonikerImpl *This = (URLMonikerImpl *)iface;
965 FIXME("(%p)->(%p,%p): stub\n",This,pmOther,ppmkRelPath);
966
967 return E_NOTIMPL;
968 }
969
970 /******************************************************************************
971 * URLMoniker_GetDisplayName
972 ******************************************************************************/
973 static HRESULT WINAPI URLMonikerImpl_GetDisplayName(IMoniker* iface,
974 IBindCtx* pbc,
975 IMoniker* pmkToLeft,
976 LPOLESTR *ppszDisplayName)
977 {
978 URLMonikerImpl *This = (URLMonikerImpl *)iface;
979
980 int len;
981
982 TRACE("(%p,%p,%p,%p)\n",This,pbc,pmkToLeft,ppszDisplayName);
983
984 if(!ppszDisplayName)
985 return E_INVALIDARG;
986
987 /* FIXME: If this is a partial URL, try and get a URL moniker from SZ_URLCONTEXT in the bind context,
988 then look at pmkToLeft to try and complete the URL
989 */
990 len = lstrlenW(This->URLName)+1;
991 *ppszDisplayName = CoTaskMemAlloc(len*sizeof(WCHAR));
992 if(!*ppszDisplayName)
993 return E_OUTOFMEMORY;
994 lstrcpyW(*ppszDisplayName, This->URLName);
995 return S_OK;
996 }
997
998 /******************************************************************************
999 * URLMoniker_ParseDisplayName
1000 ******************************************************************************/
1001 static HRESULT WINAPI URLMonikerImpl_ParseDisplayName(IMoniker* iface,
1002 IBindCtx* pbc,
1003 IMoniker* pmkToLeft,
1004 LPOLESTR pszDisplayName,
1005 ULONG* pchEaten,
1006 IMoniker** ppmkOut)
1007 {
1008 URLMonikerImpl *This = (URLMonikerImpl *)iface;
1009 FIXME("(%p)->(%p,%p,%p,%p,%p): stub\n",This,pbc,pmkToLeft,pszDisplayName,pchEaten,ppmkOut);
1010
1011 return E_NOTIMPL;
1012 }
1013
1014 /******************************************************************************
1015 * URLMoniker_IsSystemMoniker
1016 ******************************************************************************/
1017 static HRESULT WINAPI URLMonikerImpl_IsSystemMoniker(IMoniker* iface,DWORD* pwdMksys)
1018 {
1019 URLMonikerImpl *This = (URLMonikerImpl *)iface;
1020 TRACE("(%p,%p)\n",This,pwdMksys);
1021
1022 if(!pwdMksys)
1023 return E_INVALIDARG;
1024
1025 *pwdMksys = MKSYS_URLMONIKER;
1026 return S_OK;
1027 }
1028
1029 /********************************************************************************/
1030 /* Virtual function table for the URLMonikerImpl class which include IPersist,*/
1031 /* IPersistStream and IMoniker functions. */
1032 static const IMonikerVtbl VT_URLMonikerImpl =
1033 {
1034 URLMonikerImpl_QueryInterface,
1035 URLMonikerImpl_AddRef,
1036 URLMonikerImpl_Release,
1037 URLMonikerImpl_GetClassID,
1038 URLMonikerImpl_IsDirty,
1039 URLMonikerImpl_Load,
1040 URLMonikerImpl_Save,
1041 URLMonikerImpl_GetSizeMax,
1042 URLMonikerImpl_BindToObject,
1043 URLMonikerImpl_BindToStorage,
1044 URLMonikerImpl_Reduce,
1045 URLMonikerImpl_ComposeWith,
1046 URLMonikerImpl_Enum,
1047 URLMonikerImpl_IsEqual,
1048 URLMonikerImpl_Hash,
1049 URLMonikerImpl_IsRunning,
1050 URLMonikerImpl_GetTimeOfLastChange,
1051 URLMonikerImpl_Inverse,
1052 URLMonikerImpl_CommonPrefixWith,
1053 URLMonikerImpl_RelativePathTo,
1054 URLMonikerImpl_GetDisplayName,
1055 URLMonikerImpl_ParseDisplayName,
1056 URLMonikerImpl_IsSystemMoniker
1057 };
1058
1059 /******************************************************************************
1060 * URLMoniker_Construct (local function)
1061 *******************************************************************************/
1062 static HRESULT URLMonikerImpl_Construct(URLMonikerImpl* This, LPCOLESTR lpszLeftURLName, LPCOLESTR lpszURLName)
1063 {
1064 HRESULT hres;
1065 DWORD sizeStr = INTERNET_MAX_URL_LENGTH;
1066
1067 TRACE("(%p,%s,%s)\n",This,debugstr_w(lpszLeftURLName),debugstr_w(lpszURLName));
1068 memset(This, 0, sizeof(*This));
1069
1070 /* Initialize the virtual function table. */
1071 This->lpvtbl = &VT_URLMonikerImpl;
1072 This->ref = 0;
1073
1074 if(lpszLeftURLName) {
1075 hres = UrlCombineW(lpszLeftURLName, lpszURLName, NULL, &sizeStr, 0);
1076 if(FAILED(hres)) {
1077 return hres;
1078 }
1079 sizeStr++;
1080 }
1081 else
1082 sizeStr = lstrlenW(lpszURLName)+1;
1083
1084 This->URLName=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(sizeStr));
1085
1086 if (This->URLName==NULL)
1087 return E_OUTOFMEMORY;
1088
1089 if(lpszLeftURLName) {
1090 hres = UrlCombineW(lpszLeftURLName, lpszURLName, This->URLName, &sizeStr, 0);
1091 if(FAILED(hres)) {
1092 HeapFree(GetProcessHeap(), 0, This->URLName);
1093 return hres;
1094 }
1095 }else {
1096 /* FIXME:
1097 * We probably should use CoInternetParseUrl or something similar here.
1098 */
1099
1100 static const WCHAR wszFile[] = {'f','i','l','e',':','/','/',};
1101
1102 /* file protocol is a special case */
1103 if(sizeStr > sizeof(wszFile)/sizeof(WCHAR)
1104 && !memcmp(lpszURLName, wszFile, sizeof(wszFile)))
1105 UrlCanonicalizeW(lpszURLName, This->URLName, &sizeStr, URL_FILE_USE_PATHURL);
1106 else
1107 strcpyW(This->URLName,lpszURLName);
1108 }
1109
1110 URLMON_LockModule();
1111
1112 return S_OK;
1113 }
1114
1115 /***********************************************************************
1116 * CreateAsyncBindCtx (URLMON.@)
1117 */
1118 HRESULT WINAPI CreateAsyncBindCtx(DWORD reserved, IBindStatusCallback *callback,
1119 IEnumFORMATETC *format, IBindCtx **pbind)
1120 {
1121 TRACE("(%08lx %p %p %p)\n", reserved, callback, format, pbind);
1122
1123 if(!callback)
1124 return E_INVALIDARG;
1125
1126 return CreateAsyncBindCtxEx(NULL, 0, callback, format, pbind, 0);
1127 }
1128 /***********************************************************************
1129 * CreateAsyncBindCtxEx (URLMON.@)
1130 *
1131 * Create an asynchronous bind context.
1132 */
1133 HRESULT WINAPI CreateAsyncBindCtxEx(IBindCtx *ibind, DWORD options,
1134 IBindStatusCallback *callback, IEnumFORMATETC *format, IBindCtx** pbind,
1135 DWORD reserved)
1136 {
1137 HRESULT hres;
1138 BIND_OPTS bindopts;
1139 IBindCtx *bctx;
1140
1141 TRACE("(%p %08lx %p %p %p %ld)\n", ibind, options, callback, format, pbind, reserved);
1142
1143 if(!pbind)
1144 return E_INVALIDARG;
1145
1146 if(options)
1147 FIXME("not supported options %08lx", options);
1148 if(format)
1149 FIXME("format is not supported\n");
1150
1151 if(reserved)
1152 WARN("reserved=%ld\n", reserved);
1153
1154 hres = CreateBindCtx(0, &bctx);
1155 if(FAILED(hres))
1156 return hres;
1157
1158 bindopts.cbStruct = sizeof(BIND_OPTS);
1159 bindopts.grfFlags = BIND_MAYBOTHERUSER;
1160 bindopts.grfMode = STGM_READWRITE | STGM_SHARE_EXCLUSIVE;
1161 bindopts.dwTickCountDeadline = 0;
1162 IBindCtx_SetBindOptions(bctx, &bindopts);
1163
1164 if(callback)
1165 RegisterBindStatusCallback(bctx, callback, NULL, 0);
1166
1167 *pbind = bctx;
1168
1169 return S_OK;
1170 }
1171
1172
1173 /***********************************************************************
1174 * CreateURLMoniker (URLMON.@)
1175 *
1176 * Create a url moniker.
1177 *
1178 * PARAMS
1179 * pmkContext [I] Context
1180 * szURL [I] Url to create the moniker for
1181 * ppmk [O] Destination for created moniker.
1182 *
1183 * RETURNS
1184 * Success: S_OK. ppmk contains the created IMoniker object.
1185 * Failure: MK_E_SYNTAX if szURL is not a valid url, or
1186 * E_OUTOFMEMORY if memory allocation fails.
1187 */
1188 HRESULT WINAPI CreateURLMoniker(IMoniker *pmkContext, LPCWSTR szURL, IMoniker **ppmk)
1189 {
1190 URLMonikerImpl *obj;
1191 HRESULT hres;
1192 IID iid = IID_IMoniker;
1193 LPOLESTR lefturl = NULL;
1194
1195 TRACE("(%p, %s, %p)\n", pmkContext, debugstr_w(szURL), ppmk);
1196
1197 if(!(obj = HeapAlloc(GetProcessHeap(), 0, sizeof(*obj))))
1198 return E_OUTOFMEMORY;
1199
1200 if(pmkContext) {
1201 IBindCtx* bind;
1202 DWORD dwMksys = 0;
1203 IMoniker_IsSystemMoniker(pmkContext, &dwMksys);
1204 if(dwMksys == MKSYS_URLMONIKER && SUCCEEDED(CreateBindCtx(0, &bind))) {
1205 IMoniker_GetDisplayName(pmkContext, bind, NULL, &lefturl);
1206 TRACE("lefturl = %s\n", debugstr_w(lefturl));
1207 IBindCtx_Release(bind);
1208 }
1209 }
1210
1211 hres = URLMonikerImpl_Construct(obj, lefturl, szURL);
1212 CoTaskMemFree(lefturl);
1213 if(SUCCEEDED(hres))
1214 hres = URLMonikerImpl_QueryInterface((IMoniker*)obj, &iid, (void**)ppmk);
1215 else
1216 HeapFree(GetProcessHeap(), 0, obj);
1217 return hres;
1218 }
1219
1220 /***********************************************************************
1221 * CoInternetQueryInfo (URLMON.@)
1222 *
1223 * Retrieves information relevant to a specified URL
1224 *
1225 * RETURNS
1226 * S_OK success
1227 * S_FALSE buffer too small
1228 * INET_E_QUERYOPTIONUNKNOWN invalid option
1229 *
1230 */
1231 HRESULT WINAPI CoInternetQueryInfo(LPCWSTR pwzUrl, QUERYOPTION QueryOption,
1232 DWORD dwQueryFlags, LPVOID pvBuffer, DWORD cbBuffer, DWORD * pcbBuffer,
1233 DWORD dwReserved)
1234 {
1235 FIXME("(%s, %x, %lx, %p, %lx, %p, %lx): stub\n", debugstr_w(pwzUrl),
1236 QueryOption, dwQueryFlags, pvBuffer, cbBuffer, pcbBuffer, dwReserved);
1237 return S_OK;
1238 }
1239
1240 /***********************************************************************
1241 * IsAsyncMoniker (URLMON.@)
1242 */
1243 HRESULT WINAPI IsAsyncMoniker(IMoniker *pmk)
1244 {
1245 IUnknown *am;
1246
1247 TRACE("(%p)\n", pmk);
1248 if(!pmk)
1249 return E_INVALIDARG;
1250 if(SUCCEEDED(IMoniker_QueryInterface(pmk, &IID_IAsyncMoniker, (void**)&am))) {
1251 IUnknown_Release(am);
1252 return S_OK;
1253 }
1254 return S_FALSE;
1255 }
1256
1257 /***********************************************************************
1258 * BindAsyncMoniker (URLMON.@)
1259 *
1260 * Bind a bind status callback to an asynchronous URL Moniker.
1261 *
1262 * PARAMS
1263 * pmk [I] Moniker object to bind status callback to
1264 * grfOpt [I] Options, seems not used
1265 * pbsc [I] Status callback to bind
1266 * iidResult [I] Interface to return
1267 * ppvResult [O] Resulting asynchronous moniker object
1268 *
1269 * RETURNS
1270 * Success: S_OK.
1271 * Failure: E_INVALIDARG, if any argument is invalid, or
1272 * E_OUTOFMEMORY if memory allocation fails.
1273 */
1274 HRESULT WINAPI BindAsyncMoniker(IMoniker *pmk, DWORD grfOpt, IBindStatusCallback *pbsc, REFIID iidResult, LPVOID *ppvResult)
1275 {
1276 LPBC pbc = NULL;
1277 HRESULT hr = E_INVALIDARG;
1278
1279 if (pmk && ppvResult)
1280 {
1281 *ppvResult = NULL;
1282
1283 hr = CreateAsyncBindCtx(0, pbsc, NULL, &pbc);
1284 if (hr == NOERROR)
1285 {
1286 hr = IMoniker_BindToObject(pmk, pbc, NULL, iidResult, ppvResult);
1287 IBindCtx_Release(pbc);
1288 }
1289 }
1290 return hr;
1291 }
1292
1293 /***********************************************************************
1294 * RegisterBindStatusCallback (URLMON.@)
1295 *
1296 * Register a bind status callback.
1297 *
1298 * PARAMS
1299 * pbc [I] Binding context
1300 * pbsc [I] Callback to register
1301 * ppbscPrevious [O] Destination for previous callback
1302 * dwReserved [I] Reserved, must be 0.
1303 *
1304 * RETURNS
1305 * Success: S_OK.
1306 * Failure: E_INVALIDARG, if any argument is invalid, or
1307 * E_OUTOFMEMORY if memory allocation fails.
1308 */
1309 HRESULT WINAPI RegisterBindStatusCallback(
1310 IBindCtx *pbc,
1311 IBindStatusCallback *pbsc,
1312 IBindStatusCallback **ppbscPrevious,
1313 DWORD dwReserved)
1314 {
1315 IBindStatusCallback *prev;
1316
1317 TRACE("(%p,%p,%p,%lu)\n", pbc, pbsc, ppbscPrevious, dwReserved);
1318
1319 if (pbc == NULL || pbsc == NULL)
1320 return E_INVALIDARG;
1321
1322 if (SUCCEEDED(IBindCtx_GetObjectParam(pbc, (LPOLESTR)BSCBHolder, (IUnknown **)&prev)))
1323 {
1324 IBindCtx_RevokeObjectParam(pbc, (LPOLESTR)BSCBHolder);
1325 if (ppbscPrevious)
1326 *ppbscPrevious = prev;
1327 else
1328 IBindStatusCallback_Release(prev);
1329 }
1330
1331 return IBindCtx_RegisterObjectParam(pbc, (LPOLESTR)BSCBHolder, (IUnknown *)pbsc);
1332 }
1333
1334 /***********************************************************************
1335 * RevokeBindStatusCallback (URLMON.@)
1336 *
1337 * Unregister a bind status callback.
1338 *
1339 * pbc [I] Binding context
1340 * pbsc [I] Callback to unregister
1341 *
1342 * RETURNS
1343 * Success: S_OK.
1344 * Failure: E_INVALIDARG, if any argument is invalid, or
1345 * E_FAIL if pbsc wasn't registered with pbc.
1346 */
1347 HRESULT WINAPI RevokeBindStatusCallback(
1348 IBindCtx *pbc,
1349 IBindStatusCallback *pbsc)
1350 {
1351 IBindStatusCallback *callback;
1352 HRESULT hr = E_FAIL;
1353
1354 TRACE("(%p,%p)\n", pbc, pbsc);
1355
1356 if (pbc == NULL || pbsc == NULL)
1357 return E_INVALIDARG;
1358
1359 if (SUCCEEDED(IBindCtx_GetObjectParam(pbc, (LPOLESTR)BSCBHolder, (IUnknown **)&callback)))
1360 {
1361 if (callback == pbsc)
1362 {
1363 IBindCtx_RevokeObjectParam(pbc, (LPOLESTR)BSCBHolder);
1364 hr = S_OK;
1365 }
1366 IBindStatusCallback_Release(pbsc);
1367 }
1368
1369 return hr;
1370 }
1371
1372 /***********************************************************************
1373 * URLDownloadToFileA (URLMON.@)
1374 *
1375 * Downloads URL szURL to rile szFileName and call lpfnCB callback to
1376 * report progress.
1377 *
1378 * PARAMS
1379 * pCaller [I] controlling IUnknown interface.
1380 * szURL [I] URL of the file to download
1381 * szFileName [I] file name to store the content of the URL
1382 * dwReserved [I] reserved - set to 0
1383 * lpfnCB [I] callback for progress report
1384 *
1385 * RETURNS
1386 * S_OK on success
1387 * E_OUTOFMEMORY when going out of memory
1388 */
1389 HRESULT WINAPI URLDownloadToFileA(LPUNKNOWN pCaller,
1390 LPCSTR szURL,
1391 LPCSTR szFileName,
1392 DWORD dwReserved,
1393 LPBINDSTATUSCALLBACK lpfnCB)
1394 {
1395 UNICODE_STRING szURL_w, szFileName_w;
1396
1397 if ((szURL == NULL) || (szFileName == NULL)) {
1398 FIXME("(%p,%s,%s,%08lx,%p) cannot accept NULL strings !\n", pCaller, debugstr_a(szURL), debugstr_a(szFileName), dwReserved, lpfnCB);
1399 return E_INVALIDARG; /* The error code is not specified in this case... */
1400 }
1401
1402 if (RtlCreateUnicodeStringFromAsciiz(&szURL_w, szURL)) {
1403 if (RtlCreateUnicodeStringFromAsciiz(&szFileName_w, szFileName)) {
1404 HRESULT ret = URLDownloadToFileW(pCaller, szURL_w.Buffer, szFileName_w.Buffer, dwReserved, lpfnCB);
1405
1406 RtlFreeUnicodeString(&szURL_w);
1407 RtlFreeUnicodeString(&szFileName_w);
1408
1409 return ret;
1410 } else {
1411 RtlFreeUnicodeString(&szURL_w);
1412 }
1413 }
1414
1415 FIXME("(%p,%s,%s,%08lx,%p) could not allocate W strings !\n", pCaller, szURL, szFileName, dwReserved, lpfnCB);
1416 return E_OUTOFMEMORY;
1417 }
1418
1419 /***********************************************************************
1420 * URLDownloadToFileW (URLMON.@)
1421 *
1422 * Downloads URL szURL to rile szFileName and call lpfnCB callback to
1423 * report progress.
1424 *
1425 * PARAMS
1426 * pCaller [I] controlling IUnknown interface.
1427 * szURL [I] URL of the file to download
1428 * szFileName [I] file name to store the content of the URL
1429 * dwReserved [I] reserved - set to 0
1430 * lpfnCB [I] callback for progress report
1431 *
1432 * RETURNS
1433 * S_OK on success
1434 * E_OUTOFMEMORY when going out of memory
1435 */
1436 HRESULT WINAPI URLDownloadToFileW(LPUNKNOWN pCaller,
1437 LPCWSTR szURL,
1438 LPCWSTR szFileName,
1439 DWORD dwReserved,
1440 LPBINDSTATUSCALLBACK lpfnCB)
1441 {
1442 HINTERNET hinternet, hcon, hreq;
1443 BOOL r;
1444 CHAR buffer[0x1000];
1445 DWORD sz, total, written;
1446 DWORD total_size = 0xFFFFFFFF, arg_size = sizeof(total_size);
1447 URL_COMPONENTSW url;
1448 WCHAR host[0x80], path[0x100];
1449 HANDLE hfile;
1450 static const WCHAR wszAppName[]={'u','r','l','m','o','n','.','d','l','l',0};
1451
1452 /* Note: all error codes would need to be checked agains real Windows behaviour... */
1453 TRACE("(%p,%s,%s,%08lx,%p) stub!\n", pCaller, debugstr_w(szURL), debugstr_w(szFileName), dwReserved, lpfnCB);
1454
1455 if ((szURL == NULL) || (szFileName == NULL)) {
1456 FIXME(" cannot accept NULL strings !\n");
1457 return E_INVALIDARG;
1458 }
1459
1460 /* Would be better to use the application name here rather than 'urlmon' :-/ */
1461 hinternet = InternetOpenW(wszAppName, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
1462 if (hinternet == NULL) {
1463 return E_OUTOFMEMORY;
1464 }
1465
1466 memset(&url, 0, sizeof(url));
1467 url.dwStructSize = sizeof(url);
1468 url.lpszHostName = host;
1469 url.dwHostNameLength = sizeof(host);
1470 url.lpszUrlPath = path;
1471 url.dwUrlPathLength = sizeof(path);
1472
1473 if (!InternetCrackUrlW(szURL, 0, 0, &url)) {
1474 InternetCloseHandle(hinternet);
1475 return E_OUTOFMEMORY;
1476 }
1477
1478 if (lpfnCB) {
1479 if (IBindStatusCallback_OnProgress(lpfnCB, 0, 0, BINDSTATUS_CONNECTING, url.lpszHostName) == E_ABORT) {
1480 InternetCloseHandle(hinternet);
1481 return S_OK;
1482 }
1483 }
1484
1485 hcon = InternetConnectW(hinternet, url.lpszHostName, url.nPort,
1486 url.lpszUserName, url.lpszPassword,
1487 INTERNET_SERVICE_HTTP, 0, 0);
1488 if (!hcon) {
1489 InternetCloseHandle(hinternet);
1490 return E_OUTOFMEMORY;
1491 }
1492
1493 hreq = HttpOpenRequestW(hcon, NULL, url.lpszUrlPath, NULL, NULL, NULL, 0, 0);
1494 if (!hreq) {
1495 InternetCloseHandle(hinternet);
1496 InternetCloseHandle(hcon);
1497 return E_OUTOFMEMORY;
1498 }
1499
1500 if (!HttpSendRequestW(hreq, NULL, 0, NULL, 0)) {
1501 InternetCloseHandle(hinternet);
1502 InternetCloseHandle(hcon);
1503 InternetCloseHandle(hreq);
1504 return E_OUTOFMEMORY;
1505 }
1506
1507 if (HttpQueryInfoW(hreq, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER,
1508 &total_size, &arg_size, NULL)) {
1509 TRACE(" total size : %ld\n", total_size);
1510 }
1511
1512 hfile = CreateFileW(szFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
1513 FILE_ATTRIBUTE_NORMAL, NULL );
1514 if (hfile == INVALID_HANDLE_VALUE) {
1515 return E_ACCESSDENIED;
1516 }
1517
1518 if (lpfnCB) {
1519 if (IBindStatusCallback_OnProgress(lpfnCB, 0, total_size != 0xFFFFFFFF ? total_size : 0,
1520 BINDSTATUS_BEGINDOWNLOADDATA, szURL) == E_ABORT) {
1521 InternetCloseHandle(hreq);
1522 InternetCloseHandle(hcon);
1523 InternetCloseHandle(hinternet);
1524 CloseHandle(hfile);
1525 return S_OK;
1526 }
1527 }
1528
1529 total = 0;
1530 while (1) {
1531 r = InternetReadFile(hreq, buffer, sizeof(buffer), &sz);
1532 if (!r) {
1533 InternetCloseHandle(hreq);
1534 InternetCloseHandle(hcon);
1535 InternetCloseHandle(hinternet);
1536
1537 CloseHandle(hfile);
1538 return E_OUTOFMEMORY;
1539 }
1540 if (!sz)
1541 break;
1542
1543 total += sz;
1544
1545 if (lpfnCB) {
1546 if (IBindStatusCallback_OnProgress(lpfnCB, total, total_size != 0xFFFFFFFF ? total_size : 0,
1547 BINDSTATUS_DOWNLOADINGDATA, szURL) == E_ABORT) {
1548 InternetCloseHandle(hreq);
1549 InternetCloseHandle(hcon);
1550 InternetCloseHandle(hinternet);
1551 CloseHandle(hfile);
1552 return S_OK;
1553 }
1554 }
1555
1556 if (!WriteFile(hfile, buffer, sz, &written, NULL)) {
1557 InternetCloseHandle(hreq);
1558 InternetCloseHandle(hcon);
1559 InternetCloseHandle(hinternet);
1560
1561 CloseHandle(hfile);
1562 return E_OUTOFMEMORY;
1563 }
1564 }
1565
1566 if (lpfnCB) {
1567 if (IBindStatusCallback_OnProgress(lpfnCB, total, total_size != 0xFFFFFFFF ? total_size : 0,
1568 BINDSTATUS_ENDDOWNLOADDATA, szURL) == E_ABORT) {
1569 InternetCloseHandle(hreq);
1570 InternetCloseHandle(hcon);
1571 InternetCloseHandle(hinternet);
1572 CloseHandle(hfile);
1573 return S_OK;
1574 }
1575 }
1576
1577 InternetCloseHandle(hreq);
1578 InternetCloseHandle(hcon);
1579 InternetCloseHandle(hinternet);
1580
1581 CloseHandle(hfile);
1582
1583 return S_OK;
1584 }
1585
1586 /***********************************************************************
1587 * URLDownloadToCacheFileA (URLMON.@)
1588 */
1589 HRESULT WINAPI URLDownloadToCacheFileA(LPUNKNOWN lpUnkCaller, LPCSTR szURL, LPSTR szFileName,
1590 DWORD dwBufLength, DWORD dwReserved, LPBINDSTATUSCALLBACK pBSC)
1591 {
1592 LPWSTR url = NULL, file_name = NULL;
1593 int len;
1594 HRESULT hres;
1595
1596 TRACE("(%p %s %p %ld %ld %p)\n", lpUnkCaller, debugstr_a(szURL), szFileName,
1597 dwBufLength, dwReserved, pBSC);
1598
1599 if(szURL) {
1600 len = MultiByteToWideChar(CP_ACP, 0, szURL, -1, NULL, 0);
1601 url = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
1602 MultiByteToWideChar(CP_ACP, 0, szURL, -1, url, -1);
1603 }
1604
1605 if(szFileName)
1606 file_name = HeapAlloc(GetProcessHeap(), 0, dwBufLength*sizeof(WCHAR));
1607
1608 hres = URLDownloadToCacheFileW(lpUnkCaller, url, file_name, dwBufLength*sizeof(WCHAR),
1609 dwReserved, pBSC);
1610
1611 if(SUCCEEDED(hres) && file_name)
1612 WideCharToMultiByte(CP_ACP, 0, file_name, -1, szFileName, dwBufLength, NULL, NULL);
1613
1614 HeapFree(GetProcessHeap(), 0, url);
1615 HeapFree(GetProcessHeap(), 0, file_name);
1616
1617 return hres;
1618 }
1619
1620 /***********************************************************************
1621 * URLDownloadToCacheFileW (URLMON.@)
1622 */
1623 HRESULT WINAPI URLDownloadToCacheFileW(LPUNKNOWN lpUnkCaller, LPCWSTR szURL, LPWSTR szFileName,
1624 DWORD dwBufLength, DWORD dwReserved, LPBINDSTATUSCALLBACK pBSC)
1625 {
1626 FIXME("(%p %s %p %ld %ld %p)\n", lpUnkCaller, debugstr_w(szURL), szFileName,
1627 dwBufLength, dwReserved, pBSC);
1628 return E_NOTIMPL;
1629 }
1630
1631 /***********************************************************************
1632 * HlinkSimpleNavigateToString (URLMON.@)
1633 */
1634 HRESULT WINAPI HlinkSimpleNavigateToString( LPCWSTR szTarget,
1635 LPCWSTR szLocation, LPCWSTR szTargetFrameName, IUnknown *pUnk,
1636 IBindCtx *pbc, IBindStatusCallback *pbsc, DWORD grfHLNF, DWORD dwReserved)
1637 {
1638 FIXME("%s\n", debugstr_w( szTarget ) );
1639 return E_NOTIMPL;
1640 }
1641
1642 /***********************************************************************
1643 * HlinkNavigateString (URLMON.@)
1644 */
1645 HRESULT WINAPI HlinkNavigateString( IUnknown *pUnk, LPCWSTR szTarget )
1646 {
1647 TRACE("%p %s\n", pUnk, debugstr_w( szTarget ) );
1648 return HlinkSimpleNavigateToString(
1649 szTarget, NULL, NULL, pUnk, NULL, NULL, 0, 0 );
1650 }
1651
1652 /***********************************************************************
1653 * GetSoftwareUpdateInfo (URLMON.@)
1654 */
1655 HRESULT WINAPI GetSoftwareUpdateInfo( LPCWSTR szDistUnit, LPSOFTDISTINFO psdi )
1656 {
1657 FIXME("%s %p\n", debugstr_w(szDistUnit), psdi );
1658 return E_FAIL;
1659 }