blob: ee476267f23ac230d21e51ae183b1dc9ab993791 [file] [log] [blame]
Craig Tiller2a2bee92015-12-09 08:32:58 -08001#!/usr/bin/env python2.7
Craig Tiller2dd08192015-12-09 09:00:35 -08002
3# Copyright 2015, Google Inc.
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
Craig Tiller2a2bee92015-12-09 08:32:58 -080032"""Read from stdin a set of colon separated http headers:
33 :path: /foo/bar
34 content-type: application/grpc
35 Write a set of strings containing a hpack encoded http2 frame that
36 represents said headers."""
37
38import json
39import sys
40
Mark D. Roth8a1d8052016-05-03 10:44:56 -070041set_end_stream = len(sys.argv) > 1 and sys.argv[1] == '--set_end_stream'
42
Craig Tiller2a2bee92015-12-09 08:32:58 -080043# parse input, fill in vals
44vals = []
45for line in sys.stdin:
46 line = line.strip()
47 if line == '': continue
48 if line[0] == '#': continue
49 key_tail, value = line[1:].split(':')
50 key = (line[0] + key_tail).strip()
51 value = value.strip()
52 vals.append((key, value))
53
54# generate frame payload binary data
55payload_bytes = [[]] # reserve space for header
56payload_len = 0
57for key, value in vals:
58 payload_line = []
59 payload_line.append(0x10)
60 assert(len(key) <= 126)
61 payload_line.append(len(key))
62 payload_line.extend(ord(c) for c in key)
63 assert(len(value) <= 126)
64 payload_line.append(len(value))
65 payload_line.extend(ord(c) for c in value)
66 payload_len += len(payload_line)
67 payload_bytes.append(payload_line)
68
69# fill in header
Mark D. Roth8a1d8052016-05-03 10:44:56 -070070flags = 0x04 # END_HEADERS
71if set_end_stream:
72 flags |= 0x01 # END_STREAM
Craig Tiller2a2bee92015-12-09 08:32:58 -080073payload_bytes[0].extend([
74 (payload_len >> 16) & 0xff,
75 (payload_len >> 8) & 0xff,
76 (payload_len) & 0xff,
77 # header frame
78 0x01,
79 # flags
Mark D. Roth8a1d8052016-05-03 10:44:56 -070080 flags,
Craig Tiller2a2bee92015-12-09 08:32:58 -080081 # stream id
82 0x00,
83 0x00,
84 0x00,
85 0x01
86])
87
88hex_bytes = [ord(c) for c in "abcdefABCDEF0123456789"]
89
90def esc_c(line):
91 out = "\""
92 last_was_hex = False
93 for c in line:
94 if 32 <= c < 127:
95 if c in hex_bytes and last_was_hex:
96 out += "\"\""
97 if c != ord('"'):
98 out += chr(c)
99 else:
100 out += "\\\""
101 last_was_hex = False
102 else:
103 out += "\\x%02x" % c
104 last_was_hex = True
105 return out + "\""
106
107# dump bytes
108for line in payload_bytes:
109 print esc_c(line)
110