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>
47 #include "msxml_private.h"
49 #include "wine/debug.h"
50 #include "wine/list.h"
52 WINE_DEFAULT_DEBUG_CHANNEL(msxml);
56 static const WCHAR colspaceW[] = {':',' ',0};
57 static const WCHAR crlfW[] = {'\r','\n',0};
59 typedef struct BindStatusCallback BindStatusCallback;
70 IXMLHTTPRequest IXMLHTTPRequest_iface;
71 IObjectWithSite IObjectWithSite_iface;
72 IObjectSafety IObjectSafety_iface;
84 struct list reqheaders;
85 /* cached resulting custom request headers string length in WCHARs */
87 /* use UTF-8 content type */
88 BOOL use_utf8_content;
90 /* response headers */
91 struct list respheaders;
99 BindStatusCallback *bsc;
110 static inline httprequest *impl_from_IXMLHTTPRequest( IXMLHTTPRequest *iface )
112 return CONTAINING_RECORD(iface, httprequest, IXMLHTTPRequest_iface);
115 static inline httprequest *impl_from_IObjectWithSite(IObjectWithSite *iface)
117 return CONTAINING_RECORD(iface, httprequest, IObjectWithSite_iface);
120 static inline httprequest *impl_from_IObjectSafety(IObjectSafety *iface)
122 return CONTAINING_RECORD(iface, httprequest, IObjectSafety_iface);
125 static void httprequest_setreadystate(httprequest *This, READYSTATE state)
127 READYSTATE last = This->state;
131 if (This->sink && last != state)
135 memset(¶ms, 0, sizeof(params));
136 IDispatch_Invoke(This->sink, 0, &IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD, ¶ms, 0, 0, 0);
140 static void free_response_headers(httprequest *This)
142 struct httpheader *header, *header2;
144 LIST_FOR_EACH_ENTRY_SAFE(header, header2, &This->respheaders, struct httpheader, entry)
146 list_remove(&header->entry);
147 SysFreeString(header->header);
148 SysFreeString(header->value);
152 SysFreeString(This->raw_respheaders);
153 This->raw_respheaders = NULL;
156 struct BindStatusCallback
158 IBindStatusCallback IBindStatusCallback_iface;
159 IHttpNegotiate IHttpNegotiate_iface;
160 IAuthenticate IAuthenticate_iface;
164 httprequest *request;
169 /* request body data */
173 static inline BindStatusCallback *impl_from_IBindStatusCallback( IBindStatusCallback *iface )
175 return CONTAINING_RECORD(iface, BindStatusCallback, IBindStatusCallback_iface);
178 static inline BindStatusCallback *impl_from_IHttpNegotiate( IHttpNegotiate *iface )
180 return CONTAINING_RECORD(iface, BindStatusCallback, IHttpNegotiate_iface);
183 static inline BindStatusCallback *impl_from_IAuthenticate( IAuthenticate *iface )
185 return CONTAINING_RECORD(iface, BindStatusCallback, IAuthenticate_iface);
188 static void BindStatusCallback_Detach(BindStatusCallback *bsc)
192 if (bsc->binding) IBinding_Abort(bsc->binding);
194 IBindStatusCallback_Release(&bsc->IBindStatusCallback_iface);
198 static HRESULT WINAPI BindStatusCallback_QueryInterface(IBindStatusCallback *iface,
199 REFIID riid, void **ppv)
201 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
205 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
207 if (IsEqualGUID(&IID_IUnknown, riid) ||
208 IsEqualGUID(&IID_IBindStatusCallback, riid))
210 *ppv = &This->IBindStatusCallback_iface;
212 else if (IsEqualGUID(&IID_IHttpNegotiate, riid))
214 *ppv = &This->IHttpNegotiate_iface;
216 else if (IsEqualGUID(&IID_IAuthenticate, riid))
218 *ppv = &This->IAuthenticate_iface;
220 else if (IsEqualGUID(&IID_IServiceProvider, riid) ||
221 IsEqualGUID(&IID_IBindStatusCallbackEx, riid) ||
222 IsEqualGUID(&IID_IInternetProtocol, riid) ||
223 IsEqualGUID(&IID_IHttpNegotiate2, riid))
225 return E_NOINTERFACE;
230 IBindStatusCallback_AddRef(iface);
234 FIXME("Unsupported riid = %s\n", debugstr_guid(riid));
236 return E_NOINTERFACE;
239 static ULONG WINAPI BindStatusCallback_AddRef(IBindStatusCallback *iface)
241 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
242 LONG ref = InterlockedIncrement(&This->ref);
244 TRACE("(%p) ref = %d\n", This, ref);
249 static ULONG WINAPI BindStatusCallback_Release(IBindStatusCallback *iface)
251 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
252 LONG ref = InterlockedDecrement(&This->ref);
254 TRACE("(%p) ref = %d\n", This, ref);
258 if (This->binding) IBinding_Release(This->binding);
259 if (This->stream) IStream_Release(This->stream);
260 if (This->body) GlobalFree(This->body);
267 static HRESULT WINAPI BindStatusCallback_OnStartBinding(IBindStatusCallback *iface,
268 DWORD reserved, IBinding *pbind)
270 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
272 TRACE("(%p)->(%d %p)\n", This, reserved, pbind);
274 if (!pbind) return E_INVALIDARG;
276 This->binding = pbind;
277 IBinding_AddRef(pbind);
279 httprequest_setreadystate(This->request, READYSTATE_LOADED);
281 return CreateStreamOnHGlobal(NULL, TRUE, &This->stream);
284 static HRESULT WINAPI BindStatusCallback_GetPriority(IBindStatusCallback *iface, LONG *pPriority)
286 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
288 TRACE("(%p)->(%p)\n", This, pPriority);
293 static HRESULT WINAPI BindStatusCallback_OnLowResource(IBindStatusCallback *iface, DWORD reserved)
295 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
297 TRACE("(%p)->(%d)\n", This, reserved);
302 static HRESULT WINAPI BindStatusCallback_OnProgress(IBindStatusCallback *iface, ULONG ulProgress,
303 ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
305 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
307 TRACE("(%p)->(%u %u %u %s)\n", This, ulProgress, ulProgressMax, ulStatusCode,
308 debugstr_w(szStatusText));
313 static HRESULT WINAPI BindStatusCallback_OnStopBinding(IBindStatusCallback *iface,
314 HRESULT hr, LPCWSTR error)
316 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
318 TRACE("(%p)->(0x%08x %s)\n", This, hr, debugstr_w(error));
322 IBinding_Release(This->binding);
323 This->binding = NULL;
327 httprequest_setreadystate(This->request, READYSTATE_COMPLETE);
332 static HRESULT WINAPI BindStatusCallback_GetBindInfo(IBindStatusCallback *iface,
333 DWORD *bind_flags, BINDINFO *pbindinfo)
335 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
337 TRACE("(%p)->(%p %p)\n", This, bind_flags, pbindinfo);
340 if (This->request->async) *bind_flags |= BINDF_ASYNCHRONOUS;
342 if (This->request->verb != BINDVERB_GET && This->body)
344 pbindinfo->stgmedData.tymed = TYMED_HGLOBAL;
345 pbindinfo->stgmedData.u.hGlobal = This->body;
346 pbindinfo->cbstgmedData = GlobalSize(This->body);
347 /* callback owns passed body pointer */
348 IBindStatusCallback_QueryInterface(iface, &IID_IUnknown, (void**)&pbindinfo->stgmedData.pUnkForRelease);
351 pbindinfo->dwBindVerb = This->request->verb;
352 if (This->request->verb == BINDVERB_CUSTOM)
354 pbindinfo->szCustomVerb = CoTaskMemAlloc(SysStringByteLen(This->request->custom));
355 strcpyW(pbindinfo->szCustomVerb, This->request->custom);
361 static HRESULT WINAPI BindStatusCallback_OnDataAvailable(IBindStatusCallback *iface,
362 DWORD flags, DWORD size, FORMATETC *format, STGMEDIUM *stgmed)
364 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
369 TRACE("(%p)->(%08x %d %p %p)\n", This, flags, size, format, stgmed);
373 hr = IStream_Read(stgmed->u.pstm, buf, sizeof(buf), &read);
374 if (hr != S_OK) break;
376 hr = IStream_Write(This->stream, buf, read, &written);
377 } while((hr == S_OK) && written != 0 && read != 0);
379 httprequest_setreadystate(This->request, READYSTATE_INTERACTIVE);
384 static HRESULT WINAPI BindStatusCallback_OnObjectAvailable(IBindStatusCallback *iface,
385 REFIID riid, IUnknown *punk)
387 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
389 FIXME("(%p)->(%s %p): stub\n", This, debugstr_guid(riid), punk);
394 static const IBindStatusCallbackVtbl BindStatusCallbackVtbl = {
395 BindStatusCallback_QueryInterface,
396 BindStatusCallback_AddRef,
397 BindStatusCallback_Release,
398 BindStatusCallback_OnStartBinding,
399 BindStatusCallback_GetPriority,
400 BindStatusCallback_OnLowResource,
401 BindStatusCallback_OnProgress,
402 BindStatusCallback_OnStopBinding,
403 BindStatusCallback_GetBindInfo,
404 BindStatusCallback_OnDataAvailable,
405 BindStatusCallback_OnObjectAvailable
408 static HRESULT WINAPI BSCHttpNegotiate_QueryInterface(IHttpNegotiate *iface,
409 REFIID riid, void **ppv)
411 BindStatusCallback *This = impl_from_IHttpNegotiate(iface);
412 return IBindStatusCallback_QueryInterface(&This->IBindStatusCallback_iface, riid, ppv);
415 static ULONG WINAPI BSCHttpNegotiate_AddRef(IHttpNegotiate *iface)
417 BindStatusCallback *This = impl_from_IHttpNegotiate(iface);
418 return IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
421 static ULONG WINAPI BSCHttpNegotiate_Release(IHttpNegotiate *iface)
423 BindStatusCallback *This = impl_from_IHttpNegotiate(iface);
424 return IBindStatusCallback_Release(&This->IBindStatusCallback_iface);
427 static HRESULT WINAPI BSCHttpNegotiate_BeginningTransaction(IHttpNegotiate *iface,
428 LPCWSTR url, LPCWSTR headers, DWORD reserved, LPWSTR *add_headers)
430 static const WCHAR content_type_utf8W[] = {'C','o','n','t','e','n','t','-','T','y','p','e',':',' ',
431 't','e','x','t','/','p','l','a','i','n',';','c','h','a','r','s','e','t','=','u','t','f','-','8','\r','\n',0};
433 BindStatusCallback *This = impl_from_IHttpNegotiate(iface);
434 const struct httpheader *entry;
438 TRACE("(%p)->(%s %s %d %p)\n", This, debugstr_w(url), debugstr_w(headers), reserved, add_headers);
442 if (This->request->use_utf8_content)
443 size = sizeof(content_type_utf8W);
445 if (!list_empty(&This->request->reqheaders))
446 size += This->request->reqheader_size*sizeof(WCHAR);
448 if (!size) return S_OK;
450 buff = CoTaskMemAlloc(size);
451 if (!buff) return E_OUTOFMEMORY;
454 if (This->request->use_utf8_content)
456 lstrcpyW(ptr, content_type_utf8W);
457 ptr += sizeof(content_type_utf8W)/sizeof(WCHAR)-1;
461 LIST_FOR_EACH_ENTRY(entry, &This->request->reqheaders, struct httpheader, entry)
463 lstrcpyW(ptr, entry->header);
464 ptr += SysStringLen(entry->header);
466 lstrcpyW(ptr, colspaceW);
467 ptr += sizeof(colspaceW)/sizeof(WCHAR)-1;
469 lstrcpyW(ptr, entry->value);
470 ptr += SysStringLen(entry->value);
472 lstrcpyW(ptr, crlfW);
473 ptr += sizeof(crlfW)/sizeof(WCHAR)-1;
481 static void add_response_header(httprequest *This, const WCHAR *data, int len)
483 struct httpheader *entry;
484 const WCHAR *ptr = data;
491 header = SysAllocStringLen(data, ptr-data);
492 /* skip leading spaces for a value */
493 while (*++ptr == ' ')
495 value = SysAllocStringLen(ptr, len-(ptr-data));
504 TRACE("got header %s:%s\n", debugstr_w(header), debugstr_w(value));
506 entry = heap_alloc(sizeof(*entry));
507 entry->header = header;
508 entry->value = value;
509 list_add_head(&This->respheaders, &entry->entry);
512 static HRESULT WINAPI BSCHttpNegotiate_OnResponse(IHttpNegotiate *iface, DWORD code,
513 LPCWSTR resp_headers, LPCWSTR req_headers, LPWSTR *add_reqheaders)
515 BindStatusCallback *This = impl_from_IHttpNegotiate(iface);
517 TRACE("(%p)->(%d %s %s %p)\n", This, code, debugstr_w(resp_headers),
518 debugstr_w(req_headers), add_reqheaders);
520 This->request->status = code;
521 /* store headers and status text */
522 free_response_headers(This->request);
523 SysFreeString(This->request->status_text);
524 This->request->status_text = NULL;
527 const WCHAR *ptr, *line;
529 ptr = line = resp_headers;
531 /* skip status line */
534 if (*ptr == '\r' && *(ptr+1) == '\n')
536 const WCHAR *end = ptr-1;
538 /* scan back to get status phrase */
539 while (ptr > resp_headers)
543 This->request->status_text = SysAllocStringLen(ptr+1, end-ptr);
544 TRACE("status text %s\n", debugstr_w(This->request->status_text));
554 /* store as unparsed string for now */
555 This->request->raw_respheaders = SysAllocString(line);
561 static const IHttpNegotiateVtbl BSCHttpNegotiateVtbl = {
562 BSCHttpNegotiate_QueryInterface,
563 BSCHttpNegotiate_AddRef,
564 BSCHttpNegotiate_Release,
565 BSCHttpNegotiate_BeginningTransaction,
566 BSCHttpNegotiate_OnResponse
569 static HRESULT WINAPI Authenticate_QueryInterface(IAuthenticate *iface,
570 REFIID riid, void **ppv)
572 BindStatusCallback *This = impl_from_IAuthenticate(iface);
573 return IBindStatusCallback_QueryInterface(&This->IBindStatusCallback_iface, riid, ppv);
576 static ULONG WINAPI Authenticate_AddRef(IAuthenticate *iface)
578 BindStatusCallback *This = impl_from_IAuthenticate(iface);
579 return IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
582 static ULONG WINAPI Authenticate_Release(IAuthenticate *iface)
584 BindStatusCallback *This = impl_from_IAuthenticate(iface);
585 return IBindStatusCallback_Release(&This->IBindStatusCallback_iface);
588 static HRESULT WINAPI Authenticate_Authenticate(IAuthenticate *iface,
589 HWND *hwnd, LPWSTR *username, LPWSTR *password)
591 BindStatusCallback *This = impl_from_IAuthenticate(iface);
592 FIXME("(%p)->(%p %p %p)\n", This, hwnd, username, password);
596 static const IAuthenticateVtbl AuthenticateVtbl = {
597 Authenticate_QueryInterface,
599 Authenticate_Release,
600 Authenticate_Authenticate
603 static HRESULT BindStatusCallback_create(httprequest* This, BindStatusCallback **obj, const VARIANT *body)
605 BindStatusCallback *bsc;
610 hr = CreateBindCtx(0, &pbc);
611 if (hr != S_OK) return hr;
613 bsc = heap_alloc(sizeof(*bsc));
616 IBindCtx_Release(pbc);
617 return E_OUTOFMEMORY;
620 bsc->IBindStatusCallback_iface.lpVtbl = &BindStatusCallbackVtbl;
621 bsc->IHttpNegotiate_iface.lpVtbl = &BSCHttpNegotiateVtbl;
622 bsc->IAuthenticate_iface.lpVtbl = &AuthenticateVtbl;
629 TRACE("(%p)->(%p)\n", This, bsc);
631 This->use_utf8_content = FALSE;
633 if (This->verb != BINDVERB_GET)
635 void *send_data, *ptr;
636 SAFEARRAY *sa = NULL;
638 if (V_VT(body) == (VT_VARIANT|VT_BYREF))
639 body = V_VARIANTREF(body);
645 int len = SysStringLen(V_BSTR(body));
646 const WCHAR *str = V_BSTR(body);
649 for (i = 0; i < len; i++)
658 size = WideCharToMultiByte(cp, 0, str, len, NULL, 0, NULL, NULL);
659 if (!(ptr = heap_alloc(size)))
662 return E_OUTOFMEMORY;
664 WideCharToMultiByte(cp, 0, str, len, ptr, size, NULL, NULL);
665 if (cp == CP_UTF8) This->use_utf8_content = TRUE;
668 case VT_ARRAY|VT_UI1:
671 if ((hr = SafeArrayAccessData(sa, (void **)&ptr)) != S_OK) return hr;
672 if ((hr = SafeArrayGetUBound(sa, 1, &size) != S_OK))
674 SafeArrayUnaccessData(sa);
685 FIXME("unsupported body data type %d\n", V_VT(body));
689 bsc->body = GlobalAlloc(GMEM_FIXED, size);
692 if (V_VT(body) == VT_BSTR)
694 else if (V_VT(body) == (VT_ARRAY|VT_UI1))
695 SafeArrayUnaccessData(sa);
698 return E_OUTOFMEMORY;
701 send_data = GlobalLock(bsc->body);
702 memcpy(send_data, ptr, size);
703 GlobalUnlock(bsc->body);
705 if (V_VT(body) == VT_BSTR)
707 else if (V_VT(body) == (VT_ARRAY|VT_UI1))
708 SafeArrayUnaccessData(sa);
711 hr = RegisterBindStatusCallback(pbc, &bsc->IBindStatusCallback_iface, NULL, 0);
716 hr = CreateURLMoniker(NULL, This->url, &moniker);
721 hr = IMoniker_BindToStorage(moniker, pbc, NULL, &IID_IStream, (void**)&stream);
722 IMoniker_Release(moniker);
723 if (stream) IStream_Release(stream);
725 IBindCtx_Release(pbc);
730 IBindStatusCallback_Release(&bsc->IBindStatusCallback_iface);
738 static HRESULT httprequest_open(httprequest *This, BSTR method, BSTR url,
739 VARIANT async, VARIANT user, VARIANT password)
741 static const WCHAR MethodGetW[] = {'G','E','T',0};
742 static const WCHAR MethodPutW[] = {'P','U','T',0};
743 static const WCHAR MethodPostW[] = {'P','O','S','T',0};
744 static const WCHAR MethodDeleteW[] = {'D','E','L','E','T','E',0};
745 VARIANT str, is_async;
748 if (!method || !url) return E_INVALIDARG;
750 /* free previously set data */
751 SysFreeString(This->url);
752 SysFreeString(This->user);
753 SysFreeString(This->password);
754 This->url = This->user = This->password = NULL;
756 if (!strcmpiW(method, MethodGetW))
758 This->verb = BINDVERB_GET;
760 else if (!strcmpiW(method, MethodPutW))
762 This->verb = BINDVERB_PUT;
764 else if (!strcmpiW(method, MethodPostW))
766 This->verb = BINDVERB_POST;
768 else if (!strcmpiW(method, MethodDeleteW))
770 This->verb = BINDVERB_CUSTOM;
771 SysReAllocString(&This->custom, method);
775 FIXME("unsupported request type %s\n", debugstr_w(method));
780 /* try to combine with site url */
781 if (This->siteurl && PathIsRelativeW(url))
783 DWORD len = INTERNET_MAX_URL_LENGTH;
784 WCHAR *fullW = heap_alloc(len*sizeof(WCHAR));
786 hr = UrlCombineW(This->siteurl, url, fullW, &len, 0);
789 TRACE("combined url %s\n", debugstr_w(fullW));
790 This->url = SysAllocString(fullW);
795 This->url = SysAllocString(url);
797 VariantInit(&is_async);
798 hr = VariantChangeType(&is_async, &async, 0, VT_BOOL);
799 This->async = hr == S_OK && V_BOOL(&is_async) == VARIANT_TRUE;
802 hr = VariantChangeType(&str, &user, 0, VT_BSTR);
804 This->user = V_BSTR(&str);
807 hr = VariantChangeType(&str, &password, 0, VT_BSTR);
809 This->password = V_BSTR(&str);
811 httprequest_setreadystate(This, READYSTATE_LOADING);
816 static HRESULT httprequest_setRequestHeader(httprequest *This, BSTR header, BSTR value)
818 struct httpheader *entry;
820 if (!header || !*header) return E_INVALIDARG;
821 if (This->state != READYSTATE_LOADING) return E_FAIL;
822 if (!value) return E_INVALIDARG;
824 /* replace existing header value if already added */
825 LIST_FOR_EACH_ENTRY(entry, &This->reqheaders, struct httpheader, entry)
827 if (lstrcmpW(entry->header, header) == 0)
829 LONG length = SysStringLen(entry->value);
832 hr = SysReAllocString(&entry->value, value) ? S_OK : E_OUTOFMEMORY;
835 This->reqheader_size += (SysStringLen(entry->value) - length);
841 entry = heap_alloc(sizeof(*entry));
842 if (!entry) return E_OUTOFMEMORY;
845 entry->header = SysAllocString(header);
846 entry->value = SysAllocString(value);
848 /* header length including null terminator */
849 This->reqheader_size += SysStringLen(entry->header) + sizeof(colspaceW)/sizeof(WCHAR) +
850 SysStringLen(entry->value) + sizeof(crlfW)/sizeof(WCHAR) - 1;
852 list_add_head(&This->reqheaders, &entry->entry);
857 static HRESULT httprequest_getResponseHeader(httprequest *This, BSTR header, BSTR *value)
859 struct httpheader *entry;
861 if (!header || !value) return E_INVALIDARG;
863 if (This->raw_respheaders && list_empty(&This->respheaders))
867 ptr = line = This->raw_respheaders;
870 if (*ptr == '\r' && *(ptr+1) == '\n')
872 add_response_header(This, line, ptr-line);
880 LIST_FOR_EACH_ENTRY(entry, &This->respheaders, struct httpheader, entry)
882 if (!strcmpiW(entry->header, header))
884 *value = SysAllocString(entry->value);
885 TRACE("header value %s\n", debugstr_w(*value));
893 static HRESULT httprequest_getAllResponseHeaders(httprequest *This, BSTR *respheaders)
895 if (!respheaders) return E_INVALIDARG;
897 *respheaders = SysAllocString(This->raw_respheaders);
902 static HRESULT httprequest_send(httprequest *This, VARIANT body)
904 BindStatusCallback *bsc = NULL;
907 if (This->state != READYSTATE_LOADING) return E_FAIL;
909 hr = BindStatusCallback_create(This, &bsc, &body);
910 if (FAILED(hr)) return hr;
912 BindStatusCallback_Detach(This->bsc);
918 static HRESULT httprequest_abort(httprequest *This)
920 BindStatusCallback_Detach(This->bsc);
923 httprequest_setreadystate(This, READYSTATE_UNINITIALIZED);
928 static HRESULT httprequest_get_status(httprequest *This, LONG *status)
930 if (!status) return E_INVALIDARG;
931 if (This->state != READYSTATE_COMPLETE) return E_FAIL;
933 *status = This->status;
938 static HRESULT httprequest_get_statusText(httprequest *This, BSTR *status)
940 if (!status) return E_INVALIDARG;
941 if (This->state != READYSTATE_COMPLETE) return E_FAIL;
943 *status = SysAllocString(This->status_text);
948 static HRESULT httprequest_get_responseText(httprequest *This, BSTR *body)
953 if (!body) return E_INVALIDARG;
954 if (This->state != READYSTATE_COMPLETE) return E_FAIL;
956 hr = GetHGlobalFromStream(This->bsc->stream, &hglobal);
959 xmlChar *ptr = GlobalLock(hglobal);
960 DWORD size = GlobalSize(hglobal);
961 xmlCharEncoding encoding = XML_CHAR_ENCODING_UTF8;
963 /* try to determine data encoding */
966 encoding = xmlDetectCharEncoding(ptr, 4);
967 TRACE("detected encoding: %s\n", debugstr_a(xmlGetCharEncodingName(encoding)));
968 if ( encoding != XML_CHAR_ENCODING_UTF8 &&
969 encoding != XML_CHAR_ENCODING_UTF16LE &&
970 encoding != XML_CHAR_ENCODING_NONE )
972 FIXME("unsupported encoding: %s\n", debugstr_a(xmlGetCharEncodingName(encoding)));
973 GlobalUnlock(hglobal);
978 /* without BOM assume UTF-8 */
979 if (encoding == XML_CHAR_ENCODING_UTF8 ||
980 encoding == XML_CHAR_ENCODING_NONE )
982 DWORD length = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)ptr, size, NULL, 0);
984 *body = SysAllocStringLen(NULL, length);
986 MultiByteToWideChar( CP_UTF8, 0, (LPCSTR)ptr, size, *body, length);
989 *body = SysAllocStringByteLen((LPCSTR)ptr, size);
991 if (!*body) hr = E_OUTOFMEMORY;
992 GlobalUnlock(hglobal);
998 static HRESULT httprequest_get_responseXML(httprequest *This, IDispatch **body)
1000 IXMLDOMDocument3 *doc;
1004 if (!body) return E_INVALIDARG;
1005 if (This->state != READYSTATE_COMPLETE) return E_FAIL;
1007 hr = DOMDocument_create(MSXML_DEFAULT, NULL, (void**)&doc);
1008 if (hr != S_OK) return hr;
1010 hr = httprequest_get_responseText(This, &str);
1015 hr = IXMLDOMDocument3_loadXML(doc, str, &ok);
1019 IXMLDOMDocument3_QueryInterface(doc, &IID_IDispatch, (void**)body);
1020 IXMLDOMDocument3_Release(doc);
1025 static HRESULT httprequest_get_responseBody(httprequest *This, VARIANT *body)
1030 if (!body) return E_INVALIDARG;
1031 V_VT(body) = VT_EMPTY;
1033 if (This->state != READYSTATE_COMPLETE) return E_PENDING;
1035 hr = GetHGlobalFromStream(This->bsc->stream, &hglobal);
1038 void *ptr = GlobalLock(hglobal);
1039 DWORD size = GlobalSize(hglobal);
1041 SAFEARRAYBOUND bound;
1045 bound.cElements = size;
1046 array = SafeArrayCreate(VT_UI1, 1, &bound);
1052 V_VT(body) = VT_ARRAY | VT_UI1;
1053 V_ARRAY(body) = array;
1055 hr = SafeArrayAccessData(array, &dest);
1058 memcpy(dest, ptr, size);
1059 SafeArrayUnaccessData(array);
1069 GlobalUnlock(hglobal);
1075 static HRESULT httprequest_get_responseStream(httprequest *This, VARIANT *body)
1081 if (!body) return E_INVALIDARG;
1082 V_VT(body) = VT_EMPTY;
1084 if (This->state != READYSTATE_COMPLETE) return E_PENDING;
1086 hr = IStream_Clone(This->bsc->stream, &stream);
1089 IStream_Seek(stream, move, STREAM_SEEK_SET, NULL);
1091 V_VT(body) = VT_UNKNOWN;
1092 V_UNKNOWN(body) = (IUnknown*)stream;
1097 static HRESULT httprequest_get_readyState(httprequest *This, LONG *state)
1099 if (!state) return E_INVALIDARG;
1101 *state = This->state;
1105 static HRESULT httprequest_put_onreadystatechange(httprequest *This, IDispatch *sink)
1107 if (This->sink) IDispatch_Release(This->sink);
1108 if ((This->sink = sink)) IDispatch_AddRef(This->sink);
1113 static void httprequest_release(httprequest *This)
1115 struct httpheader *header, *header2;
1118 IUnknown_Release( This->site );
1120 SysFreeString(This->custom);
1121 SysFreeString(This->siteurl);
1122 SysFreeString(This->url);
1123 SysFreeString(This->user);
1124 SysFreeString(This->password);
1126 /* request headers */
1127 LIST_FOR_EACH_ENTRY_SAFE(header, header2, &This->reqheaders, struct httpheader, entry)
1129 list_remove(&header->entry);
1130 SysFreeString(header->header);
1131 SysFreeString(header->value);
1134 /* response headers */
1135 free_response_headers(This);
1136 SysFreeString(This->status_text);
1138 /* detach callback object */
1139 BindStatusCallback_Detach(This->bsc);
1141 if (This->sink) IDispatch_Release(This->sink);
1144 static HRESULT WINAPI XMLHTTPRequest_QueryInterface(IXMLHTTPRequest *iface, REFIID riid, void **ppvObject)
1146 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1147 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppvObject);
1149 if ( IsEqualGUID( riid, &IID_IXMLHTTPRequest) ||
1150 IsEqualGUID( riid, &IID_IDispatch) ||
1151 IsEqualGUID( riid, &IID_IUnknown) )
1155 else if (IsEqualGUID(&IID_IObjectWithSite, riid))
1157 *ppvObject = &This->IObjectWithSite_iface;
1159 else if (IsEqualGUID(&IID_IObjectSafety, riid))
1161 *ppvObject = &This->IObjectSafety_iface;
1165 TRACE("Unsupported interface %s\n", debugstr_guid(riid));
1167 return E_NOINTERFACE;
1170 IXMLHTTPRequest_AddRef( iface );
1175 static ULONG WINAPI XMLHTTPRequest_AddRef(IXMLHTTPRequest *iface)
1177 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1178 ULONG ref = InterlockedIncrement( &This->ref );
1179 TRACE("(%p)->(%u)\n", This, ref );
1183 static ULONG WINAPI XMLHTTPRequest_Release(IXMLHTTPRequest *iface)
1185 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1186 ULONG ref = InterlockedDecrement( &This->ref );
1188 TRACE("(%p)->(%u)\n", This, ref );
1192 httprequest_release( This );
1199 static HRESULT WINAPI XMLHTTPRequest_GetTypeInfoCount(IXMLHTTPRequest *iface, UINT *pctinfo)
1201 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1203 TRACE("(%p)->(%p)\n", This, pctinfo);
1210 static HRESULT WINAPI XMLHTTPRequest_GetTypeInfo(IXMLHTTPRequest *iface, UINT iTInfo,
1211 LCID lcid, ITypeInfo **ppTInfo)
1213 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1215 TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
1217 return get_typeinfo(IXMLHTTPRequest_tid, ppTInfo);
1220 static HRESULT WINAPI XMLHTTPRequest_GetIDsOfNames(IXMLHTTPRequest *iface, REFIID riid,
1221 LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
1223 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1224 ITypeInfo *typeinfo;
1227 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
1230 if(!rgszNames || cNames == 0 || !rgDispId)
1231 return E_INVALIDARG;
1233 hr = get_typeinfo(IXMLHTTPRequest_tid, &typeinfo);
1236 hr = ITypeInfo_GetIDsOfNames(typeinfo, rgszNames, cNames, rgDispId);
1237 ITypeInfo_Release(typeinfo);
1243 static HRESULT WINAPI XMLHTTPRequest_Invoke(IXMLHTTPRequest *iface, DISPID dispIdMember, REFIID riid,
1244 LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult,
1245 EXCEPINFO *pExcepInfo, UINT *puArgErr)
1247 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1248 ITypeInfo *typeinfo;
1251 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
1252 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
1254 hr = get_typeinfo(IXMLHTTPRequest_tid, &typeinfo);
1257 hr = ITypeInfo_Invoke(typeinfo, &This->IXMLHTTPRequest_iface, dispIdMember, wFlags,
1258 pDispParams, pVarResult, pExcepInfo, puArgErr);
1259 ITypeInfo_Release(typeinfo);
1265 static HRESULT WINAPI XMLHTTPRequest_open(IXMLHTTPRequest *iface, BSTR method, BSTR url,
1266 VARIANT async, VARIANT user, VARIANT password)
1268 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1269 TRACE("(%p)->(%s %s %s)\n", This, debugstr_w(method), debugstr_w(url),
1270 debugstr_variant(&async));
1271 return httprequest_open(This, method, url, async, user, password);
1274 static HRESULT WINAPI XMLHTTPRequest_setRequestHeader(IXMLHTTPRequest *iface, BSTR header, BSTR value)
1276 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1277 TRACE("(%p)->(%s %s)\n", This, debugstr_w(header), debugstr_w(value));
1278 return httprequest_setRequestHeader(This, header, value);
1281 static HRESULT WINAPI XMLHTTPRequest_getResponseHeader(IXMLHTTPRequest *iface, BSTR header, BSTR *value)
1283 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1284 TRACE("(%p)->(%s %p)\n", This, debugstr_w(header), value);
1285 return httprequest_getResponseHeader(This, header, value);
1288 static HRESULT WINAPI XMLHTTPRequest_getAllResponseHeaders(IXMLHTTPRequest *iface, BSTR *respheaders)
1290 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1291 TRACE("(%p)->(%p)\n", This, respheaders);
1292 return httprequest_getAllResponseHeaders(This, respheaders);
1295 static HRESULT WINAPI XMLHTTPRequest_send(IXMLHTTPRequest *iface, VARIANT body)
1297 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1298 TRACE("(%p)->(%s)\n", This, debugstr_variant(&body));
1299 return httprequest_send(This, body);
1302 static HRESULT WINAPI XMLHTTPRequest_abort(IXMLHTTPRequest *iface)
1304 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1305 TRACE("(%p)\n", This);
1306 return httprequest_abort(This);
1309 static HRESULT WINAPI XMLHTTPRequest_get_status(IXMLHTTPRequest *iface, LONG *status)
1311 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1312 TRACE("(%p)->(%p)\n", This, status);
1313 return httprequest_get_status(This, status);
1316 static HRESULT WINAPI XMLHTTPRequest_get_statusText(IXMLHTTPRequest *iface, BSTR *status)
1318 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1319 TRACE("(%p)->(%p)\n", This, status);
1320 return httprequest_get_statusText(This, status);
1323 static HRESULT WINAPI XMLHTTPRequest_get_responseXML(IXMLHTTPRequest *iface, IDispatch **body)
1325 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1326 TRACE("(%p)->(%p)\n", This, body);
1327 return httprequest_get_responseXML(This, body);
1330 static HRESULT WINAPI XMLHTTPRequest_get_responseText(IXMLHTTPRequest *iface, BSTR *body)
1332 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1333 TRACE("(%p)->(%p)\n", This, body);
1334 return httprequest_get_responseText(This, body);
1337 static HRESULT WINAPI XMLHTTPRequest_get_responseBody(IXMLHTTPRequest *iface, VARIANT *body)
1339 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1340 TRACE("(%p)->(%p)\n", This, body);
1341 return httprequest_get_responseBody(This, body);
1344 static HRESULT WINAPI XMLHTTPRequest_get_responseStream(IXMLHTTPRequest *iface, VARIANT *body)
1346 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1347 TRACE("(%p)->(%p)\n", This, body);
1348 return httprequest_get_responseStream(This, body);
1351 static HRESULT WINAPI XMLHTTPRequest_get_readyState(IXMLHTTPRequest *iface, LONG *state)
1353 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1354 TRACE("(%p)->(%p)\n", This, state);
1355 return httprequest_get_readyState(This, state);
1358 static HRESULT WINAPI XMLHTTPRequest_put_onreadystatechange(IXMLHTTPRequest *iface, IDispatch *sink)
1360 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1361 TRACE("(%p)->(%p)\n", This, sink);
1362 return httprequest_put_onreadystatechange(This, sink);
1365 static const struct IXMLHTTPRequestVtbl XMLHTTPRequestVtbl =
1367 XMLHTTPRequest_QueryInterface,
1368 XMLHTTPRequest_AddRef,
1369 XMLHTTPRequest_Release,
1370 XMLHTTPRequest_GetTypeInfoCount,
1371 XMLHTTPRequest_GetTypeInfo,
1372 XMLHTTPRequest_GetIDsOfNames,
1373 XMLHTTPRequest_Invoke,
1374 XMLHTTPRequest_open,
1375 XMLHTTPRequest_setRequestHeader,
1376 XMLHTTPRequest_getResponseHeader,
1377 XMLHTTPRequest_getAllResponseHeaders,
1378 XMLHTTPRequest_send,
1379 XMLHTTPRequest_abort,
1380 XMLHTTPRequest_get_status,
1381 XMLHTTPRequest_get_statusText,
1382 XMLHTTPRequest_get_responseXML,
1383 XMLHTTPRequest_get_responseText,
1384 XMLHTTPRequest_get_responseBody,
1385 XMLHTTPRequest_get_responseStream,
1386 XMLHTTPRequest_get_readyState,
1387 XMLHTTPRequest_put_onreadystatechange
1390 /* IObjectWithSite */
1391 static HRESULT WINAPI
1392 httprequest_ObjectWithSite_QueryInterface( IObjectWithSite* iface, REFIID riid, void** ppvObject )
1394 httprequest *This = impl_from_IObjectWithSite(iface);
1395 return IXMLHTTPRequest_QueryInterface( (IXMLHTTPRequest *)This, riid, ppvObject );
1398 static ULONG WINAPI httprequest_ObjectWithSite_AddRef( IObjectWithSite* iface )
1400 httprequest *This = impl_from_IObjectWithSite(iface);
1401 return IXMLHTTPRequest_AddRef((IXMLHTTPRequest *)This);
1404 static ULONG WINAPI httprequest_ObjectWithSite_Release( IObjectWithSite* iface )
1406 httprequest *This = impl_from_IObjectWithSite(iface);
1407 return IXMLHTTPRequest_Release((IXMLHTTPRequest *)This);
1410 static HRESULT WINAPI httprequest_ObjectWithSite_GetSite( IObjectWithSite *iface, REFIID iid, void **ppvSite )
1412 httprequest *This = impl_from_IObjectWithSite(iface);
1414 TRACE("(%p)->(%s %p)\n", This, debugstr_guid( iid ), ppvSite );
1419 return IUnknown_QueryInterface( This->site, iid, ppvSite );
1422 static HRESULT WINAPI httprequest_ObjectWithSite_SetSite( IObjectWithSite *iface, IUnknown *punk )
1424 httprequest *This = impl_from_IObjectWithSite(iface);
1425 IServiceProvider *provider;
1428 TRACE("(%p)->(%p)\n", iface, punk);
1431 IUnknown_AddRef( punk );
1434 IUnknown_Release( This->site );
1438 hr = IUnknown_QueryInterface(This->site, &IID_IServiceProvider, (void**)&provider);
1441 IHTMLDocument2 *doc;
1443 hr = IServiceProvider_QueryService(provider, &SID_SContainerDispatch, &IID_IHTMLDocument2, (void**)&doc);
1446 SysFreeString(This->siteurl);
1448 hr = IHTMLDocument2_get_URL(doc, &This->siteurl);
1449 IHTMLDocument2_Release(doc);
1450 TRACE("host url %s, 0x%08x\n", debugstr_w(This->siteurl), hr);
1452 IServiceProvider_Release(provider);
1458 static const IObjectWithSiteVtbl ObjectWithSiteVtbl =
1460 httprequest_ObjectWithSite_QueryInterface,
1461 httprequest_ObjectWithSite_AddRef,
1462 httprequest_ObjectWithSite_Release,
1463 httprequest_ObjectWithSite_SetSite,
1464 httprequest_ObjectWithSite_GetSite
1468 static HRESULT WINAPI httprequest_Safety_QueryInterface(IObjectSafety *iface, REFIID riid, void **ppv)
1470 httprequest *This = impl_from_IObjectSafety(iface);
1471 return IXMLHTTPRequest_QueryInterface( (IXMLHTTPRequest *)This, riid, ppv );
1474 static ULONG WINAPI httprequest_Safety_AddRef(IObjectSafety *iface)
1476 httprequest *This = impl_from_IObjectSafety(iface);
1477 return IXMLHTTPRequest_AddRef((IXMLHTTPRequest *)This);
1480 static ULONG WINAPI httprequest_Safety_Release(IObjectSafety *iface)
1482 httprequest *This = impl_from_IObjectSafety(iface);
1483 return IXMLHTTPRequest_Release((IXMLHTTPRequest *)This);
1486 #define SAFETY_SUPPORTED_OPTIONS (INTERFACESAFE_FOR_UNTRUSTED_CALLER|INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_SECURITY_MANAGER)
1488 static HRESULT WINAPI httprequest_Safety_GetInterfaceSafetyOptions(IObjectSafety *iface, REFIID riid,
1489 DWORD *supported, DWORD *enabled)
1491 httprequest *This = impl_from_IObjectSafety(iface);
1493 TRACE("(%p)->(%s %p %p)\n", This, debugstr_guid(riid), supported, enabled);
1495 if(!supported || !enabled) return E_POINTER;
1497 *supported = SAFETY_SUPPORTED_OPTIONS;
1498 *enabled = This->safeopt;
1503 static HRESULT WINAPI httprequest_Safety_SetInterfaceSafetyOptions(IObjectSafety *iface, REFIID riid,
1504 DWORD mask, DWORD enabled)
1506 httprequest *This = impl_from_IObjectSafety(iface);
1507 TRACE("(%p)->(%s %x %x)\n", This, debugstr_guid(riid), mask, enabled);
1509 if ((mask & ~SAFETY_SUPPORTED_OPTIONS) != 0)
1512 This->safeopt = (This->safeopt & ~mask) | (mask & enabled);
1517 #undef SAFETY_SUPPORTED_OPTIONS
1519 static const IObjectSafetyVtbl ObjectSafetyVtbl = {
1520 httprequest_Safety_QueryInterface,
1521 httprequest_Safety_AddRef,
1522 httprequest_Safety_Release,
1523 httprequest_Safety_GetInterfaceSafetyOptions,
1524 httprequest_Safety_SetInterfaceSafetyOptions
1527 static void init_httprequest(httprequest *req)
1529 req->IXMLHTTPRequest_iface.lpVtbl = &XMLHTTPRequestVtbl;
1530 req->IObjectWithSite_iface.lpVtbl = &ObjectWithSiteVtbl;
1531 req->IObjectSafety_iface.lpVtbl = &ObjectSafetyVtbl;
1537 req->url = req->siteurl = req->user = req->password = NULL;
1539 req->state = READYSTATE_UNINITIALIZED;
1544 req->status_text = NULL;
1545 req->reqheader_size = 0;
1546 req->raw_respheaders = NULL;
1547 req->use_utf8_content = FALSE;
1549 list_init(&req->reqheaders);
1550 list_init(&req->respheaders);
1556 HRESULT XMLHTTPRequest_create(IUnknown *outer, void **obj)
1560 TRACE("(%p, %p)\n", outer, obj);
1562 req = heap_alloc( sizeof (*req) );
1564 return E_OUTOFMEMORY;
1566 init_httprequest(req);
1567 *obj = &req->IXMLHTTPRequest_iface;
1569 TRACE("returning iface %p\n", *obj);
1576 HRESULT XMLHTTPRequest_create(IUnknown *pUnkOuter, void **ppObj)
1578 MESSAGE("This program tried to use a XMLHTTPRequest object, but\n"
1579 "libxml2 support was not present at compile time.\n");