Fixed unit test. Added URL encoding.

This commit is contained in:
yhirose 2012-10-11 23:52:34 -04:00
parent 487ad28a17
commit 3b3828aaff
3 changed files with 353 additions and 58 deletions

View file

@ -84,6 +84,10 @@ int main(void)
c.response.set_content(dump_headers(c.request.headers), "text/plain"); c.response.set_content(dump_headers(c.request.headers), "text/plain");
}); });
svr.get("/stop", [&](Connection& c) {
svr.stop();
});
svr.set_error_handler([](Connection& c) { svr.set_error_handler([](Connection& c) {
const char* fmt = "<p>Error Status: <span style='color:red;'>%d</span></p>"; const char* fmt = "<p>Error Status: <span style='color:red;'>%d</span></p>";
char buf[BUFSIZ]; char buf[BUFSIZ];

229
httplib.h
View file

@ -5,8 +5,8 @@
// The Boost Software License 1.0 // The Boost Software License 1.0
// //
#ifndef HTTPSVRKIT_H #ifndef _CPPHTTPLIB_HTTPSLIB_H_
#define HTTPSVRKIT_H #define _CPPHTTPLIB_HTTPSLIB_H_
#ifdef _WIN32 #ifdef _WIN32
#define _CRT_SECURE_NO_WARNINGS #define _CRT_SECURE_NO_WARNINGS
@ -123,8 +123,8 @@ public:
~Client(); ~Client();
std::shared_ptr<Response> get(const char* url); std::shared_ptr<Response> get(const char* url);
std::shared_ptr<Response> post( std::shared_ptr<Response> post(const char* url, const std::string& body, const char* content_type);
const char* url, const std::string& body, const char* content_type); std::shared_ptr<Response> post(const char* url, const Map& params);
bool send(const Request& req, Response& res); 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 #ifdef _WIN32
int osfhandle = _open_osfhandle(fd, _O_RDONLY); int osfhandle = _open_osfhandle(fd, _O_RDONLY);
fp_read = fdopen(osfhandle, "rb"); fp_read = _fdopen(osfhandle, "rb");
fp_write = fdopen(osfhandle, "wb"); fp_write = _fdopen(osfhandle, "wb");
#else #else
fp_read = fdopen(fd, "rb"); fp_read = fdopen(fd, "rb");
fp_write = fdopen(fd, "wb"); 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 #ifdef _WIN32
shutdown(sock, SD_BOTH);
return closesocket(sock); return closesocket(sock);
#else #else
shutdown(sock, SHUT_RDWR);
return close(sock); return close(sock);
#endif #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) 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); write_headers(fp, req);
if (!req.body.empty()) { 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 key;
std::string val; std::string val;
split(b, e, '=', [&](const char* b, const char* e) { 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 } // namespace detail
// Request implementation // Request implementation
@ -439,12 +580,12 @@ inline Server::~Server()
inline void Server::get(const char* pattern, Handler handler) 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) 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) inline void Server::set_error_handler(Handler handler)
@ -464,29 +605,34 @@ inline bool Server::run()
return false; return false;
} }
auto ret = true;
for (;;) { for (;;) {
socket_t sock = accept(svr_sock_, NULL, NULL); socket_t sock = accept(svr_sock_, NULL, NULL);
if (sock == -1) { if (sock == -1) {
if (svr_sock_ == -1) { if (svr_sock_ != -1) {
// The server socket was closed by user. detail::close_socket(svr_sock_);
return true; ret = false;
} else { } else {
detail::shutdown_and_close_socket(svr_sock_); ; // The server socket was closed by user.
return false;
} }
break;
} }
// TODO: should be async // TODO: should be async
process_request(sock); process_request(sock);
detail::shutdown_and_close_socket(sock); detail::shutdown_socket(sock);
detail::close_socket(sock);
} }
// NOTREACHED return ret;
} }
inline void Server::stop() inline void Server::stop()
{ {
detail::shutdown_and_close_socket(svr_sock_); detail::shutdown_socket(svr_sock_);
detail::close_socket(svr_sock_);
svr_sock_ = -1; svr_sock_ = -1;
} }
@ -503,13 +649,12 @@ inline bool Server::read_request_line(FILE* fp, Request& req)
std::cmatch m; std::cmatch m;
if (std::regex_match(buf, m, re)) { if (std::regex_match(buf, m, re)) {
req.method = std::string(m[1]); req.method = std::string(m[1]);
req.url = std::string(m[2]); req.url = detail::decode_url(m[2]);
// Parse query text // Parse query text
auto len = std::distance(m[3].first, m[3].second); auto len = std::distance(m[3].first, m[3].second);
if (len > 0) { if (len > 0) {
const auto& pos = m[3]; detail::parse_query_text(detail::decode_url(m[3]), req.params);
detail::parse_query_text(pos.first, pos.second, req.params);
} }
return true; return true;
@ -560,7 +705,7 @@ inline void Server::process_request(socket_t sock)
return; return;
} }
if (c.request.get_header_value("Content-Type") == "application/x-www-form-urlencoded") { 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); detail::write_response(fp_write, c.response);
fflush(fp_write); fflush(fp_write);
if (logger_) { if (logger_) {
@ -643,7 +787,8 @@ inline bool Client::send(const Request& req, Response& res)
return false; return false;
} }
detail::shutdown_and_close_socket(sock); detail::shutdown_socket(sock);
detail::close_socket(sock);
return true; return true;
} }
@ -673,6 +818,22 @@ inline std::shared_ptr<Response> Client::post(
return send(req, *res) ? res : nullptr; return send(req, *res) ? res : nullptr;
} }
inline std::shared_ptr<Response> 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 } // namespace httplib
#endif #endif

