2021-06-20 17:53:07 +02:00
|
|
|
#include "request.h"
|
|
|
|
|
|
|
|
#include "web_application.h"
|
|
|
|
|
|
|
|
void DRequest::send() {
|
|
|
|
//if (connection_closed) {
|
|
|
|
// DRequestPool::return_request(this);
|
|
|
|
// return;
|
|
|
|
//}
|
|
|
|
|
2021-07-06 22:20:07 +02:00
|
|
|
HttpResponsePtr response = HttpResponse::newHttpResponse();
|
|
|
|
|
2021-07-06 22:06:18 +02:00
|
|
|
response->setBody(compiled_body);
|
|
|
|
|
|
|
|
response->setExpiredTime(0);
|
2021-07-06 18:59:30 +02:00
|
|
|
callback(response);
|
2021-06-20 17:53:07 +02:00
|
|
|
|
2021-07-06 20:06:38 +02:00
|
|
|
pool();
|
2021-06-20 17:53:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void DRequest::send_file(const std::string &p_file_path) {
|
2021-07-06 22:20:38 +02:00
|
|
|
HttpResponsePtr response = HttpResponse::newFileResponse(p_file_path, "", drogon::getContentType(p_file_path));
|
2021-06-20 17:53:07 +02:00
|
|
|
|
2021-07-06 22:20:07 +02:00
|
|
|
callback(response);
|
2021-07-06 22:06:18 +02:00
|
|
|
|
2021-07-06 20:06:38 +02:00
|
|
|
pool();
|
2021-06-20 17:53:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void DRequest::reset() {
|
2021-07-06 20:23:13 +02:00
|
|
|
Request::reset();
|
2021-06-20 17:53:07 +02:00
|
|
|
|
2021-07-06 16:57:21 +02:00
|
|
|
request.reset();
|
2021-06-20 17:53:07 +02:00
|
|
|
|
2021-06-20 18:41:09 +02:00
|
|
|
//response = new HttpResponse();
|
2021-06-20 17:53:07 +02:00
|
|
|
}
|
|
|
|
|
2021-07-06 22:06:18 +02:00
|
|
|
std::string DRequest::parser_get_path() {
|
|
|
|
return request->getPath();
|
|
|
|
}
|
|
|
|
|
2021-06-20 17:53:07 +02:00
|
|
|
void DRequest::update() {
|
|
|
|
if (file_next) {
|
|
|
|
file_next = false;
|
|
|
|
_progress_send_file();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-06 20:06:38 +02:00
|
|
|
DRequest *DRequest::get() {
|
|
|
|
return _request_pool.get_request();
|
|
|
|
}
|
|
|
|
void DRequest::pool(DRequest *request) {
|
|
|
|
return _request_pool.return_request(request);
|
|
|
|
}
|
|
|
|
void DRequest::pool() {
|
|
|
|
DRequest::pool(this);
|
|
|
|
}
|
|
|
|
|
2021-07-06 22:20:38 +02:00
|
|
|
DRequest::DRequest() :
|
|
|
|
Request() {
|
2021-06-20 17:53:07 +02: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
|
|
|
|
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
DRequest::~DRequest() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void DRequest::_progress_send_file() {
|
2021-06-20 18:41:09 +02:00
|
|
|
/*
|
2021-06-20 17:53:07 +02:00
|
|
|
if (connection_closed) {
|
|
|
|
DRequestPool::return_request(this);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (current_file_progress >= file_size) {
|
|
|
|
session->postShutdown();
|
|
|
|
|
|
|
|
DRequestPool::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());
|
|
|
|
|
|
|
|
application->unregister_request_update(this);
|
|
|
|
|
|
|
|
session->postShutdown();
|
|
|
|
|
|
|
|
DRequestPool::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;
|
|
|
|
|
|
|
|
session->send(body.c_str(), body.size(), [this]() { this->_file_chunk_sent(); });
|
2021-06-20 18:41:09 +02:00
|
|
|
*/
|
2021-06-20 17:53:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void DRequest::_file_chunk_sent() {
|
|
|
|
file_next = true;
|
|
|
|
}
|
|
|
|
|
2021-07-06 20:06:38 +02:00
|
|
|
RequestPool<DRequest> DRequest::_request_pool;
|