2 * Copyright 2006-2007 Jacek Caban for CodeWeavers
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
33 #include "wine/debug.h"
34 #include "wine/unicode.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(itss);
39 const IInternetProtocolVtbl *lpInternetProtocolVtbl;
40 const IInternetProtocolInfoVtbl *lpInternetProtocolInfoVtbl;
45 struct chmFile *chm_file;
46 struct chmUnitInfo chm_object;
49 #define PROTOCOL(x) ((IInternetProtocol*) &(x)->lpInternetProtocolVtbl)
50 #define PROTINFO(x) ((IInternetProtocolInfo*) &(x)->lpInternetProtocolInfoVtbl)
52 static void release_chm(ITSProtocol *This)
55 chm_close(This->chm_file);
56 This->chm_file = NULL;
61 #define PROTOCOL_THIS(iface) DEFINE_THIS(ITSProtocol, InternetProtocol, iface)
63 static HRESULT WINAPI ITSProtocol_QueryInterface(IInternetProtocol *iface, REFIID riid, void **ppv)
65 ITSProtocol *This = PROTOCOL_THIS(iface);
68 if(IsEqualGUID(&IID_IUnknown, riid)) {
69 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
70 *ppv = PROTOCOL(This);
71 }else if(IsEqualGUID(&IID_IInternetProtocolRoot, riid)) {
72 TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", This, ppv);
73 *ppv = PROTOCOL(This);
74 }else if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
75 TRACE("(%p)->(IID_IInternetProtocol %p)\n", This, ppv);
76 *ppv = PROTOCOL(This);
77 }else if(IsEqualGUID(&IID_IInternetProtocolInfo, riid)) {
78 TRACE("(%p)->(IID_IInternetProtocolInfo %p)\n", This, ppv);
79 *ppv = PROTINFO(This);
83 IInternetProtocol_AddRef(iface);
87 WARN("not supported interface %s\n", debugstr_guid(riid));
91 static ULONG WINAPI ITSProtocol_AddRef(IInternetProtocol *iface)
93 ITSProtocol *This = PROTOCOL_THIS(iface);
94 LONG ref = InterlockedIncrement(&This->ref);
95 TRACE("(%p) ref=%d\n", This, ref);
99 static ULONG WINAPI ITSProtocol_Release(IInternetProtocol *iface)
101 ITSProtocol *This = PROTOCOL_THIS(iface);
102 LONG ref = InterlockedDecrement(&This->ref);
104 TRACE("(%p) ref=%d\n", This, ref);
108 HeapFree(GetProcessHeap(), 0, This);
116 static LPCWSTR skip_schema(LPCWSTR url)
118 static const WCHAR its_schema[] = {'i','t','s',':'};
119 static const WCHAR msits_schema[] = {'m','s','-','i','t','s',':'};
120 static const WCHAR mk_schema[] = {'m','k',':','@','M','S','I','T','S','t','o','r','e',':'};
122 if(!strncmpiW(its_schema, url, sizeof(its_schema)/sizeof(WCHAR)))
123 return url+sizeof(its_schema)/sizeof(WCHAR);
124 if(!strncmpiW(msits_schema, url, sizeof(msits_schema)/sizeof(WCHAR)))
125 return url+sizeof(msits_schema)/sizeof(WCHAR);
126 if(!strncmpiW(mk_schema, url, sizeof(mk_schema)/sizeof(WCHAR)))
127 return url+sizeof(mk_schema)/sizeof(WCHAR);
132 static HRESULT report_result(IInternetProtocolSink *sink, HRESULT hres)
134 IInternetProtocolSink_ReportResult(sink, hres, 0, NULL);
138 static HRESULT WINAPI ITSProtocol_Start(IInternetProtocol *iface, LPCWSTR szUrl,
139 IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
140 DWORD grfPI, DWORD dwReserved)
142 ITSProtocol *This = PROTOCOL_THIS(iface);
144 DWORD bindf = 0, len;
145 LPWSTR file_name, mime, object_name, p;
147 struct chmFile *chm_file;
148 struct chmUnitInfo chm_object;
152 static const WCHAR separator[] = {':',':',0};
154 TRACE("(%p)->(%s %p %p %08x %d)\n", This, debugstr_w(szUrl), pOIProtSink,
155 pOIBindInfo, grfPI, dwReserved);
157 ptr = skip_schema(szUrl);
159 return INET_E_USE_DEFAULT_PROTOCOLHANDLER;
161 memset(&bindinfo, 0, sizeof(bindinfo));
162 bindinfo.cbSize = sizeof(BINDINFO);
163 hres = IInternetBindInfo_GetBindInfo(pOIBindInfo, &bindf, &bindinfo);
165 WARN("GetBindInfo failed: %08x\n", hres);
169 ReleaseBindInfo(&bindinfo);
171 len = strlenW(ptr)+3;
172 file_name = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
173 memcpy(file_name, ptr, len*sizeof(WCHAR));
174 hres = UrlUnescapeW(file_name, NULL, &len, URL_UNESCAPE_INPLACE);
176 WARN("UrlUnescape failed: %08x\n", hres);
177 HeapFree(GetProcessHeap(), 0, file_name);
181 p = strstrW(file_name, separator);
183 WARN("invalid url\n");
184 HeapFree(GetProcessHeap(), 0, file_name);
185 return report_result(pOIProtSink, STG_E_FILENOTFOUND);
189 chm_file = chm_openW(file_name);
191 WARN("Could not open chm file\n");
192 HeapFree(GetProcessHeap(), 0, file_name);
193 return report_result(pOIProtSink, STG_E_FILENOTFOUND);
197 if(*object_name != '/' && *object_name != '\\') {
198 int len = strlenW(object_name)+1;
199 memmove(object_name+1, object_name, len*sizeof(WCHAR));
203 for(p=object_name; *p; p++) {
208 TRACE("Resolving %s\n", debugstr_w(object_name));
210 memset(&chm_object, 0, sizeof(chm_object));
211 res = chm_resolve_object(chm_file, object_name, &chm_object);
212 if(res != CHM_RESOLVE_SUCCESS) {
213 WARN("Could not resolve chm object\n");
214 HeapFree(GetProcessHeap(), 0, object_name);
216 return report_result(pOIProtSink, STG_E_FILENOTFOUND);
219 IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_SENDINGREQUEST,
220 strrchrW(object_name, '/')+1);
221 HeapFree(GetProcessHeap(), 0, file_name);
223 /* FIXME: Native doesn't use FindMimeFromData */
224 hres = FindMimeFromData(NULL, szUrl, NULL, 0, NULL, 0, &mime, 0);
225 if(SUCCEEDED(hres)) {
226 IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_MIMETYPEAVAILABLE, mime);
230 release_chm(This); /* Native leaks handle here */
231 This->chm_file = chm_file;
232 memcpy(&This->chm_object, &chm_object, sizeof(chm_object));
234 hres = IInternetProtocolSink_ReportData(pOIProtSink,
235 BSCF_FIRSTDATANOTIFICATION|BSCF_DATAFULLYAVAILABLE,
236 chm_object.length, chm_object.length);
238 WARN("ReportData failed: %08x\n", hres);
240 return report_result(pOIProtSink, hres);
243 hres = IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_BEGINDOWNLOADDATA, NULL);
245 return report_result(pOIProtSink, hres);
248 static HRESULT WINAPI ITSProtocol_Continue(IInternetProtocol *iface, PROTOCOLDATA *pProtocolData)
250 ITSProtocol *This = PROTOCOL_THIS(iface);
251 FIXME("(%p)->(%p)\n", This, pProtocolData);
255 static HRESULT WINAPI ITSProtocol_Abort(IInternetProtocol *iface, HRESULT hrReason,
258 ITSProtocol *This = PROTOCOL_THIS(iface);
259 FIXME("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);
263 static HRESULT WINAPI ITSProtocol_Terminate(IInternetProtocol *iface, DWORD dwOptions)
265 ITSProtocol *This = PROTOCOL_THIS(iface);
267 TRACE("(%p)->(%08x)\n", This, dwOptions);
272 static HRESULT WINAPI ITSProtocol_Suspend(IInternetProtocol *iface)
274 ITSProtocol *This = PROTOCOL_THIS(iface);
275 FIXME("(%p)\n", This);
279 static HRESULT WINAPI ITSProtocol_Resume(IInternetProtocol *iface)
281 ITSProtocol *This = PROTOCOL_THIS(iface);
282 FIXME("(%p)\n", This);
286 static HRESULT WINAPI ITSProtocol_Read(IInternetProtocol *iface, void *pv,
287 ULONG cb, ULONG *pcbRead)
289 ITSProtocol *This = PROTOCOL_THIS(iface);
291 TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
294 return INET_E_DATA_NOT_AVAILABLE;
296 *pcbRead = chm_retrieve_object(This->chm_file, &This->chm_object, pv, This->offset, cb);
297 This->offset += *pcbRead;
299 return *pcbRead ? S_OK : S_FALSE;
302 static HRESULT WINAPI ITSProtocol_Seek(IInternetProtocol *iface, LARGE_INTEGER dlibMove,
303 DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
305 ITSProtocol *This = PROTOCOL_THIS(iface);
306 FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
310 static HRESULT WINAPI ITSProtocol_LockRequest(IInternetProtocol *iface, DWORD dwOptions)
312 ITSProtocol *This = PROTOCOL_THIS(iface);
314 TRACE("(%p)->(%08x)\n", This, dwOptions);
319 static HRESULT WINAPI ITSProtocol_UnlockRequest(IInternetProtocol *iface)
321 ITSProtocol *This = PROTOCOL_THIS(iface);
323 TRACE("(%p)\n", This);
330 static const IInternetProtocolVtbl ITSProtocolVtbl = {
331 ITSProtocol_QueryInterface,
335 ITSProtocol_Continue,
337 ITSProtocol_Terminate,
342 ITSProtocol_LockRequest,
343 ITSProtocol_UnlockRequest
346 #define PROTINFO_THIS(iface) DEFINE_THIS(ITSProtocol, InternetProtocolInfo, iface)
348 static HRESULT WINAPI ITSProtocolInfo_QueryInterface(IInternetProtocolInfo *iface,
349 REFIID riid, void **ppv)
351 ITSProtocol *This = PROTINFO_THIS(iface);
352 return IInternetProtocol_QueryInterface(PROTOCOL(This), riid, ppv);
355 static ULONG WINAPI ITSProtocolInfo_AddRef(IInternetProtocolInfo *iface)
357 ITSProtocol *This = PROTINFO_THIS(iface);
358 return IInternetProtocol_AddRef(PROTOCOL(This));
361 static ULONG WINAPI ITSProtocolInfo_Release(IInternetProtocolInfo *iface)
363 ITSProtocol *This = PROTINFO_THIS(iface);
364 return IInternetProtocol_Release(PROTOCOL(This));
367 static HRESULT WINAPI ITSProtocolInfo_ParseUrl(IInternetProtocolInfo *iface, LPCWSTR pwzUrl,
368 PARSEACTION ParseAction, DWORD dwParseFlags, LPWSTR pwzResult, DWORD cchResult,
369 DWORD *pcchResult, DWORD dwReserved)
371 ITSProtocol *This = PROTINFO_THIS(iface);
373 TRACE("(%p)->(%s %x %08x %p %d %p %d)\n", This, debugstr_w(pwzUrl), ParseAction,
374 dwParseFlags, pwzResult, cchResult, pcchResult, dwReserved);
376 switch(ParseAction) {
377 case PARSE_CANONICALIZE:
378 FIXME("PARSE_CANONICALIZE\n");
380 case PARSE_SECURITY_URL:
381 FIXME("PARSE_SECURITY_URL\n");
384 return INET_E_DEFAULT_ACTION;
390 static HRESULT WINAPI ITSProtocolInfo_CombineUrl(IInternetProtocolInfo *iface,
391 LPCWSTR pwzBaseUrl, LPCWSTR pwzRelativeUrl, DWORD dwCombineFlags, LPWSTR pwzResult,
392 DWORD cchResult, DWORD* pcchResult, DWORD dwReserved)
394 ITSProtocol *This = PROTINFO_THIS(iface);
395 LPCWSTR base_end, ptr;
398 static const WCHAR separator[] = {':',':',0};
400 TRACE("(%p)->(%s %s %08x %p %d %p %d)\n", This, debugstr_w(pwzBaseUrl),
401 debugstr_w(pwzRelativeUrl), dwCombineFlags, pwzResult, cchResult,
402 pcchResult, dwReserved);
404 base_end = strstrW(pwzBaseUrl, separator);
409 if(!skip_schema(pwzBaseUrl))
410 return INET_E_USE_DEFAULT_PROTOCOLHANDLER;
412 if(strchrW(pwzRelativeUrl, ':'))
413 return STG_E_INVALIDNAME;
415 if(pwzRelativeUrl[0] != '/') {
416 ptr = strrchrW(base_end, '/');
420 base_end += strlenW(base_end);
423 rel_len = strlenW(pwzRelativeUrl)+1;
425 *pcchResult = rel_len + (base_end-pwzBaseUrl);
427 if(*pcchResult > cchResult)
428 return E_OUTOFMEMORY;
430 memcpy(pwzResult, pwzBaseUrl, (base_end-pwzBaseUrl)*sizeof(WCHAR));
431 strcpyW(pwzResult + (base_end-pwzBaseUrl), pwzRelativeUrl);
436 static HRESULT WINAPI ITSProtocolInfo_CompareUrl(IInternetProtocolInfo *iface, LPCWSTR pwzUrl1,
437 LPCWSTR pwzUrl2, DWORD dwCompareFlags)
439 ITSProtocol *This = PROTINFO_THIS(iface);
440 FIXME("%p)->(%s %s %08x)\n", This, debugstr_w(pwzUrl1), debugstr_w(pwzUrl2), dwCompareFlags);
444 static HRESULT WINAPI ITSProtocolInfo_QueryInfo(IInternetProtocolInfo *iface, LPCWSTR pwzUrl,
445 QUERYOPTION QueryOption, DWORD dwQueryFlags, LPVOID pBuffer, DWORD cbBuffer, DWORD* pcbBuf,
448 ITSProtocol *This = PROTINFO_THIS(iface);
449 FIXME("(%p)->(%s %08x %08x %p %d %p %d)\n", This, debugstr_w(pwzUrl), QueryOption,
450 dwQueryFlags, pBuffer, cbBuffer, pcbBuf, dwReserved);
456 static const IInternetProtocolInfoVtbl ITSProtocolInfoVtbl = {
457 ITSProtocolInfo_QueryInterface,
458 ITSProtocolInfo_AddRef,
459 ITSProtocolInfo_Release,
460 ITSProtocolInfo_ParseUrl,
461 ITSProtocolInfo_CombineUrl,
462 ITSProtocolInfo_CompareUrl,
463 ITSProtocolInfo_QueryInfo
466 HRESULT ITSProtocol_create(IUnknown *pUnkOuter, LPVOID *ppobj)
470 TRACE("(%p %p)\n", pUnkOuter, ppobj);
474 ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ITSProtocol));
476 ret->lpInternetProtocolVtbl = &ITSProtocolVtbl;
477 ret->lpInternetProtocolInfoVtbl = &ITSProtocolInfoVtbl;
480 *ppobj = PROTOCOL(ret);