mirror of
https://github.com/yhirose/cpp-httplib
synced 2024-11-21 06:26:02 -07:00
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 <alexandre.taillefer@pwc.ca>
This commit is contained in:
parent
1ccddd1b0b
commit
ed8efea98b
2 changed files with 28 additions and 1 deletions
17
httplib.h
17
httplib.h
|
@ -462,6 +462,7 @@ public:
|
|||
Server &Patch(const char *pattern, Handler handler);
|
||||
Server &Patch(const char *pattern, HandlerWithContentReader handler);
|
||||
Server &Delete(const char *pattern, Handler handler);
|
||||
Server &Delete(const char *pattern, HandlerWithContentReader handler);
|
||||
Server &Options(const char *pattern, Handler handler);
|
||||
|
||||
[[deprecated]] bool set_base_dir(const char *dir,
|
||||
|
@ -551,6 +552,7 @@ private:
|
|||
Handlers patch_handlers_;
|
||||
HandlersForContentReader patch_handlers_for_content_reader_;
|
||||
Handlers delete_handlers_;
|
||||
HandlersForContentReader delete_handlers_for_content_reader_;
|
||||
Handlers options_handlers_;
|
||||
Handler error_handler_;
|
||||
Logger logger_;
|
||||
|
@ -2515,7 +2517,7 @@ get_range_offset_and_length(const Request &req, const Response &res,
|
|||
|
||||
inline bool expect_content(const Request &req) {
|
||||
if (req.method == "POST" || req.method == "PUT" || req.method == "PATCH" ||
|
||||
req.method == "PRI") {
|
||||
req.method == "PRI" || req.method == "DELETE") {
|
||||
return true;
|
||||
}
|
||||
// TODO: check if Content-Length is set
|
||||
|
@ -2968,6 +2970,13 @@ inline Server &Server::Delete(const char *pattern, Handler handler) {
|
|||
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) {
|
||||
options_handlers_.push_back(std::make_pair(std::regex(pattern), handler));
|
||||
return *this;
|
||||
|
@ -3481,6 +3490,12 @@ inline bool Server::routing(Request &req, Response &res, Stream &strm) {
|
|||
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`
|
||||
|
|
12
test/test.cc
12
test/test.cc
|
@ -882,6 +882,11 @@ protected:
|
|||
[&](const Request & /*req*/, Response &res) {
|
||||
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"(\*)",
|
||||
[&](const Request & /*req*/, Response &res) {
|
||||
res.set_header("Allow", "GET, POST, HEAD, OPTIONS");
|
||||
|
@ -1765,6 +1770,13 @@ TEST_F(ServerTest, Delete) {
|
|||
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) {
|
||||
auto res = cli_.Options("*");
|
||||
ASSERT_TRUE(res != nullptr);
|
||||
|
|
Loading…
Reference in a new issue