libpromeki 1.0.0-alpha
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
httpheaders.h
Go to the documentation of this file.
1
8#pragma once
9
10
11#include <promeki/config.h>
12#if PROMEKI_ENABLE_HTTP
13#include <functional>
14#include <promeki/function.h>
15#include <promeki/namespace.h>
16#include <promeki/string.h>
17#include <promeki/stringlist.h>
18#include <promeki/list.h>
19#include <promeki/sharedptr.h>
20
21PROMEKI_NAMESPACE_BEGIN
22
59class HttpHeaders {
60 PROMEKI_SHARED_FINAL(HttpHeaders)
61 public:
63 using Ptr = SharedPtr<HttpHeaders>;
64
66 using List = ::promeki::List<HttpHeaders>;
67
69 using PtrList = ::promeki::List<Ptr>;
70
72 HttpHeaders() = default;
73
81 void set(const String &name, const String &value);
82
91 void add(const String &name, const String &value);
92
98 void remove(const String &name);
99
101 bool contains(const String &name) const;
102
110 String value(const String &name, const String &defaultValue = String()) const;
111
117 StringList values(const String &name) const;
118
122 void clear();
123
125 int count() const;
126
128 bool isEmpty() const { return count() == 0; }
129
139 void forEach(Function<void(const String &name, const String &value)> func) const;
140
142 bool operator==(const HttpHeaders &other) const;
143
145 bool operator!=(const HttpHeaders &other) const { return !(*this == other); }
146
156 static String foldName(const String &s);
157
158 private:
159 struct Entry {
160 using List = ::promeki::List<Entry>;
161 String name;
162 String value;
163 };
164
165 // Parallel index: lower-case key -> indices in _entries.
166 // Stored as a small associative container; we intentionally
167 // use a plain list-of-pairs because typical header counts
168 // are well under 30 and the constant-factor wins.
169 struct KeyBucket {
170 using List = ::promeki::List<KeyBucket>;
171 using IndexList = ::promeki::List<size_t>;
172 String lower;
173 IndexList indices;
174 };
175
176 // Per-name lookup uses the lower-cased name as the
177 // index into _index, which holds a list of indices
178 // into _entries. This preserves arrival order across
179 // names and supports duplicates without a multimap.
180 Entry::List _entries;
181 KeyBucket::List _index;
182
183 size_t findBucket(const String &lower) const;
184 size_t getOrCreateBucket(const String &lower);
185};
186
187PROMEKI_NAMESPACE_END
188
189#endif // PROMEKI_ENABLE_HTTP