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 /* Adopted from urlmon */
138 static void remove_dot_segments(WCHAR
*path
) {
139 const WCHAR
*in
= path
;
143 /* Move the first path segment in the input buffer to the end of
144 * the output buffer, and any subsequent characters up to, including
145 * the next "/" character (if any) or the end of the input buffer.
148 if(!(*out
++ = *in
++))
158 /* Handle ending "/." */
170 /* If we don't have "/../" or ending "/.." */
171 if(in
[1] != '.' || (in
[2] && in
[2] != '/'))
176 /* Find the slash preceding out pointer and move out pointer to it */
177 if(out
> path
+1 && *--out
== '/')
179 while(out
> path
&& *(--out
) != '/');
186 static HRESULT
report_result(IInternetProtocolSink
*sink
, HRESULT hres
)
188 IInternetProtocolSink_ReportResult(sink
, hres
, 0, NULL
);
192 static HRESULT WINAPI
ITSProtocol_Start(IInternetProtocol
*iface
, LPCWSTR szUrl
,
193 IInternetProtocolSink
*pOIProtSink
, IInternetBindInfo
*pOIBindInfo
,
194 DWORD grfPI
, HANDLE_PTR dwReserved
)
196 ITSProtocol
*This
= impl_from_IInternetProtocol(iface
);
198 DWORD bindf
= 0, len
;
199 LPWSTR file_name
, mime
, object_name
, p
;
201 struct chmFile
*chm_file
;
202 struct chmUnitInfo chm_object
;
206 static const WCHAR separator
[] = {':',':',0};
208 TRACE("(%p)->(%s %p %p %08x %lx)\n", This
, debugstr_w(szUrl
), pOIProtSink
,
209 pOIBindInfo
, grfPI
, dwReserved
);
211 ptr
= skip_schema(szUrl
);
213 return INET_E_USE_DEFAULT_PROTOCOLHANDLER
;
215 memset(&bindinfo
, 0, sizeof(bindinfo
));
216 bindinfo
.cbSize
= sizeof(BINDINFO
);
217 hres
= IInternetBindInfo_GetBindInfo(pOIBindInfo
, &bindf
, &bindinfo
);
219 WARN("GetBindInfo failed: %08x\n", hres
);
223 ReleaseBindInfo(&bindinfo
);
225 len
= strlenW(ptr
)+3;
226 file_name
= HeapAlloc(GetProcessHeap(), 0, len
*sizeof(WCHAR
));
227 memcpy(file_name
, ptr
, len
*sizeof(WCHAR
));
228 hres
= UrlUnescapeW(file_name
, NULL
, &len
, URL_UNESCAPE_INPLACE
);
230 WARN("UrlUnescape failed: %08x\n", hres
);
231 HeapFree(GetProcessHeap(), 0, file_name
);
235 p
= strstrW(file_name
, separator
);
237 WARN("invalid url\n");
238 HeapFree(GetProcessHeap(), 0, file_name
);
239 return report_result(pOIProtSink
, STG_E_FILENOTFOUND
);
243 chm_file
= chm_openW(file_name
);
245 WARN("Could not open chm file\n");
246 HeapFree(GetProcessHeap(), 0, file_name
);
247 return report_result(pOIProtSink
, STG_E_FILENOTFOUND
);
251 len
= strlenW(object_name
);
253 if(*object_name
!= '/' && *object_name
!= '\\') {
254 memmove(object_name
+1, object_name
, (len
+1)*sizeof(WCHAR
));
259 if(object_name
[len
-1] == '/')
260 object_name
[--len
] = 0;
262 for(p
=object_name
; *p
; p
++) {
267 remove_dot_segments(object_name
);
269 TRACE("Resolving %s\n", debugstr_w(object_name
));
271 memset(&chm_object
, 0, sizeof(chm_object
));
272 res
= chm_resolve_object(chm_file
, object_name
, &chm_object
);
273 if(res
!= CHM_RESOLVE_SUCCESS
) {
274 WARN("Could not resolve chm object\n");
275 HeapFree(GetProcessHeap(), 0, file_name
);
277 return report_result(pOIProtSink
, STG_E_FILENOTFOUND
);
280 IInternetProtocolSink_ReportProgress(pOIProtSink
, BINDSTATUS_SENDINGREQUEST
,
281 strrchrW(object_name
, '/')+1);
283 /* FIXME: Native doesn't use FindMimeFromData */
284 hres
= FindMimeFromData(NULL
, object_name
, NULL
, 0, NULL
, 0, &mime
, 0);
285 HeapFree(GetProcessHeap(), 0, file_name
);
286 if(SUCCEEDED(hres
)) {
287 IInternetProtocolSink_ReportProgress(pOIProtSink
, BINDSTATUS_MIMETYPEAVAILABLE
, mime
);
291 release_chm(This
); /* Native leaks handle here */
292 This
->chm_file
= chm_file
;
293 This
->chm_object
= chm_object
;
295 hres
= IInternetProtocolSink_ReportData(pOIProtSink
,
296 BSCF_FIRSTDATANOTIFICATION
|BSCF_DATAFULLYAVAILABLE
,
297 chm_object
.length
, chm_object
.length
);
299 WARN("ReportData failed: %08x\n", hres
);
301 return report_result(pOIProtSink
, hres
);
304 hres
= IInternetProtocolSink_ReportProgress(pOIProtSink
, BINDSTATUS_BEGINDOWNLOADDATA
, NULL
);
306 return report_result(pOIProtSink
, hres
);
309 static HRESULT WINAPI
ITSProtocol_Continue(IInternetProtocol
*iface
, PROTOCOLDATA
*pProtocolData
)
311 ITSProtocol
*This
= impl_from_IInternetProtocol(iface
);
312 FIXME("(%p)->(%p)\n", This
, pProtocolData
);
316 static HRESULT WINAPI
ITSProtocol_Abort(IInternetProtocol
*iface
, HRESULT hrReason
,
319 ITSProtocol
*This
= impl_from_IInternetProtocol(iface
);
320 FIXME("(%p)->(%08x %08x)\n", This
, hrReason
, dwOptions
);
324 static HRESULT WINAPI
ITSProtocol_Terminate(IInternetProtocol
*iface
, DWORD dwOptions
)
326 ITSProtocol
*This
= impl_from_IInternetProtocol(iface
);
328 TRACE("(%p)->(%08x)\n", This
, dwOptions
);
333 static HRESULT WINAPI
ITSProtocol_Suspend(IInternetProtocol
*iface
)
335 ITSProtocol
*This
= impl_from_IInternetProtocol(iface
);
336 FIXME("(%p)\n", This
);
340 static HRESULT WINAPI
ITSProtocol_Resume(IInternetProtocol
*iface
)
342 ITSProtocol
*This
= impl_from_IInternetProtocol(iface
);
343 FIXME("(%p)\n", This
);
347 static HRESULT WINAPI
ITSProtocol_Read(IInternetProtocol
*iface
, void *pv
,
348 ULONG cb
, ULONG
*pcbRead
)
350 ITSProtocol
*This
= impl_from_IInternetProtocol(iface
);
352 TRACE("(%p)->(%p %u %p)\n", This
, pv
, cb
, pcbRead
);
355 return INET_E_DATA_NOT_AVAILABLE
;
357 *pcbRead
= chm_retrieve_object(This
->chm_file
, &This
->chm_object
, pv
, This
->offset
, cb
);
358 This
->offset
+= *pcbRead
;
360 return *pcbRead
? S_OK
: S_FALSE
;
363 static HRESULT WINAPI
ITSProtocol_Seek(IInternetProtocol
*iface
, LARGE_INTEGER dlibMove
,
364 DWORD dwOrigin
, ULARGE_INTEGER
*plibNewPosition
)
366 ITSProtocol
*This
= impl_from_IInternetProtocol(iface
);
367 FIXME("(%p)->(%d %d %p)\n", This
, dlibMove
.u
.LowPart
, dwOrigin
, plibNewPosition
);
371 static HRESULT WINAPI
ITSProtocol_LockRequest(IInternetProtocol
*iface
, DWORD dwOptions
)
373 ITSProtocol
*This
= impl_from_IInternetProtocol(iface
);
375 TRACE("(%p)->(%08x)\n", This
, dwOptions
);
380 static HRESULT WINAPI
ITSProtocol_UnlockRequest(IInternetProtocol
*iface
)
382 ITSProtocol
*This
= impl_from_IInternetProtocol(iface
);
384 TRACE("(%p)\n", This
);
389 static const IInternetProtocolVtbl ITSProtocolVtbl
= {
390 ITSProtocol_QueryInterface
,
394 ITSProtocol_Continue
,
396 ITSProtocol_Terminate
,
401 ITSProtocol_LockRequest
,
402 ITSProtocol_UnlockRequest
405 static HRESULT WINAPI
ITSProtocolInfo_QueryInterface(IInternetProtocolInfo
*iface
,
406 REFIID riid
, void **ppv
)
408 ITSProtocol
*This
= impl_from_IInternetProtocolInfo(iface
);
409 return IInternetProtocol_QueryInterface(&This
->IInternetProtocol_iface
, riid
, ppv
);
412 static ULONG WINAPI
ITSProtocolInfo_AddRef(IInternetProtocolInfo
*iface
)
414 ITSProtocol
*This
= impl_from_IInternetProtocolInfo(iface
);
415 return IInternetProtocol_AddRef(&This
->IInternetProtocol_iface
);
418 static ULONG WINAPI
ITSProtocolInfo_Release(IInternetProtocolInfo
*iface
)
420 ITSProtocol
*This
= impl_from_IInternetProtocolInfo(iface
);
421 return IInternetProtocol_Release(&This
->IInternetProtocol_iface
);
424 static HRESULT WINAPI
ITSProtocolInfo_ParseUrl(IInternetProtocolInfo
*iface
, LPCWSTR pwzUrl
,
425 PARSEACTION ParseAction
, DWORD dwParseFlags
, LPWSTR pwzResult
, DWORD cchResult
,
426 DWORD
*pcchResult
, DWORD dwReserved
)
428 ITSProtocol
*This
= impl_from_IInternetProtocolInfo(iface
);
430 TRACE("(%p)->(%s %x %08x %p %d %p %d)\n", This
, debugstr_w(pwzUrl
), ParseAction
,
431 dwParseFlags
, pwzResult
, cchResult
, pcchResult
, dwReserved
);
433 switch(ParseAction
) {
434 case PARSE_CANONICALIZE
:
435 FIXME("PARSE_CANONICALIZE\n");
437 case PARSE_SECURITY_URL
:
438 FIXME("PARSE_SECURITY_URL\n");
441 return INET_E_DEFAULT_ACTION
;
447 static HRESULT WINAPI
ITSProtocolInfo_CombineUrl(IInternetProtocolInfo
*iface
,
448 LPCWSTR pwzBaseUrl
, LPCWSTR pwzRelativeUrl
, DWORD dwCombineFlags
, LPWSTR pwzResult
,
449 DWORD cchResult
, DWORD
* pcchResult
, DWORD dwReserved
)
451 ITSProtocol
*This
= impl_from_IInternetProtocolInfo(iface
);
452 LPCWSTR base_end
, ptr
;
455 static const WCHAR separator
[] = {':',':',0};
457 TRACE("(%p)->(%s %s %08x %p %d %p %d)\n", This
, debugstr_w(pwzBaseUrl
),
458 debugstr_w(pwzRelativeUrl
), dwCombineFlags
, pwzResult
, cchResult
,
459 pcchResult
, dwReserved
);
461 base_end
= strstrW(pwzBaseUrl
, separator
);
466 if(!skip_schema(pwzBaseUrl
))
467 return INET_E_USE_DEFAULT_PROTOCOLHANDLER
;
469 if(strchrW(pwzRelativeUrl
, ':'))
470 return STG_E_INVALIDNAME
;
472 if(pwzRelativeUrl
[0] == '#') {
473 base_end
+= strlenW(base_end
);
474 }else if(pwzRelativeUrl
[0] != '/') {
475 ptr
= strrchrW(base_end
, '/');
479 base_end
+= strlenW(base_end
);
482 rel_len
= strlenW(pwzRelativeUrl
)+1;
484 *pcchResult
= rel_len
+ (base_end
-pwzBaseUrl
);
486 if(*pcchResult
> cchResult
)
487 return E_OUTOFMEMORY
;
489 memcpy(pwzResult
, pwzBaseUrl
, (base_end
-pwzBaseUrl
)*sizeof(WCHAR
));
490 strcpyW(pwzResult
+ (base_end
-pwzBaseUrl
), pwzRelativeUrl
);
495 static HRESULT WINAPI
ITSProtocolInfo_CompareUrl(IInternetProtocolInfo
*iface
, LPCWSTR pwzUrl1
,
496 LPCWSTR pwzUrl2
, DWORD dwCompareFlags
)
498 ITSProtocol
*This
= impl_from_IInternetProtocolInfo(iface
);
499 FIXME("%p)->(%s %s %08x)\n", This
, debugstr_w(pwzUrl1
), debugstr_w(pwzUrl2
), dwCompareFlags
);
503 static HRESULT WINAPI
ITSProtocolInfo_QueryInfo(IInternetProtocolInfo
*iface
, LPCWSTR pwzUrl
,
504 QUERYOPTION QueryOption
, DWORD dwQueryFlags
, LPVOID pBuffer
, DWORD cbBuffer
, DWORD
* pcbBuf
,
507 ITSProtocol
*This
= impl_from_IInternetProtocolInfo(iface
);
508 FIXME("(%p)->(%s %08x %08x %p %d %p %d)\n", This
, debugstr_w(pwzUrl
), QueryOption
,
509 dwQueryFlags
, pBuffer
, cbBuffer
, pcbBuf
, dwReserved
);
513 static const IInternetProtocolInfoVtbl ITSProtocolInfoVtbl
= {
514 ITSProtocolInfo_QueryInterface
,
515 ITSProtocolInfo_AddRef
,
516 ITSProtocolInfo_Release
,
517 ITSProtocolInfo_ParseUrl
,
518 ITSProtocolInfo_CombineUrl
,
519 ITSProtocolInfo_CompareUrl
,
520 ITSProtocolInfo_QueryInfo
523 HRESULT
ITSProtocol_create(IUnknown
*pUnkOuter
, LPVOID
*ppobj
)
527 TRACE("(%p %p)\n", pUnkOuter
, ppobj
);
531 ret
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(ITSProtocol
));
533 ret
->IInternetProtocol_iface
.lpVtbl
= &ITSProtocolVtbl
;
534 ret
->IInternetProtocolInfo_iface
.lpVtbl
= &ITSProtocolInfoVtbl
;
537 *ppobj
= &ret
->IInternetProtocol_iface
;