Release 1.5.29.
[wine] / dlls / msxml3 / httprequest.c
1 /*
2  *    IXMLHTTPRequest implementation
3  *
4  * Copyright 2008 Alistair Leslie-Hughes
5  * Copyright 2010-2012 Nikolay Sivov for CodeWeavers
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #define COBJMACROS
23 #define NONAMELESSUNION
24
25 #include "config.h"
26
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
33
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"
46
47 #include "msxml_private.h"
48
49 #include "wine/debug.h"
50 #include "wine/list.h"
51
52 WINE_DEFAULT_DEBUG_CHANNEL(msxml);
53
54 #ifdef HAVE_LIBXML2
55
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;
62
63 typedef struct BindStatusCallback BindStatusCallback;
64
65 struct httpheader
66 {
67     struct list entry;
68     BSTR header;
69     BSTR value;
70 };
71
72 typedef struct
73 {
74     IXMLHTTPRequest IXMLHTTPRequest_iface;
75     IObjectWithSite IObjectWithSite_iface;
76     IObjectSafety   IObjectSafety_iface;
77     LONG ref;
78
79     READYSTATE state;
80     IDispatch *sink;
81
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;
93
94     /* response headers */
95     struct list respheaders;
96     BSTR raw_respheaders;
97
98     /* credentials */
99     BSTR user;
100     BSTR password;
101
102     /* bind callback */
103     BindStatusCallback *bsc;
104     LONG status;
105     BSTR status_text;
106
107     /* IObjectWithSite*/
108     IUnknown *site;
109
110     /* IObjectSafety */
111     DWORD safeopt;
112 } httprequest;
113
114 typedef struct
115 {
116     httprequest req;
117     IServerXMLHTTPRequest IServerXMLHTTPRequest_iface;
118     LONG ref;
119 } serverhttp;
120
121 static inline httprequest *impl_from_IXMLHTTPRequest( IXMLHTTPRequest *iface )
122 {
123     return CONTAINING_RECORD(iface, httprequest, IXMLHTTPRequest_iface);
124 }
125
126 static inline httprequest *impl_from_IObjectWithSite(IObjectWithSite *iface)
127 {
128     return CONTAINING_RECORD(iface, httprequest, IObjectWithSite_iface);
129 }
130
131 static inline httprequest *impl_from_IObjectSafety(IObjectSafety *iface)
132 {
133     return CONTAINING_RECORD(iface, httprequest, IObjectSafety_iface);
134 }
135
136 static inline serverhttp *impl_from_IServerXMLHTTPRequest(IServerXMLHTTPRequest *iface)
137 {
138     return CONTAINING_RECORD(iface, serverhttp, IServerXMLHTTPRequest_iface);
139 }
140
141 static void httprequest_setreadystate(httprequest *This, READYSTATE state)
142 {
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"};
150
151     This->state = state;
152
153     TRACE("state %s\n", readystates[state]);
154
155     if (This->sink && last != state)
156     {
157         DISPPARAMS params;
158
159         memset(&params, 0, sizeof(params));
160         IDispatch_Invoke(This->sink, 0, &IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD, &params, 0, 0, 0);
161     }
162 }
163
164 static void free_response_headers(httprequest *This)
165 {
166     struct httpheader *header, *header2;
167
168     LIST_FOR_EACH_ENTRY_SAFE(header, header2, &This->respheaders, struct httpheader, entry)
169     {
170         list_remove(&header->entry);
171         SysFreeString(header->header);
172         SysFreeString(header->value);
173         heap_free(header);
174     }
175
176     SysFreeString(This->raw_respheaders);
177     This->raw_respheaders = NULL;
178 }
179
180 struct BindStatusCallback
181 {
182     IBindStatusCallback IBindStatusCallback_iface;
183     IHttpNegotiate      IHttpNegotiate_iface;
184     IAuthenticate       IAuthenticate_iface;
185     LONG ref;
186
187     IBinding *binding;
188     httprequest *request;
189
190     /* response data */
191     IStream *stream;
192
193     /* request body data */
194     HGLOBAL body;
195 };
196
197 static inline BindStatusCallback *impl_from_IBindStatusCallback( IBindStatusCallback *iface )
198 {
199     return CONTAINING_RECORD(iface, BindStatusCallback, IBindStatusCallback_iface);
200 }
201
202 static inline BindStatusCallback *impl_from_IHttpNegotiate( IHttpNegotiate *iface )
203 {
204     return CONTAINING_RECORD(iface, BindStatusCallback, IHttpNegotiate_iface);
205 }
206
207 static inline BindStatusCallback *impl_from_IAuthenticate( IAuthenticate *iface )
208 {
209     return CONTAINING_RECORD(iface, BindStatusCallback, IAuthenticate_iface);
210 }
211
212 static void BindStatusCallback_Detach(BindStatusCallback *bsc)
213 {
214     if (bsc)
215     {
216         if (bsc->binding) IBinding_Abort(bsc->binding);
217         bsc->request->bsc = NULL;
218         bsc->request = NULL;
219         IBindStatusCallback_Release(&bsc->IBindStatusCallback_iface);
220     }
221 }
222
223 static HRESULT WINAPI BindStatusCallback_QueryInterface(IBindStatusCallback *iface,
224         REFIID riid, void **ppv)
225 {
226     BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
227
228     *ppv = NULL;
229
230     TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
231
232     if (IsEqualGUID(&IID_IUnknown, riid) ||
233         IsEqualGUID(&IID_IBindStatusCallback, riid))
234     {
235         *ppv = &This->IBindStatusCallback_iface;
236     }
237     else if (IsEqualGUID(&IID_IHttpNegotiate, riid))
238     {
239         *ppv = &This->IHttpNegotiate_iface;
240     }
241     else if (IsEqualGUID(&IID_IAuthenticate, riid))
242     {
243         *ppv = &This->IAuthenticate_iface;
244     }
245     else if (IsEqualGUID(&IID_IServiceProvider, riid) ||
246              IsEqualGUID(&IID_IBindStatusCallbackEx, riid) ||
247              IsEqualGUID(&IID_IInternetProtocol, riid) ||
248              IsEqualGUID(&IID_IHttpNegotiate2, riid))
249     {
250         return E_NOINTERFACE;
251     }
252
253     if (*ppv)
254     {
255         IBindStatusCallback_AddRef(iface);
256         return S_OK;
257     }
258
259     FIXME("Unsupported riid = %s\n", debugstr_guid(riid));
260
261     return E_NOINTERFACE;
262 }
263
264 static ULONG WINAPI BindStatusCallback_AddRef(IBindStatusCallback *iface)
265 {
266     BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
267     LONG ref = InterlockedIncrement(&This->ref);
268
269     TRACE("(%p) ref = %d\n", This, ref);
270
271     return ref;
272 }
273
274 static ULONG WINAPI BindStatusCallback_Release(IBindStatusCallback *iface)
275 {
276     BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
277     LONG ref = InterlockedDecrement(&This->ref);
278
279     TRACE("(%p) ref = %d\n", This, ref);
280
281     if (!ref)
282     {
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);
287     }
288
289     return ref;
290 }
291
292 static HRESULT WINAPI BindStatusCallback_OnStartBinding(IBindStatusCallback *iface,
293         DWORD reserved, IBinding *pbind)
294 {
295     BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
296
297     TRACE("(%p)->(%d %p)\n", This, reserved, pbind);
298
299     if (!pbind) return E_INVALIDARG;
300
301     This->binding = pbind;
302     IBinding_AddRef(pbind);
303
304     httprequest_setreadystate(This->request, READYSTATE_LOADED);
305
306     return CreateStreamOnHGlobal(NULL, TRUE, &This->stream);
307 }
308
309 static HRESULT WINAPI BindStatusCallback_GetPriority(IBindStatusCallback *iface, LONG *pPriority)
310 {
311     BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
312
313     TRACE("(%p)->(%p)\n", This, pPriority);
314
315     return E_NOTIMPL;
316 }
317
318 static HRESULT WINAPI BindStatusCallback_OnLowResource(IBindStatusCallback *iface, DWORD reserved)
319 {
320     BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
321
322     TRACE("(%p)->(%d)\n", This, reserved);
323
324     return E_NOTIMPL;
325 }
326
327 static HRESULT WINAPI BindStatusCallback_OnProgress(IBindStatusCallback *iface, ULONG ulProgress,
328         ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
329 {
330     BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
331
332     TRACE("(%p)->(%u %u %u %s)\n", This, ulProgress, ulProgressMax, ulStatusCode,
333             debugstr_w(szStatusText));
334
335     return S_OK;
336 }
337
338 static HRESULT WINAPI BindStatusCallback_OnStopBinding(IBindStatusCallback *iface,
339         HRESULT hr, LPCWSTR error)
340 {
341     BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
342
343     TRACE("(%p)->(0x%08x %s)\n", This, hr, debugstr_w(error));
344
345     if (This->binding)
346     {
347         IBinding_Release(This->binding);
348         This->binding = NULL;
349     }
350
351     if (hr == S_OK)
352     {
353         BindStatusCallback_Detach(This->request->bsc);
354         This->request->bsc = This;
355         httprequest_setreadystate(This->request, READYSTATE_COMPLETE);
356     }
357
358     return S_OK;
359 }
360
361 static HRESULT WINAPI BindStatusCallback_GetBindInfo(IBindStatusCallback *iface,
362         DWORD *bind_flags, BINDINFO *pbindinfo)
363 {
364     BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
365
366     TRACE("(%p)->(%p %p)\n", This, bind_flags, pbindinfo);
367
368     *bind_flags = 0;
369     if (This->request->async) *bind_flags |= BINDF_ASYNCHRONOUS;
370
371     if (This->request->verb != BINDVERB_GET && This->body)
372     {
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);
378     }
379
380     pbindinfo->dwBindVerb = This->request->verb;
381     if (This->request->verb == BINDVERB_CUSTOM)
382     {
383         pbindinfo->szCustomVerb = CoTaskMemAlloc(SysStringByteLen(This->request->custom));
384         strcpyW(pbindinfo->szCustomVerb, This->request->custom);
385     }
386
387     return S_OK;
388 }
389
390 static HRESULT WINAPI BindStatusCallback_OnDataAvailable(IBindStatusCallback *iface,
391         DWORD flags, DWORD size, FORMATETC *format, STGMEDIUM *stgmed)
392 {
393     BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
394     DWORD read, written;
395     BYTE buf[4096];
396     HRESULT hr;
397
398     TRACE("(%p)->(%08x %d %p %p)\n", This, flags, size, format, stgmed);
399
400     do
401     {
402         hr = IStream_Read(stgmed->u.pstm, buf, sizeof(buf), &read);
403         if (hr != S_OK) break;
404
405         hr = IStream_Write(This->stream, buf, read, &written);
406     } while((hr == S_OK) && written != 0 && read != 0);
407
408     httprequest_setreadystate(This->request, READYSTATE_INTERACTIVE);
409
410     return S_OK;
411 }
412
413 static HRESULT WINAPI BindStatusCallback_OnObjectAvailable(IBindStatusCallback *iface,
414         REFIID riid, IUnknown *punk)
415 {
416     BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
417
418     FIXME("(%p)->(%s %p): stub\n", This, debugstr_guid(riid), punk);
419
420     return E_NOTIMPL;
421 }
422
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
435 };
436
437 static HRESULT WINAPI BSCHttpNegotiate_QueryInterface(IHttpNegotiate *iface,
438         REFIID riid, void **ppv)
439 {
440     BindStatusCallback *This = impl_from_IHttpNegotiate(iface);
441     return IBindStatusCallback_QueryInterface(&This->IBindStatusCallback_iface, riid, ppv);
442 }
443
444 static ULONG WINAPI BSCHttpNegotiate_AddRef(IHttpNegotiate *iface)
445 {
446     BindStatusCallback *This = impl_from_IHttpNegotiate(iface);
447     return IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
448 }
449
450 static ULONG WINAPI BSCHttpNegotiate_Release(IHttpNegotiate *iface)
451 {
452     BindStatusCallback *This = impl_from_IHttpNegotiate(iface);
453     return IBindStatusCallback_Release(&This->IBindStatusCallback_iface);
454 }
455
456 static HRESULT WINAPI BSCHttpNegotiate_BeginningTransaction(IHttpNegotiate *iface,
457         LPCWSTR url, LPCWSTR headers, DWORD reserved, LPWSTR *add_headers)
458 {
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};
461
462     BindStatusCallback *This = impl_from_IHttpNegotiate(iface);
463     const struct httpheader *entry;
464     WCHAR *buff, *ptr;
465     int size = 0;
466
467     TRACE("(%p)->(%s %s %d %p)\n", This, debugstr_w(url), debugstr_w(headers), reserved, add_headers);
468
469     *add_headers = NULL;
470
471     if (This->request->use_utf8_content)
472         size = sizeof(content_type_utf8W);
473
474     if (!list_empty(&This->request->reqheaders))
475         size += This->request->reqheader_size*sizeof(WCHAR);
476
477     if (!size) return S_OK;
478
479     buff = CoTaskMemAlloc(size);
480     if (!buff) return E_OUTOFMEMORY;
481
482     ptr = buff;
483     if (This->request->use_utf8_content)
484     {
485         lstrcpyW(ptr, content_type_utf8W);
486         ptr += sizeof(content_type_utf8W)/sizeof(WCHAR)-1;
487     }
488
489     /* user headers */
490     LIST_FOR_EACH_ENTRY(entry, &This->request->reqheaders, struct httpheader, entry)
491     {
492         lstrcpyW(ptr, entry->header);
493         ptr += SysStringLen(entry->header);
494
495         lstrcpyW(ptr, colspaceW);
496         ptr += sizeof(colspaceW)/sizeof(WCHAR)-1;
497
498         lstrcpyW(ptr, entry->value);
499         ptr += SysStringLen(entry->value);
500
501         lstrcpyW(ptr, crlfW);
502         ptr += sizeof(crlfW)/sizeof(WCHAR)-1;
503     }
504
505     *add_headers = buff;
506
507     return S_OK;
508 }
509
510 static void add_response_header(httprequest *This, const WCHAR *data, int len)
511 {
512     struct httpheader *entry;
513     const WCHAR *ptr = data;
514     BSTR header, value;
515
516     while (*ptr)
517     {
518         if (*ptr == ':')
519         {
520             header = SysAllocStringLen(data, ptr-data);
521             /* skip leading spaces for a value */
522             while (*++ptr == ' ')
523                 ;
524             value = SysAllocStringLen(ptr, len-(ptr-data));
525             break;
526         }
527         ptr++;
528     }
529
530     if (!*ptr) return;
531
532     /* new header */
533     TRACE("got header %s:%s\n", debugstr_w(header), debugstr_w(value));
534
535     entry = heap_alloc(sizeof(*entry));
536     entry->header = header;
537     entry->value  = value;
538     list_add_head(&This->respheaders, &entry->entry);
539 }
540
541 static HRESULT WINAPI BSCHttpNegotiate_OnResponse(IHttpNegotiate *iface, DWORD code,
542         LPCWSTR resp_headers, LPCWSTR req_headers, LPWSTR *add_reqheaders)
543 {
544     BindStatusCallback *This = impl_from_IHttpNegotiate(iface);
545
546     TRACE("(%p)->(%d %s %s %p)\n", This, code, debugstr_w(resp_headers),
547           debugstr_w(req_headers), add_reqheaders);
548
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)
555     {
556         const WCHAR *ptr, *line, *status_text;
557
558         ptr = line = resp_headers;
559
560         /* skip HTTP-Version */
561         ptr = strchrW(ptr, ' ');
562         if (ptr)
563         {
564             /* skip Status-Code */
565             ptr = strchrW(++ptr, ' ');
566             if (ptr)
567             {
568                 status_text = ++ptr;
569                 /* now it supposed to end with CRLF */
570                 while (*ptr)
571                 {
572                     if (*ptr == '\r' && *(ptr+1) == '\n')
573                     {
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;
578                     }
579                     ptr++;
580                 }
581             }
582         }
583
584         /* store as unparsed string for now */
585         This->request->raw_respheaders = SysAllocString(line);
586     }
587
588     return S_OK;
589 }
590
591 static const IHttpNegotiateVtbl BSCHttpNegotiateVtbl = {
592     BSCHttpNegotiate_QueryInterface,
593     BSCHttpNegotiate_AddRef,
594     BSCHttpNegotiate_Release,
595     BSCHttpNegotiate_BeginningTransaction,
596     BSCHttpNegotiate_OnResponse
597 };
598
599 static HRESULT WINAPI Authenticate_QueryInterface(IAuthenticate *iface,
600         REFIID riid, void **ppv)
601 {
602     BindStatusCallback *This = impl_from_IAuthenticate(iface);
603     return IBindStatusCallback_QueryInterface(&This->IBindStatusCallback_iface, riid, ppv);
604 }
605
606 static ULONG WINAPI Authenticate_AddRef(IAuthenticate *iface)
607 {
608     BindStatusCallback *This = impl_from_IAuthenticate(iface);
609     return IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
610 }
611
612 static ULONG WINAPI Authenticate_Release(IAuthenticate *iface)
613 {
614     BindStatusCallback *This = impl_from_IAuthenticate(iface);
615     return IBindStatusCallback_Release(&This->IBindStatusCallback_iface);
616 }
617
618 static HRESULT WINAPI Authenticate_Authenticate(IAuthenticate *iface,
619     HWND *hwnd, LPWSTR *username, LPWSTR *password)
620 {
621     BindStatusCallback *This = impl_from_IAuthenticate(iface);
622     httprequest *request = This->request;
623
624     TRACE("(%p)->(%p %p %p)\n", This, hwnd, username, password);
625
626     if (request->user && *request->user)
627     {
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)
632         {
633             CoTaskMemFree(*username);
634             CoTaskMemFree(*password);
635             return E_OUTOFMEMORY;
636         }
637
638         memcpy(*username, request->user, SysStringByteLen(request->user)+sizeof(WCHAR));
639         memcpy(*password, request->password, SysStringByteLen(request->password)+sizeof(WCHAR));
640     }
641
642     return S_OK;
643 }
644
645 static const IAuthenticateVtbl AuthenticateVtbl = {
646     Authenticate_QueryInterface,
647     Authenticate_AddRef,
648     Authenticate_Release,
649     Authenticate_Authenticate
650 };
651
652 static HRESULT BindStatusCallback_create(httprequest* This, BindStatusCallback **obj, const VARIANT *body)
653 {
654     BindStatusCallback *bsc;
655     IBindCtx *pbc;
656     HRESULT hr;
657     int size;
658
659     hr = CreateBindCtx(0, &pbc);
660     if (hr != S_OK) return hr;
661
662     bsc = heap_alloc(sizeof(*bsc));
663     if (!bsc)
664     {
665         IBindCtx_Release(pbc);
666         return E_OUTOFMEMORY;
667     }
668
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;
677
678     TRACE("(%p)->(%p)\n", This, bsc);
679
680     This->use_utf8_content = FALSE;
681
682     if (This->verb != BINDVERB_GET)
683     {
684         void *send_data, *ptr;
685         SAFEARRAY *sa = NULL;
686
687         if (V_VT(body) == (VT_VARIANT|VT_BYREF))
688             body = V_VARIANTREF(body);
689
690         switch (V_VT(body))
691         {
692         case VT_BSTR:
693         {
694             int len = SysStringLen(V_BSTR(body));
695             const WCHAR *str = V_BSTR(body);
696             UINT i, cp = CP_ACP;
697
698             for (i = 0; i < len; i++)
699             {
700                 if (str[i] > 127)
701                 {
702                     cp = CP_UTF8;
703                     break;
704                 }
705             }
706
707             size = WideCharToMultiByte(cp, 0, str, len, NULL, 0, NULL, NULL);
708             if (!(ptr = heap_alloc(size)))
709             {
710                 heap_free(bsc);
711                 return E_OUTOFMEMORY;
712             }
713             WideCharToMultiByte(cp, 0, str, len, ptr, size, NULL, NULL);
714             if (cp == CP_UTF8) This->use_utf8_content = TRUE;
715             break;
716         }
717         case VT_ARRAY|VT_UI1:
718         {
719             sa = V_ARRAY(body);
720             if ((hr = SafeArrayAccessData(sa, (void **)&ptr)) != S_OK)
721             {
722                 heap_free(bsc);
723                 return hr;
724             }
725             if ((hr = SafeArrayGetUBound(sa, 1, &size)) != S_OK)
726             {
727                 SafeArrayUnaccessData(sa);
728                 heap_free(bsc);
729                 return hr;
730             }
731             size++;
732             break;
733         }
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             ptr = NULL;
740             size = 0;
741             break;
742         }
743
744         bsc->body = GlobalAlloc(GMEM_FIXED, size);
745         if (!bsc->body)
746         {
747             if (V_VT(body) == VT_BSTR)
748                 heap_free(ptr);
749             else if (V_VT(body) == (VT_ARRAY|VT_UI1))
750                 SafeArrayUnaccessData(sa);
751
752             heap_free(bsc);
753             return E_OUTOFMEMORY;
754         }
755
756         send_data = GlobalLock(bsc->body);
757         memcpy(send_data, ptr, size);
758         GlobalUnlock(bsc->body);
759
760         if (V_VT(body) == VT_BSTR)
761             heap_free(ptr);
762         else if (V_VT(body) == (VT_ARRAY|VT_UI1))
763             SafeArrayUnaccessData(sa);
764     }
765
766     hr = RegisterBindStatusCallback(pbc, &bsc->IBindStatusCallback_iface, NULL, 0);
767     if (hr == S_OK)
768     {
769         IMoniker *moniker;
770
771         hr = CreateURLMonikerEx2(NULL, This->uri, &moniker, URL_MK_UNIFORM);
772         if (hr == S_OK)
773         {
774             IStream *stream;
775
776             hr = IMoniker_BindToStorage(moniker, pbc, NULL, &IID_IStream, (void**)&stream);
777             IMoniker_Release(moniker);
778             if (stream) IStream_Release(stream);
779         }
780         IBindCtx_Release(pbc);
781     }
782
783     if (FAILED(hr))
784     {
785         IBindStatusCallback_Release(&bsc->IBindStatusCallback_iface);
786         bsc = NULL;
787     }
788
789     *obj = bsc;
790     return hr;
791 }
792
793 static HRESULT verify_uri(httprequest *This, IUri *uri)
794 {
795     DWORD scheme, base_scheme;
796     BSTR host, base_host;
797     HRESULT hr;
798
799     if(!(This->safeopt & INTERFACESAFE_FOR_UNTRUSTED_DATA))
800         return S_OK;
801
802     if(!This->base_uri)
803         return E_ACCESSDENIED;
804
805     hr = IUri_GetScheme(uri, &scheme);
806     if(FAILED(hr))
807         return hr;
808
809     hr = IUri_GetScheme(This->base_uri, &base_scheme);
810     if(FAILED(hr))
811         return hr;
812
813     if(scheme != base_scheme) {
814         WARN("Schemes don't match\n");
815         return E_ACCESSDENIED;
816     }
817
818     if(scheme == INTERNET_SCHEME_UNKNOWN) {
819         FIXME("Unknown scheme\n");
820         return E_ACCESSDENIED;
821     }
822
823     hr = IUri_GetHost(uri, &host);
824     if(FAILED(hr))
825         return hr;
826
827     hr = IUri_GetHost(This->base_uri, &base_host);
828     if(SUCCEEDED(hr)) {
829         if(strcmpiW(host, base_host)) {
830             WARN("Hosts don't match\n");
831             hr = E_ACCESSDENIED;
832         }
833         SysFreeString(base_host);
834     }
835
836     SysFreeString(host);
837     return hr;
838 }
839
840 static HRESULT httprequest_open(httprequest *This, BSTR method, BSTR url,
841         VARIANT async, VARIANT user, VARIANT password)
842 {
843     static const WCHAR MethodGetW[] = {'G','E','T',0};
844     static const WCHAR MethodPutW[] = {'P','U','T',0};
845     static const WCHAR MethodPostW[] = {'P','O','S','T',0};
846     static const WCHAR MethodDeleteW[] = {'D','E','L','E','T','E',0};
847     static const WCHAR MethodPropFindW[] = {'P','R','O','P','F','I','N','D',0};
848     VARIANT str, is_async;
849     IUri *uri;
850     HRESULT hr;
851
852     if (!method || !url) return E_INVALIDARG;
853
854     /* free previously set data */
855     if(This->uri) {
856         IUri_Release(This->uri);
857         This->uri = NULL;
858     }
859
860     SysFreeString(This->user);
861     SysFreeString(This->password);
862     This->user = This->password = NULL;
863
864     if (!strcmpiW(method, MethodGetW))
865     {
866         This->verb = BINDVERB_GET;
867     }
868     else if (!strcmpiW(method, MethodPutW))
869     {
870         This->verb = BINDVERB_PUT;
871     }
872     else if (!strcmpiW(method, MethodPostW))
873     {
874         This->verb = BINDVERB_POST;
875     }
876     else if (!strcmpiW(method, MethodDeleteW) ||
877              !strcmpiW(method, MethodPropFindW))
878     {
879         This->verb = BINDVERB_CUSTOM;
880         SysReAllocString(&This->custom, method);
881     }
882     else
883     {
884         FIXME("unsupported request type %s\n", debugstr_w(method));
885         This->verb = -1;
886         return E_FAIL;
887     }
888
889     if(This->base_uri)
890         hr = CoInternetCombineUrlEx(This->base_uri, url, 0, &uri, 0);
891     else
892         hr = CreateUri(url, 0, 0, &uri);
893     if(FAILED(hr)) {
894         WARN("Could not create IUri object: %08x\n", hr);
895         return hr;
896     }
897
898     hr = verify_uri(This, uri);
899     if(FAILED(hr)) {
900         IUri_Release(uri);
901         return hr;
902     }
903
904     VariantInit(&str);
905     hr = VariantChangeType(&str, &user, 0, VT_BSTR);
906     if (hr == S_OK)
907         This->user = V_BSTR(&str);
908
909     VariantInit(&str);
910     hr = VariantChangeType(&str, &password, 0, VT_BSTR);
911     if (hr == S_OK)
912         This->password = V_BSTR(&str);
913
914     /* add authentication info */
915     if (This->user && *This->user)
916     {
917         IUriBuilder *builder;
918
919         hr = CreateIUriBuilder(uri, 0, 0, &builder);
920         if (hr == S_OK)
921         {
922             IUri *full_uri;
923
924             IUriBuilder_SetUserName(builder, This->user);
925             IUriBuilder_SetPassword(builder, This->password);
926             hr = IUriBuilder_CreateUri(builder, -1, 0, 0, &full_uri);
927             if (hr == S_OK)
928             {
929                 IUri_Release(uri);
930                 uri = full_uri;
931             }
932             else
933                 WARN("failed to create modified uri, 0x%08x\n", hr);
934             IUriBuilder_Release(builder);
935         }
936         else
937             WARN("IUriBuilder creation failed, 0x%08x\n", hr);
938     }
939
940     This->uri = uri;
941
942     VariantInit(&is_async);
943     hr = VariantChangeType(&is_async, &async, 0, VT_BOOL);
944     This->async = hr == S_OK && V_BOOL(&is_async);
945
946     httprequest_setreadystate(This, READYSTATE_LOADING);
947
948     return S_OK;
949 }
950
951 static HRESULT httprequest_setRequestHeader(httprequest *This, BSTR header, BSTR value)
952 {
953     struct httpheader *entry;
954
955     if (!header || !*header) return E_INVALIDARG;
956     if (This->state != READYSTATE_LOADING) return E_FAIL;
957     if (!value) return E_INVALIDARG;
958
959     /* replace existing header value if already added */
960     LIST_FOR_EACH_ENTRY(entry, &This->reqheaders, struct httpheader, entry)
961     {
962         if (lstrcmpW(entry->header, header) == 0)
963         {
964             LONG length = SysStringLen(entry->value);
965             HRESULT hr;
966
967             hr = SysReAllocString(&entry->value, value) ? S_OK : E_OUTOFMEMORY;
968
969             if (hr == S_OK)
970                 This->reqheader_size += (SysStringLen(entry->value) - length);
971
972             return hr;
973         }
974     }
975
976     entry = heap_alloc(sizeof(*entry));
977     if (!entry) return E_OUTOFMEMORY;
978
979     /* new header */
980     entry->header = SysAllocString(header);
981     entry->value  = SysAllocString(value);
982
983     /* header length including null terminator */
984     This->reqheader_size += SysStringLen(entry->header) + sizeof(colspaceW)/sizeof(WCHAR) +
985                             SysStringLen(entry->value)  + sizeof(crlfW)/sizeof(WCHAR) - 1;
986
987     list_add_head(&This->reqheaders, &entry->entry);
988
989     return S_OK;
990 }
991
992 static HRESULT httprequest_getResponseHeader(httprequest *This, BSTR header, BSTR *value)
993 {
994     struct httpheader *entry;
995
996     if (!header) return E_INVALIDARG;
997     if (!value) return E_POINTER;
998
999     if (This->raw_respheaders && list_empty(&This->respheaders))
1000     {
1001         WCHAR *ptr, *line;
1002
1003         ptr = line = This->raw_respheaders;
1004         while (*ptr)
1005         {
1006             if (*ptr == '\r' && *(ptr+1) == '\n')
1007             {
1008                 add_response_header(This, line, ptr-line);
1009                 ptr++; line = ++ptr;
1010                 continue;
1011             }
1012             ptr++;
1013         }
1014     }
1015
1016     LIST_FOR_EACH_ENTRY(entry, &This->respheaders, struct httpheader, entry)
1017     {
1018         if (!strcmpiW(entry->header, header))
1019         {
1020             *value = SysAllocString(entry->value);
1021             TRACE("header value %s\n", debugstr_w(*value));
1022             return S_OK;
1023         }
1024     }
1025
1026     return S_FALSE;
1027 }
1028
1029 static HRESULT httprequest_getAllResponseHeaders(httprequest *This, BSTR *respheaders)
1030 {
1031     if (!respheaders) return E_POINTER;
1032
1033     *respheaders = SysAllocString(This->raw_respheaders);
1034
1035     return S_OK;
1036 }
1037
1038 static HRESULT httprequest_send(httprequest *This, VARIANT body)
1039 {
1040     BindStatusCallback *bsc = NULL;
1041     HRESULT hr;
1042
1043     if (This->state != READYSTATE_LOADING) return E_FAIL;
1044
1045     hr = BindStatusCallback_create(This, &bsc, &body);
1046     if (FAILED(hr))
1047         /* success path to detach it is OnStopBinding call */
1048         BindStatusCallback_Detach(bsc);
1049
1050     return hr;
1051 }
1052
1053 static HRESULT httprequest_abort(httprequest *This)
1054 {
1055     BindStatusCallback_Detach(This->bsc);
1056
1057     httprequest_setreadystate(This, READYSTATE_UNINITIALIZED);
1058
1059     return S_OK;
1060 }
1061
1062 static HRESULT httprequest_get_status(httprequest *This, LONG *status)
1063 {
1064     if (!status) return E_POINTER;
1065
1066     *status = This->status;
1067
1068     return This->state == READYSTATE_COMPLETE ? S_OK : E_FAIL;
1069 }
1070
1071 static HRESULT httprequest_get_statusText(httprequest *This, BSTR *status)
1072 {
1073     if (!status) return E_POINTER;
1074     if (This->state != READYSTATE_COMPLETE) return E_FAIL;
1075
1076     *status = SysAllocString(This->status_text);
1077
1078     return S_OK;
1079 }
1080
1081 static HRESULT httprequest_get_responseText(httprequest *This, BSTR *body)
1082 {
1083     HGLOBAL hglobal;
1084     HRESULT hr;
1085
1086     if (!body) return E_POINTER;
1087     if (This->state != READYSTATE_COMPLETE) return E_FAIL;
1088
1089     hr = GetHGlobalFromStream(This->bsc->stream, &hglobal);
1090     if (hr == S_OK)
1091     {
1092         xmlChar *ptr = GlobalLock(hglobal);
1093         DWORD size = GlobalSize(hglobal);
1094         xmlCharEncoding encoding = XML_CHAR_ENCODING_UTF8;
1095
1096         /* try to determine data encoding */
1097         if (size >= 4)
1098         {
1099             encoding = xmlDetectCharEncoding(ptr, 4);
1100             TRACE("detected encoding: %s\n", debugstr_a(xmlGetCharEncodingName(encoding)));
1101             if ( encoding != XML_CHAR_ENCODING_UTF8 &&
1102                  encoding != XML_CHAR_ENCODING_UTF16LE &&
1103                  encoding != XML_CHAR_ENCODING_NONE )
1104             {
1105                 FIXME("unsupported encoding: %s\n", debugstr_a(xmlGetCharEncodingName(encoding)));
1106                 GlobalUnlock(hglobal);
1107                 return E_FAIL;
1108             }
1109         }
1110
1111         /* without BOM assume UTF-8 */
1112         if (encoding == XML_CHAR_ENCODING_UTF8 ||
1113             encoding == XML_CHAR_ENCODING_NONE )
1114         {
1115             DWORD length = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)ptr, size, NULL, 0);
1116
1117             *body = SysAllocStringLen(NULL, length);
1118             if (*body)
1119                 MultiByteToWideChar( CP_UTF8, 0, (LPCSTR)ptr, size, *body, length);
1120         }
1121         else
1122             *body = SysAllocStringByteLen((LPCSTR)ptr, size);
1123
1124         if (!*body) hr = E_OUTOFMEMORY;
1125         GlobalUnlock(hglobal);
1126     }
1127
1128     return hr;
1129 }
1130
1131 static HRESULT httprequest_get_responseXML(httprequest *This, IDispatch **body)
1132 {
1133     IXMLDOMDocument3 *doc;
1134     HRESULT hr;
1135     BSTR str;
1136
1137     if (!body) return E_INVALIDARG;
1138     if (This->state != READYSTATE_COMPLETE) return E_FAIL;
1139
1140     hr = DOMDocument_create(MSXML_DEFAULT, NULL, (void**)&doc);
1141     if (hr != S_OK) return hr;
1142
1143     hr = httprequest_get_responseText(This, &str);
1144     if (hr == S_OK)
1145     {
1146         VARIANT_BOOL ok;
1147
1148         hr = IXMLDOMDocument3_loadXML(doc, str, &ok);
1149         SysFreeString(str);
1150     }
1151
1152     IXMLDOMDocument3_QueryInterface(doc, &IID_IDispatch, (void**)body);
1153     IXMLDOMDocument3_Release(doc);
1154
1155     return hr;
1156 }
1157
1158 static HRESULT httprequest_get_responseBody(httprequest *This, VARIANT *body)
1159 {
1160     HGLOBAL hglobal;
1161     HRESULT hr;
1162
1163     if (!body) return E_INVALIDARG;
1164     V_VT(body) = VT_EMPTY;
1165
1166     if (This->state != READYSTATE_COMPLETE) return E_PENDING;
1167
1168     hr = GetHGlobalFromStream(This->bsc->stream, &hglobal);
1169     if (hr == S_OK)
1170     {
1171         void *ptr = GlobalLock(hglobal);
1172         DWORD size = GlobalSize(hglobal);
1173
1174         SAFEARRAYBOUND bound;
1175         SAFEARRAY *array;
1176
1177         bound.lLbound = 0;
1178         bound.cElements = size;
1179         array = SafeArrayCreate(VT_UI1, 1, &bound);
1180
1181         if (array)
1182         {
1183             void *dest;
1184
1185             V_VT(body) = VT_ARRAY | VT_UI1;
1186             V_ARRAY(body) = array;
1187
1188             hr = SafeArrayAccessData(array, &dest);
1189             if (hr == S_OK)
1190             {
1191                 memcpy(dest, ptr, size);
1192                 SafeArrayUnaccessData(array);
1193             }
1194             else
1195             {
1196                 VariantClear(body);
1197             }
1198         }
1199         else
1200             hr = E_FAIL;
1201
1202         GlobalUnlock(hglobal);
1203     }
1204
1205     return hr;
1206 }
1207
1208 static HRESULT httprequest_get_responseStream(httprequest *This, VARIANT *body)
1209 {
1210     LARGE_INTEGER move;
1211     IStream *stream;
1212     HRESULT hr;
1213
1214     if (!body) return E_INVALIDARG;
1215     V_VT(body) = VT_EMPTY;
1216
1217     if (This->state != READYSTATE_COMPLETE) return E_PENDING;
1218
1219     hr = IStream_Clone(This->bsc->stream, &stream);
1220
1221     move.QuadPart = 0;
1222     IStream_Seek(stream, move, STREAM_SEEK_SET, NULL);
1223
1224     V_VT(body) = VT_UNKNOWN;
1225     V_UNKNOWN(body) = (IUnknown*)stream;
1226
1227     return hr;
1228 }
1229
1230 static HRESULT httprequest_get_readyState(httprequest *This, LONG *state)
1231 {
1232     if (!state) return E_POINTER;
1233
1234     *state = This->state;
1235     return S_OK;
1236 }
1237
1238 static HRESULT httprequest_put_onreadystatechange(httprequest *This, IDispatch *sink)
1239 {
1240     if (This->sink) IDispatch_Release(This->sink);
1241     if ((This->sink = sink)) IDispatch_AddRef(This->sink);
1242
1243     return S_OK;
1244 }
1245
1246 static void httprequest_release(httprequest *This)
1247 {
1248     struct httpheader *header, *header2;
1249
1250     if (This->site)
1251         IUnknown_Release( This->site );
1252     if (This->uri)
1253         IUri_Release(This->uri);
1254     if (This->base_uri)
1255         IUri_Release(This->base_uri);
1256
1257     SysFreeString(This->custom);
1258     SysFreeString(This->user);
1259     SysFreeString(This->password);
1260
1261     /* request headers */
1262     LIST_FOR_EACH_ENTRY_SAFE(header, header2, &This->reqheaders, struct httpheader, entry)
1263     {
1264         list_remove(&header->entry);
1265         SysFreeString(header->header);
1266         SysFreeString(header->value);
1267         heap_free(header);
1268     }
1269     /* response headers */
1270     free_response_headers(This);
1271     SysFreeString(This->status_text);
1272
1273     /* detach callback object */
1274     BindStatusCallback_Detach(This->bsc);
1275
1276     if (This->sink) IDispatch_Release(This->sink);
1277 }
1278
1279 static HRESULT WINAPI XMLHTTPRequest_QueryInterface(IXMLHTTPRequest *iface, REFIID riid, void **ppvObject)
1280 {
1281     httprequest *This = impl_from_IXMLHTTPRequest( iface );
1282     TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppvObject);
1283
1284     if ( IsEqualGUID( riid, &IID_IXMLHTTPRequest) ||
1285          IsEqualGUID( riid, &IID_IDispatch) ||
1286          IsEqualGUID( riid, &IID_IUnknown) )
1287     {
1288         *ppvObject = iface;
1289     }
1290     else if (IsEqualGUID(&IID_IObjectWithSite, riid))
1291     {
1292         *ppvObject = &This->IObjectWithSite_iface;
1293     }
1294     else if (IsEqualGUID(&IID_IObjectSafety, riid))
1295     {
1296         *ppvObject = &This->IObjectSafety_iface;
1297     }
1298     else
1299     {
1300         TRACE("Unsupported interface %s\n", debugstr_guid(riid));
1301         *ppvObject = NULL;
1302         return E_NOINTERFACE;
1303     }
1304
1305     IXMLHTTPRequest_AddRef( iface );
1306
1307     return S_OK;
1308 }
1309
1310 static ULONG WINAPI XMLHTTPRequest_AddRef(IXMLHTTPRequest *iface)
1311 {
1312     httprequest *This = impl_from_IXMLHTTPRequest( iface );
1313     ULONG ref = InterlockedIncrement( &This->ref );
1314     TRACE("(%p)->(%u)\n", This, ref );
1315     return ref;
1316 }
1317
1318 static ULONG WINAPI XMLHTTPRequest_Release(IXMLHTTPRequest *iface)
1319 {
1320     httprequest *This = impl_from_IXMLHTTPRequest( iface );
1321     ULONG ref = InterlockedDecrement( &This->ref );
1322
1323     TRACE("(%p)->(%u)\n", This, ref );
1324
1325     if ( ref == 0 )
1326     {
1327         httprequest_release( This );
1328         heap_free( This );
1329     }
1330
1331     return ref;
1332 }
1333
1334 static HRESULT WINAPI XMLHTTPRequest_GetTypeInfoCount(IXMLHTTPRequest *iface, UINT *pctinfo)
1335 {
1336     httprequest *This = impl_from_IXMLHTTPRequest( iface );
1337
1338     TRACE("(%p)->(%p)\n", This, pctinfo);
1339
1340     *pctinfo = 1;
1341
1342     return S_OK;
1343 }
1344
1345 static HRESULT WINAPI XMLHTTPRequest_GetTypeInfo(IXMLHTTPRequest *iface, UINT iTInfo,
1346         LCID lcid, ITypeInfo **ppTInfo)
1347 {
1348     httprequest *This = impl_from_IXMLHTTPRequest( iface );
1349
1350     TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
1351
1352     return get_typeinfo(IXMLHTTPRequest_tid, ppTInfo);
1353 }
1354
1355 static HRESULT WINAPI XMLHTTPRequest_GetIDsOfNames(IXMLHTTPRequest *iface, REFIID riid,
1356         LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
1357 {
1358     httprequest *This = impl_from_IXMLHTTPRequest( iface );
1359     ITypeInfo *typeinfo;
1360     HRESULT hr;
1361
1362     TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
1363           lcid, rgDispId);
1364
1365     if(!rgszNames || cNames == 0 || !rgDispId)
1366         return E_INVALIDARG;
1367
1368     hr = get_typeinfo(IXMLHTTPRequest_tid, &typeinfo);
1369     if(SUCCEEDED(hr))
1370     {
1371         hr = ITypeInfo_GetIDsOfNames(typeinfo, rgszNames, cNames, rgDispId);
1372         ITypeInfo_Release(typeinfo);
1373     }
1374
1375     return hr;
1376 }
1377
1378 static HRESULT WINAPI XMLHTTPRequest_Invoke(IXMLHTTPRequest *iface, DISPID dispIdMember, REFIID riid,
1379         LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult,
1380         EXCEPINFO *pExcepInfo, UINT *puArgErr)
1381 {
1382     httprequest *This = impl_from_IXMLHTTPRequest( iface );
1383     ITypeInfo *typeinfo;
1384     HRESULT hr;
1385
1386     TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
1387           lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
1388
1389     hr = get_typeinfo(IXMLHTTPRequest_tid, &typeinfo);
1390     if(SUCCEEDED(hr))
1391     {
1392         hr = ITypeInfo_Invoke(typeinfo, &This->IXMLHTTPRequest_iface, dispIdMember, wFlags,
1393                 pDispParams, pVarResult, pExcepInfo, puArgErr);
1394         ITypeInfo_Release(typeinfo);
1395     }
1396
1397     return hr;
1398 }
1399
1400 static HRESULT WINAPI XMLHTTPRequest_open(IXMLHTTPRequest *iface, BSTR method, BSTR url,
1401         VARIANT async, VARIANT user, VARIANT password)
1402 {
1403     httprequest *This = impl_from_IXMLHTTPRequest( iface );
1404     TRACE("(%p)->(%s %s %s)\n", This, debugstr_w(method), debugstr_w(url),
1405         debugstr_variant(&async));
1406     return httprequest_open(This, method, url, async, user, password);
1407 }
1408
1409 static HRESULT WINAPI XMLHTTPRequest_setRequestHeader(IXMLHTTPRequest *iface, BSTR header, BSTR value)
1410 {
1411     httprequest *This = impl_from_IXMLHTTPRequest( iface );
1412     TRACE("(%p)->(%s %s)\n", This, debugstr_w(header), debugstr_w(value));
1413     return httprequest_setRequestHeader(This, header, value);
1414 }
1415
1416 static HRESULT WINAPI XMLHTTPRequest_getResponseHeader(IXMLHTTPRequest *iface, BSTR header, BSTR *value)
1417 {
1418     httprequest *This = impl_from_IXMLHTTPRequest( iface );
1419     TRACE("(%p)->(%s %p)\n", This, debugstr_w(header), value);
1420     return httprequest_getResponseHeader(This, header, value);
1421 }
1422
1423 static HRESULT WINAPI XMLHTTPRequest_getAllResponseHeaders(IXMLHTTPRequest *iface, BSTR *respheaders)
1424 {
1425     httprequest *This = impl_from_IXMLHTTPRequest( iface );
1426     TRACE("(%p)->(%p)\n", This, respheaders);
1427     return httprequest_getAllResponseHeaders(This, respheaders);
1428 }
1429
1430 static HRESULT WINAPI XMLHTTPRequest_send(IXMLHTTPRequest *iface, VARIANT body)
1431 {
1432     httprequest *This = impl_from_IXMLHTTPRequest( iface );
1433     TRACE("(%p)->(%s)\n", This, debugstr_variant(&body));
1434     return httprequest_send(This, body);
1435 }
1436
1437 static HRESULT WINAPI XMLHTTPRequest_abort(IXMLHTTPRequest *iface)
1438 {
1439     httprequest *This = impl_from_IXMLHTTPRequest( iface );
1440     TRACE("(%p)\n", This);
1441     return httprequest_abort(This);
1442 }
1443
1444 static HRESULT WINAPI XMLHTTPRequest_get_status(IXMLHTTPRequest *iface, LONG *status)
1445 {
1446     httprequest *This = impl_from_IXMLHTTPRequest( iface );
1447     TRACE("(%p)->(%p)\n", This, status);
1448     return httprequest_get_status(This, status);
1449 }
1450
1451 static HRESULT WINAPI XMLHTTPRequest_get_statusText(IXMLHTTPRequest *iface, BSTR *status)
1452 {
1453     httprequest *This = impl_from_IXMLHTTPRequest( iface );
1454     TRACE("(%p)->(%p)\n", This, status);
1455     return httprequest_get_statusText(This, status);
1456 }
1457
1458 static HRESULT WINAPI XMLHTTPRequest_get_responseXML(IXMLHTTPRequest *iface, IDispatch **body)
1459 {
1460     httprequest *This = impl_from_IXMLHTTPRequest( iface );
1461     TRACE("(%p)->(%p)\n", This, body);
1462     return httprequest_get_responseXML(This, body);
1463 }
1464
1465 static HRESULT WINAPI XMLHTTPRequest_get_responseText(IXMLHTTPRequest *iface, BSTR *body)
1466 {
1467     httprequest *This = impl_from_IXMLHTTPRequest( iface );
1468     TRACE("(%p)->(%p)\n", This, body);
1469     return httprequest_get_responseText(This, body);
1470 }
1471
1472 static HRESULT WINAPI XMLHTTPRequest_get_responseBody(IXMLHTTPRequest *iface, VARIANT *body)
1473 {
1474     httprequest *This = impl_from_IXMLHTTPRequest( iface );
1475     TRACE("(%p)->(%p)\n", This, body);
1476     return httprequest_get_responseBody(This, body);
1477 }
1478
1479 static HRESULT WINAPI XMLHTTPRequest_get_responseStream(IXMLHTTPRequest *iface, VARIANT *body)
1480 {
1481     httprequest *This = impl_from_IXMLHTTPRequest( iface );
1482     TRACE("(%p)->(%p)\n", This, body);
1483     return httprequest_get_responseStream(This, body);
1484 }
1485
1486 static HRESULT WINAPI XMLHTTPRequest_get_readyState(IXMLHTTPRequest *iface, LONG *state)
1487 {
1488     httprequest *This = impl_from_IXMLHTTPRequest( iface );
1489     TRACE("(%p)->(%p)\n", This, state);
1490     return httprequest_get_readyState(This, state);
1491 }
1492
1493 static HRESULT WINAPI XMLHTTPRequest_put_onreadystatechange(IXMLHTTPRequest *iface, IDispatch *sink)
1494 {
1495     httprequest *This = impl_from_IXMLHTTPRequest( iface );
1496     TRACE("(%p)->(%p)\n", This, sink);
1497     return httprequest_put_onreadystatechange(This, sink);
1498 }
1499
1500 static const struct IXMLHTTPRequestVtbl XMLHTTPRequestVtbl =
1501 {
1502     XMLHTTPRequest_QueryInterface,
1503     XMLHTTPRequest_AddRef,
1504     XMLHTTPRequest_Release,
1505     XMLHTTPRequest_GetTypeInfoCount,
1506     XMLHTTPRequest_GetTypeInfo,
1507     XMLHTTPRequest_GetIDsOfNames,
1508     XMLHTTPRequest_Invoke,
1509     XMLHTTPRequest_open,
1510     XMLHTTPRequest_setRequestHeader,
1511     XMLHTTPRequest_getResponseHeader,
1512     XMLHTTPRequest_getAllResponseHeaders,
1513     XMLHTTPRequest_send,
1514     XMLHTTPRequest_abort,
1515     XMLHTTPRequest_get_status,
1516     XMLHTTPRequest_get_statusText,
1517     XMLHTTPRequest_get_responseXML,
1518     XMLHTTPRequest_get_responseText,
1519     XMLHTTPRequest_get_responseBody,
1520     XMLHTTPRequest_get_responseStream,
1521     XMLHTTPRequest_get_readyState,
1522     XMLHTTPRequest_put_onreadystatechange
1523 };
1524
1525 /* IObjectWithSite */
1526 static HRESULT WINAPI
1527 httprequest_ObjectWithSite_QueryInterface( IObjectWithSite* iface, REFIID riid, void** ppvObject )
1528 {
1529     httprequest *This = impl_from_IObjectWithSite(iface);
1530     return IXMLHTTPRequest_QueryInterface( (IXMLHTTPRequest *)This, riid, ppvObject );
1531 }
1532
1533 static ULONG WINAPI httprequest_ObjectWithSite_AddRef( IObjectWithSite* iface )
1534 {
1535     httprequest *This = impl_from_IObjectWithSite(iface);
1536     return IXMLHTTPRequest_AddRef((IXMLHTTPRequest *)This);
1537 }
1538
1539 static ULONG WINAPI httprequest_ObjectWithSite_Release( IObjectWithSite* iface )
1540 {
1541     httprequest *This = impl_from_IObjectWithSite(iface);
1542     return IXMLHTTPRequest_Release((IXMLHTTPRequest *)This);
1543 }
1544
1545 static HRESULT WINAPI httprequest_ObjectWithSite_GetSite( IObjectWithSite *iface, REFIID iid, void **ppvSite )
1546 {
1547     httprequest *This = impl_from_IObjectWithSite(iface);
1548
1549     TRACE("(%p)->(%s %p)\n", This, debugstr_guid( iid ), ppvSite );
1550
1551     if ( !This->site )
1552         return E_FAIL;
1553
1554     return IUnknown_QueryInterface( This->site, iid, ppvSite );
1555 }
1556
1557 static void get_base_uri(httprequest *This)
1558 {
1559     IServiceProvider *provider;
1560     IHTMLDocument2 *doc;
1561     IUri *uri;
1562     BSTR url;
1563     HRESULT hr;
1564
1565     hr = IUnknown_QueryInterface(This->site, &IID_IServiceProvider, (void**)&provider);
1566     if(FAILED(hr))
1567         return;
1568
1569     hr = IServiceProvider_QueryService(provider, &SID_SContainerDispatch, &IID_IHTMLDocument2, (void**)&doc);
1570     IServiceProvider_Release(provider);
1571     if(FAILED(hr))
1572         return;
1573
1574     hr = IHTMLDocument2_get_URL(doc, &url);
1575     IHTMLDocument2_Release(doc);
1576     if(FAILED(hr) || !url || !*url)
1577         return;
1578
1579     TRACE("host url %s\n", debugstr_w(url));
1580
1581     hr = CreateUri(url, 0, 0, &uri);
1582     SysFreeString(url);
1583     if(FAILED(hr))
1584         return;
1585
1586     This->base_uri = uri;
1587 }
1588
1589 static HRESULT WINAPI httprequest_ObjectWithSite_SetSite( IObjectWithSite *iface, IUnknown *punk )
1590 {
1591     httprequest *This = impl_from_IObjectWithSite(iface);
1592
1593     TRACE("(%p)->(%p)\n", This, punk);
1594
1595     if(This->site)
1596         IUnknown_Release( This->site );
1597     if(This->base_uri)
1598         IUri_Release(This->base_uri);
1599
1600     This->site = punk;
1601
1602     if (punk)
1603     {
1604         IUnknown_AddRef( punk );
1605         get_base_uri(This);
1606     }
1607
1608     return S_OK;
1609 }
1610
1611 static const IObjectWithSiteVtbl ObjectWithSiteVtbl =
1612 {
1613     httprequest_ObjectWithSite_QueryInterface,
1614     httprequest_ObjectWithSite_AddRef,
1615     httprequest_ObjectWithSite_Release,
1616     httprequest_ObjectWithSite_SetSite,
1617     httprequest_ObjectWithSite_GetSite
1618 };
1619
1620 /* IObjectSafety */
1621 static HRESULT WINAPI httprequest_Safety_QueryInterface(IObjectSafety *iface, REFIID riid, void **ppv)
1622 {
1623     httprequest *This = impl_from_IObjectSafety(iface);
1624     return IXMLHTTPRequest_QueryInterface( (IXMLHTTPRequest *)This, riid, ppv );
1625 }
1626
1627 static ULONG WINAPI httprequest_Safety_AddRef(IObjectSafety *iface)
1628 {
1629     httprequest *This = impl_from_IObjectSafety(iface);
1630     return IXMLHTTPRequest_AddRef((IXMLHTTPRequest *)This);
1631 }
1632
1633 static ULONG WINAPI httprequest_Safety_Release(IObjectSafety *iface)
1634 {
1635     httprequest *This = impl_from_IObjectSafety(iface);
1636     return IXMLHTTPRequest_Release((IXMLHTTPRequest *)This);
1637 }
1638
1639 static HRESULT WINAPI httprequest_Safety_GetInterfaceSafetyOptions(IObjectSafety *iface, REFIID riid,
1640         DWORD *supported, DWORD *enabled)
1641 {
1642     httprequest *This = impl_from_IObjectSafety(iface);
1643
1644     TRACE("(%p)->(%s %p %p)\n", This, debugstr_guid(riid), supported, enabled);
1645
1646     if(!supported || !enabled) return E_POINTER;
1647
1648     *supported = safety_supported_options;
1649     *enabled = This->safeopt;
1650
1651     return S_OK;
1652 }
1653
1654 static HRESULT WINAPI httprequest_Safety_SetInterfaceSafetyOptions(IObjectSafety *iface, REFIID riid,
1655         DWORD mask, DWORD enabled)
1656 {
1657     httprequest *This = impl_from_IObjectSafety(iface);
1658     TRACE("(%p)->(%s %x %x)\n", This, debugstr_guid(riid), mask, enabled);
1659
1660     if ((mask & ~safety_supported_options))
1661         return E_FAIL;
1662
1663     This->safeopt = (This->safeopt & ~mask) | (mask & enabled);
1664
1665     return S_OK;
1666 }
1667
1668 static const IObjectSafetyVtbl ObjectSafetyVtbl = {
1669     httprequest_Safety_QueryInterface,
1670     httprequest_Safety_AddRef,
1671     httprequest_Safety_Release,
1672     httprequest_Safety_GetInterfaceSafetyOptions,
1673     httprequest_Safety_SetInterfaceSafetyOptions
1674 };
1675
1676 /* IServerXMLHTTPRequest */
1677 static HRESULT WINAPI ServerXMLHTTPRequest_QueryInterface(IServerXMLHTTPRequest *iface, REFIID riid, void **obj)
1678 {
1679     serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1680
1681     TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), obj);
1682
1683     if ( IsEqualGUID( riid, &IID_IServerXMLHTTPRequest) ||
1684          IsEqualGUID( riid, &IID_IXMLHTTPRequest) ||
1685          IsEqualGUID( riid, &IID_IDispatch) ||
1686          IsEqualGUID( riid, &IID_IUnknown) )
1687     {
1688         *obj = iface;
1689     }
1690     else
1691     {
1692         TRACE("Unsupported interface %s\n", debugstr_guid(riid));
1693         *obj = NULL;
1694         return E_NOINTERFACE;
1695     }
1696
1697     IServerXMLHTTPRequest_AddRef( iface );
1698
1699     return S_OK;
1700 }
1701
1702 static ULONG WINAPI ServerXMLHTTPRequest_AddRef(IServerXMLHTTPRequest *iface)
1703 {
1704     serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1705     ULONG ref = InterlockedIncrement( &This->ref );
1706     TRACE("(%p)->(%u)\n", This, ref );
1707     return ref;
1708 }
1709
1710 static ULONG WINAPI ServerXMLHTTPRequest_Release(IServerXMLHTTPRequest *iface)
1711 {
1712     serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1713     ULONG ref = InterlockedDecrement( &This->ref );
1714
1715     TRACE("(%p)->(%u)\n", This, ref );
1716
1717     if ( ref == 0 )
1718     {
1719         httprequest_release( &This->req );
1720         heap_free( This );
1721     }
1722
1723     return ref;
1724 }
1725
1726 static HRESULT WINAPI ServerXMLHTTPRequest_GetTypeInfoCount(IServerXMLHTTPRequest *iface, UINT *pctinfo)
1727 {
1728     serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1729
1730     TRACE("(%p)->(%p)\n", This, pctinfo);
1731     *pctinfo = 1;
1732
1733     return S_OK;
1734 }
1735
1736 static HRESULT WINAPI ServerXMLHTTPRequest_GetTypeInfo(IServerXMLHTTPRequest *iface, UINT iTInfo,
1737         LCID lcid, ITypeInfo **ppTInfo)
1738 {
1739     serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1740
1741     TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
1742
1743     return get_typeinfo(IServerXMLHTTPRequest_tid, ppTInfo);
1744 }
1745
1746 static HRESULT WINAPI ServerXMLHTTPRequest_GetIDsOfNames(IServerXMLHTTPRequest *iface, REFIID riid,
1747         LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
1748 {
1749     serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1750     ITypeInfo *typeinfo;
1751     HRESULT hr;
1752
1753     TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
1754           lcid, rgDispId);
1755
1756     if(!rgszNames || cNames == 0 || !rgDispId)
1757         return E_INVALIDARG;
1758
1759     hr = get_typeinfo(IServerXMLHTTPRequest_tid, &typeinfo);
1760     if(SUCCEEDED(hr))
1761     {
1762         hr = ITypeInfo_GetIDsOfNames(typeinfo, rgszNames, cNames, rgDispId);
1763         ITypeInfo_Release(typeinfo);
1764     }
1765
1766     return hr;
1767 }
1768
1769 static HRESULT WINAPI ServerXMLHTTPRequest_Invoke(IServerXMLHTTPRequest *iface, DISPID dispIdMember, REFIID riid,
1770         LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult,
1771         EXCEPINFO *pExcepInfo, UINT *puArgErr)
1772 {
1773     serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1774     ITypeInfo *typeinfo;
1775     HRESULT hr;
1776
1777     TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
1778           lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
1779
1780     hr = get_typeinfo(IServerXMLHTTPRequest_tid, &typeinfo);
1781     if(SUCCEEDED(hr))
1782     {
1783         hr = ITypeInfo_Invoke(typeinfo, &This->IServerXMLHTTPRequest_iface, dispIdMember, wFlags,
1784                 pDispParams, pVarResult, pExcepInfo, puArgErr);
1785         ITypeInfo_Release(typeinfo);
1786     }
1787
1788     return hr;
1789 }
1790
1791 static HRESULT WINAPI ServerXMLHTTPRequest_open(IServerXMLHTTPRequest *iface, BSTR method, BSTR url,
1792         VARIANT async, VARIANT user, VARIANT password)
1793 {
1794     serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1795     TRACE("(%p)->(%s %s %s)\n", This, debugstr_w(method), debugstr_w(url),
1796         debugstr_variant(&async));
1797     return httprequest_open(&This->req, method, url, async, user, password);
1798 }
1799
1800 static HRESULT WINAPI ServerXMLHTTPRequest_setRequestHeader(IServerXMLHTTPRequest *iface, BSTR header, BSTR value)
1801 {
1802     serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1803     TRACE("(%p)->(%s %s)\n", This, debugstr_w(header), debugstr_w(value));
1804     return httprequest_setRequestHeader(&This->req, header, value);
1805 }
1806
1807 static HRESULT WINAPI ServerXMLHTTPRequest_getResponseHeader(IServerXMLHTTPRequest *iface, BSTR header, BSTR *value)
1808 {
1809     serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1810     TRACE("(%p)->(%s %p)\n", This, debugstr_w(header), value);
1811     return httprequest_getResponseHeader(&This->req, header, value);
1812 }
1813
1814 static HRESULT WINAPI ServerXMLHTTPRequest_getAllResponseHeaders(IServerXMLHTTPRequest *iface, BSTR *respheaders)
1815 {
1816     serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1817     TRACE("(%p)->(%p)\n", This, respheaders);
1818     return httprequest_getAllResponseHeaders(&This->req, respheaders);
1819 }
1820
1821 static HRESULT WINAPI ServerXMLHTTPRequest_send(IServerXMLHTTPRequest *iface, VARIANT body)
1822 {
1823     serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1824     TRACE("(%p)->(%s)\n", This, debugstr_variant(&body));
1825     return httprequest_send(&This->req, body);
1826 }
1827
1828 static HRESULT WINAPI ServerXMLHTTPRequest_abort(IServerXMLHTTPRequest *iface)
1829 {
1830     serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1831     TRACE("(%p)\n", This);
1832     return httprequest_abort(&This->req);
1833 }
1834
1835 static HRESULT WINAPI ServerXMLHTTPRequest_get_status(IServerXMLHTTPRequest *iface, LONG *status)
1836 {
1837     serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1838     TRACE("(%p)->(%p)\n", This, status);
1839     return httprequest_get_status(&This->req, status);
1840 }
1841
1842 static HRESULT WINAPI ServerXMLHTTPRequest_get_statusText(IServerXMLHTTPRequest *iface, BSTR *status)
1843 {
1844     serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1845     TRACE("(%p)->(%p)\n", This, status);
1846     return httprequest_get_statusText(&This->req, status);
1847 }
1848
1849 static HRESULT WINAPI ServerXMLHTTPRequest_get_responseXML(IServerXMLHTTPRequest *iface, IDispatch **body)
1850 {
1851     serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1852     TRACE("(%p)->(%p)\n", This, body);
1853     return httprequest_get_responseXML(&This->req, body);
1854 }
1855
1856 static HRESULT WINAPI ServerXMLHTTPRequest_get_responseText(IServerXMLHTTPRequest *iface, BSTR *body)
1857 {
1858     serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1859     TRACE("(%p)->(%p)\n", This, body);
1860     return httprequest_get_responseText(&This->req, body);
1861 }
1862
1863 static HRESULT WINAPI ServerXMLHTTPRequest_get_responseBody(IServerXMLHTTPRequest *iface, VARIANT *body)
1864 {
1865     serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1866     TRACE("(%p)->(%p)\n", This, body);
1867     return httprequest_get_responseBody(&This->req, body);
1868 }
1869
1870 static HRESULT WINAPI ServerXMLHTTPRequest_get_responseStream(IServerXMLHTTPRequest *iface, VARIANT *body)
1871 {
1872     serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1873     TRACE("(%p)->(%p)\n", This, body);
1874     return httprequest_get_responseStream(&This->req, body);
1875 }
1876
1877 static HRESULT WINAPI ServerXMLHTTPRequest_get_readyState(IServerXMLHTTPRequest *iface, LONG *state)
1878 {
1879     serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1880     TRACE("(%p)->(%p)\n", This, state);
1881     return httprequest_get_readyState(&This->req, state);
1882 }
1883
1884 static HRESULT WINAPI ServerXMLHTTPRequest_put_onreadystatechange(IServerXMLHTTPRequest *iface, IDispatch *sink)
1885 {
1886     serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1887     TRACE("(%p)->(%p)\n", This, sink);
1888     return httprequest_put_onreadystatechange(&This->req, sink);
1889 }
1890
1891 static HRESULT WINAPI ServerXMLHTTPRequest_setTimeouts(IServerXMLHTTPRequest *iface, LONG resolveTimeout, LONG connectTimeout,
1892     LONG sendTimeout, LONG receiveTimeout)
1893 {
1894     serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1895     FIXME("(%p)->(%d %d %d %d): stub\n", This, resolveTimeout, connectTimeout, sendTimeout, receiveTimeout);
1896     return E_NOTIMPL;
1897 }
1898
1899 static HRESULT WINAPI ServerXMLHTTPRequest_waitForResponse(IServerXMLHTTPRequest *iface, VARIANT timeout, VARIANT_BOOL *isSuccessful)
1900 {
1901     serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1902     FIXME("(%p)->(%s %p): stub\n", This, debugstr_variant(&timeout), isSuccessful);
1903     return E_NOTIMPL;
1904 }
1905
1906 static HRESULT WINAPI ServerXMLHTTPRequest_getOption(IServerXMLHTTPRequest *iface, SERVERXMLHTTP_OPTION option, VARIANT *value)
1907 {
1908     serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1909     FIXME("(%p)->(%d %p): stub\n", This, option, value);
1910     return E_NOTIMPL;
1911 }
1912
1913 static HRESULT WINAPI ServerXMLHTTPRequest_setOption(IServerXMLHTTPRequest *iface, SERVERXMLHTTP_OPTION option, VARIANT value)
1914 {
1915     serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1916     FIXME("(%p)->(%d %s): stub\n", This, option, debugstr_variant(&value));
1917     return E_NOTIMPL;
1918 }
1919
1920 static const struct IServerXMLHTTPRequestVtbl ServerXMLHTTPRequestVtbl =
1921 {
1922     ServerXMLHTTPRequest_QueryInterface,
1923     ServerXMLHTTPRequest_AddRef,
1924     ServerXMLHTTPRequest_Release,
1925     ServerXMLHTTPRequest_GetTypeInfoCount,
1926     ServerXMLHTTPRequest_GetTypeInfo,
1927     ServerXMLHTTPRequest_GetIDsOfNames,
1928     ServerXMLHTTPRequest_Invoke,
1929     ServerXMLHTTPRequest_open,
1930     ServerXMLHTTPRequest_setRequestHeader,
1931     ServerXMLHTTPRequest_getResponseHeader,
1932     ServerXMLHTTPRequest_getAllResponseHeaders,
1933     ServerXMLHTTPRequest_send,
1934     ServerXMLHTTPRequest_abort,
1935     ServerXMLHTTPRequest_get_status,
1936     ServerXMLHTTPRequest_get_statusText,
1937     ServerXMLHTTPRequest_get_responseXML,
1938     ServerXMLHTTPRequest_get_responseText,
1939     ServerXMLHTTPRequest_get_responseBody,
1940     ServerXMLHTTPRequest_get_responseStream,
1941     ServerXMLHTTPRequest_get_readyState,
1942     ServerXMLHTTPRequest_put_onreadystatechange,
1943     ServerXMLHTTPRequest_setTimeouts,
1944     ServerXMLHTTPRequest_waitForResponse,
1945     ServerXMLHTTPRequest_getOption,
1946     ServerXMLHTTPRequest_setOption
1947 };
1948
1949 static void init_httprequest(httprequest *req)
1950 {
1951     req->IXMLHTTPRequest_iface.lpVtbl = &XMLHTTPRequestVtbl;
1952     req->IObjectWithSite_iface.lpVtbl = &ObjectWithSiteVtbl;
1953     req->IObjectSafety_iface.lpVtbl = &ObjectSafetyVtbl;
1954     req->ref = 1;
1955
1956     req->async = FALSE;
1957     req->verb = -1;
1958     req->custom = NULL;
1959     req->uri = req->base_uri = NULL;
1960     req->user = req->password = NULL;
1961
1962     req->state = READYSTATE_UNINITIALIZED;
1963     req->sink = NULL;
1964
1965     req->bsc = NULL;
1966     req->status = 0;
1967     req->status_text = NULL;
1968     req->reqheader_size = 0;
1969     req->raw_respheaders = NULL;
1970     req->use_utf8_content = FALSE;
1971
1972     list_init(&req->reqheaders);
1973     list_init(&req->respheaders);
1974
1975     req->site = NULL;
1976     req->safeopt = 0;
1977 }
1978
1979 HRESULT XMLHTTPRequest_create(IUnknown *outer, void **obj)
1980 {
1981     httprequest *req;
1982
1983     TRACE("(%p, %p)\n", outer, obj);
1984
1985     req = heap_alloc( sizeof (*req) );
1986     if( !req )
1987         return E_OUTOFMEMORY;
1988
1989     init_httprequest(req);
1990     *obj = &req->IXMLHTTPRequest_iface;
1991
1992     TRACE("returning iface %p\n", *obj);
1993
1994     return S_OK;
1995 }
1996
1997 HRESULT ServerXMLHTTP_create(IUnknown *outer, void **obj)
1998 {
1999     serverhttp *req;
2000
2001     TRACE("(%p, %p)\n", outer, obj);
2002
2003     req = heap_alloc( sizeof (*req) );
2004     if( !req )
2005         return E_OUTOFMEMORY;
2006
2007     init_httprequest(&req->req);
2008     req->IServerXMLHTTPRequest_iface.lpVtbl = &ServerXMLHTTPRequestVtbl;
2009     req->ref = 1;
2010
2011     *obj = &req->IServerXMLHTTPRequest_iface;
2012
2013     TRACE("returning iface %p\n", *obj);
2014
2015     return S_OK;
2016 }
2017
2018 #else
2019
2020 HRESULT XMLHTTPRequest_create(IUnknown *pUnkOuter, void **ppObj)
2021 {
2022     MESSAGE("This program tried to use a XMLHTTPRequest object, but\n"
2023             "libxml2 support was not present at compile time.\n");
2024     return E_NOTIMPL;
2025 }
2026
2027 HRESULT ServerXMLHTTP_create(IUnknown *outer, void **obj)
2028 {
2029     MESSAGE("This program tried to use a ServerXMLHTTP object, but\n"
2030             "libxml2 support was not present at compile time.\n");
2031     return E_NOTIMPL;
2032 }
2033
2034 #endif