yhirose 7 years ago
parent
commit
6c5d0b2a18
8 changed files with 214 additions and 87 deletions
  1. 33 12
      README.md
  2. 1 1
      example/benchmark.cc
  3. 1 1
      example/client.cc
  4. 1 1
      example/hello.cc
  5. 5 5
      example/server.cc
  6. 1 1
      example/simplesvr.cc
  7. 123 27
      httplib.h
  8. 49 39
      test/test.cc

+ 33 - 12
README.md

@@ -19,11 +19,11 @@ int main(void)
 
     Server svr;
 
-    svr.get("/hi", [](const Request& req, Response& res) {
+    svr.Get("/hi", [](const Request& req, Response& res) {
         res.set_content("Hello World!", "text/plain");
     });
 
-    svr.get(R"(/numbers/(\d+))", [&](const Request& req, Response& res) {
+    svr.Get(R"(/numbers/(\d+))", [&](const Request& req, Response& res) {
         auto numbers = req.matches[1];
         res.set_content(numbers, "text/plain");
     });
@@ -32,13 +32,15 @@ int main(void)
 }
 ```
 
+`Post`, `Put`, `Delete` and `Options` methods are also supported.
+
 ### Method Chain
 
 ```cpp
-svr.get("/get", [](const auto& req, auto& res) {
+svr.Get("/get", [](const auto& req, auto& res) {
         res.set_content("get", "text/plain");
     })
-    .post("/post", [](const auto& req, auto& res) {
+    .Post("/post", [](const auto& req, auto& res) {
         res.set_content(req.body(), "text/plain");
     })
     .listen("localhost", 1234);
@@ -72,7 +74,7 @@ svr.set_error_handler([](const auto& req, auto& res) {
 ### 'multipart/form-data' POST data
 
 ```cpp
-svr.post("/multipart", [&](const auto& req, auto& res) {
+svr.Post("/multipart", [&](const auto& req, auto& res) {
     auto size = req.files.size();
     auto ret = req.has_file("name1"));
     const auto& file = req.get_file_value("name1");
@@ -95,7 +97,7 @@ int main(void)
 {
     httplib::Client cli("localhost", 1234);
 
-    auto res = cli.get("/hi");
+    auto res = cli.Get("/hi");
     if (res && res->status == 200) {
         std::cout << res->body << std::endl;
     }
@@ -105,8 +107,8 @@ int main(void)
 ### POST
 
 ```c++
-res = cli.post("/post", "text", "text/plain");
-res = cli.post("/person", "name=john1&note=coder", "application/x-www-form-urlencoded");
+res = cli.Post("/post", "text", "text/plain");
+res = cli.Post("/person", "name=john1&note=coder", "application/x-www-form-urlencoded");
 ```
 
 ### POST with parameters
@@ -115,7 +117,26 @@ res = cli.post("/person", "name=john1&note=coder", "application/x-www-form-urlen
 httplib::Map params;
 params["name"] = "john";
 params["note"] = "coder";
-auto res = cli.post("/post", params);
+auto res = cli.Post("/post", params);
+```
+
+### PUT
+
+```c++
+res = cli.Post("/resource/foo", "text", "text/plain");
+```
+
+### DELETE
+
+```c++
+res = cli.Delete("/resource/foo");
+```
+
+### OPTIONS
+
+```c++
+res = cli.Options("*");
+res = cli.Options("/resource/foo");
 ```
 
 ### Connection Timeout
@@ -130,7 +151,7 @@ httplib::Client client(url, port);
 
 // prints: 0 / 000 bytes => 50% complete
 std::shared_ptr<httplib::Response> res =
-    cli.get("/", [](uint64_t len, uint64_t total) {
+    cli.Get("/", [](uint64_t len, uint64_t total) {
         printf("%lld / %lld bytes => %d%% complete\n",
             len, total,
             (int)((len/total)*100));
@@ -150,7 +171,7 @@ httplib::Client cli("httpbin.org", 80);
 // 'Range: bytes=1-10'
 httplib::Headers headers = { httplib::make_range_header(1, 10) };
 
-auto res = cli.get("/range/32", headers);
+auto res = cli.Get("/range/32", headers);
 // res->status should be 206.
 // res->body should be "bcdefghijk".
 ```
@@ -185,4 +206,4 @@ The server applies gzip compression to the following MIME type contents:
 License
 -------
 
-MIT license (© 2017 Yuji Hirose)
+MIT license (© 2018 Yuji Hirose)

+ 1 - 1
example/benchmark.cc

@@ -25,7 +25,7 @@ int main(void) {
 
   for (int i = 0; i < 3; i++) {
     StopWatch sw(to_string(i).c_str());
-    auto res = cli.post("/post", body, "application/octet-stream");
+    auto res = cli.Post("/post", body, "application/octet-stream");
     assert(res->status == 200);
   }
 

+ 1 - 1
example/client.cc

@@ -18,7 +18,7 @@ int main(void)
     httplib::Client cli("localhost", 8080);
 #endif
 
-    auto res = cli.get("/hi");
+    auto res = cli.Get("/hi");
     if (res) {
         cout << res->status << endl;
         cout << res->get_header_value("Content-Type") << endl;

+ 1 - 1
example/hello.cc

@@ -12,7 +12,7 @@ int main(void)
 {
     Server svr;
 
-    svr.get("/hi", [](const auto& /*req*/, auto& res) {
+    svr.Get("/hi", [](const auto& /*req*/, auto& res) {
         res.set_content("Hello World!", "text/plain");
     });
 

+ 5 - 5
example/server.cc

@@ -81,25 +81,25 @@ int main(void)
         return -1;
     }
 
-    svr.get("/", [=](const auto& /*req*/, auto& res) {
+    svr.Get("/", [=](const auto& /*req*/, auto& res) {
         res.set_redirect("/hi");
     });
 
-    svr.get("/hi", [](const auto& /*req*/, auto& res) {
+    svr.Get("/hi", [](const auto& /*req*/, auto& res) {
         res.set_content("Hello World!\n", "text/plain");
     });
 
-    svr.get("/slow", [](const auto& /*req*/, auto& res) {
+    svr.Get("/slow", [](const auto& /*req*/, auto& res) {
         using namespace std::chrono_literals;
         std::this_thread::sleep_for(2s);
         res.set_content("Slow...\n", "text/plain");
     });
 
-    svr.get("/dump", [](const auto& req, auto& res) {
+    svr.Get("/dump", [](const auto& req, auto& res) {
         res.set_content(dump_headers(req.headers), "text/plain");
     });
 
-    svr.get("/stop", [&](const auto& /*req*/, auto& /*res*/) {
+    svr.Get("/stop", [&](const auto& /*req*/, auto& /*res*/) {
         svr.stop();
     });
 

+ 1 - 1
example/simplesvr.cc

@@ -107,7 +107,7 @@ int main(int argc, const char** argv)
     Server svr(version);
 #endif
 
-    svr.post("/multipart", [](const auto& req, auto& res) {
+    svr.Post("/multipart", [](const auto& req, auto& res) {
         auto body =
             dump_headers(req.headers) +
             dump_multipart_files(req.files);

+ 123 - 27
httplib.h

@@ -194,8 +194,12 @@ public:
 
     virtual bool is_valid() const;
 
-    Server& get(const char* pattern, Handler handler);
-    Server& post(const char* pattern, Handler handler);
+    Server& Get(const char* pattern, Handler handler);
+    Server& Post(const char* pattern, Handler handler);
+
+    Server& Put(const char* pattern, Handler handler);
+    Server& Delete(const char* pattern, Handler handler);
+    Server& Options(const char* pattern, Handler handler);
 
     bool set_base_dir(const char* path);
 
@@ -236,6 +240,9 @@ private:
     std::string base_dir_;
     Handlers    get_handlers_;
     Handlers    post_handlers_;
+    Handlers    put_handlers_;
+    Handlers    delete_handlers_;
+    Handlers    options_handlers_;
     Handler     error_handler_;
     Logger      logger_;
 
@@ -256,17 +263,26 @@ public:
 
     virtual bool is_valid() const;
 
-    std::shared_ptr<Response> get(const char* path, Progress progress = nullptr);
-    std::shared_ptr<Response> get(const char* path, const Headers& headers, Progress progress = nullptr);
+    std::shared_ptr<Response> Get(const char* path, Progress progress = nullptr);
+    std::shared_ptr<Response> Get(const char* path, const Headers& headers, Progress progress = nullptr);
+
+    std::shared_ptr<Response> Head(const char* path);
+    std::shared_ptr<Response> Head(const char* path, const Headers& headers);
+
+    std::shared_ptr<Response> Post(const char* path, const std::string& body, const char* content_type);
+    std::shared_ptr<Response> Post(const char* path, const Headers& headers, const std::string& body, const char* content_type);
+
+    std::shared_ptr<Response> Post(const char* path, const Params& params);
+    std::shared_ptr<Response> Post(const char* path, const Headers& headers, const Params& params);
 
-    std::shared_ptr<Response> head(const char* path);
-    std::shared_ptr<Response> head(const char* path, const Headers& headers);
+    std::shared_ptr<Response> Put(const char* path, const std::string& body, const char* content_type);
+    std::shared_ptr<Response> Put(const char* path, const Headers& headers, const std::string& body, const char* content_type);
 
-    std::shared_ptr<Response> post(const char* path, const std::string& body, const char* content_type);
-    std::shared_ptr<Response> post(const char* path, const Headers& headers, const std::string& body, const char* content_type);
+    std::shared_ptr<Response> Delete(const char* path);
+    std::shared_ptr<Response> Delete(const char* path, const Headers& headers);
 
-    std::shared_ptr<Response> post(const char* path, const Params& params);
-    std::shared_ptr<Response> post(const char* path, const Headers& headers, const Params& params);
+    std::shared_ptr<Response> Options(const char* path);
+    std::shared_ptr<Response> Options(const char* path, const Headers& headers);
 
     bool send(Request& req, Response& res);
 
@@ -1411,18 +1427,36 @@ inline Server::~Server()
 {
 }
 
-inline Server& Server::get(const char* pattern, Handler handler)
+inline Server& Server::Get(const char* pattern, Handler handler)
 {
     get_handlers_.push_back(std::make_pair(std::regex(pattern), handler));
     return *this;
 }
 
-inline Server& Server::post(const char* pattern, Handler handler)
+inline Server& Server::Post(const char* pattern, Handler handler)
 {
     post_handlers_.push_back(std::make_pair(std::regex(pattern), handler));
     return *this;
 }
 
+inline Server& Server::Put(const char* pattern, Handler handler)
+{
+    put_handlers_.push_back(std::make_pair(std::regex(pattern), handler));
+    return *this;
+}
+
+inline Server& Server::Delete(const char* pattern, Handler handler)
+{
+    delete_handlers_.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;
+}
+
 inline bool Server::set_base_dir(const char* path)
 {
     if (detail::is_dir(path)) {
@@ -1475,7 +1509,7 @@ inline void Server::stop()
 
 inline bool Server::parse_request_line(const char* s, Request& req)
 {
-    static std::regex re("(GET|HEAD|POST) ([^?]+)(?:\\?(.+?))? (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)) {
@@ -1675,6 +1709,12 @@ inline bool Server::routing(Request& req, Response& res)
         return dispatch_request(req, res, get_handlers_);
     } else if (req.method == "POST") {
         return dispatch_request(req, res, post_handlers_);
+    } else if (req.method == "PUT") {
+        return dispatch_request(req, res, put_handlers_);
+    } else if (req.method == "DELETE") {
+        return dispatch_request(req, res, delete_handlers_);
+    } else if (req.method == "OPTIONS") {
+        return dispatch_request(req, res, options_handlers_);
     }
     return false;
 }
@@ -1725,7 +1765,7 @@ inline bool Server::process_request(Stream& strm, bool last_connection)
     req.set_header("REMOTE_ADDR", strm.get_remote_addr().c_str());
 
     // Body
-    if (req.method == "POST") {
+    if (req.method == "POST" || req.method == "PUT") {
         if (!detail::read_content(strm, req)) {
             res.status = 400;
             write_response(strm, last_connection, req, res);
@@ -1947,12 +1987,12 @@ inline bool Client::read_and_close_socket(socket_t sock, Request& req, Response&
     });
 }
 
-inline std::shared_ptr<Response> Client::get(const char* path, Progress progress)
+inline std::shared_ptr<Response> Client::Get(const char* path, Progress progress)
 {
-    return get(path, Headers(), progress);
+    return Get(path, Headers(), progress);
 }
 
-inline std::shared_ptr<Response> Client::get(const char* path, const Headers& headers, Progress progress)
+inline std::shared_ptr<Response> Client::Get(const char* path, const Headers& headers, Progress progress)
 {
     Request req;
     req.method = "GET";
@@ -1965,12 +2005,12 @@ inline std::shared_ptr<Response> Client::get(const char* path, const Headers& he
     return send(req, *res) ? res : nullptr;
 }
 
-inline std::shared_ptr<Response> Client::head(const char* path)
+inline std::shared_ptr<Response> Client::Head(const char* path)
 {
-    return head(path, Headers());
+    return Head(path, Headers());
 }
 
-inline std::shared_ptr<Response> Client::head(const char* path, const Headers& headers)
+inline std::shared_ptr<Response> Client::Head(const char* path, const Headers& headers)
 {
     Request req;
     req.method = "HEAD";
@@ -1982,13 +2022,13 @@ inline std::shared_ptr<Response> Client::head(const char* path, const Headers& h
     return send(req, *res) ? res : nullptr;
 }
 
-inline std::shared_ptr<Response> Client::post(
+inline std::shared_ptr<Response> Client::Post(
     const char* path, const std::string& body, const char* content_type)
 {
-    return post(path, Headers(), body, content_type);
+    return Post(path, Headers(), body, content_type);
 }
 
-inline std::shared_ptr<Response> Client::post(
+inline std::shared_ptr<Response> Client::Post(
     const char* path, const Headers& headers, const std::string& body, const char* content_type)
 {
     Request req;
@@ -2004,12 +2044,12 @@ inline std::shared_ptr<Response> Client::post(
     return send(req, *res) ? res : nullptr;
 }
 
-inline std::shared_ptr<Response> Client::post(const char* path, const Params& params)
+inline std::shared_ptr<Response> Client::Post(const char* path, const Params& params)
 {
-    return post(path, Headers(), params);
+    return Post(path, Headers(), params);
 }
 
-inline std::shared_ptr<Response> Client::post(const char* path, const Headers& headers, const Params& params)
+inline std::shared_ptr<Response> Client::Post(const char* path, const Headers& headers, const Params& params)
 {
     std::string query;
     for (auto it = params.begin(); it != params.end(); ++it) {
@@ -2021,7 +2061,63 @@ inline std::shared_ptr<Response> Client::post(const char* path, const Headers& h
         query += it->second;
     }
 
-    return post(path, headers, query, "application/x-www-form-urlencoded");
+    return Post(path, headers, query, "application/x-www-form-urlencoded");
+}
+
+inline std::shared_ptr<Response> Client::Put(
+    const char* path, const std::string& body, const char* content_type)
+{
+    return Put(path, Headers(), body, content_type);
+}
+
+inline std::shared_ptr<Response> Client::Put(
+    const char* path, const Headers& headers, const std::string& body, const char* content_type)
+{
+    Request req;
+    req.method = "PUT";
+    req.headers = headers;
+    req.path = path;
+
+    req.headers.emplace("Content-Type", content_type);
+    req.body = body;
+
+    auto res = std::make_shared<Response>();
+
+    return send(req, *res) ? res : nullptr;
+}
+
+inline std::shared_ptr<Response> Client::Delete(const char* path)
+{
+    return Delete(path, Headers());
+}
+
+inline std::shared_ptr<Response> Client::Delete(const char* path, const Headers& headers)
+{
+    Request req;
+    req.method = "DELETE";
+    req.path = path;
+    req.headers = headers;
+
+    auto res = std::make_shared<Response>();
+
+    return send(req, *res) ? res : nullptr;
+}
+
+inline std::shared_ptr<Response> Client::Options(const char* path)
+{
+    return Options(path, Headers());
+}
+
+inline std::shared_ptr<Response> Client::Options(const char* path, const Headers& headers)
+{
+    Request req;
+    req.method = "OPTIONS";
+    req.path = path;
+    req.headers = headers;
+
+    auto res = std::make_shared<Response>();
+
+    return send(req, *res) ? res : nullptr;
 }
 
 /*

File diff suppressed because it is too large
+ 49 - 39
test/test.cc


Some files were not shown because too many files changed in this diff