2020-11-25 00:20:41 +01:00
|
|
|
#include "request.h"
|
|
|
|
|
2021-02-09 01:25:24 +01:00
|
|
|
#include "application.h"
|
|
|
|
|
2020-12-11 16:54:41 +01:00
|
|
|
void Request::compile_body() {
|
|
|
|
compiled_body.reserve(body.size() + head.size() + 13 + 14 + 15);
|
|
|
|
|
|
|
|
//13
|
|
|
|
compiled_body += "<html>"
|
2020-12-28 19:15:27 +01:00
|
|
|
"<head>";
|
2020-12-11 16:54:41 +01:00
|
|
|
|
|
|
|
compiled_body += head;
|
|
|
|
|
|
|
|
//14
|
|
|
|
compiled_body += "</head>"
|
2020-12-28 19:15:27 +01:00
|
|
|
"<body>";
|
2020-12-11 16:54:41 +01:00
|
|
|
|
|
|
|
compiled_body += body;
|
2020-12-26 00:17:36 +01:00
|
|
|
compiled_body += footer;
|
2020-12-11 16:54:41 +01:00
|
|
|
|
|
|
|
//15
|
|
|
|
compiled_body += "</body>"
|
2020-12-28 19:15:27 +01:00
|
|
|
"</html>";
|
2020-12-11 16:54:41 +01:00
|
|
|
|
|
|
|
response->setBody(compiled_body);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Request::compile_and_send_body() {
|
|
|
|
compile_body();
|
|
|
|
send();
|
|
|
|
}
|
|
|
|
|
2020-11-26 12:02:58 +01:00
|
|
|
void Request::next_stage() {
|
|
|
|
if (current_middleware_index == (*middleware_stack).size()) {
|
2020-12-01 12:56:04 +01:00
|
|
|
handler_instance.handler_func(handler_instance.instance, this);
|
2020-11-26 12:02:58 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-12-01 12:56:04 +01:00
|
|
|
const HandlerInstance &hi = (*middleware_stack)[current_middleware_index++];
|
|
|
|
|
|
|
|
hi.handler_func(hi.instance, this);
|
2020-11-26 12:02:58 +01:00
|
|
|
}
|
|
|
|
|
2020-11-26 11:26:02 +01:00
|
|
|
void Request::send() {
|
2021-04-28 21:48:34 +02:00
|
|
|
//if (connection_closed) {
|
|
|
|
// RequestPool::return_request(this);
|
|
|
|
// return;
|
|
|
|
//}
|
2021-02-09 01:25:24 +01:00
|
|
|
|
2020-11-26 11:26:02 +01:00
|
|
|
if (http_parser->isKeepAlive()) {
|
|
|
|
response->addHeadValue("Connection", "Keep-Alive");
|
|
|
|
|
|
|
|
std::string result = response->getResult();
|
|
|
|
|
2021-02-09 01:25:24 +01:00
|
|
|
session->send(result.c_str(), result.size());
|
2020-11-26 11:26:02 +01:00
|
|
|
} else {
|
|
|
|
response->addHeadValue("Connection", "Close");
|
|
|
|
|
|
|
|
std::string result = response->getResult();
|
|
|
|
|
2021-02-09 01:25:24 +01:00
|
|
|
HttpSession::Ptr lsession = session;
|
2020-11-26 11:26:02 +01:00
|
|
|
|
2021-02-09 01:25:24 +01:00
|
|
|
session->send(result.c_str(), result.size(), [lsession]() { lsession->postShutdown(); });
|
2020-11-26 11:26:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
RequestPool::return_request(this);
|
|
|
|
}
|
|
|
|
|
2021-01-08 02:55:41 +01:00
|
|
|
void Request::send_file(const std::string &p_file_path) {
|
2021-04-28 21:48:34 +02:00
|
|
|
//if (connection_closed) {
|
|
|
|
// RequestPool::return_request(this);
|
|
|
|
// return;
|
|
|
|
//}
|
2021-02-09 01:25:24 +01:00
|
|
|
|
2021-01-08 02:55:41 +01:00
|
|
|
file_path = p_file_path;
|
|
|
|
|
|
|
|
FILE *f = fopen(file_path.c_str(), "rb");
|
|
|
|
|
|
|
|
if (!f) {
|
|
|
|
printf("send_file: Error: Download: file doesn't exists! %s\n", file_path.c_str());
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fseek(f, 0, SEEK_END);
|
|
|
|
file_size = ftell(f);
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
response->addHeadValue("Connection", "Close");
|
|
|
|
std::string result = "HTTP/1.1 200 OK\r\nConnection: Close\r\n\r\n";
|
|
|
|
|
2021-02-09 01:25:24 +01:00
|
|
|
Application::register_request_update(this);
|
|
|
|
|
|
|
|
session->send(result.c_str(), result.size(), [this]() { this->_file_chunk_sent(); });
|
2021-01-08 02:55:41 +01:00
|
|
|
}
|
|
|
|
|
2020-11-25 00:20:41 +01:00
|
|
|
void Request::reset() {
|
|
|
|
http_parser = nullptr;
|
|
|
|
session = nullptr;
|
2020-11-26 12:02:58 +01:00
|
|
|
current_middleware_index = 0;
|
|
|
|
middleware_stack = nullptr;
|
2020-12-28 19:15:27 +01:00
|
|
|
_path_stack.clear();
|
|
|
|
_path_stack_pointer = 0;
|
2021-01-08 02:55:41 +01:00
|
|
|
file_size = 0;
|
|
|
|
current_file_progress = 0;
|
2021-02-09 01:25:24 +01:00
|
|
|
connection_closed = false;
|
2020-11-25 00:20:41 +01:00
|
|
|
|
2020-12-11 16:54:41 +01:00
|
|
|
head.clear();
|
|
|
|
body.clear();
|
2020-12-26 00:17:36 +01:00
|
|
|
footer.clear();
|
2020-12-11 16:54:41 +01:00
|
|
|
compiled_body.clear();
|
|
|
|
|
2020-11-25 00:20:41 +01:00
|
|
|
if (response)
|
|
|
|
delete response;
|
|
|
|
|
2021-02-08 11:01:15 +01:00
|
|
|
response = new HttpResponse();
|
2020-11-25 00:20:41 +01:00
|
|
|
}
|
|
|
|
|
2020-12-28 19:15:27 +01:00
|
|
|
void Request::setup_url_stack() {
|
|
|
|
std::string path = http_parser->getPath();
|
|
|
|
|
|
|
|
size_t pos = 0;
|
|
|
|
std::string st;
|
|
|
|
while ((pos = path.find("/")) != std::string::npos) {
|
|
|
|
st = path.substr(0, pos);
|
|
|
|
|
|
|
|
if (st.size() != 0)
|
|
|
|
_path_stack.push_back(st);
|
|
|
|
|
|
|
|
path.erase(0, pos + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (path.size() != 0)
|
|
|
|
_path_stack.push_back(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Request::get_path() const {
|
|
|
|
std::string path = "";
|
|
|
|
|
|
|
|
for (uint32_t i = _path_stack_pointer; i < _path_stack.size(); ++i) {
|
|
|
|
path += _path_stack[i];
|
|
|
|
path += "/";
|
|
|
|
}
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string &Request::get_path_full() const {
|
|
|
|
return http_parser->getPath();
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string &Request::get_path_segment(const uint32_t i) const {
|
|
|
|
return _path_stack[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string &Request::get_current_path_segment() const {
|
|
|
|
if (_path_stack_pointer >= _path_stack.size()) {
|
|
|
|
//for convenience
|
|
|
|
static const std::string e_str = "";
|
|
|
|
return e_str;
|
|
|
|
}
|
|
|
|
|
|
|
|
return _path_stack[_path_stack_pointer];
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t Request::get_path_segment_count() const {
|
|
|
|
return _path_stack.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t Request::get_current_segment_index() const {
|
|
|
|
return _path_stack_pointer;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t Request::get_remaining_segment_count() const {
|
|
|
|
if (_path_stack_pointer > _path_stack.size()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return _path_stack.size() - _path_stack_pointer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Request::pop_path() {
|
|
|
|
_path_stack_pointer -= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Request::push_path() {
|
|
|
|
_path_stack_pointer += 1;
|
|
|
|
}
|
|
|
|
|
2021-02-09 01:25:24 +01:00
|
|
|
void Request::update() {
|
|
|
|
if (file_next) {
|
|
|
|
file_next = false;
|
|
|
|
_progress_send_file();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-25 00:20:41 +01:00
|
|
|
Request::Request() {
|
2020-11-26 11:26:02 +01:00
|
|
|
response = nullptr;
|
2021-02-09 01:25:24 +01:00
|
|
|
|
|
|
|
//This value will need benchmarks, 2 MB seems to be just as fast for me as 4 MB, but 1MB is slower
|
|
|
|
//It is a tradeoff on server memory though, as every active download will consume this amount of memory
|
|
|
|
//where the file is bigger than this number
|
|
|
|
file_chunk_size = 1 << 21; //2MB
|
2020-11-25 00:20:41 +01:00
|
|
|
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
Request::~Request() {
|
|
|
|
delete response;
|
|
|
|
}
|
|
|
|
|
2021-01-08 02:55:41 +01:00
|
|
|
void Request::_progress_send_file() {
|
2021-02-09 01:25:24 +01:00
|
|
|
if (connection_closed) {
|
|
|
|
RequestPool::return_request(this);
|
|
|
|
return;
|
|
|
|
}
|
2021-01-08 02:55:41 +01:00
|
|
|
|
|
|
|
if (current_file_progress >= file_size) {
|
2021-02-09 01:25:24 +01:00
|
|
|
session->postShutdown();
|
2021-01-08 02:55:41 +01:00
|
|
|
|
|
|
|
RequestPool::return_request(this);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE *f = fopen(file_path.c_str(), "rb");
|
|
|
|
|
|
|
|
if (!f) {
|
|
|
|
printf("Error: Download: In progress file doesn't exists anymore! %s\n", file_path.c_str());
|
|
|
|
|
2021-02-09 01:25:24 +01:00
|
|
|
Application::unregister_request_update(this);
|
|
|
|
|
|
|
|
session->postShutdown();
|
2021-01-08 02:55:41 +01:00
|
|
|
|
|
|
|
RequestPool::return_request(this);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fseek(f, current_file_progress, SEEK_SET);
|
|
|
|
|
|
|
|
long nfp = current_file_progress + file_chunk_size;
|
|
|
|
|
|
|
|
long csize = file_chunk_size;
|
|
|
|
if (nfp >= file_size) {
|
|
|
|
csize = (file_size - current_file_progress);
|
|
|
|
}
|
|
|
|
|
|
|
|
body.resize(csize);
|
|
|
|
|
|
|
|
fread(&body[0], 1, csize, f);
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
current_file_progress = nfp;
|
|
|
|
|
2021-02-09 01:25:24 +01:00
|
|
|
session->send(body.c_str(), body.size(), [this]() { this->_file_chunk_sent(); });
|
|
|
|
}
|
|
|
|
|
|
|
|
void Request::_file_chunk_sent() {
|
|
|
|
file_next = true;
|
2021-01-08 02:55:41 +01:00
|
|
|
}
|
|
|
|
|
2020-11-25 00:20:41 +01:00
|
|
|
Request *RequestPool::get_request() {
|
|
|
|
_mutex.lock();
|
|
|
|
|
|
|
|
Request *request;
|
|
|
|
|
|
|
|
if (_requests.size() == 0) {
|
|
|
|
_mutex.unlock();
|
|
|
|
|
|
|
|
request = new Request();
|
|
|
|
|
|
|
|
return request;
|
|
|
|
}
|
|
|
|
|
|
|
|
request = _requests[_requests.size() - 1];
|
|
|
|
_requests.pop_back();
|
|
|
|
|
|
|
|
_mutex.unlock();
|
|
|
|
|
2021-02-09 01:25:24 +01:00
|
|
|
request->reset();
|
|
|
|
|
2020-11-25 00:20:41 +01:00
|
|
|
return request;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RequestPool::return_request(Request *request) {
|
2020-11-26 11:26:02 +01:00
|
|
|
_mutex.lock();
|
2020-11-25 00:20:41 +01:00
|
|
|
_requests.push_back(request);
|
2020-11-26 11:26:02 +01:00
|
|
|
_mutex.unlock();
|
2020-11-25 00:20:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
RequestPool::RequestPool() {
|
|
|
|
}
|
|
|
|
|
|
|
|
RequestPool::~RequestPool() {
|
|
|
|
for (uint32_t i = 0; i < _requests.size(); ++i) {
|
|
|
|
delete _requests[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
_requests.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::mutex RequestPool::_mutex;
|
|
|
|
std::vector<Request *> RequestPool::_requests;
|