-
Notifications
You must be signed in to change notification settings - Fork 1
/
httpconnection.h
69 lines (46 loc) · 1.82 KB
/
httpconnection.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#ifndef HTTPCONNECTION_H
#define HTTPCONNECTION_H
#include <boost/beast/version.hpp>
#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>
#include <boost/asio.hpp>
class FileSystemModel;
class Generator;
class http_connection : public std::enable_shared_from_this<http_connection>
{
public:
http_connection(boost::asio::ip::tcp::socket socket,
const std::string &response_html = std::string{});
// Initiate the asynchronous operations associated with the connection.
void start();
void setFileSystemModel(FileSystemModel *fileSystemModel);
void setHTMLContentGenerator(Generator *hTMLContentGenerator);
private:
FileSystemModel *mFileSystemModel;
Generator *mHTMLContentGenerator;
// Response
std::string response;
// The socket for the currently connected client.
boost::asio::ip::tcp::socket socket_;
// The buffer for performing reads.
boost::beast::flat_buffer buffer_;
// The request message.
boost::beast::http::request<boost::beast::http::dynamic_body> request_;
// The response message.
boost::beast::http::response<boost::beast::http::dynamic_body> response_;
// The timer for putting a deadline on connection processing.
boost::asio::basic_waitable_timer<std::chrono::steady_clock> deadline_;
private:
void set_response(const std::string &_response);
// Asynchronously receive a complete request message.
void read_request();
// Determine what needs to be done with the request message.
void process_request();
// Construct a response message based on the program state.
void create_response();
// Asynchronously transmit the response message.
void write_response();
// Check whether we have spent enough time on this connection.
void check_deadline();
};
#endif // HTTPCONNECTION_H