summaryrefslogtreecommitdiffhomepage
path: root/mime.cpp
blob: f64d04667fe3ad8d8d62fa264d9c3d5f92a2c893 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include "mime.h"

#include <boost/algorithm/string/predicate.hpp>
#include <boost/beast/http.hpp>

namespace beast = boost::beast; 

// Return a reasonable mime type based on the extension of a file.
std::string mime_type(const std::string& path)
{
    auto const ext = [&path]
    {
        auto const pos = path.rfind(".");
        if (pos == std::string::npos)
            return std::string{};
        return path.substr(pos);
    }();
    if(boost::algorithm::istarts_with(ext, ".htm"))  return "text/html";
    if(boost::algorithm::istarts_with(ext, ".html")) return "text/html";
    if(boost::algorithm::istarts_with(ext, ".php"))  return "text/html";
    if(boost::algorithm::istarts_with(ext, ".css"))  return "text/css";
    if(boost::algorithm::istarts_with(ext, ".txt"))  return "text/plain";
    if(boost::algorithm::istarts_with(ext, ".js"))   return "application/javascript";
    if(boost::algorithm::istarts_with(ext, ".json")) return "application/json";
    if(boost::algorithm::istarts_with(ext, ".xml"))  return "application/xml";
    if(boost::algorithm::istarts_with(ext, ".swf"))  return "application/x-shockwave-flash";
    if(boost::algorithm::istarts_with(ext, ".flv"))  return "video/x-flv";
    if(boost::algorithm::istarts_with(ext, ".png"))  return "image/png";
    if(boost::algorithm::istarts_with(ext, ".jpe"))  return "image/jpeg";
    if(boost::algorithm::istarts_with(ext, ".jpeg")) return "image/jpeg";
    if(boost::algorithm::istarts_with(ext, ".jpg"))  return "image/jpeg";
    if(boost::algorithm::istarts_with(ext, ".gif"))  return "image/gif";
    if(boost::algorithm::istarts_with(ext, ".bmp"))  return "image/bmp";
    if(boost::algorithm::istarts_with(ext, ".ico"))  return "image/vnd.microsoft.icon";
    if(boost::algorithm::istarts_with(ext, ".tiff")) return "image/tiff";
    if(boost::algorithm::istarts_with(ext, ".tif"))  return "image/tiff";
    if(boost::algorithm::istarts_with(ext, ".svg"))  return "image/svg+xml";
    if(boost::algorithm::istarts_with(ext, ".svgz")) return "image/svg+xml";
    return "application/text";
}