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};
51 static const WCHAR text_htmlW
[] = {'t','e','x','t','/','h','t','m','l',0};
53 struct nsProtocolStream
{
54 nsIInputStream nsIInputStream_iface
;
62 struct BSCallbackVtbl
{
63 void (*destroy
)(BSCallback
*);
64 HRESULT (*init_bindinfo
)(BSCallback
*);
65 HRESULT (*start_binding
)(BSCallback
*);
66 HRESULT (*stop_binding
)(BSCallback
*,HRESULT
);
67 HRESULT (*read_data
)(BSCallback
*,IStream
*);
68 HRESULT (*on_progress
)(BSCallback
*,ULONG
,LPCWSTR
);
69 HRESULT (*on_response
)(BSCallback
*,DWORD
,LPCWSTR
);
70 HRESULT (*beginning_transaction
)(BSCallback
*,WCHAR
**);
73 static inline nsProtocolStream
*impl_from_nsIInputStream(nsIInputStream
*iface
)
75 return CONTAINING_RECORD(iface
, nsProtocolStream
, nsIInputStream_iface
);
78 static nsresult NSAPI
nsInputStream_QueryInterface(nsIInputStream
*iface
, nsIIDRef riid
,
81 nsProtocolStream
*This
= impl_from_nsIInputStream(iface
);
85 if(IsEqualGUID(&IID_nsISupports
, riid
)) {
86 TRACE("(%p)->(IID_nsISupports %p)\n", This
, result
);
87 *result
= &This
->nsIInputStream_iface
;
88 }else if(IsEqualGUID(&IID_nsIInputStream
, riid
)) {
89 TRACE("(%p)->(IID_nsIInputStream %p)\n", This
, result
);
90 *result
= &This
->nsIInputStream_iface
;
94 nsIInputStream_AddRef(&This
->nsIInputStream_iface
);
98 WARN("unsupported interface %s\n", debugstr_guid(riid
));
99 return NS_NOINTERFACE
;
102 static nsrefcnt NSAPI
nsInputStream_AddRef(nsIInputStream
*iface
)
104 nsProtocolStream
*This
= impl_from_nsIInputStream(iface
);
105 LONG ref
= InterlockedIncrement(&This
->ref
);
107 TRACE("(%p) ref=%d\n", This
, ref
);
113 static nsrefcnt NSAPI
nsInputStream_Release(nsIInputStream
*iface
)
115 nsProtocolStream
*This
= impl_from_nsIInputStream(iface
);
116 LONG ref
= InterlockedDecrement(&This
->ref
);
118 TRACE("(%p) ref=%d\n", This
, ref
);
126 static nsresult NSAPI
nsInputStream_Close(nsIInputStream
*iface
)
128 nsProtocolStream
*This
= impl_from_nsIInputStream(iface
);
129 FIXME("(%p)\n", This
);
130 return NS_ERROR_NOT_IMPLEMENTED
;
133 static nsresult NSAPI
nsInputStream_Available(nsIInputStream
*iface
, PRUint32
*_retval
)
135 nsProtocolStream
*This
= impl_from_nsIInputStream(iface
);
136 FIXME("(%p)->(%p)\n", This
, _retval
);
137 return NS_ERROR_NOT_IMPLEMENTED
;
140 static nsresult NSAPI
nsInputStream_Read(nsIInputStream
*iface
, char *aBuf
, PRUint32 aCount
,
143 nsProtocolStream
*This
= impl_from_nsIInputStream(iface
);
146 TRACE("(%p)->(%p %d %p)\n", This
, aBuf
, aCount
, _retval
);
148 if(read
> This
->buf_size
)
149 read
= This
->buf_size
;
152 memcpy(aBuf
, This
->buf
, read
);
153 if(read
< This
->buf_size
)
154 memmove(This
->buf
, This
->buf
+read
, This
->buf_size
-read
);
155 This
->buf_size
-= read
;
162 static nsresult NSAPI
nsInputStream_ReadSegments(nsIInputStream
*iface
,
163 nsresult (WINAPI
*aWriter
)(nsIInputStream
*,void*,const char*,PRUint32
,PRUint32
,PRUint32
*),
164 void *aClousure
, PRUint32 aCount
, PRUint32
*_retval
)
166 nsProtocolStream
*This
= impl_from_nsIInputStream(iface
);
167 PRUint32 written
= 0;
170 TRACE("(%p)->(%p %p %d %p)\n", This
, aWriter
, aClousure
, aCount
, _retval
);
175 if(aCount
> This
->buf_size
)
176 aCount
= This
->buf_size
;
178 nsres
= aWriter(&This
->nsIInputStream_iface
, aClousure
, This
->buf
, 0, aCount
, &written
);
180 TRACE("aWritter failed: %08x\n", nsres
);
181 else if(written
!= This
->buf_size
)
182 FIXME("written %d != buf_size %d\n", written
, This
->buf_size
);
184 This
->buf_size
-= written
;
190 static nsresult NSAPI
nsInputStream_IsNonBlocking(nsIInputStream
*iface
, cpp_bool
*_retval
)
192 nsProtocolStream
*This
= impl_from_nsIInputStream(iface
);
193 FIXME("(%p)->(%p)\n", This
, _retval
);
194 return NS_ERROR_NOT_IMPLEMENTED
;
197 static const nsIInputStreamVtbl nsInputStreamVtbl
= {
198 nsInputStream_QueryInterface
,
199 nsInputStream_AddRef
,
200 nsInputStream_Release
,
202 nsInputStream_Available
,
204 nsInputStream_ReadSegments
,
205 nsInputStream_IsNonBlocking
208 static nsProtocolStream
*create_nsprotocol_stream(void)
210 nsProtocolStream
*ret
= heap_alloc(sizeof(nsProtocolStream
));
212 ret
->nsIInputStream_iface
.lpVtbl
= &nsInputStreamVtbl
;
219 static inline BSCallback
*impl_from_IBindStatusCallback(IBindStatusCallback
*iface
)
221 return CONTAINING_RECORD(iface
, BSCallback
, IBindStatusCallback_iface
);
224 static HRESULT WINAPI
BindStatusCallback_QueryInterface(IBindStatusCallback
*iface
,
225 REFIID riid
, void **ppv
)
227 BSCallback
*This
= impl_from_IBindStatusCallback(iface
);
230 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
231 TRACE("(%p)->(IID_IUnknown, %p)\n", This
, ppv
);
232 *ppv
= &This
->IBindStatusCallback_iface
;
233 }else if(IsEqualGUID(&IID_IBindStatusCallback
, riid
)) {
234 TRACE("(%p)->(IID_IBindStatusCallback, %p)\n", This
, ppv
);
235 *ppv
= &This
->IBindStatusCallback_iface
;
236 }else if(IsEqualGUID(&IID_IServiceProvider
, riid
)) {
237 TRACE("(%p)->(IID_IServiceProvider %p)\n", This
, ppv
);
238 *ppv
= &This
->IServiceProvider_iface
;
239 }else if(IsEqualGUID(&IID_IHttpNegotiate
, riid
)) {
240 TRACE("(%p)->(IID_IHttpNegotiate %p)\n", This
, ppv
);
241 *ppv
= &This
->IHttpNegotiate2_iface
;
242 }else if(IsEqualGUID(&IID_IHttpNegotiate2
, riid
)) {
243 TRACE("(%p)->(IID_IHttpNegotiate2 %p)\n", This
, ppv
);
244 *ppv
= &This
->IHttpNegotiate2_iface
;
245 }else if(IsEqualGUID(&IID_IInternetBindInfo
, riid
)) {
246 TRACE("(%p)->(IID_IInternetBindInfo %p)\n", This
, ppv
);
247 *ppv
= &This
->IInternetBindInfo_iface
;
251 IBindStatusCallback_AddRef(&This
->IBindStatusCallback_iface
);
255 TRACE("Unsupported riid = %s\n", debugstr_guid(riid
));
256 return E_NOINTERFACE
;
259 static ULONG WINAPI
BindStatusCallback_AddRef(IBindStatusCallback
*iface
)
261 BSCallback
*This
= impl_from_IBindStatusCallback(iface
);
262 LONG ref
= InterlockedIncrement(&This
->ref
);
264 TRACE("(%p) ref = %d\n", This
, ref
);
269 static ULONG WINAPI
BindStatusCallback_Release(IBindStatusCallback
*iface
)
271 BSCallback
*This
= impl_from_IBindStatusCallback(iface
);
272 LONG ref
= InterlockedDecrement(&This
->ref
);
274 TRACE("(%p) ref = %d\n", This
, ref
);
278 GlobalFree(This
->post_data
);
280 IMoniker_Release(This
->mon
);
282 IBinding_Release(This
->binding
);
283 list_remove(&This
->entry
);
284 list_init(&This
->entry
);
285 heap_free(This
->headers
);
287 This
->vtbl
->destroy(This
);
293 static HRESULT WINAPI
BindStatusCallback_OnStartBinding(IBindStatusCallback
*iface
,
294 DWORD dwReserved
, IBinding
*pbind
)
296 BSCallback
*This
= impl_from_IBindStatusCallback(iface
);
298 TRACE("(%p)->(%d %p)\n", This
, dwReserved
, pbind
);
300 IBinding_AddRef(pbind
);
301 This
->binding
= pbind
;
304 list_add_head(&This
->window
->bindings
, &This
->entry
);
306 return This
->vtbl
->start_binding(This
);
309 static HRESULT WINAPI
BindStatusCallback_GetPriority(IBindStatusCallback
*iface
, LONG
*pnPriority
)
311 BSCallback
*This
= impl_from_IBindStatusCallback(iface
);
312 FIXME("(%p)->(%p)\n", This
, pnPriority
);
316 static HRESULT WINAPI
BindStatusCallback_OnLowResource(IBindStatusCallback
*iface
, DWORD reserved
)
318 BSCallback
*This
= impl_from_IBindStatusCallback(iface
);
319 FIXME("(%p)->(%d)\n", This
, reserved
);
323 static HRESULT WINAPI
BindStatusCallback_OnProgress(IBindStatusCallback
*iface
, ULONG ulProgress
,
324 ULONG ulProgressMax
, ULONG ulStatusCode
, LPCWSTR szStatusText
)
326 BSCallback
*This
= impl_from_IBindStatusCallback(iface
);
328 TRACE("%p)->(%u %u %u %s)\n", This
, ulProgress
, ulProgressMax
, ulStatusCode
,
329 debugstr_w(szStatusText
));
331 return This
->vtbl
->on_progress(This
, ulStatusCode
, szStatusText
);
334 static HRESULT WINAPI
BindStatusCallback_OnStopBinding(IBindStatusCallback
*iface
,
335 HRESULT hresult
, LPCWSTR szError
)
337 BSCallback
*This
= impl_from_IBindStatusCallback(iface
);
340 TRACE("(%p)->(%08x %s)\n", This
, hresult
, debugstr_w(szError
));
342 /* NOTE: IE7 calls GetBindResult here */
344 hres
= This
->vtbl
->stop_binding(This
, hresult
);
347 IBinding_Release(This
->binding
);
348 This
->binding
= NULL
;
351 list_remove(&This
->entry
);
352 list_init(&This
->entry
);
358 static HRESULT WINAPI
BindStatusCallback_GetBindInfo(IBindStatusCallback
*iface
,
359 DWORD
*grfBINDF
, BINDINFO
*pbindinfo
)
361 BSCallback
*This
= impl_from_IBindStatusCallback(iface
);
364 TRACE("(%p)->(%p %p)\n", This
, grfBINDF
, pbindinfo
);
366 if(!This
->bindinfo_ready
) {
369 hres
= This
->vtbl
->init_bindinfo(This
);
373 This
->bindinfo_ready
= TRUE
;
376 *grfBINDF
= This
->bindf
;
378 size
= pbindinfo
->cbSize
;
379 memset(pbindinfo
, 0, size
);
380 pbindinfo
->cbSize
= size
;
382 pbindinfo
->cbstgmedData
= This
->post_data_len
;
383 pbindinfo
->dwCodePage
= CP_UTF8
;
384 pbindinfo
->dwOptions
= 0x80000;
386 if(This
->post_data
) {
387 pbindinfo
->dwBindVerb
= BINDVERB_POST
;
389 pbindinfo
->stgmedData
.tymed
= TYMED_HGLOBAL
;
390 pbindinfo
->stgmedData
.u
.hGlobal
= This
->post_data
;
391 pbindinfo
->stgmedData
.pUnkForRelease
= (IUnknown
*)&This
->IBindStatusCallback_iface
;
392 IBindStatusCallback_AddRef(&This
->IBindStatusCallback_iface
);
398 static HRESULT WINAPI
BindStatusCallback_OnDataAvailable(IBindStatusCallback
*iface
,
399 DWORD grfBSCF
, DWORD dwSize
, FORMATETC
*pformatetc
, STGMEDIUM
*pstgmed
)
401 BSCallback
*This
= impl_from_IBindStatusCallback(iface
);
403 TRACE("(%p)->(%08x %d %p %p)\n", This
, grfBSCF
, dwSize
, pformatetc
, pstgmed
);
405 return This
->vtbl
->read_data(This
, pstgmed
->u
.pstm
);
408 static HRESULT WINAPI
BindStatusCallback_OnObjectAvailable(IBindStatusCallback
*iface
,
409 REFIID riid
, IUnknown
*punk
)
411 BSCallback
*This
= impl_from_IBindStatusCallback(iface
);
412 FIXME("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), punk
);
416 static const IBindStatusCallbackVtbl BindStatusCallbackVtbl
= {
417 BindStatusCallback_QueryInterface
,
418 BindStatusCallback_AddRef
,
419 BindStatusCallback_Release
,
420 BindStatusCallback_OnStartBinding
,
421 BindStatusCallback_GetPriority
,
422 BindStatusCallback_OnLowResource
,
423 BindStatusCallback_OnProgress
,
424 BindStatusCallback_OnStopBinding
,
425 BindStatusCallback_GetBindInfo
,
426 BindStatusCallback_OnDataAvailable
,
427 BindStatusCallback_OnObjectAvailable
430 static inline BSCallback
*impl_from_IHttpNegotiate2(IHttpNegotiate2
*iface
)
432 return CONTAINING_RECORD(iface
, BSCallback
, IHttpNegotiate2_iface
);
435 static HRESULT WINAPI
HttpNegotiate_QueryInterface(IHttpNegotiate2
*iface
,
436 REFIID riid
, void **ppv
)
438 BSCallback
*This
= impl_from_IHttpNegotiate2(iface
);
439 return IBindStatusCallback_QueryInterface(&This
->IBindStatusCallback_iface
, riid
, ppv
);
442 static ULONG WINAPI
HttpNegotiate_AddRef(IHttpNegotiate2
*iface
)
444 BSCallback
*This
= impl_from_IHttpNegotiate2(iface
);
445 return IBindStatusCallback_AddRef(&This
->IBindStatusCallback_iface
);
448 static ULONG WINAPI
HttpNegotiate_Release(IHttpNegotiate2
*iface
)
450 BSCallback
*This
= impl_from_IHttpNegotiate2(iface
);
451 return IBindStatusCallback_Release(&This
->IBindStatusCallback_iface
);
454 static HRESULT WINAPI
HttpNegotiate_BeginningTransaction(IHttpNegotiate2
*iface
,
455 LPCWSTR szURL
, LPCWSTR szHeaders
, DWORD dwReserved
, LPWSTR
*pszAdditionalHeaders
)
457 BSCallback
*This
= impl_from_IHttpNegotiate2(iface
);
460 TRACE("(%p)->(%s %s %d %p)\n", This
, debugstr_w(szURL
), debugstr_w(szHeaders
),
461 dwReserved
, pszAdditionalHeaders
);
463 *pszAdditionalHeaders
= NULL
;
465 hres
= This
->vtbl
->beginning_transaction(This
, pszAdditionalHeaders
);
472 size
= (strlenW(This
->headers
)+1)*sizeof(WCHAR
);
473 *pszAdditionalHeaders
= CoTaskMemAlloc(size
);
474 if(!*pszAdditionalHeaders
)
475 return E_OUTOFMEMORY
;
476 memcpy(*pszAdditionalHeaders
, This
->headers
, size
);
482 static HRESULT WINAPI
HttpNegotiate_OnResponse(IHttpNegotiate2
*iface
, DWORD dwResponseCode
,
483 LPCWSTR szResponseHeaders
, LPCWSTR szRequestHeaders
, LPWSTR
*pszAdditionalRequestHeaders
)
485 BSCallback
*This
= impl_from_IHttpNegotiate2(iface
);
487 TRACE("(%p)->(%d %s %s %p)\n", This
, dwResponseCode
, debugstr_w(szResponseHeaders
),
488 debugstr_w(szRequestHeaders
), pszAdditionalRequestHeaders
);
490 return This
->vtbl
->on_response(This
, dwResponseCode
, szResponseHeaders
);
493 static HRESULT WINAPI
HttpNegotiate_GetRootSecurityId(IHttpNegotiate2
*iface
,
494 BYTE
*pbSecurityId
, DWORD
*pcbSecurityId
, DWORD_PTR dwReserved
)
496 BSCallback
*This
= impl_from_IHttpNegotiate2(iface
);
497 FIXME("(%p)->(%p %p %ld)\n", This
, pbSecurityId
, pcbSecurityId
, dwReserved
);
501 static const IHttpNegotiate2Vtbl HttpNegotiate2Vtbl
= {
502 HttpNegotiate_QueryInterface
,
503 HttpNegotiate_AddRef
,
504 HttpNegotiate_Release
,
505 HttpNegotiate_BeginningTransaction
,
506 HttpNegotiate_OnResponse
,
507 HttpNegotiate_GetRootSecurityId
510 static inline BSCallback
*impl_from_IInternetBindInfo(IInternetBindInfo
*iface
)
512 return CONTAINING_RECORD(iface
, BSCallback
, IInternetBindInfo_iface
);
515 static HRESULT WINAPI
InternetBindInfo_QueryInterface(IInternetBindInfo
*iface
,
516 REFIID riid
, void **ppv
)
518 BSCallback
*This
= impl_from_IInternetBindInfo(iface
);
519 return IBindStatusCallback_QueryInterface(&This
->IBindStatusCallback_iface
, riid
, ppv
);
522 static ULONG WINAPI
InternetBindInfo_AddRef(IInternetBindInfo
*iface
)
524 BSCallback
*This
= impl_from_IInternetBindInfo(iface
);
525 return IBindStatusCallback_AddRef(&This
->IBindStatusCallback_iface
);
528 static ULONG WINAPI
InternetBindInfo_Release(IInternetBindInfo
*iface
)
530 BSCallback
*This
= impl_from_IInternetBindInfo(iface
);
531 return IBindStatusCallback_Release(&This
->IBindStatusCallback_iface
);
534 static HRESULT WINAPI
InternetBindInfo_GetBindInfo(IInternetBindInfo
*iface
,
535 DWORD
*grfBINDF
, BINDINFO
*pbindinfo
)
537 BSCallback
*This
= impl_from_IInternetBindInfo(iface
);
538 FIXME("(%p)->(%p %p)\n", This
, grfBINDF
, pbindinfo
);
542 static HRESULT WINAPI
InternetBindInfo_GetBindString(IInternetBindInfo
*iface
,
543 ULONG ulStringType
, LPOLESTR
*ppwzStr
, ULONG cEl
, ULONG
*pcElFetched
)
545 BSCallback
*This
= impl_from_IInternetBindInfo(iface
);
546 FIXME("(%p)->(%u %p %u %p)\n", This
, ulStringType
, ppwzStr
, cEl
, pcElFetched
);
550 static const IInternetBindInfoVtbl InternetBindInfoVtbl
= {
551 InternetBindInfo_QueryInterface
,
552 InternetBindInfo_AddRef
,
553 InternetBindInfo_Release
,
554 InternetBindInfo_GetBindInfo
,
555 InternetBindInfo_GetBindString
558 static inline BSCallback
*impl_from_IServiceProvider(IServiceProvider
*iface
)
560 return CONTAINING_RECORD(iface
, BSCallback
, IServiceProvider_iface
);
563 static HRESULT WINAPI
BSCServiceProvider_QueryInterface(IServiceProvider
*iface
,
564 REFIID riid
, void **ppv
)
566 BSCallback
*This
= impl_from_IServiceProvider(iface
);
567 return IBindStatusCallback_QueryInterface(&This
->IBindStatusCallback_iface
, riid
, ppv
);
570 static ULONG WINAPI
BSCServiceProvider_AddRef(IServiceProvider
*iface
)
572 BSCallback
*This
= impl_from_IServiceProvider(iface
);
573 return IBindStatusCallback_AddRef(&This
->IBindStatusCallback_iface
);
576 static ULONG WINAPI
BSCServiceProvider_Release(IServiceProvider
*iface
)
578 BSCallback
*This
= impl_from_IServiceProvider(iface
);
579 return IBindStatusCallback_Release(&This
->IBindStatusCallback_iface
);
582 static HRESULT WINAPI
BSCServiceProvider_QueryService(IServiceProvider
*iface
,
583 REFGUID guidService
, REFIID riid
, void **ppv
)
585 BSCallback
*This
= impl_from_IServiceProvider(iface
);
587 TRACE("(%p)->(%s %s %p)\n", This
, debugstr_guid(guidService
), debugstr_guid(riid
), ppv
);
589 if(This
->window
&& IsEqualGUID(guidService
, &IID_IWindowForBindingUI
))
590 return IServiceProvider_QueryService(&This
->window
->base
.IServiceProvider_iface
, guidService
, riid
, ppv
);
591 return E_NOINTERFACE
;
594 static const IServiceProviderVtbl ServiceProviderVtbl
= {
595 BSCServiceProvider_QueryInterface
,
596 BSCServiceProvider_AddRef
,
597 BSCServiceProvider_Release
,
598 BSCServiceProvider_QueryService
601 static void init_bscallback(BSCallback
*This
, const BSCallbackVtbl
*vtbl
, IMoniker
*mon
, DWORD bindf
)
603 This
->IBindStatusCallback_iface
.lpVtbl
= &BindStatusCallbackVtbl
;
604 This
->IServiceProvider_iface
.lpVtbl
= &ServiceProviderVtbl
;
605 This
->IHttpNegotiate2_iface
.lpVtbl
= &HttpNegotiate2Vtbl
;
606 This
->IInternetBindInfo_iface
.lpVtbl
= &InternetBindInfoVtbl
;
611 list_init(&This
->entry
);
614 IMoniker_AddRef(mon
);
618 static HRESULT
read_stream(BSCallback
*This
, IStream
*stream
, void *buf
, DWORD size
, DWORD
*ret_size
)
623 hres
= IStream_Read(stream
, buf
, size
, &read_size
);
624 This
->readed
+= (*ret_size
= read_size
);
628 static void parse_content_type(nsChannelBSC
*This
, const WCHAR
*value
)
633 static const WCHAR charsetW
[] = {'c','h','a','r','s','e','t','='};
635 ptr
= strchrW(value
, ';');
640 while(*ptr
&& isspaceW(*ptr
))
643 len
= strlenW(value
);
644 if(ptr
+ sizeof(charsetW
)/sizeof(WCHAR
) < value
+len
&& !memicmpW(ptr
, charsetW
, sizeof(charsetW
)/sizeof(WCHAR
))) {
645 size_t charset_len
, lena
;
646 nsACString charset_str
;
647 const WCHAR
*charset
;
650 ptr
+= sizeof(charsetW
)/sizeof(WCHAR
);
653 FIXME("Quoted value\n");
657 while(*ptr
&& *ptr
!= ',')
659 charset_len
= ptr
-charset
;
662 lena
= WideCharToMultiByte(CP_ACP
, 0, charset
, charset_len
, NULL
, 0, NULL
, NULL
);
663 charseta
= heap_alloc(lena
+1);
667 WideCharToMultiByte(CP_ACP
, 0, charset
, charset_len
, charseta
, lena
, NULL
, NULL
);
670 nsACString_InitDepend(&charset_str
, charseta
);
671 nsIHttpChannel_SetContentCharset(&This
->nschannel
->nsIHttpChannel_iface
, &charset_str
);
672 nsACString_Finish(&charset_str
);
675 FIXME("unhandled: %s\n", debugstr_wn(ptr
, len
- (ptr
-value
)));
679 static HRESULT
parse_headers(const WCHAR
*headers
, struct list
*headers_list
)
681 const WCHAR
*header
, *header_end
, *colon
, *value
;
686 if(header
[0] == '\r' && header
[1] == '\n' && !header
[2])
688 for(colon
= header
; *colon
&& *colon
!= ':' && *colon
!= '\r'; colon
++);
698 for(header_end
= value
+1; *header_end
&& *header_end
!= '\r'; header_end
++);
700 hres
= set_http_header(headers_list
, header
, colon
-header
, value
, header_end
-value
);
705 if(header
[0] == '\r' && header
[1] == '\n')
712 static HRESULT
process_response_headers(nsChannelBSC
*This
, const WCHAR
*headers
)
717 static const WCHAR content_typeW
[] = {'c','o','n','t','e','n','t','-','t','y','p','e',0};
719 hres
= parse_headers(headers
, &This
->nschannel
->response_headers
);
723 LIST_FOR_EACH_ENTRY(iter
, &This
->nschannel
->response_headers
, http_header_t
, entry
) {
724 if(!strcmpiW(iter
->header
, content_typeW
))
725 parse_content_type(This
, iter
->data
);
731 HRESULT
start_binding(HTMLInnerWindow
*inner_window
, BSCallback
*bscallback
, IBindCtx
*bctx
)
736 TRACE("(%p %p %p)\n", inner_window
, bscallback
, bctx
);
738 bscallback
->window
= inner_window
;
740 /* NOTE: IE7 calls IsSystemMoniker here*/
743 RegisterBindStatusCallback(bctx
, &bscallback
->IBindStatusCallback_iface
, NULL
, 0);
744 IBindCtx_AddRef(bctx
);
746 hres
= CreateAsyncBindCtx(0, &bscallback
->IBindStatusCallback_iface
, NULL
, &bctx
);
748 WARN("CreateAsyncBindCtx failed: %08x\n", hres
);
749 bscallback
->vtbl
->stop_binding(bscallback
, hres
);
754 hres
= IMoniker_BindToStorage(bscallback
->mon
, bctx
, NULL
, &IID_IStream
, (void**)&str
);
755 IBindCtx_Release(bctx
);
757 WARN("BindToStorage failed: %08x\n", hres
);
758 bscallback
->vtbl
->stop_binding(bscallback
, hres
);
763 IStream_Release(str
);
765 IMoniker_Release(bscallback
->mon
);
766 bscallback
->mon
= NULL
;
779 static inline BufferBSC
*BufferBSC_from_BSCallback(BSCallback
*iface
)
781 return CONTAINING_RECORD(iface
, BufferBSC
, bsc
);
784 static void BufferBSC_destroy(BSCallback
*bsc
)
786 BufferBSC
*This
= BufferBSC_from_BSCallback(bsc
);
788 heap_free(This
->buf
);
792 static HRESULT
BufferBSC_init_bindinfo(BSCallback
*bsc
)
797 static HRESULT
BufferBSC_start_binding(BSCallback
*bsc
)
802 static HRESULT
BufferBSC_stop_binding(BSCallback
*bsc
, HRESULT result
)
804 BufferBSC
*This
= BufferBSC_from_BSCallback(bsc
);
809 heap_free(This
->buf
);
817 static HRESULT
BufferBSC_read_data(BSCallback
*bsc
, IStream
*stream
)
819 BufferBSC
*This
= BufferBSC_from_BSCallback(bsc
);
825 This
->buf
= heap_alloc(This
->size
);
829 if(This
->bsc
.readed
== This
->size
) {
831 This
->buf
= heap_realloc(This
->buf
, This
->size
);
834 hres
= read_stream(&This
->bsc
, stream
, This
->buf
+This
->bsc
.readed
, This
->size
-This
->bsc
.readed
, &readed
);
835 }while(hres
== S_OK
);
840 static HRESULT
BufferBSC_on_progress(BSCallback
*bsc
, ULONG status_code
, LPCWSTR status_text
)
845 static HRESULT
BufferBSC_on_response(BSCallback
*bsc
, DWORD response_code
,
846 LPCWSTR response_headers
)
851 static HRESULT
BufferBSC_beginning_transaction(BSCallback
*bsc
, WCHAR
**additional_headers
)
856 static const BSCallbackVtbl BufferBSCVtbl
= {
858 BufferBSC_init_bindinfo
,
859 BufferBSC_start_binding
,
860 BufferBSC_stop_binding
,
862 BufferBSC_on_progress
,
863 BufferBSC_on_response
,
864 BufferBSC_beginning_transaction
868 static BufferBSC
*create_bufferbsc(IMoniker
*mon
)
870 BufferBSC
*ret
= heap_alloc_zero(sizeof(*ret
));
872 init_bscallback(&ret
->bsc
, &BufferBSCVtbl
, mon
, 0);
878 HRESULT
bind_mon_to_wstr(HTMLInnerWindow
*window
, IMoniker
*mon
, WCHAR
**ret
)
880 BufferBSC
*bsc
= create_bufferbsc(mon
);
885 hres
= start_binding(window
, &bsc
->bsc
, NULL
);
889 IBindStatusCallback_Release(&bsc
->bsc
.IBindStatusCallback_iface
);
893 len
= MultiByteToWideChar(CP_ACP
, 0, bsc
->buf
, bsc
->bsc
.readed
, NULL
, 0);
894 text
= heap_alloc((len
+1)*sizeof(WCHAR
));
896 MultiByteToWideChar(CP_ACP
, 0, bsc
->buf
, bsc
->bsc
.readed
, text
, len
);
899 hres
= E_OUTOFMEMORY
;
901 IBindStatusCallback_Release(&bsc
->bsc
.IBindStatusCallback_iface
);
909 static HRESULT
read_post_data_stream(nsChannelBSC
*This
, nsChannel
*nschannel
)
911 PRUint32 data_len
= 0, available
= 0;
912 char *data
, *post_data
;
916 if(!nschannel
->post_data_stream
)
919 nsres
= nsIInputStream_Available(nschannel
->post_data_stream
, &available
);
923 post_data
= data
= GlobalAlloc(0, available
);
925 return E_OUTOFMEMORY
;
927 nsres
= nsIInputStream_Read(nschannel
->post_data_stream
, data
, available
, &data_len
);
928 if(NS_FAILED(nsres
)) {
933 if(nschannel
->post_data_contains_headers
) {
934 if(data_len
>= 2 && data
[0] == '\r' && data
[1] == '\n') {
942 post_data
+= data_len
;
943 for(ptr
= data
; ptr
+4 < data
+data_len
; ptr
++) {
944 if(!memcmp(ptr
, "\r\n\r\n", 4)) {
950 data_len
-= post_data
-data
;
952 size
= MultiByteToWideChar(CP_ACP
, 0, data
, post_data
-data
, NULL
, 0);
953 headers
= heap_alloc((size
+1)*sizeof(WCHAR
));
955 MultiByteToWideChar(CP_ACP
, 0, data
, post_data
-data
, headers
, size
);
957 hres
= parse_headers(headers
, &nschannel
->request_headers
);
959 This
->bsc
.headers
= headers
;
963 hres
= E_OUTOFMEMORY
;
976 }else if(post_data
!= data
) {
979 new_data
= GlobalAlloc(0, data_len
);
981 memcpy(new_data
, post_data
, data_len
);
984 return E_OUTOFMEMORY
;
985 post_data
= new_data
;
988 This
->bsc
.post_data
= post_data
;
989 This
->bsc
.post_data_len
= data_len
;
990 TRACE("post_data = %s\n", debugstr_a(This
->bsc
.post_data
));
994 static HRESULT
on_start_nsrequest(nsChannelBSC
*This
)
998 /* FIXME: it's needed for http connections from BindToObject. */
999 if(!This
->nschannel
->response_status
)
1000 This
->nschannel
->response_status
= 200;
1002 nsres
= nsIStreamListener_OnStartRequest(This
->nslistener
,
1003 (nsIRequest
*)&This
->nschannel
->nsIHttpChannel_iface
, This
->nscontext
);
1004 if(NS_FAILED(nsres
)) {
1005 FIXME("OnStartRequest failed: %08x\n", nsres
);
1009 if(This
->is_doc_channel
) {
1010 update_window_doc(This
->bsc
.window
);
1011 if(This
->bsc
.window
->base
.outer_window
->readystate
!= READYSTATE_LOADING
)
1012 set_ready_state(This
->bsc
.window
->base
.outer_window
, READYSTATE_LOADING
);
1018 static void on_stop_nsrequest(nsChannelBSC
*This
, HRESULT result
)
1020 nsresult nsres
, request_result
;
1024 request_result
= NS_OK
;
1027 request_result
= NS_BINDING_ABORTED
;
1030 request_result
= NS_ERROR_FAILURE
;
1033 if(This
->nslistener
) {
1034 nsres
= nsIStreamListener_OnStopRequest(This
->nslistener
,
1035 (nsIRequest
*)&This
->nschannel
->nsIHttpChannel_iface
, This
->nscontext
,
1037 if(NS_FAILED(nsres
))
1038 WARN("OnStopRequest failed: %08x\n", nsres
);
1041 if(This
->nschannel
->load_group
) {
1042 nsres
= nsILoadGroup_RemoveRequest(This
->nschannel
->load_group
,
1043 (nsIRequest
*)&This
->nschannel
->nsIHttpChannel_iface
, NULL
, request_result
);
1044 if(NS_FAILED(nsres
))
1045 ERR("RemoveRequest failed: %08x\n", nsres
);
1049 static HRESULT
read_stream_data(nsChannelBSC
*This
, IStream
*stream
)
1055 if(!This
->nslistener
) {
1059 hres
= read_stream(&This
->bsc
, stream
, buf
, sizeof(buf
), &read
);
1060 }while(hres
== S_OK
&& read
);
1066 This
->nsstream
= create_nsprotocol_stream();
1069 BOOL first_read
= !This
->bsc
.readed
;
1071 hres
= read_stream(&This
->bsc
, stream
, This
->nsstream
->buf
+This
->nsstream
->buf_size
,
1072 sizeof(This
->nsstream
->buf
)-This
->nsstream
->buf_size
, &read
);
1076 This
->nsstream
->buf_size
+= read
;
1079 if(This
->nsstream
->buf_size
>= 2
1080 && (BYTE
)This
->nsstream
->buf
[0] == 0xff
1081 && (BYTE
)This
->nsstream
->buf
[1] == 0xfe)
1082 This
->nschannel
->charset
= heap_strdupA(UTF16_STR
);
1083 if(This
->nsstream
->buf_size
>= 3
1084 && (BYTE
)This
->nsstream
->buf
[0] == 0xef
1085 && (BYTE
)This
->nsstream
->buf
[1] == 0xbb
1086 && (BYTE
)This
->nsstream
->buf
[2] == 0xbf)
1087 This
->nschannel
->charset
= heap_strdupA(UTF8_STR
);
1089 if(!This
->nschannel
->content_type
) {
1092 hres
= FindMimeFromData(NULL
, NULL
, This
->nsstream
->buf
, This
->nsstream
->buf_size
,
1093 This
->is_doc_channel
? text_htmlW
: NULL
, 0, &mime
, 0);
1097 TRACE("Found MIME %s\n", debugstr_w(mime
));
1099 This
->nschannel
->content_type
= heap_strdupWtoA(mime
);
1100 CoTaskMemFree(mime
);
1101 if(!This
->nschannel
->content_type
)
1102 return E_OUTOFMEMORY
;
1105 on_start_nsrequest(This
);
1108 nsres
= nsIStreamListener_OnDataAvailable(This
->nslistener
,
1109 (nsIRequest
*)&This
->nschannel
->nsIHttpChannel_iface
, This
->nscontext
,
1110 &This
->nsstream
->nsIInputStream_iface
, This
->bsc
.readed
-This
->nsstream
->buf_size
,
1111 This
->nsstream
->buf_size
);
1112 if(NS_FAILED(nsres
))
1113 ERR("OnDataAvailable failed: %08x\n", nsres
);
1115 if(This
->nsstream
->buf_size
== sizeof(This
->nsstream
->buf
)) {
1116 ERR("buffer is full\n");
1119 }while(hres
== S_OK
);
1125 nsIAsyncVerifyRedirectCallback nsIAsyncVerifyRedirectCallback_iface
;
1129 nsChannel
*nschannel
;
1131 } nsRedirectCallback
;
1133 static nsRedirectCallback
*impl_from_nsIAsyncVerifyRedirectCallback(nsIAsyncVerifyRedirectCallback
*iface
)
1135 return CONTAINING_RECORD(iface
, nsRedirectCallback
, nsIAsyncVerifyRedirectCallback_iface
);
1138 static nsresult NSAPI
nsAsyncVerifyRedirectCallback_QueryInterface(nsIAsyncVerifyRedirectCallback
*iface
,
1139 nsIIDRef riid
, void **result
)
1141 nsRedirectCallback
*This
= impl_from_nsIAsyncVerifyRedirectCallback(iface
);
1143 if(IsEqualGUID(&IID_nsISupports
, riid
)) {
1144 TRACE("(%p)->(IID_nsISuports %p)\n", This
, result
);
1145 *result
= &This
->nsIAsyncVerifyRedirectCallback_iface
;
1146 }else if(IsEqualGUID(&IID_nsIAsyncVerifyRedirectCallback
, riid
)) {
1147 TRACE("(%p)->(IID_nsIAsyncVerifyRedirectCallback %p)\n", This
, result
);
1148 *result
= &This
->nsIAsyncVerifyRedirectCallback_iface
;
1151 WARN("unimplemented iface %s\n", debugstr_guid(riid
));
1152 return NS_NOINTERFACE
;
1155 nsISupports_AddRef((nsISupports
*)*result
);
1159 static nsrefcnt NSAPI
nsAsyncVerifyRedirectCallback_AddRef(nsIAsyncVerifyRedirectCallback
*iface
)
1161 nsRedirectCallback
*This
= impl_from_nsIAsyncVerifyRedirectCallback(iface
);
1162 LONG ref
= InterlockedIncrement(&This
->ref
);
1164 TRACE("(%p) ref=%d\n", This
, ref
);
1169 static nsrefcnt NSAPI
nsAsyncVerifyRedirectCallback_Release(nsIAsyncVerifyRedirectCallback
*iface
)
1171 nsRedirectCallback
*This
= impl_from_nsIAsyncVerifyRedirectCallback(iface
);
1172 LONG ref
= InterlockedDecrement(&This
->ref
);
1174 TRACE("(%p) ref=%d\n", This
, ref
);
1177 IBindStatusCallback_Release(&This
->bsc
->bsc
.IBindStatusCallback_iface
);
1178 nsIHttpChannel_Release(&This
->nschannel
->nsIHttpChannel_iface
);
1185 static nsresult NSAPI
nsAsyncVerifyRedirectCallback_AsyncOnChannelRedirect(nsIAsyncVerifyRedirectCallback
*iface
, nsresult result
)
1187 nsRedirectCallback
*This
= impl_from_nsIAsyncVerifyRedirectCallback(iface
);
1188 nsChannel
*old_nschannel
;
1191 TRACE("(%p)->(%08x)\n", This
, result
);
1193 old_nschannel
= This
->bsc
->nschannel
;
1194 nsIHttpChannel_AddRef(&This
->nschannel
->nsIHttpChannel_iface
);
1195 This
->bsc
->nschannel
= This
->nschannel
;
1197 if(This
->nschannel
->load_group
) {
1198 nsres
= nsILoadGroup_AddRequest(This
->nschannel
->load_group
, (nsIRequest
*)&This
->nschannel
->nsIHttpChannel_iface
,
1200 if(NS_FAILED(nsres
))
1201 ERR("AddRequest failed: %08x\n", nsres
);
1204 if(This
->bsc
->is_doc_channel
) {
1205 IUri
*uri
= nsuri_get_uri(This
->nschannel
->uri
);
1208 set_current_uri(This
->bsc
->bsc
.window
->base
.outer_window
, uri
);
1211 WARN("Could not get IUri from nsWineURI\n");
1216 nsres
= nsILoadGroup_RemoveRequest(old_nschannel
->load_group
,
1217 (nsIRequest
*)&old_nschannel
->nsIHttpChannel_iface
, NULL
, NS_OK
);
1218 if(NS_FAILED(nsres
))
1219 ERR("RemoveRequest failed: %08x\n", nsres
);
1220 nsIHttpChannel_Release(&old_nschannel
->nsIHttpChannel_iface
);
1226 static const nsIAsyncVerifyRedirectCallbackVtbl nsAsyncVerifyRedirectCallbackVtbl
= {
1227 nsAsyncVerifyRedirectCallback_QueryInterface
,
1228 nsAsyncVerifyRedirectCallback_AddRef
,
1229 nsAsyncVerifyRedirectCallback_Release
,
1230 nsAsyncVerifyRedirectCallback_AsyncOnChannelRedirect
1233 static HRESULT
create_redirect_callback(nsChannel
*nschannel
, nsChannelBSC
*bsc
, nsRedirectCallback
**ret
)
1235 nsRedirectCallback
*callback
;
1237 callback
= heap_alloc(sizeof(*callback
));
1239 return E_OUTOFMEMORY
;
1241 callback
->nsIAsyncVerifyRedirectCallback_iface
.lpVtbl
= &nsAsyncVerifyRedirectCallbackVtbl
;
1244 nsIHttpChannel_AddRef(&nschannel
->nsIHttpChannel_iface
);
1245 callback
->nschannel
= nschannel
;
1247 IBindStatusCallback_AddRef(&bsc
->bsc
.IBindStatusCallback_iface
);
1248 callback
->bsc
= bsc
;
1254 static inline nsChannelBSC
*nsChannelBSC_from_BSCallback(BSCallback
*iface
)
1256 return CONTAINING_RECORD(iface
, nsChannelBSC
, bsc
);
1259 static void nsChannelBSC_destroy(BSCallback
*bsc
)
1261 nsChannelBSC
*This
= nsChannelBSC_from_BSCallback(bsc
);
1264 nsIHttpChannel_Release(&This
->nschannel
->nsIHttpChannel_iface
);
1265 if(This
->nslistener
)
1266 nsIStreamListener_Release(This
->nslistener
);
1268 nsISupports_Release(This
->nscontext
);
1270 nsIInputStream_Release(&This
->nsstream
->nsIInputStream_iface
);
1274 static HRESULT
nsChannelBSC_start_binding(BSCallback
*bsc
)
1276 nsChannelBSC
*This
= nsChannelBSC_from_BSCallback(bsc
);
1278 if(This
->is_doc_channel
)
1279 This
->bsc
.window
->base
.outer_window
->base
.inner_window
->doc
->skip_mutation_notif
= FALSE
;
1284 static HRESULT
nsChannelBSC_init_bindinfo(BSCallback
*bsc
)
1286 nsChannelBSC
*This
= nsChannelBSC_from_BSCallback(bsc
);
1289 if(This
->nschannel
&& This
->nschannel
->post_data_stream
) {
1290 hres
= read_post_data_stream(This
, This
->nschannel
);
1301 } stop_request_task_t
;
1303 static void stop_request_proc(task_t
*_task
)
1305 stop_request_task_t
*task
= (stop_request_task_t
*)_task
;
1307 TRACE("(%p)\n", task
->bsc
);
1309 list_remove(&task
->bsc
->bsc
.entry
);
1310 list_init(&task
->bsc
->bsc
.entry
);
1311 on_stop_nsrequest(task
->bsc
, S_OK
);
1314 static void stop_request_task_destr(task_t
*_task
)
1316 stop_request_task_t
*task
= (stop_request_task_t
*)_task
;
1318 IBindStatusCallback_Release(&task
->bsc
->bsc
.IBindStatusCallback_iface
);
1322 static HRESULT
async_stop_request(nsChannelBSC
*This
)
1324 stop_request_task_t
*task
;
1326 if(!This
->bsc
.readed
) {
1327 TRACE("No data read, calling OnStartRequest\n");
1328 on_start_nsrequest(This
);
1331 task
= heap_alloc(sizeof(*task
));
1333 return E_OUTOFMEMORY
;
1335 IBindStatusCallback_AddRef(&This
->bsc
.IBindStatusCallback_iface
);
1338 push_task(&task
->header
, stop_request_proc
, stop_request_task_destr
, This
->bsc
.window
->task_magic
);
1342 static void handle_navigation_error(nsChannelBSC
*This
, DWORD result
)
1344 HTMLOuterWindow
*outer_window
;
1345 HTMLDocumentObj
*doc
;
1346 IOleCommandTarget
*olecmd
;
1349 SAFEARRAYBOUND bound
;
1350 VARIANT var
, varOut
;
1355 if(!This
->is_doc_channel
|| !This
->bsc
.window
)
1358 outer_window
= This
->bsc
.window
->base
.outer_window
;
1360 doc
= outer_window
->doc_obj
;
1361 if(!doc
|| !doc
->doc_object_service
|| !doc
->client
)
1364 hres
= IDocObjectService_IsErrorUrl(doc
->doc_object_service
,
1365 outer_window
->url
, &is_error_url
);
1366 if(FAILED(hres
) || is_error_url
)
1369 hres
= IOleClientSite_QueryInterface(doc
->client
,
1370 &IID_IOleCommandTarget
, (void**)&olecmd
);
1375 bound
.cElements
= 8;
1376 sa
= SafeArrayCreate(VT_VARIANT
, 1, &bound
);
1378 IOleCommandTarget_Release(olecmd
);
1384 V_I4(&var
) = result
;
1385 SafeArrayPutElement(sa
, &ind
, &var
);
1388 V_VT(&var
) = VT_BSTR
;
1389 V_BSTR(&var
) = outer_window
->url
;
1390 SafeArrayPutElement(sa
, &ind
, &var
);
1393 V_VT(&var
) = VT_UNKNOWN
;
1394 V_UNKNOWN(&var
) = (IUnknown
*)&outer_window
->base
.IHTMLWindow2_iface
;
1395 SafeArrayPutElement(sa
, &ind
, &var
);
1397 /* FIXME: what are the following fields for? */
1399 V_VT(&var
) = VT_UNKNOWN
;
1400 V_UNKNOWN(&var
) = NULL
;
1401 SafeArrayPutElement(sa
, &ind
, &var
);
1404 V_VT(&var
) = VT_BOOL
;
1405 V_BOOL(&var
) = FALSE
;
1406 SafeArrayPutElement(sa
, &ind
, &var
);
1409 V_VT(&var
) = VT_BOOL
;
1410 V_BOOL(&var
) = FALSE
;
1411 SafeArrayPutElement(sa
, &ind
, &var
);
1414 V_VT(&var
) = VT_BSTR
;
1415 unk
= SysAllocString(NULL
);
1417 SafeArrayPutElement(sa
, &ind
, &var
);
1420 V_VT(&var
) = VT_UNKNOWN
;
1421 V_UNKNOWN(&var
) = NULL
;
1422 SafeArrayPutElement(sa
, &ind
, &var
);
1424 V_VT(&var
) = VT_ARRAY
;
1426 V_VT(&varOut
) = VT_BOOL
;
1427 V_BOOL(&varOut
) = VARIANT_TRUE
;
1428 IOleCommandTarget_Exec(olecmd
, &CGID_DocHostCmdPriv
, 1, 0, &var
, FAILED(hres
)?NULL
:&varOut
);
1431 SafeArrayDestroy(sa
);
1432 IOleCommandTarget_Release(olecmd
);
1435 static HRESULT
nsChannelBSC_stop_binding(BSCallback
*bsc
, HRESULT result
)
1437 nsChannelBSC
*This
= nsChannelBSC_from_BSCallback(bsc
);
1439 if(result
!= E_ABORT
) {
1441 handle_navigation_error(This
, result
);
1442 else if(This
->is_doc_channel
) {
1443 result
= async_stop_request(This
);
1444 if(SUCCEEDED(result
))
1449 on_stop_nsrequest(This
, result
);
1453 static HRESULT
nsChannelBSC_read_data(BSCallback
*bsc
, IStream
*stream
)
1455 nsChannelBSC
*This
= nsChannelBSC_from_BSCallback(bsc
);
1457 return read_stream_data(This
, stream
);
1460 static HRESULT
handle_redirect(nsChannelBSC
*This
, const WCHAR
*new_url
)
1462 nsRedirectCallback
*callback
;
1463 nsIChannelEventSink
*sink
;
1464 nsChannel
*new_channel
;
1468 TRACE("(%p)->(%s)\n", This
, debugstr_w(new_url
));
1470 if(!This
->nschannel
|| !This
->nschannel
->notif_callback
)
1473 nsres
= nsIInterfaceRequestor_GetInterface(This
->nschannel
->notif_callback
, &IID_nsIChannelEventSink
, (void**)&sink
);
1474 if(NS_FAILED(nsres
))
1477 hres
= create_redirect_nschannel(new_url
, This
->nschannel
, &new_channel
);
1478 if(SUCCEEDED(hres
)) {
1479 TRACE("%p %p->%p\n", This
, This
->nschannel
, new_channel
);
1481 hres
= create_redirect_callback(new_channel
, This
, &callback
);
1482 nsIHttpChannel_Release(&new_channel
->nsIHttpChannel_iface
);
1485 if(SUCCEEDED(hres
)) {
1486 nsres
= nsIChannelEventSink_AsyncOnChannelRedirect(sink
, (nsIChannel
*)&This
->nschannel
->nsIHttpChannel_iface
,
1487 (nsIChannel
*)&callback
->nschannel
->nsIHttpChannel_iface
, REDIRECT_TEMPORARY
, /* FIXME */
1488 &callback
->nsIAsyncVerifyRedirectCallback_iface
);
1490 if(NS_FAILED(nsres
))
1491 FIXME("AsyncOnChannelRedirect failed: %08x\n", hres
);
1492 else if(This
->nschannel
!= callback
->nschannel
)
1493 FIXME("nschannel not updated\n");
1495 nsIAsyncVerifyRedirectCallback_Release(&callback
->nsIAsyncVerifyRedirectCallback_iface
);
1498 nsIChannelEventSink_Release(sink
);
1502 static HRESULT
nsChannelBSC_on_progress(BSCallback
*bsc
, ULONG status_code
, LPCWSTR status_text
)
1504 nsChannelBSC
*This
= nsChannelBSC_from_BSCallback(bsc
);
1506 switch(status_code
) {
1507 case BINDSTATUS_MIMETYPEAVAILABLE
:
1508 if(!This
->nschannel
)
1511 heap_free(This
->nschannel
->content_type
);
1512 This
->nschannel
->content_type
= heap_strdupWtoA(status_text
);
1514 case BINDSTATUS_REDIRECTING
:
1515 return handle_redirect(This
, status_text
);
1516 case BINDSTATUS_BEGINDOWNLOADDATA
: {
1517 IWinInetHttpInfo
*http_info
;
1518 DWORD status
, size
= sizeof(DWORD
);
1521 if(!This
->bsc
.binding
)
1524 hres
= IBinding_QueryInterface(This
->bsc
.binding
, &IID_IWinInetHttpInfo
, (void**)&http_info
);
1528 hres
= IWinInetHttpInfo_QueryInfo(http_info
,
1529 HTTP_QUERY_STATUS_CODE
|HTTP_QUERY_FLAG_NUMBER
, &status
, &size
, NULL
, NULL
);
1530 IWinInetHttpInfo_Release(http_info
);
1531 if(FAILED(hres
) || status
== HTTP_STATUS_OK
)
1534 handle_navigation_error(This
, status
);
1541 static HRESULT
nsChannelBSC_on_response(BSCallback
*bsc
, DWORD response_code
,
1542 LPCWSTR response_headers
)
1544 nsChannelBSC
*This
= nsChannelBSC_from_BSCallback(bsc
);
1547 This
->nschannel
->response_status
= response_code
;
1549 if(response_headers
) {
1550 const WCHAR
*headers
;
1552 headers
= strchrW(response_headers
, '\r');
1553 if(headers
&& headers
[1] == '\n') {
1555 hres
= process_response_headers(This
, headers
);
1557 WARN("parsing headers failed: %08x\n", hres
);
1566 static HRESULT
nsChannelBSC_beginning_transaction(BSCallback
*bsc
, WCHAR
**additional_headers
)
1568 nsChannelBSC
*This
= nsChannelBSC_from_BSCallback(bsc
);
1569 http_header_t
*iter
;
1573 static const WCHAR content_lengthW
[] =
1574 {'C','o','n','t','e','n','t','-','L','e','n','g','t','h',0};
1576 if(!This
->nschannel
)
1579 LIST_FOR_EACH_ENTRY(iter
, &This
->nschannel
->request_headers
, http_header_t
, entry
) {
1580 if(strcmpW(iter
->header
, content_lengthW
))
1581 len
+= strlenW(iter
->header
) + 2 /* ": " */ + strlenW(iter
->data
) + 2 /* "\r\n" */;
1587 *additional_headers
= ptr
= CoTaskMemAlloc((len
+1)*sizeof(WCHAR
));
1589 return E_OUTOFMEMORY
;
1591 LIST_FOR_EACH_ENTRY(iter
, &This
->nschannel
->request_headers
, http_header_t
, entry
) {
1592 if(!strcmpW(iter
->header
, content_lengthW
))
1595 len
= strlenW(iter
->header
);
1596 memcpy(ptr
, iter
->header
, len
*sizeof(WCHAR
));
1602 len
= strlenW(iter
->data
);
1603 memcpy(ptr
, iter
->data
, len
*sizeof(WCHAR
));
1615 static const BSCallbackVtbl nsChannelBSCVtbl
= {
1616 nsChannelBSC_destroy
,
1617 nsChannelBSC_init_bindinfo
,
1618 nsChannelBSC_start_binding
,
1619 nsChannelBSC_stop_binding
,
1620 nsChannelBSC_read_data
,
1621 nsChannelBSC_on_progress
,
1622 nsChannelBSC_on_response
,
1623 nsChannelBSC_beginning_transaction
1626 HRESULT
create_channelbsc(IMoniker
*mon
, const WCHAR
*headers
, BYTE
*post_data
, DWORD post_data_size
,
1627 BOOL is_doc_binding
, nsChannelBSC
**retval
)
1631 ret
= heap_alloc_zero(sizeof(*ret
));
1633 return E_OUTOFMEMORY
;
1635 init_bscallback(&ret
->bsc
, &nsChannelBSCVtbl
, mon
, BINDF_ASYNCHRONOUS
| BINDF_ASYNCSTORAGE
| BINDF_PULLDATA
);
1636 ret
->is_doc_channel
= is_doc_binding
;
1639 ret
->bsc
.headers
= heap_strdupW(headers
);
1640 if(!ret
->bsc
.headers
) {
1641 IBindStatusCallback_Release(&ret
->bsc
.IBindStatusCallback_iface
);
1642 return E_OUTOFMEMORY
;
1647 ret
->bsc
.post_data
= GlobalAlloc(0, post_data_size
);
1648 if(!ret
->bsc
.post_data
) {
1649 heap_free(ret
->bsc
.headers
);
1650 IBindStatusCallback_Release(&ret
->bsc
.IBindStatusCallback_iface
);
1651 return E_OUTOFMEMORY
;
1654 memcpy(ret
->bsc
.post_data
, post_data
, post_data_size
);
1655 ret
->bsc
.post_data_len
= post_data_size
;
1664 HTMLOuterWindow
*window
;
1665 HTMLInnerWindow
*pending_window
;
1666 } start_doc_binding_task_t
;
1668 static void start_doc_binding_proc(task_t
*_task
)
1670 start_doc_binding_task_t
*task
= (start_doc_binding_task_t
*)_task
;
1672 set_current_mon(task
->window
, task
->pending_window
->bscallback
->bsc
.mon
);
1673 start_binding(task
->pending_window
, &task
->pending_window
->bscallback
->bsc
, NULL
);
1676 static void start_doc_binding_task_destr(task_t
*_task
)
1678 start_doc_binding_task_t
*task
= (start_doc_binding_task_t
*)_task
;
1680 IHTMLWindow2_Release(&task
->pending_window
->base
.IHTMLWindow2_iface
);
1684 HRESULT
async_start_doc_binding(HTMLOuterWindow
*window
, HTMLInnerWindow
*pending_window
)
1686 start_doc_binding_task_t
*task
;
1688 TRACE("%p\n", pending_window
);
1690 task
= heap_alloc(sizeof(start_doc_binding_task_t
));
1692 return E_OUTOFMEMORY
;
1694 task
->window
= window
;
1695 task
->pending_window
= pending_window
;
1696 IHTMLWindow2_AddRef(&pending_window
->base
.IHTMLWindow2_iface
);
1698 push_task(&task
->header
, start_doc_binding_proc
, start_doc_binding_task_destr
, pending_window
->task_magic
);
1702 void abort_window_bindings(HTMLInnerWindow
*window
)
1706 remove_target_tasks(window
->task_magic
);
1708 while(!list_empty(&window
->bindings
)) {
1709 iter
= LIST_ENTRY(window
->bindings
.next
, BSCallback
, entry
);
1711 TRACE("Aborting %p\n", iter
);
1713 IBindStatusCallback_AddRef(&iter
->IBindStatusCallback_iface
);
1716 IBinding_Abort(iter
->binding
);
1718 iter
->vtbl
->stop_binding(iter
, E_ABORT
);
1720 iter
->window
= NULL
;
1721 list_remove(&iter
->entry
);
1722 list_init(&iter
->entry
);
1724 IBindStatusCallback_Release(&iter
->IBindStatusCallback_iface
);
1727 if(window
->bscallback
) {
1728 IBindStatusCallback_Release(&window
->bscallback
->bsc
.IBindStatusCallback_iface
);
1729 window
->bscallback
= NULL
;
1733 HRESULT
channelbsc_load_stream(HTMLInnerWindow
*pending_window
, IStream
*stream
)
1735 nsChannelBSC
*bscallback
= pending_window
->bscallback
;
1736 HRESULT hres
= S_OK
;
1738 if(!bscallback
->nschannel
) {
1739 ERR("NULL nschannel\n");
1743 bscallback
->nschannel
->content_type
= heap_strdupA("text/html");
1744 if(!bscallback
->nschannel
->content_type
)
1745 return E_OUTOFMEMORY
;
1747 bscallback
->bsc
.window
= pending_window
;
1749 hres
= read_stream_data(bscallback
, stream
);
1751 hres
= async_stop_request(bscallback
);
1753 IBindStatusCallback_OnStopBinding(&bscallback
->bsc
.IBindStatusCallback_iface
, hres
,
1759 void channelbsc_set_channel(nsChannelBSC
*This
, nsChannel
*channel
, nsIStreamListener
*listener
, nsISupports
*context
)
1761 nsIHttpChannel_AddRef(&channel
->nsIHttpChannel_iface
);
1762 This
->nschannel
= channel
;
1764 nsIStreamListener_AddRef(listener
);
1765 This
->nslistener
= listener
;
1768 nsISupports_AddRef(context
);
1769 This
->nscontext
= context
;
1772 if(This
->bsc
.headers
) {
1775 hres
= parse_headers(This
->bsc
.headers
, &channel
->request_headers
);
1776 heap_free(This
->bsc
.headers
);
1777 This
->bsc
.headers
= NULL
;
1779 WARN("parse_headers failed: %08x\n", hres
);
1785 HTMLOuterWindow
*window
;
1787 } navigate_javascript_task_t
;
1789 static void navigate_javascript_proc(task_t
*_task
)
1791 navigate_javascript_task_t
*task
= (navigate_javascript_task_t
*)_task
;
1792 HTMLOuterWindow
*window
= task
->window
;
1797 static const WCHAR jscriptW
[] = {'j','s','c','r','i','p','t',0};
1799 task
->window
->readystate
= READYSTATE_COMPLETE
;
1801 hres
= IUri_GetPath(task
->uri
, &code
);
1805 hres
= UrlUnescapeW(code
, NULL
, NULL
, URL_UNESCAPE_INPLACE
);
1807 SysFreeString(code
);
1811 set_download_state(window
->doc_obj
, 1);
1813 V_VT(&v
) = VT_EMPTY
;
1814 hres
= exec_script(window
->base
.inner_window
, code
, jscriptW
, &v
);
1815 SysFreeString(code
);
1816 if(SUCCEEDED(hres
) && V_VT(&v
) != VT_EMPTY
) {
1817 FIXME("javascirpt URL returned %s\n", debugstr_variant(&v
));
1821 if(window
->doc_obj
->view_sink
)
1822 IAdviseSink_OnViewChange(window
->doc_obj
->view_sink
, DVASPECT_CONTENT
, -1);
1824 set_download_state(window
->doc_obj
, 0);
1827 static void navigate_javascript_task_destr(task_t
*_task
)
1829 navigate_javascript_task_t
*task
= (navigate_javascript_task_t
*)_task
;
1831 IUri_Release(task
->uri
);
1837 HTMLOuterWindow
*window
;
1838 nsChannelBSC
*bscallback
;
1842 static void navigate_proc(task_t
*_task
)
1844 navigate_task_t
*task
= (navigate_task_t
*)_task
;
1847 hres
= set_moniker(&task
->window
->doc_obj
->basedoc
, task
->mon
, NULL
, task
->bscallback
, TRUE
);
1848 if(SUCCEEDED(hres
)) {
1849 set_current_mon(task
->window
, task
->bscallback
->bsc
.mon
);
1850 start_binding(task
->window
->pending_window
, &task
->bscallback
->bsc
, NULL
);
1854 static void navigate_task_destr(task_t
*_task
)
1856 navigate_task_t
*task
= (navigate_task_t
*)_task
;
1858 IBindStatusCallback_Release(&task
->bscallback
->bsc
.IBindStatusCallback_iface
);
1859 IMoniker_Release(task
->mon
);
1863 static HRESULT
navigate_fragment(HTMLOuterWindow
*window
, IUri
*uri
)
1865 nsIDOMLocation
*nslocation
;
1866 nsAString nsfrag_str
;
1871 set_current_uri(window
, uri
);
1873 nsres
= nsIDOMWindow_GetLocation(window
->nswindow
, &nslocation
);
1874 if(FAILED(nsres
) || !nslocation
)
1877 hres
= IUri_GetFragment(uri
, &frag
);
1879 nsIDOMLocation_Release(nslocation
);
1883 nsAString_InitDepend(&nsfrag_str
, frag
);
1884 nsres
= nsIDOMLocation_SetHash(nslocation
, &nsfrag_str
);
1885 nsAString_Finish(&nsfrag_str
);
1886 nsIDOMLocation_Release(nslocation
);
1887 SysFreeString(frag
);
1888 if(NS_FAILED(nsres
)) {
1889 ERR("SetHash failed: %08x\n", nsres
);
1893 if(window
->doc_obj
->doc_object_service
) {
1894 IDocObjectService_FireNavigateComplete2(window
->doc_obj
->doc_object_service
, &window
->base
.IHTMLWindow2_iface
, 0x10);
1895 IDocObjectService_FireDocumentComplete(window
->doc_obj
->doc_object_service
, &window
->base
.IHTMLWindow2_iface
, 0);
1902 HRESULT
super_navigate(HTMLOuterWindow
*window
, IUri
*uri
, const WCHAR
*headers
, BYTE
*post_data
, DWORD post_data_size
)
1909 if(window
->doc_obj
->client
) {
1910 IOleCommandTarget
*cmdtrg
;
1912 hres
= IOleClientSite_QueryInterface(window
->doc_obj
->client
, &IID_IOleCommandTarget
, (void**)&cmdtrg
);
1913 if(SUCCEEDED(hres
)) {
1917 hres
= IUri_GetDisplayUri(uri
, &url_str
);
1918 if(SUCCEEDED(hres
)) {
1919 V_VT(&in
) = VT_BSTR
;
1920 V_BSTR(&in
) = url_str
;
1921 V_VT(&out
) = VT_BOOL
;
1922 V_BOOL(&out
) = VARIANT_TRUE
;
1923 hres
= IOleCommandTarget_Exec(cmdtrg
, &CGID_ShellDocView
, 67, 0, &in
, &out
);
1924 IOleCommandTarget_Release(cmdtrg
);
1927 SysFreeString(url_str
);
1932 if(window
->uri
&& !post_data_size
&& compare_ignoring_frag(window
->uri
, uri
)) {
1933 TRACE("fragment navigate\n");
1934 return navigate_fragment(window
, uri
);
1937 hres
= CreateURLMonikerEx2(NULL
, uri
, &mon
, URL_MK_UNIFORM
);
1941 /* FIXME: Why not set_ready_state? */
1942 window
->readystate
= READYSTATE_UNINITIALIZED
;
1944 hres
= create_channelbsc(mon
, headers
, post_data
, post_data_size
, TRUE
, &bsc
);
1946 IMoniker_Release(mon
);
1950 prepare_for_binding(&window
->doc_obj
->basedoc
, mon
, TRUE
);
1952 hres
= IUri_GetScheme(uri
, &scheme
);
1953 if(SUCCEEDED(hres
) && scheme
!= URL_SCHEME_JAVASCRIPT
) {
1954 navigate_task_t
*task
;
1956 task
= heap_alloc(sizeof(*task
));
1958 IBindStatusCallback_Release(&bsc
->bsc
.IBindStatusCallback_iface
);
1959 IMoniker_Release(mon
);
1960 return E_OUTOFMEMORY
;
1963 /* Silently and repeated when real loading starts? */
1964 window
->readystate
= READYSTATE_LOADING
;
1965 call_docview_84(window
->doc_obj
);
1967 task
->window
= window
;
1968 task
->bscallback
= bsc
;
1970 push_task(&task
->header
, navigate_proc
, navigate_task_destr
, window
->task_magic
);
1973 navigate_javascript_task_t
*task
;
1975 IBindStatusCallback_Release(&bsc
->bsc
.IBindStatusCallback_iface
);
1976 IMoniker_Release(mon
);
1978 task
= heap_alloc(sizeof(*task
));
1980 return E_OUTOFMEMORY
;
1983 window
->readystate
= READYSTATE_COMPLETE
;
1984 call_docview_84(window
->doc_obj
);
1987 task
->window
= window
;
1989 push_task(&task
->header
, navigate_javascript_proc
, navigate_javascript_task_destr
, window
->task_magic
);
1995 HRESULT
navigate_new_window(HTMLOuterWindow
*window
, IUri
*uri
, const WCHAR
*name
, IHTMLWindow2
**ret
)
1997 IWebBrowser2
*web_browser
;
1998 IHTMLWindow2
*new_window
;
2003 hres
= create_channelbsc(NULL
, NULL
, NULL
, 0, FALSE
, &bsc
);
2007 hres
= CreateAsyncBindCtx(0, &bsc
->bsc
.IBindStatusCallback_iface
, NULL
, &bind_ctx
);
2009 IBindStatusCallback_Release(&bsc
->bsc
.IBindStatusCallback_iface
);
2013 hres
= CoCreateInstance(&CLSID_InternetExplorer
, NULL
, CLSCTX_LOCAL_SERVER
,
2014 &IID_IWebBrowser2
, (void**)&web_browser
);
2015 if(SUCCEEDED(hres
)) {
2016 ITargetFramePriv2
*target_frame_priv
;
2018 hres
= IWebBrowser2_QueryInterface(web_browser
, &IID_ITargetFramePriv2
, (void**)&target_frame_priv
);
2019 if(SUCCEEDED(hres
)) {
2020 hres
= ITargetFramePriv2_AggregatedNavigation2(target_frame_priv
,
2021 HLNF_DISABLEWINDOWRESTRICTIONS
|HLNF_OPENINNEWWINDOW
, bind_ctx
, &bsc
->bsc
.IBindStatusCallback_iface
,
2023 ITargetFramePriv2_Release(target_frame_priv
);
2026 hres
= do_query_service((IUnknown
*)web_browser
, &SID_SHTMLWindow
, &IID_IHTMLWindow2
, (void**)&new_window
);
2029 IWebBrowser2_Quit(web_browser
);
2030 IWebBrowser2_Release(web_browser
);
2033 WARN("Could not create InternetExplorer instance: %08x\n", hres
);
2036 IBindStatusCallback_Release(&bsc
->bsc
.IBindStatusCallback_iface
);
2037 IBindCtx_Release(bind_ctx
);
2041 IWebBrowser2_put_Visible(web_browser
, VARIANT_TRUE
);
2042 IWebBrowser2_Release(web_browser
);
2047 IHTMLWindow2_Release(new_window
);
2051 HRESULT
hlink_frame_navigate(HTMLDocument
*doc
, LPCWSTR url
, nsChannel
*nschannel
, DWORD hlnf
, BOOL
*cancel
)
2053 IHlinkFrame
*hlink_frame
;
2054 nsChannelBSC
*callback
;
2062 hres
= do_query_service((IUnknown
*)doc
->doc_obj
->client
, &IID_IHlinkFrame
, &IID_IHlinkFrame
,
2063 (void**)&hlink_frame
);
2067 hres
= create_channelbsc(NULL
, NULL
, NULL
, 0, FALSE
, &callback
);
2069 IHlinkFrame_Release(hlink_frame
);
2074 read_post_data_stream(callback
, nschannel
);
2076 hres
= CreateAsyncBindCtx(0, &callback
->bsc
.IBindStatusCallback_iface
, NULL
, &bindctx
);
2078 hres
= CoCreateInstance(&CLSID_StdHlink
, NULL
, CLSCTX_INPROC_SERVER
,
2079 &IID_IHlink
, (LPVOID
*)&hlink
);
2082 hres
= CreateURLMoniker(NULL
, url
, &mon
);
2084 if(SUCCEEDED(hres
)) {
2085 IHlink_SetMonikerReference(hlink
, HLINKSETF_TARGET
, mon
, NULL
);
2087 if(hlnf
& HLNF_OPENINNEWWINDOW
) {
2088 static const WCHAR wszBlank
[] = {'_','b','l','a','n','k',0};
2089 IHlink_SetTargetFrameName(hlink
, wszBlank
); /* FIXME */
2092 hres
= IHlinkFrame_Navigate(hlink_frame
, hlnf
, bindctx
,
2093 &callback
->bsc
.IBindStatusCallback_iface
, hlink
);
2094 IMoniker_Release(mon
);
2095 *cancel
= hres
== S_OK
;
2099 IHlinkFrame_Release(hlink_frame
);
2100 IBindCtx_Release(bindctx
);
2101 IBindStatusCallback_Release(&callback
->bsc
.IBindStatusCallback_iface
);
2105 HRESULT
navigate_url(HTMLOuterWindow
*window
, const WCHAR
*new_url
, const WCHAR
*base_url
)
2107 WCHAR url
[INTERNET_MAX_URL_LENGTH
];
2113 }else if(base_url
) {
2116 hres
= CoInternetCombineUrl(base_url
, new_url
, URL_ESCAPE_SPACES_ONLY
|URL_DONT_ESCAPE_EXTRA_INFO
,
2117 url
, sizeof(url
)/sizeof(WCHAR
), &len
, 0);
2121 strcpyW(url
, new_url
);
2124 if(window
->doc_obj
&& window
->doc_obj
->hostui
) {
2125 OLECHAR
*translated_url
= NULL
;
2127 hres
= IDocHostUIHandler_TranslateUrl(window
->doc_obj
->hostui
, 0, url
,
2130 TRACE("%08x %s -> %s\n", hres
, debugstr_w(url
), debugstr_w(translated_url
));
2131 strcpyW(url
, translated_url
);
2132 CoTaskMemFree(translated_url
);
2136 if(window
->doc_obj
&& window
->doc_obj
->is_webbrowser
&& window
== window
->doc_obj
->basedoc
.window
) {
2137 BOOL cancel
= FALSE
;
2140 hres
= IDocObjectService_FireBeforeNavigate2(window
->doc_obj
->doc_object_service
, NULL
, url
, 0x40,
2141 NULL
, NULL
, 0, NULL
, TRUE
, &cancel
);
2142 if(SUCCEEDED(hres
) && cancel
) {
2143 TRACE("Navigation canceled\n");
2147 hres
= CreateUri(url
, 0, 0, &uri
);
2151 hres
= super_navigate(window
, uri
, NULL
, NULL
, 0);
2156 if(window
->doc_obj
&& window
== window
->doc_obj
->basedoc
.window
) {
2159 hres
= hlink_frame_navigate(&window
->base
.inner_window
->doc
->basedoc
, url
, NULL
, 0, &cancel
);
2164 TRACE("Navigation handled by hlink frame\n");
2169 hres
= create_doc_uri(window
, url
, &uri
);
2173 hres
= load_nsuri(window
, uri
, NULL
, LOAD_FLAGS_NONE
);
2174 nsISupports_Release((nsISupports
*)uri
);