Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python2.7 |
| 2 | |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | # Copyright 2015, Google Inc. |
Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 4 | # 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 | |
| 32 | import hashlib |
| 33 | import itertools |
| 34 | import os |
| 35 | import 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 | |
| 42 | CONFIG = [ |
| 43 | 'grpc-timeout', |
Craig Tiller | ebdef9d | 2015-11-19 17:09:49 -0800 | [diff] [blame] | 44 | 'grpc-internal-encoding-request', |
Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 45 | ':path', |
| 46 | 'grpc-encoding', |
| 47 | 'grpc-accept-encoding', |
| 48 | 'user-agent', |
| 49 | ':authority', |
| 50 | 'host', |
| 51 | 'grpc-message', |
| 52 | 'grpc-status', |
Hongyu Chen | 134cac2 | 2015-12-10 16:19:24 -0800 | [diff] [blame] | 53 | 'census-bin', |
Bogdan Drutu | 079a179 | 2016-02-08 16:51:17 -0800 | [diff] [blame] | 54 | 'census-binary-bin', |
Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 55 | '', |
| 56 | ('grpc-status', '0'), |
Craig Tiller | ebdef9d | 2015-11-19 17:09:49 -0800 | [diff] [blame] | 57 | ('grpc-status', '1'), |
| 58 | ('grpc-status', '2'), |
| 59 | ('grpc-encoding', 'identity'), |
| 60 | ('grpc-encoding', 'gzip'), |
| 61 | ('grpc-encoding', 'deflate'), |
Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 62 | ('te', 'trailers'), |
Craig Tiller | ebdef9d | 2015-11-19 17:09:49 -0800 | [diff] [blame] | 63 | ('content-type', 'application/grpc'), |
Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 64 | (':method', 'POST'), |
| 65 | (':status', '200'), |
| 66 | (':status', '404'), |
| 67 | (':scheme', 'http'), |
| 68 | (':scheme', 'https'), |
| 69 | (':scheme', 'grpc'), |
| 70 | (':authority', ''), |
| 71 | (':method', 'GET'), |
Craig Tiller | c654976 | 2016-03-09 17:10:43 -0800 | [diff] [blame] | 72 | (':method', 'PUT'), |
Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 73 | (':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 Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 130 | COMPRESSION_ALGORITHMS = [ |
| 131 | 'identity', |
| 132 | 'deflate', |
| 133 | 'gzip', |
| 134 | ] |
| 135 | |
Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 136 | # utility: mangle the name of a config |
| 137 | def 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 |
| 168 | def fake_hash(elem): |
| 169 | return hashlib.md5(elem).hexdigest()[0:8] |
| 170 | |
| 171 | # utility: print a big comment block into a set of files |
| 172 | def 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 |
| 181 | all_strs = set() |
| 182 | all_elems = set() |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 183 | static_userdata = {} |
Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 184 | for 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 Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 191 | compression_elems = [] |
| 192 | for 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 Quintas | 07503b6 | 2016-03-21 11:59:33 -0700 | [diff] [blame] | 200 | static_userdata[elem] = 1 + (mask | 1) |
Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 201 | all_strs = sorted(list(all_strs), key=mangle) |
| 202 | all_elems = sorted(list(all_elems), key=mangle) |
| 203 | |
| 204 | # output configuration |
| 205 | args = sys.argv[1:] |
| 206 | H = None |
| 207 | C = None |
Craig Tiller | 134a6b6 | 2016-04-18 08:14:20 -0700 | [diff] [blame] | 208 | D = None |
Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 209 | if 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 Tiller | 134a6b6 | 2016-04-18 08:14:20 -0700 | [diff] [blame] | 218 | if 'dictionary' in args: |
| 219 | D = sys.stdout |
| 220 | else: |
| 221 | D = open('/dev/null', 'w') |
Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 222 | else: |
| 223 | H = open(os.path.join( |
Craig Tiller | ffae43c | 2016-03-25 19:34:29 -0700 | [diff] [blame] | 224 | os.path.dirname(sys.argv[0]), '../../../src/core/lib/transport/static_metadata.h'), 'w') |
Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 225 | C = open(os.path.join( |
Craig Tiller | ffae43c | 2016-03-25 19:34:29 -0700 | [diff] [blame] | 226 | os.path.dirname(sys.argv[0]), '../../../src/core/lib/transport/static_metadata.c'), 'w') |
Craig Tiller | 134a6b6 | 2016-04-18 08:14:20 -0700 | [diff] [blame] | 227 | D = open(os.path.join( |
| 228 | os.path.dirname(sys.argv[0]), '../../../test/core/end2end/fuzzers/hpack.dictionary'), 'w') |
Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 229 | |
| 230 | # copy-paste copyright notice from this file |
| 231 | with 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 Tiller | 3bd9695 | 2016-03-28 12:43:02 -0700 | [diff] [blame] | 243 | put_banner([H,C], [line[2:].rstrip() for line in copyright]) |
Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 244 | |
Craig Tiller | 134a6b6 | 2016-04-18 08:14:20 -0700 | [diff] [blame] | 245 | |
| 246 | hex_bytes = [ord(c) for c in "abcdefABCDEF0123456789"] |
| 247 | |
| 248 | |
Craig Tiller | 69b6d4e | 2016-04-18 15:08:14 -0700 | [diff] [blame] | 249 | def esc_dict(line): |
Craig Tiller | 134a6b6 | 2016-04-18 08:14:20 -0700 | [diff] [blame] | 250 | out = "\"" |
Craig Tiller | 134a6b6 | 2016-04-18 08:14:20 -0700 | [diff] [blame] | 251 | for c in line: |
| 252 | if 32 <= c < 127: |
Craig Tiller | 134a6b6 | 2016-04-18 08:14:20 -0700 | [diff] [blame] | 253 | if c != ord('"'): |
| 254 | out += chr(c) |
| 255 | else: |
| 256 | out += "\\\"" |
Craig Tiller | 134a6b6 | 2016-04-18 08:14:20 -0700 | [diff] [blame] | 257 | else: |
Craig Tiller | 69b6d4e | 2016-04-18 15:08:14 -0700 | [diff] [blame] | 258 | out += "\\x%02X" % c |
Craig Tiller | 134a6b6 | 2016-04-18 08:14:20 -0700 | [diff] [blame] | 259 | return out + "\"" |
| 260 | |
Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 261 | put_banner([H,C], |
| 262 | """WARNING: Auto-generated code. |
| 263 | |
Craig Tiller | 3bd9695 | 2016-03-28 12:43:02 -0700 | [diff] [blame] | 264 | To make changes to this file, change |
| 265 | tools/codegen/core/gen_static_metadata.py, and then re-run it. |
Craig Tiller | 51ae0be | 2015-11-19 07:51:25 -0800 | [diff] [blame] | 266 | |
Craig Tiller | 3bd9695 | 2016-03-28 12:43:02 -0700 | [diff] [blame] | 267 | See metadata.h for an explanation of the interface here, and metadata.c for |
| 268 | an explanation of what's going on. |
Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 269 | """.splitlines()) |
| 270 | |
Craig Tiller | 3bd9695 | 2016-03-28 12:43:02 -0700 | [diff] [blame] | 271 | print >>H, '#ifndef GRPC_CORE_LIB_TRANSPORT_STATIC_METADATA_H' |
| 272 | print >>H, '#define GRPC_CORE_LIB_TRANSPORT_STATIC_METADATA_H' |
Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 273 | print >>H |
Craig Tiller | ffae43c | 2016-03-25 19:34:29 -0700 | [diff] [blame] | 274 | print >>H, '#include "src/core/lib/transport/metadata.h"' |
Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 275 | print >>H |
| 276 | |
Craig Tiller | ffae43c | 2016-03-25 19:34:29 -0700 | [diff] [blame] | 277 | print >>C, '#include "src/core/lib/transport/static_metadata.h"' |
Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 278 | print >>C |
| 279 | |
| 280 | print >>H, '#define GRPC_STATIC_MDSTR_COUNT %d' % len(all_strs) |
| 281 | print >>H, 'extern grpc_mdstr grpc_static_mdstr_table[GRPC_STATIC_MDSTR_COUNT];' |
| 282 | for 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) |
| 285 | print >>H |
| 286 | print >>C, 'grpc_mdstr grpc_static_mdstr_table[GRPC_STATIC_MDSTR_COUNT];' |
| 287 | print >>C |
| 288 | |
Craig Tiller | 134a6b6 | 2016-04-18 08:14:20 -0700 | [diff] [blame] | 289 | print >>D, '# hpack fuzzing dictionary' |
| 290 | for i, elem in enumerate(all_strs): |
Craig Tiller | 69b6d4e | 2016-04-18 15:08:14 -0700 | [diff] [blame] | 291 | print >>D, '%s' % (esc_dict([len(elem)] + [ord(c) for c in elem])) |
Craig Tiller | 942568b | 2016-04-18 22:19:00 -0700 | [diff] [blame] | 292 | for 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 Tiller | 134a6b6 | 2016-04-18 08:14:20 -0700 | [diff] [blame] | 295 | |
Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 296 | print >>H, '#define GRPC_STATIC_MDELEM_COUNT %d' % len(all_elems) |
| 297 | print >>H, 'extern grpc_mdelem grpc_static_mdelem_table[GRPC_STATIC_MDELEM_COUNT];' |
Craig Tiller | c654976 | 2016-03-09 17:10:43 -0800 | [diff] [blame] | 298 | print >>H, 'extern uintptr_t grpc_static_mdelem_user_data[GRPC_STATIC_MDELEM_COUNT];' |
Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 299 | for 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) |
| 302 | print >>H |
| 303 | print >>C, 'grpc_mdelem grpc_static_mdelem_table[GRPC_STATIC_MDELEM_COUNT];' |
Craig Tiller | c654976 | 2016-03-09 17:10:43 -0800 | [diff] [blame] | 304 | print >>C, 'uintptr_t grpc_static_mdelem_user_data[GRPC_STATIC_MDELEM_COUNT] = {' |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 305 | print >>C, ' %s' % ','.join('%d' % static_userdata.get(elem, 0) for elem in all_elems) |
| 306 | print >>C, '};' |
Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 307 | print >>C |
| 308 | |
| 309 | def str_idx(s): |
| 310 | for i, s2 in enumerate(all_strs): |
| 311 | if s == s2: |
| 312 | return i |
| 313 | |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 314 | def md_idx(m): |
| 315 | for i, m2 in enumerate(all_elems): |
| 316 | if m == m2: |
| 317 | return i |
| 318 | |
Craig Tiller | c654976 | 2016-03-09 17:10:43 -0800 | [diff] [blame] | 319 | print >>H, 'extern const uint8_t grpc_static_metadata_elem_indices[GRPC_STATIC_MDELEM_COUNT*2];' |
| 320 | print >>C, 'const uint8_t grpc_static_metadata_elem_indices[GRPC_STATIC_MDELEM_COUNT*2] = {' |
Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 321 | print >>C, ','.join('%d' % str_idx(x) for x in itertools.chain.from_iterable([a,b] for a, b in all_elems)) |
| 322 | print >>C, '};' |
| 323 | print >>C |
| 324 | |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 325 | print >>H, 'extern const char *const grpc_static_metadata_strings[GRPC_STATIC_MDSTR_COUNT];' |
Craig Tiller | 328d4b1 | 2015-11-18 15:10:41 -0800 | [diff] [blame] | 326 | print >>C, 'const char *const grpc_static_metadata_strings[GRPC_STATIC_MDSTR_COUNT] = {' |
Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 327 | print >>C, '%s' % ',\n'.join(' "%s"' % s for s in all_strs) |
| 328 | print >>C, '};' |
| 329 | print >>C |
| 330 | |
Craig Tiller | c654976 | 2016-03-09 17:10:43 -0800 | [diff] [blame] | 331 | print >>H, 'extern const uint8_t grpc_static_accept_encoding_metadata[%d];' % (1 << len(COMPRESSION_ALGORITHMS)) |
| 332 | print >>C, 'const uint8_t grpc_static_accept_encoding_metadata[%d] = {' % (1 << len(COMPRESSION_ALGORITHMS)) |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 333 | print >>C, '0,%s' % ','.join('%d' % md_idx(elem) for elem in compression_elems) |
| 334 | print >>C, '};' |
| 335 | print >>C |
| 336 | |
| 337 | print >>H, '#define GRPC_MDELEM_ACCEPT_ENCODING_FOR_ALGORITHMS(algs) (&grpc_static_mdelem_table[grpc_static_accept_encoding_metadata[(algs)]])' |
| 338 | |
Craig Tiller | 3bd9695 | 2016-03-28 12:43:02 -0700 | [diff] [blame] | 339 | print >>H, '#endif /* GRPC_CORE_LIB_TRANSPORT_STATIC_METADATA_H */' |
Craig Tiller | 2e7687c | 2015-11-18 14:56:46 -0800 | [diff] [blame] | 340 | |
| 341 | H.close() |
| 342 | C.close() |