2 * IXMLHTTPRequest implementation
4 * Copyright 2008 Alistair Leslie-Hughes
5 * Copyright 2010 Nikolay Sivov for CodeWeavers
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #define NONAMELESSUNION
34 #include "msxml_private.h"
36 #include "wine/debug.h"
37 #include "wine/list.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(msxml);
43 #include <libxml/encoding.h>
45 static const WCHAR MethodGetW[] = {'G','E','T',0};
46 static const WCHAR MethodPutW[] = {'P','U','T',0};
47 static const WCHAR MethodPostW[] = {'P','O','S','T',0};
49 static const WCHAR colspaceW[] = {':',' ',0};
50 static const WCHAR crlfW[] = {'\r','\n',0};
52 typedef struct BindStatusCallback BindStatusCallback;
63 IXMLHTTPRequest IXMLHTTPRequest_iface;
64 IObjectWithSite IObjectWithSite_iface;
74 struct list reqheaders;
75 /* cached resulting custom request headers string length in WCHARs */
83 BindStatusCallback *bsc;
90 static inline httprequest *impl_from_IXMLHTTPRequest( IXMLHTTPRequest *iface )
92 return CONTAINING_RECORD(iface, httprequest, IXMLHTTPRequest_iface);
95 static inline httprequest *impl_from_IObjectWithSite(IObjectWithSite *iface)
97 return CONTAINING_RECORD(iface, httprequest, IObjectWithSite_iface);
100 static void httprequest_setreadystate(httprequest *This, READYSTATE state)
102 READYSTATE last = This->state;
106 if (This->sink && last != state)
110 memset(¶ms, 0, sizeof(params));
111 IDispatch_Invoke(This->sink, 0, &IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD, ¶ms, 0, 0, 0);
115 struct BindStatusCallback
117 IBindStatusCallback IBindStatusCallback_iface;
118 IHttpNegotiate IHttpNegotiate_iface;
122 httprequest *request;
127 /* request body data */
131 static inline BindStatusCallback *impl_from_IBindStatusCallback( IBindStatusCallback *iface )
133 return CONTAINING_RECORD(iface, BindStatusCallback, IBindStatusCallback_iface);
136 static inline BindStatusCallback *impl_from_IHttpNegotiate( IHttpNegotiate *iface )
138 return CONTAINING_RECORD(iface, BindStatusCallback, IHttpNegotiate_iface);
141 void BindStatusCallback_Detach(BindStatusCallback *bsc)
145 if (bsc->binding) IBinding_Abort(bsc->binding);
147 IBindStatusCallback_Release(&bsc->IBindStatusCallback_iface);
151 static HRESULT WINAPI BindStatusCallback_QueryInterface(IBindStatusCallback *iface,
152 REFIID riid, void **ppv)
154 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
158 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
160 if (IsEqualGUID(&IID_IUnknown, riid) ||
161 IsEqualGUID(&IID_IBindStatusCallback, riid))
163 *ppv = &This->IBindStatusCallback_iface;
165 else if (IsEqualGUID(&IID_IHttpNegotiate, riid))
167 *ppv = &This->IHttpNegotiate_iface;
169 else if (IsEqualGUID(&IID_IServiceProvider, riid) ||
170 IsEqualGUID(&IID_IBindStatusCallbackEx, riid) ||
171 IsEqualGUID(&IID_IInternetProtocol, riid) ||
172 IsEqualGUID(&IID_IHttpNegotiate2, riid))
174 return E_NOINTERFACE;
179 IBindStatusCallback_AddRef(iface);
183 FIXME("Unsupported riid = %s\n", debugstr_guid(riid));
185 return E_NOINTERFACE;
188 static ULONG WINAPI BindStatusCallback_AddRef(IBindStatusCallback *iface)
190 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
191 LONG ref = InterlockedIncrement(&This->ref);
193 TRACE("(%p) ref = %d\n", This, ref);
198 static ULONG WINAPI BindStatusCallback_Release(IBindStatusCallback *iface)
200 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
201 LONG ref = InterlockedDecrement(&This->ref);
203 TRACE("(%p) ref = %d\n", This, ref);
207 if (This->binding) IBinding_Release(This->binding);
208 if (This->stream) IStream_Release(This->stream);
209 if (This->body) GlobalFree(This->body);
216 static HRESULT WINAPI BindStatusCallback_OnStartBinding(IBindStatusCallback *iface,
217 DWORD reserved, IBinding *pbind)
219 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
221 TRACE("(%p)->(%d %p)\n", This, reserved, pbind);
223 if (!pbind) return E_INVALIDARG;
225 This->binding = pbind;
226 IBinding_AddRef(pbind);
228 httprequest_setreadystate(This->request, READYSTATE_LOADED);
230 return CreateStreamOnHGlobal(NULL, TRUE, &This->stream);
233 static HRESULT WINAPI BindStatusCallback_GetPriority(IBindStatusCallback *iface, LONG *pPriority)
235 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
237 TRACE("(%p)->(%p)\n", This, pPriority);
242 static HRESULT WINAPI BindStatusCallback_OnLowResource(IBindStatusCallback *iface, DWORD reserved)
244 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
246 TRACE("(%p)->(%d)\n", This, reserved);
251 static HRESULT WINAPI BindStatusCallback_OnProgress(IBindStatusCallback *iface, ULONG ulProgress,
252 ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
254 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
256 TRACE("(%p)->(%u %u %u %s)\n", This, ulProgress, ulProgressMax, ulStatusCode,
257 debugstr_w(szStatusText));
262 static HRESULT WINAPI BindStatusCallback_OnStopBinding(IBindStatusCallback *iface,
263 HRESULT hr, LPCWSTR error)
265 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
267 TRACE("(%p)->(0x%08x %s)\n", This, hr, debugstr_w(error));
271 IBinding_Release(This->binding);
272 This->binding = NULL;
276 httprequest_setreadystate(This->request, READYSTATE_COMPLETE);
281 static HRESULT WINAPI BindStatusCallback_GetBindInfo(IBindStatusCallback *iface,
282 DWORD *bind_flags, BINDINFO *pbindinfo)
284 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
286 TRACE("(%p)->(%p %p)\n", This, bind_flags, pbindinfo);
289 if (This->request->async) *bind_flags |= BINDF_ASYNCHRONOUS;
291 if (This->request->verb != BINDVERB_GET && This->body)
293 pbindinfo->stgmedData.tymed = TYMED_HGLOBAL;
294 pbindinfo->stgmedData.u.hGlobal = This->body;
295 pbindinfo->cbstgmedData = GlobalSize(This->body);
296 /* callback owns passed body pointer */
297 IBindStatusCallback_QueryInterface(iface, &IID_IUnknown, (void**)&pbindinfo->stgmedData.pUnkForRelease);
300 pbindinfo->dwBindVerb = This->request->verb;
305 static HRESULT WINAPI BindStatusCallback_OnDataAvailable(IBindStatusCallback *iface,
306 DWORD flags, DWORD size, FORMATETC *format, STGMEDIUM *stgmed)
308 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
313 TRACE("(%p)->(%08x %d %p %p)\n", This, flags, size, format, stgmed);
317 hr = IStream_Read(stgmed->u.pstm, buf, sizeof(buf), &read);
318 if (hr != S_OK) break;
320 hr = IStream_Write(This->stream, buf, read, &written);
321 } while((hr == S_OK) && written != 0 && read != 0);
323 httprequest_setreadystate(This->request, READYSTATE_INTERACTIVE);
328 static HRESULT WINAPI BindStatusCallback_OnObjectAvailable(IBindStatusCallback *iface,
329 REFIID riid, IUnknown *punk)
331 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
333 FIXME("(%p)->(%s %p): stub\n", This, debugstr_guid(riid), punk);
338 static const IBindStatusCallbackVtbl BindStatusCallbackVtbl = {
339 BindStatusCallback_QueryInterface,
340 BindStatusCallback_AddRef,
341 BindStatusCallback_Release,
342 BindStatusCallback_OnStartBinding,
343 BindStatusCallback_GetPriority,
344 BindStatusCallback_OnLowResource,
345 BindStatusCallback_OnProgress,
346 BindStatusCallback_OnStopBinding,
347 BindStatusCallback_GetBindInfo,
348 BindStatusCallback_OnDataAvailable,
349 BindStatusCallback_OnObjectAvailable
352 static HRESULT WINAPI BSCHttpNegotiate_QueryInterface(IHttpNegotiate *iface,
353 REFIID riid, void **ppv)
355 BindStatusCallback *This = impl_from_IHttpNegotiate(iface);
356 return IBindStatusCallback_QueryInterface(&This->IBindStatusCallback_iface, riid, ppv);
359 static ULONG WINAPI BSCHttpNegotiate_AddRef(IHttpNegotiate *iface)
361 BindStatusCallback *This = impl_from_IHttpNegotiate(iface);
362 return IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
365 static ULONG WINAPI BSCHttpNegotiate_Release(IHttpNegotiate *iface)
367 BindStatusCallback *This = impl_from_IHttpNegotiate(iface);
368 return IBindStatusCallback_Release(&This->IBindStatusCallback_iface);
371 static HRESULT WINAPI BSCHttpNegotiate_BeginningTransaction(IHttpNegotiate *iface,
372 LPCWSTR url, LPCWSTR headers, DWORD reserved, LPWSTR *add_headers)
374 BindStatusCallback *This = impl_from_IHttpNegotiate(iface);
375 const struct reqheader *entry;
378 TRACE("(%p)->(%s %s %d %p)\n", This, debugstr_w(url), debugstr_w(headers), reserved, add_headers);
382 if (list_empty(&This->request->reqheaders)) return S_OK;
384 buff = CoTaskMemAlloc(This->request->reqheader_size*sizeof(WCHAR));
385 if (!buff) return E_OUTOFMEMORY;
388 LIST_FOR_EACH_ENTRY(entry, &This->request->reqheaders, struct reqheader, entry)
390 lstrcpyW(ptr, entry->header);
391 ptr += SysStringLen(entry->header);
393 lstrcpyW(ptr, colspaceW);
394 ptr += sizeof(colspaceW)/sizeof(WCHAR)-1;
396 lstrcpyW(ptr, entry->value);
397 ptr += SysStringLen(entry->value);
399 lstrcpyW(ptr, crlfW);
400 ptr += sizeof(crlfW)/sizeof(WCHAR)-1;
408 static HRESULT WINAPI BSCHttpNegotiate_OnResponse(IHttpNegotiate *iface, DWORD code,
409 LPCWSTR resp_headers, LPCWSTR req_headers, LPWSTR *add_reqheaders)
411 BindStatusCallback *This = impl_from_IHttpNegotiate(iface);
413 TRACE("(%p)->(%d %s %s %p)\n", This, code, debugstr_w(resp_headers),
414 debugstr_w(req_headers), add_reqheaders);
416 This->request->status = code;
421 static const IHttpNegotiateVtbl BSCHttpNegotiateVtbl = {
422 BSCHttpNegotiate_QueryInterface,
423 BSCHttpNegotiate_AddRef,
424 BSCHttpNegotiate_Release,
425 BSCHttpNegotiate_BeginningTransaction,
426 BSCHttpNegotiate_OnResponse
429 static HRESULT BindStatusCallback_create(httprequest* This, BindStatusCallback **obj, const VARIANT *body)
431 BindStatusCallback *bsc;
435 hr = CreateBindCtx(0, &pbc);
436 if (hr != S_OK) return hr;
438 bsc = heap_alloc(sizeof(*bsc));
441 IBindCtx_Release(pbc);
442 return E_OUTOFMEMORY;
445 bsc->IBindStatusCallback_iface.lpVtbl = &BindStatusCallbackVtbl;
446 bsc->IHttpNegotiate_iface.lpVtbl = &BSCHttpNegotiateVtbl;
453 TRACE("created callback %p\n", bsc);
455 if (This->verb != BINDVERB_GET)
457 if (V_VT(body) == VT_BSTR)
459 LONG size = SysStringLen(V_BSTR(body)) * sizeof(WCHAR);
462 bsc->body = GlobalAlloc(GMEM_FIXED, size);
466 return E_OUTOFMEMORY;
469 ptr = GlobalLock(bsc->body);
470 memcpy(ptr, V_BSTR(body), size);
471 GlobalUnlock(bsc->body);
474 FIXME("unsupported body data type %d\n", V_VT(body));
477 hr = RegisterBindStatusCallback(pbc, &bsc->IBindStatusCallback_iface, NULL, 0);
482 hr = CreateURLMoniker(NULL, This->url, &moniker);
487 hr = IMoniker_BindToStorage(moniker, pbc, NULL, &IID_IStream, (void**)&stream);
488 IMoniker_Release(moniker);
489 if (stream) IStream_Release(stream);
491 IBindCtx_Release(pbc);
496 IBindStatusCallback_Release(&bsc->IBindStatusCallback_iface);
504 static HRESULT WINAPI httprequest_QueryInterface(IXMLHTTPRequest *iface, REFIID riid, void **ppvObject)
506 httprequest *This = impl_from_IXMLHTTPRequest( iface );
507 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppvObject);
509 if ( IsEqualGUID( riid, &IID_IXMLHTTPRequest) ||
510 IsEqualGUID( riid, &IID_IDispatch) ||
511 IsEqualGUID( riid, &IID_IUnknown) )
515 else if (IsEqualGUID(&IID_IObjectWithSite, riid))
517 *ppvObject = &This->IObjectWithSite_iface;
521 TRACE("Unsupported interface %s\n", debugstr_guid(riid));
523 return E_NOINTERFACE;
526 IXMLHTTPRequest_AddRef( iface );
531 static ULONG WINAPI httprequest_AddRef(IXMLHTTPRequest *iface)
533 httprequest *This = impl_from_IXMLHTTPRequest( iface );
534 ULONG ref = InterlockedIncrement( &This->ref );
535 TRACE("(%p)->(%u)\n", This, ref );
539 static ULONG WINAPI httprequest_Release(IXMLHTTPRequest *iface)
541 httprequest *This = impl_from_IXMLHTTPRequest( iface );
542 ULONG ref = InterlockedDecrement( &This->ref );
544 TRACE("(%p)->(%u)\n", This, ref );
548 struct reqheader *header, *header2;
551 IUnknown_Release( This->site );
553 SysFreeString(This->url);
554 SysFreeString(This->user);
555 SysFreeString(This->password);
557 /* request headers */
558 LIST_FOR_EACH_ENTRY_SAFE(header, header2, &This->reqheaders, struct reqheader, entry)
560 list_remove(&header->entry);
561 SysFreeString(header->header);
562 SysFreeString(header->value);
566 /* detach callback object */
567 BindStatusCallback_Detach(This->bsc);
569 if (This->sink) IDispatch_Release(This->sink);
577 static HRESULT WINAPI httprequest_GetTypeInfoCount(IXMLHTTPRequest *iface, UINT *pctinfo)
579 httprequest *This = impl_from_IXMLHTTPRequest( iface );
581 TRACE("(%p)->(%p)\n", This, pctinfo);
588 static HRESULT WINAPI httprequest_GetTypeInfo(IXMLHTTPRequest *iface, UINT iTInfo,
589 LCID lcid, ITypeInfo **ppTInfo)
591 httprequest *This = impl_from_IXMLHTTPRequest( iface );
594 TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
596 hr = get_typeinfo(IXMLHTTPRequest_tid, ppTInfo);
601 static HRESULT WINAPI httprequest_GetIDsOfNames(IXMLHTTPRequest *iface, REFIID riid,
602 LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
604 httprequest *This = impl_from_IXMLHTTPRequest( iface );
608 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
611 if(!rgszNames || cNames == 0 || !rgDispId)
614 hr = get_typeinfo(IXMLHTTPRequest_tid, &typeinfo);
617 hr = ITypeInfo_GetIDsOfNames(typeinfo, rgszNames, cNames, rgDispId);
618 ITypeInfo_Release(typeinfo);
624 static HRESULT WINAPI httprequest_Invoke(IXMLHTTPRequest *iface, DISPID dispIdMember, REFIID riid,
625 LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult,
626 EXCEPINFO *pExcepInfo, UINT *puArgErr)
628 httprequest *This = impl_from_IXMLHTTPRequest( iface );
632 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
633 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
635 hr = get_typeinfo(IXMLHTTPRequest_tid, &typeinfo);
638 hr = ITypeInfo_Invoke(typeinfo, &This->IXMLHTTPRequest_iface, dispIdMember, wFlags,
639 pDispParams, pVarResult, pExcepInfo, puArgErr);
640 ITypeInfo_Release(typeinfo);
646 static HRESULT WINAPI httprequest_open(IXMLHTTPRequest *iface, BSTR method, BSTR url,
647 VARIANT async, VARIANT user, VARIANT password)
649 httprequest *This = impl_from_IXMLHTTPRequest( iface );
653 TRACE("(%p)->(%s %s)\n", This, debugstr_w(method), debugstr_w(url));
655 if (!method || !url) return E_INVALIDARG;
657 /* free previously set data */
658 SysFreeString(This->url);
659 SysFreeString(This->user);
660 SysFreeString(This->password);
661 This->url = This->user = This->password = NULL;
663 if (lstrcmpiW(method, MethodGetW) == 0)
665 This->verb = BINDVERB_GET;
667 else if (lstrcmpiW(method, MethodPutW) == 0)
669 This->verb = BINDVERB_PUT;
671 else if (lstrcmpiW(method, MethodPostW) == 0)
673 This->verb = BINDVERB_POST;
677 FIXME("unsupported request type %s\n", debugstr_w(method));
682 This->url = SysAllocString(url);
684 hr = VariantChangeType(&async, &async, 0, VT_BOOL);
685 This->async = hr == S_OK && V_BOOL(&async) == VARIANT_TRUE;
688 hr = VariantChangeType(&str, &user, 0, VT_BSTR);
690 This->user = V_BSTR(&str);
692 hr = VariantChangeType(&str, &password, 0, VT_BSTR);
694 This->password = V_BSTR(&str);
696 httprequest_setreadystate(This, READYSTATE_LOADING);
701 static HRESULT WINAPI httprequest_setRequestHeader(IXMLHTTPRequest *iface, BSTR header, BSTR value)
703 httprequest *This = impl_from_IXMLHTTPRequest( iface );
704 struct reqheader *entry;
706 TRACE("(%p)->(%s %s)\n", This, debugstr_w(header), debugstr_w(value));
708 if (!header || !*header) return E_INVALIDARG;
709 if (This->state != READYSTATE_LOADING) return E_FAIL;
710 if (!value) return E_INVALIDARG;
712 /* replace existing header value if already added */
713 LIST_FOR_EACH_ENTRY(entry, &This->reqheaders, struct reqheader, entry)
715 if (lstrcmpW(entry->header, header) == 0)
717 LONG length = SysStringLen(entry->value);
720 hr = SysReAllocString(&entry->value, value) ? S_OK : E_OUTOFMEMORY;
723 This->reqheader_size += (SysStringLen(entry->value) - length);
729 entry = heap_alloc(sizeof(*entry));
730 if (!entry) return E_OUTOFMEMORY;
733 entry->header = SysAllocString(header);
734 entry->value = SysAllocString(value);
736 /* header length including null terminator */
737 This->reqheader_size += SysStringLen(entry->header) + sizeof(colspaceW)/sizeof(WCHAR) +
738 SysStringLen(entry->value) + sizeof(crlfW)/sizeof(WCHAR) - 1;
740 list_add_head(&This->reqheaders, &entry->entry);
745 static HRESULT WINAPI httprequest_getResponseHeader(IXMLHTTPRequest *iface, BSTR bstrHeader, BSTR *pbstrValue)
747 httprequest *This = impl_from_IXMLHTTPRequest( iface );
749 FIXME("stub (%p) %s %p\n", This, debugstr_w(bstrHeader), pbstrValue);
754 static HRESULT WINAPI httprequest_getAllResponseHeaders(IXMLHTTPRequest *iface, BSTR *pbstrHeaders)
756 httprequest *This = impl_from_IXMLHTTPRequest( iface );
758 FIXME("stub (%p) %p\n", This, pbstrHeaders);
763 static HRESULT WINAPI httprequest_send(IXMLHTTPRequest *iface, VARIANT body)
765 httprequest *This = impl_from_IXMLHTTPRequest( iface );
766 BindStatusCallback *bsc = NULL;
769 TRACE("(%p)\n", This);
771 if (This->state != READYSTATE_LOADING) return E_FAIL;
773 hr = BindStatusCallback_create(This, &bsc, &body);
774 if (FAILED(hr)) return hr;
776 BindStatusCallback_Detach(This->bsc);
782 static HRESULT WINAPI httprequest_abort(IXMLHTTPRequest *iface)
784 httprequest *This = impl_from_IXMLHTTPRequest( iface );
786 TRACE("(%p)\n", This);
788 BindStatusCallback_Detach(This->bsc);
791 httprequest_setreadystate(This, READYSTATE_UNINITIALIZED);
796 static HRESULT WINAPI httprequest_get_status(IXMLHTTPRequest *iface, LONG *status)
798 httprequest *This = impl_from_IXMLHTTPRequest( iface );
800 TRACE("(%p)->(%p)\n", This, status);
802 if (!status) return E_INVALIDARG;
803 if (This->state != READYSTATE_COMPLETE) return E_FAIL;
805 *status = This->status;
810 static HRESULT WINAPI httprequest_get_statusText(IXMLHTTPRequest *iface, BSTR *pbstrStatus)
812 httprequest *This = impl_from_IXMLHTTPRequest( iface );
814 FIXME("stub %p %p\n", This, pbstrStatus);
819 static HRESULT WINAPI httprequest_get_responseXML(IXMLHTTPRequest *iface, IDispatch **body)
821 httprequest *This = impl_from_IXMLHTTPRequest( iface );
822 IXMLDOMDocument3 *doc;
826 TRACE("(%p)->(%p)\n", This, body);
828 if (!body) return E_INVALIDARG;
829 if (This->state != READYSTATE_COMPLETE) return E_FAIL;
831 hr = DOMDocument_create(&CLSID_DOMDocument, NULL, (void**)&doc);
832 if (hr != S_OK) return hr;
834 hr = IXMLHTTPRequest_get_responseText(iface, &str);
839 hr = IXMLDOMDocument3_loadXML(doc, str, &ok);
843 IXMLDOMDocument3_QueryInterface(doc, &IID_IDispatch, (void**)body);
844 IXMLDOMDocument3_Release(doc);
849 static HRESULT WINAPI httprequest_get_responseText(IXMLHTTPRequest *iface, BSTR *body)
851 httprequest *This = impl_from_IXMLHTTPRequest( iface );
855 TRACE("(%p)->(%p)\n", This, body);
857 if (!body) return E_INVALIDARG;
858 if (This->state != READYSTATE_COMPLETE) return E_FAIL;
860 hr = GetHGlobalFromStream(This->bsc->stream, &hglobal);
863 xmlChar *ptr = GlobalLock(hglobal);
864 DWORD size = GlobalSize(hglobal);
865 xmlCharEncoding encoding = XML_CHAR_ENCODING_UTF8;
867 /* try to determine data encoding */
870 encoding = xmlDetectCharEncoding(ptr, 4);
871 TRACE("detected encoding: %s\n", xmlGetCharEncodingName(encoding));
872 if ( encoding != XML_CHAR_ENCODING_UTF8 &&
873 encoding != XML_CHAR_ENCODING_UTF16LE &&
874 encoding != XML_CHAR_ENCODING_NONE )
876 FIXME("unsupported encoding: %s\n", xmlGetCharEncodingName(encoding));
877 GlobalUnlock(hglobal);
882 /* without BOM assume UTF-8 */
883 if (encoding == XML_CHAR_ENCODING_UTF8 ||
884 encoding == XML_CHAR_ENCODING_NONE )
886 DWORD length = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)ptr, size, NULL, 0);
888 *body = SysAllocStringLen(NULL, length);
890 MultiByteToWideChar( CP_UTF8, 0, (LPCSTR)ptr, size, *body, length);
893 *body = SysAllocStringByteLen((LPCSTR)ptr, size);
895 if (!*body) hr = E_OUTOFMEMORY;
896 GlobalUnlock(hglobal);
902 static HRESULT WINAPI httprequest_get_responseBody(IXMLHTTPRequest *iface, VARIANT *body)
904 httprequest *This = impl_from_IXMLHTTPRequest( iface );
908 TRACE("(%p)->(%p)\n", This, body);
910 if (!body) return E_INVALIDARG;
911 if (This->state != READYSTATE_COMPLETE) return E_FAIL;
913 hr = GetHGlobalFromStream(This->bsc->stream, &hglobal);
916 void *ptr = GlobalLock(hglobal);
917 DWORD size = GlobalSize(hglobal);
919 SAFEARRAYBOUND bound;
923 bound.cElements = size;
924 array = SafeArrayCreate(VT_UI1, 1, &bound);
930 V_VT(body) = VT_ARRAY | VT_UI1;
931 V_ARRAY(body) = array;
933 hr = SafeArrayAccessData(array, &dest);
936 memcpy(dest, ptr, size);
937 SafeArrayUnaccessData(array);
947 GlobalUnlock(hglobal);
953 static HRESULT WINAPI httprequest_get_responseStream(IXMLHTTPRequest *iface, VARIANT *pvarBody)
955 httprequest *This = impl_from_IXMLHTTPRequest( iface );
957 FIXME("stub %p %p\n", This, pvarBody);
962 static HRESULT WINAPI httprequest_get_readyState(IXMLHTTPRequest *iface, LONG *state)
964 httprequest *This = impl_from_IXMLHTTPRequest( iface );
966 TRACE("(%p)->(%p)\n", This, state);
968 if (!state) return E_INVALIDARG;
970 *state = This->state;
974 static HRESULT WINAPI httprequest_put_onreadystatechange(IXMLHTTPRequest *iface, IDispatch *sink)
976 httprequest *This = impl_from_IXMLHTTPRequest( iface );
978 TRACE("(%p)->(%p)\n", This, sink);
980 if (This->sink) IDispatch_Release(This->sink);
981 if ((This->sink = sink)) IDispatch_AddRef(This->sink);
986 static const struct IXMLHTTPRequestVtbl dimimpl_vtbl =
988 httprequest_QueryInterface,
991 httprequest_GetTypeInfoCount,
992 httprequest_GetTypeInfo,
993 httprequest_GetIDsOfNames,
996 httprequest_setRequestHeader,
997 httprequest_getResponseHeader,
998 httprequest_getAllResponseHeaders,
1001 httprequest_get_status,
1002 httprequest_get_statusText,
1003 httprequest_get_responseXML,
1004 httprequest_get_responseText,
1005 httprequest_get_responseBody,
1006 httprequest_get_responseStream,
1007 httprequest_get_readyState,
1008 httprequest_put_onreadystatechange
1011 /* IObjectWithSite */
1012 static HRESULT WINAPI
1013 httprequest_ObjectWithSite_QueryInterface( IObjectWithSite* iface, REFIID riid, void** ppvObject )
1015 httprequest *This = impl_from_IObjectWithSite(iface);
1016 return IXMLHTTPRequest_QueryInterface( (IXMLHTTPRequest *)This, riid, ppvObject );
1019 static ULONG WINAPI httprequest_ObjectWithSite_AddRef( IObjectWithSite* iface )
1021 httprequest *This = impl_from_IObjectWithSite(iface);
1022 return IXMLHTTPRequest_AddRef((IXMLHTTPRequest *)This);
1025 static ULONG WINAPI httprequest_ObjectWithSite_Release( IObjectWithSite* iface )
1027 httprequest *This = impl_from_IObjectWithSite(iface);
1028 return IXMLHTTPRequest_Release((IXMLHTTPRequest *)This);
1031 static HRESULT WINAPI httprequest_ObjectWithSite_GetSite( IObjectWithSite *iface, REFIID iid, void **ppvSite )
1033 httprequest *This = impl_from_IObjectWithSite(iface);
1035 TRACE("(%p)->(%s %p)\n", This, debugstr_guid( iid ), ppvSite );
1040 return IUnknown_QueryInterface( This->site, iid, ppvSite );
1043 static HRESULT WINAPI httprequest_ObjectWithSite_SetSite( IObjectWithSite *iface, IUnknown *punk )
1045 httprequest *This = impl_from_IObjectWithSite(iface);
1047 TRACE("(%p)->(%p)\n", iface, punk);
1050 IUnknown_AddRef( punk );
1053 IUnknown_Release( This->site );
1060 static const IObjectWithSiteVtbl httprequestObjectSite =
1062 httprequest_ObjectWithSite_QueryInterface,
1063 httprequest_ObjectWithSite_AddRef,
1064 httprequest_ObjectWithSite_Release,
1065 httprequest_ObjectWithSite_SetSite,
1066 httprequest_ObjectWithSite_GetSite
1069 HRESULT XMLHTTPRequest_create(IUnknown *pUnkOuter, void **ppObj)
1074 TRACE("(%p,%p)\n", pUnkOuter, ppObj);
1076 req = heap_alloc( sizeof (*req) );
1078 return E_OUTOFMEMORY;
1080 req->IXMLHTTPRequest_iface.lpVtbl = &dimimpl_vtbl;
1081 req->IObjectWithSite_iface.lpVtbl = &httprequestObjectSite;
1086 req->url = req->user = req->password = NULL;
1088 req->state = READYSTATE_UNINITIALIZED;
1093 req->reqheader_size = 0;
1094 list_init(&req->reqheaders);
1097 *ppObj = &req->IXMLHTTPRequest_iface;
1099 TRACE("returning iface %p\n", *ppObj);
1106 HRESULT XMLHTTPRequest_create(IUnknown *pUnkOuter, void **ppObj)
1108 MESSAGE("This program tried to use a XMLHTTPRequest object, but\n"
1109 "libxml2 support was not present at compile time.\n");