gdiplus/tests: Accept empty metafiles having 0 size on Windows.
[wine.git] / dlls / msxml3 / httprequest.c
blobe0f08eaa3ad5df37ac69f0fff5905eb458722543
1 /*
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
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 "wingdi.h"
37 #include "wininet.h"
38 #include "winreg.h"
39 #include "winuser.h"
40 #include "ole2.h"
41 #include "mshtml.h"
42 #include "msxml6.h"
43 #include "objsafe.h"
44 #include "docobj.h"
45 #include "shlwapi.h"
47 #include "msxml_private.h"
49 #include "wine/debug.h"
51 #ifdef HAVE_LIBXML2
53 WINE_DEFAULT_DEBUG_CHANNEL(msxml);
55 static const WCHAR colspaceW[] = {':',' ',0};
56 static const WCHAR crlfW[] = {'\r','\n',0};
57 static const DWORD safety_supported_options =
58 INTERFACESAFE_FOR_UNTRUSTED_CALLER |
59 INTERFACESAFE_FOR_UNTRUSTED_DATA |
60 INTERFACE_USES_SECURITY_MANAGER;
62 typedef struct BindStatusCallback BindStatusCallback;
64 struct httpheader
66 struct list entry;
67 BSTR header;
68 BSTR value;
71 typedef struct
73 IXMLHTTPRequest IXMLHTTPRequest_iface;
74 IObjectWithSite IObjectWithSite_iface;
75 IObjectSafety IObjectSafety_iface;
76 LONG ref;
78 READYSTATE state;
79 IDispatch *sink;
81 /* request */
82 BINDVERB verb;
83 BSTR custom;
84 IUri *uri;
85 IUri *base_uri;
86 BOOL async;
87 struct list reqheaders;
88 /* cached resulting custom request headers string length in WCHARs */
89 LONG reqheader_size;
90 /* use UTF-8 content type */
91 BOOL use_utf8_content;
93 /* response headers */
94 struct list respheaders;
95 BSTR raw_respheaders;
97 /* credentials */
98 BSTR user;
99 BSTR password;
101 /* bind callback */
102 BindStatusCallback *bsc;
103 LONG status;
104 BSTR status_text;
106 /* IObjectWithSite*/
107 IUnknown *site;
109 /* IObjectSafety */
110 DWORD safeopt;
111 } httprequest;
113 typedef struct
115 httprequest req;
116 IServerXMLHTTPRequest IServerXMLHTTPRequest_iface;
117 LONG ref;
118 } serverhttp;
120 static inline httprequest *impl_from_IXMLHTTPRequest( IXMLHTTPRequest *iface )
122 return CONTAINING_RECORD(iface, httprequest, IXMLHTTPRequest_iface);
125 static inline httprequest *impl_from_IObjectWithSite(IObjectWithSite *iface)
127 return CONTAINING_RECORD(iface, httprequest, IObjectWithSite_iface);
130 static inline httprequest *impl_from_IObjectSafety(IObjectSafety *iface)
132 return CONTAINING_RECORD(iface, httprequest, IObjectSafety_iface);
135 static inline serverhttp *impl_from_IServerXMLHTTPRequest(IServerXMLHTTPRequest *iface)
137 return CONTAINING_RECORD(iface, serverhttp, IServerXMLHTTPRequest_iface);
140 static void httprequest_setreadystate(httprequest *This, READYSTATE state)
142 READYSTATE last = This->state;
143 static const char* readystates[] = {
144 "READYSTATE_UNINITIALIZED",
145 "READYSTATE_LOADING",
146 "READYSTATE_LOADED",
147 "READYSTATE_INTERACTIVE",
148 "READYSTATE_COMPLETE"};
150 This->state = state;
152 TRACE("state %s\n", readystates[state]);
154 if (This->sink && last != state)
156 DISPPARAMS params;
158 memset(&params, 0, sizeof(params));
159 IDispatch_Invoke(This->sink, 0, &IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD, &params, 0, 0, 0);
163 static void free_response_headers(httprequest *This)
165 struct httpheader *header, *header2;
167 LIST_FOR_EACH_ENTRY_SAFE(header, header2, &This->respheaders, struct httpheader, entry)
169 list_remove(&header->entry);
170 SysFreeString(header->header);
171 SysFreeString(header->value);
172 heap_free(header);
175 SysFreeString(This->raw_respheaders);
176 This->raw_respheaders = NULL;
179 static void free_request_headers(httprequest *This)
181 struct httpheader *header, *header2;
183 LIST_FOR_EACH_ENTRY_SAFE(header, header2, &This->reqheaders, struct httpheader, entry)
185 list_remove(&header->entry);
186 SysFreeString(header->header);
187 SysFreeString(header->value);
188 heap_free(header);
192 struct BindStatusCallback
194 IBindStatusCallback IBindStatusCallback_iface;
195 IHttpNegotiate IHttpNegotiate_iface;
196 IAuthenticate IAuthenticate_iface;
197 LONG ref;
199 IBinding *binding;
200 httprequest *request;
202 /* response data */
203 IStream *stream;
205 /* request body data */
206 HGLOBAL body;
209 static inline BindStatusCallback *impl_from_IBindStatusCallback( IBindStatusCallback *iface )
211 return CONTAINING_RECORD(iface, BindStatusCallback, IBindStatusCallback_iface);
214 static inline BindStatusCallback *impl_from_IHttpNegotiate( IHttpNegotiate *iface )
216 return CONTAINING_RECORD(iface, BindStatusCallback, IHttpNegotiate_iface);
219 static inline BindStatusCallback *impl_from_IAuthenticate( IAuthenticate *iface )
221 return CONTAINING_RECORD(iface, BindStatusCallback, IAuthenticate_iface);
224 static void BindStatusCallback_Detach(BindStatusCallback *bsc)
226 if (bsc)
228 if (bsc->binding) IBinding_Abort(bsc->binding);
229 bsc->request->bsc = NULL;
230 bsc->request = NULL;
231 IBindStatusCallback_Release(&bsc->IBindStatusCallback_iface);
235 static HRESULT WINAPI BindStatusCallback_QueryInterface(IBindStatusCallback *iface,
236 REFIID riid, void **ppv)
238 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
240 *ppv = NULL;
242 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
244 if (IsEqualGUID(&IID_IUnknown, riid) ||
245 IsEqualGUID(&IID_IBindStatusCallback, riid))
247 *ppv = &This->IBindStatusCallback_iface;
249 else if (IsEqualGUID(&IID_IHttpNegotiate, riid))
251 *ppv = &This->IHttpNegotiate_iface;
253 else if (IsEqualGUID(&IID_IAuthenticate, riid))
255 *ppv = &This->IAuthenticate_iface;
257 else if (IsEqualGUID(&IID_IServiceProvider, riid) ||
258 IsEqualGUID(&IID_IBindStatusCallbackEx, riid) ||
259 IsEqualGUID(&IID_IInternetProtocol, riid) ||
260 IsEqualGUID(&IID_IHttpNegotiate2, riid))
262 return E_NOINTERFACE;
265 if (*ppv)
267 IBindStatusCallback_AddRef(iface);
268 return S_OK;
271 FIXME("Unsupported riid = %s\n", debugstr_guid(riid));
273 return E_NOINTERFACE;
276 static ULONG WINAPI BindStatusCallback_AddRef(IBindStatusCallback *iface)
278 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
279 LONG ref = InterlockedIncrement(&This->ref);
281 TRACE("(%p) ref = %d\n", This, ref);
283 return ref;
286 static ULONG WINAPI BindStatusCallback_Release(IBindStatusCallback *iface)
288 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
289 LONG ref = InterlockedDecrement(&This->ref);
291 TRACE("(%p) ref = %d\n", This, ref);
293 if (!ref)
295 if (This->binding) IBinding_Release(This->binding);
296 if (This->stream) IStream_Release(This->stream);
297 if (This->body) GlobalFree(This->body);
298 heap_free(This);
301 return ref;
304 static HRESULT WINAPI BindStatusCallback_OnStartBinding(IBindStatusCallback *iface,
305 DWORD reserved, IBinding *pbind)
307 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
309 TRACE("(%p)->(%d %p)\n", This, reserved, pbind);
311 if (!pbind) return E_INVALIDARG;
313 This->binding = pbind;
314 IBinding_AddRef(pbind);
316 httprequest_setreadystate(This->request, READYSTATE_LOADED);
318 return CreateStreamOnHGlobal(NULL, TRUE, &This->stream);
321 static HRESULT WINAPI BindStatusCallback_GetPriority(IBindStatusCallback *iface, LONG *pPriority)
323 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
325 TRACE("(%p)->(%p)\n", This, pPriority);
327 return E_NOTIMPL;
330 static HRESULT WINAPI BindStatusCallback_OnLowResource(IBindStatusCallback *iface, DWORD reserved)
332 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
334 TRACE("(%p)->(%d)\n", This, reserved);
336 return E_NOTIMPL;
339 static HRESULT WINAPI BindStatusCallback_OnProgress(IBindStatusCallback *iface, ULONG ulProgress,
340 ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
342 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
344 TRACE("(%p)->(%u %u %u %s)\n", This, ulProgress, ulProgressMax, ulStatusCode,
345 debugstr_w(szStatusText));
347 return S_OK;
350 static HRESULT WINAPI BindStatusCallback_OnStopBinding(IBindStatusCallback *iface,
351 HRESULT hr, LPCWSTR error)
353 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
355 TRACE("(%p)->(0x%08x %s)\n", This, hr, debugstr_w(error));
357 if (This->binding)
359 IBinding_Release(This->binding);
360 This->binding = NULL;
363 if (hr == S_OK)
365 BindStatusCallback_Detach(This->request->bsc);
366 This->request->bsc = This;
367 httprequest_setreadystate(This->request, READYSTATE_COMPLETE);
370 return S_OK;
373 static HRESULT WINAPI BindStatusCallback_GetBindInfo(IBindStatusCallback *iface,
374 DWORD *bind_flags, BINDINFO *pbindinfo)
376 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
378 TRACE("(%p)->(%p %p)\n", This, bind_flags, pbindinfo);
380 *bind_flags = 0;
381 if (This->request->async) *bind_flags |= BINDF_ASYNCHRONOUS;
383 if (This->request->verb != BINDVERB_GET && This->body)
385 pbindinfo->stgmedData.tymed = TYMED_HGLOBAL;
386 pbindinfo->stgmedData.u.hGlobal = This->body;
387 pbindinfo->cbstgmedData = GlobalSize(This->body);
388 /* callback owns passed body pointer */
389 IBindStatusCallback_QueryInterface(iface, &IID_IUnknown, (void**)&pbindinfo->stgmedData.pUnkForRelease);
392 pbindinfo->dwBindVerb = This->request->verb;
393 if (This->request->verb == BINDVERB_CUSTOM)
395 pbindinfo->szCustomVerb = CoTaskMemAlloc(SysStringByteLen(This->request->custom));
396 strcpyW(pbindinfo->szCustomVerb, This->request->custom);
399 return S_OK;
402 static HRESULT WINAPI BindStatusCallback_OnDataAvailable(IBindStatusCallback *iface,
403 DWORD flags, DWORD size, FORMATETC *format, STGMEDIUM *stgmed)
405 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
406 DWORD read, written;
407 BYTE buf[4096];
408 HRESULT hr;
410 TRACE("(%p)->(%08x %d %p %p)\n", This, flags, size, format, stgmed);
414 hr = IStream_Read(stgmed->u.pstm, buf, sizeof(buf), &read);
415 if (hr != S_OK) break;
417 hr = IStream_Write(This->stream, buf, read, &written);
418 } while((hr == S_OK) && written != 0 && read != 0);
420 httprequest_setreadystate(This->request, READYSTATE_INTERACTIVE);
422 return S_OK;
425 static HRESULT WINAPI BindStatusCallback_OnObjectAvailable(IBindStatusCallback *iface,
426 REFIID riid, IUnknown *punk)
428 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
430 FIXME("(%p)->(%s %p): stub\n", This, debugstr_guid(riid), punk);
432 return E_NOTIMPL;
435 static const IBindStatusCallbackVtbl BindStatusCallbackVtbl = {
436 BindStatusCallback_QueryInterface,
437 BindStatusCallback_AddRef,
438 BindStatusCallback_Release,
439 BindStatusCallback_OnStartBinding,
440 BindStatusCallback_GetPriority,
441 BindStatusCallback_OnLowResource,
442 BindStatusCallback_OnProgress,
443 BindStatusCallback_OnStopBinding,
444 BindStatusCallback_GetBindInfo,
445 BindStatusCallback_OnDataAvailable,
446 BindStatusCallback_OnObjectAvailable
449 static HRESULT WINAPI BSCHttpNegotiate_QueryInterface(IHttpNegotiate *iface,
450 REFIID riid, void **ppv)
452 BindStatusCallback *This = impl_from_IHttpNegotiate(iface);
453 return IBindStatusCallback_QueryInterface(&This->IBindStatusCallback_iface, riid, ppv);
456 static ULONG WINAPI BSCHttpNegotiate_AddRef(IHttpNegotiate *iface)
458 BindStatusCallback *This = impl_from_IHttpNegotiate(iface);
459 return IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
462 static ULONG WINAPI BSCHttpNegotiate_Release(IHttpNegotiate *iface)
464 BindStatusCallback *This = impl_from_IHttpNegotiate(iface);
465 return IBindStatusCallback_Release(&This->IBindStatusCallback_iface);
468 static HRESULT WINAPI BSCHttpNegotiate_BeginningTransaction(IHttpNegotiate *iface,
469 LPCWSTR url, LPCWSTR headers, DWORD reserved, LPWSTR *add_headers)
471 static const WCHAR content_type_utf8W[] = {'C','o','n','t','e','n','t','-','T','y','p','e',':',' ',
472 't','e','x','t','/','p','l','a','i','n',';','c','h','a','r','s','e','t','=','u','t','f','-','8','\r','\n',0};
473 static const WCHAR refererW[] = {'R','e','f','e','r','e','r',':',' ',0};
475 BindStatusCallback *This = impl_from_IHttpNegotiate(iface);
476 const struct httpheader *entry;
477 BSTR base_uri = NULL;
478 WCHAR *buff, *ptr;
479 int size = 0;
481 TRACE("(%p)->(%s %s %d %p)\n", This, debugstr_w(url), debugstr_w(headers), reserved, add_headers);
483 *add_headers = NULL;
485 if (This->request->use_utf8_content)
486 size = sizeof(content_type_utf8W);
488 if (!list_empty(&This->request->reqheaders))
489 size += This->request->reqheader_size*sizeof(WCHAR);
491 if (This->request->base_uri)
493 IUri_GetRawUri(This->request->base_uri, &base_uri);
494 size += SysStringLen(base_uri)*sizeof(WCHAR) + sizeof(refererW) + sizeof(crlfW);
497 if (!size)
499 SysFreeString(base_uri);
500 return S_OK;
503 buff = CoTaskMemAlloc(size);
504 if (!buff)
506 SysFreeString(base_uri);
507 return E_OUTOFMEMORY;
510 ptr = buff;
511 if (This->request->use_utf8_content)
513 lstrcpyW(ptr, content_type_utf8W);
514 ptr += sizeof(content_type_utf8W)/sizeof(WCHAR)-1;
517 if (base_uri)
519 strcpyW(ptr, refererW);
520 strcatW(ptr, base_uri);
521 strcatW(ptr, crlfW);
522 ptr += strlenW(refererW) + SysStringLen(base_uri) + strlenW(crlfW);
523 SysFreeString(base_uri);
526 /* user headers */
527 LIST_FOR_EACH_ENTRY(entry, &This->request->reqheaders, struct httpheader, entry)
529 lstrcpyW(ptr, entry->header);
530 ptr += SysStringLen(entry->header);
532 lstrcpyW(ptr, colspaceW);
533 ptr += sizeof(colspaceW)/sizeof(WCHAR)-1;
535 lstrcpyW(ptr, entry->value);
536 ptr += SysStringLen(entry->value);
538 lstrcpyW(ptr, crlfW);
539 ptr += sizeof(crlfW)/sizeof(WCHAR)-1;
542 *add_headers = buff;
544 return S_OK;
547 static void add_response_header(httprequest *This, const WCHAR *data, int len)
549 struct httpheader *entry;
550 const WCHAR *ptr = data;
551 BSTR header, value;
553 while (*ptr)
555 if (*ptr == ':')
557 header = SysAllocStringLen(data, ptr-data);
558 /* skip leading spaces for a value */
559 while (*++ptr == ' ')
561 value = SysAllocStringLen(ptr, len-(ptr-data));
562 break;
564 ptr++;
567 if (!*ptr) return;
569 /* new header */
570 TRACE("got header %s:%s\n", debugstr_w(header), debugstr_w(value));
572 entry = heap_alloc(sizeof(*entry));
573 entry->header = header;
574 entry->value = value;
575 list_add_head(&This->respheaders, &entry->entry);
578 static HRESULT WINAPI BSCHttpNegotiate_OnResponse(IHttpNegotiate *iface, DWORD code,
579 LPCWSTR resp_headers, LPCWSTR req_headers, LPWSTR *add_reqheaders)
581 BindStatusCallback *This = impl_from_IHttpNegotiate(iface);
583 TRACE("(%p)->(%d %s %s %p)\n", This, code, debugstr_w(resp_headers),
584 debugstr_w(req_headers), add_reqheaders);
586 This->request->status = code;
587 /* store headers and status text */
588 free_response_headers(This->request);
589 SysFreeString(This->request->status_text);
590 This->request->status_text = NULL;
591 if (resp_headers)
593 const WCHAR *ptr, *line, *status_text;
595 ptr = line = resp_headers;
597 /* skip HTTP-Version */
598 ptr = strchrW(ptr, ' ');
599 if (ptr)
601 /* skip Status-Code */
602 ptr = strchrW(++ptr, ' ');
603 if (ptr)
605 status_text = ++ptr;
606 /* now it supposed to end with CRLF */
607 while (*ptr)
609 if (*ptr == '\r' && *(ptr+1) == '\n')
611 line = ptr + 2;
612 This->request->status_text = SysAllocStringLen(status_text, ptr-status_text);
613 TRACE("status text %s\n", debugstr_w(This->request->status_text));
614 break;
616 ptr++;
621 /* store as unparsed string for now */
622 This->request->raw_respheaders = SysAllocString(line);
625 return S_OK;
628 static const IHttpNegotiateVtbl BSCHttpNegotiateVtbl = {
629 BSCHttpNegotiate_QueryInterface,
630 BSCHttpNegotiate_AddRef,
631 BSCHttpNegotiate_Release,
632 BSCHttpNegotiate_BeginningTransaction,
633 BSCHttpNegotiate_OnResponse
636 static HRESULT WINAPI Authenticate_QueryInterface(IAuthenticate *iface,
637 REFIID riid, void **ppv)
639 BindStatusCallback *This = impl_from_IAuthenticate(iface);
640 return IBindStatusCallback_QueryInterface(&This->IBindStatusCallback_iface, riid, ppv);
643 static ULONG WINAPI Authenticate_AddRef(IAuthenticate *iface)
645 BindStatusCallback *This = impl_from_IAuthenticate(iface);
646 return IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
649 static ULONG WINAPI Authenticate_Release(IAuthenticate *iface)
651 BindStatusCallback *This = impl_from_IAuthenticate(iface);
652 return IBindStatusCallback_Release(&This->IBindStatusCallback_iface);
655 static HRESULT WINAPI Authenticate_Authenticate(IAuthenticate *iface,
656 HWND *hwnd, LPWSTR *username, LPWSTR *password)
658 BindStatusCallback *This = impl_from_IAuthenticate(iface);
659 httprequest *request = This->request;
661 TRACE("(%p)->(%p %p %p)\n", This, hwnd, username, password);
663 if (request->user && *request->user)
665 if (hwnd) *hwnd = NULL;
666 *username = CoTaskMemAlloc(SysStringByteLen(request->user)+sizeof(WCHAR));
667 *password = CoTaskMemAlloc(SysStringByteLen(request->password)+sizeof(WCHAR));
668 if (!*username || !*password)
670 CoTaskMemFree(*username);
671 CoTaskMemFree(*password);
672 return E_OUTOFMEMORY;
675 memcpy(*username, request->user, SysStringByteLen(request->user)+sizeof(WCHAR));
676 memcpy(*password, request->password, SysStringByteLen(request->password)+sizeof(WCHAR));
679 return S_OK;
682 static const IAuthenticateVtbl AuthenticateVtbl = {
683 Authenticate_QueryInterface,
684 Authenticate_AddRef,
685 Authenticate_Release,
686 Authenticate_Authenticate
689 static HRESULT BindStatusCallback_create(httprequest* This, BindStatusCallback **obj, const VARIANT *body)
691 BindStatusCallback *bsc;
692 IBindCtx *pbc;
693 HRESULT hr;
694 int size;
696 hr = CreateBindCtx(0, &pbc);
697 if (hr != S_OK) return hr;
699 bsc = heap_alloc(sizeof(*bsc));
700 if (!bsc)
702 IBindCtx_Release(pbc);
703 return E_OUTOFMEMORY;
706 bsc->IBindStatusCallback_iface.lpVtbl = &BindStatusCallbackVtbl;
707 bsc->IHttpNegotiate_iface.lpVtbl = &BSCHttpNegotiateVtbl;
708 bsc->IAuthenticate_iface.lpVtbl = &AuthenticateVtbl;
709 bsc->ref = 1;
710 bsc->request = This;
711 bsc->binding = NULL;
712 bsc->stream = NULL;
713 bsc->body = NULL;
715 TRACE("(%p)->(%p)\n", This, bsc);
717 This->use_utf8_content = FALSE;
719 if (This->verb != BINDVERB_GET)
721 void *send_data, *ptr;
722 SAFEARRAY *sa = NULL;
724 if (V_VT(body) == (VT_VARIANT|VT_BYREF))
725 body = V_VARIANTREF(body);
727 switch (V_VT(body))
729 case VT_BSTR:
731 int len = SysStringLen(V_BSTR(body));
732 const WCHAR *str = V_BSTR(body);
733 UINT i, cp = CP_ACP;
735 for (i = 0; i < len; i++)
737 if (str[i] > 127)
739 cp = CP_UTF8;
740 break;
744 size = WideCharToMultiByte(cp, 0, str, len, NULL, 0, NULL, NULL);
745 if (!(ptr = heap_alloc(size)))
747 heap_free(bsc);
748 return E_OUTOFMEMORY;
750 WideCharToMultiByte(cp, 0, str, len, ptr, size, NULL, NULL);
751 if (cp == CP_UTF8) This->use_utf8_content = TRUE;
752 break;
754 case VT_ARRAY|VT_UI1:
756 sa = V_ARRAY(body);
757 if ((hr = SafeArrayAccessData(sa, (void **)&ptr)) != S_OK)
759 heap_free(bsc);
760 return hr;
762 if ((hr = SafeArrayGetUBound(sa, 1, &size)) != S_OK)
764 SafeArrayUnaccessData(sa);
765 heap_free(bsc);
766 return hr;
768 size++;
769 break;
771 default:
772 FIXME("unsupported body data type %d\n", V_VT(body));
773 /* fall through */
774 case VT_EMPTY:
775 case VT_ERROR:
776 case VT_NULL:
777 ptr = NULL;
778 size = 0;
779 break;
782 if (size)
784 bsc->body = GlobalAlloc(GMEM_FIXED, size);
785 if (!bsc->body)
787 if (V_VT(body) == VT_BSTR)
788 heap_free(ptr);
789 else if (V_VT(body) == (VT_ARRAY|VT_UI1))
790 SafeArrayUnaccessData(sa);
792 heap_free(bsc);
793 return E_OUTOFMEMORY;
796 send_data = GlobalLock(bsc->body);
797 memcpy(send_data, ptr, size);
798 GlobalUnlock(bsc->body);
801 if (V_VT(body) == VT_BSTR)
802 heap_free(ptr);
803 else if (V_VT(body) == (VT_ARRAY|VT_UI1))
804 SafeArrayUnaccessData(sa);
807 hr = RegisterBindStatusCallback(pbc, &bsc->IBindStatusCallback_iface, NULL, 0);
808 if (hr == S_OK)
810 IMoniker *moniker;
812 hr = CreateURLMonikerEx2(NULL, This->uri, &moniker, URL_MK_UNIFORM);
813 if (hr == S_OK)
815 IStream *stream;
817 hr = IMoniker_BindToStorage(moniker, pbc, NULL, &IID_IStream, (void**)&stream);
818 IMoniker_Release(moniker);
819 if (stream) IStream_Release(stream);
821 IBindCtx_Release(pbc);
824 if (FAILED(hr))
826 IBindStatusCallback_Release(&bsc->IBindStatusCallback_iface);
827 bsc = NULL;
830 *obj = bsc;
831 return hr;
834 static HRESULT verify_uri(httprequest *This, IUri *uri)
836 DWORD scheme, base_scheme;
837 BSTR host, base_host;
838 HRESULT hr;
840 if(!(This->safeopt & INTERFACESAFE_FOR_UNTRUSTED_DATA))
841 return S_OK;
843 if(!This->base_uri)
844 return E_ACCESSDENIED;
846 hr = IUri_GetScheme(uri, &scheme);
847 if(FAILED(hr))
848 return hr;
850 hr = IUri_GetScheme(This->base_uri, &base_scheme);
851 if(FAILED(hr))
852 return hr;
854 if(scheme != base_scheme) {
855 WARN("Schemes don't match\n");
856 return E_ACCESSDENIED;
859 if(scheme == INTERNET_SCHEME_UNKNOWN) {
860 FIXME("Unknown scheme\n");
861 return E_ACCESSDENIED;
864 hr = IUri_GetHost(uri, &host);
865 if(FAILED(hr))
866 return hr;
868 hr = IUri_GetHost(This->base_uri, &base_host);
869 if(SUCCEEDED(hr)) {
870 if(strcmpiW(host, base_host)) {
871 WARN("Hosts don't match\n");
872 hr = E_ACCESSDENIED;
874 SysFreeString(base_host);
877 SysFreeString(host);
878 return hr;
881 static HRESULT httprequest_open(httprequest *This, BSTR method, BSTR url,
882 VARIANT async, VARIANT user, VARIANT password)
884 static const WCHAR MethodGetW[] = {'G','E','T',0};
885 static const WCHAR MethodPutW[] = {'P','U','T',0};
886 static const WCHAR MethodPostW[] = {'P','O','S','T',0};
887 static const WCHAR MethodDeleteW[] = {'D','E','L','E','T','E',0};
888 static const WCHAR MethodPropFindW[] = {'P','R','O','P','F','I','N','D',0};
889 VARIANT str, is_async;
890 IUri *uri;
891 HRESULT hr;
893 if (!method || !url) return E_INVALIDARG;
895 /* free previously set data */
896 if(This->uri) {
897 IUri_Release(This->uri);
898 This->uri = NULL;
901 SysFreeString(This->user);
902 SysFreeString(This->password);
903 This->user = This->password = NULL;
904 free_request_headers(This);
906 if (!strcmpiW(method, MethodGetW))
908 This->verb = BINDVERB_GET;
910 else if (!strcmpiW(method, MethodPutW))
912 This->verb = BINDVERB_PUT;
914 else if (!strcmpiW(method, MethodPostW))
916 This->verb = BINDVERB_POST;
918 else if (!strcmpiW(method, MethodDeleteW) ||
919 !strcmpiW(method, MethodPropFindW))
921 This->verb = BINDVERB_CUSTOM;
922 SysReAllocString(&This->custom, method);
924 else
926 FIXME("unsupported request type %s\n", debugstr_w(method));
927 This->verb = -1;
928 return E_FAIL;
931 if(This->base_uri)
932 hr = CoInternetCombineUrlEx(This->base_uri, url, 0, &uri, 0);
933 else
934 hr = CreateUri(url, 0, 0, &uri);
935 if(FAILED(hr)) {
936 WARN("Could not create IUri object: %08x\n", hr);
937 return hr;
940 hr = verify_uri(This, uri);
941 if(FAILED(hr)) {
942 IUri_Release(uri);
943 return hr;
946 VariantInit(&str);
947 hr = VariantChangeType(&str, &user, 0, VT_BSTR);
948 if (hr == S_OK)
949 This->user = V_BSTR(&str);
951 VariantInit(&str);
952 hr = VariantChangeType(&str, &password, 0, VT_BSTR);
953 if (hr == S_OK)
954 This->password = V_BSTR(&str);
956 /* add authentication info */
957 if (This->user && *This->user)
959 IUriBuilder *builder;
961 hr = CreateIUriBuilder(uri, 0, 0, &builder);
962 if (hr == S_OK)
964 IUri *full_uri;
966 IUriBuilder_SetUserName(builder, This->user);
967 IUriBuilder_SetPassword(builder, This->password);
968 hr = IUriBuilder_CreateUri(builder, -1, 0, 0, &full_uri);
969 if (hr == S_OK)
971 IUri_Release(uri);
972 uri = full_uri;
974 else
975 WARN("failed to create modified uri, 0x%08x\n", hr);
976 IUriBuilder_Release(builder);
978 else
979 WARN("IUriBuilder creation failed, 0x%08x\n", hr);
982 This->uri = uri;
984 VariantInit(&is_async);
985 hr = VariantChangeType(&is_async, &async, 0, VT_BOOL);
986 This->async = hr == S_OK && V_BOOL(&is_async);
988 httprequest_setreadystate(This, READYSTATE_LOADING);
990 return S_OK;
993 static HRESULT httprequest_setRequestHeader(httprequest *This, BSTR header, BSTR value)
995 struct httpheader *entry;
997 if (!header || !*header) return E_INVALIDARG;
998 if (This->state != READYSTATE_LOADING) return E_FAIL;
999 if (!value) return E_INVALIDARG;
1001 /* replace existing header value if already added */
1002 LIST_FOR_EACH_ENTRY(entry, &This->reqheaders, struct httpheader, entry)
1004 if (lstrcmpW(entry->header, header) == 0)
1006 LONG length = SysStringLen(entry->value);
1007 HRESULT hr;
1009 hr = SysReAllocString(&entry->value, value) ? S_OK : E_OUTOFMEMORY;
1011 if (hr == S_OK)
1012 This->reqheader_size += (SysStringLen(entry->value) - length);
1014 return hr;
1018 entry = heap_alloc(sizeof(*entry));
1019 if (!entry) return E_OUTOFMEMORY;
1021 /* new header */
1022 entry->header = SysAllocString(header);
1023 entry->value = SysAllocString(value);
1025 /* header length including null terminator */
1026 This->reqheader_size += SysStringLen(entry->header) + sizeof(colspaceW)/sizeof(WCHAR) +
1027 SysStringLen(entry->value) + sizeof(crlfW)/sizeof(WCHAR) - 1;
1029 list_add_head(&This->reqheaders, &entry->entry);
1031 return S_OK;
1034 static HRESULT httprequest_getResponseHeader(httprequest *This, BSTR header, BSTR *value)
1036 struct httpheader *entry;
1038 if (!header) return E_INVALIDARG;
1039 if (!value) return E_POINTER;
1041 if (This->raw_respheaders && list_empty(&This->respheaders))
1043 WCHAR *ptr, *line;
1045 ptr = line = This->raw_respheaders;
1046 while (*ptr)
1048 if (*ptr == '\r' && *(ptr+1) == '\n')
1050 add_response_header(This, line, ptr-line);
1051 ptr++; line = ++ptr;
1052 continue;
1054 ptr++;
1058 LIST_FOR_EACH_ENTRY(entry, &This->respheaders, struct httpheader, entry)
1060 if (!strcmpiW(entry->header, header))
1062 *value = SysAllocString(entry->value);
1063 TRACE("header value %s\n", debugstr_w(*value));
1064 return S_OK;
1068 return S_FALSE;
1071 static HRESULT httprequest_getAllResponseHeaders(httprequest *This, BSTR *respheaders)
1073 if (!respheaders) return E_POINTER;
1075 *respheaders = SysAllocString(This->raw_respheaders);
1077 return S_OK;
1080 static HRESULT httprequest_send(httprequest *This, VARIANT body)
1082 BindStatusCallback *bsc = NULL;
1083 HRESULT hr;
1085 if (This->state != READYSTATE_LOADING) return E_FAIL;
1087 hr = BindStatusCallback_create(This, &bsc, &body);
1088 if (FAILED(hr))
1089 /* success path to detach it is OnStopBinding call */
1090 BindStatusCallback_Detach(bsc);
1092 return hr;
1095 static HRESULT httprequest_abort(httprequest *This)
1097 BindStatusCallback_Detach(This->bsc);
1099 httprequest_setreadystate(This, READYSTATE_UNINITIALIZED);
1101 return S_OK;
1104 static HRESULT httprequest_get_status(httprequest *This, LONG *status)
1106 if (!status) return E_POINTER;
1108 *status = This->status;
1110 return This->state == READYSTATE_COMPLETE ? S_OK : E_FAIL;
1113 static HRESULT httprequest_get_statusText(httprequest *This, BSTR *status)
1115 if (!status) return E_POINTER;
1116 if (This->state != READYSTATE_COMPLETE) return E_FAIL;
1118 *status = SysAllocString(This->status_text);
1120 return S_OK;
1123 static HRESULT httprequest_get_responseText(httprequest *This, BSTR *body)
1125 HGLOBAL hglobal;
1126 HRESULT hr;
1128 if (!body) return E_POINTER;
1129 if (This->state != READYSTATE_COMPLETE) return E_FAIL;
1131 hr = GetHGlobalFromStream(This->bsc->stream, &hglobal);
1132 if (hr == S_OK)
1134 xmlChar *ptr = GlobalLock(hglobal);
1135 DWORD size = GlobalSize(hglobal);
1136 xmlCharEncoding encoding = XML_CHAR_ENCODING_UTF8;
1138 /* try to determine data encoding */
1139 if (size >= 4)
1141 encoding = xmlDetectCharEncoding(ptr, 4);
1142 TRACE("detected encoding: %s\n", debugstr_a(xmlGetCharEncodingName(encoding)));
1143 if ( encoding != XML_CHAR_ENCODING_UTF8 &&
1144 encoding != XML_CHAR_ENCODING_UTF16LE &&
1145 encoding != XML_CHAR_ENCODING_NONE )
1147 FIXME("unsupported encoding: %s\n", debugstr_a(xmlGetCharEncodingName(encoding)));
1148 GlobalUnlock(hglobal);
1149 return E_FAIL;
1153 /* without BOM assume UTF-8 */
1154 if (encoding == XML_CHAR_ENCODING_UTF8 ||
1155 encoding == XML_CHAR_ENCODING_NONE )
1157 DWORD length = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)ptr, size, NULL, 0);
1159 *body = SysAllocStringLen(NULL, length);
1160 if (*body)
1161 MultiByteToWideChar( CP_UTF8, 0, (LPCSTR)ptr, size, *body, length);
1163 else
1164 *body = SysAllocStringByteLen((LPCSTR)ptr, size);
1166 if (!*body) hr = E_OUTOFMEMORY;
1167 GlobalUnlock(hglobal);
1170 return hr;
1173 static HRESULT httprequest_get_responseXML(httprequest *This, IDispatch **body)
1175 IXMLDOMDocument3 *doc;
1176 HRESULT hr;
1177 BSTR str;
1179 if (!body) return E_INVALIDARG;
1180 if (This->state != READYSTATE_COMPLETE) return E_FAIL;
1182 hr = DOMDocument_create(MSXML_DEFAULT, (void**)&doc);
1183 if (hr != S_OK) return hr;
1185 hr = httprequest_get_responseText(This, &str);
1186 if (hr == S_OK)
1188 VARIANT_BOOL ok;
1190 hr = IXMLDOMDocument3_loadXML(doc, str, &ok);
1191 SysFreeString(str);
1194 IXMLDOMDocument3_QueryInterface(doc, &IID_IDispatch, (void**)body);
1195 IXMLDOMDocument3_Release(doc);
1197 return hr;
1200 static HRESULT httprequest_get_responseBody(httprequest *This, VARIANT *body)
1202 HGLOBAL hglobal;
1203 HRESULT hr;
1205 if (!body) return E_INVALIDARG;
1206 V_VT(body) = VT_EMPTY;
1208 if (This->state != READYSTATE_COMPLETE) return E_PENDING;
1210 hr = GetHGlobalFromStream(This->bsc->stream, &hglobal);
1211 if (hr == S_OK)
1213 void *ptr = GlobalLock(hglobal);
1214 DWORD size = GlobalSize(hglobal);
1216 SAFEARRAYBOUND bound;
1217 SAFEARRAY *array;
1219 bound.lLbound = 0;
1220 bound.cElements = size;
1221 array = SafeArrayCreate(VT_UI1, 1, &bound);
1223 if (array)
1225 void *dest;
1227 V_VT(body) = VT_ARRAY | VT_UI1;
1228 V_ARRAY(body) = array;
1230 hr = SafeArrayAccessData(array, &dest);
1231 if (hr == S_OK)
1233 memcpy(dest, ptr, size);
1234 SafeArrayUnaccessData(array);
1236 else
1238 VariantClear(body);
1241 else
1242 hr = E_FAIL;
1244 GlobalUnlock(hglobal);
1247 return hr;
1250 static HRESULT httprequest_get_responseStream(httprequest *This, VARIANT *body)
1252 LARGE_INTEGER move;
1253 IStream *stream;
1254 HRESULT hr;
1256 if (!body) return E_INVALIDARG;
1257 V_VT(body) = VT_EMPTY;
1259 if (This->state != READYSTATE_COMPLETE) return E_PENDING;
1261 hr = IStream_Clone(This->bsc->stream, &stream);
1263 move.QuadPart = 0;
1264 IStream_Seek(stream, move, STREAM_SEEK_SET, NULL);
1266 V_VT(body) = VT_UNKNOWN;
1267 V_UNKNOWN(body) = (IUnknown*)stream;
1269 return hr;
1272 static HRESULT httprequest_get_readyState(httprequest *This, LONG *state)
1274 if (!state) return E_POINTER;
1276 *state = This->state;
1277 return S_OK;
1280 static HRESULT httprequest_put_onreadystatechange(httprequest *This, IDispatch *sink)
1282 if (This->sink) IDispatch_Release(This->sink);
1283 if ((This->sink = sink)) IDispatch_AddRef(This->sink);
1285 return S_OK;
1288 static void httprequest_release(httprequest *This)
1290 if (This->site)
1291 IUnknown_Release( This->site );
1292 if (This->uri)
1293 IUri_Release(This->uri);
1294 if (This->base_uri)
1295 IUri_Release(This->base_uri);
1297 SysFreeString(This->custom);
1298 SysFreeString(This->user);
1299 SysFreeString(This->password);
1301 /* cleanup headers lists */
1302 free_request_headers(This);
1303 free_response_headers(This);
1304 SysFreeString(This->status_text);
1306 /* detach callback object */
1307 BindStatusCallback_Detach(This->bsc);
1309 if (This->sink) IDispatch_Release(This->sink);
1312 static HRESULT WINAPI XMLHTTPRequest_QueryInterface(IXMLHTTPRequest *iface, REFIID riid, void **ppvObject)
1314 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1315 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppvObject);
1317 if ( IsEqualGUID( riid, &IID_IXMLHTTPRequest) ||
1318 IsEqualGUID( riid, &IID_IDispatch) ||
1319 IsEqualGUID( riid, &IID_IUnknown) )
1321 *ppvObject = iface;
1323 else if (IsEqualGUID(&IID_IObjectWithSite, riid))
1325 *ppvObject = &This->IObjectWithSite_iface;
1327 else if (IsEqualGUID(&IID_IObjectSafety, riid))
1329 *ppvObject = &This->IObjectSafety_iface;
1331 else
1333 TRACE("Unsupported interface %s\n", debugstr_guid(riid));
1334 *ppvObject = NULL;
1335 return E_NOINTERFACE;
1338 IXMLHTTPRequest_AddRef( iface );
1340 return S_OK;
1343 static ULONG WINAPI XMLHTTPRequest_AddRef(IXMLHTTPRequest *iface)
1345 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1346 ULONG ref = InterlockedIncrement( &This->ref );
1347 TRACE("(%p)->(%u)\n", This, ref );
1348 return ref;
1351 static ULONG WINAPI XMLHTTPRequest_Release(IXMLHTTPRequest *iface)
1353 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1354 ULONG ref = InterlockedDecrement( &This->ref );
1356 TRACE("(%p)->(%u)\n", This, ref );
1358 if ( ref == 0 )
1360 httprequest_release( This );
1361 heap_free( This );
1364 return ref;
1367 static HRESULT WINAPI XMLHTTPRequest_GetTypeInfoCount(IXMLHTTPRequest *iface, UINT *pctinfo)
1369 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1371 TRACE("(%p)->(%p)\n", This, pctinfo);
1373 *pctinfo = 1;
1375 return S_OK;
1378 static HRESULT WINAPI XMLHTTPRequest_GetTypeInfo(IXMLHTTPRequest *iface, UINT iTInfo,
1379 LCID lcid, ITypeInfo **ppTInfo)
1381 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1383 TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
1385 return get_typeinfo(IXMLHTTPRequest_tid, ppTInfo);
1388 static HRESULT WINAPI XMLHTTPRequest_GetIDsOfNames(IXMLHTTPRequest *iface, REFIID riid,
1389 LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
1391 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1392 ITypeInfo *typeinfo;
1393 HRESULT hr;
1395 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
1396 lcid, rgDispId);
1398 if(!rgszNames || cNames == 0 || !rgDispId)
1399 return E_INVALIDARG;
1401 hr = get_typeinfo(IXMLHTTPRequest_tid, &typeinfo);
1402 if(SUCCEEDED(hr))
1404 hr = ITypeInfo_GetIDsOfNames(typeinfo, rgszNames, cNames, rgDispId);
1405 ITypeInfo_Release(typeinfo);
1408 return hr;
1411 static HRESULT WINAPI XMLHTTPRequest_Invoke(IXMLHTTPRequest *iface, DISPID dispIdMember, REFIID riid,
1412 LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult,
1413 EXCEPINFO *pExcepInfo, UINT *puArgErr)
1415 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1416 ITypeInfo *typeinfo;
1417 HRESULT hr;
1419 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
1420 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
1422 hr = get_typeinfo(IXMLHTTPRequest_tid, &typeinfo);
1423 if(SUCCEEDED(hr))
1425 hr = ITypeInfo_Invoke(typeinfo, &This->IXMLHTTPRequest_iface, dispIdMember, wFlags,
1426 pDispParams, pVarResult, pExcepInfo, puArgErr);
1427 ITypeInfo_Release(typeinfo);
1430 return hr;
1433 static HRESULT WINAPI XMLHTTPRequest_open(IXMLHTTPRequest *iface, BSTR method, BSTR url,
1434 VARIANT async, VARIANT user, VARIANT password)
1436 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1437 TRACE("(%p)->(%s %s %s)\n", This, debugstr_w(method), debugstr_w(url),
1438 debugstr_variant(&async));
1439 return httprequest_open(This, method, url, async, user, password);
1442 static HRESULT WINAPI XMLHTTPRequest_setRequestHeader(IXMLHTTPRequest *iface, BSTR header, BSTR value)
1444 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1445 TRACE("(%p)->(%s %s)\n", This, debugstr_w(header), debugstr_w(value));
1446 return httprequest_setRequestHeader(This, header, value);
1449 static HRESULT WINAPI XMLHTTPRequest_getResponseHeader(IXMLHTTPRequest *iface, BSTR header, BSTR *value)
1451 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1452 TRACE("(%p)->(%s %p)\n", This, debugstr_w(header), value);
1453 return httprequest_getResponseHeader(This, header, value);
1456 static HRESULT WINAPI XMLHTTPRequest_getAllResponseHeaders(IXMLHTTPRequest *iface, BSTR *respheaders)
1458 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1459 TRACE("(%p)->(%p)\n", This, respheaders);
1460 return httprequest_getAllResponseHeaders(This, respheaders);
1463 static HRESULT WINAPI XMLHTTPRequest_send(IXMLHTTPRequest *iface, VARIANT body)
1465 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1466 TRACE("(%p)->(%s)\n", This, debugstr_variant(&body));
1467 return httprequest_send(This, body);
1470 static HRESULT WINAPI XMLHTTPRequest_abort(IXMLHTTPRequest *iface)
1472 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1473 TRACE("(%p)\n", This);
1474 return httprequest_abort(This);
1477 static HRESULT WINAPI XMLHTTPRequest_get_status(IXMLHTTPRequest *iface, LONG *status)
1479 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1480 TRACE("(%p)->(%p)\n", This, status);
1481 return httprequest_get_status(This, status);
1484 static HRESULT WINAPI XMLHTTPRequest_get_statusText(IXMLHTTPRequest *iface, BSTR *status)
1486 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1487 TRACE("(%p)->(%p)\n", This, status);
1488 return httprequest_get_statusText(This, status);
1491 static HRESULT WINAPI XMLHTTPRequest_get_responseXML(IXMLHTTPRequest *iface, IDispatch **body)
1493 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1494 TRACE("(%p)->(%p)\n", This, body);
1495 return httprequest_get_responseXML(This, body);
1498 static HRESULT WINAPI XMLHTTPRequest_get_responseText(IXMLHTTPRequest *iface, BSTR *body)
1500 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1501 TRACE("(%p)->(%p)\n", This, body);
1502 return httprequest_get_responseText(This, body);
1505 static HRESULT WINAPI XMLHTTPRequest_get_responseBody(IXMLHTTPRequest *iface, VARIANT *body)
1507 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1508 TRACE("(%p)->(%p)\n", This, body);
1509 return httprequest_get_responseBody(This, body);
1512 static HRESULT WINAPI XMLHTTPRequest_get_responseStream(IXMLHTTPRequest *iface, VARIANT *body)
1514 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1515 TRACE("(%p)->(%p)\n", This, body);
1516 return httprequest_get_responseStream(This, body);
1519 static HRESULT WINAPI XMLHTTPRequest_get_readyState(IXMLHTTPRequest *iface, LONG *state)
1521 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1522 TRACE("(%p)->(%p)\n", This, state);
1523 return httprequest_get_readyState(This, state);
1526 static HRESULT WINAPI XMLHTTPRequest_put_onreadystatechange(IXMLHTTPRequest *iface, IDispatch *sink)
1528 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1529 TRACE("(%p)->(%p)\n", This, sink);
1530 return httprequest_put_onreadystatechange(This, sink);
1533 static const struct IXMLHTTPRequestVtbl XMLHTTPRequestVtbl =
1535 XMLHTTPRequest_QueryInterface,
1536 XMLHTTPRequest_AddRef,
1537 XMLHTTPRequest_Release,
1538 XMLHTTPRequest_GetTypeInfoCount,
1539 XMLHTTPRequest_GetTypeInfo,
1540 XMLHTTPRequest_GetIDsOfNames,
1541 XMLHTTPRequest_Invoke,
1542 XMLHTTPRequest_open,
1543 XMLHTTPRequest_setRequestHeader,
1544 XMLHTTPRequest_getResponseHeader,
1545 XMLHTTPRequest_getAllResponseHeaders,
1546 XMLHTTPRequest_send,
1547 XMLHTTPRequest_abort,
1548 XMLHTTPRequest_get_status,
1549 XMLHTTPRequest_get_statusText,
1550 XMLHTTPRequest_get_responseXML,
1551 XMLHTTPRequest_get_responseText,
1552 XMLHTTPRequest_get_responseBody,
1553 XMLHTTPRequest_get_responseStream,
1554 XMLHTTPRequest_get_readyState,
1555 XMLHTTPRequest_put_onreadystatechange
1558 /* IObjectWithSite */
1559 static HRESULT WINAPI
1560 httprequest_ObjectWithSite_QueryInterface( IObjectWithSite* iface, REFIID riid, void** ppvObject )
1562 httprequest *This = impl_from_IObjectWithSite(iface);
1563 return IXMLHTTPRequest_QueryInterface( (IXMLHTTPRequest *)This, riid, ppvObject );
1566 static ULONG WINAPI httprequest_ObjectWithSite_AddRef( IObjectWithSite* iface )
1568 httprequest *This = impl_from_IObjectWithSite(iface);
1569 return IXMLHTTPRequest_AddRef((IXMLHTTPRequest *)This);
1572 static ULONG WINAPI httprequest_ObjectWithSite_Release( IObjectWithSite* iface )
1574 httprequest *This = impl_from_IObjectWithSite(iface);
1575 return IXMLHTTPRequest_Release((IXMLHTTPRequest *)This);
1578 static HRESULT WINAPI httprequest_ObjectWithSite_GetSite( IObjectWithSite *iface, REFIID iid, void **ppvSite )
1580 httprequest *This = impl_from_IObjectWithSite(iface);
1582 TRACE("(%p)->(%s %p)\n", This, debugstr_guid( iid ), ppvSite );
1584 if ( !This->site )
1585 return E_FAIL;
1587 return IUnknown_QueryInterface( This->site, iid, ppvSite );
1590 static void get_base_uri(httprequest *This)
1592 IServiceProvider *provider;
1593 IHTMLDocument2 *doc;
1594 IUri *uri;
1595 BSTR url;
1596 HRESULT hr;
1598 hr = IUnknown_QueryInterface(This->site, &IID_IServiceProvider, (void**)&provider);
1599 if(FAILED(hr))
1600 return;
1602 hr = IServiceProvider_QueryService(provider, &SID_SContainerDispatch, &IID_IHTMLDocument2, (void**)&doc);
1603 if(FAILED(hr))
1604 hr = IServiceProvider_QueryService(provider, &SID_SInternetHostSecurityManager, &IID_IHTMLDocument2, (void**)&doc);
1605 IServiceProvider_Release(provider);
1606 if(FAILED(hr))
1607 return;
1609 hr = IHTMLDocument2_get_URL(doc, &url);
1610 IHTMLDocument2_Release(doc);
1611 if(FAILED(hr) || !url || !*url)
1612 return;
1614 TRACE("host url %s\n", debugstr_w(url));
1616 hr = CreateUri(url, 0, 0, &uri);
1617 SysFreeString(url);
1618 if(FAILED(hr))
1619 return;
1621 This->base_uri = uri;
1624 static HRESULT WINAPI httprequest_ObjectWithSite_SetSite( IObjectWithSite *iface, IUnknown *punk )
1626 httprequest *This = impl_from_IObjectWithSite(iface);
1628 TRACE("(%p)->(%p)\n", This, punk);
1630 if(This->site)
1631 IUnknown_Release( This->site );
1632 if(This->base_uri)
1633 IUri_Release(This->base_uri);
1635 This->site = punk;
1637 if (punk)
1639 IUnknown_AddRef( punk );
1640 get_base_uri(This);
1643 return S_OK;
1646 static const IObjectWithSiteVtbl ObjectWithSiteVtbl =
1648 httprequest_ObjectWithSite_QueryInterface,
1649 httprequest_ObjectWithSite_AddRef,
1650 httprequest_ObjectWithSite_Release,
1651 httprequest_ObjectWithSite_SetSite,
1652 httprequest_ObjectWithSite_GetSite
1655 /* IObjectSafety */
1656 static HRESULT WINAPI httprequest_Safety_QueryInterface(IObjectSafety *iface, REFIID riid, void **ppv)
1658 httprequest *This = impl_from_IObjectSafety(iface);
1659 return IXMLHTTPRequest_QueryInterface( (IXMLHTTPRequest *)This, riid, ppv );
1662 static ULONG WINAPI httprequest_Safety_AddRef(IObjectSafety *iface)
1664 httprequest *This = impl_from_IObjectSafety(iface);
1665 return IXMLHTTPRequest_AddRef((IXMLHTTPRequest *)This);
1668 static ULONG WINAPI httprequest_Safety_Release(IObjectSafety *iface)
1670 httprequest *This = impl_from_IObjectSafety(iface);
1671 return IXMLHTTPRequest_Release((IXMLHTTPRequest *)This);
1674 static HRESULT WINAPI httprequest_Safety_GetInterfaceSafetyOptions(IObjectSafety *iface, REFIID riid,
1675 DWORD *supported, DWORD *enabled)
1677 httprequest *This = impl_from_IObjectSafety(iface);
1679 TRACE("(%p)->(%s %p %p)\n", This, debugstr_guid(riid), supported, enabled);
1681 if(!supported || !enabled) return E_POINTER;
1683 *supported = safety_supported_options;
1684 *enabled = This->safeopt;
1686 return S_OK;
1689 static HRESULT WINAPI httprequest_Safety_SetInterfaceSafetyOptions(IObjectSafety *iface, REFIID riid,
1690 DWORD mask, DWORD enabled)
1692 httprequest *This = impl_from_IObjectSafety(iface);
1693 TRACE("(%p)->(%s %x %x)\n", This, debugstr_guid(riid), mask, enabled);
1695 if ((mask & ~safety_supported_options))
1696 return E_FAIL;
1698 This->safeopt = (This->safeopt & ~mask) | (mask & enabled);
1700 return S_OK;
1703 static const IObjectSafetyVtbl ObjectSafetyVtbl = {
1704 httprequest_Safety_QueryInterface,
1705 httprequest_Safety_AddRef,
1706 httprequest_Safety_Release,
1707 httprequest_Safety_GetInterfaceSafetyOptions,
1708 httprequest_Safety_SetInterfaceSafetyOptions
1711 /* IServerXMLHTTPRequest */
1712 static HRESULT WINAPI ServerXMLHTTPRequest_QueryInterface(IServerXMLHTTPRequest *iface, REFIID riid, void **obj)
1714 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1716 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), obj);
1718 if ( IsEqualGUID( riid, &IID_IServerXMLHTTPRequest) ||
1719 IsEqualGUID( riid, &IID_IXMLHTTPRequest) ||
1720 IsEqualGUID( riid, &IID_IDispatch) ||
1721 IsEqualGUID( riid, &IID_IUnknown) )
1723 *obj = iface;
1725 else
1727 TRACE("Unsupported interface %s\n", debugstr_guid(riid));
1728 *obj = NULL;
1729 return E_NOINTERFACE;
1732 IServerXMLHTTPRequest_AddRef( iface );
1734 return S_OK;
1737 static ULONG WINAPI ServerXMLHTTPRequest_AddRef(IServerXMLHTTPRequest *iface)
1739 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1740 ULONG ref = InterlockedIncrement( &This->ref );
1741 TRACE("(%p)->(%u)\n", This, ref );
1742 return ref;
1745 static ULONG WINAPI ServerXMLHTTPRequest_Release(IServerXMLHTTPRequest *iface)
1747 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1748 ULONG ref = InterlockedDecrement( &This->ref );
1750 TRACE("(%p)->(%u)\n", This, ref );
1752 if ( ref == 0 )
1754 httprequest_release( &This->req );
1755 heap_free( This );
1758 return ref;
1761 static HRESULT WINAPI ServerXMLHTTPRequest_GetTypeInfoCount(IServerXMLHTTPRequest *iface, UINT *pctinfo)
1763 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1765 TRACE("(%p)->(%p)\n", This, pctinfo);
1766 *pctinfo = 1;
1768 return S_OK;
1771 static HRESULT WINAPI ServerXMLHTTPRequest_GetTypeInfo(IServerXMLHTTPRequest *iface, UINT iTInfo,
1772 LCID lcid, ITypeInfo **ppTInfo)
1774 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1776 TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
1778 return get_typeinfo(IServerXMLHTTPRequest_tid, ppTInfo);
1781 static HRESULT WINAPI ServerXMLHTTPRequest_GetIDsOfNames(IServerXMLHTTPRequest *iface, REFIID riid,
1782 LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
1784 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1785 ITypeInfo *typeinfo;
1786 HRESULT hr;
1788 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
1789 lcid, rgDispId);
1791 if(!rgszNames || cNames == 0 || !rgDispId)
1792 return E_INVALIDARG;
1794 hr = get_typeinfo(IServerXMLHTTPRequest_tid, &typeinfo);
1795 if(SUCCEEDED(hr))
1797 hr = ITypeInfo_GetIDsOfNames(typeinfo, rgszNames, cNames, rgDispId);
1798 ITypeInfo_Release(typeinfo);
1801 return hr;
1804 static HRESULT WINAPI ServerXMLHTTPRequest_Invoke(IServerXMLHTTPRequest *iface, DISPID dispIdMember, REFIID riid,
1805 LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult,
1806 EXCEPINFO *pExcepInfo, UINT *puArgErr)
1808 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1809 ITypeInfo *typeinfo;
1810 HRESULT hr;
1812 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
1813 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
1815 hr = get_typeinfo(IServerXMLHTTPRequest_tid, &typeinfo);
1816 if(SUCCEEDED(hr))
1818 hr = ITypeInfo_Invoke(typeinfo, &This->IServerXMLHTTPRequest_iface, dispIdMember, wFlags,
1819 pDispParams, pVarResult, pExcepInfo, puArgErr);
1820 ITypeInfo_Release(typeinfo);
1823 return hr;
1826 static HRESULT WINAPI ServerXMLHTTPRequest_open(IServerXMLHTTPRequest *iface, BSTR method, BSTR url,
1827 VARIANT async, VARIANT user, VARIANT password)
1829 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1830 TRACE("(%p)->(%s %s %s)\n", This, debugstr_w(method), debugstr_w(url),
1831 debugstr_variant(&async));
1832 return httprequest_open(&This->req, method, url, async, user, password);
1835 static HRESULT WINAPI ServerXMLHTTPRequest_setRequestHeader(IServerXMLHTTPRequest *iface, BSTR header, BSTR value)
1837 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1838 TRACE("(%p)->(%s %s)\n", This, debugstr_w(header), debugstr_w(value));
1839 return httprequest_setRequestHeader(&This->req, header, value);
1842 static HRESULT WINAPI ServerXMLHTTPRequest_getResponseHeader(IServerXMLHTTPRequest *iface, BSTR header, BSTR *value)
1844 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1845 TRACE("(%p)->(%s %p)\n", This, debugstr_w(header), value);
1846 return httprequest_getResponseHeader(&This->req, header, value);
1849 static HRESULT WINAPI ServerXMLHTTPRequest_getAllResponseHeaders(IServerXMLHTTPRequest *iface, BSTR *respheaders)
1851 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1852 TRACE("(%p)->(%p)\n", This, respheaders);
1853 return httprequest_getAllResponseHeaders(&This->req, respheaders);
1856 static HRESULT WINAPI ServerXMLHTTPRequest_send(IServerXMLHTTPRequest *iface, VARIANT body)
1858 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1859 TRACE("(%p)->(%s)\n", This, debugstr_variant(&body));
1860 return httprequest_send(&This->req, body);
1863 static HRESULT WINAPI ServerXMLHTTPRequest_abort(IServerXMLHTTPRequest *iface)
1865 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1866 TRACE("(%p)\n", This);
1867 return httprequest_abort(&This->req);
1870 static HRESULT WINAPI ServerXMLHTTPRequest_get_status(IServerXMLHTTPRequest *iface, LONG *status)
1872 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1873 TRACE("(%p)->(%p)\n", This, status);
1874 return httprequest_get_status(&This->req, status);
1877 static HRESULT WINAPI ServerXMLHTTPRequest_get_statusText(IServerXMLHTTPRequest *iface, BSTR *status)
1879 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1880 TRACE("(%p)->(%p)\n", This, status);
1881 return httprequest_get_statusText(&This->req, status);
1884 static HRESULT WINAPI ServerXMLHTTPRequest_get_responseXML(IServerXMLHTTPRequest *iface, IDispatch **body)
1886 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1887 TRACE("(%p)->(%p)\n", This, body);
1888 return httprequest_get_responseXML(&This->req, body);
1891 static HRESULT WINAPI ServerXMLHTTPRequest_get_responseText(IServerXMLHTTPRequest *iface, BSTR *body)
1893 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1894 TRACE("(%p)->(%p)\n", This, body);
1895 return httprequest_get_responseText(&This->req, body);
1898 static HRESULT WINAPI ServerXMLHTTPRequest_get_responseBody(IServerXMLHTTPRequest *iface, VARIANT *body)
1900 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1901 TRACE("(%p)->(%p)\n", This, body);
1902 return httprequest_get_responseBody(&This->req, body);
1905 static HRESULT WINAPI ServerXMLHTTPRequest_get_responseStream(IServerXMLHTTPRequest *iface, VARIANT *body)
1907 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1908 TRACE("(%p)->(%p)\n", This, body);
1909 return httprequest_get_responseStream(&This->req, body);
1912 static HRESULT WINAPI ServerXMLHTTPRequest_get_readyState(IServerXMLHTTPRequest *iface, LONG *state)
1914 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1915 TRACE("(%p)->(%p)\n", This, state);
1916 return httprequest_get_readyState(&This->req, state);
1919 static HRESULT WINAPI ServerXMLHTTPRequest_put_onreadystatechange(IServerXMLHTTPRequest *iface, IDispatch *sink)
1921 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1922 TRACE("(%p)->(%p)\n", This, sink);
1923 return httprequest_put_onreadystatechange(&This->req, sink);
1926 static HRESULT WINAPI ServerXMLHTTPRequest_setTimeouts(IServerXMLHTTPRequest *iface, LONG resolveTimeout, LONG connectTimeout,
1927 LONG sendTimeout, LONG receiveTimeout)
1929 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1930 FIXME("(%p)->(%d %d %d %d): stub\n", This, resolveTimeout, connectTimeout, sendTimeout, receiveTimeout);
1931 return E_NOTIMPL;
1934 static HRESULT WINAPI ServerXMLHTTPRequest_waitForResponse(IServerXMLHTTPRequest *iface, VARIANT timeout, VARIANT_BOOL *isSuccessful)
1936 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1937 FIXME("(%p)->(%s %p): stub\n", This, debugstr_variant(&timeout), isSuccessful);
1938 return E_NOTIMPL;
1941 static HRESULT WINAPI ServerXMLHTTPRequest_getOption(IServerXMLHTTPRequest *iface, SERVERXMLHTTP_OPTION option, VARIANT *value)
1943 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1944 FIXME("(%p)->(%d %p): stub\n", This, option, value);
1945 return E_NOTIMPL;
1948 static HRESULT WINAPI ServerXMLHTTPRequest_setOption(IServerXMLHTTPRequest *iface, SERVERXMLHTTP_OPTION option, VARIANT value)
1950 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1951 FIXME("(%p)->(%d %s): stub\n", This, option, debugstr_variant(&value));
1952 return E_NOTIMPL;
1955 static const struct IServerXMLHTTPRequestVtbl ServerXMLHTTPRequestVtbl =
1957 ServerXMLHTTPRequest_QueryInterface,
1958 ServerXMLHTTPRequest_AddRef,
1959 ServerXMLHTTPRequest_Release,
1960 ServerXMLHTTPRequest_GetTypeInfoCount,
1961 ServerXMLHTTPRequest_GetTypeInfo,
1962 ServerXMLHTTPRequest_GetIDsOfNames,
1963 ServerXMLHTTPRequest_Invoke,
1964 ServerXMLHTTPRequest_open,
1965 ServerXMLHTTPRequest_setRequestHeader,
1966 ServerXMLHTTPRequest_getResponseHeader,
1967 ServerXMLHTTPRequest_getAllResponseHeaders,
1968 ServerXMLHTTPRequest_send,
1969 ServerXMLHTTPRequest_abort,
1970 ServerXMLHTTPRequest_get_status,
1971 ServerXMLHTTPRequest_get_statusText,
1972 ServerXMLHTTPRequest_get_responseXML,
1973 ServerXMLHTTPRequest_get_responseText,
1974 ServerXMLHTTPRequest_get_responseBody,
1975 ServerXMLHTTPRequest_get_responseStream,
1976 ServerXMLHTTPRequest_get_readyState,
1977 ServerXMLHTTPRequest_put_onreadystatechange,
1978 ServerXMLHTTPRequest_setTimeouts,
1979 ServerXMLHTTPRequest_waitForResponse,
1980 ServerXMLHTTPRequest_getOption,
1981 ServerXMLHTTPRequest_setOption
1984 static void init_httprequest(httprequest *req)
1986 req->IXMLHTTPRequest_iface.lpVtbl = &XMLHTTPRequestVtbl;
1987 req->IObjectWithSite_iface.lpVtbl = &ObjectWithSiteVtbl;
1988 req->IObjectSafety_iface.lpVtbl = &ObjectSafetyVtbl;
1989 req->ref = 1;
1991 req->async = FALSE;
1992 req->verb = -1;
1993 req->custom = NULL;
1994 req->uri = req->base_uri = NULL;
1995 req->user = req->password = NULL;
1997 req->state = READYSTATE_UNINITIALIZED;
1998 req->sink = NULL;
2000 req->bsc = NULL;
2001 req->status = 0;
2002 req->status_text = NULL;
2003 req->reqheader_size = 0;
2004 req->raw_respheaders = NULL;
2005 req->use_utf8_content = FALSE;
2007 list_init(&req->reqheaders);
2008 list_init(&req->respheaders);
2010 req->site = NULL;
2011 req->safeopt = 0;
2014 HRESULT XMLHTTPRequest_create(void **obj)
2016 httprequest *req;
2018 TRACE("(%p)\n", obj);
2020 req = heap_alloc( sizeof (*req) );
2021 if( !req )
2022 return E_OUTOFMEMORY;
2024 init_httprequest(req);
2025 *obj = &req->IXMLHTTPRequest_iface;
2027 TRACE("returning iface %p\n", *obj);
2029 return S_OK;
2032 HRESULT ServerXMLHTTP_create(void **obj)
2034 serverhttp *req;
2036 TRACE("(%p)\n", obj);
2038 req = heap_alloc( sizeof (*req) );
2039 if( !req )
2040 return E_OUTOFMEMORY;
2042 init_httprequest(&req->req);
2043 req->IServerXMLHTTPRequest_iface.lpVtbl = &ServerXMLHTTPRequestVtbl;
2044 req->ref = 1;
2046 *obj = &req->IServerXMLHTTPRequest_iface;
2048 TRACE("returning iface %p\n", *obj);
2050 return S_OK;
2053 #else
2055 HRESULT XMLHTTPRequest_create(void **ppObj)
2057 MESSAGE("This program tried to use a XMLHTTPRequest object, but\n"
2058 "libxml2 support was not present at compile time.\n");
2059 return E_NOTIMPL;
2062 HRESULT ServerXMLHTTP_create(void **obj)
2064 MESSAGE("This program tried to use a ServerXMLHTTP object, but\n"
2065 "libxml2 support was not present at compile time.\n");
2066 return E_NOTIMPL;
2069 #endif