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