winhttp: Implement WinHttpGetProxyForUrl.
[wine] / dlls / urlmon / protproxy.c
1 /*
2  * Copyright 2009 Jacek Caban for CodeWeavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #include "urlmon_main.h"
20 #include "wine/debug.h"
21
22 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
23
24 static inline ProtocolProxy *impl_from_IInternetProtocol(IInternetProtocol *iface)
25 {
26     return CONTAINING_RECORD(iface, ProtocolProxy, IInternetProtocol_iface);
27 }
28
29 static HRESULT WINAPI ProtocolProxy_QueryInterface(IInternetProtocol *iface, REFIID riid, void **ppv)
30 {
31     ProtocolProxy *This = impl_from_IInternetProtocol(iface);
32
33     *ppv = NULL;
34     if(IsEqualGUID(&IID_IUnknown, riid)) {
35         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
36         *ppv = &This->IInternetProtocol_iface;
37     }else if(IsEqualGUID(&IID_IInternetProtocolRoot, riid)) {
38         TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", This, ppv);
39         *ppv = &This->IInternetProtocol_iface;
40     }else if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
41         TRACE("(%p)->(IID_IInternetProtocol %p)\n", This, ppv);
42         *ppv = &This->IInternetProtocol_iface;
43     }else if(IsEqualGUID(&IID_IInternetProtocolSink, riid)) {
44         TRACE("(%p)->(IID_IInternetProtocolSink %p)\n", This, ppv);
45         *ppv = &This->IInternetProtocolSink_iface;
46     }
47
48     if(*ppv) {
49         IInternetProtocol_AddRef(iface);
50         return S_OK;
51     }
52
53     WARN("not supported interface %s\n", debugstr_guid(riid));
54     return E_NOINTERFACE;
55 }
56
57 static ULONG WINAPI ProtocolProxy_AddRef(IInternetProtocol *iface)
58 {
59     ProtocolProxy *This = impl_from_IInternetProtocol(iface);
60     LONG ref = InterlockedIncrement(&This->ref);
61     TRACE("(%p) ref=%d\n", This, ref);
62     return ref;
63 }
64
65 static ULONG WINAPI ProtocolProxy_Release(IInternetProtocol *iface)
66 {
67     ProtocolProxy *This = impl_from_IInternetProtocol(iface);
68     LONG ref = InterlockedDecrement(&This->ref);
69
70     TRACE("(%p) ref=%d\n", This, ref);
71
72     if(!ref) {
73         if(This->protocol_sink)
74             IInternetProtocolSink_Release(This->protocol_sink);
75         if(This->protocol)
76             IInternetProtocol_Release(This->protocol);
77
78         heap_free(This);
79
80         URLMON_UnlockModule();
81     }
82
83     return ref;
84 }
85
86 static HRESULT WINAPI ProtocolProxy_Start(IInternetProtocol *iface, LPCWSTR szUrl,
87         IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
88         DWORD grfPI, HANDLE_PTR dwReserved)
89 {
90     ProtocolProxy *This = impl_from_IInternetProtocol(iface);
91
92     TRACE("(%p)->(%s %p %p %08x %lx)\n", This, debugstr_w(szUrl), pOIProtSink,
93           pOIBindInfo, grfPI, dwReserved);
94
95     return IInternetProtocol_Start(This->protocol, szUrl, pOIProtSink, pOIBindInfo, grfPI, dwReserved);
96 }
97
98 static HRESULT WINAPI ProtocolProxy_Continue(IInternetProtocol *iface, PROTOCOLDATA *pProtocolData)
99 {
100     ProtocolProxy *This = impl_from_IInternetProtocol(iface);
101
102     TRACE("(%p)->(%p)\n", This, pProtocolData);
103
104     return IInternetProtocol_Continue(This->protocol, pProtocolData);
105 }
106
107 static HRESULT WINAPI ProtocolProxy_Abort(IInternetProtocol *iface, HRESULT hrReason,
108         DWORD dwOptions)
109 {
110     ProtocolProxy *This = impl_from_IInternetProtocol(iface);
111     FIXME("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);
112     return E_NOTIMPL;
113 }
114
115 static HRESULT WINAPI ProtocolProxy_Terminate(IInternetProtocol *iface, DWORD dwOptions)
116 {
117     ProtocolProxy *This = impl_from_IInternetProtocol(iface);
118
119     TRACE("(%p)->(%08x)\n", This, dwOptions);
120
121     return IInternetProtocol_Terminate(This->protocol, dwOptions);
122 }
123
124 static HRESULT WINAPI ProtocolProxy_Suspend(IInternetProtocol *iface)
125 {
126     ProtocolProxy *This = impl_from_IInternetProtocol(iface);
127     FIXME("(%p)\n", This);
128     return E_NOTIMPL;
129 }
130
131 static HRESULT WINAPI ProtocolProxy_Resume(IInternetProtocol *iface)
132 {
133     ProtocolProxy *This = impl_from_IInternetProtocol(iface);
134     FIXME("(%p)\n", This);
135     return E_NOTIMPL;
136 }
137
138 static HRESULT WINAPI ProtocolProxy_Read(IInternetProtocol *iface, void *pv,
139         ULONG cb, ULONG *pcbRead)
140 {
141     ProtocolProxy *This = impl_from_IInternetProtocol(iface);
142
143     TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
144
145     return IInternetProtocol_Read(This->protocol, pv, cb, pcbRead);
146 }
147
148 static HRESULT WINAPI ProtocolProxy_Seek(IInternetProtocol *iface, LARGE_INTEGER dlibMove,
149         DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
150 {
151     ProtocolProxy *This = impl_from_IInternetProtocol(iface);
152     FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
153     return E_NOTIMPL;
154 }
155
156 static HRESULT WINAPI ProtocolProxy_LockRequest(IInternetProtocol *iface, DWORD dwOptions)
157 {
158     ProtocolProxy *This = impl_from_IInternetProtocol(iface);
159
160     TRACE("(%p)->(%08x)\n", This, dwOptions);
161
162     return IInternetProtocol_LockRequest(This->protocol, dwOptions);
163 }
164
165 static HRESULT WINAPI ProtocolProxy_UnlockRequest(IInternetProtocol *iface)
166 {
167     ProtocolProxy *This = impl_from_IInternetProtocol(iface);
168
169     TRACE("(%p)\n", This);
170
171     return IInternetProtocol_UnlockRequest(This->protocol);
172 }
173
174 static const IInternetProtocolVtbl ProtocolProxyVtbl = {
175     ProtocolProxy_QueryInterface,
176     ProtocolProxy_AddRef,
177     ProtocolProxy_Release,
178     ProtocolProxy_Start,
179     ProtocolProxy_Continue,
180     ProtocolProxy_Abort,
181     ProtocolProxy_Terminate,
182     ProtocolProxy_Suspend,
183     ProtocolProxy_Resume,
184     ProtocolProxy_Read,
185     ProtocolProxy_Seek,
186     ProtocolProxy_LockRequest,
187     ProtocolProxy_UnlockRequest
188 };
189
190 static inline ProtocolProxy *impl_from_IInternetProtocolSink(IInternetProtocolSink *iface)
191 {
192     return CONTAINING_RECORD(iface, ProtocolProxy, IInternetProtocolSink_iface);
193 }
194
195 static HRESULT WINAPI ProtocolProxySink_QueryInterface(IInternetProtocolSink *iface,
196         REFIID riid, void **ppv)
197 {
198     ProtocolProxy *This = impl_from_IInternetProtocolSink(iface);
199     return IInternetProtocol_QueryInterface(&This->IInternetProtocol_iface, riid, ppv);
200 }
201
202 static ULONG WINAPI ProtocolProxySink_AddRef(IInternetProtocolSink *iface)
203 {
204     ProtocolProxy *This = impl_from_IInternetProtocolSink(iface);
205     return IInternetProtocol_AddRef(&This->IInternetProtocol_iface);
206 }
207
208 static ULONG WINAPI ProtocolProxySink_Release(IInternetProtocolSink *iface)
209 {
210     ProtocolProxy *This = impl_from_IInternetProtocolSink(iface);
211     return IInternetProtocol_Release(&This->IInternetProtocol_iface);
212 }
213
214 static HRESULT WINAPI ProtocolProxySink_Switch(IInternetProtocolSink *iface,
215         PROTOCOLDATA *pProtocolData)
216 {
217     ProtocolProxy *This = impl_from_IInternetProtocolSink(iface);
218
219     TRACE("(%p)->(%p)\n", This, pProtocolData);
220
221     return IInternetProtocolSink_Switch(This->protocol_sink, pProtocolData);
222 }
223
224 static HRESULT WINAPI ProtocolProxySink_ReportProgress(IInternetProtocolSink *iface,
225         ULONG ulStatusCode, LPCWSTR szStatusText)
226 {
227     ProtocolProxy *This = impl_from_IInternetProtocolSink(iface);
228
229     TRACE("(%p)->(%u %s)\n", This, ulStatusCode, debugstr_w(szStatusText));
230
231     switch(ulStatusCode) {
232     case BINDSTATUS_VERIFIEDMIMETYPEAVAILABLE:
233         IInternetProtocolSink_ReportProgress(This->protocol_sink, BINDSTATUS_MIMETYPEAVAILABLE, szStatusText);
234         break;
235     default:
236         IInternetProtocolSink_ReportProgress(This->protocol_sink, ulStatusCode, szStatusText);
237     }
238
239     return S_OK;
240 }
241
242 static HRESULT WINAPI ProtocolProxySink_ReportData(IInternetProtocolSink *iface,
243         DWORD grfBSCF, ULONG ulProgress, ULONG ulProgressMax)
244 {
245     ProtocolProxy *This = impl_from_IInternetProtocolSink(iface);
246
247     TRACE("(%p)->(%d %u %u)\n", This, grfBSCF, ulProgress, ulProgressMax);
248
249     return IInternetProtocolSink_ReportData(This->protocol_sink, grfBSCF, ulProgress, ulProgressMax);
250 }
251
252 static HRESULT WINAPI ProtocolProxySink_ReportResult(IInternetProtocolSink *iface,
253         HRESULT hrResult, DWORD dwError, LPCWSTR szResult)
254 {
255     ProtocolProxy *This = impl_from_IInternetProtocolSink(iface);
256
257     TRACE("(%p)->(%08x %d %s)\n", This, hrResult, dwError, debugstr_w(szResult));
258
259     return IInternetProtocolSink_ReportResult(This->protocol_sink, hrResult, dwError, szResult);
260 }
261
262 static const IInternetProtocolSinkVtbl InternetProtocolSinkVtbl = {
263     ProtocolProxySink_QueryInterface,
264     ProtocolProxySink_AddRef,
265     ProtocolProxySink_Release,
266     ProtocolProxySink_Switch,
267     ProtocolProxySink_ReportProgress,
268     ProtocolProxySink_ReportData,
269     ProtocolProxySink_ReportResult
270 };
271
272 HRESULT create_protocol_proxy(IInternetProtocol *protocol, IInternetProtocolSink *protocol_sink, ProtocolProxy **ret)
273 {
274     ProtocolProxy *sink;
275
276     sink = heap_alloc(sizeof(ProtocolProxy));
277     if(!sink)
278         return E_OUTOFMEMORY;
279
280     sink->IInternetProtocol_iface.lpVtbl = &ProtocolProxyVtbl;
281     sink->IInternetProtocolSink_iface.lpVtbl = &InternetProtocolSinkVtbl;
282     sink->ref = 1;
283
284     IInternetProtocol_AddRef(protocol);
285     sink->protocol = protocol;
286
287     IInternetProtocolSink_AddRef(protocol_sink);
288     sink->protocol_sink = protocol_sink;
289
290     *ret = sink;
291     return S_OK;
292 }