msi: Include the terminating null in the buffer size for decoded stream names.
[wine/wine-gecko.git] / dlls / mshtml / protocol.c
blobc8f8d08243dfc72dfcdfa8c1c342735c9fc1008a
1 /*
2 * Copyright 2005 Jacek Caban
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include "config.h"
21 #include <stdarg.h>
22 #include <stdio.h>
24 #define COBJMACROS
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "ole2.h"
31 #include "wine/debug.h"
33 #include "mshtml_private.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
37 /********************************************************************
38 * common ProtocolFactory implementation
41 typedef struct {
42 IInternetProtocolInfo IInternetProtocolInfo_iface;
43 IClassFactory IClassFactory_iface;
44 } ProtocolFactory;
46 static inline ProtocolFactory *impl_from_IInternetProtocolInfo(IInternetProtocolInfo *iface)
48 return CONTAINING_RECORD(iface, ProtocolFactory, IInternetProtocolInfo_iface);
51 static HRESULT WINAPI InternetProtocolInfo_QueryInterface(IInternetProtocolInfo *iface, REFIID riid, void **ppv)
53 ProtocolFactory *This = impl_from_IInternetProtocolInfo(iface);
55 *ppv = NULL;
56 if(IsEqualGUID(&IID_IUnknown, riid)) {
57 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
58 *ppv = &This->IInternetProtocolInfo_iface;
59 }else if(IsEqualGUID(&IID_IInternetProtocolInfo, riid)) {
60 TRACE("(%p)->(IID_IInternetProtocolInfo %p)\n", This, ppv);
61 *ppv = &This->IInternetProtocolInfo_iface;
62 }else if(IsEqualGUID(&IID_IClassFactory, riid)) {
63 TRACE("(%p)->(IID_IClassFactory %p)\n", This, ppv);
64 *ppv = &This->IClassFactory_iface;
67 if(!*ppv) {
68 WARN("unknown interface %s\n", debugstr_guid(riid));
69 return E_NOINTERFACE;
72 IInternetProtocolInfo_AddRef(iface);
73 return S_OK;
76 static ULONG WINAPI InternetProtocolInfo_AddRef(IInternetProtocolInfo *iface)
78 TRACE("(%p)\n", iface);
79 return 2;
82 static ULONG WINAPI InternetProtocolInfo_Release(IInternetProtocolInfo *iface)
84 TRACE("(%p)\n", iface);
85 return 1;
88 static HRESULT WINAPI InternetProtocolInfo_CombineUrl(IInternetProtocolInfo *iface,
89 LPCWSTR pwzBaseUrl, LPCWSTR pwzRelativeUrl, DWORD dwCombineFlags, LPWSTR pwzResult,
90 DWORD cchResult, DWORD* pcchResult, DWORD dwReserved)
92 TRACE("%p)->(%s %s %08x %p %d %p %d)\n", iface, debugstr_w(pwzBaseUrl),
93 debugstr_w(pwzRelativeUrl), dwCombineFlags, pwzResult, cchResult,
94 pcchResult, dwReserved);
96 return INET_E_USE_DEFAULT_PROTOCOLHANDLER;
99 static HRESULT WINAPI InternetProtocolInfo_CompareUrl(IInternetProtocolInfo *iface, LPCWSTR pwzUrl1,
100 LPCWSTR pwzUrl2, DWORD dwCompareFlags)
102 TRACE("%p)->(%s %s %08x)\n", iface, debugstr_w(pwzUrl1), debugstr_w(pwzUrl2), dwCompareFlags);
103 return E_NOTIMPL;
106 static inline ProtocolFactory *impl_from_IClassFactory(IClassFactory *iface)
108 return CONTAINING_RECORD(iface, ProtocolFactory, IClassFactory_iface);
111 static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFIID riid, void **ppv)
113 ProtocolFactory *This = impl_from_IClassFactory(iface);
114 return IInternetProtocolInfo_QueryInterface(&This->IInternetProtocolInfo_iface, riid, ppv);
117 static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface)
119 ProtocolFactory *This = impl_from_IClassFactory(iface);
120 return IInternetProtocolInfo_AddRef(&This->IInternetProtocolInfo_iface);
123 static ULONG WINAPI ClassFactory_Release(IClassFactory *iface)
125 ProtocolFactory *This = impl_from_IClassFactory(iface);
126 return IInternetProtocolInfo_Release(&This->IInternetProtocolInfo_iface);
129 static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL dolock)
131 TRACE("(%p)->(%x)\n", iface, dolock);
132 return S_OK;
135 /********************************************************************
136 * AboutProtocol implementation
139 typedef struct {
140 IInternetProtocol IInternetProtocol_iface;
142 LONG ref;
144 BYTE *data;
145 ULONG data_len;
146 ULONG cur;
148 IUnknown *pUnkOuter;
149 } AboutProtocol;
151 static inline AboutProtocol *AboutProtocol_from_IInternetProtocol(IInternetProtocol *iface)
153 return CONTAINING_RECORD(iface, AboutProtocol, IInternetProtocol_iface);
156 static HRESULT WINAPI AboutProtocol_QueryInterface(IInternetProtocol *iface, REFIID riid, void **ppv)
158 AboutProtocol *This = AboutProtocol_from_IInternetProtocol(iface);
160 *ppv = NULL;
162 if(IsEqualGUID(&IID_IUnknown, riid)) {
163 TRACE("(%p)->(IID_IUnknown %p)\n", iface, ppv);
164 if(This->pUnkOuter)
165 return IUnknown_QueryInterface(This->pUnkOuter, riid, ppv);
166 *ppv = &This->IInternetProtocol_iface;
167 }else if(IsEqualGUID(&IID_IInternetProtocolRoot, riid)) {
168 TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", iface, ppv);
169 *ppv = &This->IInternetProtocol_iface;
170 }else if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
171 TRACE("(%p)->(IID_IInternetProtocol %p)\n", iface, ppv);
172 *ppv = &This->IInternetProtocol_iface;
173 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
174 FIXME("IServiceProvider is not implemented\n");
175 return E_NOINTERFACE;
178 if(!*ppv) {
179 TRACE("unknown interface %s\n", debugstr_guid(riid));
180 return E_NOINTERFACE;
183 IInternetProtocol_AddRef(iface);
184 return S_OK;
187 static ULONG WINAPI AboutProtocol_AddRef(IInternetProtocol *iface)
189 AboutProtocol *This = AboutProtocol_from_IInternetProtocol(iface);
190 ULONG ref = InterlockedIncrement(&This->ref);
191 TRACE("(%p) ref=%d\n", iface, ref);
192 return This->pUnkOuter ? IUnknown_AddRef(This->pUnkOuter) : ref;
195 static ULONG WINAPI AboutProtocol_Release(IInternetProtocol *iface)
197 AboutProtocol *This = AboutProtocol_from_IInternetProtocol(iface);
198 IUnknown *pUnkOuter = This->pUnkOuter;
199 ULONG ref = InterlockedDecrement(&This->ref);
201 TRACE("(%p) ref=%x\n", iface, ref);
203 if(!ref) {
204 heap_free(This->data);
205 heap_free(This);
208 return pUnkOuter ? IUnknown_Release(pUnkOuter) : ref;
211 static HRESULT WINAPI AboutProtocol_Start(IInternetProtocol *iface, LPCWSTR szUrl,
212 IInternetProtocolSink* pOIProtSink, IInternetBindInfo* pOIBindInfo,
213 DWORD grfPI, HANDLE_PTR dwReserved)
215 AboutProtocol *This = AboutProtocol_from_IInternetProtocol(iface);
216 BINDINFO bindinfo;
217 DWORD grfBINDF = 0;
218 LPCWSTR text = NULL;
219 DWORD data_len;
220 BYTE *data;
221 HRESULT hres;
223 static const WCHAR html_begin[] = {0xfeff,'<','H','T','M','L','>',0};
224 static const WCHAR html_end[] = {'<','/','H','T','M','L','>',0};
225 static const WCHAR wszBlank[] = {'b','l','a','n','k',0};
226 static const WCHAR wszAbout[] = {'a','b','o','u','t',':'};
227 static const WCHAR wszTextHtml[] = {'t','e','x','t','/','h','t','m','l',0};
229 /* NOTE:
230 * the about protocol seems not to work as I would expect. It creates html document
231 * for a given url, eg. about:some_text -> <HTML>some_text</HTML> except for the case when
232 * some_text = "blank", when document is blank (<HTML></HMTL>). The same happens
233 * when the url does not have "about:" in the beginning.
236 TRACE("(%p)->(%s %p %p %08x %lx)\n", This, debugstr_w(szUrl), pOIProtSink,
237 pOIBindInfo, grfPI, dwReserved);
239 memset(&bindinfo, 0, sizeof(bindinfo));
240 bindinfo.cbSize = sizeof(BINDINFO);
241 hres = IInternetBindInfo_GetBindInfo(pOIBindInfo, &grfBINDF, &bindinfo);
242 if(FAILED(hres))
243 return hres;
244 ReleaseBindInfo(&bindinfo);
246 TRACE("bindf %x\n", grfBINDF);
248 if(strlenW(szUrl)>=sizeof(wszAbout)/sizeof(WCHAR) && !memcmp(wszAbout, szUrl, sizeof(wszAbout))) {
249 text = szUrl + sizeof(wszAbout)/sizeof(WCHAR);
250 if(!strcmpW(wszBlank, text))
251 text = NULL;
254 data_len = sizeof(html_begin)+sizeof(html_end)-sizeof(WCHAR)
255 + (text ? strlenW(text)*sizeof(WCHAR) : 0);
256 data = heap_alloc(data_len);
257 if(!data)
258 return E_OUTOFMEMORY;
260 heap_free(This->data);
261 This->data = data;
262 This->data_len = data_len;
264 memcpy(This->data, html_begin, sizeof(html_begin));
265 if(text)
266 strcatW((LPWSTR)This->data, text);
267 strcatW((LPWSTR)This->data, html_end);
269 This->cur = 0;
271 IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_MIMETYPEAVAILABLE, wszTextHtml);
273 IInternetProtocolSink_ReportData(pOIProtSink,
274 BSCF_FIRSTDATANOTIFICATION | BSCF_LASTDATANOTIFICATION | BSCF_DATAFULLYAVAILABLE,
275 This->data_len, This->data_len);
277 IInternetProtocolSink_ReportResult(pOIProtSink, S_OK, 0, NULL);
279 return S_OK;
282 static HRESULT WINAPI AboutProtocol_Continue(IInternetProtocol *iface, PROTOCOLDATA* pProtocolData)
284 AboutProtocol *This = AboutProtocol_from_IInternetProtocol(iface);
285 FIXME("(%p)->(%p)\n", This, pProtocolData);
286 return E_NOTIMPL;
289 static HRESULT WINAPI AboutProtocol_Abort(IInternetProtocol *iface, HRESULT hrReason,
290 DWORD dwOptions)
292 AboutProtocol *This = AboutProtocol_from_IInternetProtocol(iface);
293 FIXME("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);
294 return E_NOTIMPL;
297 static HRESULT WINAPI AboutProtocol_Terminate(IInternetProtocol *iface, DWORD dwOptions)
299 AboutProtocol *This = AboutProtocol_from_IInternetProtocol(iface);
300 TRACE("(%p)->(%08x)\n", This, dwOptions);
301 return S_OK;
304 static HRESULT WINAPI AboutProtocol_Suspend(IInternetProtocol *iface)
306 AboutProtocol *This = AboutProtocol_from_IInternetProtocol(iface);
307 FIXME("(%p)\n", This);
308 return E_NOTIMPL;
311 static HRESULT WINAPI AboutProtocol_Resume(IInternetProtocol *iface)
313 AboutProtocol *This = AboutProtocol_from_IInternetProtocol(iface);
314 FIXME("(%p)\n", This);
315 return E_NOTIMPL;
318 static HRESULT WINAPI AboutProtocol_Read(IInternetProtocol *iface, void* pv, ULONG cb, ULONG* pcbRead)
320 AboutProtocol *This = AboutProtocol_from_IInternetProtocol(iface);
322 TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
324 if(!This->data)
325 return E_FAIL;
327 *pcbRead = (cb > This->data_len-This->cur ? This->data_len-This->cur : cb);
329 if(!*pcbRead)
330 return S_FALSE;
332 memcpy(pv, This->data+This->cur, *pcbRead);
333 This->cur += *pcbRead;
335 return S_OK;
338 static HRESULT WINAPI AboutProtocol_Seek(IInternetProtocol *iface, LARGE_INTEGER dlibMove,
339 DWORD dwOrigin, ULARGE_INTEGER* plibNewPosition)
341 AboutProtocol *This = AboutProtocol_from_IInternetProtocol(iface);
342 FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
343 return E_NOTIMPL;
346 static HRESULT WINAPI AboutProtocol_LockRequest(IInternetProtocol *iface, DWORD dwOptions)
348 AboutProtocol *This = AboutProtocol_from_IInternetProtocol(iface);
350 TRACE("(%p)->(%d)\n", This, dwOptions);
352 return S_OK;
355 static HRESULT WINAPI AboutProtocol_UnlockRequest(IInternetProtocol *iface)
357 AboutProtocol *This = AboutProtocol_from_IInternetProtocol(iface);
359 TRACE("(%p)\n", This);
361 return S_OK;
364 static const IInternetProtocolVtbl AboutProtocolVtbl = {
365 AboutProtocol_QueryInterface,
366 AboutProtocol_AddRef,
367 AboutProtocol_Release,
368 AboutProtocol_Start,
369 AboutProtocol_Continue,
370 AboutProtocol_Abort,
371 AboutProtocol_Terminate,
372 AboutProtocol_Suspend,
373 AboutProtocol_Resume,
374 AboutProtocol_Read,
375 AboutProtocol_Seek,
376 AboutProtocol_LockRequest,
377 AboutProtocol_UnlockRequest
380 static HRESULT WINAPI AboutProtocolFactory_CreateInstance(IClassFactory *iface, IUnknown *pUnkOuter,
381 REFIID riid, void **ppv)
383 AboutProtocol *ret;
384 HRESULT hres = S_OK;
386 TRACE("(%p)->(%p %s %p)\n", iface, pUnkOuter, debugstr_guid(riid), ppv);
388 ret = heap_alloc(sizeof(AboutProtocol));
389 ret->IInternetProtocol_iface.lpVtbl = &AboutProtocolVtbl;
390 ret->ref = 0;
392 ret->data = NULL;
393 ret->data_len = 0;
394 ret->cur = 0;
395 ret->pUnkOuter = pUnkOuter;
397 if(pUnkOuter) {
398 ret->ref = 1;
399 if(IsEqualGUID(&IID_IUnknown, riid))
400 *ppv = &ret->IInternetProtocol_iface;
401 else
402 hres = E_INVALIDARG;
403 }else {
404 hres = IInternetProtocol_QueryInterface(&ret->IInternetProtocol_iface, riid, ppv);
407 if(FAILED(hres))
408 heap_free(ret);
410 return hres;
413 static HRESULT WINAPI AboutProtocolInfo_ParseUrl(IInternetProtocolInfo *iface, LPCWSTR pwzUrl,
414 PARSEACTION ParseAction, DWORD dwParseFlags, LPWSTR pwzResult, DWORD cchResult,
415 DWORD* pcchResult, DWORD dwReserved)
417 TRACE("%p)->(%s %d %08x %p %d %p %d)\n", iface, debugstr_w(pwzUrl), ParseAction,
418 dwParseFlags, pwzResult, cchResult, pcchResult, dwReserved);
420 if(ParseAction == PARSE_SECURITY_URL) {
421 unsigned int len = strlenW(pwzUrl)+1;
423 *pcchResult = len;
424 if(len > cchResult)
425 return S_FALSE;
427 memcpy(pwzResult, pwzUrl, len*sizeof(WCHAR));
428 return S_OK;
431 if(ParseAction == PARSE_DOMAIN) {
432 if(!pcchResult)
433 return E_POINTER;
435 if(pwzUrl)
436 *pcchResult = strlenW(pwzUrl)+1;
437 else
438 *pcchResult = 1;
439 return E_FAIL;
442 return INET_E_DEFAULT_ACTION;
445 static HRESULT WINAPI AboutProtocolInfo_QueryInfo(IInternetProtocolInfo *iface, LPCWSTR pwzUrl,
446 QUERYOPTION QueryOption, DWORD dwQueryFlags, LPVOID pBuffer, DWORD cbBuffer, DWORD* pcbBuf,
447 DWORD dwReserved)
449 TRACE("%p)->(%s %08x %08x %p %d %p %d)\n", iface, debugstr_w(pwzUrl), QueryOption, dwQueryFlags, pBuffer,
450 cbBuffer, pcbBuf, dwReserved);
452 switch(QueryOption) {
453 case QUERY_CAN_NAVIGATE:
454 return INET_E_USE_DEFAULT_PROTOCOLHANDLER;
456 case QUERY_USES_NETWORK:
457 if(!pBuffer || cbBuffer < sizeof(DWORD))
458 return E_FAIL;
460 *(DWORD*)pBuffer = 0;
461 if(pcbBuf)
462 *pcbBuf = sizeof(DWORD);
464 break;
466 case QUERY_IS_CACHED:
467 FIXME("Unsupported option QUERY_IS_CACHED\n");
468 return E_NOTIMPL;
469 case QUERY_IS_INSTALLEDENTRY:
470 FIXME("Unsupported option QUERY_IS_INSTALLEDENTRY\n");
471 return E_NOTIMPL;
472 case QUERY_IS_CACHED_OR_MAPPED:
473 FIXME("Unsupported option QUERY_IS_CACHED_OR_MAPPED\n");
474 return E_NOTIMPL;
475 case QUERY_IS_SECURE:
476 FIXME("Unsupported option QUERY_IS_SECURE\n");
477 return E_NOTIMPL;
478 case QUERY_IS_SAFE:
479 FIXME("Unsupported option QUERY_IS_SAFE\n");
480 return E_NOTIMPL;
481 case QUERY_USES_HISTORYFOLDER:
482 FIXME("Unsupported option QUERY_USES_HISTORYFOLDER\n");
483 return E_FAIL;
484 case QUERY_IS_CACHED_AND_USABLE_OFFLINE:
485 FIXME("Unsupported option QUERY_IS_CACHED_AND_USABLE_OFFLINE\n");
486 return E_NOTIMPL;
487 default:
488 return E_FAIL;
491 return S_OK;
494 static const IInternetProtocolInfoVtbl AboutProtocolInfoVtbl = {
495 InternetProtocolInfo_QueryInterface,
496 InternetProtocolInfo_AddRef,
497 InternetProtocolInfo_Release,
498 AboutProtocolInfo_ParseUrl,
499 InternetProtocolInfo_CombineUrl,
500 InternetProtocolInfo_CompareUrl,
501 AboutProtocolInfo_QueryInfo
504 static const IClassFactoryVtbl AboutProtocolFactoryVtbl = {
505 ClassFactory_QueryInterface,
506 ClassFactory_AddRef,
507 ClassFactory_Release,
508 AboutProtocolFactory_CreateInstance,
509 ClassFactory_LockServer
512 static ProtocolFactory AboutProtocolFactory = {
513 { &AboutProtocolInfoVtbl },
514 { &AboutProtocolFactoryVtbl }
517 /********************************************************************
518 * ResProtocol implementation
521 typedef struct {
522 IInternetProtocol IInternetProtocol_iface;
523 LONG ref;
525 BYTE *data;
526 ULONG data_len;
527 ULONG cur;
529 IUnknown *pUnkOuter;
530 } ResProtocol;
532 static inline ResProtocol *ResProtocol_from_IInternetProtocol(IInternetProtocol *iface)
534 return CONTAINING_RECORD(iface, ResProtocol, IInternetProtocol_iface);
537 static HRESULT WINAPI ResProtocol_QueryInterface(IInternetProtocol *iface, REFIID riid, void **ppv)
539 ResProtocol *This = ResProtocol_from_IInternetProtocol(iface);
541 *ppv = NULL;
543 if(IsEqualGUID(&IID_IUnknown, riid)) {
544 TRACE("(%p)->(IID_IUnknown %p)\n", iface, ppv);
545 if(This->pUnkOuter)
546 return IUnknown_QueryInterface(This->pUnkOuter, &IID_IUnknown, ppv);
547 *ppv = &This->IInternetProtocol_iface;
548 }else if(IsEqualGUID(&IID_IInternetProtocolRoot, riid)) {
549 TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", iface, ppv);
550 *ppv = &This->IInternetProtocol_iface;
551 }else if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
552 TRACE("(%p)->(IID_IInternetProtocol %p)\n", iface, ppv);
553 *ppv = &This->IInternetProtocol_iface;
554 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
555 FIXME("IServiceProvider is not implemented\n");
556 return E_NOINTERFACE;
559 if(!*ppv) {
560 TRACE("unknown interface %s\n", debugstr_guid(riid));
561 return E_NOINTERFACE;
564 IInternetProtocol_AddRef(iface);
565 return S_OK;
568 static ULONG WINAPI ResProtocol_AddRef(IInternetProtocol *iface)
570 ResProtocol *This = ResProtocol_from_IInternetProtocol(iface);
571 ULONG ref = InterlockedIncrement(&This->ref);
572 TRACE("(%p) ref=%d\n", iface, ref);
573 return This->pUnkOuter ? IUnknown_AddRef(This->pUnkOuter) : ref;
576 static ULONG WINAPI ResProtocol_Release(IInternetProtocol *iface)
578 ResProtocol *This = (ResProtocol*)iface;
579 IUnknown *pUnkOuter = This->pUnkOuter;
580 ULONG ref = InterlockedDecrement(&This->ref);
582 TRACE("(%p) ref=%x\n", iface, ref);
584 if(!ref) {
585 heap_free(This->data);
586 heap_free(This);
589 return pUnkOuter ? IUnknown_Release(pUnkOuter) : ref;
592 static HRESULT WINAPI ResProtocol_Start(IInternetProtocol *iface, LPCWSTR szUrl,
593 IInternetProtocolSink* pOIProtSink, IInternetBindInfo* pOIBindInfo,
594 DWORD grfPI, HANDLE_PTR dwReserved)
596 ResProtocol *This = ResProtocol_from_IInternetProtocol(iface);
597 DWORD grfBINDF = 0, len;
598 BINDINFO bindinfo;
599 LPWSTR url_dll, url_file, url, mime, res_type = (LPWSTR)RT_HTML;
600 HMODULE hdll;
601 HRSRC src;
602 HRESULT hres;
604 static const WCHAR wszRes[] = {'r','e','s',':','/','/'};
606 TRACE("(%p)->(%s %p %p %08x %lx)\n", This, debugstr_w(szUrl), pOIProtSink,
607 pOIBindInfo, grfPI, dwReserved);
609 memset(&bindinfo, 0, sizeof(bindinfo));
610 bindinfo.cbSize = sizeof(BINDINFO);
611 hres = IInternetBindInfo_GetBindInfo(pOIBindInfo, &grfBINDF, &bindinfo);
612 if(FAILED(hres))
613 return hres;
614 ReleaseBindInfo(&bindinfo);
616 len = strlenW(szUrl)+16;
617 url = heap_alloc(len*sizeof(WCHAR));
618 hres = CoInternetParseUrl(szUrl, PARSE_ENCODE, 0, url, len, &len, 0);
619 if(FAILED(hres)) {
620 WARN("CoInternetParseUrl failed: %08x\n", hres);
621 heap_free(url);
622 IInternetProtocolSink_ReportResult(pOIProtSink, hres, 0, NULL);
623 return hres;
626 if(len < sizeof(wszRes)/sizeof(wszRes[0]) || memcmp(url, wszRes, sizeof(wszRes))) {
627 WARN("Wrong protocol of url: %s\n", debugstr_w(url));
628 IInternetProtocolSink_ReportResult(pOIProtSink, E_INVALIDARG, 0, NULL);
629 heap_free(url);
630 return E_INVALIDARG;
633 url_dll = url + sizeof(wszRes)/sizeof(wszRes[0]);
634 if(!(res_type = strchrW(url_dll, '/'))) {
635 WARN("wrong url: %s\n", debugstr_w(url));
636 IInternetProtocolSink_ReportResult(pOIProtSink, MK_E_SYNTAX, 0, NULL);
637 heap_free(url);
638 return MK_E_SYNTAX;
641 *res_type++ = 0;
642 if ((url_file = strchrW(res_type, '/'))) {
643 *url_file++ = 0;
644 }else {
645 url_file = res_type;
646 res_type = MAKEINTRESOURCEW(RT_HTML);
649 hdll = LoadLibraryExW(url_dll, NULL, LOAD_LIBRARY_AS_DATAFILE);
650 if(!hdll) {
651 WARN("Could not open dll: %s\n", debugstr_w(url_dll));
652 IInternetProtocolSink_ReportResult(pOIProtSink, HRESULT_FROM_WIN32(GetLastError()), 0, NULL);
653 heap_free(url);
654 return HRESULT_FROM_WIN32(GetLastError());
657 TRACE("trying to find resource type %s, name %s\n", debugstr_w(res_type), debugstr_w(url_file));
659 src = FindResourceW(hdll, url_file, res_type);
660 if(!src) {
661 LPWSTR endpoint = NULL;
662 DWORD file_id = strtolW(url_file, &endpoint, 10);
663 if(endpoint == url_file+strlenW(url_file))
664 src = FindResourceW(hdll, MAKEINTRESOURCEW(file_id), res_type);
666 if(!src) {
667 WARN("Could not find resource\n");
668 IInternetProtocolSink_ReportResult(pOIProtSink,
669 HRESULT_FROM_WIN32(GetLastError()), 0, NULL);
670 heap_free(url);
671 return HRESULT_FROM_WIN32(GetLastError());
675 if(This->data) {
676 WARN("data already loaded\n");
677 heap_free(This->data);
680 This->data_len = SizeofResource(hdll, src);
681 This->data = heap_alloc(This->data_len);
682 memcpy(This->data, LoadResource(hdll, src), This->data_len);
683 This->cur = 0;
685 FreeLibrary(hdll);
687 hres = FindMimeFromData(NULL, url_file, This->data, This->data_len, NULL, 0, &mime, 0);
688 heap_free(url);
689 if(SUCCEEDED(hres)) {
690 IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_MIMETYPEAVAILABLE, mime);
691 CoTaskMemFree(mime);
694 IInternetProtocolSink_ReportData(pOIProtSink,
695 BSCF_FIRSTDATANOTIFICATION | BSCF_LASTDATANOTIFICATION | BSCF_DATAFULLYAVAILABLE,
696 This->data_len, This->data_len);
698 IInternetProtocolSink_ReportResult(pOIProtSink, S_OK, 0, NULL);
700 return S_OK;
703 static HRESULT WINAPI ResProtocol_Continue(IInternetProtocol *iface, PROTOCOLDATA* pProtocolData)
705 ResProtocol *This = ResProtocol_from_IInternetProtocol(iface);
706 FIXME("(%p)->(%p)\n", This, pProtocolData);
707 return E_NOTIMPL;
710 static HRESULT WINAPI ResProtocol_Abort(IInternetProtocol *iface, HRESULT hrReason,
711 DWORD dwOptions)
713 ResProtocol *This = ResProtocol_from_IInternetProtocol(iface);
714 FIXME("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);
715 return E_NOTIMPL;
718 static HRESULT WINAPI ResProtocol_Terminate(IInternetProtocol *iface, DWORD dwOptions)
720 ResProtocol *This = ResProtocol_from_IInternetProtocol(iface);
722 TRACE("(%p)->(%08x)\n", This, dwOptions);
724 /* test show that we don't have to do anything here */
725 return S_OK;
728 static HRESULT WINAPI ResProtocol_Suspend(IInternetProtocol *iface)
730 ResProtocol *This = ResProtocol_from_IInternetProtocol(iface);
731 FIXME("(%p)\n", This);
732 return E_NOTIMPL;
735 static HRESULT WINAPI ResProtocol_Resume(IInternetProtocol *iface)
737 ResProtocol *This = ResProtocol_from_IInternetProtocol(iface);
738 FIXME("(%p)\n", This);
739 return E_NOTIMPL;
742 static HRESULT WINAPI ResProtocol_Read(IInternetProtocol *iface, void* pv, ULONG cb, ULONG* pcbRead)
744 ResProtocol *This = ResProtocol_from_IInternetProtocol(iface);
746 TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
748 if(!This->data)
749 return E_FAIL;
751 *pcbRead = (cb > This->data_len-This->cur ? This->data_len-This->cur : cb);
753 if(!*pcbRead)
754 return S_FALSE;
756 memcpy(pv, This->data+This->cur, *pcbRead);
757 This->cur += *pcbRead;
759 return S_OK;
762 static HRESULT WINAPI ResProtocol_Seek(IInternetProtocol *iface, LARGE_INTEGER dlibMove,
763 DWORD dwOrigin, ULARGE_INTEGER* plibNewPosition)
765 ResProtocol *This = ResProtocol_from_IInternetProtocol(iface);
766 FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
767 return E_NOTIMPL;
770 static HRESULT WINAPI ResProtocol_LockRequest(IInternetProtocol *iface, DWORD dwOptions)
772 ResProtocol *This = ResProtocol_from_IInternetProtocol(iface);
774 TRACE("(%p)->(%d)\n", This, dwOptions);
776 /* test show that we don't have to do anything here */
777 return S_OK;
780 static HRESULT WINAPI ResProtocol_UnlockRequest(IInternetProtocol *iface)
782 ResProtocol *This = ResProtocol_from_IInternetProtocol(iface);
784 TRACE("(%p)\n", This);
786 /* test show that we don't have to do anything here */
787 return S_OK;
790 static const IInternetProtocolVtbl ResProtocolVtbl = {
791 ResProtocol_QueryInterface,
792 ResProtocol_AddRef,
793 ResProtocol_Release,
794 ResProtocol_Start,
795 ResProtocol_Continue,
796 ResProtocol_Abort,
797 ResProtocol_Terminate,
798 ResProtocol_Suspend,
799 ResProtocol_Resume,
800 ResProtocol_Read,
801 ResProtocol_Seek,
802 ResProtocol_LockRequest,
803 ResProtocol_UnlockRequest
806 static HRESULT WINAPI ResProtocolFactory_CreateInstance(IClassFactory *iface, IUnknown *pUnkOuter,
807 REFIID riid, void **ppv)
809 ResProtocol *ret;
810 HRESULT hres = S_OK;
812 TRACE("(%p)->(%p %s %p)\n", iface, pUnkOuter, debugstr_guid(riid), ppv);
814 ret = heap_alloc(sizeof(ResProtocol));
815 ret->IInternetProtocol_iface.lpVtbl = &ResProtocolVtbl;
816 ret->ref = 0;
817 ret->data = NULL;
818 ret->data_len = 0;
819 ret->cur = 0;
820 ret->pUnkOuter = pUnkOuter;
822 if(pUnkOuter) {
823 ret->ref = 1;
824 if(IsEqualGUID(&IID_IUnknown, riid))
825 *ppv = &ret->IInternetProtocol_iface;
826 else
827 hres = E_FAIL;
828 }else {
829 hres = IInternetProtocol_QueryInterface(&ret->IInternetProtocol_iface, riid, ppv);
832 if(FAILED(hres))
833 heap_free(ret);
835 return hres;
838 static HRESULT WINAPI ResProtocolInfo_ParseUrl(IInternetProtocolInfo *iface, LPCWSTR pwzUrl,
839 PARSEACTION ParseAction, DWORD dwParseFlags, LPWSTR pwzResult, DWORD cchResult,
840 DWORD* pcchResult, DWORD dwReserved)
842 TRACE("%p)->(%s %d %x %p %d %p %d)\n", iface, debugstr_w(pwzUrl), ParseAction,
843 dwParseFlags, pwzResult, cchResult, pcchResult, dwReserved);
845 if(ParseAction == PARSE_SECURITY_URL) {
846 WCHAR file_part[MAX_PATH], full_path[MAX_PATH];
847 WCHAR *ptr;
848 DWORD size, len;
850 static const WCHAR wszFile[] = {'f','i','l','e',':','/','/'};
851 static const WCHAR wszRes[] = {'r','e','s',':','/','/'};
853 if(strlenW(pwzUrl) <= sizeof(wszRes)/sizeof(WCHAR) || memcmp(pwzUrl, wszRes, sizeof(wszRes)))
854 return E_INVALIDARG;
856 ptr = strchrW(pwzUrl + sizeof(wszRes)/sizeof(WCHAR), '/');
857 if(!ptr)
858 return E_INVALIDARG;
860 len = ptr - (pwzUrl + sizeof(wszRes)/sizeof(WCHAR));
861 if(len >= sizeof(file_part)/sizeof(WCHAR)) {
862 FIXME("Too long URL\n");
863 return MK_E_SYNTAX;
866 memcpy(file_part, pwzUrl + sizeof(wszRes)/sizeof(WCHAR), len*sizeof(WCHAR));
867 file_part[len] = 0;
869 len = SearchPathW(NULL, file_part, NULL, sizeof(full_path)/sizeof(WCHAR), full_path, NULL);
870 if(!len) {
871 HMODULE module;
873 /* SearchPath does not work well with winelib files (like our test executable),
874 * so we also try to load the library here */
875 module = LoadLibraryExW(file_part, NULL, LOAD_LIBRARY_AS_DATAFILE);
876 if(!module) {
877 WARN("Could not find file %s\n", debugstr_w(file_part));
878 return MK_E_SYNTAX;
881 len = GetModuleFileNameW(module, full_path, sizeof(full_path)/sizeof(WCHAR));
882 FreeLibrary(module);
883 if(!len)
884 return E_FAIL;
887 size = sizeof(wszFile)/sizeof(WCHAR) + len + 1;
888 if(pcchResult)
889 *pcchResult = size;
890 if(size > cchResult)
891 return S_FALSE;
893 memcpy(pwzResult, wszFile, sizeof(wszFile));
894 memcpy(pwzResult + sizeof(wszFile)/sizeof(WCHAR), full_path, (len+1)*sizeof(WCHAR));
895 return S_OK;
898 if(ParseAction == PARSE_DOMAIN) {
899 if(!pcchResult)
900 return E_POINTER;
902 if(pwzUrl)
903 *pcchResult = strlenW(pwzUrl)+1;
904 else
905 *pcchResult = 1;
906 return E_FAIL;
909 return INET_E_DEFAULT_ACTION;
912 static HRESULT WINAPI ResProtocolInfo_QueryInfo(IInternetProtocolInfo *iface, LPCWSTR pwzUrl,
913 QUERYOPTION QueryOption, DWORD dwQueryFlags, LPVOID pBuffer, DWORD cbBuffer, DWORD* pcbBuf,
914 DWORD dwReserved)
916 TRACE("%p)->(%s %08x %08x %p %d %p %d)\n", iface, debugstr_w(pwzUrl), QueryOption, dwQueryFlags, pBuffer,
917 cbBuffer, pcbBuf, dwReserved);
919 switch(QueryOption) {
920 case QUERY_USES_NETWORK:
921 if(!pBuffer || cbBuffer < sizeof(DWORD))
922 return E_FAIL;
924 *(DWORD*)pBuffer = 0;
925 if(pcbBuf)
926 *pcbBuf = sizeof(DWORD);
927 break;
929 case QUERY_IS_SECURE:
930 FIXME("QUERY_IS_SECURE not supported\n");
931 return E_NOTIMPL;
932 case QUERY_IS_SAFE:
933 FIXME("QUERY_IS_SAFE not supported\n");
934 return E_NOTIMPL;
935 default:
936 return INET_E_USE_DEFAULT_PROTOCOLHANDLER;
939 return S_OK;
942 static const IInternetProtocolInfoVtbl ResProtocolInfoVtbl = {
943 InternetProtocolInfo_QueryInterface,
944 InternetProtocolInfo_AddRef,
945 InternetProtocolInfo_Release,
946 ResProtocolInfo_ParseUrl,
947 InternetProtocolInfo_CombineUrl,
948 InternetProtocolInfo_CompareUrl,
949 ResProtocolInfo_QueryInfo
952 static const IClassFactoryVtbl ResProtocolFactoryVtbl = {
953 ClassFactory_QueryInterface,
954 ClassFactory_AddRef,
955 ClassFactory_Release,
956 ResProtocolFactory_CreateInstance,
957 ClassFactory_LockServer
960 static ProtocolFactory ResProtocolFactory = {
961 { &ResProtocolInfoVtbl },
962 { &ResProtocolFactoryVtbl }
965 /********************************************************************
966 * JSProtocol implementation
969 static HRESULT WINAPI JSProtocolFactory_CreateInstance(IClassFactory *iface, IUnknown *pUnkOuter,
970 REFIID riid, void **ppv)
972 FIXME("(%p)->(%p %s %p)\n", iface, pUnkOuter, debugstr_guid(riid), ppv);
973 return E_NOTIMPL;
976 static HRESULT WINAPI JSProtocolInfo_ParseUrl(IInternetProtocolInfo *iface, LPCWSTR pwzUrl,
977 PARSEACTION ParseAction, DWORD dwParseFlags, LPWSTR pwzResult, DWORD cchResult,
978 DWORD* pcchResult, DWORD dwReserved)
980 TRACE("%p)->(%s %d %x %p %d %p %d)\n", iface, debugstr_w(pwzUrl), ParseAction,
981 dwParseFlags, pwzResult, cchResult, pcchResult, dwReserved);
983 switch(ParseAction) {
984 case PARSE_SECURITY_URL:
985 FIXME("PARSE_SECURITY_URL\n");
986 return E_NOTIMPL;
987 case PARSE_DOMAIN:
988 FIXME("PARSE_DOMAIN\n");
989 return E_NOTIMPL;
990 default:
991 return INET_E_DEFAULT_ACTION;
994 return S_OK;
997 static HRESULT WINAPI JSProtocolInfo_QueryInfo(IInternetProtocolInfo *iface, LPCWSTR pwzUrl,
998 QUERYOPTION QueryOption, DWORD dwQueryFlags, LPVOID pBuffer, DWORD cbBuffer, DWORD* pcbBuf,
999 DWORD dwReserved)
1001 TRACE("%p)->(%s %08x %08x %p %d %p %d)\n", iface, debugstr_w(pwzUrl), QueryOption, dwQueryFlags, pBuffer,
1002 cbBuffer, pcbBuf, dwReserved);
1004 switch(QueryOption) {
1005 case QUERY_USES_NETWORK:
1006 if(!pBuffer || cbBuffer < sizeof(DWORD))
1007 return E_FAIL;
1009 *(DWORD*)pBuffer = 0;
1010 if(pcbBuf)
1011 *pcbBuf = sizeof(DWORD);
1012 break;
1014 case QUERY_IS_SECURE:
1015 FIXME("QUERY_IS_SECURE not supported\n");
1016 return E_NOTIMPL;
1018 default:
1019 return INET_E_USE_DEFAULT_PROTOCOLHANDLER;
1022 return S_OK;
1025 static const IInternetProtocolInfoVtbl JSProtocolInfoVtbl = {
1026 InternetProtocolInfo_QueryInterface,
1027 InternetProtocolInfo_AddRef,
1028 InternetProtocolInfo_Release,
1029 JSProtocolInfo_ParseUrl,
1030 InternetProtocolInfo_CombineUrl,
1031 InternetProtocolInfo_CompareUrl,
1032 JSProtocolInfo_QueryInfo
1035 static const IClassFactoryVtbl JSProtocolFactoryVtbl = {
1036 ClassFactory_QueryInterface,
1037 ClassFactory_AddRef,
1038 ClassFactory_Release,
1039 JSProtocolFactory_CreateInstance,
1040 ClassFactory_LockServer
1043 static ProtocolFactory JSProtocolFactory = {
1044 { &JSProtocolInfoVtbl },
1045 { &JSProtocolFactoryVtbl }
1048 HRESULT ProtocolFactory_Create(REFCLSID rclsid, REFIID riid, void **ppv)
1050 ProtocolFactory *cf = NULL;
1052 if(IsEqualGUID(&CLSID_AboutProtocol, rclsid))
1053 cf = &AboutProtocolFactory;
1054 else if(IsEqualGUID(&CLSID_ResProtocol, rclsid))
1055 cf = &ResProtocolFactory;
1056 else if(IsEqualGUID(&CLSID_JSProtocol, rclsid))
1057 cf = &JSProtocolFactory;
1059 if(!cf) {
1060 FIXME("not implemented protocol %s\n", debugstr_guid(rclsid));
1061 return CLASS_E_CLASSNOTAVAILABLE;
1064 return IInternetProtocolInfo_QueryInterface(&cf->IInternetProtocolInfo_iface, riid, ppv);