-
Notifications
You must be signed in to change notification settings - Fork 6
/
ftp_session.hpp
93 lines (74 loc) · 2.85 KB
/
ftp_session.hpp
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
#pragma once
#include "yasio/yasio.hpp"
#include "fsutils.hpp"
#include <unordered_set>
#include <unordered_map>
using namespace yasio;
typedef uint32_t ftp_cmd_id_t;
class ftp_server;
class ftp_session : public std::enable_shared_from_this<ftp_session>
{
friend class ftp_server;
enum class transfer_status
{
NONE = 0,
LIST = 1,
FILE,
};
public:
/// <summary>
/// ftp_session constructor
/// </summary>
/// <param name="server">ftp_server</param>
/// <param name="thandle">transport handle for control commands</param>
/// <param name="transfer_cindex">channel index to transfer data</param>
ftp_session(ftp_server& server, transport_handle_t thandle, int transfer_cindex);
~ftp_session();
// say hello to client, we can start ftp service
void say_hello();
void start_exprie_timer();
timer_cb_t create_timer_cb();
void handle_packet(yasio::io_packet& packet);
void stock_reply(cxx17::string_view code, cxx17::string_view resp_data, bool finished = true,
bool ispath = false);
void open_transimt_session(transport_handle_t thandle);
/// ---------- All supported commands handlers ------------
void process_USER(const std::string& param);
void process_PASS(const std::string& param);
void process_SYST(const std::string& param);
void process_PWD(const std::string& param);
void process_TYPE(const std::string& param);
void process_SIZE(const std::string& param);
void process_CDUP(const std::string& /*param*/);
void process_CWD(const std::string& param);
void process_PASV(const std::string& param);
void process_EPSV(const std::string& /*param*/);
void process_LIST(const std::string& param);
void process_RETR(const std::string& param);
void process_QUIT(const std::string& param);
void process_AUTH(const std::string& param);
void process_OPTS(const std::string& param);
void process_FEAT(const std::string& param);
// register all supported commands' handler
static void register_handlers_once();
static void register_handler(std::string cmd,
std::function<void(ftp_session*, const std::string&)> handler);
void do_transmit();
const std::string& to_fspath();
const std::string& to_fspath(const std::string& param);
private:
ftp_server& server_;
transport_handle_t thandle_ctl_;
transport_handle_t thandle_transfer_;
transfer_status status_;
int transfer_cindex_;
// web working directory, must be always ends with '/'
std::string dir_;
// cache filesystem path for LIST and FILE transfer
std::string fspath_;
deadline_timer_ptr expire_timer_;
bool transferring_;
static std::unordered_map<ftp_cmd_id_t, std::function<void(ftp_session*, const std::string&)>>
handlers_;
};
typedef std::shared_ptr<ftp_session> ftp_session_ptr;