Use user-defined literals for file extention match

This commit is contained in:
yhirose 2020-12-17 18:27:04 -05:00
parent 7c1c952f5a
commit 0954af2d4c

View file

@ -2135,6 +2135,25 @@ inline void get_remote_ip_and_port(socket_t sock, std::string &ip, int &port) {
} }
} }
inline constexpr unsigned int str2tag_core(const char *s, size_t l,
unsigned int h) {
return (l == 0) ? h
: str2tag_core(s + 1, l - 1,
(h * 33) ^ static_cast<unsigned char>(*s));
}
inline constexpr unsigned int str2tag(std::string_view sv) {
return str2tag_core(sv.data(), sv.size(), 0);
}
namespace udl {
inline constexpr unsigned int operator"" _(const char *s, size_t l) {
return str2tag_core(s, l, 0);
}
} // namespace udl
inline const char * inline const char *
find_content_type(const std::string &path, find_content_type(const std::string &path,
const std::map<std::string, std::string> &user_data) { const std::map<std::string, std::string> &user_data) {
@ -2143,40 +2162,29 @@ find_content_type(const std::string &path,
auto it = user_data.find(ext); auto it = user_data.find(ext);
if (it != user_data.end()) { return it->second.c_str(); } if (it != user_data.end()) { return it->second.c_str(); }
if (ext == "txt") { using namespace udl;
return "text/plain";
} else if (ext == "html" || ext == "htm") { switch (str2tag(ext)) {
return "text/html"; case "txt"_: return "text/plain";
} else if (ext == "css") { case "html"_:
return "text/css"; case "htm"_: return "text/html";
} else if (ext == "jpeg" || ext == "jpg") { case "css"_: return "text/css";
return "image/jpg"; case "jpeg"_:
} else if (ext == "vtt") { case "jpg"_: return "image/jpg";
return "text/vtt"; case "vtt"_: return "text/vtt";
} else if (ext == "png") { case "png"_: return "image/png";
return "image/png"; case "gif"_: return "image/gif";
} else if (ext == "gif") { case "svg"_: return "image/svg+xml";
return "image/gif"; case "ico"_: return "image/x-icon";
} else if (ext == "svg") { case "json"_: return "application/json";
return "image/svg+xml"; case "pdf"_: return "application/pdf";
} else if (ext == "ico") { case "js"_: return "application/javascript";
return "image/x-icon"; case "wasm"_: return "application/wasm";
} else if (ext == "json") { case "xml"_: return "application/xml";
return "application/json"; case "xhtml"_: return "application/xhtml+xml";
} else if (ext == "pdf") { case "mp4"_: return "video/mp4";
return "application/pdf"; default: return nullptr;
} else if (ext == "js") {
return "application/javascript";
} else if (ext == "wasm") {
return "application/wasm";
} else if (ext == "xml") {
return "application/xml";
} else if (ext == "xhtml") {
return "application/xhtml+xml";
} else if (ext == "mp4") {
return "video/mp4";
} }
return nullptr;
} }
inline const char *status_message(int status) { inline const char *status_message(int status) {