-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhttpconnection.cpp
More file actions
160 lines (140 loc) · 4.66 KB
/
httpconnection.cpp
File metadata and controls
160 lines (140 loc) · 4.66 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include "httpconnection.h"
#include <iostream>
#include "filesystemmodel.h"
#include "htmlcontentgenerator.h"
http_connection::http_connection(boost::asio::ip::tcp::socket socket,
const std::string &response_html)
: response(response_html)
,socket_(std::move(socket))
,buffer_{8192}
,deadline_{socket_.get_executor(), std::chrono::seconds(60)}
{
}
void http_connection::start()
{
read_request();
check_deadline();
}
void http_connection::setFileSystemModel(FileSystemModel *fileSystemModel)
{
mFileSystemModel = fileSystemModel;
}
void http_connection::setHTMLContentGenerator(Generator *hTMLContentGenerator)
{
mHTMLContentGenerator = hTMLContentGenerator;
}
void http_connection::set_response(const std::string &_response)
{
if (!_response.empty() && response != _response)
{
response = _response;
}
}
void http_connection::read_request()
{
auto self = shared_from_this();
boost::beast::http::async_read(socket_, buffer_, request_, [self](boost::beast::error_code ec,
std::size_t bytes_transferred)
{
boost::ignore_unused(bytes_transferred);
if (!ec)
{
self->process_request();
}
});
}
void http_connection::process_request()
{
response_.version(request_.version());
response_.keep_alive(false);
switch (request_.method())
{
case boost::beast::http::verb::get:
response_.result(boost::beast::http::status::ok);
response_.set(boost::beast::http::field::server, "Beast");
create_response();
break;
default:
// We return responses indicating an error if
// we do not recognize the request method.
response_.result(boost::beast::http::status::bad_request);
response_.set(boost::beast::http::field::content_type, "text/plain");
boost::beast::ostream(response_.body())
<< "Invalid request-method '"
<< request_.method_string().to_string()
<< "'";
break;
}
write_response();
}
void http_connection::create_response()
{
std::cout<<request_.target()<<std::endl;
if(request_.target() == "/count")
{
response_.set(boost::beast::http::field::content_type, "text/html");
boost::beast::ostream(response_.body())
<< "<html>\n"
<< "<head><title>Request count</title></head>\n"
<< "<body>\n"
<< "<h1>Request count</h1>\n"
<< "<p>There have been "
<< ""
<< " requests so far.</p>\n"
<< "</body>\n"
<< "</html>\n";
}
else if(request_.target() == "/time")
{
response_.set(boost::beast::http::field::content_type, "text/html");
boost::beast::ostream(response_.body())
<< "<html>\n"
<< "<head><title>Current time</title></head>\n"
<< "<body>\n"
<< "<h1>Current time</h1>\n"
<< "<p>The current time is "
<< "18 00"
<< " seconds since the epoch.</p>\n"
<< "</body>\n"
<< "</html>\n";
}
else
{
std::string indexString = request_.target().to_string();
std::cout<<indexString<<std::endl;
indexString.erase(0,1);
std::cout<<"string indexString.erase"<<indexString<<std::endl;
if(indexString.size() != 0)
{
int htmlIndex = std::stoi(indexString);
mFileSystemModel->enter(htmlIndex);
std::cout<<htmlIndex<<std::endl;
response=mHTMLContentGenerator->generate("");
}
response_.result(boost::beast::http::status::not_found);
response_.set(boost::beast::http::field::content_type, "text/html");
boost::beast::ostream(response_.body()) << response.c_str(); //"File not found\r\n";
}
}
void http_connection::write_response()
{
auto self = shared_from_this();
response_.set(boost::beast::http::field::content_length, response_.body().size());
boost::beast::http::async_write(socket_, response_, [self](boost::beast::error_code ec, std::size_t)
{
self->socket_.shutdown(boost::asio::ip::tcp::socket::shutdown_send, ec);
self->deadline_.cancel();
});
}
void http_connection::check_deadline()
{
auto self = shared_from_this();
deadline_.async_wait([self](boost::beast::error_code ec)
{
if (!ec)
{
// Close socket to cancel any outstanding operation.
self->socket_.close(ec);
}
});
}