msi: Implement Installer::OpenDatabase.
[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
32 #include "wine/debug.h"
33 #include "wine/unicode.h"
34
35 #include "mshtml_private.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
38
39 #define CONTENT_LENGTH "Content-Length"
40 #define UTF16_STR "utf-16"
41
42 #define NSINSTREAM(x) ((nsIInputStream*) &(x)->lpInputStreamVtbl)
43
44 #define NSINSTREAM_THIS(iface) DEFINE_THIS(nsProtocolStream, InputStream, iface)
45
46 static nsresult NSAPI nsInputStream_QueryInterface(nsIInputStream *iface, nsIIDRef riid,
47                                                    nsQIResult result)
48 {
49     nsProtocolStream *This = NSINSTREAM_THIS(iface);
50
51     *result = NULL;
52
53     if(IsEqualGUID(&IID_nsISupports, riid)) {
54         TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
55         *result  = NSINSTREAM(This);
56     }else if(IsEqualGUID(&IID_nsIInputStream, riid)) {
57         TRACE("(%p)->(IID_nsIInputStream %p)\n", This, result);
58         *result  = NSINSTREAM(This);
59     }
60
61     if(*result) {
62         nsIInputStream_AddRef(NSINSTREAM(This));
63         return NS_OK;
64     }
65
66     WARN("unsupported interface %s\n", debugstr_guid(riid));
67     return NS_NOINTERFACE;
68 }
69
70 static nsrefcnt NSAPI nsInputStream_AddRef(nsIInputStream *iface)
71 {
72     nsProtocolStream *This = NSINSTREAM_THIS(iface);
73     LONG ref = InterlockedIncrement(&This->ref);
74
75     TRACE("(%p) ref=%d\n", This, ref);
76
77     return ref;
78 }
79
80
81 static nsrefcnt NSAPI nsInputStream_Release(nsIInputStream *iface)
82 {
83     nsProtocolStream *This = NSINSTREAM_THIS(iface);
84     LONG ref = InterlockedDecrement(&This->ref);
85
86     TRACE("(%p) ref=%d\n", This, ref);
87
88     if(!ref)
89         mshtml_free(This);
90
91     return ref;
92 }
93
94 static nsresult NSAPI nsInputStream_Close(nsIInputStream *iface)
95 {
96     nsProtocolStream *This = NSINSTREAM_THIS(iface);
97     FIXME("(%p)\n", This);
98     return NS_ERROR_NOT_IMPLEMENTED;
99 }
100
101 static nsresult NSAPI nsInputStream_Available(nsIInputStream *iface, PRUint32 *_retval)
102 {
103     nsProtocolStream *This = NSINSTREAM_THIS(iface);
104     FIXME("(%p)->(%p)\n", This, _retval);
105     return NS_ERROR_NOT_IMPLEMENTED;
106 }
107
108 static nsresult NSAPI nsInputStream_Read(nsIInputStream *iface, char *aBuf, PRUint32 aCount,
109                                          PRUint32 *_retval)
110 {
111     nsProtocolStream *This = NSINSTREAM_THIS(iface);
112
113     TRACE("(%p)->(%p %d %p)\n", This, aBuf, aCount, _retval);
114
115     /* Gecko always calls Read with big enough buffer */
116     if(aCount < This->buf_size)
117         FIXME("aCount < This->buf_size\n");
118
119     *_retval = This->buf_size;
120     if(This->buf_size)
121         memcpy(aBuf, This->buf, This->buf_size);
122     This->buf_size = 0;
123
124     return NS_OK;
125 }
126
127 static nsresult NSAPI nsInputStream_ReadSegments(nsIInputStream *iface,
128         nsresult (WINAPI *aWriter)(nsIInputStream*,void*,const char*,PRUint32,PRUint32,PRUint32*),
129         void *aClousure, PRUint32 aCount, PRUint32 *_retval)
130 {
131     nsProtocolStream *This = NSINSTREAM_THIS(iface);
132     PRUint32 written = 0;
133     nsresult nsres;
134
135     TRACE("(%p)->(%p %p %d %p)\n", This, aWriter, aClousure, aCount, _retval);
136
137     if(!This->buf_size)
138         return S_OK;
139
140     if(This->buf_size > aCount)
141         FIXME("buf_size > aCount\n");
142
143     nsres = aWriter(NSINSTREAM(This), aClousure, This->buf, 0, This->buf_size, &written);
144     if(NS_FAILED(nsres))
145         TRACE("aWritter failed: %08x\n", nsres);
146     else if(written != This->buf_size)
147         FIXME("written %d != buf_size %d\n", written, This->buf_size);
148
149     This->buf_size -= written; 
150
151     *_retval = written;
152     return nsres;
153 }
154
155 static nsresult NSAPI nsInputStream_IsNonBlocking(nsIInputStream *iface, PRBool *_retval)
156 {
157     nsProtocolStream *This = NSINSTREAM_THIS(iface);
158     FIXME("(%p)->(%p)\n", This, _retval);
159     return NS_ERROR_NOT_IMPLEMENTED;
160 }
161
162 #undef NSINSTREAM_THIS
163
164 static const nsIInputStreamVtbl nsInputStreamVtbl = {
165     nsInputStream_QueryInterface,
166     nsInputStream_AddRef,
167     nsInputStream_Release,
168     nsInputStream_Close,
169     nsInputStream_Available,
170     nsInputStream_Read,
171     nsInputStream_ReadSegments,
172     nsInputStream_IsNonBlocking
173 };
174
175 static nsProtocolStream *create_nsprotocol_stream(void)
176 {
177     nsProtocolStream *ret = mshtml_alloc(sizeof(nsProtocolStream));
178
179     ret->lpInputStreamVtbl = &nsInputStreamVtbl;
180     ret->ref = 1;
181     ret->buf_size = 0;
182
183     return ret;
184 }
185
186 static HRESULT read_stream_data(BSCallback *This, IStream *stream)
187 {
188     nsresult nsres;
189     HRESULT hres;
190
191     if(!This->nslistener) {
192         BYTE buf[1024];
193         DWORD read;
194
195         do {
196             read = 0;
197             hres = IStream_Read(stream, buf, sizeof(buf), &read);
198         }while(hres == S_OK && read);
199
200         return S_OK;
201     }
202
203     if(!This->nsstream)
204         This->nsstream = create_nsprotocol_stream();
205
206     do {
207         hres = IStream_Read(stream, This->nsstream->buf, sizeof(This->nsstream->buf),
208                 &This->nsstream->buf_size);
209         if(!This->nsstream->buf_size)
210             break;
211
212         if(!This->readed && This->nsstream->buf_size >= 2 && *(WORD*)This->nsstream->buf == 0xfeff) {
213                 This->nschannel->charset = mshtml_alloc(sizeof(UTF16_STR));
214                 memcpy(This->nschannel->charset, UTF16_STR, sizeof(UTF16_STR));
215         }
216
217         if(!This->readed) {
218             nsres = nsIStreamListener_OnStartRequest(This->nslistener,
219                     (nsIRequest*)NSCHANNEL(This->nschannel), This->nscontext);
220             if(NS_FAILED(nsres))
221                 FIXME("OnStartRequest failed: %08x\n", nsres);
222         }
223
224         This->readed += This->nsstream->buf_size;
225
226         nsres = nsIStreamListener_OnDataAvailable(This->nslistener,
227                 (nsIRequest*)NSCHANNEL(This->nschannel), This->nscontext,
228                 NSINSTREAM(This->nsstream), This->readed-This->nsstream->buf_size,
229                 This->nsstream->buf_size);
230         if(NS_FAILED(nsres))
231             ERR("OnDataAvailable failed: %08x\n", nsres);
232
233         if(This->nsstream->buf_size)
234             FIXME("buffer is not empty!\n");
235     }while(hres == S_OK);
236
237     return S_OK;
238 }
239
240 static void add_nsrequest(BSCallback *This)
241 {
242     if(This->nschannel && This->nschannel->load_group) {
243         nsresult nsres = nsILoadGroup_AddRequest(This->nschannel->load_group,
244                 (nsIRequest*)NSCHANNEL(This->nschannel), This->nscontext);
245
246         if(NS_FAILED(nsres))
247             ERR("AddRequest failed:%08x\n", nsres);
248     }
249 }
250
251 #define STATUSCLB_THIS(iface) DEFINE_THIS(BSCallback, BindStatusCallback, iface)
252
253 static HRESULT WINAPI BindStatusCallback_QueryInterface(IBindStatusCallback *iface,
254         REFIID riid, void **ppv)
255 {
256     BSCallback *This = STATUSCLB_THIS(iface);
257
258     *ppv = NULL;
259     if(IsEqualGUID(&IID_IUnknown, riid)) {
260         TRACE("(%p)->(IID_IUnknown, %p)\n", This, ppv);
261         *ppv = STATUSCLB(This);
262     }else if(IsEqualGUID(&IID_IBindStatusCallback, riid)) {
263         TRACE("(%p)->(IID_IBindStatusCallback, %p)\n", This, ppv);
264         *ppv = STATUSCLB(This);
265     }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
266         TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
267         *ppv = SERVPROV(This);
268     }else if(IsEqualGUID(&IID_IHttpNegotiate, riid)) {
269         TRACE("(%p)->(IID_IHttpNegotiate %p)\n", This, ppv);
270         *ppv = HTTPNEG(This);
271     }else if(IsEqualGUID(&IID_IHttpNegotiate2, riid)) {
272         TRACE("(%p)->(IID_IHttpNegotiate2 %p)\n", This, ppv);
273         *ppv = HTTPNEG(This);
274     }else if(IsEqualGUID(&IID_IInternetBindInfo, riid)) {
275         TRACE("(%p)->(IID_IInternetBindInfo %p)\n", This, ppv);
276         *ppv = BINDINFO(This);
277     }
278
279     if(*ppv) {
280         IBindStatusCallback_AddRef(STATUSCLB(This));
281         return S_OK;
282     }
283
284     TRACE("Unsupported riid = %s\n", debugstr_guid(riid));
285     return E_NOINTERFACE;
286 }
287
288 static ULONG WINAPI BindStatusCallback_AddRef(IBindStatusCallback *iface)
289 {
290     BSCallback *This = STATUSCLB_THIS(iface);
291     LONG ref = InterlockedIncrement(&This->ref);
292
293     TRACE("(%p) ref = %d\n", This, ref);
294
295     return ref;
296 }
297
298 static ULONG WINAPI BindStatusCallback_Release(IBindStatusCallback *iface)
299 {
300     BSCallback *This = STATUSCLB_THIS(iface);
301     LONG ref = InterlockedDecrement(&This->ref);
302
303     TRACE("(%p) ref = %d\n", This, ref);
304
305     if(!ref) {
306         if(This->post_data)
307             GlobalFree(This->post_data);
308         if(This->nschannel)
309             nsIChannel_Release(NSCHANNEL(This->nschannel));
310         if(This->nslistener)
311             nsIStreamListener_Release(This->nslistener);
312         if(This->nscontext)
313             nsISupports_Release(This->nscontext);
314         if(This->nsstream)
315             nsIInputStream_Release(NSINSTREAM(This->nsstream));
316         if(This->mon)
317             IMoniker_Release(This->mon);
318         if(This->binding)
319             IBinding_Release(This->binding);
320         mshtml_free(This->headers);
321         mshtml_free(This);
322     }
323
324     return ref;
325 }
326
327 static HRESULT WINAPI BindStatusCallback_OnStartBinding(IBindStatusCallback *iface,
328         DWORD dwReserved, IBinding *pbind)
329 {
330     BSCallback *This = STATUSCLB_THIS(iface);
331
332     TRACE("(%p)->(%d %p)\n", This, dwReserved, pbind);
333
334     IBinding_AddRef(pbind);
335     This->binding = pbind;
336
337     add_nsrequest(This);
338
339     return S_OK;
340 }
341
342 static HRESULT WINAPI BindStatusCallback_GetPriority(IBindStatusCallback *iface, LONG *pnPriority)
343 {
344     BSCallback *This = STATUSCLB_THIS(iface);
345     FIXME("(%p)->(%p)\n", This, pnPriority);
346     return E_NOTIMPL;
347 }
348
349 static HRESULT WINAPI BindStatusCallback_OnLowResource(IBindStatusCallback *iface, DWORD reserved)
350 {
351     BSCallback *This = STATUSCLB_THIS(iface);
352     FIXME("(%p)->(%d)\n", This, reserved);
353     return E_NOTIMPL;
354 }
355
356 static HRESULT WINAPI BindStatusCallback_OnProgress(IBindStatusCallback *iface, ULONG ulProgress,
357         ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
358 {
359     BSCallback *This = STATUSCLB_THIS(iface);
360
361     TRACE("%p)->(%u %u %u %s)\n", This, ulProgress, ulProgressMax, ulStatusCode,
362             debugstr_w(szStatusText));
363
364     switch(ulStatusCode) {
365     case BINDSTATUS_MIMETYPEAVAILABLE: {
366         int len;
367
368         if(!This->nschannel)
369             return S_OK;
370         mshtml_free(This->nschannel->content);
371
372         len = WideCharToMultiByte(CP_ACP, 0, szStatusText, -1, NULL, 0, NULL, NULL);
373         This->nschannel->content = mshtml_alloc(len*sizeof(WCHAR));
374         WideCharToMultiByte(CP_ACP, 0, szStatusText, -1, This->nschannel->content, -1, NULL, NULL);
375     }
376     }
377
378     return S_OK;
379 }
380
381 static HRESULT WINAPI BindStatusCallback_OnStopBinding(IBindStatusCallback *iface,
382         HRESULT hresult, LPCWSTR szError)
383 {
384     BSCallback *This = STATUSCLB_THIS(iface);
385
386     TRACE("(%p)->(%08x %s)\n", This, hresult, debugstr_w(szError));
387
388     if(This->binding) {
389         IBinding_Release(This->binding);
390         This->binding = NULL;
391     }
392
393     if(This->nslistener) {
394         nsIStreamListener_OnStopRequest(This->nslistener, (nsIRequest*)NSCHANNEL(This->nschannel),
395                 This->nscontext, NS_OK);
396
397         if(This->nschannel->load_group) {
398             nsresult nsres;
399
400             nsres = nsILoadGroup_RemoveRequest(This->nschannel->load_group,
401                     (nsIRequest*)NSCHANNEL(This->nschannel), NULL, NS_OK);
402             if(NS_FAILED(nsres))
403                 ERR("RemoveRequest failed: %08x\n", nsres);
404         }
405     }
406
407     if(This->doc) {
408         task_t *task = mshtml_alloc(sizeof(task_t));
409
410         task->doc = This->doc;
411         task->task_id = TASK_PARSECOMPLETE;
412         task->next = NULL;
413
414         /*
415          * This should be done in the worker thread that parses HTML,
416          * but we don't have such thread (Gecko parses HTML for us).
417          */
418         push_task(task);
419     }
420
421     return S_OK;
422 }
423
424 static HRESULT WINAPI BindStatusCallback_GetBindInfo(IBindStatusCallback *iface,
425         DWORD *grfBINDF, BINDINFO *pbindinfo)
426 {
427     BSCallback *This = STATUSCLB_THIS(iface);
428     DWORD size;
429
430     TRACE("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
431
432     *grfBINDF = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA;
433
434     size = pbindinfo->cbSize;
435     memset(pbindinfo, 0, size);
436     pbindinfo->cbSize = size;
437
438     pbindinfo->cbstgmedData = This->post_data_len;
439     pbindinfo->dwCodePage = CP_UTF8;
440     pbindinfo->dwOptions = 0x80000;
441
442     if(This->post_data) {
443         pbindinfo->dwBindVerb = BINDVERB_POST;
444
445         pbindinfo->stgmedData.tymed = TYMED_HGLOBAL;
446         pbindinfo->stgmedData.u.hGlobal = This->post_data;
447         pbindinfo->stgmedData.pUnkForRelease = (IUnknown*)STATUSCLB(This);
448         IBindStatusCallback_AddRef(STATUSCLB(This));
449     }
450
451     return S_OK;
452 }
453
454 static HRESULT WINAPI BindStatusCallback_OnDataAvailable(IBindStatusCallback *iface,
455         DWORD grfBSCF, DWORD dwSize, FORMATETC *pformatetc, STGMEDIUM *pstgmed)
456 {
457     BSCallback *This = STATUSCLB_THIS(iface);
458
459     TRACE("(%p)->(%08x %d %p %p)\n", This, grfBSCF, dwSize, pformatetc, pstgmed);
460
461     return read_stream_data(This, pstgmed->u.pstm);
462 }
463
464 static HRESULT WINAPI BindStatusCallback_OnObjectAvailable(IBindStatusCallback *iface,
465         REFIID riid, IUnknown *punk)
466 {
467     BSCallback *This = STATUSCLB_THIS(iface);
468     FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), punk);
469     return E_NOTIMPL;
470 }
471
472 #undef STATUSCLB_THIS
473
474 static const IBindStatusCallbackVtbl BindStatusCallbackVtbl = {
475     BindStatusCallback_QueryInterface,
476     BindStatusCallback_AddRef,
477     BindStatusCallback_Release,
478     BindStatusCallback_OnStartBinding,
479     BindStatusCallback_GetPriority,
480     BindStatusCallback_OnLowResource,
481     BindStatusCallback_OnProgress,
482     BindStatusCallback_OnStopBinding,
483     BindStatusCallback_GetBindInfo,
484     BindStatusCallback_OnDataAvailable,
485     BindStatusCallback_OnObjectAvailable
486 };
487
488 #define HTTPNEG_THIS(iface) DEFINE_THIS(BSCallback, HttpNegotiate2, iface)
489
490 static HRESULT WINAPI HttpNegotiate_QueryInterface(IHttpNegotiate2 *iface,
491                                                    REFIID riid, void **ppv)
492 {
493     BSCallback *This = HTTPNEG_THIS(iface);
494     return IBindStatusCallback_QueryInterface(STATUSCLB(This), riid, ppv);
495 }
496
497 static ULONG WINAPI HttpNegotiate_AddRef(IHttpNegotiate2 *iface)
498 {
499     BSCallback *This = HTTPNEG_THIS(iface);
500     return IBindStatusCallback_AddRef(STATUSCLB(This));
501 }
502
503 static ULONG WINAPI HttpNegotiate_Release(IHttpNegotiate2 *iface)
504 {
505     BSCallback *This = HTTPNEG_THIS(iface);
506     return IBindStatusCallback_Release(STATUSCLB(This));
507 }
508
509 static HRESULT WINAPI HttpNegotiate_BeginningTransaction(IHttpNegotiate2 *iface,
510         LPCWSTR szURL, LPCWSTR szHeaders, DWORD dwReserved, LPWSTR *pszAdditionalHeaders)
511 {
512     BSCallback *This = HTTPNEG_THIS(iface);
513     DWORD size;
514
515     TRACE("(%p)->(%s %s %d %p)\n", This, debugstr_w(szURL), debugstr_w(szHeaders),
516           dwReserved, pszAdditionalHeaders);
517
518     if(!This->headers) {
519         *pszAdditionalHeaders = NULL;
520         return S_OK;
521     }
522
523     size = (strlenW(This->headers)+1)*sizeof(WCHAR);
524     *pszAdditionalHeaders = CoTaskMemAlloc(size);
525     memcpy(*pszAdditionalHeaders, This->headers, size);
526
527     return S_OK;
528 }
529
530 static HRESULT WINAPI HttpNegotiate_OnResponse(IHttpNegotiate2 *iface, DWORD dwResponseCode,
531         LPCWSTR szResponseHeaders, LPCWSTR szRequestHeaders, LPWSTR *pszAdditionalRequestHeaders)
532 {
533     BSCallback *This = HTTPNEG_THIS(iface);
534     FIXME("(%p)->(%d %s %s %p)\n", This, dwResponseCode, debugstr_w(szResponseHeaders),
535           debugstr_w(szRequestHeaders), pszAdditionalRequestHeaders);
536     return E_NOTIMPL;
537 }
538
539 static HRESULT WINAPI HttpNegotiate_GetRootSecurityId(IHttpNegotiate2 *iface,
540         BYTE *pbSecurityId, DWORD *pcbSecurityId, DWORD_PTR dwReserved)
541 {
542     BSCallback *This = HTTPNEG_THIS(iface);
543     FIXME("(%p)->(%p %p %ld)\n", This, pbSecurityId, pcbSecurityId, dwReserved);
544     return E_NOTIMPL;
545 }
546
547 #undef HTTPNEG
548
549 static const IHttpNegotiate2Vtbl HttpNegotiate2Vtbl = {
550     HttpNegotiate_QueryInterface,
551     HttpNegotiate_AddRef,
552     HttpNegotiate_Release,
553     HttpNegotiate_BeginningTransaction,
554     HttpNegotiate_OnResponse,
555     HttpNegotiate_GetRootSecurityId
556 };
557
558 #define BINDINFO_THIS(iface) DEFINE_THIS(BSCallback, InternetBindInfo, iface)
559
560 static HRESULT WINAPI InternetBindInfo_QueryInterface(IInternetBindInfo *iface,
561                                                       REFIID riid, void **ppv)
562 {
563     BSCallback *This = BINDINFO_THIS(iface);
564     return IBindStatusCallback_QueryInterface(STATUSCLB(This), riid, ppv);
565 }
566
567 static ULONG WINAPI InternetBindInfo_AddRef(IInternetBindInfo *iface)
568 {
569     BSCallback *This = BINDINFO_THIS(iface);
570     return IBindStatusCallback_AddRef(STATUSCLB(This));
571 }
572
573 static ULONG WINAPI InternetBindInfo_Release(IInternetBindInfo *iface)
574 {
575     BSCallback *This = BINDINFO_THIS(iface);
576     return IBindStatusCallback_Release(STATUSCLB(This));
577 }
578
579 static HRESULT WINAPI InternetBindInfo_GetBindInfo(IInternetBindInfo *iface,
580                                                    DWORD *grfBINDF, BINDINFO *pbindinfo)
581 {
582     BSCallback *This = BINDINFO_THIS(iface);
583     FIXME("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
584     return E_NOTIMPL;
585 }
586
587 static HRESULT WINAPI InternetBindInfo_GetBindString(IInternetBindInfo *iface,
588         ULONG ulStringType, LPOLESTR *ppwzStr, ULONG cEl, ULONG *pcElFetched)
589 {
590     BSCallback *This = BINDINFO_THIS(iface);
591     FIXME("(%p)->(%u %p %u %p)\n", This, ulStringType, ppwzStr, cEl, pcElFetched);
592     return E_NOTIMPL;
593 }
594
595 #undef BINDINFO_THIS
596
597 static const IInternetBindInfoVtbl InternetBindInfoVtbl = {
598     InternetBindInfo_QueryInterface,
599     InternetBindInfo_AddRef,
600     InternetBindInfo_Release,
601     InternetBindInfo_GetBindInfo,
602     InternetBindInfo_GetBindString
603 };
604
605 #define SERVPROV_THIS(iface) DEFINE_THIS(BSCallback, ServiceProvider, iface)
606
607 static HRESULT WINAPI BSCServiceProvider_QueryInterface(IServiceProvider *iface,
608                                                         REFIID riid, void **ppv)
609 {
610     BSCallback *This = SERVPROV_THIS(iface);
611     return IBindStatusCallback_QueryInterface(STATUSCLB(This), riid, ppv);
612 }
613
614 static ULONG WINAPI BSCServiceProvider_AddRef(IServiceProvider *iface)
615 {
616     BSCallback *This = SERVPROV_THIS(iface);
617     return IBindStatusCallback_AddRef(STATUSCLB(This));
618 }
619
620 static ULONG WINAPI BSCServiceProvider_Release(IServiceProvider *iface)
621 {
622     BSCallback *This = SERVPROV_THIS(iface);
623     return IBindStatusCallback_Release(STATUSCLB(This));
624 }
625
626 static HRESULT WINAPI BSCServiceProvider_QueryService(IServiceProvider *iface,
627         REFGUID guidService, REFIID riid, void **ppv)
628 {
629     BSCallback *This = SERVPROV_THIS(iface);
630     FIXME("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
631     return E_NOTIMPL;
632 }
633
634 #undef SERVPROV_THIS
635
636 static const IServiceProviderVtbl ServiceProviderVtbl = {
637     BSCServiceProvider_QueryInterface,
638     BSCServiceProvider_AddRef,
639     BSCServiceProvider_Release,
640     BSCServiceProvider_QueryService
641 };
642
643 BSCallback *create_bscallback(IMoniker *mon)
644 {
645     BSCallback *ret = mshtml_alloc(sizeof(BSCallback));
646
647     ret->lpBindStatusCallbackVtbl = &BindStatusCallbackVtbl;
648     ret->lpServiceProviderVtbl    = &ServiceProviderVtbl;
649     ret->lpHttpNegotiate2Vtbl     = &HttpNegotiate2Vtbl;
650     ret->lpInternetBindInfoVtbl   = &InternetBindInfoVtbl;
651     ret->ref = 1;
652     ret->post_data = NULL;
653     ret->headers = NULL;
654     ret->post_data_len = 0;
655     ret->readed = 0;
656     ret->nschannel = NULL;
657     ret->nslistener = NULL;
658     ret->nscontext = NULL;
659     ret->nsstream = NULL;
660     ret->binding = NULL;
661     ret->doc = NULL;
662
663     if(mon)
664         IMoniker_AddRef(mon);
665     ret->mon = mon;
666
667     return ret;
668 }
669
670 static void parse_post_data(nsIInputStream *post_data_stream, LPWSTR *headers_ret,
671                             HGLOBAL *post_data_ret, ULONG *post_data_len_ret)
672 {
673     PRUint32 post_data_len = 0, available = 0;
674     HGLOBAL post_data = NULL;
675     LPWSTR headers = NULL;
676     DWORD headers_len = 0, len;
677     const char *ptr, *ptr2, *post_data_end;
678
679     nsIInputStream_Available(post_data_stream, &available);
680     post_data = GlobalAlloc(0, available+1);
681     nsIInputStream_Read(post_data_stream, post_data, available, &post_data_len);
682     
683     TRACE("post_data = %s\n", debugstr_an(post_data, post_data_len));
684
685     ptr = ptr2 = post_data;
686     post_data_end = (const char*)post_data+post_data_len;
687
688     while(ptr < post_data_end && (*ptr != '\r' || ptr[1] != '\n')) {
689         while(ptr < post_data_end && (*ptr != '\r' || ptr[1] != '\n'))
690             ptr++;
691
692         if(!*ptr) {
693             FIXME("*ptr = 0\n");
694             return;
695         }
696
697         ptr += 2;
698
699         if(ptr-ptr2 >= sizeof(CONTENT_LENGTH)
700            && CompareStringA(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
701                              CONTENT_LENGTH, sizeof(CONTENT_LENGTH)-1,
702                              ptr2, sizeof(CONTENT_LENGTH)-1) == CSTR_EQUAL) {
703             ptr2 = ptr;
704             continue;
705         }
706
707         len = MultiByteToWideChar(CP_ACP, 0, ptr2, ptr-ptr2, NULL, 0);
708
709         if(headers)
710             headers = mshtml_realloc(headers,(headers_len+len+1)*sizeof(WCHAR));
711         else
712             headers = mshtml_alloc((len+1)*sizeof(WCHAR));
713
714         len = MultiByteToWideChar(CP_ACP, 0, ptr2, ptr-ptr2, headers+headers_len, -1);
715         headers_len += len;
716
717         ptr2 = ptr;
718     }
719
720     headers[headers_len] = 0;
721     *headers_ret = headers;
722
723     if(ptr >= post_data_end-2) {
724         GlobalFree(post_data);
725         return;
726     }
727
728     ptr += 2;
729
730     if(headers_len) {
731         post_data_len -= ptr-(const char*)post_data;
732         memmove(post_data, ptr, post_data_len);
733         post_data = GlobalReAlloc(post_data, post_data_len+1, 0);
734     }
735
736     *post_data_ret = post_data;
737     *post_data_len_ret = post_data_len;
738 }
739
740 void hlink_frame_navigate(HTMLDocument *doc, IHlinkFrame *hlink_frame,
741                           LPCWSTR uri, nsIInputStream *post_data_stream, DWORD hlnf)
742 {
743     BSCallback *callback;
744     IBindCtx *bindctx;
745     IMoniker *mon;
746     IHlink *hlink;
747
748     callback = create_bscallback(NULL);
749
750     if(post_data_stream) {
751         parse_post_data(post_data_stream, &callback->headers, &callback->post_data,
752                         &callback->post_data_len);
753         TRACE("headers = %s post_data = %s\n", debugstr_w(callback->headers),
754               debugstr_an(callback->post_data, callback->post_data_len));
755     }
756
757     CreateAsyncBindCtx(0, STATUSCLB(callback), NULL, &bindctx);
758
759     hlink = Hlink_Create();
760
761     CreateURLMoniker(NULL, uri, &mon);
762     IHlink_SetMonikerReference(hlink, 0, mon, NULL);
763
764     if(hlnf & HLNF_OPENINNEWWINDOW) {
765         static const WCHAR wszBlank[] = {'_','b','l','a','n','k',0};
766         IHlink_SetTargetFrameName(hlink, wszBlank); /* FIXME */
767     }
768
769     IHlinkFrame_Navigate(hlink_frame, hlnf, bindctx, STATUSCLB(callback), hlink);
770
771     IBindCtx_Release(bindctx);
772     IBindStatusCallback_Release(STATUSCLB(callback));
773     IMoniker_Release(mon);
774
775 }
776
777 HRESULT start_binding(BSCallback *bscallback)
778 {
779     IStream *str = NULL;
780     IBindCtx *bctx;
781     HRESULT hres;
782
783     hres = CreateAsyncBindCtx(0, STATUSCLB(bscallback), NULL, &bctx);
784     if(FAILED(hres)) {
785         WARN("CreateAsyncBindCtx failed: %08x\n", hres);
786         return hres;
787     }
788
789     hres = IMoniker_BindToStorage(bscallback->mon, bctx, NULL, &IID_IStream, (void**)&str);
790     IBindCtx_Release(bctx);
791     if(FAILED(hres)) {
792         WARN("BindToStorage failed: %08x\n", hres);
793         return hres;
794     }
795
796     if(str)
797         IStream_Release(str);
798
799     IMoniker_Release(bscallback->mon);
800     bscallback->mon = NULL;
801     return S_OK;
802 }
803
804 void set_document_bscallback(HTMLDocument *doc, BSCallback *callback)
805 {
806     if(doc->bscallback) {
807         if(doc->bscallback->binding)
808             IBinding_Abort(doc->bscallback->binding);
809         doc->bscallback->doc = NULL;
810         IBindStatusCallback_Release(STATUSCLB(doc->bscallback));
811     }
812
813     doc->bscallback = callback;
814
815     if(callback) {
816         IBindStatusCallback_AddRef(STATUSCLB(callback));
817         callback->doc = doc;
818     }
819 }
820
821 HRESULT load_stream(BSCallback *bscallback, IStream *stream)
822 {
823     HRESULT hres;
824
825     const char text_html[] = "text/html";
826
827     add_nsrequest(bscallback);
828
829     bscallback->nschannel->content = mshtml_alloc(sizeof(text_html));
830     memcpy(bscallback->nschannel->content, text_html, sizeof(text_html));
831
832     hres = read_stream_data(bscallback, stream);
833     IBindStatusCallback_OnStopBinding(STATUSCLB(bscallback), hres, ERROR_SUCCESS);
834
835     return hres;
836 }