Now the http backends are also using String.

This commit is contained in:
Relintai 2021-11-01 19:11:27 +01:00
parent adc73d593d
commit e977de894a
12 changed files with 136 additions and 126 deletions

View File

@ -13,7 +13,7 @@ void BryRequest::send() {
response->addHeadValue("Connection", "Keep-Alive");
std::string result = response->getResult();
String result = response->getResult();
session->send(result.c_str(), result.size());
} else {
@ -21,7 +21,7 @@ void BryRequest::send() {
response->addHeadValue("Connection", "Close");
std::string result = response->getResult();
String result = response->getResult();
HttpSession::Ptr lsession = session;
@ -31,7 +31,7 @@ void BryRequest::send() {
pool();
}
void BryRequest::send_file(const std::string &p_file_path) {
void BryRequest::send_file(const String &p_file_path) {
//if (connection_closed) {
// RequestPool::return_request(this);
// return;
@ -52,7 +52,7 @@ void BryRequest::send_file(const std::string &p_file_path) {
fclose(f);
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);
@ -71,7 +71,7 @@ void BryRequest::reset() {
response = new ::HttpResponse();
}
std::string BryRequest::parser_get_path() {
String BryRequest::parser_get_path() {
return http_parser->getPath();
}

View File

@ -1,6 +1,8 @@
#ifndef BRY_REQUEST_H
#define BRY_REQUEST_H
#include "core/string.h"
#include "core/http/request.h"
#include <mutex>
@ -20,9 +22,9 @@ public:
HttpResponse *response;
void send();
void send_file(const std::string &p_file_path);
void send_file(const String &p_file_path);
void reset();
std::string parser_get_path();
String parser_get_path();
void update();
@ -37,7 +39,7 @@ protected:
void _progress_send_file();
void _file_chunk_sent();
std::vector<std::string> _path_stack;
std::vector<String> _path_stack;
uint32_t _path_stack_pointer;
private:

View File

@ -1,7 +1,7 @@
#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;
secure = false;

View File

@ -1,20 +1,20 @@
#ifndef COOKIE_H
#define COOKIE_H
#include <string>
#include "core/string.h"
class Cookie {
public:
//todo date
std::string domain;
std::string path;
std::string key;
std::string value;
String domain;
String path;
String key;
String value;
bool http_only;
bool secure;
Cookie();
Cookie(const std::string &p_key, const std::string &p_value);
Cookie(const String &p_key, const String &p_value);
~Cookie();
};

View File

@ -1,49 +1,49 @@
#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);
_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);
_data.erase(key);
}
Object *HTTPSession::get_object(const std::string &key) {
Object *HTTPSession::get_object(const String &key) {
//don't lock here
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);
_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);
_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
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);
_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);
_int_data.erase(key);
}
int HTTPSession::get_int(const std::string &key) {
int HTTPSession::get_int(const String &key) {
//don't lock here
return _int_data[key];
@ -60,7 +60,7 @@ void HTTPSession::reset() {
session_id = "";
}
std::map<std::string, int> HTTPSession::get_int_data() {
std::map<String, int> HTTPSession::get_int_data() {
return _int_data;
}

View File

@ -1,33 +1,34 @@
#ifndef HTTP_SESSION_H
#define HTTP_SESSION_H
#include "core/string.h"
#include "core/object.h"
#include "core/reference.h"
#include <map>
#include <mutex>
#include <string>
class HTTPSession : public Object {
public:
void add_object(const std::string &key, Object *obj);
void remove_object(const std::string &key);
Object *get_object(const std::string &key);
void add_object(const String &key, Object *obj);
void remove_object(const String &key);
Object *get_object(const String &key);
void add_reference(const std::string &key, const Ref<Reference> &ref);
void remove_reference(const std::string &key);
Ref<Reference> get_reference(const std::string &key);
void add_reference(const String &key, const Ref<Reference> &ref);
void remove_reference(const String &key);
Ref<Reference> get_reference(const String &key);
void add_int(const std::string &key, const int val);
void remove_int(const std::string &key);
int get_int(const std::string &key);
void add_int(const String &key, const int val);
void remove_int(const String &key);
int get_int(const String &key);
std::string session_id;
String session_id;
int id;
void clear();
void reset();
std::map<std::string, int> get_int_data();
std::map<String, int> get_int_data();
HTTPSession();
~HTTPSession();
@ -36,9 +37,9 @@ protected:
std::mutex _mutex;
//todo make something similar to godot's variant. (Or get godot's variant lol)
std::map<std::string, Object *> _data;
std::map<std::string, Ref<Reference> > _reference_data;
std::map<std::string, int> _int_data;
std::map<String, Object *> _data;
std::map<String, Ref<Reference> > _reference_data;
std::map<String, int> _int_data;
};
#endif

View File

@ -17,31 +17,31 @@ HTTPSession *Request::get_or_create_session() {
return session;
}
const std::string &Request::get_cookie(const std::string &key) {
static std::string str;
const String Request::get_cookie(const String &key) {
static String str(0);
return str;
}
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 {
return HTTP_METHOD_GET;
}
const std::string &Request::get_parameter(const std::string &key) const {
static std::string str;
const String Request::get_parameter(const String &key) const {
static String str(0);
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() {
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
compiled_body += "<!DOCTYPE html>";
@ -95,7 +95,7 @@ void Request::send() {
//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);
}
@ -124,31 +124,33 @@ void Request::reset() {
reference_data.clear();
}
std::string Request::parser_get_path() {
String Request::parser_get_path() {
return "";
}
void Request::setup_url_stack() {
_full_path = parser_get_path();
std::string path = parser_get_path();
String path = parser_get_path();
size_t pos = 0;
std::string st;
while ((pos = path.find("/")) != std::string::npos) {
String st;
while ((pos = path.find('/')) != -1) {
st = path.substr(0, pos);
if (st.size() != 0)
if (st.size() != 0) {
_path_stack.push_back(st);
}
path.erase(0, pos + 1);
}
if (path.size() != 0)
if (path.size() != 0) {
_path_stack.push_back(path);
}
}
std::string Request::get_path() const {
std::string path = "";
String Request::get_path() const {
String path = "";
for (uint32_t i = _path_stack_pointer; i < _path_stack.size(); ++i) {
path += _path_stack[i];
@ -158,18 +160,18 @@ std::string Request::get_path() const {
return path;
}
const std::string &Request::get_path_full() const {
const String &Request::get_path_full() const {
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];
}
const std::string &Request::get_current_path_segment() const {
const String &Request::get_current_path_segment() const {
if (_path_stack_pointer >= _path_stack.size()) {
//for convenience
static const std::string e_str = "";
static const String e_str = "";
return e_str;
}
@ -200,8 +202,8 @@ void Request::push_path() {
_path_stack_pointer += 1;
}
std::string Request::get_url_root() const {
std::string path = "/";
String Request::get_url_root() const {
String path = "/";
for (uint32_t i = 0; i < _path_stack_pointer; ++i) {
path += _path_stack[i];
@ -211,8 +213,8 @@ std::string Request::get_url_root() const {
return path;
}
std::string Request::get_url_site() const {
std::string path = get_host();
String Request::get_url_site() const {
String path = get_host();
for (uint32_t i = _path_stack_pointer; i < _path_stack.size(); ++i) {
path += _path_stack[i];
@ -222,7 +224,7 @@ std::string Request::get_url_site() const {
return path;
}
std::string Request::get_host() const {
String Request::get_host() const {
return "";
}

View File

@ -1,9 +1,11 @@
#ifndef REQUEST_H
#define REQUEST_H
#include "core/containers/vector.h"
#include "core/string.h"
#include <map>
#include <mutex>
#include <vector>
#include "core/object.h"
#include "core/reference.h"
@ -24,12 +26,12 @@ public:
HandlerInstance handler_instance;
std::vector<HandlerInstance> *middleware_stack;
std::string head;
std::string body;
std::string footer;
std::string compiled_body;
String head;
String body;
String footer;
String compiled_body;
std::string file_path;
String file_path;
long file_size;
long current_file_progress;
long file_chunk_size;
@ -38,43 +40,43 @@ public:
bool connection_closed;
HTTPSession *session;
std::map<std::string, Object *> data;
std::map<std::string, Ref<Reference> > reference_data;
std::map<String, Object *> data;
std::map<String, Ref<Reference> > reference_data;
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 remove_cookie(const std::string &key);
virtual void remove_cookie(const String &key);
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_and_send_body();
virtual void next_stage();
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 reset();
virtual std::string parser_get_path();
virtual std::string get_host() const;
virtual String parser_get_path();
virtual String get_host() const;
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;
String get_path() const;
virtual const String &get_path_full() const;
const String &get_path_segment(const uint32_t i) const;
const 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();
std::string get_url_root() const;
std::string get_url_site() const;
String get_url_root() const;
String get_url_site() const;
virtual void update();
@ -84,8 +86,8 @@ public:
virtual ~Request();
protected:
std::string _full_path;
std::vector<std::string> _path_stack;
String _full_path;
Vector<String> _path_stack;
uint32_t _path_stack_pointer;
};

View File

@ -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();
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();
int id = session->id;
std::map<std::string, int> m = session->get_int_data();
for (std::map<std::string, int>::iterator it = m.begin(); it != m.end(); it++) {
std::map<String, int> m = session->get_int_data();
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->run_query();
}
HTTPSession *SessionManager::get_session(const std::string &session_id) {
HTTPSession *SessionManager::get_session(const String &session_id) {
return _sessions[session_id];
}
@ -157,7 +157,7 @@ void SessionManager::load_sessions() {
while (r->next_row()) {
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();
s->id = id;
@ -194,7 +194,7 @@ void SessionManager::load_sessions() {
continue;
}
std::string key = r->get_cell(1);
String key = r->get_cell(1);
int value = r->get_cell_int(2);
s->add_int(key, value);
@ -212,11 +212,11 @@ void SessionManager::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
SHA256 *h = SHA256::get();
std::string sid = base;
String sid = base;
sid += rand();
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) {
const std::string &sid = request->get_cookie("session_id");
const String sid = request->get_cookie("session_id");
if (sid == "") {
//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;
std::string SessionManager::_table_name = "sessions";
std::string SessionManager::_data_table_name = "session_data";
String SessionManager::_table_name = "sessions";
String SessionManager::_data_table_name = "session_data";

View File

@ -1,12 +1,13 @@
#ifndef SESSION_MANAGER_H
#define SESSION_MANAGER_H
#include "core/string.h"
#include "core/containers/vector.h"
#include "core/object.h"
#include <map>
#include <mutex>
#include <string>
#include <vector>
class HTTPSession;
class Request;
@ -15,16 +16,16 @@ class SessionManager : public Object {
public:
void add_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);
HTTPSession *get_session(const std::string &session_id);
HTTPSession *get_session(const String &session_id);
HTTPSession *create_session();
void load_sessions();
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);
@ -37,15 +38,15 @@ public:
SessionManager();
~SessionManager();
std::map<std::string, HTTPSession *> _sessions;
std::vector<HTTPSession *> _sessions_vec;
std::map<String, HTTPSession *> _sessions;
Vector<HTTPSession *> _sessions_vec;
std::mutex _mutex;
protected:
static SessionManager *_self;
static std::string _table_name;
static std::string _data_table_name;
static String _table_name;
static String _data_table_name;
};
#endif

View File

@ -4,13 +4,13 @@
#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);
}
void DRequest::add_cookie(const ::Cookie &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);
}
@ -20,11 +20,11 @@ HTTPMethod DRequest::get_method() const {
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);
}
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)));
_response_additional_setup(response);
@ -55,7 +55,7 @@ void DRequest::send() {
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));
_response_additional_setup(response);
@ -76,11 +76,11 @@ void DRequest::reset() {
//response = new HttpResponse();
}
std::string DRequest::parser_get_path() {
String DRequest::parser_get_path() {
return request->getPath();
}
std::string DRequest::get_host() const {
String DRequest::get_host() const {
//todo
return "/";
}

View File

@ -1,10 +1,12 @@
#ifndef DREQUEST_H
#define DREQUEST_H
#include "core/string.h"
#include "core/containers/vector.h"
#include "core/http/request.h"
#include <mutex>
#include <vector>
#include "http/HttpRequestImpl.h"
#include "http/HttpResponse.h"
@ -20,20 +22,20 @@ public:
drogon::HttpRequestImplPtr request;
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 remove_cookie(const std::string &key);
void remove_cookie(const String &key);
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_file(const std::string &p_file_path);
void send_file(const String &p_file_path);
void reset();
std::string parser_get_path();
std::string get_host() const;
String parser_get_path();
String get_host() const;
void update();
@ -49,11 +51,11 @@ protected:
void _file_chunk_sent();
void _response_additional_setup(const drogon::HttpResponsePtr &req);
std::vector<std::string> _path_stack;
Vector<String> _path_stack;
uint32_t _path_stack_pointer;
std::vector<::Cookie> _added_cookies;
std::vector<std::string> _removed_cookies;
Vector<::Cookie> _added_cookies;
Vector<String> _removed_cookies;
private:
static RequestPool<DRequest> _request_pool;