From 55be9a9f6d994a21b3655524dabdf3f6a74eabce Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Mon, 13 Mar 2017 16:11:30 -0700 Subject: [PATCH] ircd: Loop the whole input tape before returning to async. --- include/ircd/client.h | 4 +-- ircd/client.cc | 77 +++++++++++++++++++++++++++++-------------- ircd/matrix.cc | 2 +- 3 files changed, 56 insertions(+), 27 deletions(-) diff --git a/include/ircd/client.h b/include/ircd/client.h index 1e1e7c88c..184cb24a5 100644 --- a/include/ircd/client.h +++ b/include/ircd/client.h @@ -53,8 +53,8 @@ struct client bool main() noexcept; public: - explicit client(std::shared_ptr); - explicit client(const host_port &, const seconds &timeout = 5s); + client(std::shared_ptr); + client(const host_port &, const seconds &timeout = 5s); client(); client(client &&) = delete; client(const client &) = delete; diff --git a/ircd/client.cc b/ircd/client.cc index 40de36354..6c17c6a9e 100644 --- a/ircd/client.cc +++ b/ircd/client.cc @@ -206,31 +206,50 @@ catch(const std::exception &e) return false; } +namespace ircd +{ + void handle_request(client &client, parse::capstan &pc, const http::request::head &head); + bool handle_request(client &client, parse::capstan &pc); + +} // namepace ircd + bool ircd::client::serve() try { - char buffer[4096]; - + char buffer[2048]; parse::buffer pb{buffer, buffer + sizeof(buffer)}; - parse::capstan pc{pb, read_closure(*this)}; + parse::capstan pc{pb, read_closure(*this)}; do + { + if(!handle_request(*this, pc)) + break; + + pb.remove(); + } + while(pc.unparsed()); + + return true; +} +catch(const std::exception &e) +{ + log::error("client[%s] [500 Internal Error]: %s", + string(remote_addr(*this)).c_str(), + e.what()); + + return false; +} + +bool +ircd::handle_request(client &client, + parse::capstan &pc) +try +{ http::request { - pc, nullptr, write_closure(*this), [&] - (const http::request::head &head) + pc, nullptr, write_closure(client), [&client, &pc] + (const auto &head) { - log::debug("client[%s] requesting resource \"%s\"", - string(remote_addr(*this)).c_str(), - std::string(head.resource).c_str()); - try - { - const auto &resource(*resource::resources.at(head.resource)); - resource(*this, pc, head); - } - catch(const std::out_of_range &e) - { - throw http::error(http::code::NOT_FOUND); - } + handle_request(client, pc, head); } }; @@ -239,7 +258,7 @@ try catch(const http::error &e) { log::debug("client[%s] http::error(%d): %s", - string(remote_addr(*this)).c_str(), + string(remote_addr(client)).c_str(), int(e.code), e.what()); @@ -250,13 +269,23 @@ catch(const http::error &e) default: return true; } } -catch(const std::exception &e) -{ - log::error("client[%s] [500 Internal Error]: %s", - string(remote_addr(*this)).c_str(), - e.what()); - return false; +void +ircd::handle_request(client &client, + parse::capstan &pc, + const http::request::head &head) +try +{ + log::debug("client[%s] requesting resource \"%s\"", + string(remote_addr(client)).c_str(), + std::string(head.resource).c_str()); + + const auto &resource(*resource::resources.at(head.resource)); + resource(client, pc, head); +} +catch(const std::out_of_range &e) +{ + throw http::error(http::code::NOT_FOUND); } std::shared_ptr diff --git a/ircd/matrix.cc b/ircd/matrix.cc index 78d10b014..1fa64b9b7 100644 --- a/ircd/matrix.cc +++ b/ircd/matrix.cc @@ -42,7 +42,7 @@ try const auto port(lex_cast(tok.at(1))); ircd::client client { - host_port{host, port} + { host, port } }; http::request