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