Skip to content

Instantly share code, notes, and snippets.

@stephenlb
Last active June 4, 2019 18:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stephenlb/462bfefb99fe06133e029e6bd6720e6c to your computer and use it in GitHub Desktop.
Save stephenlb/462bfefb99fe06133e029e6bd6720e6c to your computer and use it in GitHub Desktop.
Make an HTTP/2 Request, works as a PIPE command into openssl s_client.
#!/usr/bin/env python
from __future__ import print_function
import struct
HTTP2_HEADER="PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"
# Does the thing for a frame
def frame(ty, flags, streamid, payload):
return struct.pack(">L", len(payload))[1:4] + struct.pack(">BBL", ty, flags, streamid) + payload
last_stream_id = 0
# Returns a single naively hpacked value
def encode_header(key, value):
key = key.lower()
return struct.pack("<BB", 0, len(key)) + key + struct.pack("<B", len(value)) + value
# Does the thing for the HEADERS frame type
def headers(headers):
global last_stream_id
last_stream_id += 1
# flags= (END_STREAM (0x1) | END_HEADERS (0x4))
return frame(0x1, 0x5, last_stream_id, "".join(encode_header(x,y) for (x,y) in headers.items()))
def request(host, path, method="GET", scheme="http", extra_headers=None):
h = {":authority": host, ":path": path, ":method": method, ":scheme": scheme}
if extra_headers is not None:
h.update(extra_headers)
return headers(h)
#print(HTTP2_HEADER + frame(0x4, 0, 0, '') + request("pubnubcoin.com", "/example.php?foo=bar", extra_headers={"User-Agent": "baz"}), end="")
#print(HTTP2_HEADER + frame(0x4, 0, 0, '') + request("pubnubcoin.com", "/time/0", extra_headers={"User-Agent": "baz"}), end="")
print(HTTP2_HEADER + frame(0x4, 0, 0, '') + request("http2.pubnub.com", "/time/0", method="GET" extra_headers={"User-Agent": "baz"}), end="")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment