urlmon: Add new IE7 QUERYOPTION.
[wine.git] / dlls / mshtml / protocol.c
blob0b502ba9cfa0224b276094490ba7ac0811855bc0
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"
32 #include "wine/unicode.h"
34 #include "mshtml_private.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
38 /********************************************************************
39 * common ProtocolFactory implementation
42 #define PROTOCOLINFO(x) ((IInternetProtocolInfo*) &(x)->lpInternetProtocolInfoVtbl)
43 #define CLASSFACTORY(x) ((IClassFactory*) &(x)->lpClassFactoryVtbl)
44 #define PROTOCOL(x) ((IInternetProtocol*) &(x)->lpInternetProtocolVtbl)
46 typedef struct {
47 const IInternetProtocolInfoVtbl *lpInternetProtocolInfoVtbl;
48 const IClassFactoryVtbl *lpClassFactoryVtbl;
49 } ProtocolFactory;
51 #define PROTOCOLINFO_THIS(iface) DEFINE_THIS(ProtocolFactory, InternetProtocolInfo, iface)
53 static HRESULT WINAPI InternetProtocolInfo_QueryInterface(IInternetProtocolInfo *iface, REFIID riid, void **ppv)
55 ProtocolFactory *This = PROTOCOLINFO_THIS(iface);
57 *ppv = NULL;
58 if(IsEqualGUID(&IID_IUnknown, riid)) {
59 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
60 *ppv = PROTOCOLINFO(This);
61 }else if(IsEqualGUID(&IID_IInternetProtocolInfo, riid)) {
62 TRACE("(%p)->(IID_IInternetProtocolInfo %p)\n", This, ppv);
63 *ppv = PROTOCOLINFO(This);
64 }else if(IsEqualGUID(&IID_IClassFactory, riid)) {
65 TRACE("(%p)->(IID_IClassFactory %p)\n", This, ppv);
66 *ppv = CLASSFACTORY(This);
69 if(!*ppv) {
70 WARN("unknown interface %s\n", debugstr_guid(riid));
71 return E_NOINTERFACE;
74 IInternetProtocolInfo_AddRef(iface);
75 return S_OK;
78 static ULONG WINAPI InternetProtocolInfo_AddRef(IInternetProtocolInfo *iface)
80 ProtocolFactory *This = PROTOCOLINFO_THIS(iface);
81 TRACE("(%p)\n", This);
82 LOCK_MODULE();
83 return 2;
86 static ULONG WINAPI InternetProtocolInfo_Release(IInternetProtocolInfo *iface)
88 ProtocolFactory *This = PROTOCOLINFO_THIS(iface);
89 TRACE("(%p)\n", This);
90 UNLOCK_MODULE();
91 return 1;
94 static HRESULT WINAPI InternetProtocolInfo_CombineUrl(IInternetProtocolInfo *iface,
95 LPCWSTR pwzBaseUrl, LPCWSTR pwzRelativeUrl, DWORD dwCombineFlags, LPWSTR pwzResult,
96 DWORD cchResult, DWORD* pcchResult, DWORD dwReserved)
98 TRACE("%p)->(%s %s %08x %p %d %p %d)\n", iface, debugstr_w(pwzBaseUrl),
99 debugstr_w(pwzRelativeUrl), dwCombineFlags, pwzResult, cchResult,
100 pcchResult, dwReserved);
102 return INET_E_USE_DEFAULT_PROTOCOLHANDLER;
105 static HRESULT WINAPI InternetProtocolInfo_CompareUrl(IInternetProtocolInfo *iface, LPCWSTR pwzUrl1,
106 LPCWSTR pwzUrl2, DWORD dwCompareFlags)
108 TRACE("%p)->(%s %s %08x)\n", iface, debugstr_w(pwzUrl1), debugstr_w(pwzUrl2), dwCompareFlags);
109 return E_NOTIMPL;
112 #undef PROTOCOLINFO_THIS
114 #define CLASSFACTORY_THIS(iface) DEFINE_THIS(ProtocolFactory, ClassFactory, iface)
116 static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFIID riid, void **ppv)
118 ProtocolFactory *This = CLASSFACTORY_THIS(iface);
119 return IInternetProtocolInfo_QueryInterface(PROTOCOLINFO(This), riid, ppv);
122 static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface)
124 ProtocolFactory *This = CLASSFACTORY_THIS(iface);
125 return IInternetProtocolInfo_AddRef(PROTOCOLINFO(This));
128 static ULONG WINAPI ClassFactory_Release(IClassFactory *iface)
130 ProtocolFactory *This = CLASSFACTORY_THIS(iface);
131 return IInternetProtocolInfo_Release(PROTOCOLINFO(This));
134 static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL dolock)
136 ProtocolFactory *This = CLASSFACTORY_THIS(iface);
138 TRACE("(%p)->(%x)\n", This, dolock);
140 if(dolock)
141 LOCK_MODULE();
142 else
143 UNLOCK_MODULE();
145 return S_OK;
148 #undef CLASSFACTORY_THIS
150 /********************************************************************
151 * AboutProtocol implementation
154 typedef struct {
155 const IInternetProtocolVtbl *lpInternetProtocolVtbl;
157 LONG ref;
159 BYTE *data;
160 ULONG data_len;
161 ULONG cur;
163 IUnknown *pUnkOuter;
164 } AboutProtocol;
166 #define PROTOCOL_THIS(iface) DEFINE_THIS(AboutProtocol, InternetProtocol, iface)
168 static HRESULT WINAPI AboutProtocol_QueryInterface(IInternetProtocol *iface, REFIID riid, void **ppv)
170 AboutProtocol *This = PROTOCOL_THIS(iface);
172 *ppv = NULL;
174 if(IsEqualGUID(&IID_IUnknown, riid)) {
175 TRACE("(%p)->(IID_IUnknown %p)\n", iface, ppv);
176 if(This->pUnkOuter)
177 return IUnknown_QueryInterface(This->pUnkOuter, riid, ppv);
178 *ppv = PROTOCOL(This);
179 }else if(IsEqualGUID(&IID_IInternetProtocolRoot, riid)) {
180 TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", iface, ppv);
181 *ppv = PROTOCOL(This);
182 }else if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
183 TRACE("(%p)->(IID_IInternetProtocol %p)\n", iface, ppv);
184 *ppv = PROTOCOL(This);
185 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
186 FIXME("IServiceProvider is not implemented\n");
187 return E_NOINTERFACE;
190 if(!*ppv) {
191 TRACE("unknown interface %s\n", debugstr_guid(riid));
192 return E_NOINTERFACE;
195 IInternetProtocol_AddRef(iface);
196 return S_OK;
199 static ULONG WINAPI AboutProtocol_AddRef(IInternetProtocol *iface)
201 AboutProtocol *This = PROTOCOL_THIS(iface);
202 ULONG ref = InterlockedIncrement(&This->ref);
203 TRACE("(%p) ref=%d\n", iface, ref);
204 return This->pUnkOuter ? IUnknown_AddRef(This->pUnkOuter) : ref;
207 static ULONG WINAPI AboutProtocol_Release(IInternetProtocol *iface)
209 AboutProtocol *This = PROTOCOL_THIS(iface);
210 IUnknown *pUnkOuter = This->pUnkOuter;
211 ULONG ref = InterlockedDecrement(&This->ref);
213 TRACE("(%p) ref=%x\n", iface, ref);
215 if(!ref) {
216 heap_free(This->data);
217 heap_free(This);
218 UNLOCK_MODULE();
221 return pUnkOuter ? IUnknown_Release(pUnkOuter) : ref;
224 static HRESULT WINAPI AboutProtocol_Start(IInternetProtocol *iface, LPCWSTR szUrl,
225 IInternetProtocolSink* pOIProtSink, IInternetBindInfo* pOIBindInfo,
226 DWORD grfPI, DWORD dwReserved)
228 AboutProtocol *This = PROTOCOL_THIS(iface);
229 BINDINFO bindinfo;
230 DWORD grfBINDF = 0;
231 LPCWSTR text = NULL;
233 static const WCHAR html_begin[] = {0xfeff,'<','H','T','M','L','>',0};
234 static const WCHAR html_end[] = {'<','/','H','T','M','L','>',0};
235 static const WCHAR wszBlank[] = {'b','l','a','n','k',0};
236 static const WCHAR wszAbout[] = {'a','b','o','u','t',':'};
237 static const WCHAR wszTextHtml[] = {'t','e','x','t','/','h','t','m','l',0};
239 /* NOTE:
240 * the about protocol seems not to work as I would expect. It creates html document
241 * for a given url, eg. about:some_text -> <HTML>some_text</HTML> except for the case when
242 * some_text = "blank", when document is blank (<HTML></HMTL>). The same happens
243 * when the url does not have "about:" in the beginning.
246 TRACE("(%p)->(%s %p %p %08x %d)\n", This, debugstr_w(szUrl), pOIProtSink,
247 pOIBindInfo, grfPI, dwReserved);
249 memset(&bindinfo, 0, sizeof(bindinfo));
250 bindinfo.cbSize = sizeof(BINDINFO);
251 IInternetBindInfo_GetBindInfo(pOIBindInfo, &grfBINDF, &bindinfo);
252 ReleaseBindInfo(&bindinfo);
254 TRACE("bindf %x\n", grfBINDF);
256 if(strlenW(szUrl)>=sizeof(wszAbout)/sizeof(WCHAR) && !memcmp(wszAbout, szUrl, sizeof(wszAbout))) {
257 text = szUrl + sizeof(wszAbout)/sizeof(WCHAR);
258 if(!strcmpW(wszBlank, text))
259 text = NULL;
262 This->data_len = sizeof(html_begin)+sizeof(html_end)-sizeof(WCHAR)
263 + (text ? strlenW(text)*sizeof(WCHAR) : 0);
264 This->data = heap_alloc(This->data_len);
266 memcpy(This->data, html_begin, sizeof(html_begin));
267 if(text)
268 strcatW((LPWSTR)This->data, text);
269 strcatW((LPWSTR)This->data, html_end);
271 This->cur = 0;
273 IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_MIMETYPEAVAILABLE, wszTextHtml);
275 IInternetProtocolSink_ReportData(pOIProtSink,
276 BSCF_FIRSTDATANOTIFICATION | BSCF_LASTDATANOTIFICATION | BSCF_DATAFULLYAVAILABLE,
277 This->data_len, This->data_len);
279 IInternetProtocolSink_ReportResult(pOIProtSink, S_OK, 0, NULL);
281 return S_OK;
284 static HRESULT WINAPI AboutProtocol_Continue(IInternetProtocol *iface, PROTOCOLDATA* pProtocolData)
286 AboutProtocol *This = PROTOCOL_THIS(iface);
287 FIXME("(%p)->(%p)\n", This, pProtocolData);
288 return E_NOTIMPL;
291 static HRESULT WINAPI AboutProtocol_Abort(IInternetProtocol *iface, HRESULT hrReason,
292 DWORD dwOptions)
294 AboutProtocol *This = PROTOCOL_THIS(iface);
295 FIXME("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);
296 return E_NOTIMPL;
299 static HRESULT WINAPI AboutProtocol_Terminate(IInternetProtocol *iface, DWORD dwOptions)
301 AboutProtocol *This = PROTOCOL_THIS(iface);
302 TRACE("(%p)->(%08x)\n", This, dwOptions);
303 return S_OK;
306 static HRESULT WINAPI AboutProtocol_Suspend(IInternetProtocol *iface)
308 AboutProtocol *This = PROTOCOL_THIS(iface);
309 FIXME("(%p)\n", This);
310 return E_NOTIMPL;
313 static HRESULT WINAPI AboutProtocol_Resume(IInternetProtocol *iface)
315 AboutProtocol *This = PROTOCOL_THIS(iface);
316 FIXME("(%p)\n", This);
317 return E_NOTIMPL;
320 static HRESULT WINAPI AboutProtocol_Read(IInternetProtocol *iface, void* pv, ULONG cb, ULONG* pcbRead)
322 AboutProtocol *This = PROTOCOL_THIS(iface);
324 TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
326 if(!This->data)
327 return E_FAIL;
329 *pcbRead = (cb > This->data_len-This->cur ? This->data_len-This->cur : cb);
331 if(!*pcbRead)
332 return S_FALSE;
334 memcpy(pv, This->data+This->cur, *pcbRead);
335 This->cur += *pcbRead;
337 return S_OK;
340 static HRESULT WINAPI AboutProtocol_Seek(IInternetProtocol *iface, LARGE_INTEGER dlibMove,
341 DWORD dwOrigin, ULARGE_INTEGER* plibNewPosition)
343 AboutProtocol *This = PROTOCOL_THIS(iface);
344 FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
345 return E_NOTIMPL;
348 static HRESULT WINAPI AboutProtocol_LockRequest(IInternetProtocol *iface, DWORD dwOptions)
350 AboutProtocol *This = PROTOCOL_THIS(iface);
352 TRACE("(%p)->(%d)\n", This, dwOptions);
354 return S_OK;
357 static HRESULT WINAPI AboutProtocol_UnlockRequest(IInternetProtocol *iface)
359 AboutProtocol *This = PROTOCOL_THIS(iface);
361 TRACE("(%p)\n", This);
363 return S_OK;
366 #undef PROTOCOL_THIS
368 static const IInternetProtocolVtbl AboutProtocolVtbl = {
369 AboutProtocol_QueryInterface,
370 AboutProtocol_AddRef,
371 AboutProtocol_Release,
372 AboutProtocol_Start,
373 AboutProtocol_Continue,
374 AboutProtocol_Abort,
375 AboutProtocol_Terminate,
376 AboutProtocol_Suspend,
377 AboutProtocol_Resume,
378 AboutProtocol_Read,
379 AboutProtocol_Seek,
380 AboutProtocol_LockRequest,
381 AboutProtocol_UnlockRequest
384 static HRESULT WINAPI AboutProtocolFactory_CreateInstance(IClassFactory *iface, IUnknown *pUnkOuter,
385 REFIID riid, void **ppv)
387 AboutProtocol *ret;
388 HRESULT hres = S_OK;
390 TRACE("(%p)->(%p %s %p)\n", iface, pUnkOuter, debugstr_guid(riid), ppv);
392 ret = heap_alloc(sizeof(AboutProtocol));
393 ret->lpInternetProtocolVtbl = &AboutProtocolVtbl;
394 ret->ref = 0;
396 ret->data = NULL;
397 ret->data_len = 0;
398 ret->cur = 0;
399 ret->pUnkOuter = pUnkOuter;
401 if(pUnkOuter) {
402 ret->ref = 1;
403 if(IsEqualGUID(&IID_IUnknown, riid))
404 *ppv = PROTOCOL(ret);
405 else
406 hres = E_INVALIDARG;
407 }else {
408 hres = IInternetProtocol_QueryInterface(PROTOCOL(ret), riid, ppv);
411 if(SUCCEEDED(hres))
412 LOCK_MODULE();
413 else
414 heap_free(ret);
416 return hres;
419 static HRESULT WINAPI AboutProtocolInfo_ParseUrl(IInternetProtocolInfo *iface, LPCWSTR pwzUrl,
420 PARSEACTION ParseAction, DWORD dwParseFlags, LPWSTR pwzResult, DWORD cchResult,
421 DWORD* pcchResult, DWORD dwReserved)
423 TRACE("%p)->(%s %08x %08x %p %d %p %d)\n", iface, debugstr_w(pwzUrl), ParseAction,
424 dwParseFlags, pwzResult, cchResult, pcchResult, dwReserved);
426 if(ParseAction == PARSE_SECURITY_URL) {
427 int len = lstrlenW(pwzUrl);
429 if(len >= cchResult)
430 return S_FALSE;
432 memcpy(pwzResult, pwzUrl, (len+1)*sizeof(WCHAR));
433 return S_OK;
436 if(ParseAction == PARSE_DOMAIN) {
437 if(!pcchResult)
438 return E_POINTER;
440 if(pwzUrl)
441 *pcchResult = strlenW(pwzUrl)+1;
442 else
443 *pcchResult = 1;
444 return E_FAIL;
447 return INET_E_DEFAULT_ACTION;
450 static HRESULT WINAPI AboutProtocolInfo_QueryInfo(IInternetProtocolInfo *iface, LPCWSTR pwzUrl,
451 QUERYOPTION QueryOption, DWORD dwQueryFlags, LPVOID pBuffer, DWORD cbBuffer, DWORD* pcbBuf,
452 DWORD dwReserved)
454 TRACE("%p)->(%s %08x %08x %p %d %p %d)\n", iface, debugstr_w(pwzUrl), QueryOption, dwQueryFlags, pBuffer,
455 cbBuffer, pcbBuf, dwReserved);
457 switch(QueryOption) {
458 case QUERY_CAN_NAVIGATE:
459 return INET_E_USE_DEFAULT_PROTOCOLHANDLER;
461 case QUERY_USES_NETWORK:
462 if(!pBuffer || cbBuffer < sizeof(DWORD))
463 return E_FAIL;
465 *(DWORD*)pBuffer = 0;
466 if(pcbBuf)
467 *pcbBuf = sizeof(DWORD);
469 break;
471 case QUERY_IS_CACHED:
472 FIXME("Unsupported option QUERY_IS_CACHED\n");
473 return E_NOTIMPL;
474 case QUERY_IS_INSTALLEDENTRY:
475 FIXME("Unsupported option QUERY_IS_INSTALLEDENTRY\n");
476 return E_NOTIMPL;
477 case QUERY_IS_CACHED_OR_MAPPED:
478 FIXME("Unsupported option QUERY_IS_CACHED_OR_MAPPED\n");
479 return E_NOTIMPL;
480 case QUERY_IS_SECURE:
481 FIXME("Unsupported option QUERY_IS_SECURE\n");
482 return E_NOTIMPL;
483 case QUERY_IS_SAFE:
484 FIXME("Unsupported option QUERY_IS_SAFE\n");
485 return E_NOTIMPL;
486 default:
487 return E_FAIL;
490 return S_OK;
493 static const IInternetProtocolInfoVtbl AboutProtocolInfoVtbl = {
494 InternetProtocolInfo_QueryInterface,
495 InternetProtocolInfo_AddRef,
496 InternetProtocolInfo_Release,
497 AboutProtocolInfo_ParseUrl,
498 InternetProtocolInfo_CombineUrl,
499 InternetProtocolInfo_CompareUrl,
500 AboutProtocolInfo_QueryInfo
503 static const IClassFactoryVtbl AboutProtocolFactoryVtbl = {
504 ClassFactory_QueryInterface,
505 ClassFactory_AddRef,
506 ClassFactory_Release,
507 AboutProtocolFactory_CreateInstance,
508 ClassFactory_LockServer
511 static ProtocolFactory AboutProtocolFactory = {
512 &AboutProtocolInfoVtbl,
513 &AboutProtocolFactoryVtbl
516 /********************************************************************
517 * ResProtocol implementation
520 typedef struct {
521 const IInternetProtocolVtbl *lpInternetProtocolVtbl;
522 LONG ref;
524 BYTE *data;
525 ULONG data_len;
526 ULONG cur;
528 IUnknown *pUnkOuter;
529 } ResProtocol;
531 #define PROTOCOL_THIS(iface) DEFINE_THIS(ResProtocol, InternetProtocol, iface)
533 static HRESULT WINAPI ResProtocol_QueryInterface(IInternetProtocol *iface, REFIID riid, void **ppv)
535 ResProtocol *This = PROTOCOL_THIS(iface);
537 *ppv = NULL;
539 if(IsEqualGUID(&IID_IUnknown, riid)) {
540 TRACE("(%p)->(IID_IUnknown %p)\n", iface, ppv);
541 if(This->pUnkOuter)
542 return IUnknown_QueryInterface(This->pUnkOuter, &IID_IUnknown, ppv);
543 *ppv = PROTOCOL(This);
544 }else if(IsEqualGUID(&IID_IInternetProtocolRoot, riid)) {
545 TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", iface, ppv);
546 *ppv = PROTOCOL(This);
547 }else if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
548 TRACE("(%p)->(IID_IInternetProtocol %p)\n", iface, ppv);
549 *ppv = PROTOCOL(This);
550 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
551 FIXME("IServiceProvider is not implemented\n");
552 return E_NOINTERFACE;
555 if(!*ppv) {
556 TRACE("unknown interface %s\n", debugstr_guid(riid));
557 return E_NOINTERFACE;
560 IInternetProtocol_AddRef(iface);
561 return S_OK;
564 static ULONG WINAPI ResProtocol_AddRef(IInternetProtocol *iface)
566 ResProtocol *This = PROTOCOL_THIS(iface);
567 ULONG ref = InterlockedIncrement(&This->ref);
568 TRACE("(%p) ref=%d\n", iface, ref);
569 return This->pUnkOuter ? IUnknown_AddRef(This->pUnkOuter) : ref;
572 static ULONG WINAPI ResProtocol_Release(IInternetProtocol *iface)
574 ResProtocol *This = (ResProtocol*)iface;
575 IUnknown *pUnkOuter = This->pUnkOuter;
576 ULONG ref = InterlockedDecrement(&This->ref);
578 TRACE("(%p) ref=%x\n", iface, ref);
580 if(!ref) {
581 heap_free(This->data);
582 heap_free(This);
583 UNLOCK_MODULE();
586 return pUnkOuter ? IUnknown_Release(pUnkOuter) : ref;
589 static HRESULT WINAPI ResProtocol_Start(IInternetProtocol *iface, LPCWSTR szUrl,
590 IInternetProtocolSink* pOIProtSink, IInternetBindInfo* pOIBindInfo,
591 DWORD grfPI, DWORD dwReserved)
593 ResProtocol *This = PROTOCOL_THIS(iface);
594 DWORD grfBINDF = 0, len;
595 BINDINFO bindinfo;
596 LPWSTR url_dll, url_file, url, mime, res_type = (LPWSTR)RT_HTML;
597 HMODULE hdll;
598 HRSRC src;
599 HRESULT hres;
601 static const WCHAR wszRes[] = {'r','e','s',':','/','/'};
603 TRACE("(%p)->(%s %p %p %08x %d)\n", This, debugstr_w(szUrl), pOIProtSink,
604 pOIBindInfo, grfPI, dwReserved);
606 memset(&bindinfo, 0, sizeof(bindinfo));
607 bindinfo.cbSize = sizeof(BINDINFO);
608 IInternetBindInfo_GetBindInfo(pOIBindInfo, &grfBINDF, &bindinfo);
609 ReleaseBindInfo(&bindinfo);
611 len = strlenW(szUrl)+16;
612 url = heap_alloc(len*sizeof(WCHAR));
613 hres = CoInternetParseUrl(szUrl, PARSE_ENCODE, 0, url, len, &len, 0);
614 if(FAILED(hres)) {
615 WARN("CoInternetParseUrl failed: %08x\n", hres);
616 heap_free(url);
617 IInternetProtocolSink_ReportResult(pOIProtSink, hres, 0, NULL);
618 return hres;
621 if(len < sizeof(wszRes)/sizeof(wszRes[0]) || memcmp(url, wszRes, sizeof(wszRes))) {
622 WARN("Wrong protocol of url: %s\n", debugstr_w(url));
623 IInternetProtocolSink_ReportResult(pOIProtSink, E_INVALIDARG, 0, NULL);
624 heap_free(url);
625 return E_INVALIDARG;
628 url_dll = url + sizeof(wszRes)/sizeof(wszRes[0]);
629 if(!(url_file = strrchrW(url_dll, '/'))) {
630 WARN("wrong url: %s\n", debugstr_w(url));
631 IInternetProtocolSink_ReportResult(pOIProtSink, MK_E_SYNTAX, 0, NULL);
632 heap_free(url);
633 return MK_E_SYNTAX;
636 *url_file++ = 0;
637 hdll = LoadLibraryExW(url_dll, NULL, LOAD_LIBRARY_AS_DATAFILE);
638 if(!hdll) {
639 if (!(res_type = strrchrW(url_dll, '/'))) {
640 WARN("Could not open dll: %s\n", debugstr_w(url_dll));
641 IInternetProtocolSink_ReportResult(pOIProtSink, HRESULT_FROM_WIN32(GetLastError()), 0, NULL);
642 heap_free(url);
643 return HRESULT_FROM_WIN32(GetLastError());
645 *res_type++ = 0;
647 hdll = LoadLibraryExW(url_dll, NULL, LOAD_LIBRARY_AS_DATAFILE);
648 if(!hdll) {
649 WARN("Could not open dll: %s\n", debugstr_w(url_dll));
650 IInternetProtocolSink_ReportResult(pOIProtSink, HRESULT_FROM_WIN32(GetLastError()), 0, NULL);
651 heap_free(url);
652 return HRESULT_FROM_WIN32(GetLastError());
656 TRACE("trying to find resource type %s, name %s\n", debugstr_w(res_type), debugstr_w(url_file));
658 src = FindResourceW(hdll, url_file, res_type);
659 if(!src) {
660 LPWSTR endpoint = NULL;
661 DWORD file_id = strtolW(url_file, &endpoint, 10);
662 if(endpoint == url_file+strlenW(url_file))
663 src = FindResourceW(hdll, (LPCWSTR)file_id, (LPCWSTR)RT_HTML);
665 if(!src) {
666 WARN("Could not find resource\n");
667 IInternetProtocolSink_ReportResult(pOIProtSink,
668 HRESULT_FROM_WIN32(GetLastError()), 0, NULL);
669 heap_free(url);
670 return HRESULT_FROM_WIN32(GetLastError());
674 if(This->data) {
675 WARN("data already loaded\n");
676 heap_free(This->data);
679 This->data_len = SizeofResource(hdll, src);
680 This->data = heap_alloc(This->data_len);
681 memcpy(This->data, LoadResource(hdll, src), This->data_len);
682 This->cur = 0;
684 FreeLibrary(hdll);
686 hres = FindMimeFromData(NULL, url_file, NULL, 0, NULL, 0, &mime, 0);
687 heap_free(url);
688 if(SUCCEEDED(hres)) {
689 IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_MIMETYPEAVAILABLE, mime);
690 CoTaskMemFree(mime);
693 IInternetProtocolSink_ReportData(pOIProtSink,
694 BSCF_FIRSTDATANOTIFICATION | BSCF_LASTDATANOTIFICATION | BSCF_DATAFULLYAVAILABLE,
695 This->data_len, This->data_len);
697 IInternetProtocolSink_ReportResult(pOIProtSink, S_OK, 0, NULL);
699 return S_OK;
702 static HRESULT WINAPI ResProtocol_Continue(IInternetProtocol *iface, PROTOCOLDATA* pProtocolData)
704 ResProtocol *This = PROTOCOL_THIS(iface);
705 FIXME("(%p)->(%p)\n", This, pProtocolData);
706 return E_NOTIMPL;
709 static HRESULT WINAPI ResProtocol_Abort(IInternetProtocol *iface, HRESULT hrReason,
710 DWORD dwOptions)
712 ResProtocol *This = PROTOCOL_THIS(iface);
713 FIXME("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);
714 return E_NOTIMPL;
717 static HRESULT WINAPI ResProtocol_Terminate(IInternetProtocol *iface, DWORD dwOptions)
719 ResProtocol *This = PROTOCOL_THIS(iface);
721 TRACE("(%p)->(%08x)\n", This, dwOptions);
723 /* test show that we don't have to do anything here */
724 return S_OK;
727 static HRESULT WINAPI ResProtocol_Suspend(IInternetProtocol *iface)
729 ResProtocol *This = PROTOCOL_THIS(iface);
730 FIXME("(%p)\n", This);
731 return E_NOTIMPL;
734 static HRESULT WINAPI ResProtocol_Resume(IInternetProtocol *iface)
736 ResProtocol *This = PROTOCOL_THIS(iface);
737 FIXME("(%p)\n", This);
738 return E_NOTIMPL;
741 static HRESULT WINAPI ResProtocol_Read(IInternetProtocol *iface, void* pv, ULONG cb, ULONG* pcbRead)
743 ResProtocol *This = PROTOCOL_THIS(iface);
745 TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
747 if(!This->data)
748 return E_FAIL;
750 *pcbRead = (cb > This->data_len-This->cur ? This->data_len-This->cur : cb);
752 if(!*pcbRead)
753 return S_FALSE;
755 memcpy(pv, This->data+This->cur, *pcbRead);
756 This->cur += *pcbRead;
758 return S_OK;
761 static HRESULT WINAPI ResProtocol_Seek(IInternetProtocol *iface, LARGE_INTEGER dlibMove,
762 DWORD dwOrigin, ULARGE_INTEGER* plibNewPosition)
764 ResProtocol *This = PROTOCOL_THIS(iface);
765 FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
766 return E_NOTIMPL;
769 static HRESULT WINAPI ResProtocol_LockRequest(IInternetProtocol *iface, DWORD dwOptions)
771 ResProtocol *This = PROTOCOL_THIS(iface);
773 TRACE("(%p)->(%d)\n", This, dwOptions);
775 /* test show that we don't have to do anything here */
776 return S_OK;
779 static HRESULT WINAPI ResProtocol_UnlockRequest(IInternetProtocol *iface)
781 ResProtocol *This = PROTOCOL_THIS(iface);
783 TRACE("(%p)\n", This);
785 /* test show that we don't have to do anything here */
786 return S_OK;
789 #undef PROTOCOL_THIS
791 static const IInternetProtocolVtbl ResProtocolVtbl = {
792 ResProtocol_QueryInterface,
793 ResProtocol_AddRef,
794 ResProtocol_Release,
795 ResProtocol_Start,
796 ResProtocol_Continue,
797 ResProtocol_Abort,
798 ResProtocol_Terminate,
799 ResProtocol_Suspend,
800 ResProtocol_Resume,
801 ResProtocol_Read,
802 ResProtocol_Seek,
803 ResProtocol_LockRequest,
804 ResProtocol_UnlockRequest
807 static HRESULT WINAPI ResProtocolFactory_CreateInstance(IClassFactory *iface, IUnknown *pUnkOuter,
808 REFIID riid, void **ppv)
810 ResProtocol *ret;
811 HRESULT hres = S_OK;
813 TRACE("(%p)->(%p %s %p)\n", iface, pUnkOuter, debugstr_guid(riid), ppv);
815 ret = heap_alloc(sizeof(ResProtocol));
816 ret->lpInternetProtocolVtbl = &ResProtocolVtbl;
817 ret->ref = 0;
818 ret->data = NULL;
819 ret->data_len = 0;
820 ret->cur = 0;
821 ret->pUnkOuter = pUnkOuter;
823 if(pUnkOuter) {
824 ret->ref = 1;
825 if(IsEqualGUID(&IID_IUnknown, riid))
826 *ppv = PROTOCOL(ret);
827 else
828 hres = E_FAIL;
829 }else {
830 hres = IInternetProtocol_QueryInterface(PROTOCOL(ret), riid, ppv);
833 if(SUCCEEDED(hres))
834 LOCK_MODULE();
835 else
836 heap_free(ret);
838 return hres;
841 static HRESULT WINAPI ResProtocolInfo_ParseUrl(IInternetProtocolInfo *iface, LPCWSTR pwzUrl,
842 PARSEACTION ParseAction, DWORD dwParseFlags, LPWSTR pwzResult, DWORD cchResult,
843 DWORD* pcchResult, DWORD dwReserved)
845 TRACE("%p)->(%s %d %x %p %d %p %d)\n", iface, debugstr_w(pwzUrl), ParseAction,
846 dwParseFlags, pwzResult, cchResult, pcchResult, dwReserved);
848 if(ParseAction == PARSE_SECURITY_URL) {
849 WCHAR file_part[MAX_PATH], full_path[MAX_PATH];
850 WCHAR *ptr;
851 DWORD size, len;
853 static const WCHAR wszFile[] = {'f','i','l','e',':','/','/'};
854 static const WCHAR wszRes[] = {'r','e','s',':','/','/'};
856 if(strlenW(pwzUrl) <= sizeof(wszRes)/sizeof(WCHAR) || memcmp(pwzUrl, wszRes, sizeof(wszRes)))
857 return E_INVALIDARG;
859 ptr = strchrW(pwzUrl + sizeof(wszRes)/sizeof(WCHAR), '/');
860 if(!ptr)
861 return E_INVALIDARG;
863 len = ptr - (pwzUrl + sizeof(wszRes)/sizeof(WCHAR));
864 if(len >= sizeof(file_part)/sizeof(WCHAR)) {
865 FIXME("Too long URL\n");
866 return MK_E_SYNTAX;
869 memcpy(file_part, pwzUrl + sizeof(wszRes)/sizeof(WCHAR), len*sizeof(WCHAR));
870 file_part[len] = 0;
872 len = SearchPathW(NULL, file_part, NULL, sizeof(full_path)/sizeof(WCHAR), full_path, NULL);
873 if(!len) {
874 WARN("Could not find file %s\n", debugstr_w(file_part));
875 return MK_E_SYNTAX;
878 size = sizeof(wszFile)/sizeof(WCHAR) + len + 1;
879 if(pcchResult)
880 *pcchResult = size;
881 if(size >= cchResult)
882 return S_FALSE;
884 memcpy(pwzResult, wszFile, sizeof(wszFile));
885 memcpy(pwzResult + sizeof(wszFile)/sizeof(WCHAR), full_path, (len+1)*sizeof(WCHAR));
886 return S_OK;
889 if(ParseAction == PARSE_DOMAIN) {
890 if(!pcchResult)
891 return E_POINTER;
893 if(pwzUrl)
894 *pcchResult = strlenW(pwzUrl)+1;
895 else
896 *pcchResult = 1;
897 return E_FAIL;
900 return INET_E_DEFAULT_ACTION;
903 static HRESULT WINAPI ResProtocolInfo_QueryInfo(IInternetProtocolInfo *iface, LPCWSTR pwzUrl,
904 QUERYOPTION QueryOption, DWORD dwQueryFlags, LPVOID pBuffer, DWORD cbBuffer, DWORD* pcbBuf,
905 DWORD dwReserved)
907 TRACE("%p)->(%s %08x %08x %p %d %p %d)\n", iface, debugstr_w(pwzUrl), QueryOption, dwQueryFlags, pBuffer,
908 cbBuffer, pcbBuf, dwReserved);
910 switch(QueryOption) {
911 case QUERY_USES_NETWORK:
912 if(!pBuffer || cbBuffer < sizeof(DWORD))
913 return E_FAIL;
915 *(DWORD*)pBuffer = 0;
916 if(pcbBuf)
917 *pcbBuf = sizeof(DWORD);
918 break;
920 case QUERY_IS_SECURE:
921 FIXME("not supporte QUERY_IS_SECURE\n");
922 return E_NOTIMPL;
923 case QUERY_IS_SAFE:
924 FIXME("not supporte QUERY_IS_SAFE\n");
925 return E_NOTIMPL;
926 default:
927 return INET_E_USE_DEFAULT_PROTOCOLHANDLER;
930 return S_OK;
933 static const IInternetProtocolInfoVtbl ResProtocolInfoVtbl = {
934 InternetProtocolInfo_QueryInterface,
935 InternetProtocolInfo_AddRef,
936 InternetProtocolInfo_Release,
937 ResProtocolInfo_ParseUrl,
938 InternetProtocolInfo_CombineUrl,
939 InternetProtocolInfo_CompareUrl,
940 ResProtocolInfo_QueryInfo
943 static const IClassFactoryVtbl ResProtocolFactoryVtbl = {
944 ClassFactory_QueryInterface,
945 ClassFactory_AddRef,
946 ClassFactory_Release,
947 ResProtocolFactory_CreateInstance,
948 ClassFactory_LockServer
951 static ProtocolFactory ResProtocolFactory = {
952 &ResProtocolInfoVtbl,
953 &ResProtocolFactoryVtbl
956 HRESULT ProtocolFactory_Create(REFCLSID rclsid, REFIID riid, void **ppv)
958 ProtocolFactory *cf = NULL;
960 if(IsEqualGUID(&CLSID_AboutProtocol, rclsid))
961 cf = &AboutProtocolFactory;
962 else if(IsEqualGUID(&CLSID_ResProtocol, rclsid))
963 cf = &ResProtocolFactory;
965 if(!cf) {
966 FIXME("not implemented protocol %s\n", debugstr_guid(rclsid));
967 return CLASS_E_CLASSNOTAVAILABLE;
970 return IUnknown_QueryInterface((IUnknown*)cf, riid, ppv);