Repository navigation
Connection - #127
Connection#127lovelydinosaur wants to merge 5 commits into
Conversation
cgl
left a comment
There was a problem hiding this comment.
The changes look good overall. I think there is one issue that needs to be addressed, that is the placeholder Stream() created when no stream is supplied. Could we represent the unopened state without a placeholder stream?
| finally: | ||
| conn.close() | ||
|
|
||
| assert backend.connect_tls_args == ("example.com", 443, "example.com") |
There was a problem hiding this comment.
def test_context_manager_uses_supplied_stream(server):
backend = hip.backends.NetworkBackend()
network_stream = backend.connect(server.host, server.port)
try:
with hip.transports.Connection(
server.url, stream=network_stream
) as connection:
assert connection._stream is network_stream
assert connection.request("GET", "/").status_code == 200
assert network_stream._is_closed
finally:
network_stream.close()Can we add a regression test like this? open() creates a new connection even when a stream was supplied.
| self._stream = backend.connect_tls(host, port) | ||
| else: | ||
| self._stream = backend.connect(host, port) | ||
| self._parser = HTTPParser(self._stream, mode="CLIENT") |
There was a problem hiding this comment.
This repeats the parser initialisation in the constructor. Maybe we can postpone the parser creation until after connection is opened.
| if scheme == "https": | ||
| self._stream = backend.connect_tls(host, port) | ||
| else: | ||
| self._stream = backend.connect(host, port) |
There was a problem hiding this comment.
These lines works ok well when no stream is supplied, but otherwise they create a second connection and overwrite self._stream, leaving the supplied stream open.
|
Thanks for the review. Yeah there's a bunch of extra state cleanup this'd need. Informally, this is motivated by wanting to have a consistent stack all the way up, so... # Working directly at the stream level
backend = hip.NetworkBackend()
with backend.connect(host="187.54.30.60", port=80) as s:
s.write(
b"GET / HTTP/1.1\r\n"
b"Host: example.com\r\n"
b"Connection: close\r\n"
b"\r\n"
)
s.read()
# Integrating an HTTP parser...
backend = hip.NetworkBackend()
with backend.connect(host="187.54.30.60", port=80) as s:
p = hip.HTTPParser(mode=hip.CLIENT)
p.send_method_line(b“GET”, b“/“)
p.send_headers([(b"Host", b"www.example.com")])
p.send_body(b"")
p.recv_status_line()
p.recv_headers()
p.recv_body()
# Integrating a request/response API...
with hip.Connection("https://www.example.com") as conn:
conn.request("GET", "/")
# Supporting multiple connections...
with hip.ConnectionPool() as pool:
pool.request("GET", "https://www.example.com")
# Adding support for redirects, cookie handling & content parsing...
with hip.Client() as cli:
cli.get("https://www.example.com") |
The connection class now supports context managed style usage.
Here's how to make a request on a single HTTP connection...
Some cleanup still to do around open/closed state.