Refactoring.

This commit is contained in:
yhirose 2012-10-04 00:38:32 -04:00
parent 17bf14a2f7
commit 1d0b3504bd
2 changed files with 33 additions and 29 deletions

View file

@ -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 = "<p>Error Status: <span style='color:red;'>%d</span></p>";
char buf[BUFSIZ];
snprintf(buf, sizeof(buf), "<p>Error Status: <span style='color:red;'>%d</span></p>", c.response.status);
snprintf(buf, sizeof(buf), fmt, c.response.status);
c.response.set_content(buf, "text/html");
});

View file

@ -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);