Code cleanup

This commit is contained in:
yhirose 2019-11-27 08:01:25 -05:00
parent 924a557fa3
commit 8f3dbf7f21

View file

@ -547,7 +547,7 @@ private:
int bind_internal(const char *host, int port, int socket_flags); int bind_internal(const char *host, int port, int socket_flags);
bool listen_internal(); bool listen_internal();
bool routing(Request &req, Response &res, ContentReader content_reader); bool routing(Request &req, Response &res, Stream &strm, bool last_connection);
bool handle_file_request(Request &req, Response &res); bool handle_file_request(Request &req, Response &res);
bool dispatch_request(Request &req, Response &res, Handlers &handlers); bool dispatch_request(Request &req, Response &res, Handlers &handlers);
bool dispatch_request_for_content_reader(Request &req, Response &res, bool dispatch_request_for_content_reader(Request &req, Response &res,
@ -2790,31 +2790,38 @@ inline bool Server::listen_internal() {
return ret; return ret;
} }
inline bool Server::routing(Request &req, Response &res, inline bool Server::routing(Request &req, Response &res, Stream &strm, bool last_connection) {
ContentReader content_reader) {
// File handler // File handler
if (req.method == "GET" && handle_file_request(req, res)) { return true; } if (req.method == "GET" && handle_file_request(req, res)) { return true; }
// Content reader handler // Content reader handler
if (req.method == "POST") { if (req.method == "POST" || req.method == "PUT" || req.method == "PATCH") {
if (dispatch_request_for_content_reader(req, res, content_reader, ContentReader content_reader = [&](ContentReceiver receiver) {
post_handlers_for_content_reader)) { return read_content_with_content_receiver(strm, last_connection, req, res, receiver);
return true; };
}
} else if (req.method == "PUT") { if (req.method == "POST") {
if (dispatch_request_for_content_reader(req, res, content_reader, if (dispatch_request_for_content_reader(req, res, content_reader,
put_handlers_for_content_reader)) { post_handlers_for_content_reader)) {
return true; return true;
} }
} else if (req.method == "PATCH") { } else if (req.method == "PUT") {
if (dispatch_request_for_content_reader( if (dispatch_request_for_content_reader(req, res, content_reader,
req, res, content_reader, patch_handlers_for_content_reader)) { put_handlers_for_content_reader)) {
return true; return true;
}
} else if (req.method == "PATCH") {
if (dispatch_request_for_content_reader(
req, res, content_reader, patch_handlers_for_content_reader)) {
return true;
}
} }
} }
// Read content into `req.body` // Read content into `req.body`
if (!content_reader(nullptr)) { return false; } if (!read_content(strm, last_connection, req, res)) {
return false;
}
// Regular handler // Regular handler
if (req.method == "GET" || req.method == "HEAD") { if (req.method == "GET" || req.method == "HEAD") {
@ -2916,23 +2923,8 @@ Server::process_request(Stream &strm, bool last_connection,
if (setup_request) { setup_request(req); } if (setup_request) { setup_request(req); }
// Body
ContentReader content_reader = [&](ContentReceiver receiver) {
if (req.method == "POST" || req.method == "PUT" || req.method == "PATCH") {
if (receiver) {
return read_content_with_content_receiver(strm, last_connection, req,
res, receiver);
} else {
return read_content(strm, last_connection, req, res);
}
} else if (req.method == "PRI") {
return read_content(strm, last_connection, req, res);
}
return true;
};
// Rounting // Rounting
if (routing(req, res, content_reader)) { if (routing(req, res, strm, last_connection)) {
if (res.status == -1) { res.status = req.ranges.empty() ? 200 : 206; } if (res.status == -1) { res.status = req.ranges.empty() ? 200 : 206; }
} else { } else {
if (res.status == -1) { res.status = 404; } if (res.status == -1) { res.status = 404; }