Coverage Report

Created: 2026-03-28 06:49

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/trafficserver/include/proxy/hdrs/HdrToken.h
Line
Count
Source
1
/** @file
2
3
  A brief file description
4
5
  @section license License
6
7
  Licensed to the Apache Software Foundation (ASF) under one
8
  or more contributor license agreements.  See the NOTICE file
9
  distributed with this work for additional information
10
  regarding copyright ownership.  The ASF licenses this file
11
  to you under the Apache License, Version 2.0 (the
12
  "License"); you may not use this file except in compliance
13
  with the License.  You may obtain a copy of the License at
14
15
      http://www.apache.org/licenses/LICENSE-2.0
16
17
  Unless required by applicable law or agreed to in writing, software
18
  distributed under the License is distributed on an "AS IS" BASIS,
19
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20
  See the License for the specific language governing permissions and
21
  limitations under the License.
22
 */
23
24
#pragma once
25
26
#include <cassert>
27
#include <sys/types.h>
28
#include "tscore/ink_assert.h"
29
#include "tscore/ink_atomic.h"
30
#include "tscore/ink_defs.h"
31
#include "tscore/ink_memory.h"
32
#include "tscore/ink_string.h"
33
#include "tscore/Allocator.h"
34
#include "tsutil/Regex.h"
35
#include "tscore/ink_apidefs.h"
36
37
////////////////////////////////////////////////////////////////////////////
38
//
39
//      tokenized string data
40
//
41
////////////////////////////////////////////////////////////////////////////
42
43
545
#define SIZEOF(x) (sizeof(x) / sizeof(x[0]))
44
45
enum class HdrTokenType { OTHER = 0, FIELD = 1, METHOD = 2, SCHEME = 3, CACHE_CONTROL = 4 };
46
47
struct HdrTokenTypeBinding {
48
  const char  *name;
49
  HdrTokenType type;
50
};
51
52
enum class HdrTokenInfoFlags : uint32_t {
53
  NONE      = 0,
54
  COMMAS    = 1 << 0,
55
  MULTVALS  = 1 << 1,
56
  HOPBYHOP  = 1 << 2,
57
  PROXYAUTH = 1 << 3,
58
};
59
60
inline HdrTokenInfoFlags
61
operator|(HdrTokenInfoFlags a, HdrTokenInfoFlags b)
62
68
{
63
68
  return static_cast<HdrTokenInfoFlags>(static_cast<uint32_t>(a) | static_cast<uint32_t>(b));
64
68
}
65
66
inline HdrTokenInfoFlags
67
operator&(HdrTokenInfoFlags a, HdrTokenInfoFlags b)
68
0
{
69
0
  return static_cast<HdrTokenInfoFlags>(static_cast<uint32_t>(a) & static_cast<uint32_t>(b));
70
0
}
71
72
struct HdrTokenFieldInfo {
73
  const char       *name;
74
  int32_t           slotid;
75
  uint64_t          mask;
76
  HdrTokenInfoFlags flags;
77
};
78
79
struct HdrTokenTypeSpecific {
80
  union {
81
    struct {
82
      uint32_t cc_mask;
83
    } cache_control;
84
  } u;
85
};
86
87
struct HdrTokenHeapPrefix {
88
  int                  wks_idx;
89
  int                  wks_length;
90
  HdrTokenType         wks_token_type;
91
  HdrTokenFieldInfo    wks_info;
92
  HdrTokenTypeSpecific wks_type_specific;
93
};
94
95
extern DFA *hdrtoken_strs_dfa;
96
extern int  hdrtoken_num_wks;
97
98
extern const char       *hdrtoken_strs[];
99
extern int               hdrtoken_str_lengths[];
100
extern HdrTokenType      hdrtoken_str_token_types[];
101
extern int32_t           hdrtoken_str_slotids[];
102
extern uint64_t          hdrtoken_str_masks[];
103
extern HdrTokenInfoFlags hdrtoken_str_flags[];
104
105
////////////////////////////////////////////////////////////////////////////
106
//
107
//      tokenized string functions
108
//
109
////////////////////////////////////////////////////////////////////////////
110
111
extern void        hdrtoken_init();
112
extern int         hdrtoken_tokenize_dfa(const char *string, int string_len, const char **wks_string_out = nullptr);
113
extern int         hdrtoken_tokenize(const char *string, int string_len, const char **wks_string_out = nullptr);
114
extern int         hdrtoken_method_tokenize(const char *string, int string_len);
115
extern const char *hdrtoken_string_to_wks(const char *string);
116
extern const char *hdrtoken_string_to_wks(const char *string, int length);
117
extern c_str_view  hdrtoken_string_to_wks_sv(const char *string);
118
extern c_str_view  hdrtoken_string_to_wks_sv(const char *string, int length);
119
120
/*-------------------------------------------------------------------------
121
  -------------------------------------------------------------------------*/
