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