Content-Type CLI

A one-shot command-line tool that prints the Content-Type header for each path or extension passed on the command line. Useful for spot-checking what a static-file middleware would emit before you put it behind a real server.

examples/content_type_cli.cpp
// content_type_cli — print the Content-Type header for every argument.
//
// Usage:  content_type_cli file1.html path/to/img.png .json bogus
//
// Demonstrates the combined contentType + fallback pattern from
// docs/sphinx/guides/lookup-unknown-extension.rst.
#include <iostream>
#include <string>

#include <polycpp/mime/mime.hpp>

int main(int argc, char** argv) {
    using namespace polycpp::mime;

    if (argc < 2) {
        std::cerr << "usage: " << argv[0] << " <path-or-ext>...\n";
        return 64;  // EX_USAGE
    }

    for (int i = 1; i < argc; ++i) {
        const std::string arg = argv[i];
        std::string ct;
        if (auto found = contentType(arg)) {
            ct = *found;
        } else {
            ct = "application/octet-stream";
        }
        std::cout << arg << "\t" << ct << '\n';
    }
    return 0;
}

Run it with a handful of paths:

./build/examples/content_type_cli index.html style.css logo.svg README

Expected output:

index.html      text/html; charset=utf-8
style.css       text/css; charset=utf-8
logo.svg        image/svg+xml
README          application/octet-stream

The last line shows the fallback path from Handle an unknown extension.