-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_win_server.cpp
More file actions
209 lines (188 loc) · 6.22 KB
/
http_win_server.cpp
File metadata and controls
209 lines (188 loc) · 6.22 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include"http_win_server.h"
namespace rhttp {
server::server() : m_addr(), m_port(), m_rootdir(nullptr), ioc()
{
}
server::~server()
{
}
template<class Body, class Allocator>
http::message_generator server::generate_response
(
beast::string_view doc_root,
http::request<Body, http::basic_fields<Allocator>>&& req
)
{
auto const bad_request =
[&req](beast::string_view why)
{
http::response<http::string_body> res{ http::status::bad_request, req.version() };
res.set(http::field::server, _BOOST_BEAST_VERSION_STRING);
res.set(http::field::content_type, "text/html");
res.keep_alive(req.keep_alive());
res.body() = std::string(why);
res.prepare_payload();
return res;
};
auto const not_found =
[&req](beast::string_view target)
{
http::response<http::string_body> res{ http::status::not_found, req.version() };
res.set(http::field::server, _BOOST_BEAST_VERSION_STRING);
res.set(http::field::content_type, "text/html");
res.keep_alive(req.keep_alive());
res.body() = "The resource " + std::string(target) + " was not found.";
res.prepare_payload();
return res;
};
auto const server_error =
[&req](beast::string_view what)
{
http::response<http::string_body> res{ http::status::internal_server_error, req.version() };
res.set(http::field::server, _BOOST_BEAST_VERSION_STRING);
res.set(http::field::content_type, "text/html");
res.keep_alive(req.keep_alive());
res.body() = "An error occurred: '" + std::string(what) + "'";
res.prepare_payload();
return res;
};
if (req.method() != http::verb::get &&
req.method() != http::verb::head)
return bad_request("Unknown HTTP-method");
if (req.target().empty() ||
req.target()[0] != '/' ||
req.target().find("..") != beast::string_view::npos)
return bad_request("Illegal request-target");
std::string path = path_cat(doc_root, req.target());
if (req.target().back() == '/')
path.append("index.html");
beast::error_code ec;
http::file_body::value_type body;
body.open(path.c_str(), beast::file_mode::read, ec);
if (ec == beast::errc::no_such_file_or_directory)
return not_found(req.target());
if (ec)
return server_error(ec.message());
const uint64_t size = body.size();
if (req.method() == http::verb::head)
{
http::response<http::empty_body> res{ http::status::ok, req.version() };
res.set(http::field::server, _BOOST_BEAST_VERSION_STRING);
res.set(http::field::content_type, mime_type(path));
res.content_length(size);
res.keep_alive(req.keep_alive());
return res;
}
http::response<http::file_body> res{
std::piecewise_construct,
std::make_tuple(std::move(body)),
std::make_tuple(http::status::ok, req.version()) };
res.set(http::field::server, _BOOST_BEAST_VERSION_STRING);
res.set(http::field::content_type, mime_type(path));
res.content_length(size);
res.keep_alive(req.keep_alive());
return res;
}
void server::fail(beast::error_code ec, const char* what)
{
std::cerr << what << " : " << ec.message() << '\n';
}
beast::string_view server::mime_type(beast::string_view path)
{
using beast::iequals;
const auto ext = [&path]
{
const auto pos = path.rfind(".");
if (pos == beast::string_view::npos)
return beast::string_view{};
return path.substr(pos);
}();
if (iequals(ext, ".htm")) return "text/html";
if (iequals(ext, ".html")) return "text/html";
if (iequals(ext, ".php")) return "text/html";
if (iequals(ext, ".css")) return "text/css";
if (iequals(ext, ".txt")) return "text/plain";
if (iequals(ext, ".js")) return "application/javascript";
if (iequals(ext, ".json")) return "application/json";
if (iequals(ext, ".xml")) return "application/xml";
if (iequals(ext, ".swf")) return "application/x-shockwave-flash";
if (iequals(ext, ".flv")) return "video/x-flv";
if (iequals(ext, ".png")) return "image/png";
if (iequals(ext, ".jpe")) return "image/jpeg";
if (iequals(ext, ".jpeg")) return "image/jpeg";
if (iequals(ext, ".jpg")) return "image/jpeg";
if (iequals(ext, ".gif")) return "image/gif";
if (iequals(ext, ".bmp")) return "image/bmp";
if (iequals(ext, ".ico")) return "image/vnd.microsoft.icon";
if (iequals(ext, ".tiff")) return "image/tiff";
if (iequals(ext, ".tif")) return "image/tiff";
if (iequals(ext, ".svg")) return "image/svg+xml";
if (iequals(ext, ".svgz")) return "image/svg+xml";
return "application/text";
}
std::string server::path_cat(beast::string_view base, beast::string_view path)
{
if (base.empty())
return std::string(path);
std::string res(base);
constexpr char path_sep = '\\';
if (res.back() == path_sep)
res.pop_back();
res.append(path.data(), path.size());
for (auto& c : res)
{
if (c == '/')
c = path_sep;
}
return res;
}
void server::do_session(tcp::socket& sock, std::shared_ptr<const std::string>& rootdir)
{
beast::flat_buffer buff;
beast::error_code ec;
const auto remote_endpoint = sock.remote_endpoint();
const std::string client_ip = remote_endpoint.address().to_string();
const uint16_t client_port = remote_endpoint.port();
std::cout << "Client connected: " << client_ip << ":" << client_port << "\n";
for (;;)
{
http::request<http::string_body> req;
http::read(sock, buff, req, ec);
if (ec == http::error::end_of_stream)
break;
if (ec)
return fail(ec, "read");
http::message_generator msg = generate_response(*rootdir, std::move(req));
bool keep_alive = msg.keep_alive();
beast::write(sock, std::move(msg), ec);
if (ec)
return fail(ec, "write");
if (!keep_alive)
break;
}
std::cout << "disconnecting!\n";
sock.shutdown(tcp::socket::shutdown_send, ec);
}
int server::start(const net::ip::address addr, const unsigned short port, const std::string& rootdir)
{
try {
m_addr = addr;
m_port = port;
m_rootdir = std::make_shared<const std::string>(rootdir);
tcp::acceptor acceptor{ ioc, {m_addr, m_port} };
for (;;)
{
tcp::socket sock{ ioc };
acceptor.accept(sock);
std::thread tr{ std::bind(&server::do_session, this, std::move(sock), m_rootdir)};
tr.detach();
}
}
catch (const std::exception& ex)
{
std::cerr << "Error what : " << ex.what() << '\n';
return EXIT_FAILURE;
}
return 0;
}
}