2012-06-04 16:48:31 +02:00
|
|
|
/*
|
2014-03-30 12:09:21 +02:00
|
|
|
* nghttp2 - HTTP/2 C Library
|
2012-06-04 16:48:31 +02:00
|
|
|
*
|
|
|
|
* Copyright (c) 2012 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
#include "shrpx_http.h"
|
|
|
|
|
|
|
|
#include "shrpx_config.h"
|
2012-12-09 13:36:02 +01:00
|
|
|
#include "shrpx_log.h"
|
2013-08-27 19:47:22 +02:00
|
|
|
#include "http2.h"
|
2012-06-12 17:08:28 +02:00
|
|
|
#include "util.h"
|
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
using namespace nghttp2;
|
2012-06-12 17:08:28 +02:00
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
namespace shrpx {
|
|
|
|
|
|
|
|
namespace http {
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
std::string create_error_html(unsigned int status_code) {
|
2013-01-16 14:43:11 +01:00
|
|
|
std::string res;
|
|
|
|
res.reserve(512);
|
2013-10-02 16:29:44 +02:00
|
|
|
auto status = http2::get_status_string(status_code);
|
2015-03-05 15:34:42 +01:00
|
|
|
res += R"(<!DOCTYPE html><html lang="en"><title>)";
|
2013-01-16 14:43:11 +01:00
|
|
|
res += status;
|
2015-01-22 15:51:22 +01:00
|
|
|
res += "</title><body><h1>";
|
2013-01-16 14:43:11 +01:00
|
|
|
res += status;
|
2015-01-22 15:51:22 +01:00
|
|
|
res += "</h1><footer>";
|
2016-01-18 09:00:20 +01:00
|
|
|
const auto &server_name = get_config()->http.server_name;
|
2016-01-16 17:15:11 +01:00
|
|
|
res.append(server_name.c_str(), server_name.size());
|
2015-01-22 15:51:22 +01:00
|
|
|
res += "</footer></body></html>";
|
2013-01-16 14:43:11 +01:00
|
|
|
return res;
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
|
2016-01-31 11:41:56 +01:00
|
|
|
std::string create_forwarded(int params, const StringRef &node_by,
|
2016-02-28 15:54:02 +01:00
|
|
|
const StringRef &node_for, const StringRef &host,
|
|
|
|
const StringRef &proto) {
|
2016-01-15 15:04:58 +01:00
|
|
|
std::string res;
|
|
|
|
if ((params & FORWARDED_BY) && !node_by.empty()) {
|
2016-01-19 14:27:09 +01:00
|
|
|
// This must be quoted-string unless it is obfuscated version
|
2016-02-01 15:31:21 +01:00
|
|
|
// (which starts with "_") or some special value (e.g.,
|
|
|
|
// "localhost" for UNIX domain socket), since ':' is not allowed
|
|
|
|
// in token. ':' is used to separate host and port.
|
|
|
|
if (node_by[0] == '_' || node_by[0] == 'l') {
|
2016-01-19 14:27:09 +01:00
|
|
|
res += "by=";
|
|
|
|
res += node_by;
|
|
|
|
res += ";";
|
|
|
|
} else {
|
|
|
|
res += "by=\"";
|
|
|
|
res += node_by;
|
|
|
|
res += "\";";
|
|
|
|
}
|
2016-01-15 15:04:58 +01:00
|
|
|
}
|
|
|
|
if ((params & FORWARDED_FOR) && !node_for.empty()) {
|
2016-01-19 14:27:09 +01:00
|
|
|
// We only quote IPv6 literal address only, which starts with '['.
|
|
|
|
if (node_for[0] == '[') {
|
|
|
|
res += "for=\"";
|
|
|
|
res += node_for;
|
|
|
|
res += "\";";
|
|
|
|
} else {
|
|
|
|
res += "for=";
|
|
|
|
res += node_for;
|
|
|
|
res += ";";
|
|
|
|
}
|
2016-01-15 15:04:58 +01:00
|
|
|
}
|
|
|
|
if ((params & FORWARDED_HOST) && !host.empty()) {
|
2016-01-19 14:27:09 +01:00
|
|
|
// Just be quoted to skip checking characters.
|
2016-01-15 15:04:58 +01:00
|
|
|
res += "host=\"";
|
|
|
|
res += host;
|
|
|
|
res += "\";";
|
|
|
|
}
|
|
|
|
if ((params & FORWARDED_PROTO) && !proto.empty()) {
|
2016-01-19 14:27:09 +01:00
|
|
|
// Scheme production rule only allow characters which are all in
|
|
|
|
// token.
|
|
|
|
res += "proto=";
|
2016-01-15 15:04:58 +01:00
|
|
|
res += proto;
|
2016-01-19 14:27:09 +01:00
|
|
|
res += ";";
|
2016-01-15 15:04:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (res.empty()) {
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
res.erase(res.size() - 1);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
std::string colorizeHeaders(const char *hdrs) {
|
2012-12-09 13:36:02 +01:00
|
|
|
std::string nhdrs;
|
|
|
|
const char *p = strchr(hdrs, '\n');
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!p) {
|
2012-12-09 13:36:02 +01:00
|
|
|
// Not valid HTTP header
|
|
|
|
return hdrs;
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
nhdrs.append(hdrs, p + 1);
|
2012-12-09 13:36:02 +01:00
|
|
|
++p;
|
2014-11-27 15:39:04 +01:00
|
|
|
while (1) {
|
|
|
|
const char *np = strchr(p, ':');
|
|
|
|
if (!np) {
|
2012-12-09 13:36:02 +01:00
|
|
|
nhdrs.append(p);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
nhdrs += TTY_HTTP_HD;
|
|
|
|
nhdrs.append(p, np);
|
|
|
|
nhdrs += TTY_RST;
|
|
|
|
p = np;
|
|
|
|
np = strchr(p, '\n');
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!np) {
|
2012-12-09 13:36:02 +01:00
|
|
|
nhdrs.append(p);
|
|
|
|
break;
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
nhdrs.append(p, np + 1);
|
|
|
|
p = np + 1;
|
2012-12-09 13:36:02 +01:00
|
|
|
}
|
|
|
|
return nhdrs;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
ssize_t select_padding_callback(nghttp2_session *session,
|
|
|
|
const nghttp2_frame *frame, size_t max_payload,
|
|
|
|
void *user_data) {
|
2014-02-15 08:40:32 +01:00
|
|
|
return std::min(max_payload, frame->hd.length + get_config()->padding);
|
2014-02-11 09:23:22 +01:00
|
|
|
}
|
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
} // namespace http
|
|
|
|
|
|
|
|
} // namespace shrpx
|