2 * IXMLHTTPRequest implementation
4 * Copyright 2008 Alistair Leslie-Hughes
5 * Copyright 2010-2012 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
29 # include <libxml/parser.h>
30 # include <libxml/xmlerror.h>
31 # include <libxml/encoding.h>
41 #include "msxml_private.h"
43 #include "wine/debug.h"
44 #include "wine/list.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(msxml);
50 static const WCHAR colspaceW[] = {':',' ',0};
51 static const WCHAR crlfW[] = {'\r','\n',0};
53 typedef struct BindStatusCallback BindStatusCallback;
64 IXMLHTTPRequest IXMLHTTPRequest_iface;
65 IObjectWithSite IObjectWithSite_iface;
66 IObjectSafety IObjectSafety_iface;
77 struct list reqheaders;
78 /* cached resulting custom request headers string length in WCHARs */
80 /* use UTF-8 content type */
81 BOOL use_utf8_content;
83 /* response headers */
84 struct list respheaders;
92 BindStatusCallback *bsc;
102 static inline httprequest *impl_from_IXMLHTTPRequest( IXMLHTTPRequest *iface )
104 return CONTAINING_RECORD(iface, httprequest, IXMLHTTPRequest_iface);
107 static inline httprequest *impl_from_IObjectWithSite(IObjectWithSite *iface)
109 return CONTAINING_RECORD(iface, httprequest, IObjectWithSite_iface);
112 static inline httprequest *impl_from_IObjectSafety(IObjectSafety *iface)
114 return CONTAINING_RECORD(iface, httprequest, IObjectSafety_iface);
117 static void httprequest_setreadystate(httprequest *This, READYSTATE state)
119 READYSTATE last = This->state;
123 if (This->sink && last != state)
127 memset(¶ms, 0, sizeof(params));
128 IDispatch_Invoke(This->sink, 0, &IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD, ¶ms, 0, 0, 0);
132 static void free_response_headers(httprequest *This)
134 struct httpheader *header, *header2;
136 LIST_FOR_EACH_ENTRY_SAFE(header, header2, &This->respheaders, struct httpheader, entry)
138 list_remove(&header->entry);
139 SysFreeString(header->header);
140 SysFreeString(header->value);
144 SysFreeString(This->raw_respheaders);
145 This->raw_respheaders = NULL;
148 struct BindStatusCallback
150 IBindStatusCallback IBindStatusCallback_iface;
151 IHttpNegotiate IHttpNegotiate_iface;
152 IAuthenticate IAuthenticate_iface;
156 httprequest *request;
161 /* request body data */
165 static inline BindStatusCallback *impl_from_IBindStatusCallback( IBindStatusCallback *iface )
167 return CONTAINING_RECORD(iface, BindStatusCallback, IBindStatusCallback_iface);
170 static inline BindStatusCallback *impl_from_IHttpNegotiate( IHttpNegotiate *iface )
172 return CONTAINING_RECORD(iface, BindStatusCallback, IHttpNegotiate_iface);
175 static inline BindStatusCallback *impl_from_IAuthenticate( IAuthenticate *iface )
177 return CONTAINING_RECORD(iface, BindStatusCallback, IAuthenticate_iface);
180 static void BindStatusCallback_Detach(BindStatusCallback *bsc)
184 if (bsc->binding) IBinding_Abort(bsc->binding);
186 IBindStatusCallback_Release(&bsc->IBindStatusCallback_iface);
190 static HRESULT WINAPI BindStatusCallback_QueryInterface(IBindStatusCallback *iface,
191 REFIID riid, void **ppv)
193 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
197 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
199 if (IsEqualGUID(&IID_IUnknown, riid) ||
200 IsEqualGUID(&IID_IBindStatusCallback, riid))
202 *ppv = &This->IBindStatusCallback_iface;
204 else if (IsEqualGUID(&IID_IHttpNegotiate, riid))
206 *ppv = &This->IHttpNegotiate_iface;
208 else if (IsEqualGUID(&IID_IAuthenticate, riid))
210 *ppv = &This->IAuthenticate_iface;
212 else if (IsEqualGUID(&IID_IServiceProvider, riid) ||
213 IsEqualGUID(&IID_IBindStatusCallbackEx, riid) ||
214 IsEqualGUID(&IID_IInternetProtocol, riid) ||
215 IsEqualGUID(&IID_IHttpNegotiate2, riid))
217 return E_NOINTERFACE;
222 IBindStatusCallback_AddRef(iface);
226 FIXME("Unsupported riid = %s\n", debugstr_guid(riid));
228 return E_NOINTERFACE;
231 static ULONG WINAPI BindStatusCallback_AddRef(IBindStatusCallback *iface)
233 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
234 LONG ref = InterlockedIncrement(&This->ref);
236 TRACE("(%p) ref = %d\n", This, ref);
241 static ULONG WINAPI BindStatusCallback_Release(IBindStatusCallback *iface)
243 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
244 LONG ref = InterlockedDecrement(&This->ref);
246 TRACE("(%p) ref = %d\n", This, ref);
250 if (This->binding) IBinding_Release(This->binding);
251 if (This->stream) IStream_Release(This->stream);
252 if (This->body) GlobalFree(This->body);
259 static HRESULT WINAPI BindStatusCallback_OnStartBinding(IBindStatusCallback *iface,
260 DWORD reserved, IBinding *pbind)
262 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
264 TRACE("(%p)->(%d %p)\n", This, reserved, pbind);
266 if (!pbind) return E_INVALIDARG;
268 This->binding = pbind;
269 IBinding_AddRef(pbind);
271 httprequest_setreadystate(This->request, READYSTATE_LOADED);
273 return CreateStreamOnHGlobal(NULL, TRUE, &This->stream);
276 static HRESULT WINAPI BindStatusCallback_GetPriority(IBindStatusCallback *iface, LONG *pPriority)
278 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
280 TRACE("(%p)->(%p)\n", This, pPriority);
285 static HRESULT WINAPI BindStatusCallback_OnLowResource(IBindStatusCallback *iface, DWORD reserved)
287 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
289 TRACE("(%p)->(%d)\n", This, reserved);
294 static HRESULT WINAPI BindStatusCallback_OnProgress(IBindStatusCallback *iface, ULONG ulProgress,
295 ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
297 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
299 TRACE("(%p)->(%u %u %u %s)\n", This, ulProgress, ulProgressMax, ulStatusCode,
300 debugstr_w(szStatusText));
305 static HRESULT WINAPI BindStatusCallback_OnStopBinding(IBindStatusCallback *iface,
306 HRESULT hr, LPCWSTR error)
308 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
310 TRACE("(%p)->(0x%08x %s)\n", This, hr, debugstr_w(error));
314 IBinding_Release(This->binding);
315 This->binding = NULL;
319 httprequest_setreadystate(This->request, READYSTATE_COMPLETE);
324 static HRESULT WINAPI BindStatusCallback_GetBindInfo(IBindStatusCallback *iface,
325 DWORD *bind_flags, BINDINFO *pbindinfo)
327 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
329 TRACE("(%p)->(%p %p)\n", This, bind_flags, pbindinfo);
332 if (This->request->async) *bind_flags |= BINDF_ASYNCHRONOUS;
334 if (This->request->verb != BINDVERB_GET && This->body)
336 pbindinfo->stgmedData.tymed = TYMED_HGLOBAL;
337 pbindinfo->stgmedData.u.hGlobal = This->body;
338 pbindinfo->cbstgmedData = GlobalSize(This->body);
339 /* callback owns passed body pointer */
340 IBindStatusCallback_QueryInterface(iface, &IID_IUnknown, (void**)&pbindinfo->stgmedData.pUnkForRelease);
343 pbindinfo->dwBindVerb = This->request->verb;
344 if (This->request->verb == BINDVERB_CUSTOM)
346 pbindinfo->szCustomVerb = CoTaskMemAlloc(SysStringByteLen(This->request->custom));
347 strcpyW(pbindinfo->szCustomVerb, This->request->custom);
353 static HRESULT WINAPI BindStatusCallback_OnDataAvailable(IBindStatusCallback *iface,
354 DWORD flags, DWORD size, FORMATETC *format, STGMEDIUM *stgmed)
356 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
361 TRACE("(%p)->(%08x %d %p %p)\n", This, flags, size, format, stgmed);
365 hr = IStream_Read(stgmed->u.pstm, buf, sizeof(buf), &read);
366 if (hr != S_OK) break;
368 hr = IStream_Write(This->stream, buf, read, &written);
369 } while((hr == S_OK) && written != 0 && read != 0);
371 httprequest_setreadystate(This->request, READYSTATE_INTERACTIVE);
376 static HRESULT WINAPI BindStatusCallback_OnObjectAvailable(IBindStatusCallback *iface,
377 REFIID riid, IUnknown *punk)
379 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
381 FIXME("(%p)->(%s %p): stub\n", This, debugstr_guid(riid), punk);
386 static const IBindStatusCallbackVtbl BindStatusCallbackVtbl = {
387 BindStatusCallback_QueryInterface,
388 BindStatusCallback_AddRef,
389 BindStatusCallback_Release,
390 BindStatusCallback_OnStartBinding,
391 BindStatusCallback_GetPriority,
392 BindStatusCallback_OnLowResource,
393 BindStatusCallback_OnProgress,
394 BindStatusCallback_OnStopBinding,
395 BindStatusCallback_GetBindInfo,
396 BindStatusCallback_OnDataAvailable,
397 BindStatusCallback_OnObjectAvailable
400 static HRESULT WINAPI BSCHttpNegotiate_QueryInterface(IHttpNegotiate *iface,
401 REFIID riid, void **ppv)
403 BindStatusCallback *This = impl_from_IHttpNegotiate(iface);
404 return IBindStatusCallback_QueryInterface(&This->IBindStatusCallback_iface, riid, ppv);
407 static ULONG WINAPI BSCHttpNegotiate_AddRef(IHttpNegotiate *iface)
409 BindStatusCallback *This = impl_from_IHttpNegotiate(iface);
410 return IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
413 static ULONG WINAPI BSCHttpNegotiate_Release(IHttpNegotiate *iface)
415 BindStatusCallback *This = impl_from_IHttpNegotiate(iface);
416 return IBindStatusCallback_Release(&This->IBindStatusCallback_iface);
419 static HRESULT WINAPI BSCHttpNegotiate_BeginningTransaction(IHttpNegotiate *iface,
420 LPCWSTR url, LPCWSTR headers, DWORD reserved, LPWSTR *add_headers)
422 static const WCHAR content_type_utf8W[] = {'C','o','n','t','e','n','t','-','T','y','p','e',':',' ',
423 't','e','x','t','/','p','l','a','i','n',';','c','h','a','r','s','e','t','=','u','t','f','-','8','\r','\n',0};
425 BindStatusCallback *This = impl_from_IHttpNegotiate(iface);
426 const struct httpheader *entry;
430 TRACE("(%p)->(%s %s %d %p)\n", This, debugstr_w(url), debugstr_w(headers), reserved, add_headers);
434 if (This->request->use_utf8_content)
435 size = sizeof(content_type_utf8W);
437 if (!list_empty(&This->request->reqheaders))
438 size += This->request->reqheader_size*sizeof(WCHAR);
440 if (!size) return S_OK;
442 buff = CoTaskMemAlloc(size);
443 if (!buff) return E_OUTOFMEMORY;
446 if (This->request->use_utf8_content)
448 lstrcpyW(ptr, content_type_utf8W);
449 ptr += sizeof(content_type_utf8W)/sizeof(WCHAR)-1;
453 LIST_FOR_EACH_ENTRY(entry, &This->request->reqheaders, struct httpheader, entry)
455 lstrcpyW(ptr, entry->header);
456 ptr += SysStringLen(entry->header);
458 lstrcpyW(ptr, colspaceW);
459 ptr += sizeof(colspaceW)/sizeof(WCHAR)-1;
461 lstrcpyW(ptr, entry->value);
462 ptr += SysStringLen(entry->value);
464 lstrcpyW(ptr, crlfW);
465 ptr += sizeof(crlfW)/sizeof(WCHAR)-1;
473 static void add_response_header(httprequest *This, const WCHAR *data, int len)
475 struct httpheader *entry;
476 const WCHAR *ptr = data;
483 header = SysAllocStringLen(data, ptr-data);
484 /* skip leading spaces for a value */
485 while (*++ptr == ' ')
487 value = SysAllocStringLen(ptr, len-(ptr-data));
496 TRACE("got header %s:%s\n", debugstr_w(header), debugstr_w(value));
498 entry = heap_alloc(sizeof(*header));
499 entry->header = header;
500 entry->value = value;
501 list_add_head(&This->respheaders, &entry->entry);
504 static HRESULT WINAPI BSCHttpNegotiate_OnResponse(IHttpNegotiate *iface, DWORD code,
505 LPCWSTR resp_headers, LPCWSTR req_headers, LPWSTR *add_reqheaders)
507 BindStatusCallback *This = impl_from_IHttpNegotiate(iface);
509 TRACE("(%p)->(%d %s %s %p)\n", This, code, debugstr_w(resp_headers),
510 debugstr_w(req_headers), add_reqheaders);
512 This->request->status = code;
514 free_response_headers(This->request);
517 const WCHAR *ptr, *line;
519 ptr = line = resp_headers;
521 /* skip status line */
524 if (*ptr == '\r' && *(ptr+1) == '\n')
532 /* store as unparsed string for now */
533 This->request->raw_respheaders = SysAllocString(line);
539 static const IHttpNegotiateVtbl BSCHttpNegotiateVtbl = {
540 BSCHttpNegotiate_QueryInterface,
541 BSCHttpNegotiate_AddRef,
542 BSCHttpNegotiate_Release,
543 BSCHttpNegotiate_BeginningTransaction,
544 BSCHttpNegotiate_OnResponse
547 static HRESULT WINAPI Authenticate_QueryInterface(IAuthenticate *iface,
548 REFIID riid, void **ppv)
550 BindStatusCallback *This = impl_from_IAuthenticate(iface);
551 return IBindStatusCallback_QueryInterface(&This->IBindStatusCallback_iface, riid, ppv);
554 static ULONG WINAPI Authenticate_AddRef(IAuthenticate *iface)
556 BindStatusCallback *This = impl_from_IAuthenticate(iface);
557 return IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
560 static ULONG WINAPI Authenticate_Release(IAuthenticate *iface)
562 BindStatusCallback *This = impl_from_IAuthenticate(iface);
563 return IBindStatusCallback_Release(&This->IBindStatusCallback_iface);
566 static HRESULT WINAPI Authenticate_Authenticate(IAuthenticate *iface,
567 HWND *hwnd, LPWSTR *username, LPWSTR *password)
569 BindStatusCallback *This = impl_from_IAuthenticate(iface);
570 FIXME("(%p)->(%p %p %p)\n", This, hwnd, username, password);
574 static const IAuthenticateVtbl AuthenticateVtbl = {
575 Authenticate_QueryInterface,
577 Authenticate_Release,
578 Authenticate_Authenticate
581 static HRESULT BindStatusCallback_create(httprequest* This, BindStatusCallback **obj, const VARIANT *body)
583 BindStatusCallback *bsc;
588 hr = CreateBindCtx(0, &pbc);
589 if (hr != S_OK) return hr;
591 bsc = heap_alloc(sizeof(*bsc));
594 IBindCtx_Release(pbc);
595 return E_OUTOFMEMORY;
598 bsc->IBindStatusCallback_iface.lpVtbl = &BindStatusCallbackVtbl;
599 bsc->IHttpNegotiate_iface.lpVtbl = &BSCHttpNegotiateVtbl;
600 bsc->IAuthenticate_iface.lpVtbl = &AuthenticateVtbl;
607 TRACE("(%p)->(%p)\n", This, bsc);
609 This->use_utf8_content = FALSE;
611 if (This->verb != BINDVERB_GET)
613 void *send_data, *ptr;
614 SAFEARRAY *sa = NULL;
616 if (V_VT(body) == (VT_VARIANT|VT_BYREF))
617 body = V_VARIANTREF(body);
623 int len = SysStringLen(V_BSTR(body));
624 const WCHAR *str = V_BSTR(body);
627 for (i = 0; i < len; i++)
636 size = WideCharToMultiByte(cp, 0, str, len, NULL, 0, NULL, NULL);
637 if (!(ptr = heap_alloc(size)))
640 return E_OUTOFMEMORY;
642 WideCharToMultiByte(cp, 0, str, len, ptr, size, NULL, NULL);
643 if (cp == CP_UTF8) This->use_utf8_content = TRUE;
646 case VT_ARRAY|VT_UI1:
649 if ((hr = SafeArrayAccessData(sa, (void **)&ptr)) != S_OK) return hr;
650 if ((hr = SafeArrayGetUBound(sa, 1, &size) != S_OK))
652 SafeArrayUnaccessData(sa);
663 FIXME("unsupported body data type %d\n", V_VT(body));
667 bsc->body = GlobalAlloc(GMEM_FIXED, size);
670 if (V_VT(body) == VT_BSTR)
672 else if (V_VT(body) == (VT_ARRAY|VT_UI1))
673 SafeArrayUnaccessData(sa);
676 return E_OUTOFMEMORY;
679 send_data = GlobalLock(bsc->body);
680 memcpy(send_data, ptr, size);
681 GlobalUnlock(bsc->body);
683 if (V_VT(body) == VT_BSTR)
685 else if (V_VT(body) == (VT_ARRAY|VT_UI1))
686 SafeArrayUnaccessData(sa);
689 hr = RegisterBindStatusCallback(pbc, &bsc->IBindStatusCallback_iface, NULL, 0);
694 hr = CreateURLMoniker(NULL, This->url, &moniker);
699 hr = IMoniker_BindToStorage(moniker, pbc, NULL, &IID_IStream, (void**)&stream);
700 IMoniker_Release(moniker);
701 if (stream) IStream_Release(stream);
703 IBindCtx_Release(pbc);
708 IBindStatusCallback_Release(&bsc->IBindStatusCallback_iface);
716 static HRESULT WINAPI httprequest_QueryInterface(IXMLHTTPRequest *iface, REFIID riid, void **ppvObject)
718 httprequest *This = impl_from_IXMLHTTPRequest( iface );
719 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppvObject);
721 if ( IsEqualGUID( riid, &IID_IXMLHTTPRequest) ||
722 IsEqualGUID( riid, &IID_IDispatch) ||
723 IsEqualGUID( riid, &IID_IUnknown) )
727 else if (IsEqualGUID(&IID_IObjectWithSite, riid))
729 *ppvObject = &This->IObjectWithSite_iface;
731 else if (IsEqualGUID(&IID_IObjectSafety, riid))
733 *ppvObject = &This->IObjectSafety_iface;
737 TRACE("Unsupported interface %s\n", debugstr_guid(riid));
739 return E_NOINTERFACE;
742 IXMLHTTPRequest_AddRef( iface );
747 static ULONG WINAPI httprequest_AddRef(IXMLHTTPRequest *iface)
749 httprequest *This = impl_from_IXMLHTTPRequest( iface );
750 ULONG ref = InterlockedIncrement( &This->ref );
751 TRACE("(%p)->(%u)\n", This, ref );
755 static ULONG WINAPI httprequest_Release(IXMLHTTPRequest *iface)
757 httprequest *This = impl_from_IXMLHTTPRequest( iface );
758 ULONG ref = InterlockedDecrement( &This->ref );
760 TRACE("(%p)->(%u)\n", This, ref );
764 struct httpheader *header, *header2;
767 IUnknown_Release( This->site );
769 SysFreeString(This->custom);
770 SysFreeString(This->url);
771 SysFreeString(This->user);
772 SysFreeString(This->password);
774 /* request headers */
775 LIST_FOR_EACH_ENTRY_SAFE(header, header2, &This->reqheaders, struct httpheader, entry)
777 list_remove(&header->entry);
778 SysFreeString(header->header);
779 SysFreeString(header->value);
782 /* response headers */
783 free_response_headers(This);
785 /* detach callback object */
786 BindStatusCallback_Detach(This->bsc);
788 if (This->sink) IDispatch_Release(This->sink);
796 static HRESULT WINAPI httprequest_GetTypeInfoCount(IXMLHTTPRequest *iface, UINT *pctinfo)
798 httprequest *This = impl_from_IXMLHTTPRequest( iface );
800 TRACE("(%p)->(%p)\n", This, pctinfo);
807 static HRESULT WINAPI httprequest_GetTypeInfo(IXMLHTTPRequest *iface, UINT iTInfo,
808 LCID lcid, ITypeInfo **ppTInfo)
810 httprequest *This = impl_from_IXMLHTTPRequest( iface );
812 TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
814 return get_typeinfo(IXMLHTTPRequest_tid, ppTInfo);
817 static HRESULT WINAPI httprequest_GetIDsOfNames(IXMLHTTPRequest *iface, REFIID riid,
818 LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
820 httprequest *This = impl_from_IXMLHTTPRequest( iface );
824 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
827 if(!rgszNames || cNames == 0 || !rgDispId)
830 hr = get_typeinfo(IXMLHTTPRequest_tid, &typeinfo);
833 hr = ITypeInfo_GetIDsOfNames(typeinfo, rgszNames, cNames, rgDispId);
834 ITypeInfo_Release(typeinfo);
840 static HRESULT WINAPI httprequest_Invoke(IXMLHTTPRequest *iface, DISPID dispIdMember, REFIID riid,
841 LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult,
842 EXCEPINFO *pExcepInfo, UINT *puArgErr)
844 httprequest *This = impl_from_IXMLHTTPRequest( iface );
848 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
849 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
851 hr = get_typeinfo(IXMLHTTPRequest_tid, &typeinfo);
854 hr = ITypeInfo_Invoke(typeinfo, &This->IXMLHTTPRequest_iface, dispIdMember, wFlags,
855 pDispParams, pVarResult, pExcepInfo, puArgErr);
856 ITypeInfo_Release(typeinfo);
862 static HRESULT WINAPI httprequest_open(IXMLHTTPRequest *iface, BSTR method, BSTR url,
863 VARIANT async, VARIANT user, VARIANT password)
865 static const WCHAR MethodGetW[] = {'G','E','T',0};
866 static const WCHAR MethodPutW[] = {'P','U','T',0};
867 static const WCHAR MethodPostW[] = {'P','O','S','T',0};
868 static const WCHAR MethodDeleteW[] = {'D','E','L','E','T','E',0};
870 httprequest *This = impl_from_IXMLHTTPRequest( iface );
871 VARIANT str, is_async;
874 TRACE("(%p)->(%s %s %s)\n", This, debugstr_w(method), debugstr_w(url),
875 debugstr_variant(&async));
877 if (!method || !url) return E_INVALIDARG;
879 /* free previously set data */
880 SysFreeString(This->url);
881 SysFreeString(This->user);
882 SysFreeString(This->password);
883 This->url = This->user = This->password = NULL;
885 if (!strcmpiW(method, MethodGetW))
887 This->verb = BINDVERB_GET;
889 else if (!strcmpiW(method, MethodPutW))
891 This->verb = BINDVERB_PUT;
893 else if (!strcmpiW(method, MethodPostW))
895 This->verb = BINDVERB_POST;
897 else if (!strcmpiW(method, MethodDeleteW))
899 This->verb = BINDVERB_CUSTOM;
900 SysReAllocString(&This->custom, method);
904 FIXME("unsupported request type %s\n", debugstr_w(method));
909 This->url = SysAllocString(url);
911 VariantInit(&is_async);
912 hr = VariantChangeType(&is_async, &async, 0, VT_BOOL);
913 This->async = hr == S_OK && V_BOOL(&is_async) == VARIANT_TRUE;
916 hr = VariantChangeType(&str, &user, 0, VT_BSTR);
918 This->user = V_BSTR(&str);
921 hr = VariantChangeType(&str, &password, 0, VT_BSTR);
923 This->password = V_BSTR(&str);
925 httprequest_setreadystate(This, READYSTATE_LOADING);
930 static HRESULT WINAPI httprequest_setRequestHeader(IXMLHTTPRequest *iface, BSTR header, BSTR value)
932 httprequest *This = impl_from_IXMLHTTPRequest( iface );
933 struct httpheader *entry;
935 TRACE("(%p)->(%s %s)\n", This, debugstr_w(header), debugstr_w(value));
937 if (!header || !*header) return E_INVALIDARG;
938 if (This->state != READYSTATE_LOADING) return E_FAIL;
939 if (!value) return E_INVALIDARG;
941 /* replace existing header value if already added */
942 LIST_FOR_EACH_ENTRY(entry, &This->reqheaders, struct httpheader, entry)
944 if (lstrcmpW(entry->header, header) == 0)
946 LONG length = SysStringLen(entry->value);
949 hr = SysReAllocString(&entry->value, value) ? S_OK : E_OUTOFMEMORY;
952 This->reqheader_size += (SysStringLen(entry->value) - length);
958 entry = heap_alloc(sizeof(*entry));
959 if (!entry) return E_OUTOFMEMORY;
962 entry->header = SysAllocString(header);
963 entry->value = SysAllocString(value);
965 /* header length including null terminator */
966 This->reqheader_size += SysStringLen(entry->header) + sizeof(colspaceW)/sizeof(WCHAR) +
967 SysStringLen(entry->value) + sizeof(crlfW)/sizeof(WCHAR) - 1;
969 list_add_head(&This->reqheaders, &entry->entry);
974 static HRESULT WINAPI httprequest_getResponseHeader(IXMLHTTPRequest *iface, BSTR header, BSTR *value)
976 httprequest *This = impl_from_IXMLHTTPRequest( iface );
977 struct httpheader *entry;
979 TRACE("(%p)->(%s %p)\n", This, debugstr_w(header), value);
981 if (!header || !value) return E_INVALIDARG;
983 if (This->raw_respheaders && list_empty(&This->respheaders))
987 ptr = line = This->raw_respheaders;
990 if (*ptr == '\r' && *(ptr+1) == '\n')
992 add_response_header(This, line, ptr-line);
1000 LIST_FOR_EACH_ENTRY(entry, &This->respheaders, struct httpheader, entry)
1002 if (!strcmpiW(entry->header, header))
1004 *value = SysAllocString(entry->value);
1005 TRACE("header value %s\n", debugstr_w(*value));
1013 static HRESULT WINAPI httprequest_getAllResponseHeaders(IXMLHTTPRequest *iface, BSTR *respheaders)
1015 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1017 TRACE("(%p)->(%p)\n", This, respheaders);
1019 if (!respheaders) return E_INVALIDARG;
1021 *respheaders = SysAllocString(This->raw_respheaders);
1026 static HRESULT WINAPI httprequest_send(IXMLHTTPRequest *iface, VARIANT body)
1028 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1029 BindStatusCallback *bsc = NULL;
1032 TRACE("(%p)->(%s)\n", This, debugstr_variant(&body));
1034 if (This->state != READYSTATE_LOADING) return E_FAIL;
1036 hr = BindStatusCallback_create(This, &bsc, &body);
1037 if (FAILED(hr)) return hr;
1039 BindStatusCallback_Detach(This->bsc);
1045 static HRESULT WINAPI httprequest_abort(IXMLHTTPRequest *iface)
1047 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1049 TRACE("(%p)\n", This);
1051 BindStatusCallback_Detach(This->bsc);
1054 httprequest_setreadystate(This, READYSTATE_UNINITIALIZED);
1059 static HRESULT WINAPI httprequest_get_status(IXMLHTTPRequest *iface, LONG *status)
1061 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1063 TRACE("(%p)->(%p)\n", This, status);
1065 if (!status) return E_INVALIDARG;
1066 if (This->state != READYSTATE_COMPLETE) return E_FAIL;
1068 *status = This->status;
1073 static HRESULT WINAPI httprequest_get_statusText(IXMLHTTPRequest *iface, BSTR *pbstrStatus)
1075 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1077 FIXME("stub %p %p\n", This, pbstrStatus);
1082 static HRESULT WINAPI httprequest_get_responseXML(IXMLHTTPRequest *iface, IDispatch **body)
1084 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1085 IXMLDOMDocument3 *doc;
1089 TRACE("(%p)->(%p)\n", This, body);
1091 if (!body) return E_INVALIDARG;
1092 if (This->state != READYSTATE_COMPLETE) return E_FAIL;
1094 hr = DOMDocument_create(MSXML_DEFAULT, NULL, (void**)&doc);
1095 if (hr != S_OK) return hr;
1097 hr = IXMLHTTPRequest_get_responseText(iface, &str);
1102 hr = IXMLDOMDocument3_loadXML(doc, str, &ok);
1106 IXMLDOMDocument3_QueryInterface(doc, &IID_IDispatch, (void**)body);
1107 IXMLDOMDocument3_Release(doc);
1112 static HRESULT WINAPI httprequest_get_responseText(IXMLHTTPRequest *iface, BSTR *body)
1114 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1118 TRACE("(%p)->(%p)\n", This, body);
1120 if (!body) return E_INVALIDARG;
1121 if (This->state != READYSTATE_COMPLETE) return E_FAIL;
1123 hr = GetHGlobalFromStream(This->bsc->stream, &hglobal);
1126 xmlChar *ptr = GlobalLock(hglobal);
1127 DWORD size = GlobalSize(hglobal);
1128 xmlCharEncoding encoding = XML_CHAR_ENCODING_UTF8;
1130 /* try to determine data encoding */
1133 encoding = xmlDetectCharEncoding(ptr, 4);
1134 TRACE("detected encoding: %s\n", debugstr_a(xmlGetCharEncodingName(encoding)));
1135 if ( encoding != XML_CHAR_ENCODING_UTF8 &&
1136 encoding != XML_CHAR_ENCODING_UTF16LE &&
1137 encoding != XML_CHAR_ENCODING_NONE )
1139 FIXME("unsupported encoding: %s\n", debugstr_a(xmlGetCharEncodingName(encoding)));
1140 GlobalUnlock(hglobal);
1145 /* without BOM assume UTF-8 */
1146 if (encoding == XML_CHAR_ENCODING_UTF8 ||
1147 encoding == XML_CHAR_ENCODING_NONE )
1149 DWORD length = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)ptr, size, NULL, 0);
1151 *body = SysAllocStringLen(NULL, length);
1153 MultiByteToWideChar( CP_UTF8, 0, (LPCSTR)ptr, size, *body, length);
1156 *body = SysAllocStringByteLen((LPCSTR)ptr, size);
1158 if (!*body) hr = E_OUTOFMEMORY;
1159 GlobalUnlock(hglobal);
1165 static HRESULT WINAPI httprequest_get_responseBody(IXMLHTTPRequest *iface, VARIANT *body)
1167 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1171 TRACE("(%p)->(%p)\n", This, body);
1173 if (!body) return E_INVALIDARG;
1174 V_VT(body) = VT_EMPTY;
1176 if (This->state != READYSTATE_COMPLETE) return E_PENDING;
1178 hr = GetHGlobalFromStream(This->bsc->stream, &hglobal);
1181 void *ptr = GlobalLock(hglobal);
1182 DWORD size = GlobalSize(hglobal);
1184 SAFEARRAYBOUND bound;
1188 bound.cElements = size;
1189 array = SafeArrayCreate(VT_UI1, 1, &bound);
1195 V_VT(body) = VT_ARRAY | VT_UI1;
1196 V_ARRAY(body) = array;
1198 hr = SafeArrayAccessData(array, &dest);
1201 memcpy(dest, ptr, size);
1202 SafeArrayUnaccessData(array);
1212 GlobalUnlock(hglobal);
1218 static HRESULT WINAPI httprequest_get_responseStream(IXMLHTTPRequest *iface, VARIANT *body)
1220 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1225 TRACE("(%p)->(%p)\n", This, body);
1227 if (!body) return E_INVALIDARG;
1228 V_VT(body) = VT_EMPTY;
1230 if (This->state != READYSTATE_COMPLETE) return E_PENDING;
1232 hr = IStream_Clone(This->bsc->stream, &stream);
1235 IStream_Seek(stream, move, STREAM_SEEK_SET, NULL);
1237 V_VT(body) = VT_UNKNOWN;
1238 V_UNKNOWN(body) = (IUnknown*)stream;
1243 static HRESULT WINAPI httprequest_get_readyState(IXMLHTTPRequest *iface, LONG *state)
1245 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1247 TRACE("(%p)->(%p)\n", This, state);
1249 if (!state) return E_INVALIDARG;
1251 *state = This->state;
1255 static HRESULT WINAPI httprequest_put_onreadystatechange(IXMLHTTPRequest *iface, IDispatch *sink)
1257 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1259 TRACE("(%p)->(%p)\n", This, sink);
1261 if (This->sink) IDispatch_Release(This->sink);
1262 if ((This->sink = sink)) IDispatch_AddRef(This->sink);
1267 static const struct IXMLHTTPRequestVtbl XMLHTTPRequestVtbl =
1269 httprequest_QueryInterface,
1271 httprequest_Release,
1272 httprequest_GetTypeInfoCount,
1273 httprequest_GetTypeInfo,
1274 httprequest_GetIDsOfNames,
1277 httprequest_setRequestHeader,
1278 httprequest_getResponseHeader,
1279 httprequest_getAllResponseHeaders,
1282 httprequest_get_status,
1283 httprequest_get_statusText,
1284 httprequest_get_responseXML,
1285 httprequest_get_responseText,
1286 httprequest_get_responseBody,
1287 httprequest_get_responseStream,
1288 httprequest_get_readyState,
1289 httprequest_put_onreadystatechange
1292 /* IObjectWithSite */
1293 static HRESULT WINAPI
1294 httprequest_ObjectWithSite_QueryInterface( IObjectWithSite* iface, REFIID riid, void** ppvObject )
1296 httprequest *This = impl_from_IObjectWithSite(iface);
1297 return IXMLHTTPRequest_QueryInterface( (IXMLHTTPRequest *)This, riid, ppvObject );
1300 static ULONG WINAPI httprequest_ObjectWithSite_AddRef( IObjectWithSite* iface )
1302 httprequest *This = impl_from_IObjectWithSite(iface);
1303 return IXMLHTTPRequest_AddRef((IXMLHTTPRequest *)This);
1306 static ULONG WINAPI httprequest_ObjectWithSite_Release( IObjectWithSite* iface )
1308 httprequest *This = impl_from_IObjectWithSite(iface);
1309 return IXMLHTTPRequest_Release((IXMLHTTPRequest *)This);
1312 static HRESULT WINAPI httprequest_ObjectWithSite_GetSite( IObjectWithSite *iface, REFIID iid, void **ppvSite )
1314 httprequest *This = impl_from_IObjectWithSite(iface);
1316 TRACE("(%p)->(%s %p)\n", This, debugstr_guid( iid ), ppvSite );
1321 return IUnknown_QueryInterface( This->site, iid, ppvSite );
1324 static HRESULT WINAPI httprequest_ObjectWithSite_SetSite( IObjectWithSite *iface, IUnknown *punk )
1326 httprequest *This = impl_from_IObjectWithSite(iface);
1328 TRACE("(%p)->(%p)\n", iface, punk);
1331 IUnknown_AddRef( punk );
1334 IUnknown_Release( This->site );
1341 static const IObjectWithSiteVtbl ObjectWithSiteVtbl =
1343 httprequest_ObjectWithSite_QueryInterface,
1344 httprequest_ObjectWithSite_AddRef,
1345 httprequest_ObjectWithSite_Release,
1346 httprequest_ObjectWithSite_SetSite,
1347 httprequest_ObjectWithSite_GetSite
1351 static HRESULT WINAPI httprequest_Safety_QueryInterface(IObjectSafety *iface, REFIID riid, void **ppv)
1353 httprequest *This = impl_from_IObjectSafety(iface);
1354 return IXMLHTTPRequest_QueryInterface( (IXMLHTTPRequest *)This, riid, ppv );
1357 static ULONG WINAPI httprequest_Safety_AddRef(IObjectSafety *iface)
1359 httprequest *This = impl_from_IObjectSafety(iface);
1360 return IXMLHTTPRequest_AddRef((IXMLHTTPRequest *)This);
1363 static ULONG WINAPI httprequest_Safety_Release(IObjectSafety *iface)
1365 httprequest *This = impl_from_IObjectSafety(iface);
1366 return IXMLHTTPRequest_Release((IXMLHTTPRequest *)This);
1369 #define SAFETY_SUPPORTED_OPTIONS (INTERFACESAFE_FOR_UNTRUSTED_CALLER|INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_SECURITY_MANAGER)
1371 static HRESULT WINAPI httprequest_Safety_GetInterfaceSafetyOptions(IObjectSafety *iface, REFIID riid,
1372 DWORD *supported, DWORD *enabled)
1374 httprequest *This = impl_from_IObjectSafety(iface);
1376 TRACE("(%p)->(%s %p %p)\n", This, debugstr_guid(riid), supported, enabled);
1378 if(!supported || !enabled) return E_POINTER;
1380 *supported = SAFETY_SUPPORTED_OPTIONS;
1381 *enabled = This->safeopt;
1386 static HRESULT WINAPI httprequest_Safety_SetInterfaceSafetyOptions(IObjectSafety *iface, REFIID riid,
1387 DWORD mask, DWORD enabled)
1389 httprequest *This = impl_from_IObjectSafety(iface);
1390 TRACE("(%p)->(%s %x %x)\n", This, debugstr_guid(riid), mask, enabled);
1392 if ((mask & ~SAFETY_SUPPORTED_OPTIONS) != 0)
1395 This->safeopt = (This->safeopt & ~mask) | (mask & enabled);
1400 #undef SAFETY_SUPPORTED_OPTIONS
1402 static const IObjectSafetyVtbl ObjectSafetyVtbl = {
1403 httprequest_Safety_QueryInterface,
1404 httprequest_Safety_AddRef,
1405 httprequest_Safety_Release,
1406 httprequest_Safety_GetInterfaceSafetyOptions,
1407 httprequest_Safety_SetInterfaceSafetyOptions
1410 HRESULT XMLHTTPRequest_create(IUnknown *pUnkOuter, void **ppObj)
1415 TRACE("(%p,%p)\n", pUnkOuter, ppObj);
1417 req = heap_alloc( sizeof (*req) );
1419 return E_OUTOFMEMORY;
1421 req->IXMLHTTPRequest_iface.lpVtbl = &XMLHTTPRequestVtbl;
1422 req->IObjectWithSite_iface.lpVtbl = &ObjectWithSiteVtbl;
1423 req->IObjectSafety_iface.lpVtbl = &ObjectSafetyVtbl;
1429 req->url = req->user = req->password = NULL;
1431 req->state = READYSTATE_UNINITIALIZED;
1436 req->reqheader_size = 0;
1437 req->raw_respheaders = NULL;
1438 req->use_utf8_content = FALSE;
1440 list_init(&req->reqheaders);
1441 list_init(&req->respheaders);
1446 *ppObj = &req->IXMLHTTPRequest_iface;
1448 TRACE("returning iface %p\n", *ppObj);
1455 HRESULT XMLHTTPRequest_create(IUnknown *pUnkOuter, void **ppObj)
1457 MESSAGE("This program tried to use a XMLHTTPRequest object, but\n"
1458 "libxml2 support was not present at compile time.\n");