This repository was archived by the owner on Apr 3, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhttp_matrix.py
More file actions
151 lines (134 loc) · 3.83 KB
/
http_matrix.py
File metadata and controls
151 lines (134 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import os
import subprocess
import tempfile
PROTOCOL_CASES = [
{
"id": "http1_cleartext",
"config_key": "http1_cleartext",
"http_mode": "http1.1",
"use_tls": False,
"expected_http_version": "1.1",
},
{
"id": "http2_cleartext_prior_knowledge",
"config_key": "http2_cleartext",
"http_mode": "http2-prior-knowledge",
"use_tls": False,
"expected_http_version": "2",
},
{
"id": "http2_cleartext_upgrade_attempt",
"config_key": "http2_cleartext",
"http_mode": "http2",
"use_tls": False,
"expected_http_version": "1.1",
},
{
"id": "http2_cleartext_upgrade_fallback_http1",
"config_key": "http1_cleartext",
"http_mode": "http2",
"use_tls": False,
"expected_http_version": "1.1",
},
{
"id": "http1_tls",
"config_key": "http1_tls",
"http_mode": "http1.1",
"use_tls": True,
"expected_http_version": "1.1",
},
{
"id": "http2_tls_alpn",
"config_key": "http2_tls",
"http_mode": "http2",
"use_tls": True,
"expected_http_version": "2",
},
{
"id": "http2_tls_fallback_http1",
"config_key": "http1_tls",
"http_mode": "http2",
"use_tls": True,
"expected_http_version": "1.1",
},
]
def curl_supports_http2():
result = subprocess.run(
["curl", "--version"],
capture_output=True,
text=True,
check=True,
)
first_line = result.stdout.splitlines()[0] if result.stdout else ""
return "HTTP2" in result.stdout or "HTTP2" in first_line
def run_curl_request(
url,
payload=None,
*,
method="POST",
headers=None,
http_mode,
insecure_tls=False,
ca_cert_path=None,
include_headers=False,
extra_args=None,
):
command = [
"curl",
"--silent",
"--show-error",
"--output",
"-",
"--write-out",
"\n__META__%{http_code} %{http_version}",
"--max-time",
"10",
"-X",
method,
]
header_file = None
if include_headers:
header_file = tempfile.NamedTemporaryFile(mode="w+b", delete=False)
header_file.close()
command.extend(["--dump-header", header_file.name])
for header in headers or []:
command.extend(["-H", header])
stdin_payload = None
if payload is not None:
command.extend(["--data-binary", "@-"])
stdin_payload = payload if isinstance(payload, bytes) else payload.encode()
if http_mode == "http1.1":
command.append("--http1.1")
elif http_mode == "http2":
command.append("--http2")
elif http_mode == "http2-prior-knowledge":
command.append("--http2-prior-knowledge")
else:
raise ValueError(f"Unsupported HTTP mode {http_mode}")
if ca_cert_path:
command.extend(["--cacert", ca_cert_path])
elif insecure_tls:
command.append("--insecure")
if extra_args:
command.extend(extra_args)
command.append(url)
try:
result = subprocess.run(command, input=stdin_payload, capture_output=True, check=True)
output = result.stdout.decode()
body, _, meta = output.rpartition("\n__META__")
status_code, http_version = meta.strip().split(" ", 1)
response = {
"body": body,
"status_code": int(status_code),
"http_version": http_version,
}
if include_headers and header_file:
with open(header_file.name, "r", encoding="utf-8", errors="replace") as file:
response["headers_raw"] = file.read()
return response
finally:
if header_file:
try:
os.unlink(header_file.name)
except FileNotFoundError:
pass