Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle unknown static file extensions #411

Merged
merged 5 commits into from
Apr 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions include/crow/http_response.h
Original file line number Diff line number Diff line change
Expand Up @@ -242,17 +242,20 @@ 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));
this->add_header("Content-Length", std::to_string(file_info.statbuf.st_size));

if (extension != "")
if (!extension.empty())
mrozigor marked this conversation as resolved.
Show resolved Hide resolved
{
mimeType = mime_types.at(extension);
if (mimeType != "")
this->add_header("Content-Type", mimeType);
const auto mimeType = mime_types.find(extension);
if (mimeType != mime_types.end())
{
this->add_header("Content-Type", mimeType->second);
}
else
this->add_header("content-Type", "text/plain");
{
this->add_header("Content-Type", "text/plain");
}
}
}
else
Expand Down
1 change: 1 addition & 0 deletions tests/img/filewith.badext
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Test file with a strange extension.
28 changes: 25 additions & 3 deletions tests/unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down