wer: Add new stubbed wer.dll.
[wine/hramrach.git] / dlls / mshtml / navigate.c
blob2ddadaeeee98e32a9cc547dc705e43873d85c59b
1 /*
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
19 #include "config.h"
21 #include <stdarg.h>
23 #define COBJMACROS
24 #define NONAMELESSUNION
25 #define NONAMELESSSTRUCT
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winuser.h"
30 #include "winreg.h"
31 #include "ole2.h"
32 #include "hlguids.h"
33 #include "shlguid.h"
34 #include "wininet.h"
35 #include "shlwapi.h"
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"
47 typedef struct {
48 const nsIInputStreamVtbl *lpInputStreamVtbl;
50 LONG ref;
52 char buf[1024];
53 DWORD buf_size;
54 } nsProtocolStream;
56 #define NSINSTREAM(x) ((nsIInputStream*) &(x)->lpInputStreamVtbl)
58 typedef struct {
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**);
67 } BSCallbackVtbl;
69 struct BSCallback {
70 const IBindStatusCallbackVtbl *lpBindStatusCallbackVtbl;
71 const IServiceProviderVtbl *lpServiceProviderVtbl;
72 const IHttpNegotiate2Vtbl *lpHttpNegotiate2Vtbl;
73 const IInternetBindInfoVtbl *lpInternetBindInfoVtbl;
75 const BSCallbackVtbl *vtbl;
77 LONG ref;
79 LPWSTR headers;
80 HGLOBAL post_data;
81 ULONG post_data_len;
82 ULONG readed;
83 DWORD bindf;
84 BOOL bindinfo_ready;
86 IMoniker *mon;
87 IBinding *binding;
89 HTMLDocumentNode *doc;
91 struct list entry;
94 #define NSINSTREAM_THIS(iface) DEFINE_THIS(nsProtocolStream, InputStream, iface)
96 static nsresult NSAPI nsInputStream_QueryInterface(nsIInputStream *iface, nsIIDRef riid,
97 void **result)
99 nsProtocolStream *This = NSINSTREAM_THIS(iface);
101 *result = NULL;
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);
111 if(*result) {
112 nsIInputStream_AddRef(NSINSTREAM(This));
113 return NS_OK;
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);
127 return 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);
138 if(!ref)
139 heap_free(This);
141 return 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,
159 PRUint32 *_retval)
161 nsProtocolStream *This = NSINSTREAM_THIS(iface);
162 DWORD read = aCount;
164 TRACE("(%p)->(%p %d %p)\n", This, aBuf, aCount, _retval);
166 if(read > This->buf_size)
167 read = This->buf_size;
169 if(read) {
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;
176 *_retval = read;
177 return NS_OK;
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;
186 nsresult nsres;
188 TRACE("(%p)->(%p %p %d %p)\n", This, aWriter, aClousure, aCount, _retval);
190 if(!This->buf_size)
191 return S_OK;
193 if(aCount > This->buf_size)
194 aCount = This->buf_size;
196 nsres = aWriter(NSINSTREAM(This), aClousure, This->buf, 0, aCount, &written);
197 if(NS_FAILED(nsres))
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;
204 *_retval = written;
205 return nsres;
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,
221 nsInputStream_Close,
222 nsInputStream_Available,
223 nsInputStream_Read,
224 nsInputStream_ReadSegments,
225 nsInputStream_IsNonBlocking
228 static nsProtocolStream *create_nsprotocol_stream(void)
230 nsProtocolStream *ret = heap_alloc(sizeof(nsProtocolStream));
232 ret->lpInputStreamVtbl = &nsInputStreamVtbl;
233 ret->ref = 1;
234 ret->buf_size = 0;
236 return ret;
239 #define STATUSCLB_THIS(iface) DEFINE_THIS(BSCallback, BindStatusCallback, iface)
241 static HRESULT WINAPI BindStatusCallback_QueryInterface(IBindStatusCallback *iface,
242 REFIID riid, void **ppv)
244 BSCallback *This = STATUSCLB_THIS(iface);
246 *ppv = NULL;
247 if(IsEqualGUID(&IID_IUnknown, riid)) {
248 TRACE("(%p)->(IID_IUnknown, %p)\n", This, ppv);
249 *ppv = STATUSCLB(This);
250 }else if(IsEqualGUID(&IID_IBindStatusCallback, riid)) {
251 TRACE("(%p)->(IID_IBindStatusCallback, %p)\n", This, ppv);
252 *ppv = STATUSCLB(This);
253 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
254 TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
255 *ppv = SERVPROV(This);
256 }else if(IsEqualGUID(&IID_IHttpNegotiate, riid)) {
257 TRACE("(%p)->(IID_IHttpNegotiate %p)\n", This, ppv);
258 *ppv = HTTPNEG(This);
259 }else if(IsEqualGUID(&IID_IHttpNegotiate2, riid)) {
260 TRACE("(%p)->(IID_IHttpNegotiate2 %p)\n", This, ppv);
261 *ppv = HTTPNEG(This);
262 }else if(IsEqualGUID(&IID_IInternetBindInfo, riid)) {
263 TRACE("(%p)->(IID_IInternetBindInfo %p)\n", This, ppv);
264 *ppv = BINDINFO(This);
267 if(*ppv) {
268 IBindStatusCallback_AddRef(STATUSCLB(This));
269 return S_OK;
272 TRACE("Unsupported riid = %s\n", debugstr_guid(riid));
273 return E_NOINTERFACE;
276 static ULONG WINAPI BindStatusCallback_AddRef(IBindStatusCallback *iface)
278 BSCallback *This = STATUSCLB_THIS(iface);
279 LONG ref = InterlockedIncrement(&This->ref);
281 TRACE("(%p) ref = %d\n", This, ref);
283 return ref;
286 static ULONG WINAPI BindStatusCallback_Release(IBindStatusCallback *iface)
288 BSCallback *This = STATUSCLB_THIS(iface);
289 LONG ref = InterlockedDecrement(&This->ref);
291 TRACE("(%p) ref = %d\n", This, ref);
293 if(!ref) {
294 if(This->post_data)
295 GlobalFree(This->post_data);
296 if(This->mon)
297 IMoniker_Release(This->mon);
298 if(This->binding)
299 IBinding_Release(This->binding);
300 list_remove(&This->entry);
301 heap_free(This->headers);
303 This->vtbl->destroy(This);
306 return ref;
309 static HRESULT WINAPI BindStatusCallback_OnStartBinding(IBindStatusCallback *iface,
310 DWORD dwReserved, IBinding *pbind)
312 BSCallback *This = STATUSCLB_THIS(iface);
314 TRACE("(%p)->(%d %p)\n", This, dwReserved, pbind);
316 IBinding_AddRef(pbind);
317 This->binding = pbind;
319 if(This->doc)
320 list_add_head(&This->doc->bindings, &This->entry);
322 return This->vtbl->start_binding(This);
325 static HRESULT WINAPI BindStatusCallback_GetPriority(IBindStatusCallback *iface, LONG *pnPriority)
327 BSCallback *This = STATUSCLB_THIS(iface);
328 FIXME("(%p)->(%p)\n", This, pnPriority);
329 return E_NOTIMPL;
332 static HRESULT WINAPI BindStatusCallback_OnLowResource(IBindStatusCallback *iface, DWORD reserved)
334 BSCallback *This = STATUSCLB_THIS(iface);
335 FIXME("(%p)->(%d)\n", This, reserved);
336 return E_NOTIMPL;
339 static HRESULT WINAPI BindStatusCallback_OnProgress(IBindStatusCallback *iface, ULONG ulProgress,
340 ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
342 BSCallback *This = STATUSCLB_THIS(iface);
344 TRACE("%p)->(%u %u %u %s)\n", This, ulProgress, ulProgressMax, ulStatusCode,
345 debugstr_w(szStatusText));
347 return This->vtbl->on_progress(This, ulStatusCode, szStatusText);
350 static HRESULT WINAPI BindStatusCallback_OnStopBinding(IBindStatusCallback *iface,
351 HRESULT hresult, LPCWSTR szError)
353 BSCallback *This = STATUSCLB_THIS(iface);
354 HRESULT hres;
356 TRACE("(%p)->(%08x %s)\n", This, hresult, debugstr_w(szError));
358 /* NOTE: IE7 calls GetBindResult here */
360 hres = This->vtbl->stop_binding(This, hresult);
362 if(This->binding) {
363 IBinding_Release(This->binding);
364 This->binding = NULL;
367 list_remove(&This->entry);
368 This->doc = NULL;
370 return hres;
373 static HRESULT WINAPI BindStatusCallback_GetBindInfo(IBindStatusCallback *iface,
374 DWORD *grfBINDF, BINDINFO *pbindinfo)
376 BSCallback *This = STATUSCLB_THIS(iface);
377 DWORD size;
379 TRACE("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
381 if(!This->bindinfo_ready) {
382 HRESULT hres;
384 hres = This->vtbl->init_bindinfo(This);
385 if(FAILED(hres))
386 return hres;
388 This->bindinfo_ready = TRUE;
391 *grfBINDF = This->bindf;
393 size = pbindinfo->cbSize;
394 memset(pbindinfo, 0, size);
395 pbindinfo->cbSize = size;
397 pbindinfo->cbstgmedData = This->post_data_len;
398 pbindinfo->dwCodePage = CP_UTF8;
399 pbindinfo->dwOptions = 0x80000;
401 if(This->post_data) {
402 pbindinfo->dwBindVerb = BINDVERB_POST;
404 pbindinfo->stgmedData.tymed = TYMED_HGLOBAL;
405 pbindinfo->stgmedData.u.hGlobal = This->post_data;
406 pbindinfo->stgmedData.pUnkForRelease = (IUnknown*)STATUSCLB(This);
407 IBindStatusCallback_AddRef(STATUSCLB(This));
410 return S_OK;
413 static HRESULT WINAPI BindStatusCallback_OnDataAvailable(IBindStatusCallback *iface,
414 DWORD grfBSCF, DWORD dwSize, FORMATETC *pformatetc, STGMEDIUM *pstgmed)
416 BSCallback *This = STATUSCLB_THIS(iface);
418 TRACE("(%p)->(%08x %d %p %p)\n", This, grfBSCF, dwSize, pformatetc, pstgmed);
420 return This->vtbl->read_data(This, pstgmed->u.pstm);
423 static HRESULT WINAPI BindStatusCallback_OnObjectAvailable(IBindStatusCallback *iface,
424 REFIID riid, IUnknown *punk)
426 BSCallback *This = STATUSCLB_THIS(iface);
427 FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), punk);
428 return E_NOTIMPL;
431 #undef STATUSCLB_THIS
433 static const IBindStatusCallbackVtbl BindStatusCallbackVtbl = {
434 BindStatusCallback_QueryInterface,
435 BindStatusCallback_AddRef,
436 BindStatusCallback_Release,
437 BindStatusCallback_OnStartBinding,
438 BindStatusCallback_GetPriority,
439 BindStatusCallback_OnLowResource,
440 BindStatusCallback_OnProgress,
441 BindStatusCallback_OnStopBinding,
442 BindStatusCallback_GetBindInfo,
443 BindStatusCallback_OnDataAvailable,
444 BindStatusCallback_OnObjectAvailable
447 #define HTTPNEG_THIS(iface) DEFINE_THIS(BSCallback, HttpNegotiate2, iface)
449 static HRESULT WINAPI HttpNegotiate_QueryInterface(IHttpNegotiate2 *iface,
450 REFIID riid, void **ppv)
452 BSCallback *This = HTTPNEG_THIS(iface);
453 return IBindStatusCallback_QueryInterface(STATUSCLB(This), riid, ppv);
456 static ULONG WINAPI HttpNegotiate_AddRef(IHttpNegotiate2 *iface)
458 BSCallback *This = HTTPNEG_THIS(iface);
459 return IBindStatusCallback_AddRef(STATUSCLB(This));
462 static ULONG WINAPI HttpNegotiate_Release(IHttpNegotiate2 *iface)
464 BSCallback *This = HTTPNEG_THIS(iface);
465 return IBindStatusCallback_Release(STATUSCLB(This));
468 static HRESULT WINAPI HttpNegotiate_BeginningTransaction(IHttpNegotiate2 *iface,
469 LPCWSTR szURL, LPCWSTR szHeaders, DWORD dwReserved, LPWSTR *pszAdditionalHeaders)
471 BSCallback *This = HTTPNEG_THIS(iface);
472 HRESULT hres;
474 TRACE("(%p)->(%s %s %d %p)\n", This, debugstr_w(szURL), debugstr_w(szHeaders),
475 dwReserved, pszAdditionalHeaders);
477 *pszAdditionalHeaders = NULL;
479 hres = This->vtbl->beginning_transaction(This, pszAdditionalHeaders);
480 if(hres != S_FALSE)
481 return hres;
483 if(This->headers) {
484 DWORD size;
486 size = (strlenW(This->headers)+1)*sizeof(WCHAR);
487 *pszAdditionalHeaders = CoTaskMemAlloc(size);
488 if(!*pszAdditionalHeaders)
489 return E_OUTOFMEMORY;
490 memcpy(*pszAdditionalHeaders, This->headers, size);
493 return S_OK;
496 static HRESULT WINAPI HttpNegotiate_OnResponse(IHttpNegotiate2 *iface, DWORD dwResponseCode,
497 LPCWSTR szResponseHeaders, LPCWSTR szRequestHeaders, LPWSTR *pszAdditionalRequestHeaders)
499 BSCallback *This = HTTPNEG_THIS(iface);
501 TRACE("(%p)->(%d %s %s %p)\n", This, dwResponseCode, debugstr_w(szResponseHeaders),
502 debugstr_w(szRequestHeaders), pszAdditionalRequestHeaders);
504 return This->vtbl->on_response(This, dwResponseCode, szResponseHeaders);
507 static HRESULT WINAPI HttpNegotiate_GetRootSecurityId(IHttpNegotiate2 *iface,
508 BYTE *pbSecurityId, DWORD *pcbSecurityId, DWORD_PTR dwReserved)
510 BSCallback *This = HTTPNEG_THIS(iface);
511 FIXME("(%p)->(%p %p %ld)\n", This, pbSecurityId, pcbSecurityId, dwReserved);
512 return E_NOTIMPL;
515 #undef HTTPNEG
517 static const IHttpNegotiate2Vtbl HttpNegotiate2Vtbl = {
518 HttpNegotiate_QueryInterface,
519 HttpNegotiate_AddRef,
520 HttpNegotiate_Release,
521 HttpNegotiate_BeginningTransaction,
522 HttpNegotiate_OnResponse,
523 HttpNegotiate_GetRootSecurityId
526 #define BINDINFO_THIS(iface) DEFINE_THIS(BSCallback, InternetBindInfo, iface)
528 static HRESULT WINAPI InternetBindInfo_QueryInterface(IInternetBindInfo *iface,
529 REFIID riid, void **ppv)
531 BSCallback *This = BINDINFO_THIS(iface);
532 return IBindStatusCallback_QueryInterface(STATUSCLB(This), riid, ppv);
535 static ULONG WINAPI InternetBindInfo_AddRef(IInternetBindInfo *iface)
537 BSCallback *This = BINDINFO_THIS(iface);
538 return IBindStatusCallback_AddRef(STATUSCLB(This));
541 static ULONG WINAPI InternetBindInfo_Release(IInternetBindInfo *iface)
543 BSCallback *This = BINDINFO_THIS(iface);
544 return IBindStatusCallback_Release(STATUSCLB(This));
547 static HRESULT WINAPI InternetBindInfo_GetBindInfo(IInternetBindInfo *iface,
548 DWORD *grfBINDF, BINDINFO *pbindinfo)
550 BSCallback *This = BINDINFO_THIS(iface);
551 FIXME("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
552 return E_NOTIMPL;
555 static HRESULT WINAPI InternetBindInfo_GetBindString(IInternetBindInfo *iface,
556 ULONG ulStringType, LPOLESTR *ppwzStr, ULONG cEl, ULONG *pcElFetched)
558 BSCallback *This = BINDINFO_THIS(iface);
559 FIXME("(%p)->(%u %p %u %p)\n", This, ulStringType, ppwzStr, cEl, pcElFetched);
560 return E_NOTIMPL;
563 #undef BINDINFO_THIS
565 static const IInternetBindInfoVtbl InternetBindInfoVtbl = {
566 InternetBindInfo_QueryInterface,
567 InternetBindInfo_AddRef,
568 InternetBindInfo_Release,
569 InternetBindInfo_GetBindInfo,
570 InternetBindInfo_GetBindString
573 #define SERVPROV_THIS(iface) DEFINE_THIS(BSCallback, ServiceProvider, iface)
575 static HRESULT WINAPI BSCServiceProvider_QueryInterface(IServiceProvider *iface,
576 REFIID riid, void **ppv)
578 BSCallback *This = SERVPROV_THIS(iface);
579 return IBindStatusCallback_QueryInterface(STATUSCLB(This), riid, ppv);
582 static ULONG WINAPI BSCServiceProvider_AddRef(IServiceProvider *iface)
584 BSCallback *This = SERVPROV_THIS(iface);
585 return IBindStatusCallback_AddRef(STATUSCLB(This));
588 static ULONG WINAPI BSCServiceProvider_Release(IServiceProvider *iface)
590 BSCallback *This = SERVPROV_THIS(iface);
591 return IBindStatusCallback_Release(STATUSCLB(This));
594 static HRESULT WINAPI BSCServiceProvider_QueryService(IServiceProvider *iface,
595 REFGUID guidService, REFIID riid, void **ppv)
597 BSCallback *This = SERVPROV_THIS(iface);
598 TRACE("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
599 return E_NOINTERFACE;
602 #undef SERVPROV_THIS
604 static const IServiceProviderVtbl ServiceProviderVtbl = {
605 BSCServiceProvider_QueryInterface,
606 BSCServiceProvider_AddRef,
607 BSCServiceProvider_Release,
608 BSCServiceProvider_QueryService
611 static void init_bscallback(BSCallback *This, const BSCallbackVtbl *vtbl, IMoniker *mon, DWORD bindf)
613 This->lpBindStatusCallbackVtbl = &BindStatusCallbackVtbl;
614 This->lpServiceProviderVtbl = &ServiceProviderVtbl;
615 This->lpHttpNegotiate2Vtbl = &HttpNegotiate2Vtbl;
616 This->lpInternetBindInfoVtbl = &InternetBindInfoVtbl;
617 This->vtbl = vtbl;
618 This->ref = 1;
619 This->bindf = bindf;
621 list_init(&This->entry);
623 if(mon)
624 IMoniker_AddRef(mon);
625 This->mon = mon;
628 /* Calls undocumented 84 cmd of CGID_ShellDocView */
629 static void call_docview_84(HTMLDocumentObj *doc)
631 IOleCommandTarget *olecmd;
632 VARIANT var;
633 HRESULT hres;
635 if(!doc->client)
636 return;
638 hres = IOleClientSite_QueryInterface(doc->client, &IID_IOleCommandTarget, (void**)&olecmd);
639 if(FAILED(hres))
640 return;
642 VariantInit(&var);
643 hres = IOleCommandTarget_Exec(olecmd, &CGID_ShellDocView, 84, 0, NULL, &var);
644 IOleCommandTarget_Release(olecmd);
645 if(SUCCEEDED(hres) && V_VT(&var) != VT_NULL)
646 FIXME("handle result\n");
649 static HRESULT parse_headers(const WCHAR *headers, struct list *headers_list)
651 const WCHAR *header, *header_end, *colon, *value;
652 HRESULT hres;
654 header = headers;
655 while(*header) {
656 if(header[0] == '\r' && header[1] == '\n' && !header[2])
657 break;
658 for(colon = header; *colon && *colon != ':' && *colon != '\r'; colon++);
659 if(*colon != ':')
660 return E_FAIL;
662 value = colon+1;
663 while(*value == ' ')
664 value++;
665 if(!*value)
666 return E_FAIL;
668 for(header_end = value+1; *header_end && *header_end != '\r'; header_end++);
670 hres = set_http_header(headers_list, header, colon-header, value, header_end-value);
671 if(FAILED(hres))
672 return hres;
674 header = header_end;
675 if(header[0] == '\r' && header[1] == '\n')
676 header += 2;
679 return S_OK;
682 static HRESULT read_post_data_stream(nsIInputStream *stream, HGLOBAL *post_data,
683 ULONG *post_data_len)
685 PRUint32 data_len = 0, available = 0;
686 char *data;
687 nsresult nsres;
689 nsres = nsIInputStream_Available(stream, &available);
690 if(NS_FAILED(nsres))
691 return E_FAIL;
693 data = GlobalAlloc(0, available+1);
694 if(!data)
695 return E_OUTOFMEMORY;
697 nsres = nsIInputStream_Read(stream, data, available, &data_len);
698 if(NS_FAILED(nsres)) {
699 GlobalFree(data);
700 return E_FAIL;
703 data[data_len] = 0;
704 *post_data = data;
705 *post_data_len = data_len;
706 return S_OK;
709 static void parse_post_data(nsIInputStream *post_data_stream, LPWSTR *headers_ret,
710 HGLOBAL *post_data_ret, ULONG *post_data_len_ret)
712 ULONG post_data_len;
713 HGLOBAL post_data = NULL;
714 LPWSTR headers = NULL;
715 DWORD headers_len = 0, len;
716 const char *ptr, *ptr2, *post_data_end;
717 HRESULT hres;
719 hres = read_post_data_stream(post_data_stream, &post_data, &post_data_len);
720 if(FAILED(hres)) {
721 FIXME("read_post_data_stream failed: %08x\n", hres);
722 return;
725 ptr = ptr2 = post_data;
726 post_data_end = (const char*)post_data+post_data_len;
728 while(ptr < post_data_end && (*ptr != '\r' || ptr[1] != '\n')) {
729 while(ptr < post_data_end && (*ptr != '\r' || ptr[1] != '\n'))
730 ptr++;
732 if(!*ptr) {
733 FIXME("*ptr = 0\n");
734 return;
737 ptr += 2;
739 if(ptr-ptr2 >= sizeof(CONTENT_LENGTH)
740 && CompareStringA(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
741 CONTENT_LENGTH, sizeof(CONTENT_LENGTH)-1,
742 ptr2, sizeof(CONTENT_LENGTH)-1) == CSTR_EQUAL) {
743 ptr2 = ptr;
744 continue;
747 len = MultiByteToWideChar(CP_ACP, 0, ptr2, ptr-ptr2, NULL, 0);
749 if(headers)
750 headers = heap_realloc(headers,(headers_len+len+1)*sizeof(WCHAR));
751 else
752 headers = heap_alloc((len+1)*sizeof(WCHAR));
754 len = MultiByteToWideChar(CP_ACP, 0, ptr2, ptr-ptr2, headers+headers_len, len);
755 headers_len += len;
757 ptr2 = ptr;
760 headers[headers_len] = 0;
761 *headers_ret = headers;
763 if(ptr >= post_data_end-2) {
764 GlobalFree(post_data);
765 return;
768 ptr += 2;
770 if(headers_len) {
771 post_data_len -= ptr-(const char*)post_data;
772 memmove(post_data, ptr, post_data_len);
773 post_data = GlobalReAlloc(post_data, post_data_len+1, 0);
776 *post_data_ret = post_data;
777 *post_data_len_ret = post_data_len;
780 HRESULT start_binding(HTMLWindow *window, HTMLDocumentNode *doc, BSCallback *bscallback, IBindCtx *bctx)
782 IStream *str = NULL;
783 HRESULT hres;
785 bscallback->doc = doc;
787 /* NOTE: IE7 calls IsSystemMoniker here*/
789 if(window) {
790 if(bscallback->mon != window->mon)
791 set_current_mon(window, bscallback->mon);
792 call_docview_84(window->doc_obj);
795 if(bctx) {
796 RegisterBindStatusCallback(bctx, STATUSCLB(bscallback), NULL, 0);
797 IBindCtx_AddRef(bctx);
798 }else {
799 hres = CreateAsyncBindCtx(0, STATUSCLB(bscallback), NULL, &bctx);
800 if(FAILED(hres)) {
801 WARN("CreateAsyncBindCtx failed: %08x\n", hres);
802 bscallback->vtbl->stop_binding(bscallback, hres);
803 return hres;
807 hres = IMoniker_BindToStorage(bscallback->mon, bctx, NULL, &IID_IStream, (void**)&str);
808 IBindCtx_Release(bctx);
809 if(FAILED(hres)) {
810 WARN("BindToStorage failed: %08x\n", hres);
811 bscallback->vtbl->stop_binding(bscallback, hres);
812 return hres;
815 if(str)
816 IStream_Release(str);
818 IMoniker_Release(bscallback->mon);
819 bscallback->mon = NULL;
821 return S_OK;
824 typedef struct {
825 BSCallback bsc;
827 DWORD size;
828 BYTE *buf;
829 HRESULT hres;
830 } BufferBSC;
832 #define BUFFERBSC_THIS(bsc) ((BufferBSC*) bsc)
834 static void BufferBSC_destroy(BSCallback *bsc)
836 BufferBSC *This = BUFFERBSC_THIS(bsc);
838 heap_free(This->buf);
839 heap_free(This);
842 static HRESULT BufferBSC_init_bindinfo(BSCallback *bsc)
844 return S_OK;
847 static HRESULT BufferBSC_start_binding(BSCallback *bsc)
849 return S_OK;
852 static HRESULT BufferBSC_stop_binding(BSCallback *bsc, HRESULT result)
854 BufferBSC *This = BUFFERBSC_THIS(bsc);
856 This->hres = result;
858 if(FAILED(result)) {
859 heap_free(This->buf);
860 This->buf = NULL;
861 This->size = 0;
864 return S_OK;
867 static HRESULT BufferBSC_read_data(BSCallback *bsc, IStream *stream)
869 BufferBSC *This = BUFFERBSC_THIS(bsc);
870 DWORD readed;
871 HRESULT hres;
873 if(!This->buf) {
874 This->size = 128;
875 This->buf = heap_alloc(This->size);
878 do {
879 if(This->bsc.readed == This->size) {
880 This->size <<= 1;
881 This->buf = heap_realloc(This->buf, This->size);
884 readed = 0;
885 hres = IStream_Read(stream, This->buf+This->bsc.readed, This->size-This->bsc.readed, &readed);
886 This->bsc.readed += readed;
887 }while(hres == S_OK);
889 return S_OK;
892 static HRESULT BufferBSC_on_progress(BSCallback *bsc, ULONG status_code, LPCWSTR status_text)
894 return S_OK;
897 static HRESULT BufferBSC_on_response(BSCallback *bsc, DWORD response_code,
898 LPCWSTR response_headers)
900 return S_OK;
903 static HRESULT BufferBSC_beginning_transaction(BSCallback *bsc, WCHAR **additional_headers)
905 return S_FALSE;
908 #undef BUFFERBSC_THIS
910 static const BSCallbackVtbl BufferBSCVtbl = {
911 BufferBSC_destroy,
912 BufferBSC_init_bindinfo,
913 BufferBSC_start_binding,
914 BufferBSC_stop_binding,
915 BufferBSC_read_data,
916 BufferBSC_on_progress,
917 BufferBSC_on_response,
918 BufferBSC_beginning_transaction
922 static BufferBSC *create_bufferbsc(IMoniker *mon)
924 BufferBSC *ret = heap_alloc_zero(sizeof(*ret));
926 init_bscallback(&ret->bsc, &BufferBSCVtbl, mon, 0);
927 ret->hres = E_FAIL;
929 return ret;
932 HRESULT bind_mon_to_buffer(HTMLDocumentNode *doc, IMoniker *mon, void **buf, DWORD *size)
934 BufferBSC *bsc = create_bufferbsc(mon);
935 HRESULT hres;
937 *buf = NULL;
939 hres = start_binding(NULL, doc, &bsc->bsc, NULL);
940 if(SUCCEEDED(hres)) {
941 hres = bsc->hres;
942 if(SUCCEEDED(hres)) {
943 *buf = bsc->buf;
944 bsc->buf = NULL;
945 *size = bsc->bsc.readed;
946 bsc->size = 0;
950 IBindStatusCallback_Release(STATUSCLB(&bsc->bsc));
952 return hres;
955 struct nsChannelBSC {
956 BSCallback bsc;
958 HTMLWindow *window;
960 nsChannel *nschannel;
961 nsIStreamListener *nslistener;
962 nsISupports *nscontext;
964 nsProtocolStream *nsstream;
967 static HRESULT on_start_nsrequest(nsChannelBSC *This)
969 nsresult nsres;
971 /* FIXME: it's needed for http connections from BindToObject. */
972 if(!This->nschannel->response_status)
973 This->nschannel->response_status = 200;
975 nsres = nsIStreamListener_OnStartRequest(This->nslistener,
976 (nsIRequest*)NSCHANNEL(This->nschannel), This->nscontext);
977 if(NS_FAILED(nsres)) {
978 FIXME("OnStartRequest failed: %08x\n", nsres);
979 return E_FAIL;
982 if(This->window) {
983 update_window_doc(This->window);
984 if(This->window->doc != This->bsc.doc)
985 This->bsc.doc = This->window->doc;
986 if(This->window->readystate != READYSTATE_LOADING)
987 set_ready_state(This->window, READYSTATE_LOADING);
990 return S_OK;
993 static void on_stop_nsrequest(nsChannelBSC *This, HRESULT result)
995 nsresult nsres;
997 if(!This->bsc.readed && SUCCEEDED(result)) {
998 TRACE("No data read! Calling OnStartRequest\n");
999 on_start_nsrequest(This);
1002 if(This->nslistener) {
1003 nsres = nsIStreamListener_OnStopRequest(This->nslistener,
1004 (nsIRequest*)NSCHANNEL(This->nschannel),
1005 This->nscontext, SUCCEEDED(result) ? NS_OK : NS_ERROR_FAILURE);
1006 if(NS_FAILED(nsres))
1007 WARN("OnStopRequet failed: %08x\n", nsres);
1010 if(This->nschannel->load_group) {
1011 nsres = nsILoadGroup_RemoveRequest(This->nschannel->load_group,
1012 (nsIRequest*)NSCHANNEL(This->nschannel), NULL, NS_OK);
1013 if(NS_FAILED(nsres))
1014 ERR("RemoveRequest failed: %08x\n", nsres);
1018 static HRESULT read_stream_data(nsChannelBSC *This, IStream *stream)
1020 DWORD read;
1021 nsresult nsres;
1022 HRESULT hres;
1024 if(!This->nslistener) {
1025 BYTE buf[1024];
1027 do {
1028 read = 0;
1029 hres = IStream_Read(stream, buf, sizeof(buf), &read);
1030 }while(hres == S_OK && read);
1032 return S_OK;
1035 if(!This->nsstream)
1036 This->nsstream = create_nsprotocol_stream();
1038 do {
1039 read = 0;
1040 hres = IStream_Read(stream, This->nsstream->buf+This->nsstream->buf_size,
1041 sizeof(This->nsstream->buf)-This->nsstream->buf_size, &read);
1042 if(!read)
1043 break;
1045 This->nsstream->buf_size += read;
1047 if(!This->bsc.readed) {
1048 if(This->nsstream->buf_size >= 2
1049 && (BYTE)This->nsstream->buf[0] == 0xff
1050 && (BYTE)This->nsstream->buf[1] == 0xfe)
1051 This->nschannel->charset = heap_strdupA(UTF16_STR);
1053 if(!This->nschannel->content_type) {
1054 WCHAR *mime;
1056 hres = FindMimeFromData(NULL, NULL, This->nsstream->buf, This->nsstream->buf_size, NULL, 0, &mime, 0);
1057 if(FAILED(hres))
1058 return hres;
1060 TRACE("Found MIME %s\n", debugstr_w(mime));
1062 This->nschannel->content_type = heap_strdupWtoA(mime);
1063 CoTaskMemFree(mime);
1064 if(!This->nschannel->content_type)
1065 return E_OUTOFMEMORY;
1068 on_start_nsrequest(This);
1071 This->bsc.readed += This->nsstream->buf_size;
1073 nsres = nsIStreamListener_OnDataAvailable(This->nslistener,
1074 (nsIRequest*)NSCHANNEL(This->nschannel), This->nscontext,
1075 NSINSTREAM(This->nsstream), This->bsc.readed-This->nsstream->buf_size,
1076 This->nsstream->buf_size);
1077 if(NS_FAILED(nsres))
1078 ERR("OnDataAvailable failed: %08x\n", nsres);
1080 if(This->nsstream->buf_size == sizeof(This->nsstream->buf)) {
1081 ERR("buffer is full\n");
1082 break;
1084 }while(hres == S_OK);
1086 return S_OK;
1089 #define NSCHANNELBSC_THIS(bsc) ((nsChannelBSC*) bsc)
1091 static void nsChannelBSC_destroy(BSCallback *bsc)
1093 nsChannelBSC *This = NSCHANNELBSC_THIS(bsc);
1095 if(This->nschannel)
1096 nsIChannel_Release(NSCHANNEL(This->nschannel));
1097 if(This->nslistener)
1098 nsIStreamListener_Release(This->nslistener);
1099 if(This->nscontext)
1100 nsISupports_Release(This->nscontext);
1101 if(This->nsstream)
1102 nsIInputStream_Release(NSINSTREAM(This->nsstream));
1103 heap_free(This);
1106 static HRESULT nsChannelBSC_start_binding(BSCallback *bsc)
1108 nsChannelBSC *This = NSCHANNELBSC_THIS(bsc);
1110 if(This->window)
1111 This->window->doc->skip_mutation_notif = FALSE;
1113 return S_OK;
1116 static HRESULT nsChannelBSC_init_bindinfo(BSCallback *bsc)
1118 nsChannelBSC *This = NSCHANNELBSC_THIS(bsc);
1119 HRESULT hres;
1121 if(This->nschannel && This->nschannel->post_data_stream) {
1122 if(This->nschannel->parse_stream) {
1123 WCHAR *headers;
1125 parse_post_data(This->nschannel->post_data_stream, &headers,
1126 &This->bsc.post_data, &This->bsc.post_data_len);
1128 hres = parse_headers(headers, &This->nschannel->request_headers);
1129 heap_free(headers);
1130 if(FAILED(hres))
1131 return hres;
1132 }else {
1133 hres = read_post_data_stream(This->nschannel->post_data_stream,
1134 &This->bsc.post_data, &This->bsc.post_data_len);
1135 if(FAILED(hres))
1136 return hres;
1139 TRACE("post_data = %s\n", debugstr_an(This->bsc.post_data, This->bsc.post_data_len));
1142 return S_OK;
1145 typedef struct {
1146 task_t header;
1147 nsChannelBSC *bsc;
1148 } stop_request_task_t;
1150 static void stop_request_proc(task_t *_task)
1152 stop_request_task_t *task = (stop_request_task_t*)_task;
1154 TRACE("(%p)\n", task->bsc);
1156 on_stop_nsrequest(task->bsc, S_OK);
1157 IBindStatusCallback_Release(STATUSCLB(&task->bsc->bsc));
1160 static HRESULT async_stop_request(nsChannelBSC *This)
1162 stop_request_task_t *task;
1164 task = heap_alloc(sizeof(*task));
1165 if(!task)
1166 return E_OUTOFMEMORY;
1168 IBindStatusCallback_AddRef(STATUSCLB(&This->bsc));
1169 task->bsc = This;
1170 push_task(&task->header, stop_request_proc, This->bsc.doc->basedoc.doc_obj->basedoc.task_magic);
1171 return S_OK;
1174 static HRESULT nsChannelBSC_stop_binding(BSCallback *bsc, HRESULT result)
1176 nsChannelBSC *This = NSCHANNELBSC_THIS(bsc);
1178 if(This->window && SUCCEEDED(result)) {
1179 result = async_stop_request(This);
1180 if(SUCCEEDED(result))
1181 return S_OK;
1184 on_stop_nsrequest(This, result);
1185 return S_OK;
1188 static HRESULT nsChannelBSC_read_data(BSCallback *bsc, IStream *stream)
1190 nsChannelBSC *This = NSCHANNELBSC_THIS(bsc);
1192 return read_stream_data(This, stream);
1195 static HRESULT nsChannelBSC_on_progress(BSCallback *bsc, ULONG status_code, LPCWSTR status_text)
1197 nsChannelBSC *This = NSCHANNELBSC_THIS(bsc);
1199 switch(status_code) {
1200 case BINDSTATUS_MIMETYPEAVAILABLE:
1201 if(!This->nschannel)
1202 return S_OK;
1204 heap_free(This->nschannel->content_type);
1205 This->nschannel->content_type = heap_strdupWtoA(status_text);
1206 break;
1207 case BINDSTATUS_REDIRECTING:
1208 TRACE("redirect to %s\n", debugstr_w(status_text));
1210 /* FIXME: We should find a better way to handle this */
1211 set_wine_url(This->nschannel->uri, status_text);
1214 return S_OK;
1217 static HRESULT nsChannelBSC_on_response(BSCallback *bsc, DWORD response_code,
1218 LPCWSTR response_headers)
1220 nsChannelBSC *This = NSCHANNELBSC_THIS(bsc);
1221 HRESULT hres;
1223 This->nschannel->response_status = response_code;
1225 if(response_headers) {
1226 const WCHAR *headers;
1228 headers = strchrW(response_headers, '\r');
1229 if(headers && headers[1] == '\n') {
1230 headers += 2;
1231 hres = parse_headers(headers, &This->nschannel->response_headers);
1232 if(FAILED(hres)) {
1233 WARN("parsing headers failed: %08x\n", hres);
1234 return hres;
1239 return S_OK;
1242 static HRESULT nsChannelBSC_beginning_transaction(BSCallback *bsc, WCHAR **additional_headers)
1244 nsChannelBSC *This = NSCHANNELBSC_THIS(bsc);
1245 http_header_t *iter;
1246 DWORD len = 0;
1247 WCHAR *ptr;
1249 static const WCHAR content_lengthW[] =
1250 {'C','o','n','t','e','n','t','-','L','e','n','g','t','h',0};
1252 if(!This->nschannel)
1253 return S_FALSE;
1255 LIST_FOR_EACH_ENTRY(iter, &This->nschannel->request_headers, http_header_t, entry) {
1256 if(strcmpW(iter->header, content_lengthW))
1257 len += strlenW(iter->header) + 2 /* ": " */ + strlenW(iter->data) + 2 /* "\r\n" */;
1260 if(!len)
1261 return S_OK;
1263 *additional_headers = ptr = CoTaskMemAlloc((len+1)*sizeof(WCHAR));
1264 if(!ptr)
1265 return E_OUTOFMEMORY;
1267 LIST_FOR_EACH_ENTRY(iter, &This->nschannel->request_headers, http_header_t, entry) {
1268 if(!strcmpW(iter->header, content_lengthW))
1269 continue;
1271 len = strlenW(iter->header);
1272 memcpy(ptr, iter->header, len*sizeof(WCHAR));
1273 ptr += len;
1275 *ptr++ = ':';
1276 *ptr++ = ' ';
1278 len = strlenW(iter->data);
1279 memcpy(ptr, iter->data, len*sizeof(WCHAR));
1280 ptr += len;
1282 *ptr++ = '\r';
1283 *ptr++ = '\n';
1286 *ptr = 0;
1288 return S_OK;
1291 #undef NSCHANNELBSC_THIS
1293 static const BSCallbackVtbl nsChannelBSCVtbl = {
1294 nsChannelBSC_destroy,
1295 nsChannelBSC_init_bindinfo,
1296 nsChannelBSC_start_binding,
1297 nsChannelBSC_stop_binding,
1298 nsChannelBSC_read_data,
1299 nsChannelBSC_on_progress,
1300 nsChannelBSC_on_response,
1301 nsChannelBSC_beginning_transaction
1304 HRESULT create_channelbsc(IMoniker *mon, WCHAR *headers, BYTE *post_data, DWORD post_data_size, nsChannelBSC **retval)
1306 nsChannelBSC *ret;
1308 ret = heap_alloc_zero(sizeof(*ret));
1309 if(!ret)
1310 return E_OUTOFMEMORY;
1312 init_bscallback(&ret->bsc, &nsChannelBSCVtbl, mon, BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA);
1314 if(headers) {
1315 ret->bsc.headers = heap_strdupW(headers);
1316 if(!ret->bsc.headers) {
1317 IBindStatusCallback_Release(STATUSCLB(&ret->bsc));
1318 return E_OUTOFMEMORY;
1322 if(post_data) {
1323 ret->bsc.post_data = GlobalAlloc(0, post_data_size);
1324 if(!ret->bsc.post_data) {
1325 heap_free(ret->bsc.headers);
1326 IBindStatusCallback_Release(STATUSCLB(&ret->bsc));
1327 return E_OUTOFMEMORY;
1330 memcpy(ret->bsc.post_data, post_data, post_data_size);
1331 ret->bsc.post_data_len = post_data_size;
1334 *retval = ret;
1335 return S_OK;
1338 IMoniker *get_channelbsc_mon(nsChannelBSC *This)
1340 if(This->bsc.mon)
1341 IMoniker_AddRef(This->bsc.mon);
1342 return This->bsc.mon;
1345 void set_window_bscallback(HTMLWindow *window, nsChannelBSC *callback)
1347 if(window->bscallback) {
1348 if(window->bscallback->bsc.binding)
1349 IBinding_Abort(window->bscallback->bsc.binding);
1350 window->bscallback->bsc.doc = NULL;
1351 window->bscallback->window = NULL;
1352 IBindStatusCallback_Release(STATUSCLB(&window->bscallback->bsc));
1355 window->bscallback = callback;
1357 if(callback) {
1358 callback->window = window;
1359 IBindStatusCallback_AddRef(STATUSCLB(&callback->bsc));
1360 callback->bsc.doc = window->doc;
1364 typedef struct {
1365 task_t header;
1366 HTMLWindow *window;
1367 nsChannelBSC *bscallback;
1368 } start_doc_binding_task_t;
1370 static void start_doc_binding_proc(task_t *_task)
1372 start_doc_binding_task_t *task = (start_doc_binding_task_t*)_task;
1374 start_binding(task->window, NULL, (BSCallback*)task->bscallback, NULL);
1375 IBindStatusCallback_Release(STATUSCLB(&task->bscallback->bsc));
1378 HRESULT async_start_doc_binding(HTMLWindow *window, nsChannelBSC *bscallback)
1380 start_doc_binding_task_t *task;
1382 task = heap_alloc(sizeof(start_doc_binding_task_t));
1383 if(!task)
1384 return E_OUTOFMEMORY;
1386 task->window = window;
1387 task->bscallback = bscallback;
1388 IBindStatusCallback_AddRef(STATUSCLB(&bscallback->bsc));
1390 push_task(&task->header, start_doc_binding_proc, window->task_magic);
1391 return S_OK;
1394 void abort_document_bindings(HTMLDocumentNode *doc)
1396 BSCallback *iter;
1398 LIST_FOR_EACH_ENTRY(iter, &doc->bindings, BSCallback, entry) {
1399 if(iter->binding)
1400 IBinding_Abort(iter->binding);
1401 iter->doc = NULL;
1402 list_remove(&iter->entry);
1406 HRESULT channelbsc_load_stream(nsChannelBSC *bscallback, IStream *stream)
1408 HRESULT hres = S_OK;
1410 if(!bscallback->nschannel) {
1411 ERR("NULL nschannel\n");
1412 return E_FAIL;
1415 bscallback->nschannel->content_type = heap_strdupA("text/html");
1416 if(!bscallback->nschannel->content_type)
1417 return E_OUTOFMEMORY;
1419 if(stream)
1420 hres = read_stream_data(bscallback, stream);
1421 if(SUCCEEDED(hres))
1422 hres = async_stop_request(bscallback);
1423 if(FAILED(hres))
1424 IBindStatusCallback_OnStopBinding(STATUSCLB(&bscallback->bsc), hres, ERROR_SUCCESS);
1426 return hres;
1429 void channelbsc_set_channel(nsChannelBSC *This, nsChannel *channel, nsIStreamListener *listener, nsISupports *context)
1431 nsIChannel_AddRef(NSCHANNEL(channel));
1432 This->nschannel = channel;
1434 nsIStreamListener_AddRef(listener);
1435 This->nslistener = listener;
1437 if(context) {
1438 nsISupports_AddRef(context);
1439 This->nscontext = context;
1442 if(This->bsc.headers) {
1443 HRESULT hres;
1445 hres = parse_headers(This->bsc.headers, &channel->request_headers);
1446 heap_free(This->bsc.headers);
1447 This->bsc.headers = NULL;
1448 if(FAILED(hres))
1449 WARN("parse_headers failed: %08x\n", hres);
1453 HRESULT hlink_frame_navigate(HTMLDocument *doc, LPCWSTR url,
1454 nsIInputStream *post_data_stream, DWORD hlnf, BOOL *cancel)
1456 IHlinkFrame *hlink_frame;
1457 nsChannelBSC *callback;
1458 IServiceProvider *sp;
1459 IBindCtx *bindctx;
1460 IMoniker *mon;
1461 IHlink *hlink;
1462 HRESULT hres;
1464 *cancel = FALSE;
1466 hres = IOleClientSite_QueryInterface(doc->doc_obj->client, &IID_IServiceProvider,
1467 (void**)&sp);
1468 if(FAILED(hres))
1469 return S_OK;
1471 hres = IServiceProvider_QueryService(sp, &IID_IHlinkFrame, &IID_IHlinkFrame,
1472 (void**)&hlink_frame);
1473 IServiceProvider_Release(sp);
1474 if(FAILED(hres))
1475 return S_OK;
1477 hres = create_channelbsc(NULL, NULL, NULL, 0, &callback);
1478 if(FAILED(hres)) {
1479 IHlinkFrame_Release(hlink_frame);
1480 return hres;
1483 if(post_data_stream) {
1484 parse_post_data(post_data_stream, &callback->bsc.headers, &callback->bsc.post_data,
1485 &callback->bsc.post_data_len);
1486 TRACE("headers = %s post_data = %s\n", debugstr_w(callback->bsc.headers),
1487 debugstr_an(callback->bsc.post_data, callback->bsc.post_data_len));
1490 hres = CreateAsyncBindCtx(0, STATUSCLB(&callback->bsc), NULL, &bindctx);
1491 if(SUCCEEDED(hres))
1492 hres = CoCreateInstance(&CLSID_StdHlink, NULL, CLSCTX_INPROC_SERVER,
1493 &IID_IHlink, (LPVOID*)&hlink);
1495 if(SUCCEEDED(hres))
1496 hres = CreateURLMoniker(NULL, url, &mon);
1498 if(SUCCEEDED(hres)) {
1499 IHlink_SetMonikerReference(hlink, HLINKSETF_TARGET, mon, NULL);
1501 if(hlnf & HLNF_OPENINNEWWINDOW) {
1502 static const WCHAR wszBlank[] = {'_','b','l','a','n','k',0};
1503 IHlink_SetTargetFrameName(hlink, wszBlank); /* FIXME */
1506 hres = IHlinkFrame_Navigate(hlink_frame, hlnf, bindctx, STATUSCLB(&callback->bsc), hlink);
1507 IMoniker_Release(mon);
1508 *cancel = hres == S_OK;
1509 hres = S_OK;
1512 IHlinkFrame_Release(hlink_frame);
1513 IBindCtx_Release(bindctx);
1514 IBindStatusCallback_Release(STATUSCLB(&callback->bsc));
1515 return hres;
1518 HRESULT navigate_url(HTMLWindow *window, const WCHAR *new_url, const WCHAR *base_url)
1520 WCHAR url[INTERNET_MAX_URL_LENGTH];
1521 nsWineURI *uri;
1522 HRESULT hres;
1524 if(!new_url) {
1525 *url = 0;
1526 }else if(base_url) {
1527 DWORD len = 0;
1529 hres = CoInternetCombineUrl(base_url, new_url, URL_ESCAPE_SPACES_ONLY|URL_DONT_ESCAPE_EXTRA_INFO,
1530 url, sizeof(url)/sizeof(WCHAR), &len, 0);
1531 if(FAILED(hres))
1532 return hres;
1533 }else {
1534 strcpyW(url, new_url);
1537 if(window->doc_obj && window->doc_obj->hostui) {
1538 OLECHAR *translated_url = NULL;
1540 hres = IDocHostUIHandler_TranslateUrl(window->doc_obj->hostui, 0, url,
1541 &translated_url);
1542 if(hres == S_OK) {
1543 TRACE("%08x %s -> %s\n", hres, debugstr_w(url), debugstr_w(translated_url));
1544 strcpyW(url, translated_url);
1545 CoTaskMemFree(translated_url);
1549 if(window->doc_obj && window == window->doc_obj->basedoc.window) {
1550 BOOL cancel;
1552 hres = hlink_frame_navigate(&window->doc->basedoc, url, NULL, 0, &cancel);
1553 if(FAILED(hres))
1554 return hres;
1556 if(cancel) {
1557 TRACE("Navigation handled by hlink frame\n");
1558 return S_OK;
1562 hres = create_doc_uri(window, url, &uri);
1563 if(FAILED(hres))
1564 return hres;
1566 hres = load_nsuri(window, uri, NULL, LOAD_FLAGS_NONE);
1567 nsISupports_Release((nsISupports*)uri);
1568 return hres;