diff options
| author | Per Larsson <[email protected]> | 2022-03-28 09:50:17 +0200 |
|---|---|---|
| committer | Per Larsson <[email protected]> | 2022-03-28 09:50:17 +0200 |
| commit | c024da6bf59a2b3cde682880341b3fdcb1d7f0bc (patch) | |
| tree | 07371d6ffa67d55f65ef40e0ef3d00eeaa5e7020 /zenhttp | |
| parent | Initial attempt of merging asio http and websockets. (diff) | |
| download | zen-asio.tar.xz zen-asio.zip | |
Added header data.asio
Diffstat (limited to 'zenhttp')
| -rw-r--r-- | zenhttp/asiohttpserver.cpp | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/zenhttp/asiohttpserver.cpp b/zenhttp/asiohttpserver.cpp index 9185f8856..b561fe8f2 100644 --- a/zenhttp/asiohttpserver.cpp +++ b/zenhttp/asiohttpserver.cpp @@ -122,6 +122,85 @@ MessageParser::Reset() } /////////////////////////////////////////////////////////////////////////////// +class HttpHeaderData +{ +public: + struct Entry + { + uint32_t Offset{0}; + uint32_t Size{0}; + }; + + struct HeaderEntry + { + Entry Name{}; + Entry Value{}; + }; + + static constexpr size_t kMaxHeaderSize = 2048; + static constexpr size_t kMaxHeaderCount = 16; + static constexpr size_t kHeaderIndexSize = kMaxHeaderCount * sizeof(HeaderEntry); + static constexpr size_t kMaxHeaderContentSize = kMaxHeaderSize - kHeaderIndexSize; + + HttpHeaderData() { Reset(); } + + std::string_view StatusLine() const + { + const Entry* StatusEntry = reinterpret_cast<const Entry*>(m_Buffer); + const char* Buffer = reinterpret_cast<const char*>(m_Buffer) + StatusEntry->Offset; + + return std::string_view(Buffer, size_t(StatusEntry->Size)); + } + + std::string_view Url() const + { + const Entry* UrlEntry = reinterpret_cast<const Entry*>(m_Buffer + sizeof(Entry)); + const char* Buffer = reinterpret_cast<const char*>(m_Buffer) + UrlEntry->Offset; + + return std::string_view(Buffer, size_t(UrlEntry->Size)); + } + + void Reset() + { + memset(m_Buffer, 0, kMaxHeaderSize); + + m_CursorView = MutableMemoryView(&m_Buffer[kHeaderIndexSize], kMaxHeaderContentSize); + m_HeaderCount = 0; + } + + void AppendData(MemoryView Data) + { + m_CursorView.CopyFrom(Data); + m_CursorView += Data.GetSize(); + } + + void AppendStatus(Entry StatusEntry) + { + MutableMemoryView BufferView(&m_Buffer, sizeof(Entry)); + BufferView.CopyFrom(MemoryView(&StatusEntry, sizeof(Entry))); + } + + void AppendUrl(Entry StatusEntry) + { + MutableMemoryView BufferView(&m_Buffer + sizeof(Entry), sizeof(Entry)); + BufferView.CopyFrom(MemoryView(&StatusEntry, sizeof(Entry))); + } + + void AppendHeader(HeaderEntry Header) + { + MutableMemoryView HeaderView(&m_Buffer + ((m_HeaderCount + 1) * sizeof(HeaderEntry)), sizeof(HeaderEntry)); + HeaderView.CopyFrom(MemoryView(&Header, sizeof(HeaderEntry))); + + m_HeaderCount++; + } + +private: + uint8_t m_Buffer[kMaxHeaderSize]; + MutableMemoryView m_CursorView; + size_t m_HeaderCount{0}; +}; + +/////////////////////////////////////////////////////////////////////////////// class HttpRequestMessage final : public HttpServerRequest { public: |