mshtml: Implement IHTMLLocation get_href.
[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 "ole2.h"
31 #include "hlguids.h"
32 #include "shlguid.h"
33
34 #include "wine/debug.h"
35 #include "wine/unicode.h"
36
37 #include "mshtml_private.h"
38
39 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
40
41 #define CONTENT_LENGTH "Content-Length"
42 #define UTF16_STR "utf-16"
43
44 typedef struct {
45     const nsIInputStreamVtbl *lpInputStreamVtbl;
46
47     LONG ref;
48
49     char buf[1024];
50     DWORD buf_size;
51 } nsProtocolStream;
52
53 #define NSINSTREAM(x) ((nsIInputStream*) &(x)->lpInputStreamVtbl)
54
55 typedef struct {
56     void (*destroy)(BSCallback*);
57     HRESULT (*start_binding)(BSCallback*);
58     HRESULT (*stop_binding)(BSCallback*,HRESULT);
59     HRESULT (*read_data)(BSCallback*,IStream*);
60     HRESULT (*on_progress)(BSCallback*,ULONG,LPCWSTR);
61     HRESULT (*on_response)(BSCallback*,DWORD);
62 } BSCallbackVtbl;
63
64 struct BSCallback {
65     const IBindStatusCallbackVtbl *lpBindStatusCallbackVtbl;
66     const IServiceProviderVtbl    *lpServiceProviderVtbl;
67     const IHttpNegotiate2Vtbl     *lpHttpNegotiate2Vtbl;
68     const IInternetBindInfoVtbl   *lpInternetBindInfoVtbl;
69
70     const BSCallbackVtbl          *vtbl;
71
72     LONG ref;
73
74     LPWSTR headers;
75     HGLOBAL post_data;
76     ULONG post_data_len;
77     ULONG readed;
78     DWORD bindf;
79
80     IMoniker *mon;
81     IBinding *binding;
82
83     HTMLDocument *doc;
84
85     struct list entry;
86 };
87
88 #define NSINSTREAM_THIS(iface) DEFINE_THIS(nsProtocolStream, InputStream, iface)
89
90 static nsresult NSAPI nsInputStream_QueryInterface(nsIInputStream *iface, nsIIDRef riid,
91                                                    nsQIResult result)
92 {
93     nsProtocolStream *This = NSINSTREAM_THIS(iface);
94
95     *result = NULL;
96
97     if(IsEqualGUID(&IID_nsISupports, riid)) {
98         TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
99         *result  = NSINSTREAM(This);
100     }else if(IsEqualGUID(&IID_nsIInputStream, riid)) {
101         TRACE("(%p)->(IID_nsIInputStream %p)\n", This, result);
102         *result  = NSINSTREAM(This);
103     }
104
105     if(*result) {
106         nsIInputStream_AddRef(NSINSTREAM(This));
107         return NS_OK;
108     }
109
110     WARN("unsupported interface %s\n", debugstr_guid(riid));
111     return NS_NOINTERFACE;
112 }
113
114 static nsrefcnt NSAPI nsInputStream_AddRef(nsIInputStream *iface)
115 {
116     nsProtocolStream *This = NSINSTREAM_THIS(iface);
117     LONG ref = InterlockedIncrement(&This->ref);
118
119     TRACE("(%p) ref=%d\n", This, ref);
120
121     return ref;
122 }
123
124
125 static nsrefcnt NSAPI nsInputStream_Release(nsIInputStream *iface)
126 {
127     nsProtocolStream *This = NSINSTREAM_THIS(iface);
128     LONG ref = InterlockedDecrement(&This->ref);
129
130     TRACE("(%p) ref=%d\n", This, ref);
131
132     if(!ref)
133         heap_free(This);
134
135     return ref;
136 }
137
138 static nsresult NSAPI nsInputStream_Close(nsIInputStream *iface)
139 {
140     nsProtocolStream *This = NSINSTREAM_THIS(iface);
141     FIXME("(%p)\n", This);
142     return NS_ERROR_NOT_IMPLEMENTED;
143 }
144
145 static nsresult NSAPI nsInputStream_Available(nsIInputStream *iface, PRUint32 *_retval)
146 {
147     nsProtocolStream *This = NSINSTREAM_THIS(iface);
148     FIXME("(%p)->(%p)\n", This, _retval);
149     return NS_ERROR_NOT_IMPLEMENTED;
150 }
151
152 static nsresult NSAPI nsInputStream_Read(nsIInputStream *iface, char *aBuf, PRUint32 aCount,
153                                          PRUint32 *_retval)
154 {
155     nsProtocolStream *This = NSINSTREAM_THIS(iface);
156     DWORD read = aCount;
157
158     TRACE("(%p)->(%p %d %p)\n", This, aBuf, aCount, _retval);
159
160     if(read > This->buf_size)
161         read = This->buf_size;
162
163     if(read) {
164         memcpy(aBuf, This->buf, read);
165         if(read < This->buf_size)
166             memmove(This->buf, This->buf+read, This->buf_size-read);
167         This->buf_size -= read;
168     }
169
170     *_retval = read;
171     return NS_OK;
172 }
173
174 static nsresult NSAPI nsInputStream_ReadSegments(nsIInputStream *iface,
175         nsresult (WINAPI *aWriter)(nsIInputStream*,void*,const char*,PRUint32,PRUint32,PRUint32*),
176         void *aClousure, PRUint32 aCount, PRUint32 *_retval)
177 {
178     nsProtocolStream *This = NSINSTREAM_THIS(iface);
179     PRUint32 written = 0;
180     nsresult nsres;
181
182     TRACE("(%p)->(%p %p %d %p)\n", This, aWriter, aClousure, aCount, _retval);
183
184     if(!This->buf_size)
185         return S_OK;
186
187     if(aCount > This->buf_size)
188         aCount = This->buf_size;
189
190     nsres = aWriter(NSINSTREAM(This), aClousure, This->buf, 0, aCount, &written);
191     if(NS_FAILED(nsres))
192         TRACE("aWritter failed: %08x\n", nsres);
193     else if(written != This->buf_size)
194         FIXME("written %d != buf_size %d\n", written, This->buf_size);
195
196     This->buf_size -= written; 
197
198     *_retval = written;
199     return nsres;
200 }
201
202 static nsresult NSAPI nsInputStream_IsNonBlocking(nsIInputStream *iface, PRBool *_retval)
203 {
204     nsProtocolStream *This = NSINSTREAM_THIS(iface);
205     FIXME("(%p)->(%p)\n", This, _retval);
206     return NS_ERROR_NOT_IMPLEMENTED;
207 }
208
209 #undef NSINSTREAM_THIS
210
211 static const nsIInputStreamVtbl nsInputStreamVtbl = {
212     nsInputStream_QueryInterface,
213     nsInputStream_AddRef,
214     nsInputStream_Release,
215     nsInputStream_Close,
216     nsInputStream_Available,
217     nsInputStream_Read,
218     nsInputStream_ReadSegments,
219     nsInputStream_IsNonBlocking
220 };
221
222 static nsProtocolStream *create_nsprotocol_stream(void)
223 {
224     nsProtocolStream *ret = heap_alloc(sizeof(nsProtocolStream));
225
226     ret->lpInputStreamVtbl = &nsInputStreamVtbl;
227     ret->ref = 1;
228     ret->buf_size = 0;
229
230     return ret;
231 }
232
233 #define STATUSCLB_THIS(iface) DEFINE_THIS(BSCallback, BindStatusCallback, iface)
234
235 static HRESULT WINAPI BindStatusCallback_QueryInterface(IBindStatusCallback *iface,
236         REFIID riid, void **ppv)
237 {
238     BSCallback *This = STATUSCLB_THIS(iface);
239
240     *ppv = NULL;
241     if(IsEqualGUID(&IID_IUnknown, riid)) {
242         TRACE("(%p)->(IID_IUnknown, %p)\n", This, ppv);
243         *ppv = STATUSCLB(This);
244     }else if(IsEqualGUID(&IID_IBindStatusCallback, riid)) {
245         TRACE("(%p)->(IID_IBindStatusCallback, %p)\n", This, ppv);
246         *ppv = STATUSCLB(This);
247     }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
248         TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
249         *ppv = SERVPROV(This);
250     }else if(IsEqualGUID(&IID_IHttpNegotiate, riid)) {
251         TRACE("(%p)->(IID_IHttpNegotiate %p)\n", This, ppv);
252         *ppv = HTTPNEG(This);
253     }else if(IsEqualGUID(&IID_IHttpNegotiate2, riid)) {
254         TRACE("(%p)->(IID_IHttpNegotiate2 %p)\n", This, ppv);
255         *ppv = HTTPNEG(This);
256     }else if(IsEqualGUID(&IID_IInternetBindInfo, riid)) {
257         TRACE("(%p)->(IID_IInternetBindInfo %p)\n", This, ppv);
258         *ppv = BINDINFO(This);
259     }
260
261     if(*ppv) {
262         IBindStatusCallback_AddRef(STATUSCLB(This));
263         return S_OK;
264     }
265
266     TRACE("Unsupported riid = %s\n", debugstr_guid(riid));
267     return E_NOINTERFACE;
268 }
269
270 static ULONG WINAPI BindStatusCallback_AddRef(IBindStatusCallback *iface)
271 {
272     BSCallback *This = STATUSCLB_THIS(iface);
273     LONG ref = InterlockedIncrement(&This->ref);
274
275     TRACE("(%p) ref = %d\n", This, ref);
276
277     return ref;
278 }
279
280 static ULONG WINAPI BindStatusCallback_Release(IBindStatusCallback *iface)
281 {
282     BSCallback *This = STATUSCLB_THIS(iface);
283     LONG ref = InterlockedDecrement(&This->ref);
284
285     TRACE("(%p) ref = %d\n", This, ref);
286
287     if(!ref) {
288         if(This->post_data)
289             GlobalFree(This->post_data);
290         if(This->mon)
291             IMoniker_Release(This->mon);
292         if(This->binding)
293             IBinding_Release(This->binding);
294         list_remove(&This->entry);
295         heap_free(This->headers);
296
297         This->vtbl->destroy(This);
298     }
299
300     return ref;
301 }
302
303 static HRESULT WINAPI BindStatusCallback_OnStartBinding(IBindStatusCallback *iface,
304         DWORD dwReserved, IBinding *pbind)
305 {
306     BSCallback *This = STATUSCLB_THIS(iface);
307
308     TRACE("(%p)->(%d %p)\n", This, dwReserved, pbind);
309
310     IBinding_AddRef(pbind);
311     This->binding = pbind;
312
313     if(This->doc)
314         list_add_head(&This->doc->bindings, &This->entry);
315
316     return This->vtbl->start_binding(This);
317 }
318
319 static HRESULT WINAPI BindStatusCallback_GetPriority(IBindStatusCallback *iface, LONG *pnPriority)
320 {
321     BSCallback *This = STATUSCLB_THIS(iface);
322     FIXME("(%p)->(%p)\n", This, pnPriority);
323     return E_NOTIMPL;
324 }
325
326 static HRESULT WINAPI BindStatusCallback_OnLowResource(IBindStatusCallback *iface, DWORD reserved)
327 {
328     BSCallback *This = STATUSCLB_THIS(iface);
329     FIXME("(%p)->(%d)\n", This, reserved);
330     return E_NOTIMPL;
331 }
332
333 static HRESULT WINAPI BindStatusCallback_OnProgress(IBindStatusCallback *iface, ULONG ulProgress,
334         ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
335 {
336     BSCallback *This = STATUSCLB_THIS(iface);
337
338     TRACE("%p)->(%u %u %u %s)\n", This, ulProgress, ulProgressMax, ulStatusCode,
339             debugstr_w(szStatusText));
340
341     return This->vtbl->on_progress(This, ulStatusCode, szStatusText);
342 }
343
344 static HRESULT WINAPI BindStatusCallback_OnStopBinding(IBindStatusCallback *iface,
345         HRESULT hresult, LPCWSTR szError)
346 {
347     BSCallback *This = STATUSCLB_THIS(iface);
348     HRESULT hres;
349
350     TRACE("(%p)->(%08x %s)\n", This, hresult, debugstr_w(szError));
351
352     /* NOTE: IE7 calls GetBindResult here */
353
354     hres = This->vtbl->stop_binding(This, hresult);
355
356     if(This->binding) {
357         IBinding_Release(This->binding);
358         This->binding = NULL;
359     }
360
361     list_remove(&This->entry);
362     This->doc = NULL;
363
364     return hres;
365 }
366
367 static HRESULT WINAPI BindStatusCallback_GetBindInfo(IBindStatusCallback *iface,
368         DWORD *grfBINDF, BINDINFO *pbindinfo)
369 {
370     BSCallback *This = STATUSCLB_THIS(iface);
371     DWORD size;
372
373     TRACE("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
374
375     *grfBINDF = This->bindf;
376
377     size = pbindinfo->cbSize;
378     memset(pbindinfo, 0, size);
379     pbindinfo->cbSize = size;
380
381     pbindinfo->cbstgmedData = This->post_data_len;
382     pbindinfo->dwCodePage = CP_UTF8;
383     pbindinfo->dwOptions = 0x80000;
384
385     if(This->post_data) {
386         pbindinfo->dwBindVerb = BINDVERB_POST;
387
388         pbindinfo->stgmedData.tymed = TYMED_HGLOBAL;
389         pbindinfo->stgmedData.u.hGlobal = This->post_data;
390         pbindinfo->stgmedData.pUnkForRelease = (IUnknown*)STATUSCLB(This);
391         IBindStatusCallback_AddRef(STATUSCLB(This));
392     }
393
394     return S_OK;
395 }
396
397 static HRESULT WINAPI BindStatusCallback_OnDataAvailable(IBindStatusCallback *iface,
398         DWORD grfBSCF, DWORD dwSize, FORMATETC *pformatetc, STGMEDIUM *pstgmed)
399 {
400     BSCallback *This = STATUSCLB_THIS(iface);
401
402     TRACE("(%p)->(%08x %d %p %p)\n", This, grfBSCF, dwSize, pformatetc, pstgmed);
403
404     return This->vtbl->read_data(This, pstgmed->u.pstm);
405 }
406
407 static HRESULT WINAPI BindStatusCallback_OnObjectAvailable(IBindStatusCallback *iface,
408         REFIID riid, IUnknown *punk)
409 {
410     BSCallback *This = STATUSCLB_THIS(iface);
411     FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), punk);
412     return E_NOTIMPL;
413 }
414
415 #undef STATUSCLB_THIS
416
417 static const IBindStatusCallbackVtbl BindStatusCallbackVtbl = {
418     BindStatusCallback_QueryInterface,
419     BindStatusCallback_AddRef,
420     BindStatusCallback_Release,
421     BindStatusCallback_OnStartBinding,
422     BindStatusCallback_GetPriority,
423     BindStatusCallback_OnLowResource,
424     BindStatusCallback_OnProgress,
425     BindStatusCallback_OnStopBinding,
426     BindStatusCallback_GetBindInfo,
427     BindStatusCallback_OnDataAvailable,
428     BindStatusCallback_OnObjectAvailable
429 };
430
431 #define HTTPNEG_THIS(iface) DEFINE_THIS(BSCallback, HttpNegotiate2, iface)
432
433 static HRESULT WINAPI HttpNegotiate_QueryInterface(IHttpNegotiate2 *iface,
434                                                    REFIID riid, void **ppv)
435 {
436     BSCallback *This = HTTPNEG_THIS(iface);
437     return IBindStatusCallback_QueryInterface(STATUSCLB(This), riid, ppv);
438 }
439
440 static ULONG WINAPI HttpNegotiate_AddRef(IHttpNegotiate2 *iface)
441 {
442     BSCallback *This = HTTPNEG_THIS(iface);
443     return IBindStatusCallback_AddRef(STATUSCLB(This));
444 }
445
446 static ULONG WINAPI HttpNegotiate_Release(IHttpNegotiate2 *iface)
447 {
448     BSCallback *This = HTTPNEG_THIS(iface);
449     return IBindStatusCallback_Release(STATUSCLB(This));
450 }
451
452 static HRESULT WINAPI HttpNegotiate_BeginningTransaction(IHttpNegotiate2 *iface,
453         LPCWSTR szURL, LPCWSTR szHeaders, DWORD dwReserved, LPWSTR *pszAdditionalHeaders)
454 {
455     BSCallback *This = HTTPNEG_THIS(iface);
456     DWORD size;
457
458     TRACE("(%p)->(%s %s %d %p)\n", This, debugstr_w(szURL), debugstr_w(szHeaders),
459           dwReserved, pszAdditionalHeaders);
460
461     if(!This->headers) {
462         *pszAdditionalHeaders = NULL;
463         return S_OK;
464     }
465
466     size = (strlenW(This->headers)+1)*sizeof(WCHAR);
467     *pszAdditionalHeaders = CoTaskMemAlloc(size);
468     memcpy(*pszAdditionalHeaders, This->headers, size);
469
470     return S_OK;
471 }
472
473 static HRESULT WINAPI HttpNegotiate_OnResponse(IHttpNegotiate2 *iface, DWORD dwResponseCode,
474         LPCWSTR szResponseHeaders, LPCWSTR szRequestHeaders, LPWSTR *pszAdditionalRequestHeaders)
475 {
476     BSCallback *This = HTTPNEG_THIS(iface);
477
478     TRACE("(%p)->(%d %s %s %p)\n", This, dwResponseCode, debugstr_w(szResponseHeaders),
479           debugstr_w(szRequestHeaders), pszAdditionalRequestHeaders);
480
481     return This->vtbl->on_response(This, dwResponseCode);
482 }
483
484 static HRESULT WINAPI HttpNegotiate_GetRootSecurityId(IHttpNegotiate2 *iface,
485         BYTE *pbSecurityId, DWORD *pcbSecurityId, DWORD_PTR dwReserved)
486 {
487     BSCallback *This = HTTPNEG_THIS(iface);
488     FIXME("(%p)->(%p %p %ld)\n", This, pbSecurityId, pcbSecurityId, dwReserved);
489     return E_NOTIMPL;
490 }
491
492 #undef HTTPNEG
493
494 static const IHttpNegotiate2Vtbl HttpNegotiate2Vtbl = {
495     HttpNegotiate_QueryInterface,
496     HttpNegotiate_AddRef,
497     HttpNegotiate_Release,
498     HttpNegotiate_BeginningTransaction,
499     HttpNegotiate_OnResponse,
500     HttpNegotiate_GetRootSecurityId
501 };
502
503 #define BINDINFO_THIS(iface) DEFINE_THIS(BSCallback, InternetBindInfo, iface)
504
505 static HRESULT WINAPI InternetBindInfo_QueryInterface(IInternetBindInfo *iface,
506                                                       REFIID riid, void **ppv)
507 {
508     BSCallback *This = BINDINFO_THIS(iface);
509     return IBindStatusCallback_QueryInterface(STATUSCLB(This), riid, ppv);
510 }
511
512 static ULONG WINAPI InternetBindInfo_AddRef(IInternetBindInfo *iface)
513 {
514     BSCallback *This = BINDINFO_THIS(iface);
515     return IBindStatusCallback_AddRef(STATUSCLB(This));
516 }
517
518 static ULONG WINAPI InternetBindInfo_Release(IInternetBindInfo *iface)
519 {
520     BSCallback *This = BINDINFO_THIS(iface);
521     return IBindStatusCallback_Release(STATUSCLB(This));
522 }
523
524 static HRESULT WINAPI InternetBindInfo_GetBindInfo(IInternetBindInfo *iface,
525                                                    DWORD *grfBINDF, BINDINFO *pbindinfo)
526 {
527     BSCallback *This = BINDINFO_THIS(iface);
528     FIXME("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
529     return E_NOTIMPL;
530 }
531
532 static HRESULT WINAPI InternetBindInfo_GetBindString(IInternetBindInfo *iface,
533         ULONG ulStringType, LPOLESTR *ppwzStr, ULONG cEl, ULONG *pcElFetched)
534 {
535     BSCallback *This = BINDINFO_THIS(iface);
536     FIXME("(%p)->(%u %p %u %p)\n", This, ulStringType, ppwzStr, cEl, pcElFetched);
537     return E_NOTIMPL;
538 }
539
540 #undef BINDINFO_THIS
541
542 static const IInternetBindInfoVtbl InternetBindInfoVtbl = {
543     InternetBindInfo_QueryInterface,
544     InternetBindInfo_AddRef,
545     InternetBindInfo_Release,
546     InternetBindInfo_GetBindInfo,
547     InternetBindInfo_GetBindString
548 };
549
550 #define SERVPROV_THIS(iface) DEFINE_THIS(BSCallback, ServiceProvider, iface)
551
552 static HRESULT WINAPI BSCServiceProvider_QueryInterface(IServiceProvider *iface,
553                                                         REFIID riid, void **ppv)
554 {
555     BSCallback *This = SERVPROV_THIS(iface);
556     return IBindStatusCallback_QueryInterface(STATUSCLB(This), riid, ppv);
557 }
558
559 static ULONG WINAPI BSCServiceProvider_AddRef(IServiceProvider *iface)
560 {
561     BSCallback *This = SERVPROV_THIS(iface);
562     return IBindStatusCallback_AddRef(STATUSCLB(This));
563 }
564
565 static ULONG WINAPI BSCServiceProvider_Release(IServiceProvider *iface)
566 {
567     BSCallback *This = SERVPROV_THIS(iface);
568     return IBindStatusCallback_Release(STATUSCLB(This));
569 }
570
571 static HRESULT WINAPI BSCServiceProvider_QueryService(IServiceProvider *iface,
572         REFGUID guidService, REFIID riid, void **ppv)
573 {
574     BSCallback *This = SERVPROV_THIS(iface);
575     TRACE("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
576     return E_NOINTERFACE;
577 }
578
579 #undef SERVPROV_THIS
580
581 static const IServiceProviderVtbl ServiceProviderVtbl = {
582     BSCServiceProvider_QueryInterface,
583     BSCServiceProvider_AddRef,
584     BSCServiceProvider_Release,
585     BSCServiceProvider_QueryService
586 };
587
588 static void init_bscallback(BSCallback *This, const BSCallbackVtbl *vtbl, IMoniker *mon, DWORD bindf)
589 {
590     This->lpBindStatusCallbackVtbl = &BindStatusCallbackVtbl;
591     This->lpServiceProviderVtbl    = &ServiceProviderVtbl;
592     This->lpHttpNegotiate2Vtbl     = &HttpNegotiate2Vtbl;
593     This->lpInternetBindInfoVtbl   = &InternetBindInfoVtbl;
594     This->vtbl = vtbl;
595     This->ref = 1;
596     This->bindf = bindf;
597
598     list_init(&This->entry);
599
600     if(mon)
601         IMoniker_AddRef(mon);
602     This->mon = mon;
603 }
604
605 /* Calls undocumented 84 cmd of CGID_ShellDocView */
606 static void call_docview_84(HTMLDocument *doc)
607 {
608     IOleCommandTarget *olecmd;
609     VARIANT var;
610     HRESULT hres;
611
612     if(!doc->client)
613         return;
614
615     hres = IOleClientSite_QueryInterface(doc->client, &IID_IOleCommandTarget, (void**)&olecmd);
616     if(FAILED(hres))
617         return;
618
619     VariantInit(&var);
620     hres = IOleCommandTarget_Exec(olecmd, &CGID_ShellDocView, 84, 0, NULL, &var);
621     IOleCommandTarget_Release(olecmd);
622     if(SUCCEEDED(hres) && V_VT(&var) != VT_NULL)
623         FIXME("handle result\n");
624 }
625
626 static void parse_post_data(nsIInputStream *post_data_stream, LPWSTR *headers_ret,
627                             HGLOBAL *post_data_ret, ULONG *post_data_len_ret)
628 {
629     PRUint32 post_data_len = 0, available = 0;
630     HGLOBAL post_data = NULL;
631     LPWSTR headers = NULL;
632     DWORD headers_len = 0, len;
633     const char *ptr, *ptr2, *post_data_end;
634
635     nsIInputStream_Available(post_data_stream, &available);
636     post_data = GlobalAlloc(0, available+1);
637     nsIInputStream_Read(post_data_stream, post_data, available, &post_data_len);
638     
639     TRACE("post_data = %s\n", debugstr_an(post_data, post_data_len));
640
641     ptr = ptr2 = post_data;
642     post_data_end = (const char*)post_data+post_data_len;
643
644     while(ptr < post_data_end && (*ptr != '\r' || ptr[1] != '\n')) {
645         while(ptr < post_data_end && (*ptr != '\r' || ptr[1] != '\n'))
646             ptr++;
647
648         if(!*ptr) {
649             FIXME("*ptr = 0\n");
650             return;
651         }
652
653         ptr += 2;
654
655         if(ptr-ptr2 >= sizeof(CONTENT_LENGTH)
656            && CompareStringA(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
657                              CONTENT_LENGTH, sizeof(CONTENT_LENGTH)-1,
658                              ptr2, sizeof(CONTENT_LENGTH)-1) == CSTR_EQUAL) {
659             ptr2 = ptr;
660             continue;
661         }
662
663         len = MultiByteToWideChar(CP_ACP, 0, ptr2, ptr-ptr2, NULL, 0);
664
665         if(headers)
666             headers = heap_realloc(headers,(headers_len+len+1)*sizeof(WCHAR));
667         else
668             headers = heap_alloc((len+1)*sizeof(WCHAR));
669
670         len = MultiByteToWideChar(CP_ACP, 0, ptr2, ptr-ptr2, headers+headers_len, len);
671         headers_len += len;
672
673         ptr2 = ptr;
674     }
675
676     headers[headers_len] = 0;
677     *headers_ret = headers;
678
679     if(ptr >= post_data_end-2) {
680         GlobalFree(post_data);
681         return;
682     }
683
684     ptr += 2;
685
686     if(headers_len) {
687         post_data_len -= ptr-(const char*)post_data;
688         memmove(post_data, ptr, post_data_len);
689         post_data = GlobalReAlloc(post_data, post_data_len+1, 0);
690     }
691
692     *post_data_ret = post_data;
693     *post_data_len_ret = post_data_len;
694 }
695
696 HRESULT start_binding(HTMLDocument *doc, BSCallback *bscallback, IBindCtx *bctx)
697 {
698     IStream *str = NULL;
699     HRESULT hres;
700
701     bscallback->doc = doc;
702
703     /* NOTE: IE7 calls IsSystemMoniker here*/
704
705     call_docview_84(doc);
706
707     if(bctx) {
708         RegisterBindStatusCallback(bctx, STATUSCLB(bscallback), NULL, 0);
709         IBindCtx_AddRef(bctx);
710     }else {
711         hres = CreateAsyncBindCtx(0, STATUSCLB(bscallback), NULL, &bctx);
712         if(FAILED(hres)) {
713             WARN("CreateAsyncBindCtx failed: %08x\n", hres);
714             bscallback->vtbl->stop_binding(bscallback, hres);
715             return hres;
716         }
717     }
718
719     hres = IMoniker_BindToStorage(bscallback->mon, bctx, NULL, &IID_IStream, (void**)&str);
720     IBindCtx_Release(bctx);
721     if(FAILED(hres)) {
722         WARN("BindToStorage failed: %08x\n", hres);
723         bscallback->vtbl->stop_binding(bscallback, hres);
724         return hres;
725     }
726
727     if(str)
728         IStream_Release(str);
729
730     IMoniker_Release(bscallback->mon);
731     bscallback->mon = NULL;
732
733     return S_OK;
734 }
735
736 typedef struct {
737     BSCallback bsc;
738
739     DWORD size;
740     BYTE *buf;
741     HRESULT hres;
742 } BufferBSC;
743
744 #define BUFFERBSC_THIS(bsc) ((BufferBSC*) bsc)
745
746 static void BufferBSC_destroy(BSCallback *bsc)
747 {
748     BufferBSC *This = BUFFERBSC_THIS(bsc);
749
750     heap_free(This->buf);
751     heap_free(This);
752 }
753
754 static HRESULT BufferBSC_start_binding(BSCallback *bsc)
755 {
756     return S_OK;
757 }
758
759 static HRESULT BufferBSC_stop_binding(BSCallback *bsc, HRESULT result)
760 {
761     BufferBSC *This = BUFFERBSC_THIS(bsc);
762
763     This->hres = result;
764
765     if(FAILED(result)) {
766         heap_free(This->buf);
767         This->buf = NULL;
768         This->size = 0;
769     }
770
771     return S_OK;
772 }
773
774 static HRESULT BufferBSC_read_data(BSCallback *bsc, IStream *stream)
775 {
776     BufferBSC *This = BUFFERBSC_THIS(bsc);
777     DWORD readed;
778     HRESULT hres;
779
780     if(!This->buf) {
781         This->size = 128;
782         This->buf = heap_alloc(This->size);
783     }
784
785     do {
786         if(This->bsc.readed == This->size) {
787             This->size <<= 1;
788             This->buf = heap_realloc(This->buf, This->size);
789         }
790
791         readed = 0;
792         hres = IStream_Read(stream, This->buf+This->bsc.readed, This->size-This->bsc.readed, &readed);
793         This->bsc.readed += readed;
794     }while(hres == S_OK);
795
796     return S_OK;
797 }
798
799 static HRESULT BufferBSC_on_progress(BSCallback *bsc, ULONG status_code, LPCWSTR status_text)
800 {
801     return S_OK;
802 }
803
804 static HRESULT BufferBSC_on_response(BSCallback *bsc, DWORD response_code)
805 {
806     return S_OK;
807 }
808
809 #undef BUFFERBSC_THIS
810
811 static const BSCallbackVtbl BufferBSCVtbl = {
812     BufferBSC_destroy,
813     BufferBSC_start_binding,
814     BufferBSC_stop_binding,
815     BufferBSC_read_data,
816     BufferBSC_on_progress,
817     BufferBSC_on_response
818 };
819
820
821 static BufferBSC *create_bufferbsc(IMoniker *mon)
822 {
823     BufferBSC *ret = heap_alloc_zero(sizeof(*ret));
824
825     init_bscallback(&ret->bsc, &BufferBSCVtbl, mon, 0);
826     ret->hres = E_FAIL;
827
828     return ret;
829 }
830
831 HRESULT bind_mon_to_buffer(HTMLDocument *doc, IMoniker *mon, void **buf, DWORD *size)
832 {
833     BufferBSC *bsc = create_bufferbsc(mon);
834     HRESULT hres;
835
836     *buf = NULL;
837
838     hres = start_binding(doc, &bsc->bsc, NULL);
839     if(SUCCEEDED(hres)) {
840         hres = bsc->hres;
841         if(SUCCEEDED(hres)) {
842             *buf = bsc->buf;
843             bsc->buf = NULL;
844             *size = bsc->bsc.readed;
845             bsc->size = 0;
846         }
847     }
848
849     IBindStatusCallback_Release(STATUSCLB(&bsc->bsc));
850
851     return hres;
852 }
853
854 struct nsChannelBSC {
855     BSCallback bsc;
856
857     nsChannel *nschannel;
858     nsIStreamListener *nslistener;
859     nsISupports *nscontext;
860
861     nsProtocolStream *nsstream;
862 };
863
864 static HRESULT read_stream_data(nsChannelBSC *This, IStream *stream)
865 {
866     DWORD read;
867     nsresult nsres;
868     HRESULT hres;
869
870     if(!This->nslistener) {
871         BYTE buf[1024];
872
873         do {
874             read = 0;
875             hres = IStream_Read(stream, buf, sizeof(buf), &read);
876         }while(hres == S_OK && read);
877
878         return S_OK;
879     }
880
881     if(!This->nsstream)
882         This->nsstream = create_nsprotocol_stream();
883
884     do {
885         read = 0;
886         hres = IStream_Read(stream, This->nsstream->buf+This->nsstream->buf_size,
887                 sizeof(This->nsstream->buf)-This->nsstream->buf_size, &read);
888         if(!read)
889             break;
890
891         This->nsstream->buf_size += read;
892
893         if(!This->bsc.readed) {
894             if(This->nsstream->buf_size >= 2
895                && (BYTE)This->nsstream->buf[0] == 0xff
896                && (BYTE)This->nsstream->buf[1] == 0xfe)
897                 This->nschannel->charset = heap_strdupA(UTF16_STR);
898
899             /* FIXME: it's needed for http connections from BindToObject. */
900             if(!This->nschannel->response_status)
901                 This->nschannel->response_status = 200;
902
903             nsres = nsIStreamListener_OnStartRequest(This->nslistener,
904                     (nsIRequest*)NSCHANNEL(This->nschannel), This->nscontext);
905             if(NS_FAILED(nsres))
906                 FIXME("OnStartRequest failed: %08x\n", nsres);
907
908             /* events are reset when a new document URI is loaded, so re-initialise them here */
909             if(This->bsc.doc && This->bsc.doc->bscallback == This && This->bsc.doc->nscontainer) {
910                 update_nsdocument(This->bsc.doc);
911                 init_nsevents(This->bsc.doc->nscontainer);
912             }
913         }
914
915         This->bsc.readed += This->nsstream->buf_size;
916
917         nsres = nsIStreamListener_OnDataAvailable(This->nslistener,
918                 (nsIRequest*)NSCHANNEL(This->nschannel), This->nscontext,
919                 NSINSTREAM(This->nsstream), This->bsc.readed-This->nsstream->buf_size,
920                 This->nsstream->buf_size);
921         if(NS_FAILED(nsres))
922             ERR("OnDataAvailable failed: %08x\n", nsres);
923
924         if(This->nsstream->buf_size == sizeof(This->nsstream->buf)) {
925             ERR("buffer is full\n");
926             break;
927         }
928     }while(hres == S_OK);
929
930     return S_OK;
931 }
932
933 static void on_stop_nsrequest(nsChannelBSC *This)
934 {
935     if(!This->nslistener)
936         return;
937
938     nsIStreamListener_OnStopRequest(This->nslistener, (nsIRequest*)NSCHANNEL(This->nschannel),
939             This->nscontext, NS_OK);
940 }
941
942 static void add_nsrequest(nsChannelBSC *This)
943 {
944     nsresult nsres;
945
946     if(!This->nschannel || !This->nschannel->load_group)
947         return;
948
949     nsres = nsILoadGroup_AddRequest(This->nschannel->load_group,
950             (nsIRequest*)NSCHANNEL(This->nschannel), This->nscontext);
951
952     if(NS_FAILED(nsres))
953         ERR("AddRequest failed:%08x\n", nsres);
954 }
955
956 #define NSCHANNELBSC_THIS(bsc) ((nsChannelBSC*) bsc)
957
958 static void nsChannelBSC_destroy(BSCallback *bsc)
959 {
960     nsChannelBSC *This = NSCHANNELBSC_THIS(bsc);
961
962     if(This->nschannel)
963         nsIChannel_Release(NSCHANNEL(This->nschannel));
964     if(This->nslistener)
965         nsIStreamListener_Release(This->nslistener);
966     if(This->nscontext)
967         nsISupports_Release(This->nscontext);
968     if(This->nsstream)
969         nsIInputStream_Release(NSINSTREAM(This->nsstream));
970     heap_free(This);
971 }
972
973 static HRESULT nsChannelBSC_start_binding(BSCallback *bsc)
974 {
975     nsChannelBSC *This = NSCHANNELBSC_THIS(bsc);
976
977     add_nsrequest(This);
978
979     return S_OK;
980 }
981
982 static HRESULT nsChannelBSC_stop_binding(BSCallback *bsc, HRESULT result)
983 {
984     nsChannelBSC *This = NSCHANNELBSC_THIS(bsc);
985
986     on_stop_nsrequest(This);
987
988     if(This->nslistener) {
989         if(This->nschannel->load_group) {
990             nsresult nsres;
991
992             nsres = nsILoadGroup_RemoveRequest(This->nschannel->load_group,
993                     (nsIRequest*)NSCHANNEL(This->nschannel), NULL, NS_OK);
994             if(NS_FAILED(nsres))
995                 ERR("RemoveRequest failed: %08x\n", nsres);
996         }
997     }
998
999     return S_OK;
1000 }
1001
1002 static HRESULT nsChannelBSC_read_data(BSCallback *bsc, IStream *stream)
1003 {
1004     nsChannelBSC *This = NSCHANNELBSC_THIS(bsc);
1005
1006     return read_stream_data(This, stream);
1007 }
1008
1009 static HRESULT nsChannelBSC_on_progress(BSCallback *bsc, ULONG status_code, LPCWSTR status_text)
1010 {
1011     nsChannelBSC *This = NSCHANNELBSC_THIS(bsc);
1012
1013     switch(status_code) {
1014     case BINDSTATUS_MIMETYPEAVAILABLE:
1015         if(!This->nschannel)
1016             return S_OK;
1017
1018         heap_free(This->nschannel->content_type);
1019         This->nschannel->content_type = heap_strdupWtoA(status_text);
1020     }
1021
1022     return S_OK;
1023 }
1024
1025 static HRESULT nsChannelBSC_on_response(BSCallback *bsc, DWORD response_code)
1026 {
1027     nsChannelBSC *This = NSCHANNELBSC_THIS(bsc);
1028
1029     This->nschannel->response_status = response_code;
1030     return S_OK;
1031 }
1032
1033 #undef NSCHANNELBSC_THIS
1034
1035 static const BSCallbackVtbl nsChannelBSCVtbl = {
1036     nsChannelBSC_destroy,
1037     nsChannelBSC_start_binding,
1038     nsChannelBSC_stop_binding,
1039     nsChannelBSC_read_data,
1040     nsChannelBSC_on_progress,
1041     nsChannelBSC_on_response
1042 };
1043
1044 nsChannelBSC *create_channelbsc(IMoniker *mon)
1045 {
1046     nsChannelBSC *ret = heap_alloc_zero(sizeof(*ret));
1047
1048     init_bscallback(&ret->bsc, &nsChannelBSCVtbl, mon, BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA);
1049
1050     return ret;
1051 }
1052
1053 IMoniker *get_channelbsc_mon(nsChannelBSC *This)
1054 {
1055     if(This->bsc.mon)
1056         IMoniker_AddRef(This->bsc.mon);
1057     return This->bsc.mon;
1058 }
1059
1060 void set_document_bscallback(HTMLDocument *doc, nsChannelBSC *callback)
1061 {
1062     BSCallback *iter;
1063
1064     if(doc->bscallback) {
1065         if(doc->bscallback->bsc.binding)
1066             IBinding_Abort(doc->bscallback->bsc.binding);
1067         doc->bscallback->bsc.doc = NULL;
1068         IBindStatusCallback_Release(STATUSCLB(&doc->bscallback->bsc));
1069     }
1070
1071     LIST_FOR_EACH_ENTRY(iter, &doc->bindings, BSCallback, entry) {
1072         iter->doc = NULL;
1073         list_remove(&iter->entry);
1074     }
1075
1076     doc->bscallback = callback;
1077
1078     if(callback) {
1079         IBindStatusCallback_AddRef(STATUSCLB(&callback->bsc));
1080         callback->bsc.doc = doc;
1081     }
1082 }
1083
1084 HRESULT channelbsc_load_stream(nsChannelBSC *bscallback, IStream *stream)
1085 {
1086     HRESULT hres;
1087
1088     const char text_html[] = "text/html";
1089
1090     add_nsrequest(bscallback);
1091
1092     if(bscallback->nschannel)
1093         bscallback->nschannel->content_type = heap_strdupA(text_html);
1094
1095     hres = read_stream_data(bscallback, stream);
1096     IBindStatusCallback_OnStopBinding(STATUSCLB(&bscallback->bsc), hres, ERROR_SUCCESS);
1097
1098     return hres;
1099 }
1100
1101 void channelbsc_set_channel(nsChannelBSC *This, nsChannel *channel, nsIStreamListener *listener, nsISupports *context)
1102 {
1103     nsIChannel_AddRef(NSCHANNEL(channel));
1104     This->nschannel = channel;
1105
1106     nsIStreamListener_AddRef(listener);
1107     This->nslistener = listener;
1108
1109     if(context) {
1110         nsISupports_AddRef(context);
1111         This->nscontext = context;
1112     }
1113
1114     if(channel->post_data_stream) {
1115         parse_post_data(channel->post_data_stream, &This->bsc.headers, &This->bsc.post_data, &This->bsc.post_data_len);
1116         TRACE("headers = %s post_data = %s\n", debugstr_w(This->bsc.headers),
1117               debugstr_an(This->bsc.post_data, This->bsc.post_data_len));
1118     }
1119 }
1120
1121 void hlink_frame_navigate(HTMLDocument *doc, IHlinkFrame *hlink_frame,
1122                           LPCWSTR uri, nsIInputStream *post_data_stream, DWORD hlnf)
1123 {
1124     BSCallback *callback;
1125     IBindCtx *bindctx;
1126     IMoniker *mon;
1127     IHlink *hlink;
1128     HRESULT hr;
1129
1130     callback = &create_channelbsc(NULL)->bsc;
1131
1132     if(post_data_stream) {
1133         parse_post_data(post_data_stream, &callback->headers, &callback->post_data,
1134                         &callback->post_data_len);
1135         TRACE("headers = %s post_data = %s\n", debugstr_w(callback->headers),
1136               debugstr_an(callback->post_data, callback->post_data_len));
1137     }
1138
1139     hr = CreateAsyncBindCtx(0, STATUSCLB(callback), NULL, &bindctx);
1140     if (FAILED(hr)) {
1141         IBindStatusCallback_Release(STATUSCLB(callback));
1142         return;
1143     }
1144
1145     hr = CoCreateInstance(&CLSID_StdHlink, NULL, CLSCTX_INPROC_SERVER, &IID_IHlink, (LPVOID*)&hlink);
1146     if (FAILED(hr)) {
1147         IBindCtx_Release(bindctx);
1148         IBindStatusCallback_Release(STATUSCLB(callback));
1149         return;
1150     }
1151
1152     hr = CreateURLMoniker(NULL, uri, &mon);
1153     if (SUCCEEDED(hr)) {
1154         IHlink_SetMonikerReference(hlink, 0, mon, NULL);
1155
1156         if(hlnf & HLNF_OPENINNEWWINDOW) {
1157             static const WCHAR wszBlank[] = {'_','b','l','a','n','k',0};
1158             IHlink_SetTargetFrameName(hlink, wszBlank); /* FIXME */
1159         }
1160
1161         IHlinkFrame_Navigate(hlink_frame, hlnf, bindctx, STATUSCLB(callback), hlink);
1162
1163         IMoniker_Release(mon);
1164     }
1165
1166     IBindCtx_Release(bindctx);
1167     IBindStatusCallback_Release(STATUSCLB(callback));
1168 }