urlmon: Added pluggable protocol support to CoInternetCombineUrlEx.
[wine] / dlls / urlmon / ftp.c
1 /*
2  * Copyright 2005-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
21 #define NO_SHLWAPI_REG
22 #include "shlwapi.h"
23
24 #include "wine/debug.h"
25
26 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
27
28 typedef struct {
29     Protocol base;
30
31     const IInternetProtocolExVtbl*lpIInternetProtocolExVtbl;
32     const IInternetPriorityVtbl  *lpInternetPriorityVtbl;
33     const IWinInetHttpInfoVtbl   *lpWinInetHttpInfoVtbl;
34
35     LONG ref;
36 } FtpProtocol;
37
38 #define PROTOCOLEX(x)    ((IInternetProtocolEx*)&(x)->lpIInternetProtocolExVtbl)
39 #define PRIORITY(x)      ((IInternetPriority*)  &(x)->lpInternetPriorityVtbl)
40 #define INETHTTPINFO(x)  ((IWinInetHttpInfo*)   &(x)->lpWinInetHttpInfoVtbl)
41
42 #define ASYNCPROTOCOL_THIS(iface) DEFINE_THIS2(FtpProtocol, base, iface)
43
44 static HRESULT FtpProtocol_open_request(Protocol *prot, IUri *uri, DWORD request_flags,
45         HINTERNET internet_session, IInternetBindInfo *bind_info)
46 {
47     FtpProtocol *This = ASYNCPROTOCOL_THIS(prot);
48     BSTR url;
49     HRESULT hres;
50
51     hres = IUri_GetAbsoluteUri(uri, &url);
52     if(FAILED(hres))
53         return hres;
54
55     This->base.request = InternetOpenUrlW(internet_session, url, NULL, 0,
56             request_flags|INTERNET_FLAG_EXISTING_CONNECT|INTERNET_FLAG_PASSIVE,
57             (DWORD_PTR)&This->base);
58     SysFreeString(url);
59     if (!This->base.request && GetLastError() != ERROR_IO_PENDING) {
60         WARN("InternetOpenUrl failed: %d\n", GetLastError());
61         return INET_E_RESOURCE_NOT_FOUND;
62     }
63
64     return S_OK;
65 }
66
67 static HRESULT FtpProtocol_end_request(Protocol *prot)
68 {
69     return E_NOTIMPL;
70 }
71
72 static HRESULT FtpProtocol_start_downloading(Protocol *prot)
73 {
74     FtpProtocol *This = ASYNCPROTOCOL_THIS(prot);
75     DWORD size;
76     BOOL res;
77
78     res = FtpGetFileSize(This->base.request, &size);
79     if(res)
80         This->base.content_length = size;
81     else
82         WARN("FtpGetFileSize failed: %d\n", GetLastError());
83
84     return S_OK;
85 }
86
87 static void FtpProtocol_close_connection(Protocol *prot)
88 {
89 }
90
91 #undef ASYNCPROTOCOL_THIS
92
93 static const ProtocolVtbl AsyncProtocolVtbl = {
94     FtpProtocol_open_request,
95     FtpProtocol_end_request,
96     FtpProtocol_start_downloading,
97     FtpProtocol_close_connection
98 };
99
100 #define PROTOCOL_THIS(iface) DEFINE_THIS(FtpProtocol, IInternetProtocolEx, iface)
101
102 static HRESULT WINAPI FtpProtocol_QueryInterface(IInternetProtocolEx *iface, REFIID riid, void **ppv)
103 {
104     FtpProtocol *This = PROTOCOL_THIS(iface);
105
106     *ppv = NULL;
107     if(IsEqualGUID(&IID_IUnknown, riid)) {
108         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
109         *ppv = PROTOCOLEX(This);
110     }else if(IsEqualGUID(&IID_IInternetProtocolRoot, riid)) {
111         TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", This, ppv);
112         *ppv = PROTOCOLEX(This);
113     }else if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
114         TRACE("(%p)->(IID_IInternetProtocol %p)\n", This, ppv);
115         *ppv = PROTOCOLEX(This);
116     }else if(IsEqualGUID(&IID_IInternetProtocolEx, riid)) {
117         TRACE("(%p)->(IID_IInternetProtocolEx %p)\n", This, ppv);
118         *ppv = PROTOCOLEX(This);
119     }else if(IsEqualGUID(&IID_IInternetPriority, riid)) {
120         TRACE("(%p)->(IID_IInternetPriority %p)\n", This, ppv);
121         *ppv = PRIORITY(This);
122     }else if(IsEqualGUID(&IID_IWinInetInfo, riid)) {
123         TRACE("(%p)->(IID_IWinInetInfo %p)\n", This, ppv);
124         *ppv = INETHTTPINFO(This);
125     }else if(IsEqualGUID(&IID_IWinInetHttpInfo, riid)) {
126         TRACE("(%p)->(IID_IWinInetHttpInfo %p)\n", This, ppv);
127         *ppv = INETHTTPINFO(This);
128     }
129
130     if(*ppv) {
131         IInternetProtocol_AddRef(iface);
132         return S_OK;
133     }
134
135     WARN("not supported interface %s\n", debugstr_guid(riid));
136     return E_NOINTERFACE;
137 }
138
139 static ULONG WINAPI FtpProtocol_AddRef(IInternetProtocolEx *iface)
140 {
141     FtpProtocol *This = PROTOCOL_THIS(iface);
142     LONG ref = InterlockedIncrement(&This->ref);
143     TRACE("(%p) ref=%d\n", This, ref);
144     return ref;
145 }
146
147 static ULONG WINAPI FtpProtocol_Release(IInternetProtocolEx *iface)
148 {
149     FtpProtocol *This = PROTOCOL_THIS(iface);
150     LONG ref = InterlockedDecrement(&This->ref);
151
152     TRACE("(%p) ref=%d\n", This, ref);
153
154     if(!ref) {
155         protocol_close_connection(&This->base);
156         heap_free(This);
157
158         URLMON_UnlockModule();
159     }
160
161     return ref;
162 }
163
164 static HRESULT WINAPI FtpProtocol_Start(IInternetProtocolEx *iface, LPCWSTR szUrl,
165         IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
166         DWORD grfPI, HANDLE_PTR dwReserved)
167 {
168     FtpProtocol *This = PROTOCOL_THIS(iface);
169     IUri *uri;
170     HRESULT hres;
171
172     TRACE("(%p)->(%s %p %p %08x %lx)\n", This, debugstr_w(szUrl), pOIProtSink,
173           pOIBindInfo, grfPI, dwReserved);
174
175     hres = CreateUri(szUrl, 0, 0, &uri);
176     if(FAILED(hres))
177         return hres;
178
179     hres = IInternetProtocolEx_StartEx(PROTOCOLEX(This), uri, pOIProtSink, pOIBindInfo,
180             grfPI, (HANDLE*)dwReserved);
181
182     IUri_Release(uri);
183     return hres;
184 }
185
186 static HRESULT WINAPI FtpProtocol_Continue(IInternetProtocolEx *iface, PROTOCOLDATA *pProtocolData)
187 {
188     FtpProtocol *This = PROTOCOL_THIS(iface);
189
190     TRACE("(%p)->(%p)\n", This, pProtocolData);
191
192     return protocol_continue(&This->base, pProtocolData);
193 }
194
195 static HRESULT WINAPI FtpProtocol_Abort(IInternetProtocolEx *iface, HRESULT hrReason,
196         DWORD dwOptions)
197 {
198     FtpProtocol *This = PROTOCOL_THIS(iface);
199
200     TRACE("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);
201
202     return protocol_abort(&This->base, hrReason);
203 }
204
205 static HRESULT WINAPI FtpProtocol_Terminate(IInternetProtocolEx *iface, DWORD dwOptions)
206 {
207     FtpProtocol *This = PROTOCOL_THIS(iface);
208
209     TRACE("(%p)->(%08x)\n", This, dwOptions);
210
211     protocol_close_connection(&This->base);
212     return S_OK;
213 }
214
215 static HRESULT WINAPI FtpProtocol_Suspend(IInternetProtocolEx *iface)
216 {
217     FtpProtocol *This = PROTOCOL_THIS(iface);
218     FIXME("(%p)\n", This);
219     return E_NOTIMPL;
220 }
221
222 static HRESULT WINAPI FtpProtocol_Resume(IInternetProtocolEx *iface)
223 {
224     FtpProtocol *This = PROTOCOL_THIS(iface);
225     FIXME("(%p)\n", This);
226     return E_NOTIMPL;
227 }
228
229 static HRESULT WINAPI FtpProtocol_Read(IInternetProtocolEx *iface, void *pv,
230         ULONG cb, ULONG *pcbRead)
231 {
232     FtpProtocol *This = PROTOCOL_THIS(iface);
233
234     TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
235
236     return protocol_read(&This->base, pv, cb, pcbRead);
237 }
238
239 static HRESULT WINAPI FtpProtocol_Seek(IInternetProtocolEx *iface, LARGE_INTEGER dlibMove,
240         DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
241 {
242     FtpProtocol *This = PROTOCOL_THIS(iface);
243     FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
244     return E_NOTIMPL;
245 }
246
247 static HRESULT WINAPI FtpProtocol_LockRequest(IInternetProtocolEx *iface, DWORD dwOptions)
248 {
249     FtpProtocol *This = PROTOCOL_THIS(iface);
250
251     TRACE("(%p)->(%08x)\n", This, dwOptions);
252
253     return protocol_lock_request(&This->base);
254 }
255
256 static HRESULT WINAPI FtpProtocol_UnlockRequest(IInternetProtocolEx *iface)
257 {
258     FtpProtocol *This = PROTOCOL_THIS(iface);
259
260     TRACE("(%p)\n", This);
261
262     return protocol_unlock_request(&This->base);
263 }
264
265 static HRESULT WINAPI FtpProtocol_StartEx(IInternetProtocolEx *iface, IUri *pUri,
266         IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
267         DWORD grfPI, HANDLE *dwReserved)
268 {
269     FtpProtocol *This = PROTOCOL_THIS(iface);
270     DWORD scheme = 0;
271     HRESULT hres;
272
273     TRACE("(%p)->(%p %p %p %08x %p)\n", This, pUri, pOIProtSink,
274             pOIBindInfo, grfPI, dwReserved);
275
276     hres = IUri_GetScheme(pUri, &scheme);
277     if(FAILED(hres))
278         return hres;
279     if(scheme != URL_SCHEME_FTP)
280         return MK_E_SYNTAX;
281
282     return protocol_start(&This->base, (IInternetProtocol*)PROTOCOLEX(This), pUri, pOIProtSink, pOIBindInfo);
283 }
284 #undef PROTOCOL_THIS
285
286 static const IInternetProtocolExVtbl FtpProtocolVtbl = {
287     FtpProtocol_QueryInterface,
288     FtpProtocol_AddRef,
289     FtpProtocol_Release,
290     FtpProtocol_Start,
291     FtpProtocol_Continue,
292     FtpProtocol_Abort,
293     FtpProtocol_Terminate,
294     FtpProtocol_Suspend,
295     FtpProtocol_Resume,
296     FtpProtocol_Read,
297     FtpProtocol_Seek,
298     FtpProtocol_LockRequest,
299     FtpProtocol_UnlockRequest,
300     FtpProtocol_StartEx
301 };
302
303 #define PRIORITY_THIS(iface) DEFINE_THIS(FtpProtocol, InternetPriority, iface)
304
305 static HRESULT WINAPI FtpPriority_QueryInterface(IInternetPriority *iface, REFIID riid, void **ppv)
306 {
307     FtpProtocol *This = PRIORITY_THIS(iface);
308     return IInternetProtocolEx_QueryInterface(PROTOCOLEX(This), riid, ppv);
309 }
310
311 static ULONG WINAPI FtpPriority_AddRef(IInternetPriority *iface)
312 {
313     FtpProtocol *This = PRIORITY_THIS(iface);
314     return IInternetProtocolEx_AddRef(PROTOCOLEX(This));
315 }
316
317 static ULONG WINAPI FtpPriority_Release(IInternetPriority *iface)
318 {
319     FtpProtocol *This = PRIORITY_THIS(iface);
320     return IInternetProtocolEx_Release(PROTOCOLEX(This));
321 }
322
323 static HRESULT WINAPI FtpPriority_SetPriority(IInternetPriority *iface, LONG nPriority)
324 {
325     FtpProtocol *This = PRIORITY_THIS(iface);
326
327     TRACE("(%p)->(%d)\n", This, nPriority);
328
329     This->base.priority = nPriority;
330     return S_OK;
331 }
332
333 static HRESULT WINAPI FtpPriority_GetPriority(IInternetPriority *iface, LONG *pnPriority)
334 {
335     FtpProtocol *This = PRIORITY_THIS(iface);
336
337     TRACE("(%p)->(%p)\n", This, pnPriority);
338
339     *pnPriority = This->base.priority;
340     return S_OK;
341 }
342
343 #undef PRIORITY_THIS
344
345 static const IInternetPriorityVtbl FtpPriorityVtbl = {
346     FtpPriority_QueryInterface,
347     FtpPriority_AddRef,
348     FtpPriority_Release,
349     FtpPriority_SetPriority,
350     FtpPriority_GetPriority
351 };
352
353 #define INETINFO_THIS(iface) DEFINE_THIS(FtpProtocol, WinInetHttpInfo, iface)
354
355 static HRESULT WINAPI HttpInfo_QueryInterface(IWinInetHttpInfo *iface, REFIID riid, void **ppv)
356 {
357     FtpProtocol *This = INETINFO_THIS(iface);
358     return IInternetProtocolEx_QueryInterface(PROTOCOLEX(This), riid, ppv);
359 }
360
361 static ULONG WINAPI HttpInfo_AddRef(IWinInetHttpInfo *iface)
362 {
363     FtpProtocol *This = INETINFO_THIS(iface);
364     return IInternetProtocolEx_AddRef(PROTOCOLEX(This));
365 }
366
367 static ULONG WINAPI HttpInfo_Release(IWinInetHttpInfo *iface)
368 {
369     FtpProtocol *This = INETINFO_THIS(iface);
370     return IInternetProtocolEx_Release(PROTOCOLEX(This));
371 }
372
373 static HRESULT WINAPI HttpInfo_QueryOption(IWinInetHttpInfo *iface, DWORD dwOption,
374         void *pBuffer, DWORD *pcbBuffer)
375 {
376     FtpProtocol *This = INETINFO_THIS(iface);
377     FIXME("(%p)->(%x %p %p)\n", This, dwOption, pBuffer, pcbBuffer);
378     return E_NOTIMPL;
379 }
380
381 static HRESULT WINAPI HttpInfo_QueryInfo(IWinInetHttpInfo *iface, DWORD dwOption,
382         void *pBuffer, DWORD *pcbBuffer, DWORD *pdwFlags, DWORD *pdwReserved)
383 {
384     FtpProtocol *This = INETINFO_THIS(iface);
385     FIXME("(%p)->(%x %p %p %p %p)\n", This, dwOption, pBuffer, pcbBuffer, pdwFlags, pdwReserved);
386     return E_NOTIMPL;
387 }
388
389 #undef INETINFO_THIS
390
391 static const IWinInetHttpInfoVtbl WinInetHttpInfoVtbl = {
392     HttpInfo_QueryInterface,
393     HttpInfo_AddRef,
394     HttpInfo_Release,
395     HttpInfo_QueryOption,
396     HttpInfo_QueryInfo
397 };
398
399 HRESULT FtpProtocol_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
400 {
401     FtpProtocol *ret;
402
403     TRACE("(%p %p)\n", pUnkOuter, ppobj);
404
405     URLMON_LockModule();
406
407     ret = heap_alloc_zero(sizeof(FtpProtocol));
408
409     ret->base.vtbl = &AsyncProtocolVtbl;
410     ret->lpIInternetProtocolExVtbl = &FtpProtocolVtbl;
411     ret->lpInternetPriorityVtbl    = &FtpPriorityVtbl;
412     ret->lpWinInetHttpInfoVtbl     = &WinInetHttpInfoVtbl;
413     ret->ref = 1;
414
415     *ppobj = PROTOCOLEX(ret);
416     
417     return S_OK;
418 }