d3d10core: Implement d3d10_device_OMGetRenderTargets().
[wine.git] / dlls / msxml3 / httprequest.c
blobcf53db4c7c04390ae1a5b75f0726ef5d2e3b9cdf
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 WINE_DEFAULT_DEBUG_CHANNEL(msxml);
53 #ifdef HAVE_LIBXML2
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 struct BindStatusCallback
181 IBindStatusCallback IBindStatusCallback_iface;
182 IHttpNegotiate IHttpNegotiate_iface;
183 IAuthenticate IAuthenticate_iface;
184 LONG ref;
186 IBinding *binding;
187 httprequest *request;
189 /* response data */
190 IStream *stream;
192 /* request body data */
193 HGLOBAL body;
196 static inline BindStatusCallback *impl_from_IBindStatusCallback( IBindStatusCallback *iface )
198 return CONTAINING_RECORD(iface, BindStatusCallback, IBindStatusCallback_iface);
201 static inline BindStatusCallback *impl_from_IHttpNegotiate( IHttpNegotiate *iface )
203 return CONTAINING_RECORD(iface, BindStatusCallback, IHttpNegotiate_iface);
206 static inline BindStatusCallback *impl_from_IAuthenticate( IAuthenticate *iface )
208 return CONTAINING_RECORD(iface, BindStatusCallback, IAuthenticate_iface);
211 static void BindStatusCallback_Detach(BindStatusCallback *bsc)
213 if (bsc)
215 if (bsc->binding) IBinding_Abort(bsc->binding);
216 bsc->request->bsc = NULL;
217 bsc->request = NULL;
218 IBindStatusCallback_Release(&bsc->IBindStatusCallback_iface);
222 static HRESULT WINAPI BindStatusCallback_QueryInterface(IBindStatusCallback *iface,
223 REFIID riid, void **ppv)
225 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
227 *ppv = NULL;
229 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
231 if (IsEqualGUID(&IID_IUnknown, riid) ||
232 IsEqualGUID(&IID_IBindStatusCallback, riid))
234 *ppv = &This->IBindStatusCallback_iface;
236 else if (IsEqualGUID(&IID_IHttpNegotiate, riid))
238 *ppv = &This->IHttpNegotiate_iface;
240 else if (IsEqualGUID(&IID_IAuthenticate, riid))
242 *ppv = &This->IAuthenticate_iface;
244 else if (IsEqualGUID(&IID_IServiceProvider, riid) ||
245 IsEqualGUID(&IID_IBindStatusCallbackEx, riid) ||
246 IsEqualGUID(&IID_IInternetProtocol, riid) ||
247 IsEqualGUID(&IID_IHttpNegotiate2, riid))
249 return E_NOINTERFACE;
252 if (*ppv)
254 IBindStatusCallback_AddRef(iface);
255 return S_OK;
258 FIXME("Unsupported riid = %s\n", debugstr_guid(riid));
260 return E_NOINTERFACE;
263 static ULONG WINAPI BindStatusCallback_AddRef(IBindStatusCallback *iface)
265 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
266 LONG ref = InterlockedIncrement(&This->ref);
268 TRACE("(%p) ref = %d\n", This, ref);
270 return ref;
273 static ULONG WINAPI BindStatusCallback_Release(IBindStatusCallback *iface)
275 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
276 LONG ref = InterlockedDecrement(&This->ref);
278 TRACE("(%p) ref = %d\n", This, ref);
280 if (!ref)
282 if (This->binding) IBinding_Release(This->binding);
283 if (This->stream) IStream_Release(This->stream);
284 if (This->body) GlobalFree(This->body);
285 heap_free(This);
288 return ref;
291 static HRESULT WINAPI BindStatusCallback_OnStartBinding(IBindStatusCallback *iface,
292 DWORD reserved, IBinding *pbind)
294 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
296 TRACE("(%p)->(%d %p)\n", This, reserved, pbind);
298 if (!pbind) return E_INVALIDARG;
300 This->binding = pbind;
301 IBinding_AddRef(pbind);
303 httprequest_setreadystate(This->request, READYSTATE_LOADED);
305 return CreateStreamOnHGlobal(NULL, TRUE, &This->stream);
308 static HRESULT WINAPI BindStatusCallback_GetPriority(IBindStatusCallback *iface, LONG *pPriority)
310 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
312 TRACE("(%p)->(%p)\n", This, pPriority);
314 return E_NOTIMPL;
317 static HRESULT WINAPI BindStatusCallback_OnLowResource(IBindStatusCallback *iface, DWORD reserved)
319 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
321 TRACE("(%p)->(%d)\n", This, reserved);
323 return E_NOTIMPL;
326 static HRESULT WINAPI BindStatusCallback_OnProgress(IBindStatusCallback *iface, ULONG ulProgress,
327 ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
329 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
331 TRACE("(%p)->(%u %u %u %s)\n", This, ulProgress, ulProgressMax, ulStatusCode,
332 debugstr_w(szStatusText));
334 return S_OK;
337 static HRESULT WINAPI BindStatusCallback_OnStopBinding(IBindStatusCallback *iface,
338 HRESULT hr, LPCWSTR error)
340 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
342 TRACE("(%p)->(0x%08x %s)\n", This, hr, debugstr_w(error));
344 if (This->binding)
346 IBinding_Release(This->binding);
347 This->binding = NULL;
350 if (hr == S_OK)
352 BindStatusCallback_Detach(This->request->bsc);
353 This->request->bsc = This;
354 httprequest_setreadystate(This->request, READYSTATE_COMPLETE);
357 return S_OK;
360 static HRESULT WINAPI BindStatusCallback_GetBindInfo(IBindStatusCallback *iface,
361 DWORD *bind_flags, BINDINFO *pbindinfo)
363 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
365 TRACE("(%p)->(%p %p)\n", This, bind_flags, pbindinfo);
367 *bind_flags = 0;
368 if (This->request->async) *bind_flags |= BINDF_ASYNCHRONOUS;
370 if (This->request->verb != BINDVERB_GET && This->body)
372 pbindinfo->stgmedData.tymed = TYMED_HGLOBAL;
373 pbindinfo->stgmedData.u.hGlobal = This->body;
374 pbindinfo->cbstgmedData = GlobalSize(This->body);
375 /* callback owns passed body pointer */
376 IBindStatusCallback_QueryInterface(iface, &IID_IUnknown, (void**)&pbindinfo->stgmedData.pUnkForRelease);
379 pbindinfo->dwBindVerb = This->request->verb;
380 if (This->request->verb == BINDVERB_CUSTOM)
382 pbindinfo->szCustomVerb = CoTaskMemAlloc(SysStringByteLen(This->request->custom));
383 strcpyW(pbindinfo->szCustomVerb, This->request->custom);
386 return S_OK;
389 static HRESULT WINAPI BindStatusCallback_OnDataAvailable(IBindStatusCallback *iface,
390 DWORD flags, DWORD size, FORMATETC *format, STGMEDIUM *stgmed)
392 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
393 DWORD read, written;
394 BYTE buf[4096];
395 HRESULT hr;
397 TRACE("(%p)->(%08x %d %p %p)\n", This, flags, size, format, stgmed);
401 hr = IStream_Read(stgmed->u.pstm, buf, sizeof(buf), &read);
402 if (hr != S_OK) break;
404 hr = IStream_Write(This->stream, buf, read, &written);
405 } while((hr == S_OK) && written != 0 && read != 0);
407 httprequest_setreadystate(This->request, READYSTATE_INTERACTIVE);
409 return S_OK;
412 static HRESULT WINAPI BindStatusCallback_OnObjectAvailable(IBindStatusCallback *iface,
413 REFIID riid, IUnknown *punk)
415 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
417 FIXME("(%p)->(%s %p): stub\n", This, debugstr_guid(riid), punk);
419 return E_NOTIMPL;
422 static const IBindStatusCallbackVtbl BindStatusCallbackVtbl = {
423 BindStatusCallback_QueryInterface,
424 BindStatusCallback_AddRef,
425 BindStatusCallback_Release,
426 BindStatusCallback_OnStartBinding,
427 BindStatusCallback_GetPriority,
428 BindStatusCallback_OnLowResource,
429 BindStatusCallback_OnProgress,
430 BindStatusCallback_OnStopBinding,
431 BindStatusCallback_GetBindInfo,
432 BindStatusCallback_OnDataAvailable,
433 BindStatusCallback_OnObjectAvailable
436 static HRESULT WINAPI BSCHttpNegotiate_QueryInterface(IHttpNegotiate *iface,
437 REFIID riid, void **ppv)
439 BindStatusCallback *This = impl_from_IHttpNegotiate(iface);
440 return IBindStatusCallback_QueryInterface(&This->IBindStatusCallback_iface, riid, ppv);
443 static ULONG WINAPI BSCHttpNegotiate_AddRef(IHttpNegotiate *iface)
445 BindStatusCallback *This = impl_from_IHttpNegotiate(iface);
446 return IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
449 static ULONG WINAPI BSCHttpNegotiate_Release(IHttpNegotiate *iface)
451 BindStatusCallback *This = impl_from_IHttpNegotiate(iface);
452 return IBindStatusCallback_Release(&This->IBindStatusCallback_iface);
455 static HRESULT WINAPI BSCHttpNegotiate_BeginningTransaction(IHttpNegotiate *iface,
456 LPCWSTR url, LPCWSTR headers, DWORD reserved, LPWSTR *add_headers)
458 static const WCHAR content_type_utf8W[] = {'C','o','n','t','e','n','t','-','T','y','p','e',':',' ',
459 't','e','x','t','/','p','l','a','i','n',';','c','h','a','r','s','e','t','=','u','t','f','-','8','\r','\n',0};
460 static const WCHAR refererW[] = {'R','e','f','e','r','e','r',':',' ',0};
462 BindStatusCallback *This = impl_from_IHttpNegotiate(iface);
463 const struct httpheader *entry;
464 BSTR base_uri = NULL;
465 WCHAR *buff, *ptr;
466 int size = 0;
468 TRACE("(%p)->(%s %s %d %p)\n", This, debugstr_w(url), debugstr_w(headers), reserved, add_headers);
470 *add_headers = NULL;
472 if (This->request->use_utf8_content)
473 size = sizeof(content_type_utf8W);
475 if (!list_empty(&This->request->reqheaders))
476 size += This->request->reqheader_size*sizeof(WCHAR);
478 if (This->request->base_uri)
480 IUri_GetRawUri(This->request->base_uri, &base_uri);
481 size += SysStringLen(base_uri)*sizeof(WCHAR) + sizeof(refererW) + sizeof(crlfW);
484 if (!size)
486 SysFreeString(base_uri);
487 return S_OK;
490 buff = CoTaskMemAlloc(size);
491 if (!buff)
493 SysFreeString(base_uri);
494 return E_OUTOFMEMORY;
497 ptr = buff;
498 if (This->request->use_utf8_content)
500 lstrcpyW(ptr, content_type_utf8W);
501 ptr += sizeof(content_type_utf8W)/sizeof(WCHAR)-1;
504 if (base_uri)
506 strcpyW(ptr, refererW);
507 strcatW(ptr, base_uri);
508 strcatW(ptr, crlfW);
509 ptr += strlenW(refererW) + SysStringLen(base_uri) + strlenW(crlfW);
510 SysFreeString(base_uri);
513 /* user headers */
514 LIST_FOR_EACH_ENTRY(entry, &This->request->reqheaders, struct httpheader, entry)
516 lstrcpyW(ptr, entry->header);
517 ptr += SysStringLen(entry->header);
519 lstrcpyW(ptr, colspaceW);
520 ptr += sizeof(colspaceW)/sizeof(WCHAR)-1;
522 lstrcpyW(ptr, entry->value);
523 ptr += SysStringLen(entry->value);
525 lstrcpyW(ptr, crlfW);
526 ptr += sizeof(crlfW)/sizeof(WCHAR)-1;
529 *add_headers = buff;
531 return S_OK;
534 static void add_response_header(httprequest *This, const WCHAR *data, int len)
536 struct httpheader *entry;
537 const WCHAR *ptr = data;
538 BSTR header, value;
540 while (*ptr)
542 if (*ptr == ':')
544 header = SysAllocStringLen(data, ptr-data);
545 /* skip leading spaces for a value */
546 while (*++ptr == ' ')
548 value = SysAllocStringLen(ptr, len-(ptr-data));
549 break;
551 ptr++;
554 if (!*ptr) return;
556 /* new header */
557 TRACE("got header %s:%s\n", debugstr_w(header), debugstr_w(value));
559 entry = heap_alloc(sizeof(*entry));
560 entry->header = header;
561 entry->value = value;
562 list_add_head(&This->respheaders, &entry->entry);
565 static HRESULT WINAPI BSCHttpNegotiate_OnResponse(IHttpNegotiate *iface, DWORD code,
566 LPCWSTR resp_headers, LPCWSTR req_headers, LPWSTR *add_reqheaders)
568 BindStatusCallback *This = impl_from_IHttpNegotiate(iface);
570 TRACE("(%p)->(%d %s %s %p)\n", This, code, debugstr_w(resp_headers),
571 debugstr_w(req_headers), add_reqheaders);
573 This->request->status = code;
574 /* store headers and status text */
575 free_response_headers(This->request);
576 SysFreeString(This->request->status_text);
577 This->request->status_text = NULL;
578 if (resp_headers)
580 const WCHAR *ptr, *line, *status_text;
582 ptr = line = resp_headers;
584 /* skip HTTP-Version */
585 ptr = strchrW(ptr, ' ');
586 if (ptr)
588 /* skip Status-Code */
589 ptr = strchrW(++ptr, ' ');
590 if (ptr)
592 status_text = ++ptr;
593 /* now it supposed to end with CRLF */
594 while (*ptr)
596 if (*ptr == '\r' && *(ptr+1) == '\n')
598 line = ptr + 2;
599 This->request->status_text = SysAllocStringLen(status_text, ptr-status_text);
600 TRACE("status text %s\n", debugstr_w(This->request->status_text));
601 break;
603 ptr++;
608 /* store as unparsed string for now */
609 This->request->raw_respheaders = SysAllocString(line);
612 return S_OK;
615 static const IHttpNegotiateVtbl BSCHttpNegotiateVtbl = {
616 BSCHttpNegotiate_QueryInterface,
617 BSCHttpNegotiate_AddRef,
618 BSCHttpNegotiate_Release,
619 BSCHttpNegotiate_BeginningTransaction,
620 BSCHttpNegotiate_OnResponse
623 static HRESULT WINAPI Authenticate_QueryInterface(IAuthenticate *iface,
624 REFIID riid, void **ppv)
626 BindStatusCallback *This = impl_from_IAuthenticate(iface);
627 return IBindStatusCallback_QueryInterface(&This->IBindStatusCallback_iface, riid, ppv);
630 static ULONG WINAPI Authenticate_AddRef(IAuthenticate *iface)
632 BindStatusCallback *This = impl_from_IAuthenticate(iface);
633 return IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
636 static ULONG WINAPI Authenticate_Release(IAuthenticate *iface)
638 BindStatusCallback *This = impl_from_IAuthenticate(iface);
639 return IBindStatusCallback_Release(&This->IBindStatusCallback_iface);
642 static HRESULT WINAPI Authenticate_Authenticate(IAuthenticate *iface,
643 HWND *hwnd, LPWSTR *username, LPWSTR *password)
645 BindStatusCallback *This = impl_from_IAuthenticate(iface);
646 httprequest *request = This->request;
648 TRACE("(%p)->(%p %p %p)\n", This, hwnd, username, password);
650 if (request->user && *request->user)
652 if (hwnd) *hwnd = NULL;
653 *username = CoTaskMemAlloc(SysStringByteLen(request->user)+sizeof(WCHAR));
654 *password = CoTaskMemAlloc(SysStringByteLen(request->password)+sizeof(WCHAR));
655 if (!*username || !*password)
657 CoTaskMemFree(*username);
658 CoTaskMemFree(*password);
659 return E_OUTOFMEMORY;
662 memcpy(*username, request->user, SysStringByteLen(request->user)+sizeof(WCHAR));
663 memcpy(*password, request->password, SysStringByteLen(request->password)+sizeof(WCHAR));
666 return S_OK;
669 static const IAuthenticateVtbl AuthenticateVtbl = {
670 Authenticate_QueryInterface,
671 Authenticate_AddRef,
672 Authenticate_Release,
673 Authenticate_Authenticate
676 static HRESULT BindStatusCallback_create(httprequest* This, BindStatusCallback **obj, const VARIANT *body)
678 BindStatusCallback *bsc;
679 IBindCtx *pbc;
680 HRESULT hr;
681 int size;
683 hr = CreateBindCtx(0, &pbc);
684 if (hr != S_OK) return hr;
686 bsc = heap_alloc(sizeof(*bsc));
687 if (!bsc)
689 IBindCtx_Release(pbc);
690 return E_OUTOFMEMORY;
693 bsc->IBindStatusCallback_iface.lpVtbl = &BindStatusCallbackVtbl;
694 bsc->IHttpNegotiate_iface.lpVtbl = &BSCHttpNegotiateVtbl;
695 bsc->IAuthenticate_iface.lpVtbl = &AuthenticateVtbl;
696 bsc->ref = 1;
697 bsc->request = This;
698 bsc->binding = NULL;
699 bsc->stream = NULL;
700 bsc->body = NULL;
702 TRACE("(%p)->(%p)\n", This, bsc);
704 This->use_utf8_content = FALSE;
706 if (This->verb != BINDVERB_GET)
708 void *send_data, *ptr;
709 SAFEARRAY *sa = NULL;
711 if (V_VT(body) == (VT_VARIANT|VT_BYREF))
712 body = V_VARIANTREF(body);
714 switch (V_VT(body))
716 case VT_BSTR:
718 int len = SysStringLen(V_BSTR(body));
719 const WCHAR *str = V_BSTR(body);
720 UINT i, cp = CP_ACP;
722 for (i = 0; i < len; i++)
724 if (str[i] > 127)
726 cp = CP_UTF8;
727 break;
731 size = WideCharToMultiByte(cp, 0, str, len, NULL, 0, NULL, NULL);
732 if (!(ptr = heap_alloc(size)))
734 heap_free(bsc);
735 return E_OUTOFMEMORY;
737 WideCharToMultiByte(cp, 0, str, len, ptr, size, NULL, NULL);
738 if (cp == CP_UTF8) This->use_utf8_content = TRUE;
739 break;
741 case VT_ARRAY|VT_UI1:
743 sa = V_ARRAY(body);
744 if ((hr = SafeArrayAccessData(sa, (void **)&ptr)) != S_OK)
746 heap_free(bsc);
747 return hr;
749 if ((hr = SafeArrayGetUBound(sa, 1, &size)) != S_OK)
751 SafeArrayUnaccessData(sa);
752 heap_free(bsc);
753 return hr;
755 size++;
756 break;
758 default:
759 FIXME("unsupported body data type %d\n", V_VT(body));
760 /* fall through */
761 case VT_EMPTY:
762 case VT_ERROR:
763 case VT_NULL:
764 ptr = NULL;
765 size = 0;
766 break;
769 if (size)
771 bsc->body = GlobalAlloc(GMEM_FIXED, size);
772 if (!bsc->body)
774 if (V_VT(body) == VT_BSTR)
775 heap_free(ptr);
776 else if (V_VT(body) == (VT_ARRAY|VT_UI1))
777 SafeArrayUnaccessData(sa);
779 heap_free(bsc);
780 return E_OUTOFMEMORY;
783 send_data = GlobalLock(bsc->body);
784 memcpy(send_data, ptr, size);
785 GlobalUnlock(bsc->body);
788 if (V_VT(body) == VT_BSTR)
789 heap_free(ptr);
790 else if (V_VT(body) == (VT_ARRAY|VT_UI1))
791 SafeArrayUnaccessData(sa);
794 hr = RegisterBindStatusCallback(pbc, &bsc->IBindStatusCallback_iface, NULL, 0);
795 if (hr == S_OK)
797 IMoniker *moniker;
799 hr = CreateURLMonikerEx2(NULL, This->uri, &moniker, URL_MK_UNIFORM);
800 if (hr == S_OK)
802 IStream *stream;
804 hr = IMoniker_BindToStorage(moniker, pbc, NULL, &IID_IStream, (void**)&stream);
805 IMoniker_Release(moniker);
806 if (stream) IStream_Release(stream);
808 IBindCtx_Release(pbc);
811 if (FAILED(hr))
813 IBindStatusCallback_Release(&bsc->IBindStatusCallback_iface);
814 bsc = NULL;
817 *obj = bsc;
818 return hr;
821 static HRESULT verify_uri(httprequest *This, IUri *uri)
823 DWORD scheme, base_scheme;
824 BSTR host, base_host;
825 HRESULT hr;
827 if(!(This->safeopt & INTERFACESAFE_FOR_UNTRUSTED_DATA))
828 return S_OK;
830 if(!This->base_uri)
831 return E_ACCESSDENIED;
833 hr = IUri_GetScheme(uri, &scheme);
834 if(FAILED(hr))
835 return hr;
837 hr = IUri_GetScheme(This->base_uri, &base_scheme);
838 if(FAILED(hr))
839 return hr;
841 if(scheme != base_scheme) {
842 WARN("Schemes don't match\n");
843 return E_ACCESSDENIED;
846 if(scheme == INTERNET_SCHEME_UNKNOWN) {
847 FIXME("Unknown scheme\n");
848 return E_ACCESSDENIED;
851 hr = IUri_GetHost(uri, &host);
852 if(FAILED(hr))
853 return hr;
855 hr = IUri_GetHost(This->base_uri, &base_host);
856 if(SUCCEEDED(hr)) {
857 if(strcmpiW(host, base_host)) {
858 WARN("Hosts don't match\n");
859 hr = E_ACCESSDENIED;
861 SysFreeString(base_host);
864 SysFreeString(host);
865 return hr;
868 static HRESULT httprequest_open(httprequest *This, BSTR method, BSTR url,
869 VARIANT async, VARIANT user, VARIANT password)
871 static const WCHAR MethodGetW[] = {'G','E','T',0};
872 static const WCHAR MethodPutW[] = {'P','U','T',0};
873 static const WCHAR MethodPostW[] = {'P','O','S','T',0};
874 static const WCHAR MethodDeleteW[] = {'D','E','L','E','T','E',0};
875 static const WCHAR MethodPropFindW[] = {'P','R','O','P','F','I','N','D',0};
876 VARIANT str, is_async;
877 IUri *uri;
878 HRESULT hr;
880 if (!method || !url) return E_INVALIDARG;
882 /* free previously set data */
883 if(This->uri) {
884 IUri_Release(This->uri);
885 This->uri = NULL;
888 SysFreeString(This->user);
889 SysFreeString(This->password);
890 This->user = This->password = NULL;
892 if (!strcmpiW(method, MethodGetW))
894 This->verb = BINDVERB_GET;
896 else if (!strcmpiW(method, MethodPutW))
898 This->verb = BINDVERB_PUT;
900 else if (!strcmpiW(method, MethodPostW))
902 This->verb = BINDVERB_POST;
904 else if (!strcmpiW(method, MethodDeleteW) ||
905 !strcmpiW(method, MethodPropFindW))
907 This->verb = BINDVERB_CUSTOM;
908 SysReAllocString(&This->custom, method);
910 else
912 FIXME("unsupported request type %s\n", debugstr_w(method));
913 This->verb = -1;
914 return E_FAIL;
917 if(This->base_uri)
918 hr = CoInternetCombineUrlEx(This->base_uri, url, 0, &uri, 0);
919 else
920 hr = CreateUri(url, 0, 0, &uri);
921 if(FAILED(hr)) {
922 WARN("Could not create IUri object: %08x\n", hr);
923 return hr;
926 hr = verify_uri(This, uri);
927 if(FAILED(hr)) {
928 IUri_Release(uri);
929 return hr;
932 VariantInit(&str);
933 hr = VariantChangeType(&str, &user, 0, VT_BSTR);
934 if (hr == S_OK)
935 This->user = V_BSTR(&str);
937 VariantInit(&str);
938 hr = VariantChangeType(&str, &password, 0, VT_BSTR);
939 if (hr == S_OK)
940 This->password = V_BSTR(&str);
942 /* add authentication info */
943 if (This->user && *This->user)
945 IUriBuilder *builder;
947 hr = CreateIUriBuilder(uri, 0, 0, &builder);
948 if (hr == S_OK)
950 IUri *full_uri;
952 IUriBuilder_SetUserName(builder, This->user);
953 IUriBuilder_SetPassword(builder, This->password);
954 hr = IUriBuilder_CreateUri(builder, -1, 0, 0, &full_uri);
955 if (hr == S_OK)
957 IUri_Release(uri);
958 uri = full_uri;
960 else
961 WARN("failed to create modified uri, 0x%08x\n", hr);
962 IUriBuilder_Release(builder);
964 else
965 WARN("IUriBuilder creation failed, 0x%08x\n", hr);
968 This->uri = uri;
970 VariantInit(&is_async);
971 hr = VariantChangeType(&is_async, &async, 0, VT_BOOL);
972 This->async = hr == S_OK && V_BOOL(&is_async);
974 httprequest_setreadystate(This, READYSTATE_LOADING);
976 return S_OK;
979 static HRESULT httprequest_setRequestHeader(httprequest *This, BSTR header, BSTR value)
981 struct httpheader *entry;
983 if (!header || !*header) return E_INVALIDARG;
984 if (This->state != READYSTATE_LOADING) return E_FAIL;
985 if (!value) return E_INVALIDARG;
987 /* replace existing header value if already added */
988 LIST_FOR_EACH_ENTRY(entry, &This->reqheaders, struct httpheader, entry)
990 if (lstrcmpW(entry->header, header) == 0)
992 LONG length = SysStringLen(entry->value);
993 HRESULT hr;
995 hr = SysReAllocString(&entry->value, value) ? S_OK : E_OUTOFMEMORY;
997 if (hr == S_OK)
998 This->reqheader_size += (SysStringLen(entry->value) - length);
1000 return hr;
1004 entry = heap_alloc(sizeof(*entry));
1005 if (!entry) return E_OUTOFMEMORY;
1007 /* new header */
1008 entry->header = SysAllocString(header);
1009 entry->value = SysAllocString(value);
1011 /* header length including null terminator */
1012 This->reqheader_size += SysStringLen(entry->header) + sizeof(colspaceW)/sizeof(WCHAR) +
1013 SysStringLen(entry->value) + sizeof(crlfW)/sizeof(WCHAR) - 1;
1015 list_add_head(&This->reqheaders, &entry->entry);
1017 return S_OK;
1020 static HRESULT httprequest_getResponseHeader(httprequest *This, BSTR header, BSTR *value)
1022 struct httpheader *entry;
1024 if (!header) return E_INVALIDARG;
1025 if (!value) return E_POINTER;
1027 if (This->raw_respheaders && list_empty(&This->respheaders))
1029 WCHAR *ptr, *line;
1031 ptr = line = This->raw_respheaders;
1032 while (*ptr)
1034 if (*ptr == '\r' && *(ptr+1) == '\n')
1036 add_response_header(This, line, ptr-line);
1037 ptr++; line = ++ptr;
1038 continue;
1040 ptr++;
1044 LIST_FOR_EACH_ENTRY(entry, &This->respheaders, struct httpheader, entry)
1046 if (!strcmpiW(entry->header, header))
1048 *value = SysAllocString(entry->value);
1049 TRACE("header value %s\n", debugstr_w(*value));
1050 return S_OK;
1054 return S_FALSE;
1057 static HRESULT httprequest_getAllResponseHeaders(httprequest *This, BSTR *respheaders)
1059 if (!respheaders) return E_POINTER;
1061 *respheaders = SysAllocString(This->raw_respheaders);
1063 return S_OK;
1066 static HRESULT httprequest_send(httprequest *This, VARIANT body)
1068 BindStatusCallback *bsc = NULL;
1069 HRESULT hr;
1071 if (This->state != READYSTATE_LOADING) return E_FAIL;
1073 hr = BindStatusCallback_create(This, &bsc, &body);
1074 if (FAILED(hr))
1075 /* success path to detach it is OnStopBinding call */
1076 BindStatusCallback_Detach(bsc);
1078 return hr;
1081 static HRESULT httprequest_abort(httprequest *This)
1083 BindStatusCallback_Detach(This->bsc);
1085 httprequest_setreadystate(This, READYSTATE_UNINITIALIZED);
1087 return S_OK;
1090 static HRESULT httprequest_get_status(httprequest *This, LONG *status)
1092 if (!status) return E_POINTER;
1094 *status = This->status;
1096 return This->state == READYSTATE_COMPLETE ? S_OK : E_FAIL;
1099 static HRESULT httprequest_get_statusText(httprequest *This, BSTR *status)
1101 if (!status) return E_POINTER;
1102 if (This->state != READYSTATE_COMPLETE) return E_FAIL;
1104 *status = SysAllocString(This->status_text);
1106 return S_OK;
1109 static HRESULT httprequest_get_responseText(httprequest *This, BSTR *body)
1111 HGLOBAL hglobal;
1112 HRESULT hr;
1114 if (!body) return E_POINTER;
1115 if (This->state != READYSTATE_COMPLETE) return E_FAIL;
1117 hr = GetHGlobalFromStream(This->bsc->stream, &hglobal);
1118 if (hr == S_OK)
1120 xmlChar *ptr = GlobalLock(hglobal);
1121 DWORD size = GlobalSize(hglobal);
1122 xmlCharEncoding encoding = XML_CHAR_ENCODING_UTF8;
1124 /* try to determine data encoding */
1125 if (size >= 4)
1127 encoding = xmlDetectCharEncoding(ptr, 4);
1128 TRACE("detected encoding: %s\n", debugstr_a(xmlGetCharEncodingName(encoding)));
1129 if ( encoding != XML_CHAR_ENCODING_UTF8 &&
1130 encoding != XML_CHAR_ENCODING_UTF16LE &&
1131 encoding != XML_CHAR_ENCODING_NONE )
1133 FIXME("unsupported encoding: %s\n", debugstr_a(xmlGetCharEncodingName(encoding)));
1134 GlobalUnlock(hglobal);
1135 return E_FAIL;
1139 /* without BOM assume UTF-8 */
1140 if (encoding == XML_CHAR_ENCODING_UTF8 ||
1141 encoding == XML_CHAR_ENCODING_NONE )
1143 DWORD length = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)ptr, size, NULL, 0);
1145 *body = SysAllocStringLen(NULL, length);
1146 if (*body)
1147 MultiByteToWideChar( CP_UTF8, 0, (LPCSTR)ptr, size, *body, length);
1149 else
1150 *body = SysAllocStringByteLen((LPCSTR)ptr, size);
1152 if (!*body) hr = E_OUTOFMEMORY;
1153 GlobalUnlock(hglobal);
1156 return hr;
1159 static HRESULT httprequest_get_responseXML(httprequest *This, IDispatch **body)
1161 IXMLDOMDocument3 *doc;
1162 HRESULT hr;
1163 BSTR str;
1165 if (!body) return E_INVALIDARG;
1166 if (This->state != READYSTATE_COMPLETE) return E_FAIL;
1168 hr = DOMDocument_create(MSXML_DEFAULT, (void**)&doc);
1169 if (hr != S_OK) return hr;
1171 hr = httprequest_get_responseText(This, &str);
1172 if (hr == S_OK)
1174 VARIANT_BOOL ok;
1176 hr = IXMLDOMDocument3_loadXML(doc, str, &ok);
1177 SysFreeString(str);
1180 IXMLDOMDocument3_QueryInterface(doc, &IID_IDispatch, (void**)body);
1181 IXMLDOMDocument3_Release(doc);
1183 return hr;
1186 static HRESULT httprequest_get_responseBody(httprequest *This, VARIANT *body)
1188 HGLOBAL hglobal;
1189 HRESULT hr;
1191 if (!body) return E_INVALIDARG;
1192 V_VT(body) = VT_EMPTY;
1194 if (This->state != READYSTATE_COMPLETE) return E_PENDING;
1196 hr = GetHGlobalFromStream(This->bsc->stream, &hglobal);
1197 if (hr == S_OK)
1199 void *ptr = GlobalLock(hglobal);
1200 DWORD size = GlobalSize(hglobal);
1202 SAFEARRAYBOUND bound;
1203 SAFEARRAY *array;
1205 bound.lLbound = 0;
1206 bound.cElements = size;
1207 array = SafeArrayCreate(VT_UI1, 1, &bound);
1209 if (array)
1211 void *dest;
1213 V_VT(body) = VT_ARRAY | VT_UI1;
1214 V_ARRAY(body) = array;
1216 hr = SafeArrayAccessData(array, &dest);
1217 if (hr == S_OK)
1219 memcpy(dest, ptr, size);
1220 SafeArrayUnaccessData(array);
1222 else
1224 VariantClear(body);
1227 else
1228 hr = E_FAIL;
1230 GlobalUnlock(hglobal);
1233 return hr;
1236 static HRESULT httprequest_get_responseStream(httprequest *This, VARIANT *body)
1238 LARGE_INTEGER move;
1239 IStream *stream;
1240 HRESULT hr;
1242 if (!body) return E_INVALIDARG;
1243 V_VT(body) = VT_EMPTY;
1245 if (This->state != READYSTATE_COMPLETE) return E_PENDING;
1247 hr = IStream_Clone(This->bsc->stream, &stream);
1249 move.QuadPart = 0;
1250 IStream_Seek(stream, move, STREAM_SEEK_SET, NULL);
1252 V_VT(body) = VT_UNKNOWN;
1253 V_UNKNOWN(body) = (IUnknown*)stream;
1255 return hr;
1258 static HRESULT httprequest_get_readyState(httprequest *This, LONG *state)
1260 if (!state) return E_POINTER;
1262 *state = This->state;
1263 return S_OK;
1266 static HRESULT httprequest_put_onreadystatechange(httprequest *This, IDispatch *sink)
1268 if (This->sink) IDispatch_Release(This->sink);
1269 if ((This->sink = sink)) IDispatch_AddRef(This->sink);
1271 return S_OK;
1274 static void httprequest_release(httprequest *This)
1276 struct httpheader *header, *header2;
1278 if (This->site)
1279 IUnknown_Release( This->site );
1280 if (This->uri)
1281 IUri_Release(This->uri);
1282 if (This->base_uri)
1283 IUri_Release(This->base_uri);
1285 SysFreeString(This->custom);
1286 SysFreeString(This->user);
1287 SysFreeString(This->password);
1289 /* request headers */
1290 LIST_FOR_EACH_ENTRY_SAFE(header, header2, &This->reqheaders, struct httpheader, entry)
1292 list_remove(&header->entry);
1293 SysFreeString(header->header);
1294 SysFreeString(header->value);
1295 heap_free(header);
1297 /* response headers */
1298 free_response_headers(This);
1299 SysFreeString(This->status_text);
1301 /* detach callback object */
1302 BindStatusCallback_Detach(This->bsc);
1304 if (This->sink) IDispatch_Release(This->sink);
1307 static HRESULT WINAPI XMLHTTPRequest_QueryInterface(IXMLHTTPRequest *iface, REFIID riid, void **ppvObject)
1309 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1310 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppvObject);
1312 if ( IsEqualGUID( riid, &IID_IXMLHTTPRequest) ||
1313 IsEqualGUID( riid, &IID_IDispatch) ||
1314 IsEqualGUID( riid, &IID_IUnknown) )
1316 *ppvObject = iface;
1318 else if (IsEqualGUID(&IID_IObjectWithSite, riid))
1320 *ppvObject = &This->IObjectWithSite_iface;
1322 else if (IsEqualGUID(&IID_IObjectSafety, riid))
1324 *ppvObject = &This->IObjectSafety_iface;
1326 else
1328 TRACE("Unsupported interface %s\n", debugstr_guid(riid));
1329 *ppvObject = NULL;
1330 return E_NOINTERFACE;
1333 IXMLHTTPRequest_AddRef( iface );
1335 return S_OK;
1338 static ULONG WINAPI XMLHTTPRequest_AddRef(IXMLHTTPRequest *iface)
1340 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1341 ULONG ref = InterlockedIncrement( &This->ref );
1342 TRACE("(%p)->(%u)\n", This, ref );
1343 return ref;
1346 static ULONG WINAPI XMLHTTPRequest_Release(IXMLHTTPRequest *iface)
1348 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1349 ULONG ref = InterlockedDecrement( &This->ref );
1351 TRACE("(%p)->(%u)\n", This, ref );
1353 if ( ref == 0 )
1355 httprequest_release( This );
1356 heap_free( This );
1359 return ref;
1362 static HRESULT WINAPI XMLHTTPRequest_GetTypeInfoCount(IXMLHTTPRequest *iface, UINT *pctinfo)
1364 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1366 TRACE("(%p)->(%p)\n", This, pctinfo);
1368 *pctinfo = 1;
1370 return S_OK;
1373 static HRESULT WINAPI XMLHTTPRequest_GetTypeInfo(IXMLHTTPRequest *iface, UINT iTInfo,
1374 LCID lcid, ITypeInfo **ppTInfo)
1376 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1378 TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
1380 return get_typeinfo(IXMLHTTPRequest_tid, ppTInfo);
1383 static HRESULT WINAPI XMLHTTPRequest_GetIDsOfNames(IXMLHTTPRequest *iface, REFIID riid,
1384 LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
1386 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1387 ITypeInfo *typeinfo;
1388 HRESULT hr;
1390 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
1391 lcid, rgDispId);
1393 if(!rgszNames || cNames == 0 || !rgDispId)
1394 return E_INVALIDARG;
1396 hr = get_typeinfo(IXMLHTTPRequest_tid, &typeinfo);
1397 if(SUCCEEDED(hr))
1399 hr = ITypeInfo_GetIDsOfNames(typeinfo, rgszNames, cNames, rgDispId);
1400 ITypeInfo_Release(typeinfo);
1403 return hr;
1406 static HRESULT WINAPI XMLHTTPRequest_Invoke(IXMLHTTPRequest *iface, DISPID dispIdMember, REFIID riid,
1407 LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult,
1408 EXCEPINFO *pExcepInfo, UINT *puArgErr)
1410 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1411 ITypeInfo *typeinfo;
1412 HRESULT hr;
1414 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
1415 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
1417 hr = get_typeinfo(IXMLHTTPRequest_tid, &typeinfo);
1418 if(SUCCEEDED(hr))
1420 hr = ITypeInfo_Invoke(typeinfo, &This->IXMLHTTPRequest_iface, dispIdMember, wFlags,
1421 pDispParams, pVarResult, pExcepInfo, puArgErr);
1422 ITypeInfo_Release(typeinfo);
1425 return hr;
1428 static HRESULT WINAPI XMLHTTPRequest_open(IXMLHTTPRequest *iface, BSTR method, BSTR url,
1429 VARIANT async, VARIANT user, VARIANT password)
1431 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1432 TRACE("(%p)->(%s %s %s)\n", This, debugstr_w(method), debugstr_w(url),
1433 debugstr_variant(&async));
1434 return httprequest_open(This, method, url, async, user, password);
1437 static HRESULT WINAPI XMLHTTPRequest_setRequestHeader(IXMLHTTPRequest *iface, BSTR header, BSTR value)
1439 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1440 TRACE("(%p)->(%s %s)\n", This, debugstr_w(header), debugstr_w(value));
1441 return httprequest_setRequestHeader(This, header, value);
1444 static HRESULT WINAPI XMLHTTPRequest_getResponseHeader(IXMLHTTPRequest *iface, BSTR header, BSTR *value)
1446 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1447 TRACE("(%p)->(%s %p)\n", This, debugstr_w(header), value);
1448 return httprequest_getResponseHeader(This, header, value);
1451 static HRESULT WINAPI XMLHTTPRequest_getAllResponseHeaders(IXMLHTTPRequest *iface, BSTR *respheaders)
1453 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1454 TRACE("(%p)->(%p)\n", This, respheaders);
1455 return httprequest_getAllResponseHeaders(This, respheaders);
1458 static HRESULT WINAPI XMLHTTPRequest_send(IXMLHTTPRequest *iface, VARIANT body)
1460 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1461 TRACE("(%p)->(%s)\n", This, debugstr_variant(&body));
1462 return httprequest_send(This, body);
1465 static HRESULT WINAPI XMLHTTPRequest_abort(IXMLHTTPRequest *iface)
1467 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1468 TRACE("(%p)\n", This);
1469 return httprequest_abort(This);
1472 static HRESULT WINAPI XMLHTTPRequest_get_status(IXMLHTTPRequest *iface, LONG *status)
1474 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1475 TRACE("(%p)->(%p)\n", This, status);
1476 return httprequest_get_status(This, status);
1479 static HRESULT WINAPI XMLHTTPRequest_get_statusText(IXMLHTTPRequest *iface, BSTR *status)
1481 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1482 TRACE("(%p)->(%p)\n", This, status);
1483 return httprequest_get_statusText(This, status);
1486 static HRESULT WINAPI XMLHTTPRequest_get_responseXML(IXMLHTTPRequest *iface, IDispatch **body)
1488 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1489 TRACE("(%p)->(%p)\n", This, body);
1490 return httprequest_get_responseXML(This, body);
1493 static HRESULT WINAPI XMLHTTPRequest_get_responseText(IXMLHTTPRequest *iface, BSTR *body)
1495 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1496 TRACE("(%p)->(%p)\n", This, body);
1497 return httprequest_get_responseText(This, body);
1500 static HRESULT WINAPI XMLHTTPRequest_get_responseBody(IXMLHTTPRequest *iface, VARIANT *body)
1502 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1503 TRACE("(%p)->(%p)\n", This, body);
1504 return httprequest_get_responseBody(This, body);
1507 static HRESULT WINAPI XMLHTTPRequest_get_responseStream(IXMLHTTPRequest *iface, VARIANT *body)
1509 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1510 TRACE("(%p)->(%p)\n", This, body);
1511 return httprequest_get_responseStream(This, body);
1514 static HRESULT WINAPI XMLHTTPRequest_get_readyState(IXMLHTTPRequest *iface, LONG *state)
1516 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1517 TRACE("(%p)->(%p)\n", This, state);
1518 return httprequest_get_readyState(This, state);
1521 static HRESULT WINAPI XMLHTTPRequest_put_onreadystatechange(IXMLHTTPRequest *iface, IDispatch *sink)
1523 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1524 TRACE("(%p)->(%p)\n", This, sink);
1525 return httprequest_put_onreadystatechange(This, sink);
1528 static const struct IXMLHTTPRequestVtbl XMLHTTPRequestVtbl =
1530 XMLHTTPRequest_QueryInterface,
1531 XMLHTTPRequest_AddRef,
1532 XMLHTTPRequest_Release,
1533 XMLHTTPRequest_GetTypeInfoCount,
1534 XMLHTTPRequest_GetTypeInfo,
1535 XMLHTTPRequest_GetIDsOfNames,
1536 XMLHTTPRequest_Invoke,
1537 XMLHTTPRequest_open,
1538 XMLHTTPRequest_setRequestHeader,
1539 XMLHTTPRequest_getResponseHeader,
1540 XMLHTTPRequest_getAllResponseHeaders,
1541 XMLHTTPRequest_send,
1542 XMLHTTPRequest_abort,
1543 XMLHTTPRequest_get_status,
1544 XMLHTTPRequest_get_statusText,
1545 XMLHTTPRequest_get_responseXML,
1546 XMLHTTPRequest_get_responseText,
1547 XMLHTTPRequest_get_responseBody,
1548 XMLHTTPRequest_get_responseStream,
1549 XMLHTTPRequest_get_readyState,
1550 XMLHTTPRequest_put_onreadystatechange
1553 /* IObjectWithSite */
1554 static HRESULT WINAPI
1555 httprequest_ObjectWithSite_QueryInterface( IObjectWithSite* iface, REFIID riid, void** ppvObject )
1557 httprequest *This = impl_from_IObjectWithSite(iface);
1558 return IXMLHTTPRequest_QueryInterface( (IXMLHTTPRequest *)This, riid, ppvObject );
1561 static ULONG WINAPI httprequest_ObjectWithSite_AddRef( IObjectWithSite* iface )
1563 httprequest *This = impl_from_IObjectWithSite(iface);
1564 return IXMLHTTPRequest_AddRef((IXMLHTTPRequest *)This);
1567 static ULONG WINAPI httprequest_ObjectWithSite_Release( IObjectWithSite* iface )
1569 httprequest *This = impl_from_IObjectWithSite(iface);
1570 return IXMLHTTPRequest_Release((IXMLHTTPRequest *)This);
1573 static HRESULT WINAPI httprequest_ObjectWithSite_GetSite( IObjectWithSite *iface, REFIID iid, void **ppvSite )
1575 httprequest *This = impl_from_IObjectWithSite(iface);
1577 TRACE("(%p)->(%s %p)\n", This, debugstr_guid( iid ), ppvSite );
1579 if ( !This->site )
1580 return E_FAIL;
1582 return IUnknown_QueryInterface( This->site, iid, ppvSite );
1585 static void get_base_uri(httprequest *This)
1587 IServiceProvider *provider;
1588 IHTMLDocument2 *doc;
1589 IUri *uri;
1590 BSTR url;
1591 HRESULT hr;
1593 hr = IUnknown_QueryInterface(This->site, &IID_IServiceProvider, (void**)&provider);
1594 if(FAILED(hr))
1595 return;
1597 hr = IServiceProvider_QueryService(provider, &SID_SContainerDispatch, &IID_IHTMLDocument2, (void**)&doc);
1598 if(FAILED(hr))
1599 hr = IServiceProvider_QueryService(provider, &SID_SInternetHostSecurityManager, &IID_IHTMLDocument2, (void**)&doc);
1600 IServiceProvider_Release(provider);
1601 if(FAILED(hr))
1602 return;
1604 hr = IHTMLDocument2_get_URL(doc, &url);
1605 IHTMLDocument2_Release(doc);
1606 if(FAILED(hr) || !url || !*url)
1607 return;
1609 TRACE("host url %s\n", debugstr_w(url));
1611 hr = CreateUri(url, 0, 0, &uri);
1612 SysFreeString(url);
1613 if(FAILED(hr))
1614 return;
1616 This->base_uri = uri;
1619 static HRESULT WINAPI httprequest_ObjectWithSite_SetSite( IObjectWithSite *iface, IUnknown *punk )
1621 httprequest *This = impl_from_IObjectWithSite(iface);
1623 TRACE("(%p)->(%p)\n", This, punk);
1625 if(This->site)
1626 IUnknown_Release( This->site );
1627 if(This->base_uri)
1628 IUri_Release(This->base_uri);
1630 This->site = punk;
1632 if (punk)
1634 IUnknown_AddRef( punk );
1635 get_base_uri(This);
1638 return S_OK;
1641 static const IObjectWithSiteVtbl ObjectWithSiteVtbl =
1643 httprequest_ObjectWithSite_QueryInterface,
1644 httprequest_ObjectWithSite_AddRef,
1645 httprequest_ObjectWithSite_Release,
1646 httprequest_ObjectWithSite_SetSite,
1647 httprequest_ObjectWithSite_GetSite
1650 /* IObjectSafety */
1651 static HRESULT WINAPI httprequest_Safety_QueryInterface(IObjectSafety *iface, REFIID riid, void **ppv)
1653 httprequest *This = impl_from_IObjectSafety(iface);
1654 return IXMLHTTPRequest_QueryInterface( (IXMLHTTPRequest *)This, riid, ppv );
1657 static ULONG WINAPI httprequest_Safety_AddRef(IObjectSafety *iface)
1659 httprequest *This = impl_from_IObjectSafety(iface);
1660 return IXMLHTTPRequest_AddRef((IXMLHTTPRequest *)This);
1663 static ULONG WINAPI httprequest_Safety_Release(IObjectSafety *iface)
1665 httprequest *This = impl_from_IObjectSafety(iface);
1666 return IXMLHTTPRequest_Release((IXMLHTTPRequest *)This);
1669 static HRESULT WINAPI httprequest_Safety_GetInterfaceSafetyOptions(IObjectSafety *iface, REFIID riid,
1670 DWORD *supported, DWORD *enabled)
1672 httprequest *This = impl_from_IObjectSafety(iface);
1674 TRACE("(%p)->(%s %p %p)\n", This, debugstr_guid(riid), supported, enabled);
1676 if(!supported || !enabled) return E_POINTER;
1678 *supported = safety_supported_options;
1679 *enabled = This->safeopt;
1681 return S_OK;
1684 static HRESULT WINAPI httprequest_Safety_SetInterfaceSafetyOptions(IObjectSafety *iface, REFIID riid,
1685 DWORD mask, DWORD enabled)
1687 httprequest *This = impl_from_IObjectSafety(iface);
1688 TRACE("(%p)->(%s %x %x)\n", This, debugstr_guid(riid), mask, enabled);
1690 if ((mask & ~safety_supported_options))
1691 return E_FAIL;
1693 This->safeopt = (This->safeopt & ~mask) | (mask & enabled);
1695 return S_OK;
1698 static const IObjectSafetyVtbl ObjectSafetyVtbl = {
1699 httprequest_Safety_QueryInterface,
1700 httprequest_Safety_AddRef,
1701 httprequest_Safety_Release,
1702 httprequest_Safety_GetInterfaceSafetyOptions,
1703 httprequest_Safety_SetInterfaceSafetyOptions
1706 /* IServerXMLHTTPRequest */
1707 static HRESULT WINAPI ServerXMLHTTPRequest_QueryInterface(IServerXMLHTTPRequest *iface, REFIID riid, void **obj)
1709 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1711 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), obj);
1713 if ( IsEqualGUID( riid, &IID_IServerXMLHTTPRequest) ||
1714 IsEqualGUID( riid, &IID_IXMLHTTPRequest) ||
1715 IsEqualGUID( riid, &IID_IDispatch) ||
1716 IsEqualGUID( riid, &IID_IUnknown) )
1718 *obj = iface;
1720 else
1722 TRACE("Unsupported interface %s\n", debugstr_guid(riid));
1723 *obj = NULL;
1724 return E_NOINTERFACE;
1727 IServerXMLHTTPRequest_AddRef( iface );
1729 return S_OK;
1732 static ULONG WINAPI ServerXMLHTTPRequest_AddRef(IServerXMLHTTPRequest *iface)
1734 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1735 ULONG ref = InterlockedIncrement( &This->ref );
1736 TRACE("(%p)->(%u)\n", This, ref );
1737 return ref;
1740 static ULONG WINAPI ServerXMLHTTPRequest_Release(IServerXMLHTTPRequest *iface)
1742 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1743 ULONG ref = InterlockedDecrement( &This->ref );
1745 TRACE("(%p)->(%u)\n", This, ref );
1747 if ( ref == 0 )
1749 httprequest_release( &This->req );
1750 heap_free( This );
1753 return ref;
1756 static HRESULT WINAPI ServerXMLHTTPRequest_GetTypeInfoCount(IServerXMLHTTPRequest *iface, UINT *pctinfo)
1758 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1760 TRACE("(%p)->(%p)\n", This, pctinfo);
1761 *pctinfo = 1;
1763 return S_OK;
1766 static HRESULT WINAPI ServerXMLHTTPRequest_GetTypeInfo(IServerXMLHTTPRequest *iface, UINT iTInfo,
1767 LCID lcid, ITypeInfo **ppTInfo)
1769 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1771 TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
1773 return get_typeinfo(IServerXMLHTTPRequest_tid, ppTInfo);
1776 static HRESULT WINAPI ServerXMLHTTPRequest_GetIDsOfNames(IServerXMLHTTPRequest *iface, REFIID riid,
1777 LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
1779 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1780 ITypeInfo *typeinfo;
1781 HRESULT hr;
1783 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
1784 lcid, rgDispId);
1786 if(!rgszNames || cNames == 0 || !rgDispId)
1787 return E_INVALIDARG;
1789 hr = get_typeinfo(IServerXMLHTTPRequest_tid, &typeinfo);
1790 if(SUCCEEDED(hr))
1792 hr = ITypeInfo_GetIDsOfNames(typeinfo, rgszNames, cNames, rgDispId);
1793 ITypeInfo_Release(typeinfo);
1796 return hr;
1799 static HRESULT WINAPI ServerXMLHTTPRequest_Invoke(IServerXMLHTTPRequest *iface, DISPID dispIdMember, REFIID riid,
1800 LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult,
1801 EXCEPINFO *pExcepInfo, UINT *puArgErr)
1803 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1804 ITypeInfo *typeinfo;
1805 HRESULT hr;
1807 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
1808 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
1810 hr = get_typeinfo(IServerXMLHTTPRequest_tid, &typeinfo);
1811 if(SUCCEEDED(hr))
1813 hr = ITypeInfo_Invoke(typeinfo, &This->IServerXMLHTTPRequest_iface, dispIdMember, wFlags,
1814 pDispParams, pVarResult, pExcepInfo, puArgErr);
1815 ITypeInfo_Release(typeinfo);
1818 return hr;
1821 static HRESULT WINAPI ServerXMLHTTPRequest_open(IServerXMLHTTPRequest *iface, BSTR method, BSTR url,
1822 VARIANT async, VARIANT user, VARIANT password)
1824 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1825 TRACE("(%p)->(%s %s %s)\n", This, debugstr_w(method), debugstr_w(url),
1826 debugstr_variant(&async));
1827 return httprequest_open(&This->req, method, url, async, user, password);
1830 static HRESULT WINAPI ServerXMLHTTPRequest_setRequestHeader(IServerXMLHTTPRequest *iface, BSTR header, BSTR value)
1832 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1833 TRACE("(%p)->(%s %s)\n", This, debugstr_w(header), debugstr_w(value));
1834 return httprequest_setRequestHeader(&This->req, header, value);
1837 static HRESULT WINAPI ServerXMLHTTPRequest_getResponseHeader(IServerXMLHTTPRequest *iface, BSTR header, BSTR *value)
1839 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1840 TRACE("(%p)->(%s %p)\n", This, debugstr_w(header), value);
1841 return httprequest_getResponseHeader(&This->req, header, value);
1844 static HRESULT WINAPI ServerXMLHTTPRequest_getAllResponseHeaders(IServerXMLHTTPRequest *iface, BSTR *respheaders)
1846 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1847 TRACE("(%p)->(%p)\n", This, respheaders);
1848 return httprequest_getAllResponseHeaders(&This->req, respheaders);
1851 static HRESULT WINAPI ServerXMLHTTPRequest_send(IServerXMLHTTPRequest *iface, VARIANT body)
1853 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1854 TRACE("(%p)->(%s)\n", This, debugstr_variant(&body));
1855 return httprequest_send(&This->req, body);
1858 static HRESULT WINAPI ServerXMLHTTPRequest_abort(IServerXMLHTTPRequest *iface)
1860 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1861 TRACE("(%p)\n", This);
1862 return httprequest_abort(&This->req);
1865 static HRESULT WINAPI ServerXMLHTTPRequest_get_status(IServerXMLHTTPRequest *iface, LONG *status)
1867 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1868 TRACE("(%p)->(%p)\n", This, status);
1869 return httprequest_get_status(&This->req, status);
1872 static HRESULT WINAPI ServerXMLHTTPRequest_get_statusText(IServerXMLHTTPRequest *iface, BSTR *status)
1874 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1875 TRACE("(%p)->(%p)\n", This, status);
1876 return httprequest_get_statusText(&This->req, status);
1879 static HRESULT WINAPI ServerXMLHTTPRequest_get_responseXML(IServerXMLHTTPRequest *iface, IDispatch **body)
1881 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1882 TRACE("(%p)->(%p)\n", This, body);
1883 return httprequest_get_responseXML(&This->req, body);
1886 static HRESULT WINAPI ServerXMLHTTPRequest_get_responseText(IServerXMLHTTPRequest *iface, BSTR *body)
1888 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1889 TRACE("(%p)->(%p)\n", This, body);
1890 return httprequest_get_responseText(&This->req, body);
1893 static HRESULT WINAPI ServerXMLHTTPRequest_get_responseBody(IServerXMLHTTPRequest *iface, VARIANT *body)
1895 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1896 TRACE("(%p)->(%p)\n", This, body);
1897 return httprequest_get_responseBody(&This->req, body);
1900 static HRESULT WINAPI ServerXMLHTTPRequest_get_responseStream(IServerXMLHTTPRequest *iface, VARIANT *body)
1902 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1903 TRACE("(%p)->(%p)\n", This, body);
1904 return httprequest_get_responseStream(&This->req, body);
1907 static HRESULT WINAPI ServerXMLHTTPRequest_get_readyState(IServerXMLHTTPRequest *iface, LONG *state)
1909 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1910 TRACE("(%p)->(%p)\n", This, state);
1911 return httprequest_get_readyState(&This->req, state);
1914 static HRESULT WINAPI ServerXMLHTTPRequest_put_onreadystatechange(IServerXMLHTTPRequest *iface, IDispatch *sink)
1916 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1917 TRACE("(%p)->(%p)\n", This, sink);
1918 return httprequest_put_onreadystatechange(&This->req, sink);
1921 static HRESULT WINAPI ServerXMLHTTPRequest_setTimeouts(IServerXMLHTTPRequest *iface, LONG resolveTimeout, LONG connectTimeout,
1922 LONG sendTimeout, LONG receiveTimeout)
1924 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1925 FIXME("(%p)->(%d %d %d %d): stub\n", This, resolveTimeout, connectTimeout, sendTimeout, receiveTimeout);
1926 return E_NOTIMPL;
1929 static HRESULT WINAPI ServerXMLHTTPRequest_waitForResponse(IServerXMLHTTPRequest *iface, VARIANT timeout, VARIANT_BOOL *isSuccessful)
1931 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1932 FIXME("(%p)->(%s %p): stub\n", This, debugstr_variant(&timeout), isSuccessful);
1933 return E_NOTIMPL;
1936 static HRESULT WINAPI ServerXMLHTTPRequest_getOption(IServerXMLHTTPRequest *iface, SERVERXMLHTTP_OPTION option, VARIANT *value)
1938 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1939 FIXME("(%p)->(%d %p): stub\n", This, option, value);
1940 return E_NOTIMPL;
1943 static HRESULT WINAPI ServerXMLHTTPRequest_setOption(IServerXMLHTTPRequest *iface, SERVERXMLHTTP_OPTION option, VARIANT value)
1945 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1946 FIXME("(%p)->(%d %s): stub\n", This, option, debugstr_variant(&value));
1947 return E_NOTIMPL;
1950 static const struct IServerXMLHTTPRequestVtbl ServerXMLHTTPRequestVtbl =
1952 ServerXMLHTTPRequest_QueryInterface,
1953 ServerXMLHTTPRequest_AddRef,
1954 ServerXMLHTTPRequest_Release,
1955 ServerXMLHTTPRequest_GetTypeInfoCount,
1956 ServerXMLHTTPRequest_GetTypeInfo,
1957 ServerXMLHTTPRequest_GetIDsOfNames,
1958 ServerXMLHTTPRequest_Invoke,
1959 ServerXMLHTTPRequest_open,
1960 ServerXMLHTTPRequest_setRequestHeader,
1961 ServerXMLHTTPRequest_getResponseHeader,
1962 ServerXMLHTTPRequest_getAllResponseHeaders,
1963 ServerXMLHTTPRequest_send,
1964 ServerXMLHTTPRequest_abort,
1965 ServerXMLHTTPRequest_get_status,
1966 ServerXMLHTTPRequest_get_statusText,
1967 ServerXMLHTTPRequest_get_responseXML,
1968 ServerXMLHTTPRequest_get_responseText,
1969 ServerXMLHTTPRequest_get_responseBody,
1970 ServerXMLHTTPRequest_get_responseStream,
1971 ServerXMLHTTPRequest_get_readyState,
1972 ServerXMLHTTPRequest_put_onreadystatechange,
1973 ServerXMLHTTPRequest_setTimeouts,
1974 ServerXMLHTTPRequest_waitForResponse,
1975 ServerXMLHTTPRequest_getOption,
1976 ServerXMLHTTPRequest_setOption
1979 static void init_httprequest(httprequest *req)
1981 req->IXMLHTTPRequest_iface.lpVtbl = &XMLHTTPRequestVtbl;
1982 req->IObjectWithSite_iface.lpVtbl = &ObjectWithSiteVtbl;
1983 req->IObjectSafety_iface.lpVtbl = &ObjectSafetyVtbl;
1984 req->ref = 1;
1986 req->async = FALSE;
1987 req->verb = -1;
1988 req->custom = NULL;
1989 req->uri = req->base_uri = NULL;
1990 req->user = req->password = NULL;
1992 req->state = READYSTATE_UNINITIALIZED;
1993 req->sink = NULL;
1995 req->bsc = NULL;
1996 req->status = 0;
1997 req->status_text = NULL;
1998 req->reqheader_size = 0;
1999 req->raw_respheaders = NULL;
2000 req->use_utf8_content = FALSE;
2002 list_init(&req->reqheaders);
2003 list_init(&req->respheaders);
2005 req->site = NULL;
2006 req->safeopt = 0;
2009 HRESULT XMLHTTPRequest_create(void **obj)
2011 httprequest *req;
2013 TRACE("(%p)\n", obj);
2015 req = heap_alloc( sizeof (*req) );
2016 if( !req )
2017 return E_OUTOFMEMORY;
2019 init_httprequest(req);
2020 *obj = &req->IXMLHTTPRequest_iface;
2022 TRACE("returning iface %p\n", *obj);
2024 return S_OK;
2027 HRESULT ServerXMLHTTP_create(void **obj)
2029 serverhttp *req;
2031 TRACE("(%p)\n", obj);
2033 req = heap_alloc( sizeof (*req) );
2034 if( !req )
2035 return E_OUTOFMEMORY;
2037 init_httprequest(&req->req);
2038 req->IServerXMLHTTPRequest_iface.lpVtbl = &ServerXMLHTTPRequestVtbl;
2039 req->ref = 1;
2041 *obj = &req->IServerXMLHTTPRequest_iface;
2043 TRACE("returning iface %p\n", *obj);
2045 return S_OK;
2048 #else
2050 HRESULT XMLHTTPRequest_create(void **ppObj)
2052 MESSAGE("This program tried to use a XMLHTTPRequest object, but\n"
2053 "libxml2 support was not present at compile time.\n");
2054 return E_NOTIMPL;
2057 HRESULT ServerXMLHTTP_create(void **obj)
2059 MESSAGE("This program tried to use a ServerXMLHTTP object, but\n"
2060 "libxml2 support was not present at compile time.\n");
2061 return E_NOTIMPL;
2064 #endif