urlmon: Optimize registering urlmon protocols.
[wine/hacks.git] / dlls / urlmon / umon.c
blobf6ea4274f033e09d55c2f6c22d952d3ff83cadf0
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, IBindCtx* pbc, VOID** ppvObject)
498 HRESULT hres;
499 BINDINFO bi;
500 DWORD bindf;
501 WCHAR szFileName[MAX_PATH + 1];
502 Binding *bind;
503 int len;
505 WARN("(%s %p %p)\n", debugstr_w(URLName), pbc, ppvObject);
507 bind = heap_alloc_zero(sizeof(Binding));
508 bind->lpVtbl = &BindingVtbl;
509 bind->ref = 1;
510 URLMON_LockModule();
512 len = lstrlenW(URLName)+1;
513 bind->URLName = heap_alloc(len*sizeof(WCHAR));
514 memcpy(bind->URLName, URLName, len*sizeof(WCHAR));
516 hres = UMCreateStreamOnCacheFile(bind->URLName, 0, szFileName, &bind->hCacheFile, &bind->pstrCache);
518 if(SUCCEEDED(hres)) {
519 TRACE("Created stream...\n");
521 *ppvObject = (void *) bind->pstrCache;
522 IStream_AddRef((IStream *) bind->pstrCache);
524 hres = IBindCtx_GetObjectParam(pbc, BSCBHolder, (IUnknown**)&bind->pbscb);
525 if(SUCCEEDED(hres)) {
526 TRACE("Got IBindStatusCallback...\n");
528 memset(&bi, 0, sizeof(bi));
529 bi.cbSize = sizeof(bi);
530 bindf = 0;
531 hres = IBindStatusCallback_GetBindInfo(bind->pbscb, &bindf, &bi);
532 if(SUCCEEDED(hres)) {
533 URL_COMPONENTSW url;
534 WCHAR *host, *path, *user, *pass;
535 DWORD lensz = sizeof(bind->expected_size);
536 DWORD dwService = 0;
537 BOOL bSuccess;
539 TRACE("got bindinfo. bindf = %08x extrainfo = %s bindinfof = %08x bindverb = %08x iid %s\n",
540 bindf, debugstr_w(bi.szExtraInfo), bi.grfBindInfoF, bi.dwBindVerb, debugstr_guid(&bi.iid));
541 hres = IBindStatusCallback_OnStartBinding(bind->pbscb, 0, (IBinding*)bind);
542 TRACE("OnStartBinding rets %08x\n", hres);
544 bind->expected_size = 0;
545 bind->total_read = 0;
547 memset(&url, 0, sizeof(url));
548 url.dwStructSize = sizeof(url);
549 url.dwSchemeLength = url.dwHostNameLength = url.dwUrlPathLength = url.dwUserNameLength = url.dwPasswordLength = 1;
550 InternetCrackUrlW(URLName, 0, ICU_ESCAPE, &url);
551 host = heap_alloc((url.dwHostNameLength + 1) * sizeof(WCHAR));
552 memcpy(host, url.lpszHostName, url.dwHostNameLength * sizeof(WCHAR));
553 host[url.dwHostNameLength] = '\0';
554 path = heap_alloc((url.dwUrlPathLength + 1) * sizeof(WCHAR));
555 memcpy(path, url.lpszUrlPath, url.dwUrlPathLength * sizeof(WCHAR));
556 path[url.dwUrlPathLength] = '\0';
557 if (url.dwUserNameLength)
559 user = heap_alloc(((url.dwUserNameLength + 1) * sizeof(WCHAR)));
560 memcpy(user, url.lpszUserName, url.dwUserNameLength * sizeof(WCHAR));
561 user[url.dwUserNameLength] = 0;
563 else
565 user = 0;
567 if (url.dwPasswordLength)
569 pass = heap_alloc(((url.dwPasswordLength + 1) * sizeof(WCHAR)));
570 memcpy(pass, url.lpszPassword, url.dwPasswordLength * sizeof(WCHAR));
571 pass[url.dwPasswordLength] = 0;
573 else
575 pass = 0;
579 do {
580 bind->hinternet = InternetOpenA("User Agent", 0, NULL, NULL, 0);
581 if (!bind->hinternet)
583 hres = HRESULT_FROM_WIN32(GetLastError());
584 break;
587 switch ((DWORD) url.nScheme)
589 case INTERNET_SCHEME_FTP:
590 if (!url.nPort)
591 url.nPort = INTERNET_DEFAULT_FTP_PORT;
592 dwService = INTERNET_SERVICE_FTP;
593 break;
595 case INTERNET_SCHEME_GOPHER:
596 if (!url.nPort)
597 url.nPort = INTERNET_DEFAULT_GOPHER_PORT;
598 dwService = INTERNET_SERVICE_GOPHER;
599 break;
601 case INTERNET_SCHEME_HTTPS:
602 if (!url.nPort)
603 url.nPort = INTERNET_DEFAULT_HTTPS_PORT;
604 dwService = INTERNET_SERVICE_HTTP;
605 break;
608 bind->hconnect = InternetConnectW(bind->hinternet, host, url.nPort, user, pass,
609 dwService, 0, (DWORD)bind);
610 if (!bind->hconnect)
612 hres = HRESULT_FROM_WIN32(GetLastError());
613 CloseHandle(bind->hinternet);
614 break;
617 hres = IBindStatusCallback_OnProgress(bind->pbscb, 0, 0, 0x22, NULL);
618 hres = IBindStatusCallback_OnProgress(bind->pbscb, 0, 0, BINDSTATUS_FINDINGRESOURCE, NULL);
619 hres = IBindStatusCallback_OnProgress(bind->pbscb, 0, 0, BINDSTATUS_CONNECTING, NULL);
620 hres = IBindStatusCallback_OnProgress(bind->pbscb, 0, 0, BINDSTATUS_SENDINGREQUEST, NULL);
622 bSuccess = FALSE;
624 switch (dwService)
626 case INTERNET_SERVICE_GOPHER:
627 bind->hrequest = GopherOpenFileW(bind->hconnect,
628 path,
630 INTERNET_FLAG_RELOAD,
632 if (bind->hrequest)
633 bSuccess = TRUE;
634 else
635 hres = HRESULT_FROM_WIN32(GetLastError());
636 break;
638 case INTERNET_SERVICE_FTP:
639 bind->hrequest = FtpOpenFileW(bind->hconnect,
640 path,
641 GENERIC_READ,
642 FTP_TRANSFER_TYPE_BINARY |
643 INTERNET_FLAG_TRANSFER_BINARY |
644 INTERNET_FLAG_RELOAD,
646 if (bind->hrequest)
647 bSuccess = TRUE;
648 else
649 hres = HRESULT_FROM_WIN32(GetLastError());
650 break;
652 case INTERNET_SERVICE_HTTP:
653 bind->hrequest = HttpOpenRequestW(bind->hconnect, NULL, path, NULL, NULL, NULL, 0, (DWORD)bind);
654 if (!bind->hrequest)
656 hres = HRESULT_FROM_WIN32(GetLastError());
658 else if (!HttpSendRequestW(bind->hrequest, NULL, 0, NULL, 0))
660 hres = HRESULT_FROM_WIN32(GetLastError());
661 InternetCloseHandle(bind->hrequest);
663 else
665 HttpQueryInfoW(bind->hrequest,
666 HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER,
667 &bind->expected_size,
668 &lensz,
669 NULL);
670 bSuccess = TRUE;
672 break;
674 if(bSuccess)
676 TRACE("res = %d gle = %u url len = %d\n", hres, GetLastError(), bind->expected_size);
678 IBindStatusCallback_OnProgress(bind->pbscb, 0, 0, BINDSTATUS_CACHEFILENAMEAVAILABLE, szFileName);
680 while(1) {
681 char buf[4096];
682 DWORD bufread;
683 if(InternetReadFile(bind->hrequest, buf, sizeof(buf), &bufread)) {
684 TRACE("read %d bytes %s...\n", bufread, debugstr_an(buf, 10));
685 if(bufread == 0) break;
686 hres = Binding_MoreCacheData(bind, buf, bufread);
687 } else
688 break;
690 InternetCloseHandle(bind->hrequest);
691 hres = S_OK;
694 InternetCloseHandle(bind->hconnect);
695 InternetCloseHandle(bind->hinternet);
696 } while(0);
698 Binding_FinishedDownload(bind, hres);
699 Binding_CloseCacheDownload(bind);
701 heap_free(user);
702 heap_free(pass);
703 heap_free(path);
704 heap_free(host);
709 IBinding_Release((IBinding*)bind);
711 return hres;
714 static HRESULT WINAPI URLMonikerImpl_BindToStorage(IMoniker* iface,
715 IBindCtx* pbc,
716 IMoniker* pmkToLeft,
717 REFIID riid,
718 VOID** ppvObject)
720 URLMonikerImpl *This = (URLMonikerImpl*)iface;
721 WCHAR schema[64];
722 BOOL bret;
724 URL_COMPONENTSW url = {sizeof(URL_COMPONENTSW), schema,
725 sizeof(schema)/sizeof(WCHAR), 0, NULL, 0, 0, NULL, 0, NULL, 0, NULL, 0, NULL, 0};
727 if(pmkToLeft)
728 FIXME("Unsupported pmkToLeft\n");
730 bret = InternetCrackUrlW(This->URLName, 0, ICU_ESCAPE, &url);
731 if(!bret) {
732 ERR("InternetCrackUrl failed: %u\n", GetLastError());
733 return E_FAIL;
736 if(IsEqualGUID(&IID_IStream, riid) &&
737 ( url.nScheme == INTERNET_SCHEME_HTTPS
738 || url.nScheme == INTERNET_SCHEME_FTP
739 || url.nScheme == INTERNET_SCHEME_GOPHER))
740 return URLMonikerImpl_BindToStorage_hack(This->URLName, pbc, ppvObject);
742 TRACE("(%p)->(%p %p %s %p)\n", This, pbc, pmkToLeft, debugstr_guid(riid), ppvObject);
744 return bind_to_storage(This->URLName, pbc, riid, ppvObject);
747 /******************************************************************************
748 * URLMoniker_Reduce
749 ******************************************************************************/
750 static HRESULT WINAPI URLMonikerImpl_Reduce(IMoniker* iface,
751 IBindCtx* pbc,
752 DWORD dwReduceHowFar,
753 IMoniker** ppmkToLeft,
754 IMoniker** ppmkReduced)
756 URLMonikerImpl *This = (URLMonikerImpl *)iface;
758 TRACE("(%p,%p,%d,%p,%p)\n",This,pbc,dwReduceHowFar,ppmkToLeft,ppmkReduced);
760 if(!ppmkReduced)
761 return E_INVALIDARG;
763 URLMonikerImpl_AddRef(iface);
764 *ppmkReduced = iface;
765 return MK_S_REDUCED_TO_SELF;
768 /******************************************************************************
769 * URLMoniker_ComposeWith
770 ******************************************************************************/
771 static HRESULT WINAPI URLMonikerImpl_ComposeWith(IMoniker* iface,
772 IMoniker* pmkRight,
773 BOOL fOnlyIfNotGeneric,
774 IMoniker** ppmkComposite)
776 URLMonikerImpl *This = (URLMonikerImpl *)iface;
777 FIXME("(%p)->(%p,%d,%p): stub\n",This,pmkRight,fOnlyIfNotGeneric,ppmkComposite);
779 return E_NOTIMPL;
782 /******************************************************************************
783 * URLMoniker_Enum
784 ******************************************************************************/
785 static HRESULT WINAPI URLMonikerImpl_Enum(IMoniker* iface,BOOL fForward, IEnumMoniker** ppenumMoniker)
787 URLMonikerImpl *This = (URLMonikerImpl *)iface;
788 TRACE("(%p,%d,%p)\n",This,fForward,ppenumMoniker);
790 if(!ppenumMoniker)
791 return E_INVALIDARG;
793 /* Does not support sub-monikers */
794 *ppenumMoniker = NULL;
795 return S_OK;
798 /******************************************************************************
799 * URLMoniker_IsEqual
800 ******************************************************************************/
801 static HRESULT WINAPI URLMonikerImpl_IsEqual(IMoniker* iface,IMoniker* pmkOtherMoniker)
803 URLMonikerImpl *This = (URLMonikerImpl *)iface;
804 CLSID clsid;
805 LPOLESTR urlPath;
806 IBindCtx* bind;
807 HRESULT res;
809 TRACE("(%p,%p)\n",This,pmkOtherMoniker);
811 if(pmkOtherMoniker==NULL)
812 return E_INVALIDARG;
814 IMoniker_GetClassID(pmkOtherMoniker,&clsid);
816 if(!IsEqualCLSID(&clsid,&CLSID_StdURLMoniker))
817 return S_FALSE;
819 res = CreateBindCtx(0,&bind);
820 if(FAILED(res))
821 return res;
823 res = S_FALSE;
824 if(SUCCEEDED(IMoniker_GetDisplayName(pmkOtherMoniker,bind,NULL,&urlPath))) {
825 int result = lstrcmpiW(urlPath, This->URLName);
826 CoTaskMemFree(urlPath);
827 if(result == 0)
828 res = S_OK;
830 IUnknown_Release(bind);
831 return res;
835 /******************************************************************************
836 * URLMoniker_Hash
837 ******************************************************************************/
838 static HRESULT WINAPI URLMonikerImpl_Hash(IMoniker* iface,DWORD* pdwHash)
840 URLMonikerImpl *This = (URLMonikerImpl *)iface;
842 int h = 0,i,skip,len;
843 int off = 0;
844 LPOLESTR val;
846 TRACE("(%p,%p)\n",This,pdwHash);
848 if(!pdwHash)
849 return E_INVALIDARG;
851 val = This->URLName;
852 len = lstrlenW(val);
854 if(len < 16) {
855 for(i = len ; i > 0; i--) {
856 h = (h * 37) + val[off++];
859 else {
860 /* only sample some characters */
861 skip = len / 8;
862 for(i = len; i > 0; i -= skip, off += skip) {
863 h = (h * 39) + val[off];
866 *pdwHash = h;
867 return S_OK;
870 /******************************************************************************
871 * URLMoniker_IsRunning
872 ******************************************************************************/
873 static HRESULT WINAPI URLMonikerImpl_IsRunning(IMoniker* iface,
874 IBindCtx* pbc,
875 IMoniker* pmkToLeft,
876 IMoniker* pmkNewlyRunning)
878 URLMonikerImpl *This = (URLMonikerImpl *)iface;
879 FIXME("(%p)->(%p,%p,%p): stub\n",This,pbc,pmkToLeft,pmkNewlyRunning);
881 return E_NOTIMPL;
884 /******************************************************************************
885 * URLMoniker_GetTimeOfLastChange
886 ******************************************************************************/
887 static HRESULT WINAPI URLMonikerImpl_GetTimeOfLastChange(IMoniker* iface,
888 IBindCtx* pbc,
889 IMoniker* pmkToLeft,
890 FILETIME* pFileTime)
892 URLMonikerImpl *This = (URLMonikerImpl *)iface;
893 FIXME("(%p)->(%p,%p,%p): stub\n",This,pbc,pmkToLeft,pFileTime);
895 return E_NOTIMPL;
898 /******************************************************************************
899 * URLMoniker_Inverse
900 ******************************************************************************/
901 static HRESULT WINAPI URLMonikerImpl_Inverse(IMoniker* iface,IMoniker** ppmk)
903 URLMonikerImpl *This = (URLMonikerImpl *)iface;
904 TRACE("(%p,%p)\n",This,ppmk);
906 return MK_E_NOINVERSE;
909 /******************************************************************************
910 * URLMoniker_CommonPrefixWith
911 ******************************************************************************/
912 static HRESULT WINAPI URLMonikerImpl_CommonPrefixWith(IMoniker* iface,IMoniker* pmkOther,IMoniker** ppmkPrefix)
914 URLMonikerImpl *This = (URLMonikerImpl *)iface;
915 FIXME("(%p)->(%p,%p): stub\n",This,pmkOther,ppmkPrefix);
917 return E_NOTIMPL;
920 /******************************************************************************
921 * URLMoniker_RelativePathTo
922 ******************************************************************************/
923 static HRESULT WINAPI URLMonikerImpl_RelativePathTo(IMoniker* iface,IMoniker* pmOther, IMoniker** ppmkRelPath)
925 URLMonikerImpl *This = (URLMonikerImpl *)iface;
926 FIXME("(%p)->(%p,%p): stub\n",This,pmOther,ppmkRelPath);
928 return E_NOTIMPL;
931 /******************************************************************************
932 * URLMoniker_GetDisplayName
933 ******************************************************************************/
934 static HRESULT WINAPI URLMonikerImpl_GetDisplayName(IMoniker* iface,
935 IBindCtx* pbc,
936 IMoniker* pmkToLeft,
937 LPOLESTR *ppszDisplayName)
939 URLMonikerImpl *This = (URLMonikerImpl *)iface;
941 int len;
943 TRACE("(%p,%p,%p,%p)\n",This,pbc,pmkToLeft,ppszDisplayName);
945 if(!ppszDisplayName)
946 return E_INVALIDARG;
948 /* FIXME: If this is a partial URL, try and get a URL moniker from SZ_URLCONTEXT in the bind context,
949 then look at pmkToLeft to try and complete the URL
951 len = lstrlenW(This->URLName)+1;
952 *ppszDisplayName = CoTaskMemAlloc(len*sizeof(WCHAR));
953 if(!*ppszDisplayName)
954 return E_OUTOFMEMORY;
955 lstrcpyW(*ppszDisplayName, This->URLName);
956 return S_OK;
959 /******************************************************************************
960 * URLMoniker_ParseDisplayName
961 ******************************************************************************/
962 static HRESULT WINAPI URLMonikerImpl_ParseDisplayName(IMoniker* iface,
963 IBindCtx* pbc,
964 IMoniker* pmkToLeft,
965 LPOLESTR pszDisplayName,
966 ULONG* pchEaten,
967 IMoniker** ppmkOut)
969 URLMonikerImpl *This = (URLMonikerImpl *)iface;
970 FIXME("(%p)->(%p,%p,%p,%p,%p): stub\n",This,pbc,pmkToLeft,pszDisplayName,pchEaten,ppmkOut);
972 return E_NOTIMPL;
975 /******************************************************************************
976 * URLMoniker_IsSystemMoniker
977 ******************************************************************************/
978 static HRESULT WINAPI URLMonikerImpl_IsSystemMoniker(IMoniker* iface,DWORD* pwdMksys)
980 URLMonikerImpl *This = (URLMonikerImpl *)iface;
981 TRACE("(%p,%p)\n",This,pwdMksys);
983 if(!pwdMksys)
984 return E_INVALIDARG;
986 *pwdMksys = MKSYS_URLMONIKER;
987 return S_OK;
990 /********************************************************************************/
991 /* Virtual function table for the URLMonikerImpl class which include IPersist,*/
992 /* IPersistStream and IMoniker functions. */
993 static const IMonikerVtbl VT_URLMonikerImpl =
995 URLMonikerImpl_QueryInterface,
996 URLMonikerImpl_AddRef,
997 URLMonikerImpl_Release,
998 URLMonikerImpl_GetClassID,
999 URLMonikerImpl_IsDirty,
1000 URLMonikerImpl_Load,
1001 URLMonikerImpl_Save,
1002 URLMonikerImpl_GetSizeMax,
1003 URLMonikerImpl_BindToObject,
1004 URLMonikerImpl_BindToStorage,
1005 URLMonikerImpl_Reduce,
1006 URLMonikerImpl_ComposeWith,
1007 URLMonikerImpl_Enum,
1008 URLMonikerImpl_IsEqual,
1009 URLMonikerImpl_Hash,
1010 URLMonikerImpl_IsRunning,
1011 URLMonikerImpl_GetTimeOfLastChange,
1012 URLMonikerImpl_Inverse,
1013 URLMonikerImpl_CommonPrefixWith,
1014 URLMonikerImpl_RelativePathTo,
1015 URLMonikerImpl_GetDisplayName,
1016 URLMonikerImpl_ParseDisplayName,
1017 URLMonikerImpl_IsSystemMoniker
1020 /******************************************************************************
1021 * URLMoniker_Construct (local function)
1022 *******************************************************************************/
1023 static HRESULT URLMonikerImpl_Construct(URLMonikerImpl* This, LPCOLESTR lpszLeftURLName, LPCOLESTR lpszURLName)
1025 HRESULT hres;
1026 DWORD sizeStr = 0;
1028 TRACE("(%p,%s,%s)\n",This,debugstr_w(lpszLeftURLName),debugstr_w(lpszURLName));
1030 This->lpvtbl = &VT_URLMonikerImpl;
1031 This->ref = 0;
1033 This->URLName = heap_alloc(INTERNET_MAX_URL_LENGTH*sizeof(WCHAR));
1035 if(lpszLeftURLName)
1036 hres = CoInternetCombineUrl(lpszLeftURLName, lpszURLName, URL_FILE_USE_PATHURL,
1037 This->URLName, INTERNET_MAX_URL_LENGTH, &sizeStr, 0);
1038 else
1039 hres = CoInternetParseUrl(lpszURLName, PARSE_CANONICALIZE, URL_FILE_USE_PATHURL,
1040 This->URLName, INTERNET_MAX_URL_LENGTH, &sizeStr, 0);
1042 if(FAILED(hres)) {
1043 heap_free(This->URLName);
1044 return hres;
1047 URLMON_LockModule();
1049 if(sizeStr != INTERNET_MAX_URL_LENGTH)
1050 This->URLName = heap_realloc(This->URLName, (sizeStr+1)*sizeof(WCHAR));
1052 TRACE("URLName = %s\n", debugstr_w(This->URLName));
1054 return S_OK;
1057 /***********************************************************************
1058 * CreateURLMonikerEx (URLMON.@)
1060 * Create a url moniker.
1062 * PARAMS
1063 * pmkContext [I] Context
1064 * szURL [I] Url to create the moniker for
1065 * ppmk [O] Destination for created moniker.
1066 * dwFlags [I] Flags.
1068 * RETURNS
1069 * Success: S_OK. ppmk contains the created IMoniker object.
1070 * Failure: MK_E_SYNTAX if szURL is not a valid url, or
1071 * E_OUTOFMEMORY if memory allocation fails.
1073 HRESULT WINAPI CreateURLMonikerEx(IMoniker *pmkContext, LPCWSTR szURL, IMoniker **ppmk, DWORD dwFlags)
1075 URLMonikerImpl *obj;
1076 HRESULT hres;
1077 LPOLESTR lefturl = NULL;
1079 TRACE("(%p, %s, %p, %08x)\n", pmkContext, debugstr_w(szURL), ppmk, dwFlags);
1081 if (dwFlags & URL_MK_UNIFORM) FIXME("ignoring flag URL_MK_UNIFORM\n");
1083 if(!(obj = heap_alloc(sizeof(*obj))))
1084 return E_OUTOFMEMORY;
1086 if(pmkContext) {
1087 IBindCtx* bind;
1088 DWORD dwMksys = 0;
1089 IMoniker_IsSystemMoniker(pmkContext, &dwMksys);
1090 if(dwMksys == MKSYS_URLMONIKER && SUCCEEDED(CreateBindCtx(0, &bind))) {
1091 IMoniker_GetDisplayName(pmkContext, bind, NULL, &lefturl);
1092 TRACE("lefturl = %s\n", debugstr_w(lefturl));
1093 IBindCtx_Release(bind);
1097 hres = URLMonikerImpl_Construct(obj, lefturl, szURL);
1098 CoTaskMemFree(lefturl);
1099 if(SUCCEEDED(hres))
1100 hres = URLMonikerImpl_QueryInterface((IMoniker*)obj, &IID_IMoniker, (void**)ppmk);
1101 else
1102 heap_free(obj);
1103 return hres;
1106 /**********************************************************************
1107 * CreateURLMoniker (URLMON.@)
1109 * Create a url moniker.
1111 * PARAMS
1112 * pmkContext [I] Context
1113 * szURL [I] Url to create the moniker for
1114 * ppmk [O] Destination for created moniker.
1116 * RETURNS
1117 * Success: S_OK. ppmk contains the created IMoniker object.
1118 * Failure: MK_E_SYNTAX if szURL is not a valid url, or
1119 * E_OUTOFMEMORY if memory allocation fails.
1121 HRESULT WINAPI CreateURLMoniker(IMoniker *pmkContext, LPCWSTR szURL, IMoniker **ppmk)
1123 return CreateURLMonikerEx(pmkContext, szURL, ppmk, URL_MK_LEGACY);
1126 /***********************************************************************
1127 * IsAsyncMoniker (URLMON.@)
1129 HRESULT WINAPI IsAsyncMoniker(IMoniker *pmk)
1131 IUnknown *am;
1133 TRACE("(%p)\n", pmk);
1134 if(!pmk)
1135 return E_INVALIDARG;
1136 if(SUCCEEDED(IMoniker_QueryInterface(pmk, &IID_IAsyncMoniker, (void**)&am))) {
1137 IUnknown_Release(am);
1138 return S_OK;
1140 return S_FALSE;
1143 /***********************************************************************
1144 * BindAsyncMoniker (URLMON.@)
1146 * Bind a bind status callback to an asynchronous URL Moniker.
1148 * PARAMS
1149 * pmk [I] Moniker object to bind status callback to
1150 * grfOpt [I] Options, seems not used
1151 * pbsc [I] Status callback to bind
1152 * iidResult [I] Interface to return
1153 * ppvResult [O] Resulting asynchronous moniker object
1155 * RETURNS
1156 * Success: S_OK.
1157 * Failure: E_INVALIDARG, if any argument is invalid, or
1158 * E_OUTOFMEMORY if memory allocation fails.
1160 HRESULT WINAPI BindAsyncMoniker(IMoniker *pmk, DWORD grfOpt, IBindStatusCallback *pbsc, REFIID iidResult, LPVOID *ppvResult)
1162 LPBC pbc = NULL;
1163 HRESULT hr = E_INVALIDARG;
1165 TRACE("(%p %08x %p %s %p)\n", pmk, grfOpt, pbsc, debugstr_guid(iidResult), ppvResult);
1167 if (pmk && ppvResult)
1169 *ppvResult = NULL;
1171 hr = CreateAsyncBindCtx(0, pbsc, NULL, &pbc);
1172 if (hr == NOERROR)
1174 hr = IMoniker_BindToObject(pmk, pbc, NULL, iidResult, ppvResult);
1175 IBindCtx_Release(pbc);
1178 return hr;
1181 /***********************************************************************
1182 * MkParseDisplayNameEx (URLMON.@)
1184 HRESULT WINAPI MkParseDisplayNameEx(IBindCtx *pbc, LPCWSTR szDisplayName, ULONG *pchEaten, LPMONIKER *ppmk)
1186 TRACE("(%p %s %p %p)\n", pbc, debugstr_w(szDisplayName), pchEaten, ppmk);
1188 if(is_registered_protocol(szDisplayName)) {
1189 HRESULT hres;
1191 hres = CreateURLMoniker(NULL, szDisplayName, ppmk);
1192 if(SUCCEEDED(hres)) {
1193 *pchEaten = strlenW(szDisplayName);
1194 return hres;
1198 return MkParseDisplayName(pbc, szDisplayName, pchEaten, ppmk);
1202 /***********************************************************************
1203 * URLDownloadToCacheFileA (URLMON.@)
1205 HRESULT WINAPI URLDownloadToCacheFileA(LPUNKNOWN lpUnkCaller, LPCSTR szURL, LPSTR szFileName,
1206 DWORD dwBufLength, DWORD dwReserved, LPBINDSTATUSCALLBACK pBSC)
1208 LPWSTR url = NULL, file_name = NULL;
1209 int len;
1210 HRESULT hres;
1212 TRACE("(%p %s %p %d %d %p)\n", lpUnkCaller, debugstr_a(szURL), szFileName,
1213 dwBufLength, dwReserved, pBSC);
1215 if(szURL) {
1216 len = MultiByteToWideChar(CP_ACP, 0, szURL, -1, NULL, 0);
1217 url = heap_alloc(len*sizeof(WCHAR));
1218 MultiByteToWideChar(CP_ACP, 0, szURL, -1, url, -1);
1221 if(szFileName)
1222 file_name = heap_alloc(dwBufLength*sizeof(WCHAR));
1224 hres = URLDownloadToCacheFileW(lpUnkCaller, url, file_name, dwBufLength*sizeof(WCHAR),
1225 dwReserved, pBSC);
1227 if(SUCCEEDED(hres) && file_name)
1228 WideCharToMultiByte(CP_ACP, 0, file_name, -1, szFileName, dwBufLength, NULL, NULL);
1230 heap_free(url);
1231 heap_free(file_name);
1233 return hres;
1236 /***********************************************************************
1237 * URLDownloadToCacheFileW (URLMON.@)
1239 HRESULT WINAPI URLDownloadToCacheFileW(LPUNKNOWN lpUnkCaller, LPCWSTR szURL, LPWSTR szFileName,
1240 DWORD dwBufLength, DWORD dwReserved, LPBINDSTATUSCALLBACK pBSC)
1242 WCHAR cache_path[MAX_PATH + 1];
1243 FILETIME expire, modified;
1244 HRESULT hr;
1245 LPWSTR ext;
1247 static WCHAR header[] = {
1248 'H','T','T','P','/','1','.','0',' ','2','0','0',' ',
1249 'O','K','\\','r','\\','n','\\','r','\\','n',0
1252 TRACE("(%p, %s, %p, %d, %d, %p)\n", lpUnkCaller, debugstr_w(szURL),
1253 szFileName, dwBufLength, dwReserved, pBSC);
1255 if (!szURL || !szFileName)
1256 return E_INVALIDARG;
1258 ext = PathFindExtensionW(szURL);
1260 if (!CreateUrlCacheEntryW(szURL, 0, ext, cache_path, 0))
1261 return E_FAIL;
1263 hr = URLDownloadToFileW(lpUnkCaller, szURL, cache_path, 0, pBSC);
1264 if (FAILED(hr))
1265 return hr;
1267 expire.dwHighDateTime = 0;
1268 expire.dwLowDateTime = 0;
1269 modified.dwHighDateTime = 0;
1270 modified.dwLowDateTime = 0;
1272 if (!CommitUrlCacheEntryW(szURL, cache_path, expire, modified, NORMAL_CACHE_ENTRY,
1273 header, sizeof(header), NULL, NULL))
1274 return E_FAIL;
1276 if (lstrlenW(cache_path) > dwBufLength)
1277 return E_OUTOFMEMORY;
1279 lstrcpyW(szFileName, cache_path);
1281 return S_OK;
1284 /***********************************************************************
1285 * HlinkSimpleNavigateToString (URLMON.@)
1287 HRESULT WINAPI HlinkSimpleNavigateToString( LPCWSTR szTarget,
1288 LPCWSTR szLocation, LPCWSTR szTargetFrameName, IUnknown *pUnk,
1289 IBindCtx *pbc, IBindStatusCallback *pbsc, DWORD grfHLNF, DWORD dwReserved)
1291 FIXME("%s\n", debugstr_w( szTarget ) );
1292 return E_NOTIMPL;
1295 /***********************************************************************
1296 * HlinkNavigateString (URLMON.@)
1298 HRESULT WINAPI HlinkNavigateString( IUnknown *pUnk, LPCWSTR szTarget )
1300 TRACE("%p %s\n", pUnk, debugstr_w( szTarget ) );
1301 return HlinkSimpleNavigateToString(
1302 szTarget, NULL, NULL, pUnk, NULL, NULL, 0, 0 );
1305 /***********************************************************************
1306 * GetSoftwareUpdateInfo (URLMON.@)
1308 HRESULT WINAPI GetSoftwareUpdateInfo( LPCWSTR szDistUnit, LPSOFTDISTINFO psdi )
1310 FIXME("%s %p\n", debugstr_w(szDistUnit), psdi );
1311 return E_FAIL;