Skip to content

Commit

Permalink
nghttpx: Allow '*' in --error-page to be used as wildcard
Browse files Browse the repository at this point in the history
  • Loading branch information
tatsuhiro-t committed Mar 19, 2016
1 parent d7051f5 commit d2b55ad
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 10 deletions.
7 changes: 4 additions & 3 deletions src/shrpx.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1843,12 +1843,13 @@ HTTP/2 and SPDY:
towards this number.
Default: )" << get_config()->http.max_response_header_fields
<< R"(
--error-page=<CODE>=<PATH>
--error-page=(<CODE>|*)=<PATH>
Set file path to custom error page served when nghttpx
originally generates HTTP error status code <CODE>.
<CODE> must be greater than or equal to 400, and at most
599. If error status code comes from backend server,
the custom error pages are not used.
599. If "*" is used instead of <CODE>, it matches all
HTTP status code. If error status code comes from
backend server, the custom error pages are not used.
Debug:
--frontend-http2-dump-request-header=<PATH>
Expand Down
19 changes: 13 additions & 6 deletions src/shrpx_config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -715,11 +715,19 @@ int parse_error_page(std::vector<ErrorPage> &error_pages, const char *opt,
}

auto codestr = StringRef{std::begin(arg), eq};
auto code = util::parse_uint(codestr);
unsigned int code;

if (code == -1 || code < 400 || code > 599) {
LOG(ERROR) << opt << ": bad code: '" << codestr << "'";
return -1;
if (codestr == "*") {
code = 0;
} else {
auto n = util::parse_uint(codestr);

if (n == -1 || n < 400 || n > 599) {
LOG(ERROR) << opt << ": bad code: '" << codestr << "'";
return -1;
}

code = static_cast<unsigned int>(n);
}

auto path = StringRef{eq + 1, std::end(arg)};
Expand Down Expand Up @@ -748,8 +756,7 @@ int parse_error_page(std::vector<ErrorPage> &error_pages, const char *opt,
content.insert(std::end(content), std::begin(buf), std::begin(buf) + n);
}

error_pages.push_back(
ErrorPage{std::move(content), static_cast<unsigned int>(code)});
error_pages.push_back(ErrorPage{std::move(content), code});

return 0;
}
Expand Down
1 change: 1 addition & 0 deletions src/shrpx_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@ struct TLSConfig {
struct ErrorPage {
// not NULL-terminated
std::vector<uint8_t> content;
// 0 is special value, and it matches all HTTP status code.
unsigned int http_status;
};

Expand Down
2 changes: 1 addition & 1 deletion src/shrpx_http.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ StringRef create_error_html(BlockAllocator &balloc, unsigned int http_status) {

const auto &error_pages = httpconf.error_pages;
for (const auto &page : error_pages) {
if (page.http_status == http_status) {
if (page.http_status == 0 || page.http_status == http_status) {
return StringRef{std::begin(page.content), std::end(page.content)};
}
}
Expand Down

0 comments on commit d2b55ad

Please sign in to comment.