@@ -2,7 +2,7 @@ Getting Started: Writing Your Own HTTP/2 Server
22===============================================
33
44This 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
66basic concepts you need to understand, and talks you through writing a very
77simple HTTP/2 server.
88
@@ -17,10 +17,10 @@ return to this documentation.
1717Connections
1818-----------
1919
20- Hyper- h2's core object is the
20+ h2's core object is the
2121:class: `H2Connection <h2.connection.H2Connection> ` object. This object is an
2222abstract 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
2424first thing you create and the object that does most of the heavy lifting.
2525
2626The interface to this object is relatively simple. For sending data, you
@@ -54,7 +54,7 @@ this document, we'll do just that.
5454
5555Some important subtleties of ``H2Connection `` objects are covered in
5656: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
5858``H2Connection `` object doesn't do I/O. Let's talk briefly about why.
5959
6060I/O
@@ -74,19 +74,19 @@ into bytes to send. So there's no reason to have lots of different versions of
7474this core protocol code: one for Twisted, one for gevent, one for threading,
7575and one for synchronous code.
7676
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
7979events 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.
8181
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
8383easy-to-understand performance envelope, it's easy to test (and as a result
8484easy to get correct behaviour out of), and it behaves in a reproducible way.
8585These are all great traits to have in a library that is doing something quite
8686complex.
8787
8888This 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
9090your software.
9191
9292
@@ -99,7 +99,7 @@ When writing a HTTP/2 implementation it's important to know what the remote
9999peer is doing: if you didn't care, writing networked programs would be a lot
100100easier!
101101
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
103103you receive data from the remote peer and pass it into your ``H2Connection ``
104104object (see :ref: `h2-connection-basic `), the ``H2Connection `` returns a list
105105of 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:
112112for example, :class: `PushedStreamReceived <h2.events.PushedStreamReceived> `
113113refers to Server Push, a very HTTP/2-specific concept.
114114
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
117117response to an event. Your code will need to handle these events, and make
118118decisions 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.
120120
121121A full list of events is available in :ref: `h2-events-api `. For the purposes
122122of 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
129129HTTP/2 web server. The goal of this server is to write a server that can handle
130130a HTTP GET, and that returns the headers sent by the client, encoded in JSON.
131131Basically, 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.
133133
134134For the sake of simplicity, we're going to write this using the Python standard
135135library, in Python 3. In reality, you'll probably want to use an asynchronous
136136framework of some kind: see the `examples directory `_ in the repository for
137137some examples of how you'd do that.
138138
139139Before 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
141141instructions in :doc: `installation `.
142142
143143Step 1: Sockets
@@ -324,7 +324,7 @@ Let's do that next.
324324Step 3: Sending the Preamble
325325~~~~~~~~~~~~~~~~~~~~~~~~~~~~
326326
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
328328the
329329:meth: `initiate_connection <h2.connection.H2Connection.initiate_connection> `
330330method, and then send the corresponding data. Let's update our ``handle ``
@@ -348,7 +348,7 @@ new method in there:
348348:meth: `data_to_send <h2.connection.H2Connection.data_to_send> `.
349349
350350When 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
352352doesn't do any I/O, so it can't do that itself. Instead, it writes it to an
353353internal buffer. You can retrieve data from this buffer using the
354354``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
477477HTTP status code that used to go at the top of a HTTP response. Here, we're
478478saying the response is ``200 OK ``, which is successful.
479479
480- To send headers in Hyper- h2, you use the
480+ To send headers in h2, you use the
481481:meth: `send_headers <h2.connection.H2Connection.send_headers> ` function.
482482
483483Next, we want to send the body data. To do that, we use the
484484: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
486486unicode 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.
488488
489489The 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
491491we're done with sending data: the response is over. Because we know that
492492``hyper `` will have ended its side of the stream, when we end ours the stream
493493will be totally done with.
@@ -703,7 +703,7 @@ see something like the following output from ``hyper``:
703703 Here you can see the HTTP/2 request 'special headers' that ``hyper `` sends.
704704These are similar to the ``:status `` header we have to send on our response:
705705they 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
707707were sending those headers.
708708
709709Congratulations!
@@ -737,10 +737,10 @@ it, there are a few directions you could investigate:
737737
738738.. _event loop : https://en.wikipedia.org/wiki/Event_loop
739739.. _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
742742.. _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
745745.. _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