blob: b38555e355350b442c0044b7943e9a3383da8b26 [file] [log] [blame]
Craig Tiller2e7687c2015-11-18 14:56:46 -08001#!/usr/bin/env python2.7
2
Craig Tiller6169d5f2016-03-31 07:46:18 -07003# Copyright 2015, Google Inc.
Craig Tiller2e7687c2015-11-18 14:56:46 -08004# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions are
8# met:
9#
10# * Redistributions of source code must retain the above copyright
11# notice, this list of conditions and the following disclaimer.
12# * Redistributions in binary form must reproduce the above
13# copyright notice, this list of conditions and the following disclaimer
14# in the documentation and/or other materials provided with the
15# distribution.
16# * Neither the name of Google Inc. nor the names of its
17# contributors may be used to endorse or promote products derived from
18# this software without specific prior written permission.
19#
20# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
32import hashlib
33import itertools
34import os
35import sys
36
37# configuration: a list of either strings or 2-tuples of strings
38# a single string represents a static grpc_mdstr
39# a 2-tuple represents a static grpc_mdelem (and appropriate grpc_mdstrs will
40# also be created)
41
42CONFIG = [
43 'grpc-timeout',
Craig Tillerebdef9d2015-11-19 17:09:49 -080044 'grpc-internal-encoding-request',
Craig Tiller2e7687c2015-11-18 14:56:46 -080045 ':path',
46 'grpc-encoding',
47 'grpc-accept-encoding',
48 'user-agent',
49 ':authority',
50 'host',
51 'grpc-message',
52 'grpc-status',
Hongyu Chen134cac22015-12-10 16:19:24 -080053 'census-bin',
Bogdan Drutu079a1792016-02-08 16:51:17 -080054 'census-binary-bin',
Craig Tiller2e7687c2015-11-18 14:56:46 -080055 '',
56 ('grpc-status', '0'),
Craig Tillerebdef9d2015-11-19 17:09:49 -080057 ('grpc-status', '1'),
58 ('grpc-status', '2'),
59 ('grpc-encoding', 'identity'),
60 ('grpc-encoding', 'gzip'),
61 ('grpc-encoding', 'deflate'),
Craig Tiller2e7687c2015-11-18 14:56:46 -080062 ('te', 'trailers'),
Craig Tillerebdef9d2015-11-19 17:09:49 -080063 ('content-type', 'application/grpc'),
Craig Tiller2e7687c2015-11-18 14:56:46 -080064 (':method', 'POST'),
65 (':status', '200'),
66 (':status', '404'),
67 (':scheme', 'http'),
68 (':scheme', 'https'),
69 (':scheme', 'grpc'),
70 (':authority', ''),
71 (':method', 'GET'),
Craig Tillerc6549762016-03-09 17:10:43 -080072 (':method', 'PUT'),
Craig Tiller2e7687c2015-11-18 14:56:46 -080073 (':path', '/'),
74 (':path', '/index.html'),
75 (':status', '204'),
76 (':status', '206'),
77 (':status', '304'),
78 (':status', '400'),
79 (':status', '500'),
80 ('accept-charset', ''),
81 ('accept-encoding', ''),
82 ('accept-encoding', 'gzip, deflate'),
83 ('accept-language', ''),
84 ('accept-ranges', ''),
85 ('accept', ''),
86 ('access-control-allow-origin', ''),
87 ('age', ''),
88 ('allow', ''),
89 ('authorization', ''),
90 ('cache-control', ''),
91 ('content-disposition', ''),
92 ('content-encoding', ''),
93 ('content-language', ''),
94 ('content-length', ''),
95 ('content-location', ''),
96 ('content-range', ''),
97 ('content-type', ''),
98 ('cookie', ''),
99 ('date', ''),
100 ('etag', ''),
101 ('expect', ''),
102 ('expires', ''),
103 ('from', ''),
104 ('host', ''),
105 ('if-match', ''),
106 ('if-modified-since', ''),
107 ('if-none-match', ''),
108 ('if-range', ''),
109 ('if-unmodified-since', ''),
110 ('last-modified', ''),
111 ('link', ''),
112 ('location', ''),
113 ('max-forwards', ''),
114 ('proxy-authenticate', ''),
115 ('proxy-authorization', ''),
116 ('range', ''),
117 ('referer', ''),
118 ('refresh', ''),
119 ('retry-after', ''),
120 ('server', ''),
121 ('set-cookie', ''),
122 ('strict-transport-security', ''),
123 ('transfer-encoding', ''),
124 ('user-agent', ''),
125 ('vary', ''),
126 ('via', ''),
127 ('www-authenticate', ''),
128]
129
Craig Tillerb2b42612015-11-20 12:02:17 -0800130COMPRESSION_ALGORITHMS = [
131 'identity',
132 'deflate',
133 'gzip',
134]
135
Craig Tiller2e7687c2015-11-18 14:56:46 -0800136# utility: mangle the name of a config
137def mangle(elem):
138 xl = {
139 '-': '_',
140 ':': '',
141 '/': 'slash',
142 '.': 'dot',
143 ',': 'comma',
144 ' ': '_',
145 }
146 def m0(x):
147 if not x: return 'empty'
148 r = ''
149 for c in x:
150 put = xl.get(c, c.lower())
151 if not put: continue
152 last_is_underscore = r[-1] == '_' if r else True
153 if last_is_underscore and put == '_': continue
154 elif len(put) > 1:
155 if not last_is_underscore: r += '_'
156 r += put
157 r += '_'
158 else:
159 r += put
160 if r[-1] == '_': r = r[:-1]
161 return r
162 if isinstance(elem, tuple):
163 return 'grpc_mdelem_%s_%s' % (m0(elem[0]), m0(elem[1]))
164 else:
165 return 'grpc_mdstr_%s' % (m0(elem))
166
167# utility: generate some hash value for a string
168def fake_hash(elem):
169 return hashlib.md5(elem).hexdigest()[0:8]
170
171# utility: print a big comment block into a set of files
172def put_banner(files, banner):
173 for f in files:
174 print >>f, '/*'
175 for line in banner:
176 print >>f, ' * %s' % line
177 print >>f, ' */'
178 print >>f
179
180# build a list of all the strings we need
181all_strs = set()
182all_elems = set()
Craig Tillerb2b42612015-11-20 12:02:17 -0800183static_userdata = {}
Craig Tiller2e7687c2015-11-18 14:56:46 -0800184for elem in CONFIG:
185 if isinstance(elem, tuple):
186 all_strs.add(elem[0])
187 all_strs.add(elem[1])
188 all_elems.add(elem)
189 else:
190 all_strs.add(elem)
Craig Tillerb2b42612015-11-20 12:02:17 -0800191compression_elems = []
192for mask in range(1, 1<<len(COMPRESSION_ALGORITHMS)):
193 val = ','.join(COMPRESSION_ALGORITHMS[alg]
194 for alg in range(0, len(COMPRESSION_ALGORITHMS))
195 if (1 << alg) & mask)
196 elem = ('grpc-accept-encoding', val)
197 all_strs.add(val)
198 all_elems.add(elem)
199 compression_elems.append(elem)
David Garcia Quintas07503b62016-03-21 11:59:33 -0700200 static_userdata[elem] = 1 + (mask | 1)
Craig Tiller2e7687c2015-11-18 14:56:46 -0800201all_strs = sorted(list(all_strs), key=mangle)
202all_elems = sorted(list(all_elems), key=mangle)
203
204# output configuration
205args = sys.argv[1:]
206H = None
207C = None
Craig Tiller134a6b62016-04-18 08:14:20 -0700208D = None
Craig Tiller2e7687c2015-11-18 14:56:46 -0800209if args:
210 if 'header' in args:
211 H = sys.stdout
212 else:
213 H = open('/dev/null', 'w')
214 if 'source' in args:
215 C = sys.stdout
216 else:
217 C = open('/dev/null', 'w')
Craig Tiller134a6b62016-04-18 08:14:20 -0700218 if 'dictionary' in args:
219 D = sys.stdout
220 else:
221 D = open('/dev/null', 'w')
Craig Tiller2e7687c2015-11-18 14:56:46 -0800222else:
223 H = open(os.path.join(
Craig Tillerffae43c2016-03-25 19:34:29 -0700224 os.path.dirname(sys.argv[0]), '../../../src/core/lib/transport/static_metadata.h'), 'w')
Craig Tiller2e7687c2015-11-18 14:56:46 -0800225 C = open(os.path.join(
Craig Tillerffae43c2016-03-25 19:34:29 -0700226 os.path.dirname(sys.argv[0]), '../../../src/core/lib/transport/static_metadata.c'), 'w')
Craig Tiller134a6b62016-04-18 08:14:20 -0700227 D = open(os.path.join(
228 os.path.dirname(sys.argv[0]), '../../../test/core/end2end/fuzzers/hpack.dictionary'), 'w')
Craig Tiller2e7687c2015-11-18 14:56:46 -0800229
230# copy-paste copyright notice from this file
231with open(sys.argv[0]) as my_source:
232 copyright = []
233 for line in my_source:
234 if line[0] != '#': break
235 for line in my_source:
236 if line[0] == '#':
237 copyright.append(line)
238 break
239 for line in my_source:
240 if line[0] != '#':
241 break
242 copyright.append(line)
Craig Tiller3bd96952016-03-28 12:43:02 -0700243 put_banner([H,C], [line[2:].rstrip() for line in copyright])
Craig Tiller2e7687c2015-11-18 14:56:46 -0800244
Craig Tiller134a6b62016-04-18 08:14:20 -0700245
246hex_bytes = [ord(c) for c in "abcdefABCDEF0123456789"]
247
248
Craig Tiller69b6d4e2016-04-18 15:08:14 -0700249def esc_dict(line):
Craig Tiller134a6b62016-04-18 08:14:20 -0700250 out = "\""
Craig Tiller134a6b62016-04-18 08:14:20 -0700251 for c in line:
252 if 32 <= c < 127:
Craig Tiller134a6b62016-04-18 08:14:20 -0700253 if c != ord('"'):
254 out += chr(c)
255 else:
256 out += "\\\""
Craig Tiller134a6b62016-04-18 08:14:20 -0700257 else:
Craig Tiller69b6d4e2016-04-18 15:08:14 -0700258 out += "\\x%02X" % c
Craig Tiller134a6b62016-04-18 08:14:20 -0700259 return out + "\""
260
Craig Tiller2e7687c2015-11-18 14:56:46 -0800261put_banner([H,C],
262"""WARNING: Auto-generated code.
263
Craig Tiller3bd96952016-03-28 12:43:02 -0700264To make changes to this file, change
265tools/codegen/core/gen_static_metadata.py, and then re-run it.
Craig Tiller51ae0be2015-11-19 07:51:25 -0800266
Craig Tiller3bd96952016-03-28 12:43:02 -0700267See metadata.h for an explanation of the interface here, and metadata.c for
268an explanation of what's going on.
Craig Tiller2e7687c2015-11-18 14:56:46 -0800269""".splitlines())
270
Craig Tiller3bd96952016-03-28 12:43:02 -0700271print >>H, '#ifndef GRPC_CORE_LIB_TRANSPORT_STATIC_METADATA_H'
272print >>H, '#define GRPC_CORE_LIB_TRANSPORT_STATIC_METADATA_H'
Craig Tiller2e7687c2015-11-18 14:56:46 -0800273print >>H
Craig Tillerffae43c2016-03-25 19:34:29 -0700274print >>H, '#include "src/core/lib/transport/metadata.h"'
Craig Tiller2e7687c2015-11-18 14:56:46 -0800275print >>H
276
Craig Tillerffae43c2016-03-25 19:34:29 -0700277print >>C, '#include "src/core/lib/transport/static_metadata.h"'
Craig Tiller2e7687c2015-11-18 14:56:46 -0800278print >>C
279
280print >>H, '#define GRPC_STATIC_MDSTR_COUNT %d' % len(all_strs)
281print >>H, 'extern grpc_mdstr grpc_static_mdstr_table[GRPC_STATIC_MDSTR_COUNT];'
282for i, elem in enumerate(all_strs):
283 print >>H, '/* "%s" */' % elem
284 print >>H, '#define %s (&grpc_static_mdstr_table[%d])' % (mangle(elem).upper(), i)
285print >>H
286print >>C, 'grpc_mdstr grpc_static_mdstr_table[GRPC_STATIC_MDSTR_COUNT];'
287print >>C
288
Craig Tiller134a6b62016-04-18 08:14:20 -0700289print >>D, '# hpack fuzzing dictionary'
290for i, elem in enumerate(all_strs):
Craig Tiller69b6d4e2016-04-18 15:08:14 -0700291 print >>D, '%s' % (esc_dict([len(elem)] + [ord(c) for c in elem]))
Craig Tiller942568b2016-04-18 22:19:00 -0700292for i, elem in enumerate(all_elems):
293 print >>D, '%s' % (esc_dict([0, len(elem[0])] + [ord(c) for c in elem[0]] +
294 [len(elem[1])] + [ord(c) for c in elem[1]]))
Craig Tiller134a6b62016-04-18 08:14:20 -0700295
Craig Tiller2e7687c2015-11-18 14:56:46 -0800296print >>H, '#define GRPC_STATIC_MDELEM_COUNT %d' % len(all_elems)
297print >>H, 'extern grpc_mdelem grpc_static_mdelem_table[GRPC_STATIC_MDELEM_COUNT];'
Craig Tillerc6549762016-03-09 17:10:43 -0800298print >>H, 'extern uintptr_t grpc_static_mdelem_user_data[GRPC_STATIC_MDELEM_COUNT];'
Craig Tiller2e7687c2015-11-18 14:56:46 -0800299for i, elem in enumerate(all_elems):
300 print >>H, '/* "%s": "%s" */' % elem
301 print >>H, '#define %s (&grpc_static_mdelem_table[%d])' % (mangle(elem).upper(), i)
302print >>H
303print >>C, 'grpc_mdelem grpc_static_mdelem_table[GRPC_STATIC_MDELEM_COUNT];'
Craig Tillerc6549762016-03-09 17:10:43 -0800304print >>C, 'uintptr_t grpc_static_mdelem_user_data[GRPC_STATIC_MDELEM_COUNT] = {'
Craig Tillerb2b42612015-11-20 12:02:17 -0800305print >>C, ' %s' % ','.join('%d' % static_userdata.get(elem, 0) for elem in all_elems)
306print >>C, '};'
Craig Tiller2e7687c2015-11-18 14:56:46 -0800307print >>C
308
309def str_idx(s):
310 for i, s2 in enumerate(all_strs):
311 if s == s2:
312 return i
313
Craig Tillerb2b42612015-11-20 12:02:17 -0800314def md_idx(m):
315 for i, m2 in enumerate(all_elems):
316 if m == m2:
317 return i
318
Craig Tillerc6549762016-03-09 17:10:43 -0800319print >>H, 'extern const uint8_t grpc_static_metadata_elem_indices[GRPC_STATIC_MDELEM_COUNT*2];'
320print >>C, 'const uint8_t grpc_static_metadata_elem_indices[GRPC_STATIC_MDELEM_COUNT*2] = {'
Craig Tiller2e7687c2015-11-18 14:56:46 -0800321print >>C, ','.join('%d' % str_idx(x) for x in itertools.chain.from_iterable([a,b] for a, b in all_elems))
322print >>C, '};'
323print >>C
324
Craig Tillerb2b42612015-11-20 12:02:17 -0800325print >>H, 'extern const char *const grpc_static_metadata_strings[GRPC_STATIC_MDSTR_COUNT];'
Craig Tiller328d4b12015-11-18 15:10:41 -0800326print >>C, 'const char *const grpc_static_metadata_strings[GRPC_STATIC_MDSTR_COUNT] = {'
Craig Tiller2e7687c2015-11-18 14:56:46 -0800327print >>C, '%s' % ',\n'.join(' "%s"' % s for s in all_strs)
328print >>C, '};'
329print >>C
330
Craig Tillerc6549762016-03-09 17:10:43 -0800331print >>H, 'extern const uint8_t grpc_static_accept_encoding_metadata[%d];' % (1 << len(COMPRESSION_ALGORITHMS))
332print >>C, 'const uint8_t grpc_static_accept_encoding_metadata[%d] = {' % (1 << len(COMPRESSION_ALGORITHMS))
Craig Tillerb2b42612015-11-20 12:02:17 -0800333print >>C, '0,%s' % ','.join('%d' % md_idx(elem) for elem in compression_elems)
334print >>C, '};'
335print >>C
336
337print >>H, '#define GRPC_MDELEM_ACCEPT_ENCODING_FOR_ALGORITHMS(algs) (&grpc_static_mdelem_table[grpc_static_accept_encoding_metadata[(algs)]])'
338
Craig Tiller3bd96952016-03-28 12:43:02 -0700339print >>H, '#endif /* GRPC_CORE_LIB_TRANSPORT_STATIC_METADATA_H */'
Craig Tiller2e7687c2015-11-18 14:56:46 -0800340
341H.close()
342C.close()