Named Read() in HTTPHeaders (see PR #35182).
| 100 | |
| 101 | // Named Read() in HTTPHeaders (see PR #35182). |
| 102 | void HTTPResponseHeaders::Read(util::LineReader& reader) |
| 103 | { |
| 104 | // Headers https://httpwg.org/specs/rfc9110.html#rfc.section.6.3 |
| 105 | // A sequence of Field Lines https://httpwg.org/specs/rfc9110.html#rfc.section.5.2 |
| 106 | while (auto maybe_line = reader.ReadLine()) { |
| 107 | const std::string_view line = *maybe_line; |
| 108 | |
| 109 | // An empty line indicates end of the headers section https://www.rfc-editor.org/rfc/rfc2616#section-4 |
| 110 | if (line.empty()) return; |
| 111 | |
| 112 | // Header line must have at least one ":" |
| 113 | // keys are not allowed to have delimiters like ":" but values are |
| 114 | // https://httpwg.org/specs/rfc9110.html#rfc.section.5.6.2 |
| 115 | const size_t pos{line.find(':')}; |
| 116 | if (pos == std::string::npos) throw HTTPError{"Header missing colon (:)"}; |
| 117 | |
| 118 | // Whitespace is optional |
| 119 | std::string key = util::TrimString(std::string_view(line).substr(0, pos)); |
| 120 | std::string value = util::TrimString(std::string_view(line).substr(pos + 1)); |
| 121 | |
| 122 | // Header keys are Field Names: https://httpwg.org/specs/rfc9110.html#fields.names |
| 123 | // which consist of "tokens": https://httpwg.org/specs/rfc9110.html#rfc.section.5.6.2 |
| 124 | // that can not be empty. |
| 125 | if (key.empty()) throw HTTPError{"Empty header name"}; |
| 126 | |
| 127 | m_headers.emplace_back(std::move(key), std::move(value)); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | std::optional<std::string> HTTPResponseHeaders::FindFirst(std::string_view key) const |
| 132 | { |
no test coverage detected