Older/Server/ResponseUtility.cpp

52 lines
2.1 KiB
C++
Raw Normal View History

2023-07-21 16:17:01 +08:00
#include "ResponseUtility.h"
#include "boost/beast.hpp"
namespace ResponseUtility {
std::string_view mimeType(std::string_view path) {
using boost::beast::iequals;
auto const ext = [&path] {
auto const pos = path.rfind(".");
if (pos == std::string_view::npos) return std::string_view{};
return path.substr(pos);
}();
if (iequals(ext, ".pdf")) return "Application/pdf";
if (iequals(ext, ".htm")) return "text/html";
if (iequals(ext, ".html")) return "text/html";
if (iequals(ext, ".php")) return "text/html";
if (iequals(ext, ".css")) return "text/css";
if (iequals(ext, ".txt")) return "text/plain";
if (iequals(ext, ".js")) return "application/javascript";
if (iequals(ext, ".json")) return "application/json";
if (iequals(ext, ".xml")) return "application/xml";
if (iequals(ext, ".swf")) return "application/x-shockwave-flash";
if (iequals(ext, ".flv")) return "video/x-flv";
if (iequals(ext, ".png")) return "image/png";
if (iequals(ext, ".jpe")) return "image/jpeg";
if (iequals(ext, ".jpeg")) return "image/jpeg";
if (iequals(ext, ".jpg")) return "image/jpeg";
if (iequals(ext, ".gif")) return "image/gif";
if (iequals(ext, ".bmp")) return "image/bmp";
if (iequals(ext, ".ico")) return "image/vnd.microsoft.icon";
if (iequals(ext, ".tiff")) return "image/tiff";
if (iequals(ext, ".tif")) return "image/tiff";
if (iequals(ext, ".svg")) return "image/svg+xml";
if (iequals(ext, ".svgz")) return "image/svg+xml";
return "application/text";
}
std::string pathCat(std::string_view base, std::string_view path) {
if (base.empty()) return std::string(path);
std::string result(base);
char constexpr path_separator = '/';
if (result.back() == path_separator && path.front() == path_separator) {
result.resize(result.size() - 1);
} else if (result.back() != path_separator && path.front() != path_separator) {
result.append("/");
}
result.append(path.data(), path.size());
return result;
}
} // namespace ResponseUtility