nghttpx: Allow '*' in --error-page to be used as wildcard

This commit is contained in:
Tatsuhiro Tsujikawa 2016-03-19 23:49:15 +09:00
parent d7051f5207
commit d2b55ad1a2
4 changed files with 19 additions and 10 deletions

View File

@ -1843,12 +1843,13 @@ HTTP:
towards this number. towards this number.
Default: )" << get_config()->http.max_response_header_fields Default: )" << get_config()->http.max_response_header_fields
<< R"( << R"(
--error-page=<CODE>=<PATH> --error-page=(<CODE>|*)=<PATH>
Set file path to custom error page served when nghttpx Set file path to custom error page served when nghttpx
originally generates HTTP error status code <CODE>. originally generates HTTP error status code <CODE>.
<CODE> must be greater than or equal to 400, and at most <CODE> must be greater than or equal to 400, and at most
599. If error status code comes from backend server, 599. If "*" is used instead of <CODE>, it matches all
the custom error pages are not used. HTTP status code. If error status code comes from
backend server, the custom error pages are not used.
Debug: Debug:
--frontend-http2-dump-request-header=<PATH> --frontend-http2-dump-request-header=<PATH>

View File

@ -715,11 +715,19 @@ int parse_error_page(std::vector<ErrorPage> &error_pages, const char *opt,
} }
auto codestr = StringRef{std::begin(arg), eq}; auto codestr = StringRef{std::begin(arg), eq};
auto code = util::parse_uint(codestr); unsigned int code;
if (code == -1 || code < 400 || code > 599) { if (codestr == "*") {
LOG(ERROR) << opt << ": bad code: '" << codestr << "'"; code = 0;
return -1; } 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)}; auto path = StringRef{eq + 1, std::end(arg)};
@ -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); content.insert(std::end(content), std::begin(buf), std::begin(buf) + n);
} }
error_pages.push_back( error_pages.push_back(ErrorPage{std::move(content), code});
ErrorPage{std::move(content), static_cast<unsigned int>(code)});
return 0; return 0;
} }

View File

@ -443,6 +443,7 @@ struct TLSConfig {
struct ErrorPage { struct ErrorPage {
// not NULL-terminated // not NULL-terminated
std::vector<uint8_t> content; std::vector<uint8_t> content;
// 0 is special value, and it matches all HTTP status code.
unsigned int http_status; unsigned int http_status;
}; };

View File

@ -40,7 +40,7 @@ StringRef create_error_html(BlockAllocator &balloc, unsigned int http_status) {
const auto &error_pages = httpconf.error_pages; const auto &error_pages = httpconf.error_pages;
for (const auto &page : 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)}; return StringRef{std::begin(page.content), std::end(page.content)};
} }
} }