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