Skip to content
This repository was archived by the owner on Jan 13, 2021. It is now read-only.

Commit cd4ddc2

Browse files
committed
Document HTTP/1.1 API.
1 parent 88da420 commit cd4ddc2

File tree

4 files changed

+86
-13
lines changed

4 files changed

+86
-13
lines changed

docs/source/advanced.rst

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,54 @@ may want to keep your connections alive only as long as you know you'll need
1313
them. In HTTP/2 this is generally not something you should do unless you're
1414
very confident you won't need the connection again anytime soon. However, if
1515
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::
1718

1819
with HTTP20Connection('twitter.com:443') as conn:
1920
conn.request('GET', '/')
2021
data = conn.get_response().read()
2122

2223
analyse(data)
2324

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!
2856

2957
Multithreading
3058
--------------
3159

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.
3564

3665
To use ``hyper`` in a multithreaded context the recommended thing to do is to
3766
place each connection in its own thread. Each thread should then have a request

docs/source/api.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ Primary HTTP/2 Interface
1919
.. autoclass:: hyper.HTTP20Push
2020
:inherited-members:
2121

22+
.. autoclass:: hyper.HTTP11Connection
23+
:inherited-members:
24+
25+
.. autoclass:: hyper.HTTP11Response
26+
:inherited-members:
27+
2228
Headers
2329
-------
2430

docs/source/index.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ Simple. ``hyper`` is written in 100% pure Python, which means no C extensions.
2626
For recent versions of Python (3.4 and onward, and 2.7.9 and onward) it's
2727
entirely self-contained with no external dependencies.
2828

29-
``hyper`` supports Python 3.4 and Python 2.7.9.
29+
``hyper`` supports Python 3.4 and Python 2.7.9, and can speak HTTP/2 and
30+
HTTP/1.1.
3031

3132
Caveat Emptor!
3233
--------------

docs/source/quickstart.rst

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
Quickstart Guide
44
================
55

6-
First, congratulations on picking ``hyper`` for your HTTP/2 needs. ``hyper``
7-
is the premier (and, as far as we're aware, the only) Python HTTP/2 library.
6+
First, congratulations on picking ``hyper`` for your HTTP needs. ``hyper``
7+
is the premier (and, as far as we're aware, the only) Python HTTP/2 library,
8+
as well as a very servicable HTTP/1.1 library.
9+
810
In this section, we'll walk you through using ``hyper``.
911

1012
Installing hyper
@@ -46,8 +48,8 @@ instructions from the `cryptography`_ project, replacing references to
4648

4749
.. _cryptography: https://cryptography.io/en/latest/installation/#installation
4850

49-
Making Your First Request
50-
-------------------------
51+
Making Your First HTTP/2 Request
52+
--------------------------------
5153

5254
With ``hyper`` installed, you can start making HTTP/2 requests. At this
5355
stage, ``hyper`` can only be used with services that *definitely* support
@@ -109,6 +111,41 @@ For example::
109111

110112
``hyper`` will ensure that each response is matched to the correct request.
111113

114+
Making Your First HTTP/1.1 Request
115+
-----------------------------------
116+
117+
With ``hyper`` installed, you can start making HTTP/2 requests. At this
118+
stage, ``hyper`` can only be used with services that *definitely* support
119+
HTTP/2. Before you begin, ensure that whichever service you're contacting
120+
definitely supports HTTP/2. For the rest of these examples, we'll use
121+
Twitter.
122+
123+
You can also use ``hyper`` to make HTTP/1.1 requests. The code is very similar.
124+
For example, to get the Twitter homepage::
125+
126+
>>> from hyper import HTTP11Connection
127+
>>> c = HTTP11Connection('twitter.com:443')
128+
>>> c.request('GET', '/')
129+
>>> resp = c.get_response()
130+
131+
The key difference between HTTP/1.1 and HTTP/2 is that when you make HTTP/1.1
132+
requests you do not get a stream ID. This is, of course, because HTTP/1.1 does
133+
not have streams.
134+
135+
Things behave exactly like they do in the HTTP/2 case, right down to the data
136+
reading::
137+
138+
>>> resp.getheader('content-encoding')
139+
'deflate'
140+
>>> resp.headers
141+
HTTPHeaderMap([(b'x-xss-protection', b'1; mode=block')...
142+
>>> resp.status
143+
200
144+
>>> body = resp.read()
145+
b'<!DOCTYPE html>\n<!--[if IE 8]><html clas ....
146+
147+
That's all it takes.
148+
112149
Requests Integration
113150
--------------------
114151

0 commit comments

Comments
 (0)