mirror of
https://github.com/yhirose/cpp-httplib
synced 2024-11-21 14:29:10 -07:00
Use user-defined literals for file extention match
This commit is contained in:
parent
7c1c952f5a
commit
0954af2d4c
1 changed files with 41 additions and 33 deletions
74
httplib.h
74
httplib.h
|
@ -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) {
|
||||||
|
|
Loading…
Reference in a new issue