2 * Copyright 2006-2010 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
24 #define NONAMELESSUNION
25 #define NONAMELESSSTRUCT
37 #include "shdeprecated.h"
39 #include "wine/debug.h"
41 #include "mshtml_private.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(mshtml
);
46 #define CONTENT_LENGTH "Content-Length"
47 #define UTF8_STR "utf-8"
48 #define UTF16_STR "utf-16"
50 static const WCHAR emptyW
[] = {0};
52 struct nsProtocolStream
{
53 nsIInputStream nsIInputStream_iface
;
61 struct BSCallbackVtbl
{
62 void (*destroy
)(BSCallback
*);
63 HRESULT (*init_bindinfo
)(BSCallback
*);
64 HRESULT (*start_binding
)(BSCallback
*);
65 HRESULT (*stop_binding
)(BSCallback
*,HRESULT
);
66 HRESULT (*read_data
)(BSCallback
*,IStream
*);
67 HRESULT (*on_progress
)(BSCallback
*,ULONG
,LPCWSTR
);
68 HRESULT (*on_response
)(BSCallback
*,DWORD
,LPCWSTR
);
69 HRESULT (*beginning_transaction
)(BSCallback
*,WCHAR
**);
72 static inline nsProtocolStream
*impl_from_nsIInputStream(nsIInputStream
*iface
)
74 return CONTAINING_RECORD(iface
, nsProtocolStream
, nsIInputStream_iface
);
77 static nsresult NSAPI
nsInputStream_QueryInterface(nsIInputStream
*iface
, nsIIDRef riid
,
80 nsProtocolStream
*This
= impl_from_nsIInputStream(iface
);
84 if(IsEqualGUID(&IID_nsISupports
, riid
)) {
85 TRACE("(%p)->(IID_nsISupports %p)\n", This
, result
);
86 *result
= &This
->nsIInputStream_iface
;
87 }else if(IsEqualGUID(&IID_nsIInputStream
, riid
)) {
88 TRACE("(%p)->(IID_nsIInputStream %p)\n", This
, result
);
89 *result
= &This
->nsIInputStream_iface
;
93 nsIInputStream_AddRef(&This
->nsIInputStream_iface
);
97 WARN("unsupported interface %s\n", debugstr_guid(riid
));
98 return NS_NOINTERFACE
;
101 static nsrefcnt NSAPI
nsInputStream_AddRef(nsIInputStream
*iface
)
103 nsProtocolStream
*This
= impl_from_nsIInputStream(iface
);
104 LONG ref
= InterlockedIncrement(&This
->ref
);
106 TRACE("(%p) ref=%d\n", This
, ref
);
112 static nsrefcnt NSAPI
nsInputStream_Release(nsIInputStream
*iface
)
114 nsProtocolStream
*This
= impl_from_nsIInputStream(iface
);
115 LONG ref
= InterlockedDecrement(&This
->ref
);
117 TRACE("(%p) ref=%d\n", This
, ref
);
125 static nsresult NSAPI
nsInputStream_Close(nsIInputStream
*iface
)
127 nsProtocolStream
*This
= impl_from_nsIInputStream(iface
);
128 FIXME("(%p)\n", This
);
129 return NS_ERROR_NOT_IMPLEMENTED
;
132 static nsresult NSAPI
nsInputStream_Available(nsIInputStream
*iface
, PRUint32
*_retval
)
134 nsProtocolStream
*This
= impl_from_nsIInputStream(iface
);
135 FIXME("(%p)->(%p)\n", This
, _retval
);
136 return NS_ERROR_NOT_IMPLEMENTED
;
139 static nsresult NSAPI
nsInputStream_Read(nsIInputStream
*iface
, char *aBuf
, PRUint32 aCount
,
142 nsProtocolStream
*This
= impl_from_nsIInputStream(iface
);
145 TRACE("(%p)->(%p %d %p)\n", This
, aBuf
, aCount
, _retval
);
147 if(read
> This
->buf_size
)
148 read
= This
->buf_size
;
151 memcpy(aBuf
, This
->buf
, read
);
152 if(read
< This
->buf_size
)
153 memmove(This
->buf
, This
->buf
+read
, This
->buf_size
-read
);
154 This
->buf_size
-= read
;
161 static nsresult NSAPI
nsInputStream_ReadSegments(nsIInputStream
*iface
,
162 nsresult (WINAPI
*aWriter
)(nsIInputStream
*,void*,const char*,PRUint32
,PRUint32
,PRUint32
*),
163 void *aClousure
, PRUint32 aCount
, PRUint32
*_retval
)
165 nsProtocolStream
*This
= impl_from_nsIInputStream(iface
);
166 PRUint32 written
= 0;
169 TRACE("(%p)->(%p %p %d %p)\n", This
, aWriter
, aClousure
, aCount
, _retval
);
174 if(aCount
> This
->buf_size
)
175 aCount
= This
->buf_size
;
177 nsres
= aWriter(&This
->nsIInputStream_iface
, aClousure
, This
->buf
, 0, aCount
, &written
);
179 TRACE("aWritter failed: %08x\n", nsres
);
180 else if(written
!= This
->buf_size
)
181 FIXME("written %d != buf_size %d\n", written
, This
->buf_size
);
183 This
->buf_size
-= written
;
189 static nsresult NSAPI
nsInputStream_IsNonBlocking(nsIInputStream
*iface
, cpp_bool
*_retval
)
191 nsProtocolStream
*This
= impl_from_nsIInputStream(iface
);
192 FIXME("(%p)->(%p)\n", This
, _retval
);
193 return NS_ERROR_NOT_IMPLEMENTED
;
196 static const nsIInputStreamVtbl nsInputStreamVtbl
= {
197 nsInputStream_QueryInterface
,
198 nsInputStream_AddRef
,
199 nsInputStream_Release
,
201 nsInputStream_Available
,
203 nsInputStream_ReadSegments
,
204 nsInputStream_IsNonBlocking
207 static nsProtocolStream
*create_nsprotocol_stream(void)
209 nsProtocolStream
*ret
= heap_alloc(sizeof(nsProtocolStream
));
211 ret
->nsIInputStream_iface
.lpVtbl
= &nsInputStreamVtbl
;
218 static inline BSCallback
*impl_from_IBindStatusCallback(IBindStatusCallback
*iface
)
220 return CONTAINING_RECORD(iface
, BSCallback
, IBindStatusCallback_iface
);
223 static HRESULT WINAPI
BindStatusCallback_QueryInterface(IBindStatusCallback
*iface
,
224 REFIID riid
, void **ppv
)
226 BSCallback
*This
= impl_from_IBindStatusCallback(iface
);
229 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
230 TRACE("(%p)->(IID_IUnknown, %p)\n", This
, ppv
);
231 *ppv
= &This
->IBindStatusCallback_iface
;
232 }else if(IsEqualGUID(&IID_IBindStatusCallback
, riid
)) {
233 TRACE("(%p)->(IID_IBindStatusCallback, %p)\n", This
, ppv
);
234 *ppv
= &This
->IBindStatusCallback_iface
;
235 }else if(IsEqualGUID(&IID_IServiceProvider
, riid
)) {
236 TRACE("(%p)->(IID_IServiceProvider %p)\n", This
, ppv
);
237 *ppv
= &This
->IServiceProvider_iface
;
238 }else if(IsEqualGUID(&IID_IHttpNegotiate
, riid
)) {
239 TRACE("(%p)->(IID_IHttpNegotiate %p)\n", This
, ppv
);
240 *ppv
= &This
->IHttpNegotiate2_iface
;
241 }else if(IsEqualGUID(&IID_IHttpNegotiate2
, riid
)) {
242 TRACE("(%p)->(IID_IHttpNegotiate2 %p)\n", This
, ppv
);
243 *ppv
= &This
->IHttpNegotiate2_iface
;
244 }else if(IsEqualGUID(&IID_IInternetBindInfo
, riid
)) {
245 TRACE("(%p)->(IID_IInternetBindInfo %p)\n", This
, ppv
);
246 *ppv
= &This
->IInternetBindInfo_iface
;
250 IBindStatusCallback_AddRef(&This
->IBindStatusCallback_iface
);
254 TRACE("Unsupported riid = %s\n", debugstr_guid(riid
));
255 return E_NOINTERFACE
;
258 static ULONG WINAPI
BindStatusCallback_AddRef(IBindStatusCallback
*iface
)
260 BSCallback
*This
= impl_from_IBindStatusCallback(iface
);
261 LONG ref
= InterlockedIncrement(&This
->ref
);
263 TRACE("(%p) ref = %d\n", This
, ref
);
268 static ULONG WINAPI
BindStatusCallback_Release(IBindStatusCallback
*iface
)
270 BSCallback
*This
= impl_from_IBindStatusCallback(iface
);
271 LONG ref
= InterlockedDecrement(&This
->ref
);
273 TRACE("(%p) ref = %d\n", This
, ref
);
277 GlobalFree(This
->post_data
);
279 IMoniker_Release(This
->mon
);
281 IBinding_Release(This
->binding
);
282 list_remove(&This
->entry
);
283 list_init(&This
->entry
);
284 heap_free(This
->headers
);
286 This
->vtbl
->destroy(This
);
292 static HRESULT WINAPI
BindStatusCallback_OnStartBinding(IBindStatusCallback
*iface
,
293 DWORD dwReserved
, IBinding
*pbind
)
295 BSCallback
*This
= impl_from_IBindStatusCallback(iface
);
297 TRACE("(%p)->(%d %p)\n", This
, dwReserved
, pbind
);
299 IBinding_AddRef(pbind
);
300 This
->binding
= pbind
;
303 list_add_head(&This
->doc
->bindings
, &This
->entry
);
305 return This
->vtbl
->start_binding(This
);
308 static HRESULT WINAPI
BindStatusCallback_GetPriority(IBindStatusCallback
*iface
, LONG
*pnPriority
)
310 BSCallback
*This
= impl_from_IBindStatusCallback(iface
);
311 FIXME("(%p)->(%p)\n", This
, pnPriority
);
315 static HRESULT WINAPI
BindStatusCallback_OnLowResource(IBindStatusCallback
*iface
, DWORD reserved
)
317 BSCallback
*This
= impl_from_IBindStatusCallback(iface
);
318 FIXME("(%p)->(%d)\n", This
, reserved
);
322 static HRESULT WINAPI
BindStatusCallback_OnProgress(IBindStatusCallback
*iface
, ULONG ulProgress
,
323 ULONG ulProgressMax
, ULONG ulStatusCode
, LPCWSTR szStatusText
)
325 BSCallback
*This
= impl_from_IBindStatusCallback(iface
);
327 TRACE("%p)->(%u %u %u %s)\n", This
, ulProgress
, ulProgressMax
, ulStatusCode
,
328 debugstr_w(szStatusText
));
330 return This
->vtbl
->on_progress(This
, ulStatusCode
, szStatusText
);
333 static HRESULT WINAPI
BindStatusCallback_OnStopBinding(IBindStatusCallback
*iface
,
334 HRESULT hresult
, LPCWSTR szError
)
336 BSCallback
*This
= impl_from_IBindStatusCallback(iface
);
339 TRACE("(%p)->(%08x %s)\n", This
, hresult
, debugstr_w(szError
));
341 /* NOTE: IE7 calls GetBindResult here */
343 hres
= This
->vtbl
->stop_binding(This
, hresult
);
346 IBinding_Release(This
->binding
);
347 This
->binding
= NULL
;
350 list_remove(&This
->entry
);
351 list_init(&This
->entry
);
357 static HRESULT WINAPI
BindStatusCallback_GetBindInfo(IBindStatusCallback
*iface
,
358 DWORD
*grfBINDF
, BINDINFO
*pbindinfo
)
360 BSCallback
*This
= impl_from_IBindStatusCallback(iface
);
363 TRACE("(%p)->(%p %p)\n", This
, grfBINDF
, pbindinfo
);
365 if(!This
->bindinfo_ready
) {
368 hres
= This
->vtbl
->init_bindinfo(This
);
372 This
->bindinfo_ready
= TRUE
;
375 *grfBINDF
= This
->bindf
;
377 size
= pbindinfo
->cbSize
;
378 memset(pbindinfo
, 0, size
);
379 pbindinfo
->cbSize
= size
;
381 pbindinfo
->cbstgmedData
= This
->post_data_len
;
382 pbindinfo
->dwCodePage
= CP_UTF8
;
383 pbindinfo
->dwOptions
= 0x80000;
385 if(This
->post_data
) {
386 pbindinfo
->dwBindVerb
= BINDVERB_POST
;
388 pbindinfo
->stgmedData
.tymed
= TYMED_HGLOBAL
;
389 pbindinfo
->stgmedData
.u
.hGlobal
= This
->post_data
;
390 pbindinfo
->stgmedData
.pUnkForRelease
= (IUnknown
*)&This
->IBindStatusCallback_iface
;
391 IBindStatusCallback_AddRef(&This
->IBindStatusCallback_iface
);
397 static HRESULT WINAPI
BindStatusCallback_OnDataAvailable(IBindStatusCallback
*iface
,
398 DWORD grfBSCF
, DWORD dwSize
, FORMATETC
*pformatetc
, STGMEDIUM
*pstgmed
)
400 BSCallback
*This
= impl_from_IBindStatusCallback(iface
);
402 TRACE("(%p)->(%08x %d %p %p)\n", This
, grfBSCF
, dwSize
, pformatetc
, pstgmed
);
404 return This
->vtbl
->read_data(This
, pstgmed
->u
.pstm
);
407 static HRESULT WINAPI
BindStatusCallback_OnObjectAvailable(IBindStatusCallback
*iface
,
408 REFIID riid
, IUnknown
*punk
)
410 BSCallback
*This
= impl_from_IBindStatusCallback(iface
);
411 FIXME("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), punk
);
415 static const IBindStatusCallbackVtbl BindStatusCallbackVtbl
= {
416 BindStatusCallback_QueryInterface
,
417 BindStatusCallback_AddRef
,
418 BindStatusCallback_Release
,
419 BindStatusCallback_OnStartBinding
,
420 BindStatusCallback_GetPriority
,
421 BindStatusCallback_OnLowResource
,
422 BindStatusCallback_OnProgress
,
423 BindStatusCallback_OnStopBinding
,
424 BindStatusCallback_GetBindInfo
,
425 BindStatusCallback_OnDataAvailable
,
426 BindStatusCallback_OnObjectAvailable
429 static inline BSCallback
*impl_from_IHttpNegotiate2(IHttpNegotiate2
*iface
)
431 return CONTAINING_RECORD(iface
, BSCallback
, IHttpNegotiate2_iface
);
434 static HRESULT WINAPI
HttpNegotiate_QueryInterface(IHttpNegotiate2
*iface
,
435 REFIID riid
, void **ppv
)
437 BSCallback
*This
= impl_from_IHttpNegotiate2(iface
);
438 return IBindStatusCallback_QueryInterface(&This
->IBindStatusCallback_iface
, riid
, ppv
);
441 static ULONG WINAPI
HttpNegotiate_AddRef(IHttpNegotiate2
*iface
)
443 BSCallback
*This
= impl_from_IHttpNegotiate2(iface
);
444 return IBindStatusCallback_AddRef(&This
->IBindStatusCallback_iface
);
447 static ULONG WINAPI
HttpNegotiate_Release(IHttpNegotiate2
*iface
)
449 BSCallback
*This
= impl_from_IHttpNegotiate2(iface
);
450 return IBindStatusCallback_Release(&This
->IBindStatusCallback_iface
);
453 static HRESULT WINAPI
HttpNegotiate_BeginningTransaction(IHttpNegotiate2
*iface
,
454 LPCWSTR szURL
, LPCWSTR szHeaders
, DWORD dwReserved
, LPWSTR
*pszAdditionalHeaders
)
456 BSCallback
*This
= impl_from_IHttpNegotiate2(iface
);
459 TRACE("(%p)->(%s %s %d %p)\n", This
, debugstr_w(szURL
), debugstr_w(szHeaders
),
460 dwReserved
, pszAdditionalHeaders
);
462 *pszAdditionalHeaders
= NULL
;
464 hres
= This
->vtbl
->beginning_transaction(This
, pszAdditionalHeaders
);
471 size
= (strlenW(This
->headers
)+1)*sizeof(WCHAR
);
472 *pszAdditionalHeaders
= CoTaskMemAlloc(size
);
473 if(!*pszAdditionalHeaders
)
474 return E_OUTOFMEMORY
;
475 memcpy(*pszAdditionalHeaders
, This
->headers
, size
);
481 static HRESULT WINAPI
HttpNegotiate_OnResponse(IHttpNegotiate2
*iface
, DWORD dwResponseCode
,
482 LPCWSTR szResponseHeaders
, LPCWSTR szRequestHeaders
, LPWSTR
*pszAdditionalRequestHeaders
)
484 BSCallback
*This
= impl_from_IHttpNegotiate2(iface
);
486 TRACE("(%p)->(%d %s %s %p)\n", This
, dwResponseCode
, debugstr_w(szResponseHeaders
),
487 debugstr_w(szRequestHeaders
), pszAdditionalRequestHeaders
);
489 return This
->vtbl
->on_response(This
, dwResponseCode
, szResponseHeaders
);
492 static HRESULT WINAPI
HttpNegotiate_GetRootSecurityId(IHttpNegotiate2
*iface
,
493 BYTE
*pbSecurityId
, DWORD
*pcbSecurityId
, DWORD_PTR dwReserved
)
495 BSCallback
*This
= impl_from_IHttpNegotiate2(iface
);
496 FIXME("(%p)->(%p %p %ld)\n", This
, pbSecurityId
, pcbSecurityId
, dwReserved
);
500 static const IHttpNegotiate2Vtbl HttpNegotiate2Vtbl
= {
501 HttpNegotiate_QueryInterface
,
502 HttpNegotiate_AddRef
,
503 HttpNegotiate_Release
,
504 HttpNegotiate_BeginningTransaction
,
505 HttpNegotiate_OnResponse
,
506 HttpNegotiate_GetRootSecurityId
509 static inline BSCallback
*impl_from_IInternetBindInfo(IInternetBindInfo
*iface
)
511 return CONTAINING_RECORD(iface
, BSCallback
, IInternetBindInfo_iface
);
514 static HRESULT WINAPI
InternetBindInfo_QueryInterface(IInternetBindInfo
*iface
,
515 REFIID riid
, void **ppv
)
517 BSCallback
*This
= impl_from_IInternetBindInfo(iface
);
518 return IBindStatusCallback_QueryInterface(&This
->IBindStatusCallback_iface
, riid
, ppv
);
521 static ULONG WINAPI
InternetBindInfo_AddRef(IInternetBindInfo
*iface
)
523 BSCallback
*This
= impl_from_IInternetBindInfo(iface
);
524 return IBindStatusCallback_AddRef(&This
->IBindStatusCallback_iface
);
527 static ULONG WINAPI
InternetBindInfo_Release(IInternetBindInfo
*iface
)
529 BSCallback
*This
= impl_from_IInternetBindInfo(iface
);
530 return IBindStatusCallback_Release(&This
->IBindStatusCallback_iface
);
533 static HRESULT WINAPI
InternetBindInfo_GetBindInfo(IInternetBindInfo
*iface
,
534 DWORD
*grfBINDF
, BINDINFO
*pbindinfo
)
536 BSCallback
*This
= impl_from_IInternetBindInfo(iface
);
537 FIXME("(%p)->(%p %p)\n", This
, grfBINDF
, pbindinfo
);
541 static HRESULT WINAPI
InternetBindInfo_GetBindString(IInternetBindInfo
*iface
,
542 ULONG ulStringType
, LPOLESTR
*ppwzStr
, ULONG cEl
, ULONG
*pcElFetched
)
544 BSCallback
*This
= impl_from_IInternetBindInfo(iface
);
545 FIXME("(%p)->(%u %p %u %p)\n", This
, ulStringType
, ppwzStr
, cEl
, pcElFetched
);
549 static const IInternetBindInfoVtbl InternetBindInfoVtbl
= {
550 InternetBindInfo_QueryInterface
,
551 InternetBindInfo_AddRef
,
552 InternetBindInfo_Release
,
553 InternetBindInfo_GetBindInfo
,
554 InternetBindInfo_GetBindString
557 static inline BSCallback
*impl_from_IServiceProvider(IServiceProvider
*iface
)
559 return CONTAINING_RECORD(iface
, BSCallback
, IServiceProvider_iface
);
562 static HRESULT WINAPI
BSCServiceProvider_QueryInterface(IServiceProvider
*iface
,
563 REFIID riid
, void **ppv
)
565 BSCallback
*This
= impl_from_IServiceProvider(iface
);
566 return IBindStatusCallback_QueryInterface(&This
->IBindStatusCallback_iface
, riid
, ppv
);
569 static ULONG WINAPI
BSCServiceProvider_AddRef(IServiceProvider
*iface
)
571 BSCallback
*This
= impl_from_IServiceProvider(iface
);
572 return IBindStatusCallback_AddRef(&This
->IBindStatusCallback_iface
);
575 static ULONG WINAPI
BSCServiceProvider_Release(IServiceProvider
*iface
)
577 BSCallback
*This
= impl_from_IServiceProvider(iface
);
578 return IBindStatusCallback_Release(&This
->IBindStatusCallback_iface
);
581 static HRESULT WINAPI
BSCServiceProvider_QueryService(IServiceProvider
*iface
,
582 REFGUID guidService
, REFIID riid
, void **ppv
)
584 BSCallback
*This
= impl_from_IServiceProvider(iface
);
585 TRACE("(%p)->(%s %s %p)\n", This
, debugstr_guid(guidService
), debugstr_guid(riid
), ppv
);
586 return E_NOINTERFACE
;
589 static const IServiceProviderVtbl ServiceProviderVtbl
= {
590 BSCServiceProvider_QueryInterface
,
591 BSCServiceProvider_AddRef
,
592 BSCServiceProvider_Release
,
593 BSCServiceProvider_QueryService
596 static void init_bscallback(BSCallback
*This
, const BSCallbackVtbl
*vtbl
, IMoniker
*mon
, DWORD bindf
)
598 This
->IBindStatusCallback_iface
.lpVtbl
= &BindStatusCallbackVtbl
;
599 This
->IServiceProvider_iface
.lpVtbl
= &ServiceProviderVtbl
;
600 This
->IHttpNegotiate2_iface
.lpVtbl
= &HttpNegotiate2Vtbl
;
601 This
->IInternetBindInfo_iface
.lpVtbl
= &InternetBindInfoVtbl
;
606 list_init(&This
->entry
);
609 IMoniker_AddRef(mon
);
613 /* Calls undocumented 84 cmd of CGID_ShellDocView */
614 static void call_docview_84(HTMLDocumentObj
*doc
)
616 IOleCommandTarget
*olecmd
;
623 hres
= IOleClientSite_QueryInterface(doc
->client
, &IID_IOleCommandTarget
, (void**)&olecmd
);
628 hres
= IOleCommandTarget_Exec(olecmd
, &CGID_ShellDocView
, 84, 0, NULL
, &var
);
629 IOleCommandTarget_Release(olecmd
);
630 if(SUCCEEDED(hres
) && V_VT(&var
) != VT_NULL
)
631 FIXME("handle result\n");
634 static void parse_content_type(nsChannelBSC
*This
, const WCHAR
*value
)
639 static const WCHAR charsetW
[] = {'c','h','a','r','s','e','t','='};
641 ptr
= strchrW(value
, ';');
646 while(*ptr
&& isspaceW(*ptr
))
649 len
= strlenW(value
);
650 if(ptr
+ sizeof(charsetW
)/sizeof(WCHAR
) < value
+len
&& !memicmpW(ptr
, charsetW
, sizeof(charsetW
)/sizeof(WCHAR
))) {
651 size_t charset_len
, lena
;
652 nsACString charset_str
;
653 const WCHAR
*charset
;
656 ptr
+= sizeof(charsetW
)/sizeof(WCHAR
);
659 FIXME("Quoted value\n");
663 while(*ptr
&& *ptr
!= ',')
665 charset_len
= ptr
-charset
;
668 lena
= WideCharToMultiByte(CP_ACP
, 0, charset
, charset_len
, NULL
, 0, NULL
, NULL
);
669 charseta
= heap_alloc(lena
+1);
673 WideCharToMultiByte(CP_ACP
, 0, charset
, charset_len
, charseta
, lena
, NULL
, NULL
);
676 nsACString_InitDepend(&charset_str
, charseta
);
677 nsIHttpChannel_SetContentCharset(&This
->nschannel
->nsIHttpChannel_iface
, &charset_str
);
678 nsACString_Finish(&charset_str
);
681 FIXME("unhandled: %s\n", debugstr_wn(ptr
, len
- (ptr
-value
)));
685 static HRESULT
parse_headers(const WCHAR
*headers
, struct list
*headers_list
)
687 const WCHAR
*header
, *header_end
, *colon
, *value
;
692 if(header
[0] == '\r' && header
[1] == '\n' && !header
[2])
694 for(colon
= header
; *colon
&& *colon
!= ':' && *colon
!= '\r'; colon
++);
704 for(header_end
= value
+1; *header_end
&& *header_end
!= '\r'; header_end
++);
706 hres
= set_http_header(headers_list
, header
, colon
-header
, value
, header_end
-value
);
711 if(header
[0] == '\r' && header
[1] == '\n')
718 static HRESULT
process_response_headers(nsChannelBSC
*This
, const WCHAR
*headers
)
723 static const WCHAR content_typeW
[] = {'c','o','n','t','e','n','t','-','t','y','p','e',0};
725 hres
= parse_headers(headers
, &This
->nschannel
->response_headers
);
729 LIST_FOR_EACH_ENTRY(iter
, &This
->nschannel
->response_headers
, http_header_t
, entry
) {
730 if(!strcmpiW(iter
->header
, content_typeW
))
731 parse_content_type(This
, iter
->data
);
737 HRESULT
start_binding(HTMLWindow
*window
, HTMLDocumentNode
*doc
, BSCallback
*bscallback
, IBindCtx
*bctx
)
742 TRACE("(%p %p %p %p)\n", window
, doc
, bscallback
, bctx
);
744 bscallback
->doc
= doc
;
746 /* NOTE: IE7 calls IsSystemMoniker here*/
749 if(bscallback
->mon
!= window
->mon
)
750 set_current_mon(window
, bscallback
->mon
);
751 call_docview_84(window
->doc_obj
);
755 RegisterBindStatusCallback(bctx
, &bscallback
->IBindStatusCallback_iface
, NULL
, 0);
756 IBindCtx_AddRef(bctx
);
758 hres
= CreateAsyncBindCtx(0, &bscallback
->IBindStatusCallback_iface
, NULL
, &bctx
);
760 WARN("CreateAsyncBindCtx failed: %08x\n", hres
);
761 bscallback
->vtbl
->stop_binding(bscallback
, hres
);
766 hres
= IMoniker_BindToStorage(bscallback
->mon
, bctx
, NULL
, &IID_IStream
, (void**)&str
);
767 IBindCtx_Release(bctx
);
769 WARN("BindToStorage failed: %08x\n", hres
);
770 bscallback
->vtbl
->stop_binding(bscallback
, hres
);
775 IStream_Release(str
);
777 IMoniker_Release(bscallback
->mon
);
778 bscallback
->mon
= NULL
;
791 static inline BufferBSC
*BufferBSC_from_BSCallback(BSCallback
*iface
)
793 return CONTAINING_RECORD(iface
, BufferBSC
, bsc
);
796 static void BufferBSC_destroy(BSCallback
*bsc
)
798 BufferBSC
*This
= BufferBSC_from_BSCallback(bsc
);
800 heap_free(This
->buf
);
804 static HRESULT
BufferBSC_init_bindinfo(BSCallback
*bsc
)
809 static HRESULT
BufferBSC_start_binding(BSCallback
*bsc
)
814 static HRESULT
BufferBSC_stop_binding(BSCallback
*bsc
, HRESULT result
)
816 BufferBSC
*This
= BufferBSC_from_BSCallback(bsc
);
821 heap_free(This
->buf
);
829 static HRESULT
BufferBSC_read_data(BSCallback
*bsc
, IStream
*stream
)
831 BufferBSC
*This
= BufferBSC_from_BSCallback(bsc
);
837 This
->buf
= heap_alloc(This
->size
);
841 if(This
->bsc
.readed
== This
->size
) {
843 This
->buf
= heap_realloc(This
->buf
, This
->size
);
847 hres
= IStream_Read(stream
, This
->buf
+This
->bsc
.readed
, This
->size
-This
->bsc
.readed
, &readed
);
848 This
->bsc
.readed
+= readed
;
849 }while(hres
== S_OK
);
854 static HRESULT
BufferBSC_on_progress(BSCallback
*bsc
, ULONG status_code
, LPCWSTR status_text
)
859 static HRESULT
BufferBSC_on_response(BSCallback
*bsc
, DWORD response_code
,
860 LPCWSTR response_headers
)
865 static HRESULT
BufferBSC_beginning_transaction(BSCallback
*bsc
, WCHAR
**additional_headers
)
870 static const BSCallbackVtbl BufferBSCVtbl
= {
872 BufferBSC_init_bindinfo
,
873 BufferBSC_start_binding
,
874 BufferBSC_stop_binding
,
876 BufferBSC_on_progress
,
877 BufferBSC_on_response
,
878 BufferBSC_beginning_transaction
882 static BufferBSC
*create_bufferbsc(IMoniker
*mon
)
884 BufferBSC
*ret
= heap_alloc_zero(sizeof(*ret
));
886 init_bscallback(&ret
->bsc
, &BufferBSCVtbl
, mon
, 0);
892 HRESULT
bind_mon_to_buffer(HTMLDocumentNode
*doc
, IMoniker
*mon
, void **buf
, DWORD
*size
)
894 BufferBSC
*bsc
= create_bufferbsc(mon
);
899 hres
= start_binding(NULL
, doc
, &bsc
->bsc
, NULL
);
900 if(SUCCEEDED(hres
)) {
902 if(SUCCEEDED(hres
)) {
905 *size
= bsc
->bsc
.readed
;
910 IBindStatusCallback_Release(&bsc
->bsc
.IBindStatusCallback_iface
);
915 static HRESULT
read_post_data_stream(nsChannelBSC
*This
, nsChannel
*nschannel
)
917 PRUint32 data_len
= 0, available
= 0;
918 char *data
, *post_data
;
922 if(!nschannel
->post_data_stream
)
925 nsres
= nsIInputStream_Available(nschannel
->post_data_stream
, &available
);
929 post_data
= data
= GlobalAlloc(0, available
);
931 return E_OUTOFMEMORY
;
933 nsres
= nsIInputStream_Read(nschannel
->post_data_stream
, data
, available
, &data_len
);
934 if(NS_FAILED(nsres
)) {
939 if(nschannel
->post_data_contains_headers
) {
940 if(data_len
>= 2 && data
[0] == '\r' && data
[1] == '\n') {
948 post_data
+= data_len
;
949 for(ptr
= data
; ptr
+4 < data
+data_len
; ptr
++) {
950 if(!memcmp(ptr
, "\r\n\r\n", 4)) {
956 data_len
-= post_data
-data
;
958 size
= MultiByteToWideChar(CP_ACP
, 0, data
, post_data
-data
, NULL
, 0);
959 headers
= heap_alloc((size
+1)*sizeof(WCHAR
));
961 MultiByteToWideChar(CP_ACP
, 0, data
, post_data
-data
, headers
, size
);
963 hres
= parse_headers(headers
, &nschannel
->request_headers
);
965 This
->bsc
.headers
= headers
;
969 hres
= E_OUTOFMEMORY
;
982 }else if(post_data
!= data
) {
985 new_data
= GlobalAlloc(0, data_len
);
987 memcpy(new_data
, post_data
, data_len
);
990 return E_OUTOFMEMORY
;
991 post_data
= new_data
;
994 This
->bsc
.post_data
= post_data
;
995 This
->bsc
.post_data_len
= data_len
;
996 TRACE("post_data = %s\n", debugstr_a(This
->bsc
.post_data
));
1000 static HRESULT
on_start_nsrequest(nsChannelBSC
*This
)
1004 /* FIXME: it's needed for http connections from BindToObject. */
1005 if(!This
->nschannel
->response_status
)
1006 This
->nschannel
->response_status
= 200;
1008 nsres
= nsIStreamListener_OnStartRequest(This
->nslistener
,
1009 (nsIRequest
*)&This
->nschannel
->nsIHttpChannel_iface
, This
->nscontext
);
1010 if(NS_FAILED(nsres
)) {
1011 FIXME("OnStartRequest failed: %08x\n", nsres
);
1016 list_remove(&This
->bsc
.entry
);
1017 list_init(&This
->bsc
.entry
);
1018 update_window_doc(This
->window
);
1019 if(This
->window
->doc
!= This
->bsc
.doc
)
1020 This
->bsc
.doc
= This
->window
->doc
;
1021 list_add_head(&This
->bsc
.doc
->bindings
, &This
->bsc
.entry
);
1022 if(This
->window
->readystate
!= READYSTATE_LOADING
)
1023 set_ready_state(This
->window
, READYSTATE_LOADING
);
1029 static void on_stop_nsrequest(nsChannelBSC
*This
, HRESULT result
)
1031 nsresult nsres
, request_result
;
1035 request_result
= NS_OK
;
1038 request_result
= NS_BINDING_ABORTED
;
1041 request_result
= NS_ERROR_FAILURE
;
1044 if(This
->nslistener
) {
1045 nsres
= nsIStreamListener_OnStopRequest(This
->nslistener
,
1046 (nsIRequest
*)&This
->nschannel
->nsIHttpChannel_iface
, This
->nscontext
,
1048 if(NS_FAILED(nsres
))
1049 WARN("OnStopRequest failed: %08x\n", nsres
);
1052 if(This
->nschannel
->load_group
) {
1053 nsres
= nsILoadGroup_RemoveRequest(This
->nschannel
->load_group
,
1054 (nsIRequest
*)&This
->nschannel
->nsIHttpChannel_iface
, NULL
, request_result
);
1055 if(NS_FAILED(nsres
))
1056 ERR("RemoveRequest failed: %08x\n", nsres
);
1060 static HRESULT
read_stream_data(nsChannelBSC
*This
, IStream
*stream
)
1062 static const WCHAR mimeTextHtml
[] = {'t','e','x','t','/','h','t','m','l',0};
1068 if(!This
->nslistener
) {
1073 hres
= IStream_Read(stream
, buf
, sizeof(buf
), &read
);
1074 }while(hres
== S_OK
&& read
);
1080 This
->nsstream
= create_nsprotocol_stream();
1084 hres
= IStream_Read(stream
, This
->nsstream
->buf
+This
->nsstream
->buf_size
,
1085 sizeof(This
->nsstream
->buf
)-This
->nsstream
->buf_size
, &read
);
1089 This
->nsstream
->buf_size
+= read
;
1091 if(!This
->bsc
.readed
) {
1092 if(This
->nsstream
->buf_size
>= 2
1093 && (BYTE
)This
->nsstream
->buf
[0] == 0xff
1094 && (BYTE
)This
->nsstream
->buf
[1] == 0xfe)
1095 This
->nschannel
->charset
= heap_strdupA(UTF16_STR
);
1096 if(This
->nsstream
->buf_size
>= 3
1097 && (BYTE
)This
->nsstream
->buf
[0] == 0xef
1098 && (BYTE
)This
->nsstream
->buf
[1] == 0xbb
1099 && (BYTE
)This
->nsstream
->buf
[2] == 0xbf)
1100 This
->nschannel
->charset
= heap_strdupA(UTF8_STR
);
1102 if(!This
->nschannel
->content_type
) {
1105 hres
= FindMimeFromData(NULL
, NULL
, This
->nsstream
->buf
, This
->nsstream
->buf_size
,
1106 This
->window
? mimeTextHtml
: NULL
, 0, &mime
, 0);
1110 TRACE("Found MIME %s\n", debugstr_w(mime
));
1112 This
->nschannel
->content_type
= heap_strdupWtoA(mime
);
1113 CoTaskMemFree(mime
);
1114 if(!This
->nschannel
->content_type
)
1115 return E_OUTOFMEMORY
;
1118 on_start_nsrequest(This
);
1121 This
->bsc
.readed
+= This
->nsstream
->buf_size
;
1123 nsres
= nsIStreamListener_OnDataAvailable(This
->nslistener
,
1124 (nsIRequest
*)&This
->nschannel
->nsIHttpChannel_iface
, This
->nscontext
,
1125 &This
->nsstream
->nsIInputStream_iface
, This
->bsc
.readed
-This
->nsstream
->buf_size
,
1126 This
->nsstream
->buf_size
);
1127 if(NS_FAILED(nsres
))
1128 ERR("OnDataAvailable failed: %08x\n", nsres
);
1130 if(This
->nsstream
->buf_size
== sizeof(This
->nsstream
->buf
)) {
1131 ERR("buffer is full\n");
1134 }while(hres
== S_OK
);
1140 nsIAsyncVerifyRedirectCallback nsIAsyncVerifyRedirectCallback_iface
;
1144 nsChannel
*nschannel
;
1146 } nsRedirectCallback
;
1148 static nsRedirectCallback
*impl_from_nsIAsyncVerifyRedirectCallback(nsIAsyncVerifyRedirectCallback
*iface
)
1150 return CONTAINING_RECORD(iface
, nsRedirectCallback
, nsIAsyncVerifyRedirectCallback_iface
);
1153 static nsresult NSAPI
nsAsyncVerifyRedirectCallback_QueryInterface(nsIAsyncVerifyRedirectCallback
*iface
,
1154 nsIIDRef riid
, void **result
)
1156 nsRedirectCallback
*This
= impl_from_nsIAsyncVerifyRedirectCallback(iface
);
1158 if(IsEqualGUID(&IID_nsISupports
, riid
)) {
1159 TRACE("(%p)->(IID_nsISuports %p)\n", This
, result
);
1160 *result
= &This
->nsIAsyncVerifyRedirectCallback_iface
;
1161 }else if(IsEqualGUID(&IID_nsIAsyncVerifyRedirectCallback
, riid
)) {
1162 TRACE("(%p)->(IID_nsIAsyncVerifyRedirectCallback %p)\n", This
, result
);
1163 *result
= &This
->nsIAsyncVerifyRedirectCallback_iface
;
1166 WARN("unimplemented iface %s\n", debugstr_guid(riid
));
1167 return NS_NOINTERFACE
;
1170 nsISupports_AddRef((nsISupports
*)*result
);
1174 static nsrefcnt NSAPI
nsAsyncVerifyRedirectCallback_AddRef(nsIAsyncVerifyRedirectCallback
*iface
)
1176 nsRedirectCallback
*This
= impl_from_nsIAsyncVerifyRedirectCallback(iface
);
1177 LONG ref
= InterlockedIncrement(&This
->ref
);
1179 TRACE("(%p) ref=%d\n", This
, ref
);
1184 static nsrefcnt NSAPI
nsAsyncVerifyRedirectCallback_Release(nsIAsyncVerifyRedirectCallback
*iface
)
1186 nsRedirectCallback
*This
= impl_from_nsIAsyncVerifyRedirectCallback(iface
);
1187 LONG ref
= InterlockedDecrement(&This
->ref
);
1189 TRACE("(%p) ref=%d\n", This
, ref
);
1192 IBindStatusCallback_Release(&This
->bsc
->bsc
.IBindStatusCallback_iface
);
1193 nsIChannel_Release(&This
->nschannel
->nsIHttpChannel_iface
);
1200 static nsresult NSAPI
nsAsyncVerifyRedirectCallback_AsyncOnChannelRedirect(nsIAsyncVerifyRedirectCallback
*iface
, nsresult result
)
1202 nsRedirectCallback
*This
= impl_from_nsIAsyncVerifyRedirectCallback(iface
);
1203 nsChannel
*old_nschannel
;
1206 TRACE("(%p)->(%08x)\n", This
, result
);
1208 old_nschannel
= This
->bsc
->nschannel
;
1209 nsIChannel_AddRef(&This
->nschannel
->nsIHttpChannel_iface
);
1210 This
->bsc
->nschannel
= This
->nschannel
;
1212 if(This
->nschannel
->load_group
) {
1213 nsres
= nsILoadGroup_AddRequest(This
->nschannel
->load_group
, (nsIRequest
*)&This
->nschannel
->nsIHttpChannel_iface
,
1215 if(NS_FAILED(nsres
))
1216 ERR("AddRequest failed: %08x\n", nsres
);
1219 if(This
->bsc
->window
) {
1220 IUri
*uri
= nsuri_get_uri(This
->nschannel
->uri
);
1223 set_current_uri(This
->bsc
->window
, uri
);
1226 WARN("Could not get IUri from nsWineURI\n");
1231 nsres
= nsILoadGroup_RemoveRequest(old_nschannel
->load_group
,
1232 (nsIRequest
*)&old_nschannel
->nsIHttpChannel_iface
, NULL
, NS_OK
);
1233 if(NS_FAILED(nsres
))
1234 ERR("RemoveRequest failed: %08x\n", nsres
);
1235 nsIHttpChannel_Release(&old_nschannel
->nsIHttpChannel_iface
);
1241 static const nsIAsyncVerifyRedirectCallbackVtbl nsAsyncVerifyRedirectCallbackVtbl
= {
1242 nsAsyncVerifyRedirectCallback_QueryInterface
,
1243 nsAsyncVerifyRedirectCallback_AddRef
,
1244 nsAsyncVerifyRedirectCallback_Release
,
1245 nsAsyncVerifyRedirectCallback_AsyncOnChannelRedirect
1248 static HRESULT
create_redirect_callback(nsChannel
*nschannel
, nsChannelBSC
*bsc
, nsRedirectCallback
**ret
)
1250 nsRedirectCallback
*callback
;
1252 callback
= heap_alloc(sizeof(*callback
));
1254 return E_OUTOFMEMORY
;
1256 callback
->nsIAsyncVerifyRedirectCallback_iface
.lpVtbl
= &nsAsyncVerifyRedirectCallbackVtbl
;
1259 nsIChannel_AddRef(&nschannel
->nsIHttpChannel_iface
);
1260 callback
->nschannel
= nschannel
;
1262 IBindStatusCallback_AddRef(&bsc
->bsc
.IBindStatusCallback_iface
);
1263 callback
->bsc
= bsc
;
1269 static inline nsChannelBSC
*nsChannelBSC_from_BSCallback(BSCallback
*iface
)
1271 return CONTAINING_RECORD(iface
, nsChannelBSC
, bsc
);
1274 static void nsChannelBSC_destroy(BSCallback
*bsc
)
1276 nsChannelBSC
*This
= nsChannelBSC_from_BSCallback(bsc
);
1279 nsIChannel_Release(&This
->nschannel
->nsIHttpChannel_iface
);
1280 if(This
->nslistener
)
1281 nsIStreamListener_Release(This
->nslistener
);
1283 nsISupports_Release(This
->nscontext
);
1285 nsIInputStream_Release(&This
->nsstream
->nsIInputStream_iface
);
1289 static HRESULT
nsChannelBSC_start_binding(BSCallback
*bsc
)
1291 nsChannelBSC
*This
= nsChannelBSC_from_BSCallback(bsc
);
1294 This
->window
->doc
->skip_mutation_notif
= FALSE
;
1299 static HRESULT
nsChannelBSC_init_bindinfo(BSCallback
*bsc
)
1301 nsChannelBSC
*This
= nsChannelBSC_from_BSCallback(bsc
);
1304 if(This
->nschannel
&& This
->nschannel
->post_data_stream
) {
1305 hres
= read_post_data_stream(This
, This
->nschannel
);
1316 } stop_request_task_t
;
1318 static void stop_request_proc(task_t
*_task
)
1320 stop_request_task_t
*task
= (stop_request_task_t
*)_task
;
1322 TRACE("(%p)\n", task
->bsc
);
1324 list_remove(&task
->bsc
->bsc
.entry
);
1325 list_init(&task
->bsc
->bsc
.entry
);
1326 on_stop_nsrequest(task
->bsc
, S_OK
);
1329 static void stop_request_task_destr(task_t
*_task
)
1331 stop_request_task_t
*task
= (stop_request_task_t
*)_task
;
1333 IBindStatusCallback_Release(&task
->bsc
->bsc
.IBindStatusCallback_iface
);
1337 static HRESULT
async_stop_request(nsChannelBSC
*This
)
1339 stop_request_task_t
*task
;
1341 if(!This
->bsc
.readed
) {
1342 TRACE("No data read, calling OnStartRequest\n");
1343 on_start_nsrequest(This
);
1346 task
= heap_alloc(sizeof(*task
));
1348 return E_OUTOFMEMORY
;
1350 IBindStatusCallback_AddRef(&This
->bsc
.IBindStatusCallback_iface
);
1352 push_task(&task
->header
, stop_request_proc
, stop_request_task_destr
, This
->bsc
.doc
->basedoc
.doc_obj
->basedoc
.task_magic
);
1356 static void handle_navigation_error(nsChannelBSC
*This
, DWORD result
)
1358 HTMLDocumentObj
*doc
;
1359 IOleCommandTarget
*olecmd
;
1362 SAFEARRAYBOUND bound
;
1363 VARIANT var
, varOut
;
1371 doc
= This
->window
->doc_obj
;
1372 if(!doc
|| !doc
->doc_object_service
|| !doc
->client
)
1375 hres
= IDocObjectService_IsErrorUrl(doc
->doc_object_service
,
1376 This
->window
->url
, &is_error_url
);
1377 if(FAILED(hres
) || is_error_url
)
1380 hres
= IOleClientSite_QueryInterface(doc
->client
,
1381 &IID_IOleCommandTarget
, (void**)&olecmd
);
1386 bound
.cElements
= 8;
1387 sa
= SafeArrayCreate(VT_VARIANT
, 1, &bound
);
1389 IOleCommandTarget_Release(olecmd
);
1395 V_I4(&var
) = result
;
1396 SafeArrayPutElement(sa
, &ind
, &var
);
1399 V_VT(&var
) = VT_BSTR
;
1400 V_BSTR(&var
) = This
->window
->url
;
1401 SafeArrayPutElement(sa
, &ind
, &var
);
1404 V_VT(&var
) = VT_UNKNOWN
;
1405 V_UNKNOWN(&var
) = (IUnknown
*)&This
->window
->IHTMLWindow2_iface
;
1406 SafeArrayPutElement(sa
, &ind
, &var
);
1408 /* FIXME: what are the following fields for? */
1410 V_VT(&var
) = VT_UNKNOWN
;
1411 V_UNKNOWN(&var
) = NULL
;
1412 SafeArrayPutElement(sa
, &ind
, &var
);
1415 V_VT(&var
) = VT_BOOL
;
1416 V_BOOL(&var
) = FALSE
;
1417 SafeArrayPutElement(sa
, &ind
, &var
);
1420 V_VT(&var
) = VT_BOOL
;
1421 V_BOOL(&var
) = FALSE
;
1422 SafeArrayPutElement(sa
, &ind
, &var
);
1425 V_VT(&var
) = VT_BSTR
;
1426 unk
= SysAllocString(NULL
);
1428 SafeArrayPutElement(sa
, &ind
, &var
);
1431 V_VT(&var
) = VT_UNKNOWN
;
1432 V_UNKNOWN(&var
) = NULL
;
1433 SafeArrayPutElement(sa
, &ind
, &var
);
1435 V_VT(&var
) = VT_ARRAY
;
1437 V_VT(&varOut
) = VT_BOOL
;
1438 V_BOOL(&varOut
) = VARIANT_TRUE
;
1439 IOleCommandTarget_Exec(olecmd
, &CGID_DocHostCmdPriv
, 1, 0, &var
, FAILED(hres
)?NULL
:&varOut
);
1442 SafeArrayDestroy(sa
);
1443 IOleCommandTarget_Release(olecmd
);
1446 static HRESULT
nsChannelBSC_stop_binding(BSCallback
*bsc
, HRESULT result
)
1448 nsChannelBSC
*This
= nsChannelBSC_from_BSCallback(bsc
);
1450 if(result
!= E_ABORT
) {
1452 handle_navigation_error(This
, result
);
1453 else if(This
->window
) {
1454 result
= async_stop_request(This
);
1455 if(SUCCEEDED(result
))
1460 on_stop_nsrequest(This
, result
);
1464 static HRESULT
nsChannelBSC_read_data(BSCallback
*bsc
, IStream
*stream
)
1466 nsChannelBSC
*This
= nsChannelBSC_from_BSCallback(bsc
);
1468 return read_stream_data(This
, stream
);
1471 static HRESULT
handle_redirect(nsChannelBSC
*This
, const WCHAR
*new_url
)
1473 nsRedirectCallback
*callback
;
1474 nsIChannelEventSink
*sink
;
1475 nsChannel
*new_channel
;
1479 TRACE("(%p)->(%s)\n", This
, debugstr_w(new_url
));
1481 if(!This
->nschannel
|| !This
->nschannel
->notif_callback
)
1484 nsres
= nsIInterfaceRequestor_GetInterface(This
->nschannel
->notif_callback
, &IID_nsIChannelEventSink
, (void**)&sink
);
1485 if(NS_FAILED(nsres
))
1488 hres
= create_redirect_nschannel(new_url
, This
->nschannel
, &new_channel
);
1489 if(SUCCEEDED(hres
)) {
1490 TRACE("%p %p->%p\n", This
, This
->nschannel
, new_channel
);
1492 hres
= create_redirect_callback(new_channel
, This
, &callback
);
1493 nsIChannel_Release(&new_channel
->nsIHttpChannel_iface
);
1496 if(SUCCEEDED(hres
)) {
1497 nsres
= nsIChannelEventSink_AsyncOnChannelRedirect(sink
, (nsIChannel
*)&This
->nschannel
->nsIHttpChannel_iface
,
1498 (nsIChannel
*)&callback
->nschannel
->nsIHttpChannel_iface
, REDIRECT_TEMPORARY
, /* FIXME */
1499 &callback
->nsIAsyncVerifyRedirectCallback_iface
);
1501 if(NS_FAILED(nsres
))
1502 FIXME("AsyncOnChannelRedirect failed: %08x\n", hres
);
1503 else if(This
->nschannel
!= callback
->nschannel
)
1504 FIXME("nschannel not updated\n");
1506 nsIAsyncVerifyRedirectCallback_Release(&callback
->nsIAsyncVerifyRedirectCallback_iface
);
1509 nsIChannelEventSink_Release(sink
);
1513 static HRESULT
nsChannelBSC_on_progress(BSCallback
*bsc
, ULONG status_code
, LPCWSTR status_text
)
1515 nsChannelBSC
*This
= nsChannelBSC_from_BSCallback(bsc
);
1517 switch(status_code
) {
1518 case BINDSTATUS_MIMETYPEAVAILABLE
:
1519 if(!This
->nschannel
)
1522 heap_free(This
->nschannel
->content_type
);
1523 This
->nschannel
->content_type
= heap_strdupWtoA(status_text
);
1525 case BINDSTATUS_REDIRECTING
:
1526 return handle_redirect(This
, status_text
);
1527 case BINDSTATUS_BEGINDOWNLOADDATA
: {
1528 IWinInetHttpInfo
*http_info
;
1529 DWORD status
, size
= sizeof(DWORD
);
1532 if(!This
->bsc
.binding
)
1535 hres
= IBinding_QueryInterface(This
->bsc
.binding
, &IID_IWinInetHttpInfo
, (void**)&http_info
);
1539 hres
= IWinInetHttpInfo_QueryInfo(http_info
,
1540 HTTP_QUERY_STATUS_CODE
|HTTP_QUERY_FLAG_NUMBER
, &status
, &size
, NULL
, NULL
);
1541 IWinInetHttpInfo_Release(http_info
);
1542 if(FAILED(hres
) || status
== HTTP_STATUS_OK
)
1545 handle_navigation_error(This
, status
);
1552 static HRESULT
nsChannelBSC_on_response(BSCallback
*bsc
, DWORD response_code
,
1553 LPCWSTR response_headers
)
1555 nsChannelBSC
*This
= nsChannelBSC_from_BSCallback(bsc
);
1558 This
->nschannel
->response_status
= response_code
;
1560 if(response_headers
) {
1561 const WCHAR
*headers
;
1563 headers
= strchrW(response_headers
, '\r');
1564 if(headers
&& headers
[1] == '\n') {
1566 hres
= process_response_headers(This
, headers
);
1568 WARN("parsing headers failed: %08x\n", hres
);
1577 static HRESULT
nsChannelBSC_beginning_transaction(BSCallback
*bsc
, WCHAR
**additional_headers
)
1579 nsChannelBSC
*This
= nsChannelBSC_from_BSCallback(bsc
);
1580 http_header_t
*iter
;
1584 static const WCHAR content_lengthW
[] =
1585 {'C','o','n','t','e','n','t','-','L','e','n','g','t','h',0};
1587 if(!This
->nschannel
)
1590 LIST_FOR_EACH_ENTRY(iter
, &This
->nschannel
->request_headers
, http_header_t
, entry
) {
1591 if(strcmpW(iter
->header
, content_lengthW
))
1592 len
+= strlenW(iter
->header
) + 2 /* ": " */ + strlenW(iter
->data
) + 2 /* "\r\n" */;
1598 *additional_headers
= ptr
= CoTaskMemAlloc((len
+1)*sizeof(WCHAR
));
1600 return E_OUTOFMEMORY
;
1602 LIST_FOR_EACH_ENTRY(iter
, &This
->nschannel
->request_headers
, http_header_t
, entry
) {
1603 if(!strcmpW(iter
->header
, content_lengthW
))
1606 len
= strlenW(iter
->header
);
1607 memcpy(ptr
, iter
->header
, len
*sizeof(WCHAR
));
1613 len
= strlenW(iter
->data
);
1614 memcpy(ptr
, iter
->data
, len
*sizeof(WCHAR
));
1626 static const BSCallbackVtbl nsChannelBSCVtbl
= {
1627 nsChannelBSC_destroy
,
1628 nsChannelBSC_init_bindinfo
,
1629 nsChannelBSC_start_binding
,
1630 nsChannelBSC_stop_binding
,
1631 nsChannelBSC_read_data
,
1632 nsChannelBSC_on_progress
,
1633 nsChannelBSC_on_response
,
1634 nsChannelBSC_beginning_transaction
1637 HRESULT
create_channelbsc(IMoniker
*mon
, const WCHAR
*headers
, BYTE
*post_data
, DWORD post_data_size
, nsChannelBSC
**retval
)
1641 ret
= heap_alloc_zero(sizeof(*ret
));
1643 return E_OUTOFMEMORY
;
1645 init_bscallback(&ret
->bsc
, &nsChannelBSCVtbl
, mon
, BINDF_ASYNCHRONOUS
| BINDF_ASYNCSTORAGE
| BINDF_PULLDATA
);
1648 ret
->bsc
.headers
= heap_strdupW(headers
);
1649 if(!ret
->bsc
.headers
) {
1650 IBindStatusCallback_Release(&ret
->bsc
.IBindStatusCallback_iface
);
1651 return E_OUTOFMEMORY
;
1656 ret
->bsc
.post_data
= GlobalAlloc(0, post_data_size
);
1657 if(!ret
->bsc
.post_data
) {
1658 heap_free(ret
->bsc
.headers
);
1659 IBindStatusCallback_Release(&ret
->bsc
.IBindStatusCallback_iface
);
1660 return E_OUTOFMEMORY
;
1663 memcpy(ret
->bsc
.post_data
, post_data
, post_data_size
);
1664 ret
->bsc
.post_data_len
= post_data_size
;
1671 void set_window_bscallback(HTMLWindow
*window
, nsChannelBSC
*callback
)
1673 if(window
->bscallback
) {
1674 if(window
->bscallback
->bsc
.binding
)
1675 IBinding_Abort(window
->bscallback
->bsc
.binding
);
1676 window
->bscallback
->bsc
.doc
= NULL
;
1677 window
->bscallback
->window
= NULL
;
1678 IBindStatusCallback_Release(&window
->bscallback
->bsc
.IBindStatusCallback_iface
);
1681 window
->bscallback
= callback
;
1684 callback
->window
= window
;
1685 IBindStatusCallback_AddRef(&callback
->bsc
.IBindStatusCallback_iface
);
1686 callback
->bsc
.doc
= window
->doc
;
1693 nsChannelBSC
*bscallback
;
1694 } start_doc_binding_task_t
;
1696 static void start_doc_binding_proc(task_t
*_task
)
1698 start_doc_binding_task_t
*task
= (start_doc_binding_task_t
*)_task
;
1700 start_binding(task
->window
, NULL
, (BSCallback
*)task
->bscallback
, NULL
);
1703 static void start_doc_binding_task_destr(task_t
*_task
)
1705 start_doc_binding_task_t
*task
= (start_doc_binding_task_t
*)_task
;
1707 IBindStatusCallback_Release(&task
->bscallback
->bsc
.IBindStatusCallback_iface
);
1711 HRESULT
async_start_doc_binding(HTMLWindow
*window
, nsChannelBSC
*bscallback
)
1713 start_doc_binding_task_t
*task
;
1715 TRACE("%p\n", bscallback
);
1717 task
= heap_alloc(sizeof(start_doc_binding_task_t
));
1719 return E_OUTOFMEMORY
;
1721 task
->window
= window
;
1722 task
->bscallback
= bscallback
;
1723 IBindStatusCallback_AddRef(&bscallback
->bsc
.IBindStatusCallback_iface
);
1725 push_task(&task
->header
, start_doc_binding_proc
, start_doc_binding_task_destr
, window
->task_magic
);
1729 void abort_document_bindings(HTMLDocumentNode
*doc
)
1731 BSCallback
*iter
, *next
;
1733 LIST_FOR_EACH_ENTRY_SAFE(iter
, next
, &doc
->bindings
, BSCallback
, entry
) {
1734 TRACE("Aborting %p\n", iter
);
1737 remove_target_tasks(iter
->doc
->basedoc
.task_magic
);
1740 IBinding_Abort(iter
->binding
);
1742 list_remove(&iter
->entry
);
1743 list_init(&iter
->entry
);
1744 iter
->vtbl
->stop_binding(iter
, E_ABORT
);
1751 HRESULT
channelbsc_load_stream(nsChannelBSC
*bscallback
, IStream
*stream
)
1753 HRESULT hres
= S_OK
;
1755 if(!bscallback
->nschannel
) {
1756 ERR("NULL nschannel\n");
1760 bscallback
->nschannel
->content_type
= heap_strdupA("text/html");
1761 if(!bscallback
->nschannel
->content_type
)
1762 return E_OUTOFMEMORY
;
1764 list_add_head(&bscallback
->bsc
.doc
->bindings
, &bscallback
->bsc
.entry
);
1766 hres
= read_stream_data(bscallback
, stream
);
1768 hres
= async_stop_request(bscallback
);
1770 IBindStatusCallback_OnStopBinding(&bscallback
->bsc
.IBindStatusCallback_iface
, hres
,
1776 void channelbsc_set_channel(nsChannelBSC
*This
, nsChannel
*channel
, nsIStreamListener
*listener
, nsISupports
*context
)
1778 nsIChannel_AddRef(&channel
->nsIHttpChannel_iface
);
1779 This
->nschannel
= channel
;
1781 nsIStreamListener_AddRef(listener
);
1782 This
->nslistener
= listener
;
1785 nsISupports_AddRef(context
);
1786 This
->nscontext
= context
;
1789 if(This
->bsc
.headers
) {
1792 hres
= parse_headers(This
->bsc
.headers
, &channel
->request_headers
);
1793 heap_free(This
->bsc
.headers
);
1794 This
->bsc
.headers
= NULL
;
1796 WARN("parse_headers failed: %08x\n", hres
);
1804 } navigate_javascript_task_t
;
1806 static void navigate_javascript_proc(task_t
*_task
)
1808 navigate_javascript_task_t
*task
= (navigate_javascript_task_t
*)_task
;
1809 HTMLWindow
*window
= task
->window
;
1814 static const WCHAR jscriptW
[] = {'j','s','c','r','i','p','t',0};
1816 task
->window
->readystate
= READYSTATE_COMPLETE
;
1818 hres
= IUri_GetPath(task
->uri
, &code
);
1822 set_download_state(window
->doc_obj
, 1);
1824 V_VT(&v
) = VT_EMPTY
;
1825 hres
= exec_script(window
, code
, jscriptW
, &v
);
1826 SysFreeString(code
);
1827 if(SUCCEEDED(hres
) && V_VT(&v
) != VT_EMPTY
) {
1828 FIXME("javascirpt URL returned %s\n", debugstr_variant(&v
));
1832 if(window
->doc_obj
->view_sink
)
1833 IAdviseSink_OnViewChange(window
->doc_obj
->view_sink
, DVASPECT_CONTENT
, -1);
1835 set_download_state(window
->doc_obj
, 0);
1838 static void navigate_javascript_task_destr(task_t
*_task
)
1840 navigate_javascript_task_t
*task
= (navigate_javascript_task_t
*)_task
;
1842 IUri_Release(task
->uri
);
1849 nsChannelBSC
*bscallback
;
1853 static void navigate_proc(task_t
*_task
)
1855 navigate_task_t
*task
= (navigate_task_t
*)_task
;
1858 hres
= set_moniker(&task
->window
->doc_obj
->basedoc
, task
->mon
, NULL
, task
->bscallback
, TRUE
);
1860 start_binding(task
->window
, NULL
, (BSCallback
*)task
->bscallback
, NULL
);
1863 static void navigate_task_destr(task_t
*_task
)
1865 navigate_task_t
*task
= (navigate_task_t
*)_task
;
1867 IUnknown_Release((IUnknown
*)task
->bscallback
);
1868 IMoniker_Release(task
->mon
);
1872 static HRESULT
navigate_fragment(HTMLWindow
*window
, IUri
*uri
)
1874 nsIDOMLocation
*nslocation
;
1875 nsAString nsfrag_str
;
1880 set_current_uri(window
, uri
);
1882 nsres
= nsIDOMWindow_GetLocation(window
->nswindow
, &nslocation
);
1883 if(FAILED(nsres
) || !nslocation
)
1886 hres
= IUri_GetFragment(uri
, &frag
);
1888 nsIDOMLocation_Release(nslocation
);
1892 nsAString_InitDepend(&nsfrag_str
, frag
);
1893 nsres
= nsIDOMLocation_SetHash(nslocation
, &nsfrag_str
);
1894 nsAString_Finish(&nsfrag_str
);
1895 nsIDOMLocation_Release(nslocation
);
1896 SysFreeString(frag
);
1897 if(NS_FAILED(nsres
)) {
1898 ERR("SetHash failed: %08x\n", nsres
);
1902 if(window
->doc_obj
->doc_object_service
) {
1903 IDocObjectService_FireNavigateComplete2(window
->doc_obj
->doc_object_service
, &window
->IHTMLWindow2_iface
, 0x10);
1904 IDocObjectService_FireDocumentComplete(window
->doc_obj
->doc_object_service
, &window
->IHTMLWindow2_iface
, 0);
1911 HRESULT
super_navigate(HTMLWindow
*window
, IUri
*uri
, const WCHAR
*headers
, BYTE
*post_data
, DWORD post_data_size
)
1918 if(window
->doc_obj
->client
) {
1919 IOleCommandTarget
*cmdtrg
;
1921 hres
= IOleClientSite_QueryInterface(window
->doc_obj
->client
, &IID_IOleCommandTarget
, (void**)&cmdtrg
);
1922 if(SUCCEEDED(hres
)) {
1926 hres
= IUri_GetDisplayUri(uri
, &url_str
);
1927 if(SUCCEEDED(hres
)) {
1928 V_VT(&in
) = VT_BSTR
;
1929 V_BSTR(&in
) = url_str
;
1930 V_VT(&out
) = VT_BOOL
;
1931 V_BOOL(&out
) = VARIANT_TRUE
;
1932 hres
= IOleCommandTarget_Exec(cmdtrg
, &CGID_ShellDocView
, 67, 0, &in
, &out
);
1933 IOleCommandTarget_Release(cmdtrg
);
1936 SysFreeString(url_str
);
1941 if(window
->uri
&& !post_data_size
&& compare_ignoring_frag(window
->uri
, uri
)) {
1942 TRACE("fragment navigate\n");
1943 return navigate_fragment(window
, uri
);
1946 hres
= CreateURLMonikerEx2(NULL
, uri
, &mon
, URL_MK_UNIFORM
);
1950 /* FIXME: Why not set_ready_state? */
1951 window
->readystate
= READYSTATE_UNINITIALIZED
;
1953 hres
= create_channelbsc(mon
, headers
, post_data
, post_data_size
, &bsc
);
1955 IMoniker_Release(mon
);
1959 prepare_for_binding(&window
->doc_obj
->basedoc
, mon
, TRUE
);
1961 hres
= IUri_GetScheme(uri
, &scheme
);
1962 if(SUCCEEDED(hres
) && scheme
!= URL_SCHEME_JAVASCRIPT
) {
1963 navigate_task_t
*task
;
1965 task
= heap_alloc(sizeof(*task
));
1967 IUnknown_Release((IUnknown
*)bsc
);
1968 IMoniker_Release(mon
);
1969 return E_OUTOFMEMORY
;
1972 task
->window
= window
;
1973 task
->bscallback
= bsc
;
1975 push_task(&task
->header
, navigate_proc
, navigate_task_destr
, window
->task_magic
);
1977 /* Silently and repeated when real loading starts? */
1978 window
->readystate
= READYSTATE_LOADING
;
1980 navigate_javascript_task_t
*task
;
1982 IUnknown_Release((IUnknown
*)bsc
);
1983 IMoniker_Release(mon
);
1985 task
= heap_alloc(sizeof(*task
));
1987 return E_OUTOFMEMORY
;
1990 task
->window
= window
;
1992 push_task(&task
->header
, navigate_javascript_proc
, navigate_javascript_task_destr
, window
->task_magic
);
1995 window
->readystate
= READYSTATE_COMPLETE
;
2001 HRESULT
navigate_new_window(HTMLWindow
*window
, IUri
*uri
, const WCHAR
*name
, IHTMLWindow2
**ret
)
2003 IWebBrowser2
*web_browser
;
2004 IHTMLWindow2
*new_window
;
2009 hres
= create_channelbsc(NULL
, NULL
, NULL
, 0, &bsc
);
2013 hres
= CreateAsyncBindCtx(0, &bsc
->bsc
.IBindStatusCallback_iface
, NULL
, &bind_ctx
);
2015 IBindStatusCallback_Release(&bsc
->bsc
.IBindStatusCallback_iface
);
2019 hres
= CoCreateInstance(&CLSID_InternetExplorer
, NULL
, CLSCTX_LOCAL_SERVER
,
2020 &IID_IWebBrowser2
, (void**)&web_browser
);
2021 if(SUCCEEDED(hres
)) {
2022 ITargetFramePriv2
*target_frame_priv
;
2024 hres
= IWebBrowser_QueryInterface(web_browser
, &IID_ITargetFramePriv2
, (void**)&target_frame_priv
);
2025 if(SUCCEEDED(hres
)) {
2026 hres
= ITargetFramePriv2_AggregatedNavigation2(target_frame_priv
,
2027 HLNF_DISABLEWINDOWRESTRICTIONS
|HLNF_OPENINNEWWINDOW
, bind_ctx
, &bsc
->bsc
.IBindStatusCallback_iface
,
2029 ITargetFramePriv2_Release(target_frame_priv
);
2032 hres
= do_query_service((IUnknown
*)web_browser
, &SID_SHTMLWindow
, &IID_IHTMLWindow2
, (void**)&new_window
);
2035 IWebBrowser2_Quit(web_browser
);
2036 IWebBrowser2_Release(web_browser
);
2039 WARN("Could not create InternetExplorer instance: %08x\n", hres
);
2042 IBindStatusCallback_Release(&bsc
->bsc
.IBindStatusCallback_iface
);
2043 IBindCtx_Release(bind_ctx
);
2047 IWebBrowser2_put_Visible(web_browser
, VARIANT_TRUE
);
2048 IWebBrowser2_Release(web_browser
);
2053 IHTMLWindow2_Release(new_window
);
2057 HRESULT
hlink_frame_navigate(HTMLDocument
*doc
, LPCWSTR url
, nsChannel
*nschannel
, DWORD hlnf
, BOOL
*cancel
)
2059 IHlinkFrame
*hlink_frame
;
2060 nsChannelBSC
*callback
;
2068 hres
= do_query_service((IUnknown
*)doc
->doc_obj
->client
, &IID_IHlinkFrame
, &IID_IHlinkFrame
,
2069 (void**)&hlink_frame
);
2073 hres
= create_channelbsc(NULL
, NULL
, NULL
, 0, &callback
);
2075 IHlinkFrame_Release(hlink_frame
);
2080 read_post_data_stream(callback
, nschannel
);
2082 hres
= CreateAsyncBindCtx(0, &callback
->bsc
.IBindStatusCallback_iface
, NULL
, &bindctx
);
2084 hres
= CoCreateInstance(&CLSID_StdHlink
, NULL
, CLSCTX_INPROC_SERVER
,
2085 &IID_IHlink
, (LPVOID
*)&hlink
);
2088 hres
= CreateURLMoniker(NULL
, url
, &mon
);
2090 if(SUCCEEDED(hres
)) {
2091 IHlink_SetMonikerReference(hlink
, HLINKSETF_TARGET
, mon
, NULL
);
2093 if(hlnf
& HLNF_OPENINNEWWINDOW
) {
2094 static const WCHAR wszBlank
[] = {'_','b','l','a','n','k',0};
2095 IHlink_SetTargetFrameName(hlink
, wszBlank
); /* FIXME */
2098 hres
= IHlinkFrame_Navigate(hlink_frame
, hlnf
, bindctx
,
2099 &callback
->bsc
.IBindStatusCallback_iface
, hlink
);
2100 IMoniker_Release(mon
);
2101 *cancel
= hres
== S_OK
;
2105 IHlinkFrame_Release(hlink_frame
);
2106 IBindCtx_Release(bindctx
);
2107 IBindStatusCallback_Release(&callback
->bsc
.IBindStatusCallback_iface
);
2111 HRESULT
navigate_url(HTMLWindow
*window
, const WCHAR
*new_url
, const WCHAR
*base_url
)
2113 WCHAR url
[INTERNET_MAX_URL_LENGTH
];
2119 }else if(base_url
) {
2122 hres
= CoInternetCombineUrl(base_url
, new_url
, URL_ESCAPE_SPACES_ONLY
|URL_DONT_ESCAPE_EXTRA_INFO
,
2123 url
, sizeof(url
)/sizeof(WCHAR
), &len
, 0);
2127 strcpyW(url
, new_url
);
2130 if(window
->doc_obj
&& window
->doc_obj
->hostui
) {
2131 OLECHAR
*translated_url
= NULL
;
2133 hres
= IDocHostUIHandler_TranslateUrl(window
->doc_obj
->hostui
, 0, url
,
2136 TRACE("%08x %s -> %s\n", hres
, debugstr_w(url
), debugstr_w(translated_url
));
2137 strcpyW(url
, translated_url
);
2138 CoTaskMemFree(translated_url
);
2142 if(window
->doc_obj
&& window
->doc_obj
->is_webbrowser
&& window
== window
->doc_obj
->basedoc
.window
) {
2143 BOOL cancel
= FALSE
;
2146 hres
= IDocObjectService_FireBeforeNavigate2(window
->doc_obj
->doc_object_service
, NULL
, url
, 0x40,
2147 NULL
, NULL
, 0, NULL
, TRUE
, &cancel
);
2148 if(SUCCEEDED(hres
) && cancel
) {
2149 TRACE("Navigation canceled\n");
2153 hres
= CreateUri(url
, 0, 0, &uri
);
2157 hres
= super_navigate(window
, uri
, NULL
, NULL
, 0);
2162 if(window
->doc_obj
&& window
== window
->doc_obj
->basedoc
.window
) {
2165 hres
= hlink_frame_navigate(&window
->doc
->basedoc
, url
, NULL
, 0, &cancel
);
2170 TRACE("Navigation handled by hlink frame\n");
2175 hres
= create_doc_uri(window
, url
, &uri
);
2179 hres
= load_nsuri(window
, uri
, NULL
, LOAD_FLAGS_NONE
);
2180 nsISupports_Release((nsISupports
*)uri
);