2 * nghttp2 - HTTP/2 C Library
4 * Copyright (c) 2015 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 #include "asio_server_request_handler.h"
31 namespace asio_http2 {
35 std::string create_html(int status_code) {
38 auto status = ::nghttp2::http2::get_status_string(status_code);
39 res += R"(<!DOCTYPE html><html lang="en"><title>)";
41 res += "</title><body><h1>";
43 res += "</h1></body></html>";
48 request_cb redirect_handler(int status_code, std::string uri) {
49 return [status_code, uri](const request &req, const response &res) {
51 h.emplace("location", header_value{std::move(uri)});
53 if (req.method() == "GET") {
54 html = create_html(status_code);
56 h.emplace("content-length", header_value{util::utos(html.size())});
58 res.write_head(status_code, std::move(h));
59 res.end(std::move(html));
63 request_cb status_handler(int status_code) {
64 return [status_code](const request &req, const response &res) {
65 if (!::nghttp2::http2::expect_response_body(status_code)) {
66 res.write_head(status_code);
70 // we supply content-length for HEAD request, but body will not be
72 auto html = create_html(status_code);
74 h.emplace("content-length", header_value{util::utos(html.size())});
75 h.emplace("content-type", header_value{"text/html; charset=utf-8"});
77 res.write_head(status_code, std::move(h));
78 res.end(std::move(html));
83 } // namespace asio_http2
84 } // namespace nghttp2