• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * nghttp2 - HTTP/2 C Library
3  *
4  * Copyright (c) 2014 Tatsuhiro Tsujikawa
5  *
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:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
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.
24  */
25 // We wrote this code based on the original code which has the
26 // following license:
27 //
28 // main.cpp
29 // ~~~~~~~~
30 //
31 // Copyright (c) 2003-2013 Christopher M. Kohlhoff (chris at kohlhoff dot com)
32 //
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)
35 //
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #ifdef HAVE_UNISTD_H
39 #  include <unistd.h>
40 #endif // HAVE_UNISTD_H
41 #ifdef HAVE_FCNTL_H
42 #  include <fcntl.h>
43 #endif // HAVE_FCNTL_H
44 #include <iostream>
45 #include <string>
46 
47 #include <nghttp2/asio_http2_server.h>
48 
49 using namespace nghttp2::asio_http2;
50 using namespace nghttp2::asio_http2::server;
51 
main(int argc,char * argv[])52 int main(int argc, char *argv[]) {
53   try {
54     // Check command line arguments.
55     if (argc < 5) {
56       std::cerr << "Usage: asio-sv2 <address> <port> <threads> <doc-root> "
57                 << "[<private-key-file> <cert-file>]\n";
58       return 1;
59     }
60 
61     boost::system::error_code ec;
62 
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];
67 
68     http2 server;
69 
70     server.num_threads(num_threads);
71 
72     server.handle("/", [&docroot](const request &req, const response &res) {
73       auto path = percent_decode(req.uri().path);
74       if (!check_path(path)) {
75         res.write_head(404);
76         res.end();
77         return;
78       }
79 
80       if (path == "/") {
81         path = "/index.html";
82       }
83 
84       path = docroot + path;
85       auto fd = open(path.c_str(), O_RDONLY);
86       if (fd == -1) {
87         res.write_head(404);
88         res.end();
89         return;
90       }
91 
92       auto header = header_map();
93 
94       struct stat stbuf;
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)});
100       }
101       res.write_head(200, std::move(header));
102       res.end(file_generator_from_fd(fd));
103     });
104 
105     if (argc >= 7) {
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]);
109 
110       configure_tls_context_easy(ec, tls);
111 
112       if (server.listen_and_serve(ec, tls, addr, port)) {
113         std::cerr << "error: " << ec.message() << std::endl;
114       }
115     } else {
116       if (server.listen_and_serve(ec, addr, port)) {
117         std::cerr << "error: " << ec.message() << std::endl;
118       }
119     }
120   } catch (std::exception &e) {
121     std::cerr << "exception: " << e.what() << "\n";
122   }
123 
124   return 0;
125 }
126