winemac: Clear the thread data explicitly on detach.
[wine.git] / dlls / msxml3 / httprequest.c
blobe2ede0e8129c6928a1e7b9536c6c77a230cdbbf9
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"
50 #include "wine/list.h"
52 WINE_DEFAULT_DEBUG_CHANNEL(msxml);
54 #ifdef HAVE_LIBXML2
56 static const WCHAR colspaceW[] = {':',' ',0};
57 static const WCHAR crlfW[] = {'\r','\n',0};
58 static const DWORD safety_supported_options =
59 INTERFACESAFE_FOR_UNTRUSTED_CALLER |
60 INTERFACESAFE_FOR_UNTRUSTED_DATA |
61 INTERFACE_USES_SECURITY_MANAGER;
63 typedef struct BindStatusCallback BindStatusCallback;
65 struct httpheader
67 struct list entry;
68 BSTR header;
69 BSTR value;
72 typedef struct
74 IXMLHTTPRequest IXMLHTTPRequest_iface;
75 IObjectWithSite IObjectWithSite_iface;
76 IObjectSafety IObjectSafety_iface;
77 LONG ref;
79 READYSTATE state;
80 IDispatch *sink;
82 /* request */
83 BINDVERB verb;
84 BSTR custom;
85 IUri *uri;
86 IUri *base_uri;
87 BOOL async;
88 struct list reqheaders;
89 /* cached resulting custom request headers string length in WCHARs */
90 LONG reqheader_size;
91 /* use UTF-8 content type */
92 BOOL use_utf8_content;
94 /* response headers */
95 struct list respheaders;
96 BSTR raw_respheaders;
98 /* credentials */
99 BSTR user;
100 BSTR password;
102 /* bind callback */
103 BindStatusCallback *bsc;
104 LONG status;
105 BSTR status_text;
107 /* IObjectWithSite*/
108 IUnknown *site;
110 /* IObjectSafety */
111 DWORD safeopt;
112 } httprequest;
114 typedef struct
116 httprequest req;
117 IServerXMLHTTPRequest IServerXMLHTTPRequest_iface;
118 LONG ref;
119 } serverhttp;
121 static inline httprequest *impl_from_IXMLHTTPRequest( IXMLHTTPRequest *iface )
123 return CONTAINING_RECORD(iface, httprequest, IXMLHTTPRequest_iface);
126 static inline httprequest *impl_from_IObjectWithSite(IObjectWithSite *iface)
128 return CONTAINING_RECORD(iface, httprequest, IObjectWithSite_iface);
131 static inline httprequest *impl_from_IObjectSafety(IObjectSafety *iface)
133 return CONTAINING_RECORD(iface, httprequest, IObjectSafety_iface);
136 static inline serverhttp *impl_from_IServerXMLHTTPRequest(IServerXMLHTTPRequest *iface)
138 return CONTAINING_RECORD(iface, serverhttp, IServerXMLHTTPRequest_iface);
141 static void httprequest_setreadystate(httprequest *This, READYSTATE state)
143 READYSTATE last = This->state;
144 static const char* readystates[] = {
145 "READYSTATE_UNINITIALIZED",
146 "READYSTATE_LOADING",
147 "READYSTATE_LOADED",
148 "READYSTATE_INTERACTIVE",
149 "READYSTATE_COMPLETE"};
151 This->state = state;
153 TRACE("state %s\n", readystates[state]);
155 if (This->sink && last != state)
157 DISPPARAMS params;
159 memset(&params, 0, sizeof(params));
160 IDispatch_Invoke(This->sink, 0, &IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD, &params, 0, 0, 0);
164 static void free_response_headers(httprequest *This)
166 struct httpheader *header, *header2;
168 LIST_FOR_EACH_ENTRY_SAFE(header, header2, &This->respheaders, struct httpheader, entry)
170 list_remove(&header->entry);
171 SysFreeString(header->header);
172 SysFreeString(header->value);
173 heap_free(header);
176 SysFreeString(This->raw_respheaders);
177 This->raw_respheaders = NULL;
180 struct BindStatusCallback
182 IBindStatusCallback IBindStatusCallback_iface;
183 IHttpNegotiate IHttpNegotiate_iface;
184 IAuthenticate IAuthenticate_iface;
185 LONG ref;
187 IBinding *binding;
188 httprequest *request;
190 /* response data */
191 IStream *stream;
193 /* request body data */
194 HGLOBAL body;
197 static inline BindStatusCallback *impl_from_IBindStatusCallback( IBindStatusCallback *iface )
199 return CONTAINING_RECORD(iface, BindStatusCallback, IBindStatusCallback_iface);
202 static inline BindStatusCallback *impl_from_IHttpNegotiate( IHttpNegotiate *iface )
204 return CONTAINING_RECORD(iface, BindStatusCallback, IHttpNegotiate_iface);
207 static inline BindStatusCallback *impl_from_IAuthenticate( IAuthenticate *iface )
209 return CONTAINING_RECORD(iface, BindStatusCallback, IAuthenticate_iface);
212 static void BindStatusCallback_Detach(BindStatusCallback *bsc)
214 if (bsc)
216 if (bsc->binding) IBinding_Abort(bsc->binding);
217 bsc->request->bsc = NULL;
218 bsc->request = NULL;
219 IBindStatusCallback_Release(&bsc->IBindStatusCallback_iface);
223 static HRESULT WINAPI BindStatusCallback_QueryInterface(IBindStatusCallback *iface,
224 REFIID riid, void **ppv)
226 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
228 *ppv = NULL;
230 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
232 if (IsEqualGUID(&IID_IUnknown, riid) ||
233 IsEqualGUID(&IID_IBindStatusCallback, riid))
235 *ppv = &This->IBindStatusCallback_iface;
237 else if (IsEqualGUID(&IID_IHttpNegotiate, riid))
239 *ppv = &This->IHttpNegotiate_iface;
241 else if (IsEqualGUID(&IID_IAuthenticate, riid))
243 *ppv = &This->IAuthenticate_iface;
245 else if (IsEqualGUID(&IID_IServiceProvider, riid) ||
246 IsEqualGUID(&IID_IBindStatusCallbackEx, riid) ||
247 IsEqualGUID(&IID_IInternetProtocol, riid) ||
248 IsEqualGUID(&IID_IHttpNegotiate2, riid))
250 return E_NOINTERFACE;
253 if (*ppv)
255 IBindStatusCallback_AddRef(iface);
256 return S_OK;
259 FIXME("Unsupported riid = %s\n", debugstr_guid(riid));
261 return E_NOINTERFACE;
264 static ULONG WINAPI BindStatusCallback_AddRef(IBindStatusCallback *iface)
266 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
267 LONG ref = InterlockedIncrement(&This->ref);
269 TRACE("(%p) ref = %d\n", This, ref);
271 return ref;
274 static ULONG WINAPI BindStatusCallback_Release(IBindStatusCallback *iface)
276 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
277 LONG ref = InterlockedDecrement(&This->ref);
279 TRACE("(%p) ref = %d\n", This, ref);
281 if (!ref)
283 if (This->binding) IBinding_Release(This->binding);
284 if (This->stream) IStream_Release(This->stream);
285 if (This->body) GlobalFree(This->body);
286 heap_free(This);
289 return ref;
292 static HRESULT WINAPI BindStatusCallback_OnStartBinding(IBindStatusCallback *iface,
293 DWORD reserved, IBinding *pbind)
295 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
297 TRACE("(%p)->(%d %p)\n", This, reserved, pbind);
299 if (!pbind) return E_INVALIDARG;
301 This->binding = pbind;
302 IBinding_AddRef(pbind);
304 httprequest_setreadystate(This->request, READYSTATE_LOADED);
306 return CreateStreamOnHGlobal(NULL, TRUE, &This->stream);
309 static HRESULT WINAPI BindStatusCallback_GetPriority(IBindStatusCallback *iface, LONG *pPriority)
311 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
313 TRACE("(%p)->(%p)\n", This, pPriority);
315 return E_NOTIMPL;
318 static HRESULT WINAPI BindStatusCallback_OnLowResource(IBindStatusCallback *iface, DWORD reserved)
320 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
322 TRACE("(%p)->(%d)\n", This, reserved);
324 return E_NOTIMPL;
327 static HRESULT WINAPI BindStatusCallback_OnProgress(IBindStatusCallback *iface, ULONG ulProgress,
328 ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
330 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
332 TRACE("(%p)->(%u %u %u %s)\n", This, ulProgress, ulProgressMax, ulStatusCode,
333 debugstr_w(szStatusText));
335 return S_OK;
338 static HRESULT WINAPI BindStatusCallback_OnStopBinding(IBindStatusCallback *iface,
339 HRESULT hr, LPCWSTR error)
341 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
343 TRACE("(%p)->(0x%08x %s)\n", This, hr, debugstr_w(error));
345 if (This->binding)
347 IBinding_Release(This->binding);
348 This->binding = NULL;
351 if (hr == S_OK)
353 BindStatusCallback_Detach(This->request->bsc);
354 This->request->bsc = This;
355 httprequest_setreadystate(This->request, READYSTATE_COMPLETE);
358 return S_OK;
361 static HRESULT WINAPI BindStatusCallback_GetBindInfo(IBindStatusCallback *iface,
362 DWORD *bind_flags, BINDINFO *pbindinfo)
364 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
366 TRACE("(%p)->(%p %p)\n", This, bind_flags, pbindinfo);
368 *bind_flags = 0;
369 if (This->request->async) *bind_flags |= BINDF_ASYNCHRONOUS;
371 if (This->request->verb != BINDVERB_GET && This->body)
373 pbindinfo->stgmedData.tymed = TYMED_HGLOBAL;
374 pbindinfo->stgmedData.u.hGlobal = This->body;
375 pbindinfo->cbstgmedData = GlobalSize(This->body);
376 /* callback owns passed body pointer */
377 IBindStatusCallback_QueryInterface(iface, &IID_IUnknown, (void**)&pbindinfo->stgmedData.pUnkForRelease);
380 pbindinfo->dwBindVerb = This->request->verb;
381 if (This->request->verb == BINDVERB_CUSTOM)
383 pbindinfo->szCustomVerb = CoTaskMemAlloc(SysStringByteLen(This->request->custom));
384 strcpyW(pbindinfo->szCustomVerb, This->request->custom);
387 return S_OK;
390 static HRESULT WINAPI BindStatusCallback_OnDataAvailable(IBindStatusCallback *iface,
391 DWORD flags, DWORD size, FORMATETC *format, STGMEDIUM *stgmed)
393 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
394 DWORD read, written;
395 BYTE buf[4096];
396 HRESULT hr;
398 TRACE("(%p)->(%08x %d %p %p)\n", This, flags, size, format, stgmed);
402 hr = IStream_Read(stgmed->u.pstm, buf, sizeof(buf), &read);
403 if (hr != S_OK) break;
405 hr = IStream_Write(This->stream, buf, read, &written);
406 } while((hr == S_OK) && written != 0 && read != 0);
408 httprequest_setreadystate(This->request, READYSTATE_INTERACTIVE);
410 return S_OK;
413 static HRESULT WINAPI BindStatusCallback_OnObjectAvailable(IBindStatusCallback *iface,
414 REFIID riid, IUnknown *punk)
416 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
418 FIXME("(%p)->(%s %p): stub\n", This, debugstr_guid(riid), punk);
420 return E_NOTIMPL;
423 static const IBindStatusCallbackVtbl BindStatusCallbackVtbl = {
424 BindStatusCallback_QueryInterface,
425 BindStatusCallback_AddRef,
426 BindStatusCallback_Release,
427 BindStatusCallback_OnStartBinding,
428 BindStatusCallback_GetPriority,
429 BindStatusCallback_OnLowResource,
430 BindStatusCallback_OnProgress,
431 BindStatusCallback_OnStopBinding,
432 BindStatusCallback_GetBindInfo,
433 BindStatusCallback_OnDataAvailable,
434 BindStatusCallback_OnObjectAvailable
437 static HRESULT WINAPI BSCHttpNegotiate_QueryInterface(IHttpNegotiate *iface,
438 REFIID riid, void **ppv)
440 BindStatusCallback *This = impl_from_IHttpNegotiate(iface);
441 return IBindStatusCallback_QueryInterface(&This->IBindStatusCallback_iface, riid, ppv);
444 static ULONG WINAPI BSCHttpNegotiate_AddRef(IHttpNegotiate *iface)
446 BindStatusCallback *This = impl_from_IHttpNegotiate(iface);
447 return IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
450 static ULONG WINAPI BSCHttpNegotiate_Release(IHttpNegotiate *iface)
452 BindStatusCallback *This = impl_from_IHttpNegotiate(iface);
453 return IBindStatusCallback_Release(&This->IBindStatusCallback_iface);
456 static HRESULT WINAPI BSCHttpNegotiate_BeginningTransaction(IHttpNegotiate *iface,
457 LPCWSTR url, LPCWSTR headers, DWORD reserved, LPWSTR *add_headers)
459 static const WCHAR content_type_utf8W[] = {'C','o','n','t','e','n','t','-','T','y','p','e',':',' ',
460 't','e','x','t','/','p','l','a','i','n',';','c','h','a','r','s','e','t','=','u','t','f','-','8','\r','\n',0};
462 BindStatusCallback *This = impl_from_IHttpNegotiate(iface);
463 const struct httpheader *entry;
464 WCHAR *buff, *ptr;
465 int size = 0;
467 TRACE("(%p)->(%s %s %d %p)\n", This, debugstr_w(url), debugstr_w(headers), reserved, add_headers);
469 *add_headers = NULL;
471 if (This->request->use_utf8_content)
472 size = sizeof(content_type_utf8W);
474 if (!list_empty(&This->request->reqheaders))
475 size += This->request->reqheader_size*sizeof(WCHAR);
477 if (!size) return S_OK;
479 buff = CoTaskMemAlloc(size);
480 if (!buff) return E_OUTOFMEMORY;
482 ptr = buff;
483 if (This->request->use_utf8_content)
485 lstrcpyW(ptr, content_type_utf8W);
486 ptr += sizeof(content_type_utf8W)/sizeof(WCHAR)-1;
489 /* user headers */
490 LIST_FOR_EACH_ENTRY(entry, &This->request->reqheaders, struct httpheader, entry)
492 lstrcpyW(ptr, entry->header);
493 ptr += SysStringLen(entry->header);
495 lstrcpyW(ptr, colspaceW);
496 ptr += sizeof(colspaceW)/sizeof(WCHAR)-1;
498 lstrcpyW(ptr, entry->value);
499 ptr += SysStringLen(entry->value);
501 lstrcpyW(ptr, crlfW);
502 ptr += sizeof(crlfW)/sizeof(WCHAR)-1;
505 *add_headers = buff;
507 return S_OK;
510 static void add_response_header(httprequest *This, const WCHAR *data, int len)
512 struct httpheader *entry;
513 const WCHAR *ptr = data;
514 BSTR header, value;
516 while (*ptr)
518 if (*ptr == ':')
520 header = SysAllocStringLen(data, ptr-data);
521 /* skip leading spaces for a value */
522 while (*++ptr == ' ')
524 value = SysAllocStringLen(ptr, len-(ptr-data));
525 break;
527 ptr++;
530 if (!*ptr) return;
532 /* new header */
533 TRACE("got header %s:%s\n", debugstr_w(header), debugstr_w(value));
535 entry = heap_alloc(sizeof(*entry));
536 entry->header = header;
537 entry->value = value;
538 list_add_head(&This->respheaders, &entry->entry);
541 static HRESULT WINAPI BSCHttpNegotiate_OnResponse(IHttpNegotiate *iface, DWORD code,
542 LPCWSTR resp_headers, LPCWSTR req_headers, LPWSTR *add_reqheaders)
544 BindStatusCallback *This = impl_from_IHttpNegotiate(iface);
546 TRACE("(%p)->(%d %s %s %p)\n", This, code, debugstr_w(resp_headers),
547 debugstr_w(req_headers), add_reqheaders);
549 This->request->status = code;
550 /* store headers and status text */
551 free_response_headers(This->request);
552 SysFreeString(This->request->status_text);
553 This->request->status_text = NULL;
554 if (resp_headers)
556 const WCHAR *ptr, *line, *status_text;
558 ptr = line = resp_headers;
560 /* skip HTTP-Version */
561 ptr = strchrW(ptr, ' ');
562 if (ptr)
564 /* skip Status-Code */
565 ptr = strchrW(++ptr, ' ');
566 if (ptr)
568 status_text = ++ptr;
569 /* now it supposed to end with CRLF */
570 while (*ptr)
572 if (*ptr == '\r' && *(ptr+1) == '\n')
574 line = ptr + 2;
575 This->request->status_text = SysAllocStringLen(status_text, ptr-status_text);
576 TRACE("status text %s\n", debugstr_w(This->request->status_text));
577 break;
579 ptr++;
584 /* store as unparsed string for now */
585 This->request->raw_respheaders = SysAllocString(line);
588 return S_OK;
591 static const IHttpNegotiateVtbl BSCHttpNegotiateVtbl = {
592 BSCHttpNegotiate_QueryInterface,
593 BSCHttpNegotiate_AddRef,
594 BSCHttpNegotiate_Release,
595 BSCHttpNegotiate_BeginningTransaction,
596 BSCHttpNegotiate_OnResponse
599 static HRESULT WINAPI Authenticate_QueryInterface(IAuthenticate *iface,
600 REFIID riid, void **ppv)
602 BindStatusCallback *This = impl_from_IAuthenticate(iface);
603 return IBindStatusCallback_QueryInterface(&This->IBindStatusCallback_iface, riid, ppv);
606 static ULONG WINAPI Authenticate_AddRef(IAuthenticate *iface)
608 BindStatusCallback *This = impl_from_IAuthenticate(iface);
609 return IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
612 static ULONG WINAPI Authenticate_Release(IAuthenticate *iface)
614 BindStatusCallback *This = impl_from_IAuthenticate(iface);
615 return IBindStatusCallback_Release(&This->IBindStatusCallback_iface);
618 static HRESULT WINAPI Authenticate_Authenticate(IAuthenticate *iface,
619 HWND *hwnd, LPWSTR *username, LPWSTR *password)
621 BindStatusCallback *This = impl_from_IAuthenticate(iface);
622 httprequest *request = This->request;
624 TRACE("(%p)->(%p %p %p)\n", This, hwnd, username, password);
626 if (request->user && *request->user)
628 if (hwnd) *hwnd = NULL;
629 *username = CoTaskMemAlloc(SysStringByteLen(request->user)+sizeof(WCHAR));
630 *password = CoTaskMemAlloc(SysStringByteLen(request->password)+sizeof(WCHAR));
631 if (!*username || !*password)
633 CoTaskMemFree(*username);
634 CoTaskMemFree(*password);
635 return E_OUTOFMEMORY;
638 memcpy(*username, request->user, SysStringByteLen(request->user)+sizeof(WCHAR));
639 memcpy(*password, request->password, SysStringByteLen(request->password)+sizeof(WCHAR));
642 return S_OK;
645 static const IAuthenticateVtbl AuthenticateVtbl = {
646 Authenticate_QueryInterface,
647 Authenticate_AddRef,
648 Authenticate_Release,
649 Authenticate_Authenticate
652 static HRESULT BindStatusCallback_create(httprequest* This, BindStatusCallback **obj, const VARIANT *body)
654 BindStatusCallback *bsc;
655 IBindCtx *pbc;
656 HRESULT hr;
657 int size;
659 hr = CreateBindCtx(0, &pbc);
660 if (hr != S_OK) return hr;
662 bsc = heap_alloc(sizeof(*bsc));
663 if (!bsc)
665 IBindCtx_Release(pbc);
666 return E_OUTOFMEMORY;
669 bsc->IBindStatusCallback_iface.lpVtbl = &BindStatusCallbackVtbl;
670 bsc->IHttpNegotiate_iface.lpVtbl = &BSCHttpNegotiateVtbl;
671 bsc->IAuthenticate_iface.lpVtbl = &AuthenticateVtbl;
672 bsc->ref = 1;
673 bsc->request = This;
674 bsc->binding = NULL;
675 bsc->stream = NULL;
676 bsc->body = NULL;
678 TRACE("(%p)->(%p)\n", This, bsc);
680 This->use_utf8_content = FALSE;
682 if (This->verb != BINDVERB_GET)
684 void *send_data, *ptr;
685 SAFEARRAY *sa = NULL;
687 if (V_VT(body) == (VT_VARIANT|VT_BYREF))
688 body = V_VARIANTREF(body);
690 switch (V_VT(body))
692 case VT_BSTR:
694 int len = SysStringLen(V_BSTR(body));
695 const WCHAR *str = V_BSTR(body);
696 UINT i, cp = CP_ACP;
698 for (i = 0; i < len; i++)
700 if (str[i] > 127)
702 cp = CP_UTF8;
703 break;
707 size = WideCharToMultiByte(cp, 0, str, len, NULL, 0, NULL, NULL);
708 if (!(ptr = heap_alloc(size)))
710 heap_free(bsc);
711 return E_OUTOFMEMORY;
713 WideCharToMultiByte(cp, 0, str, len, ptr, size, NULL, NULL);
714 if (cp == CP_UTF8) This->use_utf8_content = TRUE;
715 break;
717 case VT_ARRAY|VT_UI1:
719 sa = V_ARRAY(body);
720 if ((hr = SafeArrayAccessData(sa, (void **)&ptr)) != S_OK)
722 heap_free(bsc);
723 return hr;
725 if ((hr = SafeArrayGetUBound(sa, 1, &size)) != S_OK)
727 SafeArrayUnaccessData(sa);
728 heap_free(bsc);
729 return hr;
731 size++;
732 break;
734 default:
735 FIXME("unsupported body data type %d\n", V_VT(body));
736 /* fall through */
737 case VT_EMPTY:
738 case VT_ERROR:
739 case VT_NULL:
740 ptr = NULL;
741 size = 0;
742 break;
745 if (size)
747 bsc->body = GlobalAlloc(GMEM_FIXED, size);
748 if (!bsc->body)
750 if (V_VT(body) == VT_BSTR)
751 heap_free(ptr);
752 else if (V_VT(body) == (VT_ARRAY|VT_UI1))
753 SafeArrayUnaccessData(sa);
755 heap_free(bsc);
756 return E_OUTOFMEMORY;
759 send_data = GlobalLock(bsc->body);
760 memcpy(send_data, ptr, size);
761 GlobalUnlock(bsc->body);
764 if (V_VT(body) == VT_BSTR)
765 heap_free(ptr);
766 else if (V_VT(body) == (VT_ARRAY|VT_UI1))
767 SafeArrayUnaccessData(sa);
770 hr = RegisterBindStatusCallback(pbc, &bsc->IBindStatusCallback_iface, NULL, 0);
771 if (hr == S_OK)
773 IMoniker *moniker;
775 hr = CreateURLMonikerEx2(NULL, This->uri, &moniker, URL_MK_UNIFORM);
776 if (hr == S_OK)
778 IStream *stream;
780 hr = IMoniker_BindToStorage(moniker, pbc, NULL, &IID_IStream, (void**)&stream);
781 IMoniker_Release(moniker);
782 if (stream) IStream_Release(stream);
784 IBindCtx_Release(pbc);
787 if (FAILED(hr))
789 IBindStatusCallback_Release(&bsc->IBindStatusCallback_iface);
790 bsc = NULL;
793 *obj = bsc;
794 return hr;
797 static HRESULT verify_uri(httprequest *This, IUri *uri)
799 DWORD scheme, base_scheme;
800 BSTR host, base_host;
801 HRESULT hr;
803 if(!(This->safeopt & INTERFACESAFE_FOR_UNTRUSTED_DATA))
804 return S_OK;
806 if(!This->base_uri)
807 return E_ACCESSDENIED;
809 hr = IUri_GetScheme(uri, &scheme);
810 if(FAILED(hr))
811 return hr;
813 hr = IUri_GetScheme(This->base_uri, &base_scheme);
814 if(FAILED(hr))
815 return hr;
817 if(scheme != base_scheme) {
818 WARN("Schemes don't match\n");
819 return E_ACCESSDENIED;
822 if(scheme == INTERNET_SCHEME_UNKNOWN) {
823 FIXME("Unknown scheme\n");
824 return E_ACCESSDENIED;
827 hr = IUri_GetHost(uri, &host);
828 if(FAILED(hr))
829 return hr;
831 hr = IUri_GetHost(This->base_uri, &base_host);
832 if(SUCCEEDED(hr)) {
833 if(strcmpiW(host, base_host)) {
834 WARN("Hosts don't match\n");
835 hr = E_ACCESSDENIED;
837 SysFreeString(base_host);
840 SysFreeString(host);
841 return hr;
844 static HRESULT httprequest_open(httprequest *This, BSTR method, BSTR url,
845 VARIANT async, VARIANT user, VARIANT password)
847 static const WCHAR MethodGetW[] = {'G','E','T',0};
848 static const WCHAR MethodPutW[] = {'P','U','T',0};
849 static const WCHAR MethodPostW[] = {'P','O','S','T',0};
850 static const WCHAR MethodDeleteW[] = {'D','E','L','E','T','E',0};
851 static const WCHAR MethodPropFindW[] = {'P','R','O','P','F','I','N','D',0};
852 VARIANT str, is_async;
853 IUri *uri;
854 HRESULT hr;
856 if (!method || !url) return E_INVALIDARG;
858 /* free previously set data */
859 if(This->uri) {
860 IUri_Release(This->uri);
861 This->uri = NULL;
864 SysFreeString(This->user);
865 SysFreeString(This->password);
866 This->user = This->password = NULL;
868 if (!strcmpiW(method, MethodGetW))
870 This->verb = BINDVERB_GET;
872 else if (!strcmpiW(method, MethodPutW))
874 This->verb = BINDVERB_PUT;
876 else if (!strcmpiW(method, MethodPostW))
878 This->verb = BINDVERB_POST;
880 else if (!strcmpiW(method, MethodDeleteW) ||
881 !strcmpiW(method, MethodPropFindW))
883 This->verb = BINDVERB_CUSTOM;
884 SysReAllocString(&This->custom, method);
886 else
888 FIXME("unsupported request type %s\n", debugstr_w(method));
889 This->verb = -1;
890 return E_FAIL;
893 if(This->base_uri)
894 hr = CoInternetCombineUrlEx(This->base_uri, url, 0, &uri, 0);
895 else
896 hr = CreateUri(url, 0, 0, &uri);
897 if(FAILED(hr)) {
898 WARN("Could not create IUri object: %08x\n", hr);
899 return hr;
902 hr = verify_uri(This, uri);
903 if(FAILED(hr)) {
904 IUri_Release(uri);
905 return hr;
908 VariantInit(&str);
909 hr = VariantChangeType(&str, &user, 0, VT_BSTR);
910 if (hr == S_OK)
911 This->user = V_BSTR(&str);
913 VariantInit(&str);
914 hr = VariantChangeType(&str, &password, 0, VT_BSTR);
915 if (hr == S_OK)
916 This->password = V_BSTR(&str);
918 /* add authentication info */
919 if (This->user && *This->user)
921 IUriBuilder *builder;
923 hr = CreateIUriBuilder(uri, 0, 0, &builder);
924 if (hr == S_OK)
926 IUri *full_uri;
928 IUriBuilder_SetUserName(builder, This->user);
929 IUriBuilder_SetPassword(builder, This->password);
930 hr = IUriBuilder_CreateUri(builder, -1, 0, 0, &full_uri);
931 if (hr == S_OK)
933 IUri_Release(uri);
934 uri = full_uri;
936 else
937 WARN("failed to create modified uri, 0x%08x\n", hr);
938 IUriBuilder_Release(builder);
940 else
941 WARN("IUriBuilder creation failed, 0x%08x\n", hr);
944 This->uri = uri;
946 VariantInit(&is_async);
947 hr = VariantChangeType(&is_async, &async, 0, VT_BOOL);
948 This->async = hr == S_OK && V_BOOL(&is_async);
950 httprequest_setreadystate(This, READYSTATE_LOADING);
952 return S_OK;
955 static HRESULT httprequest_setRequestHeader(httprequest *This, BSTR header, BSTR value)
957 struct httpheader *entry;
959 if (!header || !*header) return E_INVALIDARG;
960 if (This->state != READYSTATE_LOADING) return E_FAIL;
961 if (!value) return E_INVALIDARG;
963 /* replace existing header value if already added */
964 LIST_FOR_EACH_ENTRY(entry, &This->reqheaders, struct httpheader, entry)
966 if (lstrcmpW(entry->header, header) == 0)
968 LONG length = SysStringLen(entry->value);
969 HRESULT hr;
971 hr = SysReAllocString(&entry->value, value) ? S_OK : E_OUTOFMEMORY;
973 if (hr == S_OK)
974 This->reqheader_size += (SysStringLen(entry->value) - length);
976 return hr;
980 entry = heap_alloc(sizeof(*entry));
981 if (!entry) return E_OUTOFMEMORY;
983 /* new header */
984 entry->header = SysAllocString(header);
985 entry->value = SysAllocString(value);
987 /* header length including null terminator */
988 This->reqheader_size += SysStringLen(entry->header) + sizeof(colspaceW)/sizeof(WCHAR) +
989 SysStringLen(entry->value) + sizeof(crlfW)/sizeof(WCHAR) - 1;
991 list_add_head(&This->reqheaders, &entry->entry);
993 return S_OK;
996 static HRESULT httprequest_getResponseHeader(httprequest *This, BSTR header, BSTR *value)
998 struct httpheader *entry;
1000 if (!header) return E_INVALIDARG;
1001 if (!value) return E_POINTER;
1003 if (This->raw_respheaders && list_empty(&This->respheaders))
1005 WCHAR *ptr, *line;
1007 ptr = line = This->raw_respheaders;
1008 while (*ptr)
1010 if (*ptr == '\r' && *(ptr+1) == '\n')
1012 add_response_header(This, line, ptr-line);
1013 ptr++; line = ++ptr;
1014 continue;
1016 ptr++;
1020 LIST_FOR_EACH_ENTRY(entry, &This->respheaders, struct httpheader, entry)
1022 if (!strcmpiW(entry->header, header))
1024 *value = SysAllocString(entry->value);
1025 TRACE("header value %s\n", debugstr_w(*value));
1026 return S_OK;
1030 return S_FALSE;
1033 static HRESULT httprequest_getAllResponseHeaders(httprequest *This, BSTR *respheaders)
1035 if (!respheaders) return E_POINTER;
1037 *respheaders = SysAllocString(This->raw_respheaders);
1039 return S_OK;
1042 static HRESULT httprequest_send(httprequest *This, VARIANT body)
1044 BindStatusCallback *bsc = NULL;
1045 HRESULT hr;
1047 if (This->state != READYSTATE_LOADING) return E_FAIL;
1049 hr = BindStatusCallback_create(This, &bsc, &body);
1050 if (FAILED(hr))
1051 /* success path to detach it is OnStopBinding call */
1052 BindStatusCallback_Detach(bsc);
1054 return hr;
1057 static HRESULT httprequest_abort(httprequest *This)
1059 BindStatusCallback_Detach(This->bsc);
1061 httprequest_setreadystate(This, READYSTATE_UNINITIALIZED);
1063 return S_OK;
1066 static HRESULT httprequest_get_status(httprequest *This, LONG *status)
1068 if (!status) return E_POINTER;
1070 *status = This->status;
1072 return This->state == READYSTATE_COMPLETE ? S_OK : E_FAIL;
1075 static HRESULT httprequest_get_statusText(httprequest *This, BSTR *status)
1077 if (!status) return E_POINTER;
1078 if (This->state != READYSTATE_COMPLETE) return E_FAIL;
1080 *status = SysAllocString(This->status_text);
1082 return S_OK;
1085 static HRESULT httprequest_get_responseText(httprequest *This, BSTR *body)
1087 HGLOBAL hglobal;
1088 HRESULT hr;
1090 if (!body) return E_POINTER;
1091 if (This->state != READYSTATE_COMPLETE) return E_FAIL;
1093 hr = GetHGlobalFromStream(This->bsc->stream, &hglobal);
1094 if (hr == S_OK)
1096 xmlChar *ptr = GlobalLock(hglobal);
1097 DWORD size = GlobalSize(hglobal);
1098 xmlCharEncoding encoding = XML_CHAR_ENCODING_UTF8;
1100 /* try to determine data encoding */
1101 if (size >= 4)
1103 encoding = xmlDetectCharEncoding(ptr, 4);
1104 TRACE("detected encoding: %s\n", debugstr_a(xmlGetCharEncodingName(encoding)));
1105 if ( encoding != XML_CHAR_ENCODING_UTF8 &&
1106 encoding != XML_CHAR_ENCODING_UTF16LE &&
1107 encoding != XML_CHAR_ENCODING_NONE )
1109 FIXME("unsupported encoding: %s\n", debugstr_a(xmlGetCharEncodingName(encoding)));
1110 GlobalUnlock(hglobal);
1111 return E_FAIL;
1115 /* without BOM assume UTF-8 */
1116 if (encoding == XML_CHAR_ENCODING_UTF8 ||
1117 encoding == XML_CHAR_ENCODING_NONE )
1119 DWORD length = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)ptr, size, NULL, 0);
1121 *body = SysAllocStringLen(NULL, length);
1122 if (*body)
1123 MultiByteToWideChar( CP_UTF8, 0, (LPCSTR)ptr, size, *body, length);
1125 else
1126 *body = SysAllocStringByteLen((LPCSTR)ptr, size);
1128 if (!*body) hr = E_OUTOFMEMORY;
1129 GlobalUnlock(hglobal);
1132 return hr;
1135 static HRESULT httprequest_get_responseXML(httprequest *This, IDispatch **body)
1137 IXMLDOMDocument3 *doc;
1138 HRESULT hr;
1139 BSTR str;
1141 if (!body) return E_INVALIDARG;
1142 if (This->state != READYSTATE_COMPLETE) return E_FAIL;
1144 hr = DOMDocument_create(MSXML_DEFAULT, NULL, (void**)&doc);
1145 if (hr != S_OK) return hr;
1147 hr = httprequest_get_responseText(This, &str);
1148 if (hr == S_OK)
1150 VARIANT_BOOL ok;
1152 hr = IXMLDOMDocument3_loadXML(doc, str, &ok);
1153 SysFreeString(str);
1156 IXMLDOMDocument3_QueryInterface(doc, &IID_IDispatch, (void**)body);
1157 IXMLDOMDocument3_Release(doc);
1159 return hr;
1162 static HRESULT httprequest_get_responseBody(httprequest *This, VARIANT *body)
1164 HGLOBAL hglobal;
1165 HRESULT hr;
1167 if (!body) return E_INVALIDARG;
1168 V_VT(body) = VT_EMPTY;
1170 if (This->state != READYSTATE_COMPLETE) return E_PENDING;
1172 hr = GetHGlobalFromStream(This->bsc->stream, &hglobal);
1173 if (hr == S_OK)
1175 void *ptr = GlobalLock(hglobal);
1176 DWORD size = GlobalSize(hglobal);
1178 SAFEARRAYBOUND bound;
1179 SAFEARRAY *array;
1181 bound.lLbound = 0;
1182 bound.cElements = size;
1183 array = SafeArrayCreate(VT_UI1, 1, &bound);
1185 if (array)
1187 void *dest;
1189 V_VT(body) = VT_ARRAY | VT_UI1;
1190 V_ARRAY(body) = array;
1192 hr = SafeArrayAccessData(array, &dest);
1193 if (hr == S_OK)
1195 memcpy(dest, ptr, size);
1196 SafeArrayUnaccessData(array);
1198 else
1200 VariantClear(body);
1203 else
1204 hr = E_FAIL;
1206 GlobalUnlock(hglobal);
1209 return hr;
1212 static HRESULT httprequest_get_responseStream(httprequest *This, VARIANT *body)
1214 LARGE_INTEGER move;
1215 IStream *stream;
1216 HRESULT hr;
1218 if (!body) return E_INVALIDARG;
1219 V_VT(body) = VT_EMPTY;
1221 if (This->state != READYSTATE_COMPLETE) return E_PENDING;
1223 hr = IStream_Clone(This->bsc->stream, &stream);
1225 move.QuadPart = 0;
1226 IStream_Seek(stream, move, STREAM_SEEK_SET, NULL);
1228 V_VT(body) = VT_UNKNOWN;
1229 V_UNKNOWN(body) = (IUnknown*)stream;
1231 return hr;
1234 static HRESULT httprequest_get_readyState(httprequest *This, LONG *state)
1236 if (!state) return E_POINTER;
1238 *state = This->state;
1239 return S_OK;
1242 static HRESULT httprequest_put_onreadystatechange(httprequest *This, IDispatch *sink)
1244 if (This->sink) IDispatch_Release(This->sink);
1245 if ((This->sink = sink)) IDispatch_AddRef(This->sink);
1247 return S_OK;
1250 static void httprequest_release(httprequest *This)
1252 struct httpheader *header, *header2;
1254 if (This->site)
1255 IUnknown_Release( This->site );
1256 if (This->uri)
1257 IUri_Release(This->uri);
1258 if (This->base_uri)
1259 IUri_Release(This->base_uri);
1261 SysFreeString(This->custom);
1262 SysFreeString(This->user);
1263 SysFreeString(This->password);
1265 /* request headers */
1266 LIST_FOR_EACH_ENTRY_SAFE(header, header2, &This->reqheaders, struct httpheader, entry)
1268 list_remove(&header->entry);
1269 SysFreeString(header->header);
1270 SysFreeString(header->value);
1271 heap_free(header);
1273 /* response headers */
1274 free_response_headers(This);
1275 SysFreeString(This->status_text);
1277 /* detach callback object */
1278 BindStatusCallback_Detach(This->bsc);
1280 if (This->sink) IDispatch_Release(This->sink);
1283 static HRESULT WINAPI XMLHTTPRequest_QueryInterface(IXMLHTTPRequest *iface, REFIID riid, void **ppvObject)
1285 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1286 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppvObject);
1288 if ( IsEqualGUID( riid, &IID_IXMLHTTPRequest) ||
1289 IsEqualGUID( riid, &IID_IDispatch) ||
1290 IsEqualGUID( riid, &IID_IUnknown) )
1292 *ppvObject = iface;
1294 else if (IsEqualGUID(&IID_IObjectWithSite, riid))
1296 *ppvObject = &This->IObjectWithSite_iface;
1298 else if (IsEqualGUID(&IID_IObjectSafety, riid))
1300 *ppvObject = &This->IObjectSafety_iface;
1302 else
1304 TRACE("Unsupported interface %s\n", debugstr_guid(riid));
1305 *ppvObject = NULL;
1306 return E_NOINTERFACE;
1309 IXMLHTTPRequest_AddRef( iface );
1311 return S_OK;
1314 static ULONG WINAPI XMLHTTPRequest_AddRef(IXMLHTTPRequest *iface)
1316 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1317 ULONG ref = InterlockedIncrement( &This->ref );
1318 TRACE("(%p)->(%u)\n", This, ref );
1319 return ref;
1322 static ULONG WINAPI XMLHTTPRequest_Release(IXMLHTTPRequest *iface)
1324 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1325 ULONG ref = InterlockedDecrement( &This->ref );
1327 TRACE("(%p)->(%u)\n", This, ref );
1329 if ( ref == 0 )
1331 httprequest_release( This );
1332 heap_free( This );
1335 return ref;
1338 static HRESULT WINAPI XMLHTTPRequest_GetTypeInfoCount(IXMLHTTPRequest *iface, UINT *pctinfo)
1340 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1342 TRACE("(%p)->(%p)\n", This, pctinfo);
1344 *pctinfo = 1;
1346 return S_OK;
1349 static HRESULT WINAPI XMLHTTPRequest_GetTypeInfo(IXMLHTTPRequest *iface, UINT iTInfo,
1350 LCID lcid, ITypeInfo **ppTInfo)
1352 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1354 TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
1356 return get_typeinfo(IXMLHTTPRequest_tid, ppTInfo);
1359 static HRESULT WINAPI XMLHTTPRequest_GetIDsOfNames(IXMLHTTPRequest *iface, REFIID riid,
1360 LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
1362 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1363 ITypeInfo *typeinfo;
1364 HRESULT hr;
1366 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
1367 lcid, rgDispId);
1369 if(!rgszNames || cNames == 0 || !rgDispId)
1370 return E_INVALIDARG;
1372 hr = get_typeinfo(IXMLHTTPRequest_tid, &typeinfo);
1373 if(SUCCEEDED(hr))
1375 hr = ITypeInfo_GetIDsOfNames(typeinfo, rgszNames, cNames, rgDispId);
1376 ITypeInfo_Release(typeinfo);
1379 return hr;
1382 static HRESULT WINAPI XMLHTTPRequest_Invoke(IXMLHTTPRequest *iface, DISPID dispIdMember, REFIID riid,
1383 LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult,
1384 EXCEPINFO *pExcepInfo, UINT *puArgErr)
1386 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1387 ITypeInfo *typeinfo;
1388 HRESULT hr;
1390 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
1391 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
1393 hr = get_typeinfo(IXMLHTTPRequest_tid, &typeinfo);
1394 if(SUCCEEDED(hr))
1396 hr = ITypeInfo_Invoke(typeinfo, &This->IXMLHTTPRequest_iface, dispIdMember, wFlags,
1397 pDispParams, pVarResult, pExcepInfo, puArgErr);
1398 ITypeInfo_Release(typeinfo);
1401 return hr;
1404 static HRESULT WINAPI XMLHTTPRequest_open(IXMLHTTPRequest *iface, BSTR method, BSTR url,
1405 VARIANT async, VARIANT user, VARIANT password)
1407 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1408 TRACE("(%p)->(%s %s %s)\n", This, debugstr_w(method), debugstr_w(url),
1409 debugstr_variant(&async));
1410 return httprequest_open(This, method, url, async, user, password);
1413 static HRESULT WINAPI XMLHTTPRequest_setRequestHeader(IXMLHTTPRequest *iface, BSTR header, BSTR value)
1415 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1416 TRACE("(%p)->(%s %s)\n", This, debugstr_w(header), debugstr_w(value));
1417 return httprequest_setRequestHeader(This, header, value);
1420 static HRESULT WINAPI XMLHTTPRequest_getResponseHeader(IXMLHTTPRequest *iface, BSTR header, BSTR *value)
1422 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1423 TRACE("(%p)->(%s %p)\n", This, debugstr_w(header), value);
1424 return httprequest_getResponseHeader(This, header, value);
1427 static HRESULT WINAPI XMLHTTPRequest_getAllResponseHeaders(IXMLHTTPRequest *iface, BSTR *respheaders)
1429 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1430 TRACE("(%p)->(%p)\n", This, respheaders);
1431 return httprequest_getAllResponseHeaders(This, respheaders);
1434 static HRESULT WINAPI XMLHTTPRequest_send(IXMLHTTPRequest *iface, VARIANT body)
1436 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1437 TRACE("(%p)->(%s)\n", This, debugstr_variant(&body));
1438 return httprequest_send(This, body);
1441 static HRESULT WINAPI XMLHTTPRequest_abort(IXMLHTTPRequest *iface)
1443 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1444 TRACE("(%p)\n", This);
1445 return httprequest_abort(This);
1448 static HRESULT WINAPI XMLHTTPRequest_get_status(IXMLHTTPRequest *iface, LONG *status)
1450 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1451 TRACE("(%p)->(%p)\n", This, status);
1452 return httprequest_get_status(This, status);
1455 static HRESULT WINAPI XMLHTTPRequest_get_statusText(IXMLHTTPRequest *iface, BSTR *status)
1457 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1458 TRACE("(%p)->(%p)\n", This, status);
1459 return httprequest_get_statusText(This, status);
1462 static HRESULT WINAPI XMLHTTPRequest_get_responseXML(IXMLHTTPRequest *iface, IDispatch **body)
1464 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1465 TRACE("(%p)->(%p)\n", This, body);
1466 return httprequest_get_responseXML(This, body);
1469 static HRESULT WINAPI XMLHTTPRequest_get_responseText(IXMLHTTPRequest *iface, BSTR *body)
1471 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1472 TRACE("(%p)->(%p)\n", This, body);
1473 return httprequest_get_responseText(This, body);
1476 static HRESULT WINAPI XMLHTTPRequest_get_responseBody(IXMLHTTPRequest *iface, VARIANT *body)
1478 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1479 TRACE("(%p)->(%p)\n", This, body);
1480 return httprequest_get_responseBody(This, body);
1483 static HRESULT WINAPI XMLHTTPRequest_get_responseStream(IXMLHTTPRequest *iface, VARIANT *body)
1485 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1486 TRACE("(%p)->(%p)\n", This, body);
1487 return httprequest_get_responseStream(This, body);
1490 static HRESULT WINAPI XMLHTTPRequest_get_readyState(IXMLHTTPRequest *iface, LONG *state)
1492 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1493 TRACE("(%p)->(%p)\n", This, state);
1494 return httprequest_get_readyState(This, state);
1497 static HRESULT WINAPI XMLHTTPRequest_put_onreadystatechange(IXMLHTTPRequest *iface, IDispatch *sink)
1499 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1500 TRACE("(%p)->(%p)\n", This, sink);
1501 return httprequest_put_onreadystatechange(This, sink);
1504 static const struct IXMLHTTPRequestVtbl XMLHTTPRequestVtbl =
1506 XMLHTTPRequest_QueryInterface,
1507 XMLHTTPRequest_AddRef,
1508 XMLHTTPRequest_Release,
1509 XMLHTTPRequest_GetTypeInfoCount,
1510 XMLHTTPRequest_GetTypeInfo,
1511 XMLHTTPRequest_GetIDsOfNames,
1512 XMLHTTPRequest_Invoke,
1513 XMLHTTPRequest_open,
1514 XMLHTTPRequest_setRequestHeader,
1515 XMLHTTPRequest_getResponseHeader,
1516 XMLHTTPRequest_getAllResponseHeaders,
1517 XMLHTTPRequest_send,
1518 XMLHTTPRequest_abort,
1519 XMLHTTPRequest_get_status,
1520 XMLHTTPRequest_get_statusText,
1521 XMLHTTPRequest_get_responseXML,
1522 XMLHTTPRequest_get_responseText,
1523 XMLHTTPRequest_get_responseBody,
1524 XMLHTTPRequest_get_responseStream,
1525 XMLHTTPRequest_get_readyState,
1526 XMLHTTPRequest_put_onreadystatechange
1529 /* IObjectWithSite */
1530 static HRESULT WINAPI
1531 httprequest_ObjectWithSite_QueryInterface( IObjectWithSite* iface, REFIID riid, void** ppvObject )
1533 httprequest *This = impl_from_IObjectWithSite(iface);
1534 return IXMLHTTPRequest_QueryInterface( (IXMLHTTPRequest *)This, riid, ppvObject );
1537 static ULONG WINAPI httprequest_ObjectWithSite_AddRef( IObjectWithSite* iface )
1539 httprequest *This = impl_from_IObjectWithSite(iface);
1540 return IXMLHTTPRequest_AddRef((IXMLHTTPRequest *)This);
1543 static ULONG WINAPI httprequest_ObjectWithSite_Release( IObjectWithSite* iface )
1545 httprequest *This = impl_from_IObjectWithSite(iface);
1546 return IXMLHTTPRequest_Release((IXMLHTTPRequest *)This);
1549 static HRESULT WINAPI httprequest_ObjectWithSite_GetSite( IObjectWithSite *iface, REFIID iid, void **ppvSite )
1551 httprequest *This = impl_from_IObjectWithSite(iface);
1553 TRACE("(%p)->(%s %p)\n", This, debugstr_guid( iid ), ppvSite );
1555 if ( !This->site )
1556 return E_FAIL;
1558 return IUnknown_QueryInterface( This->site, iid, ppvSite );
1561 static void get_base_uri(httprequest *This)
1563 IServiceProvider *provider;
1564 IHTMLDocument2 *doc;
1565 IUri *uri;
1566 BSTR url;
1567 HRESULT hr;
1569 hr = IUnknown_QueryInterface(This->site, &IID_IServiceProvider, (void**)&provider);
1570 if(FAILED(hr))
1571 return;
1573 hr = IServiceProvider_QueryService(provider, &SID_SContainerDispatch, &IID_IHTMLDocument2, (void**)&doc);
1574 IServiceProvider_Release(provider);
1575 if(FAILED(hr))
1576 return;
1578 hr = IHTMLDocument2_get_URL(doc, &url);
1579 IHTMLDocument2_Release(doc);
1580 if(FAILED(hr) || !url || !*url)
1581 return;
1583 TRACE("host url %s\n", debugstr_w(url));
1585 hr = CreateUri(url, 0, 0, &uri);
1586 SysFreeString(url);
1587 if(FAILED(hr))
1588 return;
1590 This->base_uri = uri;
1593 static HRESULT WINAPI httprequest_ObjectWithSite_SetSite( IObjectWithSite *iface, IUnknown *punk )
1595 httprequest *This = impl_from_IObjectWithSite(iface);
1597 TRACE("(%p)->(%p)\n", This, punk);
1599 if(This->site)
1600 IUnknown_Release( This->site );
1601 if(This->base_uri)
1602 IUri_Release(This->base_uri);
1604 This->site = punk;
1606 if (punk)
1608 IUnknown_AddRef( punk );
1609 get_base_uri(This);
1612 return S_OK;
1615 static const IObjectWithSiteVtbl ObjectWithSiteVtbl =
1617 httprequest_ObjectWithSite_QueryInterface,
1618 httprequest_ObjectWithSite_AddRef,
1619 httprequest_ObjectWithSite_Release,
1620 httprequest_ObjectWithSite_SetSite,
1621 httprequest_ObjectWithSite_GetSite
1624 /* IObjectSafety */
1625 static HRESULT WINAPI httprequest_Safety_QueryInterface(IObjectSafety *iface, REFIID riid, void **ppv)
1627 httprequest *This = impl_from_IObjectSafety(iface);
1628 return IXMLHTTPRequest_QueryInterface( (IXMLHTTPRequest *)This, riid, ppv );
1631 static ULONG WINAPI httprequest_Safety_AddRef(IObjectSafety *iface)
1633 httprequest *This = impl_from_IObjectSafety(iface);
1634 return IXMLHTTPRequest_AddRef((IXMLHTTPRequest *)This);
1637 static ULONG WINAPI httprequest_Safety_Release(IObjectSafety *iface)
1639 httprequest *This = impl_from_IObjectSafety(iface);
1640 return IXMLHTTPRequest_Release((IXMLHTTPRequest *)This);
1643 static HRESULT WINAPI httprequest_Safety_GetInterfaceSafetyOptions(IObjectSafety *iface, REFIID riid,
1644 DWORD *supported, DWORD *enabled)
1646 httprequest *This = impl_from_IObjectSafety(iface);
1648 TRACE("(%p)->(%s %p %p)\n", This, debugstr_guid(riid), supported, enabled);
1650 if(!supported || !enabled) return E_POINTER;
1652 *supported = safety_supported_options;
1653 *enabled = This->safeopt;
1655 return S_OK;
1658 static HRESULT WINAPI httprequest_Safety_SetInterfaceSafetyOptions(IObjectSafety *iface, REFIID riid,
1659 DWORD mask, DWORD enabled)
1661 httprequest *This = impl_from_IObjectSafety(iface);
1662 TRACE("(%p)->(%s %x %x)\n", This, debugstr_guid(riid), mask, enabled);
1664 if ((mask & ~safety_supported_options))
1665 return E_FAIL;
1667 This->safeopt = (This->safeopt & ~mask) | (mask & enabled);
1669 return S_OK;
1672 static const IObjectSafetyVtbl ObjectSafetyVtbl = {
1673 httprequest_Safety_QueryInterface,
1674 httprequest_Safety_AddRef,
1675 httprequest_Safety_Release,
1676 httprequest_Safety_GetInterfaceSafetyOptions,
1677 httprequest_Safety_SetInterfaceSafetyOptions
1680 /* IServerXMLHTTPRequest */
1681 static HRESULT WINAPI ServerXMLHTTPRequest_QueryInterface(IServerXMLHTTPRequest *iface, REFIID riid, void **obj)
1683 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1685 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), obj);
1687 if ( IsEqualGUID( riid, &IID_IServerXMLHTTPRequest) ||
1688 IsEqualGUID( riid, &IID_IXMLHTTPRequest) ||
1689 IsEqualGUID( riid, &IID_IDispatch) ||
1690 IsEqualGUID( riid, &IID_IUnknown) )
1692 *obj = iface;
1694 else
1696 TRACE("Unsupported interface %s\n", debugstr_guid(riid));
1697 *obj = NULL;
1698 return E_NOINTERFACE;
1701 IServerXMLHTTPRequest_AddRef( iface );
1703 return S_OK;
1706 static ULONG WINAPI ServerXMLHTTPRequest_AddRef(IServerXMLHTTPRequest *iface)
1708 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1709 ULONG ref = InterlockedIncrement( &This->ref );
1710 TRACE("(%p)->(%u)\n", This, ref );
1711 return ref;
1714 static ULONG WINAPI ServerXMLHTTPRequest_Release(IServerXMLHTTPRequest *iface)
1716 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1717 ULONG ref = InterlockedDecrement( &This->ref );
1719 TRACE("(%p)->(%u)\n", This, ref );
1721 if ( ref == 0 )
1723 httprequest_release( &This->req );
1724 heap_free( This );
1727 return ref;
1730 static HRESULT WINAPI ServerXMLHTTPRequest_GetTypeInfoCount(IServerXMLHTTPRequest *iface, UINT *pctinfo)
1732 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1734 TRACE("(%p)->(%p)\n", This, pctinfo);
1735 *pctinfo = 1;
1737 return S_OK;
1740 static HRESULT WINAPI ServerXMLHTTPRequest_GetTypeInfo(IServerXMLHTTPRequest *iface, UINT iTInfo,
1741 LCID lcid, ITypeInfo **ppTInfo)
1743 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1745 TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
1747 return get_typeinfo(IServerXMLHTTPRequest_tid, ppTInfo);
1750 static HRESULT WINAPI ServerXMLHTTPRequest_GetIDsOfNames(IServerXMLHTTPRequest *iface, REFIID riid,
1751 LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
1753 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1754 ITypeInfo *typeinfo;
1755 HRESULT hr;
1757 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
1758 lcid, rgDispId);
1760 if(!rgszNames || cNames == 0 || !rgDispId)
1761 return E_INVALIDARG;
1763 hr = get_typeinfo(IServerXMLHTTPRequest_tid, &typeinfo);
1764 if(SUCCEEDED(hr))
1766 hr = ITypeInfo_GetIDsOfNames(typeinfo, rgszNames, cNames, rgDispId);
1767 ITypeInfo_Release(typeinfo);
1770 return hr;
1773 static HRESULT WINAPI ServerXMLHTTPRequest_Invoke(IServerXMLHTTPRequest *iface, DISPID dispIdMember, REFIID riid,
1774 LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult,
1775 EXCEPINFO *pExcepInfo, UINT *puArgErr)
1777 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1778 ITypeInfo *typeinfo;
1779 HRESULT hr;
1781 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
1782 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
1784 hr = get_typeinfo(IServerXMLHTTPRequest_tid, &typeinfo);
1785 if(SUCCEEDED(hr))
1787 hr = ITypeInfo_Invoke(typeinfo, &This->IServerXMLHTTPRequest_iface, dispIdMember, wFlags,
1788 pDispParams, pVarResult, pExcepInfo, puArgErr);
1789 ITypeInfo_Release(typeinfo);
1792 return hr;
1795 static HRESULT WINAPI ServerXMLHTTPRequest_open(IServerXMLHTTPRequest *iface, BSTR method, BSTR url,
1796 VARIANT async, VARIANT user, VARIANT password)
1798 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1799 TRACE("(%p)->(%s %s %s)\n", This, debugstr_w(method), debugstr_w(url),
1800 debugstr_variant(&async));
1801 return httprequest_open(&This->req, method, url, async, user, password);
1804 static HRESULT WINAPI ServerXMLHTTPRequest_setRequestHeader(IServerXMLHTTPRequest *iface, BSTR header, BSTR value)
1806 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1807 TRACE("(%p)->(%s %s)\n", This, debugstr_w(header), debugstr_w(value));
1808 return httprequest_setRequestHeader(&This->req, header, value);
1811 static HRESULT WINAPI ServerXMLHTTPRequest_getResponseHeader(IServerXMLHTTPRequest *iface, BSTR header, BSTR *value)
1813 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1814 TRACE("(%p)->(%s %p)\n", This, debugstr_w(header), value);
1815 return httprequest_getResponseHeader(&This->req, header, value);
1818 static HRESULT WINAPI ServerXMLHTTPRequest_getAllResponseHeaders(IServerXMLHTTPRequest *iface, BSTR *respheaders)
1820 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1821 TRACE("(%p)->(%p)\n", This, respheaders);
1822 return httprequest_getAllResponseHeaders(&This->req, respheaders);
1825 static HRESULT WINAPI ServerXMLHTTPRequest_send(IServerXMLHTTPRequest *iface, VARIANT body)
1827 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1828 TRACE("(%p)->(%s)\n", This, debugstr_variant(&body));
1829 return httprequest_send(&This->req, body);
1832 static HRESULT WINAPI ServerXMLHTTPRequest_abort(IServerXMLHTTPRequest *iface)
1834 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1835 TRACE("(%p)\n", This);
1836 return httprequest_abort(&This->req);
1839 static HRESULT WINAPI ServerXMLHTTPRequest_get_status(IServerXMLHTTPRequest *iface, LONG *status)
1841 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1842 TRACE("(%p)->(%p)\n", This, status);
1843 return httprequest_get_status(&This->req, status);
1846 static HRESULT WINAPI ServerXMLHTTPRequest_get_statusText(IServerXMLHTTPRequest *iface, BSTR *status)
1848 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1849 TRACE("(%p)->(%p)\n", This, status);
1850 return httprequest_get_statusText(&This->req, status);
1853 static HRESULT WINAPI ServerXMLHTTPRequest_get_responseXML(IServerXMLHTTPRequest *iface, IDispatch **body)
1855 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1856 TRACE("(%p)->(%p)\n", This, body);
1857 return httprequest_get_responseXML(&This->req, body);
1860 static HRESULT WINAPI ServerXMLHTTPRequest_get_responseText(IServerXMLHTTPRequest *iface, BSTR *body)
1862 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1863 TRACE("(%p)->(%p)\n", This, body);
1864 return httprequest_get_responseText(&This->req, body);
1867 static HRESULT WINAPI ServerXMLHTTPRequest_get_responseBody(IServerXMLHTTPRequest *iface, VARIANT *body)
1869 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1870 TRACE("(%p)->(%p)\n", This, body);
1871 return httprequest_get_responseBody(&This->req, body);
1874 static HRESULT WINAPI ServerXMLHTTPRequest_get_responseStream(IServerXMLHTTPRequest *iface, VARIANT *body)
1876 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1877 TRACE("(%p)->(%p)\n", This, body);
1878 return httprequest_get_responseStream(&This->req, body);
1881 static HRESULT WINAPI ServerXMLHTTPRequest_get_readyState(IServerXMLHTTPRequest *iface, LONG *state)
1883 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1884 TRACE("(%p)->(%p)\n", This, state);
1885 return httprequest_get_readyState(&This->req, state);
1888 static HRESULT WINAPI ServerXMLHTTPRequest_put_onreadystatechange(IServerXMLHTTPRequest *iface, IDispatch *sink)
1890 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1891 TRACE("(%p)->(%p)\n", This, sink);
1892 return httprequest_put_onreadystatechange(&This->req, sink);
1895 static HRESULT WINAPI ServerXMLHTTPRequest_setTimeouts(IServerXMLHTTPRequest *iface, LONG resolveTimeout, LONG connectTimeout,
1896 LONG sendTimeout, LONG receiveTimeout)
1898 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1899 FIXME("(%p)->(%d %d %d %d): stub\n", This, resolveTimeout, connectTimeout, sendTimeout, receiveTimeout);
1900 return E_NOTIMPL;
1903 static HRESULT WINAPI ServerXMLHTTPRequest_waitForResponse(IServerXMLHTTPRequest *iface, VARIANT timeout, VARIANT_BOOL *isSuccessful)
1905 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1906 FIXME("(%p)->(%s %p): stub\n", This, debugstr_variant(&timeout), isSuccessful);
1907 return E_NOTIMPL;
1910 static HRESULT WINAPI ServerXMLHTTPRequest_getOption(IServerXMLHTTPRequest *iface, SERVERXMLHTTP_OPTION option, VARIANT *value)
1912 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1913 FIXME("(%p)->(%d %p): stub\n", This, option, value);
1914 return E_NOTIMPL;
1917 static HRESULT WINAPI ServerXMLHTTPRequest_setOption(IServerXMLHTTPRequest *iface, SERVERXMLHTTP_OPTION option, VARIANT value)
1919 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1920 FIXME("(%p)->(%d %s): stub\n", This, option, debugstr_variant(&value));
1921 return E_NOTIMPL;
1924 static const struct IServerXMLHTTPRequestVtbl ServerXMLHTTPRequestVtbl =
1926 ServerXMLHTTPRequest_QueryInterface,
1927 ServerXMLHTTPRequest_AddRef,
1928 ServerXMLHTTPRequest_Release,
1929 ServerXMLHTTPRequest_GetTypeInfoCount,
1930 ServerXMLHTTPRequest_GetTypeInfo,
1931 ServerXMLHTTPRequest_GetIDsOfNames,
1932 ServerXMLHTTPRequest_Invoke,
1933 ServerXMLHTTPRequest_open,
1934 ServerXMLHTTPRequest_setRequestHeader,
1935 ServerXMLHTTPRequest_getResponseHeader,
1936 ServerXMLHTTPRequest_getAllResponseHeaders,
1937 ServerXMLHTTPRequest_send,
1938 ServerXMLHTTPRequest_abort,
1939 ServerXMLHTTPRequest_get_status,
1940 ServerXMLHTTPRequest_get_statusText,
1941 ServerXMLHTTPRequest_get_responseXML,
1942 ServerXMLHTTPRequest_get_responseText,
1943 ServerXMLHTTPRequest_get_responseBody,
1944 ServerXMLHTTPRequest_get_responseStream,
1945 ServerXMLHTTPRequest_get_readyState,
1946 ServerXMLHTTPRequest_put_onreadystatechange,
1947 ServerXMLHTTPRequest_setTimeouts,
1948 ServerXMLHTTPRequest_waitForResponse,
1949 ServerXMLHTTPRequest_getOption,
1950 ServerXMLHTTPRequest_setOption
1953 static void init_httprequest(httprequest *req)
1955 req->IXMLHTTPRequest_iface.lpVtbl = &XMLHTTPRequestVtbl;
1956 req->IObjectWithSite_iface.lpVtbl = &ObjectWithSiteVtbl;
1957 req->IObjectSafety_iface.lpVtbl = &ObjectSafetyVtbl;
1958 req->ref = 1;
1960 req->async = FALSE;
1961 req->verb = -1;
1962 req->custom = NULL;
1963 req->uri = req->base_uri = NULL;
1964 req->user = req->password = NULL;
1966 req->state = READYSTATE_UNINITIALIZED;
1967 req->sink = NULL;
1969 req->bsc = NULL;
1970 req->status = 0;
1971 req->status_text = NULL;
1972 req->reqheader_size = 0;
1973 req->raw_respheaders = NULL;
1974 req->use_utf8_content = FALSE;
1976 list_init(&req->reqheaders);
1977 list_init(&req->respheaders);
1979 req->site = NULL;
1980 req->safeopt = 0;
1983 HRESULT XMLHTTPRequest_create(IUnknown *outer, void **obj)
1985 httprequest *req;
1987 TRACE("(%p, %p)\n", outer, obj);
1989 req = heap_alloc( sizeof (*req) );
1990 if( !req )
1991 return E_OUTOFMEMORY;
1993 init_httprequest(req);
1994 *obj = &req->IXMLHTTPRequest_iface;
1996 TRACE("returning iface %p\n", *obj);
1998 return S_OK;
2001 HRESULT ServerXMLHTTP_create(IUnknown *outer, void **obj)
2003 serverhttp *req;
2005 TRACE("(%p, %p)\n", outer, obj);
2007 req = heap_alloc( sizeof (*req) );
2008 if( !req )
2009 return E_OUTOFMEMORY;
2011 init_httprequest(&req->req);
2012 req->IServerXMLHTTPRequest_iface.lpVtbl = &ServerXMLHTTPRequestVtbl;
2013 req->ref = 1;
2015 *obj = &req->IServerXMLHTTPRequest_iface;
2017 TRACE("returning iface %p\n", *obj);
2019 return S_OK;
2022 #else
2024 HRESULT XMLHTTPRequest_create(IUnknown *pUnkOuter, void **ppObj)
2026 MESSAGE("This program tried to use a XMLHTTPRequest object, but\n"
2027 "libxml2 support was not present at compile time.\n");
2028 return E_NOTIMPL;
2031 HRESULT ServerXMLHTTP_create(IUnknown *outer, void **obj)
2033 MESSAGE("This program tried to use a ServerXMLHTTP object, but\n"
2034 "libxml2 support was not present at compile time.\n");
2035 return E_NOTIMPL;
2038 #endif