1'use strict';
2
3const common = require('../common.js');
4const _checkInvalidHeaderChar = require('_http_common')._checkInvalidHeaderChar;
5
6const groupedInputs = {
7  // Representative set of inputs from an AcmeAir benchmark run:
8  // all valid strings, average length 14.4, stdev 13.0
9  group_acmeair: [
10    'W/"2-d4cbb29"', 'OK', 'Express', 'X-HTTP-Method-Override', 'Express',
11    'application/json', 'application/json; charset=utf-8', '206', 'OK',
12    'sessionid=; Path=/', 'text/html; charset=utf-8',
13    'text/html; charset=utf-8', '10', 'W/"a-eda64de5"', 'OK', 'Express',
14    'application/json', 'application/json; charset=utf-8', '2', 'W/"2-d4cbb29"',
15    'OK', 'Express', 'X-HTTP-Method-Override', 'sessionid=; Path=/', 'Express',
16    'sessionid=; Path=/,sessionid=6b059402-d62f-4e6f-b3dd-ce5b9e487c39; Path=/',
17    'text/html; charset=utf-8', 'text/html; charset=utf-8', '9', 'OK',
18    'sessionid=; Path=/', 'text/html; charset=utf-8',
19    'text/html; charset=utf-8', '10', 'W/"a-eda64de5"', 'OK', 'Express',
20    'Express', 'X-HTTP-Method-Override', 'sessionid=; Path=/',
21    'application/json',
22  ],
23
24  // Put it here so the benchmark result lines will not be super long.
25  LONG_AND_INVALID: ['Here is a value that is really a folded header ' +
26    'value\r\n  this should be supported, but it is not currently'],
27};
28
29const inputs = [
30  // Valid
31  '',
32  '1',
33  '\t\t\t\t\t\t\t\t\t\tFoo bar baz',
34  'keep-alive',
35  'close',
36  'gzip',
37  '20091',
38  'private',
39  'text/html; charset=utf-8',
40  'text/plain',
41  'Sat, 07 May 2016 16:54:48 GMT',
42  'SAMEORIGIN',
43  'en-US',
44
45  // Invalid
46  '中文呢', // unicode
47  'foo\nbar',
48  '\x7F',
49];
50
51const bench = common.createBenchmark(main, {
52  input: inputs.concat(Object.keys(groupedInputs)),
53  n: [1e6],
54});
55
56function main({ n, input }) {
57  let inputs = [input];
58  if (Object.hasOwn(groupedInputs, input)) {
59    inputs = groupedInputs[input];
60  }
61
62  const len = inputs.length;
63  bench.start();
64  for (let i = 0; i < n; i++) {
65    _checkInvalidHeaderChar(inputs[i % len]);
66  }
67  bench.end(n);
68}
69