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 IUnknown IUnknown_inner
;
40 IInternetProtocol IInternetProtocol_iface
;
41 IInternetProtocolInfo IInternetProtocolInfo_iface
;
47 struct chmFile
*chm_file
;
48 struct chmUnitInfo chm_object
;
51 static inline ITSProtocol
*impl_from_IUnknown(IUnknown
*iface
)
53 return CONTAINING_RECORD(iface
, ITSProtocol
, IUnknown_inner
);
56 static inline ITSProtocol
*impl_from_IInternetProtocol(IInternetProtocol
*iface
)
58 return CONTAINING_RECORD(iface
, ITSProtocol
, IInternetProtocol_iface
);
61 static inline ITSProtocol
*impl_from_IInternetProtocolInfo(IInternetProtocolInfo
*iface
)
63 return CONTAINING_RECORD(iface
, ITSProtocol
, IInternetProtocolInfo_iface
);
66 static void release_chm(ITSProtocol
*This
)
69 chm_close(This
->chm_file
);
70 This
->chm_file
= NULL
;
75 static HRESULT WINAPI
ITSProtocol_QueryInterface(IUnknown
*iface
, REFIID riid
, void **ppv
)
77 ITSProtocol
*This
= impl_from_IUnknown(iface
);
79 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
80 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
81 *ppv
= &This
->IUnknown_inner
;
82 }else if(IsEqualGUID(&IID_IInternetProtocolRoot
, riid
)) {
83 TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", This
, ppv
);
84 *ppv
= &This
->IInternetProtocol_iface
;
85 }else if(IsEqualGUID(&IID_IInternetProtocol
, riid
)) {
86 TRACE("(%p)->(IID_IInternetProtocol %p)\n", This
, ppv
);
87 *ppv
= &This
->IInternetProtocol_iface
;
88 }else if(IsEqualGUID(&IID_IInternetProtocolInfo
, riid
)) {
89 TRACE("(%p)->(IID_IInternetProtocolInfo %p)\n", This
, ppv
);
90 *ppv
= &This
->IInternetProtocolInfo_iface
;
93 WARN("not supported interface %s\n", debugstr_guid(riid
));
97 IUnknown_AddRef((IUnknown
*)*ppv
);
101 static ULONG WINAPI
ITSProtocol_AddRef(IUnknown
*iface
)
103 ITSProtocol
*This
= impl_from_IUnknown(iface
);
104 LONG ref
= InterlockedIncrement(&This
->ref
);
105 TRACE("(%p) ref=%d\n", This
, ref
);
109 static ULONG WINAPI
ITSProtocol_Release(IUnknown
*iface
)
111 ITSProtocol
*This
= impl_from_IUnknown(iface
);
112 LONG ref
= InterlockedDecrement(&This
->ref
);
114 TRACE("(%p) ref=%d\n", This
, ref
);
118 HeapFree(GetProcessHeap(), 0, This
);
126 static const IUnknownVtbl ITSProtocolUnkVtbl
= {
127 ITSProtocol_QueryInterface
,
132 static HRESULT WINAPI
ITSInternetProtocol_QueryInterface(IInternetProtocol
*iface
, REFIID riid
, void **ppv
)
134 ITSProtocol
*This
= impl_from_IInternetProtocol(iface
);
135 return IUnknown_QueryInterface(This
->outer
, riid
, ppv
);
138 static ULONG WINAPI
ITSInternetProtocol_AddRef(IInternetProtocol
*iface
)
140 ITSProtocol
*This
= impl_from_IInternetProtocol(iface
);
141 return IUnknown_AddRef(This
->outer
);
144 static ULONG WINAPI
ITSInternetProtocol_Release(IInternetProtocol
*iface
)
146 ITSProtocol
*This
= impl_from_IInternetProtocol(iface
);
147 return IUnknown_Release(This
->outer
);
150 static LPCWSTR
skip_schema(LPCWSTR url
)
152 static const WCHAR its_schema
[] = {'i','t','s',':'};
153 static const WCHAR msits_schema
[] = {'m','s','-','i','t','s',':'};
154 static const WCHAR mk_schema
[] = {'m','k',':','@','M','S','I','T','S','t','o','r','e',':'};
156 if(!strncmpiW(its_schema
, url
, sizeof(its_schema
)/sizeof(WCHAR
)))
157 return url
+sizeof(its_schema
)/sizeof(WCHAR
);
158 if(!strncmpiW(msits_schema
, url
, sizeof(msits_schema
)/sizeof(WCHAR
)))
159 return url
+sizeof(msits_schema
)/sizeof(WCHAR
);
160 if(!strncmpiW(mk_schema
, url
, sizeof(mk_schema
)/sizeof(WCHAR
)))
161 return url
+sizeof(mk_schema
)/sizeof(WCHAR
);
166 /* Adopted from urlmon */
167 static void remove_dot_segments(WCHAR
*path
) {
168 const WCHAR
*in
= path
;
172 /* Move the first path segment in the input buffer to the end of
173 * the output buffer, and any subsequent characters up to, including
174 * the next "/" character (if any) or the end of the input buffer.
177 if(!(*out
++ = *in
++))
187 /* Handle ending "/." */
199 /* If we don't have "/../" or ending "/.." */
200 if(in
[1] != '.' || (in
[2] && in
[2] != '/'))
205 /* Find the slash preceding out pointer and move out pointer to it */
206 if(out
> path
+1 && *--out
== '/')
208 while(out
> path
&& *(--out
) != '/');
215 static HRESULT
report_result(IInternetProtocolSink
*sink
, HRESULT hres
)
217 IInternetProtocolSink_ReportResult(sink
, hres
, 0, NULL
);
221 static HRESULT WINAPI
ITSProtocol_Start(IInternetProtocol
*iface
, LPCWSTR szUrl
,
222 IInternetProtocolSink
*pOIProtSink
, IInternetBindInfo
*pOIBindInfo
,
223 DWORD grfPI
, HANDLE_PTR dwReserved
)
225 ITSProtocol
*This
= impl_from_IInternetProtocol(iface
);
227 DWORD bindf
= 0, len
;
228 LPWSTR file_name
, mime
, object_name
, p
;
230 struct chmFile
*chm_file
;
231 struct chmUnitInfo chm_object
;
235 static const WCHAR separator
[] = {':',':',0};
237 TRACE("(%p)->(%s %p %p %08x %lx)\n", This
, debugstr_w(szUrl
), pOIProtSink
,
238 pOIBindInfo
, grfPI
, dwReserved
);
240 ptr
= skip_schema(szUrl
);
242 return INET_E_USE_DEFAULT_PROTOCOLHANDLER
;
244 memset(&bindinfo
, 0, sizeof(bindinfo
));
245 bindinfo
.cbSize
= sizeof(BINDINFO
);
246 hres
= IInternetBindInfo_GetBindInfo(pOIBindInfo
, &bindf
, &bindinfo
);
248 WARN("GetBindInfo failed: %08x\n", hres
);
252 ReleaseBindInfo(&bindinfo
);
254 len
= strlenW(ptr
)+3;
255 file_name
= HeapAlloc(GetProcessHeap(), 0, len
*sizeof(WCHAR
));
256 memcpy(file_name
, ptr
, len
*sizeof(WCHAR
));
257 hres
= UrlUnescapeW(file_name
, NULL
, &len
, URL_UNESCAPE_INPLACE
);
259 WARN("UrlUnescape failed: %08x\n", hres
);
260 HeapFree(GetProcessHeap(), 0, file_name
);
264 p
= strstrW(file_name
, separator
);
266 WARN("invalid url\n");
267 HeapFree(GetProcessHeap(), 0, file_name
);
268 return report_result(pOIProtSink
, STG_E_FILENOTFOUND
);
272 chm_file
= chm_openW(file_name
);
274 WARN("Could not open chm file\n");
275 HeapFree(GetProcessHeap(), 0, file_name
);
276 return report_result(pOIProtSink
, STG_E_FILENOTFOUND
);
280 len
= strlenW(object_name
);
282 if(*object_name
!= '/' && *object_name
!= '\\') {
283 memmove(object_name
+1, object_name
, (len
+1)*sizeof(WCHAR
));
288 if(object_name
[len
-1] == '/')
289 object_name
[--len
] = 0;
291 for(p
=object_name
; *p
; p
++) {
296 remove_dot_segments(object_name
);
298 TRACE("Resolving %s\n", debugstr_w(object_name
));
300 memset(&chm_object
, 0, sizeof(chm_object
));
301 res
= chm_resolve_object(chm_file
, object_name
, &chm_object
);
302 if(res
!= CHM_RESOLVE_SUCCESS
) {
303 WARN("Could not resolve chm object\n");
304 HeapFree(GetProcessHeap(), 0, file_name
);
306 return report_result(pOIProtSink
, STG_E_FILENOTFOUND
);
309 IInternetProtocolSink_ReportProgress(pOIProtSink
, BINDSTATUS_SENDINGREQUEST
,
310 strrchrW(object_name
, '/')+1);
312 /* FIXME: Native doesn't use FindMimeFromData */
313 hres
= FindMimeFromData(NULL
, object_name
, NULL
, 0, NULL
, 0, &mime
, 0);
314 HeapFree(GetProcessHeap(), 0, file_name
);
315 if(SUCCEEDED(hres
)) {
316 IInternetProtocolSink_ReportProgress(pOIProtSink
, BINDSTATUS_MIMETYPEAVAILABLE
, mime
);
320 release_chm(This
); /* Native leaks handle here */
321 This
->chm_file
= chm_file
;
322 This
->chm_object
= chm_object
;
324 hres
= IInternetProtocolSink_ReportData(pOIProtSink
,
325 BSCF_FIRSTDATANOTIFICATION
|BSCF_DATAFULLYAVAILABLE
,
326 chm_object
.length
, chm_object
.length
);
328 WARN("ReportData failed: %08x\n", hres
);
330 return report_result(pOIProtSink
, hres
);
333 hres
= IInternetProtocolSink_ReportProgress(pOIProtSink
, BINDSTATUS_BEGINDOWNLOADDATA
, NULL
);
335 return report_result(pOIProtSink
, hres
);
338 static HRESULT WINAPI
ITSProtocol_Continue(IInternetProtocol
*iface
, PROTOCOLDATA
*pProtocolData
)
340 ITSProtocol
*This
= impl_from_IInternetProtocol(iface
);
341 FIXME("(%p)->(%p)\n", This
, pProtocolData
);
345 static HRESULT WINAPI
ITSProtocol_Abort(IInternetProtocol
*iface
, HRESULT hrReason
,
348 ITSProtocol
*This
= impl_from_IInternetProtocol(iface
);
349 FIXME("(%p)->(%08x %08x)\n", This
, hrReason
, dwOptions
);
353 static HRESULT WINAPI
ITSProtocol_Terminate(IInternetProtocol
*iface
, DWORD dwOptions
)
355 ITSProtocol
*This
= impl_from_IInternetProtocol(iface
);
357 TRACE("(%p)->(%08x)\n", This
, dwOptions
);
362 static HRESULT WINAPI
ITSProtocol_Suspend(IInternetProtocol
*iface
)
364 ITSProtocol
*This
= impl_from_IInternetProtocol(iface
);
365 FIXME("(%p)\n", This
);
369 static HRESULT WINAPI
ITSProtocol_Resume(IInternetProtocol
*iface
)
371 ITSProtocol
*This
= impl_from_IInternetProtocol(iface
);
372 FIXME("(%p)\n", This
);
376 static HRESULT WINAPI
ITSProtocol_Read(IInternetProtocol
*iface
, void *pv
,
377 ULONG cb
, ULONG
*pcbRead
)
379 ITSProtocol
*This
= impl_from_IInternetProtocol(iface
);
381 TRACE("(%p)->(%p %u %p)\n", This
, pv
, cb
, pcbRead
);
384 return INET_E_DATA_NOT_AVAILABLE
;
386 *pcbRead
= chm_retrieve_object(This
->chm_file
, &This
->chm_object
, pv
, This
->offset
, cb
);
387 This
->offset
+= *pcbRead
;
389 return *pcbRead
? S_OK
: S_FALSE
;
392 static HRESULT WINAPI
ITSProtocol_Seek(IInternetProtocol
*iface
, LARGE_INTEGER dlibMove
,
393 DWORD dwOrigin
, ULARGE_INTEGER
*plibNewPosition
)
395 ITSProtocol
*This
= impl_from_IInternetProtocol(iface
);
396 FIXME("(%p)->(%d %d %p)\n", This
, dlibMove
.u
.LowPart
, dwOrigin
, plibNewPosition
);
400 static HRESULT WINAPI
ITSProtocol_LockRequest(IInternetProtocol
*iface
, DWORD dwOptions
)
402 ITSProtocol
*This
= impl_from_IInternetProtocol(iface
);
404 TRACE("(%p)->(%08x)\n", This
, dwOptions
);
409 static HRESULT WINAPI
ITSProtocol_UnlockRequest(IInternetProtocol
*iface
)
411 ITSProtocol
*This
= impl_from_IInternetProtocol(iface
);
413 TRACE("(%p)\n", This
);
418 static const IInternetProtocolVtbl ITSProtocolVtbl
= {
419 ITSInternetProtocol_QueryInterface
,
420 ITSInternetProtocol_AddRef
,
421 ITSInternetProtocol_Release
,
423 ITSProtocol_Continue
,
425 ITSProtocol_Terminate
,
430 ITSProtocol_LockRequest
,
431 ITSProtocol_UnlockRequest
434 static HRESULT WINAPI
ITSProtocolInfo_QueryInterface(IInternetProtocolInfo
*iface
,
435 REFIID riid
, void **ppv
)
437 ITSProtocol
*This
= impl_from_IInternetProtocolInfo(iface
);
438 return IInternetProtocol_QueryInterface(&This
->IInternetProtocol_iface
, riid
, ppv
);
441 static ULONG WINAPI
ITSProtocolInfo_AddRef(IInternetProtocolInfo
*iface
)
443 ITSProtocol
*This
= impl_from_IInternetProtocolInfo(iface
);
444 return IInternetProtocol_AddRef(&This
->IInternetProtocol_iface
);
447 static ULONG WINAPI
ITSProtocolInfo_Release(IInternetProtocolInfo
*iface
)
449 ITSProtocol
*This
= impl_from_IInternetProtocolInfo(iface
);
450 return IInternetProtocol_Release(&This
->IInternetProtocol_iface
);
453 static HRESULT WINAPI
ITSProtocolInfo_ParseUrl(IInternetProtocolInfo
*iface
, LPCWSTR pwzUrl
,
454 PARSEACTION ParseAction
, DWORD dwParseFlags
, LPWSTR pwzResult
, DWORD cchResult
,
455 DWORD
*pcchResult
, DWORD dwReserved
)
457 ITSProtocol
*This
= impl_from_IInternetProtocolInfo(iface
);
459 TRACE("(%p)->(%s %x %08x %p %d %p %d)\n", This
, debugstr_w(pwzUrl
), ParseAction
,
460 dwParseFlags
, pwzResult
, cchResult
, pcchResult
, dwReserved
);
462 switch(ParseAction
) {
463 case PARSE_CANONICALIZE
:
464 FIXME("PARSE_CANONICALIZE\n");
466 case PARSE_SECURITY_URL
:
467 FIXME("PARSE_SECURITY_URL\n");
470 return INET_E_DEFAULT_ACTION
;
476 static HRESULT WINAPI
ITSProtocolInfo_CombineUrl(IInternetProtocolInfo
*iface
,
477 LPCWSTR pwzBaseUrl
, LPCWSTR pwzRelativeUrl
, DWORD dwCombineFlags
, LPWSTR pwzResult
,
478 DWORD cchResult
, DWORD
* pcchResult
, DWORD dwReserved
)
480 ITSProtocol
*This
= impl_from_IInternetProtocolInfo(iface
);
481 LPCWSTR base_end
, ptr
;
484 static const WCHAR separator
[] = {':',':',0};
486 TRACE("(%p)->(%s %s %08x %p %d %p %d)\n", This
, debugstr_w(pwzBaseUrl
),
487 debugstr_w(pwzRelativeUrl
), dwCombineFlags
, pwzResult
, cchResult
,
488 pcchResult
, dwReserved
);
490 base_end
= strstrW(pwzBaseUrl
, separator
);
495 if(!skip_schema(pwzBaseUrl
))
496 return INET_E_USE_DEFAULT_PROTOCOLHANDLER
;
498 if(strchrW(pwzRelativeUrl
, ':'))
499 return STG_E_INVALIDNAME
;
501 if(pwzRelativeUrl
[0] == '#') {
502 base_end
+= strlenW(base_end
);
503 }else if(pwzRelativeUrl
[0] != '/') {
504 ptr
= strrchrW(base_end
, '/');
508 base_end
+= strlenW(base_end
);
511 rel_len
= strlenW(pwzRelativeUrl
)+1;
513 *pcchResult
= rel_len
+ (base_end
-pwzBaseUrl
);
515 if(*pcchResult
> cchResult
)
516 return E_OUTOFMEMORY
;
518 memcpy(pwzResult
, pwzBaseUrl
, (base_end
-pwzBaseUrl
)*sizeof(WCHAR
));
519 strcpyW(pwzResult
+ (base_end
-pwzBaseUrl
), pwzRelativeUrl
);
524 static HRESULT WINAPI
ITSProtocolInfo_CompareUrl(IInternetProtocolInfo
*iface
, LPCWSTR pwzUrl1
,
525 LPCWSTR pwzUrl2
, DWORD dwCompareFlags
)
527 ITSProtocol
*This
= impl_from_IInternetProtocolInfo(iface
);
528 FIXME("%p)->(%s %s %08x)\n", This
, debugstr_w(pwzUrl1
), debugstr_w(pwzUrl2
), dwCompareFlags
);
532 static HRESULT WINAPI
ITSProtocolInfo_QueryInfo(IInternetProtocolInfo
*iface
, LPCWSTR pwzUrl
,
533 QUERYOPTION QueryOption
, DWORD dwQueryFlags
, LPVOID pBuffer
, DWORD cbBuffer
, DWORD
* pcbBuf
,
536 ITSProtocol
*This
= impl_from_IInternetProtocolInfo(iface
);
537 FIXME("(%p)->(%s %08x %08x %p %d %p %d)\n", This
, debugstr_w(pwzUrl
), QueryOption
,
538 dwQueryFlags
, pBuffer
, cbBuffer
, pcbBuf
, dwReserved
);
542 static const IInternetProtocolInfoVtbl ITSProtocolInfoVtbl
= {
543 ITSProtocolInfo_QueryInterface
,
544 ITSProtocolInfo_AddRef
,
545 ITSProtocolInfo_Release
,
546 ITSProtocolInfo_ParseUrl
,
547 ITSProtocolInfo_CombineUrl
,
548 ITSProtocolInfo_CompareUrl
,
549 ITSProtocolInfo_QueryInfo
552 HRESULT
ITSProtocol_create(IUnknown
*outer
, void **ppv
)
556 TRACE("(%p %p)\n", outer
, ppv
);
560 ret
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(ITSProtocol
));
562 return E_OUTOFMEMORY
;
564 ret
->IUnknown_inner
.lpVtbl
= &ITSProtocolUnkVtbl
;
565 ret
->IInternetProtocol_iface
.lpVtbl
= &ITSProtocolVtbl
;
566 ret
->IInternetProtocolInfo_iface
.lpVtbl
= &ITSProtocolInfoVtbl
;
568 ret
->outer
= outer
? outer
: &ret
->IUnknown_inner
;
570 *ppv
= &ret
->IUnknown_inner
;