msxml3: Support VT_ARRAY|VT_UI1 as body type.
[wine/multimedia.git] / dlls / msxml3 / httprequest.c
blob6b77d2e670a86f1c39b35591f3c1d58f293f98a3
1 /*
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
22 #define COBJMACROS
23 #define NONAMELESSUNION
25 #include "config.h"
27 #include <stdarg.h>
28 #ifdef HAVE_LIBXML2
29 # include <libxml/parser.h>
30 # include <libxml/xmlerror.h>
31 # include <libxml/encoding.h>
32 #endif
34 #include "windef.h"
35 #include "winbase.h"
36 #include "winuser.h"
37 #include "ole2.h"
38 #include "msxml6.h"
39 #include "objsafe.h"
41 #include "msxml_private.h"
43 #include "wine/debug.h"
44 #include "wine/list.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(msxml);
48 #ifdef HAVE_LIBXML2
50 static const WCHAR MethodGetW[] = {'G','E','T',0};
51 static const WCHAR MethodPutW[] = {'P','U','T',0};
52 static const WCHAR MethodPostW[] = {'P','O','S','T',0};
54 static const WCHAR colspaceW[] = {':',' ',0};
55 static const WCHAR crlfW[] = {'\r','\n',0};
57 typedef struct BindStatusCallback BindStatusCallback;
59 struct httpheader
61 struct list entry;
62 BSTR header;
63 BSTR value;
66 typedef struct
68 IXMLHTTPRequest IXMLHTTPRequest_iface;
69 IObjectWithSite IObjectWithSite_iface;
70 IObjectSafety IObjectSafety_iface;
71 LONG ref;
73 READYSTATE state;
74 IDispatch *sink;
76 /* request */
77 BINDVERB verb;
78 BSTR url;
79 BOOL async;
80 struct list reqheaders;
81 /* cached resulting custom request headers string length in WCHARs */
82 LONG reqheader_size;
83 /* use UTF-8 content type */
84 BOOL use_utf8_content;
86 /* response headers */
87 struct list respheaders;
88 BSTR raw_respheaders;
90 /* credentials */
91 BSTR user;
92 BSTR password;
94 /* bind callback */
95 BindStatusCallback *bsc;
96 LONG status;
98 /* IObjectWithSite*/
99 IUnknown *site;
101 /* IObjectSafety */
102 DWORD safeopt;
103 } httprequest;
105 static inline httprequest *impl_from_IXMLHTTPRequest( IXMLHTTPRequest *iface )
107 return CONTAINING_RECORD(iface, httprequest, IXMLHTTPRequest_iface);
110 static inline httprequest *impl_from_IObjectWithSite(IObjectWithSite *iface)
112 return CONTAINING_RECORD(iface, httprequest, IObjectWithSite_iface);
115 static inline httprequest *impl_from_IObjectSafety(IObjectSafety *iface)
117 return CONTAINING_RECORD(iface, httprequest, IObjectSafety_iface);
120 static void httprequest_setreadystate(httprequest *This, READYSTATE state)
122 READYSTATE last = This->state;
124 This->state = state;
126 if (This->sink && last != state)
128 DISPPARAMS params;
130 memset(&params, 0, sizeof(params));
131 IDispatch_Invoke(This->sink, 0, &IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD, &params, 0, 0, 0);
135 static void free_response_headers(httprequest *This)
137 struct httpheader *header, *header2;
139 LIST_FOR_EACH_ENTRY_SAFE(header, header2, &This->respheaders, struct httpheader, entry)
141 list_remove(&header->entry);
142 SysFreeString(header->header);
143 SysFreeString(header->value);
144 heap_free(header);
147 SysFreeString(This->raw_respheaders);
148 This->raw_respheaders = NULL;
151 struct BindStatusCallback
153 IBindStatusCallback IBindStatusCallback_iface;
154 IHttpNegotiate IHttpNegotiate_iface;
155 LONG ref;
157 IBinding *binding;
158 httprequest *request;
160 /* response data */
161 IStream *stream;
163 /* request body data */
164 HGLOBAL body;
167 static inline BindStatusCallback *impl_from_IBindStatusCallback( IBindStatusCallback *iface )
169 return CONTAINING_RECORD(iface, BindStatusCallback, IBindStatusCallback_iface);
172 static inline BindStatusCallback *impl_from_IHttpNegotiate( IHttpNegotiate *iface )
174 return CONTAINING_RECORD(iface, BindStatusCallback, IHttpNegotiate_iface);
177 static void BindStatusCallback_Detach(BindStatusCallback *bsc)
179 if (bsc)
181 if (bsc->binding) IBinding_Abort(bsc->binding);
182 bsc->request = NULL;
183 IBindStatusCallback_Release(&bsc->IBindStatusCallback_iface);
187 static HRESULT WINAPI BindStatusCallback_QueryInterface(IBindStatusCallback *iface,
188 REFIID riid, void **ppv)
190 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
192 *ppv = NULL;
194 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
196 if (IsEqualGUID(&IID_IUnknown, riid) ||
197 IsEqualGUID(&IID_IBindStatusCallback, riid))
199 *ppv = &This->IBindStatusCallback_iface;
201 else if (IsEqualGUID(&IID_IHttpNegotiate, riid))
203 *ppv = &This->IHttpNegotiate_iface;
205 else if (IsEqualGUID(&IID_IServiceProvider, riid) ||
206 IsEqualGUID(&IID_IBindStatusCallbackEx, riid) ||
207 IsEqualGUID(&IID_IInternetProtocol, riid) ||
208 IsEqualGUID(&IID_IHttpNegotiate2, riid))
210 return E_NOINTERFACE;
213 if (*ppv)
215 IBindStatusCallback_AddRef(iface);
216 return S_OK;
219 FIXME("Unsupported riid = %s\n", debugstr_guid(riid));
221 return E_NOINTERFACE;
224 static ULONG WINAPI BindStatusCallback_AddRef(IBindStatusCallback *iface)
226 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
227 LONG ref = InterlockedIncrement(&This->ref);
229 TRACE("(%p) ref = %d\n", This, ref);
231 return ref;
234 static ULONG WINAPI BindStatusCallback_Release(IBindStatusCallback *iface)
236 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
237 LONG ref = InterlockedDecrement(&This->ref);
239 TRACE("(%p) ref = %d\n", This, ref);
241 if (!ref)
243 if (This->binding) IBinding_Release(This->binding);
244 if (This->stream) IStream_Release(This->stream);
245 if (This->body) GlobalFree(This->body);
246 heap_free(This);
249 return ref;
252 static HRESULT WINAPI BindStatusCallback_OnStartBinding(IBindStatusCallback *iface,
253 DWORD reserved, IBinding *pbind)
255 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
257 TRACE("(%p)->(%d %p)\n", This, reserved, pbind);
259 if (!pbind) return E_INVALIDARG;
261 This->binding = pbind;
262 IBinding_AddRef(pbind);
264 httprequest_setreadystate(This->request, READYSTATE_LOADED);
266 return CreateStreamOnHGlobal(NULL, TRUE, &This->stream);
269 static HRESULT WINAPI BindStatusCallback_GetPriority(IBindStatusCallback *iface, LONG *pPriority)
271 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
273 TRACE("(%p)->(%p)\n", This, pPriority);
275 return E_NOTIMPL;
278 static HRESULT WINAPI BindStatusCallback_OnLowResource(IBindStatusCallback *iface, DWORD reserved)
280 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
282 TRACE("(%p)->(%d)\n", This, reserved);
284 return E_NOTIMPL;
287 static HRESULT WINAPI BindStatusCallback_OnProgress(IBindStatusCallback *iface, ULONG ulProgress,
288 ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
290 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
292 TRACE("(%p)->(%u %u %u %s)\n", This, ulProgress, ulProgressMax, ulStatusCode,
293 debugstr_w(szStatusText));
295 return S_OK;
298 static HRESULT WINAPI BindStatusCallback_OnStopBinding(IBindStatusCallback *iface,
299 HRESULT hr, LPCWSTR error)
301 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
303 TRACE("(%p)->(0x%08x %s)\n", This, hr, debugstr_w(error));
305 if (This->binding)
307 IBinding_Release(This->binding);
308 This->binding = NULL;
311 if (hr == S_OK)
312 httprequest_setreadystate(This->request, READYSTATE_COMPLETE);
314 return S_OK;
317 static HRESULT WINAPI BindStatusCallback_GetBindInfo(IBindStatusCallback *iface,
318 DWORD *bind_flags, BINDINFO *pbindinfo)
320 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
322 TRACE("(%p)->(%p %p)\n", This, bind_flags, pbindinfo);
324 *bind_flags = 0;
325 if (This->request->async) *bind_flags |= BINDF_ASYNCHRONOUS;
327 if (This->request->verb != BINDVERB_GET && This->body)
329 pbindinfo->stgmedData.tymed = TYMED_HGLOBAL;
330 pbindinfo->stgmedData.u.hGlobal = This->body;
331 pbindinfo->cbstgmedData = GlobalSize(This->body);
332 /* callback owns passed body pointer */
333 IBindStatusCallback_QueryInterface(iface, &IID_IUnknown, (void**)&pbindinfo->stgmedData.pUnkForRelease);
336 pbindinfo->dwBindVerb = This->request->verb;
338 return S_OK;
341 static HRESULT WINAPI BindStatusCallback_OnDataAvailable(IBindStatusCallback *iface,
342 DWORD flags, DWORD size, FORMATETC *format, STGMEDIUM *stgmed)
344 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
345 DWORD read, written;
346 BYTE buf[4096];
347 HRESULT hr;
349 TRACE("(%p)->(%08x %d %p %p)\n", This, flags, size, format, stgmed);
353 hr = IStream_Read(stgmed->u.pstm, buf, sizeof(buf), &read);
354 if (hr != S_OK) break;
356 hr = IStream_Write(This->stream, buf, read, &written);
357 } while((hr == S_OK) && written != 0 && read != 0);
359 httprequest_setreadystate(This->request, READYSTATE_INTERACTIVE);
361 return S_OK;
364 static HRESULT WINAPI BindStatusCallback_OnObjectAvailable(IBindStatusCallback *iface,
365 REFIID riid, IUnknown *punk)
367 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
369 FIXME("(%p)->(%s %p): stub\n", This, debugstr_guid(riid), punk);
371 return E_NOTIMPL;
374 static const IBindStatusCallbackVtbl BindStatusCallbackVtbl = {
375 BindStatusCallback_QueryInterface,
376 BindStatusCallback_AddRef,
377 BindStatusCallback_Release,
378 BindStatusCallback_OnStartBinding,
379 BindStatusCallback_GetPriority,
380 BindStatusCallback_OnLowResource,
381 BindStatusCallback_OnProgress,
382 BindStatusCallback_OnStopBinding,
383 BindStatusCallback_GetBindInfo,
384 BindStatusCallback_OnDataAvailable,
385 BindStatusCallback_OnObjectAvailable
388 static HRESULT WINAPI BSCHttpNegotiate_QueryInterface(IHttpNegotiate *iface,
389 REFIID riid, void **ppv)
391 BindStatusCallback *This = impl_from_IHttpNegotiate(iface);
392 return IBindStatusCallback_QueryInterface(&This->IBindStatusCallback_iface, riid, ppv);
395 static ULONG WINAPI BSCHttpNegotiate_AddRef(IHttpNegotiate *iface)
397 BindStatusCallback *This = impl_from_IHttpNegotiate(iface);
398 return IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
401 static ULONG WINAPI BSCHttpNegotiate_Release(IHttpNegotiate *iface)
403 BindStatusCallback *This = impl_from_IHttpNegotiate(iface);
404 return IBindStatusCallback_Release(&This->IBindStatusCallback_iface);
407 static HRESULT WINAPI BSCHttpNegotiate_BeginningTransaction(IHttpNegotiate *iface,
408 LPCWSTR url, LPCWSTR headers, DWORD reserved, LPWSTR *add_headers)
410 static const WCHAR content_type_utf8W[] = {'C','o','n','t','e','n','t','-','T','y','p','e',':',' ',
411 't','e','x','t','/','p','l','a','i','n',';','c','h','a','r','s','e','t','=','u','t','f','-','8','\r','\n',0};
413 BindStatusCallback *This = impl_from_IHttpNegotiate(iface);
414 const struct httpheader *entry;
415 WCHAR *buff, *ptr;
416 int size = 0;
418 TRACE("(%p)->(%s %s %d %p)\n", This, debugstr_w(url), debugstr_w(headers), reserved, add_headers);
420 *add_headers = NULL;
422 if (This->request->use_utf8_content)
423 size = sizeof(content_type_utf8W);
425 if (!list_empty(&This->request->reqheaders))
426 size += This->request->reqheader_size*sizeof(WCHAR);
428 if (!size) return S_OK;
430 buff = CoTaskMemAlloc(size);
431 if (!buff) return E_OUTOFMEMORY;
433 ptr = buff;
434 if (This->request->use_utf8_content)
436 lstrcpyW(ptr, content_type_utf8W);
437 ptr += sizeof(content_type_utf8W)/sizeof(WCHAR)-1;
440 /* user headers */
441 LIST_FOR_EACH_ENTRY(entry, &This->request->reqheaders, struct httpheader, entry)
443 lstrcpyW(ptr, entry->header);
444 ptr += SysStringLen(entry->header);
446 lstrcpyW(ptr, colspaceW);
447 ptr += sizeof(colspaceW)/sizeof(WCHAR)-1;
449 lstrcpyW(ptr, entry->value);
450 ptr += SysStringLen(entry->value);
452 lstrcpyW(ptr, crlfW);
453 ptr += sizeof(crlfW)/sizeof(WCHAR)-1;
456 *add_headers = buff;
458 return S_OK;
461 static void add_response_header(httprequest *This, const WCHAR *data, int len)
463 struct httpheader *entry;
464 const WCHAR *ptr = data;
465 BSTR header, value;
467 while (*ptr)
469 if (*ptr == ':')
471 header = SysAllocStringLen(data, ptr-data);
472 /* skip leading spaces for a value */
473 while (*++ptr == ' ')
475 value = SysAllocStringLen(ptr, len-(ptr-data));
476 break;
478 ptr++;
481 if (!*ptr) return;
483 /* new header */
484 TRACE("got header %s:%s\n", debugstr_w(header), debugstr_w(value));
486 entry = heap_alloc(sizeof(*header));
487 entry->header = header;
488 entry->value = value;
489 list_add_head(&This->respheaders, &entry->entry);
492 static HRESULT WINAPI BSCHttpNegotiate_OnResponse(IHttpNegotiate *iface, DWORD code,
493 LPCWSTR resp_headers, LPCWSTR req_headers, LPWSTR *add_reqheaders)
495 BindStatusCallback *This = impl_from_IHttpNegotiate(iface);
497 TRACE("(%p)->(%d %s %s %p)\n", This, code, debugstr_w(resp_headers),
498 debugstr_w(req_headers), add_reqheaders);
500 This->request->status = code;
501 /* store headers */
502 free_response_headers(This->request);
503 if (resp_headers)
505 const WCHAR *ptr, *line;
507 ptr = line = resp_headers;
509 /* skip status line */
510 while (*ptr)
512 if (*ptr == '\r' && *(ptr+1) == '\n')
514 line = ++ptr+1;
515 break;
517 ptr++;
520 /* store as unparsed string for now */
521 This->request->raw_respheaders = SysAllocString(line);
524 return S_OK;
527 static const IHttpNegotiateVtbl BSCHttpNegotiateVtbl = {
528 BSCHttpNegotiate_QueryInterface,
529 BSCHttpNegotiate_AddRef,
530 BSCHttpNegotiate_Release,
531 BSCHttpNegotiate_BeginningTransaction,
532 BSCHttpNegotiate_OnResponse
535 static HRESULT BindStatusCallback_create(httprequest* This, BindStatusCallback **obj, const VARIANT *body)
537 BindStatusCallback *bsc;
538 IBindCtx *pbc;
539 HRESULT hr;
540 int size;
542 hr = CreateBindCtx(0, &pbc);
543 if (hr != S_OK) return hr;
545 bsc = heap_alloc(sizeof(*bsc));
546 if (!bsc)
548 IBindCtx_Release(pbc);
549 return E_OUTOFMEMORY;
552 bsc->IBindStatusCallback_iface.lpVtbl = &BindStatusCallbackVtbl;
553 bsc->IHttpNegotiate_iface.lpVtbl = &BSCHttpNegotiateVtbl;
554 bsc->ref = 1;
555 bsc->request = This;
556 bsc->binding = NULL;
557 bsc->stream = NULL;
558 bsc->body = NULL;
560 TRACE("(%p)->(%p)\n", This, bsc);
562 This->use_utf8_content = FALSE;
564 if (This->verb != BINDVERB_GET)
566 void *send_data, *ptr;
567 SAFEARRAY *sa = NULL;
569 if (V_VT(body) == (VT_VARIANT|VT_BYREF))
570 body = V_VARIANTREF(body);
572 switch (V_VT(body))
574 case VT_BSTR:
576 int len = SysStringLen(V_BSTR(body));
577 const WCHAR *str = V_BSTR(body);
578 UINT i, cp = CP_ACP;
580 for (i = 0; i < len; i++)
582 if (str[i] > 127)
584 cp = CP_UTF8;
585 break;
589 size = WideCharToMultiByte(cp, 0, str, len, NULL, 0, NULL, NULL);
590 if (!(ptr = heap_alloc(size)))
592 heap_free(bsc);
593 return E_OUTOFMEMORY;
595 WideCharToMultiByte(cp, 0, str, len, ptr, size, NULL, NULL);
596 if (cp == CP_UTF8) This->use_utf8_content = TRUE;
598 bsc->body = GlobalAlloc(GMEM_FIXED, size);
599 if (!bsc->body)
601 heap_free(bsc);
602 heap_free(ptr);
603 return E_OUTOFMEMORY;
606 send_data = GlobalLock(bsc->body);
607 memcpy(send_data, ptr, size);
608 GlobalUnlock(bsc->body);
609 heap_free(ptr);
610 break;
612 case VT_ARRAY|VT_UI1:
614 sa = V_ARRAY(body);
615 if ((hr = SafeArrayAccessData(sa, (void **)&ptr)) != S_OK) return hr;
616 if ((hr = SafeArrayGetUBound(sa, 1, &size) != S_OK))
618 SafeArrayUnaccessData(sa);
619 return hr;
621 size++;
622 break;
624 case VT_EMPTY:
625 ptr = NULL;
626 size = 0;
627 break;
628 default:
629 FIXME("unsupported body data type %d\n", V_VT(body));
630 break;
633 bsc->body = GlobalAlloc(GMEM_FIXED, size);
634 if (!bsc->body)
636 if (V_VT(body) == VT_BSTR)
637 heap_free(ptr);
638 else if (V_VT(body) == (VT_ARRAY|VT_UI1))
639 SafeArrayUnaccessData(sa);
641 heap_free(bsc);
642 return E_OUTOFMEMORY;
645 send_data = GlobalLock(bsc->body);
646 memcpy(send_data, ptr, size);
647 GlobalUnlock(bsc->body);
649 if (V_VT(body) == VT_BSTR)
650 heap_free(ptr);
651 else if (V_VT(body) == (VT_ARRAY|VT_UI1))
652 SafeArrayUnaccessData(sa);
655 hr = RegisterBindStatusCallback(pbc, &bsc->IBindStatusCallback_iface, NULL, 0);
656 if (hr == S_OK)
658 IMoniker *moniker;
660 hr = CreateURLMoniker(NULL, This->url, &moniker);
661 if (hr == S_OK)
663 IStream *stream;
665 hr = IMoniker_BindToStorage(moniker, pbc, NULL, &IID_IStream, (void**)&stream);
666 IMoniker_Release(moniker);
667 if (stream) IStream_Release(stream);
669 IBindCtx_Release(pbc);
672 if (FAILED(hr))
674 IBindStatusCallback_Release(&bsc->IBindStatusCallback_iface);
675 bsc = NULL;
678 *obj = bsc;
679 return hr;
682 static HRESULT WINAPI httprequest_QueryInterface(IXMLHTTPRequest *iface, REFIID riid, void **ppvObject)
684 httprequest *This = impl_from_IXMLHTTPRequest( iface );
685 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppvObject);
687 if ( IsEqualGUID( riid, &IID_IXMLHTTPRequest) ||
688 IsEqualGUID( riid, &IID_IDispatch) ||
689 IsEqualGUID( riid, &IID_IUnknown) )
691 *ppvObject = iface;
693 else if (IsEqualGUID(&IID_IObjectWithSite, riid))
695 *ppvObject = &This->IObjectWithSite_iface;
697 else if (IsEqualGUID(&IID_IObjectSafety, riid))
699 *ppvObject = &This->IObjectSafety_iface;
701 else
703 TRACE("Unsupported interface %s\n", debugstr_guid(riid));
704 *ppvObject = NULL;
705 return E_NOINTERFACE;
708 IXMLHTTPRequest_AddRef( iface );
710 return S_OK;
713 static ULONG WINAPI httprequest_AddRef(IXMLHTTPRequest *iface)
715 httprequest *This = impl_from_IXMLHTTPRequest( iface );
716 ULONG ref = InterlockedIncrement( &This->ref );
717 TRACE("(%p)->(%u)\n", This, ref );
718 return ref;
721 static ULONG WINAPI httprequest_Release(IXMLHTTPRequest *iface)
723 httprequest *This = impl_from_IXMLHTTPRequest( iface );
724 ULONG ref = InterlockedDecrement( &This->ref );
726 TRACE("(%p)->(%u)\n", This, ref );
728 if ( ref == 0 )
730 struct httpheader *header, *header2;
732 if (This->site)
733 IUnknown_Release( This->site );
735 SysFreeString(This->url);
736 SysFreeString(This->user);
737 SysFreeString(This->password);
739 /* request headers */
740 LIST_FOR_EACH_ENTRY_SAFE(header, header2, &This->reqheaders, struct httpheader, entry)
742 list_remove(&header->entry);
743 SysFreeString(header->header);
744 SysFreeString(header->value);
745 heap_free(header);
747 /* response headers */
748 free_response_headers(This);
750 /* detach callback object */
751 BindStatusCallback_Detach(This->bsc);
753 if (This->sink) IDispatch_Release(This->sink);
755 heap_free( This );
758 return ref;
761 static HRESULT WINAPI httprequest_GetTypeInfoCount(IXMLHTTPRequest *iface, UINT *pctinfo)
763 httprequest *This = impl_from_IXMLHTTPRequest( iface );
765 TRACE("(%p)->(%p)\n", This, pctinfo);
767 *pctinfo = 1;
769 return S_OK;
772 static HRESULT WINAPI httprequest_GetTypeInfo(IXMLHTTPRequest *iface, UINT iTInfo,
773 LCID lcid, ITypeInfo **ppTInfo)
775 httprequest *This = impl_from_IXMLHTTPRequest( iface );
777 TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
779 return get_typeinfo(IXMLHTTPRequest_tid, ppTInfo);
782 static HRESULT WINAPI httprequest_GetIDsOfNames(IXMLHTTPRequest *iface, REFIID riid,
783 LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
785 httprequest *This = impl_from_IXMLHTTPRequest( iface );
786 ITypeInfo *typeinfo;
787 HRESULT hr;
789 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
790 lcid, rgDispId);
792 if(!rgszNames || cNames == 0 || !rgDispId)
793 return E_INVALIDARG;
795 hr = get_typeinfo(IXMLHTTPRequest_tid, &typeinfo);
796 if(SUCCEEDED(hr))
798 hr = ITypeInfo_GetIDsOfNames(typeinfo, rgszNames, cNames, rgDispId);
799 ITypeInfo_Release(typeinfo);
802 return hr;
805 static HRESULT WINAPI httprequest_Invoke(IXMLHTTPRequest *iface, DISPID dispIdMember, REFIID riid,
806 LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult,
807 EXCEPINFO *pExcepInfo, UINT *puArgErr)
809 httprequest *This = impl_from_IXMLHTTPRequest( iface );
810 ITypeInfo *typeinfo;
811 HRESULT hr;
813 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
814 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
816 hr = get_typeinfo(IXMLHTTPRequest_tid, &typeinfo);
817 if(SUCCEEDED(hr))
819 hr = ITypeInfo_Invoke(typeinfo, &This->IXMLHTTPRequest_iface, dispIdMember, wFlags,
820 pDispParams, pVarResult, pExcepInfo, puArgErr);
821 ITypeInfo_Release(typeinfo);
824 return hr;
827 static HRESULT WINAPI httprequest_open(IXMLHTTPRequest *iface, BSTR method, BSTR url,
828 VARIANT async, VARIANT user, VARIANT password)
830 httprequest *This = impl_from_IXMLHTTPRequest( iface );
831 HRESULT hr;
832 VARIANT str, is_async;
834 TRACE("(%p)->(%s %s %s)\n", This, debugstr_w(method), debugstr_w(url),
835 debugstr_variant(&async));
837 if (!method || !url) return E_INVALIDARG;
839 /* free previously set data */
840 SysFreeString(This->url);
841 SysFreeString(This->user);
842 SysFreeString(This->password);
843 This->url = This->user = This->password = NULL;
845 if (lstrcmpiW(method, MethodGetW) == 0)
847 This->verb = BINDVERB_GET;
849 else if (lstrcmpiW(method, MethodPutW) == 0)
851 This->verb = BINDVERB_PUT;
853 else if (lstrcmpiW(method, MethodPostW) == 0)
855 This->verb = BINDVERB_POST;
857 else
859 FIXME("unsupported request type %s\n", debugstr_w(method));
860 This->verb = -1;
861 return E_FAIL;
864 This->url = SysAllocString(url);
866 VariantInit(&is_async);
867 hr = VariantChangeType(&is_async, &async, 0, VT_BOOL);
868 This->async = hr == S_OK && V_BOOL(&is_async) == VARIANT_TRUE;
870 VariantInit(&str);
871 hr = VariantChangeType(&str, &user, 0, VT_BSTR);
872 if (hr == S_OK)
873 This->user = V_BSTR(&str);
875 VariantInit(&str);
876 hr = VariantChangeType(&str, &password, 0, VT_BSTR);
877 if (hr == S_OK)
878 This->password = V_BSTR(&str);
880 httprequest_setreadystate(This, READYSTATE_LOADING);
882 return S_OK;
885 static HRESULT WINAPI httprequest_setRequestHeader(IXMLHTTPRequest *iface, BSTR header, BSTR value)
887 httprequest *This = impl_from_IXMLHTTPRequest( iface );
888 struct httpheader *entry;
890 TRACE("(%p)->(%s %s)\n", This, debugstr_w(header), debugstr_w(value));
892 if (!header || !*header) return E_INVALIDARG;
893 if (This->state != READYSTATE_LOADING) return E_FAIL;
894 if (!value) return E_INVALIDARG;
896 /* replace existing header value if already added */
897 LIST_FOR_EACH_ENTRY(entry, &This->reqheaders, struct httpheader, entry)
899 if (lstrcmpW(entry->header, header) == 0)
901 LONG length = SysStringLen(entry->value);
902 HRESULT hr;
904 hr = SysReAllocString(&entry->value, value) ? S_OK : E_OUTOFMEMORY;
906 if (hr == S_OK)
907 This->reqheader_size += (SysStringLen(entry->value) - length);
909 return hr;
913 entry = heap_alloc(sizeof(*entry));
914 if (!entry) return E_OUTOFMEMORY;
916 /* new header */
917 entry->header = SysAllocString(header);
918 entry->value = SysAllocString(value);
920 /* header length including null terminator */
921 This->reqheader_size += SysStringLen(entry->header) + sizeof(colspaceW)/sizeof(WCHAR) +
922 SysStringLen(entry->value) + sizeof(crlfW)/sizeof(WCHAR) - 1;
924 list_add_head(&This->reqheaders, &entry->entry);
926 return S_OK;
929 static HRESULT WINAPI httprequest_getResponseHeader(IXMLHTTPRequest *iface, BSTR header, BSTR *value)
931 httprequest *This = impl_from_IXMLHTTPRequest( iface );
932 struct httpheader *entry;
934 TRACE("(%p)->(%s %p)\n", This, debugstr_w(header), value);
936 if (!header || !value) return E_INVALIDARG;
938 if (This->raw_respheaders && list_empty(&This->respheaders))
940 WCHAR *ptr, *line;
942 ptr = line = This->raw_respheaders;
943 while (*ptr)
945 if (*ptr == '\r' && *(ptr+1) == '\n')
947 add_response_header(This, line, ptr-line);
948 ptr++; line = ++ptr;
949 continue;
951 ptr++;
955 LIST_FOR_EACH_ENTRY(entry, &This->respheaders, struct httpheader, entry)
957 if (!strcmpiW(entry->header, header))
959 *value = SysAllocString(entry->value);
960 TRACE("header value %s\n", debugstr_w(*value));
961 return S_OK;
965 return S_FALSE;
968 static HRESULT WINAPI httprequest_getAllResponseHeaders(IXMLHTTPRequest *iface, BSTR *respheaders)
970 httprequest *This = impl_from_IXMLHTTPRequest( iface );
972 TRACE("(%p)->(%p)\n", This, respheaders);
974 if (!respheaders) return E_INVALIDARG;
976 *respheaders = SysAllocString(This->raw_respheaders);
978 return S_OK;
981 static HRESULT WINAPI httprequest_send(IXMLHTTPRequest *iface, VARIANT body)
983 httprequest *This = impl_from_IXMLHTTPRequest( iface );
984 BindStatusCallback *bsc = NULL;
985 HRESULT hr;
987 TRACE("(%p)->(%s)\n", This, debugstr_variant(&body));
989 if (This->state != READYSTATE_LOADING) return E_FAIL;
991 hr = BindStatusCallback_create(This, &bsc, &body);
992 if (FAILED(hr)) return hr;
994 BindStatusCallback_Detach(This->bsc);
995 This->bsc = bsc;
997 return hr;
1000 static HRESULT WINAPI httprequest_abort(IXMLHTTPRequest *iface)
1002 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1004 TRACE("(%p)\n", This);
1006 BindStatusCallback_Detach(This->bsc);
1007 This->bsc = NULL;
1009 httprequest_setreadystate(This, READYSTATE_UNINITIALIZED);
1011 return S_OK;
1014 static HRESULT WINAPI httprequest_get_status(IXMLHTTPRequest *iface, LONG *status)
1016 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1018 TRACE("(%p)->(%p)\n", This, status);
1020 if (!status) return E_INVALIDARG;
1021 if (This->state != READYSTATE_COMPLETE) return E_FAIL;
1023 *status = This->status;
1025 return S_OK;
1028 static HRESULT WINAPI httprequest_get_statusText(IXMLHTTPRequest *iface, BSTR *pbstrStatus)
1030 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1032 FIXME("stub %p %p\n", This, pbstrStatus);
1034 return E_NOTIMPL;
1037 static HRESULT WINAPI httprequest_get_responseXML(IXMLHTTPRequest *iface, IDispatch **body)
1039 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1040 IXMLDOMDocument3 *doc;
1041 HRESULT hr;
1042 BSTR str;
1044 TRACE("(%p)->(%p)\n", This, body);
1046 if (!body) return E_INVALIDARG;
1047 if (This->state != READYSTATE_COMPLETE) return E_FAIL;
1049 hr = DOMDocument_create(MSXML_DEFAULT, NULL, (void**)&doc);
1050 if (hr != S_OK) return hr;
1052 hr = IXMLHTTPRequest_get_responseText(iface, &str);
1053 if (hr == S_OK)
1055 VARIANT_BOOL ok;
1057 hr = IXMLDOMDocument3_loadXML(doc, str, &ok);
1058 SysFreeString(str);
1061 IXMLDOMDocument3_QueryInterface(doc, &IID_IDispatch, (void**)body);
1062 IXMLDOMDocument3_Release(doc);
1064 return hr;
1067 static HRESULT WINAPI httprequest_get_responseText(IXMLHTTPRequest *iface, BSTR *body)
1069 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1070 HGLOBAL hglobal;
1071 HRESULT hr;
1073 TRACE("(%p)->(%p)\n", This, body);
1075 if (!body) return E_INVALIDARG;
1076 if (This->state != READYSTATE_COMPLETE) return E_FAIL;
1078 hr = GetHGlobalFromStream(This->bsc->stream, &hglobal);
1079 if (hr == S_OK)
1081 xmlChar *ptr = GlobalLock(hglobal);
1082 DWORD size = GlobalSize(hglobal);
1083 xmlCharEncoding encoding = XML_CHAR_ENCODING_UTF8;
1085 /* try to determine data encoding */
1086 if (size >= 4)
1088 encoding = xmlDetectCharEncoding(ptr, 4);
1089 TRACE("detected encoding: %s\n", debugstr_a(xmlGetCharEncodingName(encoding)));
1090 if ( encoding != XML_CHAR_ENCODING_UTF8 &&
1091 encoding != XML_CHAR_ENCODING_UTF16LE &&
1092 encoding != XML_CHAR_ENCODING_NONE )
1094 FIXME("unsupported encoding: %s\n", debugstr_a(xmlGetCharEncodingName(encoding)));
1095 GlobalUnlock(hglobal);
1096 return E_FAIL;
1100 /* without BOM assume UTF-8 */
1101 if (encoding == XML_CHAR_ENCODING_UTF8 ||
1102 encoding == XML_CHAR_ENCODING_NONE )
1104 DWORD length = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)ptr, size, NULL, 0);
1106 *body = SysAllocStringLen(NULL, length);
1107 if (*body)
1108 MultiByteToWideChar( CP_UTF8, 0, (LPCSTR)ptr, size, *body, length);
1110 else
1111 *body = SysAllocStringByteLen((LPCSTR)ptr, size);
1113 if (!*body) hr = E_OUTOFMEMORY;
1114 GlobalUnlock(hglobal);
1117 return hr;
1120 static HRESULT WINAPI httprequest_get_responseBody(IXMLHTTPRequest *iface, VARIANT *body)
1122 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1123 HGLOBAL hglobal;
1124 HRESULT hr;
1126 TRACE("(%p)->(%p)\n", This, body);
1128 if (!body) return E_INVALIDARG;
1129 V_VT(body) = VT_EMPTY;
1131 if (This->state != READYSTATE_COMPLETE) return E_PENDING;
1133 hr = GetHGlobalFromStream(This->bsc->stream, &hglobal);
1134 if (hr == S_OK)
1136 void *ptr = GlobalLock(hglobal);
1137 DWORD size = GlobalSize(hglobal);
1139 SAFEARRAYBOUND bound;
1140 SAFEARRAY *array;
1142 bound.lLbound = 0;
1143 bound.cElements = size;
1144 array = SafeArrayCreate(VT_UI1, 1, &bound);
1146 if (array)
1148 void *dest;
1150 V_VT(body) = VT_ARRAY | VT_UI1;
1151 V_ARRAY(body) = array;
1153 hr = SafeArrayAccessData(array, &dest);
1154 if (hr == S_OK)
1156 memcpy(dest, ptr, size);
1157 SafeArrayUnaccessData(array);
1159 else
1161 VariantClear(body);
1164 else
1165 hr = E_FAIL;
1167 GlobalUnlock(hglobal);
1170 return hr;
1173 static HRESULT WINAPI httprequest_get_responseStream(IXMLHTTPRequest *iface, VARIANT *body)
1175 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1176 LARGE_INTEGER move;
1177 IStream *stream;
1178 HRESULT hr;
1180 TRACE("(%p)->(%p)\n", This, body);
1182 if (!body) return E_INVALIDARG;
1183 V_VT(body) = VT_EMPTY;
1185 if (This->state != READYSTATE_COMPLETE) return E_PENDING;
1187 hr = IStream_Clone(This->bsc->stream, &stream);
1189 move.QuadPart = 0;
1190 IStream_Seek(stream, move, STREAM_SEEK_SET, NULL);
1192 V_VT(body) = VT_UNKNOWN;
1193 V_UNKNOWN(body) = (IUnknown*)stream;
1195 return hr;
1198 static HRESULT WINAPI httprequest_get_readyState(IXMLHTTPRequest *iface, LONG *state)
1200 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1202 TRACE("(%p)->(%p)\n", This, state);
1204 if (!state) return E_INVALIDARG;
1206 *state = This->state;
1207 return S_OK;
1210 static HRESULT WINAPI httprequest_put_onreadystatechange(IXMLHTTPRequest *iface, IDispatch *sink)
1212 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1214 TRACE("(%p)->(%p)\n", This, sink);
1216 if (This->sink) IDispatch_Release(This->sink);
1217 if ((This->sink = sink)) IDispatch_AddRef(This->sink);
1219 return S_OK;
1222 static const struct IXMLHTTPRequestVtbl dimimpl_vtbl =
1224 httprequest_QueryInterface,
1225 httprequest_AddRef,
1226 httprequest_Release,
1227 httprequest_GetTypeInfoCount,
1228 httprequest_GetTypeInfo,
1229 httprequest_GetIDsOfNames,
1230 httprequest_Invoke,
1231 httprequest_open,
1232 httprequest_setRequestHeader,
1233 httprequest_getResponseHeader,
1234 httprequest_getAllResponseHeaders,
1235 httprequest_send,
1236 httprequest_abort,
1237 httprequest_get_status,
1238 httprequest_get_statusText,
1239 httprequest_get_responseXML,
1240 httprequest_get_responseText,
1241 httprequest_get_responseBody,
1242 httprequest_get_responseStream,
1243 httprequest_get_readyState,
1244 httprequest_put_onreadystatechange
1247 /* IObjectWithSite */
1248 static HRESULT WINAPI
1249 httprequest_ObjectWithSite_QueryInterface( IObjectWithSite* iface, REFIID riid, void** ppvObject )
1251 httprequest *This = impl_from_IObjectWithSite(iface);
1252 return IXMLHTTPRequest_QueryInterface( (IXMLHTTPRequest *)This, riid, ppvObject );
1255 static ULONG WINAPI httprequest_ObjectWithSite_AddRef( IObjectWithSite* iface )
1257 httprequest *This = impl_from_IObjectWithSite(iface);
1258 return IXMLHTTPRequest_AddRef((IXMLHTTPRequest *)This);
1261 static ULONG WINAPI httprequest_ObjectWithSite_Release( IObjectWithSite* iface )
1263 httprequest *This = impl_from_IObjectWithSite(iface);
1264 return IXMLHTTPRequest_Release((IXMLHTTPRequest *)This);
1267 static HRESULT WINAPI httprequest_ObjectWithSite_GetSite( IObjectWithSite *iface, REFIID iid, void **ppvSite )
1269 httprequest *This = impl_from_IObjectWithSite(iface);
1271 TRACE("(%p)->(%s %p)\n", This, debugstr_guid( iid ), ppvSite );
1273 if ( !This->site )
1274 return E_FAIL;
1276 return IUnknown_QueryInterface( This->site, iid, ppvSite );
1279 static HRESULT WINAPI httprequest_ObjectWithSite_SetSite( IObjectWithSite *iface, IUnknown *punk )
1281 httprequest *This = impl_from_IObjectWithSite(iface);
1283 TRACE("(%p)->(%p)\n", iface, punk);
1285 if (punk)
1286 IUnknown_AddRef( punk );
1288 if(This->site)
1289 IUnknown_Release( This->site );
1291 This->site = punk;
1293 return S_OK;
1296 static const IObjectWithSiteVtbl httprequestObjectSite =
1298 httprequest_ObjectWithSite_QueryInterface,
1299 httprequest_ObjectWithSite_AddRef,
1300 httprequest_ObjectWithSite_Release,
1301 httprequest_ObjectWithSite_SetSite,
1302 httprequest_ObjectWithSite_GetSite
1305 /* IObjectSafety */
1306 static HRESULT WINAPI httprequest_Safety_QueryInterface(IObjectSafety *iface, REFIID riid, void **ppv)
1308 httprequest *This = impl_from_IObjectSafety(iface);
1309 return IXMLHTTPRequest_QueryInterface( (IXMLHTTPRequest *)This, riid, ppv );
1312 static ULONG WINAPI httprequest_Safety_AddRef(IObjectSafety *iface)
1314 httprequest *This = impl_from_IObjectSafety(iface);
1315 return IXMLHTTPRequest_AddRef((IXMLHTTPRequest *)This);
1318 static ULONG WINAPI httprequest_Safety_Release(IObjectSafety *iface)
1320 httprequest *This = impl_from_IObjectSafety(iface);
1321 return IXMLHTTPRequest_Release((IXMLHTTPRequest *)This);
1324 #define SAFETY_SUPPORTED_OPTIONS (INTERFACESAFE_FOR_UNTRUSTED_CALLER|INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_SECURITY_MANAGER)
1326 static HRESULT WINAPI httprequest_Safety_GetInterfaceSafetyOptions(IObjectSafety *iface, REFIID riid,
1327 DWORD *supported, DWORD *enabled)
1329 httprequest *This = impl_from_IObjectSafety(iface);
1331 TRACE("(%p)->(%s %p %p)\n", This, debugstr_guid(riid), supported, enabled);
1333 if(!supported || !enabled) return E_POINTER;
1335 *supported = SAFETY_SUPPORTED_OPTIONS;
1336 *enabled = This->safeopt;
1338 return S_OK;
1341 static HRESULT WINAPI httprequest_Safety_SetInterfaceSafetyOptions(IObjectSafety *iface, REFIID riid,
1342 DWORD mask, DWORD enabled)
1344 httprequest *This = impl_from_IObjectSafety(iface);
1345 TRACE("(%p)->(%s %x %x)\n", This, debugstr_guid(riid), mask, enabled);
1347 if ((mask & ~SAFETY_SUPPORTED_OPTIONS) != 0)
1348 return E_FAIL;
1350 This->safeopt = (This->safeopt & ~mask) | (mask & enabled);
1352 return S_OK;
1355 #undef SAFETY_SUPPORTED_OPTIONS
1357 static const IObjectSafetyVtbl httprequestObjectSafety = {
1358 httprequest_Safety_QueryInterface,
1359 httprequest_Safety_AddRef,
1360 httprequest_Safety_Release,
1361 httprequest_Safety_GetInterfaceSafetyOptions,
1362 httprequest_Safety_SetInterfaceSafetyOptions
1365 HRESULT XMLHTTPRequest_create(IUnknown *pUnkOuter, void **ppObj)
1367 httprequest *req;
1368 HRESULT hr = S_OK;
1370 TRACE("(%p,%p)\n", pUnkOuter, ppObj);
1372 req = heap_alloc( sizeof (*req) );
1373 if( !req )
1374 return E_OUTOFMEMORY;
1376 req->IXMLHTTPRequest_iface.lpVtbl = &dimimpl_vtbl;
1377 req->IObjectWithSite_iface.lpVtbl = &httprequestObjectSite;
1378 req->IObjectSafety_iface.lpVtbl = &httprequestObjectSafety;
1379 req->ref = 1;
1381 req->async = FALSE;
1382 req->verb = -1;
1383 req->url = req->user = req->password = NULL;
1385 req->state = READYSTATE_UNINITIALIZED;
1386 req->sink = NULL;
1388 req->bsc = NULL;
1389 req->status = 0;
1390 req->reqheader_size = 0;
1391 req->raw_respheaders = NULL;
1392 req->use_utf8_content = FALSE;
1394 list_init(&req->reqheaders);
1395 list_init(&req->respheaders);
1397 req->site = NULL;
1398 req->safeopt = 0;
1400 *ppObj = &req->IXMLHTTPRequest_iface;
1402 TRACE("returning iface %p\n", *ppObj);
1404 return hr;
1407 #else
1409 HRESULT XMLHTTPRequest_create(IUnknown *pUnkOuter, void **ppObj)
1411 MESSAGE("This program tried to use a XMLHTTPRequest object, but\n"
1412 "libxml2 support was not present at compile time.\n");
1413 return E_NOTIMPL;
1416 #endif