rcpp_framework/core/http/request.h

138 lines
2.6 KiB
C
Raw Normal View History

2020-11-25 00:20:41 +01:00
#ifndef REQUEST_H
#define REQUEST_H
#include <mutex>
#include <vector>
#include <map>
#include "core/object.h"
2020-11-25 00:20:41 +01:00
#include "handler_instance.h"
#include "http_enums.h"
class WebApplication;
class Cookie;
2021-08-04 20:33:21 +02:00
class HTTPSession;
2020-11-25 00:20:41 +01:00
class Request {
public:
WebApplication *application;
2020-11-25 00:20:41 +01:00
uint32_t current_middleware_index;
HandlerInstance handler_instance;
std::vector<HandlerInstance> *middleware_stack;
std::string head;
std::string body;
2020-12-26 00:17:36 +01:00
std::string footer;
std::string compiled_body;
std::string file_path;
long file_size;
long current_file_progress;
long file_chunk_size;
bool file_next;
bool connection_closed;
2021-08-04 20:33:21 +02:00
HTTPSession *session;
std::map<std::string, Object*> data;
virtual const std::string &get_cookie(const std::string &key);
virtual void add_cookie(const ::Cookie &cookie);
virtual void remove_cookie(const std::string &key);
virtual HTTPMethod get_method() const;
virtual void compile_body();
virtual void compile_and_send_body();
virtual void next_stage();
virtual void send();
virtual void send_file(const std::string &p_file_path);
virtual void send_error(int error_code);
virtual void reset();
virtual std::string parser_get_path();
2020-11-25 00:20:41 +01:00
void setup_url_stack();
std::string get_path() const;
virtual const std::string &get_path_full() const;
const std::string &get_path_segment(const uint32_t i) const;
const std::string &get_current_path_segment() const;
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();
virtual void update();
virtual void pool();
Request();
virtual ~Request();
protected:
std::string _full_path;
std::vector<std::string> _path_stack;
uint32_t _path_stack_pointer;
2020-11-25 00:20:41 +01:00
};
template <class T>
2020-11-25 00:20:41 +01:00
class RequestPool {
public:
T *get_request();
void return_request(T *request);
2020-11-25 00:20:41 +01:00
RequestPool();
~RequestPool();
2020-11-25 00:20:41 +01:00
protected:
std::mutex _mutex;
std::vector<T *> _requests;
2020-11-25 00:20:41 +01:00
};
template <class T>
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;
}
template <class T>
void RequestPool<T>::return_request(T *request) {
_mutex.lock();
_requests.push_back(request);
_mutex.unlock();
}
template <class T>
RequestPool<T>::RequestPool() {
}
template <class T>
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