comctl32/imagelist: Use proper color format for merged image lists.
[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     FIXME("(%p)->(%p %p %p)\n", This, hwnd, username, password);
623     return E_NOTIMPL;
624 }
625
626 static const IAuthenticateVtbl AuthenticateVtbl = {
627     Authenticate_QueryInterface,
628     Authenticate_AddRef,
629     Authenticate_Release,
630     Authenticate_Authenticate
631 };
632
633 static HRESULT BindStatusCallback_create(httprequest* This, BindStatusCallback **obj, const VARIANT *body)
634 {
635     BindStatusCallback *bsc;
636     IBindCtx *pbc;
637     HRESULT hr;
638     int size;
639
640     hr = CreateBindCtx(0, &pbc);
641     if (hr != S_OK) return hr;
642
643     bsc = heap_alloc(sizeof(*bsc));
644     if (!bsc)
645     {
646         IBindCtx_Release(pbc);
647         return E_OUTOFMEMORY;
648     }
649
650     bsc->IBindStatusCallback_iface.lpVtbl = &BindStatusCallbackVtbl;
651     bsc->IHttpNegotiate_iface.lpVtbl = &BSCHttpNegotiateVtbl;
652     bsc->IAuthenticate_iface.lpVtbl = &AuthenticateVtbl;
653     bsc->ref = 1;
654     bsc->request = This;
655     bsc->binding = NULL;
656     bsc->stream = NULL;
657     bsc->body = NULL;
658
659     TRACE("(%p)->(%p)\n", This, bsc);
660
661     This->use_utf8_content = FALSE;
662
663     if (This->verb != BINDVERB_GET)
664     {
665         void *send_data, *ptr;
666         SAFEARRAY *sa = NULL;
667
668         if (V_VT(body) == (VT_VARIANT|VT_BYREF))
669             body = V_VARIANTREF(body);
670
671         switch (V_VT(body))
672         {
673         case VT_BSTR:
674         {
675             int len = SysStringLen(V_BSTR(body));
676             const WCHAR *str = V_BSTR(body);
677             UINT i, cp = CP_ACP;
678
679             for (i = 0; i < len; i++)
680             {
681                 if (str[i] > 127)
682                 {
683                     cp = CP_UTF8;
684                     break;
685                 }
686             }
687
688             size = WideCharToMultiByte(cp, 0, str, len, NULL, 0, NULL, NULL);
689             if (!(ptr = heap_alloc(size)))
690             {
691                 heap_free(bsc);
692                 return E_OUTOFMEMORY;
693             }
694             WideCharToMultiByte(cp, 0, str, len, ptr, size, NULL, NULL);
695             if (cp == CP_UTF8) This->use_utf8_content = TRUE;
696             break;
697         }
698         case VT_ARRAY|VT_UI1:
699         {
700             sa = V_ARRAY(body);
701             if ((hr = SafeArrayAccessData(sa, (void **)&ptr)) != S_OK)
702             {
703                 heap_free(bsc);
704                 return hr;
705             }
706             if ((hr = SafeArrayGetUBound(sa, 1, &size) != S_OK))
707             {
708                 SafeArrayUnaccessData(sa);
709                 heap_free(bsc);
710                 return hr;
711             }
712             size++;
713             break;
714         }
715         default:
716             FIXME("unsupported body data type %d\n", V_VT(body));
717             /* fall through */
718         case VT_EMPTY:
719         case VT_ERROR:
720             ptr = NULL;
721             size = 0;
722             break;
723         }
724
725         bsc->body = GlobalAlloc(GMEM_FIXED, size);
726         if (!bsc->body)
727         {
728             if (V_VT(body) == VT_BSTR)
729                 heap_free(ptr);
730             else if (V_VT(body) == (VT_ARRAY|VT_UI1))
731                 SafeArrayUnaccessData(sa);
732
733             heap_free(bsc);
734             return E_OUTOFMEMORY;
735         }
736
737         send_data = GlobalLock(bsc->body);
738         memcpy(send_data, ptr, size);
739         GlobalUnlock(bsc->body);
740
741         if (V_VT(body) == VT_BSTR)
742             heap_free(ptr);
743         else if (V_VT(body) == (VT_ARRAY|VT_UI1))
744             SafeArrayUnaccessData(sa);
745     }
746
747     hr = RegisterBindStatusCallback(pbc, &bsc->IBindStatusCallback_iface, NULL, 0);
748     if (hr == S_OK)
749     {
750         IMoniker *moniker;
751
752         hr = CreateURLMonikerEx2(NULL, This->uri, &moniker, URL_MK_UNIFORM);
753         if (hr == S_OK)
754         {
755             IStream *stream;
756
757             hr = IMoniker_BindToStorage(moniker, pbc, NULL, &IID_IStream, (void**)&stream);
758             IMoniker_Release(moniker);
759             if (stream) IStream_Release(stream);
760         }
761         IBindCtx_Release(pbc);
762     }
763
764     if (FAILED(hr))
765     {
766         IBindStatusCallback_Release(&bsc->IBindStatusCallback_iface);
767         bsc = NULL;
768     }
769
770     *obj = bsc;
771     return hr;
772 }
773
774 static HRESULT verify_uri(httprequest *This, IUri *uri)
775 {
776     DWORD scheme, base_scheme;
777     BSTR host, base_host;
778     HRESULT hr;
779
780     if(!(This->safeopt & INTERFACESAFE_FOR_UNTRUSTED_DATA))
781         return S_OK;
782
783     if(!This->base_uri)
784         return E_ACCESSDENIED;
785
786     hr = IUri_GetScheme(uri, &scheme);
787     if(FAILED(hr))
788         return hr;
789
790     hr = IUri_GetScheme(This->base_uri, &base_scheme);
791     if(FAILED(hr))
792         return hr;
793
794     if(scheme != base_scheme) {
795         WARN("Schemes don't match\n");
796         return E_ACCESSDENIED;
797     }
798
799     if(scheme == INTERNET_SCHEME_UNKNOWN) {
800         FIXME("Unknown scheme\n");
801         return E_ACCESSDENIED;
802     }
803
804     hr = IUri_GetHost(uri, &host);
805     if(FAILED(hr))
806         return hr;
807
808     hr = IUri_GetHost(This->base_uri, &base_host);
809     if(SUCCEEDED(hr)) {
810         if(strcmpiW(host, base_host)) {
811             WARN("Hosts don't match\n");
812             hr = E_ACCESSDENIED;
813         }
814         SysFreeString(base_host);
815     }
816
817     SysFreeString(host);
818     return hr;
819 }
820
821 static HRESULT httprequest_open(httprequest *This, BSTR method, BSTR url,
822         VARIANT async, VARIANT user, VARIANT password)
823 {
824     static const WCHAR MethodGetW[] = {'G','E','T',0};
825     static const WCHAR MethodPutW[] = {'P','U','T',0};
826     static const WCHAR MethodPostW[] = {'P','O','S','T',0};
827     static const WCHAR MethodDeleteW[] = {'D','E','L','E','T','E',0};
828     static const WCHAR MethodPropFindW[] = {'P','R','O','P','F','I','N','D',0};
829     VARIANT str, is_async;
830     IUri *uri;
831     HRESULT hr;
832
833     if (!method || !url) return E_INVALIDARG;
834
835     /* free previously set data */
836     if(This->uri) {
837         IUri_Release(This->uri);
838         This->uri = NULL;
839     }
840
841     SysFreeString(This->user);
842     SysFreeString(This->password);
843     This->user = This->password = NULL;
844
845     if (!strcmpiW(method, MethodGetW))
846     {
847         This->verb = BINDVERB_GET;
848     }
849     else if (!strcmpiW(method, MethodPutW))
850     {
851         This->verb = BINDVERB_PUT;
852     }
853     else if (!strcmpiW(method, MethodPostW))
854     {
855         This->verb = BINDVERB_POST;
856     }
857     else if (!strcmpiW(method, MethodDeleteW) ||
858              !strcmpiW(method, MethodPropFindW))
859     {
860         This->verb = BINDVERB_CUSTOM;
861         SysReAllocString(&This->custom, method);
862     }
863     else
864     {
865         FIXME("unsupported request type %s\n", debugstr_w(method));
866         This->verb = -1;
867         return E_FAIL;
868     }
869
870     if(This->base_uri)
871         hr = CoInternetCombineUrlEx(This->base_uri, url, 0, &uri, 0);
872     else
873         hr = CreateUri(url, 0, 0, &uri);
874     if(FAILED(hr)) {
875         WARN("Could not create IUri object: %08x\n", hr);
876         return hr;
877     }
878
879     hr = verify_uri(This, uri);
880     if(FAILED(hr)) {
881         IUri_Release(uri);
882         return hr;
883     }
884
885     This->uri = uri;
886
887     VariantInit(&is_async);
888     hr = VariantChangeType(&is_async, &async, 0, VT_BOOL);
889     This->async = hr == S_OK && V_BOOL(&is_async);
890
891     VariantInit(&str);
892     hr = VariantChangeType(&str, &user, 0, VT_BSTR);
893     if (hr == S_OK)
894         This->user = V_BSTR(&str);
895
896     VariantInit(&str);
897     hr = VariantChangeType(&str, &password, 0, VT_BSTR);
898     if (hr == S_OK)
899         This->password = V_BSTR(&str);
900
901     httprequest_setreadystate(This, READYSTATE_LOADING);
902
903     return S_OK;
904 }
905
906 static HRESULT httprequest_setRequestHeader(httprequest *This, BSTR header, BSTR value)
907 {
908     struct httpheader *entry;
909
910     if (!header || !*header) return E_INVALIDARG;
911     if (This->state != READYSTATE_LOADING) return E_FAIL;
912     if (!value) return E_INVALIDARG;
913
914     /* replace existing header value if already added */
915     LIST_FOR_EACH_ENTRY(entry, &This->reqheaders, struct httpheader, entry)
916     {
917         if (lstrcmpW(entry->header, header) == 0)
918         {
919             LONG length = SysStringLen(entry->value);
920             HRESULT hr;
921
922             hr = SysReAllocString(&entry->value, value) ? S_OK : E_OUTOFMEMORY;
923
924             if (hr == S_OK)
925                 This->reqheader_size += (SysStringLen(entry->value) - length);
926
927             return hr;
928         }
929     }
930
931     entry = heap_alloc(sizeof(*entry));
932     if (!entry) return E_OUTOFMEMORY;
933
934     /* new header */
935     entry->header = SysAllocString(header);
936     entry->value  = SysAllocString(value);
937
938     /* header length including null terminator */
939     This->reqheader_size += SysStringLen(entry->header) + sizeof(colspaceW)/sizeof(WCHAR) +
940                             SysStringLen(entry->value)  + sizeof(crlfW)/sizeof(WCHAR) - 1;
941
942     list_add_head(&This->reqheaders, &entry->entry);
943
944     return S_OK;
945 }
946
947 static HRESULT httprequest_getResponseHeader(httprequest *This, BSTR header, BSTR *value)
948 {
949     struct httpheader *entry;
950
951     if (!header || !value) return E_INVALIDARG;
952
953     if (This->raw_respheaders && list_empty(&This->respheaders))
954     {
955         WCHAR *ptr, *line;
956
957         ptr = line = This->raw_respheaders;
958         while (*ptr)
959         {
960             if (*ptr == '\r' && *(ptr+1) == '\n')
961             {
962                 add_response_header(This, line, ptr-line);
963                 ptr++; line = ++ptr;
964                 continue;
965             }
966             ptr++;
967         }
968     }
969
970     LIST_FOR_EACH_ENTRY(entry, &This->respheaders, struct httpheader, entry)
971     {
972         if (!strcmpiW(entry->header, header))
973         {
974             *value = SysAllocString(entry->value);
975             TRACE("header value %s\n", debugstr_w(*value));
976             return S_OK;
977         }
978     }
979
980     return S_FALSE;
981 }
982
983 static HRESULT httprequest_getAllResponseHeaders(httprequest *This, BSTR *respheaders)
984 {
985     if (!respheaders) return E_INVALIDARG;
986
987     *respheaders = SysAllocString(This->raw_respheaders);
988
989     return S_OK;
990 }
991
992 static HRESULT httprequest_send(httprequest *This, VARIANT body)
993 {
994     BindStatusCallback *bsc = NULL;
995     HRESULT hr;
996
997     if (This->state != READYSTATE_LOADING) return E_FAIL;
998
999     hr = BindStatusCallback_create(This, &bsc, &body);
1000     if (FAILED(hr))
1001         /* success path to detach it is OnStopBinding call */
1002         BindStatusCallback_Detach(bsc);
1003
1004     return hr;
1005 }
1006
1007 static HRESULT httprequest_abort(httprequest *This)
1008 {
1009     BindStatusCallback_Detach(This->bsc);
1010
1011     httprequest_setreadystate(This, READYSTATE_UNINITIALIZED);
1012
1013     return S_OK;
1014 }
1015
1016 static HRESULT httprequest_get_status(httprequest *This, LONG *status)
1017 {
1018     if (!status) return E_INVALIDARG;
1019     if (This->state != READYSTATE_COMPLETE) return E_FAIL;
1020
1021     *status = This->status;
1022
1023     return S_OK;
1024 }
1025
1026 static HRESULT httprequest_get_statusText(httprequest *This, BSTR *status)
1027 {
1028     if (!status) return E_INVALIDARG;
1029     if (This->state != READYSTATE_COMPLETE) return E_FAIL;
1030
1031     *status = SysAllocString(This->status_text);
1032
1033     return S_OK;
1034 }
1035
1036 static HRESULT httprequest_get_responseText(httprequest *This, BSTR *body)
1037 {
1038     HGLOBAL hglobal;
1039     HRESULT hr;
1040
1041     if (!body) return E_INVALIDARG;
1042     if (This->state != READYSTATE_COMPLETE) return E_FAIL;
1043
1044     hr = GetHGlobalFromStream(This->bsc->stream, &hglobal);
1045     if (hr == S_OK)
1046     {
1047         xmlChar *ptr = GlobalLock(hglobal);
1048         DWORD size = GlobalSize(hglobal);
1049         xmlCharEncoding encoding = XML_CHAR_ENCODING_UTF8;
1050
1051         /* try to determine data encoding */
1052         if (size >= 4)
1053         {
1054             encoding = xmlDetectCharEncoding(ptr, 4);
1055             TRACE("detected encoding: %s\n", debugstr_a(xmlGetCharEncodingName(encoding)));
1056             if ( encoding != XML_CHAR_ENCODING_UTF8 &&
1057                  encoding != XML_CHAR_ENCODING_UTF16LE &&
1058                  encoding != XML_CHAR_ENCODING_NONE )
1059             {
1060                 FIXME("unsupported encoding: %s\n", debugstr_a(xmlGetCharEncodingName(encoding)));
1061                 GlobalUnlock(hglobal);
1062                 return E_FAIL;
1063             }
1064         }
1065
1066         /* without BOM assume UTF-8 */
1067         if (encoding == XML_CHAR_ENCODING_UTF8 ||
1068             encoding == XML_CHAR_ENCODING_NONE )
1069         {
1070             DWORD length = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)ptr, size, NULL, 0);
1071
1072             *body = SysAllocStringLen(NULL, length);
1073             if (*body)
1074                 MultiByteToWideChar( CP_UTF8, 0, (LPCSTR)ptr, size, *body, length);
1075         }
1076         else
1077             *body = SysAllocStringByteLen((LPCSTR)ptr, size);
1078
1079         if (!*body) hr = E_OUTOFMEMORY;
1080         GlobalUnlock(hglobal);
1081     }
1082
1083     return hr;
1084 }
1085
1086 static HRESULT httprequest_get_responseXML(httprequest *This, IDispatch **body)
1087 {
1088     IXMLDOMDocument3 *doc;
1089     HRESULT hr;
1090     BSTR str;
1091
1092     if (!body) return E_INVALIDARG;
1093     if (This->state != READYSTATE_COMPLETE) return E_FAIL;
1094
1095     hr = DOMDocument_create(MSXML_DEFAULT, NULL, (void**)&doc);
1096     if (hr != S_OK) return hr;
1097
1098     hr = httprequest_get_responseText(This, &str);
1099     if (hr == S_OK)
1100     {
1101         VARIANT_BOOL ok;
1102
1103         hr = IXMLDOMDocument3_loadXML(doc, str, &ok);
1104         SysFreeString(str);
1105     }
1106
1107     IXMLDOMDocument3_QueryInterface(doc, &IID_IDispatch, (void**)body);
1108     IXMLDOMDocument3_Release(doc);
1109
1110     return hr;
1111 }
1112
1113 static HRESULT httprequest_get_responseBody(httprequest *This, VARIANT *body)
1114 {
1115     HGLOBAL hglobal;
1116     HRESULT hr;
1117
1118     if (!body) return E_INVALIDARG;
1119     V_VT(body) = VT_EMPTY;
1120
1121     if (This->state != READYSTATE_COMPLETE) return E_PENDING;
1122
1123     hr = GetHGlobalFromStream(This->bsc->stream, &hglobal);
1124     if (hr == S_OK)
1125     {
1126         void *ptr = GlobalLock(hglobal);
1127         DWORD size = GlobalSize(hglobal);
1128
1129         SAFEARRAYBOUND bound;
1130         SAFEARRAY *array;
1131
1132         bound.lLbound = 0;
1133         bound.cElements = size;
1134         array = SafeArrayCreate(VT_UI1, 1, &bound);
1135
1136         if (array)
1137         {
1138             void *dest;
1139
1140             V_VT(body) = VT_ARRAY | VT_UI1;
1141             V_ARRAY(body) = array;
1142
1143             hr = SafeArrayAccessData(array, &dest);
1144             if (hr == S_OK)
1145             {
1146                 memcpy(dest, ptr, size);
1147                 SafeArrayUnaccessData(array);
1148             }
1149             else
1150             {
1151                 VariantClear(body);
1152             }
1153         }
1154         else
1155             hr = E_FAIL;
1156
1157         GlobalUnlock(hglobal);
1158     }
1159
1160     return hr;
1161 }
1162
1163 static HRESULT httprequest_get_responseStream(httprequest *This, VARIANT *body)
1164 {
1165     LARGE_INTEGER move;
1166     IStream *stream;
1167     HRESULT hr;
1168
1169     if (!body) return E_INVALIDARG;
1170     V_VT(body) = VT_EMPTY;
1171
1172     if (This->state != READYSTATE_COMPLETE) return E_PENDING;
1173
1174     hr = IStream_Clone(This->bsc->stream, &stream);
1175
1176     move.QuadPart = 0;
1177     IStream_Seek(stream, move, STREAM_SEEK_SET, NULL);
1178
1179     V_VT(body) = VT_UNKNOWN;
1180     V_UNKNOWN(body) = (IUnknown*)stream;
1181
1182     return hr;
1183 }
1184
1185 static HRESULT httprequest_get_readyState(httprequest *This, LONG *state)
1186 {
1187     if (!state) return E_INVALIDARG;
1188
1189     *state = This->state;
1190     return S_OK;
1191 }
1192
1193 static HRESULT httprequest_put_onreadystatechange(httprequest *This, IDispatch *sink)
1194 {
1195     if (This->sink) IDispatch_Release(This->sink);
1196     if ((This->sink = sink)) IDispatch_AddRef(This->sink);
1197
1198     return S_OK;
1199 }
1200
1201 static void httprequest_release(httprequest *This)
1202 {
1203     struct httpheader *header, *header2;
1204
1205     if (This->site)
1206         IUnknown_Release( This->site );
1207     if (This->uri)
1208         IUri_Release(This->uri);
1209     if (This->base_uri)
1210         IUri_Release(This->base_uri);
1211
1212     SysFreeString(This->custom);
1213     SysFreeString(This->user);
1214     SysFreeString(This->password);
1215
1216     /* request headers */
1217     LIST_FOR_EACH_ENTRY_SAFE(header, header2, &This->reqheaders, struct httpheader, entry)
1218     {
1219         list_remove(&header->entry);
1220         SysFreeString(header->header);
1221         SysFreeString(header->value);
1222         heap_free(header);
1223     }
1224     /* response headers */
1225     free_response_headers(This);
1226     SysFreeString(This->status_text);
1227
1228     /* detach callback object */
1229     BindStatusCallback_Detach(This->bsc);
1230
1231     if (This->sink) IDispatch_Release(This->sink);
1232 }
1233
1234 static HRESULT WINAPI XMLHTTPRequest_QueryInterface(IXMLHTTPRequest *iface, REFIID riid, void **ppvObject)
1235 {
1236     httprequest *This = impl_from_IXMLHTTPRequest( iface );
1237     TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppvObject);
1238
1239     if ( IsEqualGUID( riid, &IID_IXMLHTTPRequest) ||
1240          IsEqualGUID( riid, &IID_IDispatch) ||
1241          IsEqualGUID( riid, &IID_IUnknown) )
1242     {
1243         *ppvObject = iface;
1244     }
1245     else if (IsEqualGUID(&IID_IObjectWithSite, riid))
1246     {
1247         *ppvObject = &This->IObjectWithSite_iface;
1248     }
1249     else if (IsEqualGUID(&IID_IObjectSafety, riid))
1250     {
1251         *ppvObject = &This->IObjectSafety_iface;
1252     }
1253     else
1254     {
1255         TRACE("Unsupported interface %s\n", debugstr_guid(riid));
1256         *ppvObject = NULL;
1257         return E_NOINTERFACE;
1258     }
1259
1260     IXMLHTTPRequest_AddRef( iface );
1261
1262     return S_OK;
1263 }
1264
1265 static ULONG WINAPI XMLHTTPRequest_AddRef(IXMLHTTPRequest *iface)
1266 {
1267     httprequest *This = impl_from_IXMLHTTPRequest( iface );
1268     ULONG ref = InterlockedIncrement( &This->ref );
1269     TRACE("(%p)->(%u)\n", This, ref );
1270     return ref;
1271 }
1272
1273 static ULONG WINAPI XMLHTTPRequest_Release(IXMLHTTPRequest *iface)
1274 {
1275     httprequest *This = impl_from_IXMLHTTPRequest( iface );
1276     ULONG ref = InterlockedDecrement( &This->ref );
1277
1278     TRACE("(%p)->(%u)\n", This, ref );
1279
1280     if ( ref == 0 )
1281     {
1282         httprequest_release( This );
1283         heap_free( This );
1284     }
1285
1286     return ref;
1287 }
1288
1289 static HRESULT WINAPI XMLHTTPRequest_GetTypeInfoCount(IXMLHTTPRequest *iface, UINT *pctinfo)
1290 {
1291     httprequest *This = impl_from_IXMLHTTPRequest( iface );
1292
1293     TRACE("(%p)->(%p)\n", This, pctinfo);
1294
1295     *pctinfo = 1;
1296
1297     return S_OK;
1298 }
1299
1300 static HRESULT WINAPI XMLHTTPRequest_GetTypeInfo(IXMLHTTPRequest *iface, UINT iTInfo,
1301         LCID lcid, ITypeInfo **ppTInfo)
1302 {
1303     httprequest *This = impl_from_IXMLHTTPRequest( iface );
1304
1305     TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
1306
1307     return get_typeinfo(IXMLHTTPRequest_tid, ppTInfo);
1308 }
1309
1310 static HRESULT WINAPI XMLHTTPRequest_GetIDsOfNames(IXMLHTTPRequest *iface, REFIID riid,
1311         LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
1312 {
1313     httprequest *This = impl_from_IXMLHTTPRequest( iface );
1314     ITypeInfo *typeinfo;
1315     HRESULT hr;
1316
1317     TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
1318           lcid, rgDispId);
1319
1320     if(!rgszNames || cNames == 0 || !rgDispId)
1321         return E_INVALIDARG;
1322
1323     hr = get_typeinfo(IXMLHTTPRequest_tid, &typeinfo);
1324     if(SUCCEEDED(hr))
1325     {
1326         hr = ITypeInfo_GetIDsOfNames(typeinfo, rgszNames, cNames, rgDispId);
1327         ITypeInfo_Release(typeinfo);
1328     }
1329
1330     return hr;
1331 }
1332
1333 static HRESULT WINAPI XMLHTTPRequest_Invoke(IXMLHTTPRequest *iface, DISPID dispIdMember, REFIID riid,
1334         LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult,
1335         EXCEPINFO *pExcepInfo, UINT *puArgErr)
1336 {
1337     httprequest *This = impl_from_IXMLHTTPRequest( iface );
1338     ITypeInfo *typeinfo;
1339     HRESULT hr;
1340
1341     TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
1342           lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
1343
1344     hr = get_typeinfo(IXMLHTTPRequest_tid, &typeinfo);
1345     if(SUCCEEDED(hr))
1346     {
1347         hr = ITypeInfo_Invoke(typeinfo, &This->IXMLHTTPRequest_iface, dispIdMember, wFlags,
1348                 pDispParams, pVarResult, pExcepInfo, puArgErr);
1349         ITypeInfo_Release(typeinfo);
1350     }
1351
1352     return hr;
1353 }
1354
1355 static HRESULT WINAPI XMLHTTPRequest_open(IXMLHTTPRequest *iface, BSTR method, BSTR url,
1356         VARIANT async, VARIANT user, VARIANT password)
1357 {
1358     httprequest *This = impl_from_IXMLHTTPRequest( iface );
1359     TRACE("(%p)->(%s %s %s)\n", This, debugstr_w(method), debugstr_w(url),
1360         debugstr_variant(&async));
1361     return httprequest_open(This, method, url, async, user, password);
1362 }
1363
1364 static HRESULT WINAPI XMLHTTPRequest_setRequestHeader(IXMLHTTPRequest *iface, BSTR header, BSTR value)
1365 {
1366     httprequest *This = impl_from_IXMLHTTPRequest( iface );
1367     TRACE("(%p)->(%s %s)\n", This, debugstr_w(header), debugstr_w(value));
1368     return httprequest_setRequestHeader(This, header, value);
1369 }
1370
1371 static HRESULT WINAPI XMLHTTPRequest_getResponseHeader(IXMLHTTPRequest *iface, BSTR header, BSTR *value)
1372 {
1373     httprequest *This = impl_from_IXMLHTTPRequest( iface );
1374     TRACE("(%p)->(%s %p)\n", This, debugstr_w(header), value);
1375     return httprequest_getResponseHeader(This, header, value);
1376 }
1377
1378 static HRESULT WINAPI XMLHTTPRequest_getAllResponseHeaders(IXMLHTTPRequest *iface, BSTR *respheaders)
1379 {
1380     httprequest *This = impl_from_IXMLHTTPRequest( iface );
1381     TRACE("(%p)->(%p)\n", This, respheaders);
1382     return httprequest_getAllResponseHeaders(This, respheaders);
1383 }
1384
1385 static HRESULT WINAPI XMLHTTPRequest_send(IXMLHTTPRequest *iface, VARIANT body)
1386 {
1387     httprequest *This = impl_from_IXMLHTTPRequest( iface );
1388     TRACE("(%p)->(%s)\n", This, debugstr_variant(&body));
1389     return httprequest_send(This, body);
1390 }
1391
1392 static HRESULT WINAPI XMLHTTPRequest_abort(IXMLHTTPRequest *iface)
1393 {
1394     httprequest *This = impl_from_IXMLHTTPRequest( iface );
1395     TRACE("(%p)\n", This);
1396     return httprequest_abort(This);
1397 }
1398
1399 static HRESULT WINAPI XMLHTTPRequest_get_status(IXMLHTTPRequest *iface, LONG *status)
1400 {
1401     httprequest *This = impl_from_IXMLHTTPRequest( iface );
1402     TRACE("(%p)->(%p)\n", This, status);
1403     return httprequest_get_status(This, status);
1404 }
1405
1406 static HRESULT WINAPI XMLHTTPRequest_get_statusText(IXMLHTTPRequest *iface, BSTR *status)
1407 {
1408     httprequest *This = impl_from_IXMLHTTPRequest( iface );
1409     TRACE("(%p)->(%p)\n", This, status);
1410     return httprequest_get_statusText(This, status);
1411 }
1412
1413 static HRESULT WINAPI XMLHTTPRequest_get_responseXML(IXMLHTTPRequest *iface, IDispatch **body)
1414 {
1415     httprequest *This = impl_from_IXMLHTTPRequest( iface );
1416     TRACE("(%p)->(%p)\n", This, body);
1417     return httprequest_get_responseXML(This, body);
1418 }
1419
1420 static HRESULT WINAPI XMLHTTPRequest_get_responseText(IXMLHTTPRequest *iface, BSTR *body)
1421 {
1422     httprequest *This = impl_from_IXMLHTTPRequest( iface );
1423     TRACE("(%p)->(%p)\n", This, body);
1424     return httprequest_get_responseText(This, body);
1425 }
1426
1427 static HRESULT WINAPI XMLHTTPRequest_get_responseBody(IXMLHTTPRequest *iface, VARIANT *body)
1428 {
1429     httprequest *This = impl_from_IXMLHTTPRequest( iface );
1430     TRACE("(%p)->(%p)\n", This, body);
1431     return httprequest_get_responseBody(This, body);
1432 }
1433
1434 static HRESULT WINAPI XMLHTTPRequest_get_responseStream(IXMLHTTPRequest *iface, VARIANT *body)
1435 {
1436     httprequest *This = impl_from_IXMLHTTPRequest( iface );
1437     TRACE("(%p)->(%p)\n", This, body);
1438     return httprequest_get_responseStream(This, body);
1439 }
1440
1441 static HRESULT WINAPI XMLHTTPRequest_get_readyState(IXMLHTTPRequest *iface, LONG *state)
1442 {
1443     httprequest *This = impl_from_IXMLHTTPRequest( iface );
1444     TRACE("(%p)->(%p)\n", This, state);
1445     return httprequest_get_readyState(This, state);
1446 }
1447
1448 static HRESULT WINAPI XMLHTTPRequest_put_onreadystatechange(IXMLHTTPRequest *iface, IDispatch *sink)
1449 {
1450     httprequest *This = impl_from_IXMLHTTPRequest( iface );
1451     TRACE("(%p)->(%p)\n", This, sink);
1452     return httprequest_put_onreadystatechange(This, sink);
1453 }
1454
1455 static const struct IXMLHTTPRequestVtbl XMLHTTPRequestVtbl =
1456 {
1457     XMLHTTPRequest_QueryInterface,
1458     XMLHTTPRequest_AddRef,
1459     XMLHTTPRequest_Release,
1460     XMLHTTPRequest_GetTypeInfoCount,
1461     XMLHTTPRequest_GetTypeInfo,
1462     XMLHTTPRequest_GetIDsOfNames,
1463     XMLHTTPRequest_Invoke,
1464     XMLHTTPRequest_open,
1465     XMLHTTPRequest_setRequestHeader,
1466     XMLHTTPRequest_getResponseHeader,
1467     XMLHTTPRequest_getAllResponseHeaders,
1468     XMLHTTPRequest_send,
1469     XMLHTTPRequest_abort,
1470     XMLHTTPRequest_get_status,
1471     XMLHTTPRequest_get_statusText,
1472     XMLHTTPRequest_get_responseXML,
1473     XMLHTTPRequest_get_responseText,
1474     XMLHTTPRequest_get_responseBody,
1475     XMLHTTPRequest_get_responseStream,
1476     XMLHTTPRequest_get_readyState,
1477     XMLHTTPRequest_put_onreadystatechange
1478 };
1479
1480 /* IObjectWithSite */
1481 static HRESULT WINAPI
1482 httprequest_ObjectWithSite_QueryInterface( IObjectWithSite* iface, REFIID riid, void** ppvObject )
1483 {
1484     httprequest *This = impl_from_IObjectWithSite(iface);
1485     return IXMLHTTPRequest_QueryInterface( (IXMLHTTPRequest *)This, riid, ppvObject );
1486 }
1487
1488 static ULONG WINAPI httprequest_ObjectWithSite_AddRef( IObjectWithSite* iface )
1489 {
1490     httprequest *This = impl_from_IObjectWithSite(iface);
1491     return IXMLHTTPRequest_AddRef((IXMLHTTPRequest *)This);
1492 }
1493
1494 static ULONG WINAPI httprequest_ObjectWithSite_Release( IObjectWithSite* iface )
1495 {
1496     httprequest *This = impl_from_IObjectWithSite(iface);
1497     return IXMLHTTPRequest_Release((IXMLHTTPRequest *)This);
1498 }
1499
1500 static HRESULT WINAPI httprequest_ObjectWithSite_GetSite( IObjectWithSite *iface, REFIID iid, void **ppvSite )
1501 {
1502     httprequest *This = impl_from_IObjectWithSite(iface);
1503
1504     TRACE("(%p)->(%s %p)\n", This, debugstr_guid( iid ), ppvSite );
1505
1506     if ( !This->site )
1507         return E_FAIL;
1508
1509     return IUnknown_QueryInterface( This->site, iid, ppvSite );
1510 }
1511
1512 static void get_base_uri(httprequest *This)
1513 {
1514     IServiceProvider *provider;
1515     IHTMLDocument2 *doc;
1516     IUri *uri;
1517     BSTR url;
1518     HRESULT hr;
1519
1520     hr = IUnknown_QueryInterface(This->site, &IID_IServiceProvider, (void**)&provider);
1521     if(FAILED(hr))
1522         return;
1523
1524     hr = IServiceProvider_QueryService(provider, &SID_SContainerDispatch, &IID_IHTMLDocument2, (void**)&doc);
1525     IServiceProvider_Release(provider);
1526     if(FAILED(hr))
1527         return;
1528
1529     hr = IHTMLDocument2_get_URL(doc, &url);
1530     IHTMLDocument2_Release(doc);
1531     if(FAILED(hr) || !url || !*url)
1532         return;
1533
1534     TRACE("host url %s\n", debugstr_w(url));
1535
1536     hr = CreateUri(url, 0, 0, &uri);
1537     SysFreeString(url);
1538     if(FAILED(hr))
1539         return;
1540
1541     This->base_uri = uri;
1542 }
1543
1544 static HRESULT WINAPI httprequest_ObjectWithSite_SetSite( IObjectWithSite *iface, IUnknown *punk )
1545 {
1546     httprequest *This = impl_from_IObjectWithSite(iface);
1547
1548     TRACE("(%p)->(%p)\n", This, punk);
1549
1550     if(This->site)
1551         IUnknown_Release( This->site );
1552     if(This->base_uri)
1553         IUri_Release(This->base_uri);
1554
1555     This->site = punk;
1556
1557     if (punk)
1558     {
1559         IUnknown_AddRef( punk );
1560         get_base_uri(This);
1561     }
1562
1563     return S_OK;
1564 }
1565
1566 static const IObjectWithSiteVtbl ObjectWithSiteVtbl =
1567 {
1568     httprequest_ObjectWithSite_QueryInterface,
1569     httprequest_ObjectWithSite_AddRef,
1570     httprequest_ObjectWithSite_Release,
1571     httprequest_ObjectWithSite_SetSite,
1572     httprequest_ObjectWithSite_GetSite
1573 };
1574
1575 /* IObjectSafety */
1576 static HRESULT WINAPI httprequest_Safety_QueryInterface(IObjectSafety *iface, REFIID riid, void **ppv)
1577 {
1578     httprequest *This = impl_from_IObjectSafety(iface);
1579     return IXMLHTTPRequest_QueryInterface( (IXMLHTTPRequest *)This, riid, ppv );
1580 }
1581
1582 static ULONG WINAPI httprequest_Safety_AddRef(IObjectSafety *iface)
1583 {
1584     httprequest *This = impl_from_IObjectSafety(iface);
1585     return IXMLHTTPRequest_AddRef((IXMLHTTPRequest *)This);
1586 }
1587
1588 static ULONG WINAPI httprequest_Safety_Release(IObjectSafety *iface)
1589 {
1590     httprequest *This = impl_from_IObjectSafety(iface);
1591     return IXMLHTTPRequest_Release((IXMLHTTPRequest *)This);
1592 }
1593
1594 static HRESULT WINAPI httprequest_Safety_GetInterfaceSafetyOptions(IObjectSafety *iface, REFIID riid,
1595         DWORD *supported, DWORD *enabled)
1596 {
1597     httprequest *This = impl_from_IObjectSafety(iface);
1598
1599     TRACE("(%p)->(%s %p %p)\n", This, debugstr_guid(riid), supported, enabled);
1600
1601     if(!supported || !enabled) return E_POINTER;
1602
1603     *supported = safety_supported_options;
1604     *enabled = This->safeopt;
1605
1606     return S_OK;
1607 }
1608
1609 static HRESULT WINAPI httprequest_Safety_SetInterfaceSafetyOptions(IObjectSafety *iface, REFIID riid,
1610         DWORD mask, DWORD enabled)
1611 {
1612     httprequest *This = impl_from_IObjectSafety(iface);
1613     TRACE("(%p)->(%s %x %x)\n", This, debugstr_guid(riid), mask, enabled);
1614
1615     if ((mask & ~safety_supported_options))
1616         return E_FAIL;
1617
1618     This->safeopt = (This->safeopt & ~mask) | (mask & enabled);
1619
1620     return S_OK;
1621 }
1622
1623 static const IObjectSafetyVtbl ObjectSafetyVtbl = {
1624     httprequest_Safety_QueryInterface,
1625     httprequest_Safety_AddRef,
1626     httprequest_Safety_Release,
1627     httprequest_Safety_GetInterfaceSafetyOptions,
1628     httprequest_Safety_SetInterfaceSafetyOptions
1629 };
1630
1631 /* IServerXMLHTTPRequest */
1632 static HRESULT WINAPI ServerXMLHTTPRequest_QueryInterface(IServerXMLHTTPRequest *iface, REFIID riid, void **obj)
1633 {
1634     serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1635
1636     TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), obj);
1637
1638     if ( IsEqualGUID( riid, &IID_IServerXMLHTTPRequest) ||
1639          IsEqualGUID( riid, &IID_IXMLHTTPRequest) ||
1640          IsEqualGUID( riid, &IID_IDispatch) ||
1641          IsEqualGUID( riid, &IID_IUnknown) )
1642     {
1643         *obj = iface;
1644     }
1645     else
1646     {
1647         TRACE("Unsupported interface %s\n", debugstr_guid(riid));
1648         *obj = NULL;
1649         return E_NOINTERFACE;
1650     }
1651
1652     IServerXMLHTTPRequest_AddRef( iface );
1653
1654     return S_OK;
1655 }
1656
1657 static ULONG WINAPI ServerXMLHTTPRequest_AddRef(IServerXMLHTTPRequest *iface)
1658 {
1659     serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1660     ULONG ref = InterlockedIncrement( &This->ref );
1661     TRACE("(%p)->(%u)\n", This, ref );
1662     return ref;
1663 }
1664
1665 static ULONG WINAPI ServerXMLHTTPRequest_Release(IServerXMLHTTPRequest *iface)
1666 {
1667     serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1668     ULONG ref = InterlockedDecrement( &This->ref );
1669
1670     TRACE("(%p)->(%u)\n", This, ref );
1671
1672     if ( ref == 0 )
1673     {
1674         httprequest_release( &This->req );
1675         heap_free( This );
1676     }
1677
1678     return ref;
1679 }
1680
1681 static HRESULT WINAPI ServerXMLHTTPRequest_GetTypeInfoCount(IServerXMLHTTPRequest *iface, UINT *pctinfo)
1682 {
1683     serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1684
1685     TRACE("(%p)->(%p)\n", This, pctinfo);
1686     *pctinfo = 1;
1687
1688     return S_OK;
1689 }
1690
1691 static HRESULT WINAPI ServerXMLHTTPRequest_GetTypeInfo(IServerXMLHTTPRequest *iface, UINT iTInfo,
1692         LCID lcid, ITypeInfo **ppTInfo)
1693 {
1694     serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1695
1696     TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
1697
1698     return get_typeinfo(IServerXMLHTTPRequest_tid, ppTInfo);
1699 }
1700
1701 static HRESULT WINAPI ServerXMLHTTPRequest_GetIDsOfNames(IServerXMLHTTPRequest *iface, REFIID riid,
1702         LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
1703 {
1704     serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1705     ITypeInfo *typeinfo;
1706     HRESULT hr;
1707
1708     TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
1709           lcid, rgDispId);
1710
1711     if(!rgszNames || cNames == 0 || !rgDispId)
1712         return E_INVALIDARG;
1713
1714     hr = get_typeinfo(IServerXMLHTTPRequest_tid, &typeinfo);
1715     if(SUCCEEDED(hr))
1716     {
1717         hr = ITypeInfo_GetIDsOfNames(typeinfo, rgszNames, cNames, rgDispId);
1718         ITypeInfo_Release(typeinfo);
1719     }
1720
1721     return hr;
1722 }
1723
1724 static HRESULT WINAPI ServerXMLHTTPRequest_Invoke(IServerXMLHTTPRequest *iface, DISPID dispIdMember, REFIID riid,
1725         LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult,
1726         EXCEPINFO *pExcepInfo, UINT *puArgErr)
1727 {
1728     serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1729     ITypeInfo *typeinfo;
1730     HRESULT hr;
1731
1732     TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
1733           lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
1734
1735     hr = get_typeinfo(IServerXMLHTTPRequest_tid, &typeinfo);
1736     if(SUCCEEDED(hr))
1737     {
1738         hr = ITypeInfo_Invoke(typeinfo, &This->IServerXMLHTTPRequest_iface, dispIdMember, wFlags,
1739                 pDispParams, pVarResult, pExcepInfo, puArgErr);
1740         ITypeInfo_Release(typeinfo);
1741     }
1742
1743     return hr;
1744 }
1745
1746 static HRESULT WINAPI ServerXMLHTTPRequest_open(IServerXMLHTTPRequest *iface, BSTR method, BSTR url,
1747         VARIANT async, VARIANT user, VARIANT password)
1748 {
1749     serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1750     TRACE("(%p)->(%s %s %s)\n", This, debugstr_w(method), debugstr_w(url),
1751         debugstr_variant(&async));
1752     return httprequest_open(&This->req, method, url, async, user, password);
1753 }
1754
1755 static HRESULT WINAPI ServerXMLHTTPRequest_setRequestHeader(IServerXMLHTTPRequest *iface, BSTR header, BSTR value)
1756 {
1757     serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1758     TRACE("(%p)->(%s %s)\n", This, debugstr_w(header), debugstr_w(value));
1759     return httprequest_setRequestHeader(&This->req, header, value);
1760 }
1761
1762 static HRESULT WINAPI ServerXMLHTTPRequest_getResponseHeader(IServerXMLHTTPRequest *iface, BSTR header, BSTR *value)
1763 {
1764     serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1765     TRACE("(%p)->(%s %p)\n", This, debugstr_w(header), value);
1766     return httprequest_getResponseHeader(&This->req, header, value);
1767 }
1768
1769 static HRESULT WINAPI ServerXMLHTTPRequest_getAllResponseHeaders(IServerXMLHTTPRequest *iface, BSTR *respheaders)
1770 {
1771     serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1772     TRACE("(%p)->(%p)\n", This, respheaders);
1773     return httprequest_getAllResponseHeaders(&This->req, respheaders);
1774 }
1775
1776 static HRESULT WINAPI ServerXMLHTTPRequest_send(IServerXMLHTTPRequest *iface, VARIANT body)
1777 {
1778     serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1779     TRACE("(%p)->(%s)\n", This, debugstr_variant(&body));
1780     return httprequest_send(&This->req, body);
1781 }
1782
1783 static HRESULT WINAPI ServerXMLHTTPRequest_abort(IServerXMLHTTPRequest *iface)
1784 {
1785     serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1786     TRACE("(%p)\n", This);
1787     return httprequest_abort(&This->req);
1788 }
1789
1790 static HRESULT WINAPI ServerXMLHTTPRequest_get_status(IServerXMLHTTPRequest *iface, LONG *status)
1791 {
1792     serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1793     TRACE("(%p)->(%p)\n", This, status);
1794     return httprequest_get_status(&This->req, status);
1795 }
1796
1797 static HRESULT WINAPI ServerXMLHTTPRequest_get_statusText(IServerXMLHTTPRequest *iface, BSTR *status)
1798 {
1799     serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1800     TRACE("(%p)->(%p)\n", This, status);
1801     return httprequest_get_statusText(&This->req, status);
1802 }
1803
1804 static HRESULT WINAPI ServerXMLHTTPRequest_get_responseXML(IServerXMLHTTPRequest *iface, IDispatch **body)
1805 {
1806     serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1807     TRACE("(%p)->(%p)\n", This, body);
1808     return httprequest_get_responseXML(&This->req, body);
1809 }
1810
1811 static HRESULT WINAPI ServerXMLHTTPRequest_get_responseText(IServerXMLHTTPRequest *iface, BSTR *body)
1812 {
1813     serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1814     TRACE("(%p)->(%p)\n", This, body);
1815     return httprequest_get_responseText(&This->req, body);
1816 }
1817
1818 static HRESULT WINAPI ServerXMLHTTPRequest_get_responseBody(IServerXMLHTTPRequest *iface, VARIANT *body)
1819 {
1820     serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1821     TRACE("(%p)->(%p)\n", This, body);
1822     return httprequest_get_responseBody(&This->req, body);
1823 }
1824
1825 static HRESULT WINAPI ServerXMLHTTPRequest_get_responseStream(IServerXMLHTTPRequest *iface, VARIANT *body)
1826 {
1827     serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1828     TRACE("(%p)->(%p)\n", This, body);
1829     return httprequest_get_responseStream(&This->req, body);
1830 }
1831
1832 static HRESULT WINAPI ServerXMLHTTPRequest_get_readyState(IServerXMLHTTPRequest *iface, LONG *state)
1833 {
1834     serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1835     TRACE("(%p)->(%p)\n", This, state);
1836     return httprequest_get_readyState(&This->req, state);
1837 }
1838
1839 static HRESULT WINAPI ServerXMLHTTPRequest_put_onreadystatechange(IServerXMLHTTPRequest *iface, IDispatch *sink)
1840 {
1841     serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1842     TRACE("(%p)->(%p)\n", This, sink);
1843     return httprequest_put_onreadystatechange(&This->req, sink);
1844 }
1845
1846 static HRESULT WINAPI ServerXMLHTTPRequest_setTimeouts(IServerXMLHTTPRequest *iface, LONG resolveTimeout, LONG connectTimeout,
1847     LONG sendTimeout, LONG receiveTimeout)
1848 {
1849     serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1850     FIXME("(%p)->(%d %d %d %d): stub\n", This, resolveTimeout, connectTimeout, sendTimeout, receiveTimeout);
1851     return E_NOTIMPL;
1852 }
1853
1854 static HRESULT WINAPI ServerXMLHTTPRequest_waitForResponse(IServerXMLHTTPRequest *iface, VARIANT timeout, VARIANT_BOOL *isSuccessful)
1855 {
1856     serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1857     FIXME("(%p)->(%s %p): stub\n", This, debugstr_variant(&timeout), isSuccessful);
1858     return E_NOTIMPL;
1859 }
1860
1861 static HRESULT WINAPI ServerXMLHTTPRequest_getOption(IServerXMLHTTPRequest *iface, SERVERXMLHTTP_OPTION option, VARIANT *value)
1862 {
1863     serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1864     FIXME("(%p)->(%d %p): stub\n", This, option, value);
1865     return E_NOTIMPL;
1866 }
1867
1868 static HRESULT WINAPI ServerXMLHTTPRequest_setOption(IServerXMLHTTPRequest *iface, SERVERXMLHTTP_OPTION option, VARIANT value)
1869 {
1870     serverhttp *This = impl_from_IServerXMLHTTPRequest( iface );
1871     FIXME("(%p)->(%d %s): stub\n", This, option, debugstr_variant(&value));
1872     return E_NOTIMPL;
1873 }
1874
1875 static const struct IServerXMLHTTPRequestVtbl ServerXMLHTTPRequestVtbl =
1876 {
1877     ServerXMLHTTPRequest_QueryInterface,
1878     ServerXMLHTTPRequest_AddRef,
1879     ServerXMLHTTPRequest_Release,
1880     ServerXMLHTTPRequest_GetTypeInfoCount,
1881     ServerXMLHTTPRequest_GetTypeInfo,
1882     ServerXMLHTTPRequest_GetIDsOfNames,
1883     ServerXMLHTTPRequest_Invoke,
1884     ServerXMLHTTPRequest_open,
1885     ServerXMLHTTPRequest_setRequestHeader,
1886     ServerXMLHTTPRequest_getResponseHeader,
1887     ServerXMLHTTPRequest_getAllResponseHeaders,
1888     ServerXMLHTTPRequest_send,
1889     ServerXMLHTTPRequest_abort,
1890     ServerXMLHTTPRequest_get_status,
1891     ServerXMLHTTPRequest_get_statusText,
1892     ServerXMLHTTPRequest_get_responseXML,
1893     ServerXMLHTTPRequest_get_responseText,
1894     ServerXMLHTTPRequest_get_responseBody,
1895     ServerXMLHTTPRequest_get_responseStream,
1896     ServerXMLHTTPRequest_get_readyState,
1897     ServerXMLHTTPRequest_put_onreadystatechange,
1898     ServerXMLHTTPRequest_setTimeouts,
1899     ServerXMLHTTPRequest_waitForResponse,
1900     ServerXMLHTTPRequest_getOption,
1901     ServerXMLHTTPRequest_setOption
1902 };
1903
1904 static void init_httprequest(httprequest *req)
1905 {
1906     req->IXMLHTTPRequest_iface.lpVtbl = &XMLHTTPRequestVtbl;
1907     req->IObjectWithSite_iface.lpVtbl = &ObjectWithSiteVtbl;
1908     req->IObjectSafety_iface.lpVtbl = &ObjectSafetyVtbl;
1909     req->ref = 1;
1910
1911     req->async = FALSE;
1912     req->verb = -1;
1913     req->custom = NULL;
1914     req->uri = req->base_uri = NULL;
1915     req->user = req->password = NULL;
1916
1917     req->state = READYSTATE_UNINITIALIZED;
1918     req->sink = NULL;
1919
1920     req->bsc = NULL;
1921     req->status = 0;
1922     req->status_text = NULL;
1923     req->reqheader_size = 0;
1924     req->raw_respheaders = NULL;
1925     req->use_utf8_content = FALSE;
1926
1927     list_init(&req->reqheaders);
1928     list_init(&req->respheaders);
1929
1930     req->site = NULL;
1931     req->safeopt = 0;
1932 }
1933
1934 HRESULT XMLHTTPRequest_create(IUnknown *outer, void **obj)
1935 {
1936     httprequest *req;
1937
1938     TRACE("(%p, %p)\n", outer, obj);
1939
1940     req = heap_alloc( sizeof (*req) );
1941     if( !req )
1942         return E_OUTOFMEMORY;
1943
1944     init_httprequest(req);
1945     *obj = &req->IXMLHTTPRequest_iface;
1946
1947     TRACE("returning iface %p\n", *obj);
1948
1949     return S_OK;
1950 }
1951
1952 HRESULT ServerXMLHTTP_create(IUnknown *outer, void **obj)
1953 {
1954     serverhttp *req;
1955
1956     TRACE("(%p, %p)\n", outer, obj);
1957
1958     req = heap_alloc( sizeof (*req) );
1959     if( !req )
1960         return E_OUTOFMEMORY;
1961
1962     init_httprequest(&req->req);
1963     req->IServerXMLHTTPRequest_iface.lpVtbl = &ServerXMLHTTPRequestVtbl;
1964     req->ref = 1;
1965
1966     *obj = &req->IServerXMLHTTPRequest_iface;
1967
1968     TRACE("returning iface %p\n", *obj);
1969
1970     return S_OK;
1971 }
1972
1973 #else
1974
1975 HRESULT XMLHTTPRequest_create(IUnknown *pUnkOuter, void **ppObj)
1976 {
1977     MESSAGE("This program tried to use a XMLHTTPRequest object, but\n"
1978             "libxml2 support was not present at compile time.\n");
1979     return E_NOTIMPL;
1980 }
1981
1982 HRESULT ServerXMLHTTP_create(IUnknown *outer, void **obj)
1983 {
1984     MESSAGE("This program tried to use a ServerXMLHTTP object, but\n"
1985             "libxml2 support was not present at compile time.\n");
1986     return E_NOTIMPL;
1987 }
1988
1989 #endif