msxml3: Fix VT_BSTR case for IXMLHttpRequest::send().
[wine] / dlls / msxml3 / httprequest.c
1 /*
2  *    IXMLHTTPRequest implementation
3  *
4  * Copyright 2008 Alistair Leslie-Hughes
5  * Copyright 2010 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 "winuser.h"
37 #include "ole2.h"
38 #include "msxml6.h"
39 #include "objsafe.h"
40
41 #include "msxml_private.h"
42
43 #include "wine/debug.h"
44 #include "wine/list.h"
45
46 WINE_DEFAULT_DEBUG_CHANNEL(msxml);
47
48 #ifdef HAVE_LIBXML2
49
50 static const WCHAR colspaceW[] = {':',' ',0};
51 static const WCHAR crlfW[] = {'\r','\n',0};
52
53 typedef struct BindStatusCallback BindStatusCallback;
54
55 struct httpheader
56 {
57     struct list entry;
58     BSTR header;
59     BSTR value;
60 };
61
62 typedef struct
63 {
64     IXMLHTTPRequest IXMLHTTPRequest_iface;
65     IObjectWithSite IObjectWithSite_iface;
66     IObjectSafety   IObjectSafety_iface;
67     LONG ref;
68
69     READYSTATE state;
70     IDispatch *sink;
71
72     /* request */
73     BINDVERB verb;
74     BSTR custom;
75     BSTR url;
76     BOOL async;
77     struct list reqheaders;
78     /* cached resulting custom request headers string length in WCHARs */
79     LONG reqheader_size;
80     /* use UTF-8 content type */
81     BOOL use_utf8_content;
82
83     /* response headers */
84     struct list respheaders;
85     BSTR raw_respheaders;
86
87     /* credentials */
88     BSTR user;
89     BSTR password;
90
91     /* bind callback */
92     BindStatusCallback *bsc;
93     LONG status;
94
95     /* IObjectWithSite*/
96     IUnknown *site;
97
98     /* IObjectSafety */
99     DWORD safeopt;
100 } httprequest;
101
102 static inline httprequest *impl_from_IXMLHTTPRequest( IXMLHTTPRequest *iface )
103 {
104     return CONTAINING_RECORD(iface, httprequest, IXMLHTTPRequest_iface);
105 }
106
107 static inline httprequest *impl_from_IObjectWithSite(IObjectWithSite *iface)
108 {
109     return CONTAINING_RECORD(iface, httprequest, IObjectWithSite_iface);
110 }
111
112 static inline httprequest *impl_from_IObjectSafety(IObjectSafety *iface)
113 {
114     return CONTAINING_RECORD(iface, httprequest, IObjectSafety_iface);
115 }
116
117 static void httprequest_setreadystate(httprequest *This, READYSTATE state)
118 {
119     READYSTATE last = This->state;
120
121     This->state = state;
122
123     if (This->sink && last != state)
124     {
125         DISPPARAMS params;
126
127         memset(&params, 0, sizeof(params));
128         IDispatch_Invoke(This->sink, 0, &IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD, &params, 0, 0, 0);
129     }
130 }
131
132 static void free_response_headers(httprequest *This)
133 {
134     struct httpheader *header, *header2;
135
136     LIST_FOR_EACH_ENTRY_SAFE(header, header2, &This->respheaders, struct httpheader, entry)
137     {
138         list_remove(&header->entry);
139         SysFreeString(header->header);
140         SysFreeString(header->value);
141         heap_free(header);
142     }
143
144     SysFreeString(This->raw_respheaders);
145     This->raw_respheaders = NULL;
146 }
147
148 struct BindStatusCallback
149 {
150     IBindStatusCallback IBindStatusCallback_iface;
151     IHttpNegotiate      IHttpNegotiate_iface;
152     LONG ref;
153
154     IBinding *binding;
155     httprequest *request;
156
157     /* response data */
158     IStream *stream;
159
160     /* request body data */
161     HGLOBAL body;
162 };
163
164 static inline BindStatusCallback *impl_from_IBindStatusCallback( IBindStatusCallback *iface )
165 {
166     return CONTAINING_RECORD(iface, BindStatusCallback, IBindStatusCallback_iface);
167 }
168
169 static inline BindStatusCallback *impl_from_IHttpNegotiate( IHttpNegotiate *iface )
170 {
171     return CONTAINING_RECORD(iface, BindStatusCallback, IHttpNegotiate_iface);
172 }
173
174 static void BindStatusCallback_Detach(BindStatusCallback *bsc)
175 {
176     if (bsc)
177     {
178         if (bsc->binding) IBinding_Abort(bsc->binding);
179         bsc->request = NULL;
180         IBindStatusCallback_Release(&bsc->IBindStatusCallback_iface);
181     }
182 }
183
184 static HRESULT WINAPI BindStatusCallback_QueryInterface(IBindStatusCallback *iface,
185         REFIID riid, void **ppv)
186 {
187     BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
188
189     *ppv = NULL;
190
191     TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
192
193     if (IsEqualGUID(&IID_IUnknown, riid) ||
194         IsEqualGUID(&IID_IBindStatusCallback, riid))
195     {
196         *ppv = &This->IBindStatusCallback_iface;
197     }
198     else if (IsEqualGUID(&IID_IHttpNegotiate, riid))
199     {
200         *ppv = &This->IHttpNegotiate_iface;
201     }
202     else if (IsEqualGUID(&IID_IServiceProvider, riid) ||
203              IsEqualGUID(&IID_IBindStatusCallbackEx, riid) ||
204              IsEqualGUID(&IID_IInternetProtocol, riid) ||
205              IsEqualGUID(&IID_IHttpNegotiate2, riid))
206     {
207         return E_NOINTERFACE;
208     }
209
210     if (*ppv)
211     {
212         IBindStatusCallback_AddRef(iface);
213         return S_OK;
214     }
215
216     FIXME("Unsupported riid = %s\n", debugstr_guid(riid));
217
218     return E_NOINTERFACE;
219 }
220
221 static ULONG WINAPI BindStatusCallback_AddRef(IBindStatusCallback *iface)
222 {
223     BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
224     LONG ref = InterlockedIncrement(&This->ref);
225
226     TRACE("(%p) ref = %d\n", This, ref);
227
228     return ref;
229 }
230
231 static ULONG WINAPI BindStatusCallback_Release(IBindStatusCallback *iface)
232 {
233     BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
234     LONG ref = InterlockedDecrement(&This->ref);
235
236     TRACE("(%p) ref = %d\n", This, ref);
237
238     if (!ref)
239     {
240         if (This->binding) IBinding_Release(This->binding);
241         if (This->stream) IStream_Release(This->stream);
242         if (This->body) GlobalFree(This->body);
243         heap_free(This);
244     }
245
246     return ref;
247 }
248
249 static HRESULT WINAPI BindStatusCallback_OnStartBinding(IBindStatusCallback *iface,
250         DWORD reserved, IBinding *pbind)
251 {
252     BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
253
254     TRACE("(%p)->(%d %p)\n", This, reserved, pbind);
255
256     if (!pbind) return E_INVALIDARG;
257
258     This->binding = pbind;
259     IBinding_AddRef(pbind);
260
261     httprequest_setreadystate(This->request, READYSTATE_LOADED);
262
263     return CreateStreamOnHGlobal(NULL, TRUE, &This->stream);
264 }
265
266 static HRESULT WINAPI BindStatusCallback_GetPriority(IBindStatusCallback *iface, LONG *pPriority)
267 {
268     BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
269
270     TRACE("(%p)->(%p)\n", This, pPriority);
271
272     return E_NOTIMPL;
273 }
274
275 static HRESULT WINAPI BindStatusCallback_OnLowResource(IBindStatusCallback *iface, DWORD reserved)
276 {
277     BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
278
279     TRACE("(%p)->(%d)\n", This, reserved);
280
281     return E_NOTIMPL;
282 }
283
284 static HRESULT WINAPI BindStatusCallback_OnProgress(IBindStatusCallback *iface, ULONG ulProgress,
285         ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
286 {
287     BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
288
289     TRACE("(%p)->(%u %u %u %s)\n", This, ulProgress, ulProgressMax, ulStatusCode,
290             debugstr_w(szStatusText));
291
292     return S_OK;
293 }
294
295 static HRESULT WINAPI BindStatusCallback_OnStopBinding(IBindStatusCallback *iface,
296         HRESULT hr, LPCWSTR error)
297 {
298     BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
299
300     TRACE("(%p)->(0x%08x %s)\n", This, hr, debugstr_w(error));
301
302     if (This->binding)
303     {
304         IBinding_Release(This->binding);
305         This->binding = NULL;
306     }
307
308     if (hr == S_OK)
309         httprequest_setreadystate(This->request, READYSTATE_COMPLETE);
310
311     return S_OK;
312 }
313
314 static HRESULT WINAPI BindStatusCallback_GetBindInfo(IBindStatusCallback *iface,
315         DWORD *bind_flags, BINDINFO *pbindinfo)
316 {
317     BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
318
319     TRACE("(%p)->(%p %p)\n", This, bind_flags, pbindinfo);
320
321     *bind_flags = 0;
322     if (This->request->async) *bind_flags |= BINDF_ASYNCHRONOUS;
323
324     if (This->request->verb != BINDVERB_GET && This->body)
325     {
326         pbindinfo->stgmedData.tymed = TYMED_HGLOBAL;
327         pbindinfo->stgmedData.u.hGlobal = This->body;
328         pbindinfo->cbstgmedData = GlobalSize(This->body);
329         /* callback owns passed body pointer */
330         IBindStatusCallback_QueryInterface(iface, &IID_IUnknown, (void**)&pbindinfo->stgmedData.pUnkForRelease);
331     }
332
333     pbindinfo->dwBindVerb = This->request->verb;
334     if (This->request->verb == BINDVERB_CUSTOM)
335     {
336         pbindinfo->szCustomVerb = CoTaskMemAlloc(SysStringByteLen(This->request->custom));
337         strcpyW(pbindinfo->szCustomVerb, This->request->custom);
338     }
339
340     return S_OK;
341 }
342
343 static HRESULT WINAPI BindStatusCallback_OnDataAvailable(IBindStatusCallback *iface,
344         DWORD flags, DWORD size, FORMATETC *format, STGMEDIUM *stgmed)
345 {
346     BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
347     DWORD read, written;
348     BYTE buf[4096];
349     HRESULT hr;
350
351     TRACE("(%p)->(%08x %d %p %p)\n", This, flags, size, format, stgmed);
352
353     do
354     {
355         hr = IStream_Read(stgmed->u.pstm, buf, sizeof(buf), &read);
356         if (hr != S_OK) break;
357
358         hr = IStream_Write(This->stream, buf, read, &written);
359     } while((hr == S_OK) && written != 0 && read != 0);
360
361     httprequest_setreadystate(This->request, READYSTATE_INTERACTIVE);
362
363     return S_OK;
364 }
365
366 static HRESULT WINAPI BindStatusCallback_OnObjectAvailable(IBindStatusCallback *iface,
367         REFIID riid, IUnknown *punk)
368 {
369     BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
370
371     FIXME("(%p)->(%s %p): stub\n", This, debugstr_guid(riid), punk);
372
373     return E_NOTIMPL;
374 }
375
376 static const IBindStatusCallbackVtbl BindStatusCallbackVtbl = {
377     BindStatusCallback_QueryInterface,
378     BindStatusCallback_AddRef,
379     BindStatusCallback_Release,
380     BindStatusCallback_OnStartBinding,
381     BindStatusCallback_GetPriority,
382     BindStatusCallback_OnLowResource,
383     BindStatusCallback_OnProgress,
384     BindStatusCallback_OnStopBinding,
385     BindStatusCallback_GetBindInfo,
386     BindStatusCallback_OnDataAvailable,
387     BindStatusCallback_OnObjectAvailable
388 };
389
390 static HRESULT WINAPI BSCHttpNegotiate_QueryInterface(IHttpNegotiate *iface,
391         REFIID riid, void **ppv)
392 {
393     BindStatusCallback *This = impl_from_IHttpNegotiate(iface);
394     return IBindStatusCallback_QueryInterface(&This->IBindStatusCallback_iface, riid, ppv);
395 }
396
397 static ULONG WINAPI BSCHttpNegotiate_AddRef(IHttpNegotiate *iface)
398 {
399     BindStatusCallback *This = impl_from_IHttpNegotiate(iface);
400     return IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
401 }
402
403 static ULONG WINAPI BSCHttpNegotiate_Release(IHttpNegotiate *iface)
404 {
405     BindStatusCallback *This = impl_from_IHttpNegotiate(iface);
406     return IBindStatusCallback_Release(&This->IBindStatusCallback_iface);
407 }
408
409 static HRESULT WINAPI BSCHttpNegotiate_BeginningTransaction(IHttpNegotiate *iface,
410         LPCWSTR url, LPCWSTR headers, DWORD reserved, LPWSTR *add_headers)
411 {
412     static const WCHAR content_type_utf8W[] = {'C','o','n','t','e','n','t','-','T','y','p','e',':',' ',
413         't','e','x','t','/','p','l','a','i','n',';','c','h','a','r','s','e','t','=','u','t','f','-','8','\r','\n',0};
414
415     BindStatusCallback *This = impl_from_IHttpNegotiate(iface);
416     const struct httpheader *entry;
417     WCHAR *buff, *ptr;
418     int size = 0;
419
420     TRACE("(%p)->(%s %s %d %p)\n", This, debugstr_w(url), debugstr_w(headers), reserved, add_headers);
421
422     *add_headers = NULL;
423
424     if (This->request->use_utf8_content)
425         size = sizeof(content_type_utf8W);
426
427     if (!list_empty(&This->request->reqheaders))
428         size += This->request->reqheader_size*sizeof(WCHAR);
429
430     if (!size) return S_OK;
431
432     buff = CoTaskMemAlloc(size);
433     if (!buff) return E_OUTOFMEMORY;
434
435     ptr = buff;
436     if (This->request->use_utf8_content)
437     {
438         lstrcpyW(ptr, content_type_utf8W);
439         ptr += sizeof(content_type_utf8W)/sizeof(WCHAR)-1;
440     }
441
442     /* user headers */
443     LIST_FOR_EACH_ENTRY(entry, &This->request->reqheaders, struct httpheader, entry)
444     {
445         lstrcpyW(ptr, entry->header);
446         ptr += SysStringLen(entry->header);
447
448         lstrcpyW(ptr, colspaceW);
449         ptr += sizeof(colspaceW)/sizeof(WCHAR)-1;
450
451         lstrcpyW(ptr, entry->value);
452         ptr += SysStringLen(entry->value);
453
454         lstrcpyW(ptr, crlfW);
455         ptr += sizeof(crlfW)/sizeof(WCHAR)-1;
456     }
457
458     *add_headers = buff;
459
460     return S_OK;
461 }
462
463 static void add_response_header(httprequest *This, const WCHAR *data, int len)
464 {
465     struct httpheader *entry;
466     const WCHAR *ptr = data;
467     BSTR header, value;
468
469     while (*ptr)
470     {
471         if (*ptr == ':')
472         {
473             header = SysAllocStringLen(data, ptr-data);
474             /* skip leading spaces for a value */
475             while (*++ptr == ' ')
476                 ;
477             value = SysAllocStringLen(ptr, len-(ptr-data));
478             break;
479         }
480         ptr++;
481     }
482
483     if (!*ptr) return;
484
485     /* new header */
486     TRACE("got header %s:%s\n", debugstr_w(header), debugstr_w(value));
487
488     entry = heap_alloc(sizeof(*header));
489     entry->header = header;
490     entry->value  = value;
491     list_add_head(&This->respheaders, &entry->entry);
492 }
493
494 static HRESULT WINAPI BSCHttpNegotiate_OnResponse(IHttpNegotiate *iface, DWORD code,
495         LPCWSTR resp_headers, LPCWSTR req_headers, LPWSTR *add_reqheaders)
496 {
497     BindStatusCallback *This = impl_from_IHttpNegotiate(iface);
498
499     TRACE("(%p)->(%d %s %s %p)\n", This, code, debugstr_w(resp_headers),
500           debugstr_w(req_headers), add_reqheaders);
501
502     This->request->status = code;
503     /* store headers */
504     free_response_headers(This->request);
505     if (resp_headers)
506     {
507         const WCHAR *ptr, *line;
508
509         ptr = line = resp_headers;
510
511         /* skip status line */
512         while (*ptr)
513         {
514             if (*ptr == '\r' && *(ptr+1) == '\n')
515             {
516                 line = ++ptr+1;
517                 break;
518             }
519             ptr++;
520         }
521
522         /* store as unparsed string for now */
523         This->request->raw_respheaders = SysAllocString(line);
524     }
525
526     return S_OK;
527 }
528
529 static const IHttpNegotiateVtbl BSCHttpNegotiateVtbl = {
530     BSCHttpNegotiate_QueryInterface,
531     BSCHttpNegotiate_AddRef,
532     BSCHttpNegotiate_Release,
533     BSCHttpNegotiate_BeginningTransaction,
534     BSCHttpNegotiate_OnResponse
535 };
536
537 static HRESULT BindStatusCallback_create(httprequest* This, BindStatusCallback **obj, const VARIANT *body)
538 {
539     BindStatusCallback *bsc;
540     IBindCtx *pbc;
541     HRESULT hr;
542     int size;
543
544     hr = CreateBindCtx(0, &pbc);
545     if (hr != S_OK) return hr;
546
547     bsc = heap_alloc(sizeof(*bsc));
548     if (!bsc)
549     {
550         IBindCtx_Release(pbc);
551         return E_OUTOFMEMORY;
552     }
553
554     bsc->IBindStatusCallback_iface.lpVtbl = &BindStatusCallbackVtbl;
555     bsc->IHttpNegotiate_iface.lpVtbl = &BSCHttpNegotiateVtbl;
556     bsc->ref = 1;
557     bsc->request = This;
558     bsc->binding = NULL;
559     bsc->stream = NULL;
560     bsc->body = NULL;
561
562     TRACE("(%p)->(%p)\n", This, bsc);
563
564     This->use_utf8_content = FALSE;
565
566     if (This->verb != BINDVERB_GET)
567     {
568         void *send_data, *ptr;
569         SAFEARRAY *sa = NULL;
570
571         if (V_VT(body) == (VT_VARIANT|VT_BYREF))
572             body = V_VARIANTREF(body);
573
574         switch (V_VT(body))
575         {
576         case VT_BSTR:
577         {
578             int len = SysStringLen(V_BSTR(body));
579             const WCHAR *str = V_BSTR(body);
580             UINT i, cp = CP_ACP;
581
582             for (i = 0; i < len; i++)
583             {
584                 if (str[i] > 127)
585                 {
586                     cp = CP_UTF8;
587                     break;
588                 }
589             }
590
591             size = WideCharToMultiByte(cp, 0, str, len, NULL, 0, NULL, NULL);
592             if (!(ptr = heap_alloc(size)))
593             {
594                 heap_free(bsc);
595                 return E_OUTOFMEMORY;
596             }
597             WideCharToMultiByte(cp, 0, str, len, ptr, size, NULL, NULL);
598             if (cp == CP_UTF8) This->use_utf8_content = TRUE;
599             break;
600         }
601         case VT_ARRAY|VT_UI1:
602         {
603             sa = V_ARRAY(body);
604             if ((hr = SafeArrayAccessData(sa, (void **)&ptr)) != S_OK) return hr;
605             if ((hr = SafeArrayGetUBound(sa, 1, &size) != S_OK))
606             {
607                 SafeArrayUnaccessData(sa);
608                 return hr;
609             }
610             size++;
611             break;
612         }
613         case VT_EMPTY:
614             ptr = NULL;
615             size = 0;
616             break;
617         default:
618             FIXME("unsupported body data type %d\n", V_VT(body));
619             break;
620         }
621
622         bsc->body = GlobalAlloc(GMEM_FIXED, size);
623         if (!bsc->body)
624         {
625             if (V_VT(body) == VT_BSTR)
626                 heap_free(ptr);
627             else if (V_VT(body) == (VT_ARRAY|VT_UI1))
628                 SafeArrayUnaccessData(sa);
629
630             heap_free(bsc);
631             return E_OUTOFMEMORY;
632         }
633
634         send_data = GlobalLock(bsc->body);
635         memcpy(send_data, ptr, size);
636         GlobalUnlock(bsc->body);
637
638         if (V_VT(body) == VT_BSTR)
639             heap_free(ptr);
640         else if (V_VT(body) == (VT_ARRAY|VT_UI1))
641             SafeArrayUnaccessData(sa);
642     }
643
644     hr = RegisterBindStatusCallback(pbc, &bsc->IBindStatusCallback_iface, NULL, 0);
645     if (hr == S_OK)
646     {
647         IMoniker *moniker;
648
649         hr = CreateURLMoniker(NULL, This->url, &moniker);
650         if (hr == S_OK)
651         {
652             IStream *stream;
653
654             hr = IMoniker_BindToStorage(moniker, pbc, NULL, &IID_IStream, (void**)&stream);
655             IMoniker_Release(moniker);
656             if (stream) IStream_Release(stream);
657         }
658         IBindCtx_Release(pbc);
659     }
660
661     if (FAILED(hr))
662     {
663         IBindStatusCallback_Release(&bsc->IBindStatusCallback_iface);
664         bsc = NULL;
665     }
666
667     *obj = bsc;
668     return hr;
669 }
670
671 static HRESULT WINAPI httprequest_QueryInterface(IXMLHTTPRequest *iface, REFIID riid, void **ppvObject)
672 {
673     httprequest *This = impl_from_IXMLHTTPRequest( iface );
674     TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppvObject);
675
676     if ( IsEqualGUID( riid, &IID_IXMLHTTPRequest) ||
677          IsEqualGUID( riid, &IID_IDispatch) ||
678          IsEqualGUID( riid, &IID_IUnknown) )
679     {
680         *ppvObject = iface;
681     }
682     else if (IsEqualGUID(&IID_IObjectWithSite, riid))
683     {
684         *ppvObject = &This->IObjectWithSite_iface;
685     }
686     else if (IsEqualGUID(&IID_IObjectSafety, riid))
687     {
688         *ppvObject = &This->IObjectSafety_iface;
689     }
690     else
691     {
692         TRACE("Unsupported interface %s\n", debugstr_guid(riid));
693         *ppvObject = NULL;
694         return E_NOINTERFACE;
695     }
696
697     IXMLHTTPRequest_AddRef( iface );
698
699     return S_OK;
700 }
701
702 static ULONG WINAPI httprequest_AddRef(IXMLHTTPRequest *iface)
703 {
704     httprequest *This = impl_from_IXMLHTTPRequest( iface );
705     ULONG ref = InterlockedIncrement( &This->ref );
706     TRACE("(%p)->(%u)\n", This, ref );
707     return ref;
708 }
709
710 static ULONG WINAPI httprequest_Release(IXMLHTTPRequest *iface)
711 {
712     httprequest *This = impl_from_IXMLHTTPRequest( iface );
713     ULONG ref = InterlockedDecrement( &This->ref );
714
715     TRACE("(%p)->(%u)\n", This, ref );
716
717     if ( ref == 0 )
718     {
719         struct httpheader *header, *header2;
720
721         if (This->site)
722             IUnknown_Release( This->site );
723
724         SysFreeString(This->custom);
725         SysFreeString(This->url);
726         SysFreeString(This->user);
727         SysFreeString(This->password);
728
729         /* request headers */
730         LIST_FOR_EACH_ENTRY_SAFE(header, header2, &This->reqheaders, struct httpheader, entry)
731         {
732             list_remove(&header->entry);
733             SysFreeString(header->header);
734             SysFreeString(header->value);
735             heap_free(header);
736         }
737         /* response headers */
738         free_response_headers(This);
739
740         /* detach callback object */
741         BindStatusCallback_Detach(This->bsc);
742
743         if (This->sink) IDispatch_Release(This->sink);
744
745         heap_free( This );
746     }
747
748     return ref;
749 }
750
751 static HRESULT WINAPI httprequest_GetTypeInfoCount(IXMLHTTPRequest *iface, UINT *pctinfo)
752 {
753     httprequest *This = impl_from_IXMLHTTPRequest( iface );
754
755     TRACE("(%p)->(%p)\n", This, pctinfo);
756
757     *pctinfo = 1;
758
759     return S_OK;
760 }
761
762 static HRESULT WINAPI httprequest_GetTypeInfo(IXMLHTTPRequest *iface, UINT iTInfo,
763         LCID lcid, ITypeInfo **ppTInfo)
764 {
765     httprequest *This = impl_from_IXMLHTTPRequest( iface );
766
767     TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
768
769     return get_typeinfo(IXMLHTTPRequest_tid, ppTInfo);
770 }
771
772 static HRESULT WINAPI httprequest_GetIDsOfNames(IXMLHTTPRequest *iface, REFIID riid,
773         LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
774 {
775     httprequest *This = impl_from_IXMLHTTPRequest( iface );
776     ITypeInfo *typeinfo;
777     HRESULT hr;
778
779     TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
780           lcid, rgDispId);
781
782     if(!rgszNames || cNames == 0 || !rgDispId)
783         return E_INVALIDARG;
784
785     hr = get_typeinfo(IXMLHTTPRequest_tid, &typeinfo);
786     if(SUCCEEDED(hr))
787     {
788         hr = ITypeInfo_GetIDsOfNames(typeinfo, rgszNames, cNames, rgDispId);
789         ITypeInfo_Release(typeinfo);
790     }
791
792     return hr;
793 }
794
795 static HRESULT WINAPI httprequest_Invoke(IXMLHTTPRequest *iface, DISPID dispIdMember, REFIID riid,
796         LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult,
797         EXCEPINFO *pExcepInfo, UINT *puArgErr)
798 {
799     httprequest *This = impl_from_IXMLHTTPRequest( iface );
800     ITypeInfo *typeinfo;
801     HRESULT hr;
802
803     TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
804           lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
805
806     hr = get_typeinfo(IXMLHTTPRequest_tid, &typeinfo);
807     if(SUCCEEDED(hr))
808     {
809         hr = ITypeInfo_Invoke(typeinfo, &This->IXMLHTTPRequest_iface, dispIdMember, wFlags,
810                 pDispParams, pVarResult, pExcepInfo, puArgErr);
811         ITypeInfo_Release(typeinfo);
812     }
813
814     return hr;
815 }
816
817 static HRESULT WINAPI httprequest_open(IXMLHTTPRequest *iface, BSTR method, BSTR url,
818         VARIANT async, VARIANT user, VARIANT password)
819 {
820     static const WCHAR MethodGetW[] = {'G','E','T',0};
821     static const WCHAR MethodPutW[] = {'P','U','T',0};
822     static const WCHAR MethodPostW[] = {'P','O','S','T',0};
823     static const WCHAR MethodDeleteW[] = {'D','E','L','E','T','E',0};
824
825     httprequest *This = impl_from_IXMLHTTPRequest( iface );
826     VARIANT str, is_async;
827     HRESULT hr;
828
829     TRACE("(%p)->(%s %s %s)\n", This, debugstr_w(method), debugstr_w(url),
830         debugstr_variant(&async));
831
832     if (!method || !url) return E_INVALIDARG;
833
834     /* free previously set data */
835     SysFreeString(This->url);
836     SysFreeString(This->user);
837     SysFreeString(This->password);
838     This->url = This->user = This->password = NULL;
839
840     if (!strcmpiW(method, MethodGetW))
841     {
842         This->verb = BINDVERB_GET;
843     }
844     else if (!strcmpiW(method, MethodPutW))
845     {
846         This->verb = BINDVERB_PUT;
847     }
848     else if (!strcmpiW(method, MethodPostW))
849     {
850         This->verb = BINDVERB_POST;
851     }
852     else if (!strcmpiW(method, MethodDeleteW))
853     {
854         This->verb = BINDVERB_CUSTOM;
855         SysReAllocString(&This->custom, method);
856     }
857     else
858     {
859         FIXME("unsupported request type %s\n", debugstr_w(method));
860         This->verb = -1;
861         return E_FAIL;
862     }
863
864     This->url = SysAllocString(url);
865
866     VariantInit(&is_async);
867     hr = VariantChangeType(&is_async, &async, 0, VT_BOOL);
868     This->async = hr == S_OK && V_BOOL(&is_async) == VARIANT_TRUE;
869
870     VariantInit(&str);
871     hr = VariantChangeType(&str, &user, 0, VT_BSTR);
872     if (hr == S_OK)
873         This->user = V_BSTR(&str);
874
875     VariantInit(&str);
876     hr = VariantChangeType(&str, &password, 0, VT_BSTR);
877     if (hr == S_OK)
878         This->password = V_BSTR(&str);
879
880     httprequest_setreadystate(This, READYSTATE_LOADING);
881
882     return S_OK;
883 }
884
885 static HRESULT WINAPI httprequest_setRequestHeader(IXMLHTTPRequest *iface, BSTR header, BSTR value)
886 {
887     httprequest *This = impl_from_IXMLHTTPRequest( iface );
888     struct httpheader *entry;
889
890     TRACE("(%p)->(%s %s)\n", This, debugstr_w(header), debugstr_w(value));
891
892     if (!header || !*header) return E_INVALIDARG;
893     if (This->state != READYSTATE_LOADING) return E_FAIL;
894     if (!value) return E_INVALIDARG;
895
896     /* replace existing header value if already added */
897     LIST_FOR_EACH_ENTRY(entry, &This->reqheaders, struct httpheader, entry)
898     {
899         if (lstrcmpW(entry->header, header) == 0)
900         {
901             LONG length = SysStringLen(entry->value);
902             HRESULT hr;
903
904             hr = SysReAllocString(&entry->value, value) ? S_OK : E_OUTOFMEMORY;
905
906             if (hr == S_OK)
907                 This->reqheader_size += (SysStringLen(entry->value) - length);
908
909             return hr;
910         }
911     }
912
913     entry = heap_alloc(sizeof(*entry));
914     if (!entry) return E_OUTOFMEMORY;
915
916     /* new header */
917     entry->header = SysAllocString(header);
918     entry->value  = SysAllocString(value);
919
920     /* header length including null terminator */
921     This->reqheader_size += SysStringLen(entry->header) + sizeof(colspaceW)/sizeof(WCHAR) +
922                             SysStringLen(entry->value)  + sizeof(crlfW)/sizeof(WCHAR) - 1;
923
924     list_add_head(&This->reqheaders, &entry->entry);
925
926     return S_OK;
927 }
928
929 static HRESULT WINAPI httprequest_getResponseHeader(IXMLHTTPRequest *iface, BSTR header, BSTR *value)
930 {
931     httprequest *This = impl_from_IXMLHTTPRequest( iface );
932     struct httpheader *entry;
933
934     TRACE("(%p)->(%s %p)\n", This, debugstr_w(header), value);
935
936     if (!header || !value) return E_INVALIDARG;
937
938     if (This->raw_respheaders && list_empty(&This->respheaders))
939     {
940         WCHAR *ptr, *line;
941
942         ptr = line = This->raw_respheaders;
943         while (*ptr)
944         {
945             if (*ptr == '\r' && *(ptr+1) == '\n')
946             {
947                 add_response_header(This, line, ptr-line);
948                 ptr++; line = ++ptr;
949                 continue;
950             }
951             ptr++;
952         }
953     }
954
955     LIST_FOR_EACH_ENTRY(entry, &This->respheaders, struct httpheader, entry)
956     {
957         if (!strcmpiW(entry->header, header))
958         {
959             *value = SysAllocString(entry->value);
960             TRACE("header value %s\n", debugstr_w(*value));
961             return S_OK;
962         }
963     }
964
965     return S_FALSE;
966 }
967
968 static HRESULT WINAPI httprequest_getAllResponseHeaders(IXMLHTTPRequest *iface, BSTR *respheaders)
969 {
970     httprequest *This = impl_from_IXMLHTTPRequest( iface );
971
972     TRACE("(%p)->(%p)\n", This, respheaders);
973
974     if (!respheaders) return E_INVALIDARG;
975
976     *respheaders = SysAllocString(This->raw_respheaders);
977
978     return S_OK;
979 }
980
981 static HRESULT WINAPI httprequest_send(IXMLHTTPRequest *iface, VARIANT body)
982 {
983     httprequest *This = impl_from_IXMLHTTPRequest( iface );
984     BindStatusCallback *bsc = NULL;
985     HRESULT hr;
986
987     TRACE("(%p)->(%s)\n", This, debugstr_variant(&body));
988
989     if (This->state != READYSTATE_LOADING) return E_FAIL;
990
991     hr = BindStatusCallback_create(This, &bsc, &body);
992     if (FAILED(hr)) return hr;
993
994     BindStatusCallback_Detach(This->bsc);
995     This->bsc = bsc;
996
997     return hr;
998 }
999
1000 static HRESULT WINAPI httprequest_abort(IXMLHTTPRequest *iface)
1001 {
1002     httprequest *This = impl_from_IXMLHTTPRequest( iface );
1003
1004     TRACE("(%p)\n", This);
1005
1006     BindStatusCallback_Detach(This->bsc);
1007     This->bsc = NULL;
1008
1009     httprequest_setreadystate(This, READYSTATE_UNINITIALIZED);
1010
1011     return S_OK;
1012 }
1013
1014 static HRESULT WINAPI httprequest_get_status(IXMLHTTPRequest *iface, LONG *status)
1015 {
1016     httprequest *This = impl_from_IXMLHTTPRequest( iface );
1017
1018     TRACE("(%p)->(%p)\n", This, status);
1019
1020     if (!status) return E_INVALIDARG;
1021     if (This->state != READYSTATE_COMPLETE) return E_FAIL;
1022
1023     *status = This->status;
1024
1025     return S_OK;
1026 }
1027
1028 static HRESULT WINAPI httprequest_get_statusText(IXMLHTTPRequest *iface, BSTR *pbstrStatus)
1029 {
1030     httprequest *This = impl_from_IXMLHTTPRequest( iface );
1031
1032     FIXME("stub %p %p\n", This, pbstrStatus);
1033
1034     return E_NOTIMPL;
1035 }
1036
1037 static HRESULT WINAPI httprequest_get_responseXML(IXMLHTTPRequest *iface, IDispatch **body)
1038 {
1039     httprequest *This = impl_from_IXMLHTTPRequest( iface );
1040     IXMLDOMDocument3 *doc;
1041     HRESULT hr;
1042     BSTR str;
1043
1044     TRACE("(%p)->(%p)\n", This, body);
1045
1046     if (!body) return E_INVALIDARG;
1047     if (This->state != READYSTATE_COMPLETE) return E_FAIL;
1048
1049     hr = DOMDocument_create(MSXML_DEFAULT, NULL, (void**)&doc);
1050     if (hr != S_OK) return hr;
1051
1052     hr = IXMLHTTPRequest_get_responseText(iface, &str);
1053     if (hr == S_OK)
1054     {
1055         VARIANT_BOOL ok;
1056
1057         hr = IXMLDOMDocument3_loadXML(doc, str, &ok);
1058         SysFreeString(str);
1059     }
1060
1061     IXMLDOMDocument3_QueryInterface(doc, &IID_IDispatch, (void**)body);
1062     IXMLDOMDocument3_Release(doc);
1063
1064     return hr;
1065 }
1066
1067 static HRESULT WINAPI httprequest_get_responseText(IXMLHTTPRequest *iface, BSTR *body)
1068 {
1069     httprequest *This = impl_from_IXMLHTTPRequest( iface );
1070     HGLOBAL hglobal;
1071     HRESULT hr;
1072
1073     TRACE("(%p)->(%p)\n", This, body);
1074
1075     if (!body) return E_INVALIDARG;
1076     if (This->state != READYSTATE_COMPLETE) return E_FAIL;
1077
1078     hr = GetHGlobalFromStream(This->bsc->stream, &hglobal);
1079     if (hr == S_OK)
1080     {
1081         xmlChar *ptr = GlobalLock(hglobal);
1082         DWORD size = GlobalSize(hglobal);
1083         xmlCharEncoding encoding = XML_CHAR_ENCODING_UTF8;
1084
1085         /* try to determine data encoding */
1086         if (size >= 4)
1087         {
1088             encoding = xmlDetectCharEncoding(ptr, 4);
1089             TRACE("detected encoding: %s\n", debugstr_a(xmlGetCharEncodingName(encoding)));
1090             if ( encoding != XML_CHAR_ENCODING_UTF8 &&
1091                  encoding != XML_CHAR_ENCODING_UTF16LE &&
1092                  encoding != XML_CHAR_ENCODING_NONE )
1093             {
1094                 FIXME("unsupported encoding: %s\n", debugstr_a(xmlGetCharEncodingName(encoding)));
1095                 GlobalUnlock(hglobal);
1096                 return E_FAIL;
1097             }
1098         }
1099
1100         /* without BOM assume UTF-8 */
1101         if (encoding == XML_CHAR_ENCODING_UTF8 ||
1102             encoding == XML_CHAR_ENCODING_NONE )
1103         {
1104             DWORD length = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)ptr, size, NULL, 0);
1105
1106             *body = SysAllocStringLen(NULL, length);
1107             if (*body)
1108                 MultiByteToWideChar( CP_UTF8, 0, (LPCSTR)ptr, size, *body, length);
1109         }
1110         else
1111             *body = SysAllocStringByteLen((LPCSTR)ptr, size);
1112
1113         if (!*body) hr = E_OUTOFMEMORY;
1114         GlobalUnlock(hglobal);
1115     }
1116
1117     return hr;
1118 }
1119
1120 static HRESULT WINAPI httprequest_get_responseBody(IXMLHTTPRequest *iface, VARIANT *body)
1121 {
1122     httprequest *This = impl_from_IXMLHTTPRequest( iface );
1123     HGLOBAL hglobal;
1124     HRESULT hr;
1125
1126     TRACE("(%p)->(%p)\n", This, body);
1127
1128     if (!body) return E_INVALIDARG;
1129     V_VT(body) = VT_EMPTY;
1130
1131     if (This->state != READYSTATE_COMPLETE) return E_PENDING;
1132
1133     hr = GetHGlobalFromStream(This->bsc->stream, &hglobal);
1134     if (hr == S_OK)
1135     {
1136         void *ptr = GlobalLock(hglobal);
1137         DWORD size = GlobalSize(hglobal);
1138
1139         SAFEARRAYBOUND bound;
1140         SAFEARRAY *array;
1141
1142         bound.lLbound = 0;
1143         bound.cElements = size;
1144         array = SafeArrayCreate(VT_UI1, 1, &bound);
1145
1146         if (array)
1147         {
1148             void *dest;
1149
1150             V_VT(body) = VT_ARRAY | VT_UI1;
1151             V_ARRAY(body) = array;
1152
1153             hr = SafeArrayAccessData(array, &dest);
1154             if (hr == S_OK)
1155             {
1156                 memcpy(dest, ptr, size);
1157                 SafeArrayUnaccessData(array);
1158             }
1159             else
1160             {
1161                 VariantClear(body);
1162             }
1163         }
1164         else
1165             hr = E_FAIL;
1166
1167         GlobalUnlock(hglobal);
1168     }
1169
1170     return hr;
1171 }
1172
1173 static HRESULT WINAPI httprequest_get_responseStream(IXMLHTTPRequest *iface, VARIANT *body)
1174 {
1175     httprequest *This = impl_from_IXMLHTTPRequest( iface );
1176     LARGE_INTEGER move;
1177     IStream *stream;
1178     HRESULT hr;
1179
1180     TRACE("(%p)->(%p)\n", This, body);
1181
1182     if (!body) return E_INVALIDARG;
1183     V_VT(body) = VT_EMPTY;
1184
1185     if (This->state != READYSTATE_COMPLETE) return E_PENDING;
1186
1187     hr = IStream_Clone(This->bsc->stream, &stream);
1188
1189     move.QuadPart = 0;
1190     IStream_Seek(stream, move, STREAM_SEEK_SET, NULL);
1191
1192     V_VT(body) = VT_UNKNOWN;
1193     V_UNKNOWN(body) = (IUnknown*)stream;
1194
1195     return hr;
1196 }
1197
1198 static HRESULT WINAPI httprequest_get_readyState(IXMLHTTPRequest *iface, LONG *state)
1199 {
1200     httprequest *This = impl_from_IXMLHTTPRequest( iface );
1201
1202     TRACE("(%p)->(%p)\n", This, state);
1203
1204     if (!state) return E_INVALIDARG;
1205
1206     *state = This->state;
1207     return S_OK;
1208 }
1209
1210 static HRESULT WINAPI httprequest_put_onreadystatechange(IXMLHTTPRequest *iface, IDispatch *sink)
1211 {
1212     httprequest *This = impl_from_IXMLHTTPRequest( iface );
1213
1214     TRACE("(%p)->(%p)\n", This, sink);
1215
1216     if (This->sink) IDispatch_Release(This->sink);
1217     if ((This->sink = sink)) IDispatch_AddRef(This->sink);
1218
1219     return S_OK;
1220 }
1221
1222 static const struct IXMLHTTPRequestVtbl XMLHTTPRequestVtbl =
1223 {
1224     httprequest_QueryInterface,
1225     httprequest_AddRef,
1226     httprequest_Release,
1227     httprequest_GetTypeInfoCount,
1228     httprequest_GetTypeInfo,
1229     httprequest_GetIDsOfNames,
1230     httprequest_Invoke,
1231     httprequest_open,
1232     httprequest_setRequestHeader,
1233     httprequest_getResponseHeader,
1234     httprequest_getAllResponseHeaders,
1235     httprequest_send,
1236     httprequest_abort,
1237     httprequest_get_status,
1238     httprequest_get_statusText,
1239     httprequest_get_responseXML,
1240     httprequest_get_responseText,
1241     httprequest_get_responseBody,
1242     httprequest_get_responseStream,
1243     httprequest_get_readyState,
1244     httprequest_put_onreadystatechange
1245 };
1246
1247 /* IObjectWithSite */
1248 static HRESULT WINAPI
1249 httprequest_ObjectWithSite_QueryInterface( IObjectWithSite* iface, REFIID riid, void** ppvObject )
1250 {
1251     httprequest *This = impl_from_IObjectWithSite(iface);
1252     return IXMLHTTPRequest_QueryInterface( (IXMLHTTPRequest *)This, riid, ppvObject );
1253 }
1254
1255 static ULONG WINAPI httprequest_ObjectWithSite_AddRef( IObjectWithSite* iface )
1256 {
1257     httprequest *This = impl_from_IObjectWithSite(iface);
1258     return IXMLHTTPRequest_AddRef((IXMLHTTPRequest *)This);
1259 }
1260
1261 static ULONG WINAPI httprequest_ObjectWithSite_Release( IObjectWithSite* iface )
1262 {
1263     httprequest *This = impl_from_IObjectWithSite(iface);
1264     return IXMLHTTPRequest_Release((IXMLHTTPRequest *)This);
1265 }
1266
1267 static HRESULT WINAPI httprequest_ObjectWithSite_GetSite( IObjectWithSite *iface, REFIID iid, void **ppvSite )
1268 {
1269     httprequest *This = impl_from_IObjectWithSite(iface);
1270
1271     TRACE("(%p)->(%s %p)\n", This, debugstr_guid( iid ), ppvSite );
1272
1273     if ( !This->site )
1274         return E_FAIL;
1275
1276     return IUnknown_QueryInterface( This->site, iid, ppvSite );
1277 }
1278
1279 static HRESULT WINAPI httprequest_ObjectWithSite_SetSite( IObjectWithSite *iface, IUnknown *punk )
1280 {
1281     httprequest *This = impl_from_IObjectWithSite(iface);
1282
1283     TRACE("(%p)->(%p)\n", iface, punk);
1284
1285     if (punk)
1286         IUnknown_AddRef( punk );
1287
1288     if(This->site)
1289         IUnknown_Release( This->site );
1290
1291     This->site = punk;
1292
1293     return S_OK;
1294 }
1295
1296 static const IObjectWithSiteVtbl ObjectWithSiteVtbl =
1297 {
1298     httprequest_ObjectWithSite_QueryInterface,
1299     httprequest_ObjectWithSite_AddRef,
1300     httprequest_ObjectWithSite_Release,
1301     httprequest_ObjectWithSite_SetSite,
1302     httprequest_ObjectWithSite_GetSite
1303 };
1304
1305 /* IObjectSafety */
1306 static HRESULT WINAPI httprequest_Safety_QueryInterface(IObjectSafety *iface, REFIID riid, void **ppv)
1307 {
1308     httprequest *This = impl_from_IObjectSafety(iface);
1309     return IXMLHTTPRequest_QueryInterface( (IXMLHTTPRequest *)This, riid, ppv );
1310 }
1311
1312 static ULONG WINAPI httprequest_Safety_AddRef(IObjectSafety *iface)
1313 {
1314     httprequest *This = impl_from_IObjectSafety(iface);
1315     return IXMLHTTPRequest_AddRef((IXMLHTTPRequest *)This);
1316 }
1317
1318 static ULONG WINAPI httprequest_Safety_Release(IObjectSafety *iface)
1319 {
1320     httprequest *This = impl_from_IObjectSafety(iface);
1321     return IXMLHTTPRequest_Release((IXMLHTTPRequest *)This);
1322 }
1323
1324 #define SAFETY_SUPPORTED_OPTIONS (INTERFACESAFE_FOR_UNTRUSTED_CALLER|INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_SECURITY_MANAGER)
1325
1326 static HRESULT WINAPI httprequest_Safety_GetInterfaceSafetyOptions(IObjectSafety *iface, REFIID riid,
1327         DWORD *supported, DWORD *enabled)
1328 {
1329     httprequest *This = impl_from_IObjectSafety(iface);
1330
1331     TRACE("(%p)->(%s %p %p)\n", This, debugstr_guid(riid), supported, enabled);
1332
1333     if(!supported || !enabled) return E_POINTER;
1334
1335     *supported = SAFETY_SUPPORTED_OPTIONS;
1336     *enabled = This->safeopt;
1337
1338     return S_OK;
1339 }
1340
1341 static HRESULT WINAPI httprequest_Safety_SetInterfaceSafetyOptions(IObjectSafety *iface, REFIID riid,
1342         DWORD mask, DWORD enabled)
1343 {
1344     httprequest *This = impl_from_IObjectSafety(iface);
1345     TRACE("(%p)->(%s %x %x)\n", This, debugstr_guid(riid), mask, enabled);
1346
1347     if ((mask & ~SAFETY_SUPPORTED_OPTIONS) != 0)
1348         return E_FAIL;
1349
1350     This->safeopt = (This->safeopt & ~mask) | (mask & enabled);
1351
1352     return S_OK;
1353 }
1354
1355 #undef SAFETY_SUPPORTED_OPTIONS
1356
1357 static const IObjectSafetyVtbl ObjectSafetyVtbl = {
1358     httprequest_Safety_QueryInterface,
1359     httprequest_Safety_AddRef,
1360     httprequest_Safety_Release,
1361     httprequest_Safety_GetInterfaceSafetyOptions,
1362     httprequest_Safety_SetInterfaceSafetyOptions
1363 };
1364
1365 HRESULT XMLHTTPRequest_create(IUnknown *pUnkOuter, void **ppObj)
1366 {
1367     httprequest *req;
1368     HRESULT hr = S_OK;
1369
1370     TRACE("(%p,%p)\n", pUnkOuter, ppObj);
1371
1372     req = heap_alloc( sizeof (*req) );
1373     if( !req )
1374         return E_OUTOFMEMORY;
1375
1376     req->IXMLHTTPRequest_iface.lpVtbl = &XMLHTTPRequestVtbl;
1377     req->IObjectWithSite_iface.lpVtbl = &ObjectWithSiteVtbl;
1378     req->IObjectSafety_iface.lpVtbl = &ObjectSafetyVtbl;
1379     req->ref = 1;
1380
1381     req->async = FALSE;
1382     req->verb = -1;
1383     req->custom = NULL;
1384     req->url = req->user = req->password = NULL;
1385
1386     req->state = READYSTATE_UNINITIALIZED;
1387     req->sink = NULL;
1388
1389     req->bsc = NULL;
1390     req->status = 0;
1391     req->reqheader_size = 0;
1392     req->raw_respheaders = NULL;
1393     req->use_utf8_content = FALSE;
1394
1395     list_init(&req->reqheaders);
1396     list_init(&req->respheaders);
1397
1398     req->site = NULL;
1399     req->safeopt = 0;
1400
1401     *ppObj = &req->IXMLHTTPRequest_iface;
1402
1403     TRACE("returning iface %p\n", *ppObj);
1404
1405     return hr;
1406 }
1407
1408 #else
1409
1410 HRESULT XMLHTTPRequest_create(IUnknown *pUnkOuter, void **ppObj)
1411 {
1412     MESSAGE("This program tried to use a XMLHTTPRequest object, but\n"
1413             "libxml2 support was not present at compile time.\n");
1414     return E_NOTIMPL;
1415 }
1416
1417 #endif