diff --git a/src/HttpServer.cc b/src/HttpServer.cc index 7649d873..3d2b759d 100644 --- a/src/HttpServer.cc +++ b/src/HttpServer.cc @@ -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 << "" << status << "" - << "

" << status << "

" - << "
" - << "
" << NGHTTPD_SERVER - << " at port " << hd->get_config()->port - << "
" - << ""; - std::string body = ss.str(); + return; + } + std::string body; + body.reserve(256); + body = ""; + body += status; + body += "

"; + body += status; + body += "


"; + body += NGHTTPD_SERVER; + body += " at port "; + body += util::utos(hd->get_config()->port); + body += "
"; + body += ""; + 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> 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> 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 diff --git a/src/HttpServer.h b/src/HttpServer.h index c7624693..3de9b07b 100644 --- a/src/HttpServer.h +++ b/src/HttpServer.h @@ -64,6 +64,7 @@ struct Config { bool daemon; bool verify_client; bool no_tls; + bool error_gzip; Config(); }; diff --git a/src/nghttpd.cc b/src/nghttpd.cc index 56434bf9..d7098872 100644 --- a/src/nghttpd.cc +++ b/src/nghttpd.cc @@ -134,6 +134,7 @@ void print_help(std::ostream& out) << " -n, --workers=\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);