refactor to use beast
This commit is contained in:
parent
2a4e32a274
commit
656ddf2941
12 changed files with 177 additions and 150 deletions
37
includes/http_connection.hpp
Normal file
37
includes/http_connection.hpp
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
#ifndef HTTP_CONNECTION_HPP
|
||||
#define HTTP_CONNECTION_HPP
|
||||
|
||||
#include "request_handler.hpp"
|
||||
#include <boost/beast/core/flat_buffer.hpp>
|
||||
#include <memory>
|
||||
#include <boost/asio.hpp>
|
||||
#include <boost/beast/http/string_body.hpp>
|
||||
#include <boost/beast/http.hpp>
|
||||
#include <boost/beast/core.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace boost::asio;
|
||||
using namespace boost::beast;
|
||||
|
||||
class HttpConnection : public enable_shared_from_this<HttpConnection> {
|
||||
public:
|
||||
typedef shared_ptr<HttpConnection> pointer;
|
||||
|
||||
static pointer create(io_context& io_context);
|
||||
ip::tcp::socket& socket();
|
||||
void process_connection();
|
||||
|
||||
private:
|
||||
ip::tcp::socket socket_;
|
||||
boost::beast::flat_buffer buffer_;
|
||||
http::request<http::string_body> request_;
|
||||
http::response<http::string_body> response_;
|
||||
RequestHandler request_handler_;
|
||||
|
||||
void read();
|
||||
void write();
|
||||
|
||||
explicit HttpConnection(io_context& io_context);
|
||||
};
|
||||
|
||||
#endif
|
||||
24
includes/http_server.hpp
Normal file
24
includes/http_server.hpp
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
#ifndef HTTP_SERVER_HPP
|
||||
#define HTTP_SERVER_HPP
|
||||
|
||||
#include "http_connection.hpp"
|
||||
#include <boost/asio.hpp>
|
||||
#include <boost/asio/io_context.hpp>
|
||||
#include <boost/asio/ip/tcp.hpp>
|
||||
#include <boost/system/detail/error_code.hpp>
|
||||
|
||||
using namespace boost::asio;
|
||||
|
||||
class HttpServer {
|
||||
public:
|
||||
HttpServer(io_context& io_context, const ip::tcp::endpoint& endpoint);
|
||||
|
||||
private:
|
||||
io_context& io_context_;
|
||||
ip::tcp::acceptor acceptor_;
|
||||
|
||||
void accept_connection();
|
||||
void handle_accept(HttpConnection::pointer& new_connection, const boost::system::error_code& error_code);
|
||||
};
|
||||
|
||||
#endif
|
||||
17
includes/request_handler.hpp
Normal file
17
includes/request_handler.hpp
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
#ifndef REQUEST_HANDLER_HPP
|
||||
#define REQUEST_HANDLER_HPP
|
||||
|
||||
#include <boost/beast/http.hpp>
|
||||
#include <boost/beast/http/message.hpp>
|
||||
#include <boost/beast/http/string_body.hpp>
|
||||
|
||||
using namespace boost::beast;
|
||||
|
||||
class RequestHandler {
|
||||
public:
|
||||
http::response<http::string_body> handle(const http::request<http::string_body>& request);
|
||||
|
||||
private:
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
@ -1,23 +0,0 @@
|
|||
#ifndef REQUEST_PARSER_HPP
|
||||
#define REQUEST_PARSER_HPP
|
||||
|
||||
#include <istream>
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
struct HttpRequest {
|
||||
string method;
|
||||
string uri;
|
||||
string version;
|
||||
map<string, string> headers;
|
||||
string body;
|
||||
};
|
||||
|
||||
class request_parser {
|
||||
public:
|
||||
HttpRequest& parse(istream& data_stream);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
@ -1,14 +0,0 @@
|
|||
#ifndef RESPONSE_HANDLER_HPP
|
||||
#define RESPONSE_HANDLER_HPP
|
||||
|
||||
#include "request_parser.hpp"
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class response_handler {
|
||||
public:
|
||||
string& handle(HttpRequest request);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
@ -1,33 +0,0 @@
|
|||
#ifndef TCP_CONNECTION_HPP
|
||||
#define TCP_CONNECTION_HPP
|
||||
|
||||
#include <boost/system/detail/error_code.hpp>
|
||||
#include <memory>
|
||||
#include <boost/asio.hpp>
|
||||
#include "request_parser.hpp"
|
||||
#include "response_handler.hpp"
|
||||
|
||||
using namespace boost;
|
||||
using namespace std;
|
||||
|
||||
class tcp_connection : public std::enable_shared_from_this<tcp_connection> {
|
||||
public:
|
||||
typedef std::shared_ptr<tcp_connection> pointer;
|
||||
|
||||
static pointer create(asio::io_context& io_context);
|
||||
asio::ip::tcp::socket& socket();
|
||||
void handle();
|
||||
|
||||
private:
|
||||
asio::ip::tcp::socket socket_;
|
||||
asio::streambuf buffer_;
|
||||
request_parser request_parser_;
|
||||
response_handler response_handler_;
|
||||
|
||||
explicit tcp_connection(asio::io_context& io_context);
|
||||
|
||||
void handle_read(const system::error_code& error, size_t bytes_transferred);
|
||||
void handle_write(const system::error_code& error, size_t bytes_transferred);
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in a new issue