2 // Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 // Official repository: https://github.com/boostorg/beast
10 //------------------------------------------------------------------------------
12 // Example: HTTP SSL server, asynchronous
14 //------------------------------------------------------------------------------
16 #include "example/common/server_certificate.hpp"
18 #include <boost/beast/core.hpp>
19 #include <boost/beast/http.hpp>
20 #include <boost/beast/version.hpp>
21 #include <boost/asio/bind_executor.hpp>
22 #include <boost/asio/ip/tcp.hpp>
23 #include <boost/asio/ssl/stream.hpp>
24 #include <boost/asio/strand.hpp>
25 #include <boost/config.hpp>
35 using tcp
= boost::asio::ip::tcp
; // from <boost/asio/ip/tcp.hpp>
36 namespace ssl
= boost::asio::ssl
; // from <boost/asio/ssl.hpp>
37 namespace http
= boost::beast::http
; // from <boost/beast/http.hpp>
39 // Return a reasonable mime type based on the extension of a file.
40 boost::beast::string_view
41 mime_type(boost::beast::string_view path
)
43 using boost::beast::iequals
;
44 auto const ext
= [&path
]
46 auto const pos
= path
.rfind(".");
47 if(pos
== boost::beast::string_view::npos
)
48 return boost::beast::string_view
{};
49 return path
.substr(pos
);
51 if(iequals(ext
, ".htm")) return "text/html";
52 if(iequals(ext
, ".html")) return "text/html";
53 if(iequals(ext
, ".php")) return "text/html";
54 if(iequals(ext
, ".css")) return "text/css";
55 if(iequals(ext
, ".txt")) return "text/plain";
56 if(iequals(ext
, ".js")) return "application/javascript";
57 if(iequals(ext
, ".json")) return "application/json";
58 if(iequals(ext
, ".xml")) return "application/xml";
59 if(iequals(ext
, ".swf")) return "application/x-shockwave-flash";
60 if(iequals(ext
, ".flv")) return "video/x-flv";
61 if(iequals(ext
, ".png")) return "image/png";
62 if(iequals(ext
, ".jpe")) return "image/jpeg";
63 if(iequals(ext
, ".jpeg")) return "image/jpeg";
64 if(iequals(ext
, ".jpg")) return "image/jpeg";
65 if(iequals(ext
, ".gif")) return "image/gif";
66 if(iequals(ext
, ".bmp")) return "image/bmp";
67 if(iequals(ext
, ".ico")) return "image/vnd.microsoft.icon";
68 if(iequals(ext
, ".tiff")) return "image/tiff";
69 if(iequals(ext
, ".tif")) return "image/tiff";
70 if(iequals(ext
, ".svg")) return "image/svg+xml";
71 if(iequals(ext
, ".svgz")) return "image/svg+xml";
72 return "application/text";
75 // Append an HTTP rel-path to a local filesystem path.
76 // The returned path is normalized for the platform.
79 boost::beast::string_view base
,
80 boost::beast::string_view path
)
83 return path
.to_string();
84 std::string result
= base
.to_string();
86 char constexpr path_separator
= '\\';
87 if(result
.back() == path_separator
)
88 result
.resize(result
.size() - 1);
89 result
.append(path
.data(), path
.size());
94 char constexpr path_separator
= '/';
95 if(result
.back() == path_separator
)
96 result
.resize(result
.size() - 1);
97 result
.append(path
.data(), path
.size());
102 // This function produces an HTTP response for the given
103 // request. The type of the response object depends on the
104 // contents of the request, so the interface requires the
105 // caller to pass a generic lambda for receiving the response.
107 class Body
, class Allocator
,
111 boost::beast::string_view doc_root
,
112 http::request
<Body
, http::basic_fields
<Allocator
>>&& req
,
115 // Returns a bad request response
116 auto const bad_request
=
117 [&req
](boost::beast::string_view why
)
119 http::response
<http::string_body
> res
{http::status::bad_request
, req
.version()};
120 res
.set(http::field::server
, BOOST_BEAST_VERSION_STRING
);
121 res
.set(http::field::content_type
, "text/html");
122 res
.keep_alive(req
.keep_alive());
123 res
.body() = why
.to_string();
124 res
.prepare_payload();
128 // Returns a not found response
129 auto const not_found
=
130 [&req
](boost::beast::string_view target
)
132 http::response
<http::string_body
> res
{http::status::not_found
, req
.version()};
133 res
.set(http::field::server
, BOOST_BEAST_VERSION_STRING
);
134 res
.set(http::field::content_type
, "text/html");
135 res
.keep_alive(req
.keep_alive());
136 res
.body() = "The resource '" + target
.to_string() + "' was not found.";
137 res
.prepare_payload();
141 // Returns a server error response
142 auto const server_error
=
143 [&req
](boost::beast::string_view what
)
145 http::response
<http::string_body
> res
{http::status::internal_server_error
, req
.version()};
146 res
.set(http::field::server
, BOOST_BEAST_VERSION_STRING
);
147 res
.set(http::field::content_type
, "text/html");
148 res
.keep_alive(req
.keep_alive());
149 res
.body() = "An error occurred: '" + what
.to_string() + "'";
150 res
.prepare_payload();
154 // Make sure we can handle the method
155 if( req
.method() != http::verb::get
&&
156 req
.method() != http::verb::head
)
157 return send(bad_request("Unknown HTTP-method"));
159 // Request path must be absolute and not contain "..".
160 if( req
.target().empty() ||
161 req
.target()[0] != '/' ||
162 req
.target().find("..") != boost::beast::string_view::npos
)
163 return send(bad_request("Illegal request-target"));
165 // Build the path to the requested file
166 std::string path
= path_cat(doc_root
, req
.target());
167 if(req
.target().back() == '/')
168 path
.append("index.html");
170 // Attempt to open the file
171 boost::beast::error_code ec
;
172 http::file_body::value_type body
;
173 body
.open(path
.c_str(), boost::beast::file_mode::scan
, ec
);
175 // Handle the case where the file doesn't exist
176 if(ec
== boost::system::errc::no_such_file_or_directory
)
177 return send(not_found(req
.target()));
179 // Handle an unknown error
181 return send(server_error(ec
.message()));
183 // Respond to HEAD request
184 if(req
.method() == http::verb::head
)
186 http::response
<http::empty_body
> res
{http::status::ok
, req
.version()};
187 res
.set(http::field::server
, BOOST_BEAST_VERSION_STRING
);
188 res
.set(http::field::content_type
, mime_type(path
));
189 res
.content_length(body
.size());
190 res
.keep_alive(req
.keep_alive());
191 return send(std::move(res
));
194 // Respond to GET request
195 http::response
<http::file_body
> res
{
196 std::piecewise_construct
,
197 std::make_tuple(std::move(body
)),
198 std::make_tuple(http::status::ok
, req
.version())};
199 res
.set(http::field::server
, BOOST_BEAST_VERSION_STRING
);
200 res
.set(http::field::content_type
, mime_type(path
));
201 res
.content_length(body
.size());
202 res
.keep_alive(req
.keep_alive());
203 return send(std::move(res
));
206 //------------------------------------------------------------------------------
210 fail(boost::system::error_code ec
, char const* what
)
212 std::cerr
<< what
<< ": " << ec
.message() << "\n";
215 // Handles an HTTP server connection
216 class session
: public std::enable_shared_from_this
<session
>
218 // This is the C++11 equivalent of a generic lambda.
219 // The function object is used to send an HTTP message.
225 send_lambda(session
& self
)
230 template<bool isRequest
, class Body
, class Fields
>
232 operator()(http::message
<isRequest
, Body
, Fields
>&& msg
) const
234 // The lifetime of the message has to extend
235 // for the duration of the async operation so
236 // we use a shared_ptr to manage it.
237 auto sp
= std::make_shared
<
238 http::message
<isRequest
, Body
, Fields
>>(std::move(msg
));
240 // Store a type-erased version of the shared
241 // pointer in the class to keep it alive.
244 // Write the response
248 boost::asio::bind_executor(
252 self_
.shared_from_this(),
253 std::placeholders::_1
,
254 std::placeholders::_2
,
260 ssl::stream
<tcp::socket
&> stream_
;
262 boost::asio::io_context::executor_type
> strand_
;
263 boost::beast::flat_buffer buffer_
;
264 std::string
const& doc_root_
;
265 http::request
<http::string_body
> req_
;
266 std::shared_ptr
<void> res_
;
270 // Take ownership of the socket
275 std::string
const& doc_root
)
276 : socket_(std::move(socket
))
277 , stream_(socket_
, ctx
)
278 , strand_(socket_
.get_executor())
279 , doc_root_(doc_root
)
284 // Start the asynchronous operation
288 // Perform the SSL handshake
289 stream_
.async_handshake(
290 ssl::stream_base::server
,
291 boost::asio::bind_executor(
294 &session::on_handshake
,
296 std::placeholders::_1
)));
300 on_handshake(boost::system::error_code ec
)
303 return fail(ec
, "handshake");
312 http::async_read(stream_
, buffer_
, req_
,
313 boost::asio::bind_executor(
318 std::placeholders::_1
,
319 std::placeholders::_2
)));
324 boost::system::error_code ec
,
325 std::size_t bytes_transferred
)
327 boost::ignore_unused(bytes_transferred
);
329 // This means they closed the connection
330 if(ec
== http::error::end_of_stream
)
334 return fail(ec
, "read");
337 handle_request(doc_root_
, std::move(req_
), lambda_
);
342 boost::system::error_code ec
,
343 std::size_t bytes_transferred
,
346 boost::ignore_unused(bytes_transferred
);
349 return fail(ec
, "write");
353 // This means we should close the connection, usually because
354 // the response indicated the "Connection: close" semantic.
358 // We're done with the response so delete it
361 // Read another request
368 // Perform the SSL shutdown
369 stream_
.async_shutdown(
370 boost::asio::bind_executor(
373 &session::on_shutdown
,
375 std::placeholders::_1
)));
379 on_shutdown(boost::system::error_code ec
)
382 return fail(ec
, "shutdown");
384 // At this point the connection is closed gracefully
388 //------------------------------------------------------------------------------
390 // Accepts incoming connections and launches the sessions
391 class listener
: public std::enable_shared_from_this
<listener
>
394 tcp::acceptor acceptor_
;
396 std::string
const& doc_root_
;
400 boost::asio::io_context
& ioc
,
402 tcp::endpoint endpoint
,
403 std::string
const& doc_root
)
407 , doc_root_(doc_root
)
409 boost::system::error_code ec
;
412 acceptor_
.open(endpoint
.protocol(), ec
);
419 // Bind to the server address
420 acceptor_
.bind(endpoint
, ec
);
427 // Start listening for connections
429 boost::asio::socket_base::max_listen_connections
, ec
);
437 // Start accepting incoming connections
441 if(! acceptor_
.is_open())
449 acceptor_
.async_accept(
452 &listener::on_accept
,
454 std::placeholders::_1
));
458 on_accept(boost::system::error_code ec
)
466 // Create the session and run it
467 std::make_shared
<session
>(
473 // Accept another connection
478 //------------------------------------------------------------------------------
480 int main(int argc
, char* argv
[])
482 // Check command line arguments.
486 "Usage: http-server-async-ssl <address> <port> <doc_root> <threads>\n" <<
488 " http-server-async-ssl 0.0.0.0 8080 . 1\n";
491 auto const address
= boost::asio::ip::make_address(argv
[1]);
492 auto const port
= static_cast<unsigned short>(std::atoi(argv
[2]));
493 std::string
const doc_root
= argv
[3];
494 auto const threads
= std::max
<int>(1, std::atoi(argv
[4]));
496 // The io_context is required for all I/O
497 boost::asio::io_context ioc
{threads
};
499 // The SSL context is required, and holds certificates
500 ssl::context ctx
{ssl::context::sslv23
};
502 // This holds the self-signed certificate used by the server
503 load_server_certificate(ctx
);
505 // Create and launch a listening port
506 std::make_shared
<listener
>(
509 tcp::endpoint
{address
, port
},
512 // Run the I/O service on the requested number of threads
513 std::vector
<std::thread
> v
;
514 v
.reserve(threads
- 1);
515 for(auto i
= threads
- 1; i
> 0; --i
)