Skip to content

Commit 3a9776b

Browse files
authored
Merge pull request #411 from danielskeenan/#410
Handle unknown static file extensions
2 parents db53bd6 + 869a0ab commit 3a9776b

File tree

3 files changed

+36
-10
lines changed

3 files changed

+36
-10
lines changed

include/crow/http_response.h

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -242,17 +242,20 @@ namespace crow
242242
{
243243
std::size_t last_dot = path.find_last_of(".");
244244
std::string extension = path.substr(last_dot + 1);
245-
std::string mimeType = "";
246245
code = 200;
247-
this->add_header("Content-length", std::to_string(file_info.statbuf.st_size));
246+
this->add_header("Content-Length", std::to_string(file_info.statbuf.st_size));
248247

249-
if (extension != "")
248+
if (!extension.empty())
250249
{
251-
mimeType = mime_types.at(extension);
252-
if (mimeType != "")
253-
this->add_header("Content-Type", mimeType);
250+
const auto mimeType = mime_types.find(extension);
251+
if (mimeType != mime_types.end())
252+
{
253+
this->add_header("Content-Type", mimeType->second);
254+
}
254255
else
255-
this->add_header("content-Type", "text/plain");
256+
{
257+
this->add_header("Content-Type", "text/plain");
258+
}
256259
}
257260
}
258261
else

tests/img/filewith.badext

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Test file with a strange extension.

tests/unittest.cpp

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1926,8 +1926,10 @@ TEST_CASE("multipart")
19261926
TEST_CASE("send_file")
19271927
{
19281928

1929-
struct stat statbuf;
1930-
stat("tests/img/cat.jpg", &statbuf);
1929+
struct stat statbuf_cat;
1930+
stat("tests/img/cat.jpg", &statbuf_cat);
1931+
struct stat statbuf_badext;
1932+
stat("tests/img/filewith.badext", &statbuf_badext);
19311933

19321934
SimpleApp app;
19331935

@@ -1944,6 +1946,12 @@ TEST_CASE("send_file")
19441946
res.end();
19451947
});
19461948

1949+
CROW_ROUTE(app, "/filewith.badext")
1950+
([](const crow::request&, crow::response& res) {
1951+
res.set_static_file_info("tests/img/filewith.badext");
1952+
res.end();
1953+
});
1954+
19471955
app.validate();
19481956

19491957
//File not found check
@@ -1971,7 +1979,21 @@ TEST_CASE("send_file")
19711979

19721980
CHECK(200 == res.code);
19731981
CHECK("image/jpeg" == res.headers.find("Content-Type")->second);
1974-
CHECK(to_string(statbuf.st_size) == res.headers.find("Content-Length")->second);
1982+
CHECK(to_string(statbuf_cat.st_size) == res.headers.find("Content-Length")->second);
1983+
}
1984+
1985+
//Unknown extension check
1986+
{
1987+
request req;
1988+
response res;
1989+
1990+
req.url = "/filewith.badext";
1991+
req.http_ver_major = 1;
1992+
1993+
CHECK_NOTHROW(app.handle(req, res));
1994+
CHECK(200 == res.code);
1995+
CHECK("text/plain" == res.headers.find("Content-Type")->second);
1996+
CHECK(to_string(statbuf_badext.st_size) == res.headers.find("Content-Length")->second);
19751997
}
19761998
} // send_file
19771999

0 commit comments

Comments
 (0)