Browse Source

Added support for DELETE request body (#418)

* Added support for DELETE request body

* Fixed DELETE request body test case typo

Co-authored-by: Alexandre Taillefer <[email protected]>
Alexandre Taillefer 5 years ago
parent
commit
ed8efea98b
2 changed files with 28 additions and 1 deletions
  1. 16 1
      httplib.h
  2. 12 0
      test/test.cc

+ 16 - 1
httplib.h

@@ -462,6 +462,7 @@ public:
   Server &Patch(const char *pattern, Handler handler);
   Server &Patch(const char *pattern, Handler handler);
   Server &Patch(const char *pattern, HandlerWithContentReader handler);
   Server &Patch(const char *pattern, HandlerWithContentReader handler);
   Server &Delete(const char *pattern, Handler handler);
   Server &Delete(const char *pattern, Handler handler);
+  Server &Delete(const char *pattern, HandlerWithContentReader handler);
   Server &Options(const char *pattern, Handler handler);
   Server &Options(const char *pattern, Handler handler);
 
 
   [[deprecated]] bool set_base_dir(const char *dir,
   [[deprecated]] bool set_base_dir(const char *dir,
@@ -551,6 +552,7 @@ private:
   Handlers patch_handlers_;
   Handlers patch_handlers_;
   HandlersForContentReader patch_handlers_for_content_reader_;
   HandlersForContentReader patch_handlers_for_content_reader_;
   Handlers delete_handlers_;
   Handlers delete_handlers_;
+  HandlersForContentReader delete_handlers_for_content_reader_;
   Handlers options_handlers_;
   Handlers options_handlers_;
   Handler error_handler_;
   Handler error_handler_;
   Logger logger_;
   Logger logger_;
@@ -2515,7 +2517,7 @@ get_range_offset_and_length(const Request &req, const Response &res,
 
 
 inline bool expect_content(const Request &req) {
 inline bool expect_content(const Request &req) {
   if (req.method == "POST" || req.method == "PUT" || req.method == "PATCH" ||
   if (req.method == "POST" || req.method == "PUT" || req.method == "PATCH" ||
-      req.method == "PRI") {
+      req.method == "PRI" || req.method == "DELETE") {
     return true;
     return true;
   }
   }
   // TODO: check if Content-Length is set
   // TODO: check if Content-Length is set
@@ -2968,6 +2970,13 @@ inline Server &Server::Delete(const char *pattern, Handler handler) {
   return *this;
   return *this;
 }
 }
 
 
+inline Server &Server::Delete(const char *pattern,
+                              HandlerWithContentReader handler) {
+  delete_handlers_for_content_reader_.push_back(
+      std::make_pair(std::regex(pattern), handler));
+  return *this;
+}
+
 inline Server &Server::Options(const char *pattern, Handler handler) {
 inline Server &Server::Options(const char *pattern, Handler handler) {
   options_handlers_.push_back(std::make_pair(std::regex(pattern), handler));
   options_handlers_.push_back(std::make_pair(std::regex(pattern), handler));
   return *this;
   return *this;
@@ -3481,6 +3490,12 @@ inline bool Server::routing(Request &req, Response &res, Stream &strm) {
           return true;
           return true;
         }
         }
       }
       }
+      else if (req.method == "DELETE") {
+        if (dispatch_request_for_content_reader(
+                req, res, reader, delete_handlers_for_content_reader_)) {
+          return true;
+        }
+      }
     }
     }
 
 
     // Read content into `req.body`
     // Read content into `req.body`

+ 12 - 0
test/test.cc

@@ -882,6 +882,11 @@ protected:
                 [&](const Request & /*req*/, Response &res) {
                 [&](const Request & /*req*/, Response &res) {
                   res.set_content("DELETE", "text/plain");
                   res.set_content("DELETE", "text/plain");
                 })
                 })
+        .Delete("/delete-body",
+               [&](const Request &req, Response &res) {
+                 EXPECT_EQ(req.body, "content");
+                 res.set_content(req.body, "text/plain");
+               })
         .Options(R"(\*)",
         .Options(R"(\*)",
                  [&](const Request & /*req*/, Response &res) {
                  [&](const Request & /*req*/, Response &res) {
                    res.set_header("Allow", "GET, POST, HEAD, OPTIONS");
                    res.set_header("Allow", "GET, POST, HEAD, OPTIONS");
@@ -1765,6 +1770,13 @@ TEST_F(ServerTest, Delete) {
   EXPECT_EQ("DELETE", res->body);
   EXPECT_EQ("DELETE", res->body);
 }
 }
 
 
+TEST_F(ServerTest, DeleteContentReceiver) {
+  auto res = cli_.Delete("/delete-body", "content", "text/plain");
+  ASSERT_TRUE(res != nullptr);
+  EXPECT_EQ(200, res->status);
+  EXPECT_EQ("content", res->body);
+}
+
 TEST_F(ServerTest, Options) {
 TEST_F(ServerTest, Options) {
   auto res = cli_.Options("*");
   auto res = cli_.Options("*");
   ASSERT_TRUE(res != nullptr);
   ASSERT_TRUE(res != nullptr);