🔨 made str_intrusion.h into a wrapper

This commit is contained in:
EntireTwix 2021-07-09 00:07:45 -07:00
parent 1d6bbb35bf
commit dc6c3d70a0
3 changed files with 38 additions and 43 deletions

View file

@ -1,5 +1,9 @@
#pragma once #pragma once
#include <string> #include <string>
void string_view_to_string(std::string &str, std::string_view sv); struct StrFromSV_Wrapper
void destroy_string(std::string &str); {
std::string str;
StrFromSV_Wrapper(std::string_view sv);
~StrFromSV_Wrapper();
};

View file

@ -15,15 +15,15 @@ static thread_local ondemand::parser parser;
static thread_local simdjson::padded_string input(req->getBody()); \ static thread_local simdjson::padded_string input(req->getBody()); \
static thread_local ondemand::document doc = parser.iterate(input) static thread_local ondemand::document doc = parser.iterate(input)
#define RESPONSE_PARSE(R) \ #define RESPONSE_PARSE(R) \
static thread_local const auto resp = HttpResponse::newCustomHttpResponse(R); \ static thread_local const auto &resp = HttpResponse::newCustomHttpResponse(R); \
CORS; \ CORS; \
callback(resp) callback(resp)
#define RESPOND_TRUE \ #define RESPOND_TRUE \
static thread_local const auto resp = HttpResponse::newCustomHttpResponse(BankResponse(k200OK, "true")); \ static thread_local const auto &resp = HttpResponse::newCustomHttpResponse(BankResponse(k200OK, "true")); \
CORS; \ CORS; \
CACHE_FOREVER; \ CACHE_FOREVER; \
callback(resp) callback(resp)
#define NAME_PARAM req->getParameter("name") #define NAME_PARAM req->getParameter("name")
@ -47,7 +47,7 @@ void api::GetLogs(req_args)
} }
else else
{ {
static thread_local const auto resp = HttpResponse::newCustomHttpResponse(BankResponse(k404NotFound, "\"Logs are Disabled\"")); static thread_local const auto &resp = HttpResponse::newCustomHttpResponse(BankResponse(k404NotFound, "\"Logs are Disabled\""));
CORS; CORS;
CACHE_FOREVER; CACHE_FOREVER;
callback(resp); callback(resp);
@ -65,9 +65,8 @@ void api::SendFunds(req_args) const
} }
else else
{ {
static thread_local std::string name_val; static thread_local StrFromSV_Wrapper name_val(name.value());
string_view_to_string(name_val, name.value()); res = bank.SendFunds(NAME_PARAM, name_val.str, amount.value());
res = bank.SendFunds(NAME_PARAM, name_val, amount.value());
} }
RESPONSE_PARSE(std::move(res)); RESPONSE_PARSE(std::move(res));
} }
@ -78,17 +77,15 @@ void api::ChangePassword(req_args) const
{ {
SIMD_JSON_GEN; SIMD_JSON_GEN;
static thread_local auto pass = doc.find_field("pass").get_string(); static thread_local auto pass = doc.find_field("pass").get_string();
static thread_local auto amount = doc.find_field("amount").get_uint64();
BankResponse res; BankResponse res;
if (pass.error() || amount.error()) if (pass.error())
{ {
res = BankResponse(k400BadRequest, "Invalid JSON"); res = BankResponse(k400BadRequest, "Invalid JSON");
} }
else else
{ {
static thread_local std::string pass_val; static thread_local StrFromSV_Wrapper pass_val(pass.value());
string_view_to_string(pass_val, pass.value()); bank.ChangePassword(NAME_PARAM, std::move(pass_val.str));
bank.ChangePassword(NAME_PARAM, std::move(pass_val));
} }
RESPOND_TRUE; RESPOND_TRUE;
} }
@ -104,10 +101,9 @@ void api::AdminChangePassword(req_args) const
} }
else else
{ {
static thread_local std::string name_val, pass_val; static thread_local StrFromSV_Wrapper name_val(name.value());
string_view_to_string(name_val, name.value()); static thread_local StrFromSV_Wrapper pass_val(pass.value());
string_view_to_string(pass_val, pass.value()); bank.ChangePassword(name_val.str, std::move(pass_val.str));
bank.ChangePassword(name_val, std::move(pass_val));
} }
RESPOND_TRUE; RESPOND_TRUE;
} }
@ -123,9 +119,8 @@ void api::SetBal(req_args) const
} }
else else
{ {
static thread_local std::string name_val; static thread_local StrFromSV_Wrapper name_val(name.value());
string_view_to_string(name_val, name.value()); res = bank.SetBal(name_val.str, amount.value());
res = bank.SetBal(name_val, amount.value());
} }
RESPONSE_PARSE(std::move(res)); RESPONSE_PARSE(std::move(res));
} }
@ -141,9 +136,8 @@ void api::ImpactBal(req_args) const
} }
else else
{ {
static thread_local std::string name_val; static thread_local StrFromSV_Wrapper name_val(name.value());
string_view_to_string(name_val, name.value()); res = bank.ImpactBal(name_val.str, amount.value());
res = bank.ImpactBal(name_val, amount.value());
} }
RESPONSE_PARSE(std::move(res)); RESPONSE_PARSE(std::move(res));
} }
@ -151,7 +145,7 @@ void api::ImpactBal(req_args) const
//System Usage //System Usage
void api::Help(req_args) const void api::Help(req_args) const
{ {
static thread_local const auto resp = HttpResponse::newRedirectionResponse("https://github.com/EntireTwix/CCash/blob/Refractor/README.md"); static thread_local const auto &resp = HttpResponse::newRedirectionResponse("https://github.com/EntireTwix/CCash/blob/Refractor/README.md");
CACHE_FOREVER; CACHE_FOREVER;
callback(resp); callback(resp);
} }
@ -200,10 +194,9 @@ void api::AddUser(req_args) const
} }
else else
{ {
static thread_local std::string name_val, pass_val; StrFromSV_Wrapper name_val(name.value());
string_view_to_string(name_val, name.value()); StrFromSV_Wrapper pass_val(pass.value());
string_view_to_string(pass_val, pass.value()); res = bank.AddUser(std::move(name_val.str), 0, std::move(pass_val.str));
res = bank.AddUser(std::move(name_val), 0, std::move(pass_val));
} }
RESPONSE_PARSE(std::move(res)); RESPONSE_PARSE(std::move(res));
} }
@ -220,10 +213,9 @@ void api::AdminAddUser(req_args) const
} }
else else
{ {
static thread_local std::string name_val, pass_val; static thread_local StrFromSV_Wrapper name_val(name.value());
string_view_to_string(name_val, name.value()); static thread_local StrFromSV_Wrapper pass_val(pass.value());
string_view_to_string(pass_val, pass.value()); res = bank.AddUser(std::move(name_val.str), amount.value(), std::move(pass_val.str));
res = bank.AddUser(std::move(name_val), amount.value(), std::move(pass_val));
} }
RESPONSE_PARSE(std::move(res)); RESPONSE_PARSE(std::move(res));
} }
@ -242,9 +234,8 @@ void api::AdminDelUser(req_args) const
} }
else else
{ {
static thread_local std::string name_val; static thread_local StrFromSV_Wrapper name_val(name.value());
string_view_to_string(name_val, name.value()); res = bank.DelUser(name_val.str);
res = bank.DelUser(name_val);
} }
RESPONSE_PARSE(std::move(res)); RESPONSE_PARSE(std::move(res));
} }

View file

@ -31,13 +31,13 @@ struct string_data
typedef void (std::string::*type)(char *); typedef void (std::string::*type)(char *);
}; };
template class rob<string_data, &std::string::_M_data>; template class rob<string_data, &std::string::_M_data>;
void string_view_to_string(std::string &str, std::string_view sv)
StrFromSV_Wrapper::StrFromSV_Wrapper(std::string_view sv)
{ {
(str.*result<string_data>::ptr)((char *)sv.data()); (str.*result<string_data>::ptr)((char *)sv.data());
(str.*result<string_length>::ptr)(sv.size()); (str.*result<string_length>::ptr)(sv.size());
} }
//may be used later StrFromSV_Wrapper::~StrFromSV_Wrapper()
void destroy_string(std::string &str)
{ {
(str.*result<string_data>::ptr)(nullptr); (str.*result<string_data>::ptr)(nullptr);
(str.*result<string_length>::ptr)(0); (str.*result<string_length>::ptr)(0);