fix url queries
This commit is contained in:
parent
a66d642f02
commit
c5a16d3ff6
|
@ -131,7 +131,7 @@ struct metric_manager_t {
|
|||
const std::string& help,
|
||||
Args&&... args) {
|
||||
auto m = std::make_shared<T>(name, help, std::forward<Args>(args)...);
|
||||
bool r = register_metric_static(m);
|
||||
bool r = register_metric_dynamic(m);
|
||||
if (!r) {
|
||||
return nullptr;
|
||||
}
|
||||
|
|
|
@ -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"
|
||||
|
@ -225,35 +226,25 @@ 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;
|
||||
|
||||
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 = "";
|
||||
}
|
||||
else if (c == '&') {
|
||||
val = {&str[pos], i - pos};
|
||||
val = trim(val);
|
||||
queries_.emplace(key, val);
|
||||
|
||||
pos = i + 1;
|
||||
}
|
||||
}
|
||||
|
||||
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, "");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -289,6 +289,43 @@ struct person_t {
|
|||
}
|
||||
};
|
||||
|
||||
void url_queries() {
|
||||
{
|
||||
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();
|
||||
assert(map["a"].empty());
|
||||
assert(map["b"].empty());
|
||||
assert(map["c"].empty());
|
||||
assert(map["d"].empty());
|
||||
assert(map["e"].empty());
|
||||
assert(map["f"] == "1");
|
||||
assert(map["g"] == "1" && 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();
|
||||
assert(map["test"].empty());
|
||||
assert(map.size() == 21);
|
||||
}
|
||||
}
|
||||
|
||||
async_simple::coro::Lazy<void> basic_usage() {
|
||||
coro_http_server server(1, 9001);
|
||||
server.set_http_handler<GET>(
|
||||
|
@ -296,6 +333,21 @@ async_simple::coro::Lazy<void> basic_usage() {
|
|||
resp.set_status_and_content(status_type::ok, "ok");
|
||||
});
|
||||
|
||||
server.set_http_handler<GET>(
|
||||
"/queries", [](coro_http_request &req, coro_http_response &resp) {
|
||||
auto map = req.get_queries();
|
||||
assert(map["test"] == "");
|
||||
resp.set_status_and_content(status_type::ok, "ok");
|
||||
});
|
||||
|
||||
server.set_http_handler<GET>(
|
||||
"/queries2", [](coro_http_request &req, coro_http_response &resp) {
|
||||
auto map = req.get_queries();
|
||||
assert(map["test"] == "");
|
||||
assert(map["a"] == "42");
|
||||
resp.set_status_and_content(status_type::ok, "ok");
|
||||
});
|
||||
|
||||
server.set_http_handler<GET>(
|
||||
"/coro",
|
||||
[](coro_http_request &req,
|
||||
|
@ -382,6 +434,9 @@ async_simple::coro::Lazy<void> basic_usage() {
|
|||
std::cout << key << ": " << val << "\n";
|
||||
}
|
||||
|
||||
co_await client.async_get("/queries?test");
|
||||
co_await client.async_get("/queries2?test&a=42");
|
||||
|
||||
result = co_await client.async_get("/coro");
|
||||
assert(result.status == 200);
|
||||
|
||||
|
@ -594,6 +649,7 @@ void coro_channel() {
|
|||
}
|
||||
|
||||
int main() {
|
||||
url_queries();
|
||||
async_simple::coro::syncAwait(basic_usage());
|
||||
async_simple::coro::syncAwait(use_aspects());
|
||||
async_simple::coro::syncAwait(static_file_server());
|
||||
|
|
Loading…
Reference in New Issue