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