From 1d0b3504bd855749077170c949b4b5483a12de09 Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 4 Oct 2012 00:38:32 -0400 Subject: [PATCH] Refactoring. --- example/server.cc | 9 ++++---- httplib.h | 53 ++++++++++++++++++++++++++--------------------- 2 files changed, 33 insertions(+), 29 deletions(-) diff --git a/example/server.cc b/example/server.cc index 84f6499..ff93960 100644 --- a/example/server.cc +++ b/example/server.cc @@ -70,12 +70,10 @@ int main(void) { using namespace httplib; - const char* hi = "/hi"; - Server svr("localhost", 8080); svr.get("/", [=](Connection& c) { - c.response.set_redirect(hi); + c.response.set_redirect("/hi"); }); svr.get("/hi", [](Connection& c) { @@ -86,9 +84,10 @@ int main(void) c.response.set_content(dump_headers(c.request.headers), "text/plain"); }); - svr.set_error_handler([](httplib::Connection& c) { + svr.set_error_handler([](Connection& c) { + const char* fmt = "

Error Status: %d

"; char buf[BUFSIZ]; - snprintf(buf, sizeof(buf), "

Error Status: %d

", c.response.status); + snprintf(buf, sizeof(buf), fmt, c.response.status); c.response.set_content(buf, "text/html"); }); diff --git a/httplib.h b/httplib.h index e2443b3..45df4e8 100644 --- a/httplib.h +++ b/httplib.h @@ -74,6 +74,8 @@ struct Response { void set_redirect(const char* url); void set_content(const std::string& s, const char* content_type); + + Response() : status(-1) {} }; struct Connection { @@ -102,7 +104,8 @@ private: void process_request(FILE* fp_read, FILE* fp_write); bool read_request_line(FILE* fp, Request& req); - void dispatch_request(Connection& c, Handlers& handlers); + bool routing(Connection& c); + bool dispatch_request(Connection& c, Handlers& handlers); const std::string host_; const int port_; @@ -533,7 +536,17 @@ inline bool Server::read_request_line(FILE* fp, Request& req) return false; } -inline void Server::dispatch_request(Connection& c, Handlers& handlers) +inline bool Server::routing(Connection& c) +{ + if (c.request.method == "GET") { + return dispatch_request(c, get_handlers_); + } else if (c.request.method == "POST") { + return dispatch_request(c, post_handlers_); + } + return false; +} + +inline bool Server::dispatch_request(Connection& c, Handlers& handlers) { for (auto it = handlers.begin(); it != handlers.end(); ++it) { const auto& pattern = it->first; @@ -541,53 +554,45 @@ inline void Server::dispatch_request(Connection& c, Handlers& handlers) if (std::regex_match(c.request.url, c.request.matches, pattern)) { handler(c); - - if (!c.response.status) { - c.response.status = 200; - } - break; + return true; } } + return false; } inline void Server::process_request(FILE* fp_read, FILE* fp_write) { Connection c; auto& req = c.request; - auto& res = c.response; if (!read_request_line(fp_read, req) || !read_headers(fp_read, req.headers)) { return; } - - // Routing - res.status = 0; - if (req.method == "GET") { - dispatch_request(c, get_handlers_); - } else if (req.method == "POST") { + if (req.method == "POST") { if (!read_content(req, fp_read)) { return; } if (req.get_header_value("Content-Type") == "application/x-www-form-urlencoded") { - // Parse query text - const char* b = &req.body[0]; - const char* e = &req.body[req.body.size()]; - parse_query_text(b, e, req.params); + parse_query_text(&req.body[0], &req.body[req.body.size()], req.params); } - dispatch_request(c, post_handlers_); } - - if (!res.status) { - res.status = 404; + + if (routing(c)) { + if (c.response.status == -1) { + c.response.status = 200; + } + } else { + c.response.status = 404; } + assert(c.response.status != -1); - if (400 <= res.status && error_handler_) { + if (400 <= c.response.status && error_handler_) { error_handler_(c); } - write_response(fp_write, res); + write_response(fp_write, c.response); if (logger_) { logger_(c);