2 * IXMLHTTPRequest implementation
4 * Copyright 2008 Alistair Leslie-Hughes
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
31 #include "msxml_private.h"
33 #include "wine/debug.h"
34 #include "wine/list.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(msxml);
40 static const WCHAR MethodGetW[] = {'G','E','T',0};
41 static const WCHAR MethodPutW[] = {'P','U','T',0};
42 static const WCHAR MethodPostW[] = {'P','O','S','T',0};
44 typedef struct BindStatusCallback BindStatusCallback;
55 const struct IXMLHTTPRequestVtbl *lpVtbl;
64 struct list reqheaders;
71 BindStatusCallback *bsc;
74 static inline httprequest *impl_from_IXMLHTTPRequest( IXMLHTTPRequest *iface )
76 return (httprequest *)((char*)iface - FIELD_OFFSET(httprequest, lpVtbl));
79 struct BindStatusCallback
81 const IBindStatusCallbackVtbl *lpBindStatusCallbackVtbl;
82 const IHttpNegotiateVtbl *lpHttpNegotiateVtbl;
86 const httprequest *request;
89 static inline BindStatusCallback *impl_from_IBindStatusCallback( IBindStatusCallback *iface )
91 return (BindStatusCallback *)((char*)iface - FIELD_OFFSET(BindStatusCallback, lpBindStatusCallbackVtbl));
94 static inline BindStatusCallback *impl_from_IHttpNegotiate( IHttpNegotiate *iface )
96 return (BindStatusCallback *)((char*)iface - FIELD_OFFSET(BindStatusCallback, lpHttpNegotiateVtbl));
99 void BindStatusCallback_Detach(BindStatusCallback *bsc)
103 if (bsc->binding) IBinding_Abort(bsc->binding);
105 IBindStatusCallback_Release((IBindStatusCallback*)bsc);
109 static HRESULT WINAPI BindStatusCallback_QueryInterface(IBindStatusCallback *iface,
110 REFIID riid, void **ppv)
112 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
116 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
118 if (IsEqualGUID(&IID_IUnknown, riid) ||
119 IsEqualGUID(&IID_IBindStatusCallback, riid))
121 *ppv = &This->lpBindStatusCallbackVtbl;
123 else if (IsEqualGUID(&IID_IServiceProvider, riid) ||
124 IsEqualGUID(&IID_IBindStatusCallbackEx, riid))
126 return E_NOINTERFACE;
131 IBindStatusCallback_AddRef(iface);
135 FIXME("Unsupported riid = %s\n", debugstr_guid(riid));
137 return E_NOINTERFACE;
140 static ULONG WINAPI BindStatusCallback_AddRef(IBindStatusCallback *iface)
142 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
143 LONG ref = InterlockedIncrement(&This->ref);
145 TRACE("(%p) ref = %d\n", This, ref);
150 static ULONG WINAPI BindStatusCallback_Release(IBindStatusCallback *iface)
152 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
153 LONG ref = InterlockedDecrement(&This->ref);
155 TRACE("(%p) ref = %d\n", This, ref);
159 if (This->binding) IBinding_Release(This->binding);
166 static HRESULT WINAPI BindStatusCallback_OnStartBinding(IBindStatusCallback *iface,
167 DWORD reserved, IBinding *pbind)
169 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
171 TRACE("(%p)->(%d %p)\n", This, reserved, pbind);
173 if (!pbind) return E_INVALIDARG;
175 This->binding = pbind;
176 IBinding_AddRef(pbind);
181 static HRESULT WINAPI BindStatusCallback_GetPriority(IBindStatusCallback *iface, LONG *pPriority)
183 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
185 TRACE("(%p)->(%p)\n", This, pPriority);
190 static HRESULT WINAPI BindStatusCallback_OnLowResource(IBindStatusCallback *iface, DWORD reserved)
192 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
194 TRACE("(%p)->(%d)\n", This, reserved);
199 static HRESULT WINAPI BindStatusCallback_OnProgress(IBindStatusCallback *iface, ULONG ulProgress,
200 ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
202 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
204 TRACE("%p)->(%u %u %u %s)\n", This, ulProgress, ulProgressMax, ulStatusCode,
205 debugstr_w(szStatusText));
210 static HRESULT WINAPI BindStatusCallback_OnStopBinding(IBindStatusCallback *iface,
211 HRESULT hr, LPCWSTR error)
213 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
215 TRACE("(%p)->(0x%08x %s)\n", This, hr, debugstr_w(error));
220 static HRESULT WINAPI BindStatusCallback_GetBindInfo(IBindStatusCallback *iface,
221 DWORD *bind_flags, BINDINFO *pbindinfo)
223 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
225 TRACE("(%p)->(%p %p)\n", This, bind_flags, pbindinfo);
228 if (This->request->async) *bind_flags |= BINDF_ASYNCHRONOUS;
230 if (This->request->verb != BINDVERB_GET)
232 FIXME("only GET verb supported. Got %d\n", This->request->verb);
236 pbindinfo->dwBindVerb = This->request->verb;
241 static HRESULT WINAPI BindStatusCallback_OnDataAvailable(IBindStatusCallback *iface,
242 DWORD grfBSCF, DWORD dwSize, FORMATETC *pformatetc, STGMEDIUM *pstgmed)
244 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
246 FIXME("(%p)->(%08x %d %p %p): stub\n", This, grfBSCF, dwSize, pformatetc, pstgmed);
251 static HRESULT WINAPI BindStatusCallback_OnObjectAvailable(IBindStatusCallback *iface,
252 REFIID riid, IUnknown *punk)
254 BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
256 FIXME("(%p)->(%s %p): stub\n", This, debugstr_guid(riid), punk);
261 #undef STATUSCLB_THIS
263 static const IBindStatusCallbackVtbl BindStatusCallbackVtbl = {
264 BindStatusCallback_QueryInterface,
265 BindStatusCallback_AddRef,
266 BindStatusCallback_Release,
267 BindStatusCallback_OnStartBinding,
268 BindStatusCallback_GetPriority,
269 BindStatusCallback_OnLowResource,
270 BindStatusCallback_OnProgress,
271 BindStatusCallback_OnStopBinding,
272 BindStatusCallback_GetBindInfo,
273 BindStatusCallback_OnDataAvailable,
274 BindStatusCallback_OnObjectAvailable
277 static HRESULT WINAPI BSCHttpNegotiate_QueryInterface(IHttpNegotiate *iface,
278 REFIID riid, void **ppv)
280 BindStatusCallback *This = impl_from_IHttpNegotiate(iface);
281 return IBindStatusCallback_QueryInterface((IBindStatusCallback*)This, riid, ppv);
284 static ULONG WINAPI BSCHttpNegotiate_AddRef(IHttpNegotiate *iface)
286 BindStatusCallback *This = impl_from_IHttpNegotiate(iface);
287 return IBindStatusCallback_AddRef((IBindStatusCallback*)This);
290 static ULONG WINAPI BSCHttpNegotiate_Release(IHttpNegotiate *iface)
292 BindStatusCallback *This = impl_from_IHttpNegotiate(iface);
293 return IBindStatusCallback_Release((IBindStatusCallback*)This);
296 static HRESULT WINAPI BSCHttpNegotiate_BeginningTransaction(IHttpNegotiate *iface,
297 LPCWSTR url, LPCWSTR headers, DWORD reserved, LPWSTR *add_headers)
299 BindStatusCallback *This = impl_from_IHttpNegotiate(iface);
301 FIXME("(%p)->(%s %s %d %p): stub\n", This, debugstr_w(url), debugstr_w(headers), reserved, add_headers);
308 static HRESULT WINAPI BSCHttpNegotiate_OnResponse(IHttpNegotiate *iface, DWORD code,
309 LPCWSTR resp_headers, LPCWSTR req_headers, LPWSTR *add_reqheaders)
311 BindStatusCallback *This = impl_from_IHttpNegotiate(iface);
313 TRACE("(%p)->(%d %s %s %p)\n", This, code, debugstr_w(resp_headers),
314 debugstr_w(req_headers), add_reqheaders);
321 static const IHttpNegotiateVtbl BSCHttpNegotiateVtbl = {
322 BSCHttpNegotiate_QueryInterface,
323 BSCHttpNegotiate_AddRef,
324 BSCHttpNegotiate_Release,
325 BSCHttpNegotiate_BeginningTransaction,
326 BSCHttpNegotiate_OnResponse
329 static HRESULT BindStatusCallback_create(const httprequest* This, BindStatusCallback **obj)
331 BindStatusCallback *bsc;
335 hr = CreateBindCtx(0, &pbc);
336 if (hr != S_OK) return hr;
338 bsc = heap_alloc(sizeof(*bsc));
341 IBindCtx_Release(pbc);
342 return E_OUTOFMEMORY;
345 bsc->lpBindStatusCallbackVtbl = &BindStatusCallbackVtbl;
346 bsc->lpHttpNegotiateVtbl = &BSCHttpNegotiateVtbl;
351 hr = RegisterBindStatusCallback(pbc, (IBindStatusCallback*)bsc, NULL, 0);
356 hr = CreateURLMoniker(NULL, This->url, &moniker);
361 hr = IMoniker_BindToStorage(moniker, pbc, NULL, &IID_IStream, (void**)&stream);
362 IMoniker_Release(moniker);
363 if (stream) IStream_Release(stream);
365 IBindCtx_Release(pbc);
370 IBindStatusCallback_Release((IBindStatusCallback*)bsc);
378 /* TODO: process OnChange callback */
379 static void httprequest_setreadystate(httprequest *This, READYSTATE state)
384 static HRESULT WINAPI httprequest_QueryInterface(IXMLHTTPRequest *iface, REFIID riid, void **ppvObject)
386 httprequest *This = impl_from_IXMLHTTPRequest( iface );
387 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppvObject);
389 if ( IsEqualGUID( riid, &IID_IXMLHTTPRequest) ||
390 IsEqualGUID( riid, &IID_IDispatch) ||
391 IsEqualGUID( riid, &IID_IUnknown) )
397 FIXME("Unsupported interface %s\n", debugstr_guid(riid));
398 return E_NOINTERFACE;
401 IXMLHTTPRequest_AddRef( iface );
406 static ULONG WINAPI httprequest_AddRef(IXMLHTTPRequest *iface)
408 httprequest *This = impl_from_IXMLHTTPRequest( iface );
409 ULONG ref = InterlockedIncrement( &This->ref );
410 TRACE("(%p)->(%u)\n", This, ref );
414 static ULONG WINAPI httprequest_Release(IXMLHTTPRequest *iface)
416 httprequest *This = impl_from_IXMLHTTPRequest( iface );
417 ULONG ref = InterlockedDecrement( &This->ref );
419 TRACE("(%p)->(%u)\n", This, ref );
423 struct reqheader *header, *header2;
425 SysFreeString(This->url);
426 SysFreeString(This->user);
427 SysFreeString(This->password);
429 /* request headers */
430 LIST_FOR_EACH_ENTRY_SAFE(header, header2, &This->reqheaders, struct reqheader, entry)
432 list_remove(&header->entry);
433 SysFreeString(header->header);
434 SysFreeString(header->value);
437 /* detach callback object */
438 BindStatusCallback_Detach(This->bsc);
446 static HRESULT WINAPI httprequest_GetTypeInfoCount(IXMLHTTPRequest *iface, UINT *pctinfo)
448 httprequest *This = impl_from_IXMLHTTPRequest( iface );
450 TRACE("(%p)->(%p)\n", This, pctinfo);
457 static HRESULT WINAPI httprequest_GetTypeInfo(IXMLHTTPRequest *iface, UINT iTInfo,
458 LCID lcid, ITypeInfo **ppTInfo)
460 httprequest *This = impl_from_IXMLHTTPRequest( iface );
463 TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
465 hr = get_typeinfo(IXMLHTTPRequest_tid, ppTInfo);
470 static HRESULT WINAPI httprequest_GetIDsOfNames(IXMLHTTPRequest *iface, REFIID riid,
471 LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
473 httprequest *This = impl_from_IXMLHTTPRequest( iface );
477 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
480 if(!rgszNames || cNames == 0 || !rgDispId)
483 hr = get_typeinfo(IXMLHTTPRequest_tid, &typeinfo);
486 hr = ITypeInfo_GetIDsOfNames(typeinfo, rgszNames, cNames, rgDispId);
487 ITypeInfo_Release(typeinfo);
493 static HRESULT WINAPI httprequest_Invoke(IXMLHTTPRequest *iface, DISPID dispIdMember, REFIID riid,
494 LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult,
495 EXCEPINFO *pExcepInfo, UINT *puArgErr)
497 httprequest *This = impl_from_IXMLHTTPRequest( iface );
501 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
502 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
504 hr = get_typeinfo(IXMLHTTPRequest_tid, &typeinfo);
507 hr = ITypeInfo_Invoke(typeinfo, &(This->lpVtbl), dispIdMember, wFlags, pDispParams,
508 pVarResult, pExcepInfo, puArgErr);
509 ITypeInfo_Release(typeinfo);
515 static HRESULT WINAPI httprequest_open(IXMLHTTPRequest *iface, BSTR method, BSTR url,
516 VARIANT async, VARIANT user, VARIANT password)
518 httprequest *This = impl_from_IXMLHTTPRequest( iface );
522 TRACE("(%p)->(%s %s)\n", This, debugstr_w(method), debugstr_w(url));
524 if (!method || !url) return E_INVALIDARG;
526 /* free previously set data */
527 SysFreeString(This->url);
528 SysFreeString(This->user);
529 SysFreeString(This->password);
530 This->url = This->user = This->password = NULL;
532 if (lstrcmpiW(method, MethodGetW) == 0)
534 This->verb = BINDVERB_GET;
536 else if (lstrcmpiW(method, MethodPutW) == 0)
538 This->verb = BINDVERB_PUT;
540 else if (lstrcmpiW(method, MethodPostW) == 0)
542 This->verb = BINDVERB_POST;
546 FIXME("unsupported request type %s\n", debugstr_w(method));
551 This->url = SysAllocString(url);
553 hr = VariantChangeType(&async, &async, 0, VT_BOOL);
554 This->async = hr == S_OK && V_BOOL(&async) == VARIANT_TRUE;
557 hr = VariantChangeType(&str, &user, 0, VT_BSTR);
559 This->user = V_BSTR(&str);
561 hr = VariantChangeType(&str, &password, 0, VT_BSTR);
563 This->password = V_BSTR(&str);
565 httprequest_setreadystate(This, READYSTATE_LOADING);
570 static HRESULT WINAPI httprequest_setRequestHeader(IXMLHTTPRequest *iface, BSTR header, BSTR value)
572 httprequest *This = impl_from_IXMLHTTPRequest( iface );
573 struct reqheader *entry;
575 TRACE("(%p)->(%s %s)\n", This, debugstr_w(header), debugstr_w(value));
577 if (!header || !*header) return E_INVALIDARG;
578 if (This->state != READYSTATE_LOADING) return E_FAIL;
579 if (!value) return E_INVALIDARG;
581 /* replace existing header value if already added */
582 LIST_FOR_EACH_ENTRY(entry, &This->reqheaders, struct reqheader, entry)
584 if (lstrcmpW(entry->header, header) == 0)
586 return SysReAllocString(&entry->value, value) ? S_OK : E_OUTOFMEMORY;
590 entry = heap_alloc(sizeof(*entry));
591 if (!entry) return E_OUTOFMEMORY;
594 entry->header = SysAllocString(header);
595 entry->value = SysAllocString(value);
597 list_add_head(&This->reqheaders, &entry->entry);
602 static HRESULT WINAPI httprequest_getResponseHeader(IXMLHTTPRequest *iface, BSTR bstrHeader, BSTR *pbstrValue)
604 httprequest *This = impl_from_IXMLHTTPRequest( iface );
606 FIXME("stub (%p) %s %p\n", This, debugstr_w(bstrHeader), pbstrValue);
611 static HRESULT WINAPI httprequest_getAllResponseHeaders(IXMLHTTPRequest *iface, BSTR *pbstrHeaders)
613 httprequest *This = impl_from_IXMLHTTPRequest( iface );
615 FIXME("stub (%p) %p\n", This, pbstrHeaders);
620 static HRESULT WINAPI httprequest_send(IXMLHTTPRequest *iface, VARIANT varBody)
622 httprequest *This = impl_from_IXMLHTTPRequest( iface );
623 BindStatusCallback *bsc = NULL;
626 TRACE("(%p)\n", This);
628 if (This->state != READYSTATE_LOADING) return E_FAIL;
630 hr = BindStatusCallback_create(This, &bsc);
631 if (FAILED(hr)) return hr;
633 BindStatusCallback_Detach(This->bsc);
639 static HRESULT WINAPI httprequest_abort(IXMLHTTPRequest *iface)
641 httprequest *This = impl_from_IXMLHTTPRequest( iface );
643 FIXME("stub (%p)\n", This);
648 static HRESULT WINAPI httprequest_get_status(IXMLHTTPRequest *iface, LONG *plStatus)
650 httprequest *This = impl_from_IXMLHTTPRequest( iface );
652 FIXME("stub %p %p\n", This, plStatus);
657 static HRESULT WINAPI httprequest_get_statusText(IXMLHTTPRequest *iface, BSTR *pbstrStatus)
659 httprequest *This = impl_from_IXMLHTTPRequest( iface );
661 FIXME("stub %p %p\n", This, pbstrStatus);
666 static HRESULT WINAPI httprequest_get_responseXML(IXMLHTTPRequest *iface, IDispatch **ppBody)
668 httprequest *This = impl_from_IXMLHTTPRequest( iface );
670 FIXME("stub %p %p\n", This, ppBody);
675 static HRESULT WINAPI httprequest_get_responseText(IXMLHTTPRequest *iface, BSTR *pbstrBody)
677 httprequest *This = impl_from_IXMLHTTPRequest( iface );
679 FIXME("stub %p %p\n", This, pbstrBody);
684 static HRESULT WINAPI httprequest_get_responseBody(IXMLHTTPRequest *iface, VARIANT *pvarBody)
686 httprequest *This = impl_from_IXMLHTTPRequest( iface );
688 FIXME("stub %p %p\n", This, pvarBody);
693 static HRESULT WINAPI httprequest_get_responseStream(IXMLHTTPRequest *iface, VARIANT *pvarBody)
695 httprequest *This = impl_from_IXMLHTTPRequest( iface );
697 FIXME("stub %p %p\n", This, pvarBody);
702 static HRESULT WINAPI httprequest_get_readyState(IXMLHTTPRequest *iface, LONG *state)
704 httprequest *This = impl_from_IXMLHTTPRequest( iface );
706 TRACE("(%p)->(%p)\n", This, state);
708 if (!state) return E_INVALIDARG;
710 *state = This->state;
714 static HRESULT WINAPI httprequest_put_onreadystatechange(IXMLHTTPRequest *iface, IDispatch *pReadyStateSink)
716 httprequest *This = impl_from_IXMLHTTPRequest( iface );
718 FIXME("stub %p %p\n", This, pReadyStateSink);
723 static const struct IXMLHTTPRequestVtbl dimimpl_vtbl =
725 httprequest_QueryInterface,
728 httprequest_GetTypeInfoCount,
729 httprequest_GetTypeInfo,
730 httprequest_GetIDsOfNames,
733 httprequest_setRequestHeader,
734 httprequest_getResponseHeader,
735 httprequest_getAllResponseHeaders,
738 httprequest_get_status,
739 httprequest_get_statusText,
740 httprequest_get_responseXML,
741 httprequest_get_responseText,
742 httprequest_get_responseBody,
743 httprequest_get_responseStream,
744 httprequest_get_readyState,
745 httprequest_put_onreadystatechange
748 HRESULT XMLHTTPRequest_create(IUnknown *pUnkOuter, void **ppObj)
753 TRACE("(%p,%p)\n", pUnkOuter, ppObj);
755 req = heap_alloc( sizeof (*req) );
757 return E_OUTOFMEMORY;
759 req->lpVtbl = &dimimpl_vtbl;
764 req->url = req->user = req->password = NULL;
765 req->state = READYSTATE_UNINITIALIZED;
767 list_init(&req->reqheaders);
769 *ppObj = &req->lpVtbl;
771 TRACE("returning iface %p\n", *ppObj);
778 HRESULT XMLHTTPRequest_create(IUnknown *pUnkOuter, void **ppObj)
780 MESSAGE("This program tried to use a XMLHTTPRequest object, but\n"
781 "libxml2 support was not present at compile time.\n");