59 lines
1.7 KiB
C++
59 lines
1.7 KiB
C++
#pragma once
|
|
|
|
#include <cstdint>
|
|
#include <string_view>
|
|
|
|
#define likely(x) __builtin_expect(!!(x), 1)
|
|
#define unlikely(x) __builtin_expect(!!(x), 0)
|
|
|
|
using std::string_view;
|
|
|
|
enum class HttpMethod : uint8_t {
|
|
GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS, UNKNOWN
|
|
};
|
|
|
|
class MimeTypes {
|
|
public:
|
|
static string_view get_mime_type(string_view extension) {
|
|
// Remove leading dot if present
|
|
if (!extension.empty() && extension[0] == '.') {
|
|
extension = extension.substr(1);
|
|
}
|
|
|
|
switch (extension.size()) {
|
|
case 2:
|
|
if (extension == "js") return "application/javascript";
|
|
break;
|
|
case 3:
|
|
if (extension == "css") return "text/css";
|
|
if (extension == "htm") return "text/html";
|
|
if (extension == "png") return "image/png";
|
|
if (extension == "jpg") return "image/jpeg";
|
|
if (extension == "gif") return "image/gif";
|
|
if (extension == "svg") return "image/svg+xml";
|
|
if (extension == "ico") return "image/x-icon";
|
|
if (extension == "ttf") return "font/ttf";
|
|
if (extension == "txt") return "text/plain";
|
|
if (extension == "pdf") return "application/pdf";
|
|
break;
|
|
case 4:
|
|
if (extension == "html") return "text/html";
|
|
if (extension == "json") return "application/json";
|
|
if (extension == "jpeg") return "image/jpeg";
|
|
if (extension == "woff") return "font/woff";
|
|
break;
|
|
case 5:
|
|
if (extension == "woff2") return "font/woff2";
|
|
break;
|
|
}
|
|
return "application/octet-stream";
|
|
}
|
|
|
|
static bool should_compress(string_view mime_type) {
|
|
return mime_type.starts_with("text/") ||
|
|
mime_type.starts_with("application/json") ||
|
|
mime_type.starts_with("application/javascript") ||
|
|
mime_type.starts_with("image/svg+xml");
|
|
}
|
|
};
|