mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2025-05-15 17:28:29 +00:00
Fix #72
This commit is contained in:
parent
5a78e1c457
commit
5b3187e2f9
2 changed files with 19 additions and 5 deletions
12
httplib.h
12
httplib.h
|
@ -120,6 +120,7 @@ typedef std::multimap<std::string, MultipartFile> MultipartFiles;
|
||||||
struct Request {
|
struct Request {
|
||||||
std::string version;
|
std::string version;
|
||||||
std::string method;
|
std::string method;
|
||||||
|
std::string target;
|
||||||
std::string path;
|
std::string path;
|
||||||
Headers headers;
|
Headers headers;
|
||||||
std::string body;
|
std::string body;
|
||||||
|
@ -150,7 +151,7 @@ struct Response {
|
||||||
std::string get_header_value(const char* key) const;
|
std::string get_header_value(const char* key) const;
|
||||||
void set_header(const char* key, const char* val);
|
void set_header(const char* key, const char* val);
|
||||||
|
|
||||||
void set_redirect(const char* url);
|
void set_redirect(const char* uri);
|
||||||
void set_content(const char* s, size_t n, const char* content_type);
|
void set_content(const char* s, size_t n, const char* content_type);
|
||||||
void set_content(const std::string& s, const char* content_type);
|
void set_content(const std::string& s, const char* content_type);
|
||||||
|
|
||||||
|
@ -1514,18 +1515,19 @@ inline void Server::stop()
|
||||||
|
|
||||||
inline bool Server::parse_request_line(const char* s, Request& req)
|
inline bool Server::parse_request_line(const char* s, Request& req)
|
||||||
{
|
{
|
||||||
static std::regex re("(GET|HEAD|POST|PUT|DELETE|OPTIONS) ([^?]+)(?:\\?(.+?))? (HTTP/1\\.[01])\r\n");
|
static std::regex re("(GET|HEAD|POST|PUT|DELETE|OPTIONS) (([^?]+)(?:\\?(.+?))?) (HTTP/1\\.[01])\r\n");
|
||||||
|
|
||||||
std::cmatch m;
|
std::cmatch m;
|
||||||
if (std::regex_match(s, m, re)) {
|
if (std::regex_match(s, m, re)) {
|
||||||
req.version = std::string(m[4]);
|
req.version = std::string(m[4]);
|
||||||
req.method = std::string(m[1]);
|
req.method = std::string(m[1]);
|
||||||
req.path = detail::decode_url(m[2]);
|
req.target = std::string(m[2]);
|
||||||
|
req.path = detail::decode_url(m[3]);
|
||||||
|
|
||||||
// Parse query text
|
// Parse query text
|
||||||
auto len = std::distance(m[3].first, m[3].second);
|
auto len = std::distance(m[4].first, m[4].second);
|
||||||
if (len > 0) {
|
if (len > 0) {
|
||||||
detail::parse_query_text(m[3], req.params);
|
detail::parse_query_text(m[4], req.params);
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
12
test/test.cc
12
test/test.cc
|
@ -329,6 +329,11 @@ protected:
|
||||||
.Options(R"(\*)", [&](const Request& /*req*/, Response& res) {
|
.Options(R"(\*)", [&](const Request& /*req*/, Response& res) {
|
||||||
res.set_header("Allow", "GET, POST, HEAD, OPTIONS");
|
res.set_header("Allow", "GET, POST, HEAD, OPTIONS");
|
||||||
})
|
})
|
||||||
|
.Get("/request-target", [&](const Request& req, Response& /*res*/) {
|
||||||
|
EXPECT_EQ("/request-target?aaa=bbb&ccc=ddd", req.target);
|
||||||
|
EXPECT_EQ("bbb", req.get_param_value("aaa"));
|
||||||
|
EXPECT_EQ("ddd", req.get_param_value("ccc"));
|
||||||
|
})
|
||||||
#ifdef CPPHTTPLIB_ZLIB_SUPPORT
|
#ifdef CPPHTTPLIB_ZLIB_SUPPORT
|
||||||
.Get("/gzip", [&](const Request& /*req*/, Response& res) {
|
.Get("/gzip", [&](const Request& /*req*/, Response& res) {
|
||||||
res.set_content("1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890", "text/plain");
|
res.set_content("1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890", "text/plain");
|
||||||
|
@ -773,6 +778,13 @@ TEST_F(ServerTest, Options)
|
||||||
EXPECT_TRUE(res->body.empty());
|
EXPECT_TRUE(res->body.empty());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_F(ServerTest, URL)
|
||||||
|
{
|
||||||
|
auto res = cli_.Get("/request-target?aaa=bbb&ccc=ddd");
|
||||||
|
ASSERT_TRUE(res != nullptr);
|
||||||
|
EXPECT_EQ(200, res->status);
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef CPPHTTPLIB_ZLIB_SUPPORT
|
#ifdef CPPHTTPLIB_ZLIB_SUPPORT
|
||||||
TEST_F(ServerTest, Gzip)
|
TEST_F(ServerTest, Gzip)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue