#include #include #include #include #include #include "helpers.h" using namespace std; using namespace boost; namespace uad { boost::beast::string_view MimeType(boost::beast::string_view path) { using boost::beast::iequals; auto const ext = [&path] { auto const pos = path.rfind("."); if (pos == boost::beast::string_view::npos) return boost::beast::string_view{}; return path.substr(pos); }(); 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(boost::beast::string_view base, boost::beast::string_view path) { if (base.empty()) return std::string(path); std::string result(base); #ifdef BOOST_MSVC char constexpr path_separator = '\\'; if (result.back() == path_separator) result.resize(result.size() - 1); result.append(path.data(), path.size()); for (auto& c : result) if (c == '/') c = path_separator; #else char constexpr path_separator = '/'; if (result.back() == path_separator) result.resize(result.size() - 1); result.append(path.data(), path.size()); #endif return result; } void Fail(boost::beast::error_code ec, char const* what) { std::cerr << what << ": " << ec.message() << "\n"; } std::string ToHex(std::byte* src, size_t len) { if (!src || !len) return ""; string ret; ret.reserve(len * 2); boost::format formatter = boost::format("%02X"); for (size_t i = 0; i < len; ++i) { byte target_byte = src[i]; formatter % static_cast(target_byte); ret += formatter.str(); formatter.clear(); } return ret; } std::string HashPassword(const std::string& password) { size_t calculated_hash = std::hash{}(password); return ToHex((byte*)&calculated_hash, sizeof(calculated_hash)); } uint64_t Random() { std::random_device device; std::mt19937_64 random_generator(device()); std::uniform_int_distribution dist(0,UINT64_MAX); return dist(random_generator); } } // namespace uad