mshtml: Moved setting HTTP header to separated function.
[wine] / dlls / mshtml / navigate.c
1 /*
2  * Copyright 2006-2007 Jacek Caban for CodeWeavers
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #include "config.h"
20
21 #include <stdarg.h>
22
23 #define COBJMACROS
24 #define NONAMELESSUNION
25 #define NONAMELESSSTRUCT
26
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"
36
37 #include "wine/debug.h"
38 #include "wine/unicode.h"
39
40 #include "mshtml_private.h"
41
42 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
43
44 #define CONTENT_LENGTH "Content-Length"
45 #define UTF16_STR "utf-16"
46
47 typedef struct {
48     const nsIInputStreamVtbl *lpInputStreamVtbl;
49
50     LONG ref;
51
52     char buf[1024];
53     DWORD buf_size;
54 } nsProtocolStream;
55
56 #define NSINSTREAM(x) ((nsIInputStream*) &(x)->lpInputStreamVtbl)
57
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 } BSCallbackVtbl;
67
68 struct BSCallback {
69     const IBindStatusCallbackVtbl *lpBindStatusCallbackVtbl;
70     const IServiceProviderVtbl    *lpServiceProviderVtbl;
71     const IHttpNegotiate2Vtbl     *lpHttpNegotiate2Vtbl;
72     const IInternetBindInfoVtbl   *lpInternetBindInfoVtbl;
73
74     const BSCallbackVtbl          *vtbl;
75
76     LONG ref;
77
78     LPWSTR headers;
79     HGLOBAL post_data;
80     ULONG post_data_len;
81     ULONG readed;
82     DWORD bindf;
83     BOOL bindinfo_ready;
84
85     IMoniker *mon;
86     IBinding *binding;
87
88     HTMLDocumentNode *doc;
89
90     struct list entry;
91 };
92
93 #define NSINSTREAM_THIS(iface) DEFINE_THIS(nsProtocolStream, InputStream, iface)
94
95 static nsresult NSAPI nsInputStream_QueryInterface(nsIInputStream *iface, nsIIDRef riid,
96         void **result)
97 {
98     nsProtocolStream *This = NSINSTREAM_THIS(iface);
99
100     *result = NULL;
101
102     if(IsEqualGUID(&IID_nsISupports, riid)) {
103         TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
104         *result  = NSINSTREAM(This);
105     }else if(IsEqualGUID(&IID_nsIInputStream, riid)) {
106         TRACE("(%p)->(IID_nsIInputStream %p)\n", This, result);
107         *result  = NSINSTREAM(This);
108     }
109
110     if(*result) {
111         nsIInputStream_AddRef(NSINSTREAM(This));
112         return NS_OK;
113     }
114
115     WARN("unsupported interface %s\n", debugstr_guid(riid));
116     return NS_NOINTERFACE;
117 }
118
119 static nsrefcnt NSAPI nsInputStream_AddRef(nsIInputStream *iface)
120 {
121     nsProtocolStream *This = NSINSTREAM_THIS(iface);
122     LONG ref = InterlockedIncrement(&This->ref);
123
124     TRACE("(%p) ref=%d\n", This, ref);
125
126     return ref;
127 }
128
129
130 static nsrefcnt NSAPI nsInputStream_Release(nsIInputStream *iface)
131 {
132     nsProtocolStream *This = NSINSTREAM_THIS(iface);
133     LONG ref = InterlockedDecrement(&This->ref);
134
135     TRACE("(%p) ref=%d\n", This, ref);
136
137     if(!ref)
138         heap_free(This);
139
140     return ref;
141 }
142
143 static nsresult NSAPI nsInputStream_Close(nsIInputStream *iface)
144 {
145     nsProtocolStream *This = NSINSTREAM_THIS(iface);
146     FIXME("(%p)\n", This);
147     return NS_ERROR_NOT_IMPLEMENTED;
148 }
149
150 static nsresult NSAPI nsInputStream_Available(nsIInputStream *iface, PRUint32 *_retval)
151 {
152     nsProtocolStream *This = NSINSTREAM_THIS(iface);
153     FIXME("(%p)->(%p)\n", This, _retval);
154     return NS_ERROR_NOT_IMPLEMENTED;
155 }
156
157 static nsresult NSAPI nsInputStream_Read(nsIInputStream *iface, char *aBuf, PRUint32 aCount,
158                                          PRUint32 *_retval)
159 {
160     nsProtocolStream *This = NSINSTREAM_THIS(iface);
161     DWORD read = aCount;
162
163     TRACE("(%p)->(%p %d %p)\n", This, aBuf, aCount, _retval);
164
165     if(read > This->buf_size)
166         read = This->buf_size;
167
168     if(read) {
169         memcpy(aBuf, This->buf, read);
170         if(read < This->buf_size)
171             memmove(This->buf, This->buf+read, This->buf_size-read);
172         This->buf_size -= read;
173     }
174
175     *_retval = read;
176     return NS_OK;
177 }
178
179 static nsresult NSAPI nsInputStream_ReadSegments(nsIInputStream *iface,
180         nsresult (WINAPI *aWriter)(nsIInputStream*,void*,const char*,PRUint32,PRUint32,PRUint32*),
181         void *aClousure, PRUint32 aCount, PRUint32 *_retval)
182 {
183     nsProtocolStream *This = NSINSTREAM_THIS(iface);
184     PRUint32 written = 0;
185     nsresult nsres;
186
187     TRACE("(%p)->(%p %p %d %p)\n", This, aWriter, aClousure, aCount, _retval);
188
189     if(!This->buf_size)
190         return S_OK;
191
192     if(aCount > This->buf_size)
193         aCount = This->buf_size;
194
195     nsres = aWriter(NSINSTREAM(This), aClousure, This->buf, 0, aCount, &written);
196     if(NS_FAILED(nsres))
197         TRACE("aWritter failed: %08x\n", nsres);
198     else if(written != This->buf_size)
199         FIXME("written %d != buf_size %d\n", written, This->buf_size);
200
201     This->buf_size -= written; 
202
203     *_retval = written;
204     return nsres;
205 }
206
207 static nsresult NSAPI nsInputStream_IsNonBlocking(nsIInputStream *iface, PRBool *_retval)
208 {
209     nsProtocolStream *This = NSINSTREAM_THIS(iface);
210     FIXME("(%p)->(%p)\n", This, _retval);
211     return NS_ERROR_NOT_IMPLEMENTED;
212 }
213
214 #undef NSINSTREAM_THIS
215
216 static const nsIInputStreamVtbl nsInputStreamVtbl = {
217     nsInputStream_QueryInterface,
218     nsInputStream_AddRef,
219     nsInputStream_Release,
220     nsInputStream_Close,
221     nsInputStream_Available,
222     nsInputStream_Read,
223     nsInputStream_ReadSegments,
224     nsInputStream_IsNonBlocking
225 };
226
227 static nsProtocolStream *create_nsprotocol_stream(void)
228 {
229     nsProtocolStream *ret = heap_alloc(sizeof(nsProtocolStream));
230
231     ret->lpInputStreamVtbl = &nsInputStreamVtbl;
232     ret->ref = 1;
233     ret->buf_size = 0;
234
235     return ret;
236 }
237
238 #define STATUSCLB_THIS(iface) DEFINE_THIS(BSCallback, BindStatusCallback, iface)
239
240 static HRESULT WINAPI BindStatusCallback_QueryInterface(IBindStatusCallback *iface,
241         REFIID riid, void **ppv)
242 {
243     BSCallback *This = STATUSCLB_THIS(iface);
244
245     *ppv = NULL;
246     if(IsEqualGUID(&IID_IUnknown, riid)) {
247         TRACE("(%p)->(IID_IUnknown, %p)\n", This, ppv);
248         *ppv = STATUSCLB(This);
249     }else if(IsEqualGUID(&IID_IBindStatusCallback, riid)) {
250         TRACE("(%p)->(IID_IBindStatusCallback, %p)\n", This, ppv);
251         *ppv = STATUSCLB(This);
252     }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
253         TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
254         *ppv = SERVPROV(This);
255     }else if(IsEqualGUID(&IID_IHttpNegotiate, riid)) {
256         TRACE("(%p)->(IID_IHttpNegotiate %p)\n", This, ppv);
257         *ppv = HTTPNEG(This);
258     }else if(IsEqualGUID(&IID_IHttpNegotiate2, riid)) {
259         TRACE("(%p)->(IID_IHttpNegotiate2 %p)\n", This, ppv);
260         *ppv = HTTPNEG(This);
261     }else if(IsEqualGUID(&IID_IInternetBindInfo, riid)) {
262         TRACE("(%p)->(IID_IInternetBindInfo %p)\n", This, ppv);
263         *ppv = BINDINFO(This);
264     }
265
266     if(*ppv) {
267         IBindStatusCallback_AddRef(STATUSCLB(This));
268         return S_OK;
269     }
270
271     TRACE("Unsupported riid = %s\n", debugstr_guid(riid));
272     return E_NOINTERFACE;
273 }
274
275 static ULONG WINAPI BindStatusCallback_AddRef(IBindStatusCallback *iface)
276 {
277     BSCallback *This = STATUSCLB_THIS(iface);
278     LONG ref = InterlockedIncrement(&This->ref);
279
280     TRACE("(%p) ref = %d\n", This, ref);
281
282     return ref;
283 }
284
285 static ULONG WINAPI BindStatusCallback_Release(IBindStatusCallback *iface)
286 {
287     BSCallback *This = STATUSCLB_THIS(iface);
288     LONG ref = InterlockedDecrement(&This->ref);
289
290     TRACE("(%p) ref = %d\n", This, ref);
291
292     if(!ref) {
293         if(This->post_data)
294             GlobalFree(This->post_data);
295         if(This->mon)
296             IMoniker_Release(This->mon);
297         if(This->binding)
298             IBinding_Release(This->binding);
299         list_remove(&This->entry);
300         heap_free(This->headers);
301
302         This->vtbl->destroy(This);
303     }
304
305     return ref;
306 }
307
308 static HRESULT WINAPI BindStatusCallback_OnStartBinding(IBindStatusCallback *iface,
309         DWORD dwReserved, IBinding *pbind)
310 {
311     BSCallback *This = STATUSCLB_THIS(iface);
312
313     TRACE("(%p)->(%d %p)\n", This, dwReserved, pbind);
314
315     IBinding_AddRef(pbind);
316     This->binding = pbind;
317
318     if(This->doc)
319         list_add_head(&This->doc->bindings, &This->entry);
320
321     return This->vtbl->start_binding(This);
322 }
323
324 static HRESULT WINAPI BindStatusCallback_GetPriority(IBindStatusCallback *iface, LONG *pnPriority)
325 {
326     BSCallback *This = STATUSCLB_THIS(iface);
327     FIXME("(%p)->(%p)\n", This, pnPriority);
328     return E_NOTIMPL;
329 }
330
331 static HRESULT WINAPI BindStatusCallback_OnLowResource(IBindStatusCallback *iface, DWORD reserved)
332 {
333     BSCallback *This = STATUSCLB_THIS(iface);
334     FIXME("(%p)->(%d)\n", This, reserved);
335     return E_NOTIMPL;
336 }
337
338 static HRESULT WINAPI BindStatusCallback_OnProgress(IBindStatusCallback *iface, ULONG ulProgress,
339         ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
340 {
341     BSCallback *This = STATUSCLB_THIS(iface);
342
343     TRACE("%p)->(%u %u %u %s)\n", This, ulProgress, ulProgressMax, ulStatusCode,
344             debugstr_w(szStatusText));
345
346     return This->vtbl->on_progress(This, ulStatusCode, szStatusText);
347 }
348
349 static HRESULT WINAPI BindStatusCallback_OnStopBinding(IBindStatusCallback *iface,
350         HRESULT hresult, LPCWSTR szError)
351 {
352     BSCallback *This = STATUSCLB_THIS(iface);
353     HRESULT hres;
354
355     TRACE("(%p)->(%08x %s)\n", This, hresult, debugstr_w(szError));
356
357     /* NOTE: IE7 calls GetBindResult here */
358
359     hres = This->vtbl->stop_binding(This, hresult);
360
361     if(This->binding) {
362         IBinding_Release(This->binding);
363         This->binding = NULL;
364     }
365
366     list_remove(&This->entry);
367     This->doc = NULL;
368
369     return hres;
370 }
371
372 static HRESULT WINAPI BindStatusCallback_GetBindInfo(IBindStatusCallback *iface,
373         DWORD *grfBINDF, BINDINFO *pbindinfo)
374 {
375     BSCallback *This = STATUSCLB_THIS(iface);
376     DWORD size;
377
378     TRACE("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
379
380     if(!This->bindinfo_ready) {
381         HRESULT hres;
382
383         hres = This->vtbl->init_bindinfo(This);
384         if(FAILED(hres))
385             return hres;
386
387         This->bindinfo_ready = TRUE;
388     }
389
390     *grfBINDF = This->bindf;
391
392     size = pbindinfo->cbSize;
393     memset(pbindinfo, 0, size);
394     pbindinfo->cbSize = size;
395
396     pbindinfo->cbstgmedData = This->post_data_len;
397     pbindinfo->dwCodePage = CP_UTF8;
398     pbindinfo->dwOptions = 0x80000;
399
400     if(This->post_data) {
401         pbindinfo->dwBindVerb = BINDVERB_POST;
402
403         pbindinfo->stgmedData.tymed = TYMED_HGLOBAL;
404         pbindinfo->stgmedData.u.hGlobal = This->post_data;
405         pbindinfo->stgmedData.pUnkForRelease = (IUnknown*)STATUSCLB(This);
406         IBindStatusCallback_AddRef(STATUSCLB(This));
407     }
408
409     return S_OK;
410 }
411
412 static HRESULT WINAPI BindStatusCallback_OnDataAvailable(IBindStatusCallback *iface,
413         DWORD grfBSCF, DWORD dwSize, FORMATETC *pformatetc, STGMEDIUM *pstgmed)
414 {
415     BSCallback *This = STATUSCLB_THIS(iface);
416
417     TRACE("(%p)->(%08x %d %p %p)\n", This, grfBSCF, dwSize, pformatetc, pstgmed);
418
419     return This->vtbl->read_data(This, pstgmed->u.pstm);
420 }
421
422 static HRESULT WINAPI BindStatusCallback_OnObjectAvailable(IBindStatusCallback *iface,
423         REFIID riid, IUnknown *punk)
424 {
425     BSCallback *This = STATUSCLB_THIS(iface);
426     FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), punk);
427     return E_NOTIMPL;
428 }
429
430 #undef STATUSCLB_THIS
431
432 static const IBindStatusCallbackVtbl BindStatusCallbackVtbl = {
433     BindStatusCallback_QueryInterface,
434     BindStatusCallback_AddRef,
435     BindStatusCallback_Release,
436     BindStatusCallback_OnStartBinding,
437     BindStatusCallback_GetPriority,
438     BindStatusCallback_OnLowResource,
439     BindStatusCallback_OnProgress,
440     BindStatusCallback_OnStopBinding,
441     BindStatusCallback_GetBindInfo,
442     BindStatusCallback_OnDataAvailable,
443     BindStatusCallback_OnObjectAvailable
444 };
445
446 #define HTTPNEG_THIS(iface) DEFINE_THIS(BSCallback, HttpNegotiate2, iface)
447
448 static HRESULT WINAPI HttpNegotiate_QueryInterface(IHttpNegotiate2 *iface,
449                                                    REFIID riid, void **ppv)
450 {
451     BSCallback *This = HTTPNEG_THIS(iface);
452     return IBindStatusCallback_QueryInterface(STATUSCLB(This), riid, ppv);
453 }
454
455 static ULONG WINAPI HttpNegotiate_AddRef(IHttpNegotiate2 *iface)
456 {
457     BSCallback *This = HTTPNEG_THIS(iface);
458     return IBindStatusCallback_AddRef(STATUSCLB(This));
459 }
460
461 static ULONG WINAPI HttpNegotiate_Release(IHttpNegotiate2 *iface)
462 {
463     BSCallback *This = HTTPNEG_THIS(iface);
464     return IBindStatusCallback_Release(STATUSCLB(This));
465 }
466
467 static HRESULT WINAPI HttpNegotiate_BeginningTransaction(IHttpNegotiate2 *iface,
468         LPCWSTR szURL, LPCWSTR szHeaders, DWORD dwReserved, LPWSTR *pszAdditionalHeaders)
469 {
470     BSCallback *This = HTTPNEG_THIS(iface);
471     DWORD size;
472
473     TRACE("(%p)->(%s %s %d %p)\n", This, debugstr_w(szURL), debugstr_w(szHeaders),
474           dwReserved, pszAdditionalHeaders);
475
476     if(!This->headers) {
477         *pszAdditionalHeaders = NULL;
478         return S_OK;
479     }
480
481     size = (strlenW(This->headers)+1)*sizeof(WCHAR);
482     *pszAdditionalHeaders = CoTaskMemAlloc(size);
483     memcpy(*pszAdditionalHeaders, This->headers, size);
484
485     return S_OK;
486 }
487
488 static HRESULT WINAPI HttpNegotiate_OnResponse(IHttpNegotiate2 *iface, DWORD dwResponseCode,
489         LPCWSTR szResponseHeaders, LPCWSTR szRequestHeaders, LPWSTR *pszAdditionalRequestHeaders)
490 {
491     BSCallback *This = HTTPNEG_THIS(iface);
492
493     TRACE("(%p)->(%d %s %s %p)\n", This, dwResponseCode, debugstr_w(szResponseHeaders),
494           debugstr_w(szRequestHeaders), pszAdditionalRequestHeaders);
495
496     return This->vtbl->on_response(This, dwResponseCode, szResponseHeaders);
497 }
498
499 static HRESULT WINAPI HttpNegotiate_GetRootSecurityId(IHttpNegotiate2 *iface,
500         BYTE *pbSecurityId, DWORD *pcbSecurityId, DWORD_PTR dwReserved)
501 {
502     BSCallback *This = HTTPNEG_THIS(iface);
503     FIXME("(%p)->(%p %p %ld)\n", This, pbSecurityId, pcbSecurityId, dwReserved);
504     return E_NOTIMPL;
505 }
506
507 #undef HTTPNEG
508
509 static const IHttpNegotiate2Vtbl HttpNegotiate2Vtbl = {
510     HttpNegotiate_QueryInterface,
511     HttpNegotiate_AddRef,
512     HttpNegotiate_Release,
513     HttpNegotiate_BeginningTransaction,
514     HttpNegotiate_OnResponse,
515     HttpNegotiate_GetRootSecurityId
516 };
517
518 #define BINDINFO_THIS(iface) DEFINE_THIS(BSCallback, InternetBindInfo, iface)
519
520 static HRESULT WINAPI InternetBindInfo_QueryInterface(IInternetBindInfo *iface,
521                                                       REFIID riid, void **ppv)
522 {
523     BSCallback *This = BINDINFO_THIS(iface);
524     return IBindStatusCallback_QueryInterface(STATUSCLB(This), riid, ppv);
525 }
526
527 static ULONG WINAPI InternetBindInfo_AddRef(IInternetBindInfo *iface)
528 {
529     BSCallback *This = BINDINFO_THIS(iface);
530     return IBindStatusCallback_AddRef(STATUSCLB(This));
531 }
532
533 static ULONG WINAPI InternetBindInfo_Release(IInternetBindInfo *iface)
534 {
535     BSCallback *This = BINDINFO_THIS(iface);
536     return IBindStatusCallback_Release(STATUSCLB(This));
537 }
538
539 static HRESULT WINAPI InternetBindInfo_GetBindInfo(IInternetBindInfo *iface,
540                                                    DWORD *grfBINDF, BINDINFO *pbindinfo)
541 {
542     BSCallback *This = BINDINFO_THIS(iface);
543     FIXME("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
544     return E_NOTIMPL;
545 }
546
547 static HRESULT WINAPI InternetBindInfo_GetBindString(IInternetBindInfo *iface,
548         ULONG ulStringType, LPOLESTR *ppwzStr, ULONG cEl, ULONG *pcElFetched)
549 {
550     BSCallback *This = BINDINFO_THIS(iface);
551     FIXME("(%p)->(%u %p %u %p)\n", This, ulStringType, ppwzStr, cEl, pcElFetched);
552     return E_NOTIMPL;
553 }
554
555 #undef BINDINFO_THIS
556
557 static const IInternetBindInfoVtbl InternetBindInfoVtbl = {
558     InternetBindInfo_QueryInterface,
559     InternetBindInfo_AddRef,
560     InternetBindInfo_Release,
561     InternetBindInfo_GetBindInfo,
562     InternetBindInfo_GetBindString
563 };
564
565 #define SERVPROV_THIS(iface) DEFINE_THIS(BSCallback, ServiceProvider, iface)
566
567 static HRESULT WINAPI BSCServiceProvider_QueryInterface(IServiceProvider *iface,
568                                                         REFIID riid, void **ppv)
569 {
570     BSCallback *This = SERVPROV_THIS(iface);
571     return IBindStatusCallback_QueryInterface(STATUSCLB(This), riid, ppv);
572 }
573
574 static ULONG WINAPI BSCServiceProvider_AddRef(IServiceProvider *iface)
575 {
576     BSCallback *This = SERVPROV_THIS(iface);
577     return IBindStatusCallback_AddRef(STATUSCLB(This));
578 }
579
580 static ULONG WINAPI BSCServiceProvider_Release(IServiceProvider *iface)
581 {
582     BSCallback *This = SERVPROV_THIS(iface);
583     return IBindStatusCallback_Release(STATUSCLB(This));
584 }
585
586 static HRESULT WINAPI BSCServiceProvider_QueryService(IServiceProvider *iface,
587         REFGUID guidService, REFIID riid, void **ppv)
588 {
589     BSCallback *This = SERVPROV_THIS(iface);
590     TRACE("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
591     return E_NOINTERFACE;
592 }
593
594 #undef SERVPROV_THIS
595
596 static const IServiceProviderVtbl ServiceProviderVtbl = {
597     BSCServiceProvider_QueryInterface,
598     BSCServiceProvider_AddRef,
599     BSCServiceProvider_Release,
600     BSCServiceProvider_QueryService
601 };
602
603 static void init_bscallback(BSCallback *This, const BSCallbackVtbl *vtbl, IMoniker *mon, DWORD bindf)
604 {
605     This->lpBindStatusCallbackVtbl = &BindStatusCallbackVtbl;
606     This->lpServiceProviderVtbl    = &ServiceProviderVtbl;
607     This->lpHttpNegotiate2Vtbl     = &HttpNegotiate2Vtbl;
608     This->lpInternetBindInfoVtbl   = &InternetBindInfoVtbl;
609     This->vtbl = vtbl;
610     This->ref = 1;
611     This->bindf = bindf;
612
613     list_init(&This->entry);
614
615     if(mon)
616         IMoniker_AddRef(mon);
617     This->mon = mon;
618 }
619
620 /* Calls undocumented 84 cmd of CGID_ShellDocView */
621 static void call_docview_84(HTMLDocumentObj *doc)
622 {
623     IOleCommandTarget *olecmd;
624     VARIANT var;
625     HRESULT hres;
626
627     if(!doc->client)
628         return;
629
630     hres = IOleClientSite_QueryInterface(doc->client, &IID_IOleCommandTarget, (void**)&olecmd);
631     if(FAILED(hres))
632         return;
633
634     VariantInit(&var);
635     hres = IOleCommandTarget_Exec(olecmd, &CGID_ShellDocView, 84, 0, NULL, &var);
636     IOleCommandTarget_Release(olecmd);
637     if(SUCCEEDED(hres) && V_VT(&var) != VT_NULL)
638         FIXME("handle result\n");
639 }
640
641 static void parse_post_data(nsIInputStream *post_data_stream, LPWSTR *headers_ret,
642                             HGLOBAL *post_data_ret, ULONG *post_data_len_ret)
643 {
644     PRUint32 post_data_len = 0, available = 0;
645     HGLOBAL post_data = NULL;
646     LPWSTR headers = NULL;
647     DWORD headers_len = 0, len;
648     const char *ptr, *ptr2, *post_data_end;
649
650     nsIInputStream_Available(post_data_stream, &available);
651     post_data = GlobalAlloc(0, available+1);
652     nsIInputStream_Read(post_data_stream, post_data, available, &post_data_len);
653     
654     TRACE("post_data = %s\n", debugstr_an(post_data, post_data_len));
655
656     ptr = ptr2 = post_data;
657     post_data_end = (const char*)post_data+post_data_len;
658
659     while(ptr < post_data_end && (*ptr != '\r' || ptr[1] != '\n')) {
660         while(ptr < post_data_end && (*ptr != '\r' || ptr[1] != '\n'))
661             ptr++;
662
663         if(!*ptr) {
664             FIXME("*ptr = 0\n");
665             return;
666         }
667
668         ptr += 2;
669
670         if(ptr-ptr2 >= sizeof(CONTENT_LENGTH)
671            && CompareStringA(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
672                              CONTENT_LENGTH, sizeof(CONTENT_LENGTH)-1,
673                              ptr2, sizeof(CONTENT_LENGTH)-1) == CSTR_EQUAL) {
674             ptr2 = ptr;
675             continue;
676         }
677
678         len = MultiByteToWideChar(CP_ACP, 0, ptr2, ptr-ptr2, NULL, 0);
679
680         if(headers)
681             headers = heap_realloc(headers,(headers_len+len+1)*sizeof(WCHAR));
682         else
683             headers = heap_alloc((len+1)*sizeof(WCHAR));
684
685         len = MultiByteToWideChar(CP_ACP, 0, ptr2, ptr-ptr2, headers+headers_len, len);
686         headers_len += len;
687
688         ptr2 = ptr;
689     }
690
691     headers[headers_len] = 0;
692     *headers_ret = headers;
693
694     if(ptr >= post_data_end-2) {
695         GlobalFree(post_data);
696         return;
697     }
698
699     ptr += 2;
700
701     if(headers_len) {
702         post_data_len -= ptr-(const char*)post_data;
703         memmove(post_data, ptr, post_data_len);
704         post_data = GlobalReAlloc(post_data, post_data_len+1, 0);
705     }
706
707     *post_data_ret = post_data;
708     *post_data_len_ret = post_data_len;
709 }
710
711 HRESULT start_binding(HTMLWindow *window, HTMLDocumentNode *doc, BSCallback *bscallback, IBindCtx *bctx)
712 {
713     IStream *str = NULL;
714     HRESULT hres;
715
716     bscallback->doc = doc;
717
718     /* NOTE: IE7 calls IsSystemMoniker here*/
719
720     if(window) {
721         if(bscallback->mon != window->mon)
722             set_current_mon(window, bscallback->mon);
723         call_docview_84(window->doc_obj);
724     }
725
726     if(bctx) {
727         RegisterBindStatusCallback(bctx, STATUSCLB(bscallback), NULL, 0);
728         IBindCtx_AddRef(bctx);
729     }else {
730         hres = CreateAsyncBindCtx(0, STATUSCLB(bscallback), NULL, &bctx);
731         if(FAILED(hres)) {
732             WARN("CreateAsyncBindCtx failed: %08x\n", hres);
733             bscallback->vtbl->stop_binding(bscallback, hres);
734             return hres;
735         }
736     }
737
738     hres = IMoniker_BindToStorage(bscallback->mon, bctx, NULL, &IID_IStream, (void**)&str);
739     IBindCtx_Release(bctx);
740     if(FAILED(hres)) {
741         WARN("BindToStorage failed: %08x\n", hres);
742         bscallback->vtbl->stop_binding(bscallback, hres);
743         return hres;
744     }
745
746     if(str)
747         IStream_Release(str);
748
749     IMoniker_Release(bscallback->mon);
750     bscallback->mon = NULL;
751
752     return S_OK;
753 }
754
755 typedef struct {
756     BSCallback bsc;
757
758     DWORD size;
759     BYTE *buf;
760     HRESULT hres;
761 } BufferBSC;
762
763 #define BUFFERBSC_THIS(bsc) ((BufferBSC*) bsc)
764
765 static void BufferBSC_destroy(BSCallback *bsc)
766 {
767     BufferBSC *This = BUFFERBSC_THIS(bsc);
768
769     heap_free(This->buf);
770     heap_free(This);
771 }
772
773 static HRESULT BufferBSC_init_bindinfo(BSCallback *bsc)
774 {
775     return S_OK;
776 }
777
778 static HRESULT BufferBSC_start_binding(BSCallback *bsc)
779 {
780     return S_OK;
781 }
782
783 static HRESULT BufferBSC_stop_binding(BSCallback *bsc, HRESULT result)
784 {
785     BufferBSC *This = BUFFERBSC_THIS(bsc);
786
787     This->hres = result;
788
789     if(FAILED(result)) {
790         heap_free(This->buf);
791         This->buf = NULL;
792         This->size = 0;
793     }
794
795     return S_OK;
796 }
797
798 static HRESULT BufferBSC_read_data(BSCallback *bsc, IStream *stream)
799 {
800     BufferBSC *This = BUFFERBSC_THIS(bsc);
801     DWORD readed;
802     HRESULT hres;
803
804     if(!This->buf) {
805         This->size = 128;
806         This->buf = heap_alloc(This->size);
807     }
808
809     do {
810         if(This->bsc.readed == This->size) {
811             This->size <<= 1;
812             This->buf = heap_realloc(This->buf, This->size);
813         }
814
815         readed = 0;
816         hres = IStream_Read(stream, This->buf+This->bsc.readed, This->size-This->bsc.readed, &readed);
817         This->bsc.readed += readed;
818     }while(hres == S_OK);
819
820     return S_OK;
821 }
822
823 static HRESULT BufferBSC_on_progress(BSCallback *bsc, ULONG status_code, LPCWSTR status_text)
824 {
825     return S_OK;
826 }
827
828 static HRESULT BufferBSC_on_response(BSCallback *bsc, DWORD response_code,
829         LPCWSTR response_headers)
830 {
831     return S_OK;
832 }
833
834 #undef BUFFERBSC_THIS
835
836 static const BSCallbackVtbl BufferBSCVtbl = {
837     BufferBSC_destroy,
838     BufferBSC_init_bindinfo,
839     BufferBSC_start_binding,
840     BufferBSC_stop_binding,
841     BufferBSC_read_data,
842     BufferBSC_on_progress,
843     BufferBSC_on_response
844 };
845
846
847 static BufferBSC *create_bufferbsc(IMoniker *mon)
848 {
849     BufferBSC *ret = heap_alloc_zero(sizeof(*ret));
850
851     init_bscallback(&ret->bsc, &BufferBSCVtbl, mon, 0);
852     ret->hres = E_FAIL;
853
854     return ret;
855 }
856
857 HRESULT bind_mon_to_buffer(HTMLDocumentNode *doc, IMoniker *mon, void **buf, DWORD *size)
858 {
859     BufferBSC *bsc = create_bufferbsc(mon);
860     HRESULT hres;
861
862     *buf = NULL;
863
864     hres = start_binding(NULL, doc, &bsc->bsc, NULL);
865     if(SUCCEEDED(hres)) {
866         hres = bsc->hres;
867         if(SUCCEEDED(hres)) {
868             *buf = bsc->buf;
869             bsc->buf = NULL;
870             *size = bsc->bsc.readed;
871             bsc->size = 0;
872         }
873     }
874
875     IBindStatusCallback_Release(STATUSCLB(&bsc->bsc));
876
877     return hres;
878 }
879
880 struct nsChannelBSC {
881     BSCallback bsc;
882
883     HTMLWindow *window;
884
885     nsChannel *nschannel;
886     nsIStreamListener *nslistener;
887     nsISupports *nscontext;
888
889     nsProtocolStream *nsstream;
890 };
891
892 static void on_start_nsrequest(nsChannelBSC *This)
893 {
894     nsresult nsres;
895
896     /* FIXME: it's needed for http connections from BindToObject. */
897     if(!This->nschannel->response_status)
898         This->nschannel->response_status = 200;
899
900     nsres = nsIStreamListener_OnStartRequest(This->nslistener,
901             (nsIRequest*)NSCHANNEL(This->nschannel), This->nscontext);
902     if(NS_FAILED(nsres))
903         FIXME("OnStartRequest failed: %08x\n", nsres);
904 }
905
906 static void on_stop_nsrequest(nsChannelBSC *This, HRESULT result)
907 {
908     nsresult nsres;
909
910     if(!This->nslistener)
911         return;
912
913     if(!This->bsc.readed && SUCCEEDED(result)) {
914         TRACE("No data read! Calling OnStartRequest\n");
915         on_start_nsrequest(This);
916     }
917
918     nsres = nsIStreamListener_OnStopRequest(This->nslistener, (nsIRequest*)NSCHANNEL(This->nschannel),
919              This->nscontext, SUCCEEDED(result) ? NS_OK : NS_ERROR_FAILURE);
920     if(NS_FAILED(nsres))
921         WARN("OnStopRequest failed: %08x\n", nsres);
922 }
923
924 static HRESULT read_stream_data(nsChannelBSC *This, IStream *stream)
925 {
926     DWORD read;
927     nsresult nsres;
928     HRESULT hres;
929
930     if(!This->nslistener) {
931         BYTE buf[1024];
932
933         do {
934             read = 0;
935             hres = IStream_Read(stream, buf, sizeof(buf), &read);
936         }while(hres == S_OK && read);
937
938         return S_OK;
939     }
940
941     if(!This->nsstream)
942         This->nsstream = create_nsprotocol_stream();
943
944     do {
945         read = 0;
946         hres = IStream_Read(stream, This->nsstream->buf+This->nsstream->buf_size,
947                 sizeof(This->nsstream->buf)-This->nsstream->buf_size, &read);
948         if(!read)
949             break;
950
951         This->nsstream->buf_size += read;
952
953         if(!This->bsc.readed) {
954             if(This->nsstream->buf_size >= 2
955                && (BYTE)This->nsstream->buf[0] == 0xff
956                && (BYTE)This->nsstream->buf[1] == 0xfe)
957                 This->nschannel->charset = heap_strdupA(UTF16_STR);
958
959             if(!This->nschannel->content_type) {
960                 WCHAR *mime;
961
962                 hres = FindMimeFromData(NULL, NULL, This->nsstream->buf, This->nsstream->buf_size, NULL, 0, &mime, 0);
963                 if(FAILED(hres))
964                     return hres;
965
966                 TRACE("Found MIME %s\n", debugstr_w(mime));
967
968                 This->nschannel->content_type = heap_strdupWtoA(mime);
969                 CoTaskMemFree(mime);
970                 if(!This->nschannel->content_type)
971                     return E_OUTOFMEMORY;
972             }
973
974             on_start_nsrequest(This);
975
976             if(This->window) {
977                 update_window_doc(This->window);
978                 if(This->window->readystate != READYSTATE_LOADING)
979                     set_ready_state(This->window, READYSTATE_LOADING);
980             }
981         }
982
983         This->bsc.readed += This->nsstream->buf_size;
984
985         nsres = nsIStreamListener_OnDataAvailable(This->nslistener,
986                 (nsIRequest*)NSCHANNEL(This->nschannel), This->nscontext,
987                 NSINSTREAM(This->nsstream), This->bsc.readed-This->nsstream->buf_size,
988                 This->nsstream->buf_size);
989         if(NS_FAILED(nsres))
990             ERR("OnDataAvailable failed: %08x\n", nsres);
991
992         if(This->nsstream->buf_size == sizeof(This->nsstream->buf)) {
993             ERR("buffer is full\n");
994             break;
995         }
996     }while(hres == S_OK);
997
998     return S_OK;
999 }
1000
1001 static void add_nsrequest(nsChannelBSC *This)
1002 {
1003     nsresult nsres;
1004
1005     if(!This->nschannel || !This->nschannel->load_group)
1006         return;
1007
1008     nsres = nsILoadGroup_AddRequest(This->nschannel->load_group,
1009             (nsIRequest*)NSCHANNEL(This->nschannel), This->nscontext);
1010
1011     if(NS_FAILED(nsres))
1012         ERR("AddRequest failed:%08x\n", nsres);
1013 }
1014
1015 #define NSCHANNELBSC_THIS(bsc) ((nsChannelBSC*) bsc)
1016
1017 static void nsChannelBSC_destroy(BSCallback *bsc)
1018 {
1019     nsChannelBSC *This = NSCHANNELBSC_THIS(bsc);
1020
1021     if(This->nschannel)
1022         nsIChannel_Release(NSCHANNEL(This->nschannel));
1023     if(This->nslistener)
1024         nsIStreamListener_Release(This->nslistener);
1025     if(This->nscontext)
1026         nsISupports_Release(This->nscontext);
1027     if(This->nsstream)
1028         nsIInputStream_Release(NSINSTREAM(This->nsstream));
1029     heap_free(This);
1030 }
1031
1032 static HRESULT nsChannelBSC_start_binding(BSCallback *bsc)
1033 {
1034     nsChannelBSC *This = NSCHANNELBSC_THIS(bsc);
1035
1036     add_nsrequest(This);
1037
1038     return S_OK;
1039 }
1040
1041 static HRESULT nsChannelBSC_init_bindinfo(BSCallback *bsc)
1042 {
1043     nsChannelBSC *This = NSCHANNELBSC_THIS(bsc);
1044
1045     if(This->nschannel && This->nschannel->post_data_stream) {
1046         parse_post_data(This->nschannel->post_data_stream, &This->bsc.headers, &This->bsc.post_data, &This->bsc.post_data_len);
1047         TRACE("headers = %s post_data = %s\n", debugstr_w(This->bsc.headers),
1048               debugstr_an(This->bsc.post_data, This->bsc.post_data_len));
1049     }
1050
1051     return S_OK;
1052 }
1053
1054 static HRESULT nsChannelBSC_stop_binding(BSCallback *bsc, HRESULT result)
1055 {
1056     nsChannelBSC *This = NSCHANNELBSC_THIS(bsc);
1057
1058     on_stop_nsrequest(This, result);
1059
1060     if(This->nslistener) {
1061         if(This->nschannel->load_group) {
1062             nsresult nsres;
1063
1064             nsres = nsILoadGroup_RemoveRequest(This->nschannel->load_group,
1065                     (nsIRequest*)NSCHANNEL(This->nschannel), NULL, NS_OK);
1066             if(NS_FAILED(nsres))
1067                 ERR("RemoveRequest failed: %08x\n", nsres);
1068         }
1069     }
1070
1071     return S_OK;
1072 }
1073
1074 static HRESULT nsChannelBSC_read_data(BSCallback *bsc, IStream *stream)
1075 {
1076     nsChannelBSC *This = NSCHANNELBSC_THIS(bsc);
1077
1078     return read_stream_data(This, stream);
1079 }
1080
1081 static HRESULT nsChannelBSC_on_progress(BSCallback *bsc, ULONG status_code, LPCWSTR status_text)
1082 {
1083     nsChannelBSC *This = NSCHANNELBSC_THIS(bsc);
1084
1085     switch(status_code) {
1086     case BINDSTATUS_MIMETYPEAVAILABLE:
1087         if(!This->nschannel)
1088             return S_OK;
1089
1090         heap_free(This->nschannel->content_type);
1091         This->nschannel->content_type = heap_strdupWtoA(status_text);
1092         break;
1093     case BINDSTATUS_REDIRECTING:
1094         TRACE("redirect to %s\n", debugstr_w(status_text));
1095
1096         /* FIXME: We should find a better way to handle this */
1097         set_wine_url(This->nschannel->uri, status_text);
1098     }
1099
1100     return S_OK;
1101 }
1102
1103 static HRESULT nsChannelBSC_on_response(BSCallback *bsc, DWORD response_code,
1104         LPCWSTR response_headers)
1105 {
1106     nsChannelBSC *This = NSCHANNELBSC_THIS(bsc);
1107     HRESULT hres;
1108
1109     This->nschannel->response_status = response_code;
1110
1111     if(response_headers) {
1112         const WCHAR *hdr_start, *hdr_end;
1113
1114         hdr_start = strchrW(response_headers, '\r');
1115         while(hdr_start) {
1116             const WCHAR *colon, *value;
1117
1118             hdr_start += 2;
1119             hdr_end = strchrW(hdr_start, '\r');
1120             if(!hdr_end) {
1121                 WARN("Header doesn't end with CRLF: %s\n", wine_dbgstr_w(hdr_start));
1122                 break;
1123             }
1124             if(hdr_end == hdr_start)
1125                 break;
1126
1127             for(colon = hdr_start; *colon != ':' && colon != hdr_end; ++colon);
1128             if(*colon != ':') {
1129                 WARN("Header missing colon: %s\n", wine_dbgstr_w(hdr_start));
1130                 hdr_start = strchrW(hdr_start, '\r');
1131                 continue;
1132             }
1133
1134             value = colon+1;
1135             while(*value == ' ')
1136                 value++;
1137
1138             hres = set_http_header(&This->nschannel->response_headers, hdr_start, colon-hdr_start,
1139                     value, hdr_end-value);
1140             if(FAILED(hres))
1141                 return hres;
1142
1143             hdr_start = strchrW(hdr_start, '\r');
1144         }
1145     }
1146
1147     return S_OK;
1148 }
1149
1150 #undef NSCHANNELBSC_THIS
1151
1152 static const BSCallbackVtbl nsChannelBSCVtbl = {
1153     nsChannelBSC_destroy,
1154     nsChannelBSC_init_bindinfo,
1155     nsChannelBSC_start_binding,
1156     nsChannelBSC_stop_binding,
1157     nsChannelBSC_read_data,
1158     nsChannelBSC_on_progress,
1159     nsChannelBSC_on_response
1160 };
1161
1162 HRESULT create_channelbsc(IMoniker *mon, WCHAR *headers, BYTE *post_data, DWORD post_data_size, nsChannelBSC **retval)
1163 {
1164     nsChannelBSC *ret;
1165
1166     ret = heap_alloc_zero(sizeof(*ret));
1167     if(!ret)
1168         return E_OUTOFMEMORY;
1169
1170     init_bscallback(&ret->bsc, &nsChannelBSCVtbl, mon, BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA);
1171
1172     if(headers) {
1173         ret->bsc.headers = heap_strdupW(headers);
1174         if(!ret->bsc.headers) {
1175             IBindStatusCallback_Release(STATUSCLB(&ret->bsc));
1176             return E_OUTOFMEMORY;
1177         }
1178     }
1179
1180     if(post_data) {
1181         ret->bsc.post_data = GlobalAlloc(0, post_data_size);
1182         if(!ret->bsc.post_data) {
1183             heap_free(ret->bsc.headers);
1184             IBindStatusCallback_Release(STATUSCLB(&ret->bsc));
1185             return E_OUTOFMEMORY;
1186         }
1187
1188         memcpy(ret->bsc.post_data, post_data, post_data_size);
1189         ret->bsc.post_data_len = post_data_size;
1190     }
1191
1192     *retval = ret;
1193     return S_OK;
1194 }
1195
1196 IMoniker *get_channelbsc_mon(nsChannelBSC *This)
1197 {
1198     if(This->bsc.mon)
1199         IMoniker_AddRef(This->bsc.mon);
1200     return This->bsc.mon;
1201 }
1202
1203 void set_window_bscallback(HTMLWindow *window, nsChannelBSC *callback)
1204 {
1205     if(window->bscallback) {
1206         if(window->bscallback->bsc.binding)
1207             IBinding_Abort(window->bscallback->bsc.binding);
1208         window->bscallback->bsc.doc = NULL;
1209         window->bscallback->window = NULL;
1210         IBindStatusCallback_Release(STATUSCLB(&window->bscallback->bsc));
1211     }
1212
1213     window->bscallback = callback;
1214
1215     if(callback) {
1216         callback->window = window;
1217         IBindStatusCallback_AddRef(STATUSCLB(&callback->bsc));
1218         callback->bsc.doc = window->doc;
1219     }
1220 }
1221
1222 typedef struct {
1223     task_t header;
1224     HTMLWindow *window;
1225     nsChannelBSC *bscallback;
1226 } start_doc_binding_task_t;
1227
1228 static void start_doc_binding_proc(task_t *_task)
1229 {
1230     start_doc_binding_task_t *task = (start_doc_binding_task_t*)_task;
1231
1232     start_binding(task->window, NULL, (BSCallback*)task->bscallback, NULL);
1233     IBindStatusCallback_Release(STATUSCLB(&task->bscallback->bsc));
1234 }
1235
1236 HRESULT async_start_doc_binding(HTMLWindow *window, nsChannelBSC *bscallback)
1237 {
1238     start_doc_binding_task_t *task;
1239
1240     task = heap_alloc(sizeof(start_doc_binding_task_t));
1241     if(!task)
1242         return E_OUTOFMEMORY;
1243
1244     task->window = window;
1245     task->bscallback = bscallback;
1246     IBindStatusCallback_AddRef(STATUSCLB(&bscallback->bsc));
1247
1248     push_task(&task->header, start_doc_binding_proc, window->task_magic);
1249     return S_OK;
1250 }
1251
1252 void abort_document_bindings(HTMLDocumentNode *doc)
1253 {
1254     BSCallback *iter;
1255
1256     LIST_FOR_EACH_ENTRY(iter, &doc->bindings, BSCallback, entry) {
1257         if(iter->binding)
1258             IBinding_Abort(iter->binding);
1259         iter->doc = NULL;
1260         list_remove(&iter->entry);
1261     }
1262 }
1263
1264 HRESULT channelbsc_load_stream(nsChannelBSC *bscallback, IStream *stream)
1265 {
1266     HRESULT hres;
1267
1268     if(!bscallback->nschannel) {
1269         ERR("NULL nschannel\n");
1270         return E_FAIL;
1271     }
1272
1273     bscallback->nschannel->content_type = heap_strdupA("text/html");
1274     if(!bscallback->nschannel->content_type)
1275         return E_OUTOFMEMORY;
1276
1277     add_nsrequest(bscallback);
1278
1279     hres = read_stream_data(bscallback, stream);
1280     IBindStatusCallback_OnStopBinding(STATUSCLB(&bscallback->bsc), hres, ERROR_SUCCESS);
1281
1282     return hres;
1283 }
1284
1285 void channelbsc_set_channel(nsChannelBSC *This, nsChannel *channel, nsIStreamListener *listener, nsISupports *context)
1286 {
1287     nsIChannel_AddRef(NSCHANNEL(channel));
1288     This->nschannel = channel;
1289
1290     nsIStreamListener_AddRef(listener);
1291     This->nslistener = listener;
1292
1293     if(context) {
1294         nsISupports_AddRef(context);
1295         This->nscontext = context;
1296     }
1297 }
1298
1299 HRESULT hlink_frame_navigate(HTMLDocument *doc, LPCWSTR url,
1300         nsIInputStream *post_data_stream, DWORD hlnf, BOOL *cancel)
1301 {
1302     IHlinkFrame *hlink_frame;
1303     nsChannelBSC *callback;
1304     IServiceProvider *sp;
1305     IBindCtx *bindctx;
1306     IMoniker *mon;
1307     IHlink *hlink;
1308     HRESULT hres;
1309
1310     *cancel = FALSE;
1311
1312     hres = IOleClientSite_QueryInterface(doc->doc_obj->client, &IID_IServiceProvider,
1313             (void**)&sp);
1314     if(FAILED(hres))
1315         return S_OK;
1316
1317     hres = IServiceProvider_QueryService(sp, &IID_IHlinkFrame, &IID_IHlinkFrame,
1318             (void**)&hlink_frame);
1319     IServiceProvider_Release(sp);
1320     if(FAILED(hres))
1321         return S_OK;
1322
1323     hres = create_channelbsc(NULL, NULL, NULL, 0, &callback);
1324     if(FAILED(hres)) {
1325         IHlinkFrame_Release(hlink_frame);
1326         return hres;
1327     }
1328
1329     if(post_data_stream) {
1330         parse_post_data(post_data_stream, &callback->bsc.headers, &callback->bsc.post_data,
1331                         &callback->bsc.post_data_len);
1332         TRACE("headers = %s post_data = %s\n", debugstr_w(callback->bsc.headers),
1333               debugstr_an(callback->bsc.post_data, callback->bsc.post_data_len));
1334     }
1335
1336     hres = CreateAsyncBindCtx(0, STATUSCLB(&callback->bsc), NULL, &bindctx);
1337     if(SUCCEEDED(hres))
1338         hres = CoCreateInstance(&CLSID_StdHlink, NULL, CLSCTX_INPROC_SERVER,
1339                 &IID_IHlink, (LPVOID*)&hlink);
1340
1341     if(SUCCEEDED(hres))
1342         hres = CreateURLMoniker(NULL, url, &mon);
1343
1344     if(SUCCEEDED(hres)) {
1345         IHlink_SetMonikerReference(hlink, HLINKSETF_TARGET, mon, NULL);
1346
1347         if(hlnf & HLNF_OPENINNEWWINDOW) {
1348             static const WCHAR wszBlank[] = {'_','b','l','a','n','k',0};
1349             IHlink_SetTargetFrameName(hlink, wszBlank); /* FIXME */
1350         }
1351
1352         hres = IHlinkFrame_Navigate(hlink_frame, hlnf, bindctx, STATUSCLB(&callback->bsc), hlink);
1353         IMoniker_Release(mon);
1354         *cancel = hres == S_OK;
1355         hres = S_OK;
1356     }
1357
1358     IHlinkFrame_Release(hlink_frame);
1359     IBindCtx_Release(bindctx);
1360     IBindStatusCallback_Release(STATUSCLB(&callback->bsc));
1361     return hres;
1362 }
1363
1364 HRESULT navigate_url(HTMLWindow *window, const WCHAR *new_url, const WCHAR *base_url)
1365 {
1366     WCHAR url[INTERNET_MAX_URL_LENGTH];
1367     nsWineURI *uri;
1368     HRESULT hres;
1369
1370     if(!new_url) {
1371         *url = 0;
1372     }else if(base_url) {
1373         DWORD len = 0;
1374
1375         hres = CoInternetCombineUrl(base_url, new_url, URL_ESCAPE_SPACES_ONLY|URL_DONT_ESCAPE_EXTRA_INFO,
1376                 url, sizeof(url)/sizeof(WCHAR), &len, 0);
1377         if(FAILED(hres))
1378             return hres;
1379     }else {
1380         strcpyW(url, new_url);
1381     }
1382
1383     if(window->doc_obj && window->doc_obj->hostui) {
1384         OLECHAR *translated_url = NULL;
1385
1386         hres = IDocHostUIHandler_TranslateUrl(window->doc_obj->hostui, 0, url,
1387                 &translated_url);
1388         if(hres == S_OK) {
1389             TRACE("%08x %s -> %s\n", hres, debugstr_w(url), debugstr_w(translated_url));
1390             strcpyW(url, translated_url);
1391             CoTaskMemFree(translated_url);
1392         }
1393     }
1394
1395     if(window->doc_obj && window == window->doc_obj->basedoc.window) {
1396         BOOL cancel;
1397
1398         hres = hlink_frame_navigate(&window->doc->basedoc, url, NULL, 0, &cancel);
1399         if(FAILED(hres))
1400             return hres;
1401
1402         if(cancel) {
1403             TRACE("Navigation handled by hlink frame\n");
1404             return S_OK;
1405         }
1406     }
1407
1408     hres = create_doc_uri(window, url, &uri);
1409     if(FAILED(hres))
1410         return hres;
1411
1412     hres = load_nsuri(window, uri, NULL, LOAD_FLAGS_NONE);
1413     nsISupports_Release((nsISupports*)uri);
1414     return hres;
1415 }