diff options
| author | Dan Engelbrecht <[email protected]> | 2022-03-21 12:42:45 +0100 |
|---|---|---|
| committer | Dan Engelbrecht <[email protected]> | 2022-03-21 12:42:45 +0100 |
| commit | d35df614722024ccdb746332eceda2b7a6cd0b80 (patch) | |
| tree | 57203b27fd9fd76a62b066140a0a7cf307012eb4 /zenserver/frontend/frontend.cpp | |
| parent | Missing return statement in websocket.h (diff) | |
| download | zen-d35df614722024ccdb746332eceda2b7a6cd0b80.tar.xz zen-d35df614722024ccdb746332eceda2b7a6cd0b80.zip | |
clang-format
Diffstat (limited to 'zenserver/frontend/frontend.cpp')
| -rw-r--r-- | zenserver/frontend/frontend.cpp | 108 |
1 files changed, 57 insertions, 51 deletions
diff --git a/zenserver/frontend/frontend.cpp b/zenserver/frontend/frontend.cpp index b87d7e313..6d576876f 100644 --- a/zenserver/frontend/frontend.cpp +++ b/zenserver/frontend/frontend.cpp @@ -15,15 +15,16 @@ ZEN_THIRD_PARTY_INCLUDES_END namespace zen { ////////////////////////////////////////////////////////////////////////// -static IoBuffer FindZipFsInBinary(const IoBuffer& BinBuffer) +static IoBuffer +FindZipFsInBinary(const IoBuffer& BinBuffer) { if (BinBuffer.GetSize() < 4) { return {}; } - uintptr_t Cursor = uintptr_t(BinBuffer.GetData()); - size_t BinSize = 0; + uintptr_t Cursor = uintptr_t(BinBuffer.GetData()); + size_t BinSize = 0; uint32_t Magic = *(uint32_t*)(BinBuffer.GetData()); #if ZEN_PLATFORM_LINUX @@ -31,35 +32,35 @@ static IoBuffer FindZipFsInBinary(const IoBuffer& BinBuffer) { struct Elf64Header { - char Ident[16]; - uint16_t Type; - uint16_t Machine; - uint32_t Version; - uint64_t Entry; - uint64_t ProgHeaderOffset; - uint64_t SectionHeaderOffset; - uint32_t Flags; - uint16_t EhSize; - uint16_t ProgHeaderEntrySize; - uint16_t ProgHeaderCount; - uint16_t SectionHeaderEntrySize; - uint16_t SectionHeaderCount; - uint16_t SectionStringIndex; + char Ident[16]; + uint16_t Type; + uint16_t Machine; + uint32_t Version; + uint64_t Entry; + uint64_t ProgHeaderOffset; + uint64_t SectionHeaderOffset; + uint32_t Flags; + uint16_t EhSize; + uint16_t ProgHeaderEntrySize; + uint16_t ProgHeaderCount; + uint16_t SectionHeaderEntrySize; + uint16_t SectionHeaderCount; + uint16_t SectionStringIndex; }; struct SectionHeader { - uint32_t NameIndex; - uint32_t Type; - uint64_t Flags; - uint64_t Address; - uint64_t Offset; - uint64_t Size; - uint64_t _Other[3]; + uint32_t NameIndex; + uint32_t Type; + uint64_t Flags; + uint64_t Address; + uint64_t Offset; + uint64_t Size; + uint64_t _Other[3]; }; const auto* Elf = (Elf64Header*)Cursor; - if (Elf->Ident[4] != 0x02) // Elf64 + if (Elf->Ident[4] != 0x02) // Elf64 { return {}; } @@ -76,21 +77,21 @@ static IoBuffer FindZipFsInBinary(const IoBuffer& BinBuffer) */ // What if the section headers aren't the last thing in the fiile though? - BinSize = Elf->SectionHeaderEntrySize; + BinSize = Elf->SectionHeaderEntrySize; BinSize *= Elf->SectionHeaderCount; BinSize += Elf->SectionHeaderOffset; } #elif ZEN_PLATFORM_WINDOWS if ((Magic & 0xffff) == 0x5a4d) { - const auto* Dos = (IMAGE_DOS_HEADER*)Cursor; - const auto* Nt = (IMAGE_NT_HEADERS64*)(Cursor + Dos->e_lfanew); + const auto* Dos = (IMAGE_DOS_HEADER*)Cursor; + const auto* Nt = (IMAGE_NT_HEADERS64*)(Cursor + Dos->e_lfanew); const auto* Section = (IMAGE_SECTION_HEADER*)(uintptr_t(&Nt->OptionalHeader) + Nt->FileHeader.SizeOfOptionalHeader); for (int i = 0, n = Nt->FileHeader.NumberOfSections; i < n; ++i, ++Section) { uint32_t SectionEnd = Section->PointerToRawData + Section->SizeOfRawData; - BinSize = (SectionEnd > BinSize) ? SectionEnd : BinSize; + BinSize = (SectionEnd > BinSize) ? SectionEnd : BinSize; } } #elif ZEN_PLATFORM_MAC @@ -98,35 +99,35 @@ static IoBuffer FindZipFsInBinary(const IoBuffer& BinBuffer) { struct MachInt32 { - operator uint32_t () const { return ByteSwap(Value); } - uint32_t Value; + operator uint32_t() const { return ByteSwap(Value); } + uint32_t Value; }; struct MachFatArch { - MachInt32 CpuType; - MachInt32 SubType; - MachInt32 Offset; - MachInt32 Size; - MachInt32 Alignment; + MachInt32 CpuType; + MachInt32 SubType; + MachInt32 Offset; + MachInt32 Size; + MachInt32 Alignment; }; struct MachFatHeader { uint32_t Magic; MachInt32 NumArchs; - MachFatArch Archs[]; + MachFatArch Archs[]; }; const auto* Header = (MachFatHeader*)Cursor; for (int i = 0, n = Header->NumArchs; i < n; ++i) { - const MachFatArch* Arch = Header->Archs + i; - uint32_t ArchEnd = Arch->Offset + Arch->Size; - BinSize = (ArchEnd > BinSize) ? ArchEnd : BinSize; + const MachFatArch* Arch = Header->Archs + i; + uint32_t ArchEnd = Arch->Offset + Arch->Size; + BinSize = (ArchEnd > BinSize) ? ArchEnd : BinSize; } } -#endif // win/linux/mac +#endif // win/linux/mac if (!BinSize || BinSize > BinBuffer.GetSize()) { @@ -137,13 +138,12 @@ static IoBuffer FindZipFsInBinary(const IoBuffer& BinBuffer) } //////////////////////////////////////////////////////////////////////////////// -HttpFrontendService::HttpFrontendService(std::filesystem::path Directory) -: m_Directory(Directory) +HttpFrontendService::HttpFrontendService(std::filesystem::path Directory) : m_Directory(Directory) { std::filesystem::path SelfPath = GetRunningExecutablePath(); // Locate a .zip file appended onto the end of this binary - IoBuffer SelfBuffer = IoBufferBuilder::MakeFromFile(SelfPath); + IoBuffer SelfBuffer = IoBufferBuilder::MakeFromFile(SelfPath); IoBuffer SelfTailBuffer = FindZipFsInBinary(SelfBuffer); if (SelfTailBuffer) { @@ -193,7 +193,8 @@ HttpFrontendService::HandleRequest(zen::HttpServerRequest& Request) using namespace std::literals; std::string_view Uri = Request.RelativeUri(); - for (; Uri[0] == '/'; Uri = Uri.substr(1)); + for (; Uri[0] == '/'; Uri = Uri.substr(1)) + ; if (Uri.empty()) { Uri = "index.html"sv; @@ -209,15 +210,20 @@ HttpFrontendService::HandleRequest(zen::HttpServerRequest& Request) // 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("."); + size_t DotIndex = Uri.rfind("."); if (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; + 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; } if (ContentType == HttpContentType::kCOUNT) |