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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
|
import argparse
import asyncio
import logging
import os
import pickle
import ssl
import time
from collections import deque
from typing import AsyncIterator, Deque, Dict, Optional, Tuple, cast
from urllib.parse import urlparse
import httpx
from aioquic.asyncio.client import connect
from aioquic.asyncio.protocol import QuicConnectionProtocol
from aioquic.h3.connection import H3_ALPN, H3Connection
from aioquic.h3.events import DataReceived, H3Event, Headers, HeadersReceived
from aioquic.quic.configuration import QuicConfiguration
from aioquic.quic.events import QuicEvent
from aioquic.quic.logger import QuicFileLogger
logger = logging.getLogger("client")
class H3ResponseStream(httpx.AsyncByteStream):
def __init__(self, aiterator: AsyncIterator[bytes]):
self._aiterator = aiterator
async def __aiter__(self) -> AsyncIterator[bytes]:
async for part in self._aiterator:
yield part
class H3Transport(QuicConnectionProtocol, httpx.AsyncBaseTransport):
def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self._http = H3Connection(self._quic)
self._read_queue: Dict[int, Deque[H3Event]] = {}
self._read_ready: Dict[int, asyncio.Event] = {}
async def handle_async_request(self, request: httpx.Request) -> httpx.Response:
assert isinstance(request.stream, httpx.AsyncByteStream)
stream_id = self._quic.get_next_available_stream_id()
self._read_queue[stream_id] = deque()
self._read_ready[stream_id] = asyncio.Event()
# prepare request
self._http.send_headers(
stream_id=stream_id,
headers=[
(b":method", request.method.encode()),
(b":scheme", request.url.raw_scheme),
(b":authority", request.url.netloc),
(b":path", request.url.raw_path),
]
+ [
(k.lower(), v)
for (k, v) in request.headers.raw
if k.lower() not in (b"connection", b"host")
],
)
async for data in request.stream:
self._http.send_data(stream_id=stream_id, data=data, end_stream=False)
self._http.send_data(stream_id=stream_id, data=b"", end_stream=True)
# transmit request
self.transmit()
# process response
status_code, headers, stream_ended = await self._receive_response(stream_id)
return httpx.Response(
status_code=status_code,
headers=headers,
stream=H3ResponseStream(
self._receive_response_data(stream_id, stream_ended)
),
extensions={
"http_version": b"HTTP/3",
},
)
def http_event_received(self, event: H3Event):
if isinstance(event, (HeadersReceived, DataReceived)):
stream_id = event.stream_id
if stream_id in self._read_queue:
self._read_queue[event.stream_id].append(event)
self._read_ready[event.stream_id].set()
def quic_event_received(self, event: QuicEvent):
# pass event to the HTTP layer
if self._http is not None:
for http_event in self._http.handle_event(event):
self.http_event_received(http_event)
async def _receive_response(self, stream_id: int) -> Tuple[int, Headers, bool]:
"""
Read the response status and headers.
"""
stream_ended = False
while True:
event = await self._wait_for_http_event(stream_id)
if isinstance(event, HeadersReceived):
stream_ended = event.stream_ended
break
headers = []
status_code = 0
for header, value in event.headers:
if header == b":status":
status_code = int(value.decode())
else:
headers.append((header, value))
return status_code, headers, stream_ended
async def _receive_response_data(
self, stream_id: int, stream_ended: bool
) -> AsyncIterator[bytes]:
"""
Read the response data.
"""
while not stream_ended:
event = await self._wait_for_http_event(stream_id)
if isinstance(event, DataReceived):
stream_ended = event.stream_ended
yield event.data
elif isinstance(event, HeadersReceived):
stream_ended = event.stream_ended
async def _wait_for_http_event(self, stream_id: int) -> H3Event:
"""
Returns the next HTTP/3 event for the given stream.
"""
if not self._read_queue[stream_id]:
await self._read_ready[stream_id].wait()
event = self._read_queue[stream_id].popleft()
if not self._read_queue[stream_id]:
self._read_ready[stream_id].clear()
return event
def save_session_ticket(ticket):
"""
Callback which is invoked by the TLS engine when a new session ticket
is received.
"""
logger.info("New session ticket received")
if args.session_ticket:
with open(args.session_ticket, "wb") as fp:
pickle.dump(ticket, fp)
async def main(
configuration: QuicConfiguration,
url: str,
data: Optional[str],
include: bool,
output_dir: Optional[str],
) -> None:
# parse URL
parsed = urlparse(url)
assert parsed.scheme == "https", "Only https:// URLs are supported."
host = parsed.hostname
if parsed.port is not None:
port = parsed.port
else:
port = 443
async with connect(
host,
port,
configuration=configuration,
create_protocol=H3Transport,
session_ticket_handler=save_session_ticket,
) as transport:
async with httpx.AsyncClient(
transport=cast(httpx.AsyncBaseTransport, transport)
) as client:
# perform request
start = time.time()
if data is not None:
response = await client.post(
url,
content=data.encode(),
headers={"content-type": "application/x-www-form-urlencoded"},
)
else:
response = await client.get(url)
elapsed = time.time() - start
# print speed
octets = len(response.content)
logger.info(
"Received %d bytes in %.1f s (%.3f Mbps)"
% (octets, elapsed, octets * 8 / elapsed / 1000000)
)
# output response
if output_dir is not None:
output_path = os.path.join(
output_dir, os.path.basename(urlparse(url).path) or "index.html"
)
with open(output_path, "wb") as output_file:
if include:
headers = ""
for header, value in response.headers.items():
headers += header + ": " + value + "\r\n"
if headers:
output_file.write(headers.encode() + b"\r\n")
output_file.write(response.content)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="HTTP/3 client")
parser.add_argument("url", type=str, help="the URL to query (must be HTTPS)")
parser.add_argument(
"--ca-certs", type=str, help="load CA certificates from the specified file"
)
parser.add_argument(
"-d", "--data", type=str, help="send the specified data in a POST request"
)
parser.add_argument(
"-i",
"--include",
action="store_true",
help="include the HTTP response headers in the output",
)
parser.add_argument(
"-k",
"--insecure",
action="store_true",
help="do not validate server certificate",
)
parser.add_argument(
"--output-dir",
type=str,
help="write downloaded files to this directory",
)
parser.add_argument(
"-q",
"--quic-log",
type=str,
help="log QUIC events to QLOG files in the specified directory",
)
parser.add_argument(
"-l",
"--secrets-log",
type=str,
help="log secrets to a file, for use with Wireshark",
)
parser.add_argument(
"-s",
"--session-ticket",
type=str,
help="read and write session ticket from the specified file",
)
parser.add_argument(
"-v", "--verbose", action="store_true", help="increase logging verbosity"
)
args = parser.parse_args()
logging.basicConfig(
format="%(asctime)s %(levelname)s %(name)s %(message)s",
level=logging.DEBUG if args.verbose else logging.INFO,
)
if args.output_dir is not None and not os.path.isdir(args.output_dir):
raise Exception("%s is not a directory" % args.output_dir)
# prepare configuration
configuration = QuicConfiguration(is_client=True, alpn_protocols=H3_ALPN)
if args.ca_certs:
configuration.load_verify_locations(args.ca_certs)
if args.insecure:
configuration.verify_mode = ssl.CERT_NONE
if args.quic_log:
configuration.quic_logger = QuicFileLogger(args.quic_log)
if args.secrets_log:
configuration.secrets_log_file = open(args.secrets_log, "a")
if args.session_ticket:
try:
with open(args.session_ticket, "rb") as fp:
configuration.session_ticket = pickle.load(fp)
except FileNotFoundError:
pass
asyncio.run(
main(
configuration=configuration,
url=args.url,
data=args.data,
include=args.include,
output_dir=args.output_dir,
)
)
|