web: api: add mimes type map (#890)
This commit is contained in:
+14
-8
@@ -144,6 +144,7 @@ void not_found(resp_https_t response, req_https_t request) {
|
|||||||
<< data.str();
|
<< data.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// todo - combine these functions into a single function that accepts the page, i.e "index", "pin", "apps"
|
||||||
void getIndexPage(resp_https_t response, req_https_t request) {
|
void getIndexPage(resp_https_t response, req_https_t request) {
|
||||||
if(!authenticate(response, request)) return;
|
if(!authenticate(response, request)) return;
|
||||||
|
|
||||||
@@ -229,6 +230,8 @@ void getTroubleshootingPage(resp_https_t response, req_https_t request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void getFaviconImage(resp_https_t response, req_https_t request) {
|
void getFaviconImage(resp_https_t response, req_https_t request) {
|
||||||
|
// todo - combine function with getSunshineLogoImage and possibly getNodeModules
|
||||||
|
// todo - use mime_types map
|
||||||
print_req(request);
|
print_req(request);
|
||||||
|
|
||||||
std::ifstream in(WEB_DIR "images/favicon.ico", std::ios::binary);
|
std::ifstream in(WEB_DIR "images/favicon.ico", std::ios::binary);
|
||||||
@@ -238,6 +241,8 @@ void getFaviconImage(resp_https_t response, req_https_t request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void getSunshineLogoImage(resp_https_t response, req_https_t request) {
|
void getSunshineLogoImage(resp_https_t response, req_https_t request) {
|
||||||
|
// todo - combine function with getFaviconImage and possibly getNodeModules
|
||||||
|
// todo - use mime_types map
|
||||||
print_req(request);
|
print_req(request);
|
||||||
|
|
||||||
std::ifstream in(WEB_DIR "images/logo-sunshine-45.png", std::ios::binary);
|
std::ifstream in(WEB_DIR "images/logo-sunshine-45.png", std::ios::binary);
|
||||||
@@ -269,17 +274,18 @@ void getNodeModules(resp_https_t response, req_https_t request) {
|
|||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
auto relPath = fs::relative(filePath, webDirPath);
|
auto relPath = fs::relative(filePath, webDirPath);
|
||||||
if(relPath.extension() == ".ttf" or relPath.extension() == ".woff2") {
|
// get the mime type from the file extension mime_types map
|
||||||
// Fonts are read differntly
|
// remove the leading period from the extension
|
||||||
|
auto mimeType = mime_types.find(relPath.extension().string().substr(1));
|
||||||
|
// check if the extension is in the map at the x position
|
||||||
|
if(mimeType != mime_types.end()) {
|
||||||
|
// if it is, set the content type to the mime type
|
||||||
SimpleWeb::CaseInsensitiveMultimap headers;
|
SimpleWeb::CaseInsensitiveMultimap headers;
|
||||||
std::ifstream in((filePath).c_str(), std::ios::binary);
|
headers.emplace("Content-Type", mimeType->second);
|
||||||
headers.emplace("Content-Type", "font/" + filePath.extension().string().substr(1));
|
std::ifstream in(filePath.string(), std::ios::binary);
|
||||||
response->write(SimpleWeb::StatusCode::success_ok, in, headers);
|
response->write(SimpleWeb::StatusCode::success_ok, in, headers);
|
||||||
}
|
}
|
||||||
else {
|
// do not return any file if the type is not in the map
|
||||||
std::string content = read_file((filePath.string()).c_str());
|
|
||||||
response->write(content);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -16,4 +16,23 @@ constexpr auto PORT_HTTPS = 1;
|
|||||||
void start();
|
void start();
|
||||||
} // namespace confighttp
|
} // namespace confighttp
|
||||||
|
|
||||||
|
// mime types map
|
||||||
|
const std::map<std::string, std::string> mime_types = {
|
||||||
|
{ "css", "text/css" },
|
||||||
|
{ "gif", "image/gif" },
|
||||||
|
{ "htm", "text/html" },
|
||||||
|
{ "html", "text/html" },
|
||||||
|
{ "ico", "image/x-icon" },
|
||||||
|
{ "jpeg", "image/jpeg" },
|
||||||
|
{ "jpg", "image/jpeg" },
|
||||||
|
{ "js", "application/javascript" },
|
||||||
|
{ "json", "application/json" },
|
||||||
|
{ "png", "image/png" },
|
||||||
|
{ "svg", "image/svg+xml" },
|
||||||
|
{ "ttf", "font/ttf" },
|
||||||
|
{ "txt", "text/plain" },
|
||||||
|
{ "woff2", "font/woff2" },
|
||||||
|
{ "xml", "text/xml" },
|
||||||
|
};
|
||||||
|
|
||||||
#endif // SUNSHINE_CONFIGHTTP_H
|
#endif // SUNSHINE_CONFIGHTTP_H
|
||||||
|
|||||||
Reference in New Issue
Block a user