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 18:59:30 +02:00
|
|
|
//response->setExpiredTime(0);
|
|
|
|
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) {
|
|
|
|
//if (connection_closed) {
|
|
|
|
// DRequestPool::return_request(this);
|
|
|
|
// return;
|
|
|
|
//}
|
2021-06-20 18:41:09 +02:00
|
|
|
/*
|
2021-06-20 17:53:07 +02: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";
|
|
|
|
|
|
|
|
application->register_request_update(this);
|
|
|
|
|
|
|
|
session->send(result.c_str(), result.size(), [this]() { this->_file_chunk_sent(); });
|
2021-06-20 18:41:09 +02:00
|
|
|
*/
|
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::reset() {
|
|
|
|
application = nullptr;
|
|
|
|
current_middleware_index = 0;
|
|
|
|
middleware_stack = nullptr;
|
|
|
|
_path_stack.clear();
|
|
|
|
_path_stack_pointer = 0;
|
|
|
|
file_size = 0;
|
|
|
|
current_file_progress = 0;
|
|
|
|
connection_closed = false;
|
|
|
|
|
|
|
|
head.clear();
|
|
|
|
body.clear();
|
|
|
|
footer.clear();
|
|
|
|
compiled_body.clear();
|
|
|
|
|
2021-07-06 16:57:21 +02:00
|
|
|
response.reset();
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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 20:15:36 +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;
|