Repository navigation
Expand file tree
/
Copy pathfields.hpp
More file actions
143 lines (111 loc) · 5.13 KB
/
Copy pathfields.hpp
File metadata and controls
143 lines (111 loc) · 5.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
This file is part of libhttpserver
Copyright (C) 2011-2026 Sebastiano Merlino
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the file
LICENSE in the distribution; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#ifndef SRC_HTTPSERVER_HTTP_FIELDS_HPP_
#define SRC_HTTPSERVER_HTTP_FIELDS_HPP_
#include <algorithm>
#include <cstddef>
#include <optional>
#include <span>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
namespace httpserver {
namespace detail { class qpack_encoder; }
namespace http {
// Insertion-ordered multivalue field collection (PRD-V3N-REQ-017/018).
// Headers and trailers preserve wire order and repeated names: append
// keeps existing entries and adds a new one at the end of the
// sequence; replace erases every entry for the name (any case) and
// appends a single new entry at the end of the sequence; first
// returns the earliest received occurrence; all returns the values in
// received order.
//
// Name lookup is case-insensitive, but the first-seen spelling of a
// name is preserved and exposed by entries() so the sequence can be
// re-emitted on the wire as received.
//
// The type is a deep-copyable value type. Views returned by first(),
// all(), and entries() refer to storage owned by the container and are
// invalidated by any subsequent mutation.
class fields {
public:
// One received field occurrence. The views refer to container
// storage and are invalidated by mutation.
struct entry {
std::string_view name;
std::string_view value;
};
// Adds a new occurrence at the end of the sequence, keeping any
// existing entries for the name.
void append(std::string_view name, std::string_view value);
// Erases every entry for the name (any case), then appends a
// single entry at the end of the sequence. The first-seen spelling
// of the name is retained.
void replace(std::string_view name, std::string_view value);
// Erases every entry for the name (any case). Returns true iff any
// entry was removed.
bool remove(std::string_view name);
// Earliest received occurrence of the name, or nullopt.
std::optional<std::string_view> first(std::string_view name) const;
// All occurrences of the name in received order; empty span when
// the name is absent.
std::span<const std::string> all(std::string_view name) const;
// Number of occurrences of the name.
std::size_t count(std::string_view name) const;
// The full sequence in insertion (wire) order. Building the cached views
// may allocate; callers performing admission can contain allocation failure.
std::span<const entry> entries() const;
bool empty() const noexcept { return order_.empty(); }
// Total number of field occurrences across all names.
std::size_t size() const noexcept { return order_.size(); }
// Entry-sequence equality: same number of occurrences in the same
// order, with equal (case-insensitive) names and equal values at
// every position.
friend bool operator==(const fields& a, const fields& b);
friend bool operator!=(const fields& a, const fields& b) {
return !(a == b);
}
private:
// The private codec traverses owned occurrences without allocating the entry cache.
friend class httpserver::detail::qpack_encoder;
static constexpr std::size_t npos = static_cast<std::size_t>(-1);
static bool iequals(std::string_view a, std::string_view b) noexcept;
static constexpr char ascii_lower(const char c) noexcept {
const auto u = static_cast<unsigned char>(c);
return (u >= 'A' && u <= 'Z')
? static_cast<char>(u - 'A' + 'a')
: c;
}
std::size_t find_key(std::string_view name) const noexcept;
std::size_t find_or_add_key(std::string_view name);
void erase_order_for(std::size_t key);
void rebuild_cache() const;
// Distinct names in first-seen order (first-seen spelling kept),
// with one value list per name in received order.
std::vector<std::string> name_store_;
std::vector<std::vector<std::string>> value_store_;
// (name key, value index) per occurrence, in insertion order.
std::vector<std::pair<std::size_t, std::size_t>> order_;
// Lazily rebuilt views for entries(); invalidated by mutation.
mutable std::vector<entry> entry_cache_;
mutable bool cache_dirty_ = true;
};
} // namespace http
} // namespace httpserver
#endif // SRC_HTTPSERVER_HTTP_FIELDS_HPP_