1 | // |
---|
2 | // FTPClientSession.h |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Net/include/Poco/Net/FTPClientSession.h#2 $ |
---|
5 | // |
---|
6 | // Library: Net |
---|
7 | // Package: FTP |
---|
8 | // Module: FTPClientSession |
---|
9 | // |
---|
10 | // Definition of the FTPClientSession class. |
---|
11 | // |
---|
12 | // Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH. |
---|
13 | // and Contributors. |
---|
14 | // |
---|
15 | // Permission is hereby granted, free of charge, to any person or organization |
---|
16 | // obtaining a copy of the software and accompanying documentation covered by |
---|
17 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
18 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
19 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
20 | // do so, all subject to the following: |
---|
21 | // |
---|
22 | // The copyright notices in the Software and this entire statement, including |
---|
23 | // the above license grant, this restriction and the following disclaimer, |
---|
24 | // must be included in all copies of the Software, in whole or in part, and |
---|
25 | // all derivative works of the Software, unless such copies or derivative |
---|
26 | // works are solely in the form of machine-executable object code generated by |
---|
27 | // a source language processor. |
---|
28 | // |
---|
29 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
30 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
31 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
32 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
33 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
34 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
35 | // DEALINGS IN THE SOFTWARE. |
---|
36 | // |
---|
37 | |
---|
38 | |
---|
39 | #ifndef Net_FTPClientSession_INCLUDED |
---|
40 | #define Net_FTPClientSession_INCLUDED |
---|
41 | |
---|
42 | |
---|
43 | #include "Poco/Net/Net.h" |
---|
44 | #include "Poco/Net/DialogSocket.h" |
---|
45 | #include "Poco/Timespan.h" |
---|
46 | #include <istream> |
---|
47 | #include <ostream> |
---|
48 | |
---|
49 | |
---|
50 | namespace Poco { |
---|
51 | namespace Net { |
---|
52 | |
---|
53 | |
---|
54 | class SocketStream; |
---|
55 | |
---|
56 | |
---|
57 | class Net_API FTPClientSession |
---|
58 | /// This class implements an File Transfer Protocol |
---|
59 | /// (FTP, RFC 959) client. |
---|
60 | /// |
---|
61 | /// Most of the features of the FTP protocol, as specified |
---|
62 | /// in RFC 959, are supported. Not supported are EBCDIC and |
---|
63 | /// LOCAL data types and format control and structured files. |
---|
64 | /// |
---|
65 | /// Also supported are the EPRT and EPSV commands from |
---|
66 | /// RFC 1738 (FTP Extensions for IPv6 and NAT). |
---|
67 | /// The client will first attempt to use the EPRT and EPSV |
---|
68 | /// commands. If the server does not supports these commands, |
---|
69 | /// the client will fall back to PORT and PASV. |
---|
70 | { |
---|
71 | public: |
---|
72 | enum |
---|
73 | { |
---|
74 | FTP_PORT = 21 |
---|
75 | }; |
---|
76 | |
---|
77 | enum FileType |
---|
78 | { |
---|
79 | TYPE_TEXT, // TYPE A (ASCII) |
---|
80 | TYPE_BINARY // TYPE I (Image) |
---|
81 | }; |
---|
82 | |
---|
83 | explicit FTPClientSession(const StreamSocket& socket); |
---|
84 | /// Creates an FTPClientSession using the given |
---|
85 | /// connected socket for the control connection. |
---|
86 | /// |
---|
87 | /// Passive mode will be used for data transfers. |
---|
88 | |
---|
89 | FTPClientSession(const std::string& host, Poco::UInt16 port = FTP_PORT); |
---|
90 | /// Creates an FTPClientSession using a socket connected |
---|
91 | /// to the given host and port. |
---|
92 | /// |
---|
93 | /// Passive mode will be used for data transfers. |
---|
94 | |
---|
95 | virtual ~FTPClientSession(); |
---|
96 | /// Destroys the FTPClientSession. |
---|
97 | |
---|
98 | void setTimeout(const Poco::Timespan& timeout); |
---|
99 | /// Sets the timeout for socket operations. |
---|
100 | |
---|
101 | Poco::Timespan getTimeout() const; |
---|
102 | /// Returns the timeout for socket operations. |
---|
103 | |
---|
104 | void setPassive(bool flag, bool useRFC1738 = true); |
---|
105 | /// Enables (default) or disables FTP passive mode for this session. |
---|
106 | /// |
---|
107 | /// If useRFC1738 is true (the default), the RFC 1738 |
---|
108 | /// EPSV command is used (with a fallback to PASV if EPSV fails) |
---|
109 | /// for switching to passive mode. The same applies to |
---|
110 | /// EPRT and PORT for active connections. |
---|
111 | |
---|
112 | bool getPassive() const; |
---|
113 | /// Returns true iff passive mode is enabled for this connection. |
---|
114 | |
---|
115 | void login(const std::string& username, const std::string& password); |
---|
116 | /// Authenticates the user against the FTP server. Must be |
---|
117 | /// called before any other commands (except QUIT) can be sent. |
---|
118 | /// |
---|
119 | /// Sends a USER command followed by a PASS command with the |
---|
120 | /// respective arguments to the server. |
---|
121 | /// |
---|
122 | /// Throws a FTPException in case of a FTP-specific error, or a |
---|
123 | /// NetException in case of a general network communication failure. |
---|
124 | |
---|
125 | void close(); |
---|
126 | /// Sends a QUIT command and closes the connection to the server. |
---|
127 | /// |
---|
128 | /// Throws a FTPException in case of a FTP-specific error, or a |
---|
129 | /// NetException in case of a general network communication failure. |
---|
130 | |
---|
131 | std::string systemType(); |
---|
132 | /// Returns the system type of the FTP server. |
---|
133 | /// |
---|
134 | /// Sends a SYST command to the server and returns the result. |
---|
135 | |
---|
136 | void setFileType(FileType type); |
---|
137 | /// Sets the file type for transferring files. |
---|
138 | /// |
---|
139 | /// Sends a TYPE command with a corresponsing argument to the |
---|
140 | /// server. |
---|
141 | /// |
---|
142 | /// Throws a FTPException in case of a FTP-specific error, or a |
---|
143 | /// NetException in case of a general network communication failure. |
---|
144 | |
---|
145 | FileType getFileType() const; |
---|
146 | /// Returns the file type for transferring files. |
---|
147 | |
---|
148 | void setWorkingDirectory(const std::string& path); |
---|
149 | /// Changes the current working directory on the server. |
---|
150 | /// |
---|
151 | /// Sends a CWD command with the given path as argument to the |
---|
152 | /// server. |
---|
153 | /// |
---|
154 | /// Throws a FTPException in case of a FTP-specific error, or a |
---|
155 | /// NetException in case of a general network communication failure. |
---|
156 | |
---|
157 | std::string getWorkingDirectory(); |
---|
158 | /// Returns the current working directory on the server. |
---|
159 | /// |
---|
160 | /// Throws a FTPException in case of a FTP-specific error, or a |
---|
161 | /// NetException in case of a general network communication failure. |
---|
162 | |
---|
163 | void cdup(); |
---|
164 | /// Moves one directory up from the current working directory |
---|
165 | /// on teh server. |
---|
166 | /// |
---|
167 | /// Sends a CDUP command to the server. |
---|
168 | /// |
---|
169 | /// Throws a FTPException in case of a FTP-specific error, or a |
---|
170 | /// NetException in case of a general network communication failure. |
---|
171 | |
---|
172 | void rename(const std::string& oldName, const std::string& newName); |
---|
173 | /// Renames the file on the server given by oldName to newName. |
---|
174 | /// |
---|
175 | /// Sends a RNFR command, followed by a RNTO command to the server. |
---|
176 | /// |
---|
177 | /// Throws a FTPException in case of a FTP-specific error, or a |
---|
178 | /// NetException in case of a general network communication failure. |
---|
179 | |
---|
180 | void remove(const std::string& path); |
---|
181 | /// Deletes the file specified by path on the server. |
---|
182 | /// |
---|
183 | /// Sends a DELE command with path as argument to the server. |
---|
184 | /// |
---|
185 | /// Throws a FTPException in case of a FTP-specific error, or a |
---|
186 | /// NetException in case of a general network communication failure. |
---|
187 | |
---|
188 | void createDirectory(const std::string& path); |
---|
189 | /// Creates a new directory with the given path on the server. |
---|
190 | /// |
---|
191 | /// Sends a MKD command with path as argument to the server. |
---|
192 | /// |
---|
193 | /// Throws a FTPException in case of a FTP-specific error, or a |
---|
194 | /// NetException in case of a general network communication failure. |
---|
195 | |
---|
196 | void removeDirectory(const std::string& path); |
---|
197 | /// Removes the directory specified by path from the server. |
---|
198 | /// |
---|
199 | /// Sends a RMD command with path as argument to the server. |
---|
200 | /// |
---|
201 | /// Throws a FTPException in case of a FTP-specific error, or a |
---|
202 | /// NetException in case of a general network communication failure. |
---|
203 | |
---|
204 | std::istream& beginDownload(const std::string& path); |
---|
205 | /// Starts downloading the file with the given name. |
---|
206 | /// After all data has been read from the returned stream, |
---|
207 | /// endDownload() must be called to finish the download. |
---|
208 | /// |
---|
209 | /// A stream for reading the file's content is returned. |
---|
210 | /// The stream is valid until endDownload() is called. |
---|
211 | /// |
---|
212 | /// Creates a data connection between the client and the |
---|
213 | /// server. If passive mode is on, then the server waits for |
---|
214 | /// a connection request from the client. Otherwise, the |
---|
215 | /// client waits for a connection request from the server. |
---|
216 | /// After establishing the data connection, a SocketStream |
---|
217 | /// for transferring the data is created. |
---|
218 | /// |
---|
219 | /// If ASCII transfer mode is selected, the caller is |
---|
220 | /// responsible for converting the received data to |
---|
221 | /// the native text file format. |
---|
222 | /// The InputLineEndingConverter class from the Foundation |
---|
223 | /// library can be used for that purpose. |
---|
224 | |
---|
225 | void endDownload(); |
---|
226 | /// Must be called to complete a download initiated with |
---|
227 | /// beginDownload(). |
---|
228 | |
---|
229 | std::ostream& beginUpload(const std::string& path); |
---|
230 | /// Starts uploading the file with the given name. |
---|
231 | /// After all data has been written to the returned stream, |
---|
232 | /// endUpload() must be called to finish the download. |
---|
233 | /// |
---|
234 | /// A stream for reading the file's content is returned. |
---|
235 | /// The stream is valid until endUpload() is called. |
---|
236 | /// |
---|
237 | /// Creates a data connection between the client and the |
---|
238 | /// server. If passive mode is on, then the server waits for |
---|
239 | /// a connection request from the client. Otherwise, the |
---|
240 | /// client waits for a connection request from the server. |
---|
241 | /// After establishing the data connection, a SocketStream |
---|
242 | /// for transferring the data is created. |
---|
243 | /// |
---|
244 | /// If ASCII transfer mode is selected, the caller is |
---|
245 | /// responsible for converting the data to be sent |
---|
246 | /// into network (CR-LF line endings) format. |
---|
247 | /// The OutputLineEndingConverter class from the Foundation |
---|
248 | /// library can be used for that purpose. |
---|
249 | |
---|
250 | void endUpload(); |
---|
251 | /// Must be called to complete an upload initiated with |
---|
252 | /// beginUpload(). |
---|
253 | |
---|
254 | std::istream& beginList(const std::string& path = "", bool extended = false); |
---|
255 | /// Starts downloading a directory listing. |
---|
256 | /// After all data has been read from the returned stream, |
---|
257 | /// endList() must be called to finish the download. |
---|
258 | /// |
---|
259 | /// A stream for reading the directory data is returned. |
---|
260 | /// The stream is valid until endList() is called. |
---|
261 | /// |
---|
262 | /// Optionally, a path to a directory or file can be specified. |
---|
263 | /// According to the FTP prototol, if a path to a filename is |
---|
264 | /// given, only information for the specific file is returned. |
---|
265 | /// If a path to a directory is given, a listing of that directory |
---|
266 | /// is returned. If no path is given, a listing of the current |
---|
267 | /// working directory is returned. |
---|
268 | /// |
---|
269 | /// If extended is false, only a filenames (one per line) are |
---|
270 | /// returned. Otherwise, a full directory listing including |
---|
271 | /// file attributes is returned. The format of this listing |
---|
272 | /// depends on the FTP server. No attempt is made to interpret |
---|
273 | /// this data. |
---|
274 | /// |
---|
275 | /// Creates a data connection between the client and the |
---|
276 | /// server. If passive mode is on, then the server waits for |
---|
277 | /// a connection request from the client. Otherwise, the |
---|
278 | /// client waits for a connection request from the server. |
---|
279 | /// After establishing the data connection, a SocketStream |
---|
280 | /// for transferring the data is created. |
---|
281 | |
---|
282 | void endList(); |
---|
283 | /// Must be called to complete a directory listing download |
---|
284 | /// initiated with beginList(). |
---|
285 | |
---|
286 | void abort(); |
---|
287 | /// Aborts the download or upload currently in progress. |
---|
288 | /// |
---|
289 | /// Sends a TELNET IP/SYNCH sequence, followed by an ABOR |
---|
290 | /// command to the server. |
---|
291 | /// |
---|
292 | /// A separate call to endDownload() or endUpload() is |
---|
293 | /// not necessary. |
---|
294 | |
---|
295 | int sendCommand(const std::string& command, std::string& response); |
---|
296 | /// Sends the given command verbatim to the server |
---|
297 | /// and waits for a response. |
---|
298 | |
---|
299 | int sendCommand(const std::string& command, const std::string& arg, std::string& response); |
---|
300 | /// Sends the given command verbatim to the server |
---|
301 | /// and waits for a response. |
---|
302 | |
---|
303 | protected: |
---|
304 | enum StatusClass |
---|
305 | { |
---|
306 | FTP_POSITIVE_PRELIMINARY = 1, |
---|
307 | FTP_POSITIVE_COMPLETION = 2, |
---|
308 | FTP_POSITIVE_INTERMEDIATE = 3, |
---|
309 | FTP_TRANSIENT_NEGATIVE = 4, |
---|
310 | FTP_PERMANENT_NEGATIVE = 5 |
---|
311 | }; |
---|
312 | enum |
---|
313 | { |
---|
314 | DEFAULT_TIMEOUT = 30000000 // 30 seconds default timeout for socket operations |
---|
315 | }; |
---|
316 | |
---|
317 | static bool isPositivePreliminary(int status); |
---|
318 | static bool isPositiveCompletion(int status); |
---|
319 | static bool isPositiveIntermediate(int status); |
---|
320 | static bool isTransientNegative(int status); |
---|
321 | static bool isPermanentNegative(int status); |
---|
322 | std::string extractPath(const std::string& response); |
---|
323 | StreamSocket establishDataConnection(const std::string& command, const std::string& arg); |
---|
324 | StreamSocket activeDataConnection(const std::string& command, const std::string& arg); |
---|
325 | StreamSocket passiveDataConnection(const std::string& command, const std::string& arg); |
---|
326 | void sendPortCommand(const SocketAddress& addr); |
---|
327 | SocketAddress sendPassiveCommand(); |
---|
328 | bool sendEPRT(const SocketAddress& addr); |
---|
329 | void sendPORT(const SocketAddress& addr); |
---|
330 | bool sendEPSV(SocketAddress& addr); |
---|
331 | void sendPASV(SocketAddress& addr); |
---|
332 | void parseAddress(const std::string& str, SocketAddress& addr); |
---|
333 | void parseExtAddress(const std::string& str, SocketAddress& addr); |
---|
334 | void endTransfer(); |
---|
335 | |
---|
336 | private: |
---|
337 | FTPClientSession(); |
---|
338 | FTPClientSession(const FTPClientSession&); |
---|
339 | FTPClientSession& operator = (const FTPClientSession&); |
---|
340 | |
---|
341 | DialogSocket _controlSocket; |
---|
342 | SocketStream* _pDataStream; |
---|
343 | bool _passiveMode; |
---|
344 | FileType _fileType; |
---|
345 | bool _supports1738; |
---|
346 | bool _isOpen; |
---|
347 | Poco::Timespan _timeout; |
---|
348 | }; |
---|
349 | |
---|
350 | |
---|
351 | // |
---|
352 | // inlines |
---|
353 | // |
---|
354 | inline bool FTPClientSession::isPositivePreliminary(int status) |
---|
355 | { |
---|
356 | return status/100 == FTP_POSITIVE_PRELIMINARY; |
---|
357 | } |
---|
358 | |
---|
359 | |
---|
360 | inline bool FTPClientSession::isPositiveCompletion(int status) |
---|
361 | { |
---|
362 | return status/100 == FTP_POSITIVE_COMPLETION; |
---|
363 | } |
---|
364 | |
---|
365 | |
---|
366 | inline bool FTPClientSession::isPositiveIntermediate(int status) |
---|
367 | { |
---|
368 | return status/100 == FTP_POSITIVE_INTERMEDIATE; |
---|
369 | } |
---|
370 | |
---|
371 | |
---|
372 | inline bool FTPClientSession::isTransientNegative(int status) |
---|
373 | { |
---|
374 | return status/100 == FTP_TRANSIENT_NEGATIVE; |
---|
375 | } |
---|
376 | |
---|
377 | |
---|
378 | inline bool FTPClientSession::isPermanentNegative(int status) |
---|
379 | { |
---|
380 | return status/100 == FTP_PERMANENT_NEGATIVE; |
---|
381 | } |
---|
382 | |
---|
383 | |
---|
384 | } } // namespace Poco::Net |
---|
385 | |
---|
386 | |
---|
387 | #endif // Net_FTPClientSession_INCLUDED |
---|