shell32/tests: Fixed potential overflow.
[wine/gsoc_dplay.git] / dlls / mshtml / navigate.c
bloba665ff0fc4b128354e5f79009b76d077067aa6ef
1 /*
2 * Copyright 2006 Jacek Caban for CodeWeavers
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.
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.
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
19 #include "config.h"
21 #include <stdarg.h>
23 #define COBJMACROS
24 #define NONAMELESSUNION
25 #define NONAMELESSSTRUCT
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winuser.h"
30 #include "ole2.h"
32 #include "wine/debug.h"
33 #include "wine/unicode.h"
35 #include "mshtml_private.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
39 #define CONTENT_LENGTH "Content-Length"
41 #define NSINSTREAM(x) ((nsIInputStream*) &(x)->lpInputStreamVtbl)
43 #define NSINSTREAM_THIS(iface) DEFINE_THIS(nsProtocolStream, InputStream, iface)
45 static nsresult NSAPI nsInputStream_QueryInterface(nsIInputStream *iface, nsIIDRef riid,
46 nsQIResult result)
48 nsProtocolStream *This = NSINSTREAM_THIS(iface);
50 *result = NULL;
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);
60 if(*result) {
61 nsIInputStream_AddRef(NSINSTREAM(This));
62 return NS_OK;
65 WARN("unsupported interface %s\n", debugstr_guid(riid));
66 return NS_NOINTERFACE;
69 static nsrefcnt NSAPI nsInputStream_AddRef(nsIInputStream *iface)
71 nsProtocolStream *This = NSINSTREAM_THIS(iface);
72 LONG ref = InterlockedIncrement(&This->ref);
74 TRACE("(%p) ref=%d\n", This, ref);
76 return ref;
80 static nsrefcnt NSAPI nsInputStream_Release(nsIInputStream *iface)
82 nsProtocolStream *This = NSINSTREAM_THIS(iface);
83 LONG ref = InterlockedDecrement(&This->ref);
85 TRACE("(%p) ref=%d\n", This, ref);
87 if(!ref)
88 mshtml_free(This);
90 return ref;
93 static nsresult NSAPI nsInputStream_Close(nsIInputStream *iface)
95 nsProtocolStream *This = NSINSTREAM_THIS(iface);
96 FIXME("(%p)\n", This);
97 return NS_ERROR_NOT_IMPLEMENTED;
100 static nsresult NSAPI nsInputStream_Available(nsIInputStream *iface, PRUint32 *_retval)
102 nsProtocolStream *This = NSINSTREAM_THIS(iface);
103 FIXME("(%p)->(%p)\n", This, _retval);
104 return NS_ERROR_NOT_IMPLEMENTED;
107 static nsresult NSAPI nsInputStream_Read(nsIInputStream *iface, char *aBuf, PRUint32 aCount,
108 PRUint32 *_retval)
110 nsProtocolStream *This = NSINSTREAM_THIS(iface);
112 TRACE("(%p)->(%p %d %p)\n", This, aBuf, aCount, _retval);
114 /* Gecko always calls Read with big enough buffer */
115 if(aCount < This->buf_size)
116 FIXME("aCount < This->buf_size\n");
118 *_retval = This->buf_size;
119 if(This->buf_size)
120 memcpy(aBuf, This->buf, This->buf_size);
121 This->buf_size = 0;
123 return NS_OK;
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)
130 nsProtocolStream *This = NSINSTREAM_THIS(iface);
131 PRUint32 written = 0;
132 nsresult nsres;
134 TRACE("(%p)->(%p %p %d %p)\n", This, aWriter, aClousure, aCount, _retval);
136 if(!This->buf_size)
137 return S_OK;
139 if(This->buf_size > aCount)
140 FIXME("buf_size > aCount\n");
142 nsres = aWriter(NSINSTREAM(This), aClousure, This->buf, 0, This->buf_size, &written);
143 if(NS_FAILED(nsres))
144 FIXME("aWritter failed: %08x\n", nsres);
145 if(written != This->buf_size)
146 FIXME("written != buf_size\n");
148 This->buf_size -= written;
150 return nsres;
153 static nsresult NSAPI nsInputStream_IsNonBlocking(nsIInputStream *iface, PRBool *_retval)
155 nsProtocolStream *This = NSINSTREAM_THIS(iface);
156 FIXME("(%p)->(%p)\n", This, _retval);
157 return NS_ERROR_NOT_IMPLEMENTED;
160 #undef NSINSTREAM_THIS
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
173 static nsProtocolStream *create_nsprotocol_stream(IStream *stream)
175 nsProtocolStream *ret = mshtml_alloc(sizeof(nsProtocolStream));
177 ret->lpInputStreamVtbl = &nsInputStreamVtbl;
178 ret->ref = 1;
179 ret->buf_size = 0;
181 return ret;
184 #define STATUSCLB_THIS(iface) DEFINE_THIS(BSCallback, BindStatusCallback, iface)
186 static HRESULT WINAPI BindStatusCallback_QueryInterface(IBindStatusCallback *iface,
187 REFIID riid, void **ppv)
189 BSCallback *This = STATUSCLB_THIS(iface);
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);
212 if(*ppv) {
213 IBindStatusCallback_AddRef(STATUSCLB(This));
214 return S_OK;
217 TRACE("Unsupported riid = %s\n", debugstr_guid(riid));
218 return E_NOINTERFACE;
221 static ULONG WINAPI BindStatusCallback_AddRef(IBindStatusCallback *iface)
223 BSCallback *This = STATUSCLB_THIS(iface);
224 LONG ref = InterlockedIncrement(&This->ref);
226 TRACE("(%p) ref = %d\n", This, ref);
228 return ref;
231 static ULONG WINAPI BindStatusCallback_Release(IBindStatusCallback *iface)
233 BSCallback *This = STATUSCLB_THIS(iface);
234 LONG ref = InterlockedDecrement(&This->ref);
236 TRACE("(%p) ref = %d\n", This, ref);
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 if(This->mon)
250 IMoniker_Release(This->mon);
251 if(This->binding)
252 IBinding_Release(This->binding);
253 mshtml_free(This->headers);
254 mshtml_free(This);
257 return ref;
260 static HRESULT WINAPI BindStatusCallback_OnStartBinding(IBindStatusCallback *iface,
261 DWORD dwReserved, IBinding *pbind)
263 BSCallback *This = STATUSCLB_THIS(iface);
265 TRACE("(%p)->(%d %p)\n", This, dwReserved, pbind);
267 IBinding_AddRef(pbind);
268 This->binding = pbind;
270 if(This->nschannel && This->nschannel->load_group) {
271 nsresult nsres = nsILoadGroup_AddRequest(This->nschannel->load_group,
272 (nsIRequest*)NSCHANNEL(This->nschannel), This->nscontext);
274 if(NS_FAILED(nsres))
275 ERR("AddRequest failed:%08x\n", nsres);
278 return S_OK;
281 static HRESULT WINAPI BindStatusCallback_GetPriority(IBindStatusCallback *iface, LONG *pnPriority)
283 BSCallback *This = STATUSCLB_THIS(iface);
284 FIXME("(%p)->(%p)\n", This, pnPriority);
285 return E_NOTIMPL;
288 static HRESULT WINAPI BindStatusCallback_OnLowResource(IBindStatusCallback *iface, DWORD reserved)
290 BSCallback *This = STATUSCLB_THIS(iface);
291 FIXME("(%p)->(%d)\n", This, reserved);
292 return E_NOTIMPL;
295 static HRESULT WINAPI BindStatusCallback_OnProgress(IBindStatusCallback *iface, ULONG ulProgress,
296 ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
298 BSCallback *This = STATUSCLB_THIS(iface);
300 TRACE("%p)->(%u %u %u %s)\n", This, ulProgress, ulProgressMax, ulStatusCode,
301 debugstr_w(szStatusText));
303 switch(ulStatusCode) {
304 case BINDSTATUS_MIMETYPEAVAILABLE: {
305 int len;
307 if(!This->nschannel)
308 return S_OK;
309 mshtml_free(This->nschannel->content);
311 len = WideCharToMultiByte(CP_ACP, 0, szStatusText, -1, NULL, 0, NULL, NULL);
312 This->nschannel->content = mshtml_alloc(len*sizeof(WCHAR));
313 WideCharToMultiByte(CP_ACP, 0, szStatusText, -1, This->nschannel->content, -1, NULL, NULL);
317 return S_OK;
320 static HRESULT WINAPI BindStatusCallback_OnStopBinding(IBindStatusCallback *iface,
321 HRESULT hresult, LPCWSTR szError)
323 BSCallback *This = STATUSCLB_THIS(iface);
325 TRACE("(%p)->(%08x %s)\n", This, hresult, debugstr_w(szError));
327 IBinding_Release(This->binding);
328 This->binding = NULL;
330 if(This->nslistener) {
331 nsIStreamListener_OnStopRequest(This->nslistener, (nsIRequest*)NSCHANNEL(This->nschannel),
332 This->nscontext, NS_OK);
334 if(This->nschannel->load_group) {
335 nsresult nsres;
337 nsres = nsILoadGroup_RemoveRequest(This->nschannel->load_group,
338 (nsIRequest*)NSCHANNEL(This->nschannel), NULL, NS_OK);
339 if(NS_FAILED(nsres))
340 ERR("RemoveRequest failed: %08x\n", nsres);
344 if(This->doc) {
345 task_t *task = mshtml_alloc(sizeof(task_t));
347 task->doc = This->doc;
348 task->task_id = TASK_PARSECOMPLETE;
349 task->next = NULL;
352 * This should be done in the worker thread that parses HTML,
353 * but we don't have such thread (Gecko parses HTML for us).
355 push_task(task);
358 return S_OK;
361 static HRESULT WINAPI BindStatusCallback_GetBindInfo(IBindStatusCallback *iface,
362 DWORD *grfBINDF, BINDINFO *pbindinfo)
364 BSCallback *This = STATUSCLB_THIS(iface);
365 DWORD size;
367 TRACE("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
369 *grfBINDF = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA;
371 size = pbindinfo->cbSize;
372 memset(pbindinfo, 0, size);
373 pbindinfo->cbSize = size;
375 pbindinfo->cbstgmedData = This->post_data_len;
376 pbindinfo->dwCodePage = CP_UTF8;
377 pbindinfo->dwOptions = 0x80000;
379 if(This->post_data) {
380 pbindinfo->dwBindVerb = BINDVERB_POST;
382 pbindinfo->stgmedData.tymed = TYMED_HGLOBAL;
383 pbindinfo->stgmedData.u.hGlobal = This->post_data;
384 pbindinfo->stgmedData.pUnkForRelease = (IUnknown*)STATUSCLB(This);
385 IBindStatusCallback_AddRef(STATUSCLB(This));
388 return S_OK;
391 static HRESULT WINAPI BindStatusCallback_OnDataAvailable(IBindStatusCallback *iface,
392 DWORD grfBSCF, DWORD dwSize, FORMATETC *pformatetc, STGMEDIUM *pstgmed)
394 BSCallback *This = STATUSCLB_THIS(iface);
395 nsresult nsres;
396 HRESULT hres;
398 TRACE("(%p)->(%08x %d %p %p)\n", This, grfBSCF, dwSize, pformatetc, pstgmed);
400 if(This->nslistener) {
401 if(!This->nsstream) {
402 This->nsstream = create_nsprotocol_stream(pstgmed->u.pstm);
404 nsres = nsIStreamListener_OnStartRequest(This->nslistener,
405 (nsIRequest*)NSCHANNEL(This->nschannel), This->nscontext);
406 if(NS_FAILED(nsres))
407 FIXME("OnStartRequest failed: %08x\n", nsres);
410 do {
411 hres = IStream_Read(pstgmed->u.pstm, This->nsstream->buf, sizeof(This->nsstream->buf),
412 &This->nsstream->buf_size);
413 if(!This->nsstream->buf_size)
414 break;
416 nsres = nsIStreamListener_OnDataAvailable(This->nslistener,
417 (nsIRequest*)NSCHANNEL(This->nschannel), This->nscontext,
418 NSINSTREAM(This->nsstream), This->readed, This->nsstream->buf_size);
419 if(NS_FAILED(nsres))
420 FIXME("OnDataAvailable failed: %08x\n", nsres);
422 if(This->nsstream->buf_size)
423 FIXME("buffer is not empty!\n");
425 This->readed += This->nsstream->buf_size;
426 }while(hres == S_OK);
429 return S_OK;
432 static HRESULT WINAPI BindStatusCallback_OnObjectAvailable(IBindStatusCallback *iface,
433 REFIID riid, IUnknown *punk)
435 BSCallback *This = STATUSCLB_THIS(iface);
436 FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), punk);
437 return E_NOTIMPL;
440 #undef STATUSCLB_THIS
442 static const IBindStatusCallbackVtbl BindStatusCallbackVtbl = {
443 BindStatusCallback_QueryInterface,
444 BindStatusCallback_AddRef,
445 BindStatusCallback_Release,
446 BindStatusCallback_OnStartBinding,
447 BindStatusCallback_GetPriority,
448 BindStatusCallback_OnLowResource,
449 BindStatusCallback_OnProgress,
450 BindStatusCallback_OnStopBinding,
451 BindStatusCallback_GetBindInfo,
452 BindStatusCallback_OnDataAvailable,
453 BindStatusCallback_OnObjectAvailable
456 #define HTTPNEG_THIS(iface) DEFINE_THIS(BSCallback, HttpNegotiate2, iface)
458 static HRESULT WINAPI HttpNegotiate_QueryInterface(IHttpNegotiate2 *iface,
459 REFIID riid, void **ppv)
461 BSCallback *This = HTTPNEG_THIS(iface);
462 return IBindStatusCallback_QueryInterface(STATUSCLB(This), riid, ppv);
465 static ULONG WINAPI HttpNegotiate_AddRef(IHttpNegotiate2 *iface)
467 BSCallback *This = HTTPNEG_THIS(iface);
468 return IBindStatusCallback_AddRef(STATUSCLB(This));
471 static ULONG WINAPI HttpNegotiate_Release(IHttpNegotiate2 *iface)
473 BSCallback *This = HTTPNEG_THIS(iface);
474 return IBindStatusCallback_Release(STATUSCLB(This));
477 static HRESULT WINAPI HttpNegotiate_BeginningTransaction(IHttpNegotiate2 *iface,
478 LPCWSTR szURL, LPCWSTR szHeaders, DWORD dwReserved, LPWSTR *pszAdditionalHeaders)
480 BSCallback *This = HTTPNEG_THIS(iface);
481 DWORD size;
483 TRACE("(%p)->(%s %s %d %p)\n", This, debugstr_w(szURL), debugstr_w(szHeaders),
484 dwReserved, pszAdditionalHeaders);
486 if(!This->headers) {
487 *pszAdditionalHeaders = NULL;
488 return S_OK;
491 size = (strlenW(This->headers)+1)*sizeof(WCHAR);
492 *pszAdditionalHeaders = CoTaskMemAlloc(size);
493 memcpy(*pszAdditionalHeaders, This->headers, size);
495 return S_OK;
498 static HRESULT WINAPI HttpNegotiate_OnResponse(IHttpNegotiate2 *iface, DWORD dwResponseCode,
499 LPCWSTR szResponseHeaders, LPCWSTR szRequestHeaders, LPWSTR *pszAdditionalRequestHeaders)
501 BSCallback *This = HTTPNEG_THIS(iface);
502 FIXME("(%p)->(%d %s %s %p)\n", This, dwResponseCode, debugstr_w(szResponseHeaders),
503 debugstr_w(szRequestHeaders), pszAdditionalRequestHeaders);
504 return E_NOTIMPL;
507 static HRESULT WINAPI HttpNegotiate_GetRootSecurityId(IHttpNegotiate2 *iface,
508 BYTE *pbSecurityId, DWORD *pcbSecurityId, DWORD_PTR dwReserved)
510 BSCallback *This = HTTPNEG_THIS(iface);
511 FIXME("(%p)->(%p %p %ld)\n", This, pbSecurityId, pcbSecurityId, dwReserved);
512 return E_NOTIMPL;
515 #undef HTTPNEG
517 static const IHttpNegotiate2Vtbl HttpNegotiate2Vtbl = {
518 HttpNegotiate_QueryInterface,
519 HttpNegotiate_AddRef,
520 HttpNegotiate_Release,
521 HttpNegotiate_BeginningTransaction,
522 HttpNegotiate_OnResponse,
523 HttpNegotiate_GetRootSecurityId
526 #define BINDINFO_THIS(iface) DEFINE_THIS(BSCallback, InternetBindInfo, iface)
528 static HRESULT WINAPI InternetBindInfo_QueryInterface(IInternetBindInfo *iface,
529 REFIID riid, void **ppv)
531 BSCallback *This = BINDINFO_THIS(iface);
532 return IBindStatusCallback_QueryInterface(STATUSCLB(This), riid, ppv);
535 static ULONG WINAPI InternetBindInfo_AddRef(IInternetBindInfo *iface)
537 BSCallback *This = BINDINFO_THIS(iface);
538 return IBindStatusCallback_AddRef(STATUSCLB(This));
541 static ULONG WINAPI InternetBindInfo_Release(IInternetBindInfo *iface)
543 BSCallback *This = BINDINFO_THIS(iface);
544 return IBindStatusCallback_Release(STATUSCLB(This));
547 static HRESULT WINAPI InternetBindInfo_GetBindInfo(IInternetBindInfo *iface,
548 DWORD *grfBINDF, BINDINFO *pbindinfo)
550 BSCallback *This = BINDINFO_THIS(iface);
551 FIXME("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
552 return E_NOTIMPL;
555 static HRESULT WINAPI InternetBindInfo_GetBindString(IInternetBindInfo *iface,
556 ULONG ulStringType, LPOLESTR *ppwzStr, ULONG cEl, ULONG *pcElFetched)
558 BSCallback *This = BINDINFO_THIS(iface);
559 FIXME("(%p)->(%u %p %u %p)\n", This, ulStringType, ppwzStr, cEl, pcElFetched);
560 return E_NOTIMPL;
563 #undef BINDINFO_THIS
565 static const IInternetBindInfoVtbl InternetBindInfoVtbl = {
566 InternetBindInfo_QueryInterface,
567 InternetBindInfo_AddRef,
568 InternetBindInfo_Release,
569 InternetBindInfo_GetBindInfo,
570 InternetBindInfo_GetBindString
573 #define SERVPROV_THIS(iface) DEFINE_THIS(BSCallback, ServiceProvider, iface)
575 static HRESULT WINAPI BSCServiceProvider_QueryInterface(IServiceProvider *iface,
576 REFIID riid, void **ppv)
578 BSCallback *This = SERVPROV_THIS(iface);
579 return IBindStatusCallback_QueryInterface(STATUSCLB(This), riid, ppv);
582 static ULONG WINAPI BSCServiceProvider_AddRef(IServiceProvider *iface)
584 BSCallback *This = SERVPROV_THIS(iface);
585 return IBindStatusCallback_AddRef(STATUSCLB(This));
588 static ULONG WINAPI BSCServiceProvider_Release(IServiceProvider *iface)
590 BSCallback *This = SERVPROV_THIS(iface);
591 return IBindStatusCallback_Release(STATUSCLB(This));
594 static HRESULT WINAPI BSCServiceProvider_QueryService(IServiceProvider *iface,
595 REFGUID guidService, REFIID riid, void **ppv)
597 BSCallback *This = SERVPROV_THIS(iface);
598 FIXME("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
599 return E_NOTIMPL;
602 #undef SERVPROV_THIS
604 static const IServiceProviderVtbl ServiceProviderVtbl = {
605 BSCServiceProvider_QueryInterface,
606 BSCServiceProvider_AddRef,
607 BSCServiceProvider_Release,
608 BSCServiceProvider_QueryService
611 BSCallback *create_bscallback(IMoniker *mon)
613 BSCallback *ret = mshtml_alloc(sizeof(BSCallback));
615 ret->lpBindStatusCallbackVtbl = &BindStatusCallbackVtbl;
616 ret->lpServiceProviderVtbl = &ServiceProviderVtbl;
617 ret->lpHttpNegotiate2Vtbl = &HttpNegotiate2Vtbl;
618 ret->lpInternetBindInfoVtbl = &InternetBindInfoVtbl;
619 ret->ref = 1;
620 ret->post_data = NULL;
621 ret->headers = NULL;
622 ret->post_data_len = 0;
623 ret->readed = 0;
624 ret->nschannel = NULL;
625 ret->nslistener = NULL;
626 ret->nscontext = NULL;
627 ret->nsstream = NULL;
628 ret->binding = NULL;
629 ret->doc = NULL;
631 if(mon)
632 IMoniker_AddRef(mon);
633 ret->mon = mon;
635 return ret;
638 static void parse_post_data(nsIInputStream *post_data_stream, LPWSTR *headers_ret,
639 HGLOBAL *post_data_ret, ULONG *post_data_len_ret)
641 PRUint32 post_data_len = 0, available = 0;
642 HGLOBAL post_data = NULL;
643 LPWSTR headers = NULL;
644 DWORD headers_len = 0, len;
645 const char *ptr, *ptr2, *post_data_end;
647 nsIInputStream_Available(post_data_stream, &available);
648 post_data = GlobalAlloc(0, available+1);
649 nsIInputStream_Read(post_data_stream, post_data, available, &post_data_len);
651 TRACE("post_data = %s\n", debugstr_an(post_data, post_data_len));
653 ptr = ptr2 = post_data;
654 post_data_end = (const char*)post_data+post_data_len;
656 while(ptr < post_data_end && (*ptr != '\r' || ptr[1] != '\n')) {
657 while(ptr < post_data_end && (*ptr != '\r' || ptr[1] != '\n'))
658 ptr++;
660 if(!*ptr) {
661 FIXME("*ptr = 0\n");
662 return;
665 ptr += 2;
667 if(ptr-ptr2 >= sizeof(CONTENT_LENGTH)
668 && CompareStringA(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
669 CONTENT_LENGTH, sizeof(CONTENT_LENGTH)-1,
670 ptr2, sizeof(CONTENT_LENGTH)-1) == CSTR_EQUAL) {
671 ptr2 = ptr;
672 continue;
675 len = MultiByteToWideChar(CP_ACP, 0, ptr2, ptr-ptr2, NULL, 0);
677 if(headers)
678 headers = mshtml_realloc(headers,(headers_len+len+1)*sizeof(WCHAR));
679 else
680 headers = mshtml_alloc((len+1)*sizeof(WCHAR));
682 len = MultiByteToWideChar(CP_ACP, 0, ptr2, ptr-ptr2, headers+headers_len, -1);
683 headers_len += len;
685 ptr2 = ptr;
688 headers[headers_len] = 0;
689 *headers_ret = headers;
691 if(ptr >= post_data_end-2) {
692 GlobalFree(post_data);
693 return;
696 ptr += 2;
698 if(headers_len) {
699 post_data_len -= ptr-(const char*)post_data;
700 memmove(post_data, ptr, post_data_len);
701 post_data = GlobalReAlloc(post_data, post_data_len+1, 0);
704 *post_data_ret = post_data;
705 *post_data_len_ret = post_data_len;
708 void hlink_frame_navigate(HTMLDocument *doc, IHlinkFrame *hlink_frame,
709 LPCWSTR uri, nsIInputStream *post_data_stream, DWORD hlnf)
711 BSCallback *callback;
712 IBindCtx *bindctx;
713 IMoniker *mon;
714 IHlink *hlink;
716 callback = create_bscallback(NULL);
718 if(post_data_stream) {
719 parse_post_data(post_data_stream, &callback->headers, &callback->post_data,
720 &callback->post_data_len);
721 TRACE("headers = %s post_data = %s\n", debugstr_w(callback->headers),
722 debugstr_an(callback->post_data, callback->post_data_len));
725 CreateAsyncBindCtx(0, STATUSCLB(callback), NULL, &bindctx);
727 hlink = Hlink_Create();
729 CreateURLMoniker(NULL, uri, &mon);
730 IHlink_SetMonikerReference(hlink, 0, mon, NULL);
732 if(hlnf & HLNF_OPENINNEWWINDOW) {
733 static const WCHAR wszBlank[] = {'_','b','l','a','n','k',0};
734 IHlink_SetTargetFrameName(hlink, wszBlank); /* FIXME */
737 IHlinkFrame_Navigate(hlink_frame, hlnf, bindctx, STATUSCLB(callback), hlink);
739 IBindCtx_Release(bindctx);
740 IBindStatusCallback_Release(STATUSCLB(callback));
741 IMoniker_Release(mon);
745 HRESULT start_binding(BSCallback *bscallback)
747 IStream *str = NULL;
748 IBindCtx *bctx;
749 HRESULT hres;
751 hres = CreateAsyncBindCtx(0, STATUSCLB(bscallback), NULL, &bctx);
752 if(FAILED(hres)) {
753 WARN("CreateAsyncBindCtx failed: %08x\n", hres);
754 return hres;
757 hres = IMoniker_BindToStorage(bscallback->mon, bctx, NULL, &IID_IStream, (void**)&str);
758 IBindCtx_Release(bctx);
759 if(FAILED(hres)) {
760 WARN("BindToStorage failed: %08x\n", hres);
761 return hres;
764 if(str)
765 IStream_Release(str);
767 IMoniker_Release(bscallback->mon);
768 bscallback->mon = NULL;
769 return S_OK;
772 void set_document_bscallback(HTMLDocument *doc, BSCallback *callback)
774 if(doc->bscallback) {
775 if(doc->bscallback->binding)
776 IBinding_Abort(doc->bscallback->binding);
777 doc->bscallback->doc = NULL;
778 IBindStatusCallback_Release(STATUSCLB(doc->bscallback));
781 doc->bscallback = callback;
783 if(callback) {
784 IBindStatusCallback_AddRef(STATUSCLB(callback));
785 callback->doc = doc;