msxml3: Add missing values for get_nodeName.
[wine.git] / dlls / urlmon / umon.c
blob5981a1c96d06aa2d77a47ac12177d6858da0a726
1 /*
2 * UrlMon
4 * Copyright 1999 Ulrich Czekalla for Corel Corporation
5 * Copyright 2002 Huw D M Davies for CodeWeavers
6 * Copyright 2005 Jacek Caban for CodeWeavers
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.
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.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include <stdio.h>
25 #include "urlmon_main.h"
27 #include "winreg.h"
28 #include "winternl.h"
29 #include "wininet.h"
30 #include "shlwapi.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
36 /* native urlmon.dll uses this key, too */
37 static WCHAR BSCBHolder[] = { '_','B','S','C','B','_','H','o','l','d','e','r','_',0 };
39 /*static BOOL registered_wndclass = FALSE;*/
41 typedef struct {
42 const IBindingVtbl *lpVtbl;
44 LONG ref;
46 LPWSTR URLName;
48 HWND hwndCallback;
49 IBindCtx *pBC;
50 HINTERNET hinternet, hconnect, hrequest;
51 HANDLE hCacheFile;
52 IUMCacheStream *pstrCache;
53 IBindStatusCallback *pbscb;
54 DWORD total_read, expected_size;
55 } Binding;
57 static HRESULT WINAPI Binding_QueryInterface(IBinding* iface, REFIID riid, void **ppvObject)
59 Binding *This = (Binding*)iface;
61 TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(riid), ppvObject);
63 if((This == NULL) || (ppvObject == NULL))
64 return E_INVALIDARG;
66 if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IBinding, riid)) {
67 *ppvObject = iface;
68 IBinding_AddRef(iface);
69 return S_OK;
72 *ppvObject = NULL;
73 return E_NOINTERFACE;
76 static ULONG WINAPI Binding_AddRef(IBinding* iface)
78 Binding *This = (Binding*)iface;
79 ULONG ref = InterlockedIncrement(&This->ref);
81 TRACE("(%p) ref=%d\n", This, ref);
83 return ref;
86 static ULONG WINAPI Binding_Release(IBinding* iface)
88 Binding *This = (Binding*)iface;
89 ULONG ref = InterlockedDecrement(&This->ref);
91 TRACE("(%p) ref=%d\n",This, ref);
93 if(!ref) {
94 heap_free(This->URLName);
95 if (This->hCacheFile)
96 CloseHandle(This->hCacheFile);
97 if (This->pstrCache)
99 UMCloseCacheFileStream(This->pstrCache);
100 IStream_Release((IStream *)This->pstrCache);
102 if (This->pbscb)
103 IBindStatusCallback_Release(This->pbscb);
105 heap_free(This);
107 URLMON_UnlockModule();
110 return ref;
113 static HRESULT WINAPI Binding_Abort(IBinding* iface)
115 Binding *This = (Binding*)iface;
117 FIXME("(%p): stub\n", This);
119 return E_NOTIMPL;
122 static HRESULT WINAPI Binding_GetBindResult(IBinding* iface, CLSID* pclsidProtocol, DWORD* pdwResult, LPOLESTR* pszResult, DWORD* pdwReserved)
124 Binding *This = (Binding*)iface;
126 FIXME("(%p)->(%p, %p, %p, %p): stub\n", This, pclsidProtocol, pdwResult, pszResult, pdwReserved);
128 return E_NOTIMPL;
131 static HRESULT WINAPI Binding_GetPriority(IBinding* iface, LONG* pnPriority)
133 Binding *This = (Binding*)iface;
135 FIXME("(%p)->(%p): stub\n", This, pnPriority);
137 return E_NOTIMPL;
140 static HRESULT WINAPI Binding_Resume(IBinding* iface)
142 Binding *This = (Binding*)iface;
144 FIXME("(%p): stub\n", This);
146 return E_NOTIMPL;
149 static HRESULT WINAPI Binding_SetPriority(IBinding* iface, LONG nPriority)
151 Binding *This = (Binding*)iface;
153 FIXME("(%p)->(%d): stub\n", This, nPriority);
155 return E_NOTIMPL;
158 static HRESULT WINAPI Binding_Suspend(IBinding* iface)
160 Binding *This = (Binding*)iface;
162 FIXME("(%p): stub\n", This);
164 return E_NOTIMPL;
167 static void Binding_CloseCacheDownload(Binding *This)
169 CloseHandle(This->hCacheFile);
170 This->hCacheFile = 0;
171 UMCloseCacheFileStream(This->pstrCache);
172 IStream_Release((IStream *)This->pstrCache);
173 This->pstrCache = 0;
176 static HRESULT Binding_MoreCacheData(Binding *This, const char *buf, DWORD dwBytes)
178 DWORD written;
180 if (WriteFile(This->hCacheFile, buf, dwBytes, &written, NULL) && written == dwBytes)
182 HRESULT hr;
184 This->total_read += written;
185 hr = IBindStatusCallback_OnProgress(This->pbscb,
186 This->total_read + written,
187 This->expected_size,
188 (This->total_read == written) ?
189 BINDSTATUS_BEGINDOWNLOADDATA :
190 BINDSTATUS_DOWNLOADINGDATA,
191 This->URLName);
192 if (!hr)
194 STGMEDIUM stg;
195 FORMATETC fmt;
197 fmt.cfFormat = 0;
198 fmt.ptd = NULL;
199 fmt.dwAspect = 0;
200 fmt.lindex = -1;
201 fmt.tymed = TYMED_ISTREAM;
203 stg.tymed = TYMED_ISTREAM;
204 stg.u.pstm = (IStream *)This->pstrCache;
205 stg.pUnkForRelease = NULL;
207 hr = IBindStatusCallback_OnDataAvailable(This->pbscb,
208 (This->total_read == written) ?
209 BSCF_FIRSTDATANOTIFICATION :
210 BSCF_INTERMEDIATEDATANOTIFICATION,
211 This->total_read + written,
212 &fmt,
213 &stg);
215 if (written < dwBytes)
216 return STG_E_MEDIUMFULL;
217 else
218 return hr;
220 return HRESULT_FROM_WIN32(GetLastError());
223 static void Binding_FinishedDownload(Binding *This, HRESULT hr)
225 STGMEDIUM stg;
226 FORMATETC fmt;
228 fmt.ptd = NULL;
229 fmt.dwAspect = 0;
230 fmt.lindex = -1;
231 fmt.tymed = TYMED_ISTREAM;
233 stg.tymed = TYMED_ISTREAM;
234 stg.u.pstm = (IStream *)This->pstrCache;
235 stg.pUnkForRelease = NULL;
237 IBindStatusCallback_OnProgress(This->pbscb, This->total_read, This->expected_size,
238 BINDSTATUS_ENDDOWNLOADDATA, This->URLName);
239 IBindStatusCallback_OnDataAvailable(This->pbscb, BSCF_LASTDATANOTIFICATION, This->total_read, &fmt, &stg);
240 if (hr)
242 WCHAR *pwchError = 0;
244 FormatMessageW (FORMAT_MESSAGE_FROM_SYSTEM |
245 FORMAT_MESSAGE_ALLOCATE_BUFFER,
246 NULL, (DWORD) hr,
247 0, (LPWSTR) &pwchError,
248 0, NULL);
249 if (!pwchError)
251 static const WCHAR achFormat[] = { '%', '0', '8', 'x', 0 };
253 pwchError =(WCHAR *) LocalAlloc(LMEM_FIXED, sizeof(WCHAR) * 9);
254 wsprintfW(pwchError, achFormat, hr);
256 IBindStatusCallback_OnStopBinding(This->pbscb, hr, pwchError);
257 LocalFree(pwchError);
259 else
261 IBindStatusCallback_OnStopBinding(This->pbscb, hr, NULL);
263 IBindStatusCallback_Release(This->pbscb);
264 This->pbscb = 0;
267 static const IBindingVtbl BindingVtbl =
269 Binding_QueryInterface,
270 Binding_AddRef,
271 Binding_Release,
272 Binding_Abort,
273 Binding_Suspend,
274 Binding_Resume,
275 Binding_SetPriority,
276 Binding_GetPriority,
277 Binding_GetBindResult
280 /* filemoniker data structure */
281 typedef struct {
283 const IMonikerVtbl* lpvtbl; /* VTable relative to the IMoniker interface.*/
285 LONG ref; /* reference counter for this object */
287 LPOLESTR URLName; /* URL string identified by this URLmoniker */
288 } URLMonikerImpl;
290 /*******************************************************************************
291 * URLMoniker_QueryInterface
292 *******************************************************************************/
293 static HRESULT WINAPI URLMonikerImpl_QueryInterface(IMoniker* iface,REFIID riid,void** ppvObject)
295 URLMonikerImpl *This = (URLMonikerImpl *)iface;
297 TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppvObject);
299 /* Perform a sanity check on the parameters.*/
300 if ( (This==0) || (ppvObject==0) )
301 return E_INVALIDARG;
303 /* Initialize the return parameter */
304 *ppvObject = 0;
306 /* Compare the riid with the interface IDs implemented by this object.*/
307 if (IsEqualIID(&IID_IUnknown, riid) ||
308 IsEqualIID(&IID_IPersist, riid) ||
309 IsEqualIID(&IID_IPersistStream,riid) ||
310 IsEqualIID(&IID_IMoniker, riid)
312 *ppvObject = iface;
314 /* Check that we obtained an interface.*/
315 if ((*ppvObject)==0)
316 return E_NOINTERFACE;
318 /* Query Interface always increases the reference count by one when it is successful */
319 IMoniker_AddRef(iface);
321 return S_OK;
324 /******************************************************************************
325 * URLMoniker_AddRef
326 ******************************************************************************/
327 static ULONG WINAPI URLMonikerImpl_AddRef(IMoniker* iface)
329 URLMonikerImpl *This = (URLMonikerImpl *)iface;
330 ULONG refCount = InterlockedIncrement(&This->ref);
332 TRACE("(%p) ref=%u\n",This, refCount);
334 return refCount;
337 /******************************************************************************
338 * URLMoniker_Release
339 ******************************************************************************/
340 static ULONG WINAPI URLMonikerImpl_Release(IMoniker* iface)
342 URLMonikerImpl *This = (URLMonikerImpl *)iface;
343 ULONG refCount = InterlockedDecrement(&This->ref);
345 TRACE("(%p) ref=%u\n",This, refCount);
347 /* destroy the object if there's no more reference on it */
348 if (!refCount) {
349 heap_free(This->URLName);
350 heap_free(This);
352 URLMON_UnlockModule();
355 return refCount;
359 /******************************************************************************
360 * URLMoniker_GetClassID
361 ******************************************************************************/
362 static HRESULT WINAPI URLMonikerImpl_GetClassID(IMoniker* iface,
363 CLSID *pClassID)/* Pointer to CLSID of object */
365 URLMonikerImpl *This = (URLMonikerImpl *)iface;
367 TRACE("(%p,%p)\n",This,pClassID);
369 if (pClassID==NULL)
370 return E_POINTER;
371 /* Windows always returns CLSID_StdURLMoniker */
372 *pClassID = CLSID_StdURLMoniker;
373 return S_OK;
376 /******************************************************************************
377 * URLMoniker_IsDirty
378 ******************************************************************************/
379 static HRESULT WINAPI URLMonikerImpl_IsDirty(IMoniker* iface)
381 URLMonikerImpl *This = (URLMonikerImpl *)iface;
382 /* Note that the OLE-provided implementations of the IPersistStream::IsDirty
383 method in the OLE-provided moniker interfaces always return S_FALSE because
384 their internal state never changes. */
386 TRACE("(%p)\n",This);
388 return S_FALSE;
391 /******************************************************************************
392 * URLMoniker_Load
394 * NOTE
395 * Writes a ULONG containing length of unicode string, followed
396 * by that many unicode characters
397 ******************************************************************************/
398 static HRESULT WINAPI URLMonikerImpl_Load(IMoniker* iface,IStream* pStm)
400 URLMonikerImpl *This = (URLMonikerImpl *)iface;
402 HRESULT res;
403 ULONG size;
404 ULONG got;
405 TRACE("(%p,%p)\n",This,pStm);
407 if(!pStm)
408 return E_INVALIDARG;
410 res = IStream_Read(pStm, &size, sizeof(ULONG), &got);
411 if(SUCCEEDED(res)) {
412 if(got == sizeof(ULONG)) {
413 heap_free(This->URLName);
414 This->URLName = heap_alloc(size);
415 if(!This->URLName)
416 res = E_OUTOFMEMORY;
417 else {
418 res = IStream_Read(pStm, This->URLName, size, NULL);
419 This->URLName[size/sizeof(WCHAR) - 1] = 0;
422 else
423 res = E_FAIL;
425 return res;
428 /******************************************************************************
429 * URLMoniker_Save
430 ******************************************************************************/
431 static HRESULT WINAPI URLMonikerImpl_Save(IMoniker* iface,
432 IStream* pStm,/* pointer to the stream where the object is to be saved */
433 BOOL fClearDirty)/* Specifies whether to clear the dirty flag */
435 URLMonikerImpl *This = (URLMonikerImpl *)iface;
437 HRESULT res;
438 ULONG size;
439 TRACE("(%p,%p,%d)\n",This,pStm,fClearDirty);
441 if(!pStm)
442 return E_INVALIDARG;
444 size = (strlenW(This->URLName) + 1)*sizeof(WCHAR);
445 res=IStream_Write(pStm,&size,sizeof(ULONG),NULL);
446 if(SUCCEEDED(res))
447 res=IStream_Write(pStm,This->URLName,size,NULL);
448 return res;
452 /******************************************************************************
453 * URLMoniker_GetSizeMax
454 ******************************************************************************/
455 static HRESULT WINAPI URLMonikerImpl_GetSizeMax(IMoniker* iface,
456 ULARGE_INTEGER* pcbSize)/* Pointer to size of stream needed to save object */
458 URLMonikerImpl *This = (URLMonikerImpl *)iface;
460 TRACE("(%p,%p)\n",This,pcbSize);
462 if(!pcbSize)
463 return E_INVALIDARG;
465 pcbSize->QuadPart = sizeof(ULONG) + ((strlenW(This->URLName)+1) * sizeof(WCHAR));
466 return S_OK;
469 /******************************************************************************
470 * URLMoniker_BindToObject
471 ******************************************************************************/
472 static HRESULT WINAPI URLMonikerImpl_BindToObject(IMoniker* iface,
473 IBindCtx* pbc,
474 IMoniker* pmkToLeft,
475 REFIID riid,
476 VOID** ppv)
478 URLMonikerImpl *This = (URLMonikerImpl *)iface;
479 IRunningObjectTable *obj_tbl;
480 HRESULT hres;
482 TRACE("(%p)->(%p,%p,%s,%p): stub\n", This, pbc, pmkToLeft, debugstr_guid(riid), ppv);
484 hres = IBindCtx_GetRunningObjectTable(pbc, &obj_tbl);
485 if(SUCCEEDED(hres)) {
486 FIXME("use running object table\n");
487 IRunningObjectTable_Release(obj_tbl);
490 return bind_to_object(iface, This->URLName, pbc, riid, ppv);
493 /******************************************************************************
494 * URLMoniker_BindToStorage
495 ******************************************************************************/
496 static HRESULT URLMonikerImpl_BindToStorage_hack(LPCWSTR URLName,
497 IBindCtx* pbc,
498 REFIID riid,
499 VOID** ppvObject)
501 HRESULT hres;
502 BINDINFO bi;
503 DWORD bindf;
504 WCHAR szFileName[MAX_PATH + 1];
505 Binding *bind;
506 int len;
508 WARN("(%s %p %s %p)\n", debugstr_w(URLName), pbc, debugstr_guid(riid), ppvObject);
510 if(!IsEqualIID(&IID_IStream, riid)) {
511 FIXME("unsupported iid\n");
512 return E_NOTIMPL;
515 bind = heap_alloc_zero(sizeof(Binding));
516 bind->lpVtbl = &BindingVtbl;
517 bind->ref = 1;
518 URLMON_LockModule();
520 len = lstrlenW(URLName)+1;
521 bind->URLName = heap_alloc(len*sizeof(WCHAR));
522 memcpy(bind->URLName, URLName, len*sizeof(WCHAR));
524 hres = UMCreateStreamOnCacheFile(bind->URLName, 0, szFileName, &bind->hCacheFile, &bind->pstrCache);
526 if(SUCCEEDED(hres)) {
527 TRACE("Created stream...\n");
529 *ppvObject = (void *) bind->pstrCache;
530 IStream_AddRef((IStream *) bind->pstrCache);
532 hres = IBindCtx_GetObjectParam(pbc, BSCBHolder, (IUnknown**)&bind->pbscb);
533 if(SUCCEEDED(hres)) {
534 TRACE("Got IBindStatusCallback...\n");
536 memset(&bi, 0, sizeof(bi));
537 bi.cbSize = sizeof(bi);
538 bindf = 0;
539 hres = IBindStatusCallback_GetBindInfo(bind->pbscb, &bindf, &bi);
540 if(SUCCEEDED(hres)) {
541 URL_COMPONENTSW url;
542 WCHAR *host, *path, *user, *pass;
543 DWORD lensz = sizeof(bind->expected_size);
544 DWORD dwService = 0;
545 BOOL bSuccess;
547 TRACE("got bindinfo. bindf = %08x extrainfo = %s bindinfof = %08x bindverb = %08x iid %s\n",
548 bindf, debugstr_w(bi.szExtraInfo), bi.grfBindInfoF, bi.dwBindVerb, debugstr_guid(&bi.iid));
549 hres = IBindStatusCallback_OnStartBinding(bind->pbscb, 0, (IBinding*)bind);
550 TRACE("OnStartBinding rets %08x\n", hres);
552 bind->expected_size = 0;
553 bind->total_read = 0;
555 memset(&url, 0, sizeof(url));
556 url.dwStructSize = sizeof(url);
557 url.dwSchemeLength = url.dwHostNameLength = url.dwUrlPathLength = url.dwUserNameLength = url.dwPasswordLength = 1;
558 InternetCrackUrlW(URLName, 0, ICU_ESCAPE, &url);
559 host = heap_alloc((url.dwHostNameLength + 1) * sizeof(WCHAR));
560 memcpy(host, url.lpszHostName, url.dwHostNameLength * sizeof(WCHAR));
561 host[url.dwHostNameLength] = '\0';
562 path = heap_alloc((url.dwUrlPathLength + 1) * sizeof(WCHAR));
563 memcpy(path, url.lpszUrlPath, url.dwUrlPathLength * sizeof(WCHAR));
564 path[url.dwUrlPathLength] = '\0';
565 if (url.dwUserNameLength)
567 user = heap_alloc(((url.dwUserNameLength + 1) * sizeof(WCHAR)));
568 memcpy(user, url.lpszUserName, url.dwUserNameLength * sizeof(WCHAR));
569 user[url.dwUserNameLength] = 0;
571 else
573 user = 0;
575 if (url.dwPasswordLength)
577 pass = heap_alloc(((url.dwPasswordLength + 1) * sizeof(WCHAR)));
578 memcpy(pass, url.lpszPassword, url.dwPasswordLength * sizeof(WCHAR));
579 pass[url.dwPasswordLength] = 0;
581 else
583 pass = 0;
587 do {
588 bind->hinternet = InternetOpenA("User Agent", 0, NULL, NULL, 0);
589 if (!bind->hinternet)
591 hres = HRESULT_FROM_WIN32(GetLastError());
592 break;
595 switch ((DWORD) url.nScheme)
597 case INTERNET_SCHEME_FTP:
598 if (!url.nPort)
599 url.nPort = INTERNET_DEFAULT_FTP_PORT;
600 dwService = INTERNET_SERVICE_FTP;
601 break;
603 case INTERNET_SCHEME_GOPHER:
604 if (!url.nPort)
605 url.nPort = INTERNET_DEFAULT_GOPHER_PORT;
606 dwService = INTERNET_SERVICE_GOPHER;
607 break;
609 case INTERNET_SCHEME_HTTPS:
610 if (!url.nPort)
611 url.nPort = INTERNET_DEFAULT_HTTPS_PORT;
612 dwService = INTERNET_SERVICE_HTTP;
613 break;
616 bind->hconnect = InternetConnectW(bind->hinternet, host, url.nPort, user, pass,
617 dwService, 0, (DWORD)bind);
618 if (!bind->hconnect)
620 hres = HRESULT_FROM_WIN32(GetLastError());
621 CloseHandle(bind->hinternet);
622 break;
625 hres = IBindStatusCallback_OnProgress(bind->pbscb, 0, 0, 0x22, NULL);
626 hres = IBindStatusCallback_OnProgress(bind->pbscb, 0, 0, BINDSTATUS_FINDINGRESOURCE, NULL);
627 hres = IBindStatusCallback_OnProgress(bind->pbscb, 0, 0, BINDSTATUS_CONNECTING, NULL);
628 hres = IBindStatusCallback_OnProgress(bind->pbscb, 0, 0, BINDSTATUS_SENDINGREQUEST, NULL);
630 bSuccess = FALSE;
632 switch (dwService)
634 case INTERNET_SERVICE_GOPHER:
635 bind->hrequest = GopherOpenFileW(bind->hconnect,
636 path,
638 INTERNET_FLAG_RELOAD,
640 if (bind->hrequest)
641 bSuccess = TRUE;
642 else
643 hres = HRESULT_FROM_WIN32(GetLastError());
644 break;
646 case INTERNET_SERVICE_FTP:
647 bind->hrequest = FtpOpenFileW(bind->hconnect,
648 path,
649 GENERIC_READ,
650 FTP_TRANSFER_TYPE_BINARY |
651 INTERNET_FLAG_TRANSFER_BINARY |
652 INTERNET_FLAG_RELOAD,
654 if (bind->hrequest)
655 bSuccess = TRUE;
656 else
657 hres = HRESULT_FROM_WIN32(GetLastError());
658 break;
660 case INTERNET_SERVICE_HTTP:
661 bind->hrequest = HttpOpenRequestW(bind->hconnect, NULL, path, NULL, NULL, NULL, 0, (DWORD)bind);
662 if (!bind->hrequest)
664 hres = HRESULT_FROM_WIN32(GetLastError());
666 else if (!HttpSendRequestW(bind->hrequest, NULL, 0, NULL, 0))
668 hres = HRESULT_FROM_WIN32(GetLastError());
669 InternetCloseHandle(bind->hrequest);
671 else
673 HttpQueryInfoW(bind->hrequest,
674 HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER,
675 &bind->expected_size,
676 &lensz,
677 NULL);
678 bSuccess = TRUE;
680 break;
682 if(bSuccess)
684 TRACE("res = %d gle = %u url len = %d\n", hres, GetLastError(), bind->expected_size);
686 IBindStatusCallback_OnProgress(bind->pbscb, 0, 0, BINDSTATUS_CACHEFILENAMEAVAILABLE, szFileName);
688 while(1) {
689 char buf[4096];
690 DWORD bufread;
691 if(InternetReadFile(bind->hrequest, buf, sizeof(buf), &bufread)) {
692 TRACE("read %d bytes %s...\n", bufread, debugstr_an(buf, 10));
693 if(bufread == 0) break;
694 hres = Binding_MoreCacheData(bind, buf, bufread);
695 } else
696 break;
698 InternetCloseHandle(bind->hrequest);
699 hres = S_OK;
702 InternetCloseHandle(bind->hconnect);
703 InternetCloseHandle(bind->hinternet);
704 } while(0);
706 Binding_FinishedDownload(bind, hres);
707 Binding_CloseCacheDownload(bind);
709 heap_free(user);
710 heap_free(pass);
711 heap_free(path);
712 heap_free(host);
717 IBinding_Release((IBinding*)bind);
719 return hres;
722 static HRESULT WINAPI URLMonikerImpl_BindToStorage(IMoniker* iface,
723 IBindCtx* pbc,
724 IMoniker* pmkToLeft,
725 REFIID riid,
726 VOID** ppvObject)
728 URLMonikerImpl *This = (URLMonikerImpl*)iface;
729 WCHAR schema[64];
730 BOOL bret;
732 URL_COMPONENTSW url = {sizeof(URL_COMPONENTSW), schema,
733 sizeof(schema)/sizeof(WCHAR), 0, NULL, 0, 0, NULL, 0, NULL, 0, NULL, 0, NULL, 0};
735 if(pmkToLeft)
736 FIXME("Unsupported pmkToLeft\n");
738 bret = InternetCrackUrlW(This->URLName, 0, ICU_ESCAPE, &url);
739 if(!bret) {
740 ERR("InternetCrackUrl failed: %u\n", GetLastError());
741 return E_FAIL;
744 if(url.nScheme== INTERNET_SCHEME_HTTPS
745 || url.nScheme== INTERNET_SCHEME_FTP
746 || url.nScheme == INTERNET_SCHEME_GOPHER)
747 return URLMonikerImpl_BindToStorage_hack(This->URLName, pbc, riid, ppvObject);
749 TRACE("(%p)->(%p %p %s %p)\n", This, pbc, pmkToLeft, debugstr_guid(riid), ppvObject);
751 return bind_to_storage(This->URLName, pbc, riid, ppvObject);
754 /******************************************************************************
755 * URLMoniker_Reduce
756 ******************************************************************************/
757 static HRESULT WINAPI URLMonikerImpl_Reduce(IMoniker* iface,
758 IBindCtx* pbc,
759 DWORD dwReduceHowFar,
760 IMoniker** ppmkToLeft,
761 IMoniker** ppmkReduced)
763 URLMonikerImpl *This = (URLMonikerImpl *)iface;
765 TRACE("(%p,%p,%d,%p,%p)\n",This,pbc,dwReduceHowFar,ppmkToLeft,ppmkReduced);
767 if(!ppmkReduced)
768 return E_INVALIDARG;
770 URLMonikerImpl_AddRef(iface);
771 *ppmkReduced = iface;
772 return MK_S_REDUCED_TO_SELF;
775 /******************************************************************************
776 * URLMoniker_ComposeWith
777 ******************************************************************************/
778 static HRESULT WINAPI URLMonikerImpl_ComposeWith(IMoniker* iface,
779 IMoniker* pmkRight,
780 BOOL fOnlyIfNotGeneric,
781 IMoniker** ppmkComposite)
783 URLMonikerImpl *This = (URLMonikerImpl *)iface;
784 FIXME("(%p)->(%p,%d,%p): stub\n",This,pmkRight,fOnlyIfNotGeneric,ppmkComposite);
786 return E_NOTIMPL;
789 /******************************************************************************
790 * URLMoniker_Enum
791 ******************************************************************************/
792 static HRESULT WINAPI URLMonikerImpl_Enum(IMoniker* iface,BOOL fForward, IEnumMoniker** ppenumMoniker)
794 URLMonikerImpl *This = (URLMonikerImpl *)iface;
795 TRACE("(%p,%d,%p)\n",This,fForward,ppenumMoniker);
797 if(!ppenumMoniker)
798 return E_INVALIDARG;
800 /* Does not support sub-monikers */
801 *ppenumMoniker = NULL;
802 return S_OK;
805 /******************************************************************************
806 * URLMoniker_IsEqual
807 ******************************************************************************/
808 static HRESULT WINAPI URLMonikerImpl_IsEqual(IMoniker* iface,IMoniker* pmkOtherMoniker)
810 URLMonikerImpl *This = (URLMonikerImpl *)iface;
811 CLSID clsid;
812 LPOLESTR urlPath;
813 IBindCtx* bind;
814 HRESULT res;
816 TRACE("(%p,%p)\n",This,pmkOtherMoniker);
818 if(pmkOtherMoniker==NULL)
819 return E_INVALIDARG;
821 IMoniker_GetClassID(pmkOtherMoniker,&clsid);
823 if(!IsEqualCLSID(&clsid,&CLSID_StdURLMoniker))
824 return S_FALSE;
826 res = CreateBindCtx(0,&bind);
827 if(FAILED(res))
828 return res;
830 res = S_FALSE;
831 if(SUCCEEDED(IMoniker_GetDisplayName(pmkOtherMoniker,bind,NULL,&urlPath))) {
832 int result = lstrcmpiW(urlPath, This->URLName);
833 CoTaskMemFree(urlPath);
834 if(result == 0)
835 res = S_OK;
837 IUnknown_Release(bind);
838 return res;
842 /******************************************************************************
843 * URLMoniker_Hash
844 ******************************************************************************/
845 static HRESULT WINAPI URLMonikerImpl_Hash(IMoniker* iface,DWORD* pdwHash)
847 URLMonikerImpl *This = (URLMonikerImpl *)iface;
849 int h = 0,i,skip,len;
850 int off = 0;
851 LPOLESTR val;
853 TRACE("(%p,%p)\n",This,pdwHash);
855 if(!pdwHash)
856 return E_INVALIDARG;
858 val = This->URLName;
859 len = lstrlenW(val);
861 if(len < 16) {
862 for(i = len ; i > 0; i--) {
863 h = (h * 37) + val[off++];
866 else {
867 /* only sample some characters */
868 skip = len / 8;
869 for(i = len; i > 0; i -= skip, off += skip) {
870 h = (h * 39) + val[off];
873 *pdwHash = h;
874 return S_OK;
877 /******************************************************************************
878 * URLMoniker_IsRunning
879 ******************************************************************************/
880 static HRESULT WINAPI URLMonikerImpl_IsRunning(IMoniker* iface,
881 IBindCtx* pbc,
882 IMoniker* pmkToLeft,
883 IMoniker* pmkNewlyRunning)
885 URLMonikerImpl *This = (URLMonikerImpl *)iface;
886 FIXME("(%p)->(%p,%p,%p): stub\n",This,pbc,pmkToLeft,pmkNewlyRunning);
888 return E_NOTIMPL;
891 /******************************************************************************
892 * URLMoniker_GetTimeOfLastChange
893 ******************************************************************************/
894 static HRESULT WINAPI URLMonikerImpl_GetTimeOfLastChange(IMoniker* iface,
895 IBindCtx* pbc,
896 IMoniker* pmkToLeft,
897 FILETIME* pFileTime)
899 URLMonikerImpl *This = (URLMonikerImpl *)iface;
900 FIXME("(%p)->(%p,%p,%p): stub\n",This,pbc,pmkToLeft,pFileTime);
902 return E_NOTIMPL;
905 /******************************************************************************
906 * URLMoniker_Inverse
907 ******************************************************************************/
908 static HRESULT WINAPI URLMonikerImpl_Inverse(IMoniker* iface,IMoniker** ppmk)
910 URLMonikerImpl *This = (URLMonikerImpl *)iface;
911 TRACE("(%p,%p)\n",This,ppmk);
913 return MK_E_NOINVERSE;
916 /******************************************************************************
917 * URLMoniker_CommonPrefixWith
918 ******************************************************************************/
919 static HRESULT WINAPI URLMonikerImpl_CommonPrefixWith(IMoniker* iface,IMoniker* pmkOther,IMoniker** ppmkPrefix)
921 URLMonikerImpl *This = (URLMonikerImpl *)iface;
922 FIXME("(%p)->(%p,%p): stub\n",This,pmkOther,ppmkPrefix);
924 return E_NOTIMPL;
927 /******************************************************************************
928 * URLMoniker_RelativePathTo
929 ******************************************************************************/
930 static HRESULT WINAPI URLMonikerImpl_RelativePathTo(IMoniker* iface,IMoniker* pmOther, IMoniker** ppmkRelPath)
932 URLMonikerImpl *This = (URLMonikerImpl *)iface;
933 FIXME("(%p)->(%p,%p): stub\n",This,pmOther,ppmkRelPath);
935 return E_NOTIMPL;
938 /******************************************************************************
939 * URLMoniker_GetDisplayName
940 ******************************************************************************/
941 static HRESULT WINAPI URLMonikerImpl_GetDisplayName(IMoniker* iface,
942 IBindCtx* pbc,
943 IMoniker* pmkToLeft,
944 LPOLESTR *ppszDisplayName)
946 URLMonikerImpl *This = (URLMonikerImpl *)iface;
948 int len;
950 TRACE("(%p,%p,%p,%p)\n",This,pbc,pmkToLeft,ppszDisplayName);
952 if(!ppszDisplayName)
953 return E_INVALIDARG;
955 /* FIXME: If this is a partial URL, try and get a URL moniker from SZ_URLCONTEXT in the bind context,
956 then look at pmkToLeft to try and complete the URL
958 len = lstrlenW(This->URLName)+1;
959 *ppszDisplayName = CoTaskMemAlloc(len*sizeof(WCHAR));
960 if(!*ppszDisplayName)
961 return E_OUTOFMEMORY;
962 lstrcpyW(*ppszDisplayName, This->URLName);
963 return S_OK;
966 /******************************************************************************
967 * URLMoniker_ParseDisplayName
968 ******************************************************************************/
969 static HRESULT WINAPI URLMonikerImpl_ParseDisplayName(IMoniker* iface,
970 IBindCtx* pbc,
971 IMoniker* pmkToLeft,
972 LPOLESTR pszDisplayName,
973 ULONG* pchEaten,
974 IMoniker** ppmkOut)
976 URLMonikerImpl *This = (URLMonikerImpl *)iface;
977 FIXME("(%p)->(%p,%p,%p,%p,%p): stub\n",This,pbc,pmkToLeft,pszDisplayName,pchEaten,ppmkOut);
979 return E_NOTIMPL;
982 /******************************************************************************
983 * URLMoniker_IsSystemMoniker
984 ******************************************************************************/
985 static HRESULT WINAPI URLMonikerImpl_IsSystemMoniker(IMoniker* iface,DWORD* pwdMksys)
987 URLMonikerImpl *This = (URLMonikerImpl *)iface;
988 TRACE("(%p,%p)\n",This,pwdMksys);
990 if(!pwdMksys)
991 return E_INVALIDARG;
993 *pwdMksys = MKSYS_URLMONIKER;
994 return S_OK;
997 /********************************************************************************/
998 /* Virtual function table for the URLMonikerImpl class which include IPersist,*/
999 /* IPersistStream and IMoniker functions. */
1000 static const IMonikerVtbl VT_URLMonikerImpl =
1002 URLMonikerImpl_QueryInterface,
1003 URLMonikerImpl_AddRef,
1004 URLMonikerImpl_Release,
1005 URLMonikerImpl_GetClassID,
1006 URLMonikerImpl_IsDirty,
1007 URLMonikerImpl_Load,
1008 URLMonikerImpl_Save,
1009 URLMonikerImpl_GetSizeMax,
1010 URLMonikerImpl_BindToObject,
1011 URLMonikerImpl_BindToStorage,
1012 URLMonikerImpl_Reduce,
1013 URLMonikerImpl_ComposeWith,
1014 URLMonikerImpl_Enum,
1015 URLMonikerImpl_IsEqual,
1016 URLMonikerImpl_Hash,
1017 URLMonikerImpl_IsRunning,
1018 URLMonikerImpl_GetTimeOfLastChange,
1019 URLMonikerImpl_Inverse,
1020 URLMonikerImpl_CommonPrefixWith,
1021 URLMonikerImpl_RelativePathTo,
1022 URLMonikerImpl_GetDisplayName,
1023 URLMonikerImpl_ParseDisplayName,
1024 URLMonikerImpl_IsSystemMoniker
1027 /******************************************************************************
1028 * URLMoniker_Construct (local function)
1029 *******************************************************************************/
1030 static HRESULT URLMonikerImpl_Construct(URLMonikerImpl* This, LPCOLESTR lpszLeftURLName, LPCOLESTR lpszURLName)
1032 HRESULT hres;
1033 DWORD sizeStr = 0;
1035 TRACE("(%p,%s,%s)\n",This,debugstr_w(lpszLeftURLName),debugstr_w(lpszURLName));
1037 This->lpvtbl = &VT_URLMonikerImpl;
1038 This->ref = 0;
1040 This->URLName = heap_alloc(INTERNET_MAX_URL_LENGTH*sizeof(WCHAR));
1042 if(lpszLeftURLName)
1043 hres = CoInternetCombineUrl(lpszLeftURLName, lpszURLName, URL_FILE_USE_PATHURL,
1044 This->URLName, INTERNET_MAX_URL_LENGTH, &sizeStr, 0);
1045 else
1046 hres = CoInternetParseUrl(lpszURLName, PARSE_CANONICALIZE, URL_FILE_USE_PATHURL,
1047 This->URLName, INTERNET_MAX_URL_LENGTH, &sizeStr, 0);
1049 if(FAILED(hres)) {
1050 heap_free(This->URLName);
1051 return hres;
1054 URLMON_LockModule();
1056 if(sizeStr != INTERNET_MAX_URL_LENGTH)
1057 This->URLName = heap_realloc(This->URLName, (sizeStr+1)*sizeof(WCHAR));
1059 TRACE("URLName = %s\n", debugstr_w(This->URLName));
1061 return S_OK;
1064 /***********************************************************************
1065 * CreateURLMonikerEx (URLMON.@)
1067 * Create a url moniker.
1069 * PARAMS
1070 * pmkContext [I] Context
1071 * szURL [I] Url to create the moniker for
1072 * ppmk [O] Destination for created moniker.
1073 * dwFlags [I] Flags.
1075 * RETURNS
1076 * Success: S_OK. ppmk contains the created IMoniker object.
1077 * Failure: MK_E_SYNTAX if szURL is not a valid url, or
1078 * E_OUTOFMEMORY if memory allocation fails.
1080 HRESULT WINAPI CreateURLMonikerEx(IMoniker *pmkContext, LPCWSTR szURL, IMoniker **ppmk, DWORD dwFlags)
1082 URLMonikerImpl *obj;
1083 HRESULT hres;
1084 LPOLESTR lefturl = NULL;
1086 TRACE("(%p, %s, %p, %08x)\n", pmkContext, debugstr_w(szURL), ppmk, dwFlags);
1088 if (dwFlags & URL_MK_UNIFORM) FIXME("ignoring flag URL_MK_UNIFORM\n");
1090 if(!(obj = heap_alloc(sizeof(*obj))))
1091 return E_OUTOFMEMORY;
1093 if(pmkContext) {
1094 IBindCtx* bind;
1095 DWORD dwMksys = 0;
1096 IMoniker_IsSystemMoniker(pmkContext, &dwMksys);
1097 if(dwMksys == MKSYS_URLMONIKER && SUCCEEDED(CreateBindCtx(0, &bind))) {
1098 IMoniker_GetDisplayName(pmkContext, bind, NULL, &lefturl);
1099 TRACE("lefturl = %s\n", debugstr_w(lefturl));
1100 IBindCtx_Release(bind);
1104 hres = URLMonikerImpl_Construct(obj, lefturl, szURL);
1105 CoTaskMemFree(lefturl);
1106 if(SUCCEEDED(hres))
1107 hres = URLMonikerImpl_QueryInterface((IMoniker*)obj, &IID_IMoniker, (void**)ppmk);
1108 else
1109 heap_free(obj);
1110 return hres;
1113 /**********************************************************************
1114 * CreateURLMoniker (URLMON.@)
1116 * Create a url moniker.
1118 * PARAMS
1119 * pmkContext [I] Context
1120 * szURL [I] Url to create the moniker for
1121 * ppmk [O] Destination for created moniker.
1123 * RETURNS
1124 * Success: S_OK. ppmk contains the created IMoniker object.
1125 * Failure: MK_E_SYNTAX if szURL is not a valid url, or
1126 * E_OUTOFMEMORY if memory allocation fails.
1128 HRESULT WINAPI CreateURLMoniker(IMoniker *pmkContext, LPCWSTR szURL, IMoniker **ppmk)
1130 return CreateURLMonikerEx(pmkContext, szURL, ppmk, URL_MK_LEGACY);
1133 /***********************************************************************
1134 * IsAsyncMoniker (URLMON.@)
1136 HRESULT WINAPI IsAsyncMoniker(IMoniker *pmk)
1138 IUnknown *am;
1140 TRACE("(%p)\n", pmk);
1141 if(!pmk)
1142 return E_INVALIDARG;
1143 if(SUCCEEDED(IMoniker_QueryInterface(pmk, &IID_IAsyncMoniker, (void**)&am))) {
1144 IUnknown_Release(am);
1145 return S_OK;
1147 return S_FALSE;
1150 /***********************************************************************
1151 * BindAsyncMoniker (URLMON.@)
1153 * Bind a bind status callback to an asynchronous URL Moniker.
1155 * PARAMS
1156 * pmk [I] Moniker object to bind status callback to
1157 * grfOpt [I] Options, seems not used
1158 * pbsc [I] Status callback to bind
1159 * iidResult [I] Interface to return
1160 * ppvResult [O] Resulting asynchronous moniker object
1162 * RETURNS
1163 * Success: S_OK.
1164 * Failure: E_INVALIDARG, if any argument is invalid, or
1165 * E_OUTOFMEMORY if memory allocation fails.
1167 HRESULT WINAPI BindAsyncMoniker(IMoniker *pmk, DWORD grfOpt, IBindStatusCallback *pbsc, REFIID iidResult, LPVOID *ppvResult)
1169 LPBC pbc = NULL;
1170 HRESULT hr = E_INVALIDARG;
1172 TRACE("(%p %08x %p %s %p)\n", pmk, grfOpt, pbsc, debugstr_guid(iidResult), ppvResult);
1174 if (pmk && ppvResult)
1176 *ppvResult = NULL;
1178 hr = CreateAsyncBindCtx(0, pbsc, NULL, &pbc);
1179 if (hr == NOERROR)
1181 hr = IMoniker_BindToObject(pmk, pbc, NULL, iidResult, ppvResult);
1182 IBindCtx_Release(pbc);
1185 return hr;
1188 /***********************************************************************
1189 * URLDownloadToFileA (URLMON.@)
1191 * Downloads URL szURL to rile szFileName and call lpfnCB callback to
1192 * report progress.
1194 * PARAMS
1195 * pCaller [I] controlling IUnknown interface.
1196 * szURL [I] URL of the file to download
1197 * szFileName [I] file name to store the content of the URL
1198 * dwReserved [I] reserved - set to 0
1199 * lpfnCB [I] callback for progress report
1201 * RETURNS
1202 * S_OK on success
1203 * E_OUTOFMEMORY when going out of memory
1205 HRESULT WINAPI URLDownloadToFileA(LPUNKNOWN pCaller,
1206 LPCSTR szURL,
1207 LPCSTR szFileName,
1208 DWORD dwReserved,
1209 LPBINDSTATUSCALLBACK lpfnCB)
1211 UNICODE_STRING szURL_w, szFileName_w;
1213 if ((szURL == NULL) || (szFileName == NULL)) {
1214 FIXME("(%p,%s,%s,%08x,%p) cannot accept NULL strings !\n", pCaller, debugstr_a(szURL), debugstr_a(szFileName), dwReserved, lpfnCB);
1215 return E_INVALIDARG; /* The error code is not specified in this case... */
1218 if (RtlCreateUnicodeStringFromAsciiz(&szURL_w, szURL)) {
1219 if (RtlCreateUnicodeStringFromAsciiz(&szFileName_w, szFileName)) {
1220 HRESULT ret = URLDownloadToFileW(pCaller, szURL_w.Buffer, szFileName_w.Buffer, dwReserved, lpfnCB);
1222 RtlFreeUnicodeString(&szURL_w);
1223 RtlFreeUnicodeString(&szFileName_w);
1225 return ret;
1226 } else {
1227 RtlFreeUnicodeString(&szURL_w);
1231 FIXME("(%p,%s,%s,%08x,%p) could not allocate W strings !\n", pCaller, szURL, szFileName, dwReserved, lpfnCB);
1232 return E_OUTOFMEMORY;
1235 /***********************************************************************
1236 * URLDownloadToFileW (URLMON.@)
1238 * Downloads URL szURL to rile szFileName and call lpfnCB callback to
1239 * report progress.
1241 * PARAMS
1242 * pCaller [I] controlling IUnknown interface.
1243 * szURL [I] URL of the file to download
1244 * szFileName [I] file name to store the content of the URL
1245 * dwReserved [I] reserved - set to 0
1246 * lpfnCB [I] callback for progress report
1248 * RETURNS
1249 * S_OK on success
1250 * E_OUTOFMEMORY when going out of memory
1252 HRESULT WINAPI URLDownloadToFileW(LPUNKNOWN pCaller,
1253 LPCWSTR szURL,
1254 LPCWSTR szFileName,
1255 DWORD dwReserved,
1256 LPBINDSTATUSCALLBACK lpfnCB)
1258 HINTERNET hinternet, hcon, hreq;
1259 BOOL r;
1260 CHAR buffer[0x1000];
1261 DWORD sz, total, written;
1262 DWORD total_size = 0xFFFFFFFF, arg_size = sizeof(total_size);
1263 URL_COMPONENTSW url;
1264 WCHAR host[0x80], path[0x100];
1265 HANDLE hfile;
1266 static const WCHAR wszAppName[]={'u','r','l','m','o','n','.','d','l','l',0};
1268 /* Note: all error codes would need to be checked agains real Windows behaviour... */
1269 TRACE("(%p,%s,%s,%08x,%p) stub!\n", pCaller, debugstr_w(szURL), debugstr_w(szFileName), dwReserved, lpfnCB);
1271 if ((szURL == NULL) || (szFileName == NULL)) {
1272 FIXME(" cannot accept NULL strings !\n");
1273 return E_INVALIDARG;
1276 /* Would be better to use the application name here rather than 'urlmon' :-/ */
1277 hinternet = InternetOpenW(wszAppName, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
1278 if (hinternet == NULL) {
1279 return E_OUTOFMEMORY;
1282 memset(&url, 0, sizeof(url));
1283 url.dwStructSize = sizeof(url);
1284 url.lpszHostName = host;
1285 url.dwHostNameLength = sizeof(host);
1286 url.lpszUrlPath = path;
1287 url.dwUrlPathLength = sizeof(path);
1289 if (!InternetCrackUrlW(szURL, 0, 0, &url)) {
1290 InternetCloseHandle(hinternet);
1291 return E_OUTOFMEMORY;
1294 if (lpfnCB) {
1295 if (IBindStatusCallback_OnProgress(lpfnCB, 0, 0, BINDSTATUS_CONNECTING, url.lpszHostName) == E_ABORT) {
1296 InternetCloseHandle(hinternet);
1297 return S_OK;
1301 hcon = InternetConnectW(hinternet, url.lpszHostName, url.nPort,
1302 url.lpszUserName, url.lpszPassword,
1303 INTERNET_SERVICE_HTTP, 0, 0);
1304 if (!hcon) {
1305 InternetCloseHandle(hinternet);
1306 return E_OUTOFMEMORY;
1309 hreq = HttpOpenRequestW(hcon, NULL, url.lpszUrlPath, NULL, NULL, NULL, 0, 0);
1310 if (!hreq) {
1311 InternetCloseHandle(hinternet);
1312 InternetCloseHandle(hcon);
1313 return E_OUTOFMEMORY;
1316 if (!HttpSendRequestW(hreq, NULL, 0, NULL, 0)) {
1317 InternetCloseHandle(hinternet);
1318 InternetCloseHandle(hcon);
1319 InternetCloseHandle(hreq);
1320 return E_OUTOFMEMORY;
1323 if (HttpQueryInfoW(hreq, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER,
1324 &total_size, &arg_size, NULL)) {
1325 TRACE(" total size : %d\n", total_size);
1328 hfile = CreateFileW(szFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
1329 FILE_ATTRIBUTE_NORMAL, NULL );
1330 if (hfile == INVALID_HANDLE_VALUE) {
1331 return E_ACCESSDENIED;
1334 if (lpfnCB) {
1335 if (IBindStatusCallback_OnProgress(lpfnCB, 0, total_size != 0xFFFFFFFF ? total_size : 0,
1336 BINDSTATUS_BEGINDOWNLOADDATA, szURL) == E_ABORT) {
1337 InternetCloseHandle(hreq);
1338 InternetCloseHandle(hcon);
1339 InternetCloseHandle(hinternet);
1340 CloseHandle(hfile);
1341 return S_OK;
1345 total = 0;
1346 while (1) {
1347 r = InternetReadFile(hreq, buffer, sizeof(buffer), &sz);
1348 if (!r) {
1349 InternetCloseHandle(hreq);
1350 InternetCloseHandle(hcon);
1351 InternetCloseHandle(hinternet);
1353 CloseHandle(hfile);
1354 return E_OUTOFMEMORY;
1356 if (!sz)
1357 break;
1359 total += sz;
1361 if (lpfnCB) {
1362 if (IBindStatusCallback_OnProgress(lpfnCB, total, total_size != 0xFFFFFFFF ? total_size : 0,
1363 BINDSTATUS_DOWNLOADINGDATA, szURL) == E_ABORT) {
1364 InternetCloseHandle(hreq);
1365 InternetCloseHandle(hcon);
1366 InternetCloseHandle(hinternet);
1367 CloseHandle(hfile);
1368 return S_OK;
1372 if (!WriteFile(hfile, buffer, sz, &written, NULL)) {
1373 InternetCloseHandle(hreq);
1374 InternetCloseHandle(hcon);
1375 InternetCloseHandle(hinternet);
1377 CloseHandle(hfile);
1378 return E_OUTOFMEMORY;
1382 if (lpfnCB) {
1383 if (IBindStatusCallback_OnProgress(lpfnCB, total, total_size != 0xFFFFFFFF ? total_size : 0,
1384 BINDSTATUS_ENDDOWNLOADDATA, szURL) == E_ABORT) {
1385 InternetCloseHandle(hreq);
1386 InternetCloseHandle(hcon);
1387 InternetCloseHandle(hinternet);
1388 CloseHandle(hfile);
1389 return S_OK;
1393 InternetCloseHandle(hreq);
1394 InternetCloseHandle(hcon);
1395 InternetCloseHandle(hinternet);
1397 CloseHandle(hfile);
1399 return S_OK;
1402 /***********************************************************************
1403 * URLDownloadToCacheFileA (URLMON.@)
1405 HRESULT WINAPI URLDownloadToCacheFileA(LPUNKNOWN lpUnkCaller, LPCSTR szURL, LPSTR szFileName,
1406 DWORD dwBufLength, DWORD dwReserved, LPBINDSTATUSCALLBACK pBSC)
1408 LPWSTR url = NULL, file_name = NULL;
1409 int len;
1410 HRESULT hres;
1412 TRACE("(%p %s %p %d %d %p)\n", lpUnkCaller, debugstr_a(szURL), szFileName,
1413 dwBufLength, dwReserved, pBSC);
1415 if(szURL) {
1416 len = MultiByteToWideChar(CP_ACP, 0, szURL, -1, NULL, 0);
1417 url = heap_alloc(len*sizeof(WCHAR));
1418 MultiByteToWideChar(CP_ACP, 0, szURL, -1, url, -1);
1421 if(szFileName)
1422 file_name = heap_alloc(dwBufLength*sizeof(WCHAR));
1424 hres = URLDownloadToCacheFileW(lpUnkCaller, url, file_name, dwBufLength*sizeof(WCHAR),
1425 dwReserved, pBSC);
1427 if(SUCCEEDED(hres) && file_name)
1428 WideCharToMultiByte(CP_ACP, 0, file_name, -1, szFileName, dwBufLength, NULL, NULL);
1430 heap_free(url);
1431 heap_free(file_name);
1433 return hres;
1436 /***********************************************************************
1437 * URLDownloadToCacheFileW (URLMON.@)
1439 HRESULT WINAPI URLDownloadToCacheFileW(LPUNKNOWN lpUnkCaller, LPCWSTR szURL, LPWSTR szFileName,
1440 DWORD dwBufLength, DWORD dwReserved, LPBINDSTATUSCALLBACK pBSC)
1442 WCHAR cache_path[MAX_PATH + 1];
1443 FILETIME expire, modified;
1444 HRESULT hr;
1445 LPWSTR ext;
1447 static WCHAR header[] = {
1448 'H','T','T','P','/','1','.','0',' ','2','0','0',' ',
1449 'O','K','\\','r','\\','n','\\','r','\\','n',0
1452 TRACE("(%p, %s, %p, %d, %d, %p)\n", lpUnkCaller, debugstr_w(szURL),
1453 szFileName, dwBufLength, dwReserved, pBSC);
1455 if (!szURL || !szFileName)
1456 return E_INVALIDARG;
1458 ext = PathFindExtensionW(szURL);
1460 if (!CreateUrlCacheEntryW(szURL, 0, ext, cache_path, 0))
1461 return E_FAIL;
1463 hr = URLDownloadToFileW(lpUnkCaller, szURL, cache_path, 0, pBSC);
1464 if (FAILED(hr))
1465 return hr;
1467 expire.dwHighDateTime = 0;
1468 expire.dwLowDateTime = 0;
1469 modified.dwHighDateTime = 0;
1470 modified.dwLowDateTime = 0;
1472 if (!CommitUrlCacheEntryW(szURL, cache_path, expire, modified, NORMAL_CACHE_ENTRY,
1473 header, sizeof(header), NULL, NULL))
1474 return E_FAIL;
1476 if (lstrlenW(cache_path) > dwBufLength)
1477 return E_OUTOFMEMORY;
1479 lstrcpyW(szFileName, cache_path);
1481 return S_OK;
1484 /***********************************************************************
1485 * HlinkSimpleNavigateToString (URLMON.@)
1487 HRESULT WINAPI HlinkSimpleNavigateToString( LPCWSTR szTarget,
1488 LPCWSTR szLocation, LPCWSTR szTargetFrameName, IUnknown *pUnk,
1489 IBindCtx *pbc, IBindStatusCallback *pbsc, DWORD grfHLNF, DWORD dwReserved)
1491 FIXME("%s\n", debugstr_w( szTarget ) );
1492 return E_NOTIMPL;
1495 /***********************************************************************
1496 * HlinkNavigateString (URLMON.@)
1498 HRESULT WINAPI HlinkNavigateString( IUnknown *pUnk, LPCWSTR szTarget )
1500 TRACE("%p %s\n", pUnk, debugstr_w( szTarget ) );
1501 return HlinkSimpleNavigateToString(
1502 szTarget, NULL, NULL, pUnk, NULL, NULL, 0, 0 );
1505 /***********************************************************************
1506 * GetSoftwareUpdateInfo (URLMON.@)
1508 HRESULT WINAPI GetSoftwareUpdateInfo( LPCWSTR szDistUnit, LPSOFTDISTINFO psdi )
1510 FIXME("%s %p\n", debugstr_w(szDistUnit), psdi );
1511 return E_FAIL;