122
123
inline bool
124
hdrtoken_is_wks(const char *str)
125
134k
{
126
134k
  extern const char *_hdrtoken_strs_heap_f;
127
134k
  extern const char *_hdrtoken_strs_heap_l;
128
129
134k
  return ((str >= _hdrtoken_strs_heap_f) && (str <= _hdrtoken_strs_heap_l));
130
134k
}
131
132
/*-------------------------------------------------------------------------
133
  -------------------------------------------------------------------------*/
134
135
inline bool
136
hdrtoken_is_valid_wks_idx(int wks_idx)
137
12.0k
{
138
12.0k
  return ((wks_idx >= 0) && (wks_idx < hdrtoken_num_wks));
139
12.0k
}
140
141
/*-------------------------------------------------------------------------
142
  -------------------------------------------------------------------------*/
143
144
// ToDo: This, and dependencies / users should probably be const HdrTokenHeapPrefix * IMO.
145
inline HdrTokenHeapPrefix *
146
hdrtoken_wks_to_prefix(const char *wks)
147
29.0k
{
148
29.0k
  ink_assert(hdrtoken_is_wks(wks));
149
29.0k
  return reinterpret_cast<HdrTokenHeapPrefix *>(const_cast<char *>(wks) - sizeof(HdrTokenHeapPrefix));
150
29.0k
}
151
152
/*-------------------------------------------------------------------------
153
  -------------------------------------------------------------------------*/
154
155
inline const char *
156
hdrtoken_index_to_wks(int wks_idx)
157
7.37k
{
158
7.37k
  ink_assert(hdrtoken_is_valid_wks_idx(wks_idx));
159
7.37k
  return hdrtoken_strs[wks_idx];
160
7.37k
}
161
162
inline int
163
hdrtoken_index_to_length(int wks_idx)
164
13
{
165
13
  ink_assert(hdrtoken_is_valid_wks_idx(wks_idx));
166
13
  return hdrtoken_str_lengths[wks_idx];
167
13
}
168
169
inline HdrTokenType
170
hdrtoken_index_to_token_type(int wks_idx)
171
0
{
172
0
  ink_assert(hdrtoken_is_valid_wks_idx(wks_idx));
173
0
  return hdrtoken_str_token_types[wks_idx];
174
0
}
175
176
inline int
177
hdrtoken_index_to_slotid(int wks_idx)
178
2.67k
{
179
2.67k
  ink_assert(hdrtoken_is_valid_wks_idx(wks_idx));
180
2.67k
  return hdrtoken_str_slotids[wks_idx];
181
2.67k
}
182
183
inline uint64_t
184
hdrtoken_index_to_mask(int wks_idx)
185
0
{
186
0
  ink_assert(hdrtoken_is_valid_wks_idx(wks_idx));
187
0
  return hdrtoken_str_masks[wks_idx];
188
0
}
189
190
inline HdrTokenInfoFlags
191
hdrtoken_index_to_flags(int wks_idx)
192
0
{
193
0
  ink_assert(hdrtoken_is_valid_wks_idx(wks_idx));
194
0
  return hdrtoken_str_flags[wks_idx];
195
0
}
196
197
inline HdrTokenHeapPrefix *
198
hdrtoken_index_to_prefix(int wks_idx)
199
254
{
200
254
  ink_assert(hdrtoken_is_valid_wks_idx(wks_idx));
201
254
  return hdrtoken_wks_to_prefix(hdrtoken_index_to_wks(wks_idx));
202
254
}
203
204
/*-------------------------------------------------------------------------
205
  -------------------------------------------------------------------------*/
