Imported Upstream version 1.0.0
[platform/upstream/nghttp2.git] / src / shrpx_http.cc
1 /*
2  * nghttp2 - HTTP/2 C Library
3  *
4  * Copyright (c) 2012 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 "shrpx_http.h"
26
27 #include "shrpx_config.h"
28 #include "shrpx_log.h"
29 #include "http2.h"
30 #include "util.h"
31
32 using namespace nghttp2;
33
34 namespace shrpx {
35
36 namespace http {
37
38 std::string create_error_html(unsigned int status_code) {
39   std::string res;
40   res.reserve(512);
41   auto status = http2::get_status_string(status_code);
42   res += R"(<!DOCTYPE html><html lang="en"><title>)";
43   res += status;
44   res += "</title><body><h1>";
45   res += status;
46   res += "</h1><footer>";
47   res += get_config()->server_name;
48   res += " at port ";
49   res += util::utos(get_config()->port);
50   res += "</footer></body></html>";
51   return res;
52 }
53
54 std::string create_via_header_value(int major, int minor) {
55   std::string hdrs;
56   hdrs += static_cast<char>(major + '0');
57   if (major < 2) {
58     hdrs += ".";
59     hdrs += static_cast<char>(minor + '0');
60   }
61   hdrs += " nghttpx";
62   return hdrs;
63 }
64
65 std::string colorizeHeaders(const char *hdrs) {
66   std::string nhdrs;
67   const char *p = strchr(hdrs, '\n');
68   if (!p) {
69     // Not valid HTTP header
70     return hdrs;
71   }
72   nhdrs.append(hdrs, p + 1);
73   ++p;
74   while (1) {
75     const char *np = strchr(p, ':');
76     if (!np) {
77       nhdrs.append(p);
78       break;
79     }
80     nhdrs += TTY_HTTP_HD;
81     nhdrs.append(p, np);
82     nhdrs += TTY_RST;
83     p = np;
84     np = strchr(p, '\n');
85     if (!np) {
86       nhdrs.append(p);
87       break;
88     }
89     nhdrs.append(p, np + 1);
90     p = np + 1;
91   }
92   return nhdrs;
93 }
94
95 ssize_t select_padding_callback(nghttp2_session *session,
96                                 const nghttp2_frame *frame, size_t max_payload,
97                                 void *user_data) {
98   return std::min(max_payload, frame->hd.length + get_config()->padding);
99 }
100
101 } // namespace http
102
103 } // namespace shrpx