🐛🐎 fixed last commit and made faster

This commit is contained in:
EntireTwix 2021-07-04 23:18:55 -07:00
parent 84ecfac199
commit b310c8ec2b
6 changed files with 16 additions and 13 deletions

View file

@ -51,14 +51,14 @@ public:
BankResponse GetBal(const std::string &name) const noexcept;
BankResponse GetLogs(const std::string &name) noexcept;
BankResponse SendFunds(const std::string &a_name, const std::string &b_name, uint32_t amount) noexcept;
bool VerifyPassword(std::string_view name, std::string_view attempt) const noexcept;
bool VerifyPassword(const std::string &name, std::string_view &&attempt) const noexcept;
void ChangePassword(const std::string &name, std::string &&new_pass) noexcept;
BankResponse SetBal(const std::string &name, uint32_t amount) noexcept;
BankResponse AddBal(const std::string &name, uint32_t amount) noexcept;
BankResponse SubBal(const std::string &name, uint32_t amount) noexcept;
bool Contains(const std::string &name) const noexcept;
bool AdminVerifyAccount(std::string_view name) noexcept;
bool AdminVerifyAccount(const std::string &name) noexcept;
BankResponse AddUser(std::string &&name, uint32_t init_bal, std::string &&init_pass) noexcept;
BankResponse DelUser(const std::string &name) noexcept;

View file

@ -5,4 +5,5 @@
struct xxHashStringGen
{
XXH64_hash_t operator()(const std::string &str) const noexcept;
XXH64_hash_t operator()(std::string_view &&str) const noexcept;
};

View file

@ -21,13 +21,13 @@ void AdminFilter::doFilter(const HttpRequestPtr &req,
if (middle != std::string::npos)
{
base64_result[middle] = '\0';
std::string_view username = results_view.substr(0, middle);
const std::string &username = results_view.substr(0, middle).data();
if (bank.AdminVerifyAccount(username))
{
base64_result[new_sz] = '\0';
std::string_view password = results_view.substr(middle + 1);
if (bank.VerifyPassword(username, password))
if (bank.VerifyPassword(std::move(username), results_view.substr(middle + 1)))
{
req->setBody(username); //feels sub optimal
fccb();
return;
}

View file

@ -126,10 +126,10 @@ BankResponse Bank::SendFunds(const std::string &a_name, const std::string &b_nam
}
return state;
}
bool Bank::VerifyPassword(std::string_view name, std::string_view attempt) const noexcept
bool Bank::VerifyPassword(const std::string &name, std::string_view &&attempt) const noexcept
{
bool res = false;
users.if_contains(name.data(), [&res, &attempt](const User &u) { res = (u.password == xxHashStringGen{}(attempt.data())); });
users.if_contains(name, [&res, &attempt](const User &u) { res = (u.password == xxHashStringGen{}(std::move(attempt))); });
return res;
}
@ -210,7 +210,7 @@ bool Bank::Contains(const std::string &name) const noexcept
{
return users.contains(name);
}
bool Bank::AdminVerifyAccount(std::string_view name) noexcept
bool Bank::AdminVerifyAccount(const std::string &name) noexcept
{
return (name == admin_account);
}

View file

@ -22,12 +22,10 @@ void UserFilter::doFilter(const HttpRequestPtr &req,
{
base64_result[middle] = '\0';
base64_result[new_sz] = '\0';
std::string_view username = results_view.substr(0, middle);
std::string_view password = results_view.substr(middle + 1);
if (bank.VerifyPassword(username, password))
const std::string &username = results_view.substr(0, middle).data();
if (bank.VerifyPassword(username, results_view.substr(middle + 1)))
{
req->setBody(username); //feels sub optimal
fccb();
return;
}

View file

@ -2,6 +2,10 @@
#include <iostream>
XXH64_hash_t xxHashStringGen::operator()(const std::string &str) const noexcept
{
return XXH3_64bits(str.data(), str.size());
}
XXH64_hash_t xxHashStringGen::operator()(std::string_view &&str) const noexcept
{
return XXH3_64bits(str.data(), str.size());
}