Skip to content

Commit

Permalink
nghttpx: Add --add-request-header option
Browse files Browse the repository at this point in the history
  • Loading branch information
tatsuhiro-t committed Jun 5, 2015
1 parent 43b3640 commit 00efa86
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 4 deletions.
11 changes: 11 additions & 0 deletions src/shrpx.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1350,6 +1350,12 @@ HTTP/2 and SPDY:
HTTP/1.1 frontend. This option can be used multiple
times to specify multiple alternative services.
Example: --altsvc=h2,443
--add-request-header=<HEADER>
Specify additional header field to add to request header
set. This option just appends header field and won't
replace anything already set. This option can be used
several times to specify multiple header fields.
Example: --add-request-header="foo: bar"
--add-response-header=<HEADER>
Specify additional header field to add to response
header set. This option just appends header field and
Expand Down Expand Up @@ -1544,6 +1550,7 @@ int main(int argc, char **argv) {
{SHRPX_OPT_NO_OCSP, no_argument, &flag, 79},
{SHRPX_OPT_HEADER_FIELD_BUFFER, required_argument, &flag, 80},
{SHRPX_OPT_MAX_HEADER_FIELDS, required_argument, &flag, 81},
{SHRPX_OPT_ADD_REQUEST_HEADER, required_argument, &flag, 82},
{nullptr, 0, nullptr, 0}};

int option_index = 0;
Expand Down Expand Up @@ -1902,6 +1909,10 @@ int main(int argc, char **argv) {
// --max-header-fields
cmdcfgs.emplace_back(SHRPX_OPT_MAX_HEADER_FIELDS, optarg);
break;
case 82:
// --add-request-header
cmdcfgs.emplace_back(SHRPX_OPT_ADD_REQUEST_HEADER, optarg);
break;
default:
break;
}
Expand Down
10 changes: 7 additions & 3 deletions src/shrpx_config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -997,14 +997,18 @@ int parse_config(const char *opt, const char *optarg) {
return 0;
}

if (util::strieq(opt, SHRPX_OPT_ADD_RESPONSE_HEADER)) {
if (util::strieq(opt, SHRPX_OPT_ADD_REQUEST_HEADER) ||
util::strieq(opt, SHRPX_OPT_ADD_RESPONSE_HEADER)) {
auto p = parse_header(optarg);
if (p.first.empty()) {
LOG(ERROR) << opt << ": header field name is empty: " << optarg;
return -1;
}
mod_config()->add_response_headers.push_back(std::move(p));

if (util::strieq(opt, SHRPX_OPT_ADD_REQUEST_HEADER)) {
mod_config()->add_request_headers.push_back(std::move(p));
} else {
mod_config()->add_response_headers.push_back(std::move(p));
}
return 0;
}

Expand Down
2 changes: 2 additions & 0 deletions src/shrpx_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ constexpr char SHRPX_OPT_HTTP2_NO_COOKIE_CRUMBLING[] =
constexpr char SHRPX_OPT_FRONTEND_FRAME_DEBUG[] = "frontend-frame-debug";
constexpr char SHRPX_OPT_PADDING[] = "padding";
constexpr char SHRPX_OPT_ALTSVC[] = "altsvc";
constexpr char SHRPX_OPT_ADD_REQUEST_HEADER[] = "add-request-header";
constexpr char SHRPX_OPT_ADD_RESPONSE_HEADER[] = "add-response-header";
constexpr char SHRPX_OPT_WORKER_FRONTEND_CONNECTIONS[] =
"worker-frontend-connections";
Expand Down Expand Up @@ -220,6 +221,7 @@ struct Config {
// The list of (private key file, certificate file) pair
std::vector<std::pair<std::string, std::string>> subcerts;
std::vector<AltSvc> altsvcs;
std::vector<std::pair<std::string, std::string>> add_request_headers;
std::vector<std::pair<std::string, std::string>> add_response_headers;
std::vector<unsigned char> alpn_prefs;
std::vector<LogFragment> accesslog_format;
Expand Down
7 changes: 6 additions & 1 deletion src/shrpx_http2_downstream_connection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,8 @@ int Http2DownstreamConnection::push_request_headers() {
// 7. x-forwarded-proto (optional)
// 8. te (optional)
auto nva = std::vector<nghttp2_nv>();
nva.reserve(nheader + 8 + cookies.size());
nva.reserve(nheader + 8 + cookies.size() +
get_config()->add_request_headers.size());

std::string via_value;
std::string xff_value;
Expand Down Expand Up @@ -411,6 +412,10 @@ int Http2DownstreamConnection::push_request_headers() {
nva.push_back(http2::make_nv_ll("te", "trailers"));
}

for (auto &p : get_config()->add_request_headers) {
nva.push_back(http2::make_nv(p.first, p.second));
}

if (LOG_ENABLED(INFO)) {
std::stringstream ss;
for (auto &nv : nva) {
Expand Down
7 changes: 7 additions & 0 deletions src/shrpx_http_downstream_connection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,13 @@ int HttpDownstreamConnection::push_request_headers() {
hdrs += "\r\n";
}

for (auto &p : get_config()->add_request_headers) {
hdrs += p.first;
hdrs += ": ";
hdrs += p.second;
hdrs += "\r\n";
}

hdrs += "\r\n";
if (LOG_ENABLED(INFO)) {
const char *hdrp;
Expand Down

0 comments on commit 00efa86

Please sign in to comment.