Release 1.3.7.
[wine/gsoc-2012-control.git] / dlls / urlmon / ftp.c
blob864940158ec000b5becedd8cb3dcede2082a9096
1 /*
2 * Copyright 2005-2009 Jacek Caban for CodeWeavers
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.
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.
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
19 #include "urlmon_main.h"
21 #define NO_SHLWAPI_REG
22 #include "shlwapi.h"
24 #include "wine/debug.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
28 typedef struct {
29 Protocol base;
31 const IInternetProtocolExVtbl*lpIInternetProtocolExVtbl;
32 const IInternetPriorityVtbl *lpInternetPriorityVtbl;
33 const IWinInetHttpInfoVtbl *lpWinInetHttpInfoVtbl;
35 LONG ref;
36 } FtpProtocol;
38 #define PROTOCOLEX(x) ((IInternetProtocolEx*)&(x)->lpIInternetProtocolExVtbl)
39 #define PRIORITY(x) ((IInternetPriority*) &(x)->lpInternetPriorityVtbl)
40 #define INETHTTPINFO(x) ((IWinInetHttpInfo*) &(x)->lpWinInetHttpInfoVtbl)
42 #define ASYNCPROTOCOL_THIS(iface) DEFINE_THIS2(FtpProtocol, base, iface)
44 static HRESULT FtpProtocol_open_request(Protocol *prot, IUri *uri, DWORD request_flags,
45 HINTERNET internet_session, IInternetBindInfo *bind_info)
47 FtpProtocol *This = ASYNCPROTOCOL_THIS(prot);
48 BSTR url;
49 HRESULT hres;
51 hres = IUri_GetAbsoluteUri(uri, &url);
52 if(FAILED(hres))
53 return hres;
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;
64 return S_OK;
67 static HRESULT FtpProtocol_end_request(Protocol *prot)
69 return E_NOTIMPL;
72 static HRESULT FtpProtocol_start_downloading(Protocol *prot)
74 FtpProtocol *This = ASYNCPROTOCOL_THIS(prot);
75 DWORD size;
76 BOOL res;
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());
84 return S_OK;
87 static void FtpProtocol_close_connection(Protocol *prot)
91 #undef ASYNCPROTOCOL_THIS
93 static const ProtocolVtbl AsyncProtocolVtbl = {
94 FtpProtocol_open_request,
95 FtpProtocol_end_request,
96 FtpProtocol_start_downloading,
97 FtpProtocol_close_connection
100 #define PROTOCOL_THIS(iface) DEFINE_THIS(FtpProtocol, IInternetProtocolEx, iface)
102 static HRESULT WINAPI FtpProtocol_QueryInterface(IInternetProtocolEx *iface, REFIID riid, void **ppv)
104 FtpProtocol *This = PROTOCOL_THIS(iface);
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);
130 if(*ppv) {
131 IInternetProtocol_AddRef(iface);
132 return S_OK;
135 WARN("not supported interface %s\n", debugstr_guid(riid));
136 return E_NOINTERFACE;
139 static ULONG WINAPI FtpProtocol_AddRef(IInternetProtocolEx *iface)
141 FtpProtocol *This = PROTOCOL_THIS(iface);
142 LONG ref = InterlockedIncrement(&This->ref);
143 TRACE("(%p) ref=%d\n", This, ref);
144 return ref;
147 static ULONG WINAPI FtpProtocol_Release(IInternetProtocolEx *iface)
149 FtpProtocol *This = PROTOCOL_THIS(iface);
150 LONG ref = InterlockedDecrement(&This->ref);
152 TRACE("(%p) ref=%d\n", This, ref);
154 if(!ref) {
155 protocol_close_connection(&This->base);
156 heap_free(This);
158 URLMON_UnlockModule();
161 return ref;
164 static HRESULT WINAPI FtpProtocol_Start(IInternetProtocolEx *iface, LPCWSTR szUrl,
165 IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
166 DWORD grfPI, HANDLE_PTR dwReserved)
168 FtpProtocol *This = PROTOCOL_THIS(iface);
169 IUri *uri;
170 HRESULT hres;
172 TRACE("(%p)->(%s %p %p %08x %lx)\n", This, debugstr_w(szUrl), pOIProtSink,
173 pOIBindInfo, grfPI, dwReserved);
175 hres = CreateUri(szUrl, 0, 0, &uri);
176 if(FAILED(hres))
177 return hres;
179 hres = IInternetProtocolEx_StartEx(PROTOCOLEX(This), uri, pOIProtSink, pOIBindInfo,
180 grfPI, (HANDLE*)dwReserved);
182 IUri_Release(uri);
183 return hres;
186 static HRESULT WINAPI FtpProtocol_Continue(IInternetProtocolEx *iface, PROTOCOLDATA *pProtocolData)
188 FtpProtocol *This = PROTOCOL_THIS(iface);
190 TRACE("(%p)->(%p)\n", This, pProtocolData);
192 return protocol_continue(&This->base, pProtocolData);
195 static HRESULT WINAPI FtpProtocol_Abort(IInternetProtocolEx *iface, HRESULT hrReason,
196 DWORD dwOptions)
198 FtpProtocol *This = PROTOCOL_THIS(iface);
200 TRACE("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);
202 return protocol_abort(&This->base, hrReason);
205 static HRESULT WINAPI FtpProtocol_Terminate(IInternetProtocolEx *iface, DWORD dwOptions)
207 FtpProtocol *This = PROTOCOL_THIS(iface);
209 TRACE("(%p)->(%08x)\n", This, dwOptions);
211 protocol_close_connection(&This->base);
212 return S_OK;
215 static HRESULT WINAPI FtpProtocol_Suspend(IInternetProtocolEx *iface)
217 FtpProtocol *This = PROTOCOL_THIS(iface);
218 FIXME("(%p)\n", This);
219 return E_NOTIMPL;
222 static HRESULT WINAPI FtpProtocol_Resume(IInternetProtocolEx *iface)
224 FtpProtocol *This = PROTOCOL_THIS(iface);
225 FIXME("(%p)\n", This);
226 return E_NOTIMPL;
229 static HRESULT WINAPI FtpProtocol_Read(IInternetProtocolEx *iface, void *pv,
230 ULONG cb, ULONG *pcbRead)
232 FtpProtocol *This = PROTOCOL_THIS(iface);
234 TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
236 return protocol_read(&This->base, pv, cb, pcbRead);
239 static HRESULT WINAPI FtpProtocol_Seek(IInternetProtocolEx *iface, LARGE_INTEGER dlibMove,
240 DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
242 FtpProtocol *This = PROTOCOL_THIS(iface);
243 FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
244 return E_NOTIMPL;
247 static HRESULT WINAPI FtpProtocol_LockRequest(IInternetProtocolEx *iface, DWORD dwOptions)
249 FtpProtocol *This = PROTOCOL_THIS(iface);
251 TRACE("(%p)->(%08x)\n", This, dwOptions);
253 return protocol_lock_request(&This->base);
256 static HRESULT WINAPI FtpProtocol_UnlockRequest(IInternetProtocolEx *iface)
258 FtpProtocol *This = PROTOCOL_THIS(iface);
260 TRACE("(%p)\n", This);
262 return protocol_unlock_request(&This->base);
265 static HRESULT WINAPI FtpProtocol_StartEx(IInternetProtocolEx *iface, IUri *pUri,
266 IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
267 DWORD grfPI, HANDLE *dwReserved)
269 FtpProtocol *This = PROTOCOL_THIS(iface);
270 DWORD scheme = 0;
271 HRESULT hres;
273 TRACE("(%p)->(%p %p %p %08x %p)\n", This, pUri, pOIProtSink,
274 pOIBindInfo, grfPI, dwReserved);
276 hres = IUri_GetScheme(pUri, &scheme);
277 if(FAILED(hres))
278 return hres;
279 if(scheme != URL_SCHEME_FTP)
280 return MK_E_SYNTAX;
282 return protocol_start(&This->base, (IInternetProtocol*)PROTOCOLEX(This), pUri, pOIProtSink, pOIBindInfo);
284 #undef PROTOCOL_THIS
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
303 #define PRIORITY_THIS(iface) DEFINE_THIS(FtpProtocol, InternetPriority, iface)
305 static HRESULT WINAPI FtpPriority_QueryInterface(IInternetPriority *iface, REFIID riid, void **ppv)
307 FtpProtocol *This = PRIORITY_THIS(iface);
308 return IInternetProtocolEx_QueryInterface(PROTOCOLEX(This), riid, ppv);
311 static ULONG WINAPI FtpPriority_AddRef(IInternetPriority *iface)
313 FtpProtocol *This = PRIORITY_THIS(iface);
314 return IInternetProtocolEx_AddRef(PROTOCOLEX(This));
317 static ULONG WINAPI FtpPriority_Release(IInternetPriority *iface)
319 FtpProtocol *This = PRIORITY_THIS(iface);
320 return IInternetProtocolEx_Release(PROTOCOLEX(This));
323 static HRESULT WINAPI FtpPriority_SetPriority(IInternetPriority *iface, LONG nPriority)
325 FtpProtocol *This = PRIORITY_THIS(iface);
327 TRACE("(%p)->(%d)\n", This, nPriority);
329 This->base.priority = nPriority;
330 return S_OK;
333 static HRESULT WINAPI FtpPriority_GetPriority(IInternetPriority *iface, LONG *pnPriority)
335 FtpProtocol *This = PRIORITY_THIS(iface);
337 TRACE("(%p)->(%p)\n", This, pnPriority);
339 *pnPriority = This->base.priority;
340 return S_OK;
343 #undef PRIORITY_THIS
345 static const IInternetPriorityVtbl FtpPriorityVtbl = {
346 FtpPriority_QueryInterface,
347 FtpPriority_AddRef,
348 FtpPriority_Release,
349 FtpPriority_SetPriority,
350 FtpPriority_GetPriority
353 #define INETINFO_THIS(iface) DEFINE_THIS(FtpProtocol, WinInetHttpInfo, iface)
355 static HRESULT WINAPI HttpInfo_QueryInterface(IWinInetHttpInfo *iface, REFIID riid, void **ppv)
357 FtpProtocol *This = INETINFO_THIS(iface);
358 return IInternetProtocolEx_QueryInterface(PROTOCOLEX(This), riid, ppv);
361 static ULONG WINAPI HttpInfo_AddRef(IWinInetHttpInfo *iface)
363 FtpProtocol *This = INETINFO_THIS(iface);
364 return IInternetProtocolEx_AddRef(PROTOCOLEX(This));
367 static ULONG WINAPI HttpInfo_Release(IWinInetHttpInfo *iface)
369 FtpProtocol *This = INETINFO_THIS(iface);
370 return IInternetProtocolEx_Release(PROTOCOLEX(This));
373 static HRESULT WINAPI HttpInfo_QueryOption(IWinInetHttpInfo *iface, DWORD dwOption,
374 void *pBuffer, DWORD *pcbBuffer)
376 FtpProtocol *This = INETINFO_THIS(iface);
377 FIXME("(%p)->(%x %p %p)\n", This, dwOption, pBuffer, pcbBuffer);
378 return E_NOTIMPL;
381 static HRESULT WINAPI HttpInfo_QueryInfo(IWinInetHttpInfo *iface, DWORD dwOption,
382 void *pBuffer, DWORD *pcbBuffer, DWORD *pdwFlags, DWORD *pdwReserved)
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;
389 #undef INETINFO_THIS
391 static const IWinInetHttpInfoVtbl WinInetHttpInfoVtbl = {
392 HttpInfo_QueryInterface,
393 HttpInfo_AddRef,
394 HttpInfo_Release,
395 HttpInfo_QueryOption,
396 HttpInfo_QueryInfo
399 HRESULT FtpProtocol_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
401 FtpProtocol *ret;
403 TRACE("(%p %p)\n", pUnkOuter, ppobj);
405 URLMON_LockModule();
407 ret = heap_alloc_zero(sizeof(FtpProtocol));
409 ret->base.vtbl = &AsyncProtocolVtbl;
410 ret->lpIInternetProtocolExVtbl = &FtpProtocolVtbl;
411 ret->lpInternetPriorityVtbl = &FtpPriorityVtbl;
412 ret->lpWinInetHttpInfoVtbl = &WinInetHttpInfoVtbl;
413 ret->ref = 1;
415 *ppobj = PROTOCOLEX(ret);
417 return S_OK;