-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHttpServer.h
160 lines (146 loc) · 5.74 KB
/
HttpServer.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
//
// Created by zerocool on 12/11/22.
//
#ifndef PROJECT_HTTPSERVER_H
#define PROJECT_HTTPSERVER_H
#include "served/multiplexer.hpp"
#include "served/net/server.hpp"
#include "MegaDownloader.h"
#include "json.hpp"
#include "log.h"
const char kAddDownloadEndpoint[] = "/adddownload";
const char kGetStatusEndpoint[] = "/getstatus";
const char kCancelDownloadEndpoint[] = "/canceldownload";
const char kLoginEndpoint[] = "/login";
const char kIPAddress[] = "0.0.0.0";
class HttpServer {
public:
HttpServer(MegaDownloader* downloader) : downloader(downloader){}
auto Login(MegaDownloader* downloader)
{
return [downloader](served::response & res, const served::request & req) {
json::JSON reqBody = json::JSON::Load(req.body());
std::string email = reqBody["email"].ToString();
std::string password = reqBody["password"].ToString();
VLOG_F(2, "Parsed email: %s | password: %s", email.c_str(), password.c_str());
json::JSON obj = json::Object();
std::ostringstream stream;
if (email == "" || password == "")
{
obj["error_code"] = 401;
obj["error_string"] = "credentials not properly provided";
res.set_status(401);
stream << obj;
res << stream.str();
} else {
auto resp = downloader->Login(email.c_str(), password.c_str());
obj["error_code"] = resp->errorCode;
obj["error_string"] = resp->errorString;
stream << obj;
res << stream.str();
}
};
}
auto AddDownload(MegaDownloader* downloader)
{
return [downloader](served::response & res, const served::request & req) {
json::JSON reqBody = json::JSON::Load(req.body());
std::string link = reqBody["link"].ToString();
std::string dir = reqBody["dir"].ToString();
json::JSON obj = json::Object();
std::ostringstream stream;
VLOG_F(2, "Parsed link: %s | dir: %s", link.c_str(), dir.c_str());
if (link == "" || dir == "")
{
obj["error_code"] = 401;
obj["error_string"] = "link or dir not provided.";
res.set_status(401);
stream << obj;
res << stream.str();
} else {
auto resp = downloader->AddDownload(link.c_str(), dir.c_str());
obj["gid"] = resp->gid;
obj["error_code"] = resp->errorCode;
obj["error_string"] = resp->errorString;
stream << obj;
res << stream.str();
}
};
}
auto CancelDownload(MegaDownloader* downloader)
{
return [downloader](served::response & res, const served::request & req) {
json::JSON reqBody = json::JSON::Load(req.body());
std::string gid = reqBody["gid"].ToString();
json::JSON obj = json::Object();
std::ostringstream stream;
LOG_F(INFO, "Parsed gid: %s", gid.c_str());
if (gid == "")
{
obj["error_code"] = 401;
obj["error_string"] = "gid not provided.";
res.set_status(401);
stream << obj;
res << stream.str();
} else {
auto code = downloader->CancelDownloadByGid(gid);
obj["gid"] = gid;
obj["error_code"] = code;
obj["error_string"] = "";
stream << obj;
res << stream.str();
}
};
}
auto GetStatus(MegaDownloader* downloader)
{
return [downloader](served::response & res, const served::request & req) {
json::JSON reqBody = json::JSON::Load(req.body());
std::string gid = reqBody["gid"].ToString();
LOG_F(INFO, "Parsed gid: %s", gid.c_str());
json::JSON obj = json::Object();
std::ostringstream stream;
obj["gid"] = gid;
auto dl = downloader->GetDownloadByGid(gid);
if (dl == nullptr) {
obj["error_code"] = 404;
obj["error_string"] = "gid not found in downloader";
res.set_status(404);
stream << obj;
res << stream.str();
return;
}
auto info = dl->ToDownloadInfo();
obj["error_code"] = info->errorCode;
obj["error_string"] = info->errorString;
obj["name"] = info->name;
obj["speed"] = info->speed;
obj["completed_length"] = info->completedLength;
obj["total_length"] = info->totalLength;
obj["state"] = info->state;
obj["is_completed"] = info->isCompleted;
obj["is_cancelled"] = info->isCancelled;
obj["is_failed"] = info->isFailed;
delete info;
stream << obj;
res << stream.str();
};
}
void InitEndpoints()
{
this->multiplexer.handle(kAddDownloadEndpoint).post(this->AddDownload(this->downloader));
this->multiplexer.handle(kLoginEndpoint).post(this->Login(this->downloader));
this->multiplexer.handle(kGetStatusEndpoint).post(this->GetStatus(this->downloader));
this->multiplexer.handle(kCancelDownloadEndpoint).post(this->CancelDownload(this->downloader));
}
void StartServer(const char* port, int threads)
{
served::net::server server(kIPAddress, port, this->multiplexer);
LOG_F(INFO, "Starting web server");
server.run(threads);
}
private:
served::multiplexer multiplexer;
MegaDownloader* downloader;
};
#endif //PROJECT_HTTPSERVER_H