From 1e7f0d833ec0b703fa2662968763b313e52e8c50 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Thu, 29 Oct 2015 23:08:52 +0900 Subject: [PATCH] nghttpd: Don't show read error if mime types file is not set by user manually --- src/nghttpd.cc | 11 ++++++++++- src/util.cc | 10 ++++------ src/util.h | 6 +++++- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/nghttpd.cc b/src/nghttpd.cc index ba41d132..58179aea 100644 --- a/src/nghttpd.cc +++ b/src/nghttpd.cc @@ -183,6 +183,8 @@ int main(int argc, char **argv) { Config config; bool color = false; + auto mime_types_file_set_manually = false; + while (1) { static int flag = 0; static option long_options[] = { @@ -335,6 +337,7 @@ int main(int argc, char **argv) { break; case 9: // mime-types-file option + mime_types_file_set_manually = true; config.mime_types_file = optarg; break; } @@ -375,7 +378,13 @@ int main(int argc, char **argv) { config.htdocs = "./"; } - config.mime_types = util::read_mime_types(config.mime_types_file.c_str()); + if (util::read_mime_types(config.mime_types, + config.mime_types_file.c_str()) != 0) { + if (mime_types_file_set_manually) { + std::cerr << "--mime-types-file: Could not open mime types file: " + << config.mime_types_file << std::endl; + } + } set_color_output(color || isatty(fileno(stdout))); diff --git a/src/util.cc b/src/util.cc index 756a3e16..0870b20e 100644 --- a/src/util.cc +++ b/src/util.cc @@ -1218,13 +1218,11 @@ uint64_t get_uint64(const uint8_t *data) { return n; } -std::map read_mime_types(const char *filename) { - std::map res; - +int read_mime_types(std::map &res, + const char *filename) { std::ifstream infile(filename); if (!infile) { - std::cerr << "Could not open mime types file: " << filename << std::endl; - return res; + return -1; } auto delim_pred = [](char c) { return c == ' ' || c == '\t'; }; @@ -1252,7 +1250,7 @@ std::map read_mime_types(const char *filename) { } } - return res; + return 0; } } // namespace util diff --git a/src/util.h b/src/util.h index c386cf52..60662926 100644 --- a/src/util.h +++ b/src/util.h @@ -707,7 +707,11 @@ uint32_t get_uint32(const uint8_t *data); // order and returns it in host byte order. uint64_t get_uint64(const uint8_t *data); -std::map read_mime_types(const char *filename); +// Reads mime types file (see /etc/mime.types), and stores extension +// -> MIME type map in |res|. This function returns 0 if it succeeds, +// or -1. +int read_mime_types(std::map &res, + const char *filename); } // namespace util