Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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:
return headers(h)
#print(HTTP2_HEADER + frame(0x4, 0, 0, '') + request("", "/example.php?foo=bar", extra_headers={"User-Agent": "baz"}), end="")
#print(HTTP2_HEADER + frame(0x4, 0, 0, '') + request("", "/time/0", extra_headers={"User-Agent": "baz"}), end="")
print(HTTP2_HEADER + frame(0x4, 0, 0, '') + request("", "/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
You can’t perform that action at this time.