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"
42 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
44 #define CONTENT_LENGTH "Content-Length"
45 #define UTF16_STR "utf-16"
48 const nsIInputStreamVtbl *lpInputStreamVtbl;
56 #define NSINSTREAM(x) ((nsIInputStream*) &(x)->lpInputStreamVtbl)
59 void (*destroy)(BSCallback*);
60 HRESULT (*init_bindinfo)(BSCallback*);
61 HRESULT (*start_binding)(BSCallback*);
62 HRESULT (*stop_binding)(BSCallback*,HRESULT);
63 HRESULT (*read_data)(BSCallback*,IStream*);
64 HRESULT (*on_progress)(BSCallback*,ULONG,LPCWSTR);
65 HRESULT (*on_response)(BSCallback*,DWORD,LPCWSTR);
66 HRESULT (*beginning_transaction)(BSCallback*,WCHAR**);
70 IBindStatusCallback IBindStatusCallback_iface;
71 IServiceProvider IServiceProvider_iface;
72 IHttpNegotiate2 IHttpNegotiate2_iface;
73 IInternetBindInfo IInternetBindInfo_iface;
75 const BSCallbackVtbl *vtbl;
89 HTMLDocumentNode *doc;
94 #define NSINSTREAM_THIS(iface) DEFINE_THIS(nsProtocolStream, InputStream, iface)
96 static nsresult NSAPI nsInputStream_QueryInterface(nsIInputStream *iface, nsIIDRef riid,
99 nsProtocolStream *This = NSINSTREAM_THIS(iface);
103 if(IsEqualGUID(&IID_nsISupports, riid)) {
104 TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
105 *result = NSINSTREAM(This);
106 }else if(IsEqualGUID(&IID_nsIInputStream, riid)) {
107 TRACE("(%p)->(IID_nsIInputStream %p)\n", This, result);
108 *result = NSINSTREAM(This);
112 nsIInputStream_AddRef(NSINSTREAM(This));
116 WARN("unsupported interface %s\n", debugstr_guid(riid));
117 return NS_NOINTERFACE;
120 static nsrefcnt NSAPI nsInputStream_AddRef(nsIInputStream *iface)
122 nsProtocolStream *This = NSINSTREAM_THIS(iface);
123 LONG ref = InterlockedIncrement(&This->ref);
125 TRACE("(%p) ref=%d\n", This, ref);
131 static nsrefcnt NSAPI nsInputStream_Release(nsIInputStream *iface)
133 nsProtocolStream *This = NSINSTREAM_THIS(iface);
134 LONG ref = InterlockedDecrement(&This->ref);
136 TRACE("(%p) ref=%d\n", This, ref);
144 static nsresult NSAPI nsInputStream_Close(nsIInputStream *iface)
146 nsProtocolStream *This = NSINSTREAM_THIS(iface);
147 FIXME("(%p)\n", This);
148 return NS_ERROR_NOT_IMPLEMENTED;
151 static nsresult NSAPI nsInputStream_Available(nsIInputStream *iface, PRUint32 *_retval)
153 nsProtocolStream *This = NSINSTREAM_THIS(iface);
154 FIXME("(%p)->(%p)\n", This, _retval);
155 return NS_ERROR_NOT_IMPLEMENTED;
158 static nsresult NSAPI nsInputStream_Read(nsIInputStream *iface, char *aBuf, PRUint32 aCount,
161 nsProtocolStream *This = NSINSTREAM_THIS(iface);
164 TRACE("(%p)->(%p %d %p)\n", This, aBuf, aCount, _retval);
166 if(read > This->buf_size)
167 read = This->buf_size;
170 memcpy(aBuf, This->buf, read);
171 if(read < This->buf_size)
172 memmove(This->buf, This->buf+read, This->buf_size-read);
173 This->buf_size -= read;
180 static nsresult NSAPI nsInputStream_ReadSegments(nsIInputStream *iface,
181 nsresult (WINAPI *aWriter)(nsIInputStream*,void*,const char*,PRUint32,PRUint32,PRUint32*),
182 void *aClousure, PRUint32 aCount, PRUint32 *_retval)
184 nsProtocolStream *This = NSINSTREAM_THIS(iface);
185 PRUint32 written = 0;
188 TRACE("(%p)->(%p %p %d %p)\n", This, aWriter, aClousure, aCount, _retval);
193 if(aCount > This->buf_size)
194 aCount = This->buf_size;
196 nsres = aWriter(NSINSTREAM(This), aClousure, This->buf, 0, aCount, &written);
198 TRACE("aWritter failed: %08x\n", nsres);
199 else if(written != This->buf_size)
200 FIXME("written %d != buf_size %d\n", written, This->buf_size);
202 This->buf_size -= written;
208 static nsresult NSAPI nsInputStream_IsNonBlocking(nsIInputStream *iface, PRBool *_retval)
210 nsProtocolStream *This = NSINSTREAM_THIS(iface);
211 FIXME("(%p)->(%p)\n", This, _retval);
212 return NS_ERROR_NOT_IMPLEMENTED;
215 #undef NSINSTREAM_THIS
217 static const nsIInputStreamVtbl nsInputStreamVtbl = {
218 nsInputStream_QueryInterface,
219 nsInputStream_AddRef,
220 nsInputStream_Release,
222 nsInputStream_Available,
224 nsInputStream_ReadSegments,
225 nsInputStream_IsNonBlocking
228 static nsProtocolStream *create_nsprotocol_stream(void)
230 nsProtocolStream *ret = heap_alloc(sizeof(nsProtocolStream));
232 ret->lpInputStreamVtbl = &nsInputStreamVtbl;
239 static inline BSCallback *impl_from_IBindStatusCallback(IBindStatusCallback *iface)
241 return CONTAINING_RECORD(iface, BSCallback, IBindStatusCallback_iface);
244 static HRESULT WINAPI BindStatusCallback_QueryInterface(IBindStatusCallback *iface,
245 REFIID riid, void **ppv)
247 BSCallback *This = impl_from_IBindStatusCallback(iface);
250 if(IsEqualGUID(&IID_IUnknown, riid)) {
251 TRACE("(%p)->(IID_IUnknown, %p)\n", This, ppv);
252 *ppv = &This->IBindStatusCallback_iface;
253 }else if(IsEqualGUID(&IID_IBindStatusCallback, riid)) {
254 TRACE("(%p)->(IID_IBindStatusCallback, %p)\n", This, ppv);
255 *ppv = &This->IBindStatusCallback_iface;
256 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
257 TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
258 *ppv = &This->IServiceProvider_iface;
259 }else if(IsEqualGUID(&IID_IHttpNegotiate, riid)) {
260 TRACE("(%p)->(IID_IHttpNegotiate %p)\n", This, ppv);
261 *ppv = &This->IHttpNegotiate2_iface;
262 }else if(IsEqualGUID(&IID_IHttpNegotiate2, riid)) {
263 TRACE("(%p)->(IID_IHttpNegotiate2 %p)\n", This, ppv);
264 *ppv = &This->IHttpNegotiate2_iface;
265 }else if(IsEqualGUID(&IID_IInternetBindInfo, riid)) {
266 TRACE("(%p)->(IID_IInternetBindInfo %p)\n", This, ppv);
267 *ppv = &This->IInternetBindInfo_iface;
271 IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
275 TRACE("Unsupported riid = %s\n", debugstr_guid(riid));
276 return E_NOINTERFACE;
279 static ULONG WINAPI BindStatusCallback_AddRef(IBindStatusCallback *iface)
281 BSCallback *This = impl_from_IBindStatusCallback(iface);
282 LONG ref = InterlockedIncrement(&This->ref);
284 TRACE("(%p) ref = %d\n", This, ref);
289 static ULONG WINAPI BindStatusCallback_Release(IBindStatusCallback *iface)
291 BSCallback *This = impl_from_IBindStatusCallback(iface);
292 LONG ref = InterlockedDecrement(&This->ref);
294 TRACE("(%p) ref = %d\n", This, ref);
298 GlobalFree(This->post_data);
300 IMoniker_Release(This->mon);
302 IBinding_Release(This->binding);
303 list_remove(&This->entry);
304 heap_free(This->headers);
306 This->vtbl->destroy(This);
312 static HRESULT WINAPI BindStatusCallback_OnStartBinding(IBindStatusCallback *iface,
313 DWORD dwReserved, IBinding *pbind)
315 BSCallback *This = impl_from_IBindStatusCallback(iface);
317 TRACE("(%p)->(%d %p)\n", This, dwReserved, pbind);
319 IBinding_AddRef(pbind);
320 This->binding = pbind;
323 list_add_head(&This->doc->bindings, &This->entry);
325 return This->vtbl->start_binding(This);
328 static HRESULT WINAPI BindStatusCallback_GetPriority(IBindStatusCallback *iface, LONG *pnPriority)
330 BSCallback *This = impl_from_IBindStatusCallback(iface);
331 FIXME("(%p)->(%p)\n", This, pnPriority);
335 static HRESULT WINAPI BindStatusCallback_OnLowResource(IBindStatusCallback *iface, DWORD reserved)
337 BSCallback *This = impl_from_IBindStatusCallback(iface);
338 FIXME("(%p)->(%d)\n", This, reserved);
342 static HRESULT WINAPI BindStatusCallback_OnProgress(IBindStatusCallback *iface, ULONG ulProgress,
343 ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
345 BSCallback *This = impl_from_IBindStatusCallback(iface);
347 TRACE("%p)->(%u %u %u %s)\n", This, ulProgress, ulProgressMax, ulStatusCode,
348 debugstr_w(szStatusText));
350 return This->vtbl->on_progress(This, ulStatusCode, szStatusText);
353 static HRESULT WINAPI BindStatusCallback_OnStopBinding(IBindStatusCallback *iface,
354 HRESULT hresult, LPCWSTR szError)
356 BSCallback *This = impl_from_IBindStatusCallback(iface);
359 TRACE("(%p)->(%08x %s)\n", This, hresult, debugstr_w(szError));
361 /* NOTE: IE7 calls GetBindResult here */
363 hres = This->vtbl->stop_binding(This, hresult);
366 IBinding_Release(This->binding);
367 This->binding = NULL;
370 list_remove(&This->entry);
376 static HRESULT WINAPI BindStatusCallback_GetBindInfo(IBindStatusCallback *iface,
377 DWORD *grfBINDF, BINDINFO *pbindinfo)
379 BSCallback *This = impl_from_IBindStatusCallback(iface);
382 TRACE("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
384 if(!This->bindinfo_ready) {
387 hres = This->vtbl->init_bindinfo(This);
391 This->bindinfo_ready = TRUE;
394 *grfBINDF = This->bindf;
396 size = pbindinfo->cbSize;
397 memset(pbindinfo, 0, size);
398 pbindinfo->cbSize = size;
400 pbindinfo->cbstgmedData = This->post_data_len;
401 pbindinfo->dwCodePage = CP_UTF8;
402 pbindinfo->dwOptions = 0x80000;
404 if(This->post_data) {
405 pbindinfo->dwBindVerb = BINDVERB_POST;
407 pbindinfo->stgmedData.tymed = TYMED_HGLOBAL;
408 pbindinfo->stgmedData.u.hGlobal = This->post_data;
409 pbindinfo->stgmedData.pUnkForRelease = (IUnknown*)&This->IBindStatusCallback_iface;
410 IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
416 static HRESULT WINAPI BindStatusCallback_OnDataAvailable(IBindStatusCallback *iface,
417 DWORD grfBSCF, DWORD dwSize, FORMATETC *pformatetc, STGMEDIUM *pstgmed)
419 BSCallback *This = impl_from_IBindStatusCallback(iface);
421 TRACE("(%p)->(%08x %d %p %p)\n", This, grfBSCF, dwSize, pformatetc, pstgmed);
423 return This->vtbl->read_data(This, pstgmed->u.pstm);
426 static HRESULT WINAPI BindStatusCallback_OnObjectAvailable(IBindStatusCallback *iface,
427 REFIID riid, IUnknown *punk)
429 BSCallback *This = impl_from_IBindStatusCallback(iface);
430 FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), punk);
434 static const IBindStatusCallbackVtbl BindStatusCallbackVtbl = {
435 BindStatusCallback_QueryInterface,
436 BindStatusCallback_AddRef,
437 BindStatusCallback_Release,
438 BindStatusCallback_OnStartBinding,
439 BindStatusCallback_GetPriority,
440 BindStatusCallback_OnLowResource,
441 BindStatusCallback_OnProgress,
442 BindStatusCallback_OnStopBinding,
443 BindStatusCallback_GetBindInfo,
444 BindStatusCallback_OnDataAvailable,
445 BindStatusCallback_OnObjectAvailable
448 static inline BSCallback *impl_from_IHttpNegotiate2(IHttpNegotiate2 *iface)
450 return CONTAINING_RECORD(iface, BSCallback, IHttpNegotiate2_iface);
453 static HRESULT WINAPI HttpNegotiate_QueryInterface(IHttpNegotiate2 *iface,
454 REFIID riid, void **ppv)
456 BSCallback *This = impl_from_IHttpNegotiate2(iface);
457 return IBindStatusCallback_QueryInterface(&This->IBindStatusCallback_iface, riid, ppv);
460 static ULONG WINAPI HttpNegotiate_AddRef(IHttpNegotiate2 *iface)
462 BSCallback *This = impl_from_IHttpNegotiate2(iface);
463 return IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
466 static ULONG WINAPI HttpNegotiate_Release(IHttpNegotiate2 *iface)
468 BSCallback *This = impl_from_IHttpNegotiate2(iface);
469 return IBindStatusCallback_Release(&This->IBindStatusCallback_iface);
472 static HRESULT WINAPI HttpNegotiate_BeginningTransaction(IHttpNegotiate2 *iface,
473 LPCWSTR szURL, LPCWSTR szHeaders, DWORD dwReserved, LPWSTR *pszAdditionalHeaders)
475 BSCallback *This = impl_from_IHttpNegotiate2(iface);
478 TRACE("(%p)->(%s %s %d %p)\n", This, debugstr_w(szURL), debugstr_w(szHeaders),
479 dwReserved, pszAdditionalHeaders);
481 *pszAdditionalHeaders = NULL;
483 hres = This->vtbl->beginning_transaction(This, pszAdditionalHeaders);
490 size = (strlenW(This->headers)+1)*sizeof(WCHAR);
491 *pszAdditionalHeaders = CoTaskMemAlloc(size);
492 if(!*pszAdditionalHeaders)
493 return E_OUTOFMEMORY;
494 memcpy(*pszAdditionalHeaders, This->headers, size);
500 static HRESULT WINAPI HttpNegotiate_OnResponse(IHttpNegotiate2 *iface, DWORD dwResponseCode,
501 LPCWSTR szResponseHeaders, LPCWSTR szRequestHeaders, LPWSTR *pszAdditionalRequestHeaders)
503 BSCallback *This = impl_from_IHttpNegotiate2(iface);
505 TRACE("(%p)->(%d %s %s %p)\n", This, dwResponseCode, debugstr_w(szResponseHeaders),
506 debugstr_w(szRequestHeaders), pszAdditionalRequestHeaders);
508 return This->vtbl->on_response(This, dwResponseCode, szResponseHeaders);
511 static HRESULT WINAPI HttpNegotiate_GetRootSecurityId(IHttpNegotiate2 *iface,
512 BYTE *pbSecurityId, DWORD *pcbSecurityId, DWORD_PTR dwReserved)
514 BSCallback *This = impl_from_IHttpNegotiate2(iface);
515 FIXME("(%p)->(%p %p %ld)\n", This, pbSecurityId, pcbSecurityId, dwReserved);
519 static const IHttpNegotiate2Vtbl HttpNegotiate2Vtbl = {
520 HttpNegotiate_QueryInterface,
521 HttpNegotiate_AddRef,
522 HttpNegotiate_Release,
523 HttpNegotiate_BeginningTransaction,
524 HttpNegotiate_OnResponse,
525 HttpNegotiate_GetRootSecurityId
528 static inline BSCallback *impl_from_IInternetBindInfo(IInternetBindInfo *iface)
530 return CONTAINING_RECORD(iface, BSCallback, IInternetBindInfo_iface);
533 static HRESULT WINAPI InternetBindInfo_QueryInterface(IInternetBindInfo *iface,
534 REFIID riid, void **ppv)
536 BSCallback *This = impl_from_IInternetBindInfo(iface);
537 return IBindStatusCallback_QueryInterface(&This->IBindStatusCallback_iface, riid, ppv);
540 static ULONG WINAPI InternetBindInfo_AddRef(IInternetBindInfo *iface)
542 BSCallback *This = impl_from_IInternetBindInfo(iface);
543 return IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
546 static ULONG WINAPI InternetBindInfo_Release(IInternetBindInfo *iface)
548 BSCallback *This = impl_from_IInternetBindInfo(iface);
549 return IBindStatusCallback_Release(&This->IBindStatusCallback_iface);
552 static HRESULT WINAPI InternetBindInfo_GetBindInfo(IInternetBindInfo *iface,
553 DWORD *grfBINDF, BINDINFO *pbindinfo)
555 BSCallback *This = impl_from_IInternetBindInfo(iface);
556 FIXME("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
560 static HRESULT WINAPI InternetBindInfo_GetBindString(IInternetBindInfo *iface,
561 ULONG ulStringType, LPOLESTR *ppwzStr, ULONG cEl, ULONG *pcElFetched)
563 BSCallback *This = impl_from_IInternetBindInfo(iface);
564 FIXME("(%p)->(%u %p %u %p)\n", This, ulStringType, ppwzStr, cEl, pcElFetched);
568 static const IInternetBindInfoVtbl InternetBindInfoVtbl = {
569 InternetBindInfo_QueryInterface,
570 InternetBindInfo_AddRef,
571 InternetBindInfo_Release,
572 InternetBindInfo_GetBindInfo,
573 InternetBindInfo_GetBindString
576 static inline BSCallback *impl_from_IServiceProvider(IServiceProvider *iface)
578 return CONTAINING_RECORD(iface, BSCallback, IServiceProvider_iface);
581 static HRESULT WINAPI BSCServiceProvider_QueryInterface(IServiceProvider *iface,
582 REFIID riid, void **ppv)
584 BSCallback *This = impl_from_IServiceProvider(iface);
585 return IBindStatusCallback_QueryInterface(&This->IBindStatusCallback_iface, riid, ppv);
588 static ULONG WINAPI BSCServiceProvider_AddRef(IServiceProvider *iface)
590 BSCallback *This = impl_from_IServiceProvider(iface);
591 return IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
594 static ULONG WINAPI BSCServiceProvider_Release(IServiceProvider *iface)
596 BSCallback *This = impl_from_IServiceProvider(iface);
597 return IBindStatusCallback_Release(&This->IBindStatusCallback_iface);
600 static HRESULT WINAPI BSCServiceProvider_QueryService(IServiceProvider *iface,
601 REFGUID guidService, REFIID riid, void **ppv)
603 BSCallback *This = impl_from_IServiceProvider(iface);
604 TRACE("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
605 return E_NOINTERFACE;
608 static const IServiceProviderVtbl ServiceProviderVtbl = {
609 BSCServiceProvider_QueryInterface,
610 BSCServiceProvider_AddRef,
611 BSCServiceProvider_Release,
612 BSCServiceProvider_QueryService
615 static void init_bscallback(BSCallback *This, const BSCallbackVtbl *vtbl, IMoniker *mon, DWORD bindf)
617 This->IBindStatusCallback_iface.lpVtbl = &BindStatusCallbackVtbl;
618 This->IServiceProvider_iface.lpVtbl = &ServiceProviderVtbl;
619 This->IHttpNegotiate2_iface.lpVtbl = &HttpNegotiate2Vtbl;
620 This->IInternetBindInfo_iface.lpVtbl = &InternetBindInfoVtbl;
625 list_init(&This->entry);
628 IMoniker_AddRef(mon);
632 /* Calls undocumented 84 cmd of CGID_ShellDocView */
633 static void call_docview_84(HTMLDocumentObj *doc)
635 IOleCommandTarget *olecmd;
642 hres = IOleClientSite_QueryInterface(doc->client, &IID_IOleCommandTarget, (void**)&olecmd);
647 hres = IOleCommandTarget_Exec(olecmd, &CGID_ShellDocView, 84, 0, NULL, &var);
648 IOleCommandTarget_Release(olecmd);
649 if(SUCCEEDED(hres) && V_VT(&var) != VT_NULL)
650 FIXME("handle result\n");
653 static HRESULT parse_headers(const WCHAR *headers, struct list *headers_list)
655 const WCHAR *header, *header_end, *colon, *value;
660 if(header[0] == '\r' && header[1] == '\n' && !header[2])
662 for(colon = header; *colon && *colon != ':' && *colon != '\r'; colon++);
672 for(header_end = value+1; *header_end && *header_end != '\r'; header_end++);
674 hres = set_http_header(headers_list, header, colon-header, value, header_end-value);
679 if(header[0] == '\r' && header[1] == '\n')
686 static HRESULT read_post_data_stream(nsIInputStream *stream, HGLOBAL *post_data,
687 ULONG *post_data_len)
689 PRUint32 data_len = 0, available = 0;
693 nsres = nsIInputStream_Available(stream, &available);
697 data = GlobalAlloc(0, available+1);
699 return E_OUTOFMEMORY;
701 nsres = nsIInputStream_Read(stream, data, available, &data_len);
702 if(NS_FAILED(nsres)) {
709 *post_data_len = data_len;
713 HRESULT start_binding(HTMLWindow *window, HTMLDocumentNode *doc, BSCallback *bscallback, IBindCtx *bctx)
718 bscallback->doc = doc;
720 /* NOTE: IE7 calls IsSystemMoniker here*/
723 if(bscallback->mon != window->mon)
724 set_current_mon(window, bscallback->mon);
725 call_docview_84(window->doc_obj);
729 RegisterBindStatusCallback(bctx, &bscallback->IBindStatusCallback_iface, NULL, 0);
730 IBindCtx_AddRef(bctx);
732 hres = CreateAsyncBindCtx(0, &bscallback->IBindStatusCallback_iface, NULL, &bctx);
734 WARN("CreateAsyncBindCtx failed: %08x\n", hres);
735 bscallback->vtbl->stop_binding(bscallback, hres);
740 hres = IMoniker_BindToStorage(bscallback->mon, bctx, NULL, &IID_IStream, (void**)&str);
741 IBindCtx_Release(bctx);
743 WARN("BindToStorage failed: %08x\n", hres);
744 bscallback->vtbl->stop_binding(bscallback, hres);
749 IStream_Release(str);
751 IMoniker_Release(bscallback->mon);
752 bscallback->mon = NULL;
765 #define BUFFERBSC_THIS(bsc) ((BufferBSC*) bsc)
767 static void BufferBSC_destroy(BSCallback *bsc)
769 BufferBSC *This = BUFFERBSC_THIS(bsc);
771 heap_free(This->buf);
775 static HRESULT BufferBSC_init_bindinfo(BSCallback *bsc)
780 static HRESULT BufferBSC_start_binding(BSCallback *bsc)
785 static HRESULT BufferBSC_stop_binding(BSCallback *bsc, HRESULT result)
787 BufferBSC *This = BUFFERBSC_THIS(bsc);
792 heap_free(This->buf);
800 static HRESULT BufferBSC_read_data(BSCallback *bsc, IStream *stream)
802 BufferBSC *This = BUFFERBSC_THIS(bsc);
808 This->buf = heap_alloc(This->size);
812 if(This->bsc.readed == This->size) {
814 This->buf = heap_realloc(This->buf, This->size);
818 hres = IStream_Read(stream, This->buf+This->bsc.readed, This->size-This->bsc.readed, &readed);
819 This->bsc.readed += readed;
820 }while(hres == S_OK);
825 static HRESULT BufferBSC_on_progress(BSCallback *bsc, ULONG status_code, LPCWSTR status_text)
830 static HRESULT BufferBSC_on_response(BSCallback *bsc, DWORD response_code,
831 LPCWSTR response_headers)
836 static HRESULT BufferBSC_beginning_transaction(BSCallback *bsc, WCHAR **additional_headers)
841 #undef BUFFERBSC_THIS
843 static const BSCallbackVtbl BufferBSCVtbl = {
845 BufferBSC_init_bindinfo,
846 BufferBSC_start_binding,
847 BufferBSC_stop_binding,
849 BufferBSC_on_progress,
850 BufferBSC_on_response,
851 BufferBSC_beginning_transaction
855 static BufferBSC *create_bufferbsc(IMoniker *mon)
857 BufferBSC *ret = heap_alloc_zero(sizeof(*ret));
859 init_bscallback(&ret->bsc, &BufferBSCVtbl, mon, 0);
865 HRESULT bind_mon_to_buffer(HTMLDocumentNode *doc, IMoniker *mon, void **buf, DWORD *size)
867 BufferBSC *bsc = create_bufferbsc(mon);
872 hres = start_binding(NULL, doc, &bsc->bsc, NULL);
873 if(SUCCEEDED(hres)) {
875 if(SUCCEEDED(hres)) {
878 *size = bsc->bsc.readed;
883 IBindStatusCallback_Release(&bsc->bsc.IBindStatusCallback_iface);
888 struct nsChannelBSC {
893 nsChannel *nschannel;
894 nsIStreamListener *nslistener;
895 nsISupports *nscontext;
897 nsProtocolStream *nsstream;
900 static HRESULT on_start_nsrequest(nsChannelBSC *This)
904 /* FIXME: it's needed for http connections from BindToObject. */
905 if(!This->nschannel->response_status)
906 This->nschannel->response_status = 200;
908 nsres = nsIStreamListener_OnStartRequest(This->nslistener,
909 (nsIRequest*)&This->nschannel->nsIHttpChannel_iface, This->nscontext);
910 if(NS_FAILED(nsres)) {
911 FIXME("OnStartRequest failed: %08x\n", nsres);
916 update_window_doc(This->window);
917 if(This->window->doc != This->bsc.doc)
918 This->bsc.doc = This->window->doc;
919 if(This->window->readystate != READYSTATE_LOADING)
920 set_ready_state(This->window, READYSTATE_LOADING);
926 static void on_stop_nsrequest(nsChannelBSC *This, HRESULT result)
928 nsresult nsres, request_result;
932 request_result = NS_OK;
935 request_result = NS_BINDING_ABORTED;
938 request_result = NS_ERROR_FAILURE;
941 if(!This->bsc.readed && SUCCEEDED(result)) {
942 TRACE("No data read! Calling OnStartRequest\n");
943 on_start_nsrequest(This);
946 if(This->nslistener) {
947 nsres = nsIStreamListener_OnStopRequest(This->nslistener,
948 (nsIRequest*)&This->nschannel->nsIHttpChannel_iface, This->nscontext,
951 WARN("OnStopRequet failed: %08x\n", nsres);
954 if(This->nschannel->load_group) {
955 nsres = nsILoadGroup_RemoveRequest(This->nschannel->load_group,
956 (nsIRequest*)&This->nschannel->nsIHttpChannel_iface, NULL, request_result);
958 ERR("RemoveRequest failed: %08x\n", nsres);
962 static HRESULT read_stream_data(nsChannelBSC *This, IStream *stream)
968 if(!This->nslistener) {
973 hres = IStream_Read(stream, buf, sizeof(buf), &read);
974 }while(hres == S_OK && read);
980 This->nsstream = create_nsprotocol_stream();
984 hres = IStream_Read(stream, This->nsstream->buf+This->nsstream->buf_size,
985 sizeof(This->nsstream->buf)-This->nsstream->buf_size, &read);
989 This->nsstream->buf_size += read;
991 if(!This->bsc.readed) {
992 if(This->nsstream->buf_size >= 2
993 && (BYTE)This->nsstream->buf[0] == 0xff
994 && (BYTE)This->nsstream->buf[1] == 0xfe)
995 This->nschannel->charset = heap_strdupA(UTF16_STR);
997 if(!This->nschannel->content_type) {
1000 hres = FindMimeFromData(NULL, NULL, This->nsstream->buf, This->nsstream->buf_size, NULL, 0, &mime, 0);
1004 TRACE("Found MIME %s\n", debugstr_w(mime));
1006 This->nschannel->content_type = heap_strdupWtoA(mime);
1007 CoTaskMemFree(mime);
1008 if(!This->nschannel->content_type)
1009 return E_OUTOFMEMORY;
1012 on_start_nsrequest(This);
1015 This->bsc.readed += This->nsstream->buf_size;
1017 nsres = nsIStreamListener_OnDataAvailable(This->nslistener,
1018 (nsIRequest*)&This->nschannel->nsIHttpChannel_iface, This->nscontext,
1019 NSINSTREAM(This->nsstream), This->bsc.readed-This->nsstream->buf_size,
1020 This->nsstream->buf_size);
1021 if(NS_FAILED(nsres))
1022 ERR("OnDataAvailable failed: %08x\n", nsres);
1024 if(This->nsstream->buf_size == sizeof(This->nsstream->buf)) {
1025 ERR("buffer is full\n");
1028 }while(hres == S_OK);
1033 #define NSCHANNELBSC_THIS(bsc) ((nsChannelBSC*) bsc)
1035 static void nsChannelBSC_destroy(BSCallback *bsc)
1037 nsChannelBSC *This = NSCHANNELBSC_THIS(bsc);
1040 nsIChannel_Release(&This->nschannel->nsIHttpChannel_iface);
1041 if(This->nslistener)
1042 nsIStreamListener_Release(This->nslistener);
1044 nsISupports_Release(This->nscontext);
1046 nsIInputStream_Release(NSINSTREAM(This->nsstream));
1050 static HRESULT nsChannelBSC_start_binding(BSCallback *bsc)
1052 nsChannelBSC *This = NSCHANNELBSC_THIS(bsc);
1055 This->window->doc->skip_mutation_notif = FALSE;
1060 static HRESULT nsChannelBSC_init_bindinfo(BSCallback *bsc)
1062 nsChannelBSC *This = NSCHANNELBSC_THIS(bsc);
1065 if(This->nschannel && This->nschannel->post_data_stream) {
1066 hres = read_post_data_stream(This->nschannel->post_data_stream,
1067 &This->bsc.post_data, &This->bsc.post_data_len);
1071 TRACE("post_data = %s\n", debugstr_an(This->bsc.post_data, This->bsc.post_data_len));
1080 } stop_request_task_t;
1082 static void stop_request_proc(task_t *_task)
1084 stop_request_task_t *task = (stop_request_task_t*)_task;
1086 TRACE("(%p)\n", task->bsc);
1088 on_stop_nsrequest(task->bsc, S_OK);
1089 IBindStatusCallback_Release(&task->bsc->bsc.IBindStatusCallback_iface);
1092 static HRESULT async_stop_request(nsChannelBSC *This)
1094 stop_request_task_t *task;
1096 task = heap_alloc(sizeof(*task));
1098 return E_OUTOFMEMORY;
1100 IBindStatusCallback_AddRef(&This->bsc.IBindStatusCallback_iface);
1102 push_task(&task->header, stop_request_proc, This->bsc.doc->basedoc.doc_obj->basedoc.task_magic);
1106 static HRESULT nsChannelBSC_stop_binding(BSCallback *bsc, HRESULT result)
1108 nsChannelBSC *This = NSCHANNELBSC_THIS(bsc);
1110 if(This->window && SUCCEEDED(result)) {
1111 result = async_stop_request(This);
1112 if(SUCCEEDED(result))
1116 on_stop_nsrequest(This, result);
1120 static HRESULT nsChannelBSC_read_data(BSCallback *bsc, IStream *stream)
1122 nsChannelBSC *This = NSCHANNELBSC_THIS(bsc);
1124 return read_stream_data(This, stream);
1127 static HRESULT nsChannelBSC_on_progress(BSCallback *bsc, ULONG status_code, LPCWSTR status_text)
1129 nsChannelBSC *This = NSCHANNELBSC_THIS(bsc);
1131 switch(status_code) {
1132 case BINDSTATUS_MIMETYPEAVAILABLE:
1133 if(!This->nschannel)
1136 heap_free(This->nschannel->content_type);
1137 This->nschannel->content_type = heap_strdupWtoA(status_text);
1139 case BINDSTATUS_REDIRECTING:
1140 TRACE("redirect to %s\n", debugstr_w(status_text));
1142 /* FIXME: We should find a better way to handle this */
1143 set_wine_url(This->nschannel->uri, status_text);
1149 static HRESULT nsChannelBSC_on_response(BSCallback *bsc, DWORD response_code,
1150 LPCWSTR response_headers)
1152 nsChannelBSC *This = NSCHANNELBSC_THIS(bsc);
1155 This->nschannel->response_status = response_code;
1157 if(response_headers) {
1158 const WCHAR *headers;
1160 headers = strchrW(response_headers, '\r');
1161 if(headers && headers[1] == '\n') {
1163 hres = parse_headers(headers, &This->nschannel->response_headers);
1165 WARN("parsing headers failed: %08x\n", hres);
1174 static HRESULT nsChannelBSC_beginning_transaction(BSCallback *bsc, WCHAR **additional_headers)
1176 nsChannelBSC *This = NSCHANNELBSC_THIS(bsc);
1177 http_header_t *iter;
1181 static const WCHAR content_lengthW[] =
1182 {'C','o','n','t','e','n','t','-','L','e','n','g','t','h',0};
1184 if(!This->nschannel)
1187 LIST_FOR_EACH_ENTRY(iter, &This->nschannel->request_headers, http_header_t, entry) {
1188 if(strcmpW(iter->header, content_lengthW))
1189 len += strlenW(iter->header) + 2 /* ": " */ + strlenW(iter->data) + 2 /* "\r\n" */;
1195 *additional_headers = ptr = CoTaskMemAlloc((len+1)*sizeof(WCHAR));
1197 return E_OUTOFMEMORY;
1199 LIST_FOR_EACH_ENTRY(iter, &This->nschannel->request_headers, http_header_t, entry) {
1200 if(!strcmpW(iter->header, content_lengthW))
1203 len = strlenW(iter->header);
1204 memcpy(ptr, iter->header, len*sizeof(WCHAR));
1210 len = strlenW(iter->data);
1211 memcpy(ptr, iter->data, len*sizeof(WCHAR));
1223 #undef NSCHANNELBSC_THIS
1225 static const BSCallbackVtbl nsChannelBSCVtbl = {
1226 nsChannelBSC_destroy,
1227 nsChannelBSC_init_bindinfo,
1228 nsChannelBSC_start_binding,
1229 nsChannelBSC_stop_binding,
1230 nsChannelBSC_read_data,
1231 nsChannelBSC_on_progress,
1232 nsChannelBSC_on_response,
1233 nsChannelBSC_beginning_transaction
1236 HRESULT create_channelbsc(IMoniker *mon, WCHAR *headers, BYTE *post_data, DWORD post_data_size, nsChannelBSC **retval)
1240 ret = heap_alloc_zero(sizeof(*ret));
1242 return E_OUTOFMEMORY;
1244 init_bscallback(&ret->bsc, &nsChannelBSCVtbl, mon, BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA);
1247 ret->bsc.headers = heap_strdupW(headers);
1248 if(!ret->bsc.headers) {
1249 IBindStatusCallback_Release(&ret->bsc.IBindStatusCallback_iface);
1250 return E_OUTOFMEMORY;
1255 ret->bsc.post_data = GlobalAlloc(0, post_data_size);
1256 if(!ret->bsc.post_data) {
1257 heap_free(ret->bsc.headers);
1258 IBindStatusCallback_Release(&ret->bsc.IBindStatusCallback_iface);
1259 return E_OUTOFMEMORY;
1262 memcpy(ret->bsc.post_data, post_data, post_data_size);
1263 ret->bsc.post_data_len = post_data_size;
1270 IMoniker *get_channelbsc_mon(nsChannelBSC *This)
1273 IMoniker_AddRef(This->bsc.mon);
1274 return This->bsc.mon;
1277 void set_window_bscallback(HTMLWindow *window, nsChannelBSC *callback)
1279 if(window->bscallback) {
1280 if(window->bscallback->bsc.binding)
1281 IBinding_Abort(window->bscallback->bsc.binding);
1282 window->bscallback->bsc.doc = NULL;
1283 window->bscallback->window = NULL;
1284 IBindStatusCallback_Release(&window->bscallback->bsc.IBindStatusCallback_iface);
1287 window->bscallback = callback;
1290 callback->window = window;
1291 IBindStatusCallback_AddRef(&callback->bsc.IBindStatusCallback_iface);
1292 callback->bsc.doc = window->doc;
1299 nsChannelBSC *bscallback;
1300 } start_doc_binding_task_t;
1302 static void start_doc_binding_proc(task_t *_task)
1304 start_doc_binding_task_t *task = (start_doc_binding_task_t*)_task;
1306 start_binding(task->window, NULL, (BSCallback*)task->bscallback, NULL);
1307 IBindStatusCallback_Release(&task->bscallback->bsc.IBindStatusCallback_iface);
1310 HRESULT async_start_doc_binding(HTMLWindow *window, nsChannelBSC *bscallback)
1312 start_doc_binding_task_t *task;
1314 task = heap_alloc(sizeof(start_doc_binding_task_t));
1316 return E_OUTOFMEMORY;
1318 task->window = window;
1319 task->bscallback = bscallback;
1320 IBindStatusCallback_AddRef(&bscallback->bsc.IBindStatusCallback_iface);
1322 push_task(&task->header, start_doc_binding_proc, window->task_magic);
1326 void abort_document_bindings(HTMLDocumentNode *doc)
1330 LIST_FOR_EACH_ENTRY(iter, &doc->bindings, BSCallback, entry) {
1332 IBinding_Abort(iter->binding);
1334 list_remove(&iter->entry);
1338 HRESULT channelbsc_load_stream(nsChannelBSC *bscallback, IStream *stream)
1340 HRESULT hres = S_OK;
1342 if(!bscallback->nschannel) {
1343 ERR("NULL nschannel\n");
1347 bscallback->nschannel->content_type = heap_strdupA("text/html");
1348 if(!bscallback->nschannel->content_type)
1349 return E_OUTOFMEMORY;
1352 hres = read_stream_data(bscallback, stream);
1354 hres = async_stop_request(bscallback);
1356 IBindStatusCallback_OnStopBinding(&bscallback->bsc.IBindStatusCallback_iface, hres,
1362 void channelbsc_set_channel(nsChannelBSC *This, nsChannel *channel, nsIStreamListener *listener, nsISupports *context)
1364 nsIChannel_AddRef(&channel->nsIHttpChannel_iface);
1365 This->nschannel = channel;
1367 nsIStreamListener_AddRef(listener);
1368 This->nslistener = listener;
1371 nsISupports_AddRef(context);
1372 This->nscontext = context;
1375 if(This->bsc.headers) {
1378 hres = parse_headers(This->bsc.headers, &channel->request_headers);
1379 heap_free(This->bsc.headers);
1380 This->bsc.headers = NULL;
1382 WARN("parse_headers failed: %08x\n", hres);
1386 HRESULT hlink_frame_navigate(HTMLDocument *doc, LPCWSTR url,
1387 nsIInputStream *post_data_stream, DWORD hlnf, BOOL *cancel)
1389 IHlinkFrame *hlink_frame;
1390 nsChannelBSC *callback;
1391 IServiceProvider *sp;
1399 hres = IOleClientSite_QueryInterface(doc->doc_obj->client, &IID_IServiceProvider,
1404 hres = IServiceProvider_QueryService(sp, &IID_IHlinkFrame, &IID_IHlinkFrame,
1405 (void**)&hlink_frame);
1406 IServiceProvider_Release(sp);
1410 hres = create_channelbsc(NULL, NULL, NULL, 0, &callback);
1412 IHlinkFrame_Release(hlink_frame);
1416 if(post_data_stream) {
1417 read_post_data_stream(post_data_stream, &callback->bsc.post_data, &callback->bsc.post_data_len);
1418 TRACE("post_data = %s\n", debugstr_an(callback->bsc.post_data, callback->bsc.post_data_len));
1421 hres = CreateAsyncBindCtx(0, &callback->bsc.IBindStatusCallback_iface, NULL, &bindctx);
1423 hres = CoCreateInstance(&CLSID_StdHlink, NULL, CLSCTX_INPROC_SERVER,
1424 &IID_IHlink, (LPVOID*)&hlink);
1427 hres = CreateURLMoniker(NULL, url, &mon);
1429 if(SUCCEEDED(hres)) {
1430 IHlink_SetMonikerReference(hlink, HLINKSETF_TARGET, mon, NULL);
1432 if(hlnf & HLNF_OPENINNEWWINDOW) {
1433 static const WCHAR wszBlank[] = {'_','b','l','a','n','k',0};
1434 IHlink_SetTargetFrameName(hlink, wszBlank); /* FIXME */
1437 hres = IHlinkFrame_Navigate(hlink_frame, hlnf, bindctx,
1438 &callback->bsc.IBindStatusCallback_iface, hlink);
1439 IMoniker_Release(mon);
1440 *cancel = hres == S_OK;
1444 IHlinkFrame_Release(hlink_frame);
1445 IBindCtx_Release(bindctx);
1446 IBindStatusCallback_Release(&callback->bsc.IBindStatusCallback_iface);
1450 HRESULT navigate_url(HTMLWindow *window, const WCHAR *new_url, const WCHAR *base_url)
1452 WCHAR url[INTERNET_MAX_URL_LENGTH];
1458 }else if(base_url) {
1461 hres = CoInternetCombineUrl(base_url, new_url, URL_ESCAPE_SPACES_ONLY|URL_DONT_ESCAPE_EXTRA_INFO,
1462 url, sizeof(url)/sizeof(WCHAR), &len, 0);
1466 strcpyW(url, new_url);
1469 if(window->doc_obj && window->doc_obj->hostui) {
1470 OLECHAR *translated_url = NULL;
1472 hres = IDocHostUIHandler_TranslateUrl(window->doc_obj->hostui, 0, url,
1475 TRACE("%08x %s -> %s\n", hres, debugstr_w(url), debugstr_w(translated_url));
1476 strcpyW(url, translated_url);
1477 CoTaskMemFree(translated_url);
1481 if(window->doc_obj && window == window->doc_obj->basedoc.window) {
1484 hres = hlink_frame_navigate(&window->doc->basedoc, url, NULL, 0, &cancel);
1489 TRACE("Navigation handled by hlink frame\n");
1494 hres = create_doc_uri(window, url, &uri);
1498 hres = load_nsuri(window, uri, NULL, LOAD_FLAGS_NONE);
1499 nsISupports_Release((nsISupports*)uri);