1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3 4# This scripts reads static table entries [1] and generates 5# nghttp2_hd_static_entry table. This table is used in 6# lib/nghttp2_hd.c. 7# 8# [1] https://httpwg.org/specs/rfc7541.html 9 10import re, sys 11 12def hd_map_hash(name): 13 h = 2166136261 14 15 # FNV hash variant: http://isthe.com/chongo/tech/comp/fnv/ 16 for c in name: 17 h ^= ord(c) 18 h *= 16777619 19 h &= 0xffffffff 20 21 return h 22 23entries = [] 24for line in sys.stdin: 25 m = re.match(r'(\d+)\s+(\S+)\s+(\S.*)?', line) 26 val = m.group(3).strip() if m.group(3) else '' 27 entries.append((int(m.group(1)), m.group(2), val)) 28 29print('static nghttp2_hd_entry static_table[] = {') 30idx = 0 31for i, ent in enumerate(entries): 32 if entries[idx][1] != ent[1]: 33 idx = i 34 print('MAKE_STATIC_ENT("{}", "{}", {}, {}u),'\ 35 .format(ent[1], ent[2], entries[idx][0] - 1, hd_map_hash(ent[1]))) 36print('};') 37