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),
daemon(false),
verify_client(false),
no_tls(false)
no_tls(false),
error_gzip(false)
{}
Request::Request(int32_t stream_id)
@ -671,30 +672,37 @@ void prepare_status_response(Request *req, Http2Handler *hd,
int pipefd[2];
if(status == STATUS_304 || pipe(pipefd) == -1) {
hd->submit_response(status, req->stream_id, 0);
} else {
std::stringstream ss;
ss << "<html><head><title>" << status << "</title></head><body>"
<< "<h1>" << status << "</h1>"
<< "<hr>"
<< "<address>" << NGHTTPD_SERVER
<< " at port " << hd->get_config()->port
<< "</address>"
<< "</body></html>";
std::string body = ss.str();
return;
}
std::string body;
body.reserve(256);
body = "<html><head><title>";
body += status;
body += "</title></head><body><h1>";
body += status;
body += "</h1><hr><address>";
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");
gzwrite(write_fd, body.c_str(), body.size());
gzclose(write_fd);
close(pipefd[1]);
req->file = pipefd[0];
nghttp2_data_provider data_prd;
data_prd.source.fd = pipefd[0];
data_prd.read_callback = file_read_callback;
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);
} else {
write(pipefd[1], body.c_str(), body.size());
}
close(pipefd[1]);
req->file = pipefd[0];
nghttp2_data_provider data_prd;
data_prd.source.fd = pipefd[0];
data_prd.read_callback = file_read_callback;
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

View File

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

View File

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