From 472cf68c31bd1355fbbf8f02972927611d7a51a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=B5=A9=E8=83=BD?= <54659396+mochazi@users.noreply.github.com> Date: Wed, 12 Oct 2022 21:43:05 +0800 Subject: [PATCH] add Content-Type support (#405) --- ixwebsocket/IXHttpServer.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/ixwebsocket/IXHttpServer.cpp b/ixwebsocket/IXHttpServer.cpp index 563dca91..c015854c 100644 --- a/ixwebsocket/IXHttpServer.cpp +++ b/ixwebsocket/IXHttpServer.cpp @@ -40,6 +40,29 @@ namespace auto vec = res.second; return std::make_pair(res.first, std::string(vec.begin(), vec.end())); } + + std::string response_head_file(const std::string& file_name){ + + if (std::string::npos != file_name.find(".html") || std::string::npos != file_name.find(".htm")) + return "text/html"; + else if (std::string::npos != file_name.find(".css")) + return "text/css"; + else if (std::string::npos != file_name.find(".js") || std::string::npos != file_name.find(".mjs")) + return "application/x-javascript"; + else if (std::string::npos != file_name.find(".ico")) + return "image/x-icon"; + else if (std::string::npos != file_name.find(".png")) + return "image/png"; + else if (std::string::npos != file_name.find(".jpg") || std::string::npos != file_name.find(".jpeg")) + return "image/jpeg"; + else if (std::string::npos != file_name.find(".gif")) + return "image/gif"; + else if (std::string::npos != file_name.find(".svg")) + return "image/svg+xml"; + else + return "application/octet-stream"; + } + } // namespace namespace ix @@ -117,6 +140,7 @@ namespace ix WebSocketHttpHeaders headers; headers["Server"] = userAgent(); + headers["Content-Type"] = response_head_file(uri); std::string path("." + uri); auto res = readAsString(path);