gen_header_frame.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 
3 # Copyright 2015 gRPC authors.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 """Read from stdin a set of colon separated http headers:
17  :path: /foo/bar
18  content-type: application/grpc
19  Write a set of strings containing a hpack encoded http2 frame that
20  represents said headers."""
21 
22 import argparse
23 import json
24 import sys
25 
26 
27 def append_never_indexed(payload_line, n, count, key, value):
28  payload_line.append(0x10)
29  assert (len(key) <= 126)
30  payload_line.append(len(key))
31  payload_line.extend(ord(c) for c in key)
32  assert (len(value) <= 126)
33  payload_line.append(len(value))
34  payload_line.extend(ord(c) for c in value)
35 
36 
37 def append_inc_indexed(payload_line, n, count, key, value):
38  payload_line.append(0x40)
39  assert (len(key) <= 126)
40  payload_line.append(len(key))
41  payload_line.extend(ord(c) for c in key)
42  assert (len(value) <= 126)
43  payload_line.append(len(value))
44  payload_line.extend(ord(c) for c in value)
45 
46 
47 def append_pre_indexed(payload_line, n, count, key, value):
48  payload_line.append(0x80 + 61 + count - n)
49 
50 
51 _COMPRESSORS = {
52  'never': append_never_indexed,
53  'inc': append_inc_indexed,
54  'pre': append_pre_indexed,
55 }
56 
57 argp = argparse.ArgumentParser('Generate header frames')
58 argp.add_argument('--set_end_stream',
59  default=False,
60  action='store_const',
61  const=True)
62 argp.add_argument('--no_framing',
63  default=False,
64  action='store_const',
65  const=True)
66 argp.add_argument('--compression',
67  choices=sorted(_COMPRESSORS.keys()),
68  default='never')
69 argp.add_argument('--hex', default=False, action='store_const', const=True)
70 args = argp.parse_args()
71 
72 # parse input, fill in vals
73 vals = []
74 for line in sys.stdin:
75  line = line.strip()
76  if line == '':
77  continue
78  if line[0] == '#':
79  continue
80  key_tail, value = line[1:].split(':')
81  key = (line[0] + key_tail).strip()
82  value = value.strip()
83  vals.append((key, value))
84 
85 # generate frame payload binary data
86 payload_bytes = []
87 if not args.no_framing:
88  payload_bytes.append([]) # reserve space for header
89 payload_len = 0
90 n = 0
91 for key, value in vals:
92  payload_line = []
93  _COMPRESSORS[args.compression](payload_line, n, len(vals), key, value)
94  n += 1
95  payload_len += len(payload_line)
96  payload_bytes.append(payload_line)
97 
98 # fill in header
99 if not args.no_framing:
100  flags = 0x04 # END_HEADERS
101  if args.set_end_stream:
102  flags |= 0x01 # END_STREAM
103  payload_bytes[0].extend([
104  (payload_len >> 16) & 0xff,
105  (payload_len >> 8) & 0xff,
106  (payload_len) & 0xff,
107  # header frame
108  0x01,
109  # flags
110  flags,
111  # stream id
112  0x00,
113  0x00,
114  0x00,
115  0x01
116  ])
117 
118 hex_bytes = [ord(c) for c in "abcdefABCDEF0123456789"]
119 
120 
121 def esc_c(line):
122  out = "\""
123  last_was_hex = False
124  for c in line:
125  if 32 <= c < 127:
126  if c in hex_bytes and last_was_hex:
127  out += "\"\""
128  if c != ord('"'):
129  out += chr(c)
130  else:
131  out += "\\\""
132  last_was_hex = False
133  else:
134  out += "\\x%02x" % c
135  last_was_hex = True
136  return out + "\""
137 
138 
139 # dump bytes
140 if args.hex:
141  all_bytes = []
142  for line in payload_bytes:
143  all_bytes.extend(line)
144  print(('{%s}' % ', '.join('0x%02x' % c for c in all_bytes)))
145 else:
146  for line in payload_bytes:
147  print((esc_c(line)))
absl::compare_internal::ord
ord
Definition: abseil-cpp/absl/types/compare.h:79
gen_header_frame.append_inc_indexed
def append_inc_indexed(payload_line, n, count, key, value)
Definition: gen_header_frame.py:37
gen_header_frame.esc_c
def esc_c(line)
Definition: gen_header_frame.py:121
gen_header_frame.append_never_indexed
def append_never_indexed(payload_line, n, count, key, value)
Definition: gen_header_frame.py:27
gen_header_frame.append_pre_indexed
def append_pre_indexed(payload_line, n, count, key, value)
Definition: gen_header_frame.py:47
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46
split
static void split(const char *s, char ***ss, size_t *ns)
Definition: debug/trace.cc:111


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:59:23