urlmon: Improve ObtainUserAgentString conformance to native.
[wine.git] / dlls / mshtml / navigate.c
blob3e05fdc84def0f65df8c545629ca03bddb84046d
1 /*
2 * Copyright 2006-2007 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"
31 #include "hlguids.h"
33 #include "wine/debug.h"
34 #include "wine/unicode.h"
36 #include "mshtml_private.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
40 #define CONTENT_LENGTH "Content-Length"
41 #define UTF16_STR "utf-16"
43 #define NSINSTREAM(x) ((nsIInputStream*) &(x)->lpInputStreamVtbl)
45 #define NSINSTREAM_THIS(iface) DEFINE_THIS(nsProtocolStream, InputStream, iface)
47 static nsresult NSAPI nsInputStream_QueryInterface(nsIInputStream *iface, nsIIDRef riid,
48 nsQIResult result)
50 nsProtocolStream *This = NSINSTREAM_THIS(iface);
52 *result = NULL;
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);
62 if(*result) {
63 nsIInputStream_AddRef(NSINSTREAM(This));
64 return NS_OK;
67 WARN("unsupported interface %s\n", debugstr_guid(riid));
68 return NS_NOINTERFACE;
71 static nsrefcnt NSAPI nsInputStream_AddRef(nsIInputStream *iface)
73 nsProtocolStream *This = NSINSTREAM_THIS(iface);
74 LONG ref = InterlockedIncrement(&This->ref);
76 TRACE("(%p) ref=%d\n", This, ref);
78 return ref;
82 static nsrefcnt NSAPI nsInputStream_Release(nsIInputStream *iface)
84 nsProtocolStream *This = NSINSTREAM_THIS(iface);
85 LONG ref = InterlockedDecrement(&This->ref);
87 TRACE("(%p) ref=%d\n", This, ref);
89 if(!ref)
90 mshtml_free(This);
92 return ref;
95 static nsresult NSAPI nsInputStream_Close(nsIInputStream *iface)
97 nsProtocolStream *This = NSINSTREAM_THIS(iface);
98 FIXME("(%p)\n", This);
99 return NS_ERROR_NOT_IMPLEMENTED;
102 static nsresult NSAPI nsInputStream_Available(nsIInputStream *iface, PRUint32 *_retval)
104 nsProtocolStream *This = NSINSTREAM_THIS(iface);
105 FIXME("(%p)->(%p)\n", This, _retval);
106 return NS_ERROR_NOT_IMPLEMENTED;
109 static nsresult NSAPI nsInputStream_Read(nsIInputStream *iface, char *aBuf, PRUint32 aCount,
110 PRUint32 *_retval)
112 nsProtocolStream *This = NSINSTREAM_THIS(iface);
114 TRACE("(%p)->(%p %d %p)\n", This, aBuf, aCount, _retval);
116 /* Gecko always calls Read with big enough buffer */
117 if(aCount < This->buf_size)
118 FIXME("aCount < This->buf_size\n");
120 *_retval = This->buf_size;
121 if(This->buf_size)
122 memcpy(aBuf, This->buf, This->buf_size);
123 This->buf_size = 0;
125 return NS_OK;
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)
132 nsProtocolStream *This = NSINSTREAM_THIS(iface);
133 PRUint32 written = 0;
134 nsresult nsres;
136 TRACE("(%p)->(%p %p %d %p)\n", This, aWriter, aClousure, aCount, _retval);
138 if(!This->buf_size)
139 return S_OK;
141 if(This->buf_size > aCount)
142 FIXME("buf_size > aCount\n");
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);
150 This->buf_size -= written;
152 *_retval = written;
153 return nsres;
156 static nsresult NSAPI nsInputStream_IsNonBlocking(nsIInputStream *iface, PRBool *_retval)
158 nsProtocolStream *This = NSINSTREAM_THIS(iface);
159 FIXME("(%p)->(%p)\n", This, _retval);
160 return NS_ERROR_NOT_IMPLEMENTED;
163 #undef NSINSTREAM_THIS
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
176 static nsProtocolStream *create_nsprotocol_stream(void)
178 nsProtocolStream *ret = mshtml_alloc(sizeof(nsProtocolStream));
180 ret->lpInputStreamVtbl = &nsInputStreamVtbl;
181 ret->ref = 1;
182 ret->buf_size = 0;
184 return ret;
187 static HRESULT read_stream_data(BSCallback *This, IStream *stream)
189 nsresult nsres;
190 HRESULT hres;
192 if(!This->nslistener) {
193 BYTE buf[1024];
194 DWORD read;
196 do {
197 read = 0;
198 hres = IStream_Read(stream, buf, sizeof(buf), &read);
199 }while(hres == S_OK && read);
201 return S_OK;
204 if(!This->nsstream)
205 This->nsstream = create_nsprotocol_stream();
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;
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));
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);
225 This->readed += This->nsstream->buf_size;
227 nsres = nsIStreamListener_OnDataAvailable(This->nslistener,
228 (nsIRequest*)NSCHANNEL(This->nschannel), This->nscontext,
229 NSINSTREAM(This->nsstream), This->readed-This->nsstream->buf_size,
230 This->nsstream->buf_size);
231 if(NS_FAILED(nsres))
232 ERR("OnDataAvailable failed: %08x\n", nsres);
234 if(This->nsstream->buf_size)
235 FIXME("buffer is not empty!\n");
236 }while(hres == S_OK);
238 return S_OK;
241 static void add_nsrequest(BSCallback *This)
243 if(This->nschannel && This->nschannel->load_group) {
244 nsresult nsres = nsILoadGroup_AddRequest(This->nschannel->load_group,
245 (nsIRequest*)NSCHANNEL(This->nschannel), This->nscontext);
247 if(NS_FAILED(nsres))
248 ERR("AddRequest failed:%08x\n", nsres);
252 #define STATUSCLB_THIS(iface) DEFINE_THIS(BSCallback, BindStatusCallback, iface)
254 static HRESULT WINAPI BindStatusCallback_QueryInterface(IBindStatusCallback *iface,
255 REFIID riid, void **ppv)
257 BSCallback *This = STATUSCLB_THIS(iface);
259 *ppv = NULL;
260 if(IsEqualGUID(&IID_IUnknown, riid)) {
261 TRACE("(%p)->(IID_IUnknown, %p)\n", This, ppv);
262 *ppv = STATUSCLB(This);
263 }else if(IsEqualGUID(&IID_IBindStatusCallback, riid)) {
264 TRACE("(%p)->(IID_IBindStatusCallback, %p)\n", This, ppv);
265 *ppv = STATUSCLB(This);
266 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
267 TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
268 *ppv = SERVPROV(This);
269 }else if(IsEqualGUID(&IID_IHttpNegotiate, riid)) {
270 TRACE("(%p)->(IID_IHttpNegotiate %p)\n", This, ppv);
271 *ppv = HTTPNEG(This);
272 }else if(IsEqualGUID(&IID_IHttpNegotiate2, riid)) {
273 TRACE("(%p)->(IID_IHttpNegotiate2 %p)\n", This, ppv);
274 *ppv = HTTPNEG(This);
275 }else if(IsEqualGUID(&IID_IInternetBindInfo, riid)) {
276 TRACE("(%p)->(IID_IInternetBindInfo %p)\n", This, ppv);
277 *ppv = BINDINFO(This);
280 if(*ppv) {
281 IBindStatusCallback_AddRef(STATUSCLB(This));
282 return S_OK;
285 TRACE("Unsupported riid = %s\n", debugstr_guid(riid));
286 return E_NOINTERFACE;
289 static ULONG WINAPI BindStatusCallback_AddRef(IBindStatusCallback *iface)
291 BSCallback *This = STATUSCLB_THIS(iface);
292 LONG ref = InterlockedIncrement(&This->ref);
294 TRACE("(%p) ref = %d\n", This, ref);
296 return ref;
299 static ULONG WINAPI BindStatusCallback_Release(IBindStatusCallback *iface)
301 BSCallback *This = STATUSCLB_THIS(iface);
302 LONG ref = InterlockedDecrement(&This->ref);
304 TRACE("(%p) ref = %d\n", This, ref);
306 if(!ref) {
307 if(This->post_data)
308 GlobalFree(This->post_data);
309 if(This->nschannel)
310 nsIChannel_Release(NSCHANNEL(This->nschannel));
311 if(This->nslistener)
312 nsIStreamListener_Release(This->nslistener);
313 if(This->nscontext)
314 nsISupports_Release(This->nscontext);
315 if(This->nsstream)
316 nsIInputStream_Release(NSINSTREAM(This->nsstream));
317 if(This->mon)
318 IMoniker_Release(This->mon);
319 if(This->binding)
320 IBinding_Release(This->binding);
321 mshtml_free(This->headers);
322 mshtml_free(This);
325 return ref;
328 static HRESULT WINAPI BindStatusCallback_OnStartBinding(IBindStatusCallback *iface,
329 DWORD dwReserved, IBinding *pbind)
331 BSCallback *This = STATUSCLB_THIS(iface);
333 TRACE("(%p)->(%d %p)\n", This, dwReserved, pbind);
335 IBinding_AddRef(pbind);
336 This->binding = pbind;
338 add_nsrequest(This);
340 return S_OK;
343 static HRESULT WINAPI BindStatusCallback_GetPriority(IBindStatusCallback *iface, LONG *pnPriority)
345 BSCallback *This = STATUSCLB_THIS(iface);
346 FIXME("(%p)->(%p)\n", This, pnPriority);
347 return E_NOTIMPL;
350 static HRESULT WINAPI BindStatusCallback_OnLowResource(IBindStatusCallback *iface, DWORD reserved)
352 BSCallback *This = STATUSCLB_THIS(iface);
353 FIXME("(%p)->(%d)\n", This, reserved);
354 return E_NOTIMPL;
357 static HRESULT WINAPI BindStatusCallback_OnProgress(IBindStatusCallback *iface, ULONG ulProgress,
358 ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
360 BSCallback *This = STATUSCLB_THIS(iface);
362 TRACE("%p)->(%u %u %u %s)\n", This, ulProgress, ulProgressMax, ulStatusCode,
363 debugstr_w(szStatusText));
365 switch(ulStatusCode) {
366 case BINDSTATUS_MIMETYPEAVAILABLE: {
367 int len;
369 if(!This->nschannel)
370 return S_OK;
371 mshtml_free(This->nschannel->content);
373 len = WideCharToMultiByte(CP_ACP, 0, szStatusText, -1, NULL, 0, NULL, NULL);
374 This->nschannel->content = mshtml_alloc(len*sizeof(WCHAR));
375 WideCharToMultiByte(CP_ACP, 0, szStatusText, -1, This->nschannel->content, -1, NULL, NULL);
379 return S_OK;
382 static HRESULT WINAPI BindStatusCallback_OnStopBinding(IBindStatusCallback *iface,
383 HRESULT hresult, LPCWSTR szError)
385 BSCallback *This = STATUSCLB_THIS(iface);
387 TRACE("(%p)->(%08x %s)\n", This, hresult, debugstr_w(szError));
389 if(This->binding) {
390 IBinding_Release(This->binding);
391 This->binding = NULL;
394 if(This->nslistener) {
395 nsIStreamListener_OnStopRequest(This->nslistener, (nsIRequest*)NSCHANNEL(This->nschannel),
396 This->nscontext, NS_OK);
398 if(This->nschannel->load_group) {
399 nsresult nsres;
401 nsres = nsILoadGroup_RemoveRequest(This->nschannel->load_group,
402 (nsIRequest*)NSCHANNEL(This->nschannel), NULL, NS_OK);
403 if(NS_FAILED(nsres))
404 ERR("RemoveRequest failed: %08x\n", nsres);
408 if(This->doc && !This->doc->nscontainer) {
409 task_t *task = mshtml_alloc(sizeof(task_t));
411 task->doc = This->doc;
412 task->task_id = TASK_PARSECOMPLETE;
413 task->next = NULL;
416 * This should be done in the worker thread that parses HTML,
417 * but we don't have such thread.
419 push_task(task);
422 return S_OK;
425 static HRESULT WINAPI BindStatusCallback_GetBindInfo(IBindStatusCallback *iface,
426 DWORD *grfBINDF, BINDINFO *pbindinfo)
428 BSCallback *This = STATUSCLB_THIS(iface);
429 DWORD size;
431 TRACE("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
433 *grfBINDF = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA;
435 size = pbindinfo->cbSize;
436 memset(pbindinfo, 0, size);
437 pbindinfo->cbSize = size;
439 pbindinfo->cbstgmedData = This->post_data_len;
440 pbindinfo->dwCodePage = CP_UTF8;
441 pbindinfo->dwOptions = 0x80000;
443 if(This->post_data) {
444 pbindinfo->dwBindVerb = BINDVERB_POST;
446 pbindinfo->stgmedData.tymed = TYMED_HGLOBAL;
447 pbindinfo->stgmedData.u.hGlobal = This->post_data;
448 pbindinfo->stgmedData.pUnkForRelease = (IUnknown*)STATUSCLB(This);
449 IBindStatusCallback_AddRef(STATUSCLB(This));
452 return S_OK;
455 static HRESULT WINAPI BindStatusCallback_OnDataAvailable(IBindStatusCallback *iface,
456 DWORD grfBSCF, DWORD dwSize, FORMATETC *pformatetc, STGMEDIUM *pstgmed)
458 BSCallback *This = STATUSCLB_THIS(iface);
460 TRACE("(%p)->(%08x %d %p %p)\n", This, grfBSCF, dwSize, pformatetc, pstgmed);
462 return read_stream_data(This, pstgmed->u.pstm);
465 static HRESULT WINAPI BindStatusCallback_OnObjectAvailable(IBindStatusCallback *iface,
466 REFIID riid, IUnknown *punk)
468 BSCallback *This = STATUSCLB_THIS(iface);
469 FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), punk);
470 return E_NOTIMPL;
473 #undef STATUSCLB_THIS
475 static const IBindStatusCallbackVtbl BindStatusCallbackVtbl = {
476 BindStatusCallback_QueryInterface,
477 BindStatusCallback_AddRef,
478 BindStatusCallback_Release,
479 BindStatusCallback_OnStartBinding,
480 BindStatusCallback_GetPriority,
481 BindStatusCallback_OnLowResource,
482 BindStatusCallback_OnProgress,
483 BindStatusCallback_OnStopBinding,
484 BindStatusCallback_GetBindInfo,
485 BindStatusCallback_OnDataAvailable,
486 BindStatusCallback_OnObjectAvailable
489 #define HTTPNEG_THIS(iface) DEFINE_THIS(BSCallback, HttpNegotiate2, iface)
491 static HRESULT WINAPI HttpNegotiate_QueryInterface(IHttpNegotiate2 *iface,
492 REFIID riid, void **ppv)
494 BSCallback *This = HTTPNEG_THIS(iface);
495 return IBindStatusCallback_QueryInterface(STATUSCLB(This), riid, ppv);
498 static ULONG WINAPI HttpNegotiate_AddRef(IHttpNegotiate2 *iface)
500 BSCallback *This = HTTPNEG_THIS(iface);
501 return IBindStatusCallback_AddRef(STATUSCLB(This));
504 static ULONG WINAPI HttpNegotiate_Release(IHttpNegotiate2 *iface)
506 BSCallback *This = HTTPNEG_THIS(iface);
507 return IBindStatusCallback_Release(STATUSCLB(This));
510 static HRESULT WINAPI HttpNegotiate_BeginningTransaction(IHttpNegotiate2 *iface,
511 LPCWSTR szURL, LPCWSTR szHeaders, DWORD dwReserved, LPWSTR *pszAdditionalHeaders)
513 BSCallback *This = HTTPNEG_THIS(iface);
514 DWORD size;
516 TRACE("(%p)->(%s %s %d %p)\n", This, debugstr_w(szURL), debugstr_w(szHeaders),
517 dwReserved, pszAdditionalHeaders);
519 if(!This->headers) {
520 *pszAdditionalHeaders = NULL;
521 return S_OK;
524 size = (strlenW(This->headers)+1)*sizeof(WCHAR);
525 *pszAdditionalHeaders = CoTaskMemAlloc(size);
526 memcpy(*pszAdditionalHeaders, This->headers, size);
528 return S_OK;
531 static HRESULT WINAPI HttpNegotiate_OnResponse(IHttpNegotiate2 *iface, DWORD dwResponseCode,
532 LPCWSTR szResponseHeaders, LPCWSTR szRequestHeaders, LPWSTR *pszAdditionalRequestHeaders)
534 BSCallback *This = HTTPNEG_THIS(iface);
535 FIXME("(%p)->(%d %s %s %p)\n", This, dwResponseCode, debugstr_w(szResponseHeaders),
536 debugstr_w(szRequestHeaders), pszAdditionalRequestHeaders);
537 return E_NOTIMPL;
540 static HRESULT WINAPI HttpNegotiate_GetRootSecurityId(IHttpNegotiate2 *iface,
541 BYTE *pbSecurityId, DWORD *pcbSecurityId, DWORD_PTR dwReserved)
543 BSCallback *This = HTTPNEG_THIS(iface);
544 FIXME("(%p)->(%p %p %ld)\n", This, pbSecurityId, pcbSecurityId, dwReserved);
545 return E_NOTIMPL;
548 #undef HTTPNEG
550 static const IHttpNegotiate2Vtbl HttpNegotiate2Vtbl = {
551 HttpNegotiate_QueryInterface,
552 HttpNegotiate_AddRef,
553 HttpNegotiate_Release,
554 HttpNegotiate_BeginningTransaction,
555 HttpNegotiate_OnResponse,
556 HttpNegotiate_GetRootSecurityId
559 #define BINDINFO_THIS(iface) DEFINE_THIS(BSCallback, InternetBindInfo, iface)
561 static HRESULT WINAPI InternetBindInfo_QueryInterface(IInternetBindInfo *iface,
562 REFIID riid, void **ppv)
564 BSCallback *This = BINDINFO_THIS(iface);
565 return IBindStatusCallback_QueryInterface(STATUSCLB(This), riid, ppv);
568 static ULONG WINAPI InternetBindInfo_AddRef(IInternetBindInfo *iface)
570 BSCallback *This = BINDINFO_THIS(iface);
571 return IBindStatusCallback_AddRef(STATUSCLB(This));
574 static ULONG WINAPI InternetBindInfo_Release(IInternetBindInfo *iface)
576 BSCallback *This = BINDINFO_THIS(iface);
577 return IBindStatusCallback_Release(STATUSCLB(This));
580 static HRESULT WINAPI InternetBindInfo_GetBindInfo(IInternetBindInfo *iface,
581 DWORD *grfBINDF, BINDINFO *pbindinfo)
583 BSCallback *This = BINDINFO_THIS(iface);
584 FIXME("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
585 return E_NOTIMPL;
588 static HRESULT WINAPI InternetBindInfo_GetBindString(IInternetBindInfo *iface,
589 ULONG ulStringType, LPOLESTR *ppwzStr, ULONG cEl, ULONG *pcElFetched)
591 BSCallback *This = BINDINFO_THIS(iface);
592 FIXME("(%p)->(%u %p %u %p)\n", This, ulStringType, ppwzStr, cEl, pcElFetched);
593 return E_NOTIMPL;
596 #undef BINDINFO_THIS
598 static const IInternetBindInfoVtbl InternetBindInfoVtbl = {
599 InternetBindInfo_QueryInterface,
600 InternetBindInfo_AddRef,
601 InternetBindInfo_Release,
602 InternetBindInfo_GetBindInfo,
603 InternetBindInfo_GetBindString
606 #define SERVPROV_THIS(iface) DEFINE_THIS(BSCallback, ServiceProvider, iface)
608 static HRESULT WINAPI BSCServiceProvider_QueryInterface(IServiceProvider *iface,
609 REFIID riid, void **ppv)
611 BSCallback *This = SERVPROV_THIS(iface);
612 return IBindStatusCallback_QueryInterface(STATUSCLB(This), riid, ppv);
615 static ULONG WINAPI BSCServiceProvider_AddRef(IServiceProvider *iface)
617 BSCallback *This = SERVPROV_THIS(iface);
618 return IBindStatusCallback_AddRef(STATUSCLB(This));
621 static ULONG WINAPI BSCServiceProvider_Release(IServiceProvider *iface)
623 BSCallback *This = SERVPROV_THIS(iface);
624 return IBindStatusCallback_Release(STATUSCLB(This));
627 static HRESULT WINAPI BSCServiceProvider_QueryService(IServiceProvider *iface,
628 REFGUID guidService, REFIID riid, void **ppv)
630 BSCallback *This = SERVPROV_THIS(iface);
631 FIXME("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
632 return E_NOTIMPL;
635 #undef SERVPROV_THIS
637 static const IServiceProviderVtbl ServiceProviderVtbl = {
638 BSCServiceProvider_QueryInterface,
639 BSCServiceProvider_AddRef,
640 BSCServiceProvider_Release,
641 BSCServiceProvider_QueryService
644 BSCallback *create_bscallback(IMoniker *mon)
646 BSCallback *ret = mshtml_alloc(sizeof(BSCallback));
648 ret->lpBindStatusCallbackVtbl = &BindStatusCallbackVtbl;
649 ret->lpServiceProviderVtbl = &ServiceProviderVtbl;
650 ret->lpHttpNegotiate2Vtbl = &HttpNegotiate2Vtbl;
651 ret->lpInternetBindInfoVtbl = &InternetBindInfoVtbl;
652 ret->ref = 1;
653 ret->post_data = NULL;
654 ret->headers = NULL;
655 ret->post_data_len = 0;
656 ret->readed = 0;
657 ret->nschannel = NULL;
658 ret->nslistener = NULL;
659 ret->nscontext = NULL;
660 ret->nsstream = NULL;
661 ret->binding = NULL;
662 ret->doc = NULL;
664 if(mon)
665 IMoniker_AddRef(mon);
666 ret->mon = mon;
668 return ret;
671 static void parse_post_data(nsIInputStream *post_data_stream, LPWSTR *headers_ret,
672 HGLOBAL *post_data_ret, ULONG *post_data_len_ret)
674 PRUint32 post_data_len = 0, available = 0;
675 HGLOBAL post_data = NULL;
676 LPWSTR headers = NULL;
677 DWORD headers_len = 0, len;
678 const char *ptr, *ptr2, *post_data_end;
680 nsIInputStream_Available(post_data_stream, &available);
681 post_data = GlobalAlloc(0, available+1);
682 nsIInputStream_Read(post_data_stream, post_data, available, &post_data_len);
684 TRACE("post_data = %s\n", debugstr_an(post_data, post_data_len));
686 ptr = ptr2 = post_data;
687 post_data_end = (const char*)post_data+post_data_len;
689 while(ptr < post_data_end && (*ptr != '\r' || ptr[1] != '\n')) {
690 while(ptr < post_data_end && (*ptr != '\r' || ptr[1] != '\n'))
691 ptr++;
693 if(!*ptr) {
694 FIXME("*ptr = 0\n");
695 return;
698 ptr += 2;
700 if(ptr-ptr2 >= sizeof(CONTENT_LENGTH)
701 && CompareStringA(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
702 CONTENT_LENGTH, sizeof(CONTENT_LENGTH)-1,
703 ptr2, sizeof(CONTENT_LENGTH)-1) == CSTR_EQUAL) {
704 ptr2 = ptr;
705 continue;
708 len = MultiByteToWideChar(CP_ACP, 0, ptr2, ptr-ptr2, NULL, 0);
710 if(headers)
711 headers = mshtml_realloc(headers,(headers_len+len+1)*sizeof(WCHAR));
712 else
713 headers = mshtml_alloc((len+1)*sizeof(WCHAR));
715 len = MultiByteToWideChar(CP_ACP, 0, ptr2, ptr-ptr2, headers+headers_len, -1);
716 headers_len += len;
718 ptr2 = ptr;
721 headers[headers_len] = 0;
722 *headers_ret = headers;
724 if(ptr >= post_data_end-2) {
725 GlobalFree(post_data);
726 return;
729 ptr += 2;
731 if(headers_len) {
732 post_data_len -= ptr-(const char*)post_data;
733 memmove(post_data, ptr, post_data_len);
734 post_data = GlobalReAlloc(post_data, post_data_len+1, 0);
737 *post_data_ret = post_data;
738 *post_data_len_ret = post_data_len;
741 void hlink_frame_navigate(HTMLDocument *doc, IHlinkFrame *hlink_frame,
742 LPCWSTR uri, nsIInputStream *post_data_stream, DWORD hlnf)
744 BSCallback *callback;
745 IBindCtx *bindctx;
746 IMoniker *mon;
747 IHlink *hlink;
748 HRESULT hr;
750 callback = create_bscallback(NULL);
752 if(post_data_stream) {
753 parse_post_data(post_data_stream, &callback->headers, &callback->post_data,
754 &callback->post_data_len);
755 TRACE("headers = %s post_data = %s\n", debugstr_w(callback->headers),
756 debugstr_an(callback->post_data, callback->post_data_len));
759 hr = CreateAsyncBindCtx(0, STATUSCLB(callback), NULL, &bindctx);
760 if (FAILED(hr)) {
761 IBindStatusCallback_Release(STATUSCLB(callback));
762 return;
765 hr = CoCreateInstance(&CLSID_StdHlink, NULL, CLSCTX_INPROC_SERVER, &IID_IHlink, (LPVOID*)&hlink);
766 if (FAILED(hr)) {
767 IBindCtx_Release(bindctx);
768 IBindStatusCallback_Release(STATUSCLB(callback));
769 return;
772 hr = CreateURLMoniker(NULL, uri, &mon);
773 if (SUCCEEDED(hr)) {
774 IHlink_SetMonikerReference(hlink, 0, mon, NULL);
776 if(hlnf & HLNF_OPENINNEWWINDOW) {
777 static const WCHAR wszBlank[] = {'_','b','l','a','n','k',0};
778 IHlink_SetTargetFrameName(hlink, wszBlank); /* FIXME */
781 IHlinkFrame_Navigate(hlink_frame, hlnf, bindctx, STATUSCLB(callback), hlink);
783 IMoniker_Release(mon);
786 IBindCtx_Release(bindctx);
787 IBindStatusCallback_Release(STATUSCLB(callback));
790 HRESULT start_binding(BSCallback *bscallback)
792 IStream *str = NULL;
793 IBindCtx *bctx;
794 HRESULT hres;
796 hres = CreateAsyncBindCtx(0, STATUSCLB(bscallback), NULL, &bctx);
797 if(FAILED(hres)) {
798 WARN("CreateAsyncBindCtx failed: %08x\n", hres);
799 nsIStreamListener_OnStopRequest(bscallback->nslistener, (nsIRequest*)NSCHANNEL(bscallback->nschannel),
800 bscallback->nscontext, NS_OK);
801 return hres;
804 hres = IMoniker_BindToStorage(bscallback->mon, bctx, NULL, &IID_IStream, (void**)&str);
805 IBindCtx_Release(bctx);
806 if(FAILED(hres)) {
807 WARN("BindToStorage failed: %08x\n", hres);
808 nsIStreamListener_OnStopRequest(bscallback->nslistener, (nsIRequest*)NSCHANNEL(bscallback->nschannel),
809 bscallback->nscontext, NS_OK);
810 return hres;
813 if(str)
814 IStream_Release(str);
816 IMoniker_Release(bscallback->mon);
817 bscallback->mon = NULL;
818 return S_OK;
821 void set_document_bscallback(HTMLDocument *doc, BSCallback *callback)
823 if(doc->bscallback) {
824 if(doc->bscallback->binding)
825 IBinding_Abort(doc->bscallback->binding);
826 doc->bscallback->doc = NULL;
827 IBindStatusCallback_Release(STATUSCLB(doc->bscallback));
830 doc->bscallback = callback;
832 if(callback) {
833 IBindStatusCallback_AddRef(STATUSCLB(callback));
834 callback->doc = doc;
838 HRESULT load_stream(BSCallback *bscallback, IStream *stream)
840 HRESULT hres;
842 const char text_html[] = "text/html";
844 add_nsrequest(bscallback);
846 if(bscallback->nschannel) {
847 bscallback->nschannel->content = mshtml_alloc(sizeof(text_html));
848 memcpy(bscallback->nschannel->content, text_html, sizeof(text_html));
851 hres = read_stream_data(bscallback, stream);
852 IBindStatusCallback_OnStopBinding(STATUSCLB(bscallback), hres, ERROR_SUCCESS);
854 return hres;