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
25 #define NONAMELESSUNION
26 #define NONAMELESSSTRUCT
38 #include "shdeprecated.h"
40 #include "wine/debug.h"
42 #include "mshtml_private.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
47 #define CONTENT_LENGTH "Content-Length"
48 #define UTF8_STR "utf-8"
49 #define UTF16_STR "utf-16"
51 static const WCHAR emptyW[] = {0};
52 static const WCHAR text_htmlW[] = {'t','e','x','t','/','h','t','m','l',0};
60 struct nsProtocolStream {
61 nsIInputStream nsIInputStream_iface;
69 struct BSCallbackVtbl {
70 void (*destroy)(BSCallback*);
71 HRESULT (*init_bindinfo)(BSCallback*);
72 HRESULT (*start_binding)(BSCallback*);
73 HRESULT (*stop_binding)(BSCallback*,HRESULT);
74 HRESULT (*read_data)(BSCallback*,IStream*);
75 HRESULT (*on_progress)(BSCallback*,ULONG,LPCWSTR);
76 HRESULT (*on_response)(BSCallback*,DWORD,LPCWSTR);
77 HRESULT (*beginning_transaction)(BSCallback*,WCHAR**);
80 static inline nsProtocolStream *impl_from_nsIInputStream(nsIInputStream *iface)
82 return CONTAINING_RECORD(iface, nsProtocolStream, nsIInputStream_iface);
85 static nsresult NSAPI nsInputStream_QueryInterface(nsIInputStream *iface, nsIIDRef riid,
88 nsProtocolStream *This = impl_from_nsIInputStream(iface);
92 if(IsEqualGUID(&IID_nsISupports, riid)) {
93 TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
94 *result = &This->nsIInputStream_iface;
95 }else if(IsEqualGUID(&IID_nsIInputStream, riid)) {
96 TRACE("(%p)->(IID_nsIInputStream %p)\n", This, result);
97 *result = &This->nsIInputStream_iface;
101 nsIInputStream_AddRef(&This->nsIInputStream_iface);
105 WARN("unsupported interface %s\n", debugstr_guid(riid));
106 return NS_NOINTERFACE;
109 static nsrefcnt NSAPI nsInputStream_AddRef(nsIInputStream *iface)
111 nsProtocolStream *This = impl_from_nsIInputStream(iface);
112 LONG ref = InterlockedIncrement(&This->ref);
114 TRACE("(%p) ref=%d\n", This, ref);
120 static nsrefcnt NSAPI nsInputStream_Release(nsIInputStream *iface)
122 nsProtocolStream *This = impl_from_nsIInputStream(iface);
123 LONG ref = InterlockedDecrement(&This->ref);
125 TRACE("(%p) ref=%d\n", This, ref);
133 static nsresult NSAPI nsInputStream_Close(nsIInputStream *iface)
135 nsProtocolStream *This = impl_from_nsIInputStream(iface);
136 FIXME("(%p)\n", This);
137 return NS_ERROR_NOT_IMPLEMENTED;
140 static nsresult NSAPI nsInputStream_Available(nsIInputStream *iface, PRUint32 *_retval)
142 nsProtocolStream *This = impl_from_nsIInputStream(iface);
143 FIXME("(%p)->(%p)\n", This, _retval);
144 return NS_ERROR_NOT_IMPLEMENTED;
147 static nsresult NSAPI nsInputStream_Read(nsIInputStream *iface, char *aBuf, PRUint32 aCount,
150 nsProtocolStream *This = impl_from_nsIInputStream(iface);
153 TRACE("(%p)->(%p %d %p)\n", This, aBuf, aCount, _retval);
155 if(read > This->buf_size)
156 read = This->buf_size;
159 memcpy(aBuf, This->buf, read);
160 if(read < This->buf_size)
161 memmove(This->buf, This->buf+read, This->buf_size-read);
162 This->buf_size -= read;
169 static nsresult NSAPI nsInputStream_ReadSegments(nsIInputStream *iface,
170 nsresult (WINAPI *aWriter)(nsIInputStream*,void*,const char*,PRUint32,PRUint32,PRUint32*),
171 void *aClousure, PRUint32 aCount, PRUint32 *_retval)
173 nsProtocolStream *This = impl_from_nsIInputStream(iface);
174 PRUint32 written = 0;
177 TRACE("(%p)->(%p %p %d %p)\n", This, aWriter, aClousure, aCount, _retval);
182 if(aCount > This->buf_size)
183 aCount = This->buf_size;
185 nsres = aWriter(&This->nsIInputStream_iface, aClousure, This->buf, 0, aCount, &written);
187 TRACE("aWritter failed: %08x\n", nsres);
188 else if(written != This->buf_size)
189 FIXME("written %d != buf_size %d\n", written, This->buf_size);
191 This->buf_size -= written;
197 static nsresult NSAPI nsInputStream_IsNonBlocking(nsIInputStream *iface, cpp_bool *_retval)
199 nsProtocolStream *This = impl_from_nsIInputStream(iface);
200 FIXME("(%p)->(%p)\n", This, _retval);
201 return NS_ERROR_NOT_IMPLEMENTED;
204 static const nsIInputStreamVtbl nsInputStreamVtbl = {
205 nsInputStream_QueryInterface,
206 nsInputStream_AddRef,
207 nsInputStream_Release,
209 nsInputStream_Available,
211 nsInputStream_ReadSegments,
212 nsInputStream_IsNonBlocking
215 static nsProtocolStream *create_nsprotocol_stream(void)
217 nsProtocolStream *ret = heap_alloc(sizeof(nsProtocolStream));
219 ret->nsIInputStream_iface.lpVtbl = &nsInputStreamVtbl;
226 static inline BSCallback *impl_from_IBindStatusCallback(IBindStatusCallback *iface)
228 return CONTAINING_RECORD(iface, BSCallback, IBindStatusCallback_iface);
231 static HRESULT WINAPI BindStatusCallback_QueryInterface(IBindStatusCallback *iface,
232 REFIID riid, void **ppv)
234 BSCallback *This = impl_from_IBindStatusCallback(iface);
237 if(IsEqualGUID(&IID_IUnknown, riid)) {
238 TRACE("(%p)->(IID_IUnknown, %p)\n", This, ppv);
239 *ppv = &This->IBindStatusCallback_iface;
240 }else if(IsEqualGUID(&IID_IBindStatusCallback, riid)) {
241 TRACE("(%p)->(IID_IBindStatusCallback, %p)\n", This, ppv);
242 *ppv = &This->IBindStatusCallback_iface;
243 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
244 TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
245 *ppv = &This->IServiceProvider_iface;
246 }else if(IsEqualGUID(&IID_IHttpNegotiate, riid)) {
247 TRACE("(%p)->(IID_IHttpNegotiate %p)\n", This, ppv);
248 *ppv = &This->IHttpNegotiate2_iface;
249 }else if(IsEqualGUID(&IID_IHttpNegotiate2, riid)) {
250 TRACE("(%p)->(IID_IHttpNegotiate2 %p)\n", This, ppv);
251 *ppv = &This->IHttpNegotiate2_iface;
252 }else if(IsEqualGUID(&IID_IInternetBindInfo, riid)) {
253 TRACE("(%p)->(IID_IInternetBindInfo %p)\n", This, ppv);
254 *ppv = &This->IInternetBindInfo_iface;
258 IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
262 TRACE("Unsupported riid = %s\n", debugstr_guid(riid));
263 return E_NOINTERFACE;
266 static ULONG WINAPI BindStatusCallback_AddRef(IBindStatusCallback *iface)
268 BSCallback *This = impl_from_IBindStatusCallback(iface);
269 LONG ref = InterlockedIncrement(&This->ref);
271 TRACE("(%p) ref = %d\n", This, ref);
276 static ULONG WINAPI BindStatusCallback_Release(IBindStatusCallback *iface)
278 BSCallback *This = impl_from_IBindStatusCallback(iface);
279 LONG ref = InterlockedDecrement(&This->ref);
281 TRACE("(%p) ref = %d\n", This, ref);
285 GlobalFree(This->post_data);
287 IMoniker_Release(This->mon);
289 IBinding_Release(This->binding);
290 list_remove(&This->entry);
291 list_init(&This->entry);
292 heap_free(This->headers);
294 This->vtbl->destroy(This);
300 static HRESULT WINAPI BindStatusCallback_OnStartBinding(IBindStatusCallback *iface,
301 DWORD dwReserved, IBinding *pbind)
303 BSCallback *This = impl_from_IBindStatusCallback(iface);
305 TRACE("(%p)->(%d %p)\n", This, dwReserved, pbind);
307 IBinding_AddRef(pbind);
308 This->binding = pbind;
311 list_add_head(&This->window->bindings, &This->entry);
313 return This->vtbl->start_binding(This);
316 static HRESULT WINAPI BindStatusCallback_GetPriority(IBindStatusCallback *iface, LONG *pnPriority)
318 BSCallback *This = impl_from_IBindStatusCallback(iface);
319 FIXME("(%p)->(%p)\n", This, pnPriority);
323 static HRESULT WINAPI BindStatusCallback_OnLowResource(IBindStatusCallback *iface, DWORD reserved)
325 BSCallback *This = impl_from_IBindStatusCallback(iface);
326 FIXME("(%p)->(%d)\n", This, reserved);
330 static HRESULT WINAPI BindStatusCallback_OnProgress(IBindStatusCallback *iface, ULONG ulProgress,
331 ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
333 BSCallback *This = impl_from_IBindStatusCallback(iface);
335 TRACE("%p)->(%u %u %u %s)\n", This, ulProgress, ulProgressMax, ulStatusCode,
336 debugstr_w(szStatusText));
338 return This->vtbl->on_progress(This, ulStatusCode, szStatusText);
341 static HRESULT WINAPI BindStatusCallback_OnStopBinding(IBindStatusCallback *iface,
342 HRESULT hresult, LPCWSTR szError)
344 BSCallback *This = impl_from_IBindStatusCallback(iface);
347 TRACE("(%p)->(%08x %s)\n", This, hresult, debugstr_w(szError));
349 /* NOTE: IE7 calls GetBindResult here */
351 hres = This->vtbl->stop_binding(This, hresult);
354 IBinding_Release(This->binding);
355 This->binding = NULL;
358 list_remove(&This->entry);
359 list_init(&This->entry);
365 static HRESULT WINAPI BindStatusCallback_GetBindInfo(IBindStatusCallback *iface,
366 DWORD *grfBINDF, BINDINFO *pbindinfo)
368 BSCallback *This = impl_from_IBindStatusCallback(iface);
371 TRACE("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
373 if(!This->bindinfo_ready) {
376 hres = This->vtbl->init_bindinfo(This);
380 This->bindinfo_ready = TRUE;
383 *grfBINDF = This->bindf;
385 size = pbindinfo->cbSize;
386 memset(pbindinfo, 0, size);
387 pbindinfo->cbSize = size;
389 pbindinfo->cbstgmedData = This->post_data_len;
390 pbindinfo->dwCodePage = CP_UTF8;
391 pbindinfo->dwOptions = 0x80000;
393 if(This->post_data) {
394 pbindinfo->dwBindVerb = BINDVERB_POST;
396 pbindinfo->stgmedData.tymed = TYMED_HGLOBAL;
397 pbindinfo->stgmedData.u.hGlobal = This->post_data;
398 pbindinfo->stgmedData.pUnkForRelease = (IUnknown*)&This->IBindStatusCallback_iface;
399 IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
405 static HRESULT WINAPI BindStatusCallback_OnDataAvailable(IBindStatusCallback *iface,
406 DWORD grfBSCF, DWORD dwSize, FORMATETC *pformatetc, STGMEDIUM *pstgmed)
408 BSCallback *This = impl_from_IBindStatusCallback(iface);
410 TRACE("(%p)->(%08x %d %p %p)\n", This, grfBSCF, dwSize, pformatetc, pstgmed);
412 return This->vtbl->read_data(This, pstgmed->u.pstm);
415 static HRESULT WINAPI BindStatusCallback_OnObjectAvailable(IBindStatusCallback *iface,
416 REFIID riid, IUnknown *punk)
418 BSCallback *This = impl_from_IBindStatusCallback(iface);
419 FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), punk);
423 static const IBindStatusCallbackVtbl BindStatusCallbackVtbl = {
424 BindStatusCallback_QueryInterface,
425 BindStatusCallback_AddRef,
426 BindStatusCallback_Release,
427 BindStatusCallback_OnStartBinding,
428 BindStatusCallback_GetPriority,
429 BindStatusCallback_OnLowResource,
430 BindStatusCallback_OnProgress,
431 BindStatusCallback_OnStopBinding,
432 BindStatusCallback_GetBindInfo,
433 BindStatusCallback_OnDataAvailable,
434 BindStatusCallback_OnObjectAvailable
437 static inline BSCallback *impl_from_IHttpNegotiate2(IHttpNegotiate2 *iface)
439 return CONTAINING_RECORD(iface, BSCallback, IHttpNegotiate2_iface);
442 static HRESULT WINAPI HttpNegotiate_QueryInterface(IHttpNegotiate2 *iface,
443 REFIID riid, void **ppv)
445 BSCallback *This = impl_from_IHttpNegotiate2(iface);
446 return IBindStatusCallback_QueryInterface(&This->IBindStatusCallback_iface, riid, ppv);
449 static ULONG WINAPI HttpNegotiate_AddRef(IHttpNegotiate2 *iface)
451 BSCallback *This = impl_from_IHttpNegotiate2(iface);
452 return IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
455 static ULONG WINAPI HttpNegotiate_Release(IHttpNegotiate2 *iface)
457 BSCallback *This = impl_from_IHttpNegotiate2(iface);
458 return IBindStatusCallback_Release(&This->IBindStatusCallback_iface);
461 static HRESULT WINAPI HttpNegotiate_BeginningTransaction(IHttpNegotiate2 *iface,
462 LPCWSTR szURL, LPCWSTR szHeaders, DWORD dwReserved, LPWSTR *pszAdditionalHeaders)
464 BSCallback *This = impl_from_IHttpNegotiate2(iface);
467 TRACE("(%p)->(%s %s %d %p)\n", This, debugstr_w(szURL), debugstr_w(szHeaders),
468 dwReserved, pszAdditionalHeaders);
470 *pszAdditionalHeaders = NULL;
472 hres = This->vtbl->beginning_transaction(This, pszAdditionalHeaders);
479 size = (strlenW(This->headers)+1)*sizeof(WCHAR);
480 *pszAdditionalHeaders = CoTaskMemAlloc(size);
481 if(!*pszAdditionalHeaders)
482 return E_OUTOFMEMORY;
483 memcpy(*pszAdditionalHeaders, This->headers, size);
489 static HRESULT WINAPI HttpNegotiate_OnResponse(IHttpNegotiate2 *iface, DWORD dwResponseCode,
490 LPCWSTR szResponseHeaders, LPCWSTR szRequestHeaders, LPWSTR *pszAdditionalRequestHeaders)
492 BSCallback *This = impl_from_IHttpNegotiate2(iface);
494 TRACE("(%p)->(%d %s %s %p)\n", This, dwResponseCode, debugstr_w(szResponseHeaders),
495 debugstr_w(szRequestHeaders), pszAdditionalRequestHeaders);
497 return This->vtbl->on_response(This, dwResponseCode, szResponseHeaders);
500 static HRESULT WINAPI HttpNegotiate_GetRootSecurityId(IHttpNegotiate2 *iface,
501 BYTE *pbSecurityId, DWORD *pcbSecurityId, DWORD_PTR dwReserved)
503 BSCallback *This = impl_from_IHttpNegotiate2(iface);
504 FIXME("(%p)->(%p %p %ld)\n", This, pbSecurityId, pcbSecurityId, dwReserved);
508 static const IHttpNegotiate2Vtbl HttpNegotiate2Vtbl = {
509 HttpNegotiate_QueryInterface,
510 HttpNegotiate_AddRef,
511 HttpNegotiate_Release,
512 HttpNegotiate_BeginningTransaction,
513 HttpNegotiate_OnResponse,
514 HttpNegotiate_GetRootSecurityId
517 static inline BSCallback *impl_from_IInternetBindInfo(IInternetBindInfo *iface)
519 return CONTAINING_RECORD(iface, BSCallback, IInternetBindInfo_iface);
522 static HRESULT WINAPI InternetBindInfo_QueryInterface(IInternetBindInfo *iface,
523 REFIID riid, void **ppv)
525 BSCallback *This = impl_from_IInternetBindInfo(iface);
526 return IBindStatusCallback_QueryInterface(&This->IBindStatusCallback_iface, riid, ppv);
529 static ULONG WINAPI InternetBindInfo_AddRef(IInternetBindInfo *iface)
531 BSCallback *This = impl_from_IInternetBindInfo(iface);
532 return IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
535 static ULONG WINAPI InternetBindInfo_Release(IInternetBindInfo *iface)
537 BSCallback *This = impl_from_IInternetBindInfo(iface);
538 return IBindStatusCallback_Release(&This->IBindStatusCallback_iface);
541 static HRESULT WINAPI InternetBindInfo_GetBindInfo(IInternetBindInfo *iface,
542 DWORD *grfBINDF, BINDINFO *pbindinfo)
544 BSCallback *This = impl_from_IInternetBindInfo(iface);
545 FIXME("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
549 static HRESULT WINAPI InternetBindInfo_GetBindString(IInternetBindInfo *iface,
550 ULONG ulStringType, LPOLESTR *ppwzStr, ULONG cEl, ULONG *pcElFetched)
552 BSCallback *This = impl_from_IInternetBindInfo(iface);
553 FIXME("(%p)->(%u %p %u %p)\n", This, ulStringType, ppwzStr, cEl, pcElFetched);
557 static const IInternetBindInfoVtbl InternetBindInfoVtbl = {
558 InternetBindInfo_QueryInterface,
559 InternetBindInfo_AddRef,
560 InternetBindInfo_Release,
561 InternetBindInfo_GetBindInfo,
562 InternetBindInfo_GetBindString
565 static inline BSCallback *impl_from_IServiceProvider(IServiceProvider *iface)
567 return CONTAINING_RECORD(iface, BSCallback, IServiceProvider_iface);
570 static HRESULT WINAPI BSCServiceProvider_QueryInterface(IServiceProvider *iface,
571 REFIID riid, void **ppv)
573 BSCallback *This = impl_from_IServiceProvider(iface);
574 return IBindStatusCallback_QueryInterface(&This->IBindStatusCallback_iface, riid, ppv);
577 static ULONG WINAPI BSCServiceProvider_AddRef(IServiceProvider *iface)
579 BSCallback *This = impl_from_IServiceProvider(iface);
580 return IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
583 static ULONG WINAPI BSCServiceProvider_Release(IServiceProvider *iface)
585 BSCallback *This = impl_from_IServiceProvider(iface);
586 return IBindStatusCallback_Release(&This->IBindStatusCallback_iface);
589 static HRESULT WINAPI BSCServiceProvider_QueryService(IServiceProvider *iface,
590 REFGUID guidService, REFIID riid, void **ppv)
592 BSCallback *This = impl_from_IServiceProvider(iface);
594 TRACE("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
596 if(This->window && IsEqualGUID(guidService, &IID_IWindowForBindingUI))
597 return IServiceProvider_QueryService(&This->window->base.IServiceProvider_iface, guidService, riid, ppv);
598 return E_NOINTERFACE;
601 static const IServiceProviderVtbl ServiceProviderVtbl = {
602 BSCServiceProvider_QueryInterface,
603 BSCServiceProvider_AddRef,
604 BSCServiceProvider_Release,
605 BSCServiceProvider_QueryService
608 static void init_bscallback(BSCallback *This, const BSCallbackVtbl *vtbl, IMoniker *mon, DWORD bindf)
610 This->IBindStatusCallback_iface.lpVtbl = &BindStatusCallbackVtbl;
611 This->IServiceProvider_iface.lpVtbl = &ServiceProviderVtbl;
612 This->IHttpNegotiate2_iface.lpVtbl = &HttpNegotiate2Vtbl;
613 This->IInternetBindInfo_iface.lpVtbl = &InternetBindInfoVtbl;
617 This->bom = BOM_NONE;
619 list_init(&This->entry);
622 IMoniker_AddRef(mon);
626 static HRESULT read_stream(BSCallback *This, IStream *stream, void *buf, DWORD size, DWORD *ret_size)
628 DWORD read_size = 0, skip=0;
632 hres = IStream_Read(stream, buf, size, &read_size);
634 if(!This->readed && This->bom == BOM_NONE) {
635 if(read_size >= 2 && data[0] == 0xff && data[1] == 0xfe) {
636 This->bom = BOM_UTF16;
638 }else if(read_size >= 3 && data[0] == 0xef && data[1] == 0xbb && data[2] == 0xbf) {
639 This->bom = BOM_UTF8;
645 memmove(data, data+skip, read_size);
649 This->readed += read_size;
650 *ret_size = read_size;
654 static void parse_content_type(nsChannelBSC *This, const WCHAR *value)
659 static const WCHAR charsetW[] = {'c','h','a','r','s','e','t','='};
661 ptr = strchrW(value, ';');
666 while(*ptr && isspaceW(*ptr))
669 len = strlenW(value);
670 if(ptr + sizeof(charsetW)/sizeof(WCHAR) < value+len && !memicmpW(ptr, charsetW, sizeof(charsetW)/sizeof(WCHAR))) {
671 size_t charset_len, lena;
672 nsACString charset_str;
673 const WCHAR *charset;
676 ptr += sizeof(charsetW)/sizeof(WCHAR);
679 FIXME("Quoted value\n");
683 while(*ptr && *ptr != ',')
685 charset_len = ptr-charset;
688 lena = WideCharToMultiByte(CP_ACP, 0, charset, charset_len, NULL, 0, NULL, NULL);
689 charseta = heap_alloc(lena+1);
693 WideCharToMultiByte(CP_ACP, 0, charset, charset_len, charseta, lena, NULL, NULL);
696 nsACString_InitDepend(&charset_str, charseta);
697 nsIHttpChannel_SetContentCharset(&This->nschannel->nsIHttpChannel_iface, &charset_str);
698 nsACString_Finish(&charset_str);
701 FIXME("unhandled: %s\n", debugstr_wn(ptr, len - (ptr-value)));
705 static HRESULT parse_headers(const WCHAR *headers, struct list *headers_list)
707 const WCHAR *header, *header_end, *colon, *value;
712 if(header[0] == '\r' && header[1] == '\n' && !header[2])
714 for(colon = header; *colon && *colon != ':' && *colon != '\r'; colon++);
724 for(header_end = value+1; *header_end && *header_end != '\r'; header_end++);
726 hres = set_http_header(headers_list, header, colon-header, value, header_end-value);
731 if(header[0] == '\r' && header[1] == '\n')
738 static HRESULT process_response_headers(nsChannelBSC *This, const WCHAR *headers)
743 static const WCHAR content_typeW[] = {'c','o','n','t','e','n','t','-','t','y','p','e',0};
745 hres = parse_headers(headers, &This->nschannel->response_headers);
749 LIST_FOR_EACH_ENTRY(iter, &This->nschannel->response_headers, http_header_t, entry) {
750 if(!strcmpiW(iter->header, content_typeW))
751 parse_content_type(This, iter->data);
757 static void query_http_info(nsChannelBSC *This, IWinInetHttpInfo *wininet_info)
763 IWinInetHttpInfo_QueryInfo(wininet_info, HTTP_QUERY_RAW_HEADERS_CRLF, NULL, &len, NULL, NULL);
767 buf = heap_alloc(len);
771 IWinInetHttpInfo_QueryInfo(wininet_info, HTTP_QUERY_RAW_HEADERS_CRLF, buf, &len, NULL, NULL);
777 ptr = strchrW(buf, '\r');
778 if(ptr && ptr[1] == '\n') {
780 process_response_headers(This, ptr);
786 HRESULT start_binding(HTMLInnerWindow *inner_window, BSCallback *bscallback, IBindCtx *bctx)
791 TRACE("(%p %p %p)\n", inner_window, bscallback, bctx);
793 bscallback->window = inner_window;
795 /* NOTE: IE7 calls IsSystemMoniker here*/
798 RegisterBindStatusCallback(bctx, &bscallback->IBindStatusCallback_iface, NULL, 0);
799 IBindCtx_AddRef(bctx);
801 hres = CreateAsyncBindCtx(0, &bscallback->IBindStatusCallback_iface, NULL, &bctx);
803 WARN("CreateAsyncBindCtx failed: %08x\n", hres);
804 bscallback->vtbl->stop_binding(bscallback, hres);
809 hres = IMoniker_BindToStorage(bscallback->mon, bctx, NULL, &IID_IStream, (void**)&str);
810 IBindCtx_Release(bctx);
812 WARN("BindToStorage failed: %08x\n", hres);
813 bscallback->vtbl->stop_binding(bscallback, hres);
818 IStream_Release(str);
820 IMoniker_Release(bscallback->mon);
821 bscallback->mon = NULL;
834 static inline BufferBSC *BufferBSC_from_BSCallback(BSCallback *iface)
836 return CONTAINING_RECORD(iface, BufferBSC, bsc);
839 static void BufferBSC_destroy(BSCallback *bsc)
841 BufferBSC *This = BufferBSC_from_BSCallback(bsc);
843 heap_free(This->buf);
847 static HRESULT BufferBSC_init_bindinfo(BSCallback *bsc)
852 static HRESULT BufferBSC_start_binding(BSCallback *bsc)
857 static HRESULT BufferBSC_stop_binding(BSCallback *bsc, HRESULT result)
859 BufferBSC *This = BufferBSC_from_BSCallback(bsc);
864 heap_free(This->buf);
872 static HRESULT BufferBSC_read_data(BSCallback *bsc, IStream *stream)
874 BufferBSC *This = BufferBSC_from_BSCallback(bsc);
880 This->buf = heap_alloc(This->size);
884 if(This->bsc.readed >= This->size) {
886 This->buf = heap_realloc(This->buf, This->size);
889 hres = read_stream(&This->bsc, stream, This->buf+This->bsc.readed, This->size-This->bsc.readed, &readed);
890 }while(hres == S_OK);
895 static HRESULT BufferBSC_on_progress(BSCallback *bsc, ULONG status_code, LPCWSTR status_text)
900 static HRESULT BufferBSC_on_response(BSCallback *bsc, DWORD response_code,
901 LPCWSTR response_headers)
906 static HRESULT BufferBSC_beginning_transaction(BSCallback *bsc, WCHAR **additional_headers)
911 static const BSCallbackVtbl BufferBSCVtbl = {
913 BufferBSC_init_bindinfo,
914 BufferBSC_start_binding,
915 BufferBSC_stop_binding,
917 BufferBSC_on_progress,
918 BufferBSC_on_response,
919 BufferBSC_beginning_transaction
923 static BufferBSC *create_bufferbsc(IMoniker *mon)
925 BufferBSC *ret = heap_alloc_zero(sizeof(*ret));
927 init_bscallback(&ret->bsc, &BufferBSCVtbl, mon, 0);
933 HRESULT bind_mon_to_wstr(HTMLInnerWindow *window, IMoniker *mon, WCHAR **ret)
935 BufferBSC *bsc = create_bufferbsc(mon);
940 hres = start_binding(window, &bsc->bsc, NULL);
944 IBindStatusCallback_Release(&bsc->bsc.IBindStatusCallback_iface);
948 if(!bsc->bsc.readed) {
953 switch(bsc->bsc.bom) {
955 if(bsc->bsc.readed % sizeof(WCHAR)) {
956 FIXME("The buffer is not a valid utf16 string\n");
961 text = heap_alloc(bsc->bsc.readed+sizeof(WCHAR));
963 hres = E_OUTOFMEMORY;
967 memcpy(text, bsc->buf, bsc->bsc.readed);
968 text[bsc->bsc.readed/sizeof(WCHAR)] = 0;
977 len = MultiByteToWideChar(cp, 0, bsc->buf, bsc->bsc.readed, NULL, 0);
978 text = heap_alloc((len+1)*sizeof(WCHAR));
980 hres = E_OUTOFMEMORY;
984 MultiByteToWideChar(CP_ACP, 0, bsc->buf, bsc->bsc.readed, text, len);
989 IBindStatusCallback_Release(&bsc->bsc.IBindStatusCallback_iface);
997 static HRESULT read_post_data_stream(nsChannelBSC *This, nsChannel *nschannel)
999 PRUint32 data_len = 0, available = 0;
1000 char *data, *post_data;
1002 HRESULT hres = S_OK;
1004 if(!nschannel->post_data_stream)
1007 nsres = nsIInputStream_Available(nschannel->post_data_stream, &available);
1008 if(NS_FAILED(nsres))
1011 post_data = data = GlobalAlloc(0, available);
1013 return E_OUTOFMEMORY;
1015 nsres = nsIInputStream_Read(nschannel->post_data_stream, data, available, &data_len);
1016 if(NS_FAILED(nsres)) {
1021 if(nschannel->post_data_contains_headers) {
1022 if(data_len >= 2 && data[0] == '\r' && data[1] == '\n') {
1030 post_data += data_len;
1031 for(ptr = data; ptr+4 < data+data_len; ptr++) {
1032 if(!memcmp(ptr, "\r\n\r\n", 4)) {
1038 data_len -= post_data-data;
1040 size = MultiByteToWideChar(CP_ACP, 0, data, post_data-data, NULL, 0);
1041 headers = heap_alloc((size+1)*sizeof(WCHAR));
1043 MultiByteToWideChar(CP_ACP, 0, data, post_data-data, headers, size);
1045 hres = parse_headers(headers , &nschannel->request_headers);
1047 This->bsc.headers = headers;
1051 hres = E_OUTOFMEMORY;
1064 }else if(post_data != data) {
1067 new_data = GlobalAlloc(0, data_len);
1069 memcpy(new_data, post_data, data_len);
1072 return E_OUTOFMEMORY;
1073 post_data = new_data;
1076 This->bsc.post_data = post_data;
1077 This->bsc.post_data_len = data_len;
1078 TRACE("post_data = %s\n", debugstr_a(This->bsc.post_data));
1082 static HRESULT on_start_nsrequest(nsChannelBSC *This)
1086 /* FIXME: it's needed for http connections from BindToObject. */
1087 if(!This->nschannel->response_status)
1088 This->nschannel->response_status = 200;
1090 nsres = nsIStreamListener_OnStartRequest(This->nslistener,
1091 (nsIRequest*)&This->nschannel->nsIHttpChannel_iface, This->nscontext);
1092 if(NS_FAILED(nsres)) {
1093 FIXME("OnStartRequest failed: %08x\n", nsres);
1097 if(This->is_doc_channel) {
1098 update_window_doc(This->bsc.window);
1099 if(This->bsc.window->base.outer_window->readystate != READYSTATE_LOADING)
1100 set_ready_state(This->bsc.window->base.outer_window, READYSTATE_LOADING);
1106 static void on_stop_nsrequest(nsChannelBSC *This, HRESULT result)
1108 nsresult nsres, request_result;
1112 request_result = NS_OK;
1115 request_result = NS_BINDING_ABORTED;
1118 request_result = NS_ERROR_FAILURE;
1121 if(This->nslistener) {
1122 nsres = nsIStreamListener_OnStopRequest(This->nslistener,
1123 (nsIRequest*)&This->nschannel->nsIHttpChannel_iface, This->nscontext,
1125 if(NS_FAILED(nsres))
1126 WARN("OnStopRequest failed: %08x\n", nsres);
1129 if(This->nschannel->load_group) {
1130 nsres = nsILoadGroup_RemoveRequest(This->nschannel->load_group,
1131 (nsIRequest*)&This->nschannel->nsIHttpChannel_iface, NULL, request_result);
1132 if(NS_FAILED(nsres))
1133 ERR("RemoveRequest failed: %08x\n", nsres);
1137 static HRESULT read_stream_data(nsChannelBSC *This, IStream *stream)
1143 if(!This->response_processed) {
1144 IWinInetHttpInfo *wininet_info;
1146 This->response_processed = TRUE;
1147 if(This->bsc.binding) {
1148 hres = IBinding_QueryInterface(This->bsc.binding, &IID_IWinInetHttpInfo, (void**)&wininet_info);
1149 if(SUCCEEDED(hres)) {
1150 query_http_info(This, wininet_info);
1151 IWinInetHttpInfo_Release(wininet_info);
1156 if(!This->nslistener) {
1160 hres = read_stream(&This->bsc, stream, buf, sizeof(buf), &read);
1161 }while(hres == S_OK && read);
1167 This->nsstream = create_nsprotocol_stream();
1170 BOOL first_read = !This->bsc.readed;
1172 hres = read_stream(&This->bsc, stream, This->nsstream->buf+This->nsstream->buf_size,
1173 sizeof(This->nsstream->buf)-This->nsstream->buf_size, &read);
1177 This->nsstream->buf_size += read;
1180 switch(This->bsc.bom) {
1182 This->nschannel->charset = heap_strdupA(UTF8_STR);
1185 This->nschannel->charset = heap_strdupA(UTF16_STR);
1188 if(!This->nschannel->content_type) {
1191 hres = FindMimeFromData(NULL, NULL, This->nsstream->buf, This->nsstream->buf_size,
1192 This->is_doc_channel ? text_htmlW : NULL, 0, &mime, 0);
1196 TRACE("Found MIME %s\n", debugstr_w(mime));
1198 This->nschannel->content_type = heap_strdupWtoA(mime);
1199 CoTaskMemFree(mime);
1200 if(!This->nschannel->content_type)
1201 return E_OUTOFMEMORY;
1204 on_start_nsrequest(This);
1207 nsres = nsIStreamListener_OnDataAvailable(This->nslistener,
1208 (nsIRequest*)&This->nschannel->nsIHttpChannel_iface, This->nscontext,
1209 &This->nsstream->nsIInputStream_iface, This->bsc.readed-This->nsstream->buf_size,
1210 This->nsstream->buf_size);
1211 if(NS_FAILED(nsres))
1212 ERR("OnDataAvailable failed: %08x\n", nsres);
1214 if(This->nsstream->buf_size == sizeof(This->nsstream->buf)) {
1215 ERR("buffer is full\n");
1218 }while(hres == S_OK);
1224 nsIAsyncVerifyRedirectCallback nsIAsyncVerifyRedirectCallback_iface;
1228 nsChannel *nschannel;
1230 } nsRedirectCallback;
1232 static nsRedirectCallback *impl_from_nsIAsyncVerifyRedirectCallback(nsIAsyncVerifyRedirectCallback *iface)
1234 return CONTAINING_RECORD(iface, nsRedirectCallback, nsIAsyncVerifyRedirectCallback_iface);
1237 static nsresult NSAPI nsAsyncVerifyRedirectCallback_QueryInterface(nsIAsyncVerifyRedirectCallback *iface,
1238 nsIIDRef riid, void **result)
1240 nsRedirectCallback *This = impl_from_nsIAsyncVerifyRedirectCallback(iface);
1242 if(IsEqualGUID(&IID_nsISupports, riid)) {
1243 TRACE("(%p)->(IID_nsISuports %p)\n", This, result);
1244 *result = &This->nsIAsyncVerifyRedirectCallback_iface;
1245 }else if(IsEqualGUID(&IID_nsIAsyncVerifyRedirectCallback, riid)) {
1246 TRACE("(%p)->(IID_nsIAsyncVerifyRedirectCallback %p)\n", This, result);
1247 *result = &This->nsIAsyncVerifyRedirectCallback_iface;
1250 WARN("unimplemented iface %s\n", debugstr_guid(riid));
1251 return NS_NOINTERFACE;
1254 nsISupports_AddRef((nsISupports*)*result);
1258 static nsrefcnt NSAPI nsAsyncVerifyRedirectCallback_AddRef(nsIAsyncVerifyRedirectCallback *iface)
1260 nsRedirectCallback *This = impl_from_nsIAsyncVerifyRedirectCallback(iface);
1261 LONG ref = InterlockedIncrement(&This->ref);
1263 TRACE("(%p) ref=%d\n", This, ref);
1268 static nsrefcnt NSAPI nsAsyncVerifyRedirectCallback_Release(nsIAsyncVerifyRedirectCallback *iface)
1270 nsRedirectCallback *This = impl_from_nsIAsyncVerifyRedirectCallback(iface);
1271 LONG ref = InterlockedDecrement(&This->ref);
1273 TRACE("(%p) ref=%d\n", This, ref);
1276 IBindStatusCallback_Release(&This->bsc->bsc.IBindStatusCallback_iface);
1277 nsIHttpChannel_Release(&This->nschannel->nsIHttpChannel_iface);
1284 static nsresult NSAPI nsAsyncVerifyRedirectCallback_AsyncOnChannelRedirect(nsIAsyncVerifyRedirectCallback *iface, nsresult result)
1286 nsRedirectCallback *This = impl_from_nsIAsyncVerifyRedirectCallback(iface);
1287 nsChannel *old_nschannel;
1290 TRACE("(%p)->(%08x)\n", This, result);
1292 old_nschannel = This->bsc->nschannel;
1293 nsIHttpChannel_AddRef(&This->nschannel->nsIHttpChannel_iface);
1294 This->bsc->nschannel = This->nschannel;
1296 if(This->nschannel->load_group) {
1297 nsres = nsILoadGroup_AddRequest(This->nschannel->load_group, (nsIRequest*)&This->nschannel->nsIHttpChannel_iface,
1299 if(NS_FAILED(nsres))
1300 ERR("AddRequest failed: %08x\n", nsres);
1303 if(This->bsc->is_doc_channel) {
1304 IUri *uri = nsuri_get_uri(This->nschannel->uri);
1307 set_current_uri(This->bsc->bsc.window->base.outer_window, uri);
1310 WARN("Could not get IUri from nsWineURI\n");
1315 if(old_nschannel->load_group) {
1316 nsres = nsILoadGroup_RemoveRequest(old_nschannel->load_group,
1317 (nsIRequest*)&old_nschannel->nsIHttpChannel_iface, NULL, NS_OK);
1318 if(NS_FAILED(nsres))
1319 ERR("RemoveRequest failed: %08x\n", nsres);
1321 nsIHttpChannel_Release(&old_nschannel->nsIHttpChannel_iface);
1327 static const nsIAsyncVerifyRedirectCallbackVtbl nsAsyncVerifyRedirectCallbackVtbl = {
1328 nsAsyncVerifyRedirectCallback_QueryInterface,
1329 nsAsyncVerifyRedirectCallback_AddRef,
1330 nsAsyncVerifyRedirectCallback_Release,
1331 nsAsyncVerifyRedirectCallback_AsyncOnChannelRedirect
1334 static HRESULT create_redirect_callback(nsChannel *nschannel, nsChannelBSC *bsc, nsRedirectCallback **ret)
1336 nsRedirectCallback *callback;
1338 callback = heap_alloc(sizeof(*callback));
1340 return E_OUTOFMEMORY;
1342 callback->nsIAsyncVerifyRedirectCallback_iface.lpVtbl = &nsAsyncVerifyRedirectCallbackVtbl;
1345 nsIHttpChannel_AddRef(&nschannel->nsIHttpChannel_iface);
1346 callback->nschannel = nschannel;
1348 IBindStatusCallback_AddRef(&bsc->bsc.IBindStatusCallback_iface);
1349 callback->bsc = bsc;
1355 static inline nsChannelBSC *nsChannelBSC_from_BSCallback(BSCallback *iface)
1357 return CONTAINING_RECORD(iface, nsChannelBSC, bsc);
1360 static void nsChannelBSC_destroy(BSCallback *bsc)
1362 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1365 nsIHttpChannel_Release(&This->nschannel->nsIHttpChannel_iface);
1366 if(This->nslistener)
1367 nsIStreamListener_Release(This->nslistener);
1369 nsISupports_Release(This->nscontext);
1371 nsIInputStream_Release(&This->nsstream->nsIInputStream_iface);
1375 static HRESULT nsChannelBSC_start_binding(BSCallback *bsc)
1377 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1379 if(This->is_doc_channel)
1380 This->bsc.window->base.outer_window->base.inner_window->doc->skip_mutation_notif = FALSE;
1385 static HRESULT nsChannelBSC_init_bindinfo(BSCallback *bsc)
1387 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1390 if(This->nschannel && This->nschannel->post_data_stream) {
1391 hres = read_post_data_stream(This, This->nschannel);
1402 } stop_request_task_t;
1404 static void stop_request_proc(task_t *_task)
1406 stop_request_task_t *task = (stop_request_task_t*)_task;
1408 TRACE("(%p)\n", task->bsc);
1410 list_remove(&task->bsc->bsc.entry);
1411 list_init(&task->bsc->bsc.entry);
1412 on_stop_nsrequest(task->bsc, S_OK);
1415 static void stop_request_task_destr(task_t *_task)
1417 stop_request_task_t *task = (stop_request_task_t*)_task;
1419 IBindStatusCallback_Release(&task->bsc->bsc.IBindStatusCallback_iface);
1423 static HRESULT async_stop_request(nsChannelBSC *This)
1425 stop_request_task_t *task;
1427 if(!This->bsc.readed) {
1428 TRACE("No data read, calling OnStartRequest\n");
1429 on_start_nsrequest(This);
1432 task = heap_alloc(sizeof(*task));
1434 return E_OUTOFMEMORY;
1436 IBindStatusCallback_AddRef(&This->bsc.IBindStatusCallback_iface);
1439 push_task(&task->header, stop_request_proc, stop_request_task_destr, This->bsc.window->task_magic);
1443 static void handle_navigation_error(nsChannelBSC *This, DWORD result)
1445 HTMLOuterWindow *outer_window;
1446 HTMLDocumentObj *doc;
1447 IOleCommandTarget *olecmd;
1450 SAFEARRAYBOUND bound;
1451 VARIANT var, varOut;
1456 if(!This->is_doc_channel || !This->bsc.window)
1459 outer_window = This->bsc.window->base.outer_window;
1461 doc = outer_window->doc_obj;
1462 if(!doc || !doc->doc_object_service || !doc->client)
1465 hres = IDocObjectService_IsErrorUrl(doc->doc_object_service,
1466 outer_window->url, &is_error_url);
1467 if(FAILED(hres) || is_error_url)
1470 hres = IOleClientSite_QueryInterface(doc->client,
1471 &IID_IOleCommandTarget, (void**)&olecmd);
1476 bound.cElements = 8;
1477 sa = SafeArrayCreate(VT_VARIANT, 1, &bound);
1479 IOleCommandTarget_Release(olecmd);
1485 V_I4(&var) = result;
1486 SafeArrayPutElement(sa, &ind, &var);
1489 V_VT(&var) = VT_BSTR;
1490 V_BSTR(&var) = outer_window->url;
1491 SafeArrayPutElement(sa, &ind, &var);
1494 V_VT(&var) = VT_UNKNOWN;
1495 V_UNKNOWN(&var) = (IUnknown*)&outer_window->base.IHTMLWindow2_iface;
1496 SafeArrayPutElement(sa, &ind, &var);
1498 /* FIXME: what are the following fields for? */
1500 V_VT(&var) = VT_UNKNOWN;
1501 V_UNKNOWN(&var) = NULL;
1502 SafeArrayPutElement(sa, &ind, &var);
1505 V_VT(&var) = VT_BOOL;
1506 V_BOOL(&var) = FALSE;
1507 SafeArrayPutElement(sa, &ind, &var);
1510 V_VT(&var) = VT_BOOL;
1511 V_BOOL(&var) = FALSE;
1512 SafeArrayPutElement(sa, &ind, &var);
1515 V_VT(&var) = VT_BSTR;
1516 unk = SysAllocString(NULL);
1518 SafeArrayPutElement(sa, &ind, &var);
1521 V_VT(&var) = VT_UNKNOWN;
1522 V_UNKNOWN(&var) = NULL;
1523 SafeArrayPutElement(sa, &ind, &var);
1525 V_VT(&var) = VT_ARRAY;
1527 V_VT(&varOut) = VT_BOOL;
1528 V_BOOL(&varOut) = VARIANT_TRUE;
1529 IOleCommandTarget_Exec(olecmd, &CGID_DocHostCmdPriv, 1, 0, &var, FAILED(hres)?NULL:&varOut);
1532 SafeArrayDestroy(sa);
1533 IOleCommandTarget_Release(olecmd);
1536 static HRESULT nsChannelBSC_stop_binding(BSCallback *bsc, HRESULT result)
1538 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1540 if(result != E_ABORT) {
1542 handle_navigation_error(This, result);
1543 else if(This->is_doc_channel) {
1544 result = async_stop_request(This);
1545 if(SUCCEEDED(result))
1550 on_stop_nsrequest(This, result);
1554 static HRESULT nsChannelBSC_read_data(BSCallback *bsc, IStream *stream)
1556 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1558 return read_stream_data(This, stream);
1561 static HRESULT handle_redirect(nsChannelBSC *This, const WCHAR *new_url)
1563 nsRedirectCallback *callback;
1564 nsIChannelEventSink *sink;
1565 nsChannel *new_channel;
1569 TRACE("(%p)->(%s)\n", This, debugstr_w(new_url));
1571 if(!This->nschannel || !This->nschannel->notif_callback)
1574 nsres = nsIInterfaceRequestor_GetInterface(This->nschannel->notif_callback, &IID_nsIChannelEventSink, (void**)&sink);
1575 if(NS_FAILED(nsres))
1578 hres = create_redirect_nschannel(new_url, This->nschannel, &new_channel);
1579 if(SUCCEEDED(hres)) {
1580 TRACE("%p %p->%p\n", This, This->nschannel, new_channel);
1582 hres = create_redirect_callback(new_channel, This, &callback);
1583 nsIHttpChannel_Release(&new_channel->nsIHttpChannel_iface);
1586 if(SUCCEEDED(hres)) {
1587 nsres = nsIChannelEventSink_AsyncOnChannelRedirect(sink, (nsIChannel*)&This->nschannel->nsIHttpChannel_iface,
1588 (nsIChannel*)&callback->nschannel->nsIHttpChannel_iface, REDIRECT_TEMPORARY, /* FIXME */
1589 &callback->nsIAsyncVerifyRedirectCallback_iface);
1591 if(NS_FAILED(nsres))
1592 FIXME("AsyncOnChannelRedirect failed: %08x\n", hres);
1593 else if(This->nschannel != callback->nschannel)
1594 FIXME("nschannel not updated\n");
1596 nsIAsyncVerifyRedirectCallback_Release(&callback->nsIAsyncVerifyRedirectCallback_iface);
1599 nsIChannelEventSink_Release(sink);
1603 static HRESULT nsChannelBSC_on_progress(BSCallback *bsc, ULONG status_code, LPCWSTR status_text)
1605 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1607 switch(status_code) {
1608 case BINDSTATUS_MIMETYPEAVAILABLE:
1609 if(!This->nschannel)
1612 heap_free(This->nschannel->content_type);
1613 This->nschannel->content_type = heap_strdupWtoA(status_text);
1615 case BINDSTATUS_REDIRECTING:
1616 return handle_redirect(This, status_text);
1617 case BINDSTATUS_BEGINDOWNLOADDATA: {
1618 IWinInetHttpInfo *http_info;
1619 DWORD status, size = sizeof(DWORD);
1622 if(!This->bsc.binding)
1625 hres = IBinding_QueryInterface(This->bsc.binding, &IID_IWinInetHttpInfo, (void**)&http_info);
1629 hres = IWinInetHttpInfo_QueryInfo(http_info,
1630 HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL, NULL);
1631 IWinInetHttpInfo_Release(http_info);
1632 if(FAILED(hres) || status == HTTP_STATUS_OK)
1635 handle_navigation_error(This, status);
1642 static HRESULT nsChannelBSC_on_response(BSCallback *bsc, DWORD response_code,
1643 LPCWSTR response_headers)
1645 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1648 This->response_processed = TRUE;
1649 This->nschannel->response_status = response_code;
1651 if(response_headers) {
1652 const WCHAR *headers;
1654 headers = strchrW(response_headers, '\r');
1655 if(headers && headers[1] == '\n') {
1657 hres = process_response_headers(This, headers);
1659 WARN("parsing headers failed: %08x\n", hres);
1668 static HRESULT nsChannelBSC_beginning_transaction(BSCallback *bsc, WCHAR **additional_headers)
1670 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1671 http_header_t *iter;
1675 static const WCHAR content_lengthW[] =
1676 {'C','o','n','t','e','n','t','-','L','e','n','g','t','h',0};
1678 if(!This->nschannel)
1681 LIST_FOR_EACH_ENTRY(iter, &This->nschannel->request_headers, http_header_t, entry) {
1682 if(strcmpW(iter->header, content_lengthW))
1683 len += strlenW(iter->header) + 2 /* ": " */ + strlenW(iter->data) + 2 /* "\r\n" */;
1689 *additional_headers = ptr = CoTaskMemAlloc((len+1)*sizeof(WCHAR));
1691 return E_OUTOFMEMORY;
1693 LIST_FOR_EACH_ENTRY(iter, &This->nschannel->request_headers, http_header_t, entry) {
1694 if(!strcmpW(iter->header, content_lengthW))
1697 len = strlenW(iter->header);
1698 memcpy(ptr, iter->header, len*sizeof(WCHAR));
1704 len = strlenW(iter->data);
1705 memcpy(ptr, iter->data, len*sizeof(WCHAR));
1717 static const BSCallbackVtbl nsChannelBSCVtbl = {
1718 nsChannelBSC_destroy,
1719 nsChannelBSC_init_bindinfo,
1720 nsChannelBSC_start_binding,
1721 nsChannelBSC_stop_binding,
1722 nsChannelBSC_read_data,
1723 nsChannelBSC_on_progress,
1724 nsChannelBSC_on_response,
1725 nsChannelBSC_beginning_transaction
1728 HRESULT create_channelbsc(IMoniker *mon, const WCHAR *headers, BYTE *post_data, DWORD post_data_size,
1729 BOOL is_doc_binding, nsChannelBSC **retval)
1733 ret = heap_alloc_zero(sizeof(*ret));
1735 return E_OUTOFMEMORY;
1737 init_bscallback(&ret->bsc, &nsChannelBSCVtbl, mon, BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA);
1738 ret->is_doc_channel = is_doc_binding;
1741 ret->bsc.headers = heap_strdupW(headers);
1742 if(!ret->bsc.headers) {
1743 IBindStatusCallback_Release(&ret->bsc.IBindStatusCallback_iface);
1744 return E_OUTOFMEMORY;
1749 ret->bsc.post_data = GlobalAlloc(0, post_data_size);
1750 if(!ret->bsc.post_data) {
1751 heap_free(ret->bsc.headers);
1752 IBindStatusCallback_Release(&ret->bsc.IBindStatusCallback_iface);
1753 return E_OUTOFMEMORY;
1756 memcpy(ret->bsc.post_data, post_data, post_data_size);
1757 ret->bsc.post_data_len = post_data_size;
1766 HTMLOuterWindow *window;
1767 HTMLInnerWindow *pending_window;
1768 } start_doc_binding_task_t;
1770 static void start_doc_binding_proc(task_t *_task)
1772 start_doc_binding_task_t *task = (start_doc_binding_task_t*)_task;
1774 set_current_mon(task->window, task->pending_window->bscallback->bsc.mon);
1775 start_binding(task->pending_window, &task->pending_window->bscallback->bsc, NULL);
1778 static void start_doc_binding_task_destr(task_t *_task)
1780 start_doc_binding_task_t *task = (start_doc_binding_task_t*)_task;
1782 IHTMLWindow2_Release(&task->pending_window->base.IHTMLWindow2_iface);
1786 HRESULT async_start_doc_binding(HTMLOuterWindow *window, HTMLInnerWindow *pending_window)
1788 start_doc_binding_task_t *task;
1790 TRACE("%p\n", pending_window);
1792 task = heap_alloc(sizeof(start_doc_binding_task_t));
1794 return E_OUTOFMEMORY;
1796 task->window = window;
1797 task->pending_window = pending_window;
1798 IHTMLWindow2_AddRef(&pending_window->base.IHTMLWindow2_iface);
1800 push_task(&task->header, start_doc_binding_proc, start_doc_binding_task_destr, pending_window->task_magic);
1804 void abort_window_bindings(HTMLInnerWindow *window)
1808 remove_target_tasks(window->task_magic);
1810 while(!list_empty(&window->bindings)) {
1811 iter = LIST_ENTRY(window->bindings.next, BSCallback, entry);
1813 TRACE("Aborting %p\n", iter);
1815 IBindStatusCallback_AddRef(&iter->IBindStatusCallback_iface);
1818 IBinding_Abort(iter->binding);
1820 iter->vtbl->stop_binding(iter, E_ABORT);
1822 iter->window = NULL;
1823 list_remove(&iter->entry);
1824 list_init(&iter->entry);
1826 IBindStatusCallback_Release(&iter->IBindStatusCallback_iface);
1829 if(window->bscallback) {
1830 IBindStatusCallback_Release(&window->bscallback->bsc.IBindStatusCallback_iface);
1831 window->bscallback = NULL;
1835 IMoniker_Release(window->mon);
1840 HRESULT channelbsc_load_stream(HTMLInnerWindow *pending_window, IStream *stream)
1842 nsChannelBSC *bscallback = pending_window->bscallback;
1843 HRESULT hres = S_OK;
1845 if(!bscallback->nschannel) {
1846 ERR("NULL nschannel\n");
1850 bscallback->nschannel->content_type = heap_strdupA("text/html");
1851 if(!bscallback->nschannel->content_type)
1852 return E_OUTOFMEMORY;
1854 bscallback->bsc.window = pending_window;
1856 hres = read_stream_data(bscallback, stream);
1858 hres = async_stop_request(bscallback);
1860 IBindStatusCallback_OnStopBinding(&bscallback->bsc.IBindStatusCallback_iface, hres,
1866 void channelbsc_set_channel(nsChannelBSC *This, nsChannel *channel, nsIStreamListener *listener, nsISupports *context)
1868 nsIHttpChannel_AddRef(&channel->nsIHttpChannel_iface);
1869 This->nschannel = channel;
1871 nsIStreamListener_AddRef(listener);
1872 This->nslistener = listener;
1875 nsISupports_AddRef(context);
1876 This->nscontext = context;
1879 if(This->bsc.headers) {
1882 hres = parse_headers(This->bsc.headers, &channel->request_headers);
1883 heap_free(This->bsc.headers);
1884 This->bsc.headers = NULL;
1886 WARN("parse_headers failed: %08x\n", hres);
1892 HTMLOuterWindow *window;
1894 } navigate_javascript_task_t;
1896 static void navigate_javascript_proc(task_t *_task)
1898 navigate_javascript_task_t *task = (navigate_javascript_task_t*)_task;
1899 HTMLOuterWindow *window = task->window;
1904 static const WCHAR jscriptW[] = {'j','s','c','r','i','p','t',0};
1906 task->window->readystate = READYSTATE_COMPLETE;
1908 hres = IUri_GetPath(task->uri, &code);
1912 hres = UrlUnescapeW(code, NULL, NULL, URL_UNESCAPE_INPLACE);
1914 SysFreeString(code);
1918 set_download_state(window->doc_obj, 1);
1920 V_VT(&v) = VT_EMPTY;
1921 hres = exec_script(window->base.inner_window, code, jscriptW, &v);
1922 SysFreeString(code);
1923 if(SUCCEEDED(hres) && V_VT(&v) != VT_EMPTY) {
1924 FIXME("javascirpt URL returned %s\n", debugstr_variant(&v));
1928 if(window->doc_obj->view_sink)
1929 IAdviseSink_OnViewChange(window->doc_obj->view_sink, DVASPECT_CONTENT, -1);
1931 set_download_state(window->doc_obj, 0);
1934 static void navigate_javascript_task_destr(task_t *_task)
1936 navigate_javascript_task_t *task = (navigate_javascript_task_t*)_task;
1938 IUri_Release(task->uri);
1944 HTMLOuterWindow *window;
1945 nsChannelBSC *bscallback;
1949 static void navigate_proc(task_t *_task)
1951 navigate_task_t *task = (navigate_task_t*)_task;
1954 hres = set_moniker(&task->window->doc_obj->basedoc, task->mon, NULL, task->bscallback, TRUE);
1955 if(SUCCEEDED(hres)) {
1956 set_current_mon(task->window, task->bscallback->bsc.mon);
1957 start_binding(task->window->pending_window, &task->bscallback->bsc, NULL);
1961 static void navigate_task_destr(task_t *_task)
1963 navigate_task_t *task = (navigate_task_t*)_task;
1965 IBindStatusCallback_Release(&task->bscallback->bsc.IBindStatusCallback_iface);
1966 IMoniker_Release(task->mon);
1970 static HRESULT navigate_fragment(HTMLOuterWindow *window, IUri *uri)
1972 nsIDOMLocation *nslocation;
1973 nsAString nsfrag_str;
1979 const WCHAR selector_formatW[] = {'a','[','i','d','=','"','%','s','"',']',0};
1981 set_current_uri(window, uri);
1983 nsres = nsIDOMWindow_GetLocation(window->nswindow, &nslocation);
1984 if(FAILED(nsres) || !nslocation)
1987 hres = IUri_GetFragment(uri, &frag);
1989 nsIDOMLocation_Release(nslocation);
1993 nsAString_InitDepend(&nsfrag_str, frag);
1994 nsres = nsIDOMLocation_SetHash(nslocation, &nsfrag_str);
1995 nsAString_Finish(&nsfrag_str);
1996 nsIDOMLocation_Release(nslocation);
1997 if(NS_FAILED(nsres))
1998 ERR("SetHash failed: %08x\n", nsres);
2001 * IE supports scrolling to anchor elements with "#hash" ids (note that '#' is part of the id),
2002 * while Gecko scrolls only to elements with "hash" ids. We scroll the page ourselves if
2003 * a[id="#hash"] element can be found.
2005 selector = heap_alloc(sizeof(selector_formatW)+SysStringLen(frag)*sizeof(WCHAR));
2007 nsIDOMElement *nselem = NULL;
2008 nsAString selector_str;
2010 sprintfW(selector, selector_formatW, frag);
2011 nsAString_InitDepend(&selector_str, selector);
2012 /* NOTE: Gecko doesn't set result to NULL if there is no match, so nselem must be initialized */
2013 nsres = nsIDOMNodeSelector_QuerySelector(window->base.inner_window->doc->nsnode_selector, &selector_str, &nselem);
2014 nsAString_Finish(&selector_str);
2015 heap_free(selector);
2016 if(NS_SUCCEEDED(nsres) && nselem) {
2017 nsIDOMHTMLElement *html_elem;
2019 nsres = nsIDOMElement_QueryInterface(nselem, &IID_nsIDOMHTMLElement, (void**)&html_elem);
2020 nsIDOMElement_Release(nselem);
2021 if(NS_SUCCEEDED(nsres)) {
2022 nsIDOMHTMLElement_ScrollIntoView(html_elem, TRUE, 1);
2023 nsIDOMHTMLElement_Release(html_elem);
2028 SysFreeString(frag);
2030 if(window->doc_obj->doc_object_service) {
2031 IDocObjectService_FireNavigateComplete2(window->doc_obj->doc_object_service, &window->base.IHTMLWindow2_iface, 0x10);
2032 IDocObjectService_FireDocumentComplete(window->doc_obj->doc_object_service, &window->base.IHTMLWindow2_iface, 0);
2039 HRESULT super_navigate(HTMLOuterWindow *window, IUri *uri, const WCHAR *headers, BYTE *post_data, DWORD post_data_size)
2046 if(window->doc_obj->client) {
2047 IOleCommandTarget *cmdtrg;
2049 hres = IOleClientSite_QueryInterface(window->doc_obj->client, &IID_IOleCommandTarget, (void**)&cmdtrg);
2050 if(SUCCEEDED(hres)) {
2054 hres = IUri_GetDisplayUri(uri, &url_str);
2055 if(SUCCEEDED(hres)) {
2056 V_VT(&in) = VT_BSTR;
2057 V_BSTR(&in) = url_str;
2058 V_VT(&out) = VT_BOOL;
2059 V_BOOL(&out) = VARIANT_TRUE;
2060 hres = IOleCommandTarget_Exec(cmdtrg, &CGID_ShellDocView, 67, 0, &in, &out);
2061 IOleCommandTarget_Release(cmdtrg);
2064 SysFreeString(url_str);
2069 if(window->uri && !post_data_size && compare_ignoring_frag(window->uri, uri)) {
2070 TRACE("fragment navigate\n");
2071 return navigate_fragment(window, uri);
2074 hres = CreateURLMonikerEx2(NULL, uri, &mon, URL_MK_UNIFORM);
2078 /* FIXME: Why not set_ready_state? */
2079 window->readystate = READYSTATE_UNINITIALIZED;
2081 hres = create_channelbsc(mon, headers, post_data, post_data_size, TRUE, &bsc);
2083 IMoniker_Release(mon);
2087 prepare_for_binding(&window->doc_obj->basedoc, mon, TRUE);
2089 hres = IUri_GetScheme(uri, &scheme);
2090 if(SUCCEEDED(hres) && scheme != URL_SCHEME_JAVASCRIPT) {
2091 navigate_task_t *task;
2093 task = heap_alloc(sizeof(*task));
2095 IBindStatusCallback_Release(&bsc->bsc.IBindStatusCallback_iface);
2096 IMoniker_Release(mon);
2097 return E_OUTOFMEMORY;
2100 /* Silently and repeated when real loading starts? */
2101 window->readystate = READYSTATE_LOADING;
2102 call_docview_84(window->doc_obj);
2104 task->window = window;
2105 task->bscallback = bsc;
2107 push_task(&task->header, navigate_proc, navigate_task_destr, window->task_magic);
2110 navigate_javascript_task_t *task;
2112 IBindStatusCallback_Release(&bsc->bsc.IBindStatusCallback_iface);
2113 IMoniker_Release(mon);
2115 task = heap_alloc(sizeof(*task));
2117 return E_OUTOFMEMORY;
2120 window->readystate = READYSTATE_COMPLETE;
2121 call_docview_84(window->doc_obj);
2124 task->window = window;
2126 push_task(&task->header, navigate_javascript_proc, navigate_javascript_task_destr, window->task_magic);
2132 HRESULT navigate_new_window(HTMLOuterWindow *window, IUri *uri, const WCHAR *name, IHTMLWindow2 **ret)
2134 IWebBrowser2 *web_browser;
2135 IHTMLWindow2 *new_window;
2140 hres = create_channelbsc(NULL, NULL, NULL, 0, FALSE, &bsc);
2144 hres = CreateAsyncBindCtx(0, &bsc->bsc.IBindStatusCallback_iface, NULL, &bind_ctx);
2146 IBindStatusCallback_Release(&bsc->bsc.IBindStatusCallback_iface);
2150 hres = CoCreateInstance(&CLSID_InternetExplorer, NULL, CLSCTX_LOCAL_SERVER,
2151 &IID_IWebBrowser2, (void**)&web_browser);
2152 if(SUCCEEDED(hres)) {
2153 ITargetFramePriv2 *target_frame_priv;
2155 hres = IWebBrowser2_QueryInterface(web_browser, &IID_ITargetFramePriv2, (void**)&target_frame_priv);
2156 if(SUCCEEDED(hres)) {
2157 hres = ITargetFramePriv2_AggregatedNavigation2(target_frame_priv,
2158 HLNF_DISABLEWINDOWRESTRICTIONS|HLNF_OPENINNEWWINDOW, bind_ctx, &bsc->bsc.IBindStatusCallback_iface,
2160 ITargetFramePriv2_Release(target_frame_priv);
2163 hres = do_query_service((IUnknown*)web_browser, &SID_SHTMLWindow, &IID_IHTMLWindow2, (void**)&new_window);
2166 IWebBrowser2_Quit(web_browser);
2167 IWebBrowser2_Release(web_browser);
2170 WARN("Could not create InternetExplorer instance: %08x\n", hres);
2173 IBindStatusCallback_Release(&bsc->bsc.IBindStatusCallback_iface);
2174 IBindCtx_Release(bind_ctx);
2178 IWebBrowser2_put_Visible(web_browser, VARIANT_TRUE);
2179 IWebBrowser2_Release(web_browser);
2184 IHTMLWindow2_Release(new_window);
2188 HRESULT hlink_frame_navigate(HTMLDocument *doc, LPCWSTR url, nsChannel *nschannel, DWORD hlnf, BOOL *cancel)
2190 IHlinkFrame *hlink_frame;
2191 nsChannelBSC *callback;
2199 hres = do_query_service((IUnknown*)doc->doc_obj->client, &IID_IHlinkFrame, &IID_IHlinkFrame,
2200 (void**)&hlink_frame);
2204 hres = create_channelbsc(NULL, NULL, NULL, 0, FALSE, &callback);
2206 IHlinkFrame_Release(hlink_frame);
2211 read_post_data_stream(callback, nschannel);
2213 hres = CreateAsyncBindCtx(0, &callback->bsc.IBindStatusCallback_iface, NULL, &bindctx);
2215 hres = CoCreateInstance(&CLSID_StdHlink, NULL, CLSCTX_INPROC_SERVER,
2216 &IID_IHlink, (LPVOID*)&hlink);
2219 hres = CreateURLMoniker(NULL, url, &mon);
2221 if(SUCCEEDED(hres)) {
2222 IHlink_SetMonikerReference(hlink, HLINKSETF_TARGET, mon, NULL);
2224 if(hlnf & HLNF_OPENINNEWWINDOW) {
2225 static const WCHAR wszBlank[] = {'_','b','l','a','n','k',0};
2226 IHlink_SetTargetFrameName(hlink, wszBlank); /* FIXME */
2229 hres = IHlinkFrame_Navigate(hlink_frame, hlnf, bindctx,
2230 &callback->bsc.IBindStatusCallback_iface, hlink);
2231 IMoniker_Release(mon);
2232 *cancel = hres == S_OK;
2236 IHlinkFrame_Release(hlink_frame);
2237 IBindCtx_Release(bindctx);
2238 IBindStatusCallback_Release(&callback->bsc.IBindStatusCallback_iface);
2242 static HRESULT navigate_uri(HTMLOuterWindow *window, IUri *uri, const WCHAR *display_uri)
2247 if(window->doc_obj && window->doc_obj->is_webbrowser && window == window->doc_obj->basedoc.window) {
2248 BOOL cancel = FALSE;
2250 hres = IDocObjectService_FireBeforeNavigate2(window->doc_obj->doc_object_service, NULL, display_uri, 0x40,
2251 NULL, NULL, 0, NULL, TRUE, &cancel);
2252 if(SUCCEEDED(hres) && cancel) {
2253 TRACE("Navigation canceled\n");
2257 return super_navigate(window, uri, NULL, NULL, 0);
2260 if(window->doc_obj && window == window->doc_obj->basedoc.window) {
2263 hres = hlink_frame_navigate(&window->base.inner_window->doc->basedoc, display_uri, NULL, 0, &cancel);
2268 TRACE("Navigation handled by hlink frame\n");
2273 hres = create_doc_uri(window, display_uri, &nsuri);
2277 hres = load_nsuri(window, nsuri, NULL, LOAD_FLAGS_NONE);
2278 nsISupports_Release((nsISupports*)nsuri);
2282 HRESULT navigate_url(HTMLOuterWindow *window, const WCHAR *new_url, IUri *base_uri)
2288 if(new_url && base_uri)
2289 hres = CoInternetCombineUrlEx(base_uri, new_url, URL_ESCAPE_SPACES_ONLY|URL_DONT_ESCAPE_EXTRA_INFO,
2292 hres = CreateUri(new_url, 0, 0, &uri);
2296 hres = IUri_GetDisplayUri(uri, &display_uri);
2302 if(window->doc_obj && window->doc_obj->hostui) {
2303 OLECHAR *translated_url = NULL;
2305 hres = IDocHostUIHandler_TranslateUrl(window->doc_obj->hostui, 0, display_uri,
2308 TRACE("%08x %s -> %s\n", hres, debugstr_w(display_uri), debugstr_w(translated_url));
2309 SysFreeString(display_uri);
2311 hres = CreateUri(translated_url, 0, 0, &uri);
2312 CoTaskMemFree(translated_url);
2316 hres = IUri_GetDisplayUri(uri, &display_uri);
2324 hres = navigate_uri(window, uri, display_uri);
2327 SysFreeString(display_uri);