| 1 | // Copyright (C) Microsoft Corporation. All rights reserved. |
| 2 | |
| 3 | #pragma once |
| 4 | |
| 5 | #include <algorithm> |
| 6 | #include <deque> |
| 7 | #include <string_view> |
| 8 | |
| 9 | namespace wsl::windows::common { |
| 10 | |
| 11 | // Detects the end-of-header marker ("\r\n\r\n") in an HTTP message |
| 12 | class HttpHeaderEndDetector |
| 13 | { |
| 14 | public: |
| 15 | // Returns true once the full "\r\n\r\n" terminator has been consumed. |
| 16 | bool Consume(char byte) |
| 17 | { |
| 18 | if (m_done) |
| 19 | { |
| 20 | return true; |
| 21 | } |
| 22 | |
| 23 | m_last4Bytes.push_back(byte); |
| 24 | if (m_last4Bytes.size() > 4) |
| 25 | { |
| 26 | m_last4Bytes.pop_front(); |
| 27 | } |
| 28 | |
| 29 | static constexpr std::string_view c_terminator = "\r\n\r\n"; |
| 30 | m_done = std::ranges::equal(m_last4Bytes, c_terminator); |
| 31 | return m_done; |
| 32 | } |
| 33 | |
| 34 | bool IsDone() const noexcept |
| 35 | { |
| 36 | return m_done; |
| 37 | } |
| 38 | |
| 39 | private: |
| 40 | std::deque<char> m_last4Bytes; |
| 41 | bool m_done = false; |
| 42 | }; |
| 43 | |
| 44 | } // namespace wsl::windows::common |