mime¶
MIME type lookup and RFC 6838 media type handling
Look up MIME types by file extension, generate Content-Type headers
with the right charset, walk in the other direction to a canonical
extension, and parse or validate RFC 6838 media types like
application/vnd.api+json. A C++20 port of the npm mime-types,
mime-db, and media-typer packages backed by 2,601 types compiled
in as constexpr data — zero startup cost, zero I/O at first lookup.
#include <polycpp/mime/mime.hpp>
using namespace polycpp::mime;
auto type = lookup("index.html"); // "text/html"
auto ct = contentType("json"); // "application/json; charset=utf-8"
auto ext = extension("image/svg+xml"); // "svg"
auto cs = charset("text/css"); // "UTF-8"
auto parsed = parse("application/vnd.api+json");
// parsed.type == "application", subtype == "vnd.api", suffix == "json"
Same four verbs as npm mime-types — lookup, contentType,
extension, charset — plus read-only data accessors and the
parse / format / test triad from media-typer.
Header-only where possible, zero-overhead abstractions, constexpr
and std::string_view throughout.
Covers the upstream mime-types and media-typer test suites
plus RFC 6838 edge cases — UTF-8 charset fallback, structured
suffixes, case-insensitive lookup.
Uses polycpp::path::extname and polycpp::TypeError where the
upstream packages map naturally to polycpp primitives.
Getting started¶
# With FetchContent
include(FetchContent)
FetchContent_Declare(
polycpp_mime
GIT_REPOSITORY https://github.com/polycpp/mime.git
GIT_TAG 44646ebf70ec63bbd4a84fe391035cc6b9be2e56
)
FetchContent_MakeAvailable(polycpp_mime)
add_executable(my_app main.cpp)
target_link_libraries(my_app PRIVATE polycpp::mime)
FetchContent is the documented consumption path today. Until release tags
exist, pin GIT_TAG to a mime commit SHA you have tested. By default,
the transitive polycpp dependency is still fetched from its master
branch; for fully reproducible builds, provide your own pinned polycpp
checkout as shown in Installation.
Installation · Quickstart · Tutorials · API reference