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