@@ -13,25 +13,54 @@ may want to keep your connections alive only as long as you know you'll need
13
13
them. In HTTP/2 this is generally not something you should do unless you're
14
14
very confident you won't need the connection again anytime soon. However, if
15
15
you decide you want to avoid keeping the connection open, you can use the
16
- :class: `HTTP20Connection <hyper.HTTP20Connection> ` as a context manager::
16
+ :class: `HTTP20Connection <hyper.HTTP20Connection> ` and
17
+ :class: `HTTP11Connection <hyper.HTTP11Connection> ` as context managers::
17
18
18
19
with HTTP20Connection('twitter.com:443') as conn:
19
20
conn.request('GET', '/')
20
21
data = conn.get_response().read()
21
22
22
23
analyse(data)
23
24
24
- You may not use any :class: `HTTP20Response <hyper.HTTP20Response> ` objects
25
- obtained from a connection after that connection is closed. Interacting with
26
- these objects when a connection has been closed is considered undefined
27
- behaviour.
25
+ You may not use any :class: `HTTP20Response <hyper.HTTP20Response> ` or
26
+ :class: `HTTP11Response <hyper.HTTP11Response> ` objects obtained from a
27
+ connection after that connection is closed. Interacting with these objects when
28
+ a connection has been closed is considered undefined behaviour.
29
+
30
+ Chunked Responses
31
+ -----------------
32
+
33
+ Plenty of APIs return chunked data, and it's often useful to iterate directly
34
+ over the chunked data. ``hyper `` lets you iterate over each data frame of a
35
+ HTTP/2 response, and each chunk of a HTTP/1.1 response delivered with
36
+ ``Transfer-Encoding: chunked ``::
37
+
38
+ for chunk in response.read_chunked():
39
+ do_something_with_chunk(chunk)
40
+
41
+ There are some important caveats with this iteration: mostly, it's not
42
+ guaranteed that each chunk will be non-empty. In HTTP/2, it's entirely legal to
43
+ send zero-length data frames, and this API will pass those through unchanged.
44
+ Additionally, by default this method will decompress a response that has a
45
+ compressed ``Content-Encoding ``: if you do that, each element of the iterator
46
+ will no longer be a single chunk, but will instead be whatever the decompressor
47
+ returns for that chunk.
48
+
49
+ If that's problematic, you can set the ``decode_content `` parameter to
50
+ ``False `` and, if necessary, handle the decompression yourself::
51
+
52
+ for compressed_chunk in response.read_chunked(decode_content=False):
53
+ decompress(compressed_chunk)
54
+
55
+ Very easy!
28
56
29
57
Multithreading
30
58
--------------
31
59
32
- Currently, ``hyper ``'s :class: `HTTP20Connection <hyper.HTTP20Connection> ` class
33
- is **not ** thread-safe. Thread-safety is planned for ``hyper ``'s core objects,
34
- but in this early alpha it is not a high priority.
60
+ Currently, ``hyper ``'s :class: `HTTP20Connection <hyper.HTTP20Connection> ` and
61
+ :class: `HTTP11Connection <hyper.HTTP11Connection> ` classes are **not **
62
+ thread-safe. Thread-safety is planned for ``hyper ``'s core objects, but in this
63
+ early alpha it is not a high priority.
35
64
36
65
To use ``hyper `` in a multithreaded context the recommended thing to do is to
37
66
place each connection in its own thread. Each thread should then have a request
0 commit comments