From 3b3828aaffe62479106caa3bd0efd0c686c96d0c Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 11 Oct 2012 23:52:34 -0400 Subject: [PATCH] Fixed unit test. Added URL encoding. --- example/server.cc | 4 + httplib.h | 229 +++++++++++++++++++++++++++++++++++++++------- test/test.cc | 178 ++++++++++++++++++++++++++++++----- 3 files changed, 353 insertions(+), 58 deletions(-) diff --git a/example/server.cc b/example/server.cc index ff93960..a0d882d 100644 --- a/example/server.cc +++ b/example/server.cc @@ -84,6 +84,10 @@ int main(void) c.response.set_content(dump_headers(c.request.headers), "text/plain"); }); + svr.get("/stop", [&](Connection& c) { + svr.stop(); + }); + svr.set_error_handler([](Connection& c) { const char* fmt = "

Error Status: %d

"; char buf[BUFSIZ]; diff --git a/httplib.h b/httplib.h index fc020aa..688662c 100644 --- a/httplib.h +++ b/httplib.h @@ -5,8 +5,8 @@ // The Boost Software License 1.0 // -#ifndef HTTPSVRKIT_H -#define HTTPSVRKIT_H +#ifndef _CPPHTTPLIB_HTTPSLIB_H_ +#define _CPPHTTPLIB_HTTPSLIB_H_ #ifdef _WIN32 #define _CRT_SECURE_NO_WARNINGS @@ -123,8 +123,8 @@ public: ~Client(); std::shared_ptr get(const char* url); - std::shared_ptr post( - const char* url, const std::string& body, const char* content_type); + std::shared_ptr post(const char* url, const std::string& body, const char* content_type); + std::shared_ptr post(const char* url, const Map& params); bool send(const Request& req, Response& res); @@ -161,8 +161,8 @@ inline void get_flie_pointers(int fd, FILE*& fp_read, FILE*& fp_write) { #ifdef _WIN32 int osfhandle = _open_osfhandle(fd, _O_RDONLY); - fp_read = fdopen(osfhandle, "rb"); - fp_write = fdopen(osfhandle, "wb"); + fp_read = _fdopen(osfhandle, "rb"); + fp_write = _fdopen(osfhandle, "wb"); #else fp_read = fdopen(fd, "rb"); fp_write = fdopen(fd, "wb"); @@ -216,13 +216,20 @@ inline socket_t create_server_socket(const char* host, int port) }); } -inline int shutdown_and_close_socket(socket_t sock) +inline int shutdown_socket(socket_t sock) +{ +#ifdef _WIN32 + return shutdown(sock, SD_BOTH); +#else + return shutdown(sock, SHUT_RDWR); +#endif +} + +inline int close_socket(socket_t sock) { #ifdef _WIN32 - shutdown(sock, SD_BOTH); return closesocket(sock); #else - shutdown(sock, SHUT_RDWR); return close(sock); #endif } @@ -335,20 +342,159 @@ inline void write_response(FILE* fp, const Response& res) } } +inline std::string encode_url(const std::string& s) +{ + std::string result; + + int i = 0; + while (s[i]) { + switch (s[i]) { + case ' ': result += "%20"; break; + case '\'': result += "%27"; break; + case ',': result += "%2C"; break; + case ':': result += "%3A"; break; + case ';': result += "%3B"; break; + default: + if (s[i] < 0) { + result += '%'; + char hex[4]; + size_t len = sprintf(hex, "%02X", (int)(unsigned char)s[i]); + assert(len == 2); + result.append(hex, len); + } else { + result += s[i]; + } + break; + } + i++; + } + + return result; +} + +inline bool is_hex(char c, int& v) +{ + if (0x20 <= c && isdigit(c)) { + v = c - '0'; + return true; + } else if ('A' <= c && c <= 'F') { + v = c - 'A' + 10; + return true; + } else if ('a' <= c && c <= 'f') { + v = c - 'a' + 10; + return true; + } + return false; +} + +inline int from_hex_to_i(const std::string& s, int i, int cnt, int& val) +{ + val = 0; + for (; s[i] && cnt; i++, cnt--) { + int v = 0; + if (is_hex(s[i], v)) { + val = val * 16 + v; + } else { + break; + } + } + return --i; +} + +size_t to_utf8(int code, char* buff) +{ + if (code < 0x0080) { + buff[0] = (uint8_t)(code & 0x7F); + return 1; + } else if (code < 0x0800) { + buff[0] = (uint8_t)(0xC0 | ((code >> 6) & 0x1F)); + buff[1] = (uint8_t)(0x80 | (code & 0x3F)); + return 2; + } else if (code < 0xD800) { + buff[0] = (uint8_t)(0xE0 | ((code >> 12) & 0xF)); + buff[1] = (uint8_t)(0x80 | ((code >> 6) & 0x3F)); + buff[2] = (uint8_t)(0x80 | (code & 0x3F)); + return 3; + } else if (code < 0xE000) { // D800 - DFFF is invalid... + assert(!"NOTREACHED"); + return 0; + } else if (code < 0x10000) { + buff[0] = (uint8_t)(0xE0 | ((code >> 12) & 0xF)); + buff[1] = (uint8_t)(0x80 | ((code >> 6) & 0x3F)); + buff[2] = (uint8_t)(0x80 | (code & 0x3F)); + return 3; + } else if (code < 0x110000) { + buff[0] = (uint8_t)(0xF0 | ((code >> 18) & 0x7)); + buff[1] = (uint8_t)(0x80 | ((code >> 12) & 0x3F)); + buff[2] = (uint8_t)(0x80 | ((code >> 6) & 0x3F)); + buff[3] = (uint8_t)(0x80 | (code & 0x3F)); + return 4; + } + + assert(!"NOTREACHED"); + // NOTREACHED +} + +inline std::string decode_url(const std::string& s) +{ + std::string result; + + 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]); + + 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); + } + } else { + // ASCII + int val = 0; + i = from_hex_to_i(s, i, 2, val); + result += (char)val; + } + } else if (s[i] == '+') { + result += ' '; + } else { + result += s[i]; + } + } + + return result; +} + inline void write_request(FILE* fp, const Request& req) { - fprintf(fp, "%s %s HTTP/1.0\r\n", req.method.c_str(), req.url.c_str()); + auto url = encode_url(req.url); + fprintf(fp, "%s %s HTTP/1.0\r\n", req.method.c_str(), url.c_str()); write_headers(fp, req); if (!req.body.empty()) { - fprintf(fp, "%s", req.body.c_str()); + if (req.has_header("application/x-www-form-urlencoded")) { + fprintf(fp, "%s", encode_url(req.body).c_str()); + } else { + fprintf(fp, "%s", req.body.c_str()); + } } } -inline void parse_query_text(const char* b, const char* e, Map& params) +inline void parse_query_text(const std::string& s, Map& params) { - split(b, e, '&', [&](const char* b, const char* e) { + split(&s[0], &s[s.size()], '&', [&](const char* b, const char* e) { std::string key; std::string val; split(b, e, '=', [&](const char* b, const char* e) { @@ -362,11 +508,6 @@ inline void parse_query_text(const char* b, const char* e, Map& params) }); } -inline void parse_query_text(const std::string& s, Map& params) -{ - parse_query_text(&s[0], &s[s.size()], params); -} - } // namespace detail // Request implementation @@ -439,12 +580,12 @@ inline Server::~Server() inline void Server::get(const char* pattern, Handler handler) { - get_handlers_.push_back(std::make_pair(pattern, handler)); + get_handlers_.push_back(std::make_pair(std::regex(pattern), handler)); } inline void Server::post(const char* pattern, Handler handler) { - post_handlers_.push_back(std::make_pair(pattern, handler)); + post_handlers_.push_back(std::make_pair(std::regex(pattern), handler)); } inline void Server::set_error_handler(Handler handler) @@ -464,29 +605,34 @@ inline bool Server::run() return false; } + auto ret = true; + for (;;) { socket_t sock = accept(svr_sock_, NULL, NULL); + if (sock == -1) { - if (svr_sock_ == -1) { - // The server socket was closed by user. - return true; + if (svr_sock_ != -1) { + detail::close_socket(svr_sock_); + ret = false; } else { - detail::shutdown_and_close_socket(svr_sock_); - return false; + ; // The server socket was closed by user. } + break; } // TODO: should be async process_request(sock); - detail::shutdown_and_close_socket(sock); + detail::shutdown_socket(sock); + detail::close_socket(sock); } - // NOTREACHED + return ret; } inline void Server::stop() { - detail::shutdown_and_close_socket(svr_sock_); + detail::shutdown_socket(svr_sock_); + detail::close_socket(svr_sock_); svr_sock_ = -1; } @@ -503,13 +649,12 @@ inline bool Server::read_request_line(FILE* fp, Request& req) std::cmatch m; if (std::regex_match(buf, m, re)) { req.method = std::string(m[1]); - req.url = std::string(m[2]); + req.url = detail::decode_url(m[2]); // Parse query text auto len = std::distance(m[3].first, m[3].second); if (len > 0) { - const auto& pos = m[3]; - detail::parse_query_text(pos.first, pos.second, req.params); + detail::parse_query_text(detail::decode_url(m[3]), req.params); } return true; @@ -560,7 +705,7 @@ inline void Server::process_request(socket_t sock) return; } if (c.request.get_header_value("Content-Type") == "application/x-www-form-urlencoded") { - detail::parse_query_text(c.request.body, c.request.params); + detail::parse_query_text(detail::decode_url(c.request.body), c.request.params); } } @@ -578,7 +723,6 @@ inline void Server::process_request(socket_t sock) } detail::write_response(fp_write, c.response); - fflush(fp_write); if (logger_) { @@ -643,7 +787,8 @@ inline bool Client::send(const Request& req, Response& res) return false; } - detail::shutdown_and_close_socket(sock); + detail::shutdown_socket(sock); + detail::close_socket(sock); return true; } @@ -673,6 +818,22 @@ inline std::shared_ptr Client::post( return send(req, *res) ? res : nullptr; } +inline std::shared_ptr Client::post( + const char* url, const Map& params) +{ + std::string query; + for (auto it = params.begin(); it != params.end(); ++it) { + if (it != params.begin()) { + query += "&"; + } + query += it->first; + query += "="; + query += it->second; + } + + return post(url, query, "application/x-www-form-urlencoded"); +} + } // namespace httplib #endif diff --git a/test/test.cc b/test/test.cc index ed23d35..5454f43 100644 --- a/test/test.cc +++ b/test/test.cc @@ -1,18 +1,98 @@ #include #include -#include +//#include #include +#ifdef _WIN32 +#include +#endif + using namespace std; using namespace httplib; +const char* HOST = "localhost"; +const int PORT = 8080; + +class thread +{ +public: + thread(std::function fn); + ~thread(); + + void join(); + +private: + thread(); + +#ifdef _WIN32 + HANDLE thread_; + static unsigned int __stdcall TreadFunc(void* arg); +#else + pthread_t thread_; + static void* TreadFunc(void* arg); +#endif + + static std::map> tasks_; +}; + +std::map> thread::tasks_; + +inline thread::thread(std::function fn) + : thread_(NULL) +{ + tasks_[this] = fn; +#ifdef _WIN32 + thread_ = (HANDLE)_beginthreadex(NULL, 0, TreadFunc, this, 0, NULL); +#else + pthread_create(&thread_, NULL, TreadFunc, this); +#endif +} + +inline thread::~thread() +{ +#ifdef _WIN32 + CloseHandle(thread_); +#endif +} + +inline void thread::join() +{ +#ifdef _WIN32 + ::WaitForSingleObject(thread_, INFINITE); +#else + pthread_join(thread_, NULL); +#endif +} + +#ifdef _WIN32 +unsigned int __stdcall thread::TreadFunc(void* arg) +#else +void* thread::TreadFunc(void* arg) +#endif +{ + thread* pThis = static_cast(arg); + tasks_[pThis](); + tasks_.erase(pThis); + + return 0; +} + +#ifdef _WIN32 +TEST(StartupTest, WSAStartup) +{ + WSADATA wsaData; + int ret = WSAStartup(0x0002, &wsaData); + ASSERT_EQ(0, ret); +} +#endif + TEST(SplitTest, ParseQueryString) { string s = "key1=val1&key2=val2&key3=val3"; map dic; - detail::split(&s[0], &s[s.size()], '&', [&](const char* b, const char* e) { + detail::split(s.c_str(), s.c_str() + s.size(), '&', [&](const char* b, const char* e) { string key, val; detail::split(b, e, '=', [&](const char* b, const char* e) { if (key.empty()) { @@ -34,7 +114,7 @@ TEST(ParseQueryTest, ParseQueryString) string s = "key1=val1&key2=val2&key3=val3"; map dic; - detail::parse_query_text(&s[0], &s[s.size()], dic); + detail::parse_query_text(s, dic); EXPECT_EQ("val1", dic["key1"]); EXPECT_EQ("val2", dic["key2"]); @@ -43,37 +123,47 @@ TEST(ParseQueryTest, ParseQueryString) TEST(SocketTest, OpenClose) { - socket_t sock = detail::create_server_socket("localhost", 1914); + socket_t sock = detail::create_server_socket(HOST, PORT); ASSERT_NE(-1, sock); - auto ret = detail::shutdown_and_close_socket(sock); + auto ret = detail::close_socket(sock); EXPECT_EQ(0, ret); } TEST(GetHeaderValueTest, DefaultValue) { - MultiMap map = {{"Dummy","Dummy"}}; + //MultiMap map = {{"Dummy","Dummy"}}; + MultiMap map; + map.insert(std::make_pair("Dummy", "Dummy")); auto val = detail::get_header_value_text(map, "Content-Type", "text/plain"); ASSERT_STREQ("text/plain", val); } TEST(GetHeaderValueTest, DefaultValueInt) { - MultiMap map = {{"Dummy","Dummy"}}; + //MultiMap map = {{"Dummy","Dummy"}}; + MultiMap map; + map.insert(std::make_pair("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"}}; + //MultiMap map = {{"Content-Type", "text/html"}, {"Dummy", "Dummy"}}; + MultiMap map; + map.insert(std::make_pair("Content-Type","text/html")); + map.insert(std::make_pair("Dummy", "Dummy")); auto val = detail::get_header_value_text(map, "Content-Type", "text/plain"); ASSERT_STREQ("text/html", val); } TEST(GetHeaderValueTest, RegularValueInt) { - MultiMap map = {{"Content-Length","100"}, {"Dummy", "Dummy"}}; + //MultiMap map = {{"Content-Length", "100"}, {"Dummy", "Dummy"}}; + MultiMap map; + map.insert(std::make_pair("Content-Length", "100")); + map.insert(std::make_pair("Dummy", "Dummy")); auto val = detail::get_header_value_int(map, "Content-Length", 0); EXPECT_EQ(100, val); } @@ -81,17 +171,18 @@ TEST(GetHeaderValueTest, RegularValueInt) class ServerTest : public ::testing::Test { protected: ServerTest() : svr_(HOST, PORT), cli_(HOST, PORT) { - persons_["john"] = "programmer"; } virtual void SetUp() { - svr_.get("/hi", [&](httplib::Connection& c) { + svr_.get("/hi", [&](Connection& c) { c.response.set_content("Hello World!", "text/plain"); }); + svr_.get("/", [&](httplib::Connection& c) { c.response.set_redirect("/hi"); }); - svr_.post("/person", [&](httplib::Connection& c) { + + svr_.post("/person", [&](Connection& c) { const auto& req = c.request; if (req.has_param("name") && req.has_param("note")) { persons_[req.params.at("name")] = req.params.at("note"); @@ -99,7 +190,8 @@ protected: c.response.status = 400; } }); - svr_.get("/person/(.*)", [&](httplib::Connection& c) { + + svr_.get("/person/(.*)", [&](Connection& c) { const auto& req = c.request; std::string name = req.matches[1]; if (persons_.find(name) != persons_.end()) { @@ -109,21 +201,30 @@ protected: c.response.status = 404; } }); - f_ = async([&](){ svr_.run(); }); + + svr_.get("/stop", [&](Connection& c) { + svr_.stop(); + }); + + persons_["john"] = "programmer"; + + //f_ = async([&](){ svr_.run(); }); + t_ = std::make_shared([&](){ svr_.run(); }); } virtual void TearDown() { - svr_.stop(); - f_.get(); - } + //svr_.stop(); // NOTE: This causes dead lock on Windows. + cli_.get("/stop"); - const char* HOST = "localhost"; - const int PORT = 1914; + //f_.get(); + t_->join(); + } std::map persons_; Server svr_; Client cli_; - std::future f_; + //std::future f_; + std::shared_ptr t_; }; TEST_F(ServerTest, GetMethod200) @@ -159,21 +260,50 @@ TEST_F(ServerTest, GetMethodPersonJohn) EXPECT_EQ("programmer", res->body); } -TEST_F(ServerTest, PostMethod) +TEST_F(ServerTest, PostMethod1) { - auto res = cli_.get("/person/john3"); + auto res = cli_.get("/person/john1"); ASSERT_TRUE(res != nullptr); ASSERT_EQ(404, res->status); - res = cli_.post("/person", "name=john3¬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/john3"); + res = cli_.get("/person/john1"); ASSERT_TRUE(res != nullptr); ASSERT_EQ(200, res->status); ASSERT_EQ("text/plain", res->get_header_value("Content-Type")); ASSERT_EQ("coder", res->body); } +TEST_F(ServerTest, PostMethod2) +{ + auto res = cli_.get("/person/john2"); + ASSERT_TRUE(res != nullptr); + ASSERT_EQ(404, res->status); + + Map params; + params["name"] = "john2"; + params["note"] = "coder"; + + res = cli_.post("/person", params); + ASSERT_TRUE(res != nullptr); + ASSERT_EQ(200, res->status); + + res = cli_.get("/person/john2"); + ASSERT_TRUE(res != nullptr); + ASSERT_EQ(200, res->status); + ASSERT_EQ("text/plain", res->get_header_value("Content-Type")); + ASSERT_EQ("coder", res->body); +} + +#ifdef _WIN32 +TEST(CleanupTest, WSACleanup) +{ + int ret = WSACleanup(); + ASSERT_EQ(0, ret); +} +#endif + // vim: et ts=4 sw=4 cin cino={1s ff=unix