From cde8465091ffafbffc888640ccc2f22ea8d5ef09 Mon Sep 17 00:00:00 2001 From: Gulliver Date: Thu, 2 Jan 2025 12:35:17 +0100 Subject: [PATCH] replaced += by append and changed the order of strings to remove necessitity to substring at the the end. --- include/crow/http_connection.h | 2 +- include/crow/http_response.h | 2 +- include/crow/query_string.h | 14 ++++++++++---- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/include/crow/http_connection.h b/include/crow/http_connection.h index 76c82f014..edc90bcc5 100644 --- a/include/crow/http_connection.h +++ b/include/crow/http_connection.h @@ -160,7 +160,7 @@ namespace crow else if (req_.upgrade) { // h2 or h2c headers - if (req_.get_header_value("upgrade").substr(0, 2) == "h2") + if (req_.get_header_value("upgrade").find("h2")==0) { // TODO(ipkn): HTTP/2 // currently, ignore upgrade header diff --git a/include/crow/http_response.h b/include/crow/http_response.h index 9e611a2a4..e235025e9 100644 --- a/include/crow/http_response.h +++ b/include/crow/http_response.h @@ -301,7 +301,7 @@ namespace crow #endif if (file_info.statResult == 0 && S_ISREG(file_info.statbuf.st_mode)) { - std::size_t last_dot = path.find_last_of("."); + std::size_t last_dot = path.find_last_of('.'); std::string extension = path.substr(last_dot + 1); code = 200; this->add_header("Content-Length", std::to_string(file_info.statbuf.st_size)); diff --git a/include/crow/query_string.h b/include/crow/query_string.h index 3ad004ad3..13a3d3fe0 100644 --- a/include/crow/query_string.h +++ b/include/crow/query_string.h @@ -378,10 +378,11 @@ namespace crow char* ret = get(name); if (ret != nullptr) { + const std::string key_name = name + '='; for (unsigned int i = 0; i < key_value_pairs_.size(); i++) { std::string str_item(key_value_pairs_[i]); - if (str_item.substr(0, name.size() + 1) == name + '=') + if (str_item.find(key_name)==0) { key_value_pairs_.erase(key_value_pairs_.begin() + i); break; @@ -416,14 +417,18 @@ namespace crow std::vector pop_list(const std::string& name, bool use_brackets = true) { std::vector ret = get_list(name, use_brackets); + const size_t name_len = name.length(); if (!ret.empty()) { for (unsigned int i = 0; i < key_value_pairs_.size(); i++) { std::string str_item(key_value_pairs_[i]); - if ((use_brackets ? (str_item.substr(0, name.size() + 3) == name + "[]=") : (str_item.substr(0, name.size() + 1) == name + '='))) - { + if (str_item.find(name)==0) { + if (use_brackets && str_item.find("[]=",name_len)==name_len) { key_value_pairs_.erase(key_value_pairs_.begin() + i--); + } else if (!use_brackets && str_item.find('=',name_len)==name_len ) { + key_value_pairs_.erase(key_value_pairs_.begin() + i--); + } } } } @@ -454,13 +459,14 @@ namespace crow /// Works the same as \ref get_dict() but removes the values from the query string. std::unordered_map pop_dict(const std::string& name) { + const std::string name_value = name +'['; std::unordered_map ret = get_dict(name); if (!ret.empty()) { for (unsigned int i = 0; i < key_value_pairs_.size(); i++) { std::string str_item(key_value_pairs_[i]); - if (str_item.substr(0, name.size() + 1) == name + '[') + if (str_item.find(name_value)==0) { key_value_pairs_.erase(key_value_pairs_.begin() + i--); }