blob: 859adcbeb14fba45f2b4ce239b5c79a8c357ca50 [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',
David Garcia Quintas8eec9ec2016-08-01 17:05:04 -070053 'grpc-tracing-bin',
54 'grpc-census-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', ''),
David Garcia Quintas01c4d992016-07-07 20:11:27 -0700111 ('load-reporting-initial', ''),
112 ('load-reporting-trailing', ''),
Craig Tiller2e7687c2015-11-18 14:56:46 -0800113 ('link', ''),
114 ('location', ''),
115 ('max-forwards', ''),
116 ('proxy-authenticate', ''),
117 ('proxy-authorization', ''),
118 ('range', ''),
119 ('referer', ''),
120 ('refresh', ''),
121 ('retry-after', ''),
122 ('server', ''),
123 ('set-cookie', ''),
124 ('strict-transport-security', ''),
125 ('transfer-encoding', ''),
126 ('user-agent', ''),
127 ('vary', ''),
128 ('via', ''),
129 ('www-authenticate', ''),
130]
131
Craig Tillerb2b42612015-11-20 12:02:17 -0800132COMPRESSION_ALGORITHMS = [
133 'identity',
134 'deflate',
135 'gzip',
136]
137
Craig Tiller2e7687c2015-11-18 14:56:46 -0800138# utility: mangle the name of a config
139def mangle(elem):
140 xl = {
141 '-': '_',
142 ':': '',
143 '/': 'slash',
144 '.': 'dot',
145 ',': 'comma',
146 ' ': '_',
147 }
148 def m0(x):
149 if not x: return 'empty'
150 r = ''
151 for c in x:
152 put = xl.get(c, c.lower())
153 if not put: continue
154 last_is_underscore = r[-1] == '_' if r else True
155 if last_is_underscore and put == '_': continue
156 elif len(put) > 1:
157 if not last_is_underscore: r += '_'
158 r += put
159 r += '_'
160 else:
161 r += put
162 if r[-1] == '_': r = r[:-1]
163 return r
164 if isinstance(elem, tuple):
165 return 'grpc_mdelem_%s_%s' % (m0(elem[0]), m0(elem[1]))
166 else:
167 return 'grpc_mdstr_%s' % (m0(elem))
168
169# utility: generate some hash value for a string
170def fake_hash(elem):
171 return hashlib.md5(elem).hexdigest()[0:8]
172
173# utility: print a big comment block into a set of files
174def put_banner(files, banner):
175 for f in files:
176 print >>f, '/*'
177 for line in banner:
178 print >>f, ' * %s' % line
179 print >>f, ' */'
180 print >>f
181
182# build a list of all the strings we need
183all_strs = set()
184all_elems = set()
Craig Tillerb2b42612015-11-20 12:02:17 -0800185static_userdata = {}
Craig Tiller2e7687c2015-11-18 14:56:46 -0800186for elem in CONFIG:
187 if isinstance(elem, tuple):
188 all_strs.add(elem[0])
189 all_strs.add(elem[1])
190 all_elems.add(elem)
191 else:
192 all_strs.add(elem)
Craig Tillerb2b42612015-11-20 12:02:17 -0800193compression_elems = []
194for mask in range(1, 1<<len(COMPRESSION_ALGORITHMS)):
195 val = ','.join(COMPRESSION_ALGORITHMS[alg]
196 for alg in range(0, len(COMPRESSION_ALGORITHMS))
197 if (1 << alg) & mask)
198 elem = ('grpc-accept-encoding', val)
199 all_strs.add(val)
200 all_elems.add(elem)
201 compression_elems.append(elem)
David Garcia Quintas07503b62016-03-21 11:59:33 -0700202 static_userdata[elem] = 1 + (mask | 1)
Craig Tiller2e7687c2015-11-18 14:56:46 -0800203all_strs = sorted(list(all_strs), key=mangle)
204all_elems = sorted(list(all_elems), key=mangle)
205
206# output configuration
207args = sys.argv[1:]
208H = None
209C = None
Craig Tiller134a6b62016-04-18 08:14:20 -0700210D = None
Craig Tiller2e7687c2015-11-18 14:56:46 -0800211if args:
212 if 'header' in args:
213 H = sys.stdout
214 else:
215 H = open('/dev/null', 'w')
216 if 'source' in args:
217 C = sys.stdout
218 else:
219 C = open('/dev/null', 'w')
Craig Tiller134a6b62016-04-18 08:14:20 -0700220 if 'dictionary' in args:
221 D = sys.stdout
222 else:
223 D = open('/dev/null', 'w')
Craig Tiller2e7687c2015-11-18 14:56:46 -0800224else:
225 H = open(os.path.join(
Craig Tillerffae43c2016-03-25 19:34:29 -0700226 os.path.dirname(sys.argv[0]), '../../../src/core/lib/transport/static_metadata.h'), 'w')
Craig Tiller2e7687c2015-11-18 14:56:46 -0800227 C = open(os.path.join(
Craig Tillerffae43c2016-03-25 19:34:29 -0700228 os.path.dirname(sys.argv[0]), '../../../src/core/lib/transport/static_metadata.c'), 'w')
Craig Tiller134a6b62016-04-18 08:14:20 -0700229 D = open(os.path.join(
230 os.path.dirname(sys.argv[0]), '../../../test/core/end2end/fuzzers/hpack.dictionary'), 'w')
Craig Tiller2e7687c2015-11-18 14:56:46 -0800231
232# copy-paste copyright notice from this file
233with open(sys.argv[0]) as my_source:
234 copyright = []
235 for line in my_source:
236 if line[0] != '#': break
237 for line in my_source:
238 if line[0] == '#':
239 copyright.append(line)
240 break
241 for line in my_source:
242 if line[0] != '#':
243 break
244 copyright.append(line)
Craig Tiller3bd96952016-03-28 12:43:02 -0700245 put_banner([H,C], [line[2:].rstrip() for line in copyright])
Craig Tiller2e7687c2015-11-18 14:56:46 -0800246
Craig Tiller134a6b62016-04-18 08:14:20 -0700247
248hex_bytes = [ord(c) for c in "abcdefABCDEF0123456789"]
249
250
Craig Tiller69b6d4e2016-04-18 15:08:14 -0700251def esc_dict(line):
Craig Tiller134a6b62016-04-18 08:14:20 -0700252 out = "\""
Craig Tiller134a6b62016-04-18 08:14:20 -0700253 for c in line:
254 if 32 <= c < 127:
Craig Tiller134a6b62016-04-18 08:14:20 -0700255 if c != ord('"'):
256 out += chr(c)
257 else:
258 out += "\\\""
Craig Tiller134a6b62016-04-18 08:14:20 -0700259 else:
Craig Tiller69b6d4e2016-04-18 15:08:14 -0700260 out += "\\x%02X" % c
Craig Tiller134a6b62016-04-18 08:14:20 -0700261 return out + "\""
262
Craig Tiller2e7687c2015-11-18 14:56:46 -0800263put_banner([H,C],
264"""WARNING: Auto-generated code.
265
Craig Tiller3bd96952016-03-28 12:43:02 -0700266To make changes to this file, change
267tools/codegen/core/gen_static_metadata.py, and then re-run it.
Craig Tiller51ae0be2015-11-19 07:51:25 -0800268
Craig Tiller3bd96952016-03-28 12:43:02 -0700269See metadata.h for an explanation of the interface here, and metadata.c for
270an explanation of what's going on.
Craig Tiller2e7687c2015-11-18 14:56:46 -0800271""".splitlines())
272
Craig Tiller3bd96952016-03-28 12:43:02 -0700273print >>H, '#ifndef GRPC_CORE_LIB_TRANSPORT_STATIC_METADATA_H'
274print >>H, '#define GRPC_CORE_LIB_TRANSPORT_STATIC_METADATA_H'
Craig Tiller2e7687c2015-11-18 14:56:46 -0800275print >>H
Craig Tillerffae43c2016-03-25 19:34:29 -0700276print >>H, '#include "src/core/lib/transport/metadata.h"'
Craig Tiller2e7687c2015-11-18 14:56:46 -0800277print >>H
278
Craig Tillerffae43c2016-03-25 19:34:29 -0700279print >>C, '#include "src/core/lib/transport/static_metadata.h"'
Craig Tiller2e7687c2015-11-18 14:56:46 -0800280print >>C
281
282print >>H, '#define GRPC_STATIC_MDSTR_COUNT %d' % len(all_strs)
283print >>H, 'extern grpc_mdstr grpc_static_mdstr_table[GRPC_STATIC_MDSTR_COUNT];'
284for i, elem in enumerate(all_strs):
285 print >>H, '/* "%s" */' % elem
286 print >>H, '#define %s (&grpc_static_mdstr_table[%d])' % (mangle(elem).upper(), i)
287print >>H
288print >>C, 'grpc_mdstr grpc_static_mdstr_table[GRPC_STATIC_MDSTR_COUNT];'
289print >>C
290
Craig Tiller134a6b62016-04-18 08:14:20 -0700291print >>D, '# hpack fuzzing dictionary'
292for i, elem in enumerate(all_strs):
Craig Tiller69b6d4e2016-04-18 15:08:14 -0700293 print >>D, '%s' % (esc_dict([len(elem)] + [ord(c) for c in elem]))
Craig Tiller942568b2016-04-18 22:19:00 -0700294for i, elem in enumerate(all_elems):
295 print >>D, '%s' % (esc_dict([0, len(elem[0])] + [ord(c) for c in elem[0]] +
296 [len(elem[1])] + [ord(c) for c in elem[1]]))
Craig Tiller134a6b62016-04-18 08:14:20 -0700297
Craig Tiller2e7687c2015-11-18 14:56:46 -0800298print >>H, '#define GRPC_STATIC_MDELEM_COUNT %d' % len(all_elems)
299print >>H, 'extern grpc_mdelem grpc_static_mdelem_table[GRPC_STATIC_MDELEM_COUNT];'
Craig Tillerc6549762016-03-09 17:10:43 -0800300print >>H, 'extern uintptr_t grpc_static_mdelem_user_data[GRPC_STATIC_MDELEM_COUNT];'
Craig Tiller2e7687c2015-11-18 14:56:46 -0800301for i, elem in enumerate(all_elems):
302 print >>H, '/* "%s": "%s" */' % elem
303 print >>H, '#define %s (&grpc_static_mdelem_table[%d])' % (mangle(elem).upper(), i)
304print >>H
305print >>C, 'grpc_mdelem grpc_static_mdelem_table[GRPC_STATIC_MDELEM_COUNT];'
Craig Tillerc6549762016-03-09 17:10:43 -0800306print >>C, 'uintptr_t grpc_static_mdelem_user_data[GRPC_STATIC_MDELEM_COUNT] = {'
Craig Tillerb2b42612015-11-20 12:02:17 -0800307print >>C, ' %s' % ','.join('%d' % static_userdata.get(elem, 0) for elem in all_elems)
308print >>C, '};'
Craig Tiller2e7687c2015-11-18 14:56:46 -0800309print >>C
310
311def str_idx(s):
312 for i, s2 in enumerate(all_strs):
313 if s == s2:
314 return i
315
Craig Tillerb2b42612015-11-20 12:02:17 -0800316def md_idx(m):
317 for i, m2 in enumerate(all_elems):
318 if m == m2:
319 return i
320
Craig Tillerc6549762016-03-09 17:10:43 -0800321print >>H, 'extern const uint8_t grpc_static_metadata_elem_indices[GRPC_STATIC_MDELEM_COUNT*2];'
322print >>C, 'const uint8_t grpc_static_metadata_elem_indices[GRPC_STATIC_MDELEM_COUNT*2] = {'
Craig Tiller2e7687c2015-11-18 14:56:46 -0800323print >>C, ','.join('%d' % str_idx(x) for x in itertools.chain.from_iterable([a,b] for a, b in all_elems))
324print >>C, '};'
325print >>C
326
Craig Tillerb2b42612015-11-20 12:02:17 -0800327print >>H, 'extern const char *const grpc_static_metadata_strings[GRPC_STATIC_MDSTR_COUNT];'
Craig Tiller328d4b12015-11-18 15:10:41 -0800328print >>C, 'const char *const grpc_static_metadata_strings[GRPC_STATIC_MDSTR_COUNT] = {'
Craig Tiller2e7687c2015-11-18 14:56:46 -0800329print >>C, '%s' % ',\n'.join(' "%s"' % s for s in all_strs)
330print >>C, '};'
331print >>C
332
Craig Tillerc6549762016-03-09 17:10:43 -0800333print >>H, 'extern const uint8_t grpc_static_accept_encoding_metadata[%d];' % (1 << len(COMPRESSION_ALGORITHMS))
334print >>C, 'const uint8_t grpc_static_accept_encoding_metadata[%d] = {' % (1 << len(COMPRESSION_ALGORITHMS))
Craig Tillerb2b42612015-11-20 12:02:17 -0800335print >>C, '0,%s' % ','.join('%d' % md_idx(elem) for elem in compression_elems)
336print >>C, '};'
337print >>C
338
339print >>H, '#define GRPC_MDELEM_ACCEPT_ENCODING_FOR_ALGORITHMS(algs) (&grpc_static_mdelem_table[grpc_static_accept_encoding_metadata[(algs)]])'
340
Craig Tiller3bd96952016-03-28 12:43:02 -0700341print >>H, '#endif /* GRPC_CORE_LIB_TRANSPORT_STATIC_METADATA_H */'
Craig Tiller2e7687c2015-11-18 14:56:46 -0800342
343H.close()
344C.close()