Skip to content

Commit

Permalink
Refactor handle_request method in server.hpp and server.cpp to accept…
Browse files Browse the repository at this point in the history
… request by value
  • Loading branch information
riccardodebenedictis committed Apr 19, 2024
1 parent 5410279 commit 80d790b
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 24 deletions.
2 changes: 1 addition & 1 deletion include/server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<request> req);
void handle_request(session &s, request &&req);

private:
bool running = false; // The server is running
Expand Down
2 changes: 1 addition & 1 deletion include/session.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace network
server &srv;
boost::asio::ip::tcp::socket socket;
boost::asio::streambuf buffer;
std::unique_ptr<request> req;
request req;
std::queue<boost::asio::const_buffer> res;
};
} // namespace network
5 changes: 3 additions & 2 deletions src/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ namespace network
do_accept();
}

void server::handle_request(session &s, std::unique_ptr<request> req)
void server::handle_request(session &s, request &&req)
{
LOG_DEBUG(*req);
LOG_DEBUG(req);
s.enqueue(response{});
}
} // namespace network
35 changes: 16 additions & 19 deletions src/session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<request>();
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)
{
Expand Down Expand Up @@ -40,37 +36,38 @@ 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;
}
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;
Expand All @@ -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
Expand All @@ -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<json_request>(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<string_request>(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));
}
Expand Down
2 changes: 1 addition & 1 deletion tests/test_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down

0 comments on commit 80d790b

Please sign in to comment.