mshtml: Moved getting HTTP header to separated function.
[wine] / dlls / mshtml / protocol.c
1 /*
2  * Copyright 2005 Jacek Caban
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #include "config.h"
20
21 #include <stdarg.h>
22 #include <stdio.h>
23
24 #define COBJMACROS
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "ole2.h"
30
31 #include "wine/debug.h"
32 #include "wine/unicode.h"
33
34 #include "mshtml_private.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
37
38 /********************************************************************
39  * common ProtocolFactory implementation
40  */
41
42 #define CLASSFACTORY(x) (&(x)->lpClassFactoryVtbl)
43 #define PROTOCOL(x)     ((IInternetProtocol*)     &(x)->lpInternetProtocolVtbl)
44 #define PROTOCOLINFO(x) ((IInternetProtocolInfo*) &(x)->lpInternetProtocolInfoVtbl)
45
46 typedef struct {
47     const IInternetProtocolInfoVtbl *lpInternetProtocolInfoVtbl;
48     const IClassFactoryVtbl         *lpClassFactoryVtbl;
49 } ProtocolFactory;
50
51 #define PROTOCOLINFO_THIS(iface) DEFINE_THIS(ProtocolFactory, InternetProtocolInfo, iface)
52
53 static HRESULT WINAPI InternetProtocolInfo_QueryInterface(IInternetProtocolInfo *iface, REFIID riid, void **ppv)
54 {
55     ProtocolFactory *This = PROTOCOLINFO_THIS(iface);
56
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);
67     }
68
69     if(!*ppv) {
70         WARN("unknown interface %s\n", debugstr_guid(riid));
71         return E_NOINTERFACE;
72     }
73
74     IInternetProtocolInfo_AddRef(iface);
75     return S_OK;
76 }
77
78 static ULONG WINAPI InternetProtocolInfo_AddRef(IInternetProtocolInfo *iface)
79 {
80     TRACE("(%p)\n", iface);
81     return 2;
82 }
83
84 static ULONG WINAPI InternetProtocolInfo_Release(IInternetProtocolInfo *iface)
85 {
86     TRACE("(%p)\n", iface);
87     return 1;
88 }
89
90 static HRESULT WINAPI InternetProtocolInfo_CombineUrl(IInternetProtocolInfo *iface,
91         LPCWSTR pwzBaseUrl, LPCWSTR pwzRelativeUrl, DWORD dwCombineFlags, LPWSTR pwzResult,
92         DWORD cchResult, DWORD* pcchResult, DWORD dwReserved)
93 {
94     TRACE("%p)->(%s %s %08x %p %d %p %d)\n", iface, debugstr_w(pwzBaseUrl),
95             debugstr_w(pwzRelativeUrl), dwCombineFlags, pwzResult, cchResult,
96             pcchResult, dwReserved);
97
98     return INET_E_USE_DEFAULT_PROTOCOLHANDLER;
99 }
100
101 static HRESULT WINAPI InternetProtocolInfo_CompareUrl(IInternetProtocolInfo *iface, LPCWSTR pwzUrl1,
102         LPCWSTR pwzUrl2, DWORD dwCompareFlags)
103 {
104     TRACE("%p)->(%s %s %08x)\n", iface, debugstr_w(pwzUrl1), debugstr_w(pwzUrl2), dwCompareFlags);
105     return E_NOTIMPL;
106 }
107
108 #undef PROTOCOLINFO_THIS
109
110 #define CLASSFACTORY_THIS(iface) DEFINE_THIS(ProtocolFactory, ClassFactory, iface)
111
112 static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFIID riid, void **ppv)
113 {
114     ProtocolFactory *This = CLASSFACTORY_THIS(iface);
115     return IInternetProtocolInfo_QueryInterface(PROTOCOLINFO(This), riid, ppv);
116 }
117
118 static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface)
119 {
120     ProtocolFactory *This = CLASSFACTORY_THIS(iface);
121     return IInternetProtocolInfo_AddRef(PROTOCOLINFO(This));
122 }
123
124 static ULONG WINAPI ClassFactory_Release(IClassFactory *iface)
125 {
126     ProtocolFactory *This = CLASSFACTORY_THIS(iface);
127     return IInternetProtocolInfo_Release(PROTOCOLINFO(This));
128 }
129
130 static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL dolock)
131 {
132     TRACE("(%p)->(%x)\n", iface, dolock);
133     return S_OK;
134 }
135
136 #undef CLASSFACTORY_THIS
137
138 /********************************************************************
139  * AboutProtocol implementation
140  */
141
142 typedef struct {
143     const IInternetProtocolVtbl *lpInternetProtocolVtbl;
144
145     LONG ref;
146
147     BYTE *data;
148     ULONG data_len;
149     ULONG cur;
150
151     IUnknown *pUnkOuter;
152 } AboutProtocol;
153
154 #define PROTOCOL_THIS(iface) DEFINE_THIS(AboutProtocol, InternetProtocol, iface)
155
156 static HRESULT WINAPI AboutProtocol_QueryInterface(IInternetProtocol *iface, REFIID riid, void **ppv)
157 {
158     AboutProtocol *This = PROTOCOL_THIS(iface);
159
160     *ppv = NULL;
161
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 = PROTOCOL(This);
167     }else if(IsEqualGUID(&IID_IInternetProtocolRoot, riid)) {
168         TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", iface, ppv);
169         *ppv = PROTOCOL(This);
170     }else if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
171         TRACE("(%p)->(IID_IInternetProtocol %p)\n", iface, ppv);
172         *ppv = PROTOCOL(This);
173     }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
174         FIXME("IServiceProvider is not implemented\n");
175         return E_NOINTERFACE;
176     }
177
178     if(!*ppv) {
179         TRACE("unknown interface %s\n", debugstr_guid(riid));
180         return E_NOINTERFACE;
181     }
182
183     IInternetProtocol_AddRef(iface);
184     return S_OK;
185 }
186
187 static ULONG WINAPI AboutProtocol_AddRef(IInternetProtocol *iface)
188 {
189     AboutProtocol *This = PROTOCOL_THIS(iface);
190     ULONG ref = InterlockedIncrement(&This->ref);
191     TRACE("(%p) ref=%d\n", iface, ref);
192     return This->pUnkOuter ? IUnknown_AddRef(This->pUnkOuter) : ref;
193 }
194
195 static ULONG WINAPI AboutProtocol_Release(IInternetProtocol *iface)
196 {
197     AboutProtocol *This = PROTOCOL_THIS(iface);
198     IUnknown *pUnkOuter = This->pUnkOuter;
199     ULONG ref = InterlockedDecrement(&This->ref);
200
201     TRACE("(%p) ref=%x\n", iface, ref);
202
203     if(!ref) {
204         heap_free(This->data);
205         heap_free(This);
206     }
207
208     return pUnkOuter ? IUnknown_Release(pUnkOuter) : ref;
209 }
210
211 static HRESULT WINAPI AboutProtocol_Start(IInternetProtocol *iface, LPCWSTR szUrl,
212         IInternetProtocolSink* pOIProtSink, IInternetBindInfo* pOIBindInfo,
213         DWORD grfPI, HANDLE_PTR dwReserved)
214 {
215     AboutProtocol *This = PROTOCOL_THIS(iface);
216     BINDINFO bindinfo;
217     DWORD grfBINDF = 0;
218     LPCWSTR text = NULL;
219
220     static const WCHAR html_begin[] = {0xfeff,'<','H','T','M','L','>',0};
221     static const WCHAR html_end[] = {'<','/','H','T','M','L','>',0};
222     static const WCHAR wszBlank[] = {'b','l','a','n','k',0};
223     static const WCHAR wszAbout[] = {'a','b','o','u','t',':'};
224     static const WCHAR wszTextHtml[] = {'t','e','x','t','/','h','t','m','l',0};
225
226     /* NOTE:
227      * the about protocol seems not to work as I would expect. It creates html document
228      * for a given url, eg. about:some_text -> <HTML>some_text</HTML> except for the case when
229      * some_text = "blank", when document is blank (<HTML></HMTL>). The same happens
230      * when the url does not have "about:" in the beginning.
231      */
232
233     TRACE("(%p)->(%s %p %p %08x %lx)\n", This, debugstr_w(szUrl), pOIProtSink,
234             pOIBindInfo, grfPI, dwReserved);
235
236     memset(&bindinfo, 0, sizeof(bindinfo));
237     bindinfo.cbSize = sizeof(BINDINFO);
238     IInternetBindInfo_GetBindInfo(pOIBindInfo, &grfBINDF, &bindinfo);
239     ReleaseBindInfo(&bindinfo);
240
241     TRACE("bindf %x\n", grfBINDF);
242
243     if(strlenW(szUrl)>=sizeof(wszAbout)/sizeof(WCHAR) && !memcmp(wszAbout, szUrl, sizeof(wszAbout))) {
244         text = szUrl + sizeof(wszAbout)/sizeof(WCHAR);
245         if(!strcmpW(wszBlank, text))
246             text = NULL;
247     }
248
249     This->data_len = sizeof(html_begin)+sizeof(html_end)-sizeof(WCHAR) 
250         + (text ? strlenW(text)*sizeof(WCHAR) : 0);
251     This->data = heap_alloc(This->data_len);
252
253     memcpy(This->data, html_begin, sizeof(html_begin));
254     if(text)
255         strcatW((LPWSTR)This->data, text);
256     strcatW((LPWSTR)This->data, html_end);
257     
258     This->cur = 0;
259
260     IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_MIMETYPEAVAILABLE, wszTextHtml);
261
262     IInternetProtocolSink_ReportData(pOIProtSink,
263             BSCF_FIRSTDATANOTIFICATION | BSCF_LASTDATANOTIFICATION | BSCF_DATAFULLYAVAILABLE,
264             This->data_len, This->data_len);
265
266     IInternetProtocolSink_ReportResult(pOIProtSink, S_OK, 0, NULL);
267
268     return S_OK;
269 }
270
271 static HRESULT WINAPI AboutProtocol_Continue(IInternetProtocol *iface, PROTOCOLDATA* pProtocolData)
272 {
273     AboutProtocol *This = PROTOCOL_THIS(iface);
274     FIXME("(%p)->(%p)\n", This, pProtocolData);
275     return E_NOTIMPL;
276 }
277
278 static HRESULT WINAPI AboutProtocol_Abort(IInternetProtocol *iface, HRESULT hrReason,
279         DWORD dwOptions)
280 {
281     AboutProtocol *This = PROTOCOL_THIS(iface);
282     FIXME("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);
283     return E_NOTIMPL;
284 }
285
286 static HRESULT WINAPI AboutProtocol_Terminate(IInternetProtocol *iface, DWORD dwOptions)
287 {
288     AboutProtocol *This = PROTOCOL_THIS(iface);
289     TRACE("(%p)->(%08x)\n", This, dwOptions);
290     return S_OK;
291 }
292
293 static HRESULT WINAPI AboutProtocol_Suspend(IInternetProtocol *iface)
294 {
295     AboutProtocol *This = PROTOCOL_THIS(iface);
296     FIXME("(%p)\n", This);
297     return E_NOTIMPL;
298 }
299
300 static HRESULT WINAPI AboutProtocol_Resume(IInternetProtocol *iface)
301 {
302     AboutProtocol *This = PROTOCOL_THIS(iface);
303     FIXME("(%p)\n", This);
304     return E_NOTIMPL;
305 }
306
307 static HRESULT WINAPI AboutProtocol_Read(IInternetProtocol *iface, void* pv, ULONG cb, ULONG* pcbRead)
308 {
309     AboutProtocol *This = PROTOCOL_THIS(iface);
310
311     TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
312
313     if(!This->data)
314         return E_FAIL;
315
316     *pcbRead = (cb > This->data_len-This->cur ? This->data_len-This->cur : cb);
317
318     if(!*pcbRead)
319         return S_FALSE;
320
321     memcpy(pv, This->data+This->cur, *pcbRead);
322     This->cur += *pcbRead;
323
324     return S_OK;
325 }
326
327 static HRESULT WINAPI AboutProtocol_Seek(IInternetProtocol *iface, LARGE_INTEGER dlibMove,
328         DWORD dwOrigin, ULARGE_INTEGER* plibNewPosition)
329 {
330     AboutProtocol *This = PROTOCOL_THIS(iface);
331     FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
332     return E_NOTIMPL;
333 }
334
335 static HRESULT WINAPI AboutProtocol_LockRequest(IInternetProtocol *iface, DWORD dwOptions)
336 {
337     AboutProtocol *This = PROTOCOL_THIS(iface);
338
339     TRACE("(%p)->(%d)\n", This, dwOptions);
340
341     return S_OK;
342 }
343
344 static HRESULT WINAPI AboutProtocol_UnlockRequest(IInternetProtocol *iface)
345 {
346     AboutProtocol *This = PROTOCOL_THIS(iface);
347
348     TRACE("(%p)\n", This);
349
350     return S_OK;
351 }
352
353 #undef PROTOCOL_THIS
354
355 static const IInternetProtocolVtbl AboutProtocolVtbl = {
356     AboutProtocol_QueryInterface,
357     AboutProtocol_AddRef,
358     AboutProtocol_Release,
359     AboutProtocol_Start,
360     AboutProtocol_Continue,
361     AboutProtocol_Abort,
362     AboutProtocol_Terminate,
363     AboutProtocol_Suspend,
364     AboutProtocol_Resume,
365     AboutProtocol_Read,
366     AboutProtocol_Seek,
367     AboutProtocol_LockRequest,
368     AboutProtocol_UnlockRequest
369 };
370
371 static HRESULT WINAPI AboutProtocolFactory_CreateInstance(IClassFactory *iface, IUnknown *pUnkOuter,
372         REFIID riid, void **ppv)
373 {
374     AboutProtocol *ret;
375     HRESULT hres = S_OK;
376
377     TRACE("(%p)->(%p %s %p)\n", iface, pUnkOuter, debugstr_guid(riid), ppv);
378
379     ret = heap_alloc(sizeof(AboutProtocol));
380     ret->lpInternetProtocolVtbl = &AboutProtocolVtbl;
381     ret->ref = 0;
382
383     ret->data = NULL;
384     ret->data_len = 0;
385     ret->cur = 0;
386     ret->pUnkOuter = pUnkOuter;
387
388     if(pUnkOuter) {
389         ret->ref = 1;
390         if(IsEqualGUID(&IID_IUnknown, riid))
391             *ppv = PROTOCOL(ret);
392         else
393             hres = E_INVALIDARG;
394     }else {
395         hres = IInternetProtocol_QueryInterface(PROTOCOL(ret), riid, ppv);
396     }
397
398     if(FAILED(hres))
399         heap_free(ret);
400
401     return hres;
402 }
403
404 static HRESULT WINAPI AboutProtocolInfo_ParseUrl(IInternetProtocolInfo *iface, LPCWSTR pwzUrl,
405         PARSEACTION ParseAction, DWORD dwParseFlags, LPWSTR pwzResult, DWORD cchResult,
406         DWORD* pcchResult, DWORD dwReserved)
407 {
408     TRACE("%p)->(%s %08x %08x %p %d %p %d)\n", iface, debugstr_w(pwzUrl), ParseAction,
409             dwParseFlags, pwzResult, cchResult, pcchResult, dwReserved);
410
411     if(ParseAction == PARSE_SECURITY_URL) {
412         unsigned int len = strlenW(pwzUrl);
413
414         if(len >= cchResult)
415             return S_FALSE;
416
417         memcpy(pwzResult, pwzUrl, (len+1)*sizeof(WCHAR));
418         return S_OK;
419     }
420
421     if(ParseAction == PARSE_DOMAIN) {
422         if(!pcchResult)
423             return E_POINTER;
424
425         if(pwzUrl)
426             *pcchResult = strlenW(pwzUrl)+1;
427         else
428             *pcchResult = 1;
429         return E_FAIL;
430     }
431
432     return INET_E_DEFAULT_ACTION;
433 }
434
435 static HRESULT WINAPI AboutProtocolInfo_QueryInfo(IInternetProtocolInfo *iface, LPCWSTR pwzUrl,
436         QUERYOPTION QueryOption, DWORD dwQueryFlags, LPVOID pBuffer, DWORD cbBuffer, DWORD* pcbBuf,
437         DWORD dwReserved)
438 {
439     TRACE("%p)->(%s %08x %08x %p %d %p %d)\n", iface, debugstr_w(pwzUrl), QueryOption, dwQueryFlags, pBuffer,
440           cbBuffer, pcbBuf, dwReserved);
441
442     switch(QueryOption) {
443     case QUERY_CAN_NAVIGATE:
444         return INET_E_USE_DEFAULT_PROTOCOLHANDLER;
445
446     case QUERY_USES_NETWORK:
447         if(!pBuffer || cbBuffer < sizeof(DWORD))
448             return E_FAIL;
449
450         *(DWORD*)pBuffer = 0;
451         if(pcbBuf)
452             *pcbBuf = sizeof(DWORD);
453
454         break;
455
456     case QUERY_IS_CACHED:
457         FIXME("Unsupported option QUERY_IS_CACHED\n");
458         return E_NOTIMPL;
459     case QUERY_IS_INSTALLEDENTRY:
460         FIXME("Unsupported option QUERY_IS_INSTALLEDENTRY\n");
461         return E_NOTIMPL;
462     case QUERY_IS_CACHED_OR_MAPPED:
463         FIXME("Unsupported option QUERY_IS_CACHED_OR_MAPPED\n");
464         return E_NOTIMPL;
465     case QUERY_IS_SECURE:
466         FIXME("Unsupported option QUERY_IS_SECURE\n");
467         return E_NOTIMPL;
468     case QUERY_IS_SAFE:
469         FIXME("Unsupported option QUERY_IS_SAFE\n");
470         return E_NOTIMPL;
471     case QUERY_USES_HISTORYFOLDER:
472         FIXME("Unsupported option QUERY_USES_HISTORYFOLDER\n");
473         return E_FAIL;
474     default:
475         return E_FAIL;
476     }
477
478     return S_OK;
479 }
480
481 static const IInternetProtocolInfoVtbl AboutProtocolInfoVtbl = {
482     InternetProtocolInfo_QueryInterface,
483     InternetProtocolInfo_AddRef,
484     InternetProtocolInfo_Release,
485     AboutProtocolInfo_ParseUrl,
486     InternetProtocolInfo_CombineUrl,
487     InternetProtocolInfo_CompareUrl,
488     AboutProtocolInfo_QueryInfo
489 };
490
491 static const IClassFactoryVtbl AboutProtocolFactoryVtbl = {
492     ClassFactory_QueryInterface,
493     ClassFactory_AddRef,
494     ClassFactory_Release,
495     AboutProtocolFactory_CreateInstance,
496     ClassFactory_LockServer
497 };
498
499 static ProtocolFactory AboutProtocolFactory = {
500     &AboutProtocolInfoVtbl,
501     &AboutProtocolFactoryVtbl
502 };
503
504 /********************************************************************
505  * ResProtocol implementation
506  */
507
508 typedef struct {
509     const IInternetProtocolVtbl *lpInternetProtocolVtbl;
510     LONG ref;
511
512     BYTE *data;
513     ULONG data_len;
514     ULONG cur;
515
516     IUnknown *pUnkOuter;
517 } ResProtocol;
518
519 #define PROTOCOL_THIS(iface) DEFINE_THIS(ResProtocol, InternetProtocol, iface)
520
521 static HRESULT WINAPI ResProtocol_QueryInterface(IInternetProtocol *iface, REFIID riid, void **ppv)
522 {
523     ResProtocol *This = PROTOCOL_THIS(iface);
524
525     *ppv = NULL;
526
527     if(IsEqualGUID(&IID_IUnknown, riid)) {
528         TRACE("(%p)->(IID_IUnknown %p)\n", iface, ppv);
529         if(This->pUnkOuter)
530             return IUnknown_QueryInterface(This->pUnkOuter, &IID_IUnknown, ppv);
531         *ppv = PROTOCOL(This);
532     }else if(IsEqualGUID(&IID_IInternetProtocolRoot, riid)) {
533         TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", iface, ppv);
534         *ppv = PROTOCOL(This);
535     }else if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
536         TRACE("(%p)->(IID_IInternetProtocol %p)\n", iface, ppv);
537         *ppv = PROTOCOL(This);
538     }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
539         FIXME("IServiceProvider is not implemented\n");
540         return E_NOINTERFACE;
541     }
542
543     if(!*ppv) {
544         TRACE("unknown interface %s\n", debugstr_guid(riid));
545         return E_NOINTERFACE;
546     }
547
548     IInternetProtocol_AddRef(iface);
549     return S_OK;
550 }
551
552 static ULONG WINAPI ResProtocol_AddRef(IInternetProtocol *iface)
553 {
554     ResProtocol *This = PROTOCOL_THIS(iface);
555     ULONG ref = InterlockedIncrement(&This->ref);
556     TRACE("(%p) ref=%d\n", iface, ref);
557     return This->pUnkOuter ? IUnknown_AddRef(This->pUnkOuter) : ref;
558 }
559
560 static ULONG WINAPI ResProtocol_Release(IInternetProtocol *iface)
561 {
562     ResProtocol *This = (ResProtocol*)iface;
563     IUnknown *pUnkOuter = This->pUnkOuter;
564     ULONG ref = InterlockedDecrement(&This->ref);
565
566     TRACE("(%p) ref=%x\n", iface, ref);
567
568     if(!ref) {
569         heap_free(This->data);
570         heap_free(This);
571     }
572
573     return pUnkOuter ? IUnknown_Release(pUnkOuter) : ref;
574 }
575
576 static HRESULT WINAPI ResProtocol_Start(IInternetProtocol *iface, LPCWSTR szUrl,
577         IInternetProtocolSink* pOIProtSink, IInternetBindInfo* pOIBindInfo,
578         DWORD grfPI, HANDLE_PTR dwReserved)
579 {
580     ResProtocol *This = PROTOCOL_THIS(iface);
581     DWORD grfBINDF = 0, len;
582     BINDINFO bindinfo;
583     LPWSTR url_dll, url_file, url, mime, res_type = (LPWSTR)RT_HTML;
584     HMODULE hdll;
585     HRSRC src;
586     HRESULT hres;
587
588     static const WCHAR wszRes[] = {'r','e','s',':','/','/'};
589
590     TRACE("(%p)->(%s %p %p %08x %lx)\n", This, debugstr_w(szUrl), pOIProtSink,
591             pOIBindInfo, grfPI, dwReserved);
592
593     memset(&bindinfo, 0, sizeof(bindinfo));
594     bindinfo.cbSize = sizeof(BINDINFO);
595     IInternetBindInfo_GetBindInfo(pOIBindInfo, &grfBINDF, &bindinfo);
596     ReleaseBindInfo(&bindinfo);
597
598     len = strlenW(szUrl)+16;
599     url = heap_alloc(len*sizeof(WCHAR));
600     hres = CoInternetParseUrl(szUrl, PARSE_ENCODE, 0, url, len, &len, 0);
601     if(FAILED(hres)) {
602         WARN("CoInternetParseUrl failed: %08x\n", hres);
603         heap_free(url);
604         IInternetProtocolSink_ReportResult(pOIProtSink, hres, 0, NULL);
605         return hres;
606     }
607
608     if(len < sizeof(wszRes)/sizeof(wszRes[0]) || memcmp(url, wszRes, sizeof(wszRes))) {
609         WARN("Wrong protocol of url: %s\n", debugstr_w(url));
610         IInternetProtocolSink_ReportResult(pOIProtSink, E_INVALIDARG, 0, NULL);
611         heap_free(url);
612         return E_INVALIDARG;
613     }
614
615     url_dll = url + sizeof(wszRes)/sizeof(wszRes[0]);
616     if(!(url_file = strrchrW(url_dll, '/'))) {
617         WARN("wrong url: %s\n", debugstr_w(url));
618         IInternetProtocolSink_ReportResult(pOIProtSink, MK_E_SYNTAX, 0, NULL);
619         heap_free(url);
620         return MK_E_SYNTAX;
621     }
622
623     *url_file++ = 0;
624     hdll = LoadLibraryExW(url_dll, NULL, LOAD_LIBRARY_AS_DATAFILE);
625     if(!hdll) {
626         if (!(res_type = strrchrW(url_dll, '/'))) {
627             WARN("Could not open dll: %s\n", debugstr_w(url_dll));
628             IInternetProtocolSink_ReportResult(pOIProtSink, HRESULT_FROM_WIN32(GetLastError()), 0, NULL);
629             heap_free(url);
630             return HRESULT_FROM_WIN32(GetLastError());
631         }
632         *res_type++ = 0;
633
634         hdll = LoadLibraryExW(url_dll, NULL, LOAD_LIBRARY_AS_DATAFILE);
635         if(!hdll) {
636             WARN("Could not open dll: %s\n", debugstr_w(url_dll));
637             IInternetProtocolSink_ReportResult(pOIProtSink, HRESULT_FROM_WIN32(GetLastError()), 0, NULL);
638             heap_free(url);
639             return HRESULT_FROM_WIN32(GetLastError());
640         }
641     }
642
643     TRACE("trying to find resource type %s, name %s\n", debugstr_w(res_type), debugstr_w(url_file));
644
645     src = FindResourceW(hdll, url_file, res_type);
646     if(!src) {
647         LPWSTR endpoint = NULL;
648         DWORD file_id = strtolW(url_file, &endpoint, 10);
649         if(endpoint == url_file+strlenW(url_file))
650             src = FindResourceW(hdll, MAKEINTRESOURCEW(file_id), MAKEINTRESOURCEW(RT_HTML));
651
652         if(!src) {
653             WARN("Could not find resource\n");
654             IInternetProtocolSink_ReportResult(pOIProtSink,
655                     HRESULT_FROM_WIN32(GetLastError()), 0, NULL);
656             heap_free(url);
657             return HRESULT_FROM_WIN32(GetLastError());
658         }
659     }
660
661     if(This->data) {
662         WARN("data already loaded\n");
663         heap_free(This->data);
664     }
665
666     This->data_len = SizeofResource(hdll, src);
667     This->data = heap_alloc(This->data_len);
668     memcpy(This->data, LoadResource(hdll, src), This->data_len);
669     This->cur = 0;
670
671     FreeLibrary(hdll);
672
673     hres = FindMimeFromData(NULL, url_file, This->data, This->data_len, NULL, 0, &mime, 0);
674     heap_free(url);
675     if(SUCCEEDED(hres)) {
676         IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_MIMETYPEAVAILABLE, mime);
677         CoTaskMemFree(mime);
678     }
679
680     IInternetProtocolSink_ReportData(pOIProtSink,
681             BSCF_FIRSTDATANOTIFICATION | BSCF_LASTDATANOTIFICATION | BSCF_DATAFULLYAVAILABLE,
682             This->data_len, This->data_len);
683
684     IInternetProtocolSink_ReportResult(pOIProtSink, S_OK, 0, NULL);
685     
686     return S_OK;
687 }
688
689 static HRESULT WINAPI ResProtocol_Continue(IInternetProtocol *iface, PROTOCOLDATA* pProtocolData)
690 {
691     ResProtocol *This = PROTOCOL_THIS(iface);
692     FIXME("(%p)->(%p)\n", This, pProtocolData);
693     return E_NOTIMPL;
694 }
695
696 static HRESULT WINAPI ResProtocol_Abort(IInternetProtocol *iface, HRESULT hrReason,
697         DWORD dwOptions)
698 {
699     ResProtocol *This = PROTOCOL_THIS(iface);
700     FIXME("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);
701     return E_NOTIMPL;
702 }
703
704 static HRESULT WINAPI ResProtocol_Terminate(IInternetProtocol *iface, DWORD dwOptions)
705 {
706     ResProtocol *This = PROTOCOL_THIS(iface);
707
708     TRACE("(%p)->(%08x)\n", This, dwOptions);
709
710     /* test show that we don't have to do anything here */
711     return S_OK;
712 }
713
714 static HRESULT WINAPI ResProtocol_Suspend(IInternetProtocol *iface)
715 {
716     ResProtocol *This = PROTOCOL_THIS(iface);
717     FIXME("(%p)\n", This);
718     return E_NOTIMPL;
719 }
720
721 static HRESULT WINAPI ResProtocol_Resume(IInternetProtocol *iface)
722 {
723     ResProtocol *This = PROTOCOL_THIS(iface);
724     FIXME("(%p)\n", This);
725     return E_NOTIMPL;
726 }
727
728 static HRESULT WINAPI ResProtocol_Read(IInternetProtocol *iface, void* pv, ULONG cb, ULONG* pcbRead)
729 {
730     ResProtocol *This = PROTOCOL_THIS(iface);
731
732     TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
733
734     if(!This->data)
735         return E_FAIL;
736
737     *pcbRead = (cb > This->data_len-This->cur ? This->data_len-This->cur : cb);
738
739     if(!*pcbRead)
740         return S_FALSE;
741
742     memcpy(pv, This->data+This->cur, *pcbRead);
743     This->cur += *pcbRead;
744
745     return S_OK;
746 }
747
748 static HRESULT WINAPI ResProtocol_Seek(IInternetProtocol *iface, LARGE_INTEGER dlibMove,
749         DWORD dwOrigin, ULARGE_INTEGER* plibNewPosition)
750 {
751     ResProtocol *This = PROTOCOL_THIS(iface);
752     FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
753     return E_NOTIMPL;
754 }
755
756 static HRESULT WINAPI ResProtocol_LockRequest(IInternetProtocol *iface, DWORD dwOptions)
757 {
758     ResProtocol *This = PROTOCOL_THIS(iface);
759
760     TRACE("(%p)->(%d)\n", This, dwOptions);
761
762     /* test show that we don't have to do anything here */
763     return S_OK;
764 }
765
766 static HRESULT WINAPI ResProtocol_UnlockRequest(IInternetProtocol *iface)
767 {
768     ResProtocol *This = PROTOCOL_THIS(iface);
769
770     TRACE("(%p)\n", This);
771
772     /* test show that we don't have to do anything here */
773     return S_OK;
774 }
775
776 #undef PROTOCOL_THIS
777
778 static const IInternetProtocolVtbl ResProtocolVtbl = {
779     ResProtocol_QueryInterface,
780     ResProtocol_AddRef,
781     ResProtocol_Release,
782     ResProtocol_Start,
783     ResProtocol_Continue,
784     ResProtocol_Abort,
785     ResProtocol_Terminate,
786     ResProtocol_Suspend,
787     ResProtocol_Resume,
788     ResProtocol_Read,
789     ResProtocol_Seek,
790     ResProtocol_LockRequest,
791     ResProtocol_UnlockRequest
792 };
793
794 static HRESULT WINAPI ResProtocolFactory_CreateInstance(IClassFactory *iface, IUnknown *pUnkOuter,
795         REFIID riid, void **ppv)
796 {
797     ResProtocol *ret;
798     HRESULT hres = S_OK;
799
800     TRACE("(%p)->(%p %s %p)\n", iface, pUnkOuter, debugstr_guid(riid), ppv);
801
802     ret = heap_alloc(sizeof(ResProtocol));
803     ret->lpInternetProtocolVtbl = &ResProtocolVtbl;
804     ret->ref = 0;
805     ret->data = NULL;
806     ret->data_len = 0;
807     ret->cur = 0;
808     ret->pUnkOuter = pUnkOuter;
809
810     if(pUnkOuter) {
811         ret->ref = 1;
812         if(IsEqualGUID(&IID_IUnknown, riid))
813             *ppv = PROTOCOL(ret);
814         else
815             hres = E_FAIL;
816     }else {
817         hres = IInternetProtocol_QueryInterface(PROTOCOL(ret), riid, ppv);
818     }
819
820     if(FAILED(hres))
821         heap_free(ret);
822
823     return hres;
824 }
825
826 static HRESULT WINAPI ResProtocolInfo_ParseUrl(IInternetProtocolInfo *iface, LPCWSTR pwzUrl,
827         PARSEACTION ParseAction, DWORD dwParseFlags, LPWSTR pwzResult, DWORD cchResult,
828         DWORD* pcchResult, DWORD dwReserved)
829 {
830     TRACE("%p)->(%s %d %x %p %d %p %d)\n", iface, debugstr_w(pwzUrl), ParseAction,
831             dwParseFlags, pwzResult, cchResult, pcchResult, dwReserved);
832
833     if(ParseAction == PARSE_SECURITY_URL) {
834         WCHAR file_part[MAX_PATH], full_path[MAX_PATH];
835         WCHAR *ptr;
836         DWORD size, len;
837
838         static const WCHAR wszFile[] = {'f','i','l','e',':','/','/'};
839         static const WCHAR wszRes[] = {'r','e','s',':','/','/'};
840
841         if(strlenW(pwzUrl) <= sizeof(wszRes)/sizeof(WCHAR) || memcmp(pwzUrl, wszRes, sizeof(wszRes)))
842             return E_INVALIDARG;
843
844         ptr = strchrW(pwzUrl + sizeof(wszRes)/sizeof(WCHAR), '/');
845         if(!ptr)
846             return E_INVALIDARG;
847
848         len = ptr - (pwzUrl + sizeof(wszRes)/sizeof(WCHAR));
849         if(len >= sizeof(file_part)/sizeof(WCHAR)) {
850             FIXME("Too long URL\n");
851             return MK_E_SYNTAX;
852         }
853
854         memcpy(file_part, pwzUrl + sizeof(wszRes)/sizeof(WCHAR), len*sizeof(WCHAR));
855         file_part[len] = 0;
856
857         len = SearchPathW(NULL, file_part, NULL, sizeof(full_path)/sizeof(WCHAR), full_path, NULL);
858         if(!len) {
859             WARN("Could not find file %s\n", debugstr_w(file_part));
860             return MK_E_SYNTAX;
861         }
862
863         size = sizeof(wszFile)/sizeof(WCHAR) + len + 1;
864         if(pcchResult)
865             *pcchResult = size;
866         if(size >= cchResult)
867             return S_FALSE;
868
869         memcpy(pwzResult, wszFile, sizeof(wszFile));
870         memcpy(pwzResult + sizeof(wszFile)/sizeof(WCHAR), full_path, (len+1)*sizeof(WCHAR));
871         return S_OK;
872     }
873
874     if(ParseAction == PARSE_DOMAIN) {
875         if(!pcchResult)
876             return E_POINTER;
877
878         if(pwzUrl)
879             *pcchResult = strlenW(pwzUrl)+1;
880         else
881             *pcchResult = 1;
882         return E_FAIL;
883     }
884
885     return INET_E_DEFAULT_ACTION;
886 }
887
888 static HRESULT WINAPI ResProtocolInfo_QueryInfo(IInternetProtocolInfo *iface, LPCWSTR pwzUrl,
889         QUERYOPTION QueryOption, DWORD dwQueryFlags, LPVOID pBuffer, DWORD cbBuffer, DWORD* pcbBuf,
890         DWORD dwReserved)
891 {
892     TRACE("%p)->(%s %08x %08x %p %d %p %d)\n", iface, debugstr_w(pwzUrl), QueryOption, dwQueryFlags, pBuffer,
893           cbBuffer, pcbBuf, dwReserved);
894
895     switch(QueryOption) {
896     case QUERY_USES_NETWORK:
897         if(!pBuffer || cbBuffer < sizeof(DWORD))
898             return E_FAIL;
899
900         *(DWORD*)pBuffer = 0;
901         if(pcbBuf)
902             *pcbBuf = sizeof(DWORD);
903         break;
904
905     case QUERY_IS_SECURE:
906         FIXME("not supporte QUERY_IS_SECURE\n");
907         return E_NOTIMPL;
908     case QUERY_IS_SAFE:
909         FIXME("not supporte QUERY_IS_SAFE\n");
910         return E_NOTIMPL;
911     default:
912         return INET_E_USE_DEFAULT_PROTOCOLHANDLER;
913     }
914
915     return S_OK;
916 }
917
918 static const IInternetProtocolInfoVtbl ResProtocolInfoVtbl = {
919     InternetProtocolInfo_QueryInterface,
920     InternetProtocolInfo_AddRef,
921     InternetProtocolInfo_Release,
922     ResProtocolInfo_ParseUrl,
923     InternetProtocolInfo_CombineUrl,
924     InternetProtocolInfo_CompareUrl,
925     ResProtocolInfo_QueryInfo
926 };
927
928 static const IClassFactoryVtbl ResProtocolFactoryVtbl = {
929     ClassFactory_QueryInterface,
930     ClassFactory_AddRef,
931     ClassFactory_Release,
932     ResProtocolFactory_CreateInstance,
933     ClassFactory_LockServer
934 };
935
936 static ProtocolFactory ResProtocolFactory = {
937     &ResProtocolInfoVtbl,
938     &ResProtocolFactoryVtbl
939 };
940
941 /********************************************************************
942  * JSProtocol implementation
943  */
944
945 static HRESULT WINAPI JSProtocolFactory_CreateInstance(IClassFactory *iface, IUnknown *pUnkOuter,
946         REFIID riid, void **ppv)
947 {
948     FIXME("(%p)->(%p %s %p)\n", iface, pUnkOuter, debugstr_guid(riid), ppv);
949     return E_NOTIMPL;
950 }
951
952 static HRESULT WINAPI JSProtocolInfo_ParseUrl(IInternetProtocolInfo *iface, LPCWSTR pwzUrl,
953         PARSEACTION ParseAction, DWORD dwParseFlags, LPWSTR pwzResult, DWORD cchResult,
954         DWORD* pcchResult, DWORD dwReserved)
955 {
956     TRACE("%p)->(%s %d %x %p %d %p %d)\n", iface, debugstr_w(pwzUrl), ParseAction,
957           dwParseFlags, pwzResult, cchResult, pcchResult, dwReserved);
958
959     switch(ParseAction) {
960     case PARSE_SECURITY_URL:
961         FIXME("PARSE_SECURITY_URL\n");
962         return E_NOTIMPL;
963     case PARSE_DOMAIN:
964         FIXME("PARSE_DOMAIN\n");
965         return E_NOTIMPL;
966     default:
967         return INET_E_DEFAULT_ACTION;
968     }
969
970     return S_OK;
971 }
972
973 static HRESULT WINAPI JSProtocolInfo_QueryInfo(IInternetProtocolInfo *iface, LPCWSTR pwzUrl,
974         QUERYOPTION QueryOption, DWORD dwQueryFlags, LPVOID pBuffer, DWORD cbBuffer, DWORD* pcbBuf,
975         DWORD dwReserved)
976 {
977     TRACE("%p)->(%s %08x %08x %p %d %p %d)\n", iface, debugstr_w(pwzUrl), QueryOption, dwQueryFlags, pBuffer,
978           cbBuffer, pcbBuf, dwReserved);
979
980     switch(QueryOption) {
981     case QUERY_USES_NETWORK:
982         if(!pBuffer || cbBuffer < sizeof(DWORD))
983             return E_FAIL;
984
985         *(DWORD*)pBuffer = 0;
986         if(pcbBuf)
987             *pcbBuf = sizeof(DWORD);
988         break;
989
990     case QUERY_IS_SECURE:
991         FIXME("not supporte QUERY_IS_SECURE\n");
992         return E_NOTIMPL;
993
994     default:
995         return INET_E_USE_DEFAULT_PROTOCOLHANDLER;
996     }
997
998     return S_OK;
999 }
1000
1001 static const IInternetProtocolInfoVtbl JSProtocolInfoVtbl = {
1002     InternetProtocolInfo_QueryInterface,
1003     InternetProtocolInfo_AddRef,
1004     InternetProtocolInfo_Release,
1005     JSProtocolInfo_ParseUrl,
1006     InternetProtocolInfo_CombineUrl,
1007     InternetProtocolInfo_CompareUrl,
1008     JSProtocolInfo_QueryInfo
1009 };
1010
1011 static const IClassFactoryVtbl JSProtocolFactoryVtbl = {
1012     ClassFactory_QueryInterface,
1013     ClassFactory_AddRef,
1014     ClassFactory_Release,
1015     JSProtocolFactory_CreateInstance,
1016     ClassFactory_LockServer
1017 };
1018
1019 static ProtocolFactory JSProtocolFactory = {
1020     &JSProtocolInfoVtbl,
1021     &JSProtocolFactoryVtbl
1022 };
1023
1024 HRESULT ProtocolFactory_Create(REFCLSID rclsid, REFIID riid, void **ppv)
1025 {
1026     ProtocolFactory *cf = NULL;
1027
1028     if(IsEqualGUID(&CLSID_AboutProtocol, rclsid))
1029         cf = &AboutProtocolFactory;
1030     else if(IsEqualGUID(&CLSID_ResProtocol, rclsid))
1031         cf = &ResProtocolFactory;
1032     else if(IsEqualGUID(&CLSID_JSProtocol, rclsid))
1033         cf = &JSProtocolFactory;
1034
1035     if(!cf) {
1036         FIXME("not implemented protocol %s\n", debugstr_guid(rclsid));
1037         return CLASS_E_CLASSNOTAVAILABLE;
1038     }
1039  
1040     return IUnknown_QueryInterface((IUnknown*)cf, riid, ppv);
1041 }