Commit 28469125 authored by Tatsuhiro Tsujikawa's avatar Tatsuhiro Tsujikawa

nghttpx: Use StringRef for http::create_forwarded parameter

parent 124d4c9f
......@@ -1138,18 +1138,18 @@ int ClientHandler::proxy_protocol_read() {
return on_proxy_protocol_finish();
}
StringRef ClientHandler::get_forwarded_by() {
StringRef ClientHandler::get_forwarded_by() const {
auto &fwdconf = get_config()->http.forwarded;
if (fwdconf.by_node_type == FORWARDED_NODE_OBFUSCATED) {
return StringRef(fwdconf.by_obfuscated);
}
return StringRef(faddr_->hostport);
return StringRef{faddr_->hostport};
}
const std::string &ClientHandler::get_forwarded_for() const {
return forwarded_for_;
StringRef ClientHandler::get_forwarded_for() const {
return StringRef{forwarded_for_};
}
} // namespace shrpx
......@@ -135,10 +135,10 @@ public:
// Returns string suitable for use in "by" parameter of Forwarded
// header field.
StringRef get_forwarded_by();
StringRef get_forwarded_by() const;
// Returns string suitable for use in "for" parameter of Forwarded
// header field.
const std::string &get_forwarded_for() const;
StringRef get_forwarded_for() const;
private:
Connection conn_;
......
......@@ -62,9 +62,8 @@ std::string create_via_header_value(int major, int minor) {
}
std::string create_forwarded(int params, const StringRef &node_by,
const std::string &node_for,
const std::string &host,
const std::string &proto) {
const StringRef &node_for, const StringRef &host,
const StringRef &proto) {
std::string res;
if ((params & FORWARDED_BY) && !node_by.empty()) {
// This must be quoted-string unless it is obfuscated version
......
......@@ -43,8 +43,8 @@ std::string create_via_header_value(int major, int minor);
// |params| is bitwise-OR of zero or more of shrpx_forwarded_param
// defined in shrpx_config.h.
std::string create_forwarded(int params, const StringRef &node_by,
const std::string &node_for,
const std::string &host, const std::string &proto);
const StringRef &node_for, const StringRef &host,
const StringRef &proto);
// Adds ANSI color codes to HTTP headers |hdrs|.
std::string colorizeHeaders(const char *hdrs);
......
......@@ -359,9 +359,9 @@ int Http2DownstreamConnection::push_request_headers() {
params &= ~FORWARDED_PROTO;
}
auto value = http::create_forwarded(params, handler->get_forwarded_by(),
handler->get_forwarded_for(),
req.authority, req.scheme);
auto value = http::create_forwarded(
params, handler->get_forwarded_by(), handler->get_forwarded_for(),
StringRef{req.authority}, StringRef{req.scheme});
if (fwd || !value.empty()) {
if (fwd) {
forwarded_value = fwd->value;
......
......@@ -366,9 +366,9 @@ int HttpDownstreamConnection::push_request_headers() {
params &= ~FORWARDED_PROTO;
}
auto value = http::create_forwarded(params, handler->get_forwarded_by(),
handler->get_forwarded_for(),
req.authority, req.scheme);
auto value = http::create_forwarded(
params, handler->get_forwarded_by(), handler->get_forwarded_for(),
StringRef{req.authority}, StringRef{req.scheme});
if (fwd || !value.empty()) {
buf->append("Forwarded: ");
if (fwd) {
......
......@@ -43,25 +43,33 @@ void test_shrpx_http_create_forwarded(void) {
http::create_forwarded(FORWARDED_BY | FORWARDED_FOR |
FORWARDED_HOST | FORWARDED_PROTO,
StringRef::from_lit("example.com:3000"),
"[::1]", "www.example.com", "https"));
StringRef::from_lit("[::1]"),
StringRef::from_lit("www.example.com"),
StringRef::from_lit("https")));
CU_ASSERT("for=192.168.0.1" ==
http::create_forwarded(FORWARDED_FOR, StringRef::from_lit("alpha"),
"192.168.0.1", "bravo", "charlie"));
StringRef::from_lit("192.168.0.1"),
StringRef::from_lit("bravo"),
StringRef::from_lit("charlie")));
CU_ASSERT("by=_hidden;for=\"[::1]\"" ==
http::create_forwarded(FORWARDED_BY | FORWARDED_FOR,
StringRef::from_lit("_hidden"), "[::1]", "",
""));
http::create_forwarded(
FORWARDED_BY | FORWARDED_FOR, StringRef::from_lit("_hidden"),
StringRef::from_lit("[::1]"), StringRef::from_lit(""),
StringRef::from_lit("")));
CU_ASSERT("by=\"[::1]\";for=_hidden" ==
http::create_forwarded(FORWARDED_BY | FORWARDED_FOR,
StringRef::from_lit("[::1]"), "_hidden", "",
""));
http::create_forwarded(
FORWARDED_BY | FORWARDED_FOR, StringRef::from_lit("[::1]"),
StringRef::from_lit("_hidden"), StringRef::from_lit(""),
StringRef::from_lit("")));
CU_ASSERT("" == http::create_forwarded(FORWARDED_BY | FORWARDED_FOR |
FORWARDED_HOST | FORWARDED_PROTO,
StringRef::from_lit(""), "", "", ""));
CU_ASSERT("" ==
http::create_forwarded(
FORWARDED_BY | FORWARDED_FOR | FORWARDED_HOST | FORWARDED_PROTO,
StringRef::from_lit(""), StringRef::from_lit(""),
StringRef::from_lit(""), StringRef::from_lit("")));
}
} // namespace shrpx
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment