blob: 96e6c67fa668a5863d2ada60ae8ae3d2ae883fb7 [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
41# parse input, fill in vals
42vals = []
43for line in sys.stdin:
44 line = line.strip()
45 if line == '': continue
46 if line[0] == '#': continue
47 key_tail, value = line[1:].split(':')
48 key = (line[0] + key_tail).strip()
49 value = value.strip()
50 vals.append((key, value))
51
52# generate frame payload binary data
53payload_bytes = [[]] # reserve space for header
54payload_len = 0
55for key, value in vals:
56 payload_line = []
57 payload_line.append(0x10)
58 assert(len(key) <= 126)
59 payload_line.append(len(key))
60 payload_line.extend(ord(c) for c in key)
61 assert(len(value) <= 126)
62 payload_line.append(len(value))
63 payload_line.extend(ord(c) for c in value)
64 payload_len += len(payload_line)
65 payload_bytes.append(payload_line)
66
67# fill in header
68payload_bytes[0].extend([
69 (payload_len >> 16) & 0xff,
70 (payload_len >> 8) & 0xff,
71 (payload_len) & 0xff,
72 # header frame
73 0x01,
74 # flags
75 0x04,
76 # stream id
77 0x00,
78 0x00,
79 0x00,
80 0x01
81])
82
83hex_bytes = [ord(c) for c in "abcdefABCDEF0123456789"]
84
85def esc_c(line):
86 out = "\""
87 last_was_hex = False
88 for c in line:
89 if 32 <= c < 127:
90 if c in hex_bytes and last_was_hex:
91 out += "\"\""
92 if c != ord('"'):
93 out += chr(c)
94 else:
95 out += "\\\""
96 last_was_hex = False
97 else:
98 out += "\\x%02x" % c
99 last_was_hex = True
100 return out + "\""
101
102# dump bytes
103for line in payload_bytes:
104 print esc_c(line)
105