msxml3: Use existing helpers for heap allocations.
[wine] / dlls / msxml3 / httprequest.c
1 /*
2  *    IXMLHTTPRequest implementation
3  *
4  * Copyright 2008 Alistair Leslie-Hughes
5  *
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.
10  *
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.
15  *
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
19  */
20 #define COBJMACROS
21
22 #include "config.h"
23
24 #include <stdarg.h>
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winuser.h"
28 #include "ole2.h"
29 #include "msxml2.h"
30
31 #include "msxml_private.h"
32
33 #include "wine/debug.h"
34
35 WINE_DEFAULT_DEBUG_CHANNEL(msxml);
36
37 #ifdef HAVE_LIBXML2
38
39 typedef struct _httprequest
40 {
41     const struct IXMLHTTPRequestVtbl *lpVtbl;
42     LONG ref;
43 } httprequest;
44
45 static inline httprequest *impl_from_IXMLHTTPRequest( IXMLHTTPRequest *iface )
46 {
47     return (httprequest *)((char*)iface - FIELD_OFFSET(httprequest, lpVtbl));
48 }
49
50 static HRESULT WINAPI httprequest_QueryInterface(IXMLHTTPRequest *iface, REFIID riid, void **ppvObject)
51 {
52     httprequest *This = impl_from_IXMLHTTPRequest( iface );
53     TRACE("%p %s %p\n", This, debugstr_guid(riid), ppvObject);
54
55     if ( IsEqualGUID( riid, &IID_IXMLHTTPRequest) ||
56          IsEqualGUID( riid, &IID_IDispatch) ||
57          IsEqualGUID( riid, &IID_IUnknown) )
58     {
59         *ppvObject = iface;
60     }
61     else
62     {
63         FIXME("Unsupported interface %s\n", debugstr_guid(riid));
64         return E_NOINTERFACE;
65     }
66
67     IXMLHTTPRequest_AddRef( iface );
68
69     return S_OK;
70 }
71
72 static ULONG WINAPI httprequest_AddRef(IXMLHTTPRequest *iface)
73 {
74     httprequest *This = impl_from_IXMLHTTPRequest( iface );
75     return InterlockedIncrement( &This->ref );
76 }
77
78 static ULONG WINAPI httprequest_Release(IXMLHTTPRequest *iface)
79 {
80     httprequest *This = impl_from_IXMLHTTPRequest( iface );
81     ULONG ref;
82
83     ref = InterlockedDecrement( &This->ref );
84     if ( ref == 0 )
85     {
86         heap_free( This );
87     }
88
89     return ref;
90 }
91
92 static HRESULT WINAPI httprequest_GetTypeInfoCount(IXMLHTTPRequest *iface, UINT *pctinfo)
93 {
94     httprequest *This = impl_from_IXMLHTTPRequest( iface );
95
96     TRACE("(%p)->(%p)\n", This, pctinfo);
97
98     *pctinfo = 1;
99
100     return S_OK;
101 }
102
103 static HRESULT WINAPI httprequest_GetTypeInfo(IXMLHTTPRequest *iface, UINT iTInfo,
104         LCID lcid, ITypeInfo **ppTInfo)
105 {
106     httprequest *This = impl_from_IXMLHTTPRequest( iface );
107     HRESULT hr;
108
109     TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
110
111     hr = get_typeinfo(IXMLHTTPRequest_tid, ppTInfo);
112
113     return hr;
114 }
115
116 static HRESULT WINAPI httprequest_GetIDsOfNames(IXMLHTTPRequest *iface, REFIID riid,
117         LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
118 {
119     httprequest *This = impl_from_IXMLHTTPRequest( iface );
120     ITypeInfo *typeinfo;
121     HRESULT hr;
122
123     TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
124           lcid, rgDispId);
125
126     if(!rgszNames || cNames == 0 || !rgDispId)
127         return E_INVALIDARG;
128
129     hr = get_typeinfo(IXMLHTTPRequest_tid, &typeinfo);
130     if(SUCCEEDED(hr))
131     {
132         hr = ITypeInfo_GetIDsOfNames(typeinfo, rgszNames, cNames, rgDispId);
133         ITypeInfo_Release(typeinfo);
134     }
135
136     return hr;
137 }
138
139 static HRESULT WINAPI httprequest_Invoke(IXMLHTTPRequest *iface, DISPID dispIdMember, REFIID riid,
140         LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult,
141         EXCEPINFO *pExcepInfo, UINT *puArgErr)
142 {
143     httprequest *This = impl_from_IXMLHTTPRequest( iface );
144     ITypeInfo *typeinfo;
145     HRESULT hr;
146
147     TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
148           lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
149
150     hr = get_typeinfo(IXMLHTTPRequest_tid, &typeinfo);
151     if(SUCCEEDED(hr))
152     {
153         hr = ITypeInfo_Invoke(typeinfo, &(This->lpVtbl), dispIdMember, wFlags, pDispParams,
154                 pVarResult, pExcepInfo, puArgErr);
155         ITypeInfo_Release(typeinfo);
156     }
157
158     return hr;
159 }
160
161 static HRESULT WINAPI httprequest_open(IXMLHTTPRequest *iface, BSTR bstrMethod, BSTR bstrUrl,
162         VARIANT varAsync, VARIANT bstrUser, VARIANT bstrPassword)
163 {
164     httprequest *This = impl_from_IXMLHTTPRequest( iface );
165
166     FIXME("stub (%p)\n", This);
167
168     return E_NOTIMPL;
169 }
170
171 static HRESULT WINAPI httprequest_setRequestHeader(IXMLHTTPRequest *iface, BSTR bstrHeader, BSTR bstrValue)
172 {
173     httprequest *This = impl_from_IXMLHTTPRequest( iface );
174
175     FIXME("stub (%p) %s %s\n", This, debugstr_w(bstrHeader), debugstr_w(bstrValue));
176
177     return E_NOTIMPL;
178 }
179
180 static HRESULT WINAPI httprequest_getResponseHeader(IXMLHTTPRequest *iface, BSTR bstrHeader, BSTR *pbstrValue)
181 {
182     httprequest *This = impl_from_IXMLHTTPRequest( iface );
183
184     FIXME("stub (%p) %s %p\n", This, debugstr_w(bstrHeader), pbstrValue);
185
186     return E_NOTIMPL;
187 }
188
189 static HRESULT WINAPI httprequest_getAllResponseHeaders(IXMLHTTPRequest *iface, BSTR *pbstrHeaders)
190 {
191     httprequest *This = impl_from_IXMLHTTPRequest( iface );
192
193     FIXME("stub (%p) %p\n", This, pbstrHeaders);
194
195     return E_NOTIMPL;
196 }
197
198 static HRESULT WINAPI httprequest_send(IXMLHTTPRequest *iface, VARIANT varBody)
199 {
200     httprequest *This = impl_from_IXMLHTTPRequest( iface );
201
202     FIXME("stub (%p)\n", This);
203
204     return E_NOTIMPL;
205 }
206
207 static HRESULT WINAPI httprequest_abort(IXMLHTTPRequest *iface)
208 {
209     httprequest *This = impl_from_IXMLHTTPRequest( iface );
210
211     FIXME("stub (%p)\n", This);
212
213     return E_NOTIMPL;
214 }
215
216 static HRESULT WINAPI httprequest_get_status(IXMLHTTPRequest *iface, LONG *plStatus)
217 {
218     httprequest *This = impl_from_IXMLHTTPRequest( iface );
219
220     FIXME("stub %p %p\n", This, plStatus);
221
222     return E_NOTIMPL;
223 }
224
225 static HRESULT WINAPI httprequest_get_statusText(IXMLHTTPRequest *iface, BSTR *pbstrStatus)
226 {
227     httprequest *This = impl_from_IXMLHTTPRequest( iface );
228
229     FIXME("stub %p %p\n", This, pbstrStatus);
230
231     return E_NOTIMPL;
232 }
233
234 static HRESULT WINAPI httprequest_get_responseXML(IXMLHTTPRequest *iface, IDispatch **ppBody)
235 {
236     httprequest *This = impl_from_IXMLHTTPRequest( iface );
237
238     FIXME("stub %p %p\n", This, ppBody);
239
240     return E_NOTIMPL;
241 }
242
243 static HRESULT WINAPI httprequest_get_responseText(IXMLHTTPRequest *iface, BSTR *pbstrBody)
244 {
245     httprequest *This = impl_from_IXMLHTTPRequest( iface );
246
247     FIXME("stub %p %p\n", This, pbstrBody);
248
249     return E_NOTIMPL;
250 }
251
252 static HRESULT WINAPI httprequest_get_responseBody(IXMLHTTPRequest *iface, VARIANT *pvarBody)
253 {
254     httprequest *This = impl_from_IXMLHTTPRequest( iface );
255
256     FIXME("stub %p %p\n", This, pvarBody);
257
258     return E_NOTIMPL;
259 }
260
261 static HRESULT WINAPI httprequest_get_responseStream(IXMLHTTPRequest *iface, VARIANT *pvarBody)
262 {
263     httprequest *This = impl_from_IXMLHTTPRequest( iface );
264
265     FIXME("stub %p %p\n", This, pvarBody);
266
267     return E_NOTIMPL;
268 }
269
270 static HRESULT WINAPI httprequest_get_readyState(IXMLHTTPRequest *iface, LONG *plState)
271 {
272     httprequest *This = impl_from_IXMLHTTPRequest( iface );
273
274     FIXME("stub %p %p\n", This, plState);
275
276     return E_NOTIMPL;
277 }
278
279 static HRESULT WINAPI httprequest_put_onreadystatechange(IXMLHTTPRequest *iface, IDispatch *pReadyStateSink)
280 {
281     httprequest *This = impl_from_IXMLHTTPRequest( iface );
282
283     FIXME("stub %p %p\n", This, pReadyStateSink);
284
285     return E_NOTIMPL;
286 }
287
288 static const struct IXMLHTTPRequestVtbl dimimpl_vtbl =
289 {
290     httprequest_QueryInterface,
291     httprequest_AddRef,
292     httprequest_Release,
293     httprequest_GetTypeInfoCount,
294     httprequest_GetTypeInfo,
295     httprequest_GetIDsOfNames,
296     httprequest_Invoke,
297     httprequest_open,
298     httprequest_setRequestHeader,
299     httprequest_getResponseHeader,
300     httprequest_getAllResponseHeaders,
301     httprequest_send,
302     httprequest_abort,
303     httprequest_get_status,
304     httprequest_get_statusText,
305     httprequest_get_responseXML,
306     httprequest_get_responseText,
307     httprequest_get_responseBody,
308     httprequest_get_responseStream,
309     httprequest_get_readyState,
310     httprequest_put_onreadystatechange
311 };
312
313 HRESULT XMLHTTPRequest_create(IUnknown *pUnkOuter, LPVOID *ppObj)
314 {
315     httprequest *req;
316     HRESULT hr = S_OK;
317
318     TRACE("(%p,%p)\n", pUnkOuter, ppObj);
319
320     req = heap_alloc( sizeof (*req) );
321     if( !req )
322         return E_OUTOFMEMORY;
323
324     req->lpVtbl = &dimimpl_vtbl;
325     req->ref = 1;
326
327     *ppObj = &req->lpVtbl;
328
329     TRACE("returning iface %p\n", *ppObj);
330
331     return hr;
332 }
333
334 #else
335
336 HRESULT XMLHTTPRequest_create(IUnknown *pUnkOuter, LPVOID *ppObj)
337 {
338     MESSAGE("This program tried to use a XMLHTTPRequest object, but\n"
339             "libxml2 support was not present at compile time.\n");
340     return E_NOTIMPL;
341 }
342
343 #endif