Compare commits

...

3 Commits

Author SHA1 Message Date
TiARETiK bd37b11cde
Merge d71e4bb442 into 1b6a4f4301 2024-05-07 15:22:31 +01:00
AndreyTokmakov 1b6a4f4301 Get rid of unnecessary string copies in the the keys() method 2024-05-06 14:20:22 +02:00
TiARETiK d71e4bb442
Changed headers handling 2024-04-19 12:03:41 +03:00
4 changed files with 69 additions and 15 deletions

View File

@ -362,13 +362,15 @@ namespace crow
if (res.code >= 400 && res.body.empty())
res.body = statusCodes[res.code].substr(9);
headers_.clear();
for (auto& kv : res.headers)
{
buffers_.emplace_back(kv.first.data(), kv.first.size());
buffers_.emplace_back(seperator.data(), seperator.size());
buffers_.emplace_back(kv.second.data(), kv.second.size());
buffers_.emplace_back(crlf.data(), crlf.size());
headers_ += kv.first;
headers_ += seperator;
headers_ += kv.second;
headers_ += crlf;
}
buffers_.emplace_back(headers_.data(), headers_.size());
if (!res.manual_length_header && !res.headers.count("content-length"))
{
@ -613,6 +615,7 @@ namespace crow
std::string content_length_;
std::string date_str_;
std::string headers_;
std::string res_body_copy_;
detail::task_timer::identifier_type task_id_{};

View File

@ -138,7 +138,7 @@ inline size_t qs_parse(char* qs, char* qs_kv[], size_t qs_kv_size, bool parse_ur
#endif
return i;
}
}
inline int qs_decode(char * qs)
@ -295,9 +295,7 @@ namespace crow
public:
static const int MAX_KEY_VALUE_PAIRS_COUNT = 256;
query_string()
{
}
query_string() = default;
query_string(const query_string& qs):
url_(qs.url_)
@ -319,7 +317,7 @@ namespace crow
return *this;
}
query_string& operator=(query_string&& qs)
query_string& operator=(query_string&& qs) noexcept
{
key_value_pairs_ = std::move(qs.key_value_pairs_);
char* old_data = (char*)qs.url_.c_str();
@ -339,9 +337,10 @@ namespace crow
return;
key_value_pairs_.resize(MAX_KEY_VALUE_PAIRS_COUNT);
size_t count = qs_parse(&url_[0], &key_value_pairs_[0], MAX_KEY_VALUE_PAIRS_COUNT, url);
key_value_pairs_.resize(count);
key_value_pairs_.shrink_to_fit();
}
void clear()
@ -472,13 +471,19 @@ namespace crow
std::vector<std::string> keys() const
{
std::vector<std::string> ret;
for (auto element : key_value_pairs_)
std::vector<std::string> keys;
keys.reserve(key_value_pairs_.size());
for (const char* const element : key_value_pairs_)
{
std::string str_element(element);
ret.emplace_back(str_element.substr(0, str_element.find('=')));
const char* delimiter = strchr(element, '=');
if (delimiter)
keys.emplace_back(element, delimiter);
else
keys.emplace_back(element);
}
return ret;
return keys;
}
private:

View File

@ -7,6 +7,7 @@ enable_testing()
set(TEST_SRCS
unittest.cpp
query_string_tests.cpp
)
add_executable(unittest ${TEST_SRCS})

View File

@ -0,0 +1,45 @@
#include <iostream>
#include <vector>
#include "catch.hpp"
#include "crow/query_string.h"
namespace
{
std::string buildQueryStr(const std::vector<std::pair<std::string, std::string>>& paramList)
{
std::string paramsStr{};
for (const auto& param : paramList)
paramsStr.append(param.first).append(1, '=').append(param.second).append(1, '&');
if (!paramsStr.empty())
paramsStr.resize(paramsStr.size() - 1);
return paramsStr;
}
}
TEST_CASE( "empty query params" )
{
const crow::query_string query_params("");
const std::vector<std::string> keys = query_params.keys();
REQUIRE(keys.empty() == true);
}
TEST_CASE( "query string keys" )
{
const std::vector<std::pair<std::string, std::string>> params {
{"foo", "bar"}, {"mode", "night"}, {"page", "2"},
{"tag", "js"}, {"name", "John Smith"}, {"age", "25"},
};
const crow::query_string query_params("params?" + buildQueryStr(params));
const std::vector<std::string> keys = query_params.keys();
for (const auto& entry: params)
{
const bool exist = std::any_of(keys.cbegin(), keys.cend(), [&](const std::string& key) {
return key == entry.first;});
REQUIRE(exist == true);
}
}