Optimized string passing with string_view.

This commit is contained in:
Ravin Perera
2019-10-14 11:17:55 +05:30
parent ebf13209e1
commit 56c3e3ee93
10 changed files with 54 additions and 44 deletions

View File

@@ -23,13 +23,13 @@ namespace usr
* Global user list. (Exposed to other sub systems)
* Map key: User socket session id (<ip:port>)
*/
std::map<std::string, util::contract_user> users;
std::map<std::string, util::contract_user, std::less<>> users;
/**
* Keep track of verification-pending challenges issued to newly connected users.
* Map key: User socket session id (<ip:port>)
*/
std::map<std::string, std::string> pending_challenges;
std::map<std::string, std::string, std::less<>> pending_challenges;
/**
* User session handler instance. This instance's methods will be fired for any user socket activity.
@@ -127,7 +127,7 @@ void create_user_challenge(std::string &msg, std::string &challengeb64)
* @param original_challenge The original base64 challenge string issued to the user.
* @return 0 if challenge response is verified. -1 if challenge not met or an error occurs.
*/
int verify_user_challenge_response(std::string &extracted_pubkeyb64, const std::string &response, const std::string &original_challenge)
int verify_user_challenge_response(std::string &extracted_pubkeyb64, std::string_view response, std::string_view original_challenge)
{
// We load response raw bytes into json document.
rapidjson::Document d;
@@ -167,10 +167,10 @@ int verify_user_challenge_response(std::string &extracted_pubkeyb64, const std::
}
// Verify the challenge signature. We do this last due to signature verification cost.
std::string sigb64 = d[CHALLENGE_RESP_SIG].GetString();
extracted_pubkeyb64 = d[CHALLENGE_RESP_PUBKEY].GetString();
if (crypto::verify_b64(original_challenge, sigb64, extracted_pubkeyb64) != 0)
if (crypto::verify_b64(
original_challenge,
util::getsv(d[CHALLENGE_RESP_SIG]),
util::getsv(d[CHALLENGE_RESP_PUBKEY])) != 0)
{
std::cerr << "User challenge response signature verification failed.\n";
return -1;
@@ -187,7 +187,7 @@ int verify_user_challenge_response(std::string &extracted_pubkeyb64, const std::
* @param pubkeyb64 User's base64 public key.
* @return 0 on successful additions. -1 on failure.
*/
int add_user(const std::string &sessionid, const std::string &pubkeyb64)
int add_user(std::string_view sessionid, std::string_view pubkeyb64)
{
if (users.count(sessionid) == 1)
{
@@ -228,7 +228,7 @@ int add_user(const std::string &sessionid, const std::string &pubkeyb64)
*
* @return 0 on successful removals. -1 on failure.
*/
int remove_user(const std::string &sessionid)
int remove_user(std::string_view sessionid)
{
auto itr = users.find(sessionid);