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
37 #include <iostream>
38 #include <string>
39
40 #include <nghttp2/asio_http2_server.h>
41
42 using namespace nghttp2::asio_http2;
43 using namespace nghttp2::asio_http2::server;
44
main(int argc,char * argv[])45 int main(int argc, char *argv[]) {
46 try {
47 // Check command line arguments.
48 if (argc < 4) {
49 std::cerr
50 << "Usage: asio-sv <address> <port> <threads> [<private-key-file> "
51 << "<cert-file>]\n";
52 return 1;
53 }
54
55 boost::system::error_code ec;
56
57 std::string addr = argv[1];
58 std::string port = argv[2];
59 std::size_t num_threads = std::stoi(argv[3]);
60
61 http2 server;
62
63 server.num_threads(num_threads);
64
65 server.handle("/", [](const request &req, const response &res) {
66 res.write_head(200, {{"foo", {"bar"}}});
67 res.end("hello, world\n");
68 });
69 server.handle("/secret/", [](const request &req, const response &res) {
70 res.write_head(200);
71 res.end("under construction!\n");
72 });
73 server.handle("/push", [](const request &req, const response &res) {
74 boost::system::error_code ec;
75 auto push = res.push(ec, "GET", "/push/1");
76 if (!ec) {
77 push->write_head(200);
78 push->end("server push FTW!\n");
79 }
80
81 res.write_head(200);
82 res.end("you'll receive server push!\n");
83 });
84 server.handle("/delay", [](const request &req, const response &res) {
85 res.write_head(200);
86
87 auto timer = std::make_shared<boost::asio::deadline_timer>(
88 res.io_service(), boost::posix_time::seconds(3));
89 auto closed = std::make_shared<bool>();
90
91 res.on_close([timer, closed](uint32_t error_code) {
92 timer->cancel();
93 *closed = true;
94 });
95
96 timer->async_wait([&res, closed](const boost::system::error_code &ec) {
97 if (ec || *closed) {
98 return;
99 }
100
101 res.end("finally!\n");
102 });
103 });
104 server.handle("/trailer", [](const request &req, const response &res) {
105 // send trailer part.
106 res.write_head(200, {{"trailers", {"digest"}}});
107
108 std::string body = "nghttp2 FTW!\n";
109 auto left = std::make_shared<size_t>(body.size());
110
111 res.end([&res, body, left](uint8_t *dst, std::size_t len,
112 uint32_t *data_flags) {
113 auto n = std::min(len, *left);
114 std::copy_n(body.c_str() + (body.size() - *left), n, dst);
115 *left -= n;
116 if (*left == 0) {
117 *data_flags |=
118 NGHTTP2_DATA_FLAG_EOF | NGHTTP2_DATA_FLAG_NO_END_STREAM;
119 // RFC 3230 Instance Digests in HTTP. The digest value is
120 // SHA-256 message digest of body.
121 res.write_trailer(
122 {{"digest",
123 {"SHA-256=qqXqskW7F3ueBSvmZRCiSwl2ym4HRO0M/pvQCBlSDis="}}});
124 }
125 return n;
126 });
127 });
128
129 if (argc >= 6) {
130 boost::asio::ssl::context tls(boost::asio::ssl::context::sslv23);
131 tls.use_private_key_file(argv[4], boost::asio::ssl::context::pem);
132 tls.use_certificate_chain_file(argv[5]);
133
134 configure_tls_context_easy(ec, tls);
135
136 if (server.listen_and_serve(ec, tls, addr, port)) {
137 std::cerr << "error: " << ec.message() << std::endl;
138 }
139 } else {
140 if (server.listen_and_serve(ec, addr, port)) {
141 std::cerr << "error: " << ec.message() << std::endl;
142 }
143 }
144 } catch (std::exception &e) {
145 std::cerr << "exception: " << e.what() << "\n";
146 }
147
148 return 0;
149 }
150