yhirose 7 years ago
parent
commit
5b3187e2f9
2 changed files with 19 additions and 5 deletions
  1. 7 5
      httplib.h
  2. 12 0
      test/test.cc

+ 7 - 5
httplib.h

@@ -120,6 +120,7 @@ typedef std::multimap<std::string, MultipartFile> MultipartFiles;
 struct Request {
     std::string    version;
     std::string    method;
+    std::string    target;
     std::string    path;
     Headers        headers;
     std::string    body;
@@ -150,7 +151,7 @@ struct Response {
     std::string get_header_value(const char* key) const;
     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 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)
 {
-    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;
     if (std::regex_match(s, m, re)) {
         req.version = std::string(m[4]);
         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
-        auto len = std::distance(m[3].first, m[3].second);
+        auto len = std::distance(m[4].first, m[4].second);
         if (len > 0) {
-            detail::parse_query_text(m[3], req.params);
+            detail::parse_query_text(m[4], req.params);
         }
 
         return true;

+ 12 - 0
test/test.cc

@@ -329,6 +329,11 @@ protected:
             .Options(R"(\*)", [&](const Request& /*req*/, Response& res) {
                 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
             .Get("/gzip", [&](const Request& /*req*/, Response& res) {
                 res.set_content("1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890", "text/plain");
@@ -773,6 +778,13 @@ TEST_F(ServerTest, Options)
     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
 TEST_F(ServerTest, Gzip)
 {