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
24 #define NONAMELESSUNION
25 #define NONAMELESSSTRUCT
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,
48 nsProtocolStream *This = NSINSTREAM_THIS(iface);
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);
61 nsIInputStream_AddRef(NSINSTREAM(This));
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=%ld\n", This, 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=%ld\n", This, ref);
88 HeapFree(GetProcessHeap(), 0, This);
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,
110 nsProtocolStream *This = NSINSTREAM_THIS(iface);
111 FIXME("(%p)->(%p %ld %p)\n", This, aBuf, aCount, _retval);
112 return NS_ERROR_NOT_IMPLEMENTED;
115 static nsresult NSAPI nsInputStream_ReadSegments(nsIInputStream *iface,
116 nsresult (NSAPI*aWriter)(nsIInputStream*,void*,const char*,PRUint32,PRUint32,PRUint32*),
117 void *aClousure, PRUint32 aCount, PRUint32 *_retval)
119 nsProtocolStream *This = NSINSTREAM_THIS(iface);
120 PRUint32 written = 0;
123 FIXME("(%p)->(%p %p %ld %p)\n", This, aWriter, aClousure, aCount, _retval);
128 if(This->buf_size > aCount)
129 FIXME("buf_size > aCount\n");
131 nsres = aWriter(NSINSTREAM(This), aClousure, This->buf, 0, This->buf_size, &written);
133 FIXME("aWritter failed: %08lx\n", nsres);
134 if(written != This->buf_size)
135 FIXME("written != buf_size\n");
137 This->buf_size -= written;
142 static nsresult NSAPI nsInputStream_IsNonBlocking(nsIInputStream *iface, PRBool *_retval)
144 nsProtocolStream *This = NSINSTREAM_THIS(iface);
145 FIXME("(%p)->(%p)\n", This, _retval);
146 return NS_ERROR_NOT_IMPLEMENTED;
149 #undef NSINSTREAM_THIS
151 static const nsIInputStreamVtbl nsInputStreamVtbl = {
152 nsInputStream_QueryInterface,
153 nsInputStream_AddRef,
154 nsInputStream_Release,
156 nsInputStream_Available,
158 nsInputStream_ReadSegments,
159 nsInputStream_IsNonBlocking
162 static nsProtocolStream *create_nsprotocol_stream(IStream *stream)
164 nsProtocolStream *ret = HeapAlloc(GetProcessHeap(), 0, sizeof(nsProtocolStream));
166 ret->lpInputStreamVtbl = &nsInputStreamVtbl;
173 #define STATUSCLB_THIS(iface) DEFINE_THIS(BSCallback, BindStatusCallback, iface)
175 static HRESULT WINAPI BindStatusCallback_QueryInterface(IBindStatusCallback *iface,
176 REFIID riid, void **ppv)
178 BSCallback *This = STATUSCLB_THIS(iface);
181 if(IsEqualGUID(&IID_IUnknown, riid)) {
182 TRACE("(%p)->(IID_IUnknown, %p)\n", This, ppv);
183 *ppv = STATUSCLB(This);
184 }else if(IsEqualGUID(&IID_IBindStatusCallback, riid)) {
185 TRACE("(%p)->(IID_IBindStatusCallback, %p)\n", This, ppv);
186 *ppv = STATUSCLB(This);
187 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
188 TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
189 *ppv = SERVPROV(This);
190 }else if(IsEqualGUID(&IID_IHttpNegotiate, riid)) {
191 TRACE("(%p)->(IID_IHttpNegotiate %p)\n", This, ppv);
192 *ppv = HTTPNEG(This);
193 }else if(IsEqualGUID(&IID_IHttpNegotiate2, riid)) {
194 TRACE("(%p)->(IID_IHttpNegotiate2 %p)\n", This, ppv);
195 *ppv = HTTPNEG(This);
196 }else if(IsEqualGUID(&IID_IInternetBindInfo, riid)) {
197 TRACE("(%p)->(IID_IInternetBindInfo %p)\n", This, ppv);
198 *ppv = BINDINFO(This);
202 IBindStatusCallback_AddRef(STATUSCLB(This));
206 TRACE("Unsupported riid = %s\n", debugstr_guid(riid));
207 return E_NOINTERFACE;
210 static ULONG WINAPI BindStatusCallback_AddRef(IBindStatusCallback *iface)
212 BSCallback *This = STATUSCLB_THIS(iface);
213 LONG ref = InterlockedIncrement(&This->ref);
215 TRACE("(%p) ref = %ld\n", This, ref);
220 static ULONG WINAPI BindStatusCallback_Release(IBindStatusCallback *iface)
222 BSCallback *This = STATUSCLB_THIS(iface);
223 LONG ref = InterlockedDecrement(&This->ref);
225 TRACE("(%p) ref = %ld\n", This, ref);
229 GlobalFree(This->post_data);
231 nsIChannel_Release(NSCHANNEL(This->nschannel));
233 nsIStreamListener_Release(This->nslistener);
235 nsISupports_Release(This->nscontext);
237 nsIInputStream_Release(NSINSTREAM(This->nsstream));
238 HeapFree(GetProcessHeap(), 0, This->headers);
239 HeapFree(GetProcessHeap(), 0, This);
245 static HRESULT WINAPI BindStatusCallback_OnStartBinding(IBindStatusCallback *iface,
246 DWORD dwReserved, IBinding *pbind)
248 BSCallback *This = STATUSCLB_THIS(iface);
249 FIXME("(%p)->(%ld %p)\n", This, dwReserved, pbind);
253 static HRESULT WINAPI BindStatusCallback_GetPriority(IBindStatusCallback *iface, LONG *pnPriority)
255 BSCallback *This = STATUSCLB_THIS(iface);
256 FIXME("(%p)->(%p)\n", This, pnPriority);
260 static HRESULT WINAPI BindStatusCallback_OnLowResource(IBindStatusCallback *iface, DWORD reserved)
262 BSCallback *This = STATUSCLB_THIS(iface);
263 FIXME("(%p)->(%ld)\n", This, reserved);
267 static HRESULT WINAPI BindStatusCallback_OnProgress(IBindStatusCallback *iface, ULONG ulProgress,
268 ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
270 BSCallback *This = STATUSCLB_THIS(iface);
271 TRACE("%p)->(%lu %lu %lu %s)\n", This, ulProgress, ulProgressMax, ulStatusCode,
272 debugstr_w(szStatusText));
276 static HRESULT WINAPI BindStatusCallback_OnStopBinding(IBindStatusCallback *iface,
277 HRESULT hresult, LPCWSTR szError)
279 BSCallback *This = STATUSCLB_THIS(iface);
280 FIXME("(%p)->(%08lx %s)\n", This, hresult, debugstr_w(szError));
284 static HRESULT WINAPI BindStatusCallback_GetBindInfo(IBindStatusCallback *iface,
285 DWORD *grfBINDF, BINDINFO *pbindinfo)
287 BSCallback *This = STATUSCLB_THIS(iface);
290 TRACE("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
292 *grfBINDF = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_PULLDATA;
294 size = pbindinfo->cbSize;
295 memset(pbindinfo, 0, size);
296 pbindinfo->cbSize = size;
298 pbindinfo->cbStgmedData = This->post_data_len;
299 pbindinfo->dwCodePage = CP_UTF8;
300 pbindinfo->dwOptions = 0x00020000;
302 if(This->post_data) {
303 pbindinfo->dwBindVerb = BINDVERB_POST;
305 pbindinfo->stgmedData.tymed = TYMED_HGLOBAL;
306 pbindinfo->stgmedData.u.hGlobal = This->post_data;
307 pbindinfo->stgmedData.pUnkForRelease = (IUnknown*)STATUSCLB(This);
308 IBindStatusCallback_AddRef(STATUSCLB(This));
314 static HRESULT WINAPI BindStatusCallback_OnDataAvailable(IBindStatusCallback *iface,
315 DWORD grfBSCF, DWORD dwSize, FORMATETC *pformatetc, STGMEDIUM *pstgmed)
317 BSCallback *This = STATUSCLB_THIS(iface);
321 TRACE("(%p)->(%08lx %ld %p %p)\n", This, grfBSCF, dwSize, pformatetc, pstgmed);
323 if(This->nslistener) {
324 if(!This->nsstream) {
325 This->nsstream = create_nsprotocol_stream(pstgmed->u.pstm);
327 nsres = nsIStreamListener_OnStartRequest(This->nslistener,
328 (nsIRequest*)NSCHANNEL(This->nschannel), This->nscontext);
330 FIXME("OnStartRequest failed: %08lx\n", nsres);
334 hres = IStream_Read(pstgmed->u.pstm, This->nsstream->buf, sizeof(This->nsstream->buf),
335 &This->nsstream->buf_size);
336 if(This->nsstream->buf_size) {
337 nsres = nsIStreamListener_OnDataAvailable(This->nslistener,
338 (nsIRequest*)NSCHANNEL(This->nschannel), This->nscontext,
339 NSINSTREAM(This->nsstream), 0 /* FIXME */, dwSize);
341 FIXME("OnDataAvailable failed: %08lx\n", nsres);
343 if(This->nsstream->buf_size)
344 FIXME("buffer is not empty!\n");
346 }while(hres == S_OK);
352 static HRESULT WINAPI BindStatusCallback_OnObjectAvailable(IBindStatusCallback *iface,
353 REFIID riid, IUnknown *punk)
355 BSCallback *This = STATUSCLB_THIS(iface);
356 FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), punk);
360 #undef STATUSCLB_THIS
362 static const IBindStatusCallbackVtbl BindStatusCallbackVtbl = {
363 BindStatusCallback_QueryInterface,
364 BindStatusCallback_AddRef,
365 BindStatusCallback_Release,
366 BindStatusCallback_OnStartBinding,
367 BindStatusCallback_GetPriority,
368 BindStatusCallback_OnLowResource,
369 BindStatusCallback_OnProgress,
370 BindStatusCallback_OnStopBinding,
371 BindStatusCallback_GetBindInfo,
372 BindStatusCallback_OnDataAvailable,
373 BindStatusCallback_OnObjectAvailable
376 #define HTTPNEG_THIS(iface) DEFINE_THIS(BSCallback, HttpNegotiate2, iface)
378 static HRESULT WINAPI HttpNegotiate_QueryInterface(IHttpNegotiate2 *iface,
379 REFIID riid, void **ppv)
381 BSCallback *This = HTTPNEG_THIS(iface);
382 return IBindStatusCallback_QueryInterface(STATUSCLB(This), riid, ppv);
385 static ULONG WINAPI HttpNegotiate_AddRef(IHttpNegotiate2 *iface)
387 BSCallback *This = HTTPNEG_THIS(iface);
388 return IBindStatusCallback_AddRef(STATUSCLB(This));
391 static ULONG WINAPI HttpNegotiate_Release(IHttpNegotiate2 *iface)
393 BSCallback *This = HTTPNEG_THIS(iface);
394 return IBindStatusCallback_Release(STATUSCLB(This));
397 static HRESULT WINAPI HttpNegotiate_BeginningTransaction(IHttpNegotiate2 *iface,
398 LPCWSTR szURL, LPCWSTR szHeaders, DWORD dwReserved, LPWSTR *pszAdditionalHeaders)
400 BSCallback *This = HTTPNEG_THIS(iface);
403 TRACE("(%p)->(%s %s %ld %p)\n", This, debugstr_w(szURL), debugstr_w(szHeaders),
404 dwReserved, pszAdditionalHeaders);
407 *pszAdditionalHeaders = NULL;
411 size = (strlenW(This->headers)+1)*sizeof(WCHAR);
412 *pszAdditionalHeaders = CoTaskMemAlloc(size);
413 memcpy(*pszAdditionalHeaders, This->headers, size);
418 static HRESULT WINAPI HttpNegotiate_OnResponse(IHttpNegotiate2 *iface, DWORD dwResponseCode,
419 LPCWSTR szResponseHeaders, LPCWSTR szRequestHeaders, LPWSTR *pszAdditionalRequestHeaders)
421 BSCallback *This = HTTPNEG_THIS(iface);
422 FIXME("(%p)->(%ld %s %s %p)\n", This, dwResponseCode, debugstr_w(szResponseHeaders),
423 debugstr_w(szRequestHeaders), pszAdditionalRequestHeaders);
427 static HRESULT WINAPI HttpNegotiate_GetRootSecurityId(IHttpNegotiate2 *iface,
428 BYTE *pbSecurityId, DWORD *pcbSecurityId, DWORD_PTR dwReserved)
430 BSCallback *This = HTTPNEG_THIS(iface);
431 FIXME("(%p)->(%p %p %ld)\n", This, pbSecurityId, pcbSecurityId, dwReserved);
437 static const IHttpNegotiate2Vtbl HttpNegotiate2Vtbl = {
438 HttpNegotiate_QueryInterface,
439 HttpNegotiate_AddRef,
440 HttpNegotiate_Release,
441 HttpNegotiate_BeginningTransaction,
442 HttpNegotiate_OnResponse,
443 HttpNegotiate_GetRootSecurityId
446 #define BINDINFO_THIS(iface) DEFINE_THIS(BSCallback, InternetBindInfo, iface)
448 static HRESULT WINAPI InternetBindInfo_QueryInterface(IInternetBindInfo *iface,
449 REFIID riid, void **ppv)
451 BSCallback *This = BINDINFO_THIS(iface);
452 return IBindStatusCallback_QueryInterface(STATUSCLB(This), riid, ppv);
455 static ULONG WINAPI InternetBindInfo_AddRef(IInternetBindInfo *iface)
457 BSCallback *This = BINDINFO_THIS(iface);
458 return IBindStatusCallback_AddRef(STATUSCLB(This));
461 static ULONG WINAPI InternetBindInfo_Release(IInternetBindInfo *iface)
463 BSCallback *This = BINDINFO_THIS(iface);
464 return IBindStatusCallback_Release(STATUSCLB(This));
467 static HRESULT WINAPI InternetBindInfo_GetBindInfo(IInternetBindInfo *iface,
468 DWORD *grfBINDF, BINDINFO *pbindinfo)
470 BSCallback *This = BINDINFO_THIS(iface);
471 FIXME("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
475 static HRESULT WINAPI InternetBindInfo_GetBindString(IInternetBindInfo *iface,
476 ULONG ulStringType, LPOLESTR *ppwzStr, ULONG cEl, ULONG *pcElFetched)
478 BSCallback *This = BINDINFO_THIS(iface);
479 FIXME("(%p)->(%lu %p %lu %p)\n", This, ulStringType, ppwzStr, cEl, pcElFetched);
485 static const IInternetBindInfoVtbl InternetBindInfoVtbl = {
486 InternetBindInfo_QueryInterface,
487 InternetBindInfo_AddRef,
488 InternetBindInfo_Release,
489 InternetBindInfo_GetBindInfo,
490 InternetBindInfo_GetBindString
493 #define SERVPROV_THIS(iface) DEFINE_THIS(BSCallback, ServiceProvider, iface)
495 static HRESULT WINAPI BSCServiceProvider_QueryInterface(IServiceProvider *iface,
496 REFIID riid, void **ppv)
498 BSCallback *This = SERVPROV_THIS(iface);
499 return IBindStatusCallback_QueryInterface(STATUSCLB(This), riid, ppv);
502 static ULONG WINAPI BSCServiceProvider_AddRef(IServiceProvider *iface)
504 BSCallback *This = SERVPROV_THIS(iface);
505 return IBindStatusCallback_AddRef(STATUSCLB(This));
508 static ULONG WINAPI BSCServiceProvider_Release(IServiceProvider *iface)
510 BSCallback *This = SERVPROV_THIS(iface);
511 return IBindStatusCallback_Release(STATUSCLB(This));
514 static HRESULT WINAPI BSCServiceProvider_QueryService(IServiceProvider *iface,
515 REFGUID guidService, REFIID riid, void **ppv)
517 BSCallback *This = SERVPROV_THIS(iface);
518 FIXME("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
524 static const IServiceProviderVtbl ServiceProviderVtbl = {
525 BSCServiceProvider_QueryInterface,
526 BSCServiceProvider_AddRef,
527 BSCServiceProvider_Release,
528 BSCServiceProvider_QueryService
531 BSCallback *create_bscallback(HTMLDocument *doc, LPCOLESTR url)
533 BSCallback *ret = HeapAlloc(GetProcessHeap(), 0, sizeof(BSCallback));
535 ret->lpBindStatusCallbackVtbl = &BindStatusCallbackVtbl;
536 ret->lpServiceProviderVtbl = &ServiceProviderVtbl;
537 ret->lpHttpNegotiate2Vtbl = &HttpNegotiate2Vtbl;
538 ret->lpInternetBindInfoVtbl = &InternetBindInfoVtbl;
540 ret->post_data = NULL;
542 ret->post_data_len = 0;
543 ret->nschannel = NULL;
544 ret->nslistener = NULL;
545 ret->nscontext = NULL;
546 ret->nsstream = NULL;
551 static void parse_post_data(nsIInputStream *post_data_stream, LPWSTR *headers_ret,
552 HGLOBAL *post_data_ret, ULONG *post_data_len_ret)
554 PRUint32 post_data_len = 0, available = 0;
555 HGLOBAL post_data = NULL;
556 LPWSTR headers = NULL;
557 DWORD headers_len = 0, len;
558 const char *ptr, *ptr2, *post_data_end;
560 nsIInputStream_Available(post_data_stream, &available);
561 post_data = GlobalAlloc(0, available+1);
562 nsIInputStream_Read(post_data_stream, post_data, available, &post_data_len);
564 TRACE("post_data = %s\n", debugstr_an(post_data, post_data_len));
566 ptr = ptr2 = post_data;
567 post_data_end = (const char*)post_data+post_data_len;
569 while(ptr < post_data_end && (*ptr != '\r' || ptr[1] != '\n')) {
570 while(ptr < post_data_end && (*ptr != '\r' || ptr[1] != '\n'))
580 if(ptr-ptr2 >= sizeof(CONTENT_LENGTH)
581 && CompareStringA(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
582 CONTENT_LENGTH, sizeof(CONTENT_LENGTH)-1,
583 ptr2, sizeof(CONTENT_LENGTH)-1) == CSTR_EQUAL) {
588 len = MultiByteToWideChar(CP_ACP, 0, ptr2, ptr-ptr2, NULL, 0);
591 headers = HeapReAlloc(GetProcessHeap(), 0, headers,
592 (headers_len+len+1)*sizeof(WCHAR));
594 headers = HeapAlloc(GetProcessHeap(), 0, (len+1)*sizeof(WCHAR));
596 len = MultiByteToWideChar(CP_ACP, 0, ptr2, ptr-ptr2, headers+headers_len, -1);
602 headers[headers_len] = 0;
603 *headers_ret = headers;
605 if(ptr >= post_data_end-2) {
606 GlobalFree(post_data);
613 post_data_len -= ptr-(const char*)post_data;
614 memmove(post_data, ptr, post_data_len);
615 post_data = GlobalReAlloc(post_data, post_data_len+1, 0);
618 *post_data_ret = post_data;
619 *post_data_len_ret = post_data_len;
622 void hlink_frame_navigate(HTMLDocument *doc, IHlinkFrame *hlink_frame,
623 LPCWSTR uri, nsIInputStream *post_data_stream, DWORD hlnf)
625 BSCallback *callback;
630 callback = create_bscallback(doc, uri);
632 if(post_data_stream) {
633 parse_post_data(post_data_stream, &callback->headers, &callback->post_data,
634 &callback->post_data_len);
635 TRACE("headers = %s post_data = %s\n", debugstr_w(callback->headers),
636 debugstr_an(callback->post_data, callback->post_data_len));
639 CreateAsyncBindCtx(0, STATUSCLB(callback), NULL, &bindctx);
641 hlink = Hlink_Create();
643 CreateURLMoniker(NULL, uri, &mon);
644 IHlink_SetMonikerReference(hlink, 0, mon, NULL);
646 if(hlnf & HLNF_OPENINNEWWINDOW) {
647 static const WCHAR wszBlank[] = {'_','b','l','a','n','k',0};
648 IHlink_SetTargetFrameName(hlink, wszBlank); /* FIXME */
651 IHlinkFrame_Navigate(hlink_frame, hlnf, bindctx, STATUSCLB(callback), hlink);
653 IBindCtx_Release(bindctx);
654 IBindStatusCallback_Release(STATUSCLB(callback));
655 IMoniker_Release(mon);
659 HRESULT start_binding(BSCallback *bscallback, IMoniker *mon)
665 hres = CreateAsyncBindCtx(0, STATUSCLB(bscallback), NULL, &bctx);
667 WARN("CreateAsyncBindCtx failed: %08lx\n", hres);
671 hres = IMoniker_BindToStorage(mon, bctx, NULL, &IID_IStream, (void**)&str);
672 IBindCtx_Release(bctx);
674 WARN("BindToStorage failed: %08lx\n", hres);
679 IStream_Release(str);