View file

@ -1,18 +1,98 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <httplib.h> #include <httplib.h>
#include <future> //#include <future>
#include <iostream> #include <iostream>
#ifdef _WIN32
#include <process.h>
#endif
using namespace std; using namespace std;
using namespace httplib; using namespace httplib;
const char* HOST = "localhost";
const int PORT = 8080;
class thread
{
public:
thread(std::function<void ()> 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<void*, std::function<void ()>> tasks_;
};
std::map<void*, std::function<void ()>> thread::tasks_;
inline thread::thread(std::function<void ()> 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<thread*>(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) TEST(SplitTest, ParseQueryString)
{ {
string s = "key1=val1&key2=val2&key3=val3"; string s = "key1=val1&key2=val2&key3=val3";
map<string, string> dic; map<string, string> 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; string key, val;
detail::split(b, e, '=', [&](const char* b, const char* e) { detail::split(b, e, '=', [&](const char* b, const char* e) {
if (key.empty()) { if (key.empty()) {
@ -34,7 +114,7 @@ TEST(ParseQueryTest, ParseQueryString)
string s = "key1=val1&key2=val2&key3=val3"; string s = "key1=val1&key2=val2&key3=val3";
map<string, string> dic; map<string, string> dic;
detail::parse_query_text(&s[0], &s[s.size()], dic); detail::parse_query_text(s, dic);
EXPECT_EQ("val1", dic["key1"]); EXPECT_EQ("val1", dic["key1"]);
EXPECT_EQ("val2", dic["key2"]); EXPECT_EQ("val2", dic["key2"]);
@ -43,37 +123,47 @@ TEST(ParseQueryTest, ParseQueryString)
TEST(SocketTest, OpenClose) 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); ASSERT_NE(-1, sock);
auto ret = detail::shutdown_and_close_socket(sock); auto ret = detail::close_socket(sock);
EXPECT_EQ(0, ret); EXPECT_EQ(0, ret);
} }
TEST(GetHeaderValueTest, DefaultValue) 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"); auto val = detail::get_header_value_text(map, "Content-Type", "text/plain");
ASSERT_STREQ("text/plain", val); ASSERT_STREQ("text/plain", val);
} }
TEST(GetHeaderValueTest, DefaultValueInt) 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); auto val = detail::get_header_value_int(map, "Content-Length", 100);
EXPECT_EQ(100, val); EXPECT_EQ(100, val);
} }
TEST(GetHeaderValueTest, RegularValue) 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"); auto val = detail::get_header_value_text(map, "Content-Type", "text/plain");
ASSERT_STREQ("text/html", val); ASSERT_STREQ("text/html", val);
} }
TEST(GetHeaderValueTest, RegularValueInt) 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); auto val = detail::get_header_value_int(map, "Content-Length", 0);
EXPECT_EQ(100, val); EXPECT_EQ(100, val);
} }
@ -81,17 +171,18 @@ TEST(GetHeaderValueTest, RegularValueInt)
class ServerTest : public ::testing::Test { class ServerTest : public ::testing::Test {
protected: protected:
ServerTest() : svr_(HOST, PORT), cli_(HOST, PORT) { ServerTest() : svr_(HOST, PORT), cli_(HOST, PORT) {
persons_["john"] = "programmer";
} }
virtual void SetUp() { virtual void SetUp() {
svr_.get("/hi", [&](httplib::Connection& c) { svr_.get("/hi", [&](Connection& c) {
c.response.set_content("Hello World!", "text/plain"); c.response.set_content("Hello World!", "text/plain");
}); });
svr_.get("/", [&](httplib::Connection& c) { svr_.get("/", [&](httplib::Connection& c) {
c.response.set_redirect("/hi"); c.response.set_redirect("/hi");
}); });
svr_.post("/person", [&](httplib::Connection& c) {
svr_.post("/person", [&](Connection& c) {
const auto& req = c.request; const auto& req = c.request;
if (req.has_param("name") && req.has_param("note")) { if (req.has_param("name") && req.has_param("note")) {
persons_[req.params.at("name")] = req.params.at("note"); persons_[req.params.at("name")] = req.params.at("note");
@ -99,7 +190,8 @@ protected:
c.response.status = 400; c.response.status = 400;
} }
}); });
svr_.get("/person/(.*)", [&](httplib::Connection& c) {
svr_.get("/person/(.*)", [&](Connection& c) {
const auto& req = c.request; const auto& req = c.request;
std::string name = req.matches[1]; std::string name = req.matches[1];
if (persons_.find(name) != persons_.end()) { if (persons_.find(name) != persons_.end()) {
@ -109,21 +201,30 @@ protected:
c.response.status = 404; 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<thread>([&](){ svr_.run(); });
} }
virtual void TearDown() { virtual void TearDown() {
svr_.stop(); //svr_.stop(); // NOTE: This causes dead lock on Windows.
f_.get(); cli_.get("/stop");
}
const char* HOST = "localhost"; //f_.get();
const int PORT = 1914; t_->join();
}
std::map<std::string, std::string> persons_; std::map<std::string, std::string> persons_;
Server svr_; Server svr_;
Client cli_; Client cli_;
std::future<void> f_; //std::future<void> f_;
std::shared_ptr<thread> t_;
}; };
TEST_F(ServerTest, GetMethod200) TEST_F(ServerTest, GetMethod200)
@ -159,21 +260,50 @@ TEST_F(ServerTest, GetMethodPersonJohn)
EXPECT_EQ("programmer", res->body); 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_TRUE(res != nullptr);
ASSERT_EQ(404, res->status); ASSERT_EQ(404, res->status);
res = cli_.post("/person", "name=john3&note=coder", "application/x-www-form-urlencoded"); res = cli_.post("/person", "name=john1&note=coder", "application/x-www-form-urlencoded");
ASSERT_TRUE(res != nullptr); ASSERT_TRUE(res != nullptr);
ASSERT_EQ(200, res->status); ASSERT_EQ(200, res->status);
res = cli_.get("/person/john3"); res = cli_.get("/person/john1");
ASSERT_TRUE(res != nullptr); ASSERT_TRUE(res != nullptr);
ASSERT_EQ(200, res->status); ASSERT_EQ(200, res->status);
ASSERT_EQ("text/plain", res->get_header_value("Content-Type")); ASSERT_EQ("text/plain", res->get_header_value("Content-Type"));
ASSERT_EQ("coder", res->body); 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 // vim: et ts=4 sw=4 cin cino={1s ff=unix