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