From e977de894acdd857a1aa1d6f3386ba5f6882ccd7 Mon Sep 17 00:00:00 2001 From: Relintai Date: Mon, 1 Nov 2021 19:11:27 +0100 Subject: [PATCH] Now the http backends are also using String. --- core/bry_http/bry_request.cpp | 10 +++---- core/bry_http/bry_request.h | 8 ++++-- core/http/cookie.cpp | 2 +- core/http/cookie.h | 12 ++++---- core/http/http_session.cpp | 20 +++++++------- core/http/http_session.h | 31 +++++++++++---------- core/http/request.cpp | 52 ++++++++++++++++++----------------- core/http/request.h | 48 ++++++++++++++++---------------- core/http/session_manager.cpp | 22 +++++++-------- core/http/session_manager.h | 19 +++++++------ modules/drogon/request.cpp | 14 +++++----- modules/drogon/request.h | 24 ++++++++-------- 12 files changed, 136 insertions(+), 126 deletions(-) diff --git a/core/bry_http/bry_request.cpp b/core/bry_http/bry_request.cpp index 2b84c19..c7e6931 100644 --- a/core/bry_http/bry_request.cpp +++ b/core/bry_http/bry_request.cpp @@ -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(); } diff --git a/core/bry_http/bry_request.h b/core/bry_http/bry_request.h index 5ddb896..447e372 100644 --- a/core/bry_http/bry_request.h +++ b/core/bry_http/bry_request.h @@ -1,6 +1,8 @@ #ifndef BRY_REQUEST_H #define BRY_REQUEST_H +#include "core/string.h" + #include "core/http/request.h" #include @@ -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 _path_stack; + std::vector _path_stack; uint32_t _path_stack_pointer; private: diff --git a/core/http/cookie.cpp b/core/http/cookie.cpp index 67eda4e..9c37396 100644 --- a/core/http/cookie.cpp +++ b/core/http/cookie.cpp @@ -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; diff --git a/core/http/cookie.h b/core/http/cookie.h index e2033f2..a853325 100644 --- a/core/http/cookie.h +++ b/core/http/cookie.h @@ -1,20 +1,20 @@ #ifndef COOKIE_H #define COOKIE_H -#include +#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(); }; diff --git a/core/http/http_session.cpp b/core/http/http_session.cpp index b0624ef..51e161c 100644 --- a/core/http/http_session.cpp +++ b/core/http/http_session.cpp @@ -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 lock(_mutex); _data[key] = obj; } -void HTTPSession::remove_object(const std::string &key) { +void HTTPSession::remove_object(const String &key) { std::lock_guard 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 &ref) { +void HTTPSession::add_reference(const String &key, const Ref &ref) { std::lock_guard lock(_mutex); _reference_data[key] = ref; } -void HTTPSession::remove_reference(const std::string &key) { +void HTTPSession::remove_reference(const String &key) { std::lock_guard lock(_mutex); _reference_data.erase(key); } -Ref HTTPSession::get_reference(const std::string &key) { +Ref 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 lock(_mutex); _int_data[key] = val; } -void HTTPSession::remove_int(const std::string &key) { +void HTTPSession::remove_int(const String &key) { std::lock_guard 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 HTTPSession::get_int_data() { +std::map HTTPSession::get_int_data() { return _int_data; } diff --git a/core/http/http_session.h b/core/http/http_session.h index e3a9a3e..e93975d 100644 --- a/core/http/http_session.h +++ b/core/http/http_session.h @@ -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 #include -#include 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 &ref); - void remove_reference(const std::string &key); - Ref get_reference(const std::string &key); + void add_reference(const String &key, const Ref &ref); + void remove_reference(const String &key); + Ref 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 get_int_data(); + std::map 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 _data; - std::map > _reference_data; - std::map _int_data; + std::map _data; + std::map > _reference_data; + std::map _int_data; }; #endif \ No newline at end of file diff --git a/core/http/request.cpp b/core/http/request.cpp index 381e6dd..6b4c952 100644 --- a/core/http/request.cpp +++ b/core/http/request.cpp @@ -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 += ""; @@ -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 ""; } diff --git a/core/http/request.h b/core/http/request.h index f901ce9..2ab305d 100644 --- a/core/http/request.h +++ b/core/http/request.h @@ -1,9 +1,11 @@ #ifndef REQUEST_H #define REQUEST_H +#include "core/containers/vector.h" +#include "core/string.h" + #include #include -#include #include "core/object.h" #include "core/reference.h" @@ -24,12 +26,12 @@ public: HandlerInstance handler_instance; std::vector *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 data; - std::map > reference_data; + std::map data; + std::map > 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 _path_stack; + String _full_path; + Vector _path_stack; uint32_t _path_stack_pointer; }; diff --git a/core/http/session_manager.cpp b/core/http/session_manager.cpp index 7f5f46c..0d894f0 100644 --- a/core/http/session_manager.cpp +++ b/core/http/session_manager.cpp @@ -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 m = session->get_int_data(); - for (std::map::iterator it = m.begin(); it != m.end(); it++) { + std::map m = session->get_int_data(); + for (std::map::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"; \ No newline at end of file +String SessionManager::_table_name = "sessions"; +String SessionManager::_data_table_name = "session_data"; \ No newline at end of file diff --git a/core/http/session_manager.h b/core/http/session_manager.h index 62c73eb..a102a2c 100644 --- a/core/http/session_manager.h +++ b/core/http/session_manager.h @@ -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 #include -#include -#include 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 _sessions; - std::vector _sessions_vec; + std::map _sessions; + Vector _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 \ No newline at end of file diff --git a/modules/drogon/request.cpp b/modules/drogon/request.cpp index d966bd7..4f00225 100644 --- a/modules/drogon/request.cpp +++ b/modules/drogon/request.cpp @@ -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(static_cast(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(static_cast(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 "/"; } diff --git a/modules/drogon/request.h b/modules/drogon/request.h index fc5b0ef..916399e 100644 --- a/modules/drogon/request.h +++ b/modules/drogon/request.h @@ -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 -#include #include "http/HttpRequestImpl.h" #include "http/HttpResponse.h" @@ -20,20 +22,20 @@ public: drogon::HttpRequestImplPtr request; std::function 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 _path_stack; + Vector _path_stack; uint32_t _path_stack_pointer; - std::vector<::Cookie> _added_cookies; - std::vector _removed_cookies; + Vector<::Cookie> _added_cookies; + Vector _removed_cookies; private: static RequestPool _request_pool;