diff --git a/include/server.hpp b/include/server.hpp index b135d51..69ae5d0 100644 --- a/include/server.hpp +++ b/include/server.hpp @@ -26,7 +26,7 @@ namespace network void do_accept(); void on_accept(const boost::system::error_code &ec, boost::asio::ip::tcp::socket socket); - void handle_request(session &s, std::unique_ptr req); + void handle_request(session &s, request &&req); private: bool running = false; // The server is running diff --git a/include/session.hpp b/include/session.hpp index 23af476..922be1b 100644 --- a/include/session.hpp +++ b/include/session.hpp @@ -30,7 +30,7 @@ namespace network server &srv; boost::asio::ip::tcp::socket socket; boost::asio::streambuf buffer; - std::unique_ptr req; + request req; std::queue res; }; } // namespace network diff --git a/src/server.cpp b/src/server.cpp index 456c511..00e2693 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -69,8 +69,9 @@ namespace network do_accept(); } - void server::handle_request(session &s, std::unique_ptr req) + void server::handle_request(session &s, request &&req) { - LOG_DEBUG(*req); + LOG_DEBUG(req); + s.enqueue(response{}); } } // namespace network \ No newline at end of file diff --git a/src/session.cpp b/src/session.cpp index 3fad2ac..c4e79c3 100644 --- a/src/session.cpp +++ b/src/session.cpp @@ -6,11 +6,7 @@ namespace network { session::session(server &srv, boost::asio::ip::tcp::socket socket) : srv(srv), socket(std::move(socket)) {} - void session::read() - { - req = std::make_unique(); - boost::asio::async_read_until(socket, buffer, "\r\n\r\n", std::bind(&session::on_read, shared_from_this(), std::placeholders::_1, std::placeholders::_2)); - } + void session::read() { boost::asio::async_read_until(socket, buffer, "\r\n\r\n", std::bind(&session::on_read, shared_from_this(), std::placeholders::_1, std::placeholders::_2)); } void session::enqueue(const response &res) { @@ -40,22 +36,22 @@ namespace network { case 'D': if (is.get() == 'E' && is.get() == 'L' && is.get() == 'E' && is.get() == 'T' && is.get() == 'E') - req->v = DELETE; + req.v = DELETE; break; case 'G': if (is.get() == 'E' && is.get() == 'T') - req->v = GET; + req.v = GET; break; case 'P': switch (is.get()) { case 'O': if (is.get() == 'S' && is.get() == 'T') - req->v = POST; + req.v = POST; break; case 'U': if (is.get() == 'T') - req->v = PUT; + req.v = PUT; break; } break; @@ -63,14 +59,15 @@ namespace network is.get(); // consume space while (is.peek() != ' ') - req->target += is.get(); + req.target += is.get(); is.get(); // consume space while (is.peek() != '\r') - req->version += is.get(); + req.version += is.get(); is.get(); // consume '\r' is.get(); // consume '\n' + req.headers.clear(); while (is.peek() != '\r') { std::string header, value; @@ -82,22 +79,22 @@ namespace network value += is.get(); is.get(); // consume '\r' is.get(); // consume '\n' - req->headers.emplace(std::move(header), std::move(value)); + req.headers.emplace(std::move(header), std::move(value)); } is.get(); // consume '\r' is.get(); // consume '\n' - if (req->headers.find("Upgrade") != req->headers.end()) + if (req.headers.find("Upgrade") != req.headers.end()) { // handle websocket LOG_DEBUG("Websocket not implemented"); return; } - bool keep_alive = req->headers.find("Connection") != req->headers.end() && req->headers["Connection"] == "keep-alive"; + bool keep_alive = req.headers.find("Connection") != req.headers.end() && req.headers["Connection"] == "keep-alive"; - if (req->headers.find("Content-Length") != req->headers.end()) + if (req.headers.find("Content-Length") != req.headers.end()) { // read body - std::size_t content_length = std::stoul(req->headers["Content-Length"]); + std::size_t content_length = std::stoul(req.headers["Content-Length"]); if (content_length > bytes_transferred) // the buffer may contain additional bytes beyond the delimiter boost::asio::async_read(socket, buffer, boost::asio::transfer_exactly(content_length - bytes_transferred), std::bind(&session::on_body, shared_from_this(), std::placeholders::_1, std::placeholders::_2)); else // the buffer contains the entire body @@ -119,14 +116,14 @@ namespace network } std::istream is(&buffer); - if (req->headers.find("Content-Type") != req->headers.end() && req->headers["Content-Type"] == "application/json") - req = std::make_unique(req->v, std::move(req->target), std::move(req->version), std::move(req->headers), json::load(is)); + if (req.headers.find("Content-Type") != req.headers.end() && req.headers["Content-Type"] == "application/json") + req = json_request(req.v, std::move(req.target), std::move(req.version), std::move(req.headers), json::load(is)); else { std::string body; while (is.peek() != EOF) body += is.get(); - req = std::make_unique(req->v, std::move(req->target), std::move(req->version), std::move(req->headers), std::move(body)); + req = string_request(req.v, std::move(req.target), std::move(req.version), std::move(req.headers), std::move(body)); } srv.handle_request(*this, std::move(req)); } diff --git a/tests/test_server.cpp b/tests/test_server.cpp index 5dfee2e..85838ef 100644 --- a/tests/test_server.cpp +++ b/tests/test_server.cpp @@ -7,7 +7,7 @@ int main(int argc, char const *argv[]) std::thread t{[&server] { server.start(); }}; - std::this_thread::sleep_for(std::chrono::seconds(10)); + std::this_thread::sleep_for(std::chrono::seconds(100)); server.stop(); t.join();