From bdfd9cb6a99a91bd9600e5b3c0ee0334417aa28e Mon Sep 17 00:00:00 2001 From: Dan Keenan Date: Sat, 16 Apr 2022 17:31:12 -0400 Subject: [PATCH 1/5] Create failing test. --- tests/img/filewith.badext | 1 + tests/unittest.cpp | 28 +++++++++++++++++++++++++--- 2 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 tests/img/filewith.badext diff --git a/tests/img/filewith.badext b/tests/img/filewith.badext new file mode 100644 index 000000000..afc96e20c --- /dev/null +++ b/tests/img/filewith.badext @@ -0,0 +1 @@ +Test file with a strange extension. diff --git a/tests/unittest.cpp b/tests/unittest.cpp index 4c980993f..c4377f2f7 100644 --- a/tests/unittest.cpp +++ b/tests/unittest.cpp @@ -1926,8 +1926,10 @@ TEST_CASE("multipart") TEST_CASE("send_file") { - struct stat statbuf; - stat("tests/img/cat.jpg", &statbuf); + struct stat statbuf_cat; + stat("tests/img/cat.jpg", &statbuf_cat); + struct stat statbuf_badext; + stat("tests/img/filewith.badext", &statbuf_badext); SimpleApp app; @@ -1944,6 +1946,12 @@ TEST_CASE("send_file") res.end(); }); + CROW_ROUTE(app, "/filewith.badext") + ([](const crow::request&, crow::response& res) { + res.set_static_file_info("tests/img/filewith.badext"); + res.end(); + }); + app.validate(); //File not found check @@ -1971,7 +1979,21 @@ TEST_CASE("send_file") CHECK(200 == res.code); CHECK("image/jpeg" == res.headers.find("Content-Type")->second); - CHECK(to_string(statbuf.st_size) == res.headers.find("Content-Length")->second); + CHECK(to_string(statbuf_cat.st_size) == res.headers.find("Content-Length")->second); + } + + //Unknown extension check + { + request req; + response res; + + req.url = "/filewith.badext"; + req.http_ver_major = 1; + + CHECK_NOTHROW(app.handle(req, res)); + CHECK(200 == res.code); + CHECK("text/plain" == res.headers.find("Content-Type")->second); + CHECK(to_string(statbuf_badext.st_size) == res.headers.find("Content-Length")->second); } } // send_file From 33df7fc35b4a7fc0ea2343cae361aca0c487e60b Mon Sep 17 00:00:00 2001 From: Dan Keenan Date: Sat, 16 Apr 2022 17:35:44 -0400 Subject: [PATCH 2/5] Handle extensions without mime types assigned to them. --- include/crow/http_response.h | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/include/crow/http_response.h b/include/crow/http_response.h index 4ba400ba0..619accdf9 100644 --- a/include/crow/http_response.h +++ b/include/crow/http_response.h @@ -246,13 +246,17 @@ namespace crow code = 200; this->add_header("Content-length", std::to_string(file_info.statbuf.st_size)); - if (extension != "") + if (!extension.empty()) { - mimeType = mime_types.at(extension); - if (mimeType != "") - this->add_header("Content-Type", mimeType); - else - this->add_header("content-Type", "text/plain"); + try + { + mimeType = mime_types.at(extension); + } + catch (const std::out_of_range&) + { + mimeType = "text/plain"; + } + this->add_header("Content-Type", mimeType); } } else From 89226687ea638bf79b90397ad33f7d29499a4ab9 Mon Sep 17 00:00:00 2001 From: Dan Keenan Date: Wed, 20 Apr 2022 16:18:50 -0400 Subject: [PATCH 3/5] Avoid exception handling. --- include/crow/http_response.h | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/include/crow/http_response.h b/include/crow/http_response.h index 619accdf9..3e1c436a7 100644 --- a/include/crow/http_response.h +++ b/include/crow/http_response.h @@ -242,21 +242,18 @@ namespace crow { std::size_t last_dot = path.find_last_of("."); std::string extension = path.substr(last_dot + 1); - std::string mimeType = ""; code = 200; this->add_header("Content-length", std::to_string(file_info.statbuf.st_size)); if (!extension.empty()) { - try + const auto mimeType = mime_types.find(extension); + if (mimeType != mime_types.end()) { - mimeType = mime_types.at(extension); + this->add_header("Content-Type", mimeType->second); + } else { + this->add_header("Content-Type", "text/plain"); } - catch (const std::out_of_range&) - { - mimeType = "text/plain"; - } - this->add_header("Content-Type", mimeType); } } else From 885ac41d9937e6c0ab4803b747aacf93a1a89480 Mon Sep 17 00:00:00 2001 From: Dan Keenan Date: Wed, 20 Apr 2022 16:26:09 -0400 Subject: [PATCH 4/5] Formatting fixes. --- include/crow/http_response.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/include/crow/http_response.h b/include/crow/http_response.h index 3e1c436a7..327515226 100644 --- a/include/crow/http_response.h +++ b/include/crow/http_response.h @@ -251,7 +251,9 @@ namespace crow if (mimeType != mime_types.end()) { this->add_header("Content-Type", mimeType->second); - } else { + } + else + { this->add_header("Content-Type", "text/plain"); } } From 869a0abb049dbb6fb5d3f22f067a38ef1875da66 Mon Sep 17 00:00:00 2001 From: Dan Keenan Date: Thu, 21 Apr 2022 19:51:22 -0400 Subject: [PATCH 5/5] Fix header name. --- include/crow/http_response.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/crow/http_response.h b/include/crow/http_response.h index 327515226..58562a4ae 100644 --- a/include/crow/http_response.h +++ b/include/crow/http_response.h @@ -243,7 +243,7 @@ namespace crow std::size_t last_dot = path.find_last_of("."); std::string extension = path.substr(last_dot + 1); code = 200; - this->add_header("Content-length", std::to_string(file_info.statbuf.st_size)); + this->add_header("Content-Length", std::to_string(file_info.statbuf.st_size)); if (!extension.empty()) {