nghttpd: Add -e option to toggle gzipped error response

This commit is contained in:
Tatsuhiro Tsujikawa 2014-03-04 23:29:30 +09:00
parent 749cc08f52
commit a61ca763df
3 changed files with 36 additions and 22 deletions

View File

@ -74,7 +74,8 @@ Config::Config()
verbose(false), verbose(false),
daemon(false), daemon(false),
verify_client(false), verify_client(false),
no_tls(false) no_tls(false),
error_gzip(false)
{} {}
Request::Request(int32_t stream_id) Request::Request(int32_t stream_id)
@ -671,19 +672,27 @@ void prepare_status_response(Request *req, Http2Handler *hd,
int pipefd[2]; int pipefd[2];
if(status == STATUS_304 || pipe(pipefd) == -1) { if(status == STATUS_304 || pipe(pipefd) == -1) {
hd->submit_response(status, req->stream_id, 0); hd->submit_response(status, req->stream_id, 0);
} else { return;
std::stringstream ss; }
ss << "<html><head><title>" << status << "</title></head><body>" std::string body;
<< "<h1>" << status << "</h1>" body.reserve(256);
<< "<hr>" body = "<html><head><title>";
<< "<address>" << NGHTTPD_SERVER body += status;
<< " at port " << hd->get_config()->port body += "</title></head><body><h1>";
<< "</address>" body += status;
<< "</body></html>"; body += "</h1><hr><address>";
std::string body = ss.str(); body += NGHTTPD_SERVER;
body += " at port ";
body += util::utos(hd->get_config()->port);
body += "</address>";
body += "</body></html>";
if(hd->get_config()->error_gzip) {
gzFile write_fd = gzdopen(pipefd[1], "w"); gzFile write_fd = gzdopen(pipefd[1], "w");
gzwrite(write_fd, body.c_str(), body.size()); gzwrite(write_fd, body.c_str(), body.size());
gzclose(write_fd); gzclose(write_fd);
} else {
write(pipefd[1], body.c_str(), body.size());
}
close(pipefd[1]); close(pipefd[1]);
req->file = pipefd[0]; req->file = pipefd[0];
@ -694,7 +703,6 @@ void prepare_status_response(Request *req, Http2Handler *hd,
headers.emplace_back("content-encoding", "gzip"); headers.emplace_back("content-encoding", "gzip");
headers.emplace_back("content-type", "text/html; charset=UTF-8"); headers.emplace_back("content-type", "text/html; charset=UTF-8");
hd->submit_response(status, req->stream_id, headers, &data_prd); hd->submit_response(status, req->stream_id, headers, &data_prd);
}
} }
} // namespace } // namespace

View File

@ -64,6 +64,7 @@ struct Config {
bool daemon; bool daemon;
bool verify_client; bool verify_client;
bool no_tls; bool no_tls;
bool error_gzip;
Config(); Config();
}; };

View File

@ -134,6 +134,7 @@ void print_help(std::ostream& out)
<< " -n, --workers=<CORE>\n" << " -n, --workers=<CORE>\n"
<< " Set the number of worker threads.\n" << " Set the number of worker threads.\n"
<< " Default: 1\n" << " Default: 1\n"
<< " -e, --error-gzip Make error response gzipped.\n"
<< " --version Display version information and exit.\n" << " --version Display version information and exit.\n"
<< " -h, --help Display this help and exit.\n" << " -h, --help Display this help and exit.\n"
<< std::endl; << std::endl;
@ -156,13 +157,14 @@ int main(int argc, char **argv)
{"push", required_argument, nullptr, 'p'}, {"push", required_argument, nullptr, 'p'},
{"padding", required_argument, nullptr, 'b'}, {"padding", required_argument, nullptr, 'b'},
{"workers", required_argument, nullptr, 'n'}, {"workers", required_argument, nullptr, 'n'},
{"error-gzip", no_argument, nullptr, 'e'},
{"no-tls", no_argument, &flag, 1}, {"no-tls", no_argument, &flag, 1},
{"color", no_argument, &flag, 2}, {"color", no_argument, &flag, 2},
{"version", no_argument, &flag, 3}, {"version", no_argument, &flag, 3},
{nullptr, 0, nullptr, 0} {nullptr, 0, nullptr, 0}
}; };
int option_index = 0; int option_index = 0;
int c = getopt_long(argc, argv, "DVb:c:d:hn:p:v", long_options, int c = getopt_long(argc, argv, "DVb:c:d:ehn:p:v", long_options,
&option_index); &option_index);
char *end; char *end;
if(c == -1) { if(c == -1) {
@ -181,6 +183,9 @@ int main(int argc, char **argv)
case 'd': case 'd':
config.htdocs = optarg; config.htdocs = optarg;
break; break;
case 'e':
config.error_gzip = true;
break;
case 'n': case 'n':
errno = 0; errno = 0;
config.num_worker = strtoul(optarg, &end, 10); config.num_worker = strtoul(optarg, &end, 10);