206
207
inline int
208
hdrtoken_wks_to_index(const char *wks)
209
5.19k
{
210
5.19k
  ink_assert(hdrtoken_is_wks(wks));
211
5.19k
  return hdrtoken_wks_to_prefix(wks)->wks_idx;
212
5.19k
}
213
214
inline int
215
hdrtoken_wks_to_length(const char *wks)
216
5.29k
{
217
5.29k
  ink_assert(hdrtoken_is_wks(wks));
218
5.29k
  return hdrtoken_wks_to_prefix(wks)->wks_length;
219
5.29k
}
220
221
inline HdrTokenType
222
hdrtoken_wks_to_token_type(const char *wks)
223
50
{
224
50
  ink_assert(hdrtoken_is_wks(wks));
225
50
  return hdrtoken_wks_to_prefix(wks)->wks_token_type;
226
50
}
227
228
inline int
229
hdrtoken_wks_to_slotid(const char *wks)
230
0
{
231
0
  ink_assert(hdrtoken_is_wks(wks));
232
0
  return hdrtoken_wks_to_prefix(wks)->wks_info.slotid;
233
0
}
234
235
inline uint64_t
236
hdrtoken_wks_to_mask(const char *wks)
237
2.67k
{
238
2.67k
  ink_assert(hdrtoken_is_wks(wks));
239
2.67k
  HdrTokenHeapPrefix *prefix = hdrtoken_wks_to_prefix(wks);
240
2.67k
  return prefix->wks_info.mask;
241
2.67k
}
242
243
inline HdrTokenInfoFlags
244
hdrtoken_wks_to_flags(const char *wks)
245
0
{
246
0
  ink_assert(hdrtoken_is_wks(wks));
247
0
  return hdrtoken_wks_to_prefix(wks)->wks_info.flags;
248
0
}
249
250
/*-------------------------------------------------------------------------
251
  -------------------------------------------------------------------------*/
