xref: /aosp_15_r20/external/cronet/net/third_party/quiche/src/quiche/balsa/header_properties.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2022 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef QUICHE_BALSA_HEADER_PROPERTIES_H_
6 #define QUICHE_BALSA_HEADER_PROPERTIES_H_
7 
8 #include <cstdint>
9 
10 #include "absl/strings/string_view.h"
11 #include "quiche/common/platform/api/quiche_export.h"
12 
13 namespace quiche::header_properties {
14 
15 // Returns true if RFC 2616 Section 14 (or other relevant standards or
16 // practices) indicates that header can have multiple values. Note that nothing
17 // stops clients from sending multiple values of other headers, so this may not
18 // be perfectly reliable in practice.
19 QUICHE_EXPORT bool IsMultivaluedHeader(absl::string_view header);
20 
21 // An array of characters that are invalid in HTTP header field names.
22 // These are control characters, including \t, \n, \r, as well as space and
23 // (),/;<=>?@[\]{} and \x7f (see
24 // https://www.rfc-editor.org/rfc/rfc9110.html#section-5.6.2, also
25 // https://tools.ietf.org/html/rfc7230#section-3.2.6).
26 inline constexpr char kInvalidHeaderKeyCharList[] = {
27     0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
28     0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13,
29     0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D,
30     0x1E, 0x1F, ' ',  '"',  '(',  ')',  ',',  '/',  ';',  '<',
31     '=',  '>',  '?',  '@',  '[',  '\\', ']',  '{',  '}',  0x7F};
32 
33 // This is a non-compliant variant of `kInvalidHeaderKeyCharList`
34 // that allows the character '"'.
35 inline constexpr char kInvalidHeaderKeyCharListAllowDoubleQuote[] = {
36     0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
37     0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13,
38     0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D,
39     0x1E, 0x1F, ' ',  '(',  ')',  ',',  '/',  ';',  '<',  '=',
40     '>',  '?',  '@',  '[',  '\\', ']',  '{',  '}',  0x7F};
41 
42 // An array of characters that are invalid in HTTP header field values,
43 // according to RFC 7230 Section 3.2.  Valid low characters not in this array
44 // are \t (0x09), \n (0x0A), and \r (0x0D).
45 // Note that HTTP header field names are even more restrictive.
46 inline constexpr char kInvalidHeaderCharList[] = {
47     0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x0B,
48     0x0C, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
49     0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x7F};
50 
51 // Returns true if the given `c` is invalid in a header field name. The first
52 // version is spec compliant, the second one incorrectly allows '"'.
53 QUICHE_EXPORT bool IsInvalidHeaderKeyChar(uint8_t c);
54 QUICHE_EXPORT bool IsInvalidHeaderKeyCharAllowDoubleQuote(uint8_t c);
55 // Returns true if the given `c` is invalid in a header field or the `value` has
56 // invalid characters.
57 QUICHE_EXPORT bool IsInvalidHeaderChar(uint8_t c);
58 QUICHE_EXPORT bool HasInvalidHeaderChars(absl::string_view value);
59 
60 }  // namespace quiche::header_properties
61 
62 #endif  // QUICHE_BALSA_HEADER_PROPERTIES_H_
63