2020-12-29 09:01:31 +01:00
|
|
|
#!/usr/bin/env python3
|
2015-02-19 16:01:15 +01:00
|
|
|
|
|
|
|
HEADERS = [
|
2015-04-15 15:32:11 +02:00
|
|
|
(':authority', 0),
|
|
|
|
(':method', 1),
|
|
|
|
(':method', 2),
|
|
|
|
(':path', 3),
|
|
|
|
(':path', 4),
|
|
|
|
(':scheme', 5),
|
|
|
|
(':scheme', 6),
|
|
|
|
(':status', 7),
|
|
|
|
(':status', 8),
|
|
|
|
(':status', 9),
|
|
|
|
(':status', 10),
|
|
|
|
(':status', 11),
|
|
|
|
(':status', 12),
|
|
|
|
(':status', 13),
|
|
|
|
('accept-charset', 14),
|
|
|
|
('accept-encoding', 15),
|
|
|
|
('accept-language', 16),
|
|
|
|
('accept-ranges', 17),
|
|
|
|
('accept', 18),
|
|
|
|
('access-control-allow-origin', 19),
|
|
|
|
('age', 20),
|
|
|
|
('allow', 21),
|
|
|
|
('authorization', 22),
|
|
|
|
('cache-control', 23),
|
|
|
|
('content-disposition', 24),
|
|
|
|
('content-encoding', 25),
|
|
|
|
('content-language', 26),
|
|
|
|
('content-length', 27),
|
|
|
|
('content-location', 28),
|
|
|
|
('content-range', 29),
|
|
|
|
('content-type', 30),
|
|
|
|
('cookie', 31),
|
|
|
|
('date', 32),
|
|
|
|
('etag', 33),
|
|
|
|
('expect', 34),
|
|
|
|
('expires', 35),
|
|
|
|
('from', 36),
|
|
|
|
('host', 37),
|
|
|
|
('if-match', 38),
|
|
|
|
('if-modified-since', 39),
|
|
|
|
('if-none-match', 40),
|
|
|
|
('if-range', 41),
|
|
|
|
('if-unmodified-since', 42),
|
|
|
|
('last-modified', 43),
|
|
|
|
('link', 44),
|
|
|
|
('location', 45),
|
|
|
|
('max-forwards', 46),
|
|
|
|
('proxy-authenticate', 47),
|
|
|
|
('proxy-authorization', 48),
|
|
|
|
('range', 49),
|
|
|
|
('referer', 50),
|
|
|
|
('refresh', 51),
|
|
|
|
('retry-after', 52),
|
|
|
|
('server', 53),
|
|
|
|
('set-cookie', 54),
|
|
|
|
('strict-transport-security', 55),
|
|
|
|
('transfer-encoding', 56),
|
|
|
|
('user-agent', 57),
|
|
|
|
('vary', 58),
|
|
|
|
('via', 59),
|
|
|
|
('www-authenticate', 60),
|
2016-03-12 07:54:48 +01:00
|
|
|
('te', None),
|
2015-04-15 15:32:11 +02:00
|
|
|
('connection', None),
|
2016-03-12 07:54:48 +01:00
|
|
|
('keep-alive',None),
|
2015-04-15 15:32:11 +02:00
|
|
|
('proxy-connection', None),
|
|
|
|
('upgrade', None),
|
2018-03-10 09:20:28 +01:00
|
|
|
(':protocol', None),
|
2015-02-19 16:01:15 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
def to_enum_hd(k):
|
|
|
|
res = 'NGHTTP2_TOKEN_'
|
|
|
|
for c in k.upper():
|
|
|
|
if c == ':' or c == '-':
|
|
|
|
res += '_'
|
|
|
|
continue
|
|
|
|
res += c
|
|
|
|
return res
|
|
|
|
|
|
|
|
def build_header(headers):
|
|
|
|
res = {}
|
2015-04-15 15:32:11 +02:00
|
|
|
for k, _ in headers:
|
2015-02-19 16:01:15 +01:00
|
|
|
size = len(k)
|
|
|
|
if size not in res:
|
|
|
|
res[size] = {}
|
|
|
|
ent = res[size]
|
|
|
|
c = k[-1]
|
|
|
|
if c not in ent:
|
|
|
|
ent[c] = []
|
2016-08-05 11:56:15 +02:00
|
|
|
if k not in ent[c]:
|
|
|
|
ent[c].append(k)
|
2015-02-19 16:01:15 +01:00
|
|
|
|
|
|
|
return res
|
|
|
|
|
|
|
|
def gen_enum():
|
2015-04-15 15:32:11 +02:00
|
|
|
name = ''
|
2020-12-29 09:01:31 +01:00
|
|
|
print('typedef enum {')
|
2015-04-15 15:32:11 +02:00
|
|
|
for k, token in HEADERS:
|
|
|
|
if token is None:
|
2020-12-29 09:01:31 +01:00
|
|
|
print(' {},'.format(to_enum_hd(k)))
|
2015-04-15 15:32:11 +02:00
|
|
|
else:
|
|
|
|
if name != k:
|
|
|
|
name = k
|
2020-12-29 09:01:31 +01:00
|
|
|
print(' {} = {},'.format(to_enum_hd(k), token))
|
|
|
|
print('} nghttp2_token;')
|
2015-02-19 16:01:15 +01:00
|
|
|
|
|
|
|
def gen_index_header():
|
2020-12-29 09:01:31 +01:00
|
|
|
print('''\
|
2016-08-05 11:56:15 +02:00
|
|
|
static int32_t lookup_token(const uint8_t *name, size_t namelen) {
|
2020-12-29 09:01:31 +01:00
|
|
|
switch (namelen) {''')
|
2015-02-19 16:01:15 +01:00
|
|
|
b = build_header(HEADERS)
|
|
|
|
for size in sorted(b.keys()):
|
|
|
|
ents = b[size]
|
2020-12-29 09:01:31 +01:00
|
|
|
print('''\
|
|
|
|
case {}:'''.format(size))
|
|
|
|
print('''\
|
|
|
|
switch (name[{}]) {{'''.format(size - 1))
|
2015-02-19 16:01:15 +01:00
|
|
|
for c in sorted(ents.keys()):
|
|
|
|
headers = sorted(ents[c])
|
2020-12-29 09:01:31 +01:00
|
|
|
print('''\
|
|
|
|
case '{}':'''.format(c))
|
2015-02-19 16:01:15 +01:00
|
|
|
for k in headers:
|
2020-12-29 09:01:31 +01:00
|
|
|
print('''\
|
2016-08-09 06:15:38 +02:00
|
|
|
if (memeq("{}", name, {})) {{
|
2015-02-19 16:01:15 +01:00
|
|
|
return {};
|
2020-12-29 09:01:31 +01:00
|
|
|
}}'''.format(k[:-1], size - 1, to_enum_hd(k)))
|
|
|
|
print('''\
|
|
|
|
break;''')
|
|
|
|
print('''\
|
2015-02-19 16:01:15 +01:00
|
|
|
}
|
2020-12-29 09:01:31 +01:00
|
|
|
break;''')
|
|
|
|
print('''\
|
2015-02-19 16:01:15 +01:00
|
|
|
}
|
|
|
|
return -1;
|
2020-12-29 09:01:31 +01:00
|
|
|
}''')
|
2015-02-19 16:01:15 +01:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
gen_enum()
|
2020-12-29 09:01:31 +01:00
|
|
|
print()
|
2015-02-19 16:01:15 +01:00
|
|
|
gen_index_header()
|