-
Notifications
You must be signed in to change notification settings - Fork 5
/
FileServerRequestHandler.cpp
209 lines (191 loc) · 6.43 KB
/
FileServerRequestHandler.cpp
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
//
// Created by victor on 11/17/17.
//
#include <folly/File.h>
#include <folly/FileUtil.h>
#include <folly/executors/GlobalExecutor.h>
#include <folly/io/async/EventBaseManager.h>
#include <folly/String.h>
#include <proxygen/httpserver/ResponseBuilder.h>
#include <proxygen/httpserver/RequestHandler.h>
#include <boost/algorithm/string.hpp>
#include "FileServerRequestHandler.h"
#include "RestDbConfiguration.h"
#include <boost/filesystem.hpp>
using proxygen::ResponseBuilder;
using proxygen::HTTPMethod;
namespace restdbxx {
void FileServerRequestHandler::onRequest(std::unique_ptr<proxygen::HTTPMessage> headers) noexcept {
if (headers->getMethod() != HTTPMethod::GET) {
ResponseBuilder(downstream_)
.status(400, "Bad method")
.body("Only GET is supported")
.sendWithEOM();
return;
}
// a real webserver would validate this path didn't contain malicious
// characters like '//' or '..'
try {
// + 1 to kill leading /
std::string path = folly::uriUnescape<std::string>(headers->getPath());
boost::algorithm::erase_first(path, path_prefix);
real_path = root + path;
file_ = std::make_unique<folly::File>(real_path.c_str());
} catch (const std::system_error &ex) {
ResponseBuilder(downstream_)
.status(404, "Not Found")
.body(folly::to<std::string>("Could not find ", headers->getPath(),
" ex=", folly::exceptionStr(ex)))
.sendWithEOM();
return;
}
ResponseBuilder(downstream_)
.status(200, "Ok")
.send();
bool is_dir = boost::filesystem::is_directory(real_path);
if (is_dir) {
readFileScheduled_ = true;
folly::getCPUExecutor()->add(
std::bind(&FileServerRequestHandler::listFiles, this,
folly::EventBaseManager::get()->getEventBase())
);
return;
}
//if (file_)
// use a CPU executor since read(2) of a file can block
readFileScheduled_ = true;
folly::getCPUExecutor()->add(
std::bind(&FileServerRequestHandler::readFile, this,
folly::EventBaseManager::get()->getEventBase()));
}
void FileServerRequestHandler::onBody(std::unique_ptr<folly::IOBuf> body) noexcept {
}
void FileServerRequestHandler::onUpgrade(proxygen::UpgradeProtocol prot) noexcept {
}
void FileServerRequestHandler::onEOM() noexcept {
}
void FileServerRequestHandler::requestComplete()noexcept {
finished_ = true;
paused_ = true;
checkForCompletion();
}
void FileServerRequestHandler::onError(proxygen::ProxygenError err) noexcept {
finished_ = true;
paused_ = true;
checkForCompletion();
}
void FileServerRequestHandler::readFile(folly::EventBase *evb) {
folly::IOBufQueue buf;
while (file_ && !paused_) {
// read 4k-ish chunks and foward each one to the client
auto data = buf.preallocate(4000, 4000);
auto rc = folly::readNoInt(file_->fd(), data.first, data.second);
if (rc < 0) {
// error
VLOG(4) << "Read error=" << rc;
file_.reset();
evb->runInEventBaseThread([this] {
LOG(ERROR) << "Error reading file";
downstream_->sendAbort();
});
break;
} else if (rc == 0) {
// done
file_.reset();
VLOG(4) << "Read EOF";
evb->runInEventBaseThread([this] {
ResponseBuilder(downstream_)
.sendWithEOM();
});
break;
} else {
buf.postallocate(rc);
evb->runInEventBaseThread([this, body = buf.move()]() mutable {
ResponseBuilder(downstream_)
.body(std::move(body))
.send();
});
}
}
// Notify the request thread that we terminated the readFile loop
evb->runInEventBaseThread([this] {
readFileScheduled_ = false;
if (!checkForCompletion() && !paused_) {
VLOG(4) << "Resuming deferred readFile";
onEgressResumed();
}
});
}
bool FileServerRequestHandler::checkForCompletion() {
if (finished_ && !readFileScheduled_) {
VLOG(4) << "deleting StaticHandler";
delete this;
return true;
}
return false;
}
void FileServerRequestHandler::onEgressPaused() noexcept {
// This will terminate readFile soon
VLOG(4) << "FileServerRequestHandler paused";
paused_ = true;
}
void FileServerRequestHandler::onEgressResumed() noexcept {
VLOG(4) << "FileServerRequestHandler resumed";
paused_ = false;
// If readFileScheduled_, it will reschedule itself
if (!readFileScheduled_ && file_) {
readFileScheduled_ = true;
folly::getCPUExecutor()->add(
std::bind(&FileServerRequestHandler::readFile, this,
folly::EventBaseManager::get()->getEventBase()));
} else {
VLOG(4) << "Deferred scheduling readFile";
}
}
FileServerRequestHandler::FileServerRequestHandler(const std::string &path_prefix, const std::string &root)
: path_prefix(path_prefix), root(root) {}
void FileServerRequestHandler::listFiles(folly::EventBase *evb) {
namespace fs = boost::filesystem;
fs::path p(real_path);
std::stringstream ss;
ss << "<html>"
"<head>"
"<title>file list</title>"
"</head>"
"<body>"
"<h1>Listing for " << real_path << "</h1>"
"<ul>";
// std::stringstream ss;
// ss << "//" << path_prefix
for (fs::directory_entry &entry: fs::directory_iterator(p)) {
std::string url = entry.path().string();
boost::erase_first(url, root);
url = path_prefix + url;
ss << "<li><a href='" << url << "'>" << entry.path().filename().string()
<< (boost::filesystem::is_directory(entry.path()) ? "/" : "") << "</a></li>";
}
ss << "</ul>"
"</body>"
"</html>";
std::string html = ss.str();
evb->runInEventBaseThread([data = std::move(html), this]() mutable {
proxygen::ResponseBuilder(downstream_)
.body(std::move(data))
.sendWithEOM();
finished_ = true;
});
}
void FileServerRequestHandlerFactory::onServerStart(folly::EventBase *evb) noexcept {
}
void FileServerRequestHandlerFactory::onServerStop() noexcept {
}
proxygen::RequestHandler *FileServerRequestHandlerFactory::onRequest(proxygen::RequestHandler *handler,
proxygen::HTTPMessage *message) noexcept {
if (boost::algorithm::starts_with(message->getPath(), path_prefix))
return new FileServerRequestHandler(path_prefix, root);
return handler;
}
FileServerRequestHandlerFactory::FileServerRequestHandlerFactory(const std::string &path_prefix,
const std::string &root)
: path_prefix(path_prefix), root(root) {}
}