Commit a61ca763 authored by Tatsuhiro Tsujikawa's avatar Tatsuhiro Tsujikawa

nghttpd: Add -e option to toggle gzipped error response

parent 749cc08f
...@@ -74,7 +74,8 @@ Config::Config() ...@@ -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,30 +672,37 @@ void prepare_status_response(Request *req, Http2Handler *hd, ...@@ -671,30 +672,37 @@ 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);
close(pipefd[1]); } else {
write(pipefd[1], body.c_str(), body.size());
req->file = pipefd[0]; }
nghttp2_data_provider data_prd; close(pipefd[1]);
data_prd.source.fd = pipefd[0];
data_prd.read_callback = file_read_callback; req->file = pipefd[0];
std::vector<std::pair<std::string, std::string>> headers; nghttp2_data_provider data_prd;
headers.emplace_back("content-encoding", "gzip"); data_prd.source.fd = pipefd[0];
headers.emplace_back("content-type", "text/html; charset=UTF-8"); data_prd.read_callback = file_read_callback;
hd->submit_response(status, req->stream_id, headers, &data_prd); std::vector<std::pair<std::string, std::string>> headers;
} headers.emplace_back("content-encoding", "gzip");
headers.emplace_back("content-type", "text/html; charset=UTF-8");
hd->submit_response(status, req->stream_id, headers, &data_prd);
} }
} // namespace } // namespace
......
...@@ -64,6 +64,7 @@ struct Config { ...@@ -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();
}; };
......
...@@ -134,6 +134,7 @@ void print_help(std::ostream& out) ...@@ -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) ...@@ -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) ...@@ -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);
......
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