Release 1.1.37.
[wine/gsoc-2012-control.git] / dlls / mshtml / navigate.c
blobb85de92e33d958adf599ff9e67e1469e67257e89
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 "winreg.h"
31 #include "ole2.h"
32 #include "hlguids.h"
33 #include "shlguid.h"
34 #include "wininet.h"
35 #include "shlwapi.h"
37 #include "wine/debug.h"
38 #include "wine/unicode.h"
40 #include "mshtml_private.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
44 #define CONTENT_LENGTH "Content-Length"
45 #define UTF16_STR "utf-16"
47 typedef struct {
48 const nsIInputStreamVtbl *lpInputStreamVtbl;
50 LONG ref;
52 char buf[1024];
53 DWORD buf_size;
54 } nsProtocolStream;
56 #define NSINSTREAM(x) ((nsIInputStream*) &(x)->lpInputStreamVtbl)
58 typedef struct {
59 void (*destroy)(BSCallback*);
60 HRESULT (*init_bindinfo)(BSCallback*);
61 HRESULT (*start_binding)(BSCallback*);
62 HRESULT (*stop_binding)(BSCallback*,HRESULT);
63 HRESULT (*read_data)(BSCallback*,IStream*);
64 HRESULT (*on_progress)(BSCallback*,ULONG,LPCWSTR);
65 HRESULT (*on_response)(BSCallback*,DWORD);
66 } BSCallbackVtbl;
68 struct BSCallback {
69 const IBindStatusCallbackVtbl *lpBindStatusCallbackVtbl;
70 const IServiceProviderVtbl *lpServiceProviderVtbl;
71 const IHttpNegotiate2Vtbl *lpHttpNegotiate2Vtbl;
72 const IInternetBindInfoVtbl *lpInternetBindInfoVtbl;
74 const BSCallbackVtbl *vtbl;
76 LONG ref;
78 LPWSTR headers;
79 HGLOBAL post_data;
80 ULONG post_data_len;
81 ULONG readed;
82 DWORD bindf;
83 BOOL bindinfo_ready;
85 IMoniker *mon;
86 IBinding *binding;
88 HTMLDocumentNode *doc;
90 struct list entry;
93 #define NSINSTREAM_THIS(iface) DEFINE_THIS(nsProtocolStream, InputStream, iface)
95 static nsresult NSAPI nsInputStream_QueryInterface(nsIInputStream *iface, nsIIDRef riid,
96 nsQIResult result)
98 nsProtocolStream *This = NSINSTREAM_THIS(iface);
100 *result = NULL;
102 if(IsEqualGUID(&IID_nsISupports, riid)) {
103 TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
104 *result = NSINSTREAM(This);
105 }else if(IsEqualGUID(&IID_nsIInputStream, riid)) {
106 TRACE("(%p)->(IID_nsIInputStream %p)\n", This, result);
107 *result = NSINSTREAM(This);
110 if(*result) {
111 nsIInputStream_AddRef(NSINSTREAM(This));
112 return NS_OK;
115 WARN("unsupported interface %s\n", debugstr_guid(riid));
116 return NS_NOINTERFACE;
119 static nsrefcnt NSAPI nsInputStream_AddRef(nsIInputStream *iface)
121 nsProtocolStream *This = NSINSTREAM_THIS(iface);
122 LONG ref = InterlockedIncrement(&This->ref);
124 TRACE("(%p) ref=%d\n", This, ref);
126 return ref;
130 static nsrefcnt NSAPI nsInputStream_Release(nsIInputStream *iface)
132 nsProtocolStream *This = NSINSTREAM_THIS(iface);
133 LONG ref = InterlockedDecrement(&This->ref);
135 TRACE("(%p) ref=%d\n", This, ref);
137 if(!ref)
138 heap_free(This);
140 return ref;
143 static nsresult NSAPI nsInputStream_Close(nsIInputStream *iface)
145 nsProtocolStream *This = NSINSTREAM_THIS(iface);
146 FIXME("(%p)\n", This);
147 return NS_ERROR_NOT_IMPLEMENTED;
150 static nsresult NSAPI nsInputStream_Available(nsIInputStream *iface, PRUint32 *_retval)
152 nsProtocolStream *This = NSINSTREAM_THIS(iface);
153 FIXME("(%p)->(%p)\n", This, _retval);
154 return NS_ERROR_NOT_IMPLEMENTED;
157 static nsresult NSAPI nsInputStream_Read(nsIInputStream *iface, char *aBuf, PRUint32 aCount,
158 PRUint32 *_retval)
160 nsProtocolStream *This = NSINSTREAM_THIS(iface);
161 DWORD read = aCount;
163 TRACE("(%p)->(%p %d %p)\n", This, aBuf, aCount, _retval);
165 if(read > This->buf_size)
166 read = This->buf_size;
168 if(read) {
169 memcpy(aBuf, This->buf, read);
170 if(read < This->buf_size)
171 memmove(This->buf, This->buf+read, This->buf_size-read);
172 This->buf_size -= read;
175 *_retval = read;
176 return NS_OK;
179 static nsresult NSAPI nsInputStream_ReadSegments(nsIInputStream *iface,
180 nsresult (WINAPI *aWriter)(nsIInputStream*,void*,const char*,PRUint32,PRUint32,PRUint32*),
181 void *aClousure, PRUint32 aCount, PRUint32 *_retval)
183 nsProtocolStream *This = NSINSTREAM_THIS(iface);
184 PRUint32 written = 0;
185 nsresult nsres;
187 TRACE("(%p)->(%p %p %d %p)\n", This, aWriter, aClousure, aCount, _retval);
189 if(!This->buf_size)
190 return S_OK;
192 if(aCount > This->buf_size)
193 aCount = This->buf_size;
195 nsres = aWriter(NSINSTREAM(This), aClousure, This->buf, 0, aCount, &written);
196 if(NS_FAILED(nsres))
197 TRACE("aWritter failed: %08x\n", nsres);
198 else if(written != This->buf_size)
199 FIXME("written %d != buf_size %d\n", written, This->buf_size);
201 This->buf_size -= written;
203 *_retval = written;
204 return nsres;
207 static nsresult NSAPI nsInputStream_IsNonBlocking(nsIInputStream *iface, PRBool *_retval)
209 nsProtocolStream *This = NSINSTREAM_THIS(iface);
210 FIXME("(%p)->(%p)\n", This, _retval);
211 return NS_ERROR_NOT_IMPLEMENTED;
214 #undef NSINSTREAM_THIS
216 static const nsIInputStreamVtbl nsInputStreamVtbl = {
217 nsInputStream_QueryInterface,
218 nsInputStream_AddRef,
219 nsInputStream_Release,
220 nsInputStream_Close,
221 nsInputStream_Available,
222 nsInputStream_Read,
223 nsInputStream_ReadSegments,
224 nsInputStream_IsNonBlocking
227 static nsProtocolStream *create_nsprotocol_stream(void)
229 nsProtocolStream *ret = heap_alloc(sizeof(nsProtocolStream));
231 ret->lpInputStreamVtbl = &nsInputStreamVtbl;
232 ret->ref = 1;
233 ret->buf_size = 0;
235 return ret;
238 #define STATUSCLB_THIS(iface) DEFINE_THIS(BSCallback, BindStatusCallback, iface)
240 static HRESULT WINAPI BindStatusCallback_QueryInterface(IBindStatusCallback *iface,
241 REFIID riid, void **ppv)
243 BSCallback *This = STATUSCLB_THIS(iface);
245 *ppv = NULL;
246 if(IsEqualGUID(&IID_IUnknown, riid)) {
247 TRACE("(%p)->(IID_IUnknown, %p)\n", This, ppv);
248 *ppv = STATUSCLB(This);
249 }else if(IsEqualGUID(&IID_IBindStatusCallback, riid)) {
250 TRACE("(%p)->(IID_IBindStatusCallback, %p)\n", This, ppv);
251 *ppv = STATUSCLB(This);
252 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
253 TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
254 *ppv = SERVPROV(This);
255 }else if(IsEqualGUID(&IID_IHttpNegotiate, riid)) {
256 TRACE("(%p)->(IID_IHttpNegotiate %p)\n", This, ppv);
257 *ppv = HTTPNEG(This);
258 }else if(IsEqualGUID(&IID_IHttpNegotiate2, riid)) {
259 TRACE("(%p)->(IID_IHttpNegotiate2 %p)\n", This, ppv);
260 *ppv = HTTPNEG(This);
261 }else if(IsEqualGUID(&IID_IInternetBindInfo, riid)) {
262 TRACE("(%p)->(IID_IInternetBindInfo %p)\n", This, ppv);
263 *ppv = BINDINFO(This);
266 if(*ppv) {
267 IBindStatusCallback_AddRef(STATUSCLB(This));
268 return S_OK;
271 TRACE("Unsupported riid = %s\n", debugstr_guid(riid));
272 return E_NOINTERFACE;
275 static ULONG WINAPI BindStatusCallback_AddRef(IBindStatusCallback *iface)
277 BSCallback *This = STATUSCLB_THIS(iface);
278 LONG ref = InterlockedIncrement(&This->ref);
280 TRACE("(%p) ref = %d\n", This, ref);
282 return ref;
285 static ULONG WINAPI BindStatusCallback_Release(IBindStatusCallback *iface)
287 BSCallback *This = STATUSCLB_THIS(iface);
288 LONG ref = InterlockedDecrement(&This->ref);
290 TRACE("(%p) ref = %d\n", This, ref);
292 if(!ref) {
293 if(This->post_data)
294 GlobalFree(This->post_data);
295 if(This->mon)
296 IMoniker_Release(This->mon);
297 if(This->binding)
298 IBinding_Release(This->binding);
299 list_remove(&This->entry);
300 heap_free(This->headers);
302 This->vtbl->destroy(This);
305 return ref;
308 static HRESULT WINAPI BindStatusCallback_OnStartBinding(IBindStatusCallback *iface,
309 DWORD dwReserved, IBinding *pbind)
311 BSCallback *This = STATUSCLB_THIS(iface);
313 TRACE("(%p)->(%d %p)\n", This, dwReserved, pbind);
315 IBinding_AddRef(pbind);
316 This->binding = pbind;
318 if(This->doc)
319 list_add_head(&This->doc->bindings, &This->entry);
321 return This->vtbl->start_binding(This);
324 static HRESULT WINAPI BindStatusCallback_GetPriority(IBindStatusCallback *iface, LONG *pnPriority)
326 BSCallback *This = STATUSCLB_THIS(iface);
327 FIXME("(%p)->(%p)\n", This, pnPriority);
328 return E_NOTIMPL;
331 static HRESULT WINAPI BindStatusCallback_OnLowResource(IBindStatusCallback *iface, DWORD reserved)
333 BSCallback *This = STATUSCLB_THIS(iface);
334 FIXME("(%p)->(%d)\n", This, reserved);
335 return E_NOTIMPL;
338 static HRESULT WINAPI BindStatusCallback_OnProgress(IBindStatusCallback *iface, ULONG ulProgress,
339 ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
341 BSCallback *This = STATUSCLB_THIS(iface);
343 TRACE("%p)->(%u %u %u %s)\n", This, ulProgress, ulProgressMax, ulStatusCode,
344 debugstr_w(szStatusText));
346 return This->vtbl->on_progress(This, ulStatusCode, szStatusText);
349 static HRESULT WINAPI BindStatusCallback_OnStopBinding(IBindStatusCallback *iface,
350 HRESULT hresult, LPCWSTR szError)
352 BSCallback *This = STATUSCLB_THIS(iface);
353 HRESULT hres;
355 TRACE("(%p)->(%08x %s)\n", This, hresult, debugstr_w(szError));
357 /* NOTE: IE7 calls GetBindResult here */
359 hres = This->vtbl->stop_binding(This, hresult);
361 if(This->binding) {
362 IBinding_Release(This->binding);
363 This->binding = NULL;
366 list_remove(&This->entry);
367 This->doc = NULL;
369 return hres;
372 static HRESULT WINAPI BindStatusCallback_GetBindInfo(IBindStatusCallback *iface,
373 DWORD *grfBINDF, BINDINFO *pbindinfo)
375 BSCallback *This = STATUSCLB_THIS(iface);
376 DWORD size;
378 TRACE("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
380 if(!This->bindinfo_ready) {
381 HRESULT hres;
383 hres = This->vtbl->init_bindinfo(This);
384 if(FAILED(hres))
385 return hres;
387 This->bindinfo_ready = TRUE;
390 *grfBINDF = This->bindf;
392 size = pbindinfo->cbSize;
393 memset(pbindinfo, 0, size);
394 pbindinfo->cbSize = size;
396 pbindinfo->cbstgmedData = This->post_data_len;
397 pbindinfo->dwCodePage = CP_UTF8;
398 pbindinfo->dwOptions = 0x80000;
400 if(This->post_data) {
401 pbindinfo->dwBindVerb = BINDVERB_POST;
403 pbindinfo->stgmedData.tymed = TYMED_HGLOBAL;
404 pbindinfo->stgmedData.u.hGlobal = This->post_data;
405 pbindinfo->stgmedData.pUnkForRelease = (IUnknown*)STATUSCLB(This);
406 IBindStatusCallback_AddRef(STATUSCLB(This));
409 return S_OK;
412 static HRESULT WINAPI BindStatusCallback_OnDataAvailable(IBindStatusCallback *iface,
413 DWORD grfBSCF, DWORD dwSize, FORMATETC *pformatetc, STGMEDIUM *pstgmed)
415 BSCallback *This = STATUSCLB_THIS(iface);
417 TRACE("(%p)->(%08x %d %p %p)\n", This, grfBSCF, dwSize, pformatetc, pstgmed);
419 return This->vtbl->read_data(This, pstgmed->u.pstm);
422 static HRESULT WINAPI BindStatusCallback_OnObjectAvailable(IBindStatusCallback *iface,
423 REFIID riid, IUnknown *punk)
425 BSCallback *This = STATUSCLB_THIS(iface);
426 FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), punk);
427 return E_NOTIMPL;
430 #undef STATUSCLB_THIS
432 static const IBindStatusCallbackVtbl BindStatusCallbackVtbl = {
433 BindStatusCallback_QueryInterface,
434 BindStatusCallback_AddRef,
435 BindStatusCallback_Release,
436 BindStatusCallback_OnStartBinding,
437 BindStatusCallback_GetPriority,
438 BindStatusCallback_OnLowResource,
439 BindStatusCallback_OnProgress,
440 BindStatusCallback_OnStopBinding,
441 BindStatusCallback_GetBindInfo,
442 BindStatusCallback_OnDataAvailable,
443 BindStatusCallback_OnObjectAvailable
446 #define HTTPNEG_THIS(iface) DEFINE_THIS(BSCallback, HttpNegotiate2, iface)
448 static HRESULT WINAPI HttpNegotiate_QueryInterface(IHttpNegotiate2 *iface,
449 REFIID riid, void **ppv)
451 BSCallback *This = HTTPNEG_THIS(iface);
452 return IBindStatusCallback_QueryInterface(STATUSCLB(This), riid, ppv);
455 static ULONG WINAPI HttpNegotiate_AddRef(IHttpNegotiate2 *iface)
457 BSCallback *This = HTTPNEG_THIS(iface);
458 return IBindStatusCallback_AddRef(STATUSCLB(This));
461 static ULONG WINAPI HttpNegotiate_Release(IHttpNegotiate2 *iface)
463 BSCallback *This = HTTPNEG_THIS(iface);
464 return IBindStatusCallback_Release(STATUSCLB(This));
467 static HRESULT WINAPI HttpNegotiate_BeginningTransaction(IHttpNegotiate2 *iface,
468 LPCWSTR szURL, LPCWSTR szHeaders, DWORD dwReserved, LPWSTR *pszAdditionalHeaders)
470 BSCallback *This = HTTPNEG_THIS(iface);
471 DWORD size;
473 TRACE("(%p)->(%s %s %d %p)\n", This, debugstr_w(szURL), debugstr_w(szHeaders),
474 dwReserved, pszAdditionalHeaders);
476 if(!This->headers) {
477 *pszAdditionalHeaders = NULL;
478 return S_OK;
481 size = (strlenW(This->headers)+1)*sizeof(WCHAR);
482 *pszAdditionalHeaders = CoTaskMemAlloc(size);
483 memcpy(*pszAdditionalHeaders, This->headers, size);
485 return S_OK;
488 static HRESULT WINAPI HttpNegotiate_OnResponse(IHttpNegotiate2 *iface, DWORD dwResponseCode,
489 LPCWSTR szResponseHeaders, LPCWSTR szRequestHeaders, LPWSTR *pszAdditionalRequestHeaders)
491 BSCallback *This = HTTPNEG_THIS(iface);
493 TRACE("(%p)->(%d %s %s %p)\n", This, dwResponseCode, debugstr_w(szResponseHeaders),
494 debugstr_w(szRequestHeaders), pszAdditionalRequestHeaders);
496 return This->vtbl->on_response(This, dwResponseCode);
499 static HRESULT WINAPI HttpNegotiate_GetRootSecurityId(IHttpNegotiate2 *iface,
500 BYTE *pbSecurityId, DWORD *pcbSecurityId, DWORD_PTR dwReserved)
502 BSCallback *This = HTTPNEG_THIS(iface);
503 FIXME("(%p)->(%p %p %ld)\n", This, pbSecurityId, pcbSecurityId, dwReserved);
504 return E_NOTIMPL;
507 #undef HTTPNEG
509 static const IHttpNegotiate2Vtbl HttpNegotiate2Vtbl = {
510 HttpNegotiate_QueryInterface,
511 HttpNegotiate_AddRef,
512 HttpNegotiate_Release,
513 HttpNegotiate_BeginningTransaction,
514 HttpNegotiate_OnResponse,
515 HttpNegotiate_GetRootSecurityId
518 #define BINDINFO_THIS(iface) DEFINE_THIS(BSCallback, InternetBindInfo, iface)
520 static HRESULT WINAPI InternetBindInfo_QueryInterface(IInternetBindInfo *iface,
521 REFIID riid, void **ppv)
523 BSCallback *This = BINDINFO_THIS(iface);
524 return IBindStatusCallback_QueryInterface(STATUSCLB(This), riid, ppv);
527 static ULONG WINAPI InternetBindInfo_AddRef(IInternetBindInfo *iface)
529 BSCallback *This = BINDINFO_THIS(iface);
530 return IBindStatusCallback_AddRef(STATUSCLB(This));
533 static ULONG WINAPI InternetBindInfo_Release(IInternetBindInfo *iface)
535 BSCallback *This = BINDINFO_THIS(iface);
536 return IBindStatusCallback_Release(STATUSCLB(This));
539 static HRESULT WINAPI InternetBindInfo_GetBindInfo(IInternetBindInfo *iface,
540 DWORD *grfBINDF, BINDINFO *pbindinfo)
542 BSCallback *This = BINDINFO_THIS(iface);
543 FIXME("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
544 return E_NOTIMPL;
547 static HRESULT WINAPI InternetBindInfo_GetBindString(IInternetBindInfo *iface,
548 ULONG ulStringType, LPOLESTR *ppwzStr, ULONG cEl, ULONG *pcElFetched)
550 BSCallback *This = BINDINFO_THIS(iface);
551 FIXME("(%p)->(%u %p %u %p)\n", This, ulStringType, ppwzStr, cEl, pcElFetched);
552 return E_NOTIMPL;
555 #undef BINDINFO_THIS
557 static const IInternetBindInfoVtbl InternetBindInfoVtbl = {
558 InternetBindInfo_QueryInterface,
559 InternetBindInfo_AddRef,
560 InternetBindInfo_Release,
561 InternetBindInfo_GetBindInfo,
562 InternetBindInfo_GetBindString
565 #define SERVPROV_THIS(iface) DEFINE_THIS(BSCallback, ServiceProvider, iface)
567 static HRESULT WINAPI BSCServiceProvider_QueryInterface(IServiceProvider *iface,
568 REFIID riid, void **ppv)
570 BSCallback *This = SERVPROV_THIS(iface);
571 return IBindStatusCallback_QueryInterface(STATUSCLB(This), riid, ppv);
574 static ULONG WINAPI BSCServiceProvider_AddRef(IServiceProvider *iface)
576 BSCallback *This = SERVPROV_THIS(iface);
577 return IBindStatusCallback_AddRef(STATUSCLB(This));
580 static ULONG WINAPI BSCServiceProvider_Release(IServiceProvider *iface)
582 BSCallback *This = SERVPROV_THIS(iface);
583 return IBindStatusCallback_Release(STATUSCLB(This));
586 static HRESULT WINAPI BSCServiceProvider_QueryService(IServiceProvider *iface,
587 REFGUID guidService, REFIID riid, void **ppv)
589 BSCallback *This = SERVPROV_THIS(iface);
590 TRACE("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
591 return E_NOINTERFACE;
594 #undef SERVPROV_THIS
596 static const IServiceProviderVtbl ServiceProviderVtbl = {
597 BSCServiceProvider_QueryInterface,
598 BSCServiceProvider_AddRef,
599 BSCServiceProvider_Release,
600 BSCServiceProvider_QueryService
603 static void init_bscallback(BSCallback *This, const BSCallbackVtbl *vtbl, IMoniker *mon, DWORD bindf)
605 This->lpBindStatusCallbackVtbl = &BindStatusCallbackVtbl;
606 This->lpServiceProviderVtbl = &ServiceProviderVtbl;
607 This->lpHttpNegotiate2Vtbl = &HttpNegotiate2Vtbl;
608 This->lpInternetBindInfoVtbl = &InternetBindInfoVtbl;
609 This->vtbl = vtbl;
610 This->ref = 1;
611 This->bindf = bindf;
613 list_init(&This->entry);
615 if(mon)
616 IMoniker_AddRef(mon);
617 This->mon = mon;
620 /* Calls undocumented 84 cmd of CGID_ShellDocView */
621 static void call_docview_84(HTMLDocumentObj *doc)
623 IOleCommandTarget *olecmd;
624 VARIANT var;
625 HRESULT hres;
627 if(!doc->client)
628 return;
630 hres = IOleClientSite_QueryInterface(doc->client, &IID_IOleCommandTarget, (void**)&olecmd);
631 if(FAILED(hres))
632 return;
634 VariantInit(&var);
635 hres = IOleCommandTarget_Exec(olecmd, &CGID_ShellDocView, 84, 0, NULL, &var);
636 IOleCommandTarget_Release(olecmd);
637 if(SUCCEEDED(hres) && V_VT(&var) != VT_NULL)
638 FIXME("handle result\n");
641 static void parse_post_data(nsIInputStream *post_data_stream, LPWSTR *headers_ret,
642 HGLOBAL *post_data_ret, ULONG *post_data_len_ret)
644 PRUint32 post_data_len = 0, available = 0;
645 HGLOBAL post_data = NULL;
646 LPWSTR headers = NULL;
647 DWORD headers_len = 0, len;
648 const char *ptr, *ptr2, *post_data_end;
650 nsIInputStream_Available(post_data_stream, &available);
651 post_data = GlobalAlloc(0, available+1);
652 nsIInputStream_Read(post_data_stream, post_data, available, &post_data_len);
654 TRACE("post_data = %s\n", debugstr_an(post_data, post_data_len));
656 ptr = ptr2 = post_data;
657 post_data_end = (const char*)post_data+post_data_len;
659 while(ptr < post_data_end && (*ptr != '\r' || ptr[1] != '\n')) {
660 while(ptr < post_data_end && (*ptr != '\r' || ptr[1] != '\n'))
661 ptr++;
663 if(!*ptr) {
664 FIXME("*ptr = 0\n");
665 return;
668 ptr += 2;
670 if(ptr-ptr2 >= sizeof(CONTENT_LENGTH)
671 && CompareStringA(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
672 CONTENT_LENGTH, sizeof(CONTENT_LENGTH)-1,
673 ptr2, sizeof(CONTENT_LENGTH)-1) == CSTR_EQUAL) {
674 ptr2 = ptr;
675 continue;
678 len = MultiByteToWideChar(CP_ACP, 0, ptr2, ptr-ptr2, NULL, 0);
680 if(headers)
681 headers = heap_realloc(headers,(headers_len+len+1)*sizeof(WCHAR));
682 else
683 headers = heap_alloc((len+1)*sizeof(WCHAR));
685 len = MultiByteToWideChar(CP_ACP, 0, ptr2, ptr-ptr2, headers+headers_len, len);
686 headers_len += len;
688 ptr2 = ptr;
691 headers[headers_len] = 0;
692 *headers_ret = headers;
694 if(ptr >= post_data_end-2) {
695 GlobalFree(post_data);
696 return;
699 ptr += 2;
701 if(headers_len) {
702 post_data_len -= ptr-(const char*)post_data;
703 memmove(post_data, ptr, post_data_len);
704 post_data = GlobalReAlloc(post_data, post_data_len+1, 0);
707 *post_data_ret = post_data;
708 *post_data_len_ret = post_data_len;
711 HRESULT start_binding(HTMLWindow *window, HTMLDocumentNode *doc, BSCallback *bscallback, IBindCtx *bctx)
713 IStream *str = NULL;
714 HRESULT hres;
716 bscallback->doc = doc;
718 /* NOTE: IE7 calls IsSystemMoniker here*/
720 if(window)
721 call_docview_84(window->doc_obj);
723 if(bctx) {
724 RegisterBindStatusCallback(bctx, STATUSCLB(bscallback), NULL, 0);
725 IBindCtx_AddRef(bctx);
726 }else {
727 hres = CreateAsyncBindCtx(0, STATUSCLB(bscallback), NULL, &bctx);
728 if(FAILED(hres)) {
729 WARN("CreateAsyncBindCtx failed: %08x\n", hres);
730 bscallback->vtbl->stop_binding(bscallback, hres);
731 return hres;
735 hres = IMoniker_BindToStorage(bscallback->mon, bctx, NULL, &IID_IStream, (void**)&str);
736 IBindCtx_Release(bctx);
737 if(FAILED(hres)) {
738 WARN("BindToStorage failed: %08x\n", hres);
739 bscallback->vtbl->stop_binding(bscallback, hres);
740 return hres;
743 if(str)
744 IStream_Release(str);
746 IMoniker_Release(bscallback->mon);
747 bscallback->mon = NULL;
749 return S_OK;
752 typedef struct {
753 BSCallback bsc;
755 DWORD size;
756 BYTE *buf;
757 HRESULT hres;
758 } BufferBSC;
760 #define BUFFERBSC_THIS(bsc) ((BufferBSC*) bsc)
762 static void BufferBSC_destroy(BSCallback *bsc)
764 BufferBSC *This = BUFFERBSC_THIS(bsc);
766 heap_free(This->buf);
767 heap_free(This);
770 static HRESULT BufferBSC_init_bindinfo(BSCallback *bsc)
772 return S_OK;
775 static HRESULT BufferBSC_start_binding(BSCallback *bsc)
777 return S_OK;
780 static HRESULT BufferBSC_stop_binding(BSCallback *bsc, HRESULT result)
782 BufferBSC *This = BUFFERBSC_THIS(bsc);
784 This->hres = result;
786 if(FAILED(result)) {
787 heap_free(This->buf);
788 This->buf = NULL;
789 This->size = 0;
792 return S_OK;
795 static HRESULT BufferBSC_read_data(BSCallback *bsc, IStream *stream)
797 BufferBSC *This = BUFFERBSC_THIS(bsc);
798 DWORD readed;
799 HRESULT hres;
801 if(!This->buf) {
802 This->size = 128;
803 This->buf = heap_alloc(This->size);
806 do {
807 if(This->bsc.readed == This->size) {
808 This->size <<= 1;
809 This->buf = heap_realloc(This->buf, This->size);
812 readed = 0;
813 hres = IStream_Read(stream, This->buf+This->bsc.readed, This->size-This->bsc.readed, &readed);
814 This->bsc.readed += readed;
815 }while(hres == S_OK);
817 return S_OK;
820 static HRESULT BufferBSC_on_progress(BSCallback *bsc, ULONG status_code, LPCWSTR status_text)
822 return S_OK;
825 static HRESULT BufferBSC_on_response(BSCallback *bsc, DWORD response_code)
827 return S_OK;
830 #undef BUFFERBSC_THIS
832 static const BSCallbackVtbl BufferBSCVtbl = {
833 BufferBSC_destroy,
834 BufferBSC_init_bindinfo,
835 BufferBSC_start_binding,
836 BufferBSC_stop_binding,
837 BufferBSC_read_data,
838 BufferBSC_on_progress,
839 BufferBSC_on_response
843 static BufferBSC *create_bufferbsc(IMoniker *mon)
845 BufferBSC *ret = heap_alloc_zero(sizeof(*ret));
847 init_bscallback(&ret->bsc, &BufferBSCVtbl, mon, 0);
848 ret->hres = E_FAIL;
850 return ret;
853 HRESULT bind_mon_to_buffer(HTMLDocumentNode *doc, IMoniker *mon, void **buf, DWORD *size)
855 BufferBSC *bsc = create_bufferbsc(mon);
856 HRESULT hres;
858 *buf = NULL;
860 hres = start_binding(NULL, doc, &bsc->bsc, NULL);
861 if(SUCCEEDED(hres)) {
862 hres = bsc->hres;
863 if(SUCCEEDED(hres)) {
864 *buf = bsc->buf;
865 bsc->buf = NULL;
866 *size = bsc->bsc.readed;
867 bsc->size = 0;
871 IBindStatusCallback_Release(STATUSCLB(&bsc->bsc));
873 return hres;
876 struct nsChannelBSC {
877 BSCallback bsc;
879 HTMLWindow *window;
881 nsChannel *nschannel;
882 nsIStreamListener *nslistener;
883 nsISupports *nscontext;
885 nsProtocolStream *nsstream;
888 static void on_start_nsrequest(nsChannelBSC *This)
890 nsresult nsres;
892 /* FIXME: it's needed for http connections from BindToObject. */
893 if(!This->nschannel->response_status)
894 This->nschannel->response_status = 200;
896 nsres = nsIStreamListener_OnStartRequest(This->nslistener,
897 (nsIRequest*)NSCHANNEL(This->nschannel), This->nscontext);
898 if(NS_FAILED(nsres))
899 FIXME("OnStartRequest failed: %08x\n", nsres);
902 static void on_stop_nsrequest(nsChannelBSC *This, HRESULT result)
904 nsresult nsres;
906 if(!This->nslistener)
907 return;
909 if(!This->bsc.readed && SUCCEEDED(result)) {
910 TRACE("No data read! Calling OnStartRequest\n");
911 on_start_nsrequest(This);
914 nsres = nsIStreamListener_OnStopRequest(This->nslistener, (nsIRequest*)NSCHANNEL(This->nschannel),
915 This->nscontext, SUCCEEDED(result) ? NS_OK : NS_ERROR_FAILURE);
916 if(NS_FAILED(nsres))
917 WARN("OnStopRequest failed: %08x\n", nsres);
920 static HRESULT read_stream_data(nsChannelBSC *This, IStream *stream)
922 DWORD read;
923 nsresult nsres;
924 HRESULT hres;
926 if(!This->nslistener) {
927 BYTE buf[1024];
929 do {
930 read = 0;
931 hres = IStream_Read(stream, buf, sizeof(buf), &read);
932 }while(hres == S_OK && read);
934 return S_OK;
937 if(!This->nsstream)
938 This->nsstream = create_nsprotocol_stream();
940 do {
941 read = 0;
942 hres = IStream_Read(stream, This->nsstream->buf+This->nsstream->buf_size,
943 sizeof(This->nsstream->buf)-This->nsstream->buf_size, &read);
944 if(!read)
945 break;
947 This->nsstream->buf_size += read;
949 if(!This->bsc.readed) {
950 if(This->nsstream->buf_size >= 2
951 && (BYTE)This->nsstream->buf[0] == 0xff
952 && (BYTE)This->nsstream->buf[1] == 0xfe)
953 This->nschannel->charset = heap_strdupA(UTF16_STR);
955 if(!This->nschannel->content_type) {
956 WCHAR *mime;
958 hres = FindMimeFromData(NULL, NULL, This->nsstream->buf, This->nsstream->buf_size, NULL, 0, &mime, 0);
959 if(FAILED(hres))
960 return hres;
962 TRACE("Found MIME %s\n", debugstr_w(mime));
964 This->nschannel->content_type = heap_strdupWtoA(mime);
965 CoTaskMemFree(mime);
966 if(!This->nschannel->content_type)
967 return E_OUTOFMEMORY;
970 on_start_nsrequest(This);
972 if(This->window)
973 update_window_doc(This->window);
976 This->bsc.readed += This->nsstream->buf_size;
978 nsres = nsIStreamListener_OnDataAvailable(This->nslistener,
979 (nsIRequest*)NSCHANNEL(This->nschannel), This->nscontext,
980 NSINSTREAM(This->nsstream), This->bsc.readed-This->nsstream->buf_size,
981 This->nsstream->buf_size);
982 if(NS_FAILED(nsres))
983 ERR("OnDataAvailable failed: %08x\n", nsres);
985 if(This->nsstream->buf_size == sizeof(This->nsstream->buf)) {
986 ERR("buffer is full\n");
987 break;
989 }while(hres == S_OK);
991 return S_OK;
994 static void add_nsrequest(nsChannelBSC *This)
996 nsresult nsres;
998 if(!This->nschannel || !This->nschannel->load_group)
999 return;
1001 nsres = nsILoadGroup_AddRequest(This->nschannel->load_group,
1002 (nsIRequest*)NSCHANNEL(This->nschannel), This->nscontext);
1004 if(NS_FAILED(nsres))
1005 ERR("AddRequest failed:%08x\n", nsres);
1008 #define NSCHANNELBSC_THIS(bsc) ((nsChannelBSC*) bsc)
1010 static void nsChannelBSC_destroy(BSCallback *bsc)
1012 nsChannelBSC *This = NSCHANNELBSC_THIS(bsc);
1014 if(This->nschannel)
1015 nsIChannel_Release(NSCHANNEL(This->nschannel));
1016 if(This->nslistener)
1017 nsIStreamListener_Release(This->nslistener);
1018 if(This->nscontext)
1019 nsISupports_Release(This->nscontext);
1020 if(This->nsstream)
1021 nsIInputStream_Release(NSINSTREAM(This->nsstream));
1022 heap_free(This);
1025 static HRESULT nsChannelBSC_start_binding(BSCallback *bsc)
1027 nsChannelBSC *This = NSCHANNELBSC_THIS(bsc);
1029 add_nsrequest(This);
1031 return S_OK;
1034 static HRESULT nsChannelBSC_init_bindinfo(BSCallback *bsc)
1036 nsChannelBSC *This = NSCHANNELBSC_THIS(bsc);
1038 if(This->nschannel && This->nschannel->post_data_stream) {
1039 parse_post_data(This->nschannel->post_data_stream, &This->bsc.headers, &This->bsc.post_data, &This->bsc.post_data_len);
1040 TRACE("headers = %s post_data = %s\n", debugstr_w(This->bsc.headers),
1041 debugstr_an(This->bsc.post_data, This->bsc.post_data_len));
1044 return S_OK;
1047 static HRESULT nsChannelBSC_stop_binding(BSCallback *bsc, HRESULT result)
1049 nsChannelBSC *This = NSCHANNELBSC_THIS(bsc);
1051 on_stop_nsrequest(This, result);
1053 if(This->nslistener) {
1054 if(This->nschannel->load_group) {
1055 nsresult nsres;
1057 nsres = nsILoadGroup_RemoveRequest(This->nschannel->load_group,
1058 (nsIRequest*)NSCHANNEL(This->nschannel), NULL, NS_OK);
1059 if(NS_FAILED(nsres))
1060 ERR("RemoveRequest failed: %08x\n", nsres);
1064 return S_OK;
1067 static HRESULT nsChannelBSC_read_data(BSCallback *bsc, IStream *stream)
1069 nsChannelBSC *This = NSCHANNELBSC_THIS(bsc);
1071 return read_stream_data(This, stream);
1074 static HRESULT nsChannelBSC_on_progress(BSCallback *bsc, ULONG status_code, LPCWSTR status_text)
1076 nsChannelBSC *This = NSCHANNELBSC_THIS(bsc);
1078 switch(status_code) {
1079 case BINDSTATUS_MIMETYPEAVAILABLE:
1080 if(!This->nschannel)
1081 return S_OK;
1083 heap_free(This->nschannel->content_type);
1084 This->nschannel->content_type = heap_strdupWtoA(status_text);
1085 break;
1086 case BINDSTATUS_REDIRECTING:
1087 TRACE("redirect to %s\n", debugstr_w(status_text));
1089 /* FIXME: We should find a better way to handle this */
1090 nsIWineURI_SetWineURL(This->nschannel->uri, status_text);
1093 return S_OK;
1096 static HRESULT nsChannelBSC_on_response(BSCallback *bsc, DWORD response_code)
1098 nsChannelBSC *This = NSCHANNELBSC_THIS(bsc);
1100 This->nschannel->response_status = response_code;
1101 return S_OK;
1104 #undef NSCHANNELBSC_THIS
1106 static const BSCallbackVtbl nsChannelBSCVtbl = {
1107 nsChannelBSC_destroy,
1108 nsChannelBSC_init_bindinfo,
1109 nsChannelBSC_start_binding,
1110 nsChannelBSC_stop_binding,
1111 nsChannelBSC_read_data,
1112 nsChannelBSC_on_progress,
1113 nsChannelBSC_on_response
1116 nsChannelBSC *create_channelbsc(IMoniker *mon)
1118 nsChannelBSC *ret = heap_alloc_zero(sizeof(*ret));
1120 init_bscallback(&ret->bsc, &nsChannelBSCVtbl, mon, BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA);
1122 return ret;
1125 IMoniker *get_channelbsc_mon(nsChannelBSC *This)
1127 if(This->bsc.mon)
1128 IMoniker_AddRef(This->bsc.mon);
1129 return This->bsc.mon;
1132 void set_window_bscallback(HTMLWindow *window, nsChannelBSC *callback)
1134 if(window->bscallback) {
1135 if(window->bscallback->bsc.binding)
1136 IBinding_Abort(window->bscallback->bsc.binding);
1137 window->bscallback->bsc.doc = NULL;
1138 window->bscallback->window = NULL;
1139 IBindStatusCallback_Release(STATUSCLB(&window->bscallback->bsc));
1142 window->bscallback = callback;
1144 if(callback) {
1145 callback->window = window;
1146 IBindStatusCallback_AddRef(STATUSCLB(&callback->bsc));
1147 callback->bsc.doc = window->doc;
1151 void abort_document_bindings(HTMLDocumentNode *doc)
1153 BSCallback *iter;
1155 LIST_FOR_EACH_ENTRY(iter, &doc->bindings, BSCallback, entry) {
1156 if(iter->binding)
1157 IBinding_Abort(iter->binding);
1158 iter->doc = NULL;
1159 list_remove(&iter->entry);
1163 HRESULT channelbsc_load_stream(nsChannelBSC *bscallback, IStream *stream)
1165 HRESULT hres;
1167 if(!bscallback->nschannel) {
1168 ERR("NULL nschannel\n");
1169 return E_FAIL;
1172 bscallback->nschannel->content_type = heap_strdupA("text/html");
1173 if(!bscallback->nschannel->content_type)
1174 return E_OUTOFMEMORY;
1176 add_nsrequest(bscallback);
1178 hres = read_stream_data(bscallback, stream);
1179 IBindStatusCallback_OnStopBinding(STATUSCLB(&bscallback->bsc), hres, ERROR_SUCCESS);
1181 return hres;
1184 void channelbsc_set_channel(nsChannelBSC *This, nsChannel *channel, nsIStreamListener *listener, nsISupports *context)
1186 nsIChannel_AddRef(NSCHANNEL(channel));
1187 This->nschannel = channel;
1189 nsIStreamListener_AddRef(listener);
1190 This->nslistener = listener;
1192 if(context) {
1193 nsISupports_AddRef(context);
1194 This->nscontext = context;
1198 HRESULT hlink_frame_navigate(HTMLDocument *doc, LPCWSTR url,
1199 nsIInputStream *post_data_stream, DWORD hlnf)
1201 IHlinkFrame *hlink_frame;
1202 IServiceProvider *sp;
1203 BSCallback *callback;
1204 IBindCtx *bindctx;
1205 IMoniker *mon;
1206 IHlink *hlink;
1207 HRESULT hres;
1209 hres = IOleClientSite_QueryInterface(doc->doc_obj->client, &IID_IServiceProvider,
1210 (void**)&sp);
1211 if(FAILED(hres))
1212 return hres;
1214 hres = IServiceProvider_QueryService(sp, &IID_IHlinkFrame, &IID_IHlinkFrame,
1215 (void**)&hlink_frame);
1216 IServiceProvider_Release(sp);
1217 if(FAILED(hres))
1218 return hres;
1220 callback = &create_channelbsc(NULL)->bsc;
1222 if(post_data_stream) {
1223 parse_post_data(post_data_stream, &callback->headers, &callback->post_data,
1224 &callback->post_data_len);
1225 TRACE("headers = %s post_data = %s\n", debugstr_w(callback->headers),
1226 debugstr_an(callback->post_data, callback->post_data_len));
1229 hres = CreateAsyncBindCtx(0, STATUSCLB(callback), NULL, &bindctx);
1230 if(SUCCEEDED(hres))
1231 hres = CoCreateInstance(&CLSID_StdHlink, NULL, CLSCTX_INPROC_SERVER,
1232 &IID_IHlink, (LPVOID*)&hlink);
1234 if(SUCCEEDED(hres))
1235 hres = CreateURLMoniker(NULL, url, &mon);
1237 if(SUCCEEDED(hres)) {
1238 IHlink_SetMonikerReference(hlink, HLINKSETF_TARGET, mon, NULL);
1240 if(hlnf & HLNF_OPENINNEWWINDOW) {
1241 static const WCHAR wszBlank[] = {'_','b','l','a','n','k',0};
1242 IHlink_SetTargetFrameName(hlink, wszBlank); /* FIXME */
1245 hres = IHlinkFrame_Navigate(hlink_frame, hlnf, bindctx, STATUSCLB(callback), hlink);
1247 IMoniker_Release(mon);
1250 IHlinkFrame_Release(hlink_frame);
1251 IBindCtx_Release(bindctx);
1252 IBindStatusCallback_Release(STATUSCLB(callback));
1253 return hres;
1256 HRESULT load_nsuri(HTMLWindow *window, nsIWineURI *uri, DWORD flags)
1258 nsIWebNavigation *web_navigation;
1259 nsIDocShell *doc_shell;
1260 nsresult nsres;
1262 nsres = get_nsinterface((nsISupports*)window->nswindow, &IID_nsIWebNavigation, (void**)&web_navigation);
1263 if(NS_FAILED(nsres)) {
1264 ERR("Could not get nsIWebNavigation interface: %08x\n", nsres);
1265 return E_FAIL;
1268 nsres = nsIWebNavigation_QueryInterface(web_navigation, &IID_nsIDocShell, (void**)&doc_shell);
1269 nsIWebNavigation_Release(web_navigation);
1270 if(NS_FAILED(nsres)) {
1271 ERR("Could not get nsIDocShell: %08x\n", nsres);
1272 return E_FAIL;
1275 nsres = nsIDocShell_LoadURI(doc_shell, (nsIURI*)uri, NULL, flags, FALSE);
1276 nsIDocShell_Release(doc_shell);
1277 if(NS_FAILED(nsres)) {
1278 WARN("LoadURI failed: %08x\n", nsres);
1279 return E_FAIL;
1282 return S_OK;
1285 HRESULT navigate_url(HTMLWindow *window, const WCHAR *new_url, const WCHAR *base_url)
1287 WCHAR url[INTERNET_MAX_URL_LENGTH];
1288 nsIWineURI *uri;
1289 HRESULT hres;
1291 if(!new_url) {
1292 *url = 0;
1293 }else if(base_url) {
1294 DWORD len = 0;
1296 hres = CoInternetCombineUrl(base_url, new_url, URL_ESCAPE_SPACES_ONLY|URL_DONT_ESCAPE_EXTRA_INFO,
1297 url, sizeof(url)/sizeof(WCHAR), &len, 0);
1298 if(FAILED(hres))
1299 return hres;
1300 }else {
1301 strcpyW(url, new_url);
1304 if(window->doc_obj && window->doc_obj->hostui) {
1305 OLECHAR *translated_url = NULL;
1307 hres = IDocHostUIHandler_TranslateUrl(window->doc_obj->hostui, 0, url,
1308 &translated_url);
1309 if(hres == S_OK) {
1310 strcpyW(url, translated_url);
1311 CoTaskMemFree(translated_url);
1315 if(window->doc_obj && window == window->doc_obj->basedoc.window) {
1316 hres = hlink_frame_navigate(&window->doc->basedoc, url, NULL, 0);
1317 if(SUCCEEDED(hres))
1318 return S_OK;
1319 TRACE("hlink_frame_navigate failed: %08x\n", hres);
1322 hres = create_doc_uri(window, url, &uri);
1323 if(FAILED(hres))
1324 return hres;
1326 hres = load_nsuri(window, uri, LOAD_FLAGS_NONE);
1327 nsIWineURI_Release(uri);
1328 return hres;