-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement a function to calculate IANA MIME types
Signed-off-by: Juan Cruz Viotti <[email protected]>
- Loading branch information
Showing
6 changed files
with
139 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#ifndef SOURCEMETA_HYDRA_HTTP_MIME_H | ||
#define SOURCEMETA_HYDRA_HTTP_MIME_H | ||
|
||
#if defined(__Unikraft__) | ||
#define SOURCEMETA_HYDRA_HTTP_EXPORT | ||
#else | ||
#include "http_export.h" | ||
#endif | ||
|
||
#include <filesystem> // std::filesystem | ||
#include <string> // std::string | ||
|
||
namespace sourcemeta::hydra::http { | ||
|
||
/// @ingroup http | ||
/// Find a IANA MIME type for the given file. For example: | ||
/// | ||
/// ```cpp | ||
/// #include <sourcemeta/hydra/http.h> | ||
/// #include <cassert> | ||
/// | ||
/// const auto result{sourcemeta::hydra::http::mime_type("image.png")}; | ||
/// assert(result == "image/png"); | ||
/// ``` | ||
auto SOURCEMETA_HYDRA_HTTP_EXPORT | ||
mime_type(const std::filesystem::path &file_path) -> std::string; | ||
|
||
} // namespace sourcemeta::hydra::http | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#include <sourcemeta/hydra/http_mime.h> | ||
|
||
#include <algorithm> // std::transform | ||
#include <cctype> // std::tolower | ||
#include <map> // std::map | ||
|
||
namespace sourcemeta::hydra::http { | ||
|
||
// See https://www.iana.org/assignments/media-types/media-types.xhtml | ||
auto mime_type(const std::filesystem::path &file_path) -> std::string { | ||
// TODO: More exhaustively define all known types | ||
static const std::map<std::string, std::string> MIME_TYPES{ | ||
{".css", "text/css"}, | ||
{".png", "image/png"}, | ||
{".webp", "image/webp"}, | ||
{".ico", "image/vnd.microsoft.icon"}, | ||
{".svg", "image/svg+xml"}, | ||
{".webmanifest", "application/manifest+json"}, | ||
{".json", "application/json"}, | ||
{".woff", "font/woff"}, | ||
{".woff2", "font/woff2"}, | ||
}; | ||
|
||
std::string extension{file_path.extension().string()}; | ||
std::transform(extension.begin(), extension.end(), extension.begin(), | ||
[](const auto character) { return std::tolower(character); }); | ||
const auto result{MIME_TYPES.find(extension)}; | ||
return result == MIME_TYPES.cend() ? "application/octet-stream" | ||
: result->second; | ||
} | ||
|
||
} // namespace sourcemeta::hydra::http |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#include <gtest/gtest.h> | ||
|
||
#include <sourcemeta/hydra/http.h> | ||
|
||
TEST(HTTP_MIME, css_lower) { | ||
const std::filesystem::path file_path{"path/to/example.css"}; | ||
EXPECT_EQ(sourcemeta::hydra::http::mime_type(file_path), "text/css"); | ||
} | ||
|
||
TEST(HTTP_MIME, css_upper) { | ||
const std::filesystem::path file_path{"path/to/example.CSS"}; | ||
EXPECT_EQ(sourcemeta::hydra::http::mime_type(file_path), "text/css"); | ||
} | ||
|
||
TEST(HTTP_MIME, css_mixed) { | ||
const std::filesystem::path file_path{"path/to/example.cSs"}; | ||
EXPECT_EQ(sourcemeta::hydra::http::mime_type(file_path), "text/css"); | ||
} | ||
|
||
TEST(HTTP_MIME, png_lower) { | ||
const std::filesystem::path file_path{"path/to/example.png"}; | ||
EXPECT_EQ(sourcemeta::hydra::http::mime_type(file_path), "image/png"); | ||
} | ||
|
||
TEST(HTTP_MIME, webp_lower) { | ||
const std::filesystem::path file_path{"path/to/example.webp"}; | ||
EXPECT_EQ(sourcemeta::hydra::http::mime_type(file_path), "image/webp"); | ||
} | ||
|
||
TEST(HTTP_MIME, ico_lower) { | ||
const std::filesystem::path file_path{"path/to/example.ico"}; | ||
EXPECT_EQ(sourcemeta::hydra::http::mime_type(file_path), | ||
"image/vnd.microsoft.icon"); | ||
} | ||
|
||
TEST(HTTP_MIME, svg_lower) { | ||
const std::filesystem::path file_path{"path/to/example.svg"}; | ||
EXPECT_EQ(sourcemeta::hydra::http::mime_type(file_path), "image/svg+xml"); | ||
} | ||
|
||
TEST(HTTP_MIME, webmanifest_lower) { | ||
const std::filesystem::path file_path{"path/to/example.webmanifest"}; | ||
EXPECT_EQ(sourcemeta::hydra::http::mime_type(file_path), | ||
"application/manifest+json"); | ||
} | ||
|
||
TEST(HTTP_MIME, json_lower) { | ||
const std::filesystem::path file_path{"path/to/example.json"}; | ||
EXPECT_EQ(sourcemeta::hydra::http::mime_type(file_path), "application/json"); | ||
} | ||
|
||
TEST(HTTP_MIME, woff_lower) { | ||
const std::filesystem::path file_path{"path/to/example.woff"}; | ||
EXPECT_EQ(sourcemeta::hydra::http::mime_type(file_path), "font/woff"); | ||
} | ||
|
||
TEST(HTTP_MIME, woff2_lower) { | ||
const std::filesystem::path file_path{"path/to/example.woff2"}; | ||
EXPECT_EQ(sourcemeta::hydra::http::mime_type(file_path), "font/woff2"); | ||
} | ||
|
||
TEST(HTTP_MIME, unknown) { | ||
const std::filesystem::path file_path{"path/to/example.foobar"}; | ||
EXPECT_EQ(sourcemeta::hydra::http::mime_type(file_path), | ||
"application/octet-stream"); | ||
} | ||
|
||
TEST(HTTP_MIME, none) { | ||
const std::filesystem::path file_path{"path/to/example"}; | ||
EXPECT_EQ(sourcemeta::hydra::http::mime_type(file_path), | ||
"application/octet-stream"); | ||
} |