252
253
////////////////////////////////////////////////////////////////////////////
254
//
255
//      tokenized string mime slot ids
256
//
257
//      (up to 32 of the most common headers are allowed to be placed in
258
//       special fast slots and contain presence bits and other info)
259
//
260
////////////////////////////////////////////////////////////////////////////
261
262
#define MIME_SLOTID_ACCEPT              0
263
#define MIME_SLOTID_ACCEPT_CHARSET      1
264
#define MIME_SLOTID_ACCEPT_ENCODING     2
265
#define MIME_SLOTID_ACCEPT_LANGUAGE     3
266
#define MIME_SLOTID_AGE                 4
267
#define MIME_SLOTID_AUTHORIZATION       5
268
#define MIME_SLOTID_CACHE_CONTROL       6
269
#define MIME_SLOTID_CLIENT_IP           7
270
#define MIME_SLOTID_CONNECTION          8
271
#define MIME_SLOTID_CONTENT_ENCODING    9
272
#define MIME_SLOTID_CONTENT_LANGUAGE    10
273
#define MIME_SLOTID_CONTENT_LENGTH      11
274
#define MIME_SLOTID_CONTENT_TYPE        12
275
#define MIME_SLOTID_COOKIE              13
276
#define MIME_SLOTID_DATE                14
277
#define MIME_SLOTID_EXPIRES             15
278
#define MIME_SLOTID_IF_MATCH            16
279
#define MIME_SLOTID_IF_MODIFIED_SINCE   17
280
#define MIME_SLOTID_IF_NONE_MATCH       18
281
#define MIME_SLOTID_IF_RANGE            19
282
#define MIME_SLOTID_IF_UNMODIFIED_SINCE 20
283
#define MIME_SLOTID_LAST_MODIFIED       21
284
#define MIME_SLOTID_PRAGMA              22
285
#define MIME_SLOTID_PROXY_CONNECTION    23
286
#define MIME_SLOTID_RANGE               24
287
#define MIME_SLOTID_SET_COOKIE          25
288
#define MIME_SLOTID_TE                  26
289
#define MIME_SLOTID_TRANSFER_ENCODING   27
290
#define MIME_SLOTID_USER_AGENT          28
291
#define MIME_SLOTID_VARY                29
292
#define MIME_SLOTID_VIA                 30
293
#define MIME_SLOTID_WWW_AUTHENTICATE    31
294
295
7.72k
#define MIME_SLOTID_NONE -1
296
297
////////////////////////////////////////////////////////////////////////////
298
//
299
//      tokenized string mime presence masks
300
//
301
//      (up to 64 headers get bitmasks for presence calculations)
302
//
303
////////////////////////////////////////////////////////////////////////////
304
305
// Windows insists on doing everything it's own completely
306
//   incompatible way, including integer constant subscripts.
307
//   It's too easy to match a subscript to a type since everything
308
//   won't break if the type is a different size.  Oh no, we
309
//   need to define the number of bits in our constants to make
310
//   life hard
311
135
#define TOK_64_CONST(x) x##LL
312
313
#define MIME_PRESENCE_ACCEPT              (TOK_64_CONST(1) << 0)
314
#define MIME_PRESENCE_ACCEPT_CHARSET      (TOK_64_CONST(1) << 1)
315
#define MIME_PRESENCE_ACCEPT_ENCODING     (TOK_64_CONST(1) << 2)
316
#define MIME_PRESENCE_ACCEPT_LANGUAGE     (TOK_64_CONST(1) << 3)
317
#define MIME_PRESENCE_ACCEPT_RANGES       (TOK_64_CONST(1) << 4)
318
#define MIME_PRESENCE_AGE                 (TOK_64_CONST(1) << 5)
319
#define MIME_PRESENCE_ALLOW               (TOK_64_CONST(1) << 6)
320
#define MIME_PRESENCE_AUTHORIZATION       (TOK_64_CONST(1) << 7)
321
#define MIME_PRESENCE_BYTES               (TOK_64_CONST(1) << 8)
322
#define MIME_PRESENCE_CACHE_CONTROL       (TOK_64_CONST(1) << 9)
323
#define MIME_PRESENCE_CLIENT_IP           (TOK_64_CONST(1) << 10)
324
#define MIME_PRESENCE_CONNECTION          (TOK_64_CONST(1) << 11)
325
#define MIME_PRESENCE_CONTENT_ENCODING    (TOK_64_CONST(1) << 12)
326
#define MIME_PRESENCE_CONTENT_LANGUAGE    (TOK_64_CONST(1) << 13)
327
#define MIME_PRESENCE_CONTENT_LENGTH      (TOK_64_CONST(1) << 14)
328
#define MIME_PRESENCE_CONTENT_LOCATION    (TOK_64_CONST(1) << 15)
329
#define MIME_PRESENCE_CONTENT_MD5         (TOK_64_CONST(1) << 16)
330
#define MIME_PRESENCE_CONTENT_RANGE       (TOK_64_CONST(1) << 17)
331
#define MIME_PRESENCE_CONTENT_TYPE        (TOK_64_CONST(1) << 18)
332
#define MIME_PRESENCE_COOKIE              (TOK_64_CONST(1) << 19)
333
#define MIME_PRESENCE_DATE                (TOK_64_CONST(1) << 20)
334
#define MIME_PRESENCE_ETAG                (TOK_64_CONST(1) << 21)
335
#define MIME_PRESENCE_EXPIRES             (TOK_64_CONST(1) << 22)
336
#define MIME_PRESENCE_FROM                (TOK_64_CONST(1) << 23)
337
#define MIME_PRESENCE_HOST                (TOK_64_CONST(1) << 24)
338
#define MIME_PRESENCE_IF_MATCH            (TOK_64_CONST(1) << 25)
339
#define MIME_PRESENCE_IF_MODIFIED_SINCE   (TOK_64_CONST(1) << 26)
340
#define MIME_PRESENCE_IF_NONE_MATCH       (TOK_64_CONST(1) << 27)
341
#define MIME_PRESENCE_IF_RANGE            (TOK_64_CONST(1) << 28)
342
#define MIME_PRESENCE_IF_UNMODIFIED_SINCE (TOK_64_CONST(1) << 29)
343
#define MIME_PRESENCE_KEEP_ALIVE          (TOK_64_CONST(1) << 30)
344
#define MIME_PRESENCE_KEYWORDS            (TOK_64_CONST(1) << 31)
345
#define MIME_PRESENCE_LAST_MODIFIED       (TOK_64_CONST(1) << 32)
346
#define MIME_PRESENCE_LINES               (TOK_64_CONST(1) << 33)
347
#define MIME_PRESENCE_LOCATION            (TOK_64_CONST(1) << 34)
348
#define MIME_PRESENCE_MAX_FORWARDS        (TOK_64_CONST(1) << 35)
349
#define MIME_PRESENCE_PATH                (TOK_64_CONST(1) << 36)
350
#define MIME_PRESENCE_PRAGMA              (TOK_64_CONST(1) << 37)
351
#define MIME_PRESENCE_PROXY_AUTHENTICATE  (TOK_64_CONST(1) << 38)
352
#define MIME_PRESENCE_PROXY_AUTHORIZATION (TOK_64_CONST(1) << 39)
353
#define MIME_PRESENCE_PROXY_CONNECTION    (TOK_64_CONST(1) << 40)
354
#define MIME_PRESENCE_PUBLIC              (TOK_64_CONST(1) << 41)
355
#define MIME_PRESENCE_RANGE               (TOK_64_CONST(1) << 42)
356
#define MIME_PRESENCE_REFERER             (TOK_64_CONST(1) << 43)
357
#define MIME_PRESENCE_SERVER              (TOK_64_CONST(1) << 44)
358
#define MIME_PRESENCE_SET_COOKIE          (TOK_64_CONST(1) << 45)
359
#define MIME_PRESENCE_SUBJECT             (TOK_64_CONST(1) << 46)
360
#define MIME_PRESENCE_SUMMARY             (TOK_64_CONST(1) << 47)
361
#define MIME_PRESENCE_TE                  (TOK_64_CONST(1) << 48)
362
#define MIME_PRESENCE_TRANSFER_ENCODING   (TOK_64_CONST(1) << 49)
363
#define MIME_PRESENCE_UPGRADE             (TOK_64_CONST(1) << 50)
364
#define MIME_PRESENCE_USER_AGENT          (TOK_64_CONST(1) << 51)
365
#define MIME_PRESENCE_VARY                (TOK_64_CONST(1) << 52)
366
#define MIME_PRESENCE_VIA                 (TOK_64_CONST(1) << 53)
367
#define MIME_PRESENCE_WARNING             (TOK_64_CONST(1) << 54)
368
#define MIME_PRESENCE_WWW_AUTHENTICATE    (TOK_64_CONST(1) << 55)
369
370
// bits 56-60 were used for a benchmark hack, but are now free to be used
371
// for something else
372
#define MIME_PRESENCE_UNUSED_1 (TOK_64_CONST(1) << 56)
373
#define MIME_PRESENCE_UNUSED_2 (TOK_64_CONST(1) << 57)
374
#define MIME_PRESENCE_UNUSED_3 (TOK_64_CONST(1) << 58)
375
#define MIME_PRESENCE_UNUSED_4 (TOK_64_CONST(1) << 59)
376
#define MIME_PRESENCE_UNUSED_5 (TOK_64_CONST(1) << 60)
377
378
#define MIME_PRESENCE_XREF (TOK_64_CONST(1) << 61)
379
380
#define MIME_PRESENCE_NONE TOK_64_CONST(0)
381
#define MIME_PRESENCE_ALL  ~(TOK_64_CONST(0))
382
383
/*-------------------------------------------------------------------------
384
  -------------------------------------------------------------------------*/
385
386
// HTTP/2 Upgrade token
387
1
#define MIME_UPGRADE_H2C_TOKEN "h2c"
388
389
/*-------------------------------------------------------------------------
390
  -------------------------------------------------------------------------*/