wined3d: Move the resource map count field up to wined3d_resource.
[wine/multimedia.git] / dlls / msxml3 / httprequest.c
blob0abe2ff37788fbe023cfed4ffd6481dd704562cf
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 BSTR siteurl;
86 BSTR url;
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;
145 This->state = state;
147 if (This->sink && last != state)
149 DISPPARAMS params;
151 memset(&params, 0, sizeof(params));
152 IDispatch_Invoke(This->sink, 0, &IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD, &params, 0, 0, 0);
156 static void free_response_headers(httprequest *This)
158 struct httpheader *header, *header2;
160 LIST_FOR_EACH_ENTRY_SAFE(header, header2, &This->respheaders, struct httpheader, entry)
162 list_remove(&header->entry);
163 SysFreeString(header->header);
164 SysFreeString(header->value);
165 heap_free(header);
168 SysFreeString(This->raw_respheaders);
169 This->raw_respheaders = NULL;
172 struct BindStatusCallback
174 IBindStatusCallback IBindStatusCallback_iface;
175 IHttpNegotiate IHttpNegotiate_iface;
176 IAuthenticate IAuthenticate_iface;
177 LONG ref;
179 IBinding *binding;
180 httprequest *request;
182 /* response data */
183 IStream *stream;
185 /* request body data */
186 HGLOBAL body;
189 static inline BindStatusCallback *impl_from_IBindStatusCallback( IBindStatusCallback *iface )
191 return CONTAINING_RECORD(iface, BindStatusCallback, IBindStatusCallback_iface);
194 static inline BindStatusCallback *impl_from_IHttpNegotiate( IHttpNegotiate *iface )
196 return CONTAINING_RECORD(iface, BindStatusCallback, IHttpNegotiate_iface);
199 static inline BindStatusCallback *impl_from_IAuthenticate( IAuthenticate *iface )
201 return CONTAINING_RECORD(iface, BindStatusCallback, IAuthenticate_iface);
204 static void BindStatusCallback_Detach(BindStatusCallback *bsc)
206 if (bsc)
208 if (bsc->binding) IBinding_Abort(bsc->binding);
209 bsc->request = NULL;
210 IBindStatusCallback_Release(&bsc->IBindStatusCallback_iface);
214 static HRESULT WINAPI BindStatusCallback_QueryInterface(IBindStatusCallback *iface,
215 REFIID riid, void **ppv)
217 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
219 *ppv = NULL;
221 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
223 if (IsEqualGUID(&IID_IUnknown, riid) ||
224 IsEqualGUID(&IID_IBindStatusCallback, riid))
226 *ppv = &This->IBindStatusCallback_iface;
228 else if (IsEqualGUID(&IID_IHttpNegotiate, riid))
230 *ppv = &This->IHttpNegotiate_iface;
232 else if (IsEqualGUID(&IID_IAuthenticate, riid))
234 *ppv = &This->IAuthenticate_iface;
236 else if (IsEqualGUID(&IID_IServiceProvider, riid) ||
237 IsEqualGUID(&IID_IBindStatusCallbackEx, riid) ||
238 IsEqualGUID(&IID_IInternetProtocol, riid) ||
239 IsEqualGUID(&IID_IHttpNegotiate2, riid))
241 return E_NOINTERFACE;
244 if (*ppv)
246 IBindStatusCallback_AddRef(iface);
247 return S_OK;
250 FIXME("Unsupported riid = %s\n", debugstr_guid(riid));
252 return E_NOINTERFACE;
255 static ULONG WINAPI BindStatusCallback_AddRef(IBindStatusCallback *iface)
257 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
258 LONG ref = InterlockedIncrement(&This->ref);
260 TRACE("(%p) ref = %d\n", This, ref);
262 return ref;
265 static ULONG WINAPI BindStatusCallback_Release(IBindStatusCallback *iface)
267 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
268 LONG ref = InterlockedDecrement(&This->ref);
270 TRACE("(%p) ref = %d\n", This, ref);
272 if (!ref)
274 if (This->binding) IBinding_Release(This->binding);
275 if (This->stream) IStream_Release(This->stream);
276 if (This->body) GlobalFree(This->body);
277 heap_free(This);
280 return ref;
283 static HRESULT WINAPI BindStatusCallback_OnStartBinding(IBindStatusCallback *iface,
284 DWORD reserved, IBinding *pbind)
286 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
288 TRACE("(%p)->(%d %p)\n", This, reserved, pbind);
290 if (!pbind) return E_INVALIDARG;
292 This->binding = pbind;
293 IBinding_AddRef(pbind);
295 httprequest_setreadystate(This->request, READYSTATE_LOADED);
297 return CreateStreamOnHGlobal(NULL, TRUE, &This->stream);
300 static HRESULT WINAPI BindStatusCallback_GetPriority(IBindStatusCallback *iface, LONG *pPriority)
302 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
304 TRACE("(%p)->(%p)\n", This, pPriority);
306 return E_NOTIMPL;
309 static HRESULT WINAPI BindStatusCallback_OnLowResource(IBindStatusCallback *iface, DWORD reserved)
311 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
313 TRACE("(%p)->(%d)\n", This, reserved);
315 return E_NOTIMPL;
318 static HRESULT WINAPI BindStatusCallback_OnProgress(IBindStatusCallback *iface, ULONG ulProgress,
319 ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
321 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
323 TRACE("(%p)->(%u %u %u %s)\n", This, ulProgress, ulProgressMax, ulStatusCode,
324 debugstr_w(szStatusText));
326 return S_OK;
329 static HRESULT WINAPI BindStatusCallback_OnStopBinding(IBindStatusCallback *iface,
330 HRESULT hr, LPCWSTR error)
332 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
334 TRACE("(%p)->(0x%08x %s)\n", This, hr, debugstr_w(error));
336 if (This->binding)
338 IBinding_Release(This->binding);
339 This->binding = NULL;
342 if (hr == S_OK)
343 httprequest_setreadystate(This->request, READYSTATE_COMPLETE);
345 return S_OK;
348 static HRESULT WINAPI BindStatusCallback_GetBindInfo(IBindStatusCallback *iface,
349 DWORD *bind_flags, BINDINFO *pbindinfo)
351 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
353 TRACE("(%p)->(%p %p)\n", This, bind_flags, pbindinfo);
355 *bind_flags = 0;
356 if (This->request->async) *bind_flags |= BINDF_ASYNCHRONOUS;
358 if (This->request->verb != BINDVERB_GET && This->body)
360 pbindinfo->stgmedData.tymed = TYMED_HGLOBAL;
361 pbindinfo->stgmedData.u.hGlobal = This->body;
362 pbindinfo->cbstgmedData = GlobalSize(This->body);
363 /* callback owns passed body pointer */
364 IBindStatusCallback_QueryInterface(iface, &IID_IUnknown, (void**)&pbindinfo->stgmedData.pUnkForRelease);
367 pbindinfo->dwBindVerb = This->request->verb;
368 if (This->request->verb == BINDVERB_CUSTOM)
370 pbindinfo->szCustomVerb = CoTaskMemAlloc(SysStringByteLen(This->request->custom));
371 strcpyW(pbindinfo->szCustomVerb, This->request->custom);
374 return S_OK;
377 static HRESULT WINAPI BindStatusCallback_OnDataAvailable(IBindStatusCallback *iface,
378 DWORD flags, DWORD size, FORMATETC *format, STGMEDIUM *stgmed)
380 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
381 DWORD read, written;
382 BYTE buf[4096];
383 HRESULT hr;
385 TRACE("(%p)->(%08x %d %p %p)\n", This, flags, size, format, stgmed);
389 hr = IStream_Read(stgmed->u.pstm, buf, sizeof(buf), &read);
390 if (hr != S_OK) break;
392 hr = IStream_Write(This->stream, buf, read, &written);
393 } while((hr == S_OK) && written != 0 && read != 0);
395 httprequest_setreadystate(This->request, READYSTATE_INTERACTIVE);
397 return S_OK;
400 static HRESULT WINAPI BindStatusCallback_OnObjectAvailable(IBindStatusCallback *iface,
401 REFIID riid, IUnknown *punk)
403 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
405 FIXME("(%p)->(%s %p): stub\n", This, debugstr_guid(riid), punk);
407 return E_NOTIMPL;
410 static const IBindStatusCallbackVtbl BindStatusCallbackVtbl = {
411 BindStatusCallback_QueryInterface,
412 BindStatusCallback_AddRef,
413 BindStatusCallback_Release,
414 BindStatusCallback_OnStartBinding,
415 BindStatusCallback_GetPriority,
416 BindStatusCallback_OnLowResource,
417 BindStatusCallback_OnProgress,
418 BindStatusCallback_OnStopBinding,
419 BindStatusCallback_GetBindInfo,
420 BindStatusCallback_OnDataAvailable,
421 BindStatusCallback_OnObjectAvailable
424 static HRESULT WINAPI BSCHttpNegotiate_QueryInterface(IHttpNegotiate *iface,
425 REFIID riid, void **ppv)
427 BindStatusCallback *This = impl_from_IHttpNegotiate(iface);
428 return IBindStatusCallback_QueryInterface(&This->IBindStatusCallback_iface, riid, ppv);
431 static ULONG WINAPI BSCHttpNegotiate_AddRef(IHttpNegotiate *iface)
433 BindStatusCallback *This = impl_from_IHttpNegotiate(iface);
434 return IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
437 static ULONG WINAPI BSCHttpNegotiate_Release(IHttpNegotiate *iface)
439 BindStatusCallback *This = impl_from_IHttpNegotiate(iface);
440 return IBindStatusCallback_Release(&This->IBindStatusCallback_iface);
443 static HRESULT WINAPI BSCHttpNegotiate_BeginningTransaction(IHttpNegotiate *iface,
444 LPCWSTR url, LPCWSTR headers, DWORD reserved, LPWSTR *add_headers)
446 static const WCHAR content_type_utf8W[] = {'C','o','n','t','e','n','t','-','T','y','p','e',':',' ',
447 't','e','x','t','/','p','l','a','i','n',';','c','h','a','r','s','e','t','=','u','t','f','-','8','\r','\n',0};
449 BindStatusCallback *This = impl_from_IHttpNegotiate(iface);
450 const struct httpheader *entry;
451 WCHAR *buff, *ptr;
452 int size = 0;
454 TRACE("(%p)->(%s %s %d %p)\n", This, debugstr_w(url), debugstr_w(headers), reserved, add_headers);
456 *add_headers = NULL;
458 if (This->request->use_utf8_content)
459 size = sizeof(content_type_utf8W);
461 if (!list_empty(&This->request->reqheaders))
462 size += This->request->reqheader_size*sizeof(WCHAR);
464 if (!size) return S_OK;
466 buff = CoTaskMemAlloc(size);
467 if (!buff) return E_OUTOFMEMORY;
469 ptr = buff;
470 if (This->request->use_utf8_content)
472 lstrcpyW(ptr, content_type_utf8W);
473 ptr += sizeof(content_type_utf8W)/sizeof(WCHAR)-1;
476 /* user headers */
477 LIST_FOR_EACH_ENTRY(entry, &This->request->reqheaders, struct httpheader, entry)
479 lstrcpyW(ptr, entry->header);
480 ptr += SysStringLen(entry->header);
482 lstrcpyW(ptr, colspaceW);
483 ptr += sizeof(colspaceW)/sizeof(WCHAR)-1;
485 lstrcpyW(ptr, entry->value);
486 ptr += SysStringLen(entry->value);
488 lstrcpyW(ptr, crlfW);
489 ptr += sizeof(crlfW)/sizeof(WCHAR)-1;
492 *add_headers = buff;
494 return S_OK;
497 static void add_response_header(httprequest *This, const WCHAR *data, int len)
499 struct httpheader *entry;
500 const WCHAR *ptr = data;
501 BSTR header, value;
503 while (*ptr)
505 if (*ptr == ':')
507 header = SysAllocStringLen(data, ptr-data);
508 /* skip leading spaces for a value */
509 while (*++ptr == ' ')
511 value = SysAllocStringLen(ptr, len-(ptr-data));
512 break;
514 ptr++;
517 if (!*ptr) return;
519 /* new header */
520 TRACE("got header %s:%s\n", debugstr_w(header), debugstr_w(value));
522 entry = heap_alloc(sizeof(*entry));
523 entry->header = header;
524 entry->value = value;
525 list_add_head(&This->respheaders, &entry->entry);
528 static HRESULT WINAPI BSCHttpNegotiate_OnResponse(IHttpNegotiate *iface, DWORD code,
529 LPCWSTR resp_headers, LPCWSTR req_headers, LPWSTR *add_reqheaders)
531 BindStatusCallback *This = impl_from_IHttpNegotiate(iface);
533 TRACE("(%p)->(%d %s %s %p)\n", This, code, debugstr_w(resp_headers),
534 debugstr_w(req_headers), add_reqheaders);
536 This->request->status = code;
537 /* store headers and status text */
538 free_response_headers(This->request);
539 SysFreeString(This->request->status_text);
540 This->request->status_text = NULL;
541 if (resp_headers)
543 const WCHAR *ptr, *line;
545 ptr = line = resp_headers;
547 /* skip status line */
548 while (*ptr)
550 if (*ptr == '\r' && *(ptr+1) == '\n')
552 const WCHAR *end = ptr-1;
553 line = ptr + 2;
554 /* scan back to get status phrase */
555 while (ptr > resp_headers)
557 if (*ptr == ' ')
559 This->request->status_text = SysAllocStringLen(ptr+1, end-ptr);
560 TRACE("status text %s\n", debugstr_w(This->request->status_text));
561 break;
563 ptr--;
565 break;
567 ptr++;
570 /* store as unparsed string for now */
571 This->request->raw_respheaders = SysAllocString(line);
574 return S_OK;
577 static const IHttpNegotiateVtbl BSCHttpNegotiateVtbl = {
578 BSCHttpNegotiate_QueryInterface,
579 BSCHttpNegotiate_AddRef,
580 BSCHttpNegotiate_Release,
581 BSCHttpNegotiate_BeginningTransaction,
582 BSCHttpNegotiate_OnResponse
585 static HRESULT WINAPI Authenticate_QueryInterface(IAuthenticate *iface,
586 REFIID riid, void **ppv)
588 BindStatusCallback *This = impl_from_IAuthenticate(iface);
589 return IBindStatusCallback_QueryInterface(&This->IBindStatusCallback_iface, riid, ppv);
592 static ULONG WINAPI Authenticate_AddRef(IAuthenticate *iface)
594 BindStatusCallback *This = impl_from_IAuthenticate(iface);
595 return IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
598 static ULONG WINAPI Authenticate_Release(IAuthenticate *iface)
600 BindStatusCallback *This = impl_from_IAuthenticate(iface);
601 return IBindStatusCallback_Release(&This->IBindStatusCallback_iface);
604 static HRESULT WINAPI Authenticate_Authenticate(IAuthenticate *iface,
605 HWND *hwnd, LPWSTR *username, LPWSTR *password)
607 BindStatusCallback *This = impl_from_IAuthenticate(iface);
608 FIXME("(%p)->(%p %p %p)\n", This, hwnd, username, password);
609 return E_NOTIMPL;
612 static const IAuthenticateVtbl AuthenticateVtbl = {
613 Authenticate_QueryInterface,
614 Authenticate_AddRef,
615 Authenticate_Release,
616 Authenticate_Authenticate
619 static HRESULT BindStatusCallback_create(httprequest* This, BindStatusCallback **obj, const VARIANT *body)
621 BindStatusCallback *bsc;
622 IBindCtx *pbc;
623 HRESULT hr;
624 int size;
626 hr = CreateBindCtx(0, &pbc);
627 if (hr != S_OK) return hr;
629 bsc = heap_alloc(sizeof(*bsc));
630 if (!bsc)
632 IBindCtx_Release(pbc);
633 return E_OUTOFMEMORY;
636 bsc->IBindStatusCallback_iface.lpVtbl = &BindStatusCallbackVtbl;
637 bsc->IHttpNegotiate_iface.lpVtbl = &BSCHttpNegotiateVtbl;
638 bsc->IAuthenticate_iface.lpVtbl = &AuthenticateVtbl;
639 bsc->ref = 1;
640 bsc->request = This;
641 bsc->binding = NULL;
642 bsc->stream = NULL;
643 bsc->body = NULL;
645 TRACE("(%p)->(%p)\n", This, bsc);
647 This->use_utf8_content = FALSE;
649 if (This->verb != BINDVERB_GET)
651 void *send_data, *ptr;
652 SAFEARRAY *sa = NULL;
654 if (V_VT(body) == (VT_VARIANT|VT_BYREF))
655 body = V_VARIANTREF(body);
657 switch (V_VT(body))
659 case VT_BSTR:
661 int len = SysStringLen(V_BSTR(body));
662 const WCHAR *str = V_BSTR(body);
663 UINT i, cp = CP_ACP;
665 for (i = 0; i < len; i++)
667 if (str[i] > 127)
669 cp = CP_UTF8;
670 break;
674 size = WideCharToMultiByte(cp, 0, str, len, NULL, 0, NULL, NULL);
675 if (!(ptr = heap_alloc(size)))
677 heap_free(bsc);
678 return E_OUTOFMEMORY;
680 WideCharToMultiByte(cp, 0, str, len, ptr, size, NULL, NULL);
681 if (cp == CP_UTF8) This->use_utf8_content = TRUE;
682 break;
684 case VT_ARRAY|VT_UI1:
686 sa = V_ARRAY(body);
687 if ((hr = SafeArrayAccessData(sa, (void **)&ptr)) != S_OK) return hr;
688 if ((hr = SafeArrayGetUBound(sa, 1, &size) != S_OK))
690 SafeArrayUnaccessData(sa);
691 return hr;
693 size++;
694 break;
696 case VT_EMPTY:
697 ptr = NULL;
698 size = 0;
699 break;
700 default:
701 FIXME("unsupported body data type %d\n", V_VT(body));
702 break;
705 bsc->body = GlobalAlloc(GMEM_FIXED, size);
706 if (!bsc->body)
708 if (V_VT(body) == VT_BSTR)
709 heap_free(ptr);
710 else if (V_VT(body) == (VT_ARRAY|VT_UI1))
711 SafeArrayUnaccessData(sa);
713 heap_free(bsc);
714 return E_OUTOFMEMORY;
717 send_data = GlobalLock(bsc->body);
718 memcpy(send_data, ptr, size);
719 GlobalUnlock(bsc->body);
721 if (V_VT(body) == VT_BSTR)
722 heap_free(ptr);
723 else if (V_VT(body) == (VT_ARRAY|VT_UI1))
724 SafeArrayUnaccessData(sa);
727 hr = RegisterBindStatusCallback(pbc, &bsc->IBindStatusCallback_iface, NULL, 0);
728 if (hr == S_OK)
730 IMoniker *moniker;
732 hr = CreateURLMoniker(NULL, This->url, &moniker);
733 if (hr == S_OK)
735 IStream *stream;
737 hr = IMoniker_BindToStorage(moniker, pbc, NULL, &IID_IStream, (void**)&stream);
738 IMoniker_Release(moniker);
739 if (stream) IStream_Release(stream);
741 IBindCtx_Release(pbc);
744 if (FAILED(hr))
746 IBindStatusCallback_Release(&bsc->IBindStatusCallback_iface);
747 bsc = NULL;
750 *obj = bsc;
751 return hr;
754 static HRESULT httprequest_open(httprequest *This, BSTR method, BSTR url,
755 VARIANT async, VARIANT user, VARIANT password)
757 static const WCHAR MethodGetW[] = {'G','E','T',0};
758 static const WCHAR MethodPutW[] = {'P','U','T',0};
759 static const WCHAR MethodPostW[] = {'P','O','S','T',0};
760 static const WCHAR MethodDeleteW[] = {'D','E','L','E','T','E',0};
761 VARIANT str, is_async;
762 HRESULT hr;
764 if (!method || !url) return E_INVALIDARG;
766 /* free previously set data */
767 SysFreeString(This->url);
768 SysFreeString(This->user);
769 SysFreeString(This->password);
770 This->url = This->user = This->password = NULL;
772 if (!strcmpiW(method, MethodGetW))
774 This->verb = BINDVERB_GET;
776 else if (!strcmpiW(method, MethodPutW))
778 This->verb = BINDVERB_PUT;
780 else if (!strcmpiW(method, MethodPostW))
782 This->verb = BINDVERB_POST;
784 else if (!strcmpiW(method, MethodDeleteW))
786 This->verb = BINDVERB_CUSTOM;
787 SysReAllocString(&This->custom, method);
789 else
791 FIXME("unsupported request type %s\n", debugstr_w(method));
792 This->verb = -1;
793 return E_FAIL;
796 /* try to combine with site url */
797 if (This->siteurl && PathIsRelativeW(url))
799 DWORD len = INTERNET_MAX_URL_LENGTH;
800 WCHAR *fullW = heap_alloc(len*sizeof(WCHAR));
802 hr = UrlCombineW(This->siteurl, url, fullW, &len, 0);
803 if (hr == S_OK)
805 TRACE("combined url %s\n", debugstr_w(fullW));
806 This->url = SysAllocString(fullW);
808 heap_free(fullW);
810 else
811 This->url = SysAllocString(url);
813 VariantInit(&is_async);
814 hr = VariantChangeType(&is_async, &async, 0, VT_BOOL);
815 This->async = hr == S_OK && V_BOOL(&is_async) == VARIANT_TRUE;
817 VariantInit(&str);
818 hr = VariantChangeType(&str, &user, 0, VT_BSTR);
819 if (hr == S_OK)
820 This->user = V_BSTR(&str);
822 VariantInit(&str);
823 hr = VariantChangeType(&str, &password, 0, VT_BSTR);
824 if (hr == S_OK)
825 This->password = V_BSTR(&str);
827 httprequest_setreadystate(This, READYSTATE_LOADING);
829 return S_OK;
832 static HRESULT httprequest_setRequestHeader(httprequest *This, BSTR header, BSTR value)
834 struct httpheader *entry;
836 if (!header || !*header) return E_INVALIDARG;
837 if (This->state != READYSTATE_LOADING) return E_FAIL;
838 if (!value) return E_INVALIDARG;
840 /* replace existing header value if already added */
841 LIST_FOR_EACH_ENTRY(entry, &This->reqheaders, struct httpheader, entry)
843 if (lstrcmpW(entry->header, header) == 0)
845 LONG length = SysStringLen(entry->value);
846 HRESULT hr;
848 hr = SysReAllocString(&entry->value, value) ? S_OK : E_OUTOFMEMORY;
850 if (hr == S_OK)
851 This->reqheader_size += (SysStringLen(entry->value) - length);
853 return hr;
857 entry = heap_alloc(sizeof(*entry));
858 if (!entry) return E_OUTOFMEMORY;
860 /* new header */
861 entry->header = SysAllocString(header);
862 entry->value = SysAllocString(value);
864 /* header length including null terminator */
865 This->reqheader_size += SysStringLen(entry->header) + sizeof(colspaceW)/sizeof(WCHAR) +
866 SysStringLen(entry->value) + sizeof(crlfW)/sizeof(WCHAR) - 1;
868 list_add_head(&This->reqheaders, &entry->entry);
870 return S_OK;
873 static HRESULT httprequest_getResponseHeader(httprequest *This, BSTR header, BSTR *value)
875 struct httpheader *entry;
877 if (!header || !value) return E_INVALIDARG;
879 if (This->raw_respheaders && list_empty(&This->respheaders))
881 WCHAR *ptr, *line;
883 ptr = line = This->raw_respheaders;
884 while (*ptr)
886 if (*ptr == '\r' && *(ptr+1) == '\n')
888 add_response_header(This, line, ptr-line);
889 ptr++; line = ++ptr;
890 continue;
892 ptr++;
896 LIST_FOR_EACH_ENTRY(entry, &This->respheaders, struct httpheader, entry)
898 if (!strcmpiW(entry->header, header))
900 *value = SysAllocString(entry->value);
901 TRACE("header value %s\n", debugstr_w(*value));
902 return S_OK;
906 return S_FALSE;
909 static HRESULT httprequest_getAllResponseHeaders(httprequest *This, BSTR *respheaders)
911 if (!respheaders) return E_INVALIDARG;
913 *respheaders = SysAllocString(This->raw_respheaders);
915 return S_OK;
918 static HRESULT httprequest_send(httprequest *This, VARIANT body)
920 BindStatusCallback *bsc = NULL;
921 HRESULT hr;
923 if (This->state != READYSTATE_LOADING) return E_FAIL;
925 hr = BindStatusCallback_create(This, &bsc, &body);
926 if (FAILED(hr)) return hr;
928 BindStatusCallback_Detach(This->bsc);
929 This->bsc = bsc;
931 return hr;
934 static HRESULT httprequest_abort(httprequest *This)
936 BindStatusCallback_Detach(This->bsc);
937 This->bsc = NULL;
939 httprequest_setreadystate(This, READYSTATE_UNINITIALIZED);
941 return S_OK;
944 static HRESULT httprequest_get_status(httprequest *This, LONG *status)
946 if (!status) return E_INVALIDARG;
947 if (This->state != READYSTATE_COMPLETE) return E_FAIL;
949 *status = This->status;
951 return S_OK;
954 static HRESULT httprequest_get_statusText(httprequest *This, BSTR *status)
956 if (!status) return E_INVALIDARG;
957 if (This->state != READYSTATE_COMPLETE) return E_FAIL;
959 *status = SysAllocString(This->status_text);
961 return S_OK;
964 static HRESULT httprequest_get_responseText(httprequest *This, BSTR *body)
966 HGLOBAL hglobal;
967 HRESULT hr;
969 if (!body) return E_INVALIDARG;
970 if (This->state != READYSTATE_COMPLETE) return E_FAIL;
972 hr = GetHGlobalFromStream(This->bsc->stream, &hglobal);
973 if (hr == S_OK)
975 xmlChar *ptr = GlobalLock(hglobal);
976 DWORD size = GlobalSize(hglobal);
977 xmlCharEncoding encoding = XML_CHAR_ENCODING_UTF8;
979 /* try to determine data encoding */
980 if (size >= 4)
982 encoding = xmlDetectCharEncoding(ptr, 4);
983 TRACE("detected encoding: %s\n", debugstr_a(xmlGetCharEncodingName(encoding)));
984 if ( encoding != XML_CHAR_ENCODING_UTF8 &&
985 encoding != XML_CHAR_ENCODING_UTF16LE &&
986 encoding != XML_CHAR_ENCODING_NONE )
988 FIXME("unsupported encoding: %s\n", debugstr_a(xmlGetCharEncodingName(encoding)));
989 GlobalUnlock(hglobal);
990 return E_FAIL;
994 /* without BOM assume UTF-8 */
995 if (encoding == XML_CHAR_ENCODING_UTF8 ||
996 encoding == XML_CHAR_ENCODING_NONE )
998 DWORD length = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)ptr, size, NULL, 0);
1000 *body = SysAllocStringLen(NULL, length);
1001 if (*body)
1002 MultiByteToWideChar( CP_UTF8, 0, (LPCSTR)ptr, size, *body, length);
1004 else
1005 *body = SysAllocStringByteLen((LPCSTR)ptr, size);
1007 if (!*body) hr = E_OUTOFMEMORY;
1008 GlobalUnlock(hglobal);
1011 return hr;
1014 static HRESULT httprequest_get_responseXML(httprequest *This, IDispatch **body)
1016 IXMLDOMDocument3 *doc;
1017 HRESULT hr;
1018 BSTR str;
1020 if (!body) return E_INVALIDARG;
1021 if (This->state != READYSTATE_COMPLETE) return E_FAIL;
1023 hr = DOMDocument_create(MSXML_DEFAULT, NULL, (void**)&doc);
1024 if (hr != S_OK) return hr;
1026 hr = httprequest_get_responseText(This, &str);
1027 if (hr == S_OK)
1029 VARIANT_BOOL ok;
1031 hr = IXMLDOMDocument3_loadXML(doc, str, &ok);
1032 SysFreeString(str);
1035 IXMLDOMDocument3_QueryInterface(doc, &IID_IDispatch, (void**)body);
1036 IXMLDOMDocument3_Release(doc);
1038 return hr;
1041 static HRESULT httprequest_get_responseBody(httprequest *This, VARIANT *body)
1043 HGLOBAL hglobal;
1044 HRESULT hr;
1046 if (!body) return E_INVALIDARG;
1047 V_VT(body) = VT_EMPTY;
1049 if (This->state != READYSTATE_COMPLETE) return E_PENDING;
1051 hr = GetHGlobalFromStream(This->bsc->stream, &hglobal);
1052 if (hr == S_OK)
1054 void *ptr = GlobalLock(hglobal);
1055 DWORD size = GlobalSize(hglobal);
1057 SAFEARRAYBOUND bound;
1058 SAFEARRAY *array;
1060 bound.lLbound = 0;
1061 bound.cElements = size;
1062 array = SafeArrayCreate(VT_UI1, 1, &bound);
1064 if (array)
1066 void *dest;
1068 V_VT(body) = VT_ARRAY | VT_UI1;
1069 V_ARRAY(body) = array;
1071 hr = SafeArrayAccessData(array, &dest);
1072 if (hr == S_OK)
1074 memcpy(dest, ptr, size);
1075 SafeArrayUnaccessData(array);
1077 else
1079 VariantClear(body);
1082 else
1083 hr = E_FAIL;
1085 GlobalUnlock(hglobal);
1088 return hr;
1091 static HRESULT httprequest_get_responseStream(httprequest *This, VARIANT *body)
1093 LARGE_INTEGER move;
1094 IStream *stream;
1095 HRESULT hr;
1097 if (!body) return E_INVALIDARG;
1098 V_VT(body) = VT_EMPTY;
1100 if (This->state != READYSTATE_COMPLETE) return E_PENDING;
1102 hr = IStream_Clone(This->bsc->stream, &stream);
1104 move.QuadPart = 0;
1105 IStream_Seek(stream, move, STREAM_SEEK_SET, NULL);
1107 V_VT(body) = VT_UNKNOWN;
1108 V_UNKNOWN(body) = (IUnknown*)stream;
1110 return hr;
1113 static HRESULT httprequest_get_readyState(httprequest *This, LONG *state)
1115 if (!state) return E_INVALIDARG;
1117 *state = This->state;
1118 return S_OK;
1121 static HRESULT httprequest_put_onreadystatechange(httprequest *This, IDispatch *sink)
1123 if (This->sink) IDispatch_Release(This->sink);
1124 if ((This->sink = sink)) IDispatch_AddRef(This->sink);
1126 return S_OK;
1129 static void httprequest_release(httprequest *This)
1131 struct httpheader *header, *header2;
1133 if (This->site)
1134 IUnknown_Release( This->site );
1136 SysFreeString(This->custom);
1137 SysFreeString(This->siteurl);
1138 SysFreeString(This->url);
1139 SysFreeString(This->user);
1140 SysFreeString(This->password);
1142 /* request headers */
1143 LIST_FOR_EACH_ENTRY_SAFE(header, header2, &This->reqheaders, struct httpheader, entry)
1145 list_remove(&header->entry);
1146 SysFreeString(header->header);
1147 SysFreeString(header->value);
1148 heap_free(header);
1150 /* response headers */
1151 free_response_headers(This);
1152 SysFreeString(This->status_text);
1154 /* detach callback object */
1155 BindStatusCallback_Detach(This->bsc);
1157 if (This->sink) IDispatch_Release(This->sink);
1160 static HRESULT WINAPI XMLHTTPRequest_QueryInterface(IXMLHTTPRequest *iface, REFIID riid, void **ppvObject)
1162 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1163 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppvObject);
1165 if ( IsEqualGUID( riid, &IID_IXMLHTTPRequest) ||
1166 IsEqualGUID( riid, &IID_IDispatch) ||
1167 IsEqualGUID( riid, &IID_IUnknown) )
1169 *ppvObject = iface;
1171 else if (IsEqualGUID(&IID_IObjectWithSite, riid))
1173 *ppvObject = &This->IObjectWithSite_iface;
1175 else if (IsEqualGUID(&IID_IObjectSafety, riid))
1177 *ppvObject = &This->IObjectSafety_iface;
1179 else
1181 TRACE("Unsupported interface %s\n", debugstr_guid(riid));
1182 *ppvObject = NULL;
1183 return E_NOINTERFACE;
1186 IXMLHTTPRequest_AddRef( iface );
1188 return S_OK;
1191 static ULONG WINAPI XMLHTTPRequest_AddRef(IXMLHTTPRequest *iface)
1193 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1194 ULONG ref = InterlockedIncrement( &This->ref );
1195 TRACE("(%p)->(%u)\n", This, ref );
1196 return ref;
1199 static ULONG WINAPI XMLHTTPRequest_Release(IXMLHTTPRequest *iface)
1201 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1202 ULONG ref = InterlockedDecrement( &This->ref );
1204 TRACE("(%p)->(%u)\n", This, ref );
1206 if ( ref == 0 )
1208 httprequest_release( This );
1209 heap_free( This );
1212 return ref;
1215 static HRESULT WINAPI XMLHTTPRequest_GetTypeInfoCount(IXMLHTTPRequest *iface, UINT *pctinfo)
1217 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1219 TRACE("(%p)->(%p)\n", This, pctinfo);
1221 *pctinfo = 1;
1223 return S_OK;
1226 static HRESULT WINAPI XMLHTTPRequest_GetTypeInfo(IXMLHTTPRequest *iface, UINT iTInfo,
1227 LCID lcid, ITypeInfo **ppTInfo)
1229 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1231 TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
1233 return get_typeinfo(IXMLHTTPRequest_tid, ppTInfo);
1236 static HRESULT WINAPI XMLHTTPRequest_GetIDsOfNames(IXMLHTTPRequest *iface, REFIID riid,
1237 LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
1239 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1240 ITypeInfo *typeinfo;
1241 HRESULT hr;
1243 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
1244 lcid, rgDispId);
1246 if(!rgszNames || cNames == 0 || !rgDispId)
1247 return E_INVALIDARG;
1249 hr = get_typeinfo(IXMLHTTPRequest_tid, &typeinfo);
1250 if(SUCCEEDED(hr))
1252 hr = ITypeInfo_GetIDsOfNames(typeinfo, rgszNames, cNames, rgDispId);
1253 ITypeInfo_Release(typeinfo);
1256 return hr;
1259 static HRESULT WINAPI XMLHTTPRequest_Invoke(IXMLHTTPRequest *iface, DISPID dispIdMember, REFIID riid,
1260 LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult,
1261 EXCEPINFO *pExcepInfo, UINT *puArgErr)
1263 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1264 ITypeInfo *typeinfo;
1265 HRESULT hr;
1267 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
1268 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
1270 hr = get_typeinfo(IXMLHTTPRequest_tid, &typeinfo);
1271 if(SUCCEEDED(hr))
1273 hr = ITypeInfo_Invoke(typeinfo, &This->IXMLHTTPRequest_iface, dispIdMember, wFlags,
1274 pDispParams, pVarResult, pExcepInfo, puArgErr);
1275 ITypeInfo_Release(typeinfo);
1278 return hr;
1281 static HRESULT WINAPI XMLHTTPRequest_open(IXMLHTTPRequest *iface, BSTR method, BSTR url,
1282 VARIANT async, VARIANT user, VARIANT password)
1284 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1285 TRACE("(%p)->(%s %s %s)\n", This, debugstr_w(method), debugstr_w(url),
1286 debugstr_variant(&async));
1287 return httprequest_open(This, method, url, async, user, password);
1290 static HRESULT WINAPI XMLHTTPRequest_setRequestHeader(IXMLHTTPRequest *iface, BSTR header, BSTR value)
1292 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1293 TRACE("(%p)->(%s %s)\n", This, debugstr_w(header), debugstr_w(value));
1294 return httprequest_setRequestHeader(This, header, value);
1297 static HRESULT WINAPI XMLHTTPRequest_getResponseHeader(IXMLHTTPRequest *iface, BSTR header, BSTR *value)
1299 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1300 TRACE("(%p)->(%s %p)\n", This, debugstr_w(header), value);
1301 return httprequest_getResponseHeader(This, header, value);
1304 static HRESULT WINAPI XMLHTTPRequest_getAllResponseHeaders(IXMLHTTPRequest *iface, BSTR *respheaders)
1306 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1307 TRACE("(%p)->(%p)\n", This, respheaders);
1308 return httprequest_getAllResponseHeaders(This, respheaders);
1311 static HRESULT WINAPI XMLHTTPRequest_send(IXMLHTTPRequest *iface, VARIANT body)
1313 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1314 TRACE("(%p)->(%s)\n", This, debugstr_variant(&body));
1315 return httprequest_send(This, body);
1318 static HRESULT WINAPI XMLHTTPRequest_abort(IXMLHTTPRequest *iface)
1320 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1321 TRACE("(%p)\n", This);
1322 return httprequest_abort(This);
1325 static HRESULT WINAPI XMLHTTPRequest_get_status(IXMLHTTPRequest *iface, LONG *status)
1327 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1328 TRACE("(%p)->(%p)\n", This, status);
1329 return httprequest_get_status(This, status);
1332 static HRESULT WINAPI XMLHTTPRequest_get_statusText(IXMLHTTPRequest *iface, BSTR *status)
1334 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1335 TRACE("(%p)->(%p)\n", This, status);
1336 return httprequest_get_statusText(This, status);
1339 static HRESULT WINAPI XMLHTTPRequest_get_responseXML(IXMLHTTPRequest *iface, IDispatch **body)
1341 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1342 TRACE("(%p)->(%p)\n", This, body);
1343 return httprequest_get_responseXML(This, body);
1346 static HRESULT WINAPI XMLHTTPRequest_get_responseText(IXMLHTTPRequest *iface, BSTR *body)
1348 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1349 TRACE("(%p)->(%p)\n", This, body);
1350 return httprequest_get_responseText(This, body);
1353 static HRESULT WINAPI XMLHTTPRequest_get_responseBody(IXMLHTTPRequest *iface, VARIANT *body)
1355 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1356 TRACE("(%p)->(%p)\n", This, body);
1357 return httprequest_get_responseBody(This, body);
1360 static HRESULT WINAPI XMLHTTPRequest_get_responseStream(IXMLHTTPRequest *iface, VARIANT *body)
1362 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1363 TRACE("(%p)->(%p)\n", This, body);
1364 return httprequest_get_responseStream(This, body);
1367 static HRESULT WINAPI XMLHTTPRequest_get_readyState(IXMLHTTPRequest *iface, LONG *state)
1369 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1370 TRACE("(%p)->(%p)\n", This, state);
1371 return httprequest_get_readyState(This, state);
1374 static HRESULT WINAPI XMLHTTPRequest_put_onreadystatechange(IXMLHTTPRequest *iface, IDispatch *sink)
1376 httprequest *This = impl_from_IXMLHTTPRequest( iface );
1377 TRACE("(%p)->(%p)\n", This, sink);
1378 return httprequest_put_onreadystatechange(This, sink);
1381 static const struct IXMLHTTPRequestVtbl XMLHTTPRequestVtbl =
1383 XMLHTTPRequest_QueryInterface,
1384 XMLHTTPRequest_AddRef,
1385 XMLHTTPRequest_Release,
1386 XMLHTTPRequest_GetTypeInfoCount,
1387 XMLHTTPRequest_GetTypeInfo,
1388 XMLHTTPRequest_GetIDsOfNames,
1389 XMLHTTPRequest_Invoke,
1390 XMLHTTPRequest_open,
1391 XMLHTTPRequest_setRequestHeader,
1392 XMLHTTPRequest_getResponseHeader,
1393 XMLHTTPRequest_getAllResponseHeaders,
1394 XMLHTTPRequest_send,
1395 XMLHTTPRequest_abort,
1396 XMLHTTPRequest_get_status,
1397 XMLHTTPRequest_get_statusText,
1398 XMLHTTPRequest_get_responseXML,
1399 XMLHTTPRequest_get_responseText,
1400 XMLHTTPRequest_get_responseBody,
1401 XMLHTTPRequest_get_responseStream,
1402 XMLHTTPRequest_get_readyState,
1403 XMLHTTPRequest_put_onreadystatechange
1406 /* IObjectWithSite */
1407 static HRESULT WINAPI
1408 httprequest_ObjectWithSite_QueryInterface( IObjectWithSite* iface, REFIID riid, void** ppvObject )
1410 httprequest *This = impl_from_IObjectWithSite(iface);
1411 return IXMLHTTPRequest_QueryInterface( (IXMLHTTPRequest *)This, riid, ppvObject );
1414 static ULONG WINAPI httprequest_ObjectWithSite_AddRef( IObjectWithSite* iface )
1416 httprequest *This = impl_from_IObjectWithSite(iface);
1417 return IXMLHTTPRequest_AddRef((IXMLHTTPRequest *)This);
1420 static ULONG WINAPI httprequest_ObjectWithSite_Release( IObjectWithSite* iface )
1422 httprequest *This = impl_from_IObjectWithSite(iface);
1423 return IXMLHTTPRequest_Release((IXMLHTTPRequest *)This);
1426 static HRESULT WINAPI httprequest_ObjectWithSite_GetSite( IObjectWithSite *iface, REFIID iid, void **ppvSite )
1428 httprequest *This = impl_from_IObjectWithSite(iface);
1430 TRACE("(%p)->(%s %p)\n", This, debugstr_guid( iid ), ppvSite );
1432 if ( !This->site )
1433 return E_FAIL;
1435 return IUnknown_QueryInterface( This->site, iid, ppvSite );
1438 static HRESULT WINAPI httprequest_ObjectWithSite_SetSite( IObjectWithSite *iface, IUnknown *punk )
1440 httprequest *This = impl_from_IObjectWithSite(iface);
1441 IServiceProvider *provider;
1442 HRESULT hr;
1444 TRACE("(%p)->(%p)\n", iface, punk);
1446 if (punk)
1447 IUnknown_AddRef( punk );
1449 if(This->site)
1450 IUnknown_Release( This->site );
1452 This->site = punk;
1454 hr = IUnknown_QueryInterface(This->site, &IID_IServiceProvider, (void**)&provider);
1455 if (hr == S_OK)
1457 IHTMLDocument2 *doc;
1459 hr = IServiceProvider_QueryService(provider, &SID_SContainerDispatch, &IID_IHTMLDocument2, (void**)&doc);
1460 if (hr == S_OK)
1462 SysFreeString(This->siteurl);
1464 hr = IHTMLDocument2_get_URL(doc, &This->siteurl);
1465 IHTMLDocument2_Release(doc);
1466 TRACE("host url %s, 0x%08x\n", debugstr_w(This->siteurl), hr);
1468 IServiceProvider_Release(provider);
1471 return S_OK;
1474 static const IObjectWithSiteVtbl ObjectWithSiteVtbl =
1476 httprequest_ObjectWithSite_QueryInterface,
1477 httprequest_ObjectWithSite_AddRef,
1478 httprequest_ObjectWithSite_Release,
1479 httprequest_ObjectWithSite_SetSite,
1480 httprequest_ObjectWithSite_GetSite
1483 /* IObjectSafety */
1484 static HRESULT WINAPI httprequest_Safety_QueryInterface(IObjectSafety *iface, REFIID riid, void **ppv)
1486 httprequest *This = impl_from_IObjectSafety(iface);
1487 return IXMLHTTPRequest_QueryInterface( (IXMLHTTPRequest *)This, riid, ppv );
1490 static ULONG WINAPI httprequest_Safety_AddRef(IObjectSafety *iface)
1492 httprequest *This = impl_from_IObjectSafety(iface);
1493 return IXMLHTTPRequest_AddRef((IXMLHTTPRequest *)This);
1496 static ULONG WINAPI httprequest_Safety_Release(IObjectSafety *iface)
1498 httprequest *This = impl_from_IObjectSafety(iface);
1499 return IXMLHTTPRequest_Release((IXMLHTTPRequest *)This);
1502 static HRESULT WINAPI httprequest_Safety_GetInterfaceSafetyOptions(IObjectSafety *iface, REFIID riid,
1503 DWORD *supported, DWORD *enabled)
1505 httprequest *This = impl_from_IObjectSafety(iface);
1507 TRACE("(%p)->(%s %p %p)\n", This, debugstr_guid(riid), supported, enabled);
1509 if(!supported || !enabled) return E_POINTER;
1511 *supported = safety_supported_options;
1512 *enabled = This->safeopt;
1514 return S_OK;
1517 static HRESULT WINAPI httprequest_Safety_SetInterfaceSafetyOptions(IObjectSafety *iface, REFIID riid,
1518 DWORD mask, DWORD enabled)
1520 httprequest *This = impl_from_IObjectSafety(iface);
1521 TRACE("(%p)->(%s %x %x)\n", This, debugstr_guid(riid), mask, enabled);
1523 if ((mask & ~safety_supported_options))
1524 return E_FAIL;
1526 This->safeopt = (This->safeopt & ~mask) | (mask & enabled);
1528 return S_OK;
1531 static const IObjectSafetyVtbl ObjectSafetyVtbl = {
1532 httprequest_Safety_QueryInterface,
1533 httprequest_Safety_AddRef,
1534 httprequest_Safety_Release,
1535 httprequest_Safety_GetInterfaceSafetyOptions,
1536 httprequest_Safety_SetInterfaceSafetyOptions
1539 /* IServerXMLHTTPRequest */
1540 static HRESULT WINAPI ServerXMLHTTPRequest_QueryInterface(IServerXMLHTTPRequest *iface, REFIID riid, void **obj)
1542 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1544 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), obj);
1546 if ( IsEqualGUID( riid, &IID_IServerXMLHTTPRequest) ||
1547 IsEqualGUID( riid, &IID_IXMLHTTPRequest) ||
1548 IsEqualGUID( riid, &IID_IDispatch) ||
1549 IsEqualGUID( riid, &IID_IUnknown) )
1551 *obj = iface;
1553 else
1555 TRACE("Unsupported interface %s\n", debugstr_guid(riid));
1556 *obj = NULL;
1557 return E_NOINTERFACE;
1560 IServerXMLHTTPRequest_AddRef( iface );
1562 return S_OK;
1565 static ULONG WINAPI ServerXMLHTTPRequest_AddRef(IServerXMLHTTPRequest *iface)
1567 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1568 ULONG ref = InterlockedIncrement( &This->ref );
1569 TRACE("(%p)->(%u)\n", This, ref );
1570 return ref;
1573 static ULONG WINAPI ServerXMLHTTPRequest_Release(IServerXMLHTTPRequest *iface)
1575 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1576 ULONG ref = InterlockedDecrement( &This->ref );
1578 TRACE("(%p)->(%u)\n", This, ref );
1580 if ( ref == 0 )
1582 httprequest_release( &This->req );
1583 heap_free( This );
1586 return ref;
1589 static HRESULT WINAPI ServerXMLHTTPRequest_GetTypeInfoCount(IServerXMLHTTPRequest *iface, UINT *pctinfo)
1591 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1593 TRACE("(%p)->(%p)\n", This, pctinfo);
1594 *pctinfo = 1;
1596 return S_OK;
1599 static HRESULT WINAPI ServerXMLHTTPRequest_GetTypeInfo(IServerXMLHTTPRequest *iface, UINT iTInfo,
1600 LCID lcid, ITypeInfo **ppTInfo)
1602 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1604 TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
1606 return get_typeinfo(IServerXMLHTTPRequest_tid, ppTInfo);
1609 static HRESULT WINAPI ServerXMLHTTPRequest_GetIDsOfNames(IServerXMLHTTPRequest *iface, REFIID riid,
1610 LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
1612 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1613 ITypeInfo *typeinfo;
1614 HRESULT hr;
1616 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
1617 lcid, rgDispId);
1619 if(!rgszNames || cNames == 0 || !rgDispId)
1620 return E_INVALIDARG;
1622 hr = get_typeinfo(IServerXMLHTTPRequest_tid, &typeinfo);
1623 if(SUCCEEDED(hr))
1625 hr = ITypeInfo_GetIDsOfNames(typeinfo, rgszNames, cNames, rgDispId);
1626 ITypeInfo_Release(typeinfo);
1629 return hr;
1632 static HRESULT WINAPI ServerXMLHTTPRequest_Invoke(IServerXMLHTTPRequest *iface, DISPID dispIdMember, REFIID riid,
1633 LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult,
1634 EXCEPINFO *pExcepInfo, UINT *puArgErr)
1636 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1637 ITypeInfo *typeinfo;
1638 HRESULT hr;
1640 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
1641 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
1643 hr = get_typeinfo(IServerXMLHTTPRequest_tid, &typeinfo);
1644 if(SUCCEEDED(hr))
1646 hr = ITypeInfo_Invoke(typeinfo, &This->IServerXMLHTTPRequest_iface, dispIdMember, wFlags,
1647 pDispParams, pVarResult, pExcepInfo, puArgErr);
1648 ITypeInfo_Release(typeinfo);
1651 return hr;
1654 static HRESULT WINAPI ServerXMLHTTPRequest_open(IServerXMLHTTPRequest *iface, BSTR method, BSTR url,
1655 VARIANT async, VARIANT user, VARIANT password)
1657 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1658 TRACE("(%p)->(%s %s %s)\n", This, debugstr_w(method), debugstr_w(url),
1659 debugstr_variant(&async));
1660 return httprequest_open(&This->req, method, url, async, user, password);
1663 static HRESULT WINAPI ServerXMLHTTPRequest_setRequestHeader(IServerXMLHTTPRequest *iface, BSTR header, BSTR value)
1665 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1666 TRACE("(%p)->(%s %s)\n", This, debugstr_w(header), debugstr_w(value));
1667 return httprequest_setRequestHeader(&This->req, header, value);
1670 static HRESULT WINAPI ServerXMLHTTPRequest_getResponseHeader(IServerXMLHTTPRequest *iface, BSTR header, BSTR *value)
1672 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1673 TRACE("(%p)->(%s %p)\n", This, debugstr_w(header), value);
1674 return httprequest_getResponseHeader(&This->req, header, value);
1677 static HRESULT WINAPI ServerXMLHTTPRequest_getAllResponseHeaders(IServerXMLHTTPRequest *iface, BSTR *respheaders)
1679 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1680 TRACE("(%p)->(%p)\n", This, respheaders);
1681 return httprequest_getAllResponseHeaders(&This->req, respheaders);
1684 static HRESULT WINAPI ServerXMLHTTPRequest_send(IServerXMLHTTPRequest *iface, VARIANT body)
1686 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1687 TRACE("(%p)->(%s)\n", This, debugstr_variant(&body));
1688 return httprequest_send(&This->req, body);
1691 static HRESULT WINAPI ServerXMLHTTPRequest_abort(IServerXMLHTTPRequest *iface)
1693 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1694 TRACE("(%p)\n", This);
1695 return httprequest_abort(&This->req);
1698 static HRESULT WINAPI ServerXMLHTTPRequest_get_status(IServerXMLHTTPRequest *iface, LONG *status)
1700 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1701 TRACE("(%p)->(%p)\n", This, status);
1702 return httprequest_get_status(&This->req, status);
1705 static HRESULT WINAPI ServerXMLHTTPRequest_get_statusText(IServerXMLHTTPRequest *iface, BSTR *status)
1707 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1708 TRACE("(%p)->(%p)\n", This, status);
1709 return httprequest_get_statusText(&This->req, status);
1712 static HRESULT WINAPI ServerXMLHTTPRequest_get_responseXML(IServerXMLHTTPRequest *iface, IDispatch **body)
1714 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1715 TRACE("(%p)->(%p)\n", This, body);
1716 return httprequest_get_responseXML(&This->req, body);
1719 static HRESULT WINAPI ServerXMLHTTPRequest_get_responseText(IServerXMLHTTPRequest *iface, BSTR *body)
1721 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1722 TRACE("(%p)->(%p)\n", This, body);
1723 return httprequest_get_responseText(&This->req, body);
1726 static HRESULT WINAPI ServerXMLHTTPRequest_get_responseBody(IServerXMLHTTPRequest *iface, VARIANT *body)
1728 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1729 TRACE("(%p)->(%p)\n", This, body);
1730 return httprequest_get_responseBody(&This->req, body);
1733 static HRESULT WINAPI ServerXMLHTTPRequest_get_responseStream(IServerXMLHTTPRequest *iface, VARIANT *body)
1735 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1736 TRACE("(%p)->(%p)\n", This, body);
1737 return httprequest_get_responseStream(&This->req, body);
1740 static HRESULT WINAPI ServerXMLHTTPRequest_get_readyState(IServerXMLHTTPRequest *iface, LONG *state)
1742 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1743 TRACE("(%p)->(%p)\n", This, state);
1744 return httprequest_get_readyState(&This->req, state);
1747 static HRESULT WINAPI ServerXMLHTTPRequest_put_onreadystatechange(IServerXMLHTTPRequest *iface, IDispatch *sink)
1749 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1750 TRACE("(%p)->(%p)\n", This, sink);
1751 return httprequest_put_onreadystatechange(&This->req, sink);
1754 static HRESULT WINAPI ServerXMLHTTPRequest_setTimeouts(IServerXMLHTTPRequest *iface, LONG resolveTimeout, LONG connectTimeout,
1755 LONG sendTimeout, LONG receiveTimeout)
1757 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1758 FIXME("(%p)->(%d %d %d %d): stub\n", This, resolveTimeout, connectTimeout, sendTimeout, receiveTimeout);
1759 return E_NOTIMPL;
1762 static HRESULT WINAPI ServerXMLHTTPRequest_waitForResponse(IServerXMLHTTPRequest *iface, VARIANT timeout, VARIANT_BOOL *isSuccessful)
1764 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1765 FIXME("(%p)->(%s %p): stub\n", This, debugstr_variant(&timeout), isSuccessful);
1766 return E_NOTIMPL;
1769 static HRESULT WINAPI ServerXMLHTTPRequest_getOption(IServerXMLHTTPRequest *iface, SERVERXMLHTTP_OPTION option, VARIANT *value)
1771 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1772 FIXME("(%p)->(%d %p): stub\n", This, option, value);
1773 return E_NOTIMPL;
1776 static HRESULT WINAPI ServerXMLHTTPRequest_setOption(IServerXMLHTTPRequest *iface, SERVERXMLHTTP_OPTION option, VARIANT value)
1778 serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1779 FIXME("(%p)->(%d %s): stub\n", This, option, debugstr_variant(&value));
1780 return E_NOTIMPL;
1783 static const struct IServerXMLHTTPRequestVtbl ServerXMLHTTPRequestVtbl =
1785 ServerXMLHTTPRequest_QueryInterface,
1786 ServerXMLHTTPRequest_AddRef,
1787 ServerXMLHTTPRequest_Release,
1788 ServerXMLHTTPRequest_GetTypeInfoCount,
1789 ServerXMLHTTPRequest_GetTypeInfo,
1790 ServerXMLHTTPRequest_GetIDsOfNames,
1791 ServerXMLHTTPRequest_Invoke,
1792 ServerXMLHTTPRequest_open,
1793 ServerXMLHTTPRequest_setRequestHeader,
1794 ServerXMLHTTPRequest_getResponseHeader,
1795 ServerXMLHTTPRequest_getAllResponseHeaders,
1796 ServerXMLHTTPRequest_send,
1797 ServerXMLHTTPRequest_abort,
1798 ServerXMLHTTPRequest_get_status,
1799 ServerXMLHTTPRequest_get_statusText,
1800 ServerXMLHTTPRequest_get_responseXML,
1801 ServerXMLHTTPRequest_get_responseText,
1802 ServerXMLHTTPRequest_get_responseBody,
1803 ServerXMLHTTPRequest_get_responseStream,
1804 ServerXMLHTTPRequest_get_readyState,
1805 ServerXMLHTTPRequest_put_onreadystatechange,
1806 ServerXMLHTTPRequest_setTimeouts,
1807 ServerXMLHTTPRequest_waitForResponse,
1808 ServerXMLHTTPRequest_getOption,
1809 ServerXMLHTTPRequest_setOption
1812 static void init_httprequest(httprequest *req)
1814 req->IXMLHTTPRequest_iface.lpVtbl = &XMLHTTPRequestVtbl;
1815 req->IObjectWithSite_iface.lpVtbl = &ObjectWithSiteVtbl;
1816 req->IObjectSafety_iface.lpVtbl = &ObjectSafetyVtbl;
1817 req->ref = 1;
1819 req->async = FALSE;
1820 req->verb = -1;
1821 req->custom = NULL;
1822 req->url = req->siteurl = req->user = req->password = NULL;
1824 req->state = READYSTATE_UNINITIALIZED;
1825 req->sink = NULL;
1827 req->bsc = NULL;
1828 req->status = 0;
1829 req->status_text = NULL;
1830 req->reqheader_size = 0;
1831 req->raw_respheaders = NULL;
1832 req->use_utf8_content = FALSE;
1834 list_init(&req->reqheaders);
1835 list_init(&req->respheaders);
1837 req->site = NULL;
1838 req->safeopt = 0;
1841 HRESULT XMLHTTPRequest_create(IUnknown *outer, void **obj)
1843 httprequest *req;
1845 TRACE("(%p, %p)\n", outer, obj);
1847 req = heap_alloc( sizeof (*req) );
1848 if( !req )
1849 return E_OUTOFMEMORY;
1851 init_httprequest(req);
1852 *obj = &req->IXMLHTTPRequest_iface;
1854 TRACE("returning iface %p\n", *obj);
1856 return S_OK;
1859 HRESULT ServerXMLHTTP_create(IUnknown *outer, void **obj)
1861 serverhttp *req;
1863 TRACE("(%p, %p)\n", outer, obj);
1865 req = heap_alloc( sizeof (*req) );
1866 if( !req )
1867 return E_OUTOFMEMORY;
1869 init_httprequest(&req->req);
1870 req->IServerXMLHTTPRequest_iface.lpVtbl = &ServerXMLHTTPRequestVtbl;
1871 req->ref = 1;
1873 *obj = &req->IServerXMLHTTPRequest_iface;
1875 TRACE("returning iface %p\n", *obj);
1877 return S_OK;
1880 #else
1882 HRESULT XMLHTTPRequest_create(IUnknown *pUnkOuter, void **ppObj)
1884 MESSAGE("This program tried to use a XMLHTTPRequest object, but\n"
1885 "libxml2 support was not present at compile time.\n");
1886 return E_NOTIMPL;
1889 HRESULT ServerXMLHTTP_create(IUnknown *outer, void **obj)
1891 MESSAGE("This program tried to use a ServerXMLHTTP object, but\n"
1892 "libxml2 support was not present at compile time.\n");
1893 return E_NOTIMPL;
1896 #endif