From b64fc3ac4964447462b0930f73cbde11c12fe8eb Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Thu, 7 Jan 2016 22:51:47 +0900 Subject: [PATCH] nghttpd: Add --no-content-length option to omit content-length in response --- src/HttpServer.cc | 14 +++++++++----- src/HttpServer.h | 1 + src/nghttpd.cc | 7 +++++++ 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/HttpServer.cc b/src/HttpServer.cc index c331d12c..b1e0b0fe 100644 --- a/src/HttpServer.cc +++ b/src/HttpServer.cc @@ -106,7 +106,7 @@ Config::Config() max_concurrent_streams(100), header_table_size(-1), port(0), verbose(false), daemon(false), verify_client(false), no_tls(false), error_gzip(false), early_response(false), hexdump(false), - echo_upload(false) {} + echo_upload(false), no_content_length(false) {} Config::~Config() {} @@ -873,12 +873,14 @@ int Http2Handler::submit_file_response(const std::string &status, std::string last_modified_str; auto nva = make_array(http2::make_nv_ls(":status", status), http2::make_nv_ll("server", NGHTTPD_SERVER), - http2::make_nv_ls("content-length", content_length), http2::make_nv_ll("cache-control", "max-age=3600"), http2::make_nv_ls("date", sessions_->get_cached_date()), http2::make_nv_ll("", ""), http2::make_nv_ll("", ""), - http2::make_nv_ll("", "")); - size_t nvlen = 5; + http2::make_nv_ll("", ""), http2::make_nv_ll("", "")); + size_t nvlen = 4; + if (!get_config()->no_content_length) { + nva[nvlen++] = http2::make_nv_ls("content-length", content_length); + } if (last_modified != 0) { last_modified_str = util::http_date(last_modified); nva[nvlen++] = http2::make_nv_ls("last-modified", last_modified_str); @@ -1088,7 +1090,9 @@ void prepare_echo_response(Stream *stream, Http2Handler *hd) { Headers headers; headers.emplace_back("nghttpd-response", "echo"); - headers.emplace_back("content-length", util::utos(length)); + if (!hd->get_config()->no_content_length) { + headers.emplace_back("content-length", util::utos(length)); + } hd->submit_response("200", stream->stream_id, headers, &data_prd); } diff --git a/src/HttpServer.h b/src/HttpServer.h index 3c1dc73e..3e889711 100644 --- a/src/HttpServer.h +++ b/src/HttpServer.h @@ -76,6 +76,7 @@ struct Config { bool early_response; bool hexdump; bool echo_upload; + bool no_content_length; Config(); ~Config(); }; diff --git a/src/nghttpd.cc b/src/nghttpd.cc index 58179aea..8add1951 100644 --- a/src/nghttpd.cc +++ b/src/nghttpd.cc @@ -164,6 +164,8 @@ Options: Path to file that contains MIME media types and the extensions that represent them. Default: )" << config.mime_types_file << R"( + --no-content-length + Don't send content-length header field. --version Display version information and exit. -h, --help Display this help and exit. @@ -209,6 +211,7 @@ int main(int argc, char **argv) { {"hexdump", no_argument, &flag, 7}, {"echo-upload", no_argument, &flag, 8}, {"mime-types-file", required_argument, &flag, 9}, + {"no-content-length", no_argument, &flag, 10}, {nullptr, 0, nullptr, 0}}; int option_index = 0; int c = getopt_long(argc, argv, "DVb:c:d:ehm:n:p:va:", long_options, @@ -340,6 +343,10 @@ int main(int argc, char **argv) { mime_types_file_set_manually = true; config.mime_types_file = optarg; break; + case 10: + // no-content-length option + config.no_content_length = true; + break; } break; default: