From bd089e9499e06f63565f6c8663373f3a6d6cfe79 Mon Sep 17 00:00:00 2001 From: yhirose Date: Mon, 6 Nov 2017 13:24:55 -0500 Subject: [PATCH 001/118] Fixed #18 --- httplib.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index dda5cc3..d94c314 100644 --- a/httplib.h +++ b/httplib.h @@ -177,6 +177,7 @@ protected: const std::string host_; const int port_; + const std::string host_and_port_; private: bool read_response_line(Stream& strm, Response& res); @@ -1035,6 +1036,7 @@ inline bool Server::read_and_close_socket(socket_t sock) inline Client::Client(const char* host, int port) : host_(host) , port_(port) + , host_and_port_(host_ + ":" + std::to_string(port_)) { } @@ -1098,7 +1100,7 @@ inline bool Client::read_and_close_socket(socket_t sock, const Request& req, Res inline void Client::add_default_headers(Request& req) { - req.set_header("Host", host_.c_str()); + req.set_header("Host", host_and_port_.c_str()); req.set_header("Accept", "*/*"); req.set_header("User-Agent", "cpp-httplib/0.1"); } From 140e5c06fb158245b09129aec4794930d4e7bce3 Mon Sep 17 00:00:00 2001 From: yhirose Date: Mon, 6 Nov 2017 13:25:42 -0500 Subject: [PATCH 002/118] Added example/benchmark.cc --- example/Makefile | 15 +++++++++------ example/benchmark.cc | 33 +++++++++++++++++++++++++++++++++ test/Makefile | 2 +- 3 files changed, 43 insertions(+), 7 deletions(-) create mode 100644 example/benchmark.cc diff --git a/example/Makefile b/example/Makefile index 750122e..575e0fa 100644 --- a/example/Makefile +++ b/example/Makefile @@ -1,22 +1,25 @@ CC = clang++ CFLAGS = -std=c++14 -I.. -OPENSSL_SUPPORT = -DCPPHTTPLIB_OPENSSL_SUPPORT -I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib -lssl -lcrypto +#OPENSSL_SUPPORT = -DCPPHTTPLIB_OPENSSL_SUPPORT -I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib -lssl -lcrypto -all: server client hello simplesvr +all: server client hello simplesvr benchmark -server : server.cc ../httplib.h +server : server.cc ../httplib.h Makefile $(CC) -o server $(CFLAGS) server.cc $(OPENSSL_SUPPORT) -client : client.cc ../httplib.h +client : client.cc ../httplib.h Makefile $(CC) -o client $(CFLAGS) client.cc $(OPENSSL_SUPPORT) -hello : hello.cc ../httplib.h +hello : hello.cc ../httplib.h Makefile $(CC) -o hello $(CFLAGS) hello.cc $(OPENSSL_SUPPORT) -simplesvr : simplesvr.cc ../httplib.h +simplesvr : simplesvr.cc ../httplib.h Makefile $(CC) -o simplesvr $(CFLAGS) simplesvr.cc $(OPENSSL_SUPPORT) +benchmark : benchmark.cc ../httplib.h Makefile + $(CC) -o benchmark $(CFLAGS) benchmark.cc $(OPENSSL_SUPPORT) + pem: openssl genrsa 2048 > key.pem openssl req -new -key key.pem | openssl x509 -days 3650 -req -signkey key.pem > cert.pem diff --git a/example/benchmark.cc b/example/benchmark.cc new file mode 100644 index 0000000..7e3c3dd --- /dev/null +++ b/example/benchmark.cc @@ -0,0 +1,33 @@ +#include +#include +#include + +using namespace std; + +struct StopWatch { + StopWatch(const string& label) : label_(label) { + start_ = chrono::system_clock::now(); + } + ~StopWatch() { + auto end = chrono::system_clock::now(); + auto diff = end - start_; + auto count = chrono::duration_cast(diff).count(); + cout << label_ << ": " << count << " millisec." << endl; + } + string label_; + chrono::system_clock::time_point start_; +}; + +int main(int argc, char* argv[]) { + string body(1024 * 5, 'a'); + + httplib::Client cli("httpbin.org", 80); + + for (int i = 0; i < 3; i++) { + StopWatch sw(to_string(i).c_str()); + auto res = cli.post("/post", body, "application/octet-stream"); + assert(res->status == 200); + } + + return 0; +} diff --git a/test/Makefile b/test/Makefile index 3672ccb..76809f3 100644 --- a/test/Makefile +++ b/test/Makefile @@ -6,7 +6,7 @@ CFLAGS = -std=c++14 -DGTEST_USE_OWN_TR1_TUPLE -I.. -I. all : test ./test -test : test.cc ../httplib.h +test : test.cc ../httplib.h Makefile $(CC) -o test $(CFLAGS) test.cc gtest/gtest-all.cc gtest/gtest_main.cc $(OPENSSL_SUPPORT) pem: From a9a9d5e0de56613828c465c4168165412efe036b Mon Sep 17 00:00:00 2001 From: Valentin Dudouyt Date: Wed, 22 Nov 2017 14:06:08 +0700 Subject: [PATCH 003/118] Example fix - should be no const qualifier --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7210bc3..b1ed15e 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ int main(void) Server svr; - svr.get("/hi", [](const Request& req, const Response& res) { + svr.get("/hi", [](const Request& req, Response& res) { res.set_content("Hello World!", "text/plain"); }); From 7c9f9c4a7340c40b0a975fad3bde56b71c63117d Mon Sep 17 00:00:00 2001 From: underscorediscovery Date: Wed, 22 Nov 2017 12:37:59 -0330 Subject: [PATCH 004/118] shield windows defines, in case they are already defined --- httplib.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/httplib.h b/httplib.h index d94c314..e0d6d98 100644 --- a/httplib.h +++ b/httplib.h @@ -9,8 +9,12 @@ #define _CPPHTTPLIB_HTTPLIB_H_ #ifdef _MSC_VER +#ifndef _CRT_SECURE_NO_WARNINGS #define _CRT_SECURE_NO_WARNINGS +#endif //_CRT_SECURE_NO_WARNINGS +#ifndef _CRT_NONSTDC_NO_DEPRECATE #define _CRT_NONSTDC_NO_DEPRECATE +#endif //_CRT_NONSTDC_NO_DEPRECATE #ifndef SO_SYNCHRONOUS_NONALERT #define SO_SYNCHRONOUS_NONALERT 0x20 From 45d79d163b353d28f1c185fd493f4420b6e3a6dd Mon Sep 17 00:00:00 2001 From: underscorediscovery Date: Wed, 22 Nov 2017 13:01:19 -0330 Subject: [PATCH 005/118] add progress callback for http clients --- httplib.h | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/httplib.h b/httplib.h index d94c314..73df545 100644 --- a/httplib.h +++ b/httplib.h @@ -66,6 +66,7 @@ namespace httplib typedef std::map Map; typedef std::multimap MultiMap; typedef std::smatch Match; +typedef std::function Progress; struct Request { std::string method; @@ -74,6 +75,7 @@ struct Request { std::string body; Map params; Match matches; + Progress progress; bool has_header(const char* key) const; std::string get_header_value(const char* key) const; @@ -165,7 +167,7 @@ public: Client(const char* host, int port); virtual ~Client(); - std::shared_ptr get(const char* path); + std::shared_ptr get(const char* path, Progress callback = [](int64_t,int64_t){}); std::shared_ptr head(const char* path); std::shared_ptr post(const char* path, const std::string& body, const char* content_type); std::shared_ptr post(const char* path, const Map& params); @@ -507,7 +509,7 @@ inline bool read_headers(Stream& strm, MultiMap& headers) } template -bool read_content(Stream& strm, T& x, bool allow_no_content_length) +bool read_content(Stream& strm, T& x, bool allow_no_content_length, Progress progress = [](int64_t,int64_t){}) { auto len = get_header_value_int(x.headers, "Content-Length", 0); if (len) { @@ -519,6 +521,7 @@ bool read_content(Stream& strm, T& x, bool allow_no_content_length) return false; } r += r_incr; + progress(r, len); } } else if (allow_no_content_length) { for (;;) { @@ -1083,7 +1086,7 @@ inline bool Client::process_request(Stream& strm, const Request& req, Response& return false; } if (req.method != "HEAD") { - if (!detail::read_content(strm, res, true)) { + if (!detail::read_content(strm, res, true, req.progress)) { return false; } } @@ -1105,11 +1108,12 @@ inline void Client::add_default_headers(Request& req) req.set_header("User-Agent", "cpp-httplib/0.1"); } -inline std::shared_ptr Client::get(const char* path) +inline std::shared_ptr Client::get(const char* path, Progress callback) { Request req; req.method = "GET"; req.path = path; + req.progress = callback; add_default_headers(req); auto res = std::make_shared(); From b7fbbf0f83a617bd6b6417a945f7d80bbae49f92 Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 24 Nov 2017 20:25:49 -0500 Subject: [PATCH 006/118] Fixed #24 --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7210bc3..ddb339a 100644 --- a/README.md +++ b/README.md @@ -21,11 +21,11 @@ int main(void) Server svr; - svr.get("/hi", [](const Request& req, const 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, const Response& res) { + svr.get(R"(/numbers/(\d+))", [&](const Request& req, Response& res) { auto numbers = req.matches[1]; res.set_content(numbers, "text/plain"); }); From cef64d5f244d15b5b33f107fdd510ead4da595f7 Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 24 Nov 2017 21:39:17 -0500 Subject: [PATCH 007/118] Added 'With Progress Callback' section to README --- README.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/README.md b/README.md index ddb339a..1c5d82e 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,25 @@ int main(void) } ``` +### With Progress Callback + +```cpp +httplib::Client client(url, port); + +// prints: 0 / 000 bytes => 50% complete +std::shared_ptr res = + cli.get("/", [](int64_t len, int64_t total) { + printf("%lld / %lld bytes => %d%% complete\n", + len, total, + (int)((len/total)*100)); + } +); +``` + +![progress](https://user-images.githubusercontent.com/236374/33138910-495c4ecc-cf86-11e7-8693-2fc6d09615c4.gif) + +This feature has been contributed by @underscorediscovery. + OpenSSL Support --------------- From 4d62b15d947bc7839a86bd2cdce588217d51bbf7 Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 24 Nov 2017 21:49:28 -0500 Subject: [PATCH 008/118] Updated README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1c5d82e..8d7b4f8 100644 --- a/README.md +++ b/README.md @@ -69,7 +69,7 @@ std::shared_ptr res = ![progress](https://user-images.githubusercontent.com/236374/33138910-495c4ecc-cf86-11e7-8693-2fc6d09615c4.gif) -This feature has been contributed by @underscorediscovery. +This feature was contributed by [underscorediscovery](https://github.com/yhirose/cpp-httplib/pull/23). OpenSSL Support --------------- From 1e3ef46862449de5788f3011c1ead3c588e88a01 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 25 Nov 2017 11:58:09 -0500 Subject: [PATCH 009/118] Changed to return 'Server&' from 'get' and 'post' --- httplib.h | 10 ++++++---- test/test.cc | 52 ++++++++++++++++++++++++---------------------------- 2 files changed, 30 insertions(+), 32 deletions(-) diff --git a/httplib.h b/httplib.h index 1e0259d..abb39ca 100644 --- a/httplib.h +++ b/httplib.h @@ -133,8 +133,8 @@ public: Server(); virtual ~Server(); - void get(const char* pattern, Handler handler); - void post(const char* pattern, Handler handler); + Server& get(const char* pattern, Handler handler); + Server& post(const char* pattern, Handler handler); bool set_base_dir(const char* path); @@ -846,14 +846,16 @@ inline Server::~Server() { } -inline void 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 void 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 bool Server::set_base_dir(const char* path) diff --git a/test/test.cc b/test/test.cc index 98f0954..b27e72b 100644 --- a/test/test.cc +++ b/test/test.cc @@ -122,34 +122,30 @@ protected: svr_.set_base_dir("./www"); svr_.get("/hi", [&](const Request& req, Response& res) { - res.set_content("Hello World!", "text/plain"); - }); - - svr_.get("/", [&](const Request& req, Response& res) { - res.set_redirect("/hi"); - }); - - svr_.post("/person", [&](const Request& req, Response& res) { - if (req.has_param("name") && req.has_param("note")) { - persons_[req.params.at("name")] = req.params.at("note"); - } else { - res.status = 400; - } - }); - - svr_.get("/person/(.*)", [&](const Request& req, Response& res) { - string name = req.matches[1]; - if (persons_.find(name) != persons_.end()) { - auto note = persons_[name]; - res.set_content(note, "text/plain"); - } else { - res.status = 404; - } - }); - - svr_.get("/stop", [&](const Request& req, Response& res) { - svr_.stop(); - }); + res.set_content("Hello World!", "text/plain"); + }) + .get("/", [&](const Request& req, Response& res) { + res.set_redirect("/hi"); + }) + .post("/person", [&](const Request& req, Response& res) { + if (req.has_param("name") && req.has_param("note")) { + persons_[req.params.at("name")] = req.params.at("note"); + } else { + res.status = 400; + } + }) + .get("/person/(.*)", [&](const Request& req, Response& res) { + string name = req.matches[1]; + if (persons_.find(name) != persons_.end()) { + auto note = persons_[name]; + res.set_content(note, "text/plain"); + } else { + res.status = 404; + } + }) + .get("/stop", [&](const Request& req, Response& res) { + svr_.stop(); + }); persons_["john"] = "programmer"; From a90e9b8a6aa4c234c97a17ac21314c1a183f2efc Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 25 Nov 2017 11:59:28 -0500 Subject: [PATCH 010/118] Updated README --- README.md | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/README.md b/README.md index 8d7b4f8..01e364d 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,43 @@ int main(void) } ``` +### Method Chain + +```cpp +svr.get("/get", [](const auto& req, auto& res) { + res.set_content("get", "text/plain"); + }) + .post("/post", [](const auto& req, auto& res) { + res.set_content(req.body(), "text/plain"); + }) + .listen("localhost", 1234); +``` + +### Static File Server + +```cpp +svr.set_base_dir("./www"); +``` + +### Logging + +```cpp +svr.set_logger([](const auto& req, const auto& res) { + your_logger(req, res); +}); +``` + +### Error Handler + +```cpp +svr.set_error_handler([](const auto& req, auto& res) { + const char* fmt = "

Error Status: %d

"; + char buf[BUFSIZ]; + snprintf(buf, sizeof(buf), fmt, res.status); + res.set_content(buf, "text/html"); +}); +``` + Client Example -------------- From 90f9cd40f94b1244ffc1902b48ee68a31e4712c7 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 25 Nov 2017 22:32:09 -0500 Subject: [PATCH 011/118] Fixed #3 --- httplib.h | 49 ++++++++++++++++++++++++++++++++++++++++++++----- test/test.cc | 30 ++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 5 deletions(-) diff --git a/httplib.h b/httplib.h index abb39ca..09d3225 100644 --- a/httplib.h +++ b/httplib.h @@ -393,16 +393,55 @@ inline socket_t create_client_socket(const char* host, int port) }); } -inline bool is_file(const std::string& s) +inline bool is_file(const std::string& path) { struct stat st; - return stat(s.c_str(), &st) >= 0 && S_ISREG(st.st_mode); + return stat(path.c_str(), &st) >= 0 && S_ISREG(st.st_mode); } -inline bool is_dir(const std::string& s) +inline bool is_dir(const std::string& path) { struct stat st; - return stat(s.c_str(), &st) >= 0 && S_ISDIR(st.st_mode); + return stat(path.c_str(), &st) >= 0 && S_ISDIR(st.st_mode); +} + +inline bool is_valid_path(const std::string& path) { + size_t level = 0; + size_t i = 0; + + // Skip slash + while (i < path.size() && path[i] == '/') { + i++; + } + + while (i < path.size()) { + // Read component + auto beg = i; + while (i < path.size() && path[i] != '/') { + i++; + } + + auto len = i - beg; + assert(len > 0); + + if (!path.compare(beg, len, ".")) { + ; + } else if (!path.compare(beg, len, "..")) { + if (level == 0) { + return false; + } + level--; + } else { + level++; + } + + // Skip slash + while (i < path.size() && path[i] == '/') { + i++; + } + } + + return true; } inline void read_file(const std::string& path, std::string& out) @@ -942,7 +981,7 @@ inline bool Server::read_request_line(Stream& strm, Request& req) inline bool Server::handle_file_request(Request& req, Response& res) { - if (!base_dir_.empty()) { + if (!base_dir_.empty() && detail::is_valid_path(req.path)) { std::string path = base_dir_ + req.path; if (!path.empty() && path.back() == '/') { diff --git a/test/test.cc b/test/test.cc index b27e72b..e2deb5d 100644 --- a/test/test.cc +++ b/test/test.cc @@ -293,6 +293,36 @@ TEST_F(ServerTest, GetMethodDirTest) EXPECT_EQ("test.html", res->body); } +TEST_F(ServerTest, GetMethodDirTestWithDoubleDots) +{ + auto res = cli_.get("/dir/../dir/test.html"); + ASSERT_TRUE(res != nullptr); + EXPECT_EQ(200, res->status); + EXPECT_EQ("text/html", res->get_header_value("Content-Type")); + EXPECT_EQ("test.html", res->body); +} + +TEST_F(ServerTest, GetMethodInvalidPath) +{ + auto res = cli_.get("/dir/../test.html"); + ASSERT_TRUE(res != nullptr); + EXPECT_EQ(404, res->status); +} + +TEST_F(ServerTest, GetMethodOutOfBaseDir) +{ + auto res = cli_.get("/../www/dir/test.html"); + ASSERT_TRUE(res != nullptr); + EXPECT_EQ(404, res->status); +} + +TEST_F(ServerTest, GetMethodOutOfBaseDir2) +{ + auto res = cli_.get("/dir/../../www/dir/test.html"); + ASSERT_TRUE(res != nullptr); + EXPECT_EQ(404, res->status); +} + TEST_F(ServerTest, InvalidBaseDir) { EXPECT_EQ(false, svr_.set_base_dir("invalid_dir")); From 28ba178fee71f3e68658db5387ccbb05a156a2fc Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 2 Dec 2017 10:24:41 -0500 Subject: [PATCH 012/118] Fixed #27 --- httplib.h | 58 ++++++++++++++++++++++++++++++------------ test/test.cc | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+), 16 deletions(-) diff --git a/httplib.h b/httplib.h index 09d3225..fe8ec38 100644 --- a/httplib.h +++ b/httplib.h @@ -155,6 +155,7 @@ private: bool dispatch_request(Request& req, Response& res, Handlers& handlers); bool read_request_line(Stream& strm, Request& req); + void write_response(Stream& strm, const Request& req, Response& res); virtual bool read_and_close_socket(socket_t sock); @@ -270,6 +271,10 @@ inline bool socket_gets(Stream& strm, char* buf, int bufsiz) } } + if (i == bufsiz) { + return false; + } + buf[i++] = byte; if (byte == '\n') { @@ -277,6 +282,10 @@ inline bool socket_gets(Stream& strm, char* buf, int bufsiz) } } + if (i == bufsiz) { + return false; + } + buf[i] = '\0'; return true; } @@ -288,7 +297,13 @@ inline void socket_printf(Stream& strm, const char* fmt, const Args& ...args) auto n = snprintf(buf, BUFSIZ, fmt, args...); if (n > 0) { if (n >= BUFSIZ) { - // TODO: buffer size is not large enough... + std::vector glowable_buf(BUFSIZ); + + while (n >= glowable_buf.size()) { + glowable_buf.resize(glowable_buf.size() * 2); + n = snprintf(&glowable_buf[0], glowable_buf.size(), fmt, args...); + } + strm.write(&glowable_buf[0], n); } else { strm.write(buf, n); } @@ -564,7 +579,9 @@ bool read_content(Stream& strm, T& x, bool allow_no_content_length, Progress pro return false; } r += r_incr; - progress(r, len); + if (progress) { + progress(r, len); + } } } else if (allow_no_content_length) { for (;;) { @@ -979,6 +996,21 @@ inline bool Server::read_request_line(Stream& strm, Request& req) return false; } +inline void Server::write_response(Stream& strm, const Request& req, Response& res) +{ + assert(res.status != -1); + + if (400 <= res.status && error_handler_) { + error_handler_(req, res); + } + + detail::write_response(strm, req, res); + + if (logger_) { + logger_(req, res); + } +} + inline bool Server::handle_file_request(Request& req, Response& res) { if (!base_dir_.empty() && detail::is_valid_path(req.path)) { @@ -1035,17 +1067,19 @@ inline void Server::process_request(Stream& strm) Request req; Response res; - if (!read_request_line(strm, req) || - !detail::read_headers(strm, req.headers)) { - // TODO: + if (!read_request_line(strm, req) || !detail::read_headers(strm, req.headers)) { + res.status = 400; + write_response(strm, req, res); return; } if (req.method == "POST") { if (!detail::read_content(strm, req, false)) { - // TODO: + res.status = 400; + write_response(strm, req, res); return; } + static std::string type = "application/x-www-form-urlencoded"; if (!req.get_header_value("Content-Type").compare(0, type.size(), type)) { detail::parse_query_text(req.body, req.params); @@ -1059,17 +1093,8 @@ inline void Server::process_request(Stream& strm) } else { res.status = 404; } - assert(res.status != -1); - if (400 <= res.status && error_handler_) { - error_handler_(req, res); - } - - detail::write_response(strm, req, res); - - if (logger_) { - logger_(req, res); - } + write_response(strm, req, res); } inline bool Server::read_and_close_socket(socket_t sock) @@ -1130,6 +1155,7 @@ inline bool Client::process_request(Stream& strm, const Request& req, Response& !detail::read_headers(strm, res.headers)) { return false; } + if (req.method != "HEAD") { if (!detail::read_content(strm, res, true, req.progress)) { return false; diff --git a/test/test.cc b/test/test.cc index e2deb5d..58fae80 100644 --- a/test/test.cc +++ b/test/test.cc @@ -329,6 +329,77 @@ TEST_F(ServerTest, InvalidBaseDir) EXPECT_EQ(true, svr_.set_base_dir(".")); } +TEST_F(ServerTest, EmptyRequest) +{ + auto res = cli_.get(""); + ASSERT_TRUE(res != nullptr); + EXPECT_EQ(400, res->status); +} + +TEST_F(ServerTest, LongRequest) +{ + auto res = cli_.get("/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/__ok__"); + + ASSERT_TRUE(res != nullptr); + EXPECT_EQ(404, res->status); +} + +TEST_F(ServerTest, TooLongRequest) +{ + auto res = cli_.get("/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/__ng___"); + + ASSERT_TRUE(res != nullptr); + EXPECT_EQ(400, res->status); +} + +TEST_F(ServerTest, LongHeader) +{ + Request req; + req.method = "GET"; + req.path = "/hi"; + + std::string host_and_port; + host_and_port += HOST; + host_and_port += ":"; + host_and_port += std::to_string(PORT); + + req.set_header("Host", host_and_port.c_str()); + req.set_header("Accept", "*/*"); + req.set_header("User-Agent", "cpp-httplib/0.1"); + + req.set_header("Header-Name", "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@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@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); + + auto res = std::make_shared(); + auto ret = cli_.send(req, *res); + + ASSERT_TRUE(ret); + EXPECT_EQ(200, res->status); +} + +TEST_F(ServerTest, TooLongHeader) +{ + Request req; + req.method = "GET"; + req.path = "/hi"; + + std::string host_and_port; + host_and_port += HOST; + host_and_port += ":"; + host_and_port += std::to_string(PORT); + + req.set_header("Host", host_and_port.c_str()); + req.set_header("Accept", "*/*"); + req.set_header("User-Agent", "cpp-httplib/0.1"); + + req.set_header("Header-Name", "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@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@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); + + auto res = std::make_shared(); + auto ret = cli_.send(req, *res); + + ASSERT_TRUE(ret); + EXPECT_EQ(400, res->status); +} + class ServerTestWithAI_PASSIVE : public ::testing::Test { protected: ServerTestWithAI_PASSIVE() From e90244e99282fbf5323e729c68be05185c95c2b1 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 3 Dec 2017 08:17:05 -0500 Subject: [PATCH 013/118] Fixed compiler warings (with -Wall and -Wextra) --- httplib.h | 4 ++-- test/Makefile | 2 +- test/test.cc | 10 +++++----- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/httplib.h b/httplib.h index fe8ec38..4bdc347 100644 --- a/httplib.h +++ b/httplib.h @@ -254,7 +254,7 @@ void split(const char* b, const char* e, char d, Fn fn) } } -inline bool socket_gets(Stream& strm, char* buf, int bufsiz) +inline bool socket_gets(Stream& strm, char* buf, size_t bufsiz) { // TODO: buffering for better performance size_t i = 0; @@ -299,7 +299,7 @@ inline void socket_printf(Stream& strm, const char* fmt, const Args& ...args) if (n >= BUFSIZ) { std::vector glowable_buf(BUFSIZ); - while (n >= glowable_buf.size()) { + while (n >= static_cast(glowable_buf.size())) { glowable_buf.resize(glowable_buf.size() * 2); n = snprintf(&glowable_buf[0], glowable_buf.size(), fmt, args...); } diff --git a/test/Makefile b/test/Makefile index 76809f3..9a78dda 100644 --- a/test/Makefile +++ b/test/Makefile @@ -1,6 +1,6 @@ CC = clang++ -CFLAGS = -std=c++14 -DGTEST_USE_OWN_TR1_TUPLE -I.. -I. +CFLAGS = -std=c++14 -DGTEST_USE_OWN_TR1_TUPLE -I.. -I. -Wall -Wextra #OPENSSL_SUPPORT = -DCPPHTTPLIB_OPENSSL_SUPPORT -I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib -lssl -lcrypto all : test diff --git a/test/test.cc b/test/test.cc index 58fae80..0cdc471 100644 --- a/test/test.cc +++ b/test/test.cc @@ -121,10 +121,10 @@ protected: virtual void SetUp() { svr_.set_base_dir("./www"); - svr_.get("/hi", [&](const Request& req, Response& res) { + svr_.get("/hi", [&](const Request& /*req*/, Response& res) { res.set_content("Hello World!", "text/plain"); }) - .get("/", [&](const Request& req, Response& res) { + .get("/", [&](const Request& /*req*/, Response& res) { res.set_redirect("/hi"); }) .post("/person", [&](const Request& req, Response& res) { @@ -143,7 +143,7 @@ protected: res.status = 404; } }) - .get("/stop", [&](const Request& req, Response& res) { + .get("/stop", [&](const Request& /*req*/, Response& /*res*/) { svr_.stop(); }); @@ -410,11 +410,11 @@ protected: , up_(false) {} virtual void SetUp() { - 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("/stop", [&](const Request& req, Response& res) { + svr_.get("/stop", [&](const Request& /*req*/, Response& /*res*/) { svr_.stop(); }); From 9bc2883090a7f89882bc7b5ded8df55105bc71d6 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 3 Dec 2017 21:25:38 -0500 Subject: [PATCH 014/118] Fixed #26 --- httplib.h | 143 ++++++++++++++++++++++++++++++++++++++------------- test/test.cc | 27 ++++++++++ 2 files changed, 133 insertions(+), 37 deletions(-) diff --git a/httplib.h b/httplib.h index 4bdc347..0647800 100644 --- a/httplib.h +++ b/httplib.h @@ -67,6 +67,8 @@ typedef int socket_t; namespace httplib { +enum class HttpVersion { v1_0 = 0, v1_1 }; + typedef std::map Map; typedef std::multimap MultiMap; typedef std::smatch Match; @@ -169,7 +171,7 @@ private: class Client { public: - Client(const char* host, int port); + Client(const char* host, int port, HttpVersion http_version = HttpVersion::v1_0); virtual ~Client(); std::shared_ptr get(const char* path, Progress callback = [](int64_t,int64_t){}); @@ -184,6 +186,7 @@ protected: const std::string host_; const int port_; + const HttpVersion http_version_; const std::string host_and_port_; private: @@ -220,7 +223,7 @@ private: class SSLClient : public Client { public: - SSLClient(const char* host, int port); + SSLClient(const char* host, int port, HttpVersion http_version = HttpVersion:v1_0); virtual ~SSLClient(); private: @@ -235,6 +238,8 @@ private: */ namespace detail { +static std::vector http_version_strings = { "HTTP/1.0", "HTTP/1.1" }; + template void split(const char* b, const char* e, char d, Fn fn) { @@ -567,36 +572,98 @@ inline bool read_headers(Stream& strm, MultiMap& headers) } template -bool read_content(Stream& strm, T& x, bool allow_no_content_length, Progress progress = [](int64_t,int64_t){}) +bool read_content_with_length(Stream& strm, T& x, size_t len, Progress progress) { - auto len = get_header_value_int(x.headers, "Content-Length", 0); - if (len) { - x.body.assign(len, 0); - auto r = 0; - while (r < len){ - auto r_incr = strm.read(&x.body[r], len - r); - if (r_incr <= 0) { - return false; - } - r += r_incr; - if (progress) { - progress(r, len); - } + x.body.assign(len, 0); + size_t r = 0; + while (r < len){ + auto r_incr = strm.read(&x.body[r], len - r); + if (r_incr <= 0) { + return false; } - } else if (allow_no_content_length) { - for (;;) { - char byte; - auto n = strm.read(&byte, 1); - if (n < 1) { - if (x.body.size() == 0) { - return true; // no body - } else { - break; - } - } - x.body += byte; + r += r_incr; + if (progress) { + progress(r, len); } } + + return true; +} + +template +bool read_content_without_length(Stream& strm, T& x) +{ + for (;;) { + char byte; + auto n = strm.read(&byte, 1); + if (n < 0) { + return false; + } else if (n == 0) { + break; + } + x.body += byte; + } + + return true; +} + +template +bool read_content_chunked(Stream& strm, T& x) +{ + const auto BUFSIZ_CHUNK_LEN = 16; + char buf[BUFSIZ_CHUNK_LEN]; + + if (!socket_gets(strm, buf, BUFSIZ_CHUNK_LEN)) { + return false; + } + + auto chunk_len = std::stoi(buf, 0, 16); + + while (chunk_len > 0){ + std::string chunk(chunk_len, 0); + + auto n = strm.read(&chunk[0], chunk_len); + if (n <= 0) { + return false; + } + + if (!socket_gets(strm, buf, BUFSIZ_CHUNK_LEN)) { + return false; + } + + if (strcmp(buf, "\r\n")) { + break; + } + + x.body += chunk; + + if (!socket_gets(strm, buf, BUFSIZ_CHUNK_LEN)) { + return false; + } + + chunk_len = std::stoi(buf, 0, 16); + } + + return true; +} + +template +bool read_content(Stream& strm, T& x, Progress progress = [](int64_t,int64_t){}) +{ + auto len = get_header_value_int(x.headers, "Content-Length", 0); + + if (len) { + return read_content_with_length(strm, x, len, progress); + } else { + auto encoding = get_header_value(x.headers, "Transfer-Encoding", ""); + + if (!strcmp(encoding, "chunked")) { + return read_content_chunked(strm, x); + } else { + return read_content_without_length(strm, x); + } + } + return true; } @@ -759,10 +826,10 @@ inline std::string decode_url(const std::string& s) return result; } -inline void write_request(Stream& strm, const Request& req) +inline void write_request(Stream& strm, const Request& req, const char* ver) { auto path = encode_url(req.path); - socket_printf(strm, "%s %s HTTP/1.0\r\n", req.method.c_str(), path.c_str()); + socket_printf(strm, "%s %s %s\r\n", req.method.c_str(), path.c_str(), ver); write_headers(strm, req); @@ -1074,7 +1141,7 @@ inline void Server::process_request(Stream& strm) } if (req.method == "POST") { - if (!detail::read_content(strm, req, false)) { + if (!detail::read_content(strm, req)) { res.status = 400; write_response(strm, req, res); return; @@ -1106,9 +1173,10 @@ inline bool Server::read_and_close_socket(socket_t sock) } // HTTP client implementation -inline Client::Client(const char* host, int port) +inline Client::Client(const char* host, int port, HttpVersion http_version) : host_(host) , port_(port) + , http_version_(http_version) , host_and_port_(host_ + ":" + std::to_string(port_)) { } @@ -1148,7 +1216,8 @@ inline bool Client::send(const Request& req, Response& res) inline bool Client::process_request(Stream& strm, const Request& req, Response& res) { // Send request - detail::write_request(strm, req); + auto ver = detail::http_version_strings[static_cast(http_version_)]; + detail::write_request(strm, req, ver); // Receive response if (!read_response_line(strm, res) || @@ -1157,7 +1226,7 @@ inline bool Client::process_request(Stream& strm, const Request& req, Response& } if (req.method != "HEAD") { - if (!detail::read_content(strm, res, true, req.progress)) { + if (!detail::read_content(strm, res, req.progress)) { return false; } } @@ -1334,7 +1403,7 @@ inline bool SSLServer::read_and_close_socket(socket_t sock) return detail::read_and_close_socket_ssl( sock, ctx_, SSL_accept, - [](SSL* ssl) {}, + [](SSL* /*ssl*/) {}, [this](Stream& strm) { process_request(strm); return true; @@ -1342,8 +1411,8 @@ inline bool SSLServer::read_and_close_socket(socket_t sock) } // SSL HTTP client implementation -inline SSLClient::SSLClient(const char* host, int port) - : Client(host, port) +inline SSLClient::SSLClient(const char* host, int port, HttpVersion http_version) + : Client(host, port, http_version) { ctx_ = SSL_CTX_new(SSLv23_client_method()); } diff --git a/test/test.cc b/test/test.cc index 0cdc471..cca1305 100644 --- a/test/test.cc +++ b/test/test.cc @@ -109,6 +109,33 @@ TEST(GetHeaderValueTest, RegularValueInt) EXPECT_EQ(100, val); } +void testChunkedEncoding(httplib::HttpVersion ver) +{ + auto host = "www.httpwatch.com"; + auto port = 80; + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + httplib::SSLClient cli(host, port, ver); +#else + httplib::Client cli(host, port, ver); +#endif + + auto res = cli.get("/httpgallery/chunked/chunkedimage.aspx?0.4153841143030137"); + ASSERT_TRUE(res != nullptr); + + std::string out; + httplib::detail::read_file("./image.jpg", out); + + EXPECT_EQ(200, res->status); + EXPECT_EQ(out, res->body); +} + +TEST(ChunkedEncodingTest, FromHTTPWatch) +{ + testChunkedEncoding(httplib::HttpVersion::v1_0); + testChunkedEncoding(httplib::HttpVersion::v1_1); +} + class ServerTest : public ::testing::Test { protected: ServerTest() From 3dded8c3e38912212004fe0d75143185f00a93e5 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 3 Dec 2017 21:25:50 -0500 Subject: [PATCH 015/118] Added -Wall and -Wextra to example/Makefile --- example/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example/Makefile b/example/Makefile index 575e0fa..bd48842 100644 --- a/example/Makefile +++ b/example/Makefile @@ -1,6 +1,6 @@ CC = clang++ -CFLAGS = -std=c++14 -I.. +CFLAGS = -std=c++14 -I.. -Wall -Wextra #OPENSSL_SUPPORT = -DCPPHTTPLIB_OPENSSL_SUPPORT -I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib -lssl -lcrypto all: server client hello simplesvr benchmark From 4fb2f51766554dcae4383df6150f60b448e9ae04 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 3 Dec 2017 22:31:00 -0500 Subject: [PATCH 016/118] Fixed #19 --- httplib.h | 49 +++++++++++++++++++++++++------------------------ test/test.cc | 31 +++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 24 deletions(-) diff --git a/httplib.h b/httplib.h index 0647800..3f8f41f 100644 --- a/httplib.h +++ b/httplib.h @@ -738,18 +738,21 @@ inline bool is_hex(char c, int& v) return false; } -inline int from_hex_to_i(const std::string& s, int i, int cnt, int& val) +inline bool from_hex_to_i(const std::string& s, int i, int cnt, int& val) { val = 0; - for (; s[i] && cnt; i++, cnt--) { + for (; cnt; i++, cnt--) { + if (!s[i]) { + return false; + } int v = 0; if (is_hex(s[i], v)) { val = val * 16 + v; } else { - break; + return false; } } - return --i; + return true; } inline size_t to_utf8(int code, char* buff) @@ -791,30 +794,28 @@ inline std::string decode_url(const std::string& s) for (int i = 0; s[i]; i++) { if (s[i] == '%') { - i++; - assert(s[i]); - - if (s[i] == '%') { - result += s[i]; - } else if (s[i] == 'u') { - // Unicode - i++; - assert(s[i]); - + if (s[i + 1] && s[i + 1] == 'u') { int val = 0; - i = from_hex_to_i(s, i, 4, val); - - char buff[4]; - size_t len = to_utf8(val, buff); - - if (len > 0) { - result.append(buff, len); + if (from_hex_to_i(s, i + 2, 4, val)) { + // 4 digits Unicode codes + char buff[4]; + size_t len = to_utf8(val, buff); + if (len > 0) { + result.append(buff, len); + } + i += 5; // 'u0000' + } else { + result += s[i]; } } else { - // HEX int val = 0; - i = from_hex_to_i(s, i, 2, val); - result += val; + if (from_hex_to_i(s, i + 1, 2, val)) { + // 2 digits hex codes + result += val; + i += 2; // '00' + } else { + result += s[i]; + } } } else if (s[i] == '+') { result += ' '; diff --git a/test/test.cc b/test/test.cc index cca1305..b3a2d47 100644 --- a/test/test.cc +++ b/test/test.cc @@ -151,6 +151,9 @@ protected: svr_.get("/hi", [&](const Request& /*req*/, Response& res) { res.set_content("Hello World!", "text/plain"); }) + .get("/endwith%", [&](const Request& /*req*/, Response& res) { + res.set_content("Hello World!", "text/plain"); + }) .get("/", [&](const Request& /*req*/, Response& res) { res.set_redirect("/hi"); }) @@ -427,6 +430,34 @@ TEST_F(ServerTest, TooLongHeader) EXPECT_EQ(400, res->status); } +TEST_F(ServerTest, PercentEncoding) +{ + auto res = cli_.get("/e%6edwith%"); + ASSERT_TRUE(res != nullptr); + EXPECT_EQ(200, res->status); +} + +TEST_F(ServerTest, PercentEncodingUnicode) +{ + auto res = cli_.get("/e%u006edwith%"); + ASSERT_TRUE(res != nullptr); + EXPECT_EQ(200, res->status); +} + +TEST_F(ServerTest, InvalidPercentEncoding) +{ + auto res = cli_.get("/%endwith%"); + ASSERT_TRUE(res != nullptr); + EXPECT_EQ(404, res->status); +} + +TEST_F(ServerTest, InvalidPercentEncodingUnicode) +{ + auto res = cli_.get("/%uendwith%"); + ASSERT_TRUE(res != nullptr); + EXPECT_EQ(404, res->status); +} + class ServerTestWithAI_PASSIVE : public ::testing::Test { protected: ServerTestWithAI_PASSIVE() From e58cfe8168049a062ced41ad00e01b7b297340b5 Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 5 Dec 2017 17:28:52 -0500 Subject: [PATCH 017/118] Fixed warnings --- example/benchmark.cc | 2 +- example/hello.cc | 2 +- example/server.cc | 8 ++++---- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/example/benchmark.cc b/example/benchmark.cc index 7e3c3dd..9d09df1 100644 --- a/example/benchmark.cc +++ b/example/benchmark.cc @@ -18,7 +18,7 @@ struct StopWatch { chrono::system_clock::time_point start_; }; -int main(int argc, char* argv[]) { +int main(void) { string body(1024 * 5, 'a'); httplib::Client cli("httpbin.org", 80); diff --git a/example/hello.cc b/example/hello.cc index de6f4fb..3b0c7f6 100644 --- a/example/hello.cc +++ b/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"); }); diff --git a/example/server.cc b/example/server.cc index b6e348d..8e1f702 100644 --- a/example/server.cc +++ b/example/server.cc @@ -72,11 +72,11 @@ int main(void) Server svr; #endif - 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!", "text/plain"); }); @@ -84,11 +84,11 @@ int main(void) 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(); }); - svr.set_error_handler([](const auto& req, auto& res) { + svr.set_error_handler([](const auto& /*req*/, auto& res) { const char* fmt = "

Error Status: %d

"; char buf[BUFSIZ]; snprintf(buf, sizeof(buf), fmt, res.status); From ea9c8ee46b015c96f10a40907cf999f6a0d48d9c Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 5 Dec 2017 19:15:52 -0500 Subject: [PATCH 018/118] Fixed build error --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 3f8f41f..4513f1e 100644 --- a/httplib.h +++ b/httplib.h @@ -223,7 +223,7 @@ private: class SSLClient : public Client { public: - SSLClient(const char* host, int port, HttpVersion http_version = HttpVersion:v1_0); + SSLClient(const char* host, int port, HttpVersion http_version = HttpVersion::v1_0); virtual ~SSLClient(); private: From bb8a1df7a383121338c62fd9ef8a31e4bbc287ca Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 5 Dec 2017 19:19:07 -0500 Subject: [PATCH 019/118] Fixed #21 --- example/simplesvr.cc | 45 ++++++++++++- httplib.h | 148 ++++++++++++++++++++++++++++++++++++++++--- test/Makefile | 2 +- test/test.cc | 55 ++++++++++++++++ 4 files changed, 238 insertions(+), 12 deletions(-) diff --git a/example/simplesvr.cc b/example/simplesvr.cc index 1c9d5a2..bcbe14d 100644 --- a/example/simplesvr.cc +++ b/example/simplesvr.cc @@ -28,6 +28,38 @@ string dump_headers(const MultiMap& headers) return s; } +string dump_multipart_files(const MultipartFiles& files) +{ + string s; + char buf[BUFSIZ]; + + s += "--------------------------------\n"; + + for (const auto& x: files) { + const auto& name = x.first; + const auto& file = x.second; + + snprintf(buf, sizeof(buf), "name: %s\n", name.c_str()); + s += buf; + + snprintf(buf, sizeof(buf), "filename: %s\n", file.filename.c_str()); + s += buf; + + snprintf(buf, sizeof(buf), "content type: %s\n", file.content_type.c_str()); + s += buf; + + snprintf(buf, sizeof(buf), "text offset: %lu\n", file.offset); + s += buf; + + snprintf(buf, sizeof(buf), "text length: %lu\n", file.length); + s += buf; + + s += "----------------\n"; + } + + return s; +} + string log(const Request& req, const Response& res) { string s; @@ -49,6 +81,7 @@ string log(const Request& req, const Response& res) s += buf; s += dump_headers(req.headers); + s += dump_multipart_files(req.files); s += "--------------------------------\n"; @@ -72,7 +105,15 @@ int main(int argc, const char** argv) Server svr; #endif - svr.set_error_handler([](const auto& req, auto& res) { + svr.post("/multipart", [](const auto& req, auto& res) { + auto body = + dump_headers(req.headers) + + dump_multipart_files(req.files); + + res.set_content(body, "text/plain"); + }); + + svr.set_error_handler([](const auto& /*req*/, auto& res) { const char* fmt = "

Error Status: %d

"; char buf[BUFSIZ]; snprintf(buf, sizeof(buf), fmt, res.status); @@ -83,7 +124,7 @@ int main(int argc, const char** argv) cout << log(req, res); }); - auto port = 80; + auto port = 8080; if (argc > 1) { port = atoi(argv[1]); } diff --git a/httplib.h b/httplib.h index 4513f1e..ab22db1 100644 --- a/httplib.h +++ b/httplib.h @@ -74,20 +74,32 @@ typedef std::multimap MultiMap; typedef std::smatch Match; typedef std::function Progress; +struct MultipartFile { + std::string filename; + std::string content_type; + size_t offset = 0; + size_t length = 0; +}; +typedef std::multimap MultipartFiles; + struct Request { - std::string method; - std::string path; - MultiMap headers; - std::string body; - Map params; - Match matches; - Progress progress; + std::string method; + std::string path; + MultiMap headers; + std::string body; + Map params; + MultipartFiles files; + Match matches; + Progress progress; bool has_header(const char* key) const; std::string get_header_value(const char* key) const; void set_header(const char* key, const char* val); bool has_param(const char* key) const; + + bool has_file(const char* key) const; + MultipartFile get_file_value(const char* key) const; }; struct Response { @@ -860,6 +872,101 @@ inline void parse_query_text(const std::string& s, Map& params) }); } +inline bool parse_multipart_boundary(const std::string& content_type, std::string& boundary) +{ + auto pos = content_type.find("boundary="); + if (pos == std::string::npos) { + return false; + } + + boundary = content_type.substr(pos + 9); + return true; +} + +inline bool parse_multipart_formdata( + const std::string& boundary, const std::string& body, MultipartFiles& files) +{ + static std::string dash = "--"; + static std::string crlf = "\r\n"; + + static std::regex re_content_type( + "Content-Type: (.*?)"); + + static std::regex re_content_disposition( + "Content-Disposition: form-data; name=\"(.*?)\"(?:; filename=\"(.*?)\")?"); + + auto dash_boundary = dash + boundary; + + auto pos = body.find(dash_boundary); + if (pos != 0) { + return false; + } + + pos += dash_boundary.size(); + + auto next_pos = body.find(crlf, pos); + if (next_pos == std::string::npos) { + return false; + } + + pos = next_pos + crlf.size(); + + while (pos < body.size()) { + next_pos = body.find(crlf, pos); + if (next_pos == std::string::npos) { + return false; + } + + std::string name; + MultipartFile file; + + auto header = body.substr(pos, (next_pos - pos)); + + while (pos != next_pos) { + std::smatch m; + if (std::regex_match(header, m, re_content_type)) { + file.content_type = m[1]; + } else if (std::regex_match(header, m, re_content_disposition)) { + name = m[1]; + file.filename = m[2]; + } + + pos = next_pos + crlf.size(); + + next_pos = body.find(crlf, pos); + if (next_pos == std::string::npos) { + return false; + } + + header = body.substr(pos, (next_pos - pos)); + } + + pos = next_pos + crlf.size(); + + next_pos = body.find(crlf + dash_boundary, pos); + + if (next_pos == std::string::npos) { + return false; + } + + file.offset = pos; + file.length = next_pos - pos; + + pos = next_pos + crlf.size() + dash_boundary.size(); + + next_pos = body.find(crlf, pos); + if (next_pos == std::string::npos) { + return false; + } + + files.insert(std::make_pair(name, file)); + + pos = next_pos + crlf.size(); + } + + return true; +} + #ifdef _MSC_VER class WSInit { public: @@ -899,6 +1006,20 @@ inline bool Request::has_param(const char* key) const return params.find(key) != params.end(); } +inline bool Request::has_file(const char* key) const +{ + return files.find(key) != files.end(); +} + +inline MultipartFile Request::get_file_value(const char* key) const +{ + auto it = files.find(key); + if (it != files.end()) { + return it->second; + } + return MultipartFile(); +} + // Response implementation inline bool Response::has_header(const char* key) const { @@ -1148,9 +1269,18 @@ inline void Server::process_request(Stream& strm) return; } - static std::string type = "application/x-www-form-urlencoded"; - if (!req.get_header_value("Content-Type").compare(0, type.size(), type)) { + const auto& content_type = req.get_header_value("Content-Type"); + + if (!content_type.find("application/x-www-form-urlencoded")) { detail::parse_query_text(req.body, req.params); + } else if(!content_type.find("multipart/form-data")) { + std::string boundary; + if (!detail::parse_multipart_boundary(content_type, boundary) || + !detail::parse_multipart_formdata(boundary, req.body, req.files)) { + res.status = 400; + write_response(strm, req, res); + return; + } } } diff --git a/test/Makefile b/test/Makefile index 9a78dda..d802397 100644 --- a/test/Makefile +++ b/test/Makefile @@ -1,6 +1,6 @@ CC = clang++ -CFLAGS = -std=c++14 -DGTEST_USE_OWN_TR1_TUPLE -I.. -I. -Wall -Wextra +CFLAGS = -std=c++11 -DGTEST_USE_OWN_TR1_TUPLE -I.. -I. -Wall -Wextra #OPENSSL_SUPPORT = -DCPPHTTPLIB_OPENSSL_SUPPORT -I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib -lssl -lcrypto all : test diff --git a/test/test.cc b/test/test.cc index b3a2d47..b38d08d 100644 --- a/test/test.cc +++ b/test/test.cc @@ -173,6 +173,36 @@ protected: res.status = 404; } }) + .post("/multipart", [&](const Request& req, Response& /*res*/) { + EXPECT_EQ(5u, req.files.size()); + ASSERT_TRUE(!req.has_file("???")); + + { + const auto& file = req.get_file_value("text1"); + EXPECT_EQ("", file.filename); + EXPECT_EQ("text default", req.body.substr(file.offset, file.length)); + } + + { + const auto& file = req.get_file_value("text2"); + EXPECT_EQ("", file.filename); + EXPECT_EQ("aωb", req.body.substr(file.offset, file.length)); + } + + { + const auto& file = req.get_file_value("file1"); + EXPECT_EQ("hello.txt", file.filename); + EXPECT_EQ("text/plain", file.content_type); + EXPECT_EQ("h\ne\n\nl\nl\no\n", req.body.substr(file.offset, file.length)); + } + + { + const auto& file = req.get_file_value("file3"); + EXPECT_EQ("", file.filename); + EXPECT_EQ("application/octet-stream", file.content_type); + EXPECT_EQ(0u, file.length); + } + }) .get("/stop", [&](const Request& /*req*/, Response& /*res*/) { svr_.stop(); }); @@ -458,6 +488,31 @@ TEST_F(ServerTest, InvalidPercentEncodingUnicode) EXPECT_EQ(404, res->status); } +TEST_F(ServerTest, MultipartFormData) +{ + Request req; + req.method = "POST"; + req.path = "/multipart"; + + std::string host_and_port; + host_and_port += HOST; + host_and_port += ":"; + host_and_port += std::to_string(PORT); + + req.set_header("Host", host_and_port.c_str()); + req.set_header("Accept", "*/*"); + req.set_header("User-Agent", "cpp-httplib/0.1"); + req.set_header("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundarysBREP3G013oUrLB4"); + + req.body = "------WebKitFormBoundarysBREP3G013oUrLB4\r\nContent-Disposition: form-data; name=\"text1\"\r\n\r\ntext default\r\n------WebKitFormBoundarysBREP3G013oUrLB4\r\nContent-Disposition: form-data; name=\"text2\"\r\n\r\naωb\r\n------WebKitFormBoundarysBREP3G013oUrLB4\r\nContent-Disposition: form-data; name=\"file1\"; filename=\"hello.txt\"\r\nContent-Type: text/plain\r\n\r\nh\ne\n\nl\nl\no\n\r\n------WebKitFormBoundarysBREP3G013oUrLB4\r\nContent-Disposition: form-data; name=\"file2\"; filename=\"world.json\"\r\nContent-Type: application/json\r\n\r\n{\n \"world\", true\n}\n\r\n------WebKitFormBoundarysBREP3G013oUrLB4\r\nContent-Disposition: form-data; name=\"file3\"; filename=\"\"\r\nContent-Type: application/octet-stream\r\n\r\n\r\n------WebKitFormBoundarysBREP3G013oUrLB4--\r\n"; + + auto res = std::make_shared(); + auto ret = cli_.send(req, *res); + + ASSERT_TRUE(ret); + EXPECT_EQ(200, res->status); +} + class ServerTestWithAI_PASSIVE : public ::testing::Test { protected: ServerTestWithAI_PASSIVE() From d2982531bdaa169168c243a1f811973f33606821 Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 5 Dec 2017 19:30:13 -0500 Subject: [PATCH 020/118] Updated README --- README.md | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 01e364d..2ed1de9 100644 --- a/README.md +++ b/README.md @@ -7,11 +7,11 @@ A C++11 header-only HTTP library. It's extremely easy to setup. Just include **httplib.h** file in your code! +Inspired by [Sinatra](http://www.sinatrarb.com/) and [express](https://github.com/visionmedia/express). + Server Example -------------- -Inspired by [Sinatra](http://www.sinatrarb.com/) and [express](https://github.com/visionmedia/express). - ```c++ #include @@ -71,9 +71,24 @@ svr.set_error_handler([](const auto& req, auto& res) { }); ``` +### `multipart/form-data` POST data + +```cpp +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"); + // file.filename; + // file.content_type; + auto body = req.body.substr(file.offset, file.length)); +}) +``` + Client Example -------------- +### GET + ```c++ #include #include @@ -89,6 +104,22 @@ int main(void) } ``` +### POST + +```c++ +res = cli.post("/post", "text", "text/plain"); +res = cli.post("/person", "name=john1¬e=coder", "application/x-www-form-urlencoded"); +``` + +### POST with parameters + +```c++ +httplib::Map params; +params["name"] = "john"; +params["note"] = "coder"; +auto res = cli.post("/post", params); +``` + ### With Progress Callback ```cpp From c3346a481538f01748e5a60b158ac18aac6197ac Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 5 Dec 2017 23:19:39 -0500 Subject: [PATCH 021/118] Changed to use std::multimap for params --- httplib.h | 45 +++++++++++++++++++++++++++------------------ test/test.cc | 26 +++++++++++++------------- 2 files changed, 40 insertions(+), 31 deletions(-) diff --git a/httplib.h b/httplib.h index ab22db1..4f1f225 100644 --- a/httplib.h +++ b/httplib.h @@ -69,9 +69,8 @@ namespace httplib enum class HttpVersion { v1_0 = 0, v1_1 }; -typedef std::map Map; -typedef std::multimap MultiMap; -typedef std::smatch Match; +typedef std::multimap MultiMap; +typedef std::smatch Match; typedef std::function Progress; struct MultipartFile { @@ -87,7 +86,7 @@ struct Request { std::string path; MultiMap headers; std::string body; - Map params; + MultiMap params; MultipartFiles files; Match matches; Progress progress; @@ -97,6 +96,7 @@ struct Request { void set_header(const char* key, const char* val); bool has_param(const char* key) const; + std::string get_param_value(const char* key) const; bool has_file(const char* key) const; MultipartFile get_file_value(const char* key) const; @@ -189,7 +189,7 @@ public: std::shared_ptr get(const char* path, Progress callback = [](int64_t,int64_t){}); std::shared_ptr head(const char* path); std::shared_ptr post(const char* path, const std::string& body, const char* content_type); - std::shared_ptr post(const char* path, const Map& params); + std::shared_ptr post(const char* path, const MultiMap& params); bool send(const Request& req, Response& res); @@ -540,19 +540,19 @@ inline const char* status_message(int status) } } -inline const char* get_header_value(const MultiMap& map, const char* key, const char* def) +inline const char* get_header_value(const MultiMap& headers, const char* key, const char* def) { - auto it = map.find(key); - if (it != map.end()) { + auto it = headers.find(key); + if (it != headers.end()) { return it->second.c_str(); } return def; } -inline int get_header_value_int(const MultiMap& map, const char* key, int def) +inline int get_header_value_int(const MultiMap& headers, const char* key, int def) { - auto it = map.find(key); - if (it != map.end()) { + auto it = headers.find(key); + if (it != headers.end()) { return std::stoi(it->second); } return def; @@ -576,7 +576,7 @@ inline bool read_headers(Stream& strm, MultiMap& headers) if (std::regex_match(buf, m, re)) { auto key = std::string(m[1]); auto val = std::string(m[2]); - headers.insert(std::make_pair(key, val)); + headers.emplace(key, val); } } @@ -856,7 +856,7 @@ inline void write_request(Stream& strm, const Request& req, const char* ver) } } -inline void parse_query_text(const std::string& s, Map& params) +inline void parse_query_text(const std::string& s, MultiMap& params) { split(&s[0], &s[s.size()], '&', [&](const char* b, const char* e) { std::string key; @@ -868,7 +868,7 @@ inline void parse_query_text(const std::string& s, Map& params) val.assign(b, e); } }); - params[key] = detail::decode_url(val); + params.emplace(key, detail::decode_url(val)); }); } @@ -959,7 +959,7 @@ inline bool parse_multipart_formdata( return false; } - files.insert(std::make_pair(name, file)); + files.emplace(name, file); pos = next_pos + crlf.size(); } @@ -998,7 +998,7 @@ inline std::string Request::get_header_value(const char* key) const inline void Request::set_header(const char* key, const char* val) { - headers.insert(std::make_pair(key, val)); + headers.emplace(key, val); } inline bool Request::has_param(const char* key) const @@ -1006,6 +1006,15 @@ inline bool Request::has_param(const char* key) const return params.find(key) != params.end(); } +inline std::string Request::get_param_value(const char* key) const +{ + auto it = params.find(key); + if (it != params.end()) { + return it->second; + } + return std::string(); +} + inline bool Request::has_file(const char* key) const { return files.find(key) != files.end(); @@ -1033,7 +1042,7 @@ inline std::string Response::get_header_value(const char* key) const inline void Response::set_header(const char* key, const char* val) { - headers.insert(std::make_pair(key, val)); + headers.emplace(key, val); } inline void Response::set_redirect(const char* url) @@ -1421,7 +1430,7 @@ inline std::shared_ptr Client::post( } inline std::shared_ptr Client::post( - const char* path, const Map& params) + const char* path, const MultiMap& params) { std::string query; for (auto it = params.begin(); it != params.end(); ++it) { diff --git a/test/test.cc b/test/test.cc index b38d08d..bb71f00 100644 --- a/test/test.cc +++ b/test/test.cc @@ -32,7 +32,7 @@ TEST(StartupTest, WSAStartup) TEST(SplitTest, ParseQueryString) { string s = "key1=val1&key2=val2&key3=val3"; - map dic; + MultiMap dic; detail::split(s.c_str(), s.c_str() + s.size(), '&', [&](const char* b, const char* e) { string key, val; @@ -43,24 +43,24 @@ TEST(SplitTest, ParseQueryString) val.assign(b, e); } }); - dic[key] = val; + dic.emplace(key, val); }); - EXPECT_EQ("val1", dic["key1"]); - EXPECT_EQ("val2", dic["key2"]); - EXPECT_EQ("val3", dic["key3"]); + EXPECT_EQ("val1", dic.find("key1")->second); + EXPECT_EQ("val2", dic.find("key2")->second); + EXPECT_EQ("val3", dic.find("key3")->second); } TEST(ParseQueryTest, ParseQueryString) { string s = "key1=val1&key2=val2&key3=val3"; - map dic; + MultiMap dic; detail::parse_query_text(s, dic); - EXPECT_EQ("val1", dic["key1"]); - EXPECT_EQ("val2", dic["key2"]); - EXPECT_EQ("val3", dic["key3"]); + EXPECT_EQ("val1", dic.find("key1")->second); + EXPECT_EQ("val2", dic.find("key2")->second); + EXPECT_EQ("val3", dic.find("key3")->second); } TEST(SocketTest, OpenClose) @@ -159,7 +159,7 @@ protected: }) .post("/person", [&](const Request& req, Response& res) { if (req.has_param("name") && req.has_param("note")) { - persons_[req.params.at("name")] = req.params.at("note"); + persons_[req.get_param_value("name")] = req.get_param_value("note"); } else { res.status = 400; } @@ -310,9 +310,9 @@ TEST_F(ServerTest, PostMethod2) ASSERT_TRUE(res != nullptr); ASSERT_EQ(404, res->status); - Map params; - params["name"] = "john2"; - params["note"] = "coder"; + MultiMap params; + params.emplace("name", "john2"); + params.emplace("note", "coder"); res = cli_.post("/person", params); ASSERT_TRUE(res != nullptr); From 315c11d6e28d48cb88e58d29f9ceac11870b4b25 Mon Sep 17 00:00:00 2001 From: yhirose Date: Wed, 6 Dec 2017 23:52:34 -0500 Subject: [PATCH 022/118] Implemented socket_reader --- httplib.h | 130 +++++++++++++++++++++++++++++++++------------------ test/test.cc | 4 +- 2 files changed, 86 insertions(+), 48 deletions(-) diff --git a/httplib.h b/httplib.h index 4f1f225..ad77c35 100644 --- a/httplib.h +++ b/httplib.h @@ -271,41 +271,69 @@ void split(const char* b, const char* e, char d, Fn fn) } } -inline bool socket_gets(Stream& strm, char* buf, size_t bufsiz) -{ - // TODO: buffering for better performance - size_t i = 0; +class socket_reader { +public: + socket_reader(Stream& strm, char* fixed_buffer, size_t fixed_buffer_size) + : strm_(strm) + , fixed_buffer_(fixed_buffer) + , fixed_buffer_size_(fixed_buffer_size) { + } - for (;;) { - char byte; - auto n = strm.read(&byte, 1); + const char* ptr() const { + if (glowable_buffer_.empty()) { + return fixed_buffer_; + } else { + return glowable_buffer_.data(); + } + } - if (n < 1) { - if (i == 0) { - return false; - } else { + bool getline() { + fixed_buffer_used_size_ = 0; + glowable_buffer_.clear(); + + size_t i = 0; + + for (;;) { + char byte; + auto n = strm_.read(&byte, 1); + + if (n < 1) { + if (i == 0) { + return false; + } else { + break; + } + } + + append(byte); + + if (byte == '\n') { break; } } - if (i == bufsiz) { - return false; - } + append('\0'); + return true; + } - buf[i++] = byte; - - if (byte == '\n') { - break; +private: + void append(char c) { + if (fixed_buffer_used_size_ < fixed_buffer_size_) { + fixed_buffer_[fixed_buffer_used_size_++] = c; + } else { + if (glowable_buffer_.empty()) { + glowable_buffer_.assign(fixed_buffer_, fixed_buffer_size_); + } + glowable_buffer_ += c; } } - if (i == bufsiz) { - return false; - } - - buf[i] = '\0'; - return true; -} + Stream& strm_; + char* fixed_buffer_; + const size_t fixed_buffer_size_; + size_t fixed_buffer_used_size_; + std::string glowable_buffer_; +}; template inline void socket_printf(Stream& strm, const char* fmt, const Args& ...args) @@ -562,18 +590,20 @@ inline bool read_headers(Stream& strm, MultiMap& headers) { static std::regex re("(.+?): (.+?)\r\n"); - const auto BUFSIZ_HEADER = 2048; - char buf[BUFSIZ_HEADER]; + const auto bufsiz = 2048; + char buf[bufsiz]; + + socket_reader reader(strm, buf, bufsiz); for (;;) { - if (!socket_gets(strm, buf, BUFSIZ_HEADER)) { + if (!reader.getline()) { return false; } - if (!strcmp(buf, "\r\n")) { + if (!strcmp(reader.ptr(), "\r\n")) { break; } std::cmatch m; - if (std::regex_match(buf, m, re)) { + if (std::regex_match(reader.ptr(), m, re)) { auto key = std::string(m[1]); auto val = std::string(m[2]); headers.emplace(key, val); @@ -622,14 +652,16 @@ bool read_content_without_length(Stream& strm, T& x) template bool read_content_chunked(Stream& strm, T& x) { - const auto BUFSIZ_CHUNK_LEN = 16; - char buf[BUFSIZ_CHUNK_LEN]; + const auto bufsiz = 16; + char buf[bufsiz]; - if (!socket_gets(strm, buf, BUFSIZ_CHUNK_LEN)) { + socket_reader reader(strm, buf, bufsiz); + + if (!reader.getline()) { return false; } - auto chunk_len = std::stoi(buf, 0, 16); + auto chunk_len = std::stoi(reader.ptr(), 0, 16); while (chunk_len > 0){ std::string chunk(chunk_len, 0); @@ -639,21 +671,21 @@ bool read_content_chunked(Stream& strm, T& x) return false; } - if (!socket_gets(strm, buf, BUFSIZ_CHUNK_LEN)) { + if (!reader.getline()) { return false; } - if (strcmp(buf, "\r\n")) { + if (strcmp(reader.ptr(), "\r\n")) { break; } x.body += chunk; - if (!socket_gets(strm, buf, BUFSIZ_CHUNK_LEN)) { + if (!reader.getline()) { return false; } - chunk_len = std::stoi(buf, 0, 16); + chunk_len = std::stoi(reader.ptr(), 0, 16); } return true; @@ -1169,16 +1201,19 @@ inline void Server::stop() inline bool Server::read_request_line(Stream& strm, Request& req) { - const auto BUFSIZ_REQUESTLINE = 2048; - char buf[BUFSIZ_REQUESTLINE]; - if (!detail::socket_gets(strm, buf, BUFSIZ_REQUESTLINE)) { + const auto bufsiz = 2048; + char buf[bufsiz]; + + detail::socket_reader reader(strm, buf, bufsiz); + + if (!reader.getline()) { return false; } static std::regex re("(GET|HEAD|POST) ([^?]+)(?:\\?(.+?))? HTTP/1\\.[01]\r\n"); std::cmatch m; - if (std::regex_match(buf, m, re)) { + if (std::regex_match(reader.ptr(), m, re)) { req.method = std::string(m[1]); req.path = detail::decode_url(m[2]); @@ -1327,16 +1362,19 @@ inline Client::~Client() inline bool Client::read_response_line(Stream& strm, Response& res) { - const auto BUFSIZ_RESPONSELINE = 2048; - char buf[BUFSIZ_RESPONSELINE]; - if (!detail::socket_gets(strm, buf, BUFSIZ_RESPONSELINE)) { + const auto bufsiz = 2048; + char buf[bufsiz]; + + detail::socket_reader reader(strm, buf, bufsiz); + + if (!reader.getline()) { return false; } const static std::regex re("HTTP/1\\.[01] (\\d+?) .+\r\n"); std::cmatch m; - if (std::regex_match(buf, m, re)) { + if (std::regex_match(reader.ptr(), m, re)) { res.status = std::stoi(std::string(m[1])); } diff --git a/test/test.cc b/test/test.cc index bb71f00..ea4d268 100644 --- a/test/test.cc +++ b/test/test.cc @@ -409,7 +409,7 @@ TEST_F(ServerTest, TooLongRequest) auto res = cli_.get("/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/__ng___"); ASSERT_TRUE(res != nullptr); - EXPECT_EQ(400, res->status); + EXPECT_EQ(404, res->status); } TEST_F(ServerTest, LongHeader) @@ -457,7 +457,7 @@ TEST_F(ServerTest, TooLongHeader) auto ret = cli_.send(req, *res); ASSERT_TRUE(ret); - EXPECT_EQ(400, res->status); + EXPECT_EQ(200, res->status); } TEST_F(ServerTest, PercentEncoding) From 6a608b3ed43aebdb23bf58bcd7ff65454e5fdc1f Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 7 Dec 2017 00:05:43 -0500 Subject: [PATCH 023/118] Fixed Unit test failures on Windows --- httplib.h | 4 ++++ test/test.cc | 3 +-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/httplib.h b/httplib.h index ad77c35..b5868be 100644 --- a/httplib.h +++ b/httplib.h @@ -1383,6 +1383,10 @@ inline bool Client::read_response_line(Stream& strm, Response& res) inline bool Client::send(const Request& req, Response& res) { + if (req.path.empty()) { + return false; + } + auto sock = detail::create_client_socket(host_.c_str(), port_); if (sock == -1) { return false; diff --git a/test/test.cc b/test/test.cc index ea4d268..2edf20e 100644 --- a/test/test.cc +++ b/test/test.cc @@ -392,8 +392,7 @@ TEST_F(ServerTest, InvalidBaseDir) TEST_F(ServerTest, EmptyRequest) { auto res = cli_.get(""); - ASSERT_TRUE(res != nullptr); - EXPECT_EQ(400, res->status); + ASSERT_TRUE(res == nullptr); } TEST_F(ServerTest, LongRequest) From 929c546f17c135557d6aa1a5d18bc5cece0a9c23 Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 7 Dec 2017 00:20:59 -0500 Subject: [PATCH 024/118] Fixed #29 --- httplib.h | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/httplib.h b/httplib.h index b5868be..3a688bb 100644 --- a/httplib.h +++ b/httplib.h @@ -8,7 +8,7 @@ #ifndef _CPPHTTPLIB_HTTPLIB_H_ #define _CPPHTTPLIB_HTTPLIB_H_ -#ifdef _MSC_VER +#ifdef _WIN32 #ifndef _CRT_SECURE_NO_WARNINGS #define _CRT_SECURE_NO_WARNINGS #endif //_CRT_SECURE_NO_WARNINGS @@ -60,6 +60,14 @@ typedef int socket_t; #include #include +#ifdef __has_include +#if __has_include() +#ifndef CPPHTTPLIB_OPENSSL_SUPPORT +#define CPPHTTPLIB_OPENSSL_SUPPORT +#endif +#endif +#endif + #ifdef CPPHTTPLIB_OPENSSL_SUPPORT #include #endif @@ -357,7 +365,7 @@ inline void socket_printf(Stream& strm, const char* fmt, const Args& ...args) inline int close_socket(socket_t sock) { -#ifdef _MSC_VER +#ifdef _WIN32 return closesocket(sock); #else return close(sock); @@ -375,7 +383,7 @@ inline bool read_and_close_socket(socket_t sock, T callback) inline int shutdown_socket(socket_t sock) { -#ifdef _MSC_VER +#ifdef _WIN32 return shutdown(sock, SD_BOTH); #else return shutdown(sock, SHUT_RDWR); @@ -385,7 +393,7 @@ inline int shutdown_socket(socket_t sock) template socket_t create_socket(const char* host, int port, Fn fn, int socket_flags = 0) { -#ifdef _MSC_VER +#ifdef _WIN32 int opt = SO_SYNCHRONOUS_NONALERT; setsockopt(INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE, (char*)&opt, sizeof(opt)); #endif @@ -999,7 +1007,7 @@ inline bool parse_multipart_formdata( return true; } -#ifdef _MSC_VER +#ifdef _WIN32 class WSInit { public: WSInit() { @@ -1123,7 +1131,7 @@ inline int SocketStream::write(const char* ptr) inline Server::Server() : svr_sock_(-1) { -#ifndef _MSC_VER +#ifndef _WIN32 signal(SIGPIPE, SIG_IGN); #endif } From 0968d71c96fa3589b46c29c0e4adb0d8f38ab082 Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 7 Dec 2017 08:28:06 -0500 Subject: [PATCH 025/118] Fixed build problems on Msys2 --- httplib.h | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/httplib.h b/httplib.h index 3a688bb..e5afb54 100644 --- a/httplib.h +++ b/httplib.h @@ -16,18 +16,16 @@ #define _CRT_NONSTDC_NO_DEPRECATE #endif //_CRT_NONSTDC_NO_DEPRECATE -#ifndef SO_SYNCHRONOUS_NONALERT -#define SO_SYNCHRONOUS_NONALERT 0x20 -#endif -#ifndef SO_OPENTYPE -#define SO_OPENTYPE 0x7008 -#endif -#if (_MSC_VER < 1900) +#if (_MSC_VER && _MSC_VER < 1900) #define snprintf _snprintf_s #endif +#ifndef S_ISREG #define S_ISREG(m) (((m)&S_IFREG)==S_IFREG) +#endif +#ifndef S_ISDIR #define S_ISDIR(m) (((m)&S_IFDIR)==S_IFDIR) +#endif #include #include @@ -394,6 +392,9 @@ template socket_t create_socket(const char* host, int port, Fn fn, int socket_flags = 0) { #ifdef _WIN32 +#define SO_SYNCHRONOUS_NONALERT 0x20 +#define SO_OPENTYPE 0x7008 + int opt = SO_SYNCHRONOUS_NONALERT; setsockopt(INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE, (char*)&opt, sizeof(opt)); #endif From f35f2b23faf05867ca523815bb9676b09253ea28 Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 7 Dec 2017 13:10:20 -0500 Subject: [PATCH 026/118] Fixed problems with Visual Studio 2013 --- httplib.h | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/httplib.h b/httplib.h index e5afb54..4f4531e 100644 --- a/httplib.h +++ b/httplib.h @@ -11,12 +11,12 @@ #ifdef _WIN32 #ifndef _CRT_SECURE_NO_WARNINGS #define _CRT_SECURE_NO_WARNINGS -#endif //_CRT_SECURE_NO_WARNINGS +#endif #ifndef _CRT_NONSTDC_NO_DEPRECATE #define _CRT_NONSTDC_NO_DEPRECATE -#endif //_CRT_NONSTDC_NO_DEPRECATE +#endif -#if (_MSC_VER && _MSC_VER < 1900) +#if defined(_MSC_VER) && _MSC_VER < 1900 #define snprintf _snprintf_s #endif @@ -344,15 +344,21 @@ private: template inline void socket_printf(Stream& strm, const char* fmt, const Args& ...args) { - char buf[BUFSIZ]; - auto n = snprintf(buf, BUFSIZ, fmt, args...); - if (n > 0) { - if (n >= BUFSIZ) { - std::vector glowable_buf(BUFSIZ); + const auto bufsiz = 2048; + char buf[bufsiz]; - while (n >= static_cast(glowable_buf.size())) { + auto n = snprintf(buf, bufsiz - 1, fmt, args...); + if (n > 0) { + if (n >= bufsiz - 1) { + std::vector glowable_buf(bufsiz); + + while (n >= static_cast(glowable_buf.size() - 1)) { glowable_buf.resize(glowable_buf.size() * 2); - n = snprintf(&glowable_buf[0], glowable_buf.size(), fmt, args...); +#if defined(_MSC_VER) && _MSC_VER < 1900 + n = _snprintf_s(&glowable_buf[0], glowable_buf.size(), glowable_buf.size() - 1, fmt, args...); +#else + n = snprintf(&glowable_buf[0], glowable_buf.size() - 1, fmt, args...); +#endif } strm.write(&glowable_buf[0], n); } else { @@ -763,7 +769,7 @@ inline std::string encode_url(const std::string& s) if (s[i] < 0) { result += '%'; char hex[4]; - size_t len = snprintf(hex, sizeof(hex), "%02X", (unsigned char)s[i]); + size_t len = snprintf(hex, sizeof(hex) - 1, "%02X", (unsigned char)s[i]); assert(len == 2); result.append(hex, len); } else { From e557282641b363f988e493cc5482cb78f05a65a6 Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 7 Dec 2017 13:18:47 -0500 Subject: [PATCH 027/118] Rename argument name --- httplib.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/httplib.h b/httplib.h index 4f4531e..97c1b46 100644 --- a/httplib.h +++ b/httplib.h @@ -727,19 +727,19 @@ bool read_content(Stream& strm, T& x, Progress progress = [](int64_t,int64_t){}) } template -inline void write_headers(Stream& strm, const T& res) +inline void write_headers(Stream& strm, const T& info) { strm.write("Connection: close\r\n"); - for (const auto& x: res.headers) { + for (const auto& x: info.headers) { if (x.first != "Content-Type" && x.first != "Content-Length") { socket_printf(strm, "%s: %s\r\n", x.first.c_str(), x.second.c_str()); } } - auto t = get_header_value(res.headers, "Content-Type", "text/plain"); + auto t = get_header_value(info.headers, "Content-Type", "text/plain"); socket_printf(strm, "Content-Type: %s\r\n", t); - socket_printf(strm, "Content-Length: %ld\r\n", res.body.size()); + socket_printf(strm, "Content-Length: %ld\r\n", info.body.size()); strm.write("\r\n"); } From cca90184aadde05504a7ae90d89700dfe511c184 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 9 Dec 2017 16:45:40 -0500 Subject: [PATCH 028/118] Raname refactoring --- httplib.h | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/httplib.h b/httplib.h index 97c1b46..cc2545c 100644 --- a/httplib.h +++ b/httplib.h @@ -277,9 +277,9 @@ void split(const char* b, const char* e, char d, Fn fn) } } -class socket_reader { +class stream_line_reader { public: - socket_reader(Stream& strm, char* fixed_buffer, size_t fixed_buffer_size) + stream_line_reader(Stream& strm, char* fixed_buffer, size_t fixed_buffer_size) : strm_(strm) , fixed_buffer_(fixed_buffer) , fixed_buffer_size_(fixed_buffer_size) { @@ -342,7 +342,7 @@ private: }; template -inline void socket_printf(Stream& strm, const char* fmt, const Args& ...args) +inline void stream_write_format(Stream& strm, const char* fmt, const Args& ...args) { const auto bufsiz = 2048; char buf[bufsiz]; @@ -541,7 +541,7 @@ inline std::string file_extension(const std::string& path) inline const char* content_type(const std::string& path) { - auto ext = detail::file_extension(path); + auto ext = file_extension(path); if (ext == "txt") { return "text/plain"; } else if (ext == "html") { @@ -608,7 +608,7 @@ inline bool read_headers(Stream& strm, MultiMap& headers) const auto bufsiz = 2048; char buf[bufsiz]; - socket_reader reader(strm, buf, bufsiz); + stream_line_reader reader(strm, buf, bufsiz); for (;;) { if (!reader.getline()) { @@ -670,7 +670,7 @@ bool read_content_chunked(Stream& strm, T& x) const auto bufsiz = 16; char buf[bufsiz]; - socket_reader reader(strm, buf, bufsiz); + stream_line_reader reader(strm, buf, bufsiz); if (!reader.getline()) { return false; @@ -733,19 +733,19 @@ inline void write_headers(Stream& strm, const T& info) for (const auto& x: info.headers) { if (x.first != "Content-Type" && x.first != "Content-Length") { - socket_printf(strm, "%s: %s\r\n", x.first.c_str(), x.second.c_str()); + stream_write_format(strm, "%s: %s\r\n", x.first.c_str(), x.second.c_str()); } } auto t = get_header_value(info.headers, "Content-Type", "text/plain"); - socket_printf(strm, "Content-Type: %s\r\n", t); - socket_printf(strm, "Content-Length: %ld\r\n", info.body.size()); + stream_write_format(strm, "Content-Type: %s\r\n", t); + stream_write_format(strm, "Content-Length: %ld\r\n", info.body.size()); strm.write("\r\n"); } inline void write_response(Stream& strm, const Request& req, const Response& res) { - socket_printf(strm, "HTTP/1.0 %d %s\r\n", res.status, status_message(res.status)); + stream_write_format(strm, "HTTP/1.0 %d %s\r\n", res.status, status_message(res.status)); write_headers(strm, res); @@ -889,7 +889,7 @@ inline std::string decode_url(const std::string& s) inline void write_request(Stream& strm, const Request& req, const char* ver) { auto path = encode_url(req.path); - socket_printf(strm, "%s %s %s\r\n", req.method.c_str(), path.c_str(), ver); + stream_write_format(strm, "%s %s %s\r\n", req.method.c_str(), path.c_str(), ver); write_headers(strm, req); @@ -915,7 +915,7 @@ inline void parse_query_text(const std::string& s, MultiMap& params) val.assign(b, e); } }); - params.emplace(key, detail::decode_url(val)); + params.emplace(key, decode_url(val)); }); } @@ -1219,7 +1219,7 @@ inline bool Server::read_request_line(Stream& strm, Request& req) const auto bufsiz = 2048; char buf[bufsiz]; - detail::socket_reader reader(strm, buf, bufsiz); + detail::stream_line_reader reader(strm, buf, bufsiz); if (!reader.getline()) { return false; @@ -1380,7 +1380,7 @@ inline bool Client::read_response_line(Stream& strm, Response& res) const auto bufsiz = 2048; char buf[bufsiz]; - detail::socket_reader reader(strm, buf, bufsiz); + detail::stream_line_reader reader(strm, buf, bufsiz); if (!reader.getline()) { return false; From 459f197ed0d851409888e2c5a67ed1c9b4578148 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 10 Dec 2017 15:11:03 -0500 Subject: [PATCH 029/118] Fixed #30 --- httplib.h | 152 +++++++++++++++++++++++++++++------------------------- 1 file changed, 81 insertions(+), 71 deletions(-) diff --git a/httplib.h b/httplib.h index cc2545c..95d5a27 100644 --- a/httplib.h +++ b/httplib.h @@ -130,6 +130,9 @@ public: virtual int read(char* ptr, size_t size) = 0; virtual int write(const char* ptr, size_t size1) = 0; virtual int write(const char* ptr) = 0; + + template + void write_format(const char* fmt, const Args& ...args); }; class SocketStream : public Stream { @@ -209,7 +212,7 @@ protected: private: bool read_response_line(Stream& strm, Response& res); - void add_default_headers(Request& req); + void write_request(Stream& strm, const Request& req, const char* ver); virtual bool read_and_close_socket(socket_t sock, const Request& req, Response& res); }; @@ -277,6 +280,8 @@ void split(const char* b, const char* e, char d, Fn fn) } } +// NOTE: until the read size reaches `fixed_buffer_size`, use `fixed_buffer` +// to store data. The call can set memory on stack for performance. class stream_line_reader { public: stream_line_reader(Stream& strm, char* fixed_buffer, size_t fixed_buffer_size) @@ -341,32 +346,6 @@ private: std::string glowable_buffer_; }; -template -inline void stream_write_format(Stream& strm, const char* fmt, const Args& ...args) -{ - const auto bufsiz = 2048; - char buf[bufsiz]; - - auto n = snprintf(buf, bufsiz - 1, fmt, args...); - if (n > 0) { - if (n >= bufsiz - 1) { - std::vector glowable_buf(bufsiz); - - while (n >= static_cast(glowable_buf.size() - 1)) { - glowable_buf.resize(glowable_buf.size() * 2); -#if defined(_MSC_VER) && _MSC_VER < 1900 - n = _snprintf_s(&glowable_buf[0], glowable_buf.size(), glowable_buf.size() - 1, fmt, args...); -#else - n = snprintf(&glowable_buf[0], glowable_buf.size() - 1, fmt, args...); -#endif - } - strm.write(&glowable_buf[0], n); - } else { - strm.write(buf, n); - } - } -} - inline int close_socket(socket_t sock) { #ifdef _WIN32 @@ -733,25 +712,13 @@ inline void write_headers(Stream& strm, const T& info) for (const auto& x: info.headers) { if (x.first != "Content-Type" && x.first != "Content-Length") { - stream_write_format(strm, "%s: %s\r\n", x.first.c_str(), x.second.c_str()); + strm.write_format("%s: %s\r\n", x.first.c_str(), x.second.c_str()); } } auto t = get_header_value(info.headers, "Content-Type", "text/plain"); - stream_write_format(strm, "Content-Type: %s\r\n", t); - stream_write_format(strm, "Content-Length: %ld\r\n", info.body.size()); - strm.write("\r\n"); -} - -inline void write_response(Stream& strm, const Request& req, const Response& res) -{ - stream_write_format(strm, "HTTP/1.0 %d %s\r\n", res.status, status_message(res.status)); - - write_headers(strm, res); - - if (!res.body.empty() && req.method != "HEAD") { - strm.write(res.body.c_str(), res.body.size()); - } + strm.write_format("Content-Type: %s\r\n", t); + strm.write_format("Content-Length: %ld\r\n", info.body.size()); } inline std::string encode_url(const std::string& s) @@ -886,23 +853,6 @@ inline std::string decode_url(const std::string& s) return result; } -inline void write_request(Stream& strm, const Request& req, const char* ver) -{ - auto path = encode_url(req.path); - stream_write_format(strm, "%s %s %s\r\n", req.method.c_str(), path.c_str(), ver); - - write_headers(strm, req); - - if (!req.body.empty()) { - if (req.has_header("application/x-www-form-urlencoded")) { - auto str = encode_url(req.body); - strm.write(str.c_str(), str.size()); - } else { - strm.write(req.body.c_str(), req.body.size()); - } - } -} - inline void parse_query_text(const std::string& s, MultiMap& params) { split(&s[0], &s[s.size()], '&', [&](const char* b, const char* e) { @@ -1110,6 +1060,33 @@ inline void Response::set_content(const std::string& s, const char* content_type set_header("Content-Type", content_type); } +// Rstream implementation +template +inline void Stream::write_format(const char* fmt, const Args& ...args) +{ + const auto bufsiz = 2048; + char buf[bufsiz]; + + auto n = snprintf(buf, bufsiz - 1, fmt, args...); + if (n > 0) { + if (n >= bufsiz - 1) { + std::vector glowable_buf(bufsiz); + + while (n >= static_cast(glowable_buf.size() - 1)) { + glowable_buf.resize(glowable_buf.size() * 2); +#if defined(_MSC_VER) && _MSC_VER < 1900 + n = _snprintf_s(&glowable_buf[0], glowable_buf.size(), glowable_buf.size() - 1, fmt, args...); +#else + n = snprintf(&glowable_buf[0], glowable_buf.size() - 1, fmt, args...); +#endif + } + write(&glowable_buf[0], n); + } else { + write(buf, n); + } + } +} + // Socket stream implementation inline SocketStream::SocketStream(socket_t sock): sock_(sock) { @@ -1252,7 +1229,16 @@ inline void Server::write_response(Stream& strm, const Request& req, Response& r error_handler_(req, res); } - detail::write_response(strm, req, res); + strm.write_format( + "HTTP/1.0 %d %s\r\n", + res.status, detail::status_message(res.status)); + + detail::write_headers(strm, res); + strm.write("\r\n"); + + if (!res.body.empty() && req.method != "HEAD") { + strm.write(res.body.c_str(), res.body.size()); + } if (logger_) { logger_(req, res); @@ -1410,11 +1396,45 @@ inline bool Client::send(const Request& req, Response& res) return read_and_close_socket(sock, req, res); } +inline void Client::write_request(Stream& strm, const Request& req, const char* ver) +{ + auto path = detail::encode_url(req.path); + + // Request line + strm.write_format( + "%s %s %s\r\n", req.method.c_str(), path.c_str(), ver); + + // Headers + strm.write_format("Host: %s\r\n", host_and_port_.c_str()); + + if (!req.has_header("Accept")) { + strm.write("Accept: */*\r\n"); + } + + if (!req.has_header("User-Agent")) { + strm.write("User-Agent: cpp-httplib/0.1\r\n"); + } + + detail::write_headers(strm, req); + + strm.write("\r\n"); + + // Body + if (!req.body.empty()) { + if (req.has_header("application/x-www-form-urlencoded")) { + auto str = detail::encode_url(req.body); + strm.write(str.c_str(), str.size()); + } else { + strm.write(req.body.c_str(), req.body.size()); + } + } +} + inline bool Client::process_request(Stream& strm, const Request& req, Response& res) { // Send request auto ver = detail::http_version_strings[static_cast(http_version_)]; - detail::write_request(strm, req, ver); + write_request(strm, req, ver); // Receive response if (!read_response_line(strm, res) || @@ -1438,20 +1458,12 @@ inline bool Client::read_and_close_socket(socket_t sock, const Request& req, Res }); } -inline void Client::add_default_headers(Request& req) -{ - req.set_header("Host", host_and_port_.c_str()); - req.set_header("Accept", "*/*"); - req.set_header("User-Agent", "cpp-httplib/0.1"); -} - inline std::shared_ptr Client::get(const char* path, Progress callback) { Request req; req.method = "GET"; req.path = path; req.progress = callback; - add_default_headers(req); auto res = std::make_shared(); @@ -1463,7 +1475,6 @@ inline std::shared_ptr Client::head(const char* path) Request req; req.method = "HEAD"; req.path = path; - add_default_headers(req); auto res = std::make_shared(); @@ -1476,7 +1487,6 @@ inline std::shared_ptr Client::post( Request req; req.method = "POST"; req.path = path; - add_default_headers(req); req.set_header("Content-Type", content_type); req.body = body; From 31e53d21e47d0427a56492221329c97f4ddbd7ea Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 10 Dec 2017 22:34:37 -0500 Subject: [PATCH 030/118] Fixed #32 --- example/server.cc | 2 +- example/simplesvr.cc | 2 +- httplib.h | 47 ++++++++++++++++++++++++++++++++++---------- test/test.cc | 23 +++++++++++++++------- 4 files changed, 55 insertions(+), 19 deletions(-) diff --git a/example/server.cc b/example/server.cc index 8e1f702..a4b7f6d 100644 --- a/example/server.cc +++ b/example/server.cc @@ -13,7 +13,7 @@ using namespace httplib; -std::string dump_headers(const MultiMap& headers) +std::string dump_headers(const Headers& headers) { std::string s; char buf[BUFSIZ]; diff --git a/example/simplesvr.cc b/example/simplesvr.cc index bcbe14d..13b6b66 100644 --- a/example/simplesvr.cc +++ b/example/simplesvr.cc @@ -15,7 +15,7 @@ using namespace httplib; using namespace std; -string dump_headers(const MultiMap& headers) +string dump_headers(const Headers& headers) { string s; char buf[BUFSIZ]; diff --git a/httplib.h b/httplib.h index 95d5a27..f72c9b4 100644 --- a/httplib.h +++ b/httplib.h @@ -73,9 +73,25 @@ typedef int socket_t; namespace httplib { +namespace detail { + +struct ci { + bool operator() (const std::string & s1, const std::string & s2) const { + return std::lexicographical_compare( + s1.begin(), s1.end(), + s2.begin(), s2.end(), + [](char c1, char c2) { + return std::tolower(c1) < std::tolower(c2); + }); + } +}; + +} // namespace detail + enum class HttpVersion { v1_0 = 0, v1_1 }; -typedef std::multimap MultiMap; +typedef std::multimap Headers; +typedef std::multimap Params; typedef std::smatch Match; typedef std::function Progress; @@ -90,9 +106,9 @@ typedef std::multimap MultipartFiles; struct Request { std::string method; std::string path; - MultiMap headers; + Headers headers; std::string body; - MultiMap params; + Params params; MultipartFiles files; Match matches; Progress progress; @@ -110,7 +126,7 @@ struct Request { struct Response { int status; - MultiMap headers; + Headers headers; std::string body; bool has_header(const char* key) const; @@ -198,7 +214,7 @@ public: std::shared_ptr get(const char* path, Progress callback = [](int64_t,int64_t){}); std::shared_ptr head(const char* path); std::shared_ptr post(const char* path, const std::string& body, const char* content_type); - std::shared_ptr post(const char* path, const MultiMap& params); + std::shared_ptr post(const char* path, const Params& params); bool send(const Request& req, Response& res); @@ -562,7 +578,7 @@ inline const char* status_message(int status) } } -inline const char* get_header_value(const MultiMap& headers, const char* key, const char* def) +inline const char* get_header_value(const Headers& headers, const char* key, const char* def) { auto it = headers.find(key); if (it != headers.end()) { @@ -571,7 +587,7 @@ inline const char* get_header_value(const MultiMap& headers, const char* key, co return def; } -inline int get_header_value_int(const MultiMap& headers, const char* key, int def) +inline int get_header_value_int(const Headers& headers, const char* key, int def) { auto it = headers.find(key); if (it != headers.end()) { @@ -580,7 +596,7 @@ inline int get_header_value_int(const MultiMap& headers, const char* key, int de return def; } -inline bool read_headers(Stream& strm, MultiMap& headers) +inline bool read_headers(Stream& strm, Headers& headers) { static std::regex re("(.+?): (.+?)\r\n"); @@ -853,7 +869,7 @@ inline std::string decode_url(const std::string& s) return result; } -inline void parse_query_text(const std::string& s, MultiMap& params) +inline void parse_query_text(const std::string& s, Params& params) { split(&s[0], &s[s.size()], '&', [&](const char* b, const char* e) { std::string key; @@ -980,6 +996,17 @@ public: static WSInit wsinit_; #endif +inline std::string to_lower(const char* beg, const char* end) +{ + std::string out; + auto it = beg; + while (it != end) { + out += ::tolower(*it); + it++; + } + return out; +} + } // namespace detail // Request implementation @@ -1497,7 +1524,7 @@ inline std::shared_ptr Client::post( } inline std::shared_ptr Client::post( - const char* path, const MultiMap& params) + const char* path, const Params& params) { std::string query; for (auto it = params.begin(); it != params.end(); ++it) { diff --git a/test/test.cc b/test/test.cc index 2edf20e..abda551 100644 --- a/test/test.cc +++ b/test/test.cc @@ -32,7 +32,7 @@ TEST(StartupTest, WSAStartup) TEST(SplitTest, ParseQueryString) { string s = "key1=val1&key2=val2&key3=val3"; - MultiMap dic; + Params dic; detail::split(s.c_str(), s.c_str() + s.size(), '&', [&](const char* b, const char* e) { string key, val; @@ -54,7 +54,7 @@ TEST(SplitTest, ParseQueryString) TEST(ParseQueryTest, ParseQueryString) { string s = "key1=val1&key2=val2&key3=val3"; - MultiMap dic; + Params dic; detail::parse_query_text(s, dic); @@ -83,28 +83,28 @@ TEST(SocketTest, OpenCloseWithAI_PASSIVE) TEST(GetHeaderValueTest, DefaultValue) { - MultiMap map = {{"Dummy","Dummy"}}; + Headers map = {{"Dummy","Dummy"}}; auto val = detail::get_header_value(map, "Content-Type", "text/plain"); ASSERT_STREQ("text/plain", val); } TEST(GetHeaderValueTest, DefaultValueInt) { - MultiMap map = {{"Dummy","Dummy"}}; + Headers map = {{"Dummy","Dummy"}}; auto val = detail::get_header_value_int(map, "Content-Length", 100); EXPECT_EQ(100, val); } TEST(GetHeaderValueTest, RegularValue) { - MultiMap map = {{"Content-Type", "text/html"}, {"Dummy", "Dummy"}}; + Headers map = {{"Content-Type", "text/html"}, {"Dummy", "Dummy"}}; auto val = detail::get_header_value(map, "Content-Type", "text/plain"); ASSERT_STREQ("text/html", val); } TEST(GetHeaderValueTest, RegularValueInt) { - MultiMap map = {{"Content-Length", "100"}, {"Dummy", "Dummy"}}; + Headers map = {{"Content-Length", "100"}, {"Dummy", "Dummy"}}; auto val = detail::get_header_value_int(map, "Content-Length", 0); EXPECT_EQ(100, val); } @@ -310,7 +310,7 @@ TEST_F(ServerTest, PostMethod2) ASSERT_TRUE(res != nullptr); ASSERT_EQ(404, res->status); - MultiMap params; + Params params; params.emplace("name", "john2"); params.emplace("note", "coder"); @@ -512,6 +512,15 @@ TEST_F(ServerTest, MultipartFormData) EXPECT_EQ(200, res->status); } +TEST_F(ServerTest, CaseInsensitiveHeaderName) +{ + auto res = cli_.get("/hi"); + ASSERT_TRUE(res != nullptr); + EXPECT_EQ(200, res->status); + EXPECT_EQ("text/plain", res->get_header_value("content-type")); + EXPECT_EQ("Hello World!", res->body); +} + class ServerTestWithAI_PASSIVE : public ::testing::Test { protected: ServerTestWithAI_PASSIVE() From c76d0e4ab363b82949113fa5370c462c9679b151 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 10 Dec 2017 22:42:11 -0500 Subject: [PATCH 031/118] Allow leading and trailing whilespaces before/after header field values --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index f72c9b4..eaf701c 100644 --- a/httplib.h +++ b/httplib.h @@ -598,7 +598,7 @@ inline int get_header_value_int(const Headers& headers, const char* key, int def inline bool read_headers(Stream& strm, Headers& headers) { - static std::regex re("(.+?): (.+?)\r\n"); + static std::regex re(R"((.+?):\s*(.+?)\s*\r\n)"); const auto bufsiz = 2048; char buf[bufsiz]; From 7e5db48bdfca5cddf353d7a4a8b316fd77b9538c Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 12 Dec 2017 22:20:40 -0500 Subject: [PATCH 032/118] Fixed #33 --- README.md | 19 +++++++-- httplib.h | 114 +++++++++++++++++++++++++++++++++++++++------------ test/test.cc | 107 +++++++++++++++++++++++++++++++++++++---------- 3 files changed, 189 insertions(+), 51 deletions(-) diff --git a/README.md b/README.md index 2ed1de9..2708834 100644 --- a/README.md +++ b/README.md @@ -126,15 +126,28 @@ auto res = cli.post("/post", params); httplib::Client client(url, port); // prints: 0 / 000 bytes => 50% complete -std::shared_ptr res = - cli.get("/", [](int64_t len, int64_t total) { - printf("%lld / %lld bytes => %d%% complete\n", +std::shared_ptr res = + cli.get("/", [](uint64_t len, uint64_t total) { + printf("%lld / %lld bytes => %d%% complete\n", len, total, (int)((len/total)*100)); } ); ``` +### Range (HTTP/1.1) + +```cpp +httplib::Client cli("httpbin.org", 80, httplib::HttpVersion::v1_1); + +// 'Range: bytes=1-10' +httplib::Headers headers = { httplib::make_range_header(1, 10) }; + +auto res = cli.get("/range/32", headers); +// res->status should be 206. +// res->body should be "bcdefghijk". +``` + ![progress](https://user-images.githubusercontent.com/236374/33138910-495c4ecc-cf86-11e7-8693-2fc6d09615c4.gif) This feature was contributed by [underscorediscovery](https://github.com/yhirose/cpp-httplib/pull/23). diff --git a/httplib.h b/httplib.h index eaf701c..d159b9f 100644 --- a/httplib.h +++ b/httplib.h @@ -91,9 +91,13 @@ struct ci { enum class HttpVersion { v1_0 = 0, v1_1 }; typedef std::multimap Headers; + +template +std::pair make_range_header(uint64_t value, Args... args); + typedef std::multimap Params; typedef std::smatch Match; -typedef std::function Progress; +typedef std::function Progress; struct MultipartFile { std::string filename; @@ -111,11 +115,11 @@ struct Request { Params params; MultipartFiles files; Match matches; + Progress progress; bool has_header(const char* key) const; std::string get_header_value(const char* key) const; - void set_header(const char* key, const char* val); bool has_param(const char* key) const; std::string get_param_value(const char* key) const; @@ -211,10 +215,17 @@ public: Client(const char* host, int port, HttpVersion http_version = HttpVersion::v1_0); virtual ~Client(); - std::shared_ptr get(const char* path, Progress callback = [](int64_t,int64_t){}); + std::shared_ptr get(const char* path, Progress progress = nullptr); + std::shared_ptr get(const char* path, const Headers& headers, Progress progress = nullptr); + std::shared_ptr head(const char* path); + std::shared_ptr head(const char* path, const Headers& headers); + std::shared_ptr post(const char* path, const std::string& body, const char* content_type); + std::shared_ptr post(const char* path, const Headers& headers, const std::string& body, const char* content_type); + std::shared_ptr post(const char* path, const Params& params); + std::shared_ptr post(const char* path, const Headers& headers, const Params& params); bool send(const Request& req, Response& res); @@ -633,7 +644,9 @@ bool read_content_with_length(Stream& strm, T& x, size_t len, Progress progress) if (r_incr <= 0) { return false; } + r += r_incr; + if (progress) { progress(r, len); } @@ -702,7 +715,7 @@ bool read_content_chunked(Stream& strm, T& x) } template -bool read_content(Stream& strm, T& x, Progress progress = [](int64_t,int64_t){}) +bool read_content(Stream& strm, T& x, Progress progress = Progress()) { auto len = get_header_value_int(x.headers, "Content-Length", 0); @@ -980,6 +993,38 @@ inline bool parse_multipart_formdata( return true; } +inline std::string to_lower(const char* beg, const char* end) +{ + std::string out; + auto it = beg; + while (it != end) { + out += ::tolower(*it); + it++; + } + return out; +} + +inline void make_range_header_core(std::string&) {} + +template +inline void make_range_header_core(std::string& field, uint64_t value) +{ + if (!field.empty()) { + field += ", "; + } + field += std::to_string(value) + "-"; +} + +template +inline void make_range_header_core(std::string& field, uint64_t value1, uint64_t value2, Args... args) +{ + if (!field.empty()) { + field += ", "; + } + field += std::to_string(value1) + "-" + std::to_string(value2); + make_range_header_core(field, args...); +} + #ifdef _WIN32 class WSInit { public: @@ -996,19 +1041,18 @@ public: static WSInit wsinit_; #endif -inline std::string to_lower(const char* beg, const char* end) -{ - std::string out; - auto it = beg; - while (it != end) { - out += ::tolower(*it); - it++; - } - return out; -} - } // namespace detail +// Header utilities +template +inline std::pair make_range_header(uint64_t value, Args... args) +{ + std::string field; + detail::make_range_header_core(field, value, args...); + field.insert(0, "bytes="); + return std::make_pair("Range", field); +} + // Request implementation inline bool Request::has_header(const char* key) const { @@ -1020,11 +1064,6 @@ inline std::string Request::get_header_value(const char* key) const return detail::get_header_value(headers, key, ""); } -inline void Request::set_header(const char* key, const char* val) -{ - headers.emplace(key, val); -} - inline bool Request::has_param(const char* key) const { return params.find(key) != params.end(); @@ -1485,12 +1524,18 @@ inline bool Client::read_and_close_socket(socket_t sock, const Request& req, Res }); } -inline std::shared_ptr Client::get(const char* path, Progress callback) +inline std::shared_ptr Client::get(const char* path, Progress progress) +{ + return get(path, Headers(), progress); +} + +inline std::shared_ptr Client::get(const char* path, const Headers& headers, Progress progress) { Request req; req.method = "GET"; req.path = path; - req.progress = callback; + req.headers = headers; + req.progress = progress; auto res = std::make_shared(); @@ -1498,9 +1543,15 @@ inline std::shared_ptr Client::get(const char* path, Progress callback } inline std::shared_ptr Client::head(const char* path) +{ + return head(path, Headers()); +} + +inline std::shared_ptr Client::head(const char* path, const Headers& headers) { Request req; req.method = "HEAD"; + req.headers = headers; req.path = path; auto res = std::make_shared(); @@ -1510,12 +1561,19 @@ inline std::shared_ptr Client::head(const char* path) inline std::shared_ptr Client::post( const char* path, const std::string& body, const char* content_type) +{ + return post(path, Headers(), body, content_type); +} + +inline std::shared_ptr Client::post( + const char* path, const Headers& headers, const std::string& body, const char* content_type) { Request req; req.method = "POST"; + req.headers = headers; req.path = path; - req.set_header("Content-Type", content_type); + req.headers.emplace("Content-Type", content_type); req.body = body; auto res = std::make_shared(); @@ -1523,8 +1581,12 @@ inline std::shared_ptr Client::post( return send(req, *res) ? res : nullptr; } -inline std::shared_ptr Client::post( - const char* path, const Params& params) +inline std::shared_ptr Client::post(const char* path, const Params& params) +{ + return post(path, Headers(), params); +} + +inline std::shared_ptr Client::post(const char* path, const Headers& headers, const Params& params) { std::string query; for (auto it = params.begin(); it != params.end(); ++it) { @@ -1536,7 +1598,7 @@ inline std::shared_ptr Client::post( query += it->second; } - return post(path, query, "application/x-www-form-urlencoded"); + return post(path, headers, query, "application/x-www-form-urlencoded"); } /* diff --git a/test/test.cc b/test/test.cc index abda551..d1996da 100644 --- a/test/test.cc +++ b/test/test.cc @@ -83,32 +83,59 @@ TEST(SocketTest, OpenCloseWithAI_PASSIVE) TEST(GetHeaderValueTest, DefaultValue) { - Headers map = {{"Dummy","Dummy"}}; - auto val = detail::get_header_value(map, "Content-Type", "text/plain"); - ASSERT_STREQ("text/plain", val); + Headers headers = {{"Dummy","Dummy"}}; + auto val = detail::get_header_value(headers, "Content-Type", "text/plain"); + EXPECT_STREQ("text/plain", val); } TEST(GetHeaderValueTest, DefaultValueInt) { - Headers map = {{"Dummy","Dummy"}}; - auto val = detail::get_header_value_int(map, "Content-Length", 100); + Headers headers = {{"Dummy","Dummy"}}; + auto val = detail::get_header_value_int(headers, "Content-Length", 100); EXPECT_EQ(100, val); } TEST(GetHeaderValueTest, RegularValue) { - Headers map = {{"Content-Type", "text/html"}, {"Dummy", "Dummy"}}; - auto val = detail::get_header_value(map, "Content-Type", "text/plain"); - ASSERT_STREQ("text/html", val); + Headers headers = {{"Content-Type", "text/html"}, {"Dummy", "Dummy"}}; + auto val = detail::get_header_value(headers, "Content-Type", "text/plain"); + EXPECT_STREQ("text/html", val); } TEST(GetHeaderValueTest, RegularValueInt) { - Headers map = {{"Content-Length", "100"}, {"Dummy", "Dummy"}}; - auto val = detail::get_header_value_int(map, "Content-Length", 0); + Headers headers = {{"Content-Length", "100"}, {"Dummy", "Dummy"}}; + auto val = detail::get_header_value_int(headers, "Content-Length", 0); EXPECT_EQ(100, val); } +TEST(GetHeaderValueTest, Range) +{ + { + Headers headers = { make_range_header(1) }; + auto val = detail::get_header_value(headers, "Range", 0); + EXPECT_STREQ("bytes=1-", val); + } + + { + Headers headers = { make_range_header(1, 10) }; + auto val = detail::get_header_value(headers, "Range", 0); + EXPECT_STREQ("bytes=1-10", val); + } + + { + Headers headers = { make_range_header(1, 10, 100) }; + auto val = detail::get_header_value(headers, "Range", 0); + EXPECT_STREQ("bytes=1-10, 100-", val); + } + + { + Headers headers = { make_range_header(1, 10, 100, 200) }; + auto val = detail::get_header_value(headers, "Range", 0); + EXPECT_STREQ("bytes=1-10, 100-200", val); + } +} + void testChunkedEncoding(httplib::HttpVersion ver) { auto host = "www.httpwatch.com"; @@ -136,6 +163,42 @@ TEST(ChunkedEncodingTest, FromHTTPWatch) testChunkedEncoding(httplib::HttpVersion::v1_1); } +TEST(RangeTest, FromHTTPBin) +{ + auto host = "httpbin.org"; + auto port = 80; + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + httplib::SSLClient cli(host, port, httplib::HttpVersion::v1_1); +#else + httplib::Client cli(host, port, httplib::HttpVersion::v1_1); +#endif + + { + httplib::Headers headers; + auto res = cli.get("/range/32", headers); + ASSERT_TRUE(res != nullptr); + EXPECT_EQ(res->body, "abcdefghijklmnopqrstuvwxyzabcdef"); + EXPECT_EQ(200, res->status); + } + + { + httplib::Headers headers = { httplib::make_range_header(1) }; + auto res = cli.get("/range/32", headers); + ASSERT_TRUE(res != nullptr); + EXPECT_EQ(res->body, "bcdefghijklmnopqrstuvwxyzabcdef"); + EXPECT_EQ(206, res->status); + } + + { + httplib::Headers headers = { httplib::make_range_header(1, 10) }; + auto res = cli.get("/range/32", headers); + ASSERT_TRUE(res != nullptr); + EXPECT_EQ(res->body, "bcdefghijk"); + EXPECT_EQ(206, res->status); + } +} + class ServerTest : public ::testing::Test { protected: ServerTest() @@ -422,11 +485,11 @@ TEST_F(ServerTest, LongHeader) host_and_port += ":"; host_and_port += std::to_string(PORT); - req.set_header("Host", host_and_port.c_str()); - req.set_header("Accept", "*/*"); - req.set_header("User-Agent", "cpp-httplib/0.1"); + req.headers.emplace("Host", host_and_port.c_str()); + req.headers.emplace("Accept", "*/*"); + req.headers.emplace("User-Agent", "cpp-httplib/0.1"); - req.set_header("Header-Name", "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@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@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); auto res = std::make_shared(); auto ret = cli_.send(req, *res); @@ -446,11 +509,11 @@ TEST_F(ServerTest, TooLongHeader) host_and_port += ":"; host_and_port += std::to_string(PORT); - req.set_header("Host", host_and_port.c_str()); - req.set_header("Accept", "*/*"); - req.set_header("User-Agent", "cpp-httplib/0.1"); + req.headers.emplace("Host", host_and_port.c_str()); + req.headers.emplace("Accept", "*/*"); + req.headers.emplace("User-Agent", "cpp-httplib/0.1"); - req.set_header("Header-Name", "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@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@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); auto res = std::make_shared(); auto ret = cli_.send(req, *res); @@ -498,10 +561,10 @@ TEST_F(ServerTest, MultipartFormData) host_and_port += ":"; host_and_port += std::to_string(PORT); - req.set_header("Host", host_and_port.c_str()); - req.set_header("Accept", "*/*"); - req.set_header("User-Agent", "cpp-httplib/0.1"); - req.set_header("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundarysBREP3G013oUrLB4"); + req.headers.emplace("Host", host_and_port.c_str()); + req.headers.emplace("Accept", "*/*"); + req.headers.emplace("User-Agent", "cpp-httplib/0.1"); + req.headers.emplace("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundarysBREP3G013oUrLB4"); req.body = "------WebKitFormBoundarysBREP3G013oUrLB4\r\nContent-Disposition: form-data; name=\"text1\"\r\n\r\ntext default\r\n------WebKitFormBoundarysBREP3G013oUrLB4\r\nContent-Disposition: form-data; name=\"text2\"\r\n\r\naωb\r\n------WebKitFormBoundarysBREP3G013oUrLB4\r\nContent-Disposition: form-data; name=\"file1\"; filename=\"hello.txt\"\r\nContent-Type: text/plain\r\n\r\nh\ne\n\nl\nl\no\n\r\n------WebKitFormBoundarysBREP3G013oUrLB4\r\nContent-Disposition: form-data; name=\"file2\"; filename=\"world.json\"\r\nContent-Type: application/json\r\n\r\n{\n \"world\", true\n}\n\r\n------WebKitFormBoundarysBREP3G013oUrLB4\r\nContent-Disposition: form-data; name=\"file3\"; filename=\"\"\r\nContent-Type: application/octet-stream\r\n\r\n\r\n------WebKitFormBoundarysBREP3G013oUrLB4--\r\n"; From 61d800053ed6cfad5f979fa9bec6e570cd7f5100 Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 12 Dec 2017 22:22:10 -0500 Subject: [PATCH 033/118] Updated README --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 2708834..737382d 100644 --- a/README.md +++ b/README.md @@ -71,7 +71,7 @@ svr.set_error_handler([](const auto& req, auto& res) { }); ``` -### `multipart/form-data` POST data +### 'multipart/form-data' POST data ```cpp svr.post("/multipart", [&](const auto& req, auto& res) { @@ -135,6 +135,10 @@ std::shared_ptr res = ); ``` +![progress](https://user-images.githubusercontent.com/236374/33138910-495c4ecc-cf86-11e7-8693-2fc6d09615c4.gif) + +This feature was contributed by [underscorediscovery](https://github.com/yhirose/cpp-httplib/pull/23). + ### Range (HTTP/1.1) ```cpp @@ -148,10 +152,6 @@ auto res = cli.get("/range/32", headers); // res->body should be "bcdefghijk". ``` -![progress](https://user-images.githubusercontent.com/236374/33138910-495c4ecc-cf86-11e7-8693-2fc6d09615c4.gif) - -This feature was contributed by [underscorediscovery](https://github.com/yhirose/cpp-httplib/pull/23). - OpenSSL Support --------------- From 38bbe4ec4c9010c06c8d62b7f69652f5efcd3ded Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 16 Dec 2017 18:40:35 -0500 Subject: [PATCH 034/118] Removed automatic inclution of with '__has_incude' --- httplib.h | 8 -------- 1 file changed, 8 deletions(-) diff --git a/httplib.h b/httplib.h index d159b9f..4a448e1 100644 --- a/httplib.h +++ b/httplib.h @@ -58,14 +58,6 @@ typedef int socket_t; #include #include -#ifdef __has_include -#if __has_include() -#ifndef CPPHTTPLIB_OPENSSL_SUPPORT -#define CPPHTTPLIB_OPENSSL_SUPPORT -#endif -#endif -#endif - #ifdef CPPHTTPLIB_OPENSSL_SUPPORT #include #endif From 95b22a980a74083d78320ea18b828a884f660763 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 16 Dec 2017 19:07:58 -0500 Subject: [PATCH 035/118] Fixed #35 --- example/server.cc | 9 ++++++++- httplib.h | 7 +++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/example/server.cc b/example/server.cc index a4b7f6d..8106fa5 100644 --- a/example/server.cc +++ b/example/server.cc @@ -7,6 +7,7 @@ #include #include +#include #define SERVER_CERT_FILE "./cert.pem" #define SERVER_PRIVATE_KEY_FILE "./key.pem" @@ -77,7 +78,13 @@ int main(void) }); svr.get("/hi", [](const auto& /*req*/, auto& res) { - res.set_content("Hello World!", "text/plain"); + res.set_content("Hello World!\n", "text/plain"); + }); + + 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) { diff --git a/httplib.h b/httplib.h index 4a448e1..ab79cc5 100644 --- a/httplib.h +++ b/httplib.h @@ -55,6 +55,7 @@ typedef int socket_t; #include #include #include +#include #include #include @@ -1236,7 +1237,13 @@ inline bool Server::listen(const char* host, int port, int socket_flags) } // TODO: should be async +#ifdef CPPHTTPLIB_NO_MULTI_THREAD_SUPPORT read_and_close_socket(sock); +#else + std::thread([=]() { + read_and_close_socket(sock); + }).detach(); +#endif } return ret; From a83dcefe8665789f8b94377749595a04962321b4 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 17 Dec 2017 22:23:05 -0500 Subject: [PATCH 036/118] Fixed SSL server problem with bad key.pem and cert.pem --- example/server.cc | 5 +++++ httplib.h | 37 ++++++++++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/example/server.cc b/example/server.cc index 8106fa5..a18bfc6 100644 --- a/example/server.cc +++ b/example/server.cc @@ -73,6 +73,11 @@ int main(void) Server svr; #endif + if (!svr.is_valid()) { + printf("server has an error...\n"); + return -1; + } + svr.get("/", [=](const auto& /*req*/, auto& res) { res.set_redirect("/hi"); }); diff --git a/httplib.h b/httplib.h index ab79cc5..8630559 100644 --- a/httplib.h +++ b/httplib.h @@ -169,6 +169,8 @@ public: Server(); virtual ~Server(); + virtual bool is_valid() const; + Server& get(const char* pattern, Handler handler); Server& post(const char* pattern, Handler handler); @@ -208,6 +210,8 @@ public: Client(const char* host, int port, HttpVersion http_version = HttpVersion::v1_0); virtual ~Client(); + virtual bool is_valid() const; + std::shared_ptr get(const char* path, Progress progress = nullptr); std::shared_ptr get(const char* path, const Headers& headers, Progress progress = nullptr); @@ -256,6 +260,8 @@ public: SSLServer(const char* cert_path, const char* private_key_path); virtual ~SSLServer(); + virtual bool is_valid() const; + private: virtual bool read_and_close_socket(socket_t sock); @@ -267,6 +273,8 @@ public: SSLClient(const char* host, int port, HttpVersion http_version = HttpVersion::v1_0); virtual ~SSLClient(); + virtual bool is_valid() const; + private: virtual bool read_and_close_socket(socket_t sock, const Request& req, Response& res); @@ -1216,6 +1224,10 @@ inline void Server::set_logger(Logger logger) inline bool Server::listen(const char* host, int port, int socket_flags) { + if (!is_valid()) { + return false; + } + svr_sock_ = detail::create_server_socket(host, port, socket_flags); if (svr_sock_ == -1) { return false; @@ -1405,6 +1417,11 @@ inline void Server::process_request(Stream& strm) write_response(strm, req, res); } +inline bool Server::is_valid() const +{ + return true; +} + inline bool Server::read_and_close_socket(socket_t sock) { return detail::read_and_close_socket(sock, [this](Stream& strm) { @@ -1426,6 +1443,11 @@ inline Client::~Client() { } +inline bool Client::is_valid() const +{ + return true; +} + inline bool Client::read_response_line(Stream& strm, Response& res) { const auto bufsiz = 2048; @@ -1610,6 +1632,9 @@ template inline bool read_and_close_socket_ssl(socket_t sock, SSL_CTX* ctx, U SSL_connect_or_accept, V setup, T callback) { auto ssl = SSL_new(ctx); + if (!ssl) { + return false; + } auto bio = BIO_new_socket(sock, BIO_NOCLOSE); SSL_set_bio(ssl, bio, bio); @@ -1693,6 +1718,11 @@ inline SSLServer::~SSLServer() } } +inline bool SSLServer::is_valid() const +{ + return ctx_; +} + inline bool SSLServer::read_and_close_socket(socket_t sock) { return detail::read_and_close_socket_ssl( @@ -1719,9 +1749,14 @@ inline SSLClient::~SSLClient() } } +inline bool SSLClient::is_valid() const +{ + return ctx_; +} + inline bool SSLClient::read_and_close_socket(socket_t sock, const Request& req, Response& res) { - return detail::read_and_close_socket_ssl( + return is_valid() && detail::read_and_close_socket_ssl( sock, ctx_, SSL_connect, [&](SSL* ssl) { From ca7b942196d4699349e1da3344be2869e6231095 Mon Sep 17 00:00:00 2001 From: yhirose Date: Wed, 20 Dec 2017 17:27:36 -0500 Subject: [PATCH 037/118] Changed license to MIT --- LICENSE | 22 ++++++++++++++++++++++ README.md | 11 ++++++----- 2 files changed, 28 insertions(+), 5 deletions(-) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..3e5ed35 --- /dev/null +++ b/LICENSE @@ -0,0 +1,22 @@ +The MIT License (MIT) + +Copyright (c) 2017 yhirose + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + diff --git a/README.md b/README.md index 737382d..c440cfa 100644 --- a/README.md +++ b/README.md @@ -3,8 +3,6 @@ cpp-httplib A C++11 header-only HTTP library. -[The Boost Software License 1.0](http://www.boost.org/LICENSE_1_0.txt) - It's extremely easy to setup. Just include **httplib.h** file in your code! Inspired by [Sinatra](http://www.sinatrarb.com/) and [express](https://github.com/visionmedia/express). @@ -139,10 +137,10 @@ std::shared_ptr res = This feature was contributed by [underscorediscovery](https://github.com/yhirose/cpp-httplib/pull/23). -### Range (HTTP/1.1) +### Range ```cpp -httplib::Client cli("httpbin.org", 80, httplib::HttpVersion::v1_1); +httplib::Client cli("httpbin.org", 80); // 'Range: bytes=1-10' httplib::Headers headers = { httplib::make_range_header(1, 10) }; @@ -165,4 +163,7 @@ SSLServer svr("./cert.pem", "./key.pem"); SSLClient cli("localhost", 8080); ``` -Copyright (c) 2017 Yuji Hirose. All rights reserved. +License +------- + +MIT license (© 2017 Yuji Hirose) From 23c8f0c738aadfb5f61f4af3c2e8d844d85f3191 Mon Sep 17 00:00:00 2001 From: yhirose Date: Wed, 20 Dec 2017 17:28:36 -0500 Subject: [PATCH 038/118] Fixed #28. (Keep-Alive connection support) --- example/server.cc | 11 +- example/simplesvr.cc | 8 +- httplib.h | 346 ++++++++++++++++++++++++++++++------------- test/test.cc | 20 ++- 4 files changed, 263 insertions(+), 122 deletions(-) diff --git a/example/server.cc b/example/server.cc index a18bfc6..b8eb510 100644 --- a/example/server.cc +++ b/example/server.cc @@ -35,7 +35,7 @@ std::string log(const Request& req, const Response& res) s += "================================\n"; - snprintf(buf, sizeof(buf), "%s %s", req.method.c_str(), req.path.c_str()); + snprintf(buf, sizeof(buf), "%s %s %s", req.method.c_str(), req.path.c_str(), req.version.c_str()); s += buf; std::string query; @@ -52,9 +52,10 @@ std::string log(const Request& req, const Response& res) s += "--------------------------------\n"; - snprintf(buf, sizeof(buf), "%d\n", res.status); + snprintf(buf, sizeof(buf), "%d %s\n", res.status, res.version.c_str()); s += buf; s += dump_headers(res.headers); + s += "\n"; if (!res.body.empty()) { s += res.body; @@ -67,10 +68,12 @@ std::string log(const Request& req, const Response& res) int main(void) { + auto version = httplib::HttpVersion::v1_1; + #ifdef CPPHTTPLIB_OPENSSL_SUPPORT - SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE); + SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, version); #else - Server svr; + Server svr(version); #endif if (!svr.is_valid()) { diff --git a/example/simplesvr.cc b/example/simplesvr.cc index 13b6b66..8a590a2 100644 --- a/example/simplesvr.cc +++ b/example/simplesvr.cc @@ -67,7 +67,7 @@ string log(const Request& req, const Response& res) s += "================================\n"; - snprintf(buf, sizeof(buf), "%s %s", req.method.c_str(), req.path.c_str()); + snprintf(buf, sizeof(buf), "%s %s %s", req.method.c_str(), req.path.c_str(), req.version.c_str()); s += buf; string query; @@ -99,10 +99,12 @@ int main(int argc, const char** argv) return 1; } + auto version = httplib::HttpVersion::v1_1; + #ifdef CPPHTTPLIB_OPENSSL_SUPPORT - SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE); + SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, version); #else - Server svr; + Server svr(version); #endif svr.post("/multipart", [](const auto& req, auto& res) { diff --git a/httplib.h b/httplib.h index 8630559..996dd92 100644 --- a/httplib.h +++ b/httplib.h @@ -2,7 +2,7 @@ // httplib.h // // Copyright (c) 2017 Yuji Hirose. All rights reserved. -// The Boost Software License 1.0 +// MIT License // #ifndef _CPPHTTPLIB_HTTPLIB_H_ @@ -63,6 +63,13 @@ typedef int socket_t; #include #endif +/* + * Configuration + */ +#define CPPHTTPLIB_KEEPALIVE_MAX_COUNT 5 +#define CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND 5 +#define CPPHTTPLIB_KEEPALIVE_TIMEOUT_USECOND 0 + namespace httplib { @@ -74,7 +81,7 @@ struct ci { s1.begin(), s1.end(), s2.begin(), s2.end(), [](char c1, char c2) { - return std::tolower(c1) < std::tolower(c2); + return ::tolower(c1) < ::tolower(c2); }); } }; @@ -88,8 +95,8 @@ typedef std::multimap Headers; template std::pair make_range_header(uint64_t value, Args... args); -typedef std::multimap Params; -typedef std::smatch Match; +typedef std::multimap Params; +typedef std::smatch Match; typedef std::function Progress; struct MultipartFile { @@ -101,6 +108,7 @@ struct MultipartFile { typedef std::multimap MultipartFiles; struct Request { + std::string version; std::string method; std::string path; Headers headers; @@ -113,6 +121,7 @@ struct Request { bool has_header(const char* key) const; std::string get_header_value(const char* key) const; + void set_header(const char* key, const char* val); bool has_param(const char* key) const; std::string get_param_value(const char* key) const; @@ -122,6 +131,7 @@ struct Request { }; struct Response { + std::string version; int status; Headers headers; std::string body; @@ -166,7 +176,8 @@ public: typedef std::function Handler; typedef std::function Logger; - Server(); + Server(HttpVersion http_version = HttpVersion::v1_0); + virtual ~Server(); virtual bool is_valid() const; @@ -180,10 +191,14 @@ public: void set_logger(Logger logger); bool listen(const char* host, int port, int socket_flags = 0); + + bool is_running() const; void stop(); protected: - void process_request(Stream& strm); + bool process_request(Stream& strm, bool last_connection); + + const HttpVersion http_version_; private: typedef std::vector> Handlers; @@ -192,8 +207,8 @@ private: bool handle_file_request(Request& req, Response& res); bool dispatch_request(Request& req, Response& res, Handlers& handlers); - bool read_request_line(Stream& strm, Request& req); - void write_response(Stream& strm, const Request& req, Response& res); + bool parse_request_line(const char* s, Request& req); + void write_response(Stream& strm, bool last_connection, const Request& req, Response& res); virtual bool read_and_close_socket(socket_t sock); @@ -224,10 +239,10 @@ public: std::shared_ptr post(const char* path, const Params& params); std::shared_ptr post(const char* path, const Headers& headers, const Params& params); - bool send(const Request& req, Response& res); + bool send(Request& req, Response& res); protected: - bool process_request(Stream& strm, const Request& req, Response& res); + bool process_request(Stream& strm, Request& req, Response& res); const std::string host_; const int port_; @@ -236,9 +251,9 @@ protected: private: bool read_response_line(Stream& strm, Response& res); - void write_request(Stream& strm, const Request& req, const char* ver); + void write_request(Stream& strm, Request& req); - virtual bool read_and_close_socket(socket_t sock, const Request& req, Response& res); + virtual bool read_and_close_socket(socket_t sock, Request& req, Response& res); }; #ifdef CPPHTTPLIB_OPENSSL_SUPPORT @@ -257,7 +272,10 @@ private: class SSLServer : public Server { public: - SSLServer(const char* cert_path, const char* private_key_path); + SSLServer( + const char* cert_path, const char* private_key_path, + HttpVersion http_version = HttpVersion::v1_0); + virtual ~SSLServer(); virtual bool is_valid() const; @@ -276,7 +294,7 @@ public: virtual bool is_valid() const; private: - virtual bool read_and_close_socket(socket_t sock, const Request& req, Response& res); + virtual bool read_and_close_socket(socket_t sock, Request& req, Response& res); SSL_CTX* ctx_; }; @@ -330,13 +348,13 @@ public: fixed_buffer_used_size_ = 0; glowable_buffer_.clear(); - size_t i = 0; - - for (;;) { + for (size_t i = 0; ; i++) { char byte; auto n = strm_.read(&byte, 1); - if (n < 1) { + if (n < 0) { + return false; + } else if (n == 0) { if (i == 0) { return false; } else { @@ -351,17 +369,18 @@ public: } } - append('\0'); return true; } private: void append(char c) { - if (fixed_buffer_used_size_ < fixed_buffer_size_) { + if (fixed_buffer_used_size_ < fixed_buffer_size_ - 1) { fixed_buffer_[fixed_buffer_used_size_++] = c; + fixed_buffer_[fixed_buffer_used_size_] = '\0'; } else { if (glowable_buffer_.empty()) { - glowable_buffer_.assign(fixed_buffer_, fixed_buffer_size_); + assert(fixed_buffer_[fixed_buffer_used_size_] == '\0'); + glowable_buffer_.assign(fixed_buffer_, fixed_buffer_used_size_); } glowable_buffer_ += c; } @@ -383,11 +402,43 @@ inline int close_socket(socket_t sock) #endif } -template -inline bool read_and_close_socket(socket_t sock, T callback) +inline int select(socket_t sock, size_t sec, size_t usec) { - SocketStream strm(sock); - auto ret = callback(strm); + fd_set fds; + FD_ZERO(&fds); + FD_SET(sock, &fds); + + timeval tv; + tv.tv_sec = sec; + tv.tv_usec = usec; + + return ::select(sock + 1, &fds, NULL, NULL, &tv); +} + +template +inline bool read_and_close_socket(socket_t sock, bool keep_alive, T callback) +{ + bool ret = false; + + if (keep_alive) { + auto count = CPPHTTPLIB_KEEPALIVE_MAX_COUNT; + while (count > 0 && + detail::select(sock, + CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND, + CPPHTTPLIB_KEEPALIVE_TIMEOUT_USECOND) > 0) { + auto last_connection = count == 1; + SocketStream strm(sock); + ret = callback(strm, last_connection); + if (!ret) { + break; + } + count--; + } + } else { + SocketStream strm(sock); + ret = callback(strm, true); + } + close_socket(sock); return ret; } @@ -641,12 +692,12 @@ bool read_content_with_length(Stream& strm, T& x, size_t len, Progress progress) x.body.assign(len, 0); size_t r = 0; while (r < len){ - auto r_incr = strm.read(&x.body[r], len - r); - if (r_incr <= 0) { + auto n = strm.read(&x.body[r], len - r); + if (n <= 0) { return false; } - r += r_incr; + r += n; if (progress) { progress(r, len); @@ -665,7 +716,7 @@ bool read_content_without_length(Stream& strm, T& x) if (n < 0) { return false; } else if (n == 0) { - break; + return true; } x.body += byte; } @@ -738,17 +789,10 @@ bool read_content(Stream& strm, T& x, Progress progress = Progress()) template inline void write_headers(Stream& strm, const T& info) { - strm.write("Connection: close\r\n"); - for (const auto& x: info.headers) { - if (x.first != "Content-Type" && x.first != "Content-Length") { - strm.write_format("%s: %s\r\n", x.first.c_str(), x.second.c_str()); - } + strm.write_format("%s: %s\r\n", x.first.c_str(), x.second.c_str()); } - - auto t = get_header_value(info.headers, "Content-Type", "text/plain"); - strm.write_format("Content-Type: %s\r\n", t); - strm.write_format("Content-Length: %ld\r\n", info.body.size()); + strm.write("\r\n"); } inline std::string encode_url(const std::string& s) @@ -1065,6 +1109,11 @@ inline std::string Request::get_header_value(const char* key) const return detail::get_header_value(headers, key, ""); } +inline void Request::set_header(const char* key, const char* val) +{ + headers.emplace(key, val); +} + inline bool Request::has_param(const char* key) const { return params.find(key) != params.end(); @@ -1134,7 +1183,11 @@ inline void Stream::write_format(const char* fmt, const Args& ...args) const auto bufsiz = 2048; char buf[bufsiz]; +#if defined(_MSC_VER) && _MSC_VER < 1900 + auto n = _snprintf_s(buf, bufsiz, bufsiz - 1, fmt, args...); +#else auto n = snprintf(buf, bufsiz - 1, fmt, args...); +#endif if (n > 0) { if (n >= bufsiz - 1) { std::vector glowable_buf(bufsiz); @@ -1179,8 +1232,9 @@ inline int SocketStream::write(const char* ptr) } // HTTP server implementation -inline Server::Server() - : svr_sock_(-1) +inline Server::Server(HttpVersion http_version) + : http_version_(http_version) + , svr_sock_(-1) { #ifndef _WIN32 signal(SIGPIPE, SIG_IGN); @@ -1236,6 +1290,16 @@ inline bool Server::listen(const char* host, int port, int socket_flags) auto ret = true; for (;;) { + auto val = detail::select(svr_sock_, 0, 100000); + + if (val == 0) { // Timeout + if (svr_sock_ == -1) { + // The server socket was closed by 'stop' method. + break; + } + continue; + } + socket_t sock = accept(svr_sock_, NULL, NULL); if (sock == -1) { @@ -1248,19 +1312,20 @@ inline bool Server::listen(const char* host, int port, int socket_flags) break; } - // TODO: should be async -#ifdef CPPHTTPLIB_NO_MULTI_THREAD_SUPPORT - read_and_close_socket(sock); -#else + // TODO: Use thread pool... std::thread([=]() { read_and_close_socket(sock); }).detach(); -#endif } return ret; } +inline bool Server::is_running() const +{ + return svr_sock_ != -1; +} + inline void Server::stop() { detail::shutdown_socket(svr_sock_); @@ -1268,21 +1333,13 @@ inline void Server::stop() svr_sock_ = -1; } -inline bool Server::read_request_line(Stream& strm, Request& req) +inline bool Server::parse_request_line(const char* s, Request& req) { - const auto bufsiz = 2048; - char buf[bufsiz]; - - detail::stream_line_reader reader(strm, buf, bufsiz); - - if (!reader.getline()) { - return false; - } - - static std::regex re("(GET|HEAD|POST) ([^?]+)(?:\\?(.+?))? HTTP/1\\.[01]\r\n"); + static std::regex re("(GET|HEAD|POST) ([^?]+)(?:\\?(.+?))? (HTTP/1\\.[01])\r\n"); std::cmatch m; - if (std::regex_match(reader.ptr(), m, re)) { + 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]); @@ -1298,7 +1355,7 @@ inline bool Server::read_request_line(Stream& strm, Request& req) return false; } -inline void Server::write_response(Stream& strm, const Request& req, Response& res) +inline void Server::write_response(Stream& strm, bool last_connection, const Request& req, Response& res) { assert(res.status != -1); @@ -1306,17 +1363,34 @@ inline void Server::write_response(Stream& strm, const Request& req, Response& r error_handler_(req, res); } - strm.write_format( - "HTTP/1.0 %d %s\r\n", - res.status, detail::status_message(res.status)); + // Response line + strm.write_format("%s %d %s\r\n", + detail::http_version_strings[static_cast(http_version_)], + res.status, + detail::status_message(res.status)); + + // Headers + if (!res.has_header("Connection") && (last_connection || req.version == "HTTP/1.0")) { + res.set_header("Connection", "close"); + } + + if (!res.body.empty()) { + if (!res.has_header("Content-Type")) { + res.set_header("Content-Type", "application/octet-stream"); + } + + auto length = std::to_string(res.body.size()); + res.set_header("Content-Length", length.c_str()); + } detail::write_headers(strm, res); - strm.write("\r\n"); + // Body if (!res.body.empty() && req.method != "HEAD") { strm.write(res.body.c_str(), res.body.size()); } + // Log if (logger_) { logger_(req, res); } @@ -1373,22 +1447,41 @@ inline bool Server::dispatch_request(Request& req, Response& res, Handlers& hand return false; } -inline void Server::process_request(Stream& strm) +inline bool Server::process_request(Stream& strm, bool last_connection) { + const auto bufsiz = 2048; + char buf[bufsiz]; + + detail::stream_line_reader reader(strm, buf, bufsiz); + + // Connection has been closed on client + if (!reader.getline()) { + return false; + } + Request req; Response res; - if (!read_request_line(strm, req) || !detail::read_headers(strm, req.headers)) { + res.version = detail::http_version_strings[static_cast(http_version_)]; + + // Request line and headers + if (!parse_request_line(reader.ptr(), req) || !detail::read_headers(strm, req.headers)) { res.status = 400; - write_response(strm, req, res); - return; + write_response(strm, last_connection, req, res); + return true; } + auto ret = true; + if (req.get_header_value("Connection") == "close") { + ret = false; + } + + // Body if (req.method == "POST") { if (!detail::read_content(strm, req)) { res.status = 400; - write_response(strm, req, res); - return; + write_response(strm, last_connection, req, res); + return ret; } const auto& content_type = req.get_header_value("Content-Type"); @@ -1400,8 +1493,8 @@ inline void Server::process_request(Stream& strm) if (!detail::parse_multipart_boundary(content_type, boundary) || !detail::parse_multipart_formdata(boundary, req.body, req.files)) { res.status = 400; - write_response(strm, req, res); - return; + write_response(strm, last_connection, req, res); + return ret; } } } @@ -1414,7 +1507,8 @@ inline void Server::process_request(Stream& strm) res.status = 404; } - write_response(strm, req, res); + write_response(strm, last_connection, req, res); + return ret; } inline bool Server::is_valid() const @@ -1424,10 +1518,14 @@ inline bool Server::is_valid() const inline bool Server::read_and_close_socket(socket_t sock) { - return detail::read_and_close_socket(sock, [this](Stream& strm) { - process_request(strm); - return true; - }); + auto keep_alive = http_version_ == HttpVersion::v1_1; + + return detail::read_and_close_socket( + sock, + keep_alive, + [this](Stream& strm, bool last_connection) { + return process_request(strm, last_connection); + }); } // HTTP client implementation @@ -1469,7 +1567,7 @@ inline bool Client::read_response_line(Stream& strm, Response& res) return true; } -inline bool Client::send(const Request& req, Response& res) +inline bool Client::send(Request& req, Response& res) { if (req.path.empty()) { return false; @@ -1483,29 +1581,43 @@ inline bool Client::send(const Request& req, Response& res) return read_and_close_socket(sock, req, res); } -inline void Client::write_request(Stream& strm, const Request& req, const char* ver) +inline void Client::write_request(Stream& strm, Request& req) { auto path = detail::encode_url(req.path); // Request line - strm.write_format( - "%s %s %s\r\n", req.method.c_str(), path.c_str(), ver); + strm.write_format("%s %s %s\r\n", + req.method.c_str(), + path.c_str(), + detail::http_version_strings[static_cast(http_version_)]); // Headers - strm.write_format("Host: %s\r\n", host_and_port_.c_str()); + req.set_header("Host", host_and_port_.c_str()); if (!req.has_header("Accept")) { - strm.write("Accept: */*\r\n"); + req.set_header("Accept", "*/*"); } if (!req.has_header("User-Agent")) { - strm.write("User-Agent: cpp-httplib/0.1\r\n"); + req.set_header("User-Agent", "cpp-httplib/0.2"); + } + + // TODO: if (!req.has_header("Connection") && (last_connection || http_version_ == detail::HttpVersion::v1_0)) { + if (!req.has_header("Connection")) { + req.set_header("Connection", "close"); + } + + if (!req.body.empty()) { + if (!req.has_header("Content-Type")) { + req.set_header("Content-Type", "application/octet-stream"); + } + + auto length = std::to_string(req.body.size()); + req.set_header("Content-Length", length.c_str()); } detail::write_headers(strm, req); - strm.write("\r\n"); - // Body if (!req.body.empty()) { if (req.has_header("application/x-www-form-urlencoded")) { @@ -1517,18 +1629,19 @@ inline void Client::write_request(Stream& strm, const Request& req, const char* } } -inline bool Client::process_request(Stream& strm, const Request& req, Response& res) +inline bool Client::process_request(Stream& strm, Request& req, Response& res) { // Send request - auto ver = detail::http_version_strings[static_cast(http_version_)]; - write_request(strm, req, ver); + write_request(strm, req); - // Receive response - if (!read_response_line(strm, res) || - !detail::read_headers(strm, res.headers)) { + // Receive response and headers + if (!read_response_line(strm, res) || !detail::read_headers(strm, res.headers)) { return false; } + // TODO: Check if 'Connection' header is 'close' or HTTP version is 1.0, then close socket... + + // Body if (req.method != "HEAD") { if (!detail::read_content(strm, res, req.progress)) { return false; @@ -1538,9 +1651,9 @@ inline bool Client::process_request(Stream& strm, const Request& req, Response& return true; } -inline bool Client::read_and_close_socket(socket_t sock, const Request& req, Response& res) +inline bool Client::read_and_close_socket(socket_t sock, Request& req, Response& res) { - return detail::read_and_close_socket(sock, [&](Stream& strm) { + return detail::read_and_close_socket(sock, false, [&](Stream& strm, bool /*last_connection*/) { return process_request(strm, req, res); }); } @@ -1629,7 +1742,10 @@ inline std::shared_ptr Client::post(const char* path, const Headers& h namespace detail { template -inline bool read_and_close_socket_ssl(socket_t sock, SSL_CTX* ctx, U SSL_connect_or_accept, V setup, T callback) +inline bool read_and_close_socket_ssl( + socket_t sock, bool keep_alive, + SSL_CTX* ctx, U SSL_connect_or_accept, V setup, + T callback) { auto ssl = SSL_new(ctx); if (!ssl) { @@ -1643,8 +1759,26 @@ inline bool read_and_close_socket_ssl(socket_t sock, SSL_CTX* ctx, U SSL_connect SSL_connect_or_accept(ssl); - SSLSocketStream strm(ssl); - auto ret = callback(strm); + bool ret = false; + + if (keep_alive) { + auto count = CPPHTTPLIB_KEEPALIVE_MAX_COUNT; + while (count > 0 && + detail::select(sock, + CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND, + CPPHTTPLIB_KEEPALIVE_TIMEOUT_USECOND) > 0) { + auto last_connection = count == 1; + SSLSocketStream strm(ssl); + ret = callback(strm, last_connection); + if (!ret) { + break; + } + count--; + } + } else { + SSLSocketStream strm(ssl); + ret = callback(strm, true); + } SSL_shutdown(ssl); SSL_free(ssl); @@ -1689,7 +1823,8 @@ inline int SSLSocketStream::write(const char* ptr) } // SSL HTTP server implementation -inline SSLServer::SSLServer(const char* cert_path, const char* private_key_path) +inline SSLServer::SSLServer(const char* cert_path, const char* private_key_path, HttpVersion http_version) + : Server(http_version) { ctx_ = SSL_CTX_new(SSLv23_server_method()); @@ -1725,13 +1860,16 @@ inline bool SSLServer::is_valid() const inline bool SSLServer::read_and_close_socket(socket_t sock) { + auto keep_alive = http_version_ == HttpVersion::v1_1; + return detail::read_and_close_socket_ssl( - sock, ctx_, + sock, + keep_alive, + ctx_, SSL_accept, [](SSL* /*ssl*/) {}, - [this](Stream& strm) { - process_request(strm); - return true; + [this](Stream& strm, bool last_connection) { + return process_request(strm, last_connection); }); } @@ -1754,15 +1892,15 @@ inline bool SSLClient::is_valid() const return ctx_; } -inline bool SSLClient::read_and_close_socket(socket_t sock, const Request& req, Response& res) +inline bool SSLClient::read_and_close_socket(socket_t sock, Request& req, Response& res) { return is_valid() && detail::read_and_close_socket_ssl( - sock, ctx_, - SSL_connect, + sock, false, + ctx_, SSL_connect, [&](SSL* ssl) { SSL_set_tlsext_host_name(ssl, host_.c_str()); }, - [&](Stream& strm) { + [&](Stream& strm, bool /*last_connection*/) { return process_request(strm, req, res); }); } diff --git a/test/test.cc b/test/test.cc index d1996da..8db9373 100644 --- a/test/test.cc +++ b/test/test.cc @@ -139,11 +139,12 @@ TEST(GetHeaderValueTest, Range) void testChunkedEncoding(httplib::HttpVersion ver) { auto host = "www.httpwatch.com"; - auto port = 80; #ifdef CPPHTTPLIB_OPENSSL_SUPPORT + auto port = 443; httplib::SSLClient cli(host, port, ver); #else + auto port = 80; httplib::Client cli(host, port, ver); #endif @@ -166,11 +167,12 @@ TEST(ChunkedEncodingTest, FromHTTPWatch) TEST(RangeTest, FromHTTPBin) { auto host = "httpbin.org"; - auto port = 80; #ifdef CPPHTTPLIB_OPENSSL_SUPPORT + auto port = 443; httplib::SSLClient cli(host, port, httplib::HttpVersion::v1_1); #else + auto port = 80; httplib::Client cli(host, port, httplib::HttpVersion::v1_1); #endif @@ -265,27 +267,23 @@ protected: EXPECT_EQ("application/octet-stream", file.content_type); EXPECT_EQ(0u, file.length); } - }) - .get("/stop", [&](const Request& /*req*/, Response& /*res*/) { - svr_.stop(); }); persons_["john"] = "programmer"; - f_ = async([&](){ + t_ = thread([&](){ up_ = true; svr_.listen(HOST, PORT); }); - while (!up_) { + while (!svr_.is_running()) { msleep(1); } } virtual void TearDown() { - //svr_.stop(); // NOTE: This causes dead lock on Windows. - cli_.get("/stop"); - f_.get(); + svr_.stop(); + t_.join(); } map persons_; @@ -296,7 +294,7 @@ protected: Client cli_; Server svr_; #endif - future f_; + thread t_; bool up_; }; From 1c86540fe524857c808ccb97890119e500b64c30 Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 22 Dec 2017 23:16:28 -0500 Subject: [PATCH 039/118] Fixed content-type problems --- httplib.h | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/httplib.h b/httplib.h index 996dd92..4f66cbf 100644 --- a/httplib.h +++ b/httplib.h @@ -1376,7 +1376,7 @@ inline void Server::write_response(Stream& strm, bool last_connection, const Req if (!res.body.empty()) { if (!res.has_header("Content-Type")) { - res.set_header("Content-Type", "application/octet-stream"); + res.set_header("Content-Type", "text/plain"); } auto length = std::to_string(res.body.size()); @@ -1602,14 +1602,15 @@ inline void Client::write_request(Stream& strm, Request& req) req.set_header("User-Agent", "cpp-httplib/0.2"); } - // TODO: if (!req.has_header("Connection") && (last_connection || http_version_ == detail::HttpVersion::v1_0)) { + // TODO: if (!req.has_header("Connection") && + // (last_connection || http_version_ == detail::HttpVersion::v1_0)) { if (!req.has_header("Connection")) { req.set_header("Connection", "close"); } if (!req.body.empty()) { if (!req.has_header("Content-Type")) { - req.set_header("Content-Type", "application/octet-stream"); + req.set_header("Content-Type", "text/plain"); } auto length = std::to_string(req.body.size()); @@ -1620,7 +1621,7 @@ inline void Client::write_request(Stream& strm, Request& req) // Body if (!req.body.empty()) { - if (req.has_header("application/x-www-form-urlencoded")) { + if (req.get_header_value("Content-Type") == "application/x-www-form-urlencoded") { auto str = detail::encode_url(req.body); strm.write(str.c_str(), str.size()); } else { From 7a87dd1039616b07e7640e4bf7984a218d6ead81 Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 29 Dec 2017 22:34:46 -0500 Subject: [PATCH 040/118] Added -lpthread --- example/Makefile | 2 +- test/Makefile | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/example/Makefile b/example/Makefile index bd48842..a0e4944 100644 --- a/example/Makefile +++ b/example/Makefile @@ -1,6 +1,6 @@ CC = clang++ -CFLAGS = -std=c++14 -I.. -Wall -Wextra +CFLAGS = -std=c++14 -I.. -Wall -Wextra -lpthread #OPENSSL_SUPPORT = -DCPPHTTPLIB_OPENSSL_SUPPORT -I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib -lssl -lcrypto all: server client hello simplesvr benchmark diff --git a/test/Makefile b/test/Makefile index d802397..f466617 100644 --- a/test/Makefile +++ b/test/Makefile @@ -1,6 +1,6 @@ CC = clang++ -CFLAGS = -std=c++11 -DGTEST_USE_OWN_TR1_TUPLE -I.. -I. -Wall -Wextra +CFLAGS = -std=c++11 -DGTEST_USE_OWN_TR1_TUPLE -I.. -I. -Wall -Wextra -lpthread #OPENSSL_SUPPORT = -DCPPHTTPLIB_OPENSSL_SUPPORT -I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib -lssl -lcrypto all : test From 0b49065583019acf347a58c98fd2c86ad5f9ae54 Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 29 Dec 2017 22:34:59 -0500 Subject: [PATCH 041/118] Add a file for test --- test/image.jpg | Bin 0 -> 33653 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 test/image.jpg diff --git a/test/image.jpg b/test/image.jpg new file mode 100644 index 0000000000000000000000000000000000000000..f74b495328f3cf26a12dd2c4843484cc6a13561c GIT binary patch literal 33653 zcmce82UL@5@_qyaMHG=PB`PR_(xsP(AVrkk5s}`dcSuB}NH0q0h=}ywyV62OdhfmW z8WR3r@ZP(-zrB5T|2Zd|GfYe__Hrgzk-M^V3lCSVV@%c zVG*6fCOUUof9?}-gLvmKk019A0^yv;#=<=Z!n<%0|I#@S78dqDe+pc{#yO9B_Tg0! z);Vl!EF3IcJZ#_(mhCw#Y$6=c`J30+MdWdbb#7UEkZ^=1;*sV(qWh-MHN{D$yGd{J zX6v@-1I4gU`FggsTw)C2Nd?^GO8Rzn-P0HDcwWS(pnUuOvH0ggl^*50*SBYO>U(E* z4eY%>Bo{UG&FvXFct?CGZtS1m=aEn~a`b_xlpqEc4tOQijGcTVQ%jo$7Y`Zvq|{BE z-$kXBH4iNvG4VgqFm>^ZPA_j6UOonXI2Lf1=g$Mb0p|iXwgvEeh_G=mcYIzRxMN&v zk5G=pyiJmCT~lHToMd(1 z?>CyR!;_tvJxjh>@zoRVd>kc=MrSSvrFLyiQA+UI9)O4U#_Jt?d{QH@9GbY%xhP$< zNL=h>g0dNwMuU8Dd4Gn9?4xBd>`&p%ut|+17Vi{#zH43K(m^n#N0w#_?N9hG`Rc_< zSS#u6`UTf<-W(1G%bl-n9^&onmP9JXXbUb~`!18OSz=QchFVcPG&C2GrCiYY);y#x z!4zg|mbNt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=V@HFVb9`C*ik+SJPpqEll8q>iB&@(McC>xf)z5)|@ z1o#S~v9?=tC3ET8{o7HS`=yBzx+w)7GN8AH;;#vDw-kkYtJFozAgm#`jqbf)&tJ z<9DIP+nI59t?#Uz*sPgo&~8z5#mS}+Hl4bx6+1RbPKr}C!EK!rj%mfEVQ)A=t;V{! z>$LIOwdWal@l|Pu%LRYAIQwo8{{#8$dw0o5a}B#F#L{^4Iy@f zy|!vPDHLUB=@rBsn-6W9l!RnI@7j7MDIjDTmRA$Y%S+JpO%mn$Y$vxOLX`mvSEKb>N$bjbNO&*4|AkrC|2hVpR$lymO0ZvJrL_ zqSw0_#367?HkbG6m22ymWsFXK8PmXd7c+Bp;(2% zwuhKBe@t6tvG_{@o?h~EjvSMh*BYoH?mCOay1Wzfl5U8U?w0+Fv@9+p z zUL!(L)t&nj!k)C_mQo#>Q02`Hx4~NkntA4IwNp~BI2NJRHQ|GCv92C6>LxZ^gUef| zoz8oR=qJ5fxniqwghkz+C`|GzD#K5}uIygfIJs~)YL_B+#&!9B{?g9>({~V9Y06i* zt?yt~R*nHr>h%vNbHlDpBnA@4Ng@l{x-%}mTp21EYP5L?miv}Mt;F`ZjBho{%<_*% kMs7)8UuVLtzS0rDEn?^O_wUth^tgWF&wg=z^FMa~2i7_KH2?qr literal 0 HcmV?d00001 From d1f903fc588f4c109423acc646fbece6442e20da Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 29 Dec 2017 23:09:17 -0500 Subject: [PATCH 042/118] Cleanup test code --- test/test.cc | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/test/test.cc b/test/test.cc index 8db9373..ba4b173 100644 --- a/test/test.cc +++ b/test/test.cc @@ -208,7 +208,7 @@ protected: #ifdef CPPHTTPLIB_OPENSSL_SUPPORT , svr_(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE) #endif - , up_(false) {} + {} virtual void SetUp() { svr_.set_base_dir("./www"); @@ -272,7 +272,6 @@ protected: persons_["john"] = "programmer"; t_ = thread([&](){ - up_ = true; svr_.listen(HOST, PORT); }); @@ -295,7 +294,6 @@ protected: Server svr_; #endif thread t_; - bool up_; }; TEST_F(ServerTest, GetMethod200) @@ -589,31 +587,25 @@ protected: #ifdef CPPHTTPLIB_OPENSSL_SUPPORT , svr_(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE) #endif - , up_(false) {} + {} virtual void SetUp() { svr_.get("/hi", [&](const Request& /*req*/, Response& res) { res.set_content("Hello World!", "text/plain"); }); - svr_.get("/stop", [&](const Request& /*req*/, Response& /*res*/) { - svr_.stop(); - }); - - f_ = async([&](){ - up_ = true; + t_ = thread([&](){ svr_.listen(nullptr, PORT, AI_PASSIVE); }); - while (!up_) { + while (!svr_.is_running()) { msleep(1); } } virtual void TearDown() { - //svr_.stop(); // NOTE: This causes dead lock on Windows. - cli_.get("/stop"); - f_.get(); + svr_.stop(); + t_.join(); } #ifdef CPPHTTPLIB_OPENSSL_SUPPORT @@ -623,8 +615,7 @@ protected: Client cli_; Server svr_; #endif - future f_; - bool up_; + thread t_; }; TEST_F(ServerTestWithAI_PASSIVE, GetMethod200) From 1d5fbe6a5b2d57838aecee878791d7df392a1bfc Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 28 Dec 2017 20:47:52 -0500 Subject: [PATCH 043/118] Add gzip support. resolved #11 --- README.md | 14 +++++++++ example/Makefile | 11 +++---- httplib.h | 75 +++++++++++++++++++++++++++++++++++++++++++----- test/Makefile | 3 +- test/test.cc | 39 ++++++++++++++++++++++++- 5 files changed, 128 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index c440cfa..69a7f11 100644 --- a/README.md +++ b/README.md @@ -163,6 +163,20 @@ SSLServer svr("./cert.pem", "./key.pem"); SSLClient cli("localhost", 8080); ``` +Zlib Support +------------ + +'gzip' compression is available with `CPPHTTPLIB_ZLIB_SUPPORT`. + +The server applies gzip compression to the following MIME type contents: + + * all text types + * image/svg+xml + * application/javascript + * application/json + * application/xml + * application/xhtml+xml + License ------- diff --git a/example/Makefile b/example/Makefile index a0e4944..28af76c 100644 --- a/example/Makefile +++ b/example/Makefile @@ -2,23 +2,24 @@ CC = clang++ CFLAGS = -std=c++14 -I.. -Wall -Wextra -lpthread #OPENSSL_SUPPORT = -DCPPHTTPLIB_OPENSSL_SUPPORT -I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib -lssl -lcrypto +ZLIB_SUPPORT = -DCPPHTTPLIB_ZLIB_SUPPORT -lz all: server client hello simplesvr benchmark server : server.cc ../httplib.h Makefile - $(CC) -o server $(CFLAGS) server.cc $(OPENSSL_SUPPORT) + $(CC) -o server $(CFLAGS) server.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) client : client.cc ../httplib.h Makefile - $(CC) -o client $(CFLAGS) client.cc $(OPENSSL_SUPPORT) + $(CC) -o client $(CFLAGS) client.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) hello : hello.cc ../httplib.h Makefile - $(CC) -o hello $(CFLAGS) hello.cc $(OPENSSL_SUPPORT) + $(CC) -o hello $(CFLAGS) hello.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) simplesvr : simplesvr.cc ../httplib.h Makefile - $(CC) -o simplesvr $(CFLAGS) simplesvr.cc $(OPENSSL_SUPPORT) + $(CC) -o simplesvr $(CFLAGS) simplesvr.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) benchmark : benchmark.cc ../httplib.h Makefile - $(CC) -o benchmark $(CFLAGS) benchmark.cc $(OPENSSL_SUPPORT) + $(CC) -o benchmark $(CFLAGS) benchmark.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) pem: openssl genrsa 2048 > key.pem diff --git a/httplib.h b/httplib.h index 4f66cbf..cded0e1 100644 --- a/httplib.h +++ b/httplib.h @@ -63,6 +63,10 @@ typedef int socket_t; #include #endif +#ifdef CPPHTTPLIB_ZLIB_SUPPORT +#include +#endif + /* * Configuration */ @@ -597,19 +601,15 @@ inline std::string file_extension(const std::string& path) return std::string(); } -inline const char* content_type(const std::string& path) +inline const char* find_content_type(const std::string& path) { auto ext = file_extension(path); if (ext == "txt") { return "text/plain"; } else if (ext == "html") { return "text/html"; - } else if (ext == "js") { - return "text/javascript"; } else if (ext == "css") { return "text/css"; - } else if (ext == "xml") { - return "text/xml"; } else if (ext == "jpeg" || ext == "jpg") { return "image/jpg"; } else if (ext == "png") { @@ -624,6 +624,10 @@ inline const char* content_type(const std::string& path) return "application/json"; } else if (ext == "pdf") { return "application/pdf"; + } else if (ext == "js") { + return "application/javascript"; + } else if (ext == "xml") { + return "application/xml"; } else if (ext == "xhtml") { return "application/xhtml+xml"; } @@ -774,7 +778,7 @@ bool read_content(Stream& strm, T& x, Progress progress = Progress()) if (len) { return read_content_with_length(strm, x, len, progress); } else { - auto encoding = get_header_value(x.headers, "Transfer-Encoding", ""); + const auto& encoding = get_header_value(x.headers, "Transfer-Encoding", ""); if (!strcmp(encoding, "chunked")) { return read_content_chunked(strm, x); @@ -1070,6 +1074,59 @@ inline void make_range_header_core(std::string& field, uint64_t value1, uint64_t make_range_header_core(field, args...); } +#ifdef CPPHTTPLIB_ZLIB_SUPPORT +inline bool can_compress(const std::string& content_type) { + return !content_type.find("text/") || + content_type == "image/svg+xml" || + content_type == "application/javascript" || + content_type == "application/json" || + content_type == "application/xml" || + content_type == "application/xhtml+xml"; +} + +inline void compress(const Request& req, Response& res) +{ + // TODO: Server version is HTTP/1.1 and 'Accpet-Encoding' has gzip, not gzip;q=0 + const auto& encodings = req.get_header_value("Accept-Encoding"); + if (encodings.find("gzip") == std::string::npos) { + return; + } + + if (!can_compress(res.get_header_value("Content-Type"))) { + return; + } + + z_stream strm; + strm.zalloc = Z_NULL; + strm.zfree = Z_NULL; + strm.opaque = Z_NULL; + + auto ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 31, 8, Z_DEFAULT_STRATEGY); + if (ret != Z_OK) { + return; + } + + strm.avail_in = res.body.size(); + strm.next_in = (Bytef *)res.body.data(); + + std::string compressed; + + const auto bufsiz = 16384; + char buff[bufsiz]; + do { + strm.avail_out = bufsiz; + strm.next_out = (Bytef *)buff; + deflate(&strm, Z_FINISH); + compressed.append(buff, bufsiz - strm.avail_out); + } while (strm.avail_out == 0); + + res.set_header("Content-Encoding", "gzip"); + res.body.swap(compressed); + + deflateEnd(&strm); +} +#endif + #ifdef _WIN32 class WSInit { public: @@ -1375,6 +1432,10 @@ inline void Server::write_response(Stream& strm, bool last_connection, const Req } if (!res.body.empty()) { +#ifdef CPPHTTPLIB_ZLIB_SUPPORT + detail::compress(req, res); +#endif + if (!res.has_header("Content-Type")) { res.set_header("Content-Type", "text/plain"); } @@ -1407,7 +1468,7 @@ inline bool Server::handle_file_request(Request& req, Response& res) if (detail::is_file(path)) { detail::read_file(path, res.body); - auto type = detail::content_type(path); + auto type = detail::find_content_type(path); if (type) { res.set_header("Content-Type", type); } diff --git a/test/Makefile b/test/Makefile index f466617..8c76650 100644 --- a/test/Makefile +++ b/test/Makefile @@ -2,12 +2,13 @@ CC = clang++ CFLAGS = -std=c++11 -DGTEST_USE_OWN_TR1_TUPLE -I.. -I. -Wall -Wextra -lpthread #OPENSSL_SUPPORT = -DCPPHTTPLIB_OPENSSL_SUPPORT -I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib -lssl -lcrypto +#ZLIB_SUPPORT = -DCPPHTTPLIB_ZLIB_SUPPORT -lz all : test ./test test : test.cc ../httplib.h Makefile - $(CC) -o test $(CFLAGS) test.cc gtest/gtest-all.cc gtest/gtest_main.cc $(OPENSSL_SUPPORT) + $(CC) -o test $(CFLAGS) test.cc gtest/gtest-all.cc gtest/gtest_main.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) pem: openssl genrsa 2048 > key.pem diff --git a/test/test.cc b/test/test.cc index ba4b173..1edb8af 100644 --- a/test/test.cc +++ b/test/test.cc @@ -267,7 +267,16 @@ protected: EXPECT_EQ("application/octet-stream", file.content_type); EXPECT_EQ(0u, file.length); } - }); + }) +#ifdef CPPHTTPLIB_ZLIB_SUPPORT + .get("/gzip", [&](const Request& /*req*/, Response& res) { + res.set_content("1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890", "text/plain"); + }) + .get("/nogzip", [&](const Request& /*req*/, Response& res) { + res.set_content("1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890", "application/octet-stream"); + }) +#endif + ; persons_["john"] = "programmer"; @@ -580,6 +589,34 @@ TEST_F(ServerTest, CaseInsensitiveHeaderName) EXPECT_EQ("Hello World!", res->body); } +#ifdef CPPHTTPLIB_ZLIB_SUPPORT +TEST_F(ServerTest, Gzip) +{ + Headers headers; + headers.emplace("Accept-Encoding", "gzip, deflate"); + auto res = cli_.get("/gzip", headers); + + ASSERT_TRUE(res != nullptr); + EXPECT_EQ("gzip", res->get_header_value("Content-Encoding")); + EXPECT_EQ("text/plain", res->get_header_value("Content-Type")); + EXPECT_EQ("33", res->get_header_value("Content-Length")); + EXPECT_EQ(200, res->status); +} + +TEST_F(ServerTest, NoGzip) +{ + Headers headers; + headers.emplace("Accept-Encoding", "gzip, deflate"); + auto res = cli_.get("/nogzip", headers); + + ASSERT_TRUE(res != nullptr); + EXPECT_EQ(false, res->has_header("Content-Encoding")); + EXPECT_EQ("application/octet-stream", res->get_header_value("Content-Type")); + EXPECT_EQ("100", res->get_header_value("Content-Length")); + EXPECT_EQ(200, res->status); +} +#endif + class ServerTestWithAI_PASSIVE : public ::testing::Test { protected: ServerTestWithAI_PASSIVE() From 1afcc6e702af1eebcebd92c8b001f1ab29b50794 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 30 Dec 2017 14:49:01 -0500 Subject: [PATCH 044/118] Upgraded VC++ projects to 2017 version --- example/client.vcxproj | 7 ++++--- example/server.vcxproj | 7 ++++--- test/test.vcxproj | 5 +++-- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/example/client.vcxproj b/example/client.vcxproj index 51d06c1..6788d19 100644 --- a/example/client.vcxproj +++ b/example/client.vcxproj @@ -1,5 +1,5 @@  - + Debug @@ -14,20 +14,21 @@ {6DB1FC63-B153-4279-92B7-D8A11AF285D6} Win32Proj client + 10.0.15063.0 Application true Unicode - v140 + v141 Application false true Unicode - v140 + v141 diff --git a/example/server.vcxproj b/example/server.vcxproj index f4e7181..76b3da5 100644 --- a/example/server.vcxproj +++ b/example/server.vcxproj @@ -1,5 +1,5 @@  - + Debug @@ -14,20 +14,21 @@ {864CD288-050A-4C8B-9BEF-3048BD876C5B} Win32Proj sample + 10.0.15063.0 Application true Unicode - v140 + v141 Application false true Unicode - v140 + v141 diff --git a/test/test.vcxproj b/test/test.vcxproj index b19fd4c..e238d19 100644 --- a/test/test.vcxproj +++ b/test/test.vcxproj @@ -22,12 +22,13 @@ {6B3E6769-052D-4BC0-9D2C-E9127C3DBB26} Win32Proj test + 8.1 Application true - v140 + v141 Unicode @@ -39,7 +40,7 @@ Application false - v140 + v141 true Unicode From b7b3588afbd203395497bc2b5f1cfb8fe555a838 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 30 Dec 2017 14:47:55 -0500 Subject: [PATCH 045/118] Connection timeout support on Client (Fixed #34) --- README.md | 5 ++ httplib.h | 126 +++++++++++++++++++++++++++++++++++++++------------ test/test.cc | 31 ++++--------- 3 files changed, 109 insertions(+), 53 deletions(-) diff --git a/README.md b/README.md index 69a7f11..291e4bd 100644 --- a/README.md +++ b/README.md @@ -118,6 +118,11 @@ params["note"] = "coder"; auto res = cli.post("/post", params); ``` +### Connection Timeout + +```c++ +httplib::Client cli("localhost", 8080, 5); // timeouts in 5 seconds +``` ### With Progress Callback ```cpp diff --git a/httplib.h b/httplib.h index cded0e1..cc000e6 100644 --- a/httplib.h +++ b/httplib.h @@ -27,7 +27,6 @@ #define S_ISDIR(m) (((m)&S_IFDIR)==S_IFDIR) #endif -#include #include #include #include @@ -57,6 +56,7 @@ typedef int socket_t; #include #include #include +#include #include #ifdef CPPHTTPLIB_OPENSSL_SUPPORT @@ -207,6 +207,8 @@ protected: private: typedef std::vector> Handlers; + socket_t create_server_socket(const char* host, int port, int socket_flags) const; + bool routing(Request& req, Response& res); bool handle_file_request(Request& req, Response& res); bool dispatch_request(Request& req, Response& res, Handlers& handlers); @@ -226,7 +228,12 @@ private: class Client { public: - Client(const char* host, int port, HttpVersion http_version = HttpVersion::v1_0); + Client( + const char* host, + int port = 80, + size_t timeout_sec = 300, + HttpVersion http_version = HttpVersion::v1_0); + virtual ~Client(); virtual bool is_valid() const; @@ -250,10 +257,12 @@ protected: const std::string host_; const int port_; + size_t timeout_sec_; const HttpVersion http_version_; const std::string host_and_port_; private: + socket_t create_client_socket() const; bool read_response_line(Stream& strm, Response& res); void write_request(Stream& strm, Request& req); @@ -292,7 +301,12 @@ private: class SSLClient : public Client { public: - SSLClient(const char* host, int port, HttpVersion http_version = HttpVersion::v1_0); + SSLClient( + const char* host, + int port = 80, + size_t timeout_sec = 300, + HttpVersion http_version = HttpVersion::v1_0); + virtual ~SSLClient(); virtual bool is_valid() const; @@ -406,7 +420,7 @@ inline int close_socket(socket_t sock) #endif } -inline int select(socket_t sock, size_t sec, size_t usec) +inline int select_read(socket_t sock, size_t sec, size_t usec) { fd_set fds; FD_ZERO(&fds); @@ -416,7 +430,28 @@ inline int select(socket_t sock, size_t sec, size_t usec) tv.tv_sec = sec; tv.tv_usec = usec; - return ::select(sock + 1, &fds, NULL, NULL, &tv); + return select(sock + 1, &fds, NULL, NULL, &tv); +} + +inline bool is_socket_writable(socket_t sock, size_t sec, size_t usec) +{ + fd_set fdsw; + FD_ZERO(&fdsw); + FD_SET(sock, &fdsw); + + fd_set fdse; + FD_ZERO(&fdse); + FD_SET(sock, &fdse); + + timeval tv; + tv.tv_sec = sec; + tv.tv_usec = usec; + + if (select(sock + 1, NULL, &fdsw, &fdse, &tv) <= 0) { + return false; + } + + return FD_ISSET(sock, &fdsw) != 0; } template @@ -427,7 +462,7 @@ inline bool read_and_close_socket(socket_t sock, bool keep_alive, T callback) if (keep_alive) { auto count = CPPHTTPLIB_KEEPALIVE_MAX_COUNT; while (count > 0 && - detail::select(sock, + detail::select_read(sock, CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND, CPPHTTPLIB_KEEPALIVE_TIMEOUT_USECOND) > 0) { auto last_connection = count == 1; @@ -507,27 +542,24 @@ socket_t create_socket(const char* host, int port, Fn fn, int socket_flags = 0) return -1; } -inline socket_t create_server_socket(const char* host, int port, int socket_flags) +inline void set_nonblocking(socket_t sock, bool nonblocking) { - return create_socket(host, port, [](socket_t sock, struct addrinfo& ai) -> socket_t { - if (::bind(sock, ai.ai_addr, ai.ai_addrlen)) { - return false; - } - if (listen(sock, 5)) { // Listen through 5 channels - return false; - } - return true; - }, socket_flags); +#ifdef _WIN32 + auto flags = nonblocking ? 1UL : 0UL; + ioctlsocket(sock, FIONBIO, &flags); +#else + auto flags = fcntl(sock, F_GETFL, 0); + fcntl(sock, F_SETFL, nonblocking ? (flags | O_NONBLOCK) : (flags & (~O_NONBLOCK))); +#endif } -inline socket_t create_client_socket(const char* host, int port) +inline bool is_connection_error() { - return create_socket(host, port, [](socket_t sock, struct addrinfo& ai) -> socket_t { - if (connect(sock, ai.ai_addr, ai.ai_addrlen)) { - return false; - } - return true; - }); +#ifdef _WIN32 + return WSAGetLastError() != WSAEWOULDBLOCK; +#else + return errno != EINPROGRESS; +#endif } inline bool is_file(const std::string& path) @@ -1339,7 +1371,7 @@ inline bool Server::listen(const char* host, int port, int socket_flags) return false; } - svr_sock_ = detail::create_server_socket(host, port, socket_flags); + svr_sock_ = create_server_socket(host, port, socket_flags); if (svr_sock_ == -1) { return false; } @@ -1347,7 +1379,7 @@ inline bool Server::listen(const char* host, int port, int socket_flags) auto ret = true; for (;;) { - auto val = detail::select(svr_sock_, 0, 100000); + auto val = detail::select_read(svr_sock_, 0, 100000); if (val == 0) { // Timeout if (svr_sock_ == -1) { @@ -1480,6 +1512,20 @@ inline bool Server::handle_file_request(Request& req, Response& res) return false; } +inline socket_t Server::create_server_socket(const char* host, int port, int socket_flags) const +{ + return detail::create_socket(host, port, + [](socket_t sock, struct addrinfo& ai) -> bool { + if (::bind(sock, ai.ai_addr, ai.ai_addrlen)) { + return false; + } + if (::listen(sock, 5)) { // Listen through 5 channels + return false; + } + return true; + }, socket_flags); +} + inline bool Server::routing(Request& req, Response& res) { if (req.method == "GET" && handle_file_request(req, res)) { @@ -1590,9 +1636,11 @@ inline bool Server::read_and_close_socket(socket_t sock) } // HTTP client implementation -inline Client::Client(const char* host, int port, HttpVersion http_version) +inline Client::Client( + const char* host, int port, size_t timeout_sec, HttpVersion http_version) : host_(host) , port_(port) + , timeout_sec_(timeout_sec) , http_version_(http_version) , host_and_port_(host_ + ":" + std::to_string(port_)) { @@ -1607,6 +1655,23 @@ inline bool Client::is_valid() const return true; } +inline socket_t Client::create_client_socket() const +{ + return detail::create_socket(host_.c_str(), port_, + [=](socket_t sock, struct addrinfo& ai) -> bool { + detail::set_nonblocking(sock, true); + + auto ret = connect(sock, ai.ai_addr, ai.ai_addrlen); + if (ret == -1 && detail::is_connection_error()) { + return false; + } + + detail::set_nonblocking(sock, false); + + return detail::is_socket_writable(sock, timeout_sec_, 0); + }); +} + inline bool Client::read_response_line(Stream& strm, Response& res) { const auto bufsiz = 2048; @@ -1634,7 +1699,7 @@ inline bool Client::send(Request& req, Response& res) return false; } - auto sock = detail::create_client_socket(host_.c_str(), port_); + auto sock = create_client_socket(); if (sock == -1) { return false; } @@ -1826,7 +1891,7 @@ inline bool read_and_close_socket_ssl( if (keep_alive) { auto count = CPPHTTPLIB_KEEPALIVE_MAX_COUNT; while (count > 0 && - detail::select(sock, + detail::select_read(sock, CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND, CPPHTTPLIB_KEEPALIVE_TIMEOUT_USECOND) > 0) { auto last_connection = count == 1; @@ -1936,8 +2001,9 @@ inline bool SSLServer::read_and_close_socket(socket_t sock) } // SSL HTTP client implementation -inline SSLClient::SSLClient(const char* host, int port, HttpVersion http_version) - : Client(host, port, http_version) +inline SSLClient::SSLClient( + const char* host, int port, size_t timeout_sec, HttpVersion http_version) + : Client(host, port, timeout_sec, http_version) { ctx_ = SSL_CTX_new(SSLv23_client_method()); } diff --git a/test/test.cc b/test/test.cc index 1edb8af..48e776b 100644 --- a/test/test.cc +++ b/test/test.cc @@ -63,24 +63,6 @@ TEST(ParseQueryTest, ParseQueryString) EXPECT_EQ("val3", dic.find("key3")->second); } -TEST(SocketTest, OpenClose) -{ - socket_t sock = detail::create_server_socket(HOST, PORT, 0); - ASSERT_NE(-1, sock); - - auto ret = detail::close_socket(sock); - EXPECT_EQ(0, ret); -} - -TEST(SocketTest, OpenCloseWithAI_PASSIVE) -{ - socket_t sock = detail::create_server_socket(nullptr, PORT, AI_PASSIVE); - ASSERT_NE(-1, sock); - - auto ret = detail::close_socket(sock); - EXPECT_EQ(0, ret); -} - TEST(GetHeaderValueTest, DefaultValue) { Headers headers = {{"Dummy","Dummy"}}; @@ -139,13 +121,14 @@ TEST(GetHeaderValueTest, Range) void testChunkedEncoding(httplib::HttpVersion ver) { auto host = "www.httpwatch.com"; + auto sec = 5; #ifdef CPPHTTPLIB_OPENSSL_SUPPORT auto port = 443; - httplib::SSLClient cli(host, port, ver); + httplib::SSLClient cli(host, port, sec, ver); #else auto port = 80; - httplib::Client cli(host, port, ver); + httplib::Client cli(host, port, sec, ver); #endif auto res = cli.get("/httpgallery/chunked/chunkedimage.aspx?0.4153841143030137"); @@ -167,13 +150,15 @@ TEST(ChunkedEncodingTest, FromHTTPWatch) TEST(RangeTest, FromHTTPBin) { auto host = "httpbin.org"; + auto sec = 5; + auto ver = httplib::HttpVersion::v1_1; #ifdef CPPHTTPLIB_OPENSSL_SUPPORT auto port = 443; - httplib::SSLClient cli(host, port, httplib::HttpVersion::v1_1); + httplib::SSLClient cli(host, port, sec, ver); #else auto port = 80; - httplib::Client cli(host, port, httplib::HttpVersion::v1_1); + httplib::Client cli(host, port, sec, ver); #endif { @@ -631,7 +616,7 @@ protected: res.set_content("Hello World!", "text/plain"); }); - t_ = thread([&](){ + t_ = thread([&]() { svr_.listen(nullptr, PORT, AI_PASSIVE); }); From 73fa1158038c55b994ace459dc20d2f348d5cd0c Mon Sep 17 00:00:00 2001 From: "Kevin B. Carpenter" Date: Tue, 6 Mar 2018 08:20:51 -0700 Subject: [PATCH 046/118] Added capture and setting of REMOTE_ADDR in request. --- httplib.h | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/httplib.h b/httplib.h index cc000e6..49539a4 100644 --- a/httplib.h +++ b/httplib.h @@ -157,6 +157,7 @@ public: virtual int read(char* ptr, size_t size) = 0; virtual int write(const char* ptr, size_t size1) = 0; virtual int write(const char* ptr) = 0; + virtual std::string get_remote_addr() = 0; template void write_format(const char* fmt, const Args& ...args); @@ -170,6 +171,26 @@ public: virtual int read(char* ptr, size_t size); virtual int write(const char* ptr, size_t size); virtual int write(const char* ptr); + + std::string get_remote_addr() { + socklen_t len; + struct sockaddr_storage addr; + char ipstr[INET6_ADDRSTRLEN]; + + len = sizeof addr; + getpeername(sock_, (struct sockaddr*)&addr, &len); + + // deal with both IPv4 and IPv6: + if (addr.ss_family == AF_INET) { + struct sockaddr_in *s = (struct sockaddr_in *)&addr; + inet_ntop(AF_INET, &s->sin_addr, ipstr, sizeof ipstr); + } else { // AF_INET6 + struct sockaddr_in6 *s = (struct sockaddr_in6 *)&addr; + inet_ntop(AF_INET6, &s->sin6_addr, ipstr, sizeof ipstr); + } + + return ipstr; + } private: socket_t sock_; @@ -1583,6 +1604,8 @@ inline bool Server::process_request(Stream& strm, bool last_connection) ret = false; } + req.set_header("REMOTE_ADDR", strm.get_remote_addr().c_str()); + // Body if (req.method == "POST") { if (!detail::read_content(strm, req)) { From b6790b39c15339aef740c927ed167d2f6023f8ba Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 13 Mar 2018 22:44:28 -0400 Subject: [PATCH 047/118] Fixed build problem with OPENSSL_SUPPORT --- httplib.h | 58 ++++++++++++++++++++++++++++++++----------------------- 1 file changed, 34 insertions(+), 24 deletions(-) diff --git a/httplib.h b/httplib.h index 49539a4..0a60c4b 100644 --- a/httplib.h +++ b/httplib.h @@ -171,26 +171,7 @@ public: virtual int read(char* ptr, size_t size); virtual int write(const char* ptr, size_t size); virtual int write(const char* ptr); - - std::string get_remote_addr() { - socklen_t len; - struct sockaddr_storage addr; - char ipstr[INET6_ADDRSTRLEN]; - - len = sizeof addr; - getpeername(sock_, (struct sockaddr*)&addr, &len); - - // deal with both IPv4 and IPv6: - if (addr.ss_family == AF_INET) { - struct sockaddr_in *s = (struct sockaddr_in *)&addr; - inet_ntop(AF_INET, &s->sin_addr, ipstr, sizeof ipstr); - } else { // AF_INET6 - struct sockaddr_in6 *s = (struct sockaddr_in6 *)&addr; - inet_ntop(AF_INET6, &s->sin6_addr, ipstr, sizeof ipstr); - } - - return ipstr; - } + virtual std::string get_remote_addr(); private: socket_t sock_; @@ -293,14 +274,16 @@ private: #ifdef CPPHTTPLIB_OPENSSL_SUPPORT class SSLSocketStream : public Stream { public: - SSLSocketStream(SSL* ssl); + SSLSocketStream(socket_t sock, SSL* ssl); virtual ~SSLSocketStream(); virtual int read(char* ptr, size_t size); virtual int write(const char* ptr, size_t size); virtual int write(const char* ptr); + virtual std::string get_remote_addr(); private: + socket_t sock_; SSL* ssl_; }; @@ -583,6 +566,24 @@ inline bool is_connection_error() #endif } +inline std::string get_remote_addr(socket_t sock) { + struct sockaddr_storage addr; + char ipstr[INET6_ADDRSTRLEN]; + socklen_t len = sizeof(addr); + getpeername(sock, (struct sockaddr*)&addr, &len); + + // deal with both IPv4 and IPv6: + if (addr.ss_family == AF_INET) { + auto s = (struct sockaddr_in *)&addr; + inet_ntop(AF_INET, &s->sin_addr, ipstr, sizeof(ipstr)); + } else { // AF_INET6 + auto s = (struct sockaddr_in6 *)&addr; + inet_ntop(AF_INET6, &s->sin6_addr, ipstr, sizeof(ipstr)); + } + + return ipstr; +} + inline bool is_file(const std::string& path) { struct stat st; @@ -1341,6 +1342,10 @@ inline int SocketStream::write(const char* ptr) return write(ptr, strlen(ptr)); } +inline std::string SocketStream::get_remote_addr() { + return detail::get_remote_addr(sock_); +} + // HTTP server implementation inline Server::Server(HttpVersion http_version) : http_version_(http_version) @@ -1918,7 +1923,7 @@ inline bool read_and_close_socket_ssl( CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND, CPPHTTPLIB_KEEPALIVE_TIMEOUT_USECOND) > 0) { auto last_connection = count == 1; - SSLSocketStream strm(ssl); + SSLSocketStream strm(sock, ssl); ret = callback(strm, last_connection); if (!ret) { break; @@ -1926,7 +1931,7 @@ inline bool read_and_close_socket_ssl( count--; } } else { - SSLSocketStream strm(ssl); + SSLSocketStream strm(sock, ssl); ret = callback(strm, true); } @@ -1949,7 +1954,8 @@ static SSLInit sslinit_; } // namespace detail // SSL socket stream implementation -inline SSLSocketStream::SSLSocketStream(SSL* ssl): ssl_(ssl) +inline SSLSocketStream::SSLSocketStream(socket_t sock, SSL* ssl) + : sock_(sock), ssl_(ssl) { } @@ -1972,6 +1978,10 @@ inline int SSLSocketStream::write(const char* ptr) return write(ptr, strlen(ptr)); } +inline std::string SSLSocketStream::get_remote_addr() { + return detail::get_remote_addr(sock_); +} + // SSL HTTP server implementation inline SSLServer::SSLServer(const char* cert_path, const char* private_key_path, HttpVersion http_version) : Server(http_version) From 4e391fdae663f7fdf31c4f637f7ec55beef427ea Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 13 Mar 2018 23:03:54 -0400 Subject: [PATCH 048/118] Added a unit test for REMOTE_ADDR --- test/test.cc | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/test.cc b/test/test.cc index 48e776b..3e4daf1 100644 --- a/test/test.cc +++ b/test/test.cc @@ -201,6 +201,10 @@ protected: svr_.get("/hi", [&](const Request& /*req*/, Response& res) { res.set_content("Hello World!", "text/plain"); }) + .get("/remote_addr", [&](const Request& req, Response& res) { + auto remote_addr = req.headers.find("REMOTE_ADDR")->second; + res.set_content(remote_addr.c_str(), "text/plain"); + }) .get("/endwith%", [&](const Request& /*req*/, Response& res) { res.set_content("Hello World!", "text/plain"); }) @@ -574,6 +578,15 @@ TEST_F(ServerTest, CaseInsensitiveHeaderName) EXPECT_EQ("Hello World!", res->body); } +TEST_F(ServerTest, GetMethodRemoteAddr) +{ + auto res = cli_.get("/remote_addr"); + ASSERT_TRUE(res != nullptr); + EXPECT_EQ(200, res->status); + EXPECT_EQ("text/plain", res->get_header_value("Content-Type")); + EXPECT_EQ("::1", res->body); // NOTE: depends on user's environment... +} + #ifdef CPPHTTPLIB_ZLIB_SUPPORT TEST_F(ServerTest, Gzip) { From a0f50911e176a616855ec911728f069cf6f81bca Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 6 Apr 2018 16:09:41 -0400 Subject: [PATCH 049/118] Fixed toolset setting of test/test.vcxproj --- test/test.vcxproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test.vcxproj b/test/test.vcxproj index e238d19..7b27b69 100644 --- a/test/test.vcxproj +++ b/test/test.vcxproj @@ -47,7 +47,7 @@ Application false - v140 + v141 true Unicode From 5536d4c1ffb4f0725b6fca347de79918821c806a Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 6 Apr 2018 17:02:37 -0400 Subject: [PATCH 050/118] Fix #44 --- httplib.h | 36 ++++++++++++++++++++------------ test/test.cc | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 79 insertions(+), 15 deletions(-) diff --git a/httplib.h b/httplib.h index 0a60c4b..1716d0e 100644 --- a/httplib.h +++ b/httplib.h @@ -437,25 +437,32 @@ inline int select_read(socket_t sock, size_t sec, size_t usec) return select(sock + 1, &fds, NULL, NULL, &tv); } -inline bool is_socket_writable(socket_t sock, size_t sec, size_t usec) +inline bool wait_until_socket_is_ready(socket_t sock, size_t sec, size_t usec) { - fd_set fdsw; - FD_ZERO(&fdsw); - FD_SET(sock, &fdsw); + fd_set fdsr; + FD_ZERO(&fdsr); + FD_SET(sock, &fdsr); - fd_set fdse; - FD_ZERO(&fdse); - FD_SET(sock, &fdse); + auto fdsw = fdsr; + auto fdse = fdsr; timeval tv; tv.tv_sec = sec; tv.tv_usec = usec; - if (select(sock + 1, NULL, &fdsw, &fdse, &tv) <= 0) { + if (select(sock + 1, &fdsr, &fdsw, &fdse, &tv) < 0) { + return false; + } else if (FD_ISSET(sock, &fdsr) || FD_ISSET(sock, &fdsw)) { + int error = 0; + socklen_t len = sizeof(error); + if (getsockopt(sock, SOL_SOCKET, SO_ERROR, (char*)&error, &len) < 0 || error) { + return false; + } + } else { return false; } - return FD_ISSET(sock, &fdsw) != 0; + return true; } template @@ -1690,13 +1697,16 @@ inline socket_t Client::create_client_socket() const detail::set_nonblocking(sock, true); auto ret = connect(sock, ai.ai_addr, ai.ai_addrlen); - if (ret == -1 && detail::is_connection_error()) { - return false; + if (ret < 0) { + if (detail::is_connection_error() || + !detail::wait_until_socket_is_ready(sock, timeout_sec_, 0)) { + detail::close_socket(sock); + return false; + } } detail::set_nonblocking(sock, false); - - return detail::is_socket_writable(sock, timeout_sec_, 0); + return true; }); } diff --git a/test/test.cc b/test/test.cc index 3e4daf1..815246b 100644 --- a/test/test.cc +++ b/test/test.cc @@ -121,7 +121,7 @@ TEST(GetHeaderValueTest, Range) void testChunkedEncoding(httplib::HttpVersion ver) { auto host = "www.httpwatch.com"; - auto sec = 5; + auto sec = 2; #ifdef CPPHTTPLIB_OPENSSL_SUPPORT auto port = 443; @@ -186,6 +186,60 @@ TEST(RangeTest, FromHTTPBin) } } +TEST(ConnectionErrorTest, InvalidHost) +{ + auto host = "abcde.com"; + auto sec = 2; + auto ver = httplib::HttpVersion::v1_1; + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + auto port = 443; + httplib::SSLClient cli(host, port, sec, ver); +#else + auto port = 80; + httplib::Client cli(host, port, sec, ver); +#endif + + auto res = cli.get("/"); + ASSERT_TRUE(res == nullptr); +} + +TEST(ConnectionErrorTest, InvalidPort) +{ + auto host = "localhost"; + auto sec = 2; + auto ver = httplib::HttpVersion::v1_1; + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + auto port = 44380; + httplib::SSLClient cli(host, port, sec, ver); +#else + auto port = 8080; + httplib::Client cli(host, port, sec, ver); +#endif + + auto res = cli.get("/"); + ASSERT_TRUE(res == nullptr); +} + +TEST(ConnectionErrorTest, Timeout) +{ + auto host = "google.com"; + auto sec = 2; + auto ver = httplib::HttpVersion::v1_1; + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + auto port = 44380; + httplib::SSLClient cli(host, port, sec, ver); +#else + auto port = 8080; + httplib::Client cli(host, port, sec, ver); +#endif + + auto res = cli.get("/"); + ASSERT_TRUE(res == nullptr); +} + class ServerTest : public ::testing::Test { protected: ServerTest() @@ -584,7 +638,7 @@ TEST_F(ServerTest, GetMethodRemoteAddr) ASSERT_TRUE(res != nullptr); EXPECT_EQ(200, res->status); EXPECT_EQ("text/plain", res->get_header_value("Content-Type")); - EXPECT_EQ("::1", res->body); // NOTE: depends on user's environment... + EXPECT_TRUE(res->body == "::1" || res->body == "127.0.0.1"); } #ifdef CPPHTTPLIB_ZLIB_SUPPORT From 0e239a0014146ab8f8fa112d86a11343be5196c6 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 14 Apr 2018 18:42:56 -0400 Subject: [PATCH 051/118] Fix #47 --- httplib.h | 5 +++-- test/test.cc | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/httplib.h b/httplib.h index 1716d0e..dbb47de 100644 --- a/httplib.h +++ b/httplib.h @@ -1026,10 +1026,11 @@ inline bool parse_multipart_formdata( static std::string crlf = "\r\n"; static std::regex re_content_type( - "Content-Type: (.*?)"); + "Content-Type: (.*?)", std::regex_constants::icase); static std::regex re_content_disposition( - "Content-Disposition: form-data; name=\"(.*?)\"(?:; filename=\"(.*?)\")?"); + "Content-Disposition: form-data; name=\"(.*?)\"(?:; filename=\"(.*?)\")?", + std::regex_constants::icase); auto dash_boundary = dash + boundary; diff --git a/test/test.cc b/test/test.cc index 815246b..53474a7 100644 --- a/test/test.cc +++ b/test/test.cc @@ -614,7 +614,7 @@ TEST_F(ServerTest, MultipartFormData) req.headers.emplace("User-Agent", "cpp-httplib/0.1"); req.headers.emplace("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundarysBREP3G013oUrLB4"); - req.body = "------WebKitFormBoundarysBREP3G013oUrLB4\r\nContent-Disposition: form-data; name=\"text1\"\r\n\r\ntext default\r\n------WebKitFormBoundarysBREP3G013oUrLB4\r\nContent-Disposition: form-data; name=\"text2\"\r\n\r\naωb\r\n------WebKitFormBoundarysBREP3G013oUrLB4\r\nContent-Disposition: form-data; name=\"file1\"; filename=\"hello.txt\"\r\nContent-Type: text/plain\r\n\r\nh\ne\n\nl\nl\no\n\r\n------WebKitFormBoundarysBREP3G013oUrLB4\r\nContent-Disposition: form-data; name=\"file2\"; filename=\"world.json\"\r\nContent-Type: application/json\r\n\r\n{\n \"world\", true\n}\n\r\n------WebKitFormBoundarysBREP3G013oUrLB4\r\nContent-Disposition: form-data; name=\"file3\"; filename=\"\"\r\nContent-Type: application/octet-stream\r\n\r\n\r\n------WebKitFormBoundarysBREP3G013oUrLB4--\r\n"; + req.body = "------WebKitFormBoundarysBREP3G013oUrLB4\r\nContent-Disposition: form-data; name=\"text1\"\r\n\r\ntext default\r\n------WebKitFormBoundarysBREP3G013oUrLB4\r\nContent-Disposition: form-data; name=\"text2\"\r\n\r\naωb\r\n------WebKitFormBoundarysBREP3G013oUrLB4\r\nContent-Disposition: form-data; name=\"file1\"; filename=\"hello.txt\"\r\nContent-Type: text/plain\r\n\r\nh\ne\n\nl\nl\no\n\r\n------WebKitFormBoundarysBREP3G013oUrLB4\r\nContent-Disposition: form-data; name=\"file2\"; filename=\"world.json\"\r\nContent-Type: application/json\r\n\r\n{\n \"world\", true\n}\n\r\n------WebKitFormBoundarysBREP3G013oUrLB4\r\ncontent-disposition: form-data; name=\"file3\"; filename=\"\"\r\ncontent-type: application/octet-stream\r\n\r\n\r\n------WebKitFormBoundarysBREP3G013oUrLB4--\r\n"; auto res = std::make_shared(); auto ret = cli_.send(req, *res); From 0515c6aad6834a2a329db226b9312294d0002a13 Mon Sep 17 00:00:00 2001 From: Scott Graham Date: Sun, 15 Apr 2018 00:56:00 -0700 Subject: [PATCH 052/118] Support system-assigned port via two part listen() This fixes #46 by allowing the user to separate the port bind from the blocking listen(). Two new API functions bind_to_any_port() (which returns the system-assigned port) and listen_after_bind() are equivalent to the existing listen(). --- httplib.h | 115 +++++++++++++++++++++++++++++++++------------------ test/test.cc | 7 ++++ 2 files changed, 82 insertions(+), 40 deletions(-) diff --git a/httplib.h b/httplib.h index dbb47de..3ef624f 100644 --- a/httplib.h +++ b/httplib.h @@ -196,6 +196,9 @@ public: void set_error_handler(Handler handler); void set_logger(Logger logger); + int bind_to_any_port(const char* host, int socket_flags = 0); + bool listen_after_bind(); + bool listen(const char* host, int port, int socket_flags = 0); bool is_running() const; @@ -210,6 +213,8 @@ private: typedef std::vector> Handlers; socket_t create_server_socket(const char* host, int port, int socket_flags) const; + int bind_internal(const char* host, int port, int socket_flags); + bool listen_internal(); bool routing(Request& req, Response& res); bool handle_file_request(Request& req, Response& res); @@ -1399,49 +1404,20 @@ inline void Server::set_logger(Logger logger) logger_ = logger; } +inline int Server::bind_to_any_port(const char* host, int socket_flags) +{ + return bind_internal(host, 0, socket_flags); +} + +inline bool Server::listen_after_bind() { + return listen_internal(); +} + inline bool Server::listen(const char* host, int port, int socket_flags) { - if (!is_valid()) { + if (bind_internal(host, port, socket_flags) < 0) return false; - } - - svr_sock_ = create_server_socket(host, port, socket_flags); - if (svr_sock_ == -1) { - return false; - } - - auto ret = true; - - for (;;) { - auto val = detail::select_read(svr_sock_, 0, 100000); - - if (val == 0) { // Timeout - if (svr_sock_ == -1) { - // The server socket was closed by 'stop' method. - break; - } - continue; - } - - socket_t sock = accept(svr_sock_, NULL, NULL); - - if (sock == -1) { - if (svr_sock_ != -1) { - detail::close_socket(svr_sock_); - ret = false; - } else { - ; // The server socket was closed by user. - } - break; - } - - // TODO: Use thread pool... - std::thread([=]() { - read_and_close_socket(sock); - }).detach(); - } - - return ret; + return listen_internal(); } inline bool Server::is_running() const @@ -1560,6 +1536,65 @@ inline socket_t Server::create_server_socket(const char* host, int port, int soc }, socket_flags); } +inline int Server::bind_internal(const char* host, int port, int socket_flags) +{ + if (!is_valid()) { + return -1; + } + + svr_sock_ = create_server_socket(host, port, socket_flags); + if (svr_sock_ == -1) { + return -1; + } + + if (port == 0) { + struct sockaddr_in sin; + socklen_t len = sizeof(sin); + if (getsockname(svr_sock_, reinterpret_cast(&sin), &len) == -1) { + return -1; + } + return ntohs(sin.sin_port); + } else { + return port; + } +} + +inline bool Server::listen_internal() +{ + auto ret = true; + + for (;;) { + auto val = detail::select_read(svr_sock_, 0, 100000); + + if (val == 0) { // Timeout + if (svr_sock_ == -1) { + // The server socket was closed by 'stop' method. + break; + } + continue; + } + + socket_t sock = accept(svr_sock_, NULL, NULL); + + if (sock == -1) { + if (svr_sock_ != -1) { + detail::close_socket(svr_sock_); + ret = false; + } else { + ; // The server socket was closed by user. + } + break; + } + + // TODO: Use thread pool... + std::thread([=]() { + read_and_close_socket(sock); + }).detach(); + } + + return ret; +} + inline bool Server::routing(Request& req, Response& res) { if (req.method == "GET" && handle_file_request(req, res)) { diff --git a/test/test.cc b/test/test.cc index 53474a7..8b027d4 100644 --- a/test/test.cc +++ b/test/test.cc @@ -240,6 +240,13 @@ TEST(ConnectionErrorTest, Timeout) ASSERT_TRUE(res == nullptr); } +TEST(Server, BindAndListenSeparately) { + Server svr(httplib::HttpVersion::v1_1); + int port = svr.bind_to_any_port("localhost"); + ASSERT_TRUE(port > 0); + svr.stop(); +} + class ServerTest : public ::testing::Test { protected: ServerTest() From 87c673fd6736c5f95e9bf20d017b9eb6b8bf54f2 Mon Sep 17 00:00:00 2001 From: Scott Graham Date: Mon, 16 Apr 2018 10:28:59 -0700 Subject: [PATCH 053/118] Read \r\n terminator after chunked encoding --- httplib.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/httplib.h b/httplib.h index dbb47de..015988a 100644 --- a/httplib.h +++ b/httplib.h @@ -828,6 +828,12 @@ bool read_content_chunked(Stream& strm, T& x) chunk_len = std::stoi(reader.ptr(), 0, 16); } + if (chunk_len == 0) { + // Reader terminator after chunks + if (!reader.getline() || strcmp(reader.ptr(), "\r\n")) + return false; + } + return true; } From 5579d4d10130d1412e0db144339268525c4f4ae2 Mon Sep 17 00:00:00 2001 From: Scott Graham Date: Mon, 16 Apr 2018 11:01:17 -0700 Subject: [PATCH 054/118] Support Content-Encoding: gzip on server side If the client specifies Content-Encoding: gzip for POST requests, decompress the body before attempting to parse it. --- httplib.h | 48 ++++++++++++++++++++++++++++++++++++ test/test.cc | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 117 insertions(+) diff --git a/httplib.h b/httplib.h index dbb47de..6e4b5d6 100644 --- a/httplib.h +++ b/httplib.h @@ -701,6 +701,7 @@ inline const char* status_message(int status) case 200: return "OK"; case 400: return "Bad Request"; case 404: return "Not Found"; + case 406: return "Not Acceptable"; default: case 500: return "Internal Server Error"; } @@ -1187,6 +1188,43 @@ inline void compress(const Request& req, Response& res) deflateEnd(&strm); } + +inline void decompress_request_body(Request& req) +{ + if (req.get_header_value("Content-Encoding") != "gzip") + return; + + z_stream strm; + strm.zalloc = Z_NULL; + strm.zfree = Z_NULL; + strm.opaque = Z_NULL; + + // 15 is the value of wbits, which should be at the maximum possible value to ensure + // that any gzip stream can be decoded. The offset of 16 specifies that the stream + // to decompress will be formatted with a gzip wrapper. + auto ret = inflateInit2(&strm, 16 + 15); + if (ret != Z_OK) { + return; + } + + strm.avail_in = req.body.size(); + strm.next_in = (Bytef *)req.body.data(); + + std::string decompressed; + + const auto bufsiz = 16384; + char buff[bufsiz]; + do { + strm.avail_out = bufsiz; + strm.next_out = (Bytef *)buff; + inflate(&strm, Z_NO_FLUSH); + decompressed.append(buff, bufsiz - strm.avail_out); + } while (strm.avail_out == 0); + + req.body.swap(decompressed); + + inflateEnd(&strm); +} #endif #ifdef _WIN32 @@ -1629,6 +1667,16 @@ inline bool Server::process_request(Stream& strm, bool last_connection) const auto& content_type = req.get_header_value("Content-Type"); +#ifdef CPPHTTPLIB_ZLIB_SUPPORT + detail::decompress_request_body(req); +#else + if (req.get_header_value("Content-Encoding") == "gzip") { + res.status = 406; + write_response(strm, last_connection, req, res); + return ret; + } +#endif + if (!content_type.find("application/x-www-form-urlencoded")) { detail::parse_query_text(req.body, req.params); } else if(!content_type.find("multipart/form-data")) { diff --git a/test/test.cc b/test/test.cc index 53474a7..859d51f 100644 --- a/test/test.cc +++ b/test/test.cc @@ -318,6 +318,22 @@ protected: .get("/nogzip", [&](const Request& /*req*/, Response& res) { res.set_content("1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890", "application/octet-stream"); }) + .post("/gzipmultipart", [&](const Request& req, Response& /*res*/) { + EXPECT_EQ(2u, req.files.size()); + ASSERT_TRUE(!req.has_file("???")); + + { + const auto& file = req.get_file_value("key1"); + EXPECT_EQ("", file.filename); + EXPECT_EQ("test", req.body.substr(file.offset, file.length)); + } + + { + const auto& file = req.get_file_value("key2"); + EXPECT_EQ("", file.filename); + EXPECT_EQ("--abcdefg123", req.body.substr(file.offset, file.length)); + } + }) #endif ; @@ -667,6 +683,59 @@ TEST_F(ServerTest, NoGzip) EXPECT_EQ("100", res->get_header_value("Content-Length")); EXPECT_EQ(200, res->status); } + +TEST_F(ServerTest, MultipartFormDataGzip) +{ + Request req; + req.method = "POST"; + req.path = "/gzipmultipart"; + + std::string host_and_port; + host_and_port += HOST; + host_and_port += ":"; + host_and_port += std::to_string(PORT); + + req.headers.emplace("Host", host_and_port.c_str()); + req.headers.emplace("Accept", "*/*"); + req.headers.emplace("User-Agent", "cpp-httplib/0.1"); + req.headers.emplace("Content-Type", "multipart/form-data; boundary=------------------------fcba8368a9f48c0f"); + req.headers.emplace("Content-Encoding", "gzip"); + + // compressed_body generated by creating input.txt to this file: + /* + --------------------------fcba8368a9f48c0f + Content-Disposition: form-data; name="key1" + + test + --------------------------fcba8368a9f48c0f + Content-Disposition: form-data; name="key2" + + --abcdefg123 + --------------------------fcba8368a9f48c0f-- + */ + // then running unix2dos input.txt; gzip -9 -c input.txt | xxd -i. + uint8_t compressed_body[] = { + 0x1f, 0x8b, 0x08, 0x08, 0x48, 0xf1, 0xd4, 0x5a, 0x02, 0x03, 0x69, 0x6e, + 0x70, 0x75, 0x74, 0x2e, 0x74, 0x78, 0x74, 0x00, 0xd3, 0xd5, 0xc5, 0x05, + 0xd2, 0x92, 0x93, 0x12, 0x2d, 0x8c, 0xcd, 0x2c, 0x12, 0x2d, 0xd3, 0x4c, + 0x2c, 0x92, 0x0d, 0xd2, 0x78, 0xb9, 0x9c, 0xf3, 0xf3, 0x4a, 0x52, 0xf3, + 0x4a, 0x74, 0x5d, 0x32, 0x8b, 0x0b, 0xf2, 0x8b, 0x33, 0x4b, 0x32, 0xf3, + 0xf3, 0xac, 0x14, 0xd2, 0xf2, 0x8b, 0x72, 0x75, 0x53, 0x12, 0x4b, 0x12, + 0xad, 0x15, 0xf2, 0x12, 0x73, 0x53, 0x6d, 0x95, 0xb2, 0x53, 0x2b, 0x0d, + 0x95, 0x78, 0xb9, 0x78, 0xb9, 0x4a, 0x52, 0x8b, 0x4b, 0x78, 0xb9, 0x74, + 0x69, 0x61, 0x81, 0x11, 0xd8, 0x02, 0x5d, 0xdd, 0xc4, 0xa4, 0xe4, 0x94, + 0xd4, 0xb4, 0x74, 0x43, 0x23, 0x63, 0x52, 0x2c, 0xd2, 0xd5, 0xe5, 0xe5, + 0x02, 0x00, 0xff, 0x0e, 0x72, 0xdf, 0xf8, 0x00, 0x00, 0x00 + }; + + req.body = std::string((char*)compressed_body, sizeof(compressed_body) / sizeof(compressed_body[0])); + + auto res = std::make_shared(); + auto ret = cli_.send(req, *res); + + ASSERT_TRUE(ret); + EXPECT_EQ(200, res->status); +} #endif class ServerTestWithAI_PASSIVE : public ::testing::Test { From 66550eb71b8fef96f169124469281e5fafc42763 Mon Sep 17 00:00:00 2001 From: yhirose Date: Mon, 16 Apr 2018 21:01:36 -0400 Subject: [PATCH 055/118] Changed to return 415 instead of 406 for invalid Content-Encoding. --- httplib.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/httplib.h b/httplib.h index 3c902f6..65de818 100644 --- a/httplib.h +++ b/httplib.h @@ -706,7 +706,7 @@ inline const char* status_message(int status) case 200: return "OK"; case 400: return "Bad Request"; case 404: return "Not Found"; - case 406: return "Not Acceptable"; + case 415: return "Unsupported Media Type"; default: case 500: return "Internal Server Error"; } @@ -1712,7 +1712,7 @@ inline bool Server::process_request(Stream& strm, bool last_connection) detail::decompress_request_body(req); #else if (req.get_header_value("Content-Encoding") == "gzip") { - res.status = 406; + res.status = 415; write_response(strm, last_connection, req, res); return ret; } From 3c711089e58a1ffc6ce74ef4e0012fd06dc3065e Mon Sep 17 00:00:00 2001 From: yhirose Date: Mon, 16 Apr 2018 22:12:45 -0400 Subject: [PATCH 056/118] Temporary solution for #52 --- httplib.h | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/httplib.h b/httplib.h index 65de818..aabb0f8 100644 --- a/httplib.h +++ b/httplib.h @@ -52,6 +52,7 @@ typedef int socket_t; #include #include #include +#include #include #include #include @@ -231,6 +232,10 @@ private: Handlers post_handlers_; Handler error_handler_; Logger logger_; + + // TODO: Use thread pool... + std::mutex running_threads_mutex_; + int running_threads_; }; class Client { @@ -1407,6 +1412,7 @@ inline std::string SocketStream::get_remote_addr() { inline Server::Server(HttpVersion http_version) : http_version_(http_version) , svr_sock_(-1) + , running_threads_(0) { #ifndef _WIN32 signal(SIGPIPE, SIG_IGN); @@ -1632,10 +1638,29 @@ inline bool Server::listen_internal() // TODO: Use thread pool... std::thread([=]() { + { + std::lock_guard guard(running_threads_mutex_); + running_threads_++; + } + read_and_close_socket(sock); + + { + std::lock_guard guard(running_threads_mutex_); + running_threads_--; + } }).detach(); } + // TODO: Use thread pool... + for (;;) { + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + std::lock_guard guard(running_threads_mutex_); + if (!running_threads_) { + break; + } + } + return ret; } From 9dc4e230821339d74fabfb87328396a3a7abe319 Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 17 Apr 2018 00:05:05 -0400 Subject: [PATCH 057/118] Unit test for #52 --- httplib.h | 7 +++++++ test/test.cc | 13 +++++++++++++ 2 files changed, 20 insertions(+) diff --git a/httplib.h b/httplib.h index aabb0f8..4607342 100644 --- a/httplib.h +++ b/httplib.h @@ -205,6 +205,8 @@ public: bool is_running() const; void stop(); + bool is_handling_requests() const; + protected: bool process_request(Stream& strm, bool last_connection); @@ -1482,6 +1484,11 @@ inline void Server::stop() svr_sock_ = -1; } +inline bool Server::is_handling_requests() const +{ + return running_threads_ > 0; +} + inline bool Server::parse_request_line(const char* s, Request& req) { static std::regex re("(GET|HEAD|POST) ([^?]+)(?:\\?(.+?))? (HTTP/1\\.[01])\r\n"); diff --git a/test/test.cc b/test/test.cc index de2ebf2..2e91ad7 100644 --- a/test/test.cc +++ b/test/test.cc @@ -262,6 +262,10 @@ protected: svr_.get("/hi", [&](const Request& /*req*/, Response& res) { res.set_content("Hello World!", "text/plain"); }) + .get("/slow", [&](const Request& /*req*/, Response& res) { + msleep(3000); + res.set_content("slow", "text/plain"); + }) .get("/remote_addr", [&](const Request& req, Response& res) { auto remote_addr = req.headers.find("REMOTE_ADDR")->second; res.set_content(remote_addr.c_str(), "text/plain"); @@ -358,6 +362,7 @@ protected: virtual void TearDown() { svr_.stop(); t_.join(); + EXPECT_EQ(false, svr_.is_handling_requests()); } map persons_; @@ -664,6 +669,14 @@ TEST_F(ServerTest, GetMethodRemoteAddr) EXPECT_TRUE(res->body == "::1" || res->body == "127.0.0.1"); } +TEST_F(ServerTest, SlowRequest) +{ + std::thread([=]() { auto res = cli_.get("/slow"); }).detach(); + std::thread([=]() { auto res = cli_.get("/slow"); }).detach(); + std::thread([=]() { auto res = cli_.get("/slow"); }).detach(); + msleep(1000); +} + #ifdef CPPHTTPLIB_ZLIB_SUPPORT TEST_F(ServerTest, Gzip) { From ca3613cd21aa1f578b6e7828cc130dea865d52de Mon Sep 17 00:00:00 2001 From: Scott Graham Date: Mon, 16 Apr 2018 21:43:41 -0700 Subject: [PATCH 058/118] Make 'chunked' in Transfer-Encoding case-insensitive --- httplib.h | 6 +++++- test/test.cc | 31 +++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 4607342..9bf7365 100644 --- a/httplib.h +++ b/httplib.h @@ -34,6 +34,10 @@ #undef min #undef max +#ifndef strcasecmp +#define strcasecmp _stricmp +#endif + typedef SOCKET socket_t; #else #include @@ -860,7 +864,7 @@ bool read_content(Stream& strm, T& x, Progress progress = Progress()) } else { const auto& encoding = get_header_value(x.headers, "Transfer-Encoding", ""); - if (!strcmp(encoding, "chunked")) { + if (!strcasecmp(encoding, "chunked")) { return read_content_chunked(strm, x); } else { return read_content_without_length(strm, x); diff --git a/test/test.cc b/test/test.cc index 2e91ad7..46a9c5e 100644 --- a/test/test.cc +++ b/test/test.cc @@ -292,6 +292,9 @@ protected: res.status = 404; } }) + .post("/chunked", [&](const Request& req, Response& /*res*/) { + EXPECT_EQ(req.body, "dechunked post body"); + }) .post("/multipart", [&](const Request& req, Response& /*res*/) { EXPECT_EQ(5u, req.files.size()); ASSERT_TRUE(!req.has_file("???")); @@ -660,6 +663,34 @@ TEST_F(ServerTest, CaseInsensitiveHeaderName) EXPECT_EQ("Hello World!", res->body); } +TEST_F(ServerTest, CaseInsensitiveTransferEncoding) +{ + Request req; + req.method = "POST"; + req.path = "/chunked"; + + std::string host_and_port; + host_and_port += HOST; + host_and_port += ":"; + host_and_port += std::to_string(PORT); + + req.headers.emplace("Host", host_and_port.c_str()); + req.headers.emplace("Accept", "*/*"); + req.headers.emplace("User-Agent", "cpp-httplib/0.1"); + req.headers.emplace("Content-Type", "text/plain"); + req.headers.emplace("Content-Length", "0"); + req.headers.emplace("Transfer-Encoding", "Chunked"); // Note, "Chunked" rather than typical "chunked". + + // Client does not chunk, so make a chunked body manually. + req.body = "4\r\ndech\r\nf\r\nunked post body\r\n0\r\n\r\n"; + + auto res = std::make_shared(); + auto ret = cli_.send(req, *res); + + ASSERT_TRUE(ret); + EXPECT_EQ(200, res->status); +} + TEST_F(ServerTest, GetMethodRemoteAddr) { auto res = cli_.get("/remote_addr"); From bc051219f9f6e28bbe7aa8e177e5aa8f08f81dfa Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 17 Apr 2018 13:06:35 -0400 Subject: [PATCH 059/118] Removed is_handling_requests --- httplib.h | 2 -- test/test.cc | 1 - 2 files changed, 3 deletions(-) diff --git a/httplib.h b/httplib.h index 9bf7365..b822a29 100644 --- a/httplib.h +++ b/httplib.h @@ -209,8 +209,6 @@ public: bool is_running() const; void stop(); - bool is_handling_requests() const; - protected: bool process_request(Stream& strm, bool last_connection); diff --git a/test/test.cc b/test/test.cc index 46a9c5e..bae6901 100644 --- a/test/test.cc +++ b/test/test.cc @@ -365,7 +365,6 @@ protected: virtual void TearDown() { svr_.stop(); t_.join(); - EXPECT_EQ(false, svr_.is_handling_requests()); } map persons_; From 7d6df0c65157af844afa6bd66db59fdcc9d9e5fc Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 17 Apr 2018 13:07:15 -0400 Subject: [PATCH 060/118] Fixed toolset setting --- test/test.vcxproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test.vcxproj b/test/test.vcxproj index 7b27b69..3f15c7c 100644 --- a/test/test.vcxproj +++ b/test/test.vcxproj @@ -34,7 +34,7 @@ Application true - v140 + v141 Unicode From b6df220b557decf24f54dec3b72d6f6885f89656 Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 17 Apr 2018 13:09:39 -0400 Subject: [PATCH 061/118] Fixed #48 --- httplib.h | 22 +++++++++++++--------- test/test.cc | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 9 deletions(-) diff --git a/httplib.h b/httplib.h index b822a29..96d82ad 100644 --- a/httplib.h +++ b/httplib.h @@ -230,6 +230,7 @@ private: virtual bool read_and_close_socket(socket_t sock); + bool is_running_; socket_t svr_sock_; std::string base_dir_; Handlers get_handlers_; @@ -1415,6 +1416,7 @@ inline std::string SocketStream::get_remote_addr() { // HTTP server implementation inline Server::Server(HttpVersion http_version) : http_version_(http_version) + , is_running_(false) , svr_sock_(-1) , running_threads_(0) { @@ -1476,19 +1478,17 @@ inline bool Server::listen(const char* host, int port, int socket_flags) inline bool Server::is_running() const { - return svr_sock_ != -1; + return is_running_; } inline void Server::stop() { - detail::shutdown_socket(svr_sock_); - detail::close_socket(svr_sock_); - svr_sock_ = -1; -} - -inline bool Server::is_handling_requests() const -{ - return running_threads_ > 0; + if (is_running_) { + assert(svr_sock_ != -1); + detail::shutdown_socket(svr_sock_); + detail::close_socket(svr_sock_); + svr_sock_ = -1; + } } inline bool Server::parse_request_line(const char* s, Request& req) @@ -1622,6 +1622,8 @@ inline bool Server::listen_internal() { auto ret = true; + is_running_ = true; + for (;;) { auto val = detail::select_read(svr_sock_, 0, 100000); @@ -1670,6 +1672,8 @@ inline bool Server::listen_internal() } } + is_running_ = false; + return ret; } diff --git a/test/test.cc b/test/test.cc index bae6901..19151e9 100644 --- a/test/test.cc +++ b/test/test.cc @@ -835,6 +835,40 @@ TEST_F(ServerTestWithAI_PASSIVE, GetMethod200) EXPECT_EQ("Hello World!", res->body); } +class ServerUpDownTest : public ::testing::Test { +protected: + ServerUpDownTest() + : cli_(HOST, PORT) + {} + + virtual void SetUp() { + t_ = thread([&](){ + svr_.bind_to_any_port(HOST); + msleep(500); + svr_.listen_after_bind(); + }); + + while (!svr_.is_running()) { + msleep(1); + } + } + + virtual void TearDown() { + svr_.stop(); + t_.join(); + } + + Client cli_; + Server svr_; + thread t_; +}; + +TEST_F(ServerUpDownTest, QuickStartStop) +{ + // Should not crash, especially when run with + // --gtest_filter=ServerUpDownTest.QuickStartStop --gtest_repeat=1000 +} + #ifdef CPPHTTPLIB_OPENSSL_SUPPORT TEST(SSLClientTest, ServerNameIndication) { From 956faae6f07503b017b86d6989306d76b7b97f40 Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 17 Apr 2018 23:47:24 -0400 Subject: [PATCH 062/118] Changed output type of read_content_??? functions to be std::string& --- httplib.h | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/httplib.h b/httplib.h index 96d82ad..b5cc85d 100644 --- a/httplib.h +++ b/httplib.h @@ -767,13 +767,12 @@ inline bool read_headers(Stream& strm, Headers& headers) return true; } -template -bool read_content_with_length(Stream& strm, T& x, size_t len, Progress progress) +bool read_content_with_length(Stream& strm, std::string& out, size_t len, Progress progress) { - x.body.assign(len, 0); + out.assign(len, 0); size_t r = 0; while (r < len){ - auto n = strm.read(&x.body[r], len - r); + auto n = strm.read(&out[r], len - r); if (n <= 0) { return false; } @@ -788,8 +787,7 @@ bool read_content_with_length(Stream& strm, T& x, size_t len, Progress progress) return true; } -template -bool read_content_without_length(Stream& strm, T& x) +bool read_content_without_length(Stream& strm, std::string& out) { for (;;) { char byte; @@ -799,14 +797,13 @@ bool read_content_without_length(Stream& strm, T& x) } else if (n == 0) { return true; } - x.body += byte; + out += byte; } return true; } -template -bool read_content_chunked(Stream& strm, T& x) +bool read_content_chunked(Stream& strm, std::string& out) { const auto bufsiz = 16; char buf[bufsiz]; @@ -835,7 +832,7 @@ bool read_content_chunked(Stream& strm, T& x) break; } - x.body += chunk; + out += chunk; if (!reader.getline()) { return false; @@ -859,14 +856,14 @@ bool read_content(Stream& strm, T& x, Progress progress = Progress()) auto len = get_header_value_int(x.headers, "Content-Length", 0); if (len) { - return read_content_with_length(strm, x, len, progress); + return read_content_with_length(strm, x.body, len, progress); } else { const auto& encoding = get_header_value(x.headers, "Transfer-Encoding", ""); if (!strcasecmp(encoding, "chunked")) { - return read_content_chunked(strm, x); + return read_content_chunked(strm, x.body); } else { - return read_content_without_length(strm, x); + return read_content_without_length(strm, x.body); } } From 889041f05f0c4351f811f2ab0c0c3d92116316d7 Mon Sep 17 00:00:00 2001 From: Scott Graham Date: Tue, 17 Apr 2018 22:02:24 -0700 Subject: [PATCH 063/118] Don't fail chunked read if buffer not yet filled --- httplib.h | 6 ++---- test/test.cc | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/httplib.h b/httplib.h index b5cc85d..c7a5efc 100644 --- a/httplib.h +++ b/httplib.h @@ -817,10 +817,8 @@ bool read_content_chunked(Stream& strm, std::string& out) auto chunk_len = std::stoi(reader.ptr(), 0, 16); while (chunk_len > 0){ - std::string chunk(chunk_len, 0); - - auto n = strm.read(&chunk[0], chunk_len); - if (n <= 0) { + std::string chunk; + if (!read_content_with_length(strm, chunk, chunk_len, nullptr)) { return false; } diff --git a/test/test.cc b/test/test.cc index 19151e9..88a0202 100644 --- a/test/test.cc +++ b/test/test.cc @@ -295,6 +295,10 @@ protected: .post("/chunked", [&](const Request& req, Response& /*res*/) { EXPECT_EQ(req.body, "dechunked post body"); }) + .post("/largechunked", [&](const Request& req, Response& /*res*/) { + std::string expected(6 * 30 * 1024u, 'a'); + EXPECT_EQ(req.body, expected); + }) .post("/multipart", [&](const Request& req, Response& /*res*/) { EXPECT_EQ(5u, req.files.size()); ASSERT_TRUE(!req.has_file("???")); @@ -690,6 +694,37 @@ TEST_F(ServerTest, CaseInsensitiveTransferEncoding) EXPECT_EQ(200, res->status); } +TEST_F(ServerTest, LargeChunkedPost) { + Request req; + req.method = "POST"; + req.path = "/largechunked"; + + std::string host_and_port; + host_and_port += HOST; + host_and_port += ":"; + host_and_port += std::to_string(PORT); + + req.headers.emplace("Host", host_and_port.c_str()); + req.headers.emplace("Accept", "*/*"); + req.headers.emplace("User-Agent", "cpp-httplib/0.1"); + req.headers.emplace("Content-Type", "text/plain"); + req.headers.emplace("Content-Length", "0"); + req.headers.emplace("Transfer-Encoding", "chunked"); + + std::string long_string(30 * 1024u, 'a'); + std::string chunk = "7800\r\n" + long_string + "\r\n"; + + // Attempt to make a large enough post to exceed OS buffers, to test that + // the server handles short reads if the full chunk data isn't available. + req.body = chunk + chunk + chunk + chunk + chunk + chunk + "0\r\n\r\n"; + + auto res = std::make_shared(); + auto ret = cli_.send(req, *res); + + ASSERT_TRUE(ret); + EXPECT_EQ(200, res->status); +} + TEST_F(ServerTest, GetMethodRemoteAddr) { auto res = cli_.get("/remote_addr"); From e2e33a7f0bf1011335de238671ed5fb46b317d82 Mon Sep 17 00:00:00 2001 From: yhirose Date: Wed, 18 Apr 2018 08:35:12 -0400 Subject: [PATCH 064/118] Updated .gitignore --- .gitignore | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.gitignore b/.gitignore index 99d000d..08f6383 100644 --- a/.gitignore +++ b/.gitignore @@ -4,9 +4,12 @@ example/server example/client example/hello example/simplesvr +example/benchmark +example/*.pem test/test test/test.xcodeproj/xcuser* test/test.xcodeproj/*/xcuser* +test/*.pem *.swp @@ -16,5 +19,7 @@ Release *.sdf *.suo *.opensdf +*.db ipch *.dSYM +.* From 5574d82eb31c7024beac1a6c5caf3df441afe1ed Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 20 Apr 2018 00:17:51 -0400 Subject: [PATCH 065/118] Made a temporary fix for OpenSSL thread problem --- httplib.h | 22 ++++++++++++++++++---- test/test.cc | 14 +++++++++----- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/httplib.h b/httplib.h index c7a5efc..b0cb0ab 100644 --- a/httplib.h +++ b/httplib.h @@ -2029,15 +2029,23 @@ inline std::shared_ptr Client::post(const char* path, const Headers& h #ifdef CPPHTTPLIB_OPENSSL_SUPPORT namespace detail { +// TODO: OpenSSL 1.0.2 occasionally crashes... The upcoming 1.1.0 is going to be thread safe. +static std::mutex ssl_ctx_mutex_; + template inline bool read_and_close_socket_ssl( socket_t sock, bool keep_alive, SSL_CTX* ctx, U SSL_connect_or_accept, V setup, T callback) { - auto ssl = SSL_new(ctx); - if (!ssl) { - return false; + SSL* ssl = nullptr; + { + std::lock_guard guard(ssl_ctx_mutex_); + + ssl = SSL_new(ctx); + if (!ssl) { + return false; + } } auto bio = BIO_new_socket(sock, BIO_NOCLOSE); @@ -2069,8 +2077,14 @@ inline bool read_and_close_socket_ssl( } SSL_shutdown(ssl); - SSL_free(ssl); + + { + std::lock_guard guard(ssl_ctx_mutex_); + SSL_free(ssl); + } + close_socket(sock); + return ret; } diff --git a/test/test.cc b/test/test.cc index 88a0202..009b6e0 100644 --- a/test/test.cc +++ b/test/test.cc @@ -263,7 +263,7 @@ protected: res.set_content("Hello World!", "text/plain"); }) .get("/slow", [&](const Request& /*req*/, Response& res) { - msleep(3000); + msleep(2000); res.set_content("slow", "text/plain"); }) .get("/remote_addr", [&](const Request& req, Response& res) { @@ -368,6 +368,9 @@ protected: virtual void TearDown() { svr_.stop(); + for (auto& t: request_threads_) { + t.join(); + } t_.join(); } @@ -380,6 +383,7 @@ protected: Server svr_; #endif thread t_; + std::vector request_threads_; }; TEST_F(ServerTest, GetMethod200) @@ -736,10 +740,10 @@ TEST_F(ServerTest, GetMethodRemoteAddr) TEST_F(ServerTest, SlowRequest) { - std::thread([=]() { auto res = cli_.get("/slow"); }).detach(); - std::thread([=]() { auto res = cli_.get("/slow"); }).detach(); - std::thread([=]() { auto res = cli_.get("/slow"); }).detach(); - msleep(1000); + request_threads_.push_back(std::thread([=]() { auto res = cli_.get("/slow"); })); + request_threads_.push_back(std::thread([=]() { auto res = cli_.get("/slow"); })); + request_threads_.push_back(std::thread([=]() { auto res = cli_.get("/slow"); })); + msleep(100); } #ifdef CPPHTTPLIB_ZLIB_SUPPORT From 755f05c02b2c986f7ee014c203e4d8fdbe8fc9d2 Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 20 Apr 2018 09:37:59 -0400 Subject: [PATCH 066/118] Removed global mutex --- httplib.h | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/httplib.h b/httplib.h index b0cb0ab..5c3c241 100644 --- a/httplib.h +++ b/httplib.h @@ -316,6 +316,7 @@ private: virtual bool read_and_close_socket(socket_t sock); SSL_CTX* ctx_; + std::mutex ctx_mutex_; }; class SSLClient : public Client { @@ -334,6 +335,7 @@ private: virtual bool read_and_close_socket(socket_t sock, Request& req, Response& res); SSL_CTX* ctx_; + std::mutex ctx_mutex_; }; #endif @@ -2029,18 +2031,17 @@ inline std::shared_ptr Client::post(const char* path, const Headers& h #ifdef CPPHTTPLIB_OPENSSL_SUPPORT namespace detail { -// TODO: OpenSSL 1.0.2 occasionally crashes... The upcoming 1.1.0 is going to be thread safe. -static std::mutex ssl_ctx_mutex_; - template inline bool read_and_close_socket_ssl( socket_t sock, bool keep_alive, - SSL_CTX* ctx, U SSL_connect_or_accept, V setup, + // TODO: OpenSSL 1.0.2 occasionally crashes... The upcoming 1.1.0 is going to be thread safe. + SSL_CTX* ctx, std::mutex& ctx_mutex, + U SSL_connect_or_accept, V setup, T callback) { SSL* ssl = nullptr; { - std::lock_guard guard(ssl_ctx_mutex_); + std::lock_guard guard(ctx_mutex); ssl = SSL_new(ctx); if (!ssl) { @@ -2079,7 +2080,7 @@ inline bool read_and_close_socket_ssl( SSL_shutdown(ssl); { - std::lock_guard guard(ssl_ctx_mutex_); + std::lock_guard guard(ctx_mutex); SSL_free(ssl); } @@ -2172,7 +2173,7 @@ inline bool SSLServer::read_and_close_socket(socket_t sock) return detail::read_and_close_socket_ssl( sock, keep_alive, - ctx_, + ctx_, ctx_mutex_, SSL_accept, [](SSL* /*ssl*/) {}, [this](Stream& strm, bool last_connection) { @@ -2204,7 +2205,8 @@ inline bool SSLClient::read_and_close_socket(socket_t sock, Request& req, Respon { return is_valid() && detail::read_and_close_socket_ssl( sock, false, - ctx_, SSL_connect, + ctx_, ctx_mutex_, + SSL_connect, [&](SSL* ssl) { SSL_set_tlsext_host_name(ssl, host_.c_str()); }, From 6aa3fd6b59712b0a16c2c2e31d2af7dfbcea941d Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 20 Apr 2018 09:33:08 -0400 Subject: [PATCH 067/118] Fix #38 --- httplib.h | 54 ++++++++++++++++++++++++++-------------------------- test/test.cc | 2 ++ 2 files changed, 29 insertions(+), 27 deletions(-) diff --git a/httplib.h b/httplib.h index 5c3c241..546b10f 100644 --- a/httplib.h +++ b/httplib.h @@ -1165,18 +1165,8 @@ inline bool can_compress(const std::string& content_type) { content_type == "application/xhtml+xml"; } -inline void compress(const Request& req, Response& res) +inline void compress(std::string& content) { - // TODO: Server version is HTTP/1.1 and 'Accpet-Encoding' has gzip, not gzip;q=0 - const auto& encodings = req.get_header_value("Accept-Encoding"); - if (encodings.find("gzip") == std::string::npos) { - return; - } - - if (!can_compress(res.get_header_value("Content-Type"))) { - return; - } - z_stream strm; strm.zalloc = Z_NULL; strm.zfree = Z_NULL; @@ -1187,8 +1177,8 @@ inline void compress(const Request& req, Response& res) return; } - strm.avail_in = res.body.size(); - strm.next_in = (Bytef *)res.body.data(); + strm.avail_in = content.size(); + strm.next_in = (Bytef *)content.data(); std::string compressed; @@ -1201,17 +1191,13 @@ inline void compress(const Request& req, Response& res) compressed.append(buff, bufsiz - strm.avail_out); } while (strm.avail_out == 0); - res.set_header("Content-Encoding", "gzip"); - res.body.swap(compressed); + content.swap(compressed); deflateEnd(&strm); } -inline void decompress_request_body(Request& req) +inline void decompress(std::string& content) { - if (req.get_header_value("Content-Encoding") != "gzip") - return; - z_stream strm; strm.zalloc = Z_NULL; strm.zfree = Z_NULL; @@ -1225,8 +1211,8 @@ inline void decompress_request_body(Request& req) return; } - strm.avail_in = req.body.size(); - strm.next_in = (Bytef *)req.body.data(); + strm.avail_in = content.size(); + strm.next_in = (Bytef *)content.data(); std::string decompressed; @@ -1239,7 +1225,7 @@ inline void decompress_request_body(Request& req) decompressed.append(buff, bufsiz - strm.avail_out); } while (strm.avail_out == 0); - req.body.swap(decompressed); + content.swap(decompressed); inflateEnd(&strm); } @@ -1531,7 +1517,13 @@ inline void Server::write_response(Stream& strm, bool last_connection, const Req if (!res.body.empty()) { #ifdef CPPHTTPLIB_ZLIB_SUPPORT - detail::compress(req, res); + // TODO: Server version is HTTP/1.1 and 'Accpet-Encoding' has gzip, not gzip;q=0 + const auto& encodings = req.get_header_value("Accept-Encoding"); + if (encodings.find("gzip") != std::string::npos && + detail::can_compress(res.get_header_value("Content-Type"))) { + detail::compress(res.body); + res.set_header("Content-Encoding", "gzip"); + } #endif if (!res.has_header("Content-Type")) { @@ -1743,15 +1735,15 @@ inline bool Server::process_request(Stream& strm, bool last_connection) const auto& content_type = req.get_header_value("Content-Type"); -#ifdef CPPHTTPLIB_ZLIB_SUPPORT - detail::decompress_request_body(req); -#else if (req.get_header_value("Content-Encoding") == "gzip") { +#ifdef CPPHTTPLIB_ZLIB_SUPPORT + detail::decompress(req.body); +#else res.status = 415; write_response(strm, last_connection, req, res); return ret; - } #endif + } if (!content_type.find("application/x-www-form-urlencoded")) { detail::parse_query_text(req.body, req.params); @@ -1936,6 +1928,14 @@ inline bool Client::process_request(Stream& strm, Request& req, Response& res) if (!detail::read_content(strm, res, req.progress)) { return false; } + + if (res.get_header_value("Content-Encoding") == "gzip") { +#ifdef CPPHTTPLIB_ZLIB_SUPPORT + detail::decompress(res.body); +#else + return false; +#endif + } } return true; diff --git a/test/test.cc b/test/test.cc index 009b6e0..1d0a2af 100644 --- a/test/test.cc +++ b/test/test.cc @@ -757,6 +757,7 @@ TEST_F(ServerTest, Gzip) EXPECT_EQ("gzip", res->get_header_value("Content-Encoding")); EXPECT_EQ("text/plain", res->get_header_value("Content-Type")); EXPECT_EQ("33", res->get_header_value("Content-Length")); + EXPECT_EQ("1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890", res->body); EXPECT_EQ(200, res->status); } @@ -770,6 +771,7 @@ TEST_F(ServerTest, NoGzip) EXPECT_EQ(false, res->has_header("Content-Encoding")); EXPECT_EQ("application/octet-stream", res->get_header_value("Content-Type")); EXPECT_EQ("100", res->get_header_value("Content-Length")); + EXPECT_EQ("1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890", res->body); EXPECT_EQ(200, res->status); } From ef5c4144d787a0c03d296e29f17f5f9deba7ba9b Mon Sep 17 00:00:00 2001 From: adikabintang Date: Thu, 26 Apr 2018 12:07:44 +0700 Subject: [PATCH 068/118] add keyword inline to some function definitions to avoid linking error --- httplib.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/httplib.h b/httplib.h index 546b10f..c3a0021 100644 --- a/httplib.h +++ b/httplib.h @@ -769,7 +769,7 @@ inline bool read_headers(Stream& strm, Headers& headers) return true; } -bool read_content_with_length(Stream& strm, std::string& out, size_t len, Progress progress) +inline bool read_content_with_length(Stream& strm, std::string& out, size_t len, Progress progress) { out.assign(len, 0); size_t r = 0; @@ -789,7 +789,7 @@ bool read_content_with_length(Stream& strm, std::string& out, size_t len, Progre return true; } -bool read_content_without_length(Stream& strm, std::string& out) +inline bool read_content_without_length(Stream& strm, std::string& out) { for (;;) { char byte; @@ -805,7 +805,7 @@ bool read_content_without_length(Stream& strm, std::string& out) return true; } -bool read_content_chunked(Stream& strm, std::string& out) +inline bool read_content_chunked(Stream& strm, std::string& out) { const auto bufsiz = 16; char buf[bufsiz]; From 3eff00bbc82302f626ebc7fafa567f1c2e34c5d8 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 29 Apr 2018 16:14:47 -0400 Subject: [PATCH 069/118] Fix #60 --- httplib.h | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/httplib.h b/httplib.h index c3a0021..e7366ae 100644 --- a/httplib.h +++ b/httplib.h @@ -592,20 +592,18 @@ inline bool is_connection_error() inline std::string get_remote_addr(socket_t sock) { struct sockaddr_storage addr; - char ipstr[INET6_ADDRSTRLEN]; socklen_t len = sizeof(addr); - getpeername(sock, (struct sockaddr*)&addr, &len); - // deal with both IPv4 and IPv6: - if (addr.ss_family == AF_INET) { - auto s = (struct sockaddr_in *)&addr; - inet_ntop(AF_INET, &s->sin_addr, ipstr, sizeof(ipstr)); - } else { // AF_INET6 - auto s = (struct sockaddr_in6 *)&addr; - inet_ntop(AF_INET6, &s->sin6_addr, ipstr, sizeof(ipstr)); + if (!getpeername(sock, (struct sockaddr*)&addr, &len)) { + char ipstr[NI_MAXHOST]; + + if (!getnameinfo((struct sockaddr*)&addr, len, + ipstr, sizeof(ipstr), nullptr, 0, NI_NUMERICHOST)) { + return ipstr; + } } - return ipstr; + return std::string(); } inline bool is_file(const std::string& path) From 632df52b4fa6032a695a9129a541e8416a6f424b Mon Sep 17 00:00:00 2001 From: Enzo AGUADO Date: Mon, 7 May 2018 02:15:12 +0800 Subject: [PATCH 070/118] add sys/select.h for musl libc --- httplib.h | 1 + 1 file changed, 1 insertion(+) diff --git a/httplib.h b/httplib.h index e7366ae..3eb48b9 100644 --- a/httplib.h +++ b/httplib.h @@ -61,6 +61,7 @@ typedef int socket_t; #include #include #include +#include #include #include From 911e620a5436638f6ffaa2d40d66404cf2a88a42 Mon Sep 17 00:00:00 2001 From: Enzo AGUADO Date: Mon, 7 May 2018 16:59:08 +0800 Subject: [PATCH 071/118] don't include sys/select on windows --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 3eb48b9..ae3df2e 100644 --- a/httplib.h +++ b/httplib.h @@ -48,6 +48,7 @@ typedef SOCKET socket_t; #include #include #include +#include typedef int socket_t; #endif @@ -61,7 +62,6 @@ typedef int socket_t; #include #include #include -#include #include #include From 6c5d0b2a18de67ae4dc821fd23e104022506a780 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 6 May 2018 21:16:35 -0400 Subject: [PATCH 072/118] Fix #57 and #62 --- README.md | 45 +++++++++---- example/benchmark.cc | 2 +- example/client.cc | 2 +- example/hello.cc | 2 +- example/server.cc | 10 +-- example/simplesvr.cc | 2 +- httplib.h | 150 +++++++++++++++++++++++++++++++++++-------- test/test.cc | 145 +++++++++++++++++++++++++---------------- 8 files changed, 255 insertions(+), 103 deletions(-) diff --git a/README.md b/README.md index 291e4bd..0c4fb2d 100644 --- a/README.md +++ b/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¬e=coder", "application/x-www-form-urlencoded"); +res = cli.Post("/post", "text", "text/plain"); +res = cli.Post("/person", "name=john1¬e=coder", "application/x-www-form-urlencoded"); ``` ### POST with parameters @@ -115,7 +117,26 @@ res = cli.post("/person", "name=john1¬e=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 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) diff --git a/example/benchmark.cc b/example/benchmark.cc index 9d09df1..d4092f5 100644 --- a/example/benchmark.cc +++ b/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); } diff --git a/example/client.cc b/example/client.cc index 3bd1641..d3ad4ba 100644 --- a/example/client.cc +++ b/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; diff --git a/example/hello.cc b/example/hello.cc index 3b0c7f6..f315511 100644 --- a/example/hello.cc +++ b/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"); }); diff --git a/example/server.cc b/example/server.cc index b8eb510..bd24b35 100644 --- a/example/server.cc +++ b/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(); }); diff --git a/example/simplesvr.cc b/example/simplesvr.cc index 8a590a2..daeb90a 100644 --- a/example/simplesvr.cc +++ b/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); diff --git a/httplib.h b/httplib.h index ae3df2e..a5eeaa2 100644 --- a/httplib.h +++ b/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 get(const char* path, Progress progress = nullptr); - std::shared_ptr get(const char* path, const Headers& headers, Progress progress = nullptr); + std::shared_ptr Get(const char* path, Progress progress = nullptr); + std::shared_ptr Get(const char* path, const Headers& headers, Progress progress = nullptr); - std::shared_ptr head(const char* path); - std::shared_ptr head(const char* path, const Headers& headers); + std::shared_ptr Head(const char* path); + std::shared_ptr Head(const char* path, const Headers& headers); - std::shared_ptr post(const char* path, const std::string& body, const char* content_type); - std::shared_ptr post(const char* path, const Headers& headers, const std::string& body, const char* content_type); + std::shared_ptr Post(const char* path, const std::string& body, const char* content_type); + std::shared_ptr Post(const char* path, const Headers& headers, const std::string& body, const char* content_type); - std::shared_ptr post(const char* path, const Params& params); - std::shared_ptr post(const char* path, const Headers& headers, const Params& params); + std::shared_ptr Post(const char* path, const Params& params); + std::shared_ptr Post(const char* path, const Headers& headers, const Params& params); + + std::shared_ptr Put(const char* path, const std::string& body, const char* content_type); + std::shared_ptr Put(const char* path, const Headers& headers, const std::string& body, const char* content_type); + + std::shared_ptr Delete(const char* path); + std::shared_ptr Delete(const char* path, const Headers& headers); + + std::shared_ptr Options(const char* path); + std::shared_ptr 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 Client::get(const char* path, Progress progress) +inline std::shared_ptr Client::Get(const char* path, Progress progress) { - return get(path, Headers(), progress); + return Get(path, Headers(), progress); } -inline std::shared_ptr Client::get(const char* path, const Headers& headers, Progress progress) +inline std::shared_ptr Client::Get(const char* path, const Headers& headers, Progress progress) { Request req; req.method = "GET"; @@ -1965,12 +2005,12 @@ inline std::shared_ptr Client::get(const char* path, const Headers& he return send(req, *res) ? res : nullptr; } -inline std::shared_ptr Client::head(const char* path) +inline std::shared_ptr Client::Head(const char* path) { - return head(path, Headers()); + return Head(path, Headers()); } -inline std::shared_ptr Client::head(const char* path, const Headers& headers) +inline std::shared_ptr Client::Head(const char* path, const Headers& headers) { Request req; req.method = "HEAD"; @@ -1982,13 +2022,13 @@ inline std::shared_ptr Client::head(const char* path, const Headers& h return send(req, *res) ? res : nullptr; } -inline std::shared_ptr Client::post( +inline std::shared_ptr 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 Client::post( +inline std::shared_ptr 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 Client::post( return send(req, *res) ? res : nullptr; } -inline std::shared_ptr Client::post(const char* path, const Params& params) +inline std::shared_ptr Client::Post(const char* path, const Params& params) { - return post(path, Headers(), params); + return Post(path, Headers(), params); } -inline std::shared_ptr Client::post(const char* path, const Headers& headers, const Params& params) +inline std::shared_ptr 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 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 Client::Put( + const char* path, const std::string& body, const char* content_type) +{ + return Put(path, Headers(), body, content_type); +} + +inline std::shared_ptr 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(); + + return send(req, *res) ? res : nullptr; +} + +inline std::shared_ptr Client::Delete(const char* path) +{ + return Delete(path, Headers()); +} + +inline std::shared_ptr Client::Delete(const char* path, const Headers& headers) +{ + Request req; + req.method = "DELETE"; + req.path = path; + req.headers = headers; + + auto res = std::make_shared(); + + return send(req, *res) ? res : nullptr; +} + +inline std::shared_ptr Client::Options(const char* path) +{ + return Options(path, Headers()); +} + +inline std::shared_ptr Client::Options(const char* path, const Headers& headers) +{ + Request req; + req.method = "OPTIONS"; + req.path = path; + req.headers = headers; + + auto res = std::make_shared(); + + return send(req, *res) ? res : nullptr; } /* diff --git a/test/test.cc b/test/test.cc index 1d0a2af..2330683 100644 --- a/test/test.cc +++ b/test/test.cc @@ -131,7 +131,7 @@ void testChunkedEncoding(httplib::HttpVersion ver) httplib::Client cli(host, port, sec, ver); #endif - auto res = cli.get("/httpgallery/chunked/chunkedimage.aspx?0.4153841143030137"); + auto res = cli.Get("/httpgallery/chunked/chunkedimage.aspx?0.4153841143030137"); ASSERT_TRUE(res != nullptr); std::string out; @@ -163,7 +163,7 @@ TEST(RangeTest, FromHTTPBin) { httplib::Headers headers; - auto res = cli.get("/range/32", headers); + auto res = cli.Get("/range/32", headers); ASSERT_TRUE(res != nullptr); EXPECT_EQ(res->body, "abcdefghijklmnopqrstuvwxyzabcdef"); EXPECT_EQ(200, res->status); @@ -171,7 +171,7 @@ TEST(RangeTest, FromHTTPBin) { httplib::Headers headers = { httplib::make_range_header(1) }; - auto res = cli.get("/range/32", headers); + auto res = cli.Get("/range/32", headers); ASSERT_TRUE(res != nullptr); EXPECT_EQ(res->body, "bcdefghijklmnopqrstuvwxyzabcdef"); EXPECT_EQ(206, res->status); @@ -179,7 +179,7 @@ TEST(RangeTest, FromHTTPBin) { httplib::Headers headers = { httplib::make_range_header(1, 10) }; - auto res = cli.get("/range/32", headers); + auto res = cli.Get("/range/32", headers); ASSERT_TRUE(res != nullptr); EXPECT_EQ(res->body, "bcdefghijk"); EXPECT_EQ(206, res->status); @@ -200,7 +200,7 @@ TEST(ConnectionErrorTest, InvalidHost) httplib::Client cli(host, port, sec, ver); #endif - auto res = cli.get("/"); + auto res = cli.Get("/"); ASSERT_TRUE(res == nullptr); } @@ -218,7 +218,7 @@ TEST(ConnectionErrorTest, InvalidPort) httplib::Client cli(host, port, sec, ver); #endif - auto res = cli.get("/"); + auto res = cli.Get("/"); ASSERT_TRUE(res == nullptr); } @@ -236,7 +236,7 @@ TEST(ConnectionErrorTest, Timeout) httplib::Client cli(host, port, sec, ver); #endif - auto res = cli.get("/"); + auto res = cli.Get("/"); ASSERT_TRUE(res == nullptr); } @@ -259,31 +259,31 @@ protected: virtual void SetUp() { svr_.set_base_dir("./www"); - svr_.get("/hi", [&](const Request& /*req*/, Response& res) { + svr_.Get("/hi", [&](const Request& /*req*/, Response& res) { res.set_content("Hello World!", "text/plain"); }) - .get("/slow", [&](const Request& /*req*/, Response& res) { + .Get("/slow", [&](const Request& /*req*/, Response& res) { msleep(2000); res.set_content("slow", "text/plain"); }) - .get("/remote_addr", [&](const Request& req, Response& res) { + .Get("/remote_addr", [&](const Request& req, Response& res) { auto remote_addr = req.headers.find("REMOTE_ADDR")->second; res.set_content(remote_addr.c_str(), "text/plain"); }) - .get("/endwith%", [&](const Request& /*req*/, Response& res) { + .Get("/endwith%", [&](const Request& /*req*/, Response& res) { res.set_content("Hello World!", "text/plain"); }) - .get("/", [&](const Request& /*req*/, Response& res) { + .Get("/", [&](const Request& /*req*/, Response& res) { res.set_redirect("/hi"); }) - .post("/person", [&](const Request& req, Response& res) { + .Post("/person", [&](const Request& req, Response& res) { if (req.has_param("name") && req.has_param("note")) { persons_[req.get_param_value("name")] = req.get_param_value("note"); } else { res.status = 400; } }) - .get("/person/(.*)", [&](const Request& req, Response& res) { + .Get("/person/(.*)", [&](const Request& req, Response& res) { string name = req.matches[1]; if (persons_.find(name) != persons_.end()) { auto note = persons_[name]; @@ -292,14 +292,14 @@ protected: res.status = 404; } }) - .post("/chunked", [&](const Request& req, Response& /*res*/) { + .Post("/chunked", [&](const Request& req, Response& /*res*/) { EXPECT_EQ(req.body, "dechunked post body"); }) - .post("/largechunked", [&](const Request& req, Response& /*res*/) { + .Post("/largechunked", [&](const Request& req, Response& /*res*/) { std::string expected(6 * 30 * 1024u, 'a'); EXPECT_EQ(req.body, expected); }) - .post("/multipart", [&](const Request& req, Response& /*res*/) { + .Post("/multipart", [&](const Request& req, Response& /*res*/) { EXPECT_EQ(5u, req.files.size()); ASSERT_TRUE(!req.has_file("???")); @@ -329,14 +329,24 @@ protected: EXPECT_EQ(0u, file.length); } }) + .Put("/put", [&](const Request& req, Response& res) { + EXPECT_EQ(req.body, "PUT"); + res.set_content(req.body, "text/plain"); + }) + .Delete("/delete", [&](const Request& /*req*/, Response& res) { + res.set_content("DELETE", "text/plain"); + }) + .Options(R"(\*)", [&](const Request& /*req*/, Response& res) { + res.set_header("Allow", "GET, POST, HEAD, OPTIONS"); + }) #ifdef CPPHTTPLIB_ZLIB_SUPPORT - .get("/gzip", [&](const Request& /*req*/, Response& res) { + .Get("/gzip", [&](const Request& /*req*/, Response& res) { res.set_content("1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890", "text/plain"); }) - .get("/nogzip", [&](const Request& /*req*/, Response& res) { + .Get("/nogzip", [&](const Request& /*req*/, Response& res) { res.set_content("1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890", "application/octet-stream"); }) - .post("/gzipmultipart", [&](const Request& req, Response& /*res*/) { + .Post("/gzipmultipart", [&](const Request& req, Response& /*res*/) { EXPECT_EQ(2u, req.files.size()); ASSERT_TRUE(!req.has_file("???")); @@ -388,7 +398,7 @@ protected: TEST_F(ServerTest, GetMethod200) { - auto res = cli_.get("/hi"); + auto res = cli_.Get("/hi"); ASSERT_TRUE(res != nullptr); EXPECT_EQ(200, res->status); EXPECT_EQ("text/plain", res->get_header_value("Content-Type")); @@ -397,7 +407,7 @@ TEST_F(ServerTest, GetMethod200) TEST_F(ServerTest, GetMethod302) { - auto res = cli_.get("/"); + auto res = cli_.Get("/"); ASSERT_TRUE(res != nullptr); EXPECT_EQ(302, res->status); EXPECT_EQ("/hi", res->get_header_value("Location")); @@ -405,14 +415,14 @@ TEST_F(ServerTest, GetMethod302) TEST_F(ServerTest, GetMethod404) { - auto res = cli_.get("/invalid"); + auto res = cli_.Get("/invalid"); ASSERT_TRUE(res != nullptr); EXPECT_EQ(404, res->status); } TEST_F(ServerTest, HeadMethod200) { - auto res = cli_.head("/hi"); + auto res = cli_.Head("/hi"); ASSERT_TRUE(res != nullptr); EXPECT_EQ(200, res->status); EXPECT_EQ("text/plain", res->get_header_value("Content-Type")); @@ -421,7 +431,7 @@ TEST_F(ServerTest, HeadMethod200) TEST_F(ServerTest, HeadMethod404) { - auto res = cli_.head("/invalid"); + auto res = cli_.Head("/invalid"); ASSERT_TRUE(res != nullptr); EXPECT_EQ(404, res->status); EXPECT_EQ("", res->body); @@ -429,7 +439,7 @@ TEST_F(ServerTest, HeadMethod404) TEST_F(ServerTest, GetMethodPersonJohn) { - auto res = cli_.get("/person/john"); + auto res = cli_.Get("/person/john"); ASSERT_TRUE(res != nullptr); EXPECT_EQ(200, res->status); EXPECT_EQ("text/plain", res->get_header_value("Content-Type")); @@ -438,15 +448,15 @@ TEST_F(ServerTest, GetMethodPersonJohn) TEST_F(ServerTest, PostMethod1) { - auto res = cli_.get("/person/john1"); + auto res = cli_.Get("/person/john1"); ASSERT_TRUE(res != nullptr); ASSERT_EQ(404, res->status); - res = cli_.post("/person", "name=john1¬e=coder", "application/x-www-form-urlencoded"); + res = cli_.Post("/person", "name=john1¬e=coder", "application/x-www-form-urlencoded"); ASSERT_TRUE(res != nullptr); ASSERT_EQ(200, res->status); - res = cli_.get("/person/john1"); + res = cli_.Get("/person/john1"); ASSERT_TRUE(res != nullptr); ASSERT_EQ(200, res->status); ASSERT_EQ("text/plain", res->get_header_value("Content-Type")); @@ -455,7 +465,7 @@ TEST_F(ServerTest, PostMethod1) TEST_F(ServerTest, PostMethod2) { - auto res = cli_.get("/person/john2"); + auto res = cli_.Get("/person/john2"); ASSERT_TRUE(res != nullptr); ASSERT_EQ(404, res->status); @@ -463,11 +473,11 @@ TEST_F(ServerTest, PostMethod2) params.emplace("name", "john2"); params.emplace("note", "coder"); - res = cli_.post("/person", params); + res = cli_.Post("/person", params); ASSERT_TRUE(res != nullptr); ASSERT_EQ(200, res->status); - res = cli_.get("/person/john2"); + res = cli_.Get("/person/john2"); ASSERT_TRUE(res != nullptr); ASSERT_EQ(200, res->status); ASSERT_EQ("text/plain", res->get_header_value("Content-Type")); @@ -476,7 +486,7 @@ TEST_F(ServerTest, PostMethod2) TEST_F(ServerTest, GetMethodDir) { - auto res = cli_.get("/dir/"); + auto res = cli_.Get("/dir/"); ASSERT_TRUE(res != nullptr); EXPECT_EQ(200, res->status); EXPECT_EQ("text/html", res->get_header_value("Content-Type")); @@ -495,7 +505,7 @@ TEST_F(ServerTest, GetMethodDir) TEST_F(ServerTest, GetMethodDirTest) { - auto res = cli_.get("/dir/test.html"); + auto res = cli_.Get("/dir/test.html"); ASSERT_TRUE(res != nullptr); EXPECT_EQ(200, res->status); EXPECT_EQ("text/html", res->get_header_value("Content-Type")); @@ -504,7 +514,7 @@ TEST_F(ServerTest, GetMethodDirTest) TEST_F(ServerTest, GetMethodDirTestWithDoubleDots) { - auto res = cli_.get("/dir/../dir/test.html"); + auto res = cli_.Get("/dir/../dir/test.html"); ASSERT_TRUE(res != nullptr); EXPECT_EQ(200, res->status); EXPECT_EQ("text/html", res->get_header_value("Content-Type")); @@ -513,21 +523,21 @@ TEST_F(ServerTest, GetMethodDirTestWithDoubleDots) TEST_F(ServerTest, GetMethodInvalidPath) { - auto res = cli_.get("/dir/../test.html"); + auto res = cli_.Get("/dir/../test.html"); ASSERT_TRUE(res != nullptr); EXPECT_EQ(404, res->status); } TEST_F(ServerTest, GetMethodOutOfBaseDir) { - auto res = cli_.get("/../www/dir/test.html"); + auto res = cli_.Get("/../www/dir/test.html"); ASSERT_TRUE(res != nullptr); EXPECT_EQ(404, res->status); } TEST_F(ServerTest, GetMethodOutOfBaseDir2) { - auto res = cli_.get("/dir/../../www/dir/test.html"); + auto res = cli_.Get("/dir/../../www/dir/test.html"); ASSERT_TRUE(res != nullptr); EXPECT_EQ(404, res->status); } @@ -540,13 +550,13 @@ TEST_F(ServerTest, InvalidBaseDir) TEST_F(ServerTest, EmptyRequest) { - auto res = cli_.get(""); + auto res = cli_.Get(""); ASSERT_TRUE(res == nullptr); } TEST_F(ServerTest, LongRequest) { - auto res = cli_.get("/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/__ok__"); + auto res = cli_.Get("/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/__ok__"); ASSERT_TRUE(res != nullptr); EXPECT_EQ(404, res->status); @@ -554,7 +564,7 @@ TEST_F(ServerTest, LongRequest) TEST_F(ServerTest, TooLongRequest) { - auto res = cli_.get("/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/__ng___"); + auto res = cli_.Get("/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/__ng___"); ASSERT_TRUE(res != nullptr); EXPECT_EQ(404, res->status); @@ -610,28 +620,28 @@ TEST_F(ServerTest, TooLongHeader) TEST_F(ServerTest, PercentEncoding) { - auto res = cli_.get("/e%6edwith%"); + auto res = cli_.Get("/e%6edwith%"); ASSERT_TRUE(res != nullptr); EXPECT_EQ(200, res->status); } TEST_F(ServerTest, PercentEncodingUnicode) { - auto res = cli_.get("/e%u006edwith%"); + auto res = cli_.Get("/e%u006edwith%"); ASSERT_TRUE(res != nullptr); EXPECT_EQ(200, res->status); } TEST_F(ServerTest, InvalidPercentEncoding) { - auto res = cli_.get("/%endwith%"); + auto res = cli_.Get("/%endwith%"); ASSERT_TRUE(res != nullptr); EXPECT_EQ(404, res->status); } TEST_F(ServerTest, InvalidPercentEncodingUnicode) { - auto res = cli_.get("/%uendwith%"); + auto res = cli_.Get("/%uendwith%"); ASSERT_TRUE(res != nullptr); EXPECT_EQ(404, res->status); } @@ -663,7 +673,7 @@ TEST_F(ServerTest, MultipartFormData) TEST_F(ServerTest, CaseInsensitiveHeaderName) { - auto res = cli_.get("/hi"); + auto res = cli_.Get("/hi"); ASSERT_TRUE(res != nullptr); EXPECT_EQ(200, res->status); EXPECT_EQ("text/plain", res->get_header_value("content-type")); @@ -731,7 +741,7 @@ TEST_F(ServerTest, LargeChunkedPost) { TEST_F(ServerTest, GetMethodRemoteAddr) { - auto res = cli_.get("/remote_addr"); + auto res = cli_.Get("/remote_addr"); ASSERT_TRUE(res != nullptr); EXPECT_EQ(200, res->status); EXPECT_EQ("text/plain", res->get_header_value("Content-Type")); @@ -740,18 +750,43 @@ TEST_F(ServerTest, GetMethodRemoteAddr) TEST_F(ServerTest, SlowRequest) { - request_threads_.push_back(std::thread([=]() { auto res = cli_.get("/slow"); })); - request_threads_.push_back(std::thread([=]() { auto res = cli_.get("/slow"); })); - request_threads_.push_back(std::thread([=]() { auto res = cli_.get("/slow"); })); + request_threads_.push_back(std::thread([=]() { auto res = cli_.Get("/slow"); })); + request_threads_.push_back(std::thread([=]() { auto res = cli_.Get("/slow"); })); + request_threads_.push_back(std::thread([=]() { auto res = cli_.Get("/slow"); })); msleep(100); } +TEST_F(ServerTest, Put) +{ + auto res = cli_.Put("/put", "PUT", "text/plain"); + ASSERT_TRUE(res != nullptr); + EXPECT_EQ(200, res->status); + EXPECT_EQ("PUT", res->body); +} + +TEST_F(ServerTest, Delete) +{ + auto res = cli_.Delete("/delete"); + ASSERT_TRUE(res != nullptr); + EXPECT_EQ(200, res->status); + EXPECT_EQ("DELETE", res->body); +} + +TEST_F(ServerTest, Options) +{ + auto res = cli_.Options("*"); + ASSERT_TRUE(res != nullptr); + EXPECT_EQ(200, res->status); + EXPECT_EQ("GET, POST, HEAD, OPTIONS", res->get_header_value("Allow")); + EXPECT_TRUE(res->body.empty()); +} + #ifdef CPPHTTPLIB_ZLIB_SUPPORT TEST_F(ServerTest, Gzip) { Headers headers; headers.emplace("Accept-Encoding", "gzip, deflate"); - auto res = cli_.get("/gzip", headers); + auto res = cli_.Get("/gzip", headers); ASSERT_TRUE(res != nullptr); EXPECT_EQ("gzip", res->get_header_value("Content-Encoding")); @@ -765,7 +800,7 @@ TEST_F(ServerTest, NoGzip) { Headers headers; headers.emplace("Accept-Encoding", "gzip, deflate"); - auto res = cli_.get("/nogzip", headers); + auto res = cli_.Get("/nogzip", headers); ASSERT_TRUE(res != nullptr); EXPECT_EQ(false, res->has_header("Content-Encoding")); @@ -839,7 +874,7 @@ protected: {} virtual void SetUp() { - svr_.get("/hi", [&](const Request& /*req*/, Response& res) { + svr_.Get("/hi", [&](const Request& /*req*/, Response& res) { res.set_content("Hello World!", "text/plain"); }); @@ -869,7 +904,7 @@ protected: TEST_F(ServerTestWithAI_PASSIVE, GetMethod200) { - auto res = cli_.get("/hi"); + auto res = cli_.Get("/hi"); ASSERT_TRUE(res != nullptr); EXPECT_EQ(200, res->status); EXPECT_EQ("text/plain", res->get_header_value("Content-Type")); @@ -914,7 +949,7 @@ TEST_F(ServerUpDownTest, QuickStartStop) TEST(SSLClientTest, ServerNameIndication) { SSLClient cli("httpbin.org", 443); - auto res = cli.get("/get"); + auto res = cli.Get("/get"); ASSERT_TRUE(res != nullptr); ASSERT_EQ(200, res->status); } From e6abebf98999656844cb1bb1d64c33e6712b87ce Mon Sep 17 00:00:00 2001 From: yhirose Date: Wed, 9 May 2018 07:17:45 -0400 Subject: [PATCH 073/118] Fix #65, #66 --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 0c4fb2d..84685b4 100644 --- a/README.md +++ b/README.md @@ -114,7 +114,7 @@ res = cli.Post("/person", "name=john1¬e=coder", "application/x-www-form-urlen ### POST with parameters ```c++ -httplib::Map params; +httplib::Params params; params["name"] = "john"; params["note"] = "coder"; auto res = cli.Post("/post", params); @@ -123,7 +123,7 @@ auto res = cli.Post("/post", params); ### PUT ```c++ -res = cli.Post("/resource/foo", "text", "text/plain"); +res = cli.Put("/resource/foo", "text", "text/plain"); ``` ### DELETE From 75285e87132ead1a176e8dc38338ea493a7c5ecd Mon Sep 17 00:00:00 2001 From: Joshua Peraza Date: Wed, 9 May 2018 16:28:39 -0700 Subject: [PATCH 074/118] Define and use kInvalidSocket --- httplib.h | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/httplib.h b/httplib.h index a5eeaa2..0070e2d 100644 --- a/httplib.h +++ b/httplib.h @@ -39,6 +39,7 @@ #endif typedef SOCKET socket_t; +constexpr socket_t kInvalidSocket = INVALID_SOCKET; #else #include #include @@ -51,6 +52,7 @@ typedef SOCKET socket_t; #include typedef int socket_t; +constexpr socket_t kInvalidSocket = -1; #endif #include @@ -560,13 +562,13 @@ socket_t create_socket(const char* host, int port, Fn fn, int socket_flags = 0) auto service = std::to_string(port); if (getaddrinfo(host, service.c_str(), &hints, &result)) { - return -1; + return kInvalidSocket; } for (auto rp = result; rp; rp = rp->ai_next) { // Create a socket auto sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); - if (sock == -1) { + if (sock == kInvalidSocket) { continue; } @@ -584,7 +586,7 @@ socket_t create_socket(const char* host, int port, Fn fn, int socket_flags = 0) } freeaddrinfo(result); - return -1; + return kInvalidSocket; } inline void set_nonblocking(socket_t sock, bool nonblocking) @@ -1415,7 +1417,7 @@ inline std::string SocketStream::get_remote_addr() { inline Server::Server(HttpVersion http_version) : http_version_(http_version) , is_running_(false) - , svr_sock_(-1) + , svr_sock_(kInvalidSocket) , running_threads_(0) { #ifndef _WIN32 @@ -1500,10 +1502,10 @@ inline bool Server::is_running() const inline void Server::stop() { if (is_running_) { - assert(svr_sock_ != -1); + assert(svr_sock_ != kInvalidSocket); detail::shutdown_socket(svr_sock_); detail::close_socket(svr_sock_); - svr_sock_ = -1; + svr_sock_ = kInvalidSocket; } } @@ -1624,7 +1626,7 @@ inline int Server::bind_internal(const char* host, int port, int socket_flags) } svr_sock_ = create_server_socket(host, port, socket_flags); - if (svr_sock_ == -1) { + if (svr_sock_ == kInvalidSocket) { return -1; } @@ -1650,7 +1652,7 @@ inline bool Server::listen_internal() auto val = detail::select_read(svr_sock_, 0, 100000); if (val == 0) { // Timeout - if (svr_sock_ == -1) { + if (svr_sock_ == kInvalidSocket) { // The server socket was closed by 'stop' method. break; } @@ -1659,8 +1661,8 @@ inline bool Server::listen_internal() socket_t sock = accept(svr_sock_, NULL, NULL); - if (sock == -1) { - if (svr_sock_ != -1) { + if (sock == kInvalidSocket) { + if (svr_sock_ != kInvalidSocket) { detail::close_socket(svr_sock_); ret = false; } else { @@ -1894,7 +1896,7 @@ inline bool Client::send(Request& req, Response& res) } auto sock = create_client_socket(); - if (sock == -1) { + if (sock == kInvalidSocket) { return false; } From 37130cd7f9df4fb4454d94dd9f0a3bb7c6ccbad8 Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 10 May 2018 23:54:53 -0400 Subject: [PATCH 075/118] Changed to use INVALID_SOCKET --- httplib.h | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/httplib.h b/httplib.h index 0070e2d..3066f13 100644 --- a/httplib.h +++ b/httplib.h @@ -39,7 +39,6 @@ #endif typedef SOCKET socket_t; -constexpr socket_t kInvalidSocket = INVALID_SOCKET; #else #include #include @@ -52,7 +51,7 @@ constexpr socket_t kInvalidSocket = INVALID_SOCKET; #include typedef int socket_t; -constexpr socket_t kInvalidSocket = -1; +#define INVALID_SOCKET (-1) #endif #include @@ -562,13 +561,13 @@ socket_t create_socket(const char* host, int port, Fn fn, int socket_flags = 0) auto service = std::to_string(port); if (getaddrinfo(host, service.c_str(), &hints, &result)) { - return kInvalidSocket; + return INVALID_SOCKET; } for (auto rp = result; rp; rp = rp->ai_next) { // Create a socket auto sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); - if (sock == kInvalidSocket) { + if (sock == INVALID_SOCKET) { continue; } @@ -586,7 +585,7 @@ socket_t create_socket(const char* host, int port, Fn fn, int socket_flags = 0) } freeaddrinfo(result); - return kInvalidSocket; + return INVALID_SOCKET; } inline void set_nonblocking(socket_t sock, bool nonblocking) @@ -1417,7 +1416,7 @@ inline std::string SocketStream::get_remote_addr() { inline Server::Server(HttpVersion http_version) : http_version_(http_version) , is_running_(false) - , svr_sock_(kInvalidSocket) + , svr_sock_(INVALID_SOCKET) , running_threads_(0) { #ifndef _WIN32 @@ -1502,10 +1501,10 @@ inline bool Server::is_running() const inline void Server::stop() { if (is_running_) { - assert(svr_sock_ != kInvalidSocket); + assert(svr_sock_ != INVALID_SOCKET); detail::shutdown_socket(svr_sock_); detail::close_socket(svr_sock_); - svr_sock_ = kInvalidSocket; + svr_sock_ = INVALID_SOCKET; } } @@ -1626,7 +1625,7 @@ inline int Server::bind_internal(const char* host, int port, int socket_flags) } svr_sock_ = create_server_socket(host, port, socket_flags); - if (svr_sock_ == kInvalidSocket) { + if (svr_sock_ == INVALID_SOCKET) { return -1; } @@ -1652,7 +1651,7 @@ inline bool Server::listen_internal() auto val = detail::select_read(svr_sock_, 0, 100000); if (val == 0) { // Timeout - if (svr_sock_ == kInvalidSocket) { + if (svr_sock_ == INVALID_SOCKET) { // The server socket was closed by 'stop' method. break; } @@ -1661,8 +1660,8 @@ inline bool Server::listen_internal() socket_t sock = accept(svr_sock_, NULL, NULL); - if (sock == kInvalidSocket) { - if (svr_sock_ != kInvalidSocket) { + if (sock == INVALID_SOCKET) { + if (svr_sock_ != INVALID_SOCKET) { detail::close_socket(svr_sock_); ret = false; } else { @@ -1896,7 +1895,7 @@ inline bool Client::send(Request& req, Response& res) } auto sock = create_client_socket(); - if (sock == kInvalidSocket) { + if (sock == INVALID_SOCKET) { return false; } From 312a8d7523573ebf7ace8c3c29d7fce118d27a48 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 13 May 2018 19:00:05 -0400 Subject: [PATCH 076/118] Removed HTTP version. It's now always 'HTTP/1.1'. --- example/server.cc | 6 ++--- example/simplesvr.cc | 6 ++--- httplib.h | 60 ++++++++++++++++---------------------------- test/Makefile | 6 ++--- test/test.cc | 35 ++++++++++---------------- 5 files changed, 42 insertions(+), 71 deletions(-) diff --git a/example/server.cc b/example/server.cc index bd24b35..7cef68b 100644 --- a/example/server.cc +++ b/example/server.cc @@ -68,12 +68,10 @@ std::string log(const Request& req, const Response& res) int main(void) { - auto version = httplib::HttpVersion::v1_1; - #ifdef CPPHTTPLIB_OPENSSL_SUPPORT - SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, version); + SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE); #else - Server svr(version); + Server svr; #endif if (!svr.is_valid()) { diff --git a/example/simplesvr.cc b/example/simplesvr.cc index daeb90a..d1496fa 100644 --- a/example/simplesvr.cc +++ b/example/simplesvr.cc @@ -99,12 +99,10 @@ int main(int argc, const char** argv) return 1; } - auto version = httplib::HttpVersion::v1_1; - #ifdef CPPHTTPLIB_OPENSSL_SUPPORT - SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, version); + SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE); #else - Server svr(version); + Server svr; #endif svr.Post("/multipart", [](const auto& req, auto& res) { diff --git a/httplib.h b/httplib.h index 3066f13..d7e2283 100644 --- a/httplib.h +++ b/httplib.h @@ -189,7 +189,7 @@ public: typedef std::function Handler; typedef std::function Logger; - Server(HttpVersion http_version = HttpVersion::v1_0); + Server(); virtual ~Server(); @@ -218,8 +218,6 @@ public: protected: bool process_request(Stream& strm, bool last_connection); - const HttpVersion http_version_; - private: typedef std::vector> Handlers; @@ -257,8 +255,7 @@ public: Client( const char* host, int port = 80, - size_t timeout_sec = 300, - HttpVersion http_version = HttpVersion::v1_0); + size_t timeout_sec = 300); virtual ~Client(); @@ -293,7 +290,6 @@ protected: const std::string host_; const int port_; size_t timeout_sec_; - const HttpVersion http_version_; const std::string host_and_port_; private: @@ -323,8 +319,7 @@ private: class SSLServer : public Server { public: SSLServer( - const char* cert_path, const char* private_key_path, - HttpVersion http_version = HttpVersion::v1_0); + const char* cert_path, const char* private_key_path); virtual ~SSLServer(); @@ -342,8 +337,7 @@ public: SSLClient( const char* host, int port = 80, - size_t timeout_sec = 300, - HttpVersion http_version = HttpVersion::v1_0); + size_t timeout_sec = 300); virtual ~SSLClient(); @@ -362,8 +356,6 @@ private: */ namespace detail { -static std::vector http_version_strings = { "HTTP/1.0", "HTTP/1.1" }; - template void split(const char* b, const char* e, char d, Fn fn) { @@ -1413,9 +1405,8 @@ inline std::string SocketStream::get_remote_addr() { } // HTTP server implementation -inline Server::Server(HttpVersion http_version) - : http_version_(http_version) - , is_running_(false) +inline Server::Server() + : is_running_(false) , svr_sock_(INVALID_SOCKET) , running_threads_(0) { @@ -1539,8 +1530,7 @@ inline void Server::write_response(Stream& strm, bool last_connection, const Req } // Response line - strm.write_format("%s %d %s\r\n", - detail::http_version_strings[static_cast(http_version_)], + strm.write_format("HTTP/1.1 %d %s\r\n", res.status, detail::status_message(res.status)); @@ -1551,7 +1541,7 @@ inline void Server::write_response(Stream& strm, bool last_connection, const Req if (!res.body.empty()) { #ifdef CPPHTTPLIB_ZLIB_SUPPORT - // TODO: Server version is HTTP/1.1 and 'Accpet-Encoding' has gzip, not gzip;q=0 + // TODO: 'Accpet-Encoding' has gzip, not gzip;q=0 const auto& encodings = req.get_header_value("Accept-Encoding"); if (encodings.find("gzip") != std::string::npos && detail::can_compress(res.get_header_value("Content-Type"))) { @@ -1749,7 +1739,7 @@ inline bool Server::process_request(Stream& strm, bool last_connection) Request req; Response res; - res.version = detail::http_version_strings[static_cast(http_version_)]; + res.version = "HTTP/1.1"; // Request line and headers if (!parse_request_line(reader.ptr(), req) || !detail::read_headers(strm, req.headers)) { @@ -1817,11 +1807,9 @@ inline bool Server::is_valid() const inline bool Server::read_and_close_socket(socket_t sock) { - auto keep_alive = http_version_ == HttpVersion::v1_1; - return detail::read_and_close_socket( sock, - keep_alive, + true, [this](Stream& strm, bool last_connection) { return process_request(strm, last_connection); }); @@ -1829,11 +1817,10 @@ inline bool Server::read_and_close_socket(socket_t sock) // HTTP client implementation inline Client::Client( - const char* host, int port, size_t timeout_sec, HttpVersion http_version) + const char* host, int port, size_t timeout_sec) : host_(host) , port_(port) , timeout_sec_(timeout_sec) - , http_version_(http_version) , host_and_port_(host_ + ":" + std::to_string(port_)) { } @@ -1878,11 +1865,12 @@ inline bool Client::read_response_line(Stream& strm, Response& res) return false; } - const static std::regex re("HTTP/1\\.[01] (\\d+?) .+\r\n"); + const static std::regex re("(HTTP/1\\.[01]) (\\d+?) .+\r\n"); std::cmatch m; if (std::regex_match(reader.ptr(), m, re)) { - res.status = std::stoi(std::string(m[1])); + res.version = std::string(m[1]); + res.status = std::stoi(std::string(m[2])); } return true; @@ -1907,10 +1895,9 @@ inline void Client::write_request(Stream& strm, Request& req) auto path = detail::encode_url(req.path); // Request line - strm.write_format("%s %s %s\r\n", + strm.write_format("%s %s HTTP/1.1\r\n", req.method.c_str(), - path.c_str(), - detail::http_version_strings[static_cast(http_version_)]); + path.c_str()); // Headers req.set_header("Host", host_and_port_.c_str()); @@ -1961,7 +1948,8 @@ inline bool Client::process_request(Stream& strm, Request& req, Response& res) return false; } - // TODO: Check if 'Connection' header is 'close' or HTTP version is 1.0, then close socket... + // TODO: Check if 'Connection' header is 'close' or HTTP version is 1.0, + // then close socket... // Body if (req.method != "HEAD") { @@ -2227,8 +2215,7 @@ inline std::string SSLSocketStream::get_remote_addr() { } // SSL HTTP server implementation -inline SSLServer::SSLServer(const char* cert_path, const char* private_key_path, HttpVersion http_version) - : Server(http_version) +inline SSLServer::SSLServer(const char* cert_path, const char* private_key_path) { ctx_ = SSL_CTX_new(SSLv23_server_method()); @@ -2264,11 +2251,9 @@ inline bool SSLServer::is_valid() const inline bool SSLServer::read_and_close_socket(socket_t sock) { - auto keep_alive = http_version_ == HttpVersion::v1_1; - return detail::read_and_close_socket_ssl( sock, - keep_alive, + true, ctx_, ctx_mutex_, SSL_accept, [](SSL* /*ssl*/) {}, @@ -2278,9 +2263,8 @@ inline bool SSLServer::read_and_close_socket(socket_t sock) } // SSL HTTP client implementation -inline SSLClient::SSLClient( - const char* host, int port, size_t timeout_sec, HttpVersion http_version) - : Client(host, port, timeout_sec, http_version) +inline SSLClient::SSLClient(const char* host, int port, size_t timeout_sec) + : Client(host, port, timeout_sec) { ctx_ = SSL_CTX_new(SSLv23_client_method()); } diff --git a/test/Makefile b/test/Makefile index 8c76650..ef10343 100644 --- a/test/Makefile +++ b/test/Makefile @@ -1,8 +1,8 @@ CC = clang++ -CFLAGS = -std=c++11 -DGTEST_USE_OWN_TR1_TUPLE -I.. -I. -Wall -Wextra -lpthread -#OPENSSL_SUPPORT = -DCPPHTTPLIB_OPENSSL_SUPPORT -I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib -lssl -lcrypto -#ZLIB_SUPPORT = -DCPPHTTPLIB_ZLIB_SUPPORT -lz +CFLAGS = -O0 -std=c++11 -DGTEST_USE_OWN_TR1_TUPLE -I.. -I. -Wall -Wextra -lpthread +OPENSSL_SUPPORT = -DCPPHTTPLIB_OPENSSL_SUPPORT -I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib -lssl -lcrypto +ZLIB_SUPPORT = -DCPPHTTPLIB_ZLIB_SUPPORT -lz all : test ./test diff --git a/test/test.cc b/test/test.cc index 2330683..3f2a692 100644 --- a/test/test.cc +++ b/test/test.cc @@ -118,17 +118,17 @@ TEST(GetHeaderValueTest, Range) } } -void testChunkedEncoding(httplib::HttpVersion ver) +TEST(ChunkedEncodingTest, FromHTTPWatch) { auto host = "www.httpwatch.com"; auto sec = 2; #ifdef CPPHTTPLIB_OPENSSL_SUPPORT auto port = 443; - httplib::SSLClient cli(host, port, sec, ver); + httplib::SSLClient cli(host, port, sec); #else auto port = 80; - httplib::Client cli(host, port, sec, ver); + httplib::Client cli(host, port, sec); #endif auto res = cli.Get("/httpgallery/chunked/chunkedimage.aspx?0.4153841143030137"); @@ -141,24 +141,17 @@ void testChunkedEncoding(httplib::HttpVersion ver) EXPECT_EQ(out, res->body); } -TEST(ChunkedEncodingTest, FromHTTPWatch) -{ - testChunkedEncoding(httplib::HttpVersion::v1_0); - testChunkedEncoding(httplib::HttpVersion::v1_1); -} - TEST(RangeTest, FromHTTPBin) { auto host = "httpbin.org"; auto sec = 5; - auto ver = httplib::HttpVersion::v1_1; #ifdef CPPHTTPLIB_OPENSSL_SUPPORT auto port = 443; - httplib::SSLClient cli(host, port, sec, ver); + httplib::SSLClient cli(host, port, sec); #else auto port = 80; - httplib::Client cli(host, port, sec, ver); + httplib::Client cli(host, port, sec); #endif { @@ -190,14 +183,13 @@ TEST(ConnectionErrorTest, InvalidHost) { auto host = "abcde.com"; auto sec = 2; - auto ver = httplib::HttpVersion::v1_1; #ifdef CPPHTTPLIB_OPENSSL_SUPPORT auto port = 443; - httplib::SSLClient cli(host, port, sec, ver); + httplib::SSLClient cli(host, port, sec); #else auto port = 80; - httplib::Client cli(host, port, sec, ver); + httplib::Client cli(host, port, sec); #endif auto res = cli.Get("/"); @@ -208,14 +200,13 @@ TEST(ConnectionErrorTest, InvalidPort) { auto host = "localhost"; auto sec = 2; - auto ver = httplib::HttpVersion::v1_1; #ifdef CPPHTTPLIB_OPENSSL_SUPPORT auto port = 44380; - httplib::SSLClient cli(host, port, sec, ver); + httplib::SSLClient cli(host, port, sec); #else auto port = 8080; - httplib::Client cli(host, port, sec, ver); + httplib::Client cli(host, port, sec); #endif auto res = cli.Get("/"); @@ -226,14 +217,13 @@ TEST(ConnectionErrorTest, Timeout) { auto host = "google.com"; auto sec = 2; - auto ver = httplib::HttpVersion::v1_1; #ifdef CPPHTTPLIB_OPENSSL_SUPPORT auto port = 44380; - httplib::SSLClient cli(host, port, sec, ver); + httplib::SSLClient cli(host, port, sec); #else auto port = 8080; - httplib::Client cli(host, port, sec, ver); + httplib::Client cli(host, port, sec); #endif auto res = cli.Get("/"); @@ -241,7 +231,7 @@ TEST(ConnectionErrorTest, Timeout) } TEST(Server, BindAndListenSeparately) { - Server svr(httplib::HttpVersion::v1_1); + Server svr; int port = svr.bind_to_any_port("localhost"); ASSERT_TRUE(port > 0); svr.stop(); @@ -400,6 +390,7 @@ TEST_F(ServerTest, GetMethod200) { auto res = cli_.Get("/hi"); ASSERT_TRUE(res != nullptr); + EXPECT_EQ("HTTP/1.1", res->version); EXPECT_EQ(200, res->status); EXPECT_EQ("text/plain", res->get_header_value("Content-Type")); EXPECT_EQ("Hello World!", res->body); From 7b9d752583700b631fbe08e74c0279272e707e53 Mon Sep 17 00:00:00 2001 From: yhirose Date: Mon, 14 May 2018 00:05:14 -0400 Subject: [PATCH 077/118] Fixed problem with connection close --- httplib.h | 75 +++++++++++++++++++++++++++++++--------------------- test/test.cc | 1 + 2 files changed, 46 insertions(+), 30 deletions(-) diff --git a/httplib.h b/httplib.h index d7e2283..e76f390 100644 --- a/httplib.h +++ b/httplib.h @@ -216,7 +216,7 @@ public: void stop(); protected: - bool process_request(Stream& strm, bool last_connection); + bool process_request(Stream& strm, bool last_connection, bool& connection_close); private: typedef std::vector> Handlers; @@ -285,7 +285,7 @@ public: bool send(Request& req, Response& res); protected: - bool process_request(Stream& strm, Request& req, Response& res); + bool process_request(Stream& strm, Request& req, Response& res, bool& connection_close); const std::string host_; const int port_; @@ -503,17 +503,21 @@ inline bool read_and_close_socket(socket_t sock, bool keep_alive, T callback) detail::select_read(sock, CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND, CPPHTTPLIB_KEEPALIVE_TIMEOUT_USECOND) > 0) { - auto last_connection = count == 1; SocketStream strm(sock); - ret = callback(strm, last_connection); - if (!ret) { + auto last_connection = count == 1; + auto connection_close = false; + + ret = callback(strm, last_connection, connection_close); + if (!ret || connection_close) { break; } + count--; } } else { SocketStream strm(sock); - ret = callback(strm, true); + auto dummy_connection_close = false; + ret = callback(strm, true, dummy_connection_close); } close_socket(sock); @@ -1535,7 +1539,9 @@ inline void Server::write_response(Stream& strm, bool last_connection, const Req detail::status_message(res.status)); // Headers - if (!res.has_header("Connection") && (last_connection || req.version == "HTTP/1.0")) { + if (last_connection || + req.version == "HTTP/1.0" || + req.get_header_value("Connection") == "close") { res.set_header("Connection", "close"); } @@ -1724,7 +1730,7 @@ inline bool Server::dispatch_request(Request& req, Response& res, Handlers& hand return false; } -inline bool Server::process_request(Stream& strm, bool last_connection) +inline bool Server::process_request(Stream& strm, bool last_connection, bool& connection_close) { const auto bufsiz = 2048; char buf[bufsiz]; @@ -1750,7 +1756,8 @@ inline bool Server::process_request(Stream& strm, bool last_connection) auto ret = true; if (req.get_header_value("Connection") == "close") { - ret = false; + // ret = false; + connection_close = true; } req.set_header("REMOTE_ADDR", strm.get_remote_addr().c_str()); @@ -1810,8 +1817,8 @@ inline bool Server::read_and_close_socket(socket_t sock) return detail::read_and_close_socket( sock, true, - [this](Stream& strm, bool last_connection) { - return process_request(strm, last_connection); + [this](Stream& strm, bool last_connection, bool& connection_close) { + return process_request(strm, last_connection, connection_close); }); } @@ -1910,11 +1917,10 @@ inline void Client::write_request(Stream& strm, Request& req) req.set_header("User-Agent", "cpp-httplib/0.2"); } - // TODO: if (!req.has_header("Connection") && - // (last_connection || http_version_ == detail::HttpVersion::v1_0)) { - if (!req.has_header("Connection")) { + // TODO: Support KeepAlive connection + // if (!req.has_header("Connection")) { req.set_header("Connection", "close"); - } + // } if (!req.body.empty()) { if (!req.has_header("Content-Type")) { @@ -1938,7 +1944,7 @@ inline void Client::write_request(Stream& strm, Request& req) } } -inline bool Client::process_request(Stream& strm, Request& req, Response& res) +inline bool Client::process_request(Stream& strm, Request& req, Response& res, bool& connection_close) { // Send request write_request(strm, req); @@ -1948,8 +1954,9 @@ inline bool Client::process_request(Stream& strm, Request& req, Response& res) return false; } - // TODO: Check if 'Connection' header is 'close' or HTTP version is 1.0, - // then close socket... + if (res.get_header_value("Connection") == "close" || res.version == "HTTP/1.0") { + connection_close = true; + } // Body if (req.method != "HEAD") { @@ -1971,9 +1978,12 @@ inline bool Client::process_request(Stream& strm, Request& req, Response& res) inline bool Client::read_and_close_socket(socket_t sock, Request& req, Response& res) { - return detail::read_and_close_socket(sock, false, [&](Stream& strm, bool /*last_connection*/) { - return process_request(strm, req, res); - }); + return detail::read_and_close_socket( + sock, + false, + [&](Stream& strm, bool /*last_connection*/, bool& connection_close) { + return process_request(strm, req, res, connection_close); + }); } inline std::shared_ptr Client::Get(const char* path, Progress progress) @@ -2118,7 +2128,8 @@ namespace detail { template inline bool read_and_close_socket_ssl( socket_t sock, bool keep_alive, - // TODO: OpenSSL 1.0.2 occasionally crashes... The upcoming 1.1.0 is going to be thread safe. + // TODO: OpenSSL 1.0.2 occasionally crashes... + // The upcoming 1.1.0 is going to be thread safe. SSL_CTX* ctx, std::mutex& ctx_mutex, U SSL_connect_or_accept, V setup, T callback) @@ -2148,17 +2159,21 @@ inline bool read_and_close_socket_ssl( detail::select_read(sock, CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND, CPPHTTPLIB_KEEPALIVE_TIMEOUT_USECOND) > 0) { - auto last_connection = count == 1; SSLSocketStream strm(sock, ssl); - ret = callback(strm, last_connection); - if (!ret) { + auto last_connection = count == 1; + auto connection_close = false; + + ret = callback(strm, last_connection, connection_close); + if (!ret || connection_close) { break; } + count--; } } else { SSLSocketStream strm(sock, ssl); - ret = callback(strm, true); + auto dummy_connection_close = false; + ret = callback(strm, true, dummy_connection_close); } SSL_shutdown(ssl); @@ -2257,8 +2272,8 @@ inline bool SSLServer::read_and_close_socket(socket_t sock) ctx_, ctx_mutex_, SSL_accept, [](SSL* /*ssl*/) {}, - [this](Stream& strm, bool last_connection) { - return process_request(strm, last_connection); + [this](Stream& strm, bool last_connection, bool& connection_close) { + return process_request(strm, last_connection, connection_close); }); } @@ -2290,8 +2305,8 @@ inline bool SSLClient::read_and_close_socket(socket_t sock, Request& req, Respon [&](SSL* ssl) { SSL_set_tlsext_host_name(ssl, host_.c_str()); }, - [&](Stream& strm, bool /*last_connection*/) { - return process_request(strm, req, res); + [&](Stream& strm, bool /*last_connection*/, bool& connection_close) { + return process_request(strm, req, res, connection_close); }); } #endif diff --git a/test/test.cc b/test/test.cc index 3f2a692..13c1032 100644 --- a/test/test.cc +++ b/test/test.cc @@ -393,6 +393,7 @@ TEST_F(ServerTest, GetMethod200) EXPECT_EQ("HTTP/1.1", res->version); EXPECT_EQ(200, res->status); EXPECT_EQ("text/plain", res->get_header_value("Content-Type")); + EXPECT_EQ("close", res->get_header_value("Connection")); EXPECT_EQ("Hello World!", res->body); } From 40662d5e3c99165f734295ffa1999fd588228d9b Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 18 May 2018 16:43:08 -0400 Subject: [PATCH 078/118] Fix #68 --- README.md | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 84685b4..1e8b4b1 100644 --- a/README.md +++ b/README.md @@ -115,8 +115,19 @@ res = cli.Post("/person", "name=john1¬e=coder", "application/x-www-form-urlen ```c++ httplib::Params params; -params["name"] = "john"; -params["note"] = "coder"; +params.emplace("name", "john"); +params.emplace("note", "coder"); + +auto res = cli.Post("/post", params); +``` + or + +```c++ +httplib::Params params{ + { "name", "john" }, + { "note", "coder" } +}; + auto res = cli.Post("/post", params); ``` From f275352cba8a6cd91f408ac26d75ab2eb323e209 Mon Sep 17 00:00:00 2001 From: Scott Graham Date: Tue, 29 May 2018 12:59:13 -0700 Subject: [PATCH 079/118] Handle port==0 when socket is bound on ipv6 I discovered https://github.com/yhirose/cpp-httplib/commit/0515c6aad6834a2a329db226b9312294d0002a13 doesn't work when the server is bound on an AF_INET6 address on Windows due to the getsockname() call failing. --- httplib.h | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/httplib.h b/httplib.h index e76f390..4420144 100644 --- a/httplib.h +++ b/httplib.h @@ -1626,12 +1626,18 @@ inline int Server::bind_internal(const char* host, int port, int socket_flags) } if (port == 0) { - struct sockaddr_in sin; - socklen_t len = sizeof(sin); - if (getsockname(svr_sock_, reinterpret_cast(&sin), &len) == -1) { + struct sockaddr_storage address; + socklen_t len = sizeof(address); + if (getsockname(svr_sock_, reinterpret_cast(&address), &len) == -1) { return -1; } - return ntohs(sin.sin_port); + if (address.ss_family == AF_INET) { + return ntohs(reinterpret_cast(&address)->sin_port); + } else if (address.ss_family == AF_INET6) { + return ntohs(reinterpret_cast(&address)->sin6_port); + } else { + return -1; + } } else { return port; } From 5a78e1c4574e5c102c8da6e548134766cc0e8e56 Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 31 May 2018 19:01:24 -0400 Subject: [PATCH 080/118] Added 'set_keep_alive_max_count' method on Server --- httplib.h | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/httplib.h b/httplib.h index 4420144..162d96e 100644 --- a/httplib.h +++ b/httplib.h @@ -77,7 +77,6 @@ typedef int socket_t; /* * Configuration */ -#define CPPHTTPLIB_KEEPALIVE_MAX_COUNT 5 #define CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND 5 #define CPPHTTPLIB_KEEPALIVE_TIMEOUT_USECOND 0 @@ -207,6 +206,8 @@ public: void set_error_handler(Handler handler); void set_logger(Logger logger); + void set_keep_alive_max_count(size_t count); + int bind_to_any_port(const char* host, int socket_flags = 0); bool listen_after_bind(); @@ -218,6 +219,8 @@ public: protected: bool process_request(Stream& strm, bool last_connection, bool& connection_close); + size_t keep_alive_max_count_; + private: typedef std::vector> Handlers; @@ -493,12 +496,12 @@ inline bool wait_until_socket_is_ready(socket_t sock, size_t sec, size_t usec) } template -inline bool read_and_close_socket(socket_t sock, bool keep_alive, T callback) +inline bool read_and_close_socket(socket_t sock, size_t keep_alive_max_count, T callback) { bool ret = false; - if (keep_alive) { - auto count = CPPHTTPLIB_KEEPALIVE_MAX_COUNT; + if (keep_alive_max_count > 0) { + auto count = keep_alive_max_count; while (count > 0 && detail::select_read(sock, CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND, @@ -1410,7 +1413,8 @@ inline std::string SocketStream::get_remote_addr() { // HTTP server implementation inline Server::Server() - : is_running_(false) + : keep_alive_max_count_(5) + , is_running_(false) , svr_sock_(INVALID_SOCKET) , running_threads_(0) { @@ -1472,6 +1476,11 @@ inline void Server::set_logger(Logger logger) logger_ = logger; } +inline void Server::set_keep_alive_max_count(size_t count) +{ + keep_alive_max_count_ = count; +} + inline int Server::bind_to_any_port(const char* host, int socket_flags) { return bind_internal(host, 0, socket_flags); @@ -1822,7 +1831,7 @@ inline bool Server::read_and_close_socket(socket_t sock) { return detail::read_and_close_socket( sock, - true, + keep_alive_max_count_, [this](Stream& strm, bool last_connection, bool& connection_close) { return process_request(strm, last_connection, connection_close); }); @@ -1986,7 +1995,7 @@ inline bool Client::read_and_close_socket(socket_t sock, Request& req, Response& { return detail::read_and_close_socket( sock, - false, + 0, [&](Stream& strm, bool /*last_connection*/, bool& connection_close) { return process_request(strm, req, res, connection_close); }); @@ -2133,7 +2142,7 @@ namespace detail { template inline bool read_and_close_socket_ssl( - socket_t sock, bool keep_alive, + socket_t sock, size_t keep_alive_max_count, // TODO: OpenSSL 1.0.2 occasionally crashes... // The upcoming 1.1.0 is going to be thread safe. SSL_CTX* ctx, std::mutex& ctx_mutex, @@ -2159,8 +2168,8 @@ inline bool read_and_close_socket_ssl( bool ret = false; - if (keep_alive) { - auto count = CPPHTTPLIB_KEEPALIVE_MAX_COUNT; + if (keep_alive_max_count > 0) { + auto count = keep_alive_max_count; while (count > 0 && detail::select_read(sock, CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND, @@ -2274,7 +2283,7 @@ inline bool SSLServer::read_and_close_socket(socket_t sock) { return detail::read_and_close_socket_ssl( sock, - true, + keep_alive_max_count_, ctx_, ctx_mutex_, SSL_accept, [](SSL* /*ssl*/) {}, @@ -2305,7 +2314,7 @@ inline bool SSLClient::is_valid() const inline bool SSLClient::read_and_close_socket(socket_t sock, Request& req, Response& res) { return is_valid() && detail::read_and_close_socket_ssl( - sock, false, + sock, 0, ctx_, ctx_mutex_, SSL_connect, [&](SSL* ssl) { From 5b3187e2f9e77c672063d49a1167bbb563da023e Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 31 May 2018 22:08:14 -0400 Subject: [PATCH 081/118] Fix #72 --- httplib.h | 12 +++++++----- test/test.cc | 12 ++++++++++++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/httplib.h b/httplib.h index 162d96e..fc525c3 100644 --- a/httplib.h +++ b/httplib.h @@ -120,6 +120,7 @@ typedef std::multimap 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; diff --git a/test/test.cc b/test/test.cc index 13c1032..6b7f624 100644 --- a/test/test.cc +++ b/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) { From bb2f96afebdaa4d369b4efdfac9a643c85b62149 Mon Sep 17 00:00:00 2001 From: Albert S Date: Fri, 1 Jun 2018 14:34:58 +0200 Subject: [PATCH 082/118] Added a few more common http status codes --- httplib.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/httplib.h b/httplib.h index fc525c3..39d0e9f 100644 --- a/httplib.h +++ b/httplib.h @@ -732,7 +732,12 @@ inline const char* status_message(int status) { switch (status) { case 200: return "OK"; + case 301: return "Moved Permanently"; + case 302: return "Found"; + case 303: return "See Other"; + case 304: return "Not Modified"; case 400: return "Bad Request"; + case 403: return "Forbidden"; case 404: return "Not Found"; case 415: return "Unsupported Media Type"; default: From 2bb27aa25d51020ad8b3570c880530ba0bff833b Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 1 Jun 2018 23:04:17 -0400 Subject: [PATCH 083/118] Fix #74 --- httplib.h | 10 +++++++--- test/test.cc | 7 +++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/httplib.h b/httplib.h index fc525c3..683cc2b 100644 --- a/httplib.h +++ b/httplib.h @@ -938,8 +938,12 @@ inline bool is_hex(char c, int& v) return false; } -inline bool from_hex_to_i(const std::string& s, int i, int cnt, int& val) +inline bool from_hex_to_i(const std::string& s, size_t i, size_t cnt, int& val) { + if (i >= s.size()) { + return false; + } + val = 0; for (; cnt; i++, cnt--) { if (!s[i]) { @@ -992,8 +996,8 @@ inline std::string decode_url(const std::string& s) { std::string result; - for (int i = 0; s[i]; i++) { - if (s[i] == '%') { + for (size_t i = 0; s[i]; i++) { + if (s[i] == '%' && i + 1 < s.size()) { if (s[i + 1] && s[i + 1] == 'u') { int val = 0; if (from_hex_to_i(s, i + 2, 4, val)) { diff --git a/test/test.cc b/test/test.cc index 6b7f624..3e85730 100644 --- a/test/test.cc +++ b/test/test.cc @@ -643,6 +643,13 @@ TEST_F(ServerTest, InvalidPercentEncodingUnicode) EXPECT_EQ(404, res->status); } +TEST_F(ServerTest, EndWithPercentCharacterInQuery) +{ + auto res = cli_.Get("/hello?aaa=bbb%"); + ASSERT_TRUE(res != nullptr); + EXPECT_EQ(404, res->status); +} + TEST_F(ServerTest, MultipartFormData) { Request req; From d9479bc0b12e8a1e8bce2d34da4feeef488581f3 Mon Sep 17 00:00:00 2001 From: Albert S Date: Sat, 2 Jun 2018 08:40:19 +0200 Subject: [PATCH 084/118] Fixed bound checks for #74 --- httplib.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/httplib.h b/httplib.h index 77c2fab..5fb8308 100644 --- a/httplib.h +++ b/httplib.h @@ -1001,9 +1001,9 @@ inline std::string decode_url(const std::string& s) { std::string result; - for (size_t i = 0; s[i]; i++) { + for (size_t i = 0; i < s.size(); i++) { if (s[i] == '%' && i + 1 < s.size()) { - if (s[i + 1] && s[i + 1] == 'u') { + if (s[i + 1] == 'u') { int val = 0; if (from_hex_to_i(s, i + 2, 4, val)) { // 4 digits Unicode codes From 85a30e73a5895fcfafc82f78e003d06fda867584 Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 14 Jun 2018 15:30:55 +0000 Subject: [PATCH 085/118] Fixed -lpthread linker option position --- test/Makefile | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/Makefile b/test/Makefile index ef10343..b30859a 100644 --- a/test/Makefile +++ b/test/Makefile @@ -1,6 +1,8 @@ CC = clang++ -CFLAGS = -O0 -std=c++11 -DGTEST_USE_OWN_TR1_TUPLE -I.. -I. -Wall -Wextra -lpthread +#CC = g++ + +CFLAGS = -O0 -std=c++11 -DGTEST_USE_OWN_TR1_TUPLE -I.. -I. -Wall -Wextra OPENSSL_SUPPORT = -DCPPHTTPLIB_OPENSSL_SUPPORT -I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib -lssl -lcrypto ZLIB_SUPPORT = -DCPPHTTPLIB_ZLIB_SUPPORT -lz @@ -8,7 +10,7 @@ all : test ./test test : test.cc ../httplib.h Makefile - $(CC) -o test $(CFLAGS) test.cc gtest/gtest-all.cc gtest/gtest_main.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) + $(CC) -o test $(CFLAGS) test.cc gtest/gtest-all.cc gtest/gtest_main.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) -lpthread pem: openssl genrsa 2048 > key.pem From 86ec676408c15730c43a322141f2917fedbd1aad Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 14 Jun 2018 12:05:31 -0400 Subject: [PATCH 086/118] Add additional note for SSL support --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 1e8b4b1..4c10f3c 100644 --- a/README.md +++ b/README.md @@ -187,10 +187,10 @@ auto res = cli.Get("/range/32", headers); // res->body should be "bcdefghijk". ``` -OpenSSL Support ---------------- +OpenSSL 1.0.2 Support +--------------------- -SSL support is available with `CPPHTTPLIB_OPENSSL_SUPPORT`. `libssl` and `libcrypto` should be linked. +SSL (OpenSSL 1.0.2) support is available with `CPPHTTPLIB_OPENSSL_SUPPORT`. `libssl` and `libcrypto` should be linked. ```c++ #define CPPHTTPLIB_OPENSSL_SUPPORT From 222f49a1252b0dc289818dfb9b4c67bb5dbd6715 Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 14 Jun 2018 12:25:55 -0400 Subject: [PATCH 087/118] Revert "Add additional note for SSL support" This reverts commit 86ec676408c15730c43a322141f2917fedbd1aad. --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 4c10f3c..1e8b4b1 100644 --- a/README.md +++ b/README.md @@ -187,10 +187,10 @@ auto res = cli.Get("/range/32", headers); // res->body should be "bcdefghijk". ``` -OpenSSL 1.0.2 Support ---------------------- +OpenSSL Support +--------------- -SSL (OpenSSL 1.0.2) support is available with `CPPHTTPLIB_OPENSSL_SUPPORT`. `libssl` and `libcrypto` should be linked. +SSL support is available with `CPPHTTPLIB_OPENSSL_SUPPORT`. `libssl` and `libcrypto` should be linked. ```c++ #define CPPHTTPLIB_OPENSSL_SUPPORT From d26ee036134c1286ab990574ab5b7758c674697e Mon Sep 17 00:00:00 2001 From: Maksim Kolinichenko Date: Fri, 27 Jul 2018 17:26:14 +0300 Subject: [PATCH 088/118] Fixed request parsing regex typo --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 5fb8308..3fd843d 100644 --- a/httplib.h +++ b/httplib.h @@ -1528,7 +1528,7 @@ inline bool Server::parse_request_line(const char* s, Request& req) std::cmatch m; if (std::regex_match(s, m, re)) { - req.version = std::string(m[4]); + req.version = std::string(m[5]); req.method = std::string(m[1]); req.target = std::string(m[2]); req.path = detail::decode_url(m[3]); From 15ed1b4883c63bce20c62d08ab9b1d3e1e24bc25 Mon Sep 17 00:00:00 2001 From: Maksim Kolinichenko Date: Fri, 27 Jul 2018 17:39:04 +0300 Subject: [PATCH 089/118] Add Keep-Alive header to response --- httplib.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 3fd843d..2544c88 100644 --- a/httplib.h +++ b/httplib.h @@ -1560,10 +1560,14 @@ inline void Server::write_response(Stream& strm, bool last_connection, const Req // Headers if (last_connection || - req.version == "HTTP/1.0" || req.get_header_value("Connection") == "close") { res.set_header("Connection", "close"); } + + if (!last_connection && + req.get_header_value("Connection") == "Keep-Alive") { + res.set_header("Connection", "Keep-Alive"); + } if (!res.body.empty()) { #ifdef CPPHTTPLIB_ZLIB_SUPPORT From ca343ae1d864522f79829f4ba2fccc5ec5f7c0c2 Mon Sep 17 00:00:00 2001 From: David Guillen Fandos Date: Tue, 31 Jul 2018 22:08:38 +0200 Subject: [PATCH 090/118] Fix small issues in tests and added some extra checks. --- test/Makefile | 10 +++++----- test/test.cc | 6 +++--- test/test.conf | 18 ++++++++++++++++++ 3 files changed, 26 insertions(+), 8 deletions(-) create mode 100644 test/test.conf diff --git a/test/Makefile b/test/Makefile index b30859a..2034063 100644 --- a/test/Makefile +++ b/test/Makefile @@ -2,19 +2,19 @@ CC = clang++ #CC = g++ -CFLAGS = -O0 -std=c++11 -DGTEST_USE_OWN_TR1_TUPLE -I.. -I. -Wall -Wextra +CFLAGS = -ggdb -O0 -std=c++11 -DGTEST_USE_OWN_TR1_TUPLE -I.. -I. -Wall -Wextra OPENSSL_SUPPORT = -DCPPHTTPLIB_OPENSSL_SUPPORT -I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib -lssl -lcrypto ZLIB_SUPPORT = -DCPPHTTPLIB_ZLIB_SUPPORT -lz all : test ./test -test : test.cc ../httplib.h Makefile +test : test.cc ../httplib.h Makefile cert.pem $(CC) -o test $(CFLAGS) test.cc gtest/gtest-all.cc gtest/gtest_main.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) -lpthread -pem: +cert.pem: openssl genrsa 2048 > key.pem - openssl req -new -key key.pem | openssl x509 -days 3650 -req -signkey key.pem > cert.pem + openssl req -new -batch -config test.conf -key key.pem | openssl x509 -days 3650 -req -signkey key.pem > cert.pem clean: - rm test *.pem + rm -f test *.pem diff --git a/test/test.cc b/test/test.cc index 3e85730..d4e6791 100644 --- a/test/test.cc +++ b/test/test.cc @@ -363,7 +363,7 @@ protected: persons_["john"] = "programmer"; t_ = thread([&](){ - svr_.listen(HOST, PORT); + ASSERT_TRUE(svr_.listen(HOST, PORT)); }); while (!svr_.is_running()) { @@ -890,7 +890,7 @@ protected: }); t_ = thread([&]() { - svr_.listen(nullptr, PORT, AI_PASSIVE); + ASSERT_TRUE(svr_.listen(nullptr, PORT, AI_PASSIVE)); }); while (!svr_.is_running()) { @@ -932,7 +932,7 @@ protected: t_ = thread([&](){ svr_.bind_to_any_port(HOST); msleep(500); - svr_.listen_after_bind(); + ASSERT_TRUE(svr_.listen_after_bind()); }); while (!svr_.is_running()) { diff --git a/test/test.conf b/test/test.conf new file mode 100644 index 0000000..b4ae121 --- /dev/null +++ b/test/test.conf @@ -0,0 +1,18 @@ +[req] +default_bits = 2048 +distinguished_name = req_distinguished_name +attributes = req_attributes +prompt = no +output_password = mypass + +[req_distinguished_name] +C = US +ST = Test State or Province +L = Test Locality +O = Organization Name +OU = Organizational Unit Name +CN = Common Name +emailAddress = test@email.address + +[req_attributes] +challengePassword = 1234 From 07910f73a929186ff8daaa769e6dc3f4ea8ccb3e Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 31 Jul 2018 19:46:04 -0400 Subject: [PATCH 091/118] Fixed problem that `listen` may return incorrect value when calling `stop`; --- httplib.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/httplib.h b/httplib.h index 2544c88..909d9ec 100644 --- a/httplib.h +++ b/httplib.h @@ -1516,9 +1516,10 @@ inline void Server::stop() { if (is_running_) { assert(svr_sock_ != INVALID_SOCKET); - detail::shutdown_socket(svr_sock_); - detail::close_socket(svr_sock_); + auto sock = svr_sock_; svr_sock_ = INVALID_SOCKET; + detail::shutdown_socket(sock); + detail::close_socket(sock); } } From dae4124039c540063927eb36ae6ab372f00476b1 Mon Sep 17 00:00:00 2001 From: David Guillen Fandos Date: Wed, 1 Aug 2018 01:26:18 +0200 Subject: [PATCH 092/118] Implementing streaming Responses This enables a much easier handling of big queries after all. --- httplib.h | 37 +++++++++++++++++++++++++++++++++++-- test/test.cc | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 2 deletions(-) diff --git a/httplib.h b/httplib.h index 2544c88..f288f0c 100644 --- a/httplib.h +++ b/httplib.h @@ -146,6 +146,7 @@ struct Response { int status; Headers headers; std::string body; + std::function streamcb; bool has_header(const char* key) const; std::string get_header_value(const char* key) const; @@ -964,6 +965,17 @@ inline bool from_hex_to_i(const std::string& s, size_t i, size_t cnt, int& val) return true; } +inline std::string from_i_to_hex(uint64_t n) +{ + const char *charset = "0123456789abcdef"; + std::string ret; + do { + ret = charset[n & 15] + ret; + n >>= 4; + } while (n > 0); + return ret; +} + inline size_t to_utf8(int code, char* buff) { if (code < 0x0080) { @@ -1586,13 +1598,34 @@ inline void Server::write_response(Stream& strm, bool last_connection, const Req auto length = std::to_string(res.body.size()); res.set_header("Content-Length", length.c_str()); + } else if (res.streamcb) { + // Streamed response + bool chunked_response = !res.has_header("Content-Length"); + if (chunked_response) + res.set_header("Transfer-Encoding", "chunked"); } detail::write_headers(strm, res); // Body - if (!res.body.empty() && req.method != "HEAD") { - strm.write(res.body.c_str(), res.body.size()); + if (req.method != "HEAD") { + if (!res.body.empty()) { + strm.write(res.body.c_str(), res.body.size()); + } else if (res.streamcb) { + bool chunked_response = !res.has_header("Content-Length"); + uint64_t offset = 0; + bool data_available = true; + while (data_available) { + std::string chunk = res.streamcb(offset); + offset += chunk.size(); + data_available = !chunk.empty(); + // Emit chunked response header and footer for each chunk + if (chunked_response) + chunk = detail::from_i_to_hex(chunk.size()) + "\r\n" + chunk + "\r\n"; + if (strm.write(chunk.c_str(), chunk.size()) < 0) + break; // Stop on error + } + } } // Log diff --git a/test/test.cc b/test/test.cc index d4e6791..ca0c6e0 100644 --- a/test/test.cc +++ b/test/test.cc @@ -282,6 +282,25 @@ protected: res.status = 404; } }) + .Get("/streamedchunked", [&](const Request& /*req*/, Response& res) { + res.streamcb = [] (uint64_t offset) { + if (offset < 3) + return "a"; + if (offset < 6) + return "b"; + return ""; + }; + }) + .Get("/streamed", [&](const Request& /*req*/, Response& res) { + res.set_header("Content-Length", "6"); + res.streamcb = [] (uint64_t offset) { + if (offset < 3) + return "a"; + if (offset < 6) + return "b"; + return ""; + }; + }) .Post("/chunked", [&](const Request& req, Response& /*res*/) { EXPECT_EQ(req.body, "dechunked post body"); }) @@ -712,6 +731,23 @@ TEST_F(ServerTest, CaseInsensitiveTransferEncoding) EXPECT_EQ(200, res->status); } +TEST_F(ServerTest, GetStreamed) +{ + auto res = cli_.Get("/streamed"); + ASSERT_TRUE(res != nullptr); + EXPECT_EQ(200, res->status); + EXPECT_EQ("6", res->get_header_value("Content-Length")); + EXPECT_TRUE(res->body == "aaabbb"); +} + +TEST_F(ServerTest, GetStreamedChunked) +{ + auto res = cli_.Get("/streamedchunked"); + ASSERT_TRUE(res != nullptr); + EXPECT_EQ(200, res->status); + EXPECT_TRUE(res->body == "aaabbb"); +} + TEST_F(ServerTest, LargeChunkedPost) { Request req; req.method = "POST"; From cc983be31f079f7d5f46fec1d2e1cf787196ad50 Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 2 Aug 2018 22:31:36 -0400 Subject: [PATCH 093/118] Removed unused build projects --- test/test.vcxproj | 158 ----------- test/test.xcodeproj/project.pbxproj | 248 ------------------ .../contents.xcworkspacedata | 7 - 3 files changed, 413 deletions(-) delete mode 100644 test/test.vcxproj delete mode 100644 test/test.xcodeproj/project.pbxproj delete mode 100644 test/test.xcodeproj/project.xcworkspace/contents.xcworkspacedata diff --git a/test/test.vcxproj b/test/test.vcxproj deleted file mode 100644 index 3f15c7c..0000000 --- a/test/test.vcxproj +++ /dev/null @@ -1,158 +0,0 @@ - - - - - Debug - Win32 - - - Debug - x64 - - - Release - Win32 - - - Release - x64 - - - - {6B3E6769-052D-4BC0-9D2C-E9127C3DBB26} - Win32Proj - test - 8.1 - - - - Application - true - v141 - Unicode - - - Application - true - v141 - Unicode - - - Application - false - v141 - true - Unicode - - - Application - false - v141 - true - Unicode - - - - - - - - - - - - - - - - - - - true - - - true - - - false - - - false - - - - - - Level3 - Disabled - WIN32;_DEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions) - ./;../ - - - Console - true - Ws2_32.lib;%(AdditionalDependencies) - - - - - - - Level3 - Disabled - WIN32;_DEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions) - ./;../ - - - Console - true - Ws2_32.lib;%(AdditionalDependencies) - - - - - Level3 - - - MaxSpeed - true - true - WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions) - ./;../ - - - Console - true - true - true - Ws2_32.lib;%(AdditionalDependencies) - - - - - Level3 - - - MaxSpeed - true - true - WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions) - ./;../ - - - Console - true - true - true - Ws2_32.lib;%(AdditionalDependencies) - - - - - - - - - - - \ No newline at end of file diff --git a/test/test.xcodeproj/project.pbxproj b/test/test.xcodeproj/project.pbxproj deleted file mode 100644 index abff30e..0000000 --- a/test/test.xcodeproj/project.pbxproj +++ /dev/null @@ -1,248 +0,0 @@ -// !$*UTF8*$! -{ - archiveVersion = 1; - classes = { - }; - objectVersion = 46; - objects = { - -/* Begin PBXBuildFile section */ - BAA4BF2517280236003EF6AD /* test.cc in Sources */ = {isa = PBXBuildFile; fileRef = BAA4BF2417280236003EF6AD /* test.cc */; }; - BAF46809172813BB0069D928 /* gtest-all.cc in Sources */ = {isa = PBXBuildFile; fileRef = BAF46806172813BB0069D928 /* gtest-all.cc */; }; - BAF4680A172813BB0069D928 /* gtest_main.cc in Sources */ = {isa = PBXBuildFile; fileRef = BAF46808172813BB0069D928 /* gtest_main.cc */; }; -/* End PBXBuildFile section */ - -/* Begin PBXCopyFilesBuildPhase section */ - BAE5F9C51727F08F001D0075 /* CopyFiles */ = { - isa = PBXCopyFilesBuildPhase; - buildActionMask = 2147483647; - dstPath = /usr/share/man/man1/; - dstSubfolderSpec = 0; - files = ( - ); - runOnlyForDeploymentPostprocessing = 1; - }; -/* End PBXCopyFilesBuildPhase section */ - -/* Begin PBXFileReference section */ - BAA4BF2417280236003EF6AD /* test.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = test.cc; sourceTree = SOURCE_ROOT; }; - BAE5F9C71727F08F001D0075 /* test */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = test; sourceTree = BUILT_PRODUCTS_DIR; }; - BAF46806172813BB0069D928 /* gtest-all.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = "gtest-all.cc"; sourceTree = ""; }; - BAF46807172813BB0069D928 /* gtest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = gtest.h; sourceTree = ""; }; - BAF46808172813BB0069D928 /* gtest_main.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = gtest_main.cc; sourceTree = ""; }; -/* End PBXFileReference section */ - -/* Begin PBXFrameworksBuildPhase section */ - BAE5F9C41727F08F001D0075 /* Frameworks */ = { - isa = PBXFrameworksBuildPhase; - buildActionMask = 2147483647; - files = ( - ); - runOnlyForDeploymentPostprocessing = 0; - }; -/* End PBXFrameworksBuildPhase section */ - -/* Begin PBXGroup section */ - BAE5F9BE1727F08F001D0075 = { - isa = PBXGroup; - children = ( - BAE5F9C91727F08F001D0075 /* test */, - BAE5F9C81727F08F001D0075 /* Products */, - ); - sourceTree = ""; - }; - BAE5F9C81727F08F001D0075 /* Products */ = { - isa = PBXGroup; - children = ( - BAE5F9C71727F08F001D0075 /* test */, - ); - name = Products; - sourceTree = ""; - }; - BAE5F9C91727F08F001D0075 /* test */ = { - isa = PBXGroup; - children = ( - BAF46805172813BB0069D928 /* gtest */, - BAA4BF2417280236003EF6AD /* test.cc */, - ); - path = test; - sourceTree = ""; - }; - BAF46805172813BB0069D928 /* gtest */ = { - isa = PBXGroup; - children = ( - BAF46806172813BB0069D928 /* gtest-all.cc */, - BAF46807172813BB0069D928 /* gtest.h */, - BAF46808172813BB0069D928 /* gtest_main.cc */, - ); - path = gtest; - sourceTree = SOURCE_ROOT; - }; -/* End PBXGroup section */ - -/* Begin PBXNativeTarget section */ - BAE5F9C61727F08F001D0075 /* test */ = { - isa = PBXNativeTarget; - buildConfigurationList = BAE5F9D01727F08F001D0075 /* Build configuration list for PBXNativeTarget "test" */; - buildPhases = ( - BAE5F9C31727F08F001D0075 /* Sources */, - BAE5F9C41727F08F001D0075 /* Frameworks */, - BAE5F9C51727F08F001D0075 /* CopyFiles */, - ); - buildRules = ( - ); - dependencies = ( - ); - name = test; - productName = test; - productReference = BAE5F9C71727F08F001D0075 /* test */; - productType = "com.apple.product-type.tool"; - }; -/* End PBXNativeTarget section */ - -/* Begin PBXProject section */ - BAE5F9BF1727F08F001D0075 /* Project object */ = { - isa = PBXProject; - attributes = { - LastUpgradeCheck = 0460; - ORGANIZATIONNAME = "Yuji Hirose"; - }; - buildConfigurationList = BAE5F9C21727F08F001D0075 /* Build configuration list for PBXProject "test" */; - compatibilityVersion = "Xcode 3.2"; - developmentRegion = English; - hasScannedForEncodings = 0; - knownRegions = ( - en, - ); - mainGroup = BAE5F9BE1727F08F001D0075; - productRefGroup = BAE5F9C81727F08F001D0075 /* Products */; - projectDirPath = ""; - projectRoot = ""; - targets = ( - BAE5F9C61727F08F001D0075 /* test */, - ); - }; -/* End PBXProject section */ - -/* Begin PBXSourcesBuildPhase section */ - BAE5F9C31727F08F001D0075 /* Sources */ = { - isa = PBXSourcesBuildPhase; - buildActionMask = 2147483647; - files = ( - BAA4BF2517280236003EF6AD /* test.cc in Sources */, - BAF46809172813BB0069D928 /* gtest-all.cc in Sources */, - BAF4680A172813BB0069D928 /* gtest_main.cc in Sources */, - ); - runOnlyForDeploymentPostprocessing = 0; - }; -/* End PBXSourcesBuildPhase section */ - -/* Begin XCBuildConfiguration section */ - BAE5F9CE1727F08F001D0075 /* Debug */ = { - isa = XCBuildConfiguration; - buildSettings = { - ALWAYS_SEARCH_USER_PATHS = NO; - ARCHS = "$(ARCHS_STANDARD_64_BIT)"; - CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; - CLANG_CXX_LIBRARY = "libc++"; - CLANG_WARN_CONSTANT_CONVERSION = YES; - CLANG_WARN_EMPTY_BODY = YES; - CLANG_WARN_ENUM_CONVERSION = YES; - CLANG_WARN_INT_CONVERSION = YES; - CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; - COPY_PHASE_STRIP = NO; - GCC_C_LANGUAGE_STANDARD = gnu99; - GCC_DYNAMIC_NO_PIC = NO; - GCC_ENABLE_OBJC_EXCEPTIONS = YES; - GCC_OPTIMIZATION_LEVEL = 0; - GCC_PREPROCESSOR_DEFINITIONS = ( - GTEST_USE_OWN_TR1_TUPLE, - "DEBUG=1", - "$(inherited)", - ); - GCC_SYMBOLS_PRIVATE_EXTERN = NO; - GCC_WARN_64_TO_32_BIT_CONVERSION = YES; - GCC_WARN_ABOUT_RETURN_TYPE = YES; - GCC_WARN_UNINITIALIZED_AUTOS = YES; - GCC_WARN_UNUSED_VARIABLE = YES; - HEADER_SEARCH_PATHS = ( - "../**", - "./**", - ); - MACOSX_DEPLOYMENT_TARGET = 10.7; - ONLY_ACTIVE_ARCH = YES; - SDKROOT = macosx; - USER_HEADER_SEARCH_PATHS = ""; - }; - name = Debug; - }; - BAE5F9CF1727F08F001D0075 /* Release */ = { - isa = XCBuildConfiguration; - buildSettings = { - ALWAYS_SEARCH_USER_PATHS = NO; - ARCHS = "$(ARCHS_STANDARD_64_BIT)"; - CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; - CLANG_CXX_LIBRARY = "libc++"; - CLANG_WARN_CONSTANT_CONVERSION = YES; - CLANG_WARN_EMPTY_BODY = YES; - CLANG_WARN_ENUM_CONVERSION = YES; - CLANG_WARN_INT_CONVERSION = YES; - CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; - COPY_PHASE_STRIP = YES; - DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; - GCC_C_LANGUAGE_STANDARD = gnu99; - GCC_ENABLE_OBJC_EXCEPTIONS = YES; - GCC_PREPROCESSOR_DEFINITIONS = GTEST_USE_OWN_TR1_TUPLE; - GCC_WARN_64_TO_32_BIT_CONVERSION = YES; - GCC_WARN_ABOUT_RETURN_TYPE = YES; - GCC_WARN_UNINITIALIZED_AUTOS = YES; - GCC_WARN_UNUSED_VARIABLE = YES; - HEADER_SEARCH_PATHS = ( - "../**", - "./**", - ); - MACOSX_DEPLOYMENT_TARGET = 10.7; - SDKROOT = macosx; - USER_HEADER_SEARCH_PATHS = ""; - }; - name = Release; - }; - BAE5F9D11727F08F001D0075 /* Debug */ = { - isa = XCBuildConfiguration; - buildSettings = { - PRODUCT_NAME = "$(TARGET_NAME)"; - }; - name = Debug; - }; - BAE5F9D21727F08F001D0075 /* Release */ = { - isa = XCBuildConfiguration; - buildSettings = { - PRODUCT_NAME = "$(TARGET_NAME)"; - }; - name = Release; - }; -/* End XCBuildConfiguration section */ - -/* Begin XCConfigurationList section */ - BAE5F9C21727F08F001D0075 /* Build configuration list for PBXProject "test" */ = { - isa = XCConfigurationList; - buildConfigurations = ( - BAE5F9CE1727F08F001D0075 /* Debug */, - BAE5F9CF1727F08F001D0075 /* Release */, - ); - defaultConfigurationIsVisible = 0; - defaultConfigurationName = Release; - }; - BAE5F9D01727F08F001D0075 /* Build configuration list for PBXNativeTarget "test" */ = { - isa = XCConfigurationList; - buildConfigurations = ( - BAE5F9D11727F08F001D0075 /* Debug */, - BAE5F9D21727F08F001D0075 /* Release */, - ); - defaultConfigurationIsVisible = 0; - defaultConfigurationName = Release; - }; -/* End XCConfigurationList section */ - }; - rootObject = BAE5F9BF1727F08F001D0075 /* Project object */; -} diff --git a/test/test.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/test/test.xcodeproj/project.xcworkspace/contents.xcworkspacedata deleted file mode 100644 index d683bc9..0000000 --- a/test/test.xcodeproj/project.xcworkspace/contents.xcworkspacedata +++ /dev/null @@ -1,7 +0,0 @@ - - - - - From 82fc7d5591237b49e3cb87e13ec4aec55c39a98b Mon Sep 17 00:00:00 2001 From: Thomas Tissot Date: Mon, 6 Aug 2018 11:54:52 +0200 Subject: [PATCH 094/118] Request cancelation feature This commit modifies the signature of the `Progress` callback so that its return value will indicate whether the request shall continue to be processed by returning `true`, or if it shall be aborted by returning `false`. Such modification will allow one to cancel an ongoing request before it has completed. When migrating, developers should modify there `Progress` callbacks to always return `true` by default in case there do not want to benefit from the cancelation feature. A few unit tests use cases were provided, but anyone should feel free to provide additional uses cases that they find relevant. --- httplib.h | 6 ++++-- test/test.cc | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/httplib.h b/httplib.h index bffdf91..dff8aad 100644 --- a/httplib.h +++ b/httplib.h @@ -107,7 +107,7 @@ std::pair make_range_header(uint64_t value, Args... ar typedef std::multimap Params; typedef std::smatch Match; -typedef std::function Progress; +typedef std::function Progress; struct MultipartFile { std::string filename; @@ -804,7 +804,9 @@ inline bool read_content_with_length(Stream& strm, std::string& out, size_t len, r += n; if (progress) { - progress(r, len); + if (!progress(r, len)) { + return false; + } } } diff --git a/test/test.cc b/test/test.cc index ca0c6e0..6169b60 100644 --- a/test/test.cc +++ b/test/test.cc @@ -230,6 +230,66 @@ TEST(ConnectionErrorTest, Timeout) ASSERT_TRUE(res == nullptr); } +TEST(CancelTest, NoCancel) { + auto host = "httpbin.org"; + auto sec = 5; + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + auto port = 443; + httplib::SSLClient cli(host, port, sec); +#else + auto port = 80; + httplib::Client cli(host, port, sec); +#endif + + httplib::Headers headers; + auto res = cli.Get("/range/32", headers, [](uint64_t, uint64_t) { + return true; + }); + ASSERT_TRUE(res != nullptr); + EXPECT_EQ(res->body, "abcdefghijklmnopqrstuvwxyzabcdef"); + EXPECT_EQ(200, res->status); +} + +TEST(CancelTest, WithCancelSmallPayload) { + auto host = "httpbin.org"; + auto sec = 5; + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + auto port = 443; + httplib::SSLClient cli(host, port, sec); +#else + auto port = 80; + httplib::Client cli(host, port, sec); +#endif + + httplib::Headers headers; + auto res = cli.Get("/range/32", headers, [](uint64_t, uint64_t) { + return false; + }); + ASSERT_TRUE(res == nullptr); +} + +TEST(CancelTest, WithCancelLargePayload) { + auto host = "httpbin.org"; + auto sec = 5; + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + auto port = 443; + httplib::SSLClient cli(host, port, sec); +#else + auto port = 80; + httplib::Client cli(host, port, sec); +#endif + + uint32_t count = 0; + httplib::Headers headers; + auto res = cli.Get("/range/65536", headers, [&count](uint64_t, uint64_t) { + return (count++ == 0); + }); + ASSERT_TRUE(res == nullptr); +} + TEST(Server, BindAndListenSeparately) { Server svr; int port = svr.bind_to_any_port("localhost"); From 48b47ef209c52eb106bdec7a5ec5d1ba1166a093 Mon Sep 17 00:00:00 2001 From: yhirose Date: Mon, 6 Aug 2018 22:52:48 -0400 Subject: [PATCH 095/118] Updated README for cancelation feature --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 1e8b4b1..2dc38fa 100644 --- a/README.md +++ b/README.md @@ -166,6 +166,7 @@ std::shared_ptr res = printf("%lld / %lld bytes => %d%% complete\n", len, total, (int)((len/total)*100)); + return true; // return 'false' if you want to cancel the request. } ); ``` From 4af5b1e441b925dfff341053f5e5fbe904ab5b6f Mon Sep 17 00:00:00 2001 From: Duncan Ogilvie Date: Fri, 17 Aug 2018 11:49:42 +0200 Subject: [PATCH 096/118] Refactor the examples to compile with a C++11 compiler --- example/hello.cc | 2 +- example/server.cc | 17 ++++++++--------- example/simplesvr.cc | 6 +++--- 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/example/hello.cc b/example/hello.cc index f315511..9db3195 100644 --- a/example/hello.cc +++ b/example/hello.cc @@ -12,7 +12,7 @@ int main(void) { Server svr; - svr.Get("/hi", [](const auto& /*req*/, auto& res) { + svr.Get("/hi", [](const Request& /*req*/, Response& res) { res.set_content("Hello World!", "text/plain"); }); diff --git a/example/server.cc b/example/server.cc index 7cef68b..b3de034 100644 --- a/example/server.cc +++ b/example/server.cc @@ -79,36 +79,35 @@ int main(void) return -1; } - svr.Get("/", [=](const auto& /*req*/, auto& res) { + svr.Get("/", [=](const Request& /*req*/, Response& res) { res.set_redirect("/hi"); }); - svr.Get("/hi", [](const auto& /*req*/, auto& res) { + svr.Get("/hi", [](const Request& /*req*/, Response& res) { res.set_content("Hello World!\n", "text/plain"); }); - svr.Get("/slow", [](const auto& /*req*/, auto& res) { - using namespace std::chrono_literals; - std::this_thread::sleep_for(2s); + svr.Get("/slow", [](const Request& /*req*/, Response& res) { + std::this_thread::sleep_for(std::chrono::seconds(2)); res.set_content("Slow...\n", "text/plain"); }); - svr.Get("/dump", [](const auto& req, auto& res) { + svr.Get("/dump", [](const Request& req, Response& res) { res.set_content(dump_headers(req.headers), "text/plain"); }); - svr.Get("/stop", [&](const auto& /*req*/, auto& /*res*/) { + svr.Get("/stop", [&](const Request& /*req*/, Response& res) { svr.stop(); }); - svr.set_error_handler([](const auto& /*req*/, auto& res) { + svr.set_error_handler([](const Request& /*req*/, Response& res) { const char* fmt = "

Error Status: %d

"; char buf[BUFSIZ]; snprintf(buf, sizeof(buf), fmt, res.status); res.set_content(buf, "text/html"); }); - svr.set_logger([](const auto& req, const auto& res) { + svr.set_logger([](const Request& req, const Response& res) { printf("%s", log(req, res).c_str()); }); diff --git a/example/simplesvr.cc b/example/simplesvr.cc index d1496fa..4a6fe0c 100644 --- a/example/simplesvr.cc +++ b/example/simplesvr.cc @@ -105,7 +105,7 @@ int main(int argc, const char** argv) Server svr; #endif - svr.Post("/multipart", [](const auto& req, auto& res) { + svr.Post("/multipart", [](const Request& req, Response& res) { auto body = dump_headers(req.headers) + dump_multipart_files(req.files); @@ -113,14 +113,14 @@ int main(int argc, const char** argv) res.set_content(body, "text/plain"); }); - svr.set_error_handler([](const auto& /*req*/, auto& res) { + svr.set_error_handler([](const Request& /*req*/, Response& res) { const char* fmt = "

Error Status: %d

"; char buf[BUFSIZ]; snprintf(buf, sizeof(buf), fmt, res.status); res.set_content(buf, "text/html"); }); - svr.set_logger([](const auto& req, const auto& res) { + svr.set_logger([](const Request& req, const Response& res) { cout << log(req, res); }); From e3927382515563532eda9a1c42b8181bfc35ded3 Mon Sep 17 00:00:00 2001 From: Duncan Ogilvie Date: Fri, 17 Aug 2018 11:51:20 +0200 Subject: [PATCH 097/118] Fix intermediate directory in project + add x64 configurations in MSVC example solution --- example/client.vcxproj | 71 ++++++++++++++++++++++++++++++++++++++++++ example/example.sln | 19 +++++++++-- example/server.vcxproj | 71 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 159 insertions(+), 2 deletions(-) diff --git a/example/client.vcxproj b/example/client.vcxproj index 6788d19..1abb773 100644 --- a/example/client.vcxproj +++ b/example/client.vcxproj @@ -5,10 +5,18 @@ Debug Win32 + + Debug + x64 + Release Win32 + + Release + x64 + {6DB1FC63-B153-4279-92B7-D8A11AF285D6} @@ -23,6 +31,12 @@ Unicode v141 + + Application + true + Unicode + v141 + Application false @@ -30,21 +44,44 @@ Unicode v141 + + Application + false + true + Unicode + v141 + + + + + + + true + $(Configuration)\$(ProjectName)_obj\ + + + true + $(Platform)\$(Configuration)\$(ProjectName)_obj\ false + $(Configuration)\$(ProjectName)_obj\ + + + false + $(Platform)\$(Configuration)\$(ProjectName)_obj\ @@ -61,6 +98,21 @@ Ws2_32.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) + + + + + Level3 + Disabled + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + .. + + + Console + true + Ws2_32.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) + + Level3 @@ -80,6 +132,25 @@ Ws2_32.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) + + + Level3 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + .. + + + Console + true + true + true + Ws2_32.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) + + diff --git a/example/example.sln b/example/example.sln index e11aa85..81a3e78 100644 --- a/example/example.sln +++ b/example/example.sln @@ -1,6 +1,8 @@  -Microsoft Visual Studio Solution File, Format Version 11.00 -# Visual Studio 2010 +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.27703.2047 +MinimumVisualStudioVersion = 10.0.40219.1 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "server", "server.vcxproj", "{864CD288-050A-4C8B-9BEF-3048BD876C5B}" EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "client", "client.vcxproj", "{6DB1FC63-B153-4279-92B7-D8A11AF285D6}" @@ -13,19 +15,32 @@ EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Win32 = Debug|Win32 + Debug|x64 = Debug|x64 Release|Win32 = Release|Win32 + Release|x64 = Release|x64 EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution {864CD288-050A-4C8B-9BEF-3048BD876C5B}.Debug|Win32.ActiveCfg = Debug|Win32 {864CD288-050A-4C8B-9BEF-3048BD876C5B}.Debug|Win32.Build.0 = Debug|Win32 + {864CD288-050A-4C8B-9BEF-3048BD876C5B}.Debug|x64.ActiveCfg = Debug|x64 + {864CD288-050A-4C8B-9BEF-3048BD876C5B}.Debug|x64.Build.0 = Debug|x64 {864CD288-050A-4C8B-9BEF-3048BD876C5B}.Release|Win32.ActiveCfg = Release|Win32 {864CD288-050A-4C8B-9BEF-3048BD876C5B}.Release|Win32.Build.0 = Release|Win32 + {864CD288-050A-4C8B-9BEF-3048BD876C5B}.Release|x64.ActiveCfg = Release|x64 + {864CD288-050A-4C8B-9BEF-3048BD876C5B}.Release|x64.Build.0 = Release|x64 {6DB1FC63-B153-4279-92B7-D8A11AF285D6}.Debug|Win32.ActiveCfg = Debug|Win32 {6DB1FC63-B153-4279-92B7-D8A11AF285D6}.Debug|Win32.Build.0 = Debug|Win32 + {6DB1FC63-B153-4279-92B7-D8A11AF285D6}.Debug|x64.ActiveCfg = Debug|x64 + {6DB1FC63-B153-4279-92B7-D8A11AF285D6}.Debug|x64.Build.0 = Debug|x64 {6DB1FC63-B153-4279-92B7-D8A11AF285D6}.Release|Win32.ActiveCfg = Release|Win32 {6DB1FC63-B153-4279-92B7-D8A11AF285D6}.Release|Win32.Build.0 = Release|Win32 + {6DB1FC63-B153-4279-92B7-D8A11AF285D6}.Release|x64.ActiveCfg = Release|x64 + {6DB1FC63-B153-4279-92B7-D8A11AF285D6}.Release|x64.Build.0 = Release|x64 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {7097C9E4-07F8-48C6-A888-BBA9EBB5D17D} + EndGlobalSection EndGlobal diff --git a/example/server.vcxproj b/example/server.vcxproj index 76b3da5..31ff203 100644 --- a/example/server.vcxproj +++ b/example/server.vcxproj @@ -5,10 +5,18 @@ Debug Win32 + + Debug + x64 + Release Win32 + + Release + x64 + {864CD288-050A-4C8B-9BEF-3048BD876C5B} @@ -23,6 +31,12 @@ Unicode v141 + + Application + true + Unicode + v141 + Application false @@ -30,21 +44,44 @@ Unicode v141 + + Application + false + true + Unicode + v141 + + + + + + + true + $(Configuration)\$(ProjectName)_obj\ + + + true + $(Platform)\$(Configuration)\$(ProjectName)_obj\ false + $(Configuration)\$(ProjectName)_obj\ + + + false + $(Platform)\$(Configuration)\$(ProjectName)_obj\ @@ -61,6 +98,21 @@ Ws2_32.lib;%(AdditionalDependencies) + + + + + Level3 + Disabled + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + .. + + + Console + true + Ws2_32.lib;%(AdditionalDependencies) + + Level3 @@ -80,6 +132,25 @@ Ws2_32.lib;%(AdditionalDependencies) + + + Level3 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + .. + + + Console + true + true + true + Ws2_32.lib;%(AdditionalDependencies) + + From bc16283a2f1a5df5e8159b4a07e5e6d6c0df35d3 Mon Sep 17 00:00:00 2001 From: Duncan Ogilvie Date: Fri, 17 Aug 2018 11:51:41 +0200 Subject: [PATCH 098/118] Change size_t to time_t where applicable --- httplib.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/httplib.h b/httplib.h index dff8aad..fd6049b 100644 --- a/httplib.h +++ b/httplib.h @@ -260,7 +260,7 @@ public: Client( const char* host, int port = 80, - size_t timeout_sec = 300); + time_t timeout_sec = 300); virtual ~Client(); @@ -294,7 +294,7 @@ protected: const std::string host_; const int port_; - size_t timeout_sec_; + time_t timeout_sec_; const std::string host_and_port_; private: @@ -342,7 +342,7 @@ public: SSLClient( const char* host, int port = 80, - size_t timeout_sec = 300); + time_t timeout_sec = 300); virtual ~SSLClient(); @@ -456,7 +456,7 @@ inline int close_socket(socket_t sock) #endif } -inline int select_read(socket_t sock, size_t sec, size_t usec) +inline int select_read(socket_t sock, time_t sec, time_t usec) { fd_set fds; FD_ZERO(&fds); @@ -469,7 +469,7 @@ inline int select_read(socket_t sock, size_t sec, size_t usec) return select(sock + 1, &fds, NULL, NULL, &tv); } -inline bool wait_until_socket_is_ready(socket_t sock, size_t sec, size_t usec) +inline bool wait_until_socket_is_ready(socket_t sock, time_t sec, time_t usec) { fd_set fdsr; FD_ZERO(&fdsr); @@ -1890,7 +1890,7 @@ inline bool Server::read_and_close_socket(socket_t sock) // HTTP client implementation inline Client::Client( - const char* host, int port, size_t timeout_sec) + const char* host, int port, time_t timeout_sec) : host_(host) , port_(port) , timeout_sec_(timeout_sec) @@ -2344,7 +2344,7 @@ inline bool SSLServer::read_and_close_socket(socket_t sock) } // SSL HTTP client implementation -inline SSLClient::SSLClient(const char* host, int port, size_t timeout_sec) +inline SSLClient::SSLClient(const char* host, int port, time_t timeout_sec) : Client(host, port, timeout_sec) { ctx_ = SSL_CTX_new(SSLv23_client_method()); From 28d17448b7212305ae1526eb307b913b05544999 Mon Sep 17 00:00:00 2001 From: Yuji Hirose Date: Fri, 14 Sep 2018 15:57:20 -0400 Subject: [PATCH 099/118] Revert test.vcproj. This reverts commit cc983be31f079f7d5f46fec1d2e1cf787196ad50. --- test/test.vcxproj | 158 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 158 insertions(+) create mode 100644 test/test.vcxproj diff --git a/test/test.vcxproj b/test/test.vcxproj new file mode 100644 index 0000000..3f15c7c --- /dev/null +++ b/test/test.vcxproj @@ -0,0 +1,158 @@ + + + + + Debug + Win32 + + + Debug + x64 + + + Release + Win32 + + + Release + x64 + + + + {6B3E6769-052D-4BC0-9D2C-E9127C3DBB26} + Win32Proj + test + 8.1 + + + + Application + true + v141 + Unicode + + + Application + true + v141 + Unicode + + + Application + false + v141 + true + Unicode + + + Application + false + v141 + true + Unicode + + + + + + + + + + + + + + + + + + + true + + + true + + + false + + + false + + + + + + Level3 + Disabled + WIN32;_DEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions) + ./;../ + + + Console + true + Ws2_32.lib;%(AdditionalDependencies) + + + + + + + Level3 + Disabled + WIN32;_DEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions) + ./;../ + + + Console + true + Ws2_32.lib;%(AdditionalDependencies) + + + + + Level3 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions) + ./;../ + + + Console + true + true + true + Ws2_32.lib;%(AdditionalDependencies) + + + + + Level3 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions) + ./;../ + + + Console + true + true + true + Ws2_32.lib;%(AdditionalDependencies) + + + + + + + + + + + \ No newline at end of file From 9546ec842b4188e459be6ed2d52c256c442e3942 Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 14 Sep 2018 17:53:11 -0400 Subject: [PATCH 100/118] Fixed problem with log format in server examples --- example/server.cc | 2 +- example/simplesvr.cc | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/example/server.cc b/example/server.cc index b3de034..4adcb57 100644 --- a/example/server.cc +++ b/example/server.cc @@ -35,7 +35,7 @@ std::string log(const Request& req, const Response& res) s += "================================\n"; - snprintf(buf, sizeof(buf), "%s %s %s", req.method.c_str(), req.path.c_str(), req.version.c_str()); + snprintf(buf, sizeof(buf), "%s %s %s", req.method.c_str(), req.version.c_str(), req.path.c_str()); s += buf; std::string query; diff --git a/example/simplesvr.cc b/example/simplesvr.cc index 4a6fe0c..124a314 100644 --- a/example/simplesvr.cc +++ b/example/simplesvr.cc @@ -67,7 +67,7 @@ string log(const Request& req, const Response& res) s += "================================\n"; - snprintf(buf, sizeof(buf), "%s %s %s", req.method.c_str(), req.path.c_str(), req.version.c_str()); + snprintf(buf, sizeof(buf), "%s %s %s", req.method.c_str(), req.version.c_str(), req.path.c_str()); s += buf; string query; From 4d7cee81eb106c502738b8a9980422a93dba148a Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 23 Sep 2018 12:02:17 -0400 Subject: [PATCH 101/118] Fix #95 --- httplib.h | 36 ++++++++++++++++++++++++------------ test/test.cc | 12 ++++++++++++ 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/httplib.h b/httplib.h index fd6049b..ba37d16 100644 --- a/httplib.h +++ b/httplib.h @@ -746,7 +746,13 @@ inline const char* status_message(int status) } } -inline const char* get_header_value(const Headers& headers, const char* key, const char* def) +inline bool has_header(const Headers& headers, const char* key) +{ + return headers.find(key) != headers.end(); +} + +inline const char* get_header_value( + const Headers& headers, const char* key, const char* def = nullptr) { auto it = headers.find(key); if (it != headers.end()) { @@ -755,7 +761,7 @@ inline const char* get_header_value(const Headers& headers, const char* key, con return def; } -inline int get_header_value_int(const Headers& headers, const char* key, int def) +inline int get_header_value_int(const Headers& headers, const char* key, int def = 0) { auto it = headers.find(key); if (it != headers.end()) { @@ -877,20 +883,22 @@ inline bool read_content_chunked(Stream& strm, std::string& out) template bool read_content(Stream& strm, T& x, Progress progress = Progress()) { - auto len = get_header_value_int(x.headers, "Content-Length", 0); - - if (len) { + if (has_header(x.headers, "Content-Length")) { + auto len = get_header_value_int(x.headers, "Content-Length", 0); + if (len == 0) { + const auto& encoding = get_header_value(x.headers, "Transfer-Encoding", ""); + if (!strcasecmp(encoding, "chunked")) { + return read_content_chunked(strm, x.body); + } + } return read_content_with_length(strm, x.body, len, progress); } else { const auto& encoding = get_header_value(x.headers, "Transfer-Encoding", ""); - if (!strcasecmp(encoding, "chunked")) { return read_content_chunked(strm, x.body); - } else { - return read_content_without_length(strm, x.body); } + return read_content_without_length(strm, x.body); } - return true; } @@ -1301,7 +1309,7 @@ inline std::pair make_range_header(uint64_t value, Arg // Request implementation inline bool Request::has_header(const char* key) const { - return headers.find(key) != headers.end(); + return detail::has_header(headers, key); } inline std::string Request::get_header_value(const char* key) const @@ -1578,7 +1586,7 @@ inline void Server::write_response(Stream& strm, bool last_connection, const Req req.get_header_value("Connection") == "close") { res.set_header("Connection", "close"); } - + if (!last_connection && req.get_header_value("Connection") == "Keep-Alive") { res.set_header("Connection", "Keep-Alive"); @@ -1988,7 +1996,11 @@ inline void Client::write_request(Stream& strm, Request& req) req.set_header("Connection", "close"); // } - if (!req.body.empty()) { + if (req.body.empty()) { + if (req.method == "POST" || req.method == "PUT") { + req.set_header("Content-Length", "0"); + } + } else { if (!req.has_header("Content-Type")) { req.set_header("Content-Type", "text/plain"); } diff --git a/test/test.cc b/test/test.cc index 6169b60..66b8c45 100644 --- a/test/test.cc +++ b/test/test.cc @@ -398,6 +398,10 @@ protected: EXPECT_EQ(0u, file.length); } }) + .Post("/empty", [&](const Request& req, Response& res) { + EXPECT_EQ(req.body, ""); + res.set_content("empty", "text/plain"); + }) .Put("/put", [&](const Request& req, Response& res) { EXPECT_EQ(req.body, "PUT"); res.set_content(req.body, "text/plain"); @@ -560,6 +564,14 @@ TEST_F(ServerTest, PostMethod2) ASSERT_EQ("coder", res->body); } +TEST_F(ServerTest, PostEmptyContent) +{ + auto res = cli_.Post("/empty", "", "text/plain"); + ASSERT_TRUE(res != nullptr); + ASSERT_EQ(200, res->status); + ASSERT_EQ("empty", res->body); +} + TEST_F(ServerTest, GetMethodDir) { auto res = cli_.Get("/dir/"); From abf79d5a38c413ea747fdb5c5ccd1b541af05332 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 23 Sep 2018 12:32:26 -0400 Subject: [PATCH 102/118] Code cleanup --- example/server.cc | 2 +- httplib.h | 10 ++++------ 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/example/server.cc b/example/server.cc index 4adcb57..469e02f 100644 --- a/example/server.cc +++ b/example/server.cc @@ -96,7 +96,7 @@ int main(void) res.set_content(dump_headers(req.headers), "text/plain"); }); - svr.Get("/stop", [&](const Request& /*req*/, Response& res) { + svr.Get("/stop", [&](const Request& /*req*/, Response& /*res*/) { svr.stop(); }); diff --git a/httplib.h b/httplib.h index ba37d16..f3115f9 100644 --- a/httplib.h +++ b/httplib.h @@ -1828,9 +1828,7 @@ inline bool Server::process_request(Stream& strm, bool last_connection, bool& co return true; } - auto ret = true; if (req.get_header_value("Connection") == "close") { - // ret = false; connection_close = true; } @@ -1841,7 +1839,7 @@ inline bool Server::process_request(Stream& strm, bool last_connection, bool& co if (!detail::read_content(strm, req)) { res.status = 400; write_response(strm, last_connection, req, res); - return ret; + return true; } const auto& content_type = req.get_header_value("Content-Type"); @@ -1852,7 +1850,7 @@ inline bool Server::process_request(Stream& strm, bool last_connection, bool& co #else res.status = 415; write_response(strm, last_connection, req, res); - return ret; + return true; #endif } @@ -1864,7 +1862,7 @@ inline bool Server::process_request(Stream& strm, bool last_connection, bool& co !detail::parse_multipart_formdata(boundary, req.body, req.files)) { res.status = 400; write_response(strm, last_connection, req, res); - return ret; + return true; } } } @@ -1878,7 +1876,7 @@ inline bool Server::process_request(Stream& strm, bool last_connection, bool& co } write_response(strm, last_connection, req, res); - return ret; + return true; } inline bool Server::is_valid() const From d32eee76274d9c8f7e3c6b690b3de53ff8445e81 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 30 Sep 2018 08:40:31 -0400 Subject: [PATCH 103/118] Fix #96 --- httplib.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/httplib.h b/httplib.h index f3115f9..142ae54 100644 --- a/httplib.h +++ b/httplib.h @@ -923,10 +923,11 @@ inline std::string encode_url(const std::string& s) case ':': result += "%3A"; break; case ';': result += "%3B"; break; default: - if (s[i] < 0) { + auto c = static_cast(s[i]); + if (c >= 0x80) { result += '%'; char hex[4]; - size_t len = snprintf(hex, sizeof(hex) - 1, "%02X", (unsigned char)s[i]); + size_t len = snprintf(hex, sizeof(hex) - 1, "%02X", c); assert(len == 2); result.append(hex, len); } else { From d0c5c66bd65e273faf38a5f6adf40c8021c9605e Mon Sep 17 00:00:00 2001 From: Duncan Ogilvie Date: Sun, 14 Oct 2018 12:39:51 +0200 Subject: [PATCH 104/118] Fix undefined behavior + make some ifdefs more readable --- httplib.h | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/httplib.h b/httplib.h index 142ae54..5515292 100644 --- a/httplib.h +++ b/httplib.h @@ -5,38 +5,39 @@ // MIT License // -#ifndef _CPPHTTPLIB_HTTPLIB_H_ -#define _CPPHTTPLIB_HTTPLIB_H_ +#ifndef CPPHTTPLIB_HTTPLIB_H +#define CPPHTTPLIB_HTTPLIB_H #ifdef _WIN32 #ifndef _CRT_SECURE_NO_WARNINGS #define _CRT_SECURE_NO_WARNINGS -#endif +#endif //_CRT_SECURE_NO_WARNINGS + #ifndef _CRT_NONSTDC_NO_DEPRECATE #define _CRT_NONSTDC_NO_DEPRECATE -#endif +#endif //_CRT_NONSTDC_NO_DEPRECATE #if defined(_MSC_VER) && _MSC_VER < 1900 #define snprintf _snprintf_s -#endif +#endif // _MSC_VER #ifndef S_ISREG #define S_ISREG(m) (((m)&S_IFREG)==S_IFREG) -#endif +#endif //S_ISREG + #ifndef S_ISDIR #define S_ISDIR(m) (((m)&S_IFDIR)==S_IFDIR) -#endif +#endif //S_ISDIR + +#define NOMINMAX #include #include #include -#undef min -#undef max - #ifndef strcasecmp #define strcasecmp _stricmp -#endif +#endif //strcasecmp typedef SOCKET socket_t; #else @@ -52,7 +53,7 @@ typedef SOCKET socket_t; typedef int socket_t; #define INVALID_SOCKET (-1) -#endif +#endif //_WIN32 #include #include @@ -2390,6 +2391,6 @@ inline bool SSLClient::read_and_close_socket(socket_t sock, Request& req, Respon } // namespace httplib -#endif +#endif //CPPHTTPLIB_HTTPLIB_H // vim: et ts=4 sw=4 cin cino={1s ff=unix From f88ab8ad4c0d3ee1bf2a5c2834b813e887b0564a Mon Sep 17 00:00:00 2001 From: Duncan Ogilvie Date: Sun, 14 Oct 2018 16:47:13 +0200 Subject: [PATCH 105/118] travis and appveyor configuration --- .gitignore | 1 + .travis.yml | 12 ++++++++++++ appveyor.yml | 9 +++++++++ 3 files changed, 22 insertions(+) create mode 100644 .travis.yml create mode 100644 appveyor.yml diff --git a/.gitignore b/.gitignore index 08f6383..e91bf78 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,4 @@ Release ipch *.dSYM .* +!/.travis.yml \ No newline at end of file diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..101b7a0 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,12 @@ +# Environment +language: cpp +os: osx + +# Compiler selection +compiler: + - clang + +# Build/test steps +script: + - cd ${TRAVIS_BUILD_DIR}/test + - make all \ No newline at end of file diff --git a/appveyor.yml b/appveyor.yml new file mode 100644 index 0000000..0b3bc9e --- /dev/null +++ b/appveyor.yml @@ -0,0 +1,9 @@ +version: 1.0.{build} +image: Visual Studio 2017 +build_script: +- cmd: >- + cd test + + msbuild.exe test.sln /verbosity:minimal /t:Build /p:Configuration=Debug;Platform=Win32 +test_script: +- cmd: Debug\test.exe \ No newline at end of file From 49c82c9c508b7caa076de956caa075942421c6a4 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 28 Oct 2018 16:15:22 +0900 Subject: [PATCH 106/118] Fix #97. (Thanks to DJm00n.) --- httplib.h | 74 ++++++++++++++++++++++++++++++++++++++++++++------- test/Makefile | 2 +- 2 files changed, 65 insertions(+), 11 deletions(-) diff --git a/httplib.h b/httplib.h index 5515292..73267f4 100644 --- a/httplib.h +++ b/httplib.h @@ -166,7 +166,7 @@ public: virtual int read(char* ptr, size_t size) = 0; virtual int write(const char* ptr, size_t size1) = 0; virtual int write(const char* ptr) = 0; - virtual std::string get_remote_addr() = 0; + virtual std::string get_remote_addr() const = 0; template void write_format(const char* fmt, const Args& ...args); @@ -180,12 +180,28 @@ public: virtual int read(char* ptr, size_t size); virtual int write(const char* ptr, size_t size); virtual int write(const char* ptr); - virtual std::string get_remote_addr(); + virtual std::string get_remote_addr() const; private: socket_t sock_; }; +class BufferStream : public Stream { +public: + BufferStream() {} + virtual ~BufferStream() {} + + virtual int read(char* ptr, size_t size); + virtual int write(const char* ptr, size_t size); + virtual int write(const char* ptr); + virtual std::string get_remote_addr() const; + + const std::string& get_buffer() const; + +private: + std::string buffer; +}; + class Server { public: typedef std::function Handler; @@ -315,7 +331,7 @@ public: virtual int read(char* ptr, size_t size); virtual int write(const char* ptr, size_t size); virtual int write(const char* ptr); - virtual std::string get_remote_addr(); + virtual std::string get_remote_addr() const; private: socket_t sock_; @@ -1441,10 +1457,42 @@ inline int SocketStream::write(const char* ptr) return write(ptr, strlen(ptr)); } -inline std::string SocketStream::get_remote_addr() { +inline std::string SocketStream::get_remote_addr() const { return detail::get_remote_addr(sock_); } +// Buffer stream implementation +inline int BufferStream::read(char* ptr, size_t size) +{ +#ifdef _WIN32 + return static_cast(buffer._Copy_s(ptr, size, size)); +#else + return static_cast(buffer.copy(ptr, size)); +#endif +} + +inline int BufferStream::write(const char* ptr, size_t size) +{ + buffer.append(ptr, size); + return static_cast(size); +} + +inline int BufferStream::write(const char* ptr) +{ + size_t size = strlen(ptr); + buffer.append(ptr, size); + return static_cast(size); +} + +inline std::string BufferStream::get_remote_addr() const { + return ""; +} + +inline const std::string& BufferStream::get_buffer() const { + return buffer; +} + + // HTTP server implementation inline Server::Server() : keep_alive_max_count_(5) @@ -1973,10 +2021,12 @@ inline bool Client::send(Request& req, Response& res) inline void Client::write_request(Stream& strm, Request& req) { - auto path = detail::encode_url(req.path); + BufferStream bstrm; // Request line - strm.write_format("%s %s HTTP/1.1\r\n", + auto path = detail::encode_url(req.path); + + bstrm.write_format("%s %s HTTP/1.1\r\n", req.method.c_str(), path.c_str()); @@ -2009,17 +2059,21 @@ inline void Client::write_request(Stream& strm, Request& req) req.set_header("Content-Length", length.c_str()); } - detail::write_headers(strm, req); + detail::write_headers(bstrm, req); // Body if (!req.body.empty()) { if (req.get_header_value("Content-Type") == "application/x-www-form-urlencoded") { auto str = detail::encode_url(req.body); - strm.write(str.c_str(), str.size()); + bstrm.write(str.c_str(), str.size()); } else { - strm.write(req.body.c_str(), req.body.size()); + bstrm.write(req.body.c_str(), req.body.size()); } } + + // Flush buffer + auto& data = bstrm.get_buffer(); + strm.write(data.data(), data.size()); } inline bool Client::process_request(Stream& strm, Request& req, Response& res, bool& connection_close) @@ -2303,7 +2357,7 @@ inline int SSLSocketStream::write(const char* ptr) return write(ptr, strlen(ptr)); } -inline std::string SSLSocketStream::get_remote_addr() { +inline std::string SSLSocketStream::get_remote_addr() const { return detail::get_remote_addr(sock_); } diff --git a/test/Makefile b/test/Makefile index 2034063..0a72252 100644 --- a/test/Makefile +++ b/test/Makefile @@ -2,7 +2,7 @@ CC = clang++ #CC = g++ -CFLAGS = -ggdb -O0 -std=c++11 -DGTEST_USE_OWN_TR1_TUPLE -I.. -I. -Wall -Wextra +CFLAGS = -ggdb -O0 -std=c++11 -DGTEST_USE_OWN_TR1_TUPLE -I.. -I. -Wall -Wextra -Wtype-limits OPENSSL_SUPPORT = -DCPPHTTPLIB_OPENSSL_SUPPORT -I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib -lssl -lcrypto ZLIB_SUPPORT = -DCPPHTTPLIB_ZLIB_SUPPORT -lz From c4f9062552e1ac74b8fca08466b11b08a76bd6b8 Mon Sep 17 00:00:00 2001 From: Geraldo Intmain Date: Mon, 29 Oct 2018 20:46:11 -0300 Subject: [PATCH 107/118] Encode space and plus characters --- httplib.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 73267f4..6c6ad0e 100644 --- a/httplib.h +++ b/httplib.h @@ -934,7 +934,8 @@ inline std::string encode_url(const std::string& s) for (auto i = 0; s[i]; i++) { switch (s[i]) { - case ' ': result += "+"; break; + case ' ': result += "%20"; break; + case '+': result += "%2B"; break; case '\'': result += "%27"; break; case ',': result += "%2C"; break; case ':': result += "%3A"; break; From bfd354e695fdd6ecc89f941cf95e4ec43da4c81e Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 8 Nov 2018 11:46:53 +0900 Subject: [PATCH 108/118] Updated README. fix #102 --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 2dc38fa..16f8be5 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,13 @@ int main(void) `Post`, `Put`, `Delete` and `Options` methods are also supported. +### Bind a socket to multiple interfaces and any available port + +```cpp +svr.bind_to_any_port("0.0.0.0"); +svr.listen_after_bind(); +``` + ### Method Chain ```cpp From 903b23ea5a9c275231603415aeaf96212a775adb Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 9 Nov 2018 23:30:58 -0500 Subject: [PATCH 109/118] Updated README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 16f8be5..9e106a7 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ int main(void) ### Bind a socket to multiple interfaces and any available port ```cpp -svr.bind_to_any_port("0.0.0.0"); +int port = svr.bind_to_any_port("0.0.0.0"); svr.listen_after_bind(); ``` From 76ea8dd560cddb079c560bd6766b5657c1f143b6 Mon Sep 17 00:00:00 2001 From: yhirose Date: Wed, 14 Nov 2018 17:35:27 -0500 Subject: [PATCH 110/118] Added LongQueryValue test --- test/test.cc | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/test.cc b/test/test.cc index 66b8c45..f29c7d3 100644 --- a/test/test.cc +++ b/test/test.cc @@ -20,6 +20,9 @@ using namespace httplib; const char* HOST = "localhost"; const int PORT = 1234; +const string LONG_QUERY_VALUE = string(25000, '@'); +const string LONG_QUERY_URL = "/long-query-value?key=" + LONG_QUERY_VALUE; + #ifdef _WIN32 TEST(StartupTest, WSAStartup) { @@ -417,6 +420,11 @@ protected: EXPECT_EQ("bbb", req.get_param_value("aaa")); EXPECT_EQ("ddd", req.get_param_value("ccc")); }) + .Get("/long-query-value", [&](const Request& req, Response& /*res*/) { + EXPECT_EQ(LONG_QUERY_URL, req.target); + EXPECT_EQ(LONG_QUERY_VALUE, req.get_param_value("key")); + }) + #ifdef CPPHTTPLIB_ZLIB_SUPPORT .Get("/gzip", [&](const Request& /*req*/, Response& res) { res.set_content("1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890", "text/plain"); @@ -682,6 +690,14 @@ TEST_F(ServerTest, LongHeader) EXPECT_EQ(200, res->status); } +TEST_F(ServerTest, LongQueryValue) +{ + auto res = cli_.Get(LONG_QUERY_URL.c_str()); + + ASSERT_TRUE(res != nullptr); + EXPECT_EQ(200, res->status); +} + TEST_F(ServerTest, TooLongHeader) { Request req; From 5ad4311fb0257c1ced9d2f8e4fe32b0c478120da Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 22 Nov 2018 20:50:54 -0500 Subject: [PATCH 111/118] fix #109 --- httplib.h | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/httplib.h b/httplib.h index 6c6ad0e..e494b84 100644 --- a/httplib.h +++ b/httplib.h @@ -1643,7 +1643,16 @@ inline void Server::write_response(Stream& strm, bool last_connection, const Req res.set_header("Connection", "Keep-Alive"); } - if (!res.body.empty()) { + if (res.body.empty()) { + if (!res.has_header("Content-Length")) { + if (res.streamcb) { + // Streamed response + res.set_header("Transfer-Encoding", "chunked"); + } else { + res.set_header("Content-Length", "0"); + } + } + } else { #ifdef CPPHTTPLIB_ZLIB_SUPPORT // TODO: 'Accpet-Encoding' has gzip, not gzip;q=0 const auto& encodings = req.get_header_value("Accept-Encoding"); @@ -1660,11 +1669,6 @@ inline void Server::write_response(Stream& strm, bool last_connection, const Req auto length = std::to_string(res.body.size()); res.set_header("Content-Length", length.c_str()); - } else if (res.streamcb) { - // Streamed response - bool chunked_response = !res.has_header("Content-Length"); - if (chunked_response) - res.set_header("Transfer-Encoding", "chunked"); } detail::write_headers(strm, res); From 86b3dfc480b5700e379fe7ecd2573445330d975b Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 30 Nov 2018 21:18:35 -0500 Subject: [PATCH 112/118] fix #110 --- httplib.h | 7 +------ test/test.cc | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/httplib.h b/httplib.h index e494b84..878f16d 100644 --- a/httplib.h +++ b/httplib.h @@ -2068,12 +2068,7 @@ inline void Client::write_request(Stream& strm, Request& req) // Body if (!req.body.empty()) { - if (req.get_header_value("Content-Type") == "application/x-www-form-urlencoded") { - auto str = detail::encode_url(req.body); - bstrm.write(str.c_str(), str.size()); - } else { - bstrm.write(req.body.c_str(), req.body.size()); - } + bstrm.write(req.body.c_str(), req.body.size()); } // Flush buffer diff --git a/test/test.cc b/test/test.cc index f29c7d3..5487db8 100644 --- a/test/test.cc +++ b/test/test.cc @@ -23,6 +23,8 @@ const int PORT = 1234; const string LONG_QUERY_VALUE = string(25000, '@'); const string LONG_QUERY_URL = "/long-query-value?key=" + LONG_QUERY_VALUE; +const std::string JSON_DATA = "{\"hello\":\"world\"}"; + #ifdef _WIN32 TEST(StartupTest, WSAStartup) { @@ -345,6 +347,12 @@ protected: res.status = 404; } }) + .Post("/x-www-form-urlencoded-json", [&](const Request& req, Response& res) { + auto json = req.get_param_value("json"); + ASSERT_EQ(JSON_DATA, json); + res.set_content(json, "appliation/json"); + res.status = 200; + }) .Get("/streamedchunked", [&](const Request& /*req*/, Response& res) { res.streamcb = [] (uint64_t offset) { if (offset < 3) @@ -572,6 +580,18 @@ TEST_F(ServerTest, PostMethod2) ASSERT_EQ("coder", res->body); } +TEST_F(ServerTest, PostWwwFormUrlEncodedJson) +{ + Params params; + params.emplace("json", JSON_DATA); + + auto res = cli_.Post("/x-www-form-urlencoded-json", params); + + ASSERT_TRUE(res != nullptr); + ASSERT_EQ(200, res->status); + ASSERT_EQ(JSON_DATA, res->body); +} + TEST_F(ServerTest, PostEmptyContent) { auto res = cli_.Post("/empty", "", "text/plain"); From d0090b158f4d5da571271c4b04252b3b8aef0155 Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 13 Dec 2018 19:37:44 -0500 Subject: [PATCH 113/118] fix #112 --- httplib.h | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/httplib.h b/httplib.h index 878f16d..7d4c7a0 100644 --- a/httplib.h +++ b/httplib.h @@ -320,6 +320,7 @@ private: void write_request(Stream& strm, Request& req); virtual bool read_and_close_socket(socket_t sock, Request& req, Response& res); + virtual bool is_ssl() const; }; #ifdef CPPHTTPLIB_OPENSSL_SUPPORT @@ -358,7 +359,7 @@ class SSLClient : public Client { public: SSLClient( const char* host, - int port = 80, + int port = 443, time_t timeout_sec = 300); virtual ~SSLClient(); @@ -367,6 +368,7 @@ public: private: virtual bool read_and_close_socket(socket_t sock, Request& req, Response& res); + virtual bool is_ssl() const; SSL_CTX* ctx_; std::mutex ctx_mutex_; @@ -2036,7 +2038,19 @@ inline void Client::write_request(Stream& strm, Request& req) path.c_str()); // Headers - req.set_header("Host", host_and_port_.c_str()); + if (is_ssl()) { + if (port_ == 443) { + req.set_header("Host", host_.c_str()); + } else { + req.set_header("Host", host_and_port_.c_str()); + } + } else { + if (port_ == 80) { + req.set_header("Host", host_.c_str()); + } else { + req.set_header("Host", host_and_port_.c_str()); + } + } if (!req.has_header("Accept")) { req.set_header("Accept", "*/*"); @@ -2118,6 +2132,11 @@ inline bool Client::read_and_close_socket(socket_t sock, Request& req, Response& }); } +inline bool Client::is_ssl() const +{ + return false; +} + inline std::shared_ptr Client::Get(const char* path, Progress progress) { return Get(path, Headers(), progress); @@ -2441,6 +2460,11 @@ inline bool SSLClient::read_and_close_socket(socket_t sock, Request& req, Respon return process_request(strm, req, res, connection_close); }); } + +inline bool SSLClient::is_ssl() const +{ + return true; +} #endif } // namespace httplib From b5927aec123351dcf796e1fba8a6a1805d294cbe Mon Sep 17 00:00:00 2001 From: yhirose Date: Mon, 17 Dec 2018 21:07:38 -0500 Subject: [PATCH 114/118] fix #116 --- httplib.h | 45 ++++++++++++++++++++++++++++++++++----------- test/test.cc | 27 ++++++++++++++++++++------- 2 files changed, 54 insertions(+), 18 deletions(-) diff --git a/httplib.h b/httplib.h index 7d4c7a0..7576cee 100644 --- a/httplib.h +++ b/httplib.h @@ -132,11 +132,13 @@ struct Request { Progress progress; bool has_header(const char* key) const; - std::string get_header_value(const char* key) const; + std::string get_header_value(const char* key, size_t id = 0) const; + size_t get_header_value_count(const char* key) const; void set_header(const char* key, const char* val); bool has_param(const char* key) const; - std::string get_param_value(const char* key) const; + std::string get_param_value(const char* key, size_t id = 0) const; + size_t get_param_value_count(const char* key) const; bool has_file(const char* key) const; MultipartFile get_file_value(const char* key) const; @@ -150,7 +152,8 @@ struct Response { std::function streamcb; bool has_header(const char* key) const; - std::string get_header_value(const char* key) const; + std::string get_header_value(const char* key, size_t id = 0) const; + size_t get_header_value_count(const char* key) const; void set_header(const char* key, const char* val); void set_redirect(const char* uri); @@ -771,9 +774,10 @@ inline bool has_header(const Headers& headers, const char* key) } inline const char* get_header_value( - const Headers& headers, const char* key, const char* def = nullptr) + const Headers& headers, const char* key, size_t id = 0, const char* def = nullptr) { auto it = headers.find(key); + std::advance(it, id); if (it != headers.end()) { return it->second.c_str(); } @@ -905,14 +909,14 @@ bool read_content(Stream& strm, T& x, Progress progress = Progress()) if (has_header(x.headers, "Content-Length")) { auto len = get_header_value_int(x.headers, "Content-Length", 0); if (len == 0) { - const auto& encoding = get_header_value(x.headers, "Transfer-Encoding", ""); + const auto& encoding = get_header_value(x.headers, "Transfer-Encoding", 0, ""); if (!strcasecmp(encoding, "chunked")) { return read_content_chunked(strm, x.body); } } return read_content_with_length(strm, x.body, len, progress); } else { - const auto& encoding = get_header_value(x.headers, "Transfer-Encoding", ""); + const auto& encoding = get_header_value(x.headers, "Transfer-Encoding", 0, ""); if (!strcasecmp(encoding, "chunked")) { return read_content_chunked(strm, x.body); } @@ -1333,9 +1337,15 @@ inline bool Request::has_header(const char* key) const return detail::has_header(headers, key); } -inline std::string Request::get_header_value(const char* key) const +inline std::string Request::get_header_value(const char* key, size_t id) const { - return detail::get_header_value(headers, key, ""); + return detail::get_header_value(headers, key, id, ""); +} + +inline size_t Request::get_header_value_count(const char* key) const +{ + auto r = headers.equal_range(key); + return std::distance(r.first, r.second); } inline void Request::set_header(const char* key, const char* val) @@ -1348,15 +1358,22 @@ inline bool Request::has_param(const char* key) const return params.find(key) != params.end(); } -inline std::string Request::get_param_value(const char* key) const +inline std::string Request::get_param_value(const char* key, size_t id) const { auto it = params.find(key); + std::advance(it, id); if (it != params.end()) { return it->second; } return std::string(); } +inline size_t Request::get_param_value_count(const char* key) const +{ + auto r = params.equal_range(key); + return std::distance(r.first, r.second); +} + inline bool Request::has_file(const char* key) const { return files.find(key) != files.end(); @@ -1377,9 +1394,15 @@ inline bool Response::has_header(const char* key) const return headers.find(key) != headers.end(); } -inline std::string Response::get_header_value(const char* key) const +inline std::string Response::get_header_value(const char* key, size_t id) const { - return detail::get_header_value(headers, key, ""); + return detail::get_header_value(headers, key, id, ""); +} + +inline size_t Response::get_header_value_count(const char* key) const +{ + auto r = headers.equal_range(key); + return std::distance(r.first, r.second); } inline void Response::set_header(const char* key, const char* val) diff --git a/test/test.cc b/test/test.cc index 5487db8..d87f104 100644 --- a/test/test.cc +++ b/test/test.cc @@ -71,7 +71,7 @@ TEST(ParseQueryTest, ParseQueryString) TEST(GetHeaderValueTest, DefaultValue) { Headers headers = {{"Dummy","Dummy"}}; - auto val = detail::get_header_value(headers, "Content-Type", "text/plain"); + auto val = detail::get_header_value(headers, "Content-Type", 0, "text/plain"); EXPECT_STREQ("text/plain", val); } @@ -85,7 +85,7 @@ TEST(GetHeaderValueTest, DefaultValueInt) TEST(GetHeaderValueTest, RegularValue) { Headers headers = {{"Content-Type", "text/html"}, {"Dummy", "Dummy"}}; - auto val = detail::get_header_value(headers, "Content-Type", "text/plain"); + auto val = detail::get_header_value(headers, "Content-Type", 0, "text/plain"); EXPECT_STREQ("text/html", val); } @@ -100,25 +100,25 @@ TEST(GetHeaderValueTest, Range) { { Headers headers = { make_range_header(1) }; - auto val = detail::get_header_value(headers, "Range", 0); + auto val = detail::get_header_value(headers, "Range", 0, 0); EXPECT_STREQ("bytes=1-", val); } { Headers headers = { make_range_header(1, 10) }; - auto val = detail::get_header_value(headers, "Range", 0); + auto val = detail::get_header_value(headers, "Range", 0, 0); EXPECT_STREQ("bytes=1-10", val); } { Headers headers = { make_range_header(1, 10, 100) }; - auto val = detail::get_header_value(headers, "Range", 0); + auto val = detail::get_header_value(headers, "Range", 0, 0); EXPECT_STREQ("bytes=1-10, 100-", val); } { Headers headers = { make_range_header(1, 10, 100, 200) }; - auto val = detail::get_header_value(headers, "Range", 0); + auto val = detail::get_header_value(headers, "Range", 0, 0); EXPECT_STREQ("bytes=1-10, 100-200", val); } } @@ -432,7 +432,12 @@ protected: EXPECT_EQ(LONG_QUERY_URL, req.target); EXPECT_EQ(LONG_QUERY_VALUE, req.get_param_value("key")); }) - + .Get("/array-param", [&](const Request& req, Response& /*res*/) { + EXPECT_EQ(3u, req.get_param_value_count("array")); + EXPECT_EQ("value1", req.get_param_value("array", 0)); + EXPECT_EQ("value2", req.get_param_value("array", 1)); + EXPECT_EQ("value3", req.get_param_value("array", 2)); + }) #ifdef CPPHTTPLIB_ZLIB_SUPPORT .Get("/gzip", [&](const Request& /*req*/, Response& res) { res.set_content("1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890", "text/plain"); @@ -497,6 +502,7 @@ TEST_F(ServerTest, GetMethod200) EXPECT_EQ("HTTP/1.1", res->version); EXPECT_EQ(200, res->status); EXPECT_EQ("text/plain", res->get_header_value("Content-Type")); + EXPECT_EQ(1, res->get_header_value_count("Content-Type")); EXPECT_EQ("close", res->get_header_value("Connection")); EXPECT_EQ("Hello World!", res->body); } @@ -936,6 +942,13 @@ TEST_F(ServerTest, URL) EXPECT_EQ(200, res->status); } +TEST_F(ServerTest, ArrayParam) +{ + auto res = cli_.Get("/array-param?array=value1&array=value2&array=value3"); + ASSERT_TRUE(res != nullptr); + EXPECT_EQ(200, res->status); +} + #ifdef CPPHTTPLIB_ZLIB_SUPPORT TEST_F(ServerTest, Gzip) { From a8b202a8b9c33b547e6bd0022ee75bd8de321584 Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 11 Jan 2019 20:31:06 -0500 Subject: [PATCH 115/118] Added build status badges --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 9e106a7..2022717 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,9 @@ cpp-httplib =========== +[![Build Status](https://travis-ci.org/yhirose/cpp-httplib.svg?branch=master)](https://travis-ci.org/yhirose/cpp-httplib) +[![Bulid Status](https://ci.appveyor.com/api/projects/status/github/yhirose/cpp-httplib?branch=master&svg=true)](https://ci.appveyor.com/project/yhirose/cpp-httplib) + A C++11 header-only HTTP library. It's extremely easy to setup. Just include **httplib.h** file in your code! From 8d908fadb6b17e66b7b2718d3b89b367d2f835d1 Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 15 Jan 2019 08:08:34 -0500 Subject: [PATCH 116/118] Fixed #120 --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 7576cee..521657a 100644 --- a/httplib.h +++ b/httplib.h @@ -1490,7 +1490,7 @@ inline std::string SocketStream::get_remote_addr() const { // Buffer stream implementation inline int BufferStream::read(char* ptr, size_t size) { -#ifdef _WIN32 +#if defined(_MSC_VER) && _MSC_VER < 1900 return static_cast(buffer._Copy_s(ptr, size, size)); #else return static_cast(buffer.copy(ptr, size)); From 61c19052f0b80e5b0fbfdfd7127591180007b1dc Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 15 Jan 2019 08:24:17 -0500 Subject: [PATCH 117/118] Fixed #118 --- README.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 2022717..fa59575 100644 --- a/README.md +++ b/README.md @@ -225,7 +225,12 @@ The server applies gzip compression to the following MIME type contents: * application/xml * application/xhtml+xml +NOTE +---- + +g++ 4.8 cannot build this library since `` in g++4.8 is [broken](https://stackoverflow.com/questions/12530406/is-gcc-4-8-or-earlier-buggy-about-regular-expressions). + License ------- -MIT license (© 2018 Yuji Hirose) +MIT license (© 2019 Yuji Hirose) From a72eef7fb4190e5519bd920d0d6180ab687de209 Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 29 Jan 2019 12:05:32 -0500 Subject: [PATCH 118/118] Fixed #123 --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 521657a..b0b5c88 100644 --- a/httplib.h +++ b/httplib.h @@ -2418,7 +2418,7 @@ inline SSLServer::SSLServer(const char* cert_path, const char* private_key_path) // SSL_CTX_set_tmp_ecdh(ctx_, ecdh); // EC_KEY_free(ecdh); - if (SSL_CTX_use_certificate_file(ctx_, cert_path, SSL_FILETYPE_PEM) != 1 || + if (SSL_CTX_use_certificate_chain_file(ctx_, cert_path) != 1 || SSL_CTX_use_PrivateKey_file(ctx_, private_key_path, SSL_FILETYPE_PEM) != 1) { SSL_CTX_free(ctx_); ctx_ = nullptr;