2 * nghttp2 - HTTP/2 C Library
4 * Copyright (c) 2014 Tatsuhiro Tsujikawa
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 // We wrote this code based on the original code which has the
31 // Copyright (c) 2003-2013 Christopher M. Kohlhoff (chris at kohlhoff dot com)
33 // Distributed under the Boost Software License, Version 1.0. (See accompanying
34 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
36 #include <sys/types.h>
40 #endif // HAVE_UNISTD_H
43 #endif // HAVE_FCNTL_H
47 #include <nghttp2/asio_http2_server.h>
49 using namespace nghttp2::asio_http2;
50 using namespace nghttp2::asio_http2::server;
52 int main(int argc, char *argv[]) {
54 // Check command line arguments.
56 std::cerr << "Usage: asio-sv2 <address> <port> <threads> <doc-root> "
57 << "[<private-key-file> <cert-file>]\n";
61 boost::system::error_code ec;
63 std::string addr = argv[1];
64 std::string port = argv[2];
65 std::size_t num_threads = std::stoi(argv[3]);
66 std::string docroot = argv[4];
70 server.num_threads(num_threads);
72 server.handle("/", [&docroot](const request &req, const response &res) {
73 auto path = percent_decode(req.uri().path);
74 if (!check_path(path)) {
84 path = docroot + path;
85 auto fd = open(path.c_str(), O_RDONLY);
92 auto header = header_map();
95 if (stat(path.c_str(), &stbuf) == 0) {
96 header.emplace("content-length",
97 header_value{std::to_string(stbuf.st_size)});
98 header.emplace("last-modified",
99 header_value{http_date(stbuf.st_mtime)});
101 res.write_head(200, std::move(header));
102 res.end(file_generator_from_fd(fd));
106 boost::asio::ssl::context tls(boost::asio::ssl::context::sslv23);
107 tls.use_private_key_file(argv[5], boost::asio::ssl::context::pem);
108 tls.use_certificate_chain_file(argv[6]);
110 configure_tls_context_easy(ec, tls);
112 if (server.listen_and_serve(ec, tls, addr, port)) {
113 std::cerr << "error: " << ec.message() << std::endl;
116 if (server.listen_and_serve(ec, addr, port)) {
117 std::cerr << "error: " << ec.message() << std::endl;
120 } catch (std::exception &e) {
121 std::cerr << "exception: " << e.what() << "\n";