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 IInternetProtocol IInternetProtocol_iface;
40 IInternetProtocolInfo IInternetProtocolInfo_iface;
45 struct chmFile *chm_file;
46 struct chmUnitInfo chm_object;
49 static inline ITSProtocol *impl_from_IInternetProtocol(IInternetProtocol *iface)
51 return CONTAINING_RECORD(iface, ITSProtocol, IInternetProtocol_iface);
54 static inline ITSProtocol *impl_from_IInternetProtocolInfo(IInternetProtocolInfo *iface)
56 return CONTAINING_RECORD(iface, ITSProtocol, IInternetProtocolInfo_iface);
59 static void release_chm(ITSProtocol *This)
62 chm_close(This->chm_file);
63 This->chm_file = NULL;
68 static HRESULT WINAPI ITSProtocol_QueryInterface(IInternetProtocol *iface, REFIID riid, void **ppv)
70 ITSProtocol *This = impl_from_IInternetProtocol(iface);
73 if(IsEqualGUID(&IID_IUnknown, riid)) {
74 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
75 *ppv = &This->IInternetProtocol_iface;
76 }else if(IsEqualGUID(&IID_IInternetProtocolRoot, riid)) {
77 TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", This, ppv);
78 *ppv = &This->IInternetProtocol_iface;
79 }else if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
80 TRACE("(%p)->(IID_IInternetProtocol %p)\n", This, ppv);
81 *ppv = &This->IInternetProtocol_iface;
82 }else if(IsEqualGUID(&IID_IInternetProtocolInfo, riid)) {
83 TRACE("(%p)->(IID_IInternetProtocolInfo %p)\n", This, ppv);
84 *ppv = &This->IInternetProtocolInfo_iface;
88 IInternetProtocol_AddRef(iface);
92 WARN("not supported interface %s\n", debugstr_guid(riid));
96 static ULONG WINAPI ITSProtocol_AddRef(IInternetProtocol *iface)
98 ITSProtocol *This = impl_from_IInternetProtocol(iface);
99 LONG ref = InterlockedIncrement(&This->ref);
100 TRACE("(%p) ref=%d\n", This, ref);
104 static ULONG WINAPI ITSProtocol_Release(IInternetProtocol *iface)
106 ITSProtocol *This = impl_from_IInternetProtocol(iface);
107 LONG ref = InterlockedDecrement(&This->ref);
109 TRACE("(%p) ref=%d\n", This, ref);
113 HeapFree(GetProcessHeap(), 0, This);
121 static LPCWSTR skip_schema(LPCWSTR url)
123 static const WCHAR its_schema[] = {'i','t','s',':'};
124 static const WCHAR msits_schema[] = {'m','s','-','i','t','s',':'};
125 static const WCHAR mk_schema[] = {'m','k',':','@','M','S','I','T','S','t','o','r','e',':'};
127 if(!strncmpiW(its_schema, url, sizeof(its_schema)/sizeof(WCHAR)))
128 return url+sizeof(its_schema)/sizeof(WCHAR);
129 if(!strncmpiW(msits_schema, url, sizeof(msits_schema)/sizeof(WCHAR)))
130 return url+sizeof(msits_schema)/sizeof(WCHAR);
131 if(!strncmpiW(mk_schema, url, sizeof(mk_schema)/sizeof(WCHAR)))
132 return url+sizeof(mk_schema)/sizeof(WCHAR);
137 static HRESULT report_result(IInternetProtocolSink *sink, HRESULT hres)
139 IInternetProtocolSink_ReportResult(sink, hres, 0, NULL);
143 static HRESULT WINAPI ITSProtocol_Start(IInternetProtocol *iface, LPCWSTR szUrl,
144 IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
145 DWORD grfPI, HANDLE_PTR dwReserved)
147 ITSProtocol *This = impl_from_IInternetProtocol(iface);
149 DWORD bindf = 0, len;
150 LPWSTR file_name, mime, object_name, p;
152 struct chmFile *chm_file;
153 struct chmUnitInfo chm_object;
157 static const WCHAR separator[] = {':',':',0};
159 TRACE("(%p)->(%s %p %p %08x %lx)\n", This, debugstr_w(szUrl), pOIProtSink,
160 pOIBindInfo, grfPI, dwReserved);
162 ptr = skip_schema(szUrl);
164 return INET_E_USE_DEFAULT_PROTOCOLHANDLER;
166 memset(&bindinfo, 0, sizeof(bindinfo));
167 bindinfo.cbSize = sizeof(BINDINFO);
168 hres = IInternetBindInfo_GetBindInfo(pOIBindInfo, &bindf, &bindinfo);
170 WARN("GetBindInfo failed: %08x\n", hres);
174 ReleaseBindInfo(&bindinfo);
176 len = strlenW(ptr)+3;
177 file_name = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
178 memcpy(file_name, ptr, len*sizeof(WCHAR));
179 hres = UrlUnescapeW(file_name, NULL, &len, URL_UNESCAPE_INPLACE);
181 WARN("UrlUnescape failed: %08x\n", hres);
182 HeapFree(GetProcessHeap(), 0, file_name);
186 p = strstrW(file_name, separator);
188 WARN("invalid url\n");
189 HeapFree(GetProcessHeap(), 0, file_name);
190 return report_result(pOIProtSink, STG_E_FILENOTFOUND);
194 chm_file = chm_openW(file_name);
196 WARN("Could not open chm file\n");
197 HeapFree(GetProcessHeap(), 0, file_name);
198 return report_result(pOIProtSink, STG_E_FILENOTFOUND);
202 len = strlenW(object_name);
204 if(*object_name != '/' && *object_name != '\\') {
205 memmove(object_name+1, object_name, (len+1)*sizeof(WCHAR));
210 if(object_name[len-1] == '/')
211 object_name[--len] = 0;
213 for(p=object_name; *p; p++) {
218 TRACE("Resolving %s\n", debugstr_w(object_name));
220 memset(&chm_object, 0, sizeof(chm_object));
221 res = chm_resolve_object(chm_file, object_name, &chm_object);
222 if(res != CHM_RESOLVE_SUCCESS) {
223 WARN("Could not resolve chm object\n");
224 HeapFree(GetProcessHeap(), 0, file_name);
226 return report_result(pOIProtSink, STG_E_FILENOTFOUND);
229 IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_SENDINGREQUEST,
230 strrchrW(object_name, '/')+1);
232 /* FIXME: Native doesn't use FindMimeFromData */
233 hres = FindMimeFromData(NULL, object_name, NULL, 0, NULL, 0, &mime, 0);
234 HeapFree(GetProcessHeap(), 0, file_name);
235 if(SUCCEEDED(hres)) {
236 IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_MIMETYPEAVAILABLE, mime);
240 release_chm(This); /* Native leaks handle here */
241 This->chm_file = chm_file;
242 This->chm_object = chm_object;
244 hres = IInternetProtocolSink_ReportData(pOIProtSink,
245 BSCF_FIRSTDATANOTIFICATION|BSCF_DATAFULLYAVAILABLE,
246 chm_object.length, chm_object.length);
248 WARN("ReportData failed: %08x\n", hres);
250 return report_result(pOIProtSink, hres);
253 hres = IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_BEGINDOWNLOADDATA, NULL);
255 return report_result(pOIProtSink, hres);
258 static HRESULT WINAPI ITSProtocol_Continue(IInternetProtocol *iface, PROTOCOLDATA *pProtocolData)
260 ITSProtocol *This = impl_from_IInternetProtocol(iface);
261 FIXME("(%p)->(%p)\n", This, pProtocolData);
265 static HRESULT WINAPI ITSProtocol_Abort(IInternetProtocol *iface, HRESULT hrReason,
268 ITSProtocol *This = impl_from_IInternetProtocol(iface);
269 FIXME("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);
273 static HRESULT WINAPI ITSProtocol_Terminate(IInternetProtocol *iface, DWORD dwOptions)
275 ITSProtocol *This = impl_from_IInternetProtocol(iface);
277 TRACE("(%p)->(%08x)\n", This, dwOptions);
282 static HRESULT WINAPI ITSProtocol_Suspend(IInternetProtocol *iface)
284 ITSProtocol *This = impl_from_IInternetProtocol(iface);
285 FIXME("(%p)\n", This);
289 static HRESULT WINAPI ITSProtocol_Resume(IInternetProtocol *iface)
291 ITSProtocol *This = impl_from_IInternetProtocol(iface);
292 FIXME("(%p)\n", This);
296 static HRESULT WINAPI ITSProtocol_Read(IInternetProtocol *iface, void *pv,
297 ULONG cb, ULONG *pcbRead)
299 ITSProtocol *This = impl_from_IInternetProtocol(iface);
301 TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
304 return INET_E_DATA_NOT_AVAILABLE;
306 *pcbRead = chm_retrieve_object(This->chm_file, &This->chm_object, pv, This->offset, cb);
307 This->offset += *pcbRead;
309 return *pcbRead ? S_OK : S_FALSE;
312 static HRESULT WINAPI ITSProtocol_Seek(IInternetProtocol *iface, LARGE_INTEGER dlibMove,
313 DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
315 ITSProtocol *This = impl_from_IInternetProtocol(iface);
316 FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
320 static HRESULT WINAPI ITSProtocol_LockRequest(IInternetProtocol *iface, DWORD dwOptions)
322 ITSProtocol *This = impl_from_IInternetProtocol(iface);
324 TRACE("(%p)->(%08x)\n", This, dwOptions);
329 static HRESULT WINAPI ITSProtocol_UnlockRequest(IInternetProtocol *iface)
331 ITSProtocol *This = impl_from_IInternetProtocol(iface);
333 TRACE("(%p)\n", This);
338 static const IInternetProtocolVtbl ITSProtocolVtbl = {
339 ITSProtocol_QueryInterface,
343 ITSProtocol_Continue,
345 ITSProtocol_Terminate,
350 ITSProtocol_LockRequest,
351 ITSProtocol_UnlockRequest
354 static HRESULT WINAPI ITSProtocolInfo_QueryInterface(IInternetProtocolInfo *iface,
355 REFIID riid, void **ppv)
357 ITSProtocol *This = impl_from_IInternetProtocolInfo(iface);
358 return IInternetProtocol_QueryInterface(&This->IInternetProtocol_iface, riid, ppv);
361 static ULONG WINAPI ITSProtocolInfo_AddRef(IInternetProtocolInfo *iface)
363 ITSProtocol *This = impl_from_IInternetProtocolInfo(iface);
364 return IInternetProtocol_AddRef(&This->IInternetProtocol_iface);
367 static ULONG WINAPI ITSProtocolInfo_Release(IInternetProtocolInfo *iface)
369 ITSProtocol *This = impl_from_IInternetProtocolInfo(iface);
370 return IInternetProtocol_Release(&This->IInternetProtocol_iface);
373 static HRESULT WINAPI ITSProtocolInfo_ParseUrl(IInternetProtocolInfo *iface, LPCWSTR pwzUrl,
374 PARSEACTION ParseAction, DWORD dwParseFlags, LPWSTR pwzResult, DWORD cchResult,
375 DWORD *pcchResult, DWORD dwReserved)
377 ITSProtocol *This = impl_from_IInternetProtocolInfo(iface);
379 TRACE("(%p)->(%s %x %08x %p %d %p %d)\n", This, debugstr_w(pwzUrl), ParseAction,
380 dwParseFlags, pwzResult, cchResult, pcchResult, dwReserved);
382 switch(ParseAction) {
383 case PARSE_CANONICALIZE:
384 FIXME("PARSE_CANONICALIZE\n");
386 case PARSE_SECURITY_URL:
387 FIXME("PARSE_SECURITY_URL\n");
390 return INET_E_DEFAULT_ACTION;
396 static HRESULT WINAPI ITSProtocolInfo_CombineUrl(IInternetProtocolInfo *iface,
397 LPCWSTR pwzBaseUrl, LPCWSTR pwzRelativeUrl, DWORD dwCombineFlags, LPWSTR pwzResult,
398 DWORD cchResult, DWORD* pcchResult, DWORD dwReserved)
400 ITSProtocol *This = impl_from_IInternetProtocolInfo(iface);
401 LPCWSTR base_end, ptr;
404 static const WCHAR separator[] = {':',':',0};
406 TRACE("(%p)->(%s %s %08x %p %d %p %d)\n", This, debugstr_w(pwzBaseUrl),
407 debugstr_w(pwzRelativeUrl), dwCombineFlags, pwzResult, cchResult,
408 pcchResult, dwReserved);
410 base_end = strstrW(pwzBaseUrl, separator);
415 if(!skip_schema(pwzBaseUrl))
416 return INET_E_USE_DEFAULT_PROTOCOLHANDLER;
418 if(strchrW(pwzRelativeUrl, ':'))
419 return STG_E_INVALIDNAME;
421 if(pwzRelativeUrl[0] != '/') {
422 ptr = strrchrW(base_end, '/');
426 base_end += strlenW(base_end);
429 rel_len = strlenW(pwzRelativeUrl)+1;
431 *pcchResult = rel_len + (base_end-pwzBaseUrl);
433 if(*pcchResult > cchResult)
434 return E_OUTOFMEMORY;
436 memcpy(pwzResult, pwzBaseUrl, (base_end-pwzBaseUrl)*sizeof(WCHAR));
437 strcpyW(pwzResult + (base_end-pwzBaseUrl), pwzRelativeUrl);
442 static HRESULT WINAPI ITSProtocolInfo_CompareUrl(IInternetProtocolInfo *iface, LPCWSTR pwzUrl1,
443 LPCWSTR pwzUrl2, DWORD dwCompareFlags)
445 ITSProtocol *This = impl_from_IInternetProtocolInfo(iface);
446 FIXME("%p)->(%s %s %08x)\n", This, debugstr_w(pwzUrl1), debugstr_w(pwzUrl2), dwCompareFlags);
450 static HRESULT WINAPI ITSProtocolInfo_QueryInfo(IInternetProtocolInfo *iface, LPCWSTR pwzUrl,
451 QUERYOPTION QueryOption, DWORD dwQueryFlags, LPVOID pBuffer, DWORD cbBuffer, DWORD* pcbBuf,
454 ITSProtocol *This = impl_from_IInternetProtocolInfo(iface);
455 FIXME("(%p)->(%s %08x %08x %p %d %p %d)\n", This, debugstr_w(pwzUrl), QueryOption,
456 dwQueryFlags, pBuffer, cbBuffer, pcbBuf, dwReserved);
460 static const IInternetProtocolInfoVtbl ITSProtocolInfoVtbl = {
461 ITSProtocolInfo_QueryInterface,
462 ITSProtocolInfo_AddRef,
463 ITSProtocolInfo_Release,
464 ITSProtocolInfo_ParseUrl,
465 ITSProtocolInfo_CombineUrl,
466 ITSProtocolInfo_CompareUrl,
467 ITSProtocolInfo_QueryInfo
470 HRESULT ITSProtocol_create(IUnknown *pUnkOuter, LPVOID *ppobj)
474 TRACE("(%p %p)\n", pUnkOuter, ppobj);
478 ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ITSProtocol));
480 ret->IInternetProtocol_iface.lpVtbl = &ITSProtocolVtbl;
481 ret->IInternetProtocolInfo_iface.lpVtbl = &ITSProtocolInfoVtbl;
484 *ppobj = &ret->IInternetProtocol_iface;