diff --git a/README.md b/README.md index 131cec0..c700e78 100644 --- a/README.md +++ b/README.md @@ -11,13 +11,17 @@ Server Example Inspired by [Sinatra](http://www.sinatrarb.com/) #include + using namespace httplib; - int main(void) { - HTTP_SERVER("localhost", 1234) { - GET("/hi", { - res.set_content("Hello World!"); - }); - } + int main(void) + { + Server svr("localhost", 1234); + + svr.get("/hi", [](Connection& c) { + c.response.set_content("Hello World!"); + }); + + svr.run(); } Copyright (c) 2012 Yuji Hirose. All rights reserved. diff --git a/example/hello.cc b/example/hello.cc index aacacdf..414896c 100644 --- a/example/hello.cc +++ b/example/hello.cc @@ -6,14 +6,17 @@ // #include +using namespace httplib; int main(void) { - HTTP_SERVER("localhost", 1234) /* svr_ */ { - GET("/hi", /* req_, res_ */ { - res_.set_content("Hello World!"); - }); - } + Server svr("localhost", 1234); + + svr.get("/hi", [](Connection& c) { + c.response.set_content("Hello World!"); + }); + + svr.run(); } // vim: et ts=4 sw=4 cin cino={1s ff=unix diff --git a/example/sample.cc b/example/sample.cc index 69d21b3..e470042 100644 --- a/example/sample.cc +++ b/example/sample.cc @@ -21,26 +21,27 @@ template void signal(int sig, Fn fn) int main(void) { + using namespace httplib; + const char* hi = "/hi"; - HTTP_SERVER("localhost", 1234) /* svr_ */ { + Server svr("localhost", 1234); - GET("/", { - res_.set_redirect(hi); - }); + svr.get("/", [=](Connection& c) { + c.response.set_redirect(hi); + }); - GET("/hi", { - res_.set_content("Hello World!"); - }); + svr.get("/hi", [](Connection& c) { + c.response.set_content("Hello World!"); + }); - GET("/dump", { - res_.set_content(dump_request(cxt)); - }); + svr.get("/dump", [](Connection& c) { + c.response.set_content(dump_request(c)); + }); - signal(SIGINT, [&](){ - svr_->stop(); - }); - } + signal(SIGINT, [&]() { svr.stop(); }); + + svr.run(); } // vim: et ts=4 sw=4 cin cino={1s ff=unix diff --git a/httplib.h b/httplib.h index a3d5361..fb24007 100644 --- a/httplib.h +++ b/httplib.h @@ -69,7 +69,7 @@ struct Response { void set_content(const std::string& s, const char* content_type = "text/plain"); }; -struct Context { +struct Connection { Request request; Response response; }; @@ -77,7 +77,7 @@ struct Context { // HTTP server class Server { public: - typedef std::function Handler; + typedef std::function Handler; Server(const char* ipaddr_or_hostname, int port); ~Server(); @@ -176,9 +176,9 @@ inline int close_server_socket(socket_t sock) #endif } -std::string dump_request(Context& cxt) +std::string dump_request(Connection& c) { - const auto& req = cxt.request; + const auto& req = c.request; std::string s; char buf[BUFSIZ]; @@ -413,61 +413,49 @@ inline void write_error(FILE* fp, int status) inline void Server::process_request(FILE* fp_read, FILE* fp_write) { - Context cxt; + Connection c; // Read and parse request line - if (!read_request_line(fp_read, cxt.request)) { + if (!read_request_line(fp_read, c.request)) { write_error(fp_write, 400); return; } // Read headers - read_headers(fp_read, cxt.request.headers); + read_headers(fp_read, c.request.headers); - printf("%s", dump_request(cxt).c_str()); + printf("%s", dump_request(c).c_str()); // Routing - cxt.response.status = 404; + c.response.status = 404; - if (cxt.request.method == "GET") { + if (c.request.method == "GET") { for (auto it = get_handlers_.begin(); it != get_handlers_.end(); ++it) { const auto& pattern = it->first; const auto& handler = it->second; std::smatch m; - if (std::regex_match(cxt.request.url, m, pattern)) { + if (std::regex_match(c.request.url, m, pattern)) { for (size_t i = 1; i < m.size(); i++) { - cxt.request.params.push_back(m[i]); + c.request.params.push_back(m[i]); } - handler(cxt); + handler(c); break; } } - } else if (cxt.request.method == "POST") { + } else if (c.request.method == "POST") { // TODO: parse body } else { - cxt.response.status = 400; + c.response.status = 400; } - if (200 <= cxt.response.status && cxt.response.status < 400) { - write_response(fp_write, cxt.response); + if (200 <= c.response.status && c.response.status < 400) { + write_response(fp_write, c.response); } else { - write_error(fp_write, cxt.response.status); + write_error(fp_write, c.response.status); } } -#define HTTP_SERVER(host, port) \ - for (std::shared_ptr svr_ = std::make_shared(host, port); \ - svr_; \ - svr_->run(), svr_.reset()) - -#define GET(url, body) \ - svr_->get(url, [&](httplib::Context& cxt) { \ - const auto& req_ = cxt.request; \ - auto& res_ = cxt.response; \ - body \ - }); - } // namespace httplib #endif diff --git a/test/test.cc b/test/test.cc index 7acfc4b..bb39426 100644 --- a/test/test.cc +++ b/test/test.cc @@ -55,8 +55,8 @@ TEST(ServerTest, GetMethod) { Server svr("localhost", 1914); - svr.get("hi", [&](httplib::Context& cxt) { - cxt.response.set_content("Hello World!"); + svr.get("hi", [&](httplib::Connection& c) { + c.response.set_content("Hello World!"); }); svr.on_ready([&]() {