Bug 1795082 - Part 2/2: Drop post-processing from getURL() r=zombie
[gecko.git] / netwerk / protocol / http / nsHttpHeaderArray.h
blobe9aa0a8e7e9b149a6fb912681b2ddd4fb12e0017
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set sw=2 ts=8 et tw=80 : */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef nsHttpHeaderArray_h__
8 #define nsHttpHeaderArray_h__
10 #include "nsHttp.h"
11 #include "nsTArray.h"
12 #include "nsString.h"
14 class nsIHttpHeaderVisitor;
16 // This needs to be forward declared here so we can include only this header
17 // without also including PHttpChannelParams.h
18 namespace IPC {
19 template <typename>
20 struct ParamTraits;
21 } // namespace IPC
23 namespace mozilla {
24 namespace net {
26 class nsHttpHeaderArray {
27 public:
28 const char* PeekHeader(const nsHttpAtom& header) const;
30 // For nsHttpResponseHead nsHttpHeaderArray will keep track of the original
31 // headers as they come from the network and the parse headers used in
32 // firefox.
33 // If the original and the firefox header are the same, we will keep just
34 // one copy and marked it as eVarietyResponseNetOriginalAndResponse.
35 // If firefox header representation changes a header coming from the
36 // network (e.g. merged it) or a eVarietyResponseNetOriginalAndResponse
37 // header has been changed by SetHeader method, we will keep the original
38 // header as eVarietyResponseNetOriginal and make a copy for the new header
39 // and mark it as eVarietyResponse.
40 enum HeaderVariety {
41 eVarietyUnknown,
42 // Used only for request header.
43 eVarietyRequestOverride,
44 eVarietyRequestDefault,
45 eVarietyRequestEnforceDefault,
46 // Used only for response header.
47 eVarietyResponseNetOriginalAndResponse,
48 eVarietyResponseNetOriginal,
49 eVarietyResponse
52 // Used by internal setters: to set header from network use SetHeaderFromNet
53 [[nodiscard]] nsresult SetHeader(const nsACString& headerName,
54 const nsACString& value, bool merge,
55 HeaderVariety variety);
56 [[nodiscard]] nsresult SetHeader(const nsHttpAtom& header,
57 const nsACString& value, bool merge,
58 HeaderVariety variety);
59 [[nodiscard]] nsresult SetHeader(const nsHttpAtom& header,
60 const nsACString& headerName,
61 const nsACString& value, bool merge,
62 HeaderVariety variety);
64 // Used by internal setters to set an empty header
65 [[nodiscard]] nsresult SetEmptyHeader(const nsACString& headerName,
66 HeaderVariety variety);
68 // Merges supported headers. For other duplicate values, determines if error
69 // needs to be thrown or 1st value kept.
70 // For the response header we keep the original headers as well.
71 [[nodiscard]] nsresult SetHeaderFromNet(const nsHttpAtom& header,
72 const nsACString& headerNameOriginal,
73 const nsACString& value,
74 bool response);
76 [[nodiscard]] nsresult SetResponseHeaderFromCache(
77 const nsHttpAtom& header, const nsACString& headerNameOriginal,
78 const nsACString& value, HeaderVariety variety);
80 [[nodiscard]] nsresult GetHeader(const nsHttpAtom& header,
81 nsACString& result) const;
82 [[nodiscard]] nsresult GetOriginalHeader(const nsHttpAtom& aHeader,
83 nsIHttpHeaderVisitor* aVisitor);
84 void ClearHeader(const nsHttpAtom& h);
86 // Find the location of the given header value, or null if none exists.
87 const char* FindHeaderValue(const nsHttpAtom& header,
88 const char* value) const {
89 return nsHttp::FindToken(PeekHeader(header), value, HTTP_HEADER_VALUE_SEPS);
92 // Determine if the given header value exists.
93 bool HasHeaderValue(const nsHttpAtom& header, const char* value) const {
94 return FindHeaderValue(header, value) != nullptr;
97 bool HasHeader(const nsHttpAtom& header) const;
99 enum VisitorFilter {
100 eFilterAll,
101 eFilterSkipDefault,
102 eFilterResponse,
103 eFilterResponseOriginal
106 [[nodiscard]] nsresult VisitHeaders(nsIHttpHeaderVisitor* visitor,
107 VisitorFilter filter = eFilterAll);
109 // parse a header line, return the header atom, the header name, and the
110 // header value
111 [[nodiscard]] static nsresult ParseHeaderLine(
112 const nsACString& line, nsHttpAtom* hdr = nullptr,
113 nsACString* headerNameOriginal = nullptr, nsACString* value = nullptr);
115 void Flatten(nsACString&, bool pruneProxyHeaders, bool pruneTransients);
116 void FlattenOriginalHeader(nsACString&);
118 uint32_t Count() const { return mHeaders.Length(); }
120 const char* PeekHeaderAt(uint32_t i, nsHttpAtom& header,
121 nsACString& headerNameOriginal) const;
123 void Clear();
125 // Must be copy-constructable and assignable
126 struct nsEntry {
127 nsHttpAtom header;
128 nsCString headerNameOriginal;
129 nsCString value;
130 HeaderVariety variety = eVarietyUnknown;
132 struct MatchHeader {
133 bool Equals(const nsEntry& aEntry, const nsHttpAtom& aHeader) const {
134 return aEntry.header == aHeader;
138 bool operator==(const nsEntry& aOther) const {
139 return header == aOther.header && value == aOther.value;
143 bool operator==(const nsHttpHeaderArray& aOther) const {
144 return mHeaders == aOther.mHeaders;
147 private:
148 // LookupEntry function will never return eVarietyResponseNetOriginal.
149 // It will ignore original headers from the network.
150 int32_t LookupEntry(const nsHttpAtom& header, const nsEntry**) const;
151 int32_t LookupEntry(const nsHttpAtom& header, nsEntry**);
152 [[nodiscard]] nsresult MergeHeader(const nsHttpAtom& header, nsEntry* entry,
153 const nsACString& value,
154 HeaderVariety variety);
155 [[nodiscard]] nsresult SetHeader_internal(const nsHttpAtom& header,
156 const nsACString& headerName,
157 const nsACString& value,
158 HeaderVariety variety);
160 // Header cannot be merged: only one value possible
161 bool IsSingletonHeader(const nsHttpAtom& header);
162 // Header cannot be merged, and subsequent values should be ignored
163 bool IsIgnoreMultipleHeader(const nsHttpAtom& header);
165 // Subset of singleton headers: should never see multiple, different
166 // instances of these, else something fishy may be going on (like CLRF
167 // injection)
168 bool IsSuspectDuplicateHeader(const nsHttpAtom& header);
170 // Removes duplicate header values entries
171 // Will return unmodified header value if the header values contains
172 // non-duplicate entries
173 void RemoveDuplicateHeaderValues(const nsACString& aHeaderValue,
174 nsACString& aResult);
176 // All members must be copy-constructable and assignable
177 CopyableTArray<nsEntry> mHeaders;
179 friend struct IPC::ParamTraits<nsHttpHeaderArray>;
180 friend class nsHttpRequestHead;
183 //-----------------------------------------------------------------------------
184 // nsHttpHeaderArray <private>: inline functions
185 //-----------------------------------------------------------------------------
187 inline int32_t nsHttpHeaderArray::LookupEntry(const nsHttpAtom& header,
188 const nsEntry** entry) const {
189 uint32_t index = 0;
190 while (index != UINT32_MAX) {
191 index = mHeaders.IndexOf(header, index, nsEntry::MatchHeader());
192 if (index != UINT32_MAX) {
193 if ((&mHeaders[index])->variety != eVarietyResponseNetOriginal) {
194 *entry = &mHeaders[index];
195 return index;
197 index++;
201 return index;
204 inline int32_t nsHttpHeaderArray::LookupEntry(const nsHttpAtom& header,
205 nsEntry** entry) {
206 uint32_t index = 0;
207 while (index != UINT32_MAX) {
208 index = mHeaders.IndexOf(header, index, nsEntry::MatchHeader());
209 if (index != UINT32_MAX) {
210 if ((&mHeaders[index])->variety != eVarietyResponseNetOriginal) {
211 *entry = &mHeaders[index];
212 return index;
214 index++;
217 return index;
220 inline bool nsHttpHeaderArray::IsSingletonHeader(const nsHttpAtom& header) {
221 return header == nsHttp::Content_Type ||
222 header == nsHttp::Content_Disposition ||
223 header == nsHttp::Content_Length || header == nsHttp::User_Agent ||
224 header == nsHttp::Referer || header == nsHttp::Host ||
225 header == nsHttp::Authorization ||
226 header == nsHttp::Proxy_Authorization ||
227 header == nsHttp::If_Modified_Since ||
228 header == nsHttp::If_Unmodified_Since || header == nsHttp::From ||
229 header == nsHttp::Location || header == nsHttp::Max_Forwards ||
230 header == nsHttp::GlobalPrivacyControl ||
231 // Ignore-multiple-headers are singletons in the sense that they
232 // shouldn't be merged.
233 IsIgnoreMultipleHeader(header);
236 // These are headers for which, in the presence of multiple values, we only
237 // consider the first.
238 inline bool nsHttpHeaderArray::IsIgnoreMultipleHeader(
239 const nsHttpAtom& header) {
240 // https://tools.ietf.org/html/rfc6797#section-8:
242 // If a UA receives more than one STS header field in an HTTP
243 // response message over secure transport, then the UA MUST process
244 // only the first such header field.
245 return header == nsHttp::Strict_Transport_Security;
248 [[nodiscard]] inline nsresult nsHttpHeaderArray::MergeHeader(
249 const nsHttpAtom& header, nsEntry* entry, const nsACString& value,
250 nsHttpHeaderArray::HeaderVariety variety) {
251 // merge of empty header = no-op
252 if (value.IsEmpty() && header != nsHttp::X_Frame_Options) {
253 return NS_OK;
256 // x-frame-options having an empty header value still has an effect so we make
257 // sure that we retain encountering it
258 nsCString newValue = entry->value;
259 if (!newValue.IsEmpty() || header == nsHttp::X_Frame_Options) {
260 // Append the new value to the existing value
261 if (header == nsHttp::Set_Cookie || header == nsHttp::WWW_Authenticate ||
262 header == nsHttp::Proxy_Authenticate) {
263 // Special case these headers and use a newline delimiter to
264 // delimit the values from one another as commas may appear
265 // in the values of these headers contrary to what the spec says.
266 newValue.Append('\n');
267 } else {
268 // Delimit each value from the others using a comma (per HTTP spec)
269 newValue.AppendLiteral(", ");
273 newValue.Append(value);
274 if (entry->variety == eVarietyResponseNetOriginalAndResponse) {
275 MOZ_ASSERT(variety == eVarietyResponse);
276 entry->variety = eVarietyResponseNetOriginal;
277 // Copy entry->headerNameOriginal because in SetHeader_internal we are going
278 // to a new one and a realocation can happen.
279 nsCString headerNameOriginal = entry->headerNameOriginal;
280 nsresult rv = SetHeader_internal(header, headerNameOriginal, newValue,
281 eVarietyResponse);
282 if (NS_FAILED(rv)) {
283 return rv;
285 } else {
286 entry->value = newValue;
287 entry->variety = variety;
289 return NS_OK;
292 inline bool nsHttpHeaderArray::IsSuspectDuplicateHeader(
293 const nsHttpAtom& header) {
294 bool retval = header == nsHttp::Content_Length ||
295 header == nsHttp::Content_Disposition ||
296 header == nsHttp::Location;
298 MOZ_ASSERT(!retval || IsSingletonHeader(header),
299 "Only non-mergeable headers should be in this list\n");
301 return retval;
304 inline void nsHttpHeaderArray::RemoveDuplicateHeaderValues(
305 const nsACString& aHeaderValue, nsACString& aResult) {
306 mozilla::Maybe<nsAutoCString> result;
307 for (const nsACString& token :
308 nsCCharSeparatedTokenizer(aHeaderValue, ',').ToRange()) {
309 if (result.isNothing()) {
310 // assign the first value
311 result.emplace(token);
312 continue;
314 if (*result != token) {
315 // non-identical header values. Do not change the header values
316 result.reset();
317 break;
321 if (result.isSome()) {
322 aResult = *result;
323 } else {
324 // either header values do not have multiple values or
325 // has unequal multiple values
326 // for both the cases restore the original header value
327 aResult = aHeaderValue;
331 } // namespace net
332 } // namespace mozilla
334 #endif