2020-11-25 00:20:41 +01:00
|
|
|
#ifndef REQUEST_H
|
|
|
|
#define REQUEST_H
|
|
|
|
|
2021-11-01 19:11:27 +01:00
|
|
|
#include "core/containers/vector.h"
|
|
|
|
#include "core/string.h"
|
|
|
|
|
2021-08-05 18:24:55 +02:00
|
|
|
#include <map>
|
2020-11-25 00:20:41 +01:00
|
|
|
#include <mutex>
|
2022-02-10 16:17:31 +01:00
|
|
|
#include <vector>
|
2021-08-04 15:44:52 +02:00
|
|
|
|
|
|
|
#include "core/object.h"
|
2021-08-22 22:17:32 +02:00
|
|
|
#include "core/reference.h"
|
2020-11-25 00:20:41 +01:00
|
|
|
|
2021-08-05 17:11:13 +02:00
|
|
|
#include "http_enums.h"
|
|
|
|
|
2022-01-07 20:11:06 +01:00
|
|
|
class WebServer;
|
2021-08-04 15:36:26 +02:00
|
|
|
class Cookie;
|
2021-08-04 20:33:21 +02:00
|
|
|
class HTTPSession;
|
2022-02-10 16:17:31 +01:00
|
|
|
class WebPermission;
|
2021-04-28 22:50:05 +02:00
|
|
|
|
2020-11-25 00:20:41 +01:00
|
|
|
class Request {
|
|
|
|
public:
|
2022-01-07 20:11:06 +01:00
|
|
|
WebServer *server;
|
2020-11-25 00:20:41 +01:00
|
|
|
|
2021-11-01 19:11:27 +01:00
|
|
|
String head;
|
|
|
|
String body;
|
|
|
|
String footer;
|
|
|
|
String compiled_body;
|
2020-12-11 16:54:41 +01:00
|
|
|
|
2021-11-01 19:11:27 +01:00
|
|
|
String file_path;
|
2021-01-08 02:55:41 +01:00
|
|
|
long file_size;
|
|
|
|
long current_file_progress;
|
|
|
|
long file_chunk_size;
|
|
|
|
bool file_next;
|
|
|
|
|
2021-02-09 01:25:24 +01:00
|
|
|
bool connection_closed;
|
|
|
|
|
2022-01-09 14:32:09 +01:00
|
|
|
Ref<HTTPSession> session;
|
2021-11-01 19:11:27 +01:00
|
|
|
std::map<String, Object *> data;
|
|
|
|
std::map<String, Ref<Reference> > reference_data;
|
2021-08-04 15:44:52 +02:00
|
|
|
|
2022-01-09 14:32:09 +01:00
|
|
|
Ref<HTTPSession> get_or_create_session();
|
2022-02-10 16:17:31 +01:00
|
|
|
|
|
|
|
Ref<WebPermission> active_permission;
|
|
|
|
int permissions;
|
|
|
|
|
|
|
|
bool can_view() const;
|
|
|
|
bool can_create() const;
|
|
|
|
bool can_edit() const;
|
|
|
|
bool can_delete() const;
|
|
|
|
|
2022-01-09 15:00:59 +01:00
|
|
|
bool has_csrf_token();
|
2022-01-09 14:51:04 +01:00
|
|
|
String get_csrf_token();
|
2022-01-09 15:25:55 +01:00
|
|
|
void set_csrf_token(const String &value);
|
|
|
|
bool validate_csrf_token();
|
2021-08-05 19:34:11 +02:00
|
|
|
|
2021-11-01 19:11:27 +01:00
|
|
|
virtual const String get_cookie(const String &key);
|
2021-08-04 15:36:26 +02:00
|
|
|
virtual void add_cookie(const ::Cookie &cookie);
|
2021-11-01 19:11:27 +01:00
|
|
|
virtual void remove_cookie(const String &key);
|
2021-08-04 15:36:26 +02:00
|
|
|
|
2021-08-05 17:11:13 +02:00
|
|
|
virtual HTTPMethod get_method() const;
|
|
|
|
|
2022-04-30 16:59:18 +02:00
|
|
|
virtual void parse_files();
|
|
|
|
virtual int get_file_count() const;
|
|
|
|
virtual int get_file_length(const int index) const;
|
|
|
|
virtual const uint8_t *get_file_data(const int index) const;
|
|
|
|
|
2021-11-01 19:11:27 +01:00
|
|
|
virtual const String get_parameter(const String &key) const;
|
2021-08-05 18:24:55 +02:00
|
|
|
|
2022-01-08 11:42:57 +01:00
|
|
|
HTTPStatusCode get_status_code() const;
|
|
|
|
void set_status_code(const HTTPStatusCode status_code);
|
|
|
|
|
2021-11-01 19:11:27 +01:00
|
|
|
virtual void send_redirect(const String &location, const HTTPStatusCode status_code = HTTP_STATUS_CODE_302_FOUND);
|
2021-07-06 20:06:38 +02:00
|
|
|
virtual void compile_body();
|
|
|
|
virtual void compile_and_send_body();
|
|
|
|
virtual void send();
|
2021-11-01 19:11:27 +01:00
|
|
|
virtual void send_file(const String &p_file_path);
|
2021-07-06 20:06:38 +02:00
|
|
|
virtual void send_error(int error_code);
|
|
|
|
virtual void reset();
|
2021-11-01 19:11:27 +01:00
|
|
|
virtual String parser_get_path();
|
|
|
|
virtual String get_host() const;
|
2020-11-25 00:20:41 +01:00
|
|
|
|
2020-12-28 19:15:27 +01:00
|
|
|
void setup_url_stack();
|
2022-02-06 10:54:42 +01:00
|
|
|
String get_path(const bool beginning_slash = false, const bool end_slash = true) const;
|
2021-11-01 19:11:27 +01:00
|
|
|
virtual const String &get_path_full() const;
|
|
|
|
const String &get_path_segment(const uint32_t i) const;
|
|
|
|
const String &get_current_path_segment() const;
|
2021-11-02 12:25:04 +01:00
|
|
|
const String &get_next_path_segment() const;
|
2020-12-28 19:15:27 +01:00
|
|
|
uint32_t get_path_segment_count() const;
|
|
|
|
uint32_t get_current_segment_index() const;
|
|
|
|
uint32_t get_remaining_segment_count() const;
|
|
|
|
void pop_path();
|
|
|
|
void push_path();
|
|
|
|
|
2021-11-02 00:18:57 +01:00
|
|
|
String get_url_root_parent(const int parent = 1) const;
|
2021-11-01 19:11:27 +01:00
|
|
|
String get_url_root() const;
|
2021-11-02 12:25:04 +01:00
|
|
|
String get_url_root_current() const;
|
2021-11-01 19:11:27 +01:00
|
|
|
String get_url_site() const;
|
2021-11-01 00:38:26 +01:00
|
|
|
|
2021-11-01 21:20:42 +01:00
|
|
|
String get_url_root_parent(const String &add) const;
|
|
|
|
String get_url_root(const String &add) const;
|
|
|
|
String get_url_site(const String &add) const;
|
2022-02-10 16:17:31 +01:00
|
|
|
|
2021-07-06 20:06:38 +02:00
|
|
|
virtual void update();
|
|
|
|
virtual void pool();
|
2021-02-09 01:25:24 +01:00
|
|
|
|
2020-11-26 11:26:02 +01:00
|
|
|
Request();
|
2021-07-06 20:06:38 +02:00
|
|
|
virtual ~Request();
|
2020-12-28 19:15:27 +01:00
|
|
|
|
|
|
|
protected:
|
2022-01-08 11:42:57 +01:00
|
|
|
HTTPStatusCode _status_code;
|
2021-11-01 19:11:27 +01:00
|
|
|
String _full_path;
|
|
|
|
Vector<String> _path_stack;
|
2020-12-28 19:15:27 +01:00
|
|
|
uint32_t _path_stack_pointer;
|
2020-11-25 00:20:41 +01:00
|
|
|
};
|
|
|
|
|
2021-08-04 15:36:26 +02:00
|
|
|
template <class T>
|
2020-11-25 00:20:41 +01:00
|
|
|
class RequestPool {
|
|
|
|
public:
|
2021-07-06 20:06:38 +02:00
|
|
|
T *get_request();
|
|
|
|
void return_request(T *request);
|
2020-11-25 00:20:41 +01:00
|
|
|
|
2020-11-26 11:26:02 +01:00
|
|
|
RequestPool();
|
|
|
|
~RequestPool();
|
2020-11-25 00:20:41 +01:00
|
|
|
|
|
|
|
protected:
|
2021-07-06 20:06:38 +02:00
|
|
|
std::mutex _mutex;
|
|
|
|
std::vector<T *> _requests;
|
2020-11-25 00:20:41 +01:00
|
|
|
};
|
|
|
|
|
2021-08-04 15:36:26 +02:00
|
|
|
template <class T>
|
2021-07-06 20:06:38 +02:00
|
|
|
T *RequestPool<T>::get_request() {
|
|
|
|
_mutex.lock();
|
|
|
|
|
|
|
|
T *request;
|
|
|
|
|
|
|
|
if (_requests.size() == 0) {
|
|
|
|
_mutex.unlock();
|
|
|
|
|
|
|
|
request = new T();
|
|
|
|
|
|
|
|
return request;
|
|
|
|
}
|
|
|
|
|
|
|
|
request = _requests[_requests.size() - 1];
|
|
|
|
_requests.pop_back();
|
|
|
|
|
|
|
|
_mutex.unlock();
|
|
|
|
|
|
|
|
request->reset();
|
|
|
|
|
|
|
|
return request;
|
|
|
|
}
|
|
|
|
|
2021-08-04 15:36:26 +02:00
|
|
|
template <class T>
|
2021-07-06 20:06:38 +02:00
|
|
|
void RequestPool<T>::return_request(T *request) {
|
|
|
|
_mutex.lock();
|
|
|
|
_requests.push_back(request);
|
|
|
|
_mutex.unlock();
|
|
|
|
}
|
|
|
|
|
2021-08-04 15:36:26 +02:00
|
|
|
template <class T>
|
2021-07-06 20:06:38 +02:00
|
|
|
RequestPool<T>::RequestPool() {
|
|
|
|
}
|
|
|
|
|
2021-08-04 15:36:26 +02:00
|
|
|
template <class T>
|
2021-07-06 20:06:38 +02:00
|
|
|
RequestPool<T>::~RequestPool() {
|
|
|
|
for (uint32_t i = 0; i < _requests.size(); ++i) {
|
|
|
|
delete _requests[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
_requests.clear();
|
|
|
|
}
|
|
|
|
|
2020-11-25 00:20:41 +01:00
|
|
|
#endif
|