-
Notifications
You must be signed in to change notification settings - Fork 60
/
ftp_session.h
executable file
路232 lines (188 loc) 路 8.59 KB
/
ftp_session.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#pragma once
#include <asio.hpp>
#include <deque>
#include <fstream>
#include "ftp_message.h"
#include "filesystem.h"
#include "user_database.h"
#include "ftp_user.h"
#include "win_str_convert.h"
namespace fineftp
{
class ReadableFile;
class FtpSession
: public std::enable_shared_from_this<FtpSession>
{
private:
struct WriteableFile
{
WriteableFile(const std::string& filename, std::ios::openmode mode)
#if defined(WIN32) && !defined(__GNUG__)
: file_stream_(StrConvert::Utf8ToWide(filename), mode)
#else
: file_stream_(filename, mode)
#endif
, stream_buffer_(1024 * 1024)
{
file_stream_.rdbuf()->pubsetbuf(stream_buffer_.data(), static_cast<std::streamsize>(stream_buffer_.size()));
}
// Copy
WriteableFile(const WriteableFile&) = delete;
WriteableFile& operator=(const WriteableFile&) = delete;
// Move disabled (as we are storing the shared_from_this() pointer in lambda captures)
WriteableFile& operator=(WriteableFile&&) = delete;
WriteableFile(WriteableFile&&) = delete;
~WriteableFile()
{
file_stream_.flush();
file_stream_.close();
}
std::fstream file_stream_;
std::vector<char> stream_buffer_;
};
////////////////////////////////////////////////////////
// Public API
////////////////////////////////////////////////////////
public:
FtpSession(asio::io_service& io_service, const UserDatabase& user_database, const std::function<void()>& completion_handler);
// Copy (disabled, as we are inheriting from shared_from_this)
FtpSession(const FtpSession&) = delete;
FtpSession& operator=(const FtpSession&) = delete;
// Move (disabled, as we are inheriting from shared_from_this)
FtpSession& operator=(FtpSession&&) = delete;
FtpSession(FtpSession&&) = delete;
~FtpSession();
void start();
asio::ip::tcp::socket& getSocket();
////////////////////////////////////////////////////////
// FTP command-socket
////////////////////////////////////////////////////////
private:
void sendFtpMessage(const FtpMessage& message);
void sendFtpMessage(FtpReplyCode code, const std::string& message);
void sendRawFtpMessage(const std::string& raw_message);
void startSendingMessages();
void readFtpCommand();
void handleFtpCommand(const std::string& command);
////////////////////////////////////////////////////////
// FTP Commands
////////////////////////////////////////////////////////
private:
// Access control commands
void handleFtpCommandUSER(const std::string& param);
void handleFtpCommandPASS(const std::string& param);
void handleFtpCommandACCT(const std::string& param);
void handleFtpCommandCWD(const std::string& param);
void handleFtpCommandCDUP(const std::string& param);
void handleFtpCommandREIN(const std::string& param);
void handleFtpCommandQUIT(const std::string& param);
// Transfer parameter commands
void handleFtpCommandPORT(const std::string& param);
void handleFtpCommandPASV(const std::string& param);
void handleFtpCommandTYPE(const std::string& param);
void handleFtpCommandSTRU(const std::string& param);
void handleFtpCommandMODE(const std::string& param);
// Ftp service commands
void handleFtpCommandRETR(const std::string& param);
void handleFtpCommandSIZE(const std::string& param);
void handleFtpCommandSTOR(const std::string& param);
void handleFtpCommandSTOU(const std::string& param);
void handleFtpCommandAPPE(const std::string& param);
void handleFtpCommandALLO(const std::string& param);
void handleFtpCommandREST(const std::string& param);
void handleFtpCommandRNFR(const std::string& param);
void handleFtpCommandRNTO(const std::string& param);
void handleFtpCommandABOR(const std::string& param);
void handleFtpCommandDELE(const std::string& param);
void handleFtpCommandRMD(const std::string& param);
void handleFtpCommandMKD(const std::string& param);
void handleFtpCommandPWD(const std::string& param);
void handleFtpCommandLIST(const std::string& param);
void handleFtpCommandNLST(const std::string& param);
void handleFtpCommandSITE(const std::string& param);
void handleFtpCommandSYST(const std::string& param);
void handleFtpCommandSTAT(const std::string& param);
void handleFtpCommandHELP(const std::string& param);
void handleFtpCommandNOOP(const std::string& param);
// Modern FTP Commands
void handleFtpCommandFEAT(const std::string& param);
void handleFtpCommandOPTS(const std::string& param);
////////////////////////////////////////////////////////
// FTP data-socket send
////////////////////////////////////////////////////////
private:
void sendDirectoryListing (const std::map<std::string, Filesystem::FileStatus>& directory_content);
void sendNameList (const std::map<std::string, Filesystem::FileStatus>& directory_content);
void sendFile (const std::shared_ptr<ReadableFile>& file);
void addDataToBufferAndSend (const std::shared_ptr<std::vector<char>>& data
, const std::shared_ptr<asio::ip::tcp::socket>& data_socket
, const std::function<void(void)>& fetch_more = []() {return; });
void writeDataToSocket (const std::shared_ptr<asio::ip::tcp::socket>& data_socket
, const std::function<void(void)>& fetch_more);
////////////////////////////////////////////////////////
// FTP data-socket receive
////////////////////////////////////////////////////////
private:
void receiveFile(const std::shared_ptr<WriteableFile>& file);
void receiveDataFromSocketAndWriteToFile(const std::shared_ptr<WriteableFile>& file
, const std::shared_ptr<asio::ip::tcp::socket>& data_socket);
void writeDataToFile(const std::shared_ptr<std::vector<char>>& data
, const std::shared_ptr<WriteableFile>& file
, const std::function<void(void)>& fetch_more = []() {return; });
void endDataReceiving(const std::shared_ptr<WriteableFile>& file);
////////////////////////////////////////////////////////
// Helpers
////////////////////////////////////////////////////////
private:
std::string toAbsoluteFtpPath(const std::string& rel_or_abs_ftp_path) const;
std::string toLocalPath(const std::string& ftp_path) const;
static std::string createQuotedFtpPath(const std::string& unquoted_ftp_path);
/** @brief Checks if a path is renamable
*
* Checks if the current user can rename the given path. A path is renameable
* if it exists, a user is logged in and the user has sufficient permissions
* (file / dir / both) to rename it.
*
* @param ftp_path: The source path
*
* @return (COMMAND_OK, "") if the path can be renamed or any other meaningfull error message if not.
*/
FtpMessage checkIfPathIsRenamable(const std::string& ftp_path) const;
FtpMessage executeCWD(const std::string& param);
#ifdef WIN32
/**
* @brief Returns Windows' GetLastError() as human readable string
* @return The message of the last error
*/
static std::string GetLastErrorStr();
#endif // WIN32
////////////////////////////////////////////////////////
// Member variables
////////////////////////////////////////////////////////
private:
// Completion handler
const std::function<void()> completion_handler_;
// User management
const UserDatabase& user_database_;
std::shared_ptr<FtpUser> logged_in_user_;
// "Global" io service
asio::io_service& io_service_;
// Command Socket
asio::ip::tcp::socket command_socket_;
asio::io_service::strand command_write_strand_;
asio::streambuf command_input_stream_;
std::deque<std::string> command_output_queue_;
std::string last_command_;
std::string rename_from_path_;
std::string username_for_login_;
// Data Socket (=> passive mode)
bool data_type_binary_;
asio::ip::tcp::acceptor data_acceptor_;
std::weak_ptr<asio::ip::tcp::socket> data_socket_weakptr_;
std::deque<std::shared_ptr<std::vector<char>>> data_buffer_;
asio::io_service::strand data_buffer_strand_;
asio::io_service::strand file_rw_strand_;
// Current state
std::string ftp_working_directory_;
};
}