mshtml: Added edit mode IDM_CUT implementation.
[wine/gsoc_dplay.git] / dlls / urlmon / umon.c
blobb8c6bfeffff4a8ca1231f5994d5b54e6d70d9254
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 <stdarg.h>
24 #include <stdio.h>
26 #define COBJMACROS
27 #define NONAMELESSUNION
28 #define NONAMELESSSTRUCT
30 #include "windef.h"
31 #include "winbase.h"
32 #include "winreg.h"
33 #include "winternl.h"
34 #include "winuser.h"
35 #include "objbase.h"
36 #include "wine/debug.h"
37 #include "wine/unicode.h"
38 #include "ole2.h"
39 #include "urlmon.h"
40 #include "wininet.h"
41 #include "shlwapi.h"
42 #include "urlmon_main.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
46 /* native urlmon.dll uses this key, too */
47 static WCHAR BSCBHolder[] = { '_','B','S','C','B','_','H','o','l','d','e','r','_',0 };
49 /*static BOOL registered_wndclass = FALSE;*/
51 typedef struct {
52 const IBindingVtbl *lpVtbl;
54 LONG ref;
56 LPWSTR URLName;
58 HWND hwndCallback;
59 IBindCtx *pBC;
60 HINTERNET hinternet, hconnect, hrequest;
61 HANDLE hCacheFile;
62 IUMCacheStream *pstrCache;
63 IBindStatusCallback *pbscb;
64 DWORD total_read, expected_size;
65 } Binding;
67 static HRESULT WINAPI Binding_QueryInterface(IBinding* iface, REFIID riid, void **ppvObject)
69 Binding *This = (Binding*)iface;
71 TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(riid), ppvObject);
73 if((This == NULL) || (ppvObject == NULL))
74 return E_INVALIDARG;
76 if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IBinding, riid)) {
77 *ppvObject = iface;
78 IBinding_AddRef(iface);
79 return S_OK;
82 *ppvObject = NULL;
83 return E_NOINTERFACE;
86 static ULONG WINAPI Binding_AddRef(IBinding* iface)
88 Binding *This = (Binding*)iface;
89 ULONG ref = InterlockedIncrement(&This->ref);
91 TRACE("(%p) ref=%d\n", This, ref);
93 return ref;
96 static ULONG WINAPI Binding_Release(IBinding* iface)
98 Binding *This = (Binding*)iface;
99 ULONG ref = InterlockedDecrement(&This->ref);
101 TRACE("(%p) ref=%d\n",This, ref);
103 if(!ref) {
104 HeapFree(GetProcessHeap(), 0, This->URLName);
105 if (This->hCacheFile)
106 CloseHandle(This->hCacheFile);
107 if (This->pstrCache)
109 UMCloseCacheFileStream(This->pstrCache);
110 IStream_Release((IStream *)This->pstrCache);
112 if (This->pbscb)
113 IBindStatusCallback_Release(This->pbscb);
115 HeapFree(GetProcessHeap(), 0, This);
117 URLMON_UnlockModule();
120 return ref;
123 static HRESULT WINAPI Binding_Abort(IBinding* iface)
125 Binding *This = (Binding*)iface;
127 FIXME("(%p): stub\n", This);
129 return E_NOTIMPL;
132 static HRESULT WINAPI Binding_GetBindResult(IBinding* iface, CLSID* pclsidProtocol, DWORD* pdwResult, LPOLESTR* pszResult, DWORD* pdwReserved)
134 Binding *This = (Binding*)iface;
136 FIXME("(%p)->(%p, %p, %p, %p): stub\n", This, pclsidProtocol, pdwResult, pszResult, pdwReserved);
138 return E_NOTIMPL;
141 static HRESULT WINAPI Binding_GetPriority(IBinding* iface, LONG* pnPriority)
143 Binding *This = (Binding*)iface;
145 FIXME("(%p)->(%p): stub\n", This, pnPriority);
147 return E_NOTIMPL;
150 static HRESULT WINAPI Binding_Resume(IBinding* iface)
152 Binding *This = (Binding*)iface;
154 FIXME("(%p): stub\n", This);
156 return E_NOTIMPL;
159 static HRESULT WINAPI Binding_SetPriority(IBinding* iface, LONG nPriority)
161 Binding *This = (Binding*)iface;
163 FIXME("(%p)->(%d): stub\n", This, nPriority);
165 return E_NOTIMPL;
168 static HRESULT WINAPI Binding_Suspend(IBinding* iface)
170 Binding *This = (Binding*)iface;
172 FIXME("(%p): stub\n", This);
174 return E_NOTIMPL;
177 static void Binding_CloseCacheDownload(Binding *This)
179 CloseHandle(This->hCacheFile);
180 This->hCacheFile = 0;
181 UMCloseCacheFileStream(This->pstrCache);
182 IStream_Release((IStream *)This->pstrCache);
183 This->pstrCache = 0;
186 static HRESULT Binding_MoreCacheData(Binding *This, char *buf, DWORD dwBytes)
188 DWORD written;
190 if (WriteFile(This->hCacheFile, buf, dwBytes, &written, NULL) && written == dwBytes)
192 HRESULT hr;
194 This->total_read += written;
195 hr = IBindStatusCallback_OnProgress(This->pbscb,
196 This->total_read + written,
197 This->expected_size,
198 (This->total_read == written) ?
199 BINDSTATUS_BEGINDOWNLOADDATA :
200 BINDSTATUS_DOWNLOADINGDATA,
201 This->URLName);
202 if (!hr)
204 STGMEDIUM stg;
205 FORMATETC fmt;
207 fmt.cfFormat = 0;
208 fmt.ptd = NULL;
209 fmt.dwAspect = 0;
210 fmt.lindex = -1;
211 fmt.tymed = TYMED_ISTREAM;
213 stg.tymed = TYMED_ISTREAM;
214 stg.u.pstm = (IStream *)This->pstrCache;
215 stg.pUnkForRelease = NULL;
217 hr = IBindStatusCallback_OnDataAvailable(This->pbscb,
218 (This->total_read == written) ?
219 BSCF_FIRSTDATANOTIFICATION :
220 BSCF_INTERMEDIATEDATANOTIFICATION,
221 This->total_read + written,
222 &fmt,
223 &stg);
225 if (written < dwBytes)
226 return STG_E_MEDIUMFULL;
227 else
228 return hr;
230 return HRESULT_FROM_WIN32(GetLastError());
233 static void Binding_FinishedDownload(Binding *This, HRESULT hr)
235 STGMEDIUM stg;
236 FORMATETC fmt;
238 fmt.ptd = NULL;
239 fmt.dwAspect = 0;
240 fmt.lindex = -1;
241 fmt.tymed = TYMED_ISTREAM;
243 stg.tymed = TYMED_ISTREAM;
244 stg.u.pstm = (IStream *)This->pstrCache;
245 stg.pUnkForRelease = NULL;
247 IBindStatusCallback_OnProgress(This->pbscb, This->total_read, This->expected_size,
248 BINDSTATUS_ENDDOWNLOADDATA, This->URLName);
249 IBindStatusCallback_OnDataAvailable(This->pbscb, BSCF_LASTDATANOTIFICATION, This->total_read, &fmt, &stg);
250 if (hr)
252 WCHAR *pwchError = 0;
254 FormatMessageW (FORMAT_MESSAGE_FROM_SYSTEM |
255 FORMAT_MESSAGE_ALLOCATE_BUFFER,
256 NULL, (DWORD) hr,
257 0, (LPWSTR) &pwchError,
258 0, NULL);
259 if (!pwchError)
261 static const WCHAR achFormat[] = { '%', '0', '8', 'x', 0 };
263 pwchError =(WCHAR *) LocalAlloc(LMEM_FIXED, sizeof(WCHAR) * 9);
264 wsprintfW(pwchError, achFormat, hr);
266 IBindStatusCallback_OnStopBinding(This->pbscb, hr, pwchError);
267 LocalFree(pwchError);
269 else
271 IBindStatusCallback_OnStopBinding(This->pbscb, hr, NULL);
273 IBindStatusCallback_Release(This->pbscb);
274 This->pbscb = 0;
277 static const IBindingVtbl BindingVtbl =
279 Binding_QueryInterface,
280 Binding_AddRef,
281 Binding_Release,
282 Binding_Abort,
283 Binding_Suspend,
284 Binding_Resume,
285 Binding_SetPriority,
286 Binding_GetPriority,
287 Binding_GetBindResult
290 /* filemoniker data structure */
291 typedef struct {
293 const IMonikerVtbl* lpvtbl; /* VTable relative to the IMoniker interface.*/
295 LONG ref; /* reference counter for this object */
297 LPOLESTR URLName; /* URL string identified by this URLmoniker */
298 } URLMonikerImpl;
300 /*******************************************************************************
301 * URLMoniker_QueryInterface
302 *******************************************************************************/
303 static HRESULT WINAPI URLMonikerImpl_QueryInterface(IMoniker* iface,REFIID riid,void** ppvObject)
305 URLMonikerImpl *This = (URLMonikerImpl *)iface;
307 TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppvObject);
309 /* Perform a sanity check on the parameters.*/
310 if ( (This==0) || (ppvObject==0) )
311 return E_INVALIDARG;
313 /* Initialize the return parameter */
314 *ppvObject = 0;
316 /* Compare the riid with the interface IDs implemented by this object.*/
317 if (IsEqualIID(&IID_IUnknown, riid) ||
318 IsEqualIID(&IID_IPersist, riid) ||
319 IsEqualIID(&IID_IPersistStream,riid) ||
320 IsEqualIID(&IID_IMoniker, riid)
322 *ppvObject = iface;
324 /* Check that we obtained an interface.*/
325 if ((*ppvObject)==0)
326 return E_NOINTERFACE;
328 /* Query Interface always increases the reference count by one when it is successful */
329 IMoniker_AddRef(iface);
331 return S_OK;
334 /******************************************************************************
335 * URLMoniker_AddRef
336 ******************************************************************************/
337 static ULONG WINAPI URLMonikerImpl_AddRef(IMoniker* iface)
339 URLMonikerImpl *This = (URLMonikerImpl *)iface;
340 ULONG refCount = InterlockedIncrement(&This->ref);
342 TRACE("(%p) ref=%u\n",This, refCount);
344 return refCount;
347 /******************************************************************************
348 * URLMoniker_Release
349 ******************************************************************************/
350 static ULONG WINAPI URLMonikerImpl_Release(IMoniker* iface)
352 URLMonikerImpl *This = (URLMonikerImpl *)iface;
353 ULONG refCount = InterlockedDecrement(&This->ref);
355 TRACE("(%p) ref=%u\n",This, refCount);
357 /* destroy the object if there's no more reference on it */
358 if (!refCount) {
359 HeapFree(GetProcessHeap(),0,This->URLName);
360 HeapFree(GetProcessHeap(),0,This);
362 URLMON_UnlockModule();
365 return refCount;
369 /******************************************************************************
370 * URLMoniker_GetClassID
371 ******************************************************************************/
372 static HRESULT WINAPI URLMonikerImpl_GetClassID(IMoniker* iface,
373 CLSID *pClassID)/* Pointer to CLSID of object */
375 URLMonikerImpl *This = (URLMonikerImpl *)iface;
377 TRACE("(%p,%p)\n",This,pClassID);
379 if (pClassID==NULL)
380 return E_POINTER;
381 /* Windows always returns CLSID_StdURLMoniker */
382 *pClassID = CLSID_StdURLMoniker;
383 return S_OK;
386 /******************************************************************************
387 * URLMoniker_IsDirty
388 ******************************************************************************/
389 static HRESULT WINAPI URLMonikerImpl_IsDirty(IMoniker* iface)
391 URLMonikerImpl *This = (URLMonikerImpl *)iface;
392 /* Note that the OLE-provided implementations of the IPersistStream::IsDirty
393 method in the OLE-provided moniker interfaces always return S_FALSE because
394 their internal state never changes. */
396 TRACE("(%p)\n",This);
398 return S_FALSE;
401 /******************************************************************************
402 * URLMoniker_Load
404 * NOTE
405 * Writes a ULONG containing length of unicode string, followed
406 * by that many unicode characters
407 ******************************************************************************/
408 static HRESULT WINAPI URLMonikerImpl_Load(IMoniker* iface,IStream* pStm)
410 URLMonikerImpl *This = (URLMonikerImpl *)iface;
412 HRESULT res;
413 ULONG size;
414 ULONG got;
415 TRACE("(%p,%p)\n",This,pStm);
417 if(!pStm)
418 return E_INVALIDARG;
420 res = IStream_Read(pStm, &size, sizeof(ULONG), &got);
421 if(SUCCEEDED(res)) {
422 if(got == sizeof(ULONG)) {
423 HeapFree(GetProcessHeap(), 0, This->URLName);
424 This->URLName=HeapAlloc(GetProcessHeap(),0,size);
425 if(!This->URLName)
426 res = E_OUTOFMEMORY;
427 else {
428 res = IStream_Read(pStm, This->URLName, size, NULL);
429 This->URLName[size/sizeof(WCHAR) - 1] = 0;
432 else
433 res = E_FAIL;
435 return res;
438 /******************************************************************************
439 * URLMoniker_Save
440 ******************************************************************************/
441 static HRESULT WINAPI URLMonikerImpl_Save(IMoniker* iface,
442 IStream* pStm,/* pointer to the stream where the object is to be saved */
443 BOOL fClearDirty)/* Specifies whether to clear the dirty flag */
445 URLMonikerImpl *This = (URLMonikerImpl *)iface;
447 HRESULT res;
448 ULONG size;
449 TRACE("(%p,%p,%d)\n",This,pStm,fClearDirty);
451 if(!pStm)
452 return E_INVALIDARG;
454 size = (strlenW(This->URLName) + 1)*sizeof(WCHAR);
455 res=IStream_Write(pStm,&size,sizeof(ULONG),NULL);
456 if(SUCCEEDED(res))
457 res=IStream_Write(pStm,This->URLName,size,NULL);
458 return res;
462 /******************************************************************************
463 * URLMoniker_GetSizeMax
464 ******************************************************************************/
465 static HRESULT WINAPI URLMonikerImpl_GetSizeMax(IMoniker* iface,
466 ULARGE_INTEGER* pcbSize)/* Pointer to size of stream needed to save object */
468 URLMonikerImpl *This = (URLMonikerImpl *)iface;
470 TRACE("(%p,%p)\n",This,pcbSize);
472 if(!pcbSize)
473 return E_INVALIDARG;
475 pcbSize->QuadPart = sizeof(ULONG) + ((strlenW(This->URLName)+1) * sizeof(WCHAR));
476 return S_OK;
479 /******************************************************************************
480 * URLMoniker_BindToObject
481 ******************************************************************************/
482 static HRESULT WINAPI URLMonikerImpl_BindToObject(IMoniker* iface,
483 IBindCtx* pbc,
484 IMoniker* pmkToLeft,
485 REFIID riid,
486 VOID** ppvResult)
488 URLMonikerImpl *This = (URLMonikerImpl *)iface;
490 *ppvResult=0;
492 FIXME("(%p)->(%p,%p,%s,%p): stub\n",This,pbc,pmkToLeft,debugstr_guid(riid),
493 ppvResult);
495 return E_NOTIMPL;
498 /******************************************************************************
499 * URLMoniker_BindToStorage
500 ******************************************************************************/
501 static HRESULT URLMonikerImpl_BindToStorage_hack(LPCWSTR URLName,
502 IBindCtx* pbc,
503 IMoniker* pmkToLeft,
504 REFIID riid,
505 VOID** ppvObject)
507 HRESULT hres;
508 BINDINFO bi;
509 DWORD bindf;
510 WCHAR szFileName[MAX_PATH + 1];
511 Binding *bind;
512 int len;
514 WARN("(%s %p %p %s %p)\n", debugstr_w(URLName), pbc, pmkToLeft, debugstr_guid(riid),
515 ppvObject);
517 if(pmkToLeft) {
518 FIXME("pmkToLeft != NULL\n");
519 return E_NOTIMPL;
521 if(!IsEqualIID(&IID_IStream, riid)) {
522 FIXME("unsupported iid\n");
523 return E_NOTIMPL;
526 bind = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(Binding));
527 bind->lpVtbl = &BindingVtbl;
528 bind->ref = 1;
529 URLMON_LockModule();
531 len = lstrlenW(URLName)+1;
532 bind->URLName = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
533 memcpy(bind->URLName, URLName, len*sizeof(WCHAR));
535 hres = UMCreateStreamOnCacheFile(bind->URLName, 0, szFileName, &bind->hCacheFile, &bind->pstrCache);
537 if(SUCCEEDED(hres)) {
538 TRACE("Created stream...\n");
540 *ppvObject = (void *) bind->pstrCache;
541 IStream_AddRef((IStream *) bind->pstrCache);
543 hres = IBindCtx_GetObjectParam(pbc, BSCBHolder, (IUnknown**)&bind->pbscb);
544 if(SUCCEEDED(hres)) {
545 TRACE("Got IBindStatusCallback...\n");
547 memset(&bi, 0, sizeof(bi));
548 bi.cbSize = sizeof(bi);
549 bindf = 0;
550 hres = IBindStatusCallback_GetBindInfo(bind->pbscb, &bindf, &bi);
551 if(SUCCEEDED(hres)) {
552 URL_COMPONENTSW url;
553 WCHAR *host, *path, *user, *pass;
554 DWORD lensz = sizeof(bind->expected_size);
555 DWORD dwService = 0;
556 BOOL bSuccess;
558 TRACE("got bindinfo. bindf = %08x extrainfo = %s bindinfof = %08x bindverb = %08x iid %s\n",
559 bindf, debugstr_w(bi.szExtraInfo), bi.grfBindInfoF, bi.dwBindVerb, debugstr_guid(&bi.iid));
560 hres = IBindStatusCallback_OnStartBinding(bind->pbscb, 0, (IBinding*)bind);
561 TRACE("OnStartBinding rets %08x\n", hres);
563 bind->expected_size = 0;
564 bind->total_read = 0;
566 memset(&url, 0, sizeof(url));
567 url.dwStructSize = sizeof(url);
568 url.dwSchemeLength = url.dwHostNameLength = url.dwUrlPathLength = url.dwUserNameLength = url.dwPasswordLength = 1;
569 InternetCrackUrlW(URLName, 0, ICU_ESCAPE, &url);
570 host = HeapAlloc(GetProcessHeap(), 0, (url.dwHostNameLength + 1) * sizeof(WCHAR));
571 memcpy(host, url.lpszHostName, url.dwHostNameLength * sizeof(WCHAR));
572 host[url.dwHostNameLength] = '\0';
573 path = HeapAlloc(GetProcessHeap(), 0, (url.dwUrlPathLength + 1) * sizeof(WCHAR));
574 memcpy(path, url.lpszUrlPath, url.dwUrlPathLength * sizeof(WCHAR));
575 path[url.dwUrlPathLength] = '\0';
576 if (url.dwUserNameLength)
578 user = HeapAlloc(GetProcessHeap(), 0, ((url.dwUserNameLength + 1) * sizeof(WCHAR)));
579 memcpy(user, url.lpszUserName, url.dwUserNameLength * sizeof(WCHAR));
580 user[url.dwUserNameLength] = 0;
582 else
584 user = 0;
586 if (url.dwPasswordLength)
588 pass = HeapAlloc(GetProcessHeap(), 0, ((url.dwPasswordLength + 1) * sizeof(WCHAR)));
589 memcpy(pass, url.lpszPassword, url.dwPasswordLength * sizeof(WCHAR));
590 pass[url.dwPasswordLength] = 0;
592 else
594 pass = 0;
598 do {
599 bind->hinternet = InternetOpenA("User Agent", 0, NULL, NULL, 0);
600 if (!bind->hinternet)
602 hres = HRESULT_FROM_WIN32(GetLastError());
603 break;
606 switch ((DWORD) url.nScheme)
608 case INTERNET_SCHEME_FTP:
609 if (!url.nPort)
610 url.nPort = INTERNET_DEFAULT_FTP_PORT;
611 dwService = INTERNET_SERVICE_FTP;
612 break;
614 case INTERNET_SCHEME_GOPHER:
615 if (!url.nPort)
616 url.nPort = INTERNET_DEFAULT_GOPHER_PORT;
617 dwService = INTERNET_SERVICE_GOPHER;
618 break;
620 case INTERNET_SCHEME_HTTP:
621 if (!url.nPort)
622 url.nPort = INTERNET_DEFAULT_HTTP_PORT;
623 dwService = INTERNET_SERVICE_HTTP;
624 break;
626 case INTERNET_SCHEME_HTTPS:
627 if (!url.nPort)
628 url.nPort = INTERNET_DEFAULT_HTTPS_PORT;
629 dwService = INTERNET_SERVICE_HTTP;
630 break;
633 bind->hconnect = InternetConnectW(bind->hinternet, host, url.nPort, user, pass,
634 dwService, 0, (DWORD)bind);
635 if (!bind->hconnect)
637 hres = HRESULT_FROM_WIN32(GetLastError());
638 CloseHandle(bind->hinternet);
639 break;
642 hres = IBindStatusCallback_OnProgress(bind->pbscb, 0, 0, 0x22, NULL);
643 hres = IBindStatusCallback_OnProgress(bind->pbscb, 0, 0, BINDSTATUS_FINDINGRESOURCE, NULL);
644 hres = IBindStatusCallback_OnProgress(bind->pbscb, 0, 0, BINDSTATUS_CONNECTING, NULL);
645 hres = IBindStatusCallback_OnProgress(bind->pbscb, 0, 0, BINDSTATUS_SENDINGREQUEST, NULL);
647 bSuccess = FALSE;
649 switch (dwService)
651 case INTERNET_SERVICE_GOPHER:
652 bind->hrequest = GopherOpenFileW(bind->hconnect,
653 path,
655 INTERNET_FLAG_RELOAD,
657 if (bind->hrequest)
658 bSuccess = TRUE;
659 else
660 hres = HRESULT_FROM_WIN32(GetLastError());
661 break;
663 case INTERNET_SERVICE_FTP:
664 bind->hrequest = FtpOpenFileW(bind->hconnect,
665 path,
666 GENERIC_READ,
667 FTP_TRANSFER_TYPE_BINARY |
668 INTERNET_FLAG_TRANSFER_BINARY |
669 INTERNET_FLAG_RELOAD,
671 if (bind->hrequest)
672 bSuccess = TRUE;
673 else
674 hres = HRESULT_FROM_WIN32(GetLastError());
675 break;
677 case INTERNET_SERVICE_HTTP:
678 bind->hrequest = HttpOpenRequestW(bind->hconnect, NULL, path, NULL, NULL, NULL, 0, (DWORD)bind);
679 if (!bind->hrequest)
681 hres = HRESULT_FROM_WIN32(GetLastError());
683 else if (!HttpSendRequestW(bind->hrequest, NULL, 0, NULL, 0))
685 hres = HRESULT_FROM_WIN32(GetLastError());
686 InternetCloseHandle(bind->hrequest);
688 else
690 HttpQueryInfoW(bind->hrequest,
691 HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER,
692 &bind->expected_size,
693 &lensz,
694 NULL);
695 bSuccess = TRUE;
697 break;
699 if(bSuccess)
701 TRACE("res = %d gle = %u url len = %d\n", hres, GetLastError(), bind->expected_size);
703 IBindStatusCallback_OnProgress(bind->pbscb, 0, 0, BINDSTATUS_CACHEFILENAMEAVAILABLE, szFileName);
705 while(1) {
706 char buf[4096];
707 DWORD bufread;
708 if(InternetReadFile(bind->hrequest, buf, sizeof(buf), &bufread)) {
709 TRACE("read %d bytes %s...\n", bufread, debugstr_an(buf, 10));
710 if(bufread == 0) break;
711 hres = Binding_MoreCacheData(bind, buf, bufread);
712 } else
713 break;
715 InternetCloseHandle(bind->hrequest);
716 hres = S_OK;
719 InternetCloseHandle(bind->hconnect);
720 InternetCloseHandle(bind->hinternet);
721 } while(0);
723 Binding_FinishedDownload(bind, hres);
724 Binding_CloseCacheDownload(bind);
726 HeapFree(GetProcessHeap(), 0, user);
727 HeapFree(GetProcessHeap(), 0, pass);
728 HeapFree(GetProcessHeap(), 0, path);
729 HeapFree(GetProcessHeap(), 0, host);
734 IBinding_Release((IBinding*)bind);
736 return hres;
739 static HRESULT WINAPI URLMonikerImpl_BindToStorage(IMoniker* iface,
740 IBindCtx* pbc,
741 IMoniker* pmkToLeft,
742 REFIID riid,
743 VOID** ppvObject)
745 URLMonikerImpl *This = (URLMonikerImpl*)iface;
746 WCHAR schema[64];
747 BOOL bret;
749 URL_COMPONENTSW url = {sizeof(URL_COMPONENTSW), schema,
750 sizeof(schema)/sizeof(WCHAR), 0, NULL, 0, 0, NULL, 0, NULL, 0, NULL, 0, NULL, 0};
752 bret = InternetCrackUrlW(This->URLName, 0, ICU_ESCAPE, &url);
753 if(!bret) {
754 ERR("InternetCrackUrl failed: %u\n", GetLastError());
755 return E_FAIL;
758 if(url.nScheme == INTERNET_SCHEME_HTTP
759 || url.nScheme== INTERNET_SCHEME_HTTPS
760 || url.nScheme== INTERNET_SCHEME_FTP
761 || url.nScheme == INTERNET_SCHEME_GOPHER)
762 return URLMonikerImpl_BindToStorage_hack(This->URLName, pbc, pmkToLeft, riid, ppvObject);
764 TRACE("(%p)->(%p %p %s %p)\n", This, pbc, pmkToLeft, debugstr_guid(riid), ppvObject);
766 return start_binding(This->URLName, pbc, riid, ppvObject);
769 /******************************************************************************
770 * URLMoniker_Reduce
771 ******************************************************************************/
772 static HRESULT WINAPI URLMonikerImpl_Reduce(IMoniker* iface,
773 IBindCtx* pbc,
774 DWORD dwReduceHowFar,
775 IMoniker** ppmkToLeft,
776 IMoniker** ppmkReduced)
778 URLMonikerImpl *This = (URLMonikerImpl *)iface;
780 TRACE("(%p,%p,%d,%p,%p)\n",This,pbc,dwReduceHowFar,ppmkToLeft,ppmkReduced);
782 if(!ppmkReduced)
783 return E_INVALIDARG;
785 URLMonikerImpl_AddRef(iface);
786 *ppmkReduced = iface;
787 return MK_S_REDUCED_TO_SELF;
790 /******************************************************************************
791 * URLMoniker_ComposeWith
792 ******************************************************************************/
793 static HRESULT WINAPI URLMonikerImpl_ComposeWith(IMoniker* iface,
794 IMoniker* pmkRight,
795 BOOL fOnlyIfNotGeneric,
796 IMoniker** ppmkComposite)
798 URLMonikerImpl *This = (URLMonikerImpl *)iface;
799 FIXME("(%p)->(%p,%d,%p): stub\n",This,pmkRight,fOnlyIfNotGeneric,ppmkComposite);
801 return E_NOTIMPL;
804 /******************************************************************************
805 * URLMoniker_Enum
806 ******************************************************************************/
807 static HRESULT WINAPI URLMonikerImpl_Enum(IMoniker* iface,BOOL fForward, IEnumMoniker** ppenumMoniker)
809 URLMonikerImpl *This = (URLMonikerImpl *)iface;
810 TRACE("(%p,%d,%p)\n",This,fForward,ppenumMoniker);
812 if(!ppenumMoniker)
813 return E_INVALIDARG;
815 /* Does not support sub-monikers */
816 *ppenumMoniker = NULL;
817 return S_OK;
820 /******************************************************************************
821 * URLMoniker_IsEqual
822 ******************************************************************************/
823 static HRESULT WINAPI URLMonikerImpl_IsEqual(IMoniker* iface,IMoniker* pmkOtherMoniker)
825 URLMonikerImpl *This = (URLMonikerImpl *)iface;
826 CLSID clsid;
827 LPOLESTR urlPath;
828 IBindCtx* bind;
829 HRESULT res;
831 TRACE("(%p,%p)\n",This,pmkOtherMoniker);
833 if(pmkOtherMoniker==NULL)
834 return E_INVALIDARG;
836 IMoniker_GetClassID(pmkOtherMoniker,&clsid);
838 if(!IsEqualCLSID(&clsid,&CLSID_StdURLMoniker))
839 return S_FALSE;
841 res = CreateBindCtx(0,&bind);
842 if(FAILED(res))
843 return res;
845 res = S_FALSE;
846 if(SUCCEEDED(IMoniker_GetDisplayName(pmkOtherMoniker,bind,NULL,&urlPath))) {
847 int result = lstrcmpiW(urlPath, This->URLName);
848 CoTaskMemFree(urlPath);
849 if(result == 0)
850 res = S_OK;
852 IUnknown_Release(bind);
853 return res;
857 /******************************************************************************
858 * URLMoniker_Hash
859 ******************************************************************************/
860 static HRESULT WINAPI URLMonikerImpl_Hash(IMoniker* iface,DWORD* pdwHash)
862 URLMonikerImpl *This = (URLMonikerImpl *)iface;
864 int h = 0,i,skip,len;
865 int off = 0;
866 LPOLESTR val;
868 TRACE("(%p,%p)\n",This,pdwHash);
870 if(!pdwHash)
871 return E_INVALIDARG;
873 val = This->URLName;
874 len = lstrlenW(val);
876 if(len < 16) {
877 for(i = len ; i > 0; i--) {
878 h = (h * 37) + val[off++];
881 else {
882 /* only sample some characters */
883 skip = len / 8;
884 for(i = len; i > 0; i -= skip, off += skip) {
885 h = (h * 39) + val[off];
888 *pdwHash = h;
889 return S_OK;
892 /******************************************************************************
893 * URLMoniker_IsRunning
894 ******************************************************************************/
895 static HRESULT WINAPI URLMonikerImpl_IsRunning(IMoniker* iface,
896 IBindCtx* pbc,
897 IMoniker* pmkToLeft,
898 IMoniker* pmkNewlyRunning)
900 URLMonikerImpl *This = (URLMonikerImpl *)iface;
901 FIXME("(%p)->(%p,%p,%p): stub\n",This,pbc,pmkToLeft,pmkNewlyRunning);
903 return E_NOTIMPL;
906 /******************************************************************************
907 * URLMoniker_GetTimeOfLastChange
908 ******************************************************************************/
909 static HRESULT WINAPI URLMonikerImpl_GetTimeOfLastChange(IMoniker* iface,
910 IBindCtx* pbc,
911 IMoniker* pmkToLeft,
912 FILETIME* pFileTime)
914 URLMonikerImpl *This = (URLMonikerImpl *)iface;
915 FIXME("(%p)->(%p,%p,%p): stub\n",This,pbc,pmkToLeft,pFileTime);
917 return E_NOTIMPL;
920 /******************************************************************************
921 * URLMoniker_Inverse
922 ******************************************************************************/
923 static HRESULT WINAPI URLMonikerImpl_Inverse(IMoniker* iface,IMoniker** ppmk)
925 URLMonikerImpl *This = (URLMonikerImpl *)iface;
926 TRACE("(%p,%p)\n",This,ppmk);
928 return MK_E_NOINVERSE;
931 /******************************************************************************
932 * URLMoniker_CommonPrefixWith
933 ******************************************************************************/
934 static HRESULT WINAPI URLMonikerImpl_CommonPrefixWith(IMoniker* iface,IMoniker* pmkOther,IMoniker** ppmkPrefix)
936 URLMonikerImpl *This = (URLMonikerImpl *)iface;
937 FIXME("(%p)->(%p,%p): stub\n",This,pmkOther,ppmkPrefix);
939 return E_NOTIMPL;
942 /******************************************************************************
943 * URLMoniker_RelativePathTo
944 ******************************************************************************/
945 static HRESULT WINAPI URLMonikerImpl_RelativePathTo(IMoniker* iface,IMoniker* pmOther, IMoniker** ppmkRelPath)
947 URLMonikerImpl *This = (URLMonikerImpl *)iface;
948 FIXME("(%p)->(%p,%p): stub\n",This,pmOther,ppmkRelPath);
950 return E_NOTIMPL;
953 /******************************************************************************
954 * URLMoniker_GetDisplayName
955 ******************************************************************************/
956 static HRESULT WINAPI URLMonikerImpl_GetDisplayName(IMoniker* iface,
957 IBindCtx* pbc,
958 IMoniker* pmkToLeft,
959 LPOLESTR *ppszDisplayName)
961 URLMonikerImpl *This = (URLMonikerImpl *)iface;
963 int len;
965 TRACE("(%p,%p,%p,%p)\n",This,pbc,pmkToLeft,ppszDisplayName);
967 if(!ppszDisplayName)
968 return E_INVALIDARG;
970 /* FIXME: If this is a partial URL, try and get a URL moniker from SZ_URLCONTEXT in the bind context,
971 then look at pmkToLeft to try and complete the URL
973 len = lstrlenW(This->URLName)+1;
974 *ppszDisplayName = CoTaskMemAlloc(len*sizeof(WCHAR));
975 if(!*ppszDisplayName)
976 return E_OUTOFMEMORY;
977 lstrcpyW(*ppszDisplayName, This->URLName);
978 return S_OK;
981 /******************************************************************************
982 * URLMoniker_ParseDisplayName
983 ******************************************************************************/
984 static HRESULT WINAPI URLMonikerImpl_ParseDisplayName(IMoniker* iface,
985 IBindCtx* pbc,
986 IMoniker* pmkToLeft,
987 LPOLESTR pszDisplayName,
988 ULONG* pchEaten,
989 IMoniker** ppmkOut)
991 URLMonikerImpl *This = (URLMonikerImpl *)iface;
992 FIXME("(%p)->(%p,%p,%p,%p,%p): stub\n",This,pbc,pmkToLeft,pszDisplayName,pchEaten,ppmkOut);
994 return E_NOTIMPL;
997 /******************************************************************************
998 * URLMoniker_IsSystemMoniker
999 ******************************************************************************/
1000 static HRESULT WINAPI URLMonikerImpl_IsSystemMoniker(IMoniker* iface,DWORD* pwdMksys)
1002 URLMonikerImpl *This = (URLMonikerImpl *)iface;
1003 TRACE("(%p,%p)\n",This,pwdMksys);
1005 if(!pwdMksys)
1006 return E_INVALIDARG;
1008 *pwdMksys = MKSYS_URLMONIKER;
1009 return S_OK;
1012 /********************************************************************************/
1013 /* Virtual function table for the URLMonikerImpl class which include IPersist,*/
1014 /* IPersistStream and IMoniker functions. */
1015 static const IMonikerVtbl VT_URLMonikerImpl =
1017 URLMonikerImpl_QueryInterface,
1018 URLMonikerImpl_AddRef,
1019 URLMonikerImpl_Release,
1020 URLMonikerImpl_GetClassID,
1021 URLMonikerImpl_IsDirty,
1022 URLMonikerImpl_Load,
1023 URLMonikerImpl_Save,
1024 URLMonikerImpl_GetSizeMax,
1025 URLMonikerImpl_BindToObject,
1026 URLMonikerImpl_BindToStorage,
1027 URLMonikerImpl_Reduce,
1028 URLMonikerImpl_ComposeWith,
1029 URLMonikerImpl_Enum,
1030 URLMonikerImpl_IsEqual,
1031 URLMonikerImpl_Hash,
1032 URLMonikerImpl_IsRunning,
1033 URLMonikerImpl_GetTimeOfLastChange,
1034 URLMonikerImpl_Inverse,
1035 URLMonikerImpl_CommonPrefixWith,
1036 URLMonikerImpl_RelativePathTo,
1037 URLMonikerImpl_GetDisplayName,
1038 URLMonikerImpl_ParseDisplayName,
1039 URLMonikerImpl_IsSystemMoniker
1042 /******************************************************************************
1043 * URLMoniker_Construct (local function)
1044 *******************************************************************************/
1045 static HRESULT URLMonikerImpl_Construct(URLMonikerImpl* This, LPCOLESTR lpszLeftURLName, LPCOLESTR lpszURLName)
1047 HRESULT hres;
1048 DWORD sizeStr = 0;
1050 TRACE("(%p,%s,%s)\n",This,debugstr_w(lpszLeftURLName),debugstr_w(lpszURLName));
1052 This->lpvtbl = &VT_URLMonikerImpl;
1053 This->ref = 0;
1055 This->URLName = HeapAlloc(GetProcessHeap(), 0, INTERNET_MAX_URL_LENGTH*sizeof(WCHAR));
1057 if(lpszLeftURLName)
1058 hres = CoInternetCombineUrl(lpszLeftURLName, lpszURLName, URL_FILE_USE_PATHURL,
1059 This->URLName, INTERNET_MAX_URL_LENGTH, &sizeStr, 0);
1060 else
1061 hres = CoInternetParseUrl(lpszURLName, PARSE_CANONICALIZE, URL_FILE_USE_PATHURL,
1062 This->URLName, INTERNET_MAX_URL_LENGTH, &sizeStr, 0);
1064 if(FAILED(hres)) {
1065 HeapFree(GetProcessHeap(), 0, This->URLName);
1066 return hres;
1069 URLMON_LockModule();
1071 if(sizeStr != INTERNET_MAX_URL_LENGTH)
1072 This->URLName = HeapReAlloc(GetProcessHeap(), 0, This->URLName, (sizeStr+1)*sizeof(WCHAR));
1074 TRACE("URLName = %s\n", debugstr_w(This->URLName));
1076 return S_OK;
1079 /***********************************************************************
1080 * CreateAsyncBindCtx (URLMON.@)
1082 HRESULT WINAPI CreateAsyncBindCtx(DWORD reserved, IBindStatusCallback *callback,
1083 IEnumFORMATETC *format, IBindCtx **pbind)
1085 TRACE("(%08x %p %p %p)\n", reserved, callback, format, pbind);
1087 if(!callback)
1088 return E_INVALIDARG;
1090 return CreateAsyncBindCtxEx(NULL, 0, callback, format, pbind, 0);
1092 /***********************************************************************
1093 * CreateAsyncBindCtxEx (URLMON.@)
1095 * Create an asynchronous bind context.
1097 HRESULT WINAPI CreateAsyncBindCtxEx(IBindCtx *ibind, DWORD options,
1098 IBindStatusCallback *callback, IEnumFORMATETC *format, IBindCtx** pbind,
1099 DWORD reserved)
1101 HRESULT hres;
1102 BIND_OPTS bindopts;
1103 IBindCtx *bctx;
1105 TRACE("(%p %08x %p %p %p %d)\n", ibind, options, callback, format, pbind, reserved);
1107 if(!pbind)
1108 return E_INVALIDARG;
1110 if(options)
1111 FIXME("not supported options %08x\n", options);
1112 if(format)
1113 FIXME("format is not supported\n");
1115 if(reserved)
1116 WARN("reserved=%d\n", reserved);
1118 hres = CreateBindCtx(0, &bctx);
1119 if(FAILED(hres))
1120 return hres;
1122 bindopts.cbStruct = sizeof(BIND_OPTS);
1123 bindopts.grfFlags = BIND_MAYBOTHERUSER;
1124 bindopts.grfMode = STGM_READWRITE | STGM_SHARE_EXCLUSIVE;
1125 bindopts.dwTickCountDeadline = 0;
1126 IBindCtx_SetBindOptions(bctx, &bindopts);
1128 if(callback)
1129 RegisterBindStatusCallback(bctx, callback, NULL, 0);
1131 *pbind = bctx;
1133 return S_OK;
1137 /***********************************************************************
1138 * CreateURLMonikerEx (URLMON.@)
1140 * Create a url moniker.
1142 * PARAMS
1143 * pmkContext [I] Context
1144 * szURL [I] Url to create the moniker for
1145 * ppmk [O] Destination for created moniker.
1146 * dwFlags [I] Flags.
1148 * RETURNS
1149 * Success: S_OK. ppmk contains the created IMoniker object.
1150 * Failure: MK_E_SYNTAX if szURL is not a valid url, or
1151 * E_OUTOFMEMORY if memory allocation fails.
1153 HRESULT WINAPI CreateURLMonikerEx(IMoniker *pmkContext, LPCWSTR szURL, IMoniker **ppmk, DWORD dwFlags)
1155 URLMonikerImpl *obj;
1156 HRESULT hres;
1157 LPOLESTR lefturl = NULL;
1159 TRACE("(%p, %s, %p, %08x)\n", pmkContext, debugstr_w(szURL), ppmk, dwFlags);
1161 if (dwFlags & URL_MK_UNIFORM) FIXME("ignoring flag URL_MK_UNIFORM\n");
1163 if(!(obj = HeapAlloc(GetProcessHeap(), 0, sizeof(*obj))))
1164 return E_OUTOFMEMORY;
1166 if(pmkContext) {
1167 IBindCtx* bind;
1168 DWORD dwMksys = 0;
1169 IMoniker_IsSystemMoniker(pmkContext, &dwMksys);
1170 if(dwMksys == MKSYS_URLMONIKER && SUCCEEDED(CreateBindCtx(0, &bind))) {
1171 IMoniker_GetDisplayName(pmkContext, bind, NULL, &lefturl);
1172 TRACE("lefturl = %s\n", debugstr_w(lefturl));
1173 IBindCtx_Release(bind);
1177 hres = URLMonikerImpl_Construct(obj, lefturl, szURL);
1178 CoTaskMemFree(lefturl);
1179 if(SUCCEEDED(hres))
1180 hres = URLMonikerImpl_QueryInterface((IMoniker*)obj, &IID_IMoniker, (void**)ppmk);
1181 else
1182 HeapFree(GetProcessHeap(), 0, obj);
1183 return hres;
1186 /**********************************************************************
1187 * CreateURLMoniker (URLMON.@)
1189 * Create a url moniker.
1191 * PARAMS
1192 * pmkContext [I] Context
1193 * szURL [I] Url to create the moniker for
1194 * ppmk [O] Destination for created moniker.
1196 * RETURNS
1197 * Success: S_OK. ppmk contains the created IMoniker object.
1198 * Failure: MK_E_SYNTAX if szURL is not a valid url, or
1199 * E_OUTOFMEMORY if memory allocation fails.
1201 HRESULT WINAPI CreateURLMoniker(IMoniker *pmkContext, LPCWSTR szURL, IMoniker **ppmk)
1203 return CreateURLMonikerEx(pmkContext, szURL, ppmk, URL_MK_LEGACY);
1206 /***********************************************************************
1207 * CoInternetQueryInfo (URLMON.@)
1209 * Retrieves information relevant to a specified URL
1211 * RETURNS
1212 * S_OK success
1213 * S_FALSE buffer too small
1214 * INET_E_QUERYOPTIONUNKNOWN invalid option
1217 HRESULT WINAPI CoInternetQueryInfo(LPCWSTR pwzUrl, QUERYOPTION QueryOption,
1218 DWORD dwQueryFlags, LPVOID pvBuffer, DWORD cbBuffer, DWORD * pcbBuffer,
1219 DWORD dwReserved)
1221 FIXME("(%s, %x, %x, %p, %x, %p, %x): stub\n", debugstr_w(pwzUrl),
1222 QueryOption, dwQueryFlags, pvBuffer, cbBuffer, pcbBuffer, dwReserved);
1223 return S_OK;
1226 /***********************************************************************
1227 * IsAsyncMoniker (URLMON.@)
1229 HRESULT WINAPI IsAsyncMoniker(IMoniker *pmk)
1231 IUnknown *am;
1233 TRACE("(%p)\n", pmk);
1234 if(!pmk)
1235 return E_INVALIDARG;
1236 if(SUCCEEDED(IMoniker_QueryInterface(pmk, &IID_IAsyncMoniker, (void**)&am))) {
1237 IUnknown_Release(am);
1238 return S_OK;
1240 return S_FALSE;
1243 /***********************************************************************
1244 * BindAsyncMoniker (URLMON.@)
1246 * Bind a bind status callback to an asynchronous URL Moniker.
1248 * PARAMS
1249 * pmk [I] Moniker object to bind status callback to
1250 * grfOpt [I] Options, seems not used
1251 * pbsc [I] Status callback to bind
1252 * iidResult [I] Interface to return
1253 * ppvResult [O] Resulting asynchronous moniker object
1255 * RETURNS
1256 * Success: S_OK.
1257 * Failure: E_INVALIDARG, if any argument is invalid, or
1258 * E_OUTOFMEMORY if memory allocation fails.
1260 HRESULT WINAPI BindAsyncMoniker(IMoniker *pmk, DWORD grfOpt, IBindStatusCallback *pbsc, REFIID iidResult, LPVOID *ppvResult)
1262 LPBC pbc = NULL;
1263 HRESULT hr = E_INVALIDARG;
1265 if (pmk && ppvResult)
1267 *ppvResult = NULL;
1269 hr = CreateAsyncBindCtx(0, pbsc, NULL, &pbc);
1270 if (hr == NOERROR)
1272 hr = IMoniker_BindToObject(pmk, pbc, NULL, iidResult, ppvResult);
1273 IBindCtx_Release(pbc);
1276 return hr;
1279 /***********************************************************************
1280 * RegisterBindStatusCallback (URLMON.@)
1282 * Register a bind status callback.
1284 * PARAMS
1285 * pbc [I] Binding context
1286 * pbsc [I] Callback to register
1287 * ppbscPrevious [O] Destination for previous callback
1288 * dwReserved [I] Reserved, must be 0.
1290 * RETURNS
1291 * Success: S_OK.
1292 * Failure: E_INVALIDARG, if any argument is invalid, or
1293 * E_OUTOFMEMORY if memory allocation fails.
1295 HRESULT WINAPI RegisterBindStatusCallback(
1296 IBindCtx *pbc,
1297 IBindStatusCallback *pbsc,
1298 IBindStatusCallback **ppbscPrevious,
1299 DWORD dwReserved)
1301 IBindStatusCallback *prev;
1303 TRACE("(%p,%p,%p,%u)\n", pbc, pbsc, ppbscPrevious, dwReserved);
1305 if (pbc == NULL || pbsc == NULL)
1306 return E_INVALIDARG;
1308 if (SUCCEEDED(IBindCtx_GetObjectParam(pbc, BSCBHolder, (IUnknown **)&prev)))
1310 IBindCtx_RevokeObjectParam(pbc, BSCBHolder);
1311 if (ppbscPrevious)
1312 *ppbscPrevious = prev;
1313 else
1314 IBindStatusCallback_Release(prev);
1317 return IBindCtx_RegisterObjectParam(pbc, BSCBHolder, (IUnknown *)pbsc);
1320 /***********************************************************************
1321 * RevokeBindStatusCallback (URLMON.@)
1323 * Unregister a bind status callback.
1325 * pbc [I] Binding context
1326 * pbsc [I] Callback to unregister
1328 * RETURNS
1329 * Success: S_OK.
1330 * Failure: E_INVALIDARG, if any argument is invalid, or
1331 * E_FAIL if pbsc wasn't registered with pbc.
1333 HRESULT WINAPI RevokeBindStatusCallback(
1334 IBindCtx *pbc,
1335 IBindStatusCallback *pbsc)
1337 IBindStatusCallback *callback;
1338 HRESULT hr = E_FAIL;
1340 TRACE("(%p,%p)\n", pbc, pbsc);
1342 if (pbc == NULL || pbsc == NULL)
1343 return E_INVALIDARG;
1345 if (SUCCEEDED(IBindCtx_GetObjectParam(pbc, BSCBHolder, (IUnknown **)&callback)))
1347 if (callback == pbsc)
1349 IBindCtx_RevokeObjectParam(pbc, BSCBHolder);
1350 hr = S_OK;
1352 IBindStatusCallback_Release(pbsc);
1355 return hr;
1358 /***********************************************************************
1359 * URLDownloadToFileA (URLMON.@)
1361 * Downloads URL szURL to rile szFileName and call lpfnCB callback to
1362 * report progress.
1364 * PARAMS
1365 * pCaller [I] controlling IUnknown interface.
1366 * szURL [I] URL of the file to download
1367 * szFileName [I] file name to store the content of the URL
1368 * dwReserved [I] reserved - set to 0
1369 * lpfnCB [I] callback for progress report
1371 * RETURNS
1372 * S_OK on success
1373 * E_OUTOFMEMORY when going out of memory
1375 HRESULT WINAPI URLDownloadToFileA(LPUNKNOWN pCaller,
1376 LPCSTR szURL,
1377 LPCSTR szFileName,
1378 DWORD dwReserved,
1379 LPBINDSTATUSCALLBACK lpfnCB)
1381 UNICODE_STRING szURL_w, szFileName_w;
1383 if ((szURL == NULL) || (szFileName == NULL)) {
1384 FIXME("(%p,%s,%s,%08x,%p) cannot accept NULL strings !\n", pCaller, debugstr_a(szURL), debugstr_a(szFileName), dwReserved, lpfnCB);
1385 return E_INVALIDARG; /* The error code is not specified in this case... */
1388 if (RtlCreateUnicodeStringFromAsciiz(&szURL_w, szURL)) {
1389 if (RtlCreateUnicodeStringFromAsciiz(&szFileName_w, szFileName)) {
1390 HRESULT ret = URLDownloadToFileW(pCaller, szURL_w.Buffer, szFileName_w.Buffer, dwReserved, lpfnCB);
1392 RtlFreeUnicodeString(&szURL_w);
1393 RtlFreeUnicodeString(&szFileName_w);
1395 return ret;
1396 } else {
1397 RtlFreeUnicodeString(&szURL_w);
1401 FIXME("(%p,%s,%s,%08x,%p) could not allocate W strings !\n", pCaller, szURL, szFileName, dwReserved, lpfnCB);
1402 return E_OUTOFMEMORY;
1405 /***********************************************************************
1406 * URLDownloadToFileW (URLMON.@)
1408 * Downloads URL szURL to rile szFileName and call lpfnCB callback to
1409 * report progress.
1411 * PARAMS
1412 * pCaller [I] controlling IUnknown interface.
1413 * szURL [I] URL of the file to download
1414 * szFileName [I] file name to store the content of the URL
1415 * dwReserved [I] reserved - set to 0
1416 * lpfnCB [I] callback for progress report
1418 * RETURNS
1419 * S_OK on success
1420 * E_OUTOFMEMORY when going out of memory
1422 HRESULT WINAPI URLDownloadToFileW(LPUNKNOWN pCaller,
1423 LPCWSTR szURL,
1424 LPCWSTR szFileName,
1425 DWORD dwReserved,
1426 LPBINDSTATUSCALLBACK lpfnCB)
1428 HINTERNET hinternet, hcon, hreq;
1429 BOOL r;
1430 CHAR buffer[0x1000];
1431 DWORD sz, total, written;
1432 DWORD total_size = 0xFFFFFFFF, arg_size = sizeof(total_size);
1433 URL_COMPONENTSW url;
1434 WCHAR host[0x80], path[0x100];
1435 HANDLE hfile;
1436 static const WCHAR wszAppName[]={'u','r','l','m','o','n','.','d','l','l',0};
1438 /* Note: all error codes would need to be checked agains real Windows behaviour... */
1439 TRACE("(%p,%s,%s,%08x,%p) stub!\n", pCaller, debugstr_w(szURL), debugstr_w(szFileName), dwReserved, lpfnCB);
1441 if ((szURL == NULL) || (szFileName == NULL)) {
1442 FIXME(" cannot accept NULL strings !\n");
1443 return E_INVALIDARG;
1446 /* Would be better to use the application name here rather than 'urlmon' :-/ */
1447 hinternet = InternetOpenW(wszAppName, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
1448 if (hinternet == NULL) {
1449 return E_OUTOFMEMORY;
1452 memset(&url, 0, sizeof(url));
1453 url.dwStructSize = sizeof(url);
1454 url.lpszHostName = host;
1455 url.dwHostNameLength = sizeof(host);
1456 url.lpszUrlPath = path;
1457 url.dwUrlPathLength = sizeof(path);
1459 if (!InternetCrackUrlW(szURL, 0, 0, &url)) {
1460 InternetCloseHandle(hinternet);
1461 return E_OUTOFMEMORY;
1464 if (lpfnCB) {
1465 if (IBindStatusCallback_OnProgress(lpfnCB, 0, 0, BINDSTATUS_CONNECTING, url.lpszHostName) == E_ABORT) {
1466 InternetCloseHandle(hinternet);
1467 return S_OK;
1471 hcon = InternetConnectW(hinternet, url.lpszHostName, url.nPort,
1472 url.lpszUserName, url.lpszPassword,
1473 INTERNET_SERVICE_HTTP, 0, 0);
1474 if (!hcon) {
1475 InternetCloseHandle(hinternet);
1476 return E_OUTOFMEMORY;
1479 hreq = HttpOpenRequestW(hcon, NULL, url.lpszUrlPath, NULL, NULL, NULL, 0, 0);
1480 if (!hreq) {
1481 InternetCloseHandle(hinternet);
1482 InternetCloseHandle(hcon);
1483 return E_OUTOFMEMORY;
1486 if (!HttpSendRequestW(hreq, NULL, 0, NULL, 0)) {
1487 InternetCloseHandle(hinternet);
1488 InternetCloseHandle(hcon);
1489 InternetCloseHandle(hreq);
1490 return E_OUTOFMEMORY;
1493 if (HttpQueryInfoW(hreq, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER,
1494 &total_size, &arg_size, NULL)) {
1495 TRACE(" total size : %d\n", total_size);
1498 hfile = CreateFileW(szFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
1499 FILE_ATTRIBUTE_NORMAL, NULL );
1500 if (hfile == INVALID_HANDLE_VALUE) {
1501 return E_ACCESSDENIED;
1504 if (lpfnCB) {
1505 if (IBindStatusCallback_OnProgress(lpfnCB, 0, total_size != 0xFFFFFFFF ? total_size : 0,
1506 BINDSTATUS_BEGINDOWNLOADDATA, szURL) == E_ABORT) {
1507 InternetCloseHandle(hreq);
1508 InternetCloseHandle(hcon);
1509 InternetCloseHandle(hinternet);
1510 CloseHandle(hfile);
1511 return S_OK;
1515 total = 0;
1516 while (1) {
1517 r = InternetReadFile(hreq, buffer, sizeof(buffer), &sz);
1518 if (!r) {
1519 InternetCloseHandle(hreq);
1520 InternetCloseHandle(hcon);
1521 InternetCloseHandle(hinternet);
1523 CloseHandle(hfile);
1524 return E_OUTOFMEMORY;
1526 if (!sz)
1527 break;
1529 total += sz;
1531 if (lpfnCB) {
1532 if (IBindStatusCallback_OnProgress(lpfnCB, total, total_size != 0xFFFFFFFF ? total_size : 0,
1533 BINDSTATUS_DOWNLOADINGDATA, szURL) == E_ABORT) {
1534 InternetCloseHandle(hreq);
1535 InternetCloseHandle(hcon);
1536 InternetCloseHandle(hinternet);
1537 CloseHandle(hfile);
1538 return S_OK;
1542 if (!WriteFile(hfile, buffer, sz, &written, NULL)) {
1543 InternetCloseHandle(hreq);
1544 InternetCloseHandle(hcon);
1545 InternetCloseHandle(hinternet);
1547 CloseHandle(hfile);
1548 return E_OUTOFMEMORY;
1552 if (lpfnCB) {
1553 if (IBindStatusCallback_OnProgress(lpfnCB, total, total_size != 0xFFFFFFFF ? total_size : 0,
1554 BINDSTATUS_ENDDOWNLOADDATA, szURL) == E_ABORT) {
1555 InternetCloseHandle(hreq);
1556 InternetCloseHandle(hcon);
1557 InternetCloseHandle(hinternet);
1558 CloseHandle(hfile);
1559 return S_OK;
1563 InternetCloseHandle(hreq);
1564 InternetCloseHandle(hcon);
1565 InternetCloseHandle(hinternet);
1567 CloseHandle(hfile);
1569 return S_OK;
1572 /***********************************************************************
1573 * URLDownloadToCacheFileA (URLMON.@)
1575 HRESULT WINAPI URLDownloadToCacheFileA(LPUNKNOWN lpUnkCaller, LPCSTR szURL, LPSTR szFileName,
1576 DWORD dwBufLength, DWORD dwReserved, LPBINDSTATUSCALLBACK pBSC)
1578 LPWSTR url = NULL, file_name = NULL;
1579 int len;
1580 HRESULT hres;
1582 TRACE("(%p %s %p %d %d %p)\n", lpUnkCaller, debugstr_a(szURL), szFileName,
1583 dwBufLength, dwReserved, pBSC);
1585 if(szURL) {
1586 len = MultiByteToWideChar(CP_ACP, 0, szURL, -1, NULL, 0);
1587 url = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
1588 MultiByteToWideChar(CP_ACP, 0, szURL, -1, url, -1);
1591 if(szFileName)
1592 file_name = HeapAlloc(GetProcessHeap(), 0, dwBufLength*sizeof(WCHAR));
1594 hres = URLDownloadToCacheFileW(lpUnkCaller, url, file_name, dwBufLength*sizeof(WCHAR),
1595 dwReserved, pBSC);
1597 if(SUCCEEDED(hres) && file_name)
1598 WideCharToMultiByte(CP_ACP, 0, file_name, -1, szFileName, dwBufLength, NULL, NULL);
1600 HeapFree(GetProcessHeap(), 0, url);
1601 HeapFree(GetProcessHeap(), 0, file_name);
1603 return hres;
1606 /***********************************************************************
1607 * URLDownloadToCacheFileW (URLMON.@)
1609 HRESULT WINAPI URLDownloadToCacheFileW(LPUNKNOWN lpUnkCaller, LPCWSTR szURL, LPWSTR szFileName,
1610 DWORD dwBufLength, DWORD dwReserved, LPBINDSTATUSCALLBACK pBSC)
1612 WCHAR cache_path[MAX_PATH + 1];
1613 FILETIME expire, modified;
1614 HRESULT hr;
1615 LPWSTR ext;
1617 static WCHAR header[] = {
1618 'H','T','T','P','/','1','.','0',' ','2','0','0',' ',
1619 'O','K','\\','r','\\','n','\\','r','\\','n',0
1622 TRACE("(%p, %s, %p, %d, %d, %p)\n", lpUnkCaller, debugstr_w(szURL),
1623 szFileName, dwBufLength, dwReserved, pBSC);
1625 if (!szURL || !szFileName)
1626 return E_INVALIDARG;
1628 ext = PathFindExtensionW(szURL);
1630 if (!CreateUrlCacheEntryW(szURL, 0, ext, cache_path, 0))
1631 return E_FAIL;
1633 hr = URLDownloadToFileW(lpUnkCaller, szURL, cache_path, 0, pBSC);
1634 if (FAILED(hr))
1635 return hr;
1637 expire.dwHighDateTime = 0;
1638 expire.dwLowDateTime = 0;
1639 modified.dwHighDateTime = 0;
1640 modified.dwLowDateTime = 0;
1642 if (!CommitUrlCacheEntryW(szURL, cache_path, expire, modified, NORMAL_CACHE_ENTRY,
1643 header, sizeof(header), NULL, NULL))
1644 return E_FAIL;
1646 if (lstrlenW(cache_path) > dwBufLength)
1647 return E_OUTOFMEMORY;
1649 lstrcpyW(szFileName, cache_path);
1651 return S_OK;
1654 /***********************************************************************
1655 * HlinkSimpleNavigateToString (URLMON.@)
1657 HRESULT WINAPI HlinkSimpleNavigateToString( LPCWSTR szTarget,
1658 LPCWSTR szLocation, LPCWSTR szTargetFrameName, IUnknown *pUnk,
1659 IBindCtx *pbc, IBindStatusCallback *pbsc, DWORD grfHLNF, DWORD dwReserved)
1661 FIXME("%s\n", debugstr_w( szTarget ) );
1662 return E_NOTIMPL;
1665 /***********************************************************************
1666 * HlinkNavigateString (URLMON.@)
1668 HRESULT WINAPI HlinkNavigateString( IUnknown *pUnk, LPCWSTR szTarget )
1670 TRACE("%p %s\n", pUnk, debugstr_w( szTarget ) );
1671 return HlinkSimpleNavigateToString(
1672 szTarget, NULL, NULL, pUnk, NULL, NULL, 0, 0 );
1675 /***********************************************************************
1676 * GetSoftwareUpdateInfo (URLMON.@)
1678 HRESULT WINAPI GetSoftwareUpdateInfo( LPCWSTR szDistUnit, LPSOFTDISTINFO psdi )
1680 FIXME("%s %p\n", debugstr_w(szDistUnit), psdi );
1681 return E_FAIL;