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 "wine/debug.h"
38 #include "wine/unicode.h"
40 #include "mshtml_private.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
45 #define CONTENT_LENGTH "Content-Length"
46 #define UTF16_STR "utf-16"
48 struct nsProtocolStream {
49 nsIInputStream nsIInputStream_iface;
57 struct BSCallbackVtbl {
58 void (*destroy)(BSCallback*);
59 HRESULT (*init_bindinfo)(BSCallback*);
60 HRESULT (*start_binding)(BSCallback*);
61 HRESULT (*stop_binding)(BSCallback*,HRESULT);
62 HRESULT (*read_data)(BSCallback*,IStream*);
63 HRESULT (*on_progress)(BSCallback*,ULONG,LPCWSTR);
64 HRESULT (*on_response)(BSCallback*,DWORD,LPCWSTR);
65 HRESULT (*beginning_transaction)(BSCallback*,WCHAR**);
68 static inline nsProtocolStream *impl_from_nsIInputStream(nsIInputStream *iface)
70 return CONTAINING_RECORD(iface, nsProtocolStream, nsIInputStream_iface);
73 static nsresult NSAPI nsInputStream_QueryInterface(nsIInputStream *iface, nsIIDRef riid,
76 nsProtocolStream *This = impl_from_nsIInputStream(iface);
80 if(IsEqualGUID(&IID_nsISupports, riid)) {
81 TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
82 *result = &This->nsIInputStream_iface;
83 }else if(IsEqualGUID(&IID_nsIInputStream, riid)) {
84 TRACE("(%p)->(IID_nsIInputStream %p)\n", This, result);
85 *result = &This->nsIInputStream_iface;
89 nsIInputStream_AddRef(&This->nsIInputStream_iface);
93 WARN("unsupported interface %s\n", debugstr_guid(riid));
94 return NS_NOINTERFACE;
97 static nsrefcnt NSAPI nsInputStream_AddRef(nsIInputStream *iface)
99 nsProtocolStream *This = impl_from_nsIInputStream(iface);
100 LONG ref = InterlockedIncrement(&This->ref);
102 TRACE("(%p) ref=%d\n", This, ref);
108 static nsrefcnt NSAPI nsInputStream_Release(nsIInputStream *iface)
110 nsProtocolStream *This = impl_from_nsIInputStream(iface);
111 LONG ref = InterlockedDecrement(&This->ref);
113 TRACE("(%p) ref=%d\n", This, ref);
121 static nsresult NSAPI nsInputStream_Close(nsIInputStream *iface)
123 nsProtocolStream *This = impl_from_nsIInputStream(iface);
124 FIXME("(%p)\n", This);
125 return NS_ERROR_NOT_IMPLEMENTED;
128 static nsresult NSAPI nsInputStream_Available(nsIInputStream *iface, PRUint32 *_retval)
130 nsProtocolStream *This = impl_from_nsIInputStream(iface);
131 FIXME("(%p)->(%p)\n", This, _retval);
132 return NS_ERROR_NOT_IMPLEMENTED;
135 static nsresult NSAPI nsInputStream_Read(nsIInputStream *iface, char *aBuf, PRUint32 aCount,
138 nsProtocolStream *This = impl_from_nsIInputStream(iface);
141 TRACE("(%p)->(%p %d %p)\n", This, aBuf, aCount, _retval);
143 if(read > This->buf_size)
144 read = This->buf_size;
147 memcpy(aBuf, This->buf, read);
148 if(read < This->buf_size)
149 memmove(This->buf, This->buf+read, This->buf_size-read);
150 This->buf_size -= read;
157 static nsresult NSAPI nsInputStream_ReadSegments(nsIInputStream *iface,
158 nsresult (WINAPI *aWriter)(nsIInputStream*,void*,const char*,PRUint32,PRUint32,PRUint32*),
159 void *aClousure, PRUint32 aCount, PRUint32 *_retval)
161 nsProtocolStream *This = impl_from_nsIInputStream(iface);
162 PRUint32 written = 0;
165 TRACE("(%p)->(%p %p %d %p)\n", This, aWriter, aClousure, aCount, _retval);
170 if(aCount > This->buf_size)
171 aCount = This->buf_size;
173 nsres = aWriter(&This->nsIInputStream_iface, aClousure, This->buf, 0, aCount, &written);
175 TRACE("aWritter failed: %08x\n", nsres);
176 else if(written != This->buf_size)
177 FIXME("written %d != buf_size %d\n", written, This->buf_size);
179 This->buf_size -= written;
185 static nsresult NSAPI nsInputStream_IsNonBlocking(nsIInputStream *iface, PRBool *_retval)
187 nsProtocolStream *This = impl_from_nsIInputStream(iface);
188 FIXME("(%p)->(%p)\n", This, _retval);
189 return NS_ERROR_NOT_IMPLEMENTED;
192 static const nsIInputStreamVtbl nsInputStreamVtbl = {
193 nsInputStream_QueryInterface,
194 nsInputStream_AddRef,
195 nsInputStream_Release,
197 nsInputStream_Available,
199 nsInputStream_ReadSegments,
200 nsInputStream_IsNonBlocking
203 static nsProtocolStream *create_nsprotocol_stream(void)
205 nsProtocolStream *ret = heap_alloc(sizeof(nsProtocolStream));
207 ret->nsIInputStream_iface.lpVtbl = &nsInputStreamVtbl;
214 static inline BSCallback *impl_from_IBindStatusCallback(IBindStatusCallback *iface)
216 return CONTAINING_RECORD(iface, BSCallback, IBindStatusCallback_iface);
219 static HRESULT WINAPI BindStatusCallback_QueryInterface(IBindStatusCallback *iface,
220 REFIID riid, void **ppv)
222 BSCallback *This = impl_from_IBindStatusCallback(iface);
225 if(IsEqualGUID(&IID_IUnknown, riid)) {
226 TRACE("(%p)->(IID_IUnknown, %p)\n", This, ppv);
227 *ppv = &This->IBindStatusCallback_iface;
228 }else if(IsEqualGUID(&IID_IBindStatusCallback, riid)) {
229 TRACE("(%p)->(IID_IBindStatusCallback, %p)\n", This, ppv);
230 *ppv = &This->IBindStatusCallback_iface;
231 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
232 TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
233 *ppv = &This->IServiceProvider_iface;
234 }else if(IsEqualGUID(&IID_IHttpNegotiate, riid)) {
235 TRACE("(%p)->(IID_IHttpNegotiate %p)\n", This, ppv);
236 *ppv = &This->IHttpNegotiate2_iface;
237 }else if(IsEqualGUID(&IID_IHttpNegotiate2, riid)) {
238 TRACE("(%p)->(IID_IHttpNegotiate2 %p)\n", This, ppv);
239 *ppv = &This->IHttpNegotiate2_iface;
240 }else if(IsEqualGUID(&IID_IInternetBindInfo, riid)) {
241 TRACE("(%p)->(IID_IInternetBindInfo %p)\n", This, ppv);
242 *ppv = &This->IInternetBindInfo_iface;
246 IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
250 TRACE("Unsupported riid = %s\n", debugstr_guid(riid));
251 return E_NOINTERFACE;
254 static ULONG WINAPI BindStatusCallback_AddRef(IBindStatusCallback *iface)
256 BSCallback *This = impl_from_IBindStatusCallback(iface);
257 LONG ref = InterlockedIncrement(&This->ref);
259 TRACE("(%p) ref = %d\n", This, ref);
264 static ULONG WINAPI BindStatusCallback_Release(IBindStatusCallback *iface)
266 BSCallback *This = impl_from_IBindStatusCallback(iface);
267 LONG ref = InterlockedDecrement(&This->ref);
269 TRACE("(%p) ref = %d\n", This, ref);
273 GlobalFree(This->post_data);
275 IMoniker_Release(This->mon);
277 IBinding_Release(This->binding);
278 list_remove(&This->entry);
279 list_init(&This->entry);
280 heap_free(This->headers);
282 This->vtbl->destroy(This);
288 static HRESULT WINAPI BindStatusCallback_OnStartBinding(IBindStatusCallback *iface,
289 DWORD dwReserved, IBinding *pbind)
291 BSCallback *This = impl_from_IBindStatusCallback(iface);
293 TRACE("(%p)->(%d %p)\n", This, dwReserved, pbind);
295 IBinding_AddRef(pbind);
296 This->binding = pbind;
299 list_add_head(&This->doc->bindings, &This->entry);
301 return This->vtbl->start_binding(This);
304 static HRESULT WINAPI BindStatusCallback_GetPriority(IBindStatusCallback *iface, LONG *pnPriority)
306 BSCallback *This = impl_from_IBindStatusCallback(iface);
307 FIXME("(%p)->(%p)\n", This, pnPriority);
311 static HRESULT WINAPI BindStatusCallback_OnLowResource(IBindStatusCallback *iface, DWORD reserved)
313 BSCallback *This = impl_from_IBindStatusCallback(iface);
314 FIXME("(%p)->(%d)\n", This, reserved);
318 static HRESULT WINAPI BindStatusCallback_OnProgress(IBindStatusCallback *iface, ULONG ulProgress,
319 ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
321 BSCallback *This = impl_from_IBindStatusCallback(iface);
323 TRACE("%p)->(%u %u %u %s)\n", This, ulProgress, ulProgressMax, ulStatusCode,
324 debugstr_w(szStatusText));
326 return This->vtbl->on_progress(This, ulStatusCode, szStatusText);
329 static HRESULT WINAPI BindStatusCallback_OnStopBinding(IBindStatusCallback *iface,
330 HRESULT hresult, LPCWSTR szError)
332 BSCallback *This = impl_from_IBindStatusCallback(iface);
335 TRACE("(%p)->(%08x %s)\n", This, hresult, debugstr_w(szError));
337 /* NOTE: IE7 calls GetBindResult here */
339 hres = This->vtbl->stop_binding(This, hresult);
342 IBinding_Release(This->binding);
343 This->binding = NULL;
346 list_remove(&This->entry);
347 list_init(&This->entry);
353 static HRESULT WINAPI BindStatusCallback_GetBindInfo(IBindStatusCallback *iface,
354 DWORD *grfBINDF, BINDINFO *pbindinfo)
356 BSCallback *This = impl_from_IBindStatusCallback(iface);
359 TRACE("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
361 if(!This->bindinfo_ready) {
364 hres = This->vtbl->init_bindinfo(This);
368 This->bindinfo_ready = TRUE;
371 *grfBINDF = This->bindf;
373 size = pbindinfo->cbSize;
374 memset(pbindinfo, 0, size);
375 pbindinfo->cbSize = size;
377 pbindinfo->cbstgmedData = This->post_data_len;
378 pbindinfo->dwCodePage = CP_UTF8;
379 pbindinfo->dwOptions = 0x80000;
381 if(This->post_data) {
382 pbindinfo->dwBindVerb = BINDVERB_POST;
384 pbindinfo->stgmedData.tymed = TYMED_HGLOBAL;
385 pbindinfo->stgmedData.u.hGlobal = This->post_data;
386 pbindinfo->stgmedData.pUnkForRelease = (IUnknown*)&This->IBindStatusCallback_iface;
387 IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
393 static HRESULT WINAPI BindStatusCallback_OnDataAvailable(IBindStatusCallback *iface,
394 DWORD grfBSCF, DWORD dwSize, FORMATETC *pformatetc, STGMEDIUM *pstgmed)
396 BSCallback *This = impl_from_IBindStatusCallback(iface);
398 TRACE("(%p)->(%08x %d %p %p)\n", This, grfBSCF, dwSize, pformatetc, pstgmed);
400 return This->vtbl->read_data(This, pstgmed->u.pstm);
403 static HRESULT WINAPI BindStatusCallback_OnObjectAvailable(IBindStatusCallback *iface,
404 REFIID riid, IUnknown *punk)
406 BSCallback *This = impl_from_IBindStatusCallback(iface);
407 FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), punk);
411 static const IBindStatusCallbackVtbl BindStatusCallbackVtbl = {
412 BindStatusCallback_QueryInterface,
413 BindStatusCallback_AddRef,
414 BindStatusCallback_Release,
415 BindStatusCallback_OnStartBinding,
416 BindStatusCallback_GetPriority,
417 BindStatusCallback_OnLowResource,
418 BindStatusCallback_OnProgress,
419 BindStatusCallback_OnStopBinding,
420 BindStatusCallback_GetBindInfo,
421 BindStatusCallback_OnDataAvailable,
422 BindStatusCallback_OnObjectAvailable
425 static inline BSCallback *impl_from_IHttpNegotiate2(IHttpNegotiate2 *iface)
427 return CONTAINING_RECORD(iface, BSCallback, IHttpNegotiate2_iface);
430 static HRESULT WINAPI HttpNegotiate_QueryInterface(IHttpNegotiate2 *iface,
431 REFIID riid, void **ppv)
433 BSCallback *This = impl_from_IHttpNegotiate2(iface);
434 return IBindStatusCallback_QueryInterface(&This->IBindStatusCallback_iface, riid, ppv);
437 static ULONG WINAPI HttpNegotiate_AddRef(IHttpNegotiate2 *iface)
439 BSCallback *This = impl_from_IHttpNegotiate2(iface);
440 return IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
443 static ULONG WINAPI HttpNegotiate_Release(IHttpNegotiate2 *iface)
445 BSCallback *This = impl_from_IHttpNegotiate2(iface);
446 return IBindStatusCallback_Release(&This->IBindStatusCallback_iface);
449 static HRESULT WINAPI HttpNegotiate_BeginningTransaction(IHttpNegotiate2 *iface,
450 LPCWSTR szURL, LPCWSTR szHeaders, DWORD dwReserved, LPWSTR *pszAdditionalHeaders)
452 BSCallback *This = impl_from_IHttpNegotiate2(iface);
455 TRACE("(%p)->(%s %s %d %p)\n", This, debugstr_w(szURL), debugstr_w(szHeaders),
456 dwReserved, pszAdditionalHeaders);
458 *pszAdditionalHeaders = NULL;
460 hres = This->vtbl->beginning_transaction(This, pszAdditionalHeaders);
467 size = (strlenW(This->headers)+1)*sizeof(WCHAR);
468 *pszAdditionalHeaders = CoTaskMemAlloc(size);
469 if(!*pszAdditionalHeaders)
470 return E_OUTOFMEMORY;
471 memcpy(*pszAdditionalHeaders, This->headers, size);
477 static HRESULT WINAPI HttpNegotiate_OnResponse(IHttpNegotiate2 *iface, DWORD dwResponseCode,
478 LPCWSTR szResponseHeaders, LPCWSTR szRequestHeaders, LPWSTR *pszAdditionalRequestHeaders)
480 BSCallback *This = impl_from_IHttpNegotiate2(iface);
482 TRACE("(%p)->(%d %s %s %p)\n", This, dwResponseCode, debugstr_w(szResponseHeaders),
483 debugstr_w(szRequestHeaders), pszAdditionalRequestHeaders);
485 return This->vtbl->on_response(This, dwResponseCode, szResponseHeaders);
488 static HRESULT WINAPI HttpNegotiate_GetRootSecurityId(IHttpNegotiate2 *iface,
489 BYTE *pbSecurityId, DWORD *pcbSecurityId, DWORD_PTR dwReserved)
491 BSCallback *This = impl_from_IHttpNegotiate2(iface);
492 FIXME("(%p)->(%p %p %ld)\n", This, pbSecurityId, pcbSecurityId, dwReserved);
496 static const IHttpNegotiate2Vtbl HttpNegotiate2Vtbl = {
497 HttpNegotiate_QueryInterface,
498 HttpNegotiate_AddRef,
499 HttpNegotiate_Release,
500 HttpNegotiate_BeginningTransaction,
501 HttpNegotiate_OnResponse,
502 HttpNegotiate_GetRootSecurityId
505 static inline BSCallback *impl_from_IInternetBindInfo(IInternetBindInfo *iface)
507 return CONTAINING_RECORD(iface, BSCallback, IInternetBindInfo_iface);
510 static HRESULT WINAPI InternetBindInfo_QueryInterface(IInternetBindInfo *iface,
511 REFIID riid, void **ppv)
513 BSCallback *This = impl_from_IInternetBindInfo(iface);
514 return IBindStatusCallback_QueryInterface(&This->IBindStatusCallback_iface, riid, ppv);
517 static ULONG WINAPI InternetBindInfo_AddRef(IInternetBindInfo *iface)
519 BSCallback *This = impl_from_IInternetBindInfo(iface);
520 return IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
523 static ULONG WINAPI InternetBindInfo_Release(IInternetBindInfo *iface)
525 BSCallback *This = impl_from_IInternetBindInfo(iface);
526 return IBindStatusCallback_Release(&This->IBindStatusCallback_iface);
529 static HRESULT WINAPI InternetBindInfo_GetBindInfo(IInternetBindInfo *iface,
530 DWORD *grfBINDF, BINDINFO *pbindinfo)
532 BSCallback *This = impl_from_IInternetBindInfo(iface);
533 FIXME("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
537 static HRESULT WINAPI InternetBindInfo_GetBindString(IInternetBindInfo *iface,
538 ULONG ulStringType, LPOLESTR *ppwzStr, ULONG cEl, ULONG *pcElFetched)
540 BSCallback *This = impl_from_IInternetBindInfo(iface);
541 FIXME("(%p)->(%u %p %u %p)\n", This, ulStringType, ppwzStr, cEl, pcElFetched);
545 static const IInternetBindInfoVtbl InternetBindInfoVtbl = {
546 InternetBindInfo_QueryInterface,
547 InternetBindInfo_AddRef,
548 InternetBindInfo_Release,
549 InternetBindInfo_GetBindInfo,
550 InternetBindInfo_GetBindString
553 static inline BSCallback *impl_from_IServiceProvider(IServiceProvider *iface)
555 return CONTAINING_RECORD(iface, BSCallback, IServiceProvider_iface);
558 static HRESULT WINAPI BSCServiceProvider_QueryInterface(IServiceProvider *iface,
559 REFIID riid, void **ppv)
561 BSCallback *This = impl_from_IServiceProvider(iface);
562 return IBindStatusCallback_QueryInterface(&This->IBindStatusCallback_iface, riid, ppv);
565 static ULONG WINAPI BSCServiceProvider_AddRef(IServiceProvider *iface)
567 BSCallback *This = impl_from_IServiceProvider(iface);
568 return IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
571 static ULONG WINAPI BSCServiceProvider_Release(IServiceProvider *iface)
573 BSCallback *This = impl_from_IServiceProvider(iface);
574 return IBindStatusCallback_Release(&This->IBindStatusCallback_iface);
577 static HRESULT WINAPI BSCServiceProvider_QueryService(IServiceProvider *iface,
578 REFGUID guidService, REFIID riid, void **ppv)
580 BSCallback *This = impl_from_IServiceProvider(iface);
581 TRACE("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
582 return E_NOINTERFACE;
585 static const IServiceProviderVtbl ServiceProviderVtbl = {
586 BSCServiceProvider_QueryInterface,
587 BSCServiceProvider_AddRef,
588 BSCServiceProvider_Release,
589 BSCServiceProvider_QueryService
592 static void init_bscallback(BSCallback *This, const BSCallbackVtbl *vtbl, IMoniker *mon, DWORD bindf)
594 This->IBindStatusCallback_iface.lpVtbl = &BindStatusCallbackVtbl;
595 This->IServiceProvider_iface.lpVtbl = &ServiceProviderVtbl;
596 This->IHttpNegotiate2_iface.lpVtbl = &HttpNegotiate2Vtbl;
597 This->IInternetBindInfo_iface.lpVtbl = &InternetBindInfoVtbl;
602 list_init(&This->entry);
605 IMoniker_AddRef(mon);
609 /* Calls undocumented 84 cmd of CGID_ShellDocView */
610 static void call_docview_84(HTMLDocumentObj *doc)
612 IOleCommandTarget *olecmd;
619 hres = IOleClientSite_QueryInterface(doc->client, &IID_IOleCommandTarget, (void**)&olecmd);
624 hres = IOleCommandTarget_Exec(olecmd, &CGID_ShellDocView, 84, 0, NULL, &var);
625 IOleCommandTarget_Release(olecmd);
626 if(SUCCEEDED(hres) && V_VT(&var) != VT_NULL)
627 FIXME("handle result\n");
630 static HRESULT parse_headers(const WCHAR *headers, struct list *headers_list)
632 const WCHAR *header, *header_end, *colon, *value;
637 if(header[0] == '\r' && header[1] == '\n' && !header[2])
639 for(colon = header; *colon && *colon != ':' && *colon != '\r'; colon++);
649 for(header_end = value+1; *header_end && *header_end != '\r'; header_end++);
651 hres = set_http_header(headers_list, header, colon-header, value, header_end-value);
656 if(header[0] == '\r' && header[1] == '\n')
663 HRESULT start_binding(HTMLWindow *window, HTMLDocumentNode *doc, BSCallback *bscallback, IBindCtx *bctx)
668 TRACE("(%p %p %p %p)\n", window, doc, bscallback, bctx);
670 bscallback->doc = doc;
672 /* NOTE: IE7 calls IsSystemMoniker here*/
675 if(bscallback->mon != window->mon)
676 set_current_mon(window, bscallback->mon);
677 call_docview_84(window->doc_obj);
681 RegisterBindStatusCallback(bctx, &bscallback->IBindStatusCallback_iface, NULL, 0);
682 IBindCtx_AddRef(bctx);
684 hres = CreateAsyncBindCtx(0, &bscallback->IBindStatusCallback_iface, NULL, &bctx);
686 WARN("CreateAsyncBindCtx failed: %08x\n", hres);
687 bscallback->vtbl->stop_binding(bscallback, hres);
692 hres = IMoniker_BindToStorage(bscallback->mon, bctx, NULL, &IID_IStream, (void**)&str);
693 IBindCtx_Release(bctx);
695 WARN("BindToStorage failed: %08x\n", hres);
696 bscallback->vtbl->stop_binding(bscallback, hres);
701 IStream_Release(str);
703 IMoniker_Release(bscallback->mon);
704 bscallback->mon = NULL;
717 static inline BufferBSC *BufferBSC_from_BSCallback(BSCallback *iface)
719 return CONTAINING_RECORD(iface, BufferBSC, bsc);
722 static void BufferBSC_destroy(BSCallback *bsc)
724 BufferBSC *This = BufferBSC_from_BSCallback(bsc);
726 heap_free(This->buf);
730 static HRESULT BufferBSC_init_bindinfo(BSCallback *bsc)
735 static HRESULT BufferBSC_start_binding(BSCallback *bsc)
740 static HRESULT BufferBSC_stop_binding(BSCallback *bsc, HRESULT result)
742 BufferBSC *This = BufferBSC_from_BSCallback(bsc);
747 heap_free(This->buf);
755 static HRESULT BufferBSC_read_data(BSCallback *bsc, IStream *stream)
757 BufferBSC *This = BufferBSC_from_BSCallback(bsc);
763 This->buf = heap_alloc(This->size);
767 if(This->bsc.readed == This->size) {
769 This->buf = heap_realloc(This->buf, This->size);
773 hres = IStream_Read(stream, This->buf+This->bsc.readed, This->size-This->bsc.readed, &readed);
774 This->bsc.readed += readed;
775 }while(hres == S_OK);
780 static HRESULT BufferBSC_on_progress(BSCallback *bsc, ULONG status_code, LPCWSTR status_text)
785 static HRESULT BufferBSC_on_response(BSCallback *bsc, DWORD response_code,
786 LPCWSTR response_headers)
791 static HRESULT BufferBSC_beginning_transaction(BSCallback *bsc, WCHAR **additional_headers)
796 static const BSCallbackVtbl BufferBSCVtbl = {
798 BufferBSC_init_bindinfo,
799 BufferBSC_start_binding,
800 BufferBSC_stop_binding,
802 BufferBSC_on_progress,
803 BufferBSC_on_response,
804 BufferBSC_beginning_transaction
808 static BufferBSC *create_bufferbsc(IMoniker *mon)
810 BufferBSC *ret = heap_alloc_zero(sizeof(*ret));
812 init_bscallback(&ret->bsc, &BufferBSCVtbl, mon, 0);
818 HRESULT bind_mon_to_buffer(HTMLDocumentNode *doc, IMoniker *mon, void **buf, DWORD *size)
820 BufferBSC *bsc = create_bufferbsc(mon);
825 hres = start_binding(NULL, doc, &bsc->bsc, NULL);
826 if(SUCCEEDED(hres)) {
828 if(SUCCEEDED(hres)) {
831 *size = bsc->bsc.readed;
836 IBindStatusCallback_Release(&bsc->bsc.IBindStatusCallback_iface);
841 static HRESULT read_post_data_stream(nsChannelBSC *This, nsChannel *nschannel)
843 PRUint32 data_len = 0, available = 0;
844 char *data, *post_data;
848 if(!nschannel->post_data_stream)
851 nsres = nsIInputStream_Available(nschannel->post_data_stream, &available);
855 post_data = data = GlobalAlloc(0, available);
857 return E_OUTOFMEMORY;
859 nsres = nsIInputStream_Read(nschannel->post_data_stream, data, available, &data_len);
860 if(NS_FAILED(nsres)) {
865 if(nschannel->post_data_contains_headers) {
866 if(data_len >= 2 && data[0] == '\r' && data[1] == '\n') {
874 post_data += data_len;
875 for(ptr = data; ptr+4 < data+data_len; ptr++) {
876 if(!memcmp(ptr, "\r\n\r\n", 4)) {
882 data_len -= post_data-data;
884 size = MultiByteToWideChar(CP_ACP, 0, data, post_data-data, NULL, 0);
885 headers = heap_alloc((size+1)*sizeof(WCHAR));
887 MultiByteToWideChar(CP_ACP, 0, data, post_data-data, headers, size);
889 hres = parse_headers(headers , &nschannel->request_headers);
891 This->bsc.headers = headers;
895 hres = E_OUTOFMEMORY;
908 }else if(post_data != data) {
911 new_data = GlobalAlloc(0, data_len);
913 memcpy(new_data, post_data, data_len);
916 return E_OUTOFMEMORY;
917 post_data = new_data;
920 This->bsc.post_data = post_data;
921 This->bsc.post_data_len = data_len;
922 TRACE("post_data = %s\n", debugstr_a(This->bsc.post_data));
926 static HRESULT on_start_nsrequest(nsChannelBSC *This)
930 /* FIXME: it's needed for http connections from BindToObject. */
931 if(!This->nschannel->response_status)
932 This->nschannel->response_status = 200;
934 nsres = nsIStreamListener_OnStartRequest(This->nslistener,
935 (nsIRequest*)&This->nschannel->nsIHttpChannel_iface, This->nscontext);
936 if(NS_FAILED(nsres)) {
937 FIXME("OnStartRequest failed: %08x\n", nsres);
942 list_remove(&This->bsc.entry);
943 list_init(&This->bsc.entry);
944 update_window_doc(This->window);
945 if(This->window->doc != This->bsc.doc)
946 This->bsc.doc = This->window->doc;
947 list_add_head(&This->bsc.doc->bindings, &This->bsc.entry);
948 if(This->window->readystate != READYSTATE_LOADING)
949 set_ready_state(This->window, READYSTATE_LOADING);
955 static void on_stop_nsrequest(nsChannelBSC *This, HRESULT result)
957 nsresult nsres, request_result;
961 request_result = NS_OK;
964 request_result = NS_BINDING_ABORTED;
967 request_result = NS_ERROR_FAILURE;
970 if(This->nslistener) {
971 nsres = nsIStreamListener_OnStopRequest(This->nslistener,
972 (nsIRequest*)&This->nschannel->nsIHttpChannel_iface, This->nscontext,
975 WARN("OnStopRequet failed: %08x\n", nsres);
978 if(This->nschannel->load_group) {
979 nsres = nsILoadGroup_RemoveRequest(This->nschannel->load_group,
980 (nsIRequest*)&This->nschannel->nsIHttpChannel_iface, NULL, request_result);
982 ERR("RemoveRequest failed: %08x\n", nsres);
986 static HRESULT read_stream_data(nsChannelBSC *This, IStream *stream)
992 if(!This->nslistener) {
997 hres = IStream_Read(stream, buf, sizeof(buf), &read);
998 }while(hres == S_OK && read);
1004 This->nsstream = create_nsprotocol_stream();
1008 hres = IStream_Read(stream, This->nsstream->buf+This->nsstream->buf_size,
1009 sizeof(This->nsstream->buf)-This->nsstream->buf_size, &read);
1013 This->nsstream->buf_size += read;
1015 if(!This->bsc.readed) {
1016 if(This->nsstream->buf_size >= 2
1017 && (BYTE)This->nsstream->buf[0] == 0xff
1018 && (BYTE)This->nsstream->buf[1] == 0xfe)
1019 This->nschannel->charset = heap_strdupA(UTF16_STR);
1021 if(!This->nschannel->content_type) {
1024 hres = FindMimeFromData(NULL, NULL, This->nsstream->buf, This->nsstream->buf_size, NULL, 0, &mime, 0);
1028 TRACE("Found MIME %s\n", debugstr_w(mime));
1030 This->nschannel->content_type = heap_strdupWtoA(mime);
1031 CoTaskMemFree(mime);
1032 if(!This->nschannel->content_type)
1033 return E_OUTOFMEMORY;
1036 on_start_nsrequest(This);
1039 This->bsc.readed += This->nsstream->buf_size;
1041 nsres = nsIStreamListener_OnDataAvailable(This->nslistener,
1042 (nsIRequest*)&This->nschannel->nsIHttpChannel_iface, This->nscontext,
1043 &This->nsstream->nsIInputStream_iface, This->bsc.readed-This->nsstream->buf_size,
1044 This->nsstream->buf_size);
1045 if(NS_FAILED(nsres))
1046 ERR("OnDataAvailable failed: %08x\n", nsres);
1048 if(This->nsstream->buf_size == sizeof(This->nsstream->buf)) {
1049 ERR("buffer is full\n");
1052 }while(hres == S_OK);
1058 nsIAsyncVerifyRedirectCallback nsIAsyncVerifyRedirectCallback_iface;
1062 nsChannel *nschannel;
1064 } nsRedirectCallback;
1066 static nsRedirectCallback *impl_from_nsIAsyncVerifyRedirectCallback(nsIAsyncVerifyRedirectCallback *iface)
1068 return CONTAINING_RECORD(iface, nsRedirectCallback, nsIAsyncVerifyRedirectCallback_iface);
1071 static nsresult NSAPI nsAsyncVerifyRedirectCallback_QueryInterface(nsIAsyncVerifyRedirectCallback *iface,
1072 nsIIDRef riid, void **result)
1074 nsRedirectCallback *This = impl_from_nsIAsyncVerifyRedirectCallback(iface);
1076 if(IsEqualGUID(&IID_nsISupports, riid)) {
1077 TRACE("(%p)->(IID_nsISuports %p)\n", This, result);
1078 *result = &This->nsIAsyncVerifyRedirectCallback_iface;
1079 }else if(IsEqualGUID(&IID_nsIAsyncVerifyRedirectCallback, riid)) {
1080 TRACE("(%p)->(IID_nsIAsyncVerifyRedirectCallback %p)\n", This, result);
1081 *result = &This->nsIAsyncVerifyRedirectCallback_iface;
1084 WARN("unimplemented iface %s\n", debugstr_guid(riid));
1085 return NS_NOINTERFACE;
1088 nsISupports_AddRef((nsISupports*)*result);
1092 static nsrefcnt NSAPI nsAsyncVerifyRedirectCallback_AddRef(nsIAsyncVerifyRedirectCallback *iface)
1094 nsRedirectCallback *This = impl_from_nsIAsyncVerifyRedirectCallback(iface);
1095 LONG ref = InterlockedIncrement(&This->ref);
1097 TRACE("(%p) ref=%d\n", This, ref);
1102 static nsrefcnt NSAPI nsAsyncVerifyRedirectCallback_Release(nsIAsyncVerifyRedirectCallback *iface)
1104 nsRedirectCallback *This = impl_from_nsIAsyncVerifyRedirectCallback(iface);
1105 LONG ref = InterlockedDecrement(&This->ref);
1107 TRACE("(%p) ref=%d\n", This, ref);
1110 IBindStatusCallback_Release(&This->bsc->bsc.IBindStatusCallback_iface);
1111 nsIChannel_Release(&This->nschannel->nsIHttpChannel_iface);
1118 static nsresult NSAPI nsAsyncVerifyRedirectCallback_AsyncOnChannelRedirect(nsIAsyncVerifyRedirectCallback *iface, nsresult result)
1120 nsRedirectCallback *This = impl_from_nsIAsyncVerifyRedirectCallback(iface);
1122 TRACE("(%p)->(%08x)\n", This, result);
1124 if(This->bsc->nschannel)
1125 nsIHttpChannel_Release(&This->bsc->nschannel->nsIHttpChannel_iface);
1126 nsIChannel_AddRef(&This->nschannel->nsIHttpChannel_iface);
1127 This->bsc->nschannel = This->nschannel;
1129 if(This->nschannel->load_group) {
1132 nsres = nsILoadGroup_AddRequest(This->nschannel->load_group, (nsIRequest*)&This->nschannel->nsIHttpChannel_iface,
1134 if(NS_FAILED(nsres))
1135 ERR("AddRequest failed: %08x\n", nsres);
1141 static const nsIAsyncVerifyRedirectCallbackVtbl nsAsyncVerifyRedirectCallbackVtbl = {
1142 nsAsyncVerifyRedirectCallback_QueryInterface,
1143 nsAsyncVerifyRedirectCallback_AddRef,
1144 nsAsyncVerifyRedirectCallback_Release,
1145 nsAsyncVerifyRedirectCallback_AsyncOnChannelRedirect
1148 static HRESULT create_redirect_callback(nsChannel *nschannel, nsChannelBSC *bsc, nsRedirectCallback **ret)
1150 nsRedirectCallback *callback;
1152 callback = heap_alloc(sizeof(*callback));
1154 return E_OUTOFMEMORY;
1156 callback->nsIAsyncVerifyRedirectCallback_iface.lpVtbl = &nsAsyncVerifyRedirectCallbackVtbl;
1159 nsIChannel_AddRef(&nschannel->nsIHttpChannel_iface);
1160 callback->nschannel = nschannel;
1162 IBindStatusCallback_AddRef(&bsc->bsc.IBindStatusCallback_iface);
1163 callback->bsc = bsc;
1169 static inline nsChannelBSC *nsChannelBSC_from_BSCallback(BSCallback *iface)
1171 return CONTAINING_RECORD(iface, nsChannelBSC, bsc);
1174 static void nsChannelBSC_destroy(BSCallback *bsc)
1176 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1179 nsIChannel_Release(&This->nschannel->nsIHttpChannel_iface);
1180 if(This->nslistener)
1181 nsIStreamListener_Release(This->nslistener);
1183 nsISupports_Release(This->nscontext);
1185 nsIInputStream_Release(&This->nsstream->nsIInputStream_iface);
1189 static HRESULT nsChannelBSC_start_binding(BSCallback *bsc)
1191 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1194 This->window->doc->skip_mutation_notif = FALSE;
1199 static HRESULT nsChannelBSC_init_bindinfo(BSCallback *bsc)
1201 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1204 if(This->nschannel && This->nschannel->post_data_stream) {
1205 hres = read_post_data_stream(This, This->nschannel);
1216 } stop_request_task_t;
1218 static void stop_request_proc(task_t *_task)
1220 stop_request_task_t *task = (stop_request_task_t*)_task;
1222 TRACE("(%p)\n", task->bsc);
1224 list_remove(&task->bsc->bsc.entry);
1225 list_init(&task->bsc->bsc.entry);
1226 on_stop_nsrequest(task->bsc, S_OK);
1227 IBindStatusCallback_Release(&task->bsc->bsc.IBindStatusCallback_iface);
1230 static HRESULT async_stop_request(nsChannelBSC *This)
1232 stop_request_task_t *task;
1234 if(!This->bsc.readed) {
1235 TRACE("No data read, calling OnStartRequest\n");
1236 on_start_nsrequest(This);
1239 task = heap_alloc(sizeof(*task));
1241 return E_OUTOFMEMORY;
1243 IBindStatusCallback_AddRef(&This->bsc.IBindStatusCallback_iface);
1245 push_task(&task->header, stop_request_proc, This->bsc.doc->basedoc.doc_obj->basedoc.task_magic);
1249 static void handle_navigation_error(nsChannelBSC *This, DWORD result)
1251 HTMLDocumentObj *doc;
1252 IOleCommandTarget *olecmd;
1255 SAFEARRAYBOUND bound;
1256 VARIANT var, varOut;
1264 doc = This->window->doc_obj;
1265 if(!doc || !doc->doc_object_service || !doc->client)
1268 hres = IDocObjectService_IsErrorUrl(doc->doc_object_service,
1269 This->window->url, &is_error_url);
1270 if(FAILED(hres) || is_error_url)
1273 hres = IOleClientSite_QueryInterface(doc->client,
1274 &IID_IOleCommandTarget, (void**)&olecmd);
1279 bound.cElements = 8;
1280 sa = SafeArrayCreate(VT_VARIANT, 1, &bound);
1282 IOleCommandTarget_Release(olecmd);
1288 V_I4(&var) = result;
1289 SafeArrayPutElement(sa, &ind, &var);
1292 V_VT(&var) = VT_BSTR;
1293 url = SysAllocString(This->window->url);
1295 SafeArrayPutElement(sa, &ind, &var);
1298 V_VT(&var) = VT_UNKNOWN;
1299 V_UNKNOWN(&var) = (IUnknown*)&This->window->IHTMLWindow2_iface;
1300 SafeArrayPutElement(sa, &ind, &var);
1302 /* FIXME: what are the following fields for? */
1304 V_VT(&var) = VT_UNKNOWN;
1305 V_UNKNOWN(&var) = NULL;
1306 SafeArrayPutElement(sa, &ind, &var);
1309 V_VT(&var) = VT_BOOL;
1310 V_BOOL(&var) = FALSE;
1311 SafeArrayPutElement(sa, &ind, &var);
1314 V_VT(&var) = VT_BOOL;
1315 V_BOOL(&var) = FALSE;
1316 SafeArrayPutElement(sa, &ind, &var);
1319 V_VT(&var) = VT_BSTR;
1320 unk = SysAllocString(NULL);
1322 SafeArrayPutElement(sa, &ind, &var);
1325 V_VT(&var) = VT_UNKNOWN;
1326 V_UNKNOWN(&var) = NULL;
1327 SafeArrayPutElement(sa, &ind, &var);
1329 V_VT(&var) = VT_ARRAY;
1331 V_VT(&varOut) = VT_BOOL;
1332 V_BOOL(&varOut) = VARIANT_TRUE;
1333 IOleCommandTarget_Exec(olecmd, &CGID_DocHostCmdPriv, 1, 0, &var, FAILED(hres)?NULL:&varOut);
1337 SafeArrayDestroy(sa);
1338 IOleCommandTarget_Release(olecmd);
1341 static HRESULT nsChannelBSC_stop_binding(BSCallback *bsc, HRESULT result)
1343 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1345 if(result != E_ABORT) {
1347 handle_navigation_error(This, result);
1348 else if(This->window) {
1349 result = async_stop_request(This);
1350 if(SUCCEEDED(result))
1355 on_stop_nsrequest(This, result);
1359 static HRESULT nsChannelBSC_read_data(BSCallback *bsc, IStream *stream)
1361 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1363 return read_stream_data(This, stream);
1366 static HRESULT handle_redirect(nsChannelBSC *This, const WCHAR *new_url)
1368 nsRedirectCallback *callback;
1369 nsIChannelEventSink *sink;
1370 nsChannel *new_channel;
1374 TRACE("(%p)->(%s)\n", This, debugstr_w(new_url));
1376 if(!This->nschannel || !This->nschannel->notif_callback)
1379 nsres = nsIInterfaceRequestor_GetInterface(This->nschannel->notif_callback, &IID_nsIChannelEventSink, (void**)&sink);
1380 if(NS_FAILED(nsres))
1383 hres = create_redirect_nschannel(new_url, This->nschannel, &new_channel);
1384 if(SUCCEEDED(hres)) {
1385 hres = create_redirect_callback(new_channel, This, &callback);
1386 nsIChannel_Release(&new_channel->nsIHttpChannel_iface);
1389 if(SUCCEEDED(hres)) {
1390 nsres = nsIChannelEventSink_AsyncOnChannelRedirect(sink, (nsIChannel*)&This->nschannel->nsIHttpChannel_iface,
1391 (nsIChannel*)&callback->nschannel->nsIHttpChannel_iface, REDIRECT_TEMPORARY, /* FIXME */
1392 &callback->nsIAsyncVerifyRedirectCallback_iface);
1394 if(NS_FAILED(nsres))
1395 FIXME("AsyncOnChannelRedirect failed: %08x\n", hres);
1396 else if(This->nschannel != callback->nschannel)
1397 FIXME("nschannel not updated\n");
1399 nsIAsyncVerifyRedirectCallback_Release(&callback->nsIAsyncVerifyRedirectCallback_iface);
1402 nsIChannelEventSink_Release(sink);
1406 static HRESULT nsChannelBSC_on_progress(BSCallback *bsc, ULONG status_code, LPCWSTR status_text)
1408 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1410 switch(status_code) {
1411 case BINDSTATUS_MIMETYPEAVAILABLE:
1412 if(!This->nschannel)
1415 heap_free(This->nschannel->content_type);
1416 This->nschannel->content_type = heap_strdupWtoA(status_text);
1418 case BINDSTATUS_REDIRECTING:
1419 return handle_redirect(This, status_text);
1420 case BINDSTATUS_BEGINDOWNLOADDATA: {
1421 IWinInetHttpInfo *http_info;
1422 DWORD status, size = sizeof(DWORD);
1425 if(!This->bsc.binding)
1428 hres = IBinding_QueryInterface(This->bsc.binding, &IID_IWinInetHttpInfo, (void**)&http_info);
1432 hres = IWinInetHttpInfo_QueryInfo(http_info,
1433 HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL, NULL);
1434 IWinInetHttpInfo_Release(http_info);
1435 if(FAILED(hres) || status == HTTP_STATUS_OK)
1438 handle_navigation_error(This, status);
1445 static HRESULT nsChannelBSC_on_response(BSCallback *bsc, DWORD response_code,
1446 LPCWSTR response_headers)
1448 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1451 This->nschannel->response_status = response_code;
1453 if(response_headers) {
1454 const WCHAR *headers;
1456 headers = strchrW(response_headers, '\r');
1457 if(headers && headers[1] == '\n') {
1459 hres = parse_headers(headers, &This->nschannel->response_headers);
1461 WARN("parsing headers failed: %08x\n", hres);
1470 static HRESULT nsChannelBSC_beginning_transaction(BSCallback *bsc, WCHAR **additional_headers)
1472 nsChannelBSC *This = nsChannelBSC_from_BSCallback(bsc);
1473 http_header_t *iter;
1477 static const WCHAR content_lengthW[] =
1478 {'C','o','n','t','e','n','t','-','L','e','n','g','t','h',0};
1480 if(!This->nschannel)
1483 LIST_FOR_EACH_ENTRY(iter, &This->nschannel->request_headers, http_header_t, entry) {
1484 if(strcmpW(iter->header, content_lengthW))
1485 len += strlenW(iter->header) + 2 /* ": " */ + strlenW(iter->data) + 2 /* "\r\n" */;
1491 *additional_headers = ptr = CoTaskMemAlloc((len+1)*sizeof(WCHAR));
1493 return E_OUTOFMEMORY;
1495 LIST_FOR_EACH_ENTRY(iter, &This->nschannel->request_headers, http_header_t, entry) {
1496 if(!strcmpW(iter->header, content_lengthW))
1499 len = strlenW(iter->header);
1500 memcpy(ptr, iter->header, len*sizeof(WCHAR));
1506 len = strlenW(iter->data);
1507 memcpy(ptr, iter->data, len*sizeof(WCHAR));
1519 static const BSCallbackVtbl nsChannelBSCVtbl = {
1520 nsChannelBSC_destroy,
1521 nsChannelBSC_init_bindinfo,
1522 nsChannelBSC_start_binding,
1523 nsChannelBSC_stop_binding,
1524 nsChannelBSC_read_data,
1525 nsChannelBSC_on_progress,
1526 nsChannelBSC_on_response,
1527 nsChannelBSC_beginning_transaction
1530 HRESULT create_channelbsc(IMoniker *mon, WCHAR *headers, BYTE *post_data, DWORD post_data_size, nsChannelBSC **retval)
1534 ret = heap_alloc_zero(sizeof(*ret));
1536 return E_OUTOFMEMORY;
1538 init_bscallback(&ret->bsc, &nsChannelBSCVtbl, mon, BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA);
1541 ret->bsc.headers = heap_strdupW(headers);
1542 if(!ret->bsc.headers) {
1543 IBindStatusCallback_Release(&ret->bsc.IBindStatusCallback_iface);
1544 return E_OUTOFMEMORY;
1549 ret->bsc.post_data = GlobalAlloc(0, post_data_size);
1550 if(!ret->bsc.post_data) {
1551 heap_free(ret->bsc.headers);
1552 IBindStatusCallback_Release(&ret->bsc.IBindStatusCallback_iface);
1553 return E_OUTOFMEMORY;
1556 memcpy(ret->bsc.post_data, post_data, post_data_size);
1557 ret->bsc.post_data_len = post_data_size;
1564 void set_window_bscallback(HTMLWindow *window, nsChannelBSC *callback)
1566 if(window->bscallback) {
1567 if(window->bscallback->bsc.binding)
1568 IBinding_Abort(window->bscallback->bsc.binding);
1569 window->bscallback->bsc.doc = NULL;
1570 window->bscallback->window = NULL;
1571 IBindStatusCallback_Release(&window->bscallback->bsc.IBindStatusCallback_iface);
1574 window->bscallback = callback;
1577 callback->window = window;
1578 IBindStatusCallback_AddRef(&callback->bsc.IBindStatusCallback_iface);
1579 callback->bsc.doc = window->doc;
1586 nsChannelBSC *bscallback;
1587 } start_doc_binding_task_t;
1589 static void start_doc_binding_proc(task_t *_task)
1591 start_doc_binding_task_t *task = (start_doc_binding_task_t*)_task;
1593 start_binding(task->window, NULL, (BSCallback*)task->bscallback, NULL);
1594 IBindStatusCallback_Release(&task->bscallback->bsc.IBindStatusCallback_iface);
1597 HRESULT async_start_doc_binding(HTMLWindow *window, nsChannelBSC *bscallback)
1599 start_doc_binding_task_t *task;
1601 task = heap_alloc(sizeof(start_doc_binding_task_t));
1603 return E_OUTOFMEMORY;
1605 task->window = window;
1606 task->bscallback = bscallback;
1607 IBindStatusCallback_AddRef(&bscallback->bsc.IBindStatusCallback_iface);
1609 push_task(&task->header, start_doc_binding_proc, window->task_magic);
1613 void abort_document_bindings(HTMLDocumentNode *doc)
1615 BSCallback *iter, *next;
1617 LIST_FOR_EACH_ENTRY_SAFE(iter, next, &doc->bindings, BSCallback, entry) {
1618 TRACE("Aborting %p\n", iter);
1621 remove_target_tasks(iter->doc->basedoc.task_magic);
1624 IBinding_Abort(iter->binding);
1626 list_remove(&iter->entry);
1627 list_init(&iter->entry);
1628 iter->vtbl->stop_binding(iter, E_ABORT);
1635 HRESULT channelbsc_load_stream(nsChannelBSC *bscallback, IStream *stream)
1637 HRESULT hres = S_OK;
1639 if(!bscallback->nschannel) {
1640 ERR("NULL nschannel\n");
1644 bscallback->nschannel->content_type = heap_strdupA("text/html");
1645 if(!bscallback->nschannel->content_type)
1646 return E_OUTOFMEMORY;
1648 list_add_head(&bscallback->bsc.doc->bindings, &bscallback->bsc.entry);
1650 hres = read_stream_data(bscallback, stream);
1652 hres = async_stop_request(bscallback);
1654 IBindStatusCallback_OnStopBinding(&bscallback->bsc.IBindStatusCallback_iface, hres,
1660 void channelbsc_set_channel(nsChannelBSC *This, nsChannel *channel, nsIStreamListener *listener, nsISupports *context)
1662 nsIChannel_AddRef(&channel->nsIHttpChannel_iface);
1663 This->nschannel = channel;
1665 nsIStreamListener_AddRef(listener);
1666 This->nslistener = listener;
1669 nsISupports_AddRef(context);
1670 This->nscontext = context;
1673 if(This->bsc.headers) {
1676 hres = parse_headers(This->bsc.headers, &channel->request_headers);
1677 heap_free(This->bsc.headers);
1678 This->bsc.headers = NULL;
1680 WARN("parse_headers failed: %08x\n", hres);
1684 HRESULT hlink_frame_navigate(HTMLDocument *doc, LPCWSTR url, nsChannel *nschannel, DWORD hlnf, BOOL *cancel)
1686 IHlinkFrame *hlink_frame;
1687 nsChannelBSC *callback;
1688 IServiceProvider *sp;
1696 hres = IOleClientSite_QueryInterface(doc->doc_obj->client, &IID_IServiceProvider,
1701 hres = IServiceProvider_QueryService(sp, &IID_IHlinkFrame, &IID_IHlinkFrame,
1702 (void**)&hlink_frame);
1703 IServiceProvider_Release(sp);
1707 hres = create_channelbsc(NULL, NULL, NULL, 0, &callback);
1709 IHlinkFrame_Release(hlink_frame);
1714 read_post_data_stream(callback, nschannel);
1716 hres = CreateAsyncBindCtx(0, &callback->bsc.IBindStatusCallback_iface, NULL, &bindctx);
1718 hres = CoCreateInstance(&CLSID_StdHlink, NULL, CLSCTX_INPROC_SERVER,
1719 &IID_IHlink, (LPVOID*)&hlink);
1722 hres = CreateURLMoniker(NULL, url, &mon);
1724 if(SUCCEEDED(hres)) {
1725 IHlink_SetMonikerReference(hlink, HLINKSETF_TARGET, mon, NULL);
1727 if(hlnf & HLNF_OPENINNEWWINDOW) {
1728 static const WCHAR wszBlank[] = {'_','b','l','a','n','k',0};
1729 IHlink_SetTargetFrameName(hlink, wszBlank); /* FIXME */
1732 hres = IHlinkFrame_Navigate(hlink_frame, hlnf, bindctx,
1733 &callback->bsc.IBindStatusCallback_iface, hlink);
1734 IMoniker_Release(mon);
1735 *cancel = hres == S_OK;
1739 IHlinkFrame_Release(hlink_frame);
1740 IBindCtx_Release(bindctx);
1741 IBindStatusCallback_Release(&callback->bsc.IBindStatusCallback_iface);
1745 HRESULT navigate_url(HTMLWindow *window, const WCHAR *new_url, const WCHAR *base_url)
1747 WCHAR url[INTERNET_MAX_URL_LENGTH];
1753 }else if(base_url) {
1756 hres = CoInternetCombineUrl(base_url, new_url, URL_ESCAPE_SPACES_ONLY|URL_DONT_ESCAPE_EXTRA_INFO,
1757 url, sizeof(url)/sizeof(WCHAR), &len, 0);
1761 strcpyW(url, new_url);
1764 if(window->doc_obj && window->doc_obj->hostui) {
1765 OLECHAR *translated_url = NULL;
1767 hres = IDocHostUIHandler_TranslateUrl(window->doc_obj->hostui, 0, url,
1770 TRACE("%08x %s -> %s\n", hres, debugstr_w(url), debugstr_w(translated_url));
1771 strcpyW(url, translated_url);
1772 CoTaskMemFree(translated_url);
1776 if(window->doc_obj && window == window->doc_obj->basedoc.window) {
1779 hres = hlink_frame_navigate(&window->doc->basedoc, url, NULL, 0, &cancel);
1784 TRACE("Navigation handled by hlink frame\n");
1789 hres = create_doc_uri(window, url, &uri);
1793 hres = load_nsuri(window, uri, NULL, LOAD_FLAGS_NONE);
1794 nsISupports_Release((nsISupports*)uri);