2020-11-25 00:20:41 +01:00
|
|
|
#include "request.h"
|
|
|
|
|
2021-08-04 15:36:26 +02:00
|
|
|
#include "core/http/cookie.h"
|
2021-05-27 17:37:23 +02:00
|
|
|
#include "web_application.h"
|
2021-02-09 01:25:24 +01:00
|
|
|
|
2021-08-04 20:33:21 +02:00
|
|
|
#include "http_session.h"
|
|
|
|
|
2021-08-05 19:34:11 +02:00
|
|
|
#include "session_manager.h"
|
|
|
|
|
|
|
|
HTTPSession *Request::get_or_create_session() {
|
|
|
|
if (session) {
|
|
|
|
return session;
|
|
|
|
}
|
|
|
|
|
|
|
|
session = SessionManager::get_singleton()->create_session();
|
|
|
|
|
|
|
|
return session;
|
|
|
|
}
|
|
|
|
|
2021-11-01 19:11:27 +01:00
|
|
|
const String Request::get_cookie(const String &key) {
|
|
|
|
static String str(0);
|
2021-08-22 23:01:39 +02:00
|
|
|
return str;
|
2021-08-04 15:36:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Request::add_cookie(const ::Cookie &cookie) {
|
|
|
|
}
|
|
|
|
|
2021-11-01 19:11:27 +01:00
|
|
|
void Request::remove_cookie(const String &key) {
|
2021-08-04 15:36:26 +02:00
|
|
|
}
|
|
|
|
|
2021-08-05 17:11:13 +02:00
|
|
|
HTTPMethod Request::get_method() const {
|
|
|
|
return HTTP_METHOD_GET;
|
|
|
|
}
|
|
|
|
|
2021-11-01 19:11:27 +01:00
|
|
|
const String Request::get_parameter(const String &key) const {
|
|
|
|
static String str(0);
|
2021-08-22 22:17:32 +02:00
|
|
|
return str;
|
2021-08-05 18:24:55 +02:00
|
|
|
}
|
|
|
|
|
2021-11-01 19:11:27 +01:00
|
|
|
void Request::send_redirect(const String &location, const HTTPStatusCode status_code) {
|
2021-10-31 02:50:06 +02:00
|
|
|
}
|
|
|
|
|
2020-12-11 16:54:41 +01:00
|
|
|
void Request::compile_body() {
|
2021-11-01 19:11:27 +01:00
|
|
|
compiled_body.ensure_capacity(body.size() + head.size() + 15 + 13 + 14 + 15 + 1);
|
2021-10-30 23:32:06 +02:00
|
|
|
|
|
|
|
//15
|
|
|
|
compiled_body += "<!DOCTYPE html>";
|
2020-12-11 16:54:41 +01:00
|
|
|
|
|
|
|
//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
|
|
|
|
2021-07-06 20:06:38 +02:00
|
|
|
//response->setBody(compiled_body);
|
2020-12-11 16:54:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Request::compile_and_send_body() {
|
|
|
|
compile_body();
|
|
|
|
send();
|
|
|
|
}
|
|
|
|
|
2020-11-26 12:02:58 +01:00
|
|
|
void Request::next_stage() {
|
2021-07-06 22:06:18 +02:00
|
|
|
//if (middleware_stack == nullptr) {
|
|
|
|
// printf("Error Request::next_stage-> middleware_stack == nullptr\n");
|
|
|
|
//}
|
|
|
|
|
2020-11-26 12:02:58 +01:00
|
|
|
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
|
|
|
|
2021-07-06 20:06:38 +02:00
|
|
|
//RequestPool::return_request(this);
|
2020-11-26 11:26:02 +01:00
|
|
|
}
|
|
|
|
|
2021-11-01 19:11:27 +01:00
|
|
|
void Request::send_file(const String &p_file_path) {
|
2021-07-06 20:06:38 +02:00
|
|
|
//RequestPool::return_request(this);
|
2021-01-08 02:55:41 +01:00
|
|
|
}
|
|
|
|
|
2021-04-28 22:50:05 +02:00
|
|
|
void Request::send_error(int error_code) {
|
|
|
|
application->send_error(error_code, this);
|
|
|
|
}
|
|
|
|
|
2020-11-25 00:20:41 +01:00
|
|
|
void Request::reset() {
|
2021-08-04 20:33:21 +02:00
|
|
|
session = nullptr;
|
2021-04-28 22:50:05 +02:00
|
|
|
application = 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;
|
2021-07-06 20:23:13 +02:00
|
|
|
_full_path = "";
|
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();
|
2021-08-04 15:44:52 +02:00
|
|
|
|
|
|
|
data.clear();
|
2021-08-22 22:17:32 +02:00
|
|
|
reference_data.clear();
|
2021-07-06 20:06:38 +02:00
|
|
|
}
|
2020-12-11 16:54:41 +01:00
|
|
|
|
2021-11-01 19:11:27 +01:00
|
|
|
String Request::parser_get_path() {
|
2021-07-06 20:06:38 +02:00
|
|
|
return "";
|
2020-11-25 00:20:41 +01:00
|
|
|
}
|
|
|
|
|
2020-12-28 19:15:27 +01:00
|
|
|
void Request::setup_url_stack() {
|
2021-07-06 20:06:38 +02:00
|
|
|
_full_path = parser_get_path();
|
2021-11-01 19:11:27 +01:00
|
|
|
String path = parser_get_path();
|
2020-12-28 19:15:27 +01:00
|
|
|
|
|
|
|
size_t pos = 0;
|
2021-11-01 19:11:27 +01:00
|
|
|
String st;
|
|
|
|
while ((pos = path.find('/')) != -1) {
|
2020-12-28 19:15:27 +01:00
|
|
|
st = path.substr(0, pos);
|
|
|
|
|
2021-11-01 19:11:27 +01:00
|
|
|
if (st.size() != 0) {
|
2020-12-28 19:15:27 +01:00
|
|
|
_path_stack.push_back(st);
|
2021-11-01 19:11:27 +01:00
|
|
|
}
|
2020-12-28 19:15:27 +01:00
|
|
|
|
|
|
|
path.erase(0, pos + 1);
|
|
|
|
}
|
|
|
|
|
2021-11-01 19:11:27 +01:00
|
|
|
if (path.size() != 0) {
|
2020-12-28 19:15:27 +01:00
|
|
|
_path_stack.push_back(path);
|
2021-11-01 19:11:27 +01:00
|
|
|
}
|
2020-12-28 19:15:27 +01:00
|
|
|
}
|
|
|
|
|
2021-11-01 19:11:27 +01:00
|
|
|
String Request::get_path() const {
|
|
|
|
String path = "";
|
2020-12-28 19:15:27 +01:00
|
|
|
|
|
|
|
for (uint32_t i = _path_stack_pointer; i < _path_stack.size(); ++i) {
|
|
|
|
path += _path_stack[i];
|
|
|
|
path += "/";
|
|
|
|
}
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2021-11-01 19:11:27 +01:00
|
|
|
const String &Request::get_path_full() const {
|
2021-07-06 20:06:38 +02:00
|
|
|
return _full_path;
|
2020-12-28 19:15:27 +01:00
|
|
|
}
|
|
|
|
|
2021-11-01 19:11:27 +01:00
|
|
|
const String &Request::get_path_segment(const uint32_t i) const {
|
2020-12-28 19:15:27 +01:00
|
|
|
return _path_stack[i];
|
|
|
|
}
|
|
|
|
|
2021-11-01 19:11:27 +01:00
|
|
|
const String &Request::get_current_path_segment() const {
|
2020-12-28 19:15:27 +01:00
|
|
|
if (_path_stack_pointer >= _path_stack.size()) {
|
|
|
|
//for convenience
|
2021-11-01 19:11:27 +01:00
|
|
|
static const String e_str = "";
|
2020-12-28 19:15:27 +01:00
|
|
|
return e_str;
|
|
|
|
}
|
|
|
|
|
|
|
|
return _path_stack[_path_stack_pointer];
|
|
|
|
}
|
|
|
|
|
2021-11-02 12:25:04 +01:00
|
|
|
const String &Request::get_next_path_segment() const {
|
|
|
|
int pst = _path_stack_pointer + 1;
|
|
|
|
|
|
|
|
if (pst >= _path_stack.size()) {
|
|
|
|
//for convenience
|
|
|
|
static const String e_str = "";
|
|
|
|
return e_str;
|
|
|
|
}
|
|
|
|
|
|
|
|
return _path_stack[pst];
|
|
|
|
}
|
|
|
|
|
2020-12-28 19:15:27 +01:00
|
|
|
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-11-02 00:18:57 +01:00
|
|
|
String Request::get_url_root_parent(const int parent) const {
|
2021-11-01 20:57:43 +01:00
|
|
|
String path = "/";
|
|
|
|
|
2021-11-02 00:18:57 +01:00
|
|
|
for (uint32_t i = 0; i < _path_stack_pointer - parent; ++i) {
|
2021-11-01 20:57:43 +01:00
|
|
|
path += _path_stack[i];
|
|
|
|
path += "/";
|
|
|
|
}
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2021-11-01 19:11:27 +01:00
|
|
|
String Request::get_url_root() const {
|
2021-11-02 12:25:04 +01:00
|
|
|
int pst = _path_stack_pointer + 1;
|
|
|
|
|
|
|
|
if (pst > _path_stack.size()) {
|
|
|
|
pst = _path_stack.size();
|
|
|
|
}
|
|
|
|
|
2021-11-01 19:11:27 +01:00
|
|
|
String path = "/";
|
2021-11-01 00:38:26 +01:00
|
|
|
|
|
|
|
for (uint32_t i = 0; i < _path_stack_pointer; ++i) {
|
|
|
|
path += _path_stack[i];
|
|
|
|
path += "/";
|
|
|
|
}
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2021-11-02 12:25:04 +01:00
|
|
|
String Request::get_url_root_current() const {
|
|
|
|
int pst = _path_stack_pointer + 1;
|
|
|
|
|
|
|
|
if (pst > _path_stack.size()) {
|
|
|
|
pst = _path_stack.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
String path = "/";
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < pst; ++i) {
|
|
|
|
path += _path_stack[i];
|
|
|
|
path += "/";
|
|
|
|
}
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2021-11-01 19:11:27 +01:00
|
|
|
String Request::get_url_site() const {
|
|
|
|
String path = get_host();
|
2021-11-01 00:38:26 +01:00
|
|
|
|
|
|
|
for (uint32_t i = _path_stack_pointer; i < _path_stack.size(); ++i) {
|
|
|
|
path += _path_stack[i];
|
|
|
|
path += "/";
|
|
|
|
}
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2021-11-01 21:20:42 +01:00
|
|
|
String Request::get_url_root_parent(const String &add) const {
|
|
|
|
return get_url_root_parent() + add;
|
|
|
|
}
|
|
|
|
String Request::get_url_root(const String &add) const {
|
|
|
|
return get_url_root() + add;
|
|
|
|
}
|
|
|
|
String Request::get_url_site(const String &add) const {
|
|
|
|
return get_url_site() + add;
|
|
|
|
}
|
|
|
|
|
2021-11-01 19:11:27 +01:00
|
|
|
String Request::get_host() const {
|
2021-11-01 00:38:26 +01:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2021-02-09 01:25:24 +01:00
|
|
|
void Request::update() {
|
|
|
|
}
|
|
|
|
|
2021-07-06 20:06:38 +02:00
|
|
|
void Request::pool() {
|
|
|
|
}
|
2021-02-09 01:25:24 +01:00
|
|
|
|
2021-07-06 20:06:38 +02:00
|
|
|
Request::Request() {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
Request::~Request() {
|
2021-02-09 01:25:24 +01:00
|
|
|
}
|