1#ifndef SOURCEMETA_CORE_HTTP_MESSAGE_H_
2#define SOURCEMETA_CORE_HTTP_MESSAGE_H_
4#include <sourcemeta/core/http_syntax.h>
5#include <sourcemeta/core/text.h>
15namespace sourcemeta::core {
33 return line.starts_with(
"HTTP/");
54template <
typename Buffer>
55 requires requires(Buffer buffer, std::string_view line) {
60 const std::string_view line) ->
void {
88template <
typename Callback>
89 requires std::invocable<Callback, std::string_view, std::string_view>
92 std::size_t cursor{input.find(
"\r\n")};
93 while (cursor != std::string_view::npos) {
95 const auto end{input.find(
"\r\n", cursor)};
96 if (end == std::string_view::npos || end == cursor) {
100 auto line{input.substr(cursor, end - cursor)};
108 if (line.front() ==
' ' || line.front() ==
'\t') {
109 while (!line.empty() && (line.front() ==
' ' || line.front() ==
'\t')) {
110 line.remove_prefix(1);
113 while (!line.empty() && (line.back() ==
' ' || line.back() ==
'\t')) {
114 line.remove_suffix(1);
124 callback(std::string_view{}, line);
129 if (!parts.has_value() || parts->first.empty()) {
133 const auto name{parts->first};
137 if (name.back() ==
' ' || name.back() ==
'\t') {
141 auto value{parts->second};
145 while (!value.empty() && (value.front() ==
' ' || value.front() ==
'\t')) {
146 value.remove_prefix(1);
149 while (!value.empty() && (value.back() ==
' ' || value.back() ==
'\t')) {
150 value.remove_suffix(1);
162 callback(name, value);
192template <
typename Callback>
193 requires std::invocable<Callback, std::string_view, std::string_view>
196 std::string_view rest{input};
197 while (!rest.empty()) {
198 std::string_view pair;
199 const auto separator{rest.find(
';')};
200 if (separator == std::string_view::npos) {
204 pair = rest.substr(0, separator);
205 rest = rest.substr(separator + 1);
209 if (!parts.has_value()) {
213 const auto name{
trim(parts->first)};
218 callback(name,
trim(parts->second));
241template <
typename Container>
242 requires requires(Container container, std::string_view entry) {
243 container.emplace_back(entry, entry);
248 [&cookies](
const std::string_view name,
249 const std::string_view value) ->
void {
250 cookies.emplace_back(name, value);
281template <
typename Container>
282 requires requires(Container container, std::string_view entry) {
283 container.emplace_back(entry);
286 const std::string_view name, Container &values)
289 [&name, &values](
const std::string_view cookie,
290 const std::string_view value) ->
void {
291 if (cookie == name) {
292 values.emplace_back(value);
318template <
typename Container>
319 requires requires(Container container, std::string name, std::string value,
320 const char character) {
321 container.emplace_back(std::move(name), std::move(value));
322 { container.empty() } -> std::convertible_to<bool>;
323 container.back().second += character;
328 [&headers](
const std::string_view name,
329 const std::string_view value) ->
auto {
334 if (!headers.empty()) {
335 auto &previous_value{headers.back().second};
336 previous_value +=
' ';
337 previous_value += value;
343 std::string header_name{name};
345 headers.emplace_back(std::move(header_name),
366template <
typename Headers>
368 std::size_t total_size{0};
369 for (
const auto &[name, value] : headers) {
376 if constexpr (
requires { value.bytes(); }) {
380 total_size += name.size() + value.bytes().size() + 4;
385 total_size += name.size() + value.size() + 4;
390 result.reserve(total_size);
391 for (
const auto &[name, value] : headers) {
405 std::string_view bytes;
406 if constexpr (
requires { value.bytes(); }) {
407 static_assert(std::is_same_v<
decltype(value.bytes()), std::string_view>,
408 "A field value must hand back a view of bytes it keeps, "
409 "as an owning result would leave nothing to write out");
410 bytes = value.bytes();
447template <
typename Headers>
449 const std::string_view name)
450 -> std::optional<std::string_view> {
454 if constexpr (
requires { headers.find(name) != headers.end(); }) {
455 const auto match{headers.find(name)};
456 if (match != headers.end()) {
457 return std::string_view{match->second};
462 for (
const auto &[header_name, header_value] : headers) {
463 if (header_name == name) {
464 return std::string_view{header_value};
auto http_parse_cookies(const std::string_view input, Callback callback) -> void
Definition http_message.h:194
auto http_accumulate_header_line(Buffer &buffer, const std::string_view line) -> void
Definition http_message.h:59
auto http_field_line_has_forbidden_byte(const std::string_view value) noexcept -> bool
Definition http_syntax.h:220
constexpr auto http_is_status_line(const std::string_view line) noexcept -> bool
Definition http_message.h:28
auto http_parse_headers(const std::string_view input, Callback callback) -> void
Definition http_message.h:90
auto http_serialize_headers(const Headers &headers) -> std::string
Definition http_message.h:367
auto http_cookie_values(const std::string_view input, const std::string_view name, Container &values) -> void
Definition http_message.h:285
auto http_header_find(const Headers &headers, const std::string_view name) -> std::optional< std::string_view >
Definition http_message.h:448
constexpr auto to_lowercase(const Character character) noexcept -> Character
Definition text.h:75
SOURCEMETA_CORE_TEXT_EXPORT auto trim(const std::string_view input) noexcept -> std::string_view
SOURCEMETA_CORE_TEXT_EXPORT auto split_once(const std::string_view input, const char delimiter) noexcept -> std::optional< std::pair< std::string_view, std::string_view > >