Ftp.hpp
1 //
3 // SFML - Simple and Fast Multimedia Library
4 // Copyright (C) 2007-2015 Laurent Gomila (laurent@sfml-dev.org)
5 //
6 // This software is provided 'as-is', without any express or implied warranty.
7 // In no event will the authors be held liable for any damages arising from the use of this software.
8 //
9 // Permission is granted to anyone to use this software for any purpose,
10 // including commercial applications, and to alter it and redistribute it freely,
11 // subject to the following restrictions:
12 //
13 // 1. The origin of this software must not be misrepresented;
14 // you must not claim that you wrote the original software.
15 // If you use this software in a product, an acknowledgment
16 // in the product documentation would be appreciated but is not required.
17 //
18 // 2. Altered source versions must be plainly marked as such,
19 // and must not be misrepresented as being the original software.
20 //
21 // 3. This notice may not be removed or altered from any source distribution.
22 //
24 
25 #ifndef CPP3DS_FTP_HPP
26 #define CPP3DS_FTP_HPP
27 
29 // Headers
31 #include <cpp3ds/Network/TcpSocket.hpp>
32 #include <cpp3ds/System/NonCopyable.hpp>
33 #include <cpp3ds/System/Time.hpp>
34 #include <string>
35 #include <vector>
36 
37 
38 namespace cpp3ds
39 {
40 class IpAddress;
41 
47 {
48 public:
49 
55  {
59  };
60 
65  class Response
66  {
67  public:
68 
73  enum Status
74  {
75  // 1xx: the requested action is being initiated,
76  // expect another reply before proceeding with a new command
81 
82  // 2xx: the requested action has been successfully completed
83  Ok = 200,
85  SystemStatus = 211,
87  FileStatus = 213,
88  HelpMessage = 214,
89  SystemType = 215,
90  ServiceReady = 220,
95  LoggedIn = 230,
96  FileActionOk = 250,
97  DirectoryOk = 257,
98 
99  // 3xx: the command has been accepted, but the requested action
100  // is dormant, pending receipt of further information
101  NeedPassword = 331,
104 
105  // 4xx: the command was not accepted and the requested action did not take place,
106  // but the error condition is temporary and the action may be requested again
111  LocalError = 451,
113 
114  // 5xx: the command was not accepted and
115  // the requested action did not take place
121  NotLoggedIn = 530,
127 
128  // 10xx: cpp3ds custom codes
132  InvalidFile = 1003
133  };
134 
145  explicit Response(Status code = InvalidResponse, const std::string& message = "");
146 
156  bool isOk() const;
157 
164  Status getStatus() const;
165 
172  const std::string& getMessage() const;
173 
174  private:
175 
177  // Member data
179  Status m_status;
180  std::string m_message;
181  };
182 
188  {
189  public:
190 
197  DirectoryResponse(const Response& response);
198 
205  const std::string& getDirectory() const;
206 
207  private:
208 
210  // Member data
212  std::string m_directory;
213  };
214 
215 
220  class ListingResponse : public Response
221  {
222  public:
223 
231  ListingResponse(const Response& response, const std::string& data);
232 
239  const std::vector<std::string>& getListing() const;
240 
241  private:
242 
244  // Member data
246  std::vector<std::string> m_listing;
247  };
248 
249 
257  ~Ftp();
258 
280  Response connect(const IpAddress& server, unsigned short port = 21, Time timeout = Time::Zero);
281 
291 
301  Response login();
302 
315  Response login(const std::string& name, const std::string& password);
316 
327 
340 
356  ListingResponse getDirectoryListing(const std::string& directory = "");
357 
370  Response changeDirectory(const std::string& directory);
371 
381 
395  Response createDirectory(const std::string& name);
396 
412  Response deleteDirectory(const std::string& name);
413 
428  Response renameFile(const std::string& file, const std::string& newName);
429 
445  Response deleteFile(const std::string& name);
446 
467  Response download(const std::string& remoteFile, const std::string& localPath, TransferMode mode = Binary);
468 
486  Response upload(const std::string& localFile, const std::string& remotePath, TransferMode mode = Binary);
487 
504  Response sendCommand(const std::string& command, const std::string& parameter = "");
505 
506 private:
507 
517  Response getResponse();
518 
524  class DataChannel;
525 
526  friend class DataChannel;
527 
529  // Member data
531  TcpSocket m_commandSocket;
532 };
533 
534 } // namespace cpp3ds
535 
536 
537 #endif // CPP3DS_FTP_HPP
538 
539 
Service ready in N minutes.
Definition: Ftp.hpp:78
Requested action not taken, file name not allowed.
Definition: Ftp.hpp:126
System status, or system help reply.
Definition: Ftp.hpp:85
Requested file action ok.
Definition: Ftp.hpp:96
Data connection already opened, transfer starting.
Definition: Ftp.hpp:79
Encapsulate an IPv4 network address.
Definition: IpAddress.hpp:43
Entering passive mode.
Definition: Ftp.hpp:94
Command ok.
Definition: Ftp.hpp:83
Data connection open, no transfer in progress.
Definition: Ftp.hpp:92
Response download(const std::string &remoteFile, const std::string &localPath, TransferMode mode=Binary)
Download a file from the server.
Requested action not taken, file unavailable.
Definition: Ftp.hpp:123
Text mode using ASCII encoding.
Definition: Ftp.hpp:57
Need account for storing files.
Definition: Ftp.hpp:122
Text mode using EBCDIC encoding.
Definition: Ftp.hpp:58
Command not implemented.
Definition: Ftp.hpp:84
Command not implemented for that parameter.
Definition: Ftp.hpp:120
Status getStatus() const
Get the status code of the response.
File status ok, about to open data connection.
Definition: Ftp.hpp:80
DirectoryResponse(const Response &response)
Default constructor.
A FTP client.
Definition: Ftp.hpp:46
TransferMode
Enumeration of transfer modes.
Definition: Ftp.hpp:54
Response upload(const std::string &localFile, const std::string &remotePath, TransferMode mode=Binary)
Upload a file to the server.
Response(Status code=InvalidResponse, const std::string &message="")
Default constructor.
Define a FTP response.
Definition: Ftp.hpp:65
Closing data connection, requested file action successful.
Definition: Ftp.hpp:93
Not part of the FTP standard, generated by cpp3ds when a received response cannot be parsed...
Definition: Ftp.hpp:129
Specialization of FTP response returning a directory.
Definition: Ftp.hpp:187
Service ready for new user.
Definition: Ftp.hpp:90
Represents a time value.
Definition: Time.hpp:37
Connection closed, transfer aborted.
Definition: Ftp.hpp:109
NAME system type, where NAME is an official system name from the list in the Assigned Numbers documen...
Definition: Ftp.hpp:89
bool isOk() const
Check if the status code means a success.
Can't open data connection.
Definition: Ftp.hpp:108
Requested action aborted, page type unknown.
Definition: Ftp.hpp:124
const std::vector< std::string > & getListing() const
Return the array of directory/file names.
Binary mode (file is transfered as a sequence of bytes)
Definition: Ftp.hpp:56
Command not implemented.
Definition: Ftp.hpp:118
Response parentDirectory()
Go to the parent directory of the current one.
Response login()
Log in using an anonymous account.
Requested file action aborted, exceeded storage allocation.
Definition: Ftp.hpp:125
const std::string & getMessage() const
Get the full message contained in the response.
~Ftp()
Destructor.
Response deleteDirectory(const std::string &name)
Remove an existing directory.
Need account for login.
Definition: Ftp.hpp:102
Response keepAlive()
Send a null command to keep the connection alive.
Bad sequence of commands.
Definition: Ftp.hpp:119
Requested action not taken; insufficient storage space in system, file unavailable.
Definition: Ftp.hpp:112
Response connect(const IpAddress &server, unsigned short port=21, Time timeout=Time::Zero)
Connect to the specified FTP server.
Requested file action pending further information.
Definition: Ftp.hpp:103
User logged in, proceed. Logged out if appropriate.
Definition: Ftp.hpp:95
Response sendCommand(const std::string &command, const std::string &parameter="")
Send a command to the FTP server.
Not part of the FTP standard, generated by cpp3ds when a local file cannot be read or written...
Definition: Ftp.hpp:132
Not part of the FTP standard, generated by cpp3ds when the low-level socket connection with the serve...
Definition: Ftp.hpp:130
Requested action aborted, local error in processing.
Definition: Ftp.hpp:111
const std::string & getDirectory() const
Get the directory returned in the response.
User name ok, need password.
Definition: Ftp.hpp:101
Utility class that makes any derived class non-copyable.
Definition: NonCopyable.hpp:35
Response changeDirectory(const std::string &directory)
Change the current working directory.
Restart marker reply.
Definition: Ftp.hpp:77
Specialized socket using the TCP protocol.
Definition: TcpSocket.hpp:45
ListingResponse(const Response &response, const std::string &data)
Default constructor.
Response renameFile(const std::string &file, const std::string &newName)
Rename an existing file.
Response disconnect()
Close the connection with the server.
Response createDirectory(const std::string &name)
Create a new directory.
Syntax error, command unrecognized.
Definition: Ftp.hpp:116
Service not available, closing control connection.
Definition: Ftp.hpp:107
DirectoryResponse getWorkingDirectory()
Get the current working directory.
Not part of the FTP standard, generated by cpp3ds when the low-level socket connection is unexpectedl...
Definition: Ftp.hpp:131
Specialization of FTP response returning a filename listing.
Definition: Ftp.hpp:220
Response deleteFile(const std::string &name)
Remove an existing file.
Syntax error in parameters or arguments.
Definition: Ftp.hpp:117
PATHNAME created.
Definition: Ftp.hpp:97
Service closing control connection.
Definition: Ftp.hpp:91
Status
Status codes possibly returned by a FTP response.
Definition: Ftp.hpp:73
ListingResponse getDirectoryListing(const std::string &directory="")
Get the contents of the given directory.
Requested file action not taken.
Definition: Ftp.hpp:110
static const Time Zero
Predefined "zero" time value.
Definition: Time.hpp:82