Imported Upstream version 1.0.0
[platform/upstream/nghttp2.git] / src / asio_server_request_handler.cc
1 /*
2  * nghttp2 - HTTP/2 C Library
3  *
4  * Copyright (c) 2015 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 #include "asio_server_request_handler.h"
26
27 #include "util.h"
28 #include "http2.h"
29
30 namespace nghttp2 {
31 namespace asio_http2 {
32 namespace server {
33
34 namespace {
35 std::string create_html(int status_code) {
36   std::string res;
37   res.reserve(512);
38   auto status = ::nghttp2::http2::get_status_string(status_code);
39   res += R"(<!DOCTYPE html><html lang="en"><title>)";
40   res += status;
41   res += "</title><body><h1>";
42   res += status;
43   res += "</h1></body></html>";
44   return res;
45 }
46 } // namespace
47
48 request_cb redirect_handler(int status_code, std::string uri) {
49   return [status_code, uri](const request &req, const response &res) {
50     header_map h;
51     h.emplace("location", header_value{std::move(uri)});
52     std::string html;
53     if (req.method() == "GET") {
54       html = create_html(status_code);
55     }
56     h.emplace("content-length", header_value{util::utos(html.size())});
57
58     res.write_head(status_code, std::move(h));
59     res.end(std::move(html));
60   };
61 }
62
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);
67       res.end();
68       return;
69     }
70     // we supply content-length for HEAD request, but body will not be
71     // sent.
72     auto html = create_html(status_code);
73     header_map h;
74     h.emplace("content-length", header_value{util::utos(html.size())});
75     h.emplace("content-type", header_value{"text/html; charset=utf-8"});
76
77     res.write_head(status_code, std::move(h));
78     res.end(std::move(html));
79   };
80 }
81
82 } // namespace server
83 } // namespace asio_http2
84 } // namespace nghttp2