Skip to content

Commit a21e231

Browse files
web: api: add mimes type map (#890)
1 parent 2c4e293 commit a21e231

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

src/confighttp.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ void not_found(resp_https_t response, req_https_t request) {
144144
<< data.str();
145145
}
146146

147+
// todo - combine these functions into a single function that accepts the page, i.e "index", "pin", "apps"
147148
void getIndexPage(resp_https_t response, req_https_t request) {
148149
if(!authenticate(response, request)) return;
149150

@@ -229,6 +230,8 @@ void getTroubleshootingPage(resp_https_t response, req_https_t request) {
229230
}
230231

231232
void getFaviconImage(resp_https_t response, req_https_t request) {
233+
// todo - combine function with getSunshineLogoImage and possibly getNodeModules
234+
// todo - use mime_types map
232235
print_req(request);
233236

234237
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) {
238241
}
239242

240243
void getSunshineLogoImage(resp_https_t response, req_https_t request) {
244+
// todo - combine function with getFaviconImage and possibly getNodeModules
245+
// todo - use mime_types map
241246
print_req(request);
242247

243248
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) {
269274
}
270275
else {
271276
auto relPath = fs::relative(filePath, webDirPath);
272-
if(relPath.extension() == ".ttf" or relPath.extension() == ".woff2") {
273-
// Fonts are read differntly
277+
// get the mime type from the file extension mime_types map
278+
// remove the leading period from the extension
279+
auto mimeType = mime_types.find(relPath.extension().string().substr(1));
280+
// check if the extension is in the map at the x position
281+
if(mimeType != mime_types.end()) {
282+
// if it is, set the content type to the mime type
274283
SimpleWeb::CaseInsensitiveMultimap headers;
275-
std::ifstream in((filePath).c_str(), std::ios::binary);
276-
headers.emplace("Content-Type", "font/" + filePath.extension().string().substr(1));
284+
headers.emplace("Content-Type", mimeType->second);
285+
std::ifstream in(filePath.string(), std::ios::binary);
277286
response->write(SimpleWeb::StatusCode::success_ok, in, headers);
278287
}
279-
else {
280-
std::string content = read_file((filePath.string()).c_str());
281-
response->write(content);
282-
}
288+
// do not return any file if the type is not in the map
283289
}
284290
}
285291

src/confighttp.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,23 @@ constexpr auto PORT_HTTPS = 1;
1616
void start();
1717
} // namespace confighttp
1818

19+
// mime types map
20+
const std::map<std::string, std::string> mime_types = {
21+
{ "css", "text/css" },
22+
{ "gif", "image/gif" },
23+
{ "htm", "text/html" },
24+
{ "html", "text/html" },
25+
{ "ico", "image/x-icon" },
26+
{ "jpeg", "image/jpeg" },
27+
{ "jpg", "image/jpeg" },
28+
{ "js", "application/javascript" },
29+
{ "json", "application/json" },
30+
{ "png", "image/png" },
31+
{ "svg", "image/svg+xml" },
32+
{ "ttf", "font/ttf" },
33+
{ "txt", "text/plain" },
34+
{ "woff2", "font/woff2" },
35+
{ "xml", "text/xml" },
36+
};
37+
1938
#endif // SUNSHINE_CONFIGHTTP_H

0 commit comments

Comments
 (0)