From 7c803330e010e66e2458a4e9a343069da3e0f275 Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 25 Sep 2012 23:21:57 -0400 Subject: [PATCH] Simplified the sample code. --- example/sample.cc | 74 +++++------------------------------------------ httpsvrkit.h | 31 ++++++++++++++++++++ 2 files changed, 39 insertions(+), 66 deletions(-) diff --git a/example/sample.cc b/example/sample.cc index 2735e79..bf7804d 100644 --- a/example/sample.cc +++ b/example/sample.cc @@ -10,81 +10,23 @@ using namespace httpsvrkit; -std::string dump_request(Context& cxt) -{ - std::string s; - char buf[BUFSIZ]; - - s += "================================\n"; - - sprintf(buf, "Method: %s\n", cxt.request.method.c_str()); - s += buf; - - sprintf(buf, "URL: %s\n", cxt.request.url.c_str()); - s += buf; - - std::string query; - for (auto it = cxt.request.query.begin(); it != cxt.request.query.end(); ++it) { - const auto& x = *it; - sprintf(buf, "(%s:%s)", x.first.c_str(), x.second.c_str()); - query += buf; - } - sprintf(buf, "QUERY: %s\n", query.c_str()); - s += buf; - - //for (const auto& x : cxt.request.headers) { - for (auto it = cxt.request.headers.begin(); it != cxt.request.headers.end(); ++it) { - const auto& x = *it; - sprintf(buf, "%s: %s\n", x.first.c_str(), x.second.c_str()); - s += buf; - } - - s += "================================\n"; - - return s; -} - int main(void) { - if (true) { - const char* s = "abcde"; + const char* hi = "/hi"; - // DSL style - HTTP_SERVER("localhost", 1234) { + HTTP_SERVER("localhost", 1234) { - GET("/", { - res.set_redirect("/home"); - }); - - GET("/home", { - res.set_content(dump_request(cxt)); - }); - - GET("/abcde", { - res.set_content(s); - }); - } - } else { - // Regular style - Server svr("localhost", 1234); - - svr.get("/", [](Context& cxt) { - cxt.response.set_redirect("/home"); + GET("/", { + res.set_redirect(hi); }); - svr.get("/home", [](Context& cxt) { - cxt.response.set_content(dump_request(cxt)); + GET("/hi", { + res.set_content("Hello World!"); }); - svr.post("/item", [](Context& cxt) { - cxt.response.set_content(dump_request(cxt)); + GET("/dump", { + res.set_content(dump_request(cxt)); }); - - svr.get("/item/([^/]+)", [](Context& cxt) { - cxt.response.set_content(dump_request(cxt)); - }); - - svr.run(); } } diff --git a/httpsvrkit.h b/httpsvrkit.h index 63deb0d..336e31d 100644 --- a/httpsvrkit.h +++ b/httpsvrkit.h @@ -170,6 +170,35 @@ inline void close_socket(socket_t sock) #endif } +std::string dump_request(Context& cxt) +{ + const auto& req = cxt.request; + std::string s; + char buf[BUFSIZ]; + + s += "================================\n"; + + sprintf(buf, "%s %s", req.method.c_str(), req.url.c_str()); + s += buf; + + std::string query; + for (auto it = req.query.begin(); it != req.query.end(); ++it) { + const auto& x = *it; + sprintf(buf, "%c%s=%s", (it == req.query.begin()) ? '?' : '&', x.first.c_str(), x.second.c_str()); + query += buf; + } + sprintf(buf, "%s\n", query.c_str()); + s += buf; + + for (auto it = req.headers.begin(); it != req.headers.end(); ++it) { + const auto& x = *it; + sprintf(buf, "%s: %s\n", x.first.c_str(), x.second.c_str()); + s += buf; + } + + return s; +} + void Response::set_redirect(const char* url) { headers.insert(std::make_pair("Location", url)); @@ -379,6 +408,8 @@ inline void Server::process_request(FILE* fp_read, FILE* fp_write) // Read headers read_headers(fp_read, cxt.request.headers); + + printf("%s", dump_request(cxt).c_str()); // Routing cxt.response.status = 404;