Skip to content

Commit

Permalink
fix query (#610)
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos authored Jul 2, 2024
1 parent bdbed30 commit 43932dc
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 38 deletions.
4 changes: 2 additions & 2 deletions include/cinatra/coro_http_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1053,10 +1053,10 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
char buf[32];
auto [ptr, _] = std::to_chars(buf, buf + 32, content_length);
if (headers.empty()) {
add_header("Content-Length", std::string{buf, ptr});
add_header("Content-Length", std::string(buf, ptr - buf));
}
else {
headers.emplace("Content-Length", std::string_view{buf, ptr});
headers.emplace("Content-Length", std::string_view(buf, ptr - buf));
}

std::string header_str =
Expand Down
43 changes: 17 additions & 26 deletions include/cinatra/http_parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <string_view>
#include <unordered_map>

#include "cinatra/utils.hpp"
#include "cinatra_log_wrapper.hpp"
#include "define.h"
#include "picohttpparser.h"
Expand Down Expand Up @@ -225,36 +226,26 @@ class http_parser {
void parse_query(std::string_view str) {
std::string_view key;
std::string_view val;
size_t pos = 0;
size_t length = str.length();
for (size_t i = 0; i < length; i++) {
char c = str[i];
if (c == '=') {
key = {&str[pos], i - pos};
key = trim(key);
pos = i + 1;
}
else if (c == '&') {
val = {&str[pos], i - pos};
val = trim(val);
queries_.emplace(key, val);

pos = i + 1;
auto vec = split_sv(str, "&");
for (auto s : vec) {
if (s.empty()) {
continue;
}
size_t pos = s.find('=');
if (s.find('=') != std::string_view::npos) {
key = s.substr(0, pos);
if (key.empty()) {
continue;
}
val = s.substr(pos + 1, s.length() - pos);
}
else {
key = s;
val = "";
}
}

if (pos == 0) {
return;
}

if ((length - pos) > 0) {
val = {&str[pos], length - pos};
val = trim(val);
queries_.emplace(key, val);
}
else if ((length - pos) == 0) {
queries_.emplace(key, "");
}
}

std::string_view trim(std::string_view v) {
Expand Down
12 changes: 6 additions & 6 deletions include/cinatra/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,10 +291,10 @@ get_cookies_map(std::string_view cookies_str) {
};

template <bool is_first_time, bool is_last_time>
std::string_view get_chuncked_buffers(size_t length,
std::array<char, 24> &buffer) {
inline std::string_view get_chuncked_buffers(size_t length,
std::array<char, 24> &buffer) {
if constexpr (is_last_time) {
return {"\r\n0\r\n\r\n"};
return std::string_view{"\r\n0\r\n\r\n"};
}
else {
auto [ptr, ec] = std::to_chars(
Expand All @@ -304,14 +304,14 @@ std::string_view get_chuncked_buffers(size_t length,
if constexpr (is_first_time) {
buffer[0] = '\r';
buffer[1] = '\n';
return {buffer.data() + 2, ptr};
return std::string_view(buffer.data() + 2,
std::distance(buffer.data() + 2, ptr));
}
else {
return {buffer.data(), ptr};
return std::string_view(buffer.data(), std::distance(buffer.data(), ptr));
}
}
}

} // namespace cinatra

#endif // CINATRA_UTILS_HPP
8 changes: 4 additions & 4 deletions include/cinatra/ylt/coro_io/io_context_pool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
#include <async_simple/Executor.h>
#include <async_simple/coro/Lazy.h>

#include <asio/dispatch.hpp>
#include <asio/io_context.hpp>
#include <asio/post.hpp>
#include <asio/steady_timer.hpp>
#include <atomic>
#include <future>
Expand Down Expand Up @@ -52,10 +52,10 @@ class ExecutorWrapper : public async_simple::Executor {

virtual bool schedule(Func func) override {
if constexpr (requires(ExecutorImpl e) { e.post(std::move(func)); }) {
executor_.post(std::move(func));
executor_.dispatch(std::move(func));
}
else {
asio::post(executor_, std::move(func));
asio::dispatch(executor_, std::move(func));
}

return true;
Expand All @@ -68,7 +68,7 @@ class ExecutorWrapper : public async_simple::Executor {
executor.post(std::move(func));
}
else {
asio::post(executor, std::move(func));
asio::dispatch(executor, std::move(func));
}
return true;
}
Expand Down
38 changes: 38 additions & 0 deletions tests/test_cinatra.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,44 @@ TEST_CASE("test cinatra::string with SSO") {
CHECK(s == "HelloHi233");
}

TEST_CASE("test parse query") {
{
http_parser parser{};
parser.parse_query("=");
parser.parse_query("&a");
parser.parse_query("&b=");
parser.parse_query("&c=&d");
parser.parse_query("&e=&f=1");
parser.parse_query("&g=1&h=1");
auto map = parser.queries();
CHECK(map["a"].empty());
CHECK(map["b"].empty());
CHECK(map["c"].empty());
CHECK(map["d"].empty());
CHECK(map["e"].empty());
CHECK(map["f"] == "1");
CHECK(map["g"] == "1");
CHECK(map["h"] == "1");
}
{
http_parser parser{};
parser.parse_query("test");
parser.parse_query("test1=");
parser.parse_query("test2=&");
parser.parse_query("test3&");
parser.parse_query("test4&a");
parser.parse_query("test5&b=2");
parser.parse_query("test6=1&c=2");
parser.parse_query("test7=1&d");
parser.parse_query("test8=1&e=");
parser.parse_query("test9=1&f");
parser.parse_query("test10=1&g=10&h&i=3&j");
auto map = parser.queries();
CHECK(map["test"].empty());
CHECK(map.size() == 21);
}
}

TEST_CASE("test cinatra::string without SSO") {
std::string s(1000, 'A');
std::string s2(5000, 'B');
Expand Down

0 comments on commit 43932dc

Please sign in to comment.