mirror of
https://github.com/Relintai/rcpp_framework.git
synced 2025-02-20 15:14:26 +01:00
Now the http backends are also using String.
This commit is contained in:
parent
adc73d593d
commit
e977de894a
@ -13,7 +13,7 @@ void BryRequest::send() {
|
|||||||
|
|
||||||
response->addHeadValue("Connection", "Keep-Alive");
|
response->addHeadValue("Connection", "Keep-Alive");
|
||||||
|
|
||||||
std::string result = response->getResult();
|
String result = response->getResult();
|
||||||
|
|
||||||
session->send(result.c_str(), result.size());
|
session->send(result.c_str(), result.size());
|
||||||
} else {
|
} else {
|
||||||
@ -21,7 +21,7 @@ void BryRequest::send() {
|
|||||||
|
|
||||||
response->addHeadValue("Connection", "Close");
|
response->addHeadValue("Connection", "Close");
|
||||||
|
|
||||||
std::string result = response->getResult();
|
String result = response->getResult();
|
||||||
|
|
||||||
HttpSession::Ptr lsession = session;
|
HttpSession::Ptr lsession = session;
|
||||||
|
|
||||||
@ -31,7 +31,7 @@ void BryRequest::send() {
|
|||||||
pool();
|
pool();
|
||||||
}
|
}
|
||||||
|
|
||||||
void BryRequest::send_file(const std::string &p_file_path) {
|
void BryRequest::send_file(const String &p_file_path) {
|
||||||
//if (connection_closed) {
|
//if (connection_closed) {
|
||||||
// RequestPool::return_request(this);
|
// RequestPool::return_request(this);
|
||||||
// return;
|
// return;
|
||||||
@ -52,7 +52,7 @@ void BryRequest::send_file(const std::string &p_file_path) {
|
|||||||
fclose(f);
|
fclose(f);
|
||||||
|
|
||||||
response->addHeadValue("Connection", "Close");
|
response->addHeadValue("Connection", "Close");
|
||||||
std::string result = "HTTP/1.1 200 OK\r\nConnection: Close\r\n\r\n";
|
String result = "HTTP/1.1 200 OK\r\nConnection: Close\r\n\r\n";
|
||||||
|
|
||||||
application->register_request_update(this);
|
application->register_request_update(this);
|
||||||
|
|
||||||
@ -71,7 +71,7 @@ void BryRequest::reset() {
|
|||||||
response = new ::HttpResponse();
|
response = new ::HttpResponse();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string BryRequest::parser_get_path() {
|
String BryRequest::parser_get_path() {
|
||||||
return http_parser->getPath();
|
return http_parser->getPath();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
#ifndef BRY_REQUEST_H
|
#ifndef BRY_REQUEST_H
|
||||||
#define BRY_REQUEST_H
|
#define BRY_REQUEST_H
|
||||||
|
|
||||||
|
#include "core/string.h"
|
||||||
|
|
||||||
#include "core/http/request.h"
|
#include "core/http/request.h"
|
||||||
|
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
@ -20,9 +22,9 @@ public:
|
|||||||
HttpResponse *response;
|
HttpResponse *response;
|
||||||
|
|
||||||
void send();
|
void send();
|
||||||
void send_file(const std::string &p_file_path);
|
void send_file(const String &p_file_path);
|
||||||
void reset();
|
void reset();
|
||||||
std::string parser_get_path();
|
String parser_get_path();
|
||||||
|
|
||||||
void update();
|
void update();
|
||||||
|
|
||||||
@ -37,7 +39,7 @@ protected:
|
|||||||
void _progress_send_file();
|
void _progress_send_file();
|
||||||
void _file_chunk_sent();
|
void _file_chunk_sent();
|
||||||
|
|
||||||
std::vector<std::string> _path_stack;
|
std::vector<String> _path_stack;
|
||||||
uint32_t _path_stack_pointer;
|
uint32_t _path_stack_pointer;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
|
|
||||||
#include "cookie.h"
|
#include "cookie.h"
|
||||||
|
|
||||||
Cookie::Cookie(const std::string &p_key, const std::string &p_value) {
|
Cookie::Cookie(const String &p_key, const String &p_value) {
|
||||||
http_only = true;
|
http_only = true;
|
||||||
secure = false;
|
secure = false;
|
||||||
|
|
||||||
|
@ -1,20 +1,20 @@
|
|||||||
#ifndef COOKIE_H
|
#ifndef COOKIE_H
|
||||||
#define COOKIE_H
|
#define COOKIE_H
|
||||||
|
|
||||||
#include <string>
|
#include "core/string.h"
|
||||||
|
|
||||||
class Cookie {
|
class Cookie {
|
||||||
public:
|
public:
|
||||||
//todo date
|
//todo date
|
||||||
std::string domain;
|
String domain;
|
||||||
std::string path;
|
String path;
|
||||||
std::string key;
|
String key;
|
||||||
std::string value;
|
String value;
|
||||||
bool http_only;
|
bool http_only;
|
||||||
bool secure;
|
bool secure;
|
||||||
|
|
||||||
Cookie();
|
Cookie();
|
||||||
Cookie(const std::string &p_key, const std::string &p_value);
|
Cookie(const String &p_key, const String &p_value);
|
||||||
~Cookie();
|
~Cookie();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -1,49 +1,49 @@
|
|||||||
|
|
||||||
#include "http_session.h"
|
#include "http_session.h"
|
||||||
|
|
||||||
void HTTPSession::add_object(const std::string &key, Object *obj) {
|
void HTTPSession::add_object(const String &key, Object *obj) {
|
||||||
std::lock_guard<std::mutex> lock(_mutex);
|
std::lock_guard<std::mutex> lock(_mutex);
|
||||||
|
|
||||||
_data[key] = obj;
|
_data[key] = obj;
|
||||||
}
|
}
|
||||||
void HTTPSession::remove_object(const std::string &key) {
|
void HTTPSession::remove_object(const String &key) {
|
||||||
std::lock_guard<std::mutex> lock(_mutex);
|
std::lock_guard<std::mutex> lock(_mutex);
|
||||||
|
|
||||||
_data.erase(key);
|
_data.erase(key);
|
||||||
}
|
}
|
||||||
Object *HTTPSession::get_object(const std::string &key) {
|
Object *HTTPSession::get_object(const String &key) {
|
||||||
//don't lock here
|
//don't lock here
|
||||||
|
|
||||||
return _data[key];
|
return _data[key];
|
||||||
}
|
}
|
||||||
|
|
||||||
void HTTPSession::add_reference(const std::string &key, const Ref<Reference> &ref) {
|
void HTTPSession::add_reference(const String &key, const Ref<Reference> &ref) {
|
||||||
std::lock_guard<std::mutex> lock(_mutex);
|
std::lock_guard<std::mutex> lock(_mutex);
|
||||||
|
|
||||||
_reference_data[key] = ref;
|
_reference_data[key] = ref;
|
||||||
}
|
}
|
||||||
void HTTPSession::remove_reference(const std::string &key) {
|
void HTTPSession::remove_reference(const String &key) {
|
||||||
std::lock_guard<std::mutex> lock(_mutex);
|
std::lock_guard<std::mutex> lock(_mutex);
|
||||||
|
|
||||||
_reference_data.erase(key);
|
_reference_data.erase(key);
|
||||||
}
|
}
|
||||||
Ref<Reference> HTTPSession::get_reference(const std::string &key) {
|
Ref<Reference> HTTPSession::get_reference(const String &key) {
|
||||||
//don't lock here
|
//don't lock here
|
||||||
|
|
||||||
return _reference_data[key];
|
return _reference_data[key];
|
||||||
}
|
}
|
||||||
|
|
||||||
void HTTPSession::add_int(const std::string &key, const int val) {
|
void HTTPSession::add_int(const String &key, const int val) {
|
||||||
std::lock_guard<std::mutex> lock(_mutex);
|
std::lock_guard<std::mutex> lock(_mutex);
|
||||||
|
|
||||||
_int_data[key] = val;
|
_int_data[key] = val;
|
||||||
}
|
}
|
||||||
void HTTPSession::remove_int(const std::string &key) {
|
void HTTPSession::remove_int(const String &key) {
|
||||||
std::lock_guard<std::mutex> lock(_mutex);
|
std::lock_guard<std::mutex> lock(_mutex);
|
||||||
|
|
||||||
_int_data.erase(key);
|
_int_data.erase(key);
|
||||||
}
|
}
|
||||||
int HTTPSession::get_int(const std::string &key) {
|
int HTTPSession::get_int(const String &key) {
|
||||||
//don't lock here
|
//don't lock here
|
||||||
|
|
||||||
return _int_data[key];
|
return _int_data[key];
|
||||||
@ -60,7 +60,7 @@ void HTTPSession::reset() {
|
|||||||
session_id = "";
|
session_id = "";
|
||||||
}
|
}
|
||||||
|
|
||||||
std::map<std::string, int> HTTPSession::get_int_data() {
|
std::map<String, int> HTTPSession::get_int_data() {
|
||||||
return _int_data;
|
return _int_data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,33 +1,34 @@
|
|||||||
#ifndef HTTP_SESSION_H
|
#ifndef HTTP_SESSION_H
|
||||||
#define HTTP_SESSION_H
|
#define HTTP_SESSION_H
|
||||||
|
|
||||||
|
#include "core/string.h"
|
||||||
|
|
||||||
#include "core/object.h"
|
#include "core/object.h"
|
||||||
#include "core/reference.h"
|
#include "core/reference.h"
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <string>
|
|
||||||
|
|
||||||
class HTTPSession : public Object {
|
class HTTPSession : public Object {
|
||||||
public:
|
public:
|
||||||
void add_object(const std::string &key, Object *obj);
|
void add_object(const String &key, Object *obj);
|
||||||
void remove_object(const std::string &key);
|
void remove_object(const String &key);
|
||||||
Object *get_object(const std::string &key);
|
Object *get_object(const String &key);
|
||||||
|
|
||||||
void add_reference(const std::string &key, const Ref<Reference> &ref);
|
void add_reference(const String &key, const Ref<Reference> &ref);
|
||||||
void remove_reference(const std::string &key);
|
void remove_reference(const String &key);
|
||||||
Ref<Reference> get_reference(const std::string &key);
|
Ref<Reference> get_reference(const String &key);
|
||||||
|
|
||||||
void add_int(const std::string &key, const int val);
|
void add_int(const String &key, const int val);
|
||||||
void remove_int(const std::string &key);
|
void remove_int(const String &key);
|
||||||
int get_int(const std::string &key);
|
int get_int(const String &key);
|
||||||
|
|
||||||
std::string session_id;
|
String session_id;
|
||||||
int id;
|
int id;
|
||||||
|
|
||||||
void clear();
|
void clear();
|
||||||
void reset();
|
void reset();
|
||||||
|
|
||||||
std::map<std::string, int> get_int_data();
|
std::map<String, int> get_int_data();
|
||||||
|
|
||||||
HTTPSession();
|
HTTPSession();
|
||||||
~HTTPSession();
|
~HTTPSession();
|
||||||
@ -36,9 +37,9 @@ protected:
|
|||||||
std::mutex _mutex;
|
std::mutex _mutex;
|
||||||
|
|
||||||
//todo make something similar to godot's variant. (Or get godot's variant lol)
|
//todo make something similar to godot's variant. (Or get godot's variant lol)
|
||||||
std::map<std::string, Object *> _data;
|
std::map<String, Object *> _data;
|
||||||
std::map<std::string, Ref<Reference> > _reference_data;
|
std::map<String, Ref<Reference> > _reference_data;
|
||||||
std::map<std::string, int> _int_data;
|
std::map<String, int> _int_data;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
@ -17,31 +17,31 @@ HTTPSession *Request::get_or_create_session() {
|
|||||||
return session;
|
return session;
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::string &Request::get_cookie(const std::string &key) {
|
const String Request::get_cookie(const String &key) {
|
||||||
static std::string str;
|
static String str(0);
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Request::add_cookie(const ::Cookie &cookie) {
|
void Request::add_cookie(const ::Cookie &cookie) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Request::remove_cookie(const std::string &key) {
|
void Request::remove_cookie(const String &key) {
|
||||||
}
|
}
|
||||||
|
|
||||||
HTTPMethod Request::get_method() const {
|
HTTPMethod Request::get_method() const {
|
||||||
return HTTP_METHOD_GET;
|
return HTTP_METHOD_GET;
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::string &Request::get_parameter(const std::string &key) const {
|
const String Request::get_parameter(const String &key) const {
|
||||||
static std::string str;
|
static String str(0);
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Request::send_redirect(const std::string &location, const HTTPStatusCode status_code) {
|
void Request::send_redirect(const String &location, const HTTPStatusCode status_code) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Request::compile_body() {
|
void Request::compile_body() {
|
||||||
compiled_body.reserve(body.size() + head.size() + 15 + 13 + 14 + 15);
|
compiled_body.ensure_capacity(body.size() + head.size() + 15 + 13 + 14 + 15 + 1);
|
||||||
|
|
||||||
//15
|
//15
|
||||||
compiled_body += "<!DOCTYPE html>";
|
compiled_body += "<!DOCTYPE html>";
|
||||||
@ -95,7 +95,7 @@ void Request::send() {
|
|||||||
//RequestPool::return_request(this);
|
//RequestPool::return_request(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Request::send_file(const std::string &p_file_path) {
|
void Request::send_file(const String &p_file_path) {
|
||||||
//RequestPool::return_request(this);
|
//RequestPool::return_request(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -124,31 +124,33 @@ void Request::reset() {
|
|||||||
reference_data.clear();
|
reference_data.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string Request::parser_get_path() {
|
String Request::parser_get_path() {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
void Request::setup_url_stack() {
|
void Request::setup_url_stack() {
|
||||||
_full_path = parser_get_path();
|
_full_path = parser_get_path();
|
||||||
std::string path = parser_get_path();
|
String path = parser_get_path();
|
||||||
|
|
||||||
size_t pos = 0;
|
size_t pos = 0;
|
||||||
std::string st;
|
String st;
|
||||||
while ((pos = path.find("/")) != std::string::npos) {
|
while ((pos = path.find('/')) != -1) {
|
||||||
st = path.substr(0, pos);
|
st = path.substr(0, pos);
|
||||||
|
|
||||||
if (st.size() != 0)
|
if (st.size() != 0) {
|
||||||
_path_stack.push_back(st);
|
_path_stack.push_back(st);
|
||||||
|
}
|
||||||
|
|
||||||
path.erase(0, pos + 1);
|
path.erase(0, pos + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (path.size() != 0)
|
if (path.size() != 0) {
|
||||||
_path_stack.push_back(path);
|
_path_stack.push_back(path);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string Request::get_path() const {
|
String Request::get_path() const {
|
||||||
std::string path = "";
|
String path = "";
|
||||||
|
|
||||||
for (uint32_t i = _path_stack_pointer; i < _path_stack.size(); ++i) {
|
for (uint32_t i = _path_stack_pointer; i < _path_stack.size(); ++i) {
|
||||||
path += _path_stack[i];
|
path += _path_stack[i];
|
||||||
@ -158,18 +160,18 @@ std::string Request::get_path() const {
|
|||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::string &Request::get_path_full() const {
|
const String &Request::get_path_full() const {
|
||||||
return _full_path;
|
return _full_path;
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::string &Request::get_path_segment(const uint32_t i) const {
|
const String &Request::get_path_segment(const uint32_t i) const {
|
||||||
return _path_stack[i];
|
return _path_stack[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::string &Request::get_current_path_segment() const {
|
const String &Request::get_current_path_segment() const {
|
||||||
if (_path_stack_pointer >= _path_stack.size()) {
|
if (_path_stack_pointer >= _path_stack.size()) {
|
||||||
//for convenience
|
//for convenience
|
||||||
static const std::string e_str = "";
|
static const String e_str = "";
|
||||||
return e_str;
|
return e_str;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -200,8 +202,8 @@ void Request::push_path() {
|
|||||||
_path_stack_pointer += 1;
|
_path_stack_pointer += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string Request::get_url_root() const {
|
String Request::get_url_root() const {
|
||||||
std::string path = "/";
|
String path = "/";
|
||||||
|
|
||||||
for (uint32_t i = 0; i < _path_stack_pointer; ++i) {
|
for (uint32_t i = 0; i < _path_stack_pointer; ++i) {
|
||||||
path += _path_stack[i];
|
path += _path_stack[i];
|
||||||
@ -211,8 +213,8 @@ std::string Request::get_url_root() const {
|
|||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string Request::get_url_site() const {
|
String Request::get_url_site() const {
|
||||||
std::string path = get_host();
|
String path = get_host();
|
||||||
|
|
||||||
for (uint32_t i = _path_stack_pointer; i < _path_stack.size(); ++i) {
|
for (uint32_t i = _path_stack_pointer; i < _path_stack.size(); ++i) {
|
||||||
path += _path_stack[i];
|
path += _path_stack[i];
|
||||||
@ -222,7 +224,7 @@ std::string Request::get_url_site() const {
|
|||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string Request::get_host() const {
|
String Request::get_host() const {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
#ifndef REQUEST_H
|
#ifndef REQUEST_H
|
||||||
#define REQUEST_H
|
#define REQUEST_H
|
||||||
|
|
||||||
|
#include "core/containers/vector.h"
|
||||||
|
#include "core/string.h"
|
||||||
|
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
#include "core/object.h"
|
#include "core/object.h"
|
||||||
#include "core/reference.h"
|
#include "core/reference.h"
|
||||||
@ -24,12 +26,12 @@ public:
|
|||||||
HandlerInstance handler_instance;
|
HandlerInstance handler_instance;
|
||||||
std::vector<HandlerInstance> *middleware_stack;
|
std::vector<HandlerInstance> *middleware_stack;
|
||||||
|
|
||||||
std::string head;
|
String head;
|
||||||
std::string body;
|
String body;
|
||||||
std::string footer;
|
String footer;
|
||||||
std::string compiled_body;
|
String compiled_body;
|
||||||
|
|
||||||
std::string file_path;
|
String file_path;
|
||||||
long file_size;
|
long file_size;
|
||||||
long current_file_progress;
|
long current_file_progress;
|
||||||
long file_chunk_size;
|
long file_chunk_size;
|
||||||
@ -38,43 +40,43 @@ public:
|
|||||||
bool connection_closed;
|
bool connection_closed;
|
||||||
|
|
||||||
HTTPSession *session;
|
HTTPSession *session;
|
||||||
std::map<std::string, Object *> data;
|
std::map<String, Object *> data;
|
||||||
std::map<std::string, Ref<Reference> > reference_data;
|
std::map<String, Ref<Reference> > reference_data;
|
||||||
|
|
||||||
HTTPSession *get_or_create_session();
|
HTTPSession *get_or_create_session();
|
||||||
|
|
||||||
virtual const std::string &get_cookie(const std::string &key);
|
virtual const String get_cookie(const String &key);
|
||||||
virtual void add_cookie(const ::Cookie &cookie);
|
virtual void add_cookie(const ::Cookie &cookie);
|
||||||
virtual void remove_cookie(const std::string &key);
|
virtual void remove_cookie(const String &key);
|
||||||
|
|
||||||
virtual HTTPMethod get_method() const;
|
virtual HTTPMethod get_method() const;
|
||||||
|
|
||||||
virtual const std::string &get_parameter(const std::string &key) const;
|
virtual const String get_parameter(const String &key) const;
|
||||||
|
|
||||||
virtual void send_redirect(const std::string &location, const HTTPStatusCode status_code = HTTP_STATUS_CODE_302_FOUND);
|
virtual void send_redirect(const String &location, const HTTPStatusCode status_code = HTTP_STATUS_CODE_302_FOUND);
|
||||||
virtual void compile_body();
|
virtual void compile_body();
|
||||||
virtual void compile_and_send_body();
|
virtual void compile_and_send_body();
|
||||||
virtual void next_stage();
|
virtual void next_stage();
|
||||||
virtual void send();
|
virtual void send();
|
||||||
virtual void send_file(const std::string &p_file_path);
|
virtual void send_file(const String &p_file_path);
|
||||||
virtual void send_error(int error_code);
|
virtual void send_error(int error_code);
|
||||||
virtual void reset();
|
virtual void reset();
|
||||||
virtual std::string parser_get_path();
|
virtual String parser_get_path();
|
||||||
virtual std::string get_host() const;
|
virtual String get_host() const;
|
||||||
|
|
||||||
void setup_url_stack();
|
void setup_url_stack();
|
||||||
std::string get_path() const;
|
String get_path() const;
|
||||||
virtual const std::string &get_path_full() const;
|
virtual const String &get_path_full() const;
|
||||||
const std::string &get_path_segment(const uint32_t i) const;
|
const String &get_path_segment(const uint32_t i) const;
|
||||||
const std::string &get_current_path_segment() const;
|
const String &get_current_path_segment() const;
|
||||||
uint32_t get_path_segment_count() const;
|
uint32_t get_path_segment_count() const;
|
||||||
uint32_t get_current_segment_index() const;
|
uint32_t get_current_segment_index() const;
|
||||||
uint32_t get_remaining_segment_count() const;
|
uint32_t get_remaining_segment_count() const;
|
||||||
void pop_path();
|
void pop_path();
|
||||||
void push_path();
|
void push_path();
|
||||||
|
|
||||||
std::string get_url_root() const;
|
String get_url_root() const;
|
||||||
std::string get_url_site() const;
|
String get_url_site() const;
|
||||||
|
|
||||||
|
|
||||||
virtual void update();
|
virtual void update();
|
||||||
@ -84,8 +86,8 @@ public:
|
|||||||
virtual ~Request();
|
virtual ~Request();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
std::string _full_path;
|
String _full_path;
|
||||||
std::vector<std::string> _path_stack;
|
Vector<String> _path_stack;
|
||||||
uint32_t _path_stack_pointer;
|
uint32_t _path_stack_pointer;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -46,7 +46,7 @@ void SessionManager::remove_session(HTTPSession *session) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SessionManager::delete_session(const std::string &session_id) {
|
void SessionManager::delete_session(const String &session_id) {
|
||||||
_mutex.lock();
|
_mutex.lock();
|
||||||
|
|
||||||
HTTPSession *s = _sessions[session_id];
|
HTTPSession *s = _sessions[session_id];
|
||||||
@ -105,15 +105,15 @@ void SessionManager::save_session(HTTPSession *session) {
|
|||||||
b->del(_data_table_name)->where()->wp("session_db_id", session->id)->end_command();
|
b->del(_data_table_name)->where()->wp("session_db_id", session->id)->end_command();
|
||||||
int id = session->id;
|
int id = session->id;
|
||||||
|
|
||||||
std::map<std::string, int> m = session->get_int_data();
|
std::map<String, int> m = session->get_int_data();
|
||||||
for (std::map<std::string, int>::iterator it = m.begin(); it != m.end(); it++) {
|
for (std::map<String, int>::iterator it = m.begin(); it != m.end(); it++) {
|
||||||
b->insert(_data_table_name, "session_db_id, key, value")->values()->val(id)->val(it->first)->val(it->second)->cvalues()->end_command();
|
b->insert(_data_table_name, "session_db_id, key, value")->values()->val(id)->val(it->first)->val(it->second)->cvalues()->end_command();
|
||||||
}
|
}
|
||||||
|
|
||||||
b->run_query();
|
b->run_query();
|
||||||
}
|
}
|
||||||
|
|
||||||
HTTPSession *SessionManager::get_session(const std::string &session_id) {
|
HTTPSession *SessionManager::get_session(const String &session_id) {
|
||||||
return _sessions[session_id];
|
return _sessions[session_id];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -157,7 +157,7 @@ void SessionManager::load_sessions() {
|
|||||||
|
|
||||||
while (r->next_row()) {
|
while (r->next_row()) {
|
||||||
int id = r->get_cell_int(0);
|
int id = r->get_cell_int(0);
|
||||||
std::string session_id = r->get_cell(1);
|
String session_id = r->get_cell(1);
|
||||||
|
|
||||||
HTTPSession *s = new HTTPSession();
|
HTTPSession *s = new HTTPSession();
|
||||||
s->id = id;
|
s->id = id;
|
||||||
@ -194,7 +194,7 @@ void SessionManager::load_sessions() {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string key = r->get_cell(1);
|
String key = r->get_cell(1);
|
||||||
int value = r->get_cell_int(2);
|
int value = r->get_cell_int(2);
|
||||||
|
|
||||||
s->add_int(key, value);
|
s->add_int(key, value);
|
||||||
@ -212,11 +212,11 @@ void SessionManager::clear() {
|
|||||||
_sessions_vec.clear();
|
_sessions_vec.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string SessionManager::generate_session_id(const std::string &base) {
|
String SessionManager::generate_session_id(const String &base) {
|
||||||
//todo make something simpler / better
|
//todo make something simpler / better
|
||||||
|
|
||||||
SHA256 *h = SHA256::get();
|
SHA256 *h = SHA256::get();
|
||||||
std::string sid = base;
|
String sid = base;
|
||||||
|
|
||||||
sid += rand();
|
sid += rand();
|
||||||
h->compute(sid);
|
h->compute(sid);
|
||||||
@ -230,7 +230,7 @@ std::string SessionManager::generate_session_id(const std::string &base) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void SessionManager::session_setup_middleware(Object *instance, Request *request) {
|
void SessionManager::session_setup_middleware(Object *instance, Request *request) {
|
||||||
const std::string &sid = request->get_cookie("session_id");
|
const String sid = request->get_cookie("session_id");
|
||||||
|
|
||||||
if (sid == "") {
|
if (sid == "") {
|
||||||
//You could create a session here if you want to always assign sessions to visitors.
|
//You could create a session here if you want to always assign sessions to visitors.
|
||||||
@ -309,5 +309,5 @@ SessionManager::~SessionManager() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
SessionManager *SessionManager::_self = nullptr;
|
SessionManager *SessionManager::_self = nullptr;
|
||||||
std::string SessionManager::_table_name = "sessions";
|
String SessionManager::_table_name = "sessions";
|
||||||
std::string SessionManager::_data_table_name = "session_data";
|
String SessionManager::_data_table_name = "session_data";
|
@ -1,12 +1,13 @@
|
|||||||
#ifndef SESSION_MANAGER_H
|
#ifndef SESSION_MANAGER_H
|
||||||
#define SESSION_MANAGER_H
|
#define SESSION_MANAGER_H
|
||||||
|
|
||||||
|
#include "core/string.h"
|
||||||
|
#include "core/containers/vector.h"
|
||||||
|
|
||||||
#include "core/object.h"
|
#include "core/object.h"
|
||||||
|
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <string>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
class HTTPSession;
|
class HTTPSession;
|
||||||
class Request;
|
class Request;
|
||||||
@ -15,16 +16,16 @@ class SessionManager : public Object {
|
|||||||
public:
|
public:
|
||||||
void add_session(HTTPSession *session);
|
void add_session(HTTPSession *session);
|
||||||
void remove_session(HTTPSession *session);
|
void remove_session(HTTPSession *session);
|
||||||
void delete_session(const std::string &session_id);
|
void delete_session(const String &session_id);
|
||||||
void save_session(HTTPSession *session);
|
void save_session(HTTPSession *session);
|
||||||
HTTPSession *get_session(const std::string &session_id);
|
HTTPSession *get_session(const String &session_id);
|
||||||
HTTPSession *create_session();
|
HTTPSession *create_session();
|
||||||
|
|
||||||
void load_sessions();
|
void load_sessions();
|
||||||
|
|
||||||
void clear();
|
void clear();
|
||||||
|
|
||||||
virtual std::string generate_session_id(const std::string &base = "");
|
virtual String generate_session_id(const String &base = "");
|
||||||
|
|
||||||
static void session_setup_middleware(Object *instance, Request *request);
|
static void session_setup_middleware(Object *instance, Request *request);
|
||||||
|
|
||||||
@ -37,15 +38,15 @@ public:
|
|||||||
SessionManager();
|
SessionManager();
|
||||||
~SessionManager();
|
~SessionManager();
|
||||||
|
|
||||||
std::map<std::string, HTTPSession *> _sessions;
|
std::map<String, HTTPSession *> _sessions;
|
||||||
std::vector<HTTPSession *> _sessions_vec;
|
Vector<HTTPSession *> _sessions_vec;
|
||||||
std::mutex _mutex;
|
std::mutex _mutex;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
static SessionManager *_self;
|
static SessionManager *_self;
|
||||||
|
|
||||||
static std::string _table_name;
|
static String _table_name;
|
||||||
static std::string _data_table_name;
|
static String _data_table_name;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
@ -4,13 +4,13 @@
|
|||||||
|
|
||||||
#include "core/http/cookie.h"
|
#include "core/http/cookie.h"
|
||||||
|
|
||||||
const std::string &DRequest::get_cookie(const std::string &key) {
|
const String DRequest::get_cookie(const String &key) {
|
||||||
return request->getCookie(key);
|
return request->getCookie(key);
|
||||||
}
|
}
|
||||||
void DRequest::add_cookie(const ::Cookie &cookie) {
|
void DRequest::add_cookie(const ::Cookie &cookie) {
|
||||||
_added_cookies.push_back(cookie);
|
_added_cookies.push_back(cookie);
|
||||||
}
|
}
|
||||||
void DRequest::remove_cookie(const std::string &key) {
|
void DRequest::remove_cookie(const String &key) {
|
||||||
_removed_cookies.push_back(key);
|
_removed_cookies.push_back(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -20,11 +20,11 @@ HTTPMethod DRequest::get_method() const {
|
|||||||
return static_cast<HTTPMethod>(static_cast<int>(request->getMethod()));
|
return static_cast<HTTPMethod>(static_cast<int>(request->getMethod()));
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::string &DRequest::get_parameter(const std::string &key) const {
|
const String DRequest::get_parameter(const String &key) const {
|
||||||
return request->getParameter(key);
|
return request->getParameter(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
void DRequest::send_redirect(const std::string &location, const HTTPStatusCode status_code) {
|
void DRequest::send_redirect(const String &location, const HTTPStatusCode status_code) {
|
||||||
drogon::HttpResponsePtr response = drogon::HttpResponse::newRedirectionResponse(location, static_cast<const HttpStatusCode>(static_cast<const int>(status_code)));
|
drogon::HttpResponsePtr response = drogon::HttpResponse::newRedirectionResponse(location, static_cast<const HttpStatusCode>(static_cast<const int>(status_code)));
|
||||||
|
|
||||||
_response_additional_setup(response);
|
_response_additional_setup(response);
|
||||||
@ -55,7 +55,7 @@ void DRequest::send() {
|
|||||||
pool();
|
pool();
|
||||||
}
|
}
|
||||||
|
|
||||||
void DRequest::send_file(const std::string &p_file_path) {
|
void DRequest::send_file(const String &p_file_path) {
|
||||||
drogon::HttpResponsePtr response = drogon::HttpResponse::newFileResponse(p_file_path, "", drogon::getContentType(p_file_path));
|
drogon::HttpResponsePtr response = drogon::HttpResponse::newFileResponse(p_file_path, "", drogon::getContentType(p_file_path));
|
||||||
|
|
||||||
_response_additional_setup(response);
|
_response_additional_setup(response);
|
||||||
@ -76,11 +76,11 @@ void DRequest::reset() {
|
|||||||
//response = new HttpResponse();
|
//response = new HttpResponse();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string DRequest::parser_get_path() {
|
String DRequest::parser_get_path() {
|
||||||
return request->getPath();
|
return request->getPath();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string DRequest::get_host() const {
|
String DRequest::get_host() const {
|
||||||
//todo
|
//todo
|
||||||
return "/";
|
return "/";
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,12 @@
|
|||||||
#ifndef DREQUEST_H
|
#ifndef DREQUEST_H
|
||||||
#define DREQUEST_H
|
#define DREQUEST_H
|
||||||
|
|
||||||
|
#include "core/string.h"
|
||||||
|
#include "core/containers/vector.h"
|
||||||
|
|
||||||
#include "core/http/request.h"
|
#include "core/http/request.h"
|
||||||
|
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
#include "http/HttpRequestImpl.h"
|
#include "http/HttpRequestImpl.h"
|
||||||
#include "http/HttpResponse.h"
|
#include "http/HttpResponse.h"
|
||||||
@ -20,20 +22,20 @@ public:
|
|||||||
drogon::HttpRequestImplPtr request;
|
drogon::HttpRequestImplPtr request;
|
||||||
std::function<void(const drogon::HttpResponsePtr &)> callback;
|
std::function<void(const drogon::HttpResponsePtr &)> callback;
|
||||||
|
|
||||||
const std::string &get_cookie(const std::string &key);
|
const String get_cookie(const String &key);
|
||||||
void add_cookie(const ::Cookie &cookie);
|
void add_cookie(const ::Cookie &cookie);
|
||||||
void remove_cookie(const std::string &key);
|
void remove_cookie(const String &key);
|
||||||
|
|
||||||
HTTPMethod get_method() const;
|
HTTPMethod get_method() const;
|
||||||
|
|
||||||
const std::string &get_parameter(const std::string &key) const;
|
const String get_parameter(const String &key) const;
|
||||||
|
|
||||||
void send_redirect(const std::string &location, const HTTPStatusCode status_code = HTTP_STATUS_CODE_302_FOUND);
|
void send_redirect(const String &location, const HTTPStatusCode status_code = HTTP_STATUS_CODE_302_FOUND);
|
||||||
void send();
|
void send();
|
||||||
void send_file(const std::string &p_file_path);
|
void send_file(const String &p_file_path);
|
||||||
void reset();
|
void reset();
|
||||||
std::string parser_get_path();
|
String parser_get_path();
|
||||||
std::string get_host() const;
|
String get_host() const;
|
||||||
|
|
||||||
void update();
|
void update();
|
||||||
|
|
||||||
@ -49,11 +51,11 @@ protected:
|
|||||||
void _file_chunk_sent();
|
void _file_chunk_sent();
|
||||||
void _response_additional_setup(const drogon::HttpResponsePtr &req);
|
void _response_additional_setup(const drogon::HttpResponsePtr &req);
|
||||||
|
|
||||||
std::vector<std::string> _path_stack;
|
Vector<String> _path_stack;
|
||||||
uint32_t _path_stack_pointer;
|
uint32_t _path_stack_pointer;
|
||||||
|
|
||||||
std::vector<::Cookie> _added_cookies;
|
Vector<::Cookie> _added_cookies;
|
||||||
std::vector<std::string> _removed_cookies;
|
Vector<String> _removed_cookies;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static RequestPool<DRequest> _request_pool;
|
static RequestPool<DRequest> _request_pool;
|
||||||
|
Loading…
Reference in New Issue
Block a user