@@ -2,7 +2,7 @@ Getting Started: Writing Your Own HTTP/2 Server
2
2
===============================================
3
3
4
4
This document explains how to get started writing fully-fledged HTTP/2
5
- implementations using Hyper- h2 as the underlying protocol stack. It covers the
5
+ implementations using h2 as the underlying protocol stack. It covers the
6
6
basic concepts you need to understand, and talks you through writing a very
7
7
simple HTTP/2 server.
8
8
@@ -17,10 +17,10 @@ return to this documentation.
17
17
Connections
18
18
-----------
19
19
20
- Hyper- h2's core object is the
20
+ h2's core object is the
21
21
:class: `H2Connection <h2.connection.H2Connection> ` object. This object is an
22
22
abstract representation of the state of a single HTTP/2 connection, and holds
23
- all the important protocol state. When using Hyper- h2, this object will be the
23
+ all the important protocol state. When using h2, this object will be the
24
24
first thing you create and the object that does most of the heavy lifting.
25
25
26
26
The interface to this object is relatively simple. For sending data, you
@@ -54,7 +54,7 @@ this document, we'll do just that.
54
54
55
55
Some important subtleties of ``H2Connection `` objects are covered in
56
56
:doc: `advanced-usage `: see :ref: `h2-connection-advanced ` for more information.
57
- However, one subtlety should be covered, and that is this: Hyper- h2's
57
+ However, one subtlety should be covered, and that is this: h2's
58
58
``H2Connection `` object doesn't do I/O. Let's talk briefly about why.
59
59
60
60
I/O
@@ -74,19 +74,19 @@ into bytes to send. So there's no reason to have lots of different versions of
74
74
this core protocol code: one for Twisted, one for gevent, one for threading,
75
75
and one for synchronous code.
76
76
77
- This is why we said at the top that Hyper- h2 is a *HTTP/2 Protocol Stack *, not
78
- a *fully-fledged implementation *. Hyper- h2 knows how to transform bytes into
77
+ This is why we said at the top that h2 is a *HTTP/2 Protocol Stack *, not
78
+ a *fully-fledged implementation *. h2 knows how to transform bytes into
79
79
events and back, but that's it. The I/O and smarts might be different, but
80
- the core HTTP/2 logic is the same: that's what Hyper- h2 provides.
80
+ the core HTTP/2 logic is the same: that's what h2 provides.
81
81
82
- Not doing I/O makes Hyper- h2 general, and also relatively simple. It has an
82
+ Not doing I/O makes h2 general, and also relatively simple. It has an
83
83
easy-to-understand performance envelope, it's easy to test (and as a result
84
84
easy to get correct behaviour out of), and it behaves in a reproducible way.
85
85
These are all great traits to have in a library that is doing something quite
86
86
complex.
87
87
88
88
This document will talk you through how to build a relatively simple HTTP/2
89
- implementation using Hyper- h2, to give you an understanding of where it fits in
89
+ implementation using h2, to give you an understanding of where it fits in
90
90
your software.
91
91
92
92
@@ -99,7 +99,7 @@ When writing a HTTP/2 implementation it's important to know what the remote
99
99
peer is doing: if you didn't care, writing networked programs would be a lot
100
100
easier!
101
101
102
- Hyper- h2 encodes the actions of the remote peer in the form of *events *. When
102
+ h2 encodes the actions of the remote peer in the form of *events *. When
103
103
you receive data from the remote peer and pass it into your ``H2Connection ``
104
104
object (see :ref: `h2-connection-basic `), the ``H2Connection `` returns a list
105
105
of objects, each one representing a single event that has occurred. Each
@@ -112,11 +112,11 @@ concept, not just a HTTP/2 one. Other events are extremely HTTP/2-specific:
112
112
for example, :class: `PushedStreamReceived <h2.events.PushedStreamReceived> `
113
113
refers to Server Push, a very HTTP/2-specific concept.
114
114
115
- The reason these events exist is that Hyper- h2 is intended to be very general.
116
- This means that, in many cases, Hyper- h2 does not know exactly what to do in
115
+ The reason these events exist is that h2 is intended to be very general.
116
+ This means that, in many cases, h2 does not know exactly what to do in
117
117
response to an event. Your code will need to handle these events, and make
118
118
decisions about what to do. That's the major role of any HTTP/2 implementation
119
- built on top of Hyper- h2.
119
+ built on top of h2.
120
120
121
121
A full list of events is available in :ref: `h2-events-api `. For the purposes
122
122
of this example, we will handle only a small set of events.
@@ -129,15 +129,15 @@ Armed with the knowledge you just obtained, we're going to write a very simple
129
129
HTTP/2 web server. The goal of this server is to write a server that can handle
130
130
a HTTP GET, and that returns the headers sent by the client, encoded in JSON.
131
131
Basically, something a lot like `httpbin.org/get `_. Nothing fancy, but this is
132
- a good way to get a handle on how you should interact with Hyper- h2.
132
+ a good way to get a handle on how you should interact with h2.
133
133
134
134
For the sake of simplicity, we're going to write this using the Python standard
135
135
library, in Python 3. In reality, you'll probably want to use an asynchronous
136
136
framework of some kind: see the `examples directory `_ in the repository for
137
137
some examples of how you'd do that.
138
138
139
139
Before we start, create a new file called ``h2server.py ``: we'll use that as
140
- our workspace. Additionally, you should install Hyper- h2: follow the
140
+ our workspace. Additionally, you should install h2: follow the
141
141
instructions in :doc: `installation `.
142
142
143
143
Step 1: Sockets
@@ -324,7 +324,7 @@ Let's do that next.
324
324
Step 3: Sending the Preamble
325
325
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
326
326
327
- Hyper- h2 makes doing connection setup really easy. All you need to do is call
327
+ h2 makes doing connection setup really easy. All you need to do is call
328
328
the
329
329
:meth: `initiate_connection <h2.connection.H2Connection.initiate_connection> `
330
330
method, and then send the corresponding data. Let's update our ``handle ``
@@ -348,7 +348,7 @@ new method in there:
348
348
:meth: `data_to_send <h2.connection.H2Connection.data_to_send> `.
349
349
350
350
When you make function calls on your ``H2Connection `` object, these will often
351
- want to cause HTTP/2 data to be written out to the network. But Hyper- h2
351
+ want to cause HTTP/2 data to be written out to the network. But h2
352
352
doesn't do any I/O, so it can't do that itself. Instead, it writes it to an
353
353
internal buffer. You can retrieve data from this buffer using the
354
354
``data_to_send `` method. There are some subtleties about that method, but we
@@ -477,17 +477,17 @@ there: ``:status``. This is a HTTP/2-specific header, and it's used to hold the
477
477
HTTP status code that used to go at the top of a HTTP response. Here, we're
478
478
saying the response is ``200 OK ``, which is successful.
479
479
480
- To send headers in Hyper- h2, you use the
480
+ To send headers in h2, you use the
481
481
:meth: `send_headers <h2.connection.H2Connection.send_headers> ` function.
482
482
483
483
Next, we want to send the body data. To do that, we use the
484
484
:meth: `send_data <h2.connection.H2Connection.send_data> ` function. This also
485
- takes a stream ID. Note that the data is binary: Hyper- h2 does not work with
485
+ takes a stream ID. Note that the data is binary: h2 does not work with
486
486
unicode strings, so you *must * pass bytestrings to the ``H2Connection ``. The
487
- one exception is headers: Hyper- h2 will automatically encode those into UTF-8.
487
+ one exception is headers: h2 will automatically encode those into UTF-8.
488
488
489
489
The last thing to note is that on our call to ``send_data ``, we set
490
- ``end_stream `` to ``True ``. This tells Hyper- h2 (and the remote peer) that
490
+ ``end_stream `` to ``True ``. This tells h2 (and the remote peer) that
491
491
we're done with sending data: the response is over. Because we know that
492
492
``hyper `` will have ended its side of the stream, when we end ours the stream
493
493
will be totally done with.
@@ -703,7 +703,7 @@ see something like the following output from ``hyper``:
703
703
Here you can see the HTTP/2 request 'special headers' that ``hyper `` sends.
704
704
These are similar to the ``:status `` header we have to send on our response:
705
705
they encode important parts of the HTTP request in a clearly-defined way. If
706
- you were writing a client stack using Hyper- h2, you'd need to make sure you
706
+ you were writing a client stack using h2, you'd need to make sure you
707
707
were sending those headers.
708
708
709
709
Congratulations!
@@ -737,10 +737,10 @@ it, there are a few directions you could investigate:
737
737
738
738
.. _event loop : https://en.wikipedia.org/wiki/Event_loop
739
739
.. _httpbin.org/get : https://httpbin.org/get
740
- .. _examples directory : https://github.com/python-hyper/hyper- h2/tree/master/examples
741
- .. _standard library's socket module : https://docs.python.org/3.5 /library/socket.html
740
+ .. _examples directory : https://github.com/python-hyper/h2/tree/master/examples
741
+ .. _standard library's socket module : https://docs.python.org/3/library/socket.html
742
742
.. _Application Layer Protocol Negotiation : https://en.wikipedia.org/wiki/Application-Layer_Protocol_Negotiation
743
- .. _get your certificate here : https://raw.githubusercontent.com/python-hyper/hyper- h2/master/examples/twisted/server.crt
744
- .. _get your private key here : https://raw.githubusercontent.com/python-hyper/hyper- h2/master/examples/twisted/server.key
743
+ .. _get your certificate here : https://raw.githubusercontent.com/python-hyper/h2/master/examples/twisted/server.crt
744
+ .. _get your private key here : https://raw.githubusercontent.com/python-hyper/h2/master/examples/twisted/server.key
745
745
.. _PyOpenSSL : http://pyopenssl.readthedocs.org/
746
- .. _Eventlet example : https://github.com/python-hyper/hyper- h2/blob/master/examples/eventlet/eventlet-server.py
746
+ .. _Eventlet example : https://github.com/python-hyper/h2/blob/master/examples/eventlet/eventlet-server.py
0 commit comments