aboutsummaryrefslogtreecommitdiff
path: root/zenserver/frontend/frontend.cpp
diff options
context:
space:
mode:
authorStefan Boberg <[email protected]>2022-06-10 15:10:59 +0200
committerStefan Boberg <[email protected]>2022-06-10 15:10:59 +0200
commitc7363b3a98df6ae20deb8eb2c1c139282954a6aa (patch)
treea4b66550b7a5c8dbd129d500d6a17a504642fc3b /zenserver/frontend/frontend.cpp
parentfixed issue where projects would not be discovered via DiscoverProjects due t... (diff)
downloadzen-c7363b3a98df6ae20deb8eb2c1c139282954a6aa.tar.xz
zen-c7363b3a98df6ae20deb8eb2c1c139282954a6aa.zip
frontend: simplified content-type logic
Diffstat (limited to 'zenserver/frontend/frontend.cpp')
-rw-r--r--zenserver/frontend/frontend.cpp41
1 files changed, 15 insertions, 26 deletions
diff --git a/zenserver/frontend/frontend.cpp b/zenserver/frontend/frontend.cpp
index 6d576876f..93e1b5e82 100644
--- a/zenserver/frontend/frontend.cpp
+++ b/zenserver/frontend/frontend.cpp
@@ -203,51 +203,40 @@ HttpFrontendService::HandleRequest(zen::HttpServerRequest& Request)
// Dismiss if the URI contains .. anywhere to prevent arbitrary file reads
if (Uri.find("..") != Uri.npos)
{
- Request.WriteResponse(HttpResponseCode::Forbidden);
- return;
+ return Request.WriteResponse(HttpResponseCode::Forbidden);
}
// Map the file extension to a MIME type. To keep things constrained, only a
- // small subset of file extensions is allowed.
- HttpContentType ContentType = HttpContentType::kCOUNT;
- size_t DotIndex = Uri.rfind(".");
- if (DotIndex != Uri.npos)
+ // small subset of file extensions is allowed
+
+ HttpContentType ContentType = HttpContentType::kUnknownContentType;
+
+ if (const size_t DotIndex = Uri.rfind("."); DotIndex != Uri.npos)
{
- const std::string_view DotExt = Uri.substr(DotIndex);
- if (DotExt == ".html")
- ContentType = HttpContentType::kHTML;
- else if (DotExt == ".js")
- ContentType = HttpContentType::kJSON;
- else if (DotExt == ".css")
- ContentType = HttpContentType::kCSS;
- else if (DotExt == ".png")
- ContentType = HttpContentType::kPNG;
- else if (DotExt == ".ico")
- ContentType = HttpContentType::kIcon;
+ const std::string_view DotExt = Uri.substr(DotIndex + 1);
+
+ ContentType = ParseContentType(DotExt);
}
- if (ContentType == HttpContentType::kCOUNT)
+ if (ContentType == HttpContentType::kUnknownContentType)
{
- Request.WriteResponse(HttpResponseCode::Forbidden);
- return;
+ return Request.WriteResponse(HttpResponseCode::Forbidden);
}
// The given content directory overrides any zip-fs discovered in the binary
if (!m_Directory.empty())
{
FileContents File = ReadFile(m_Directory / Uri);
+
if (!File.ErrorCode)
{
- Request.WriteResponse(HttpResponseCode::OK, ContentType, File.Data[0]);
- return;
+ return Request.WriteResponse(HttpResponseCode::OK, ContentType, File.Data[0]);
}
}
- IoBuffer FileBuffer = m_ZipFs.GetFile(Uri);
- if (FileBuffer)
+ if (IoBuffer FileBuffer = m_ZipFs.GetFile(Uri))
{
- Request.WriteResponse(HttpResponseCode::OK, ContentType, FileBuffer);
- return;
+ return Request.WriteResponse(HttpResponseCode::OK, ContentType, FileBuffer);
}
Request.WriteResponse(HttpResponseCode::NotFound, HttpContentType::kText, "Not found"sv);