README.rst 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. h11
  2. ===
  3. .. image:: https://travis-ci.org/python-hyper/h11.svg?branch=master
  4. :target: https://travis-ci.org/python-hyper/h11
  5. :alt: Automated test status
  6. .. image:: https://codecov.io/gh/python-hyper/h11/branch/master/graph/badge.svg
  7. :target: https://codecov.io/gh/python-hyper/h11
  8. :alt: Test coverage
  9. .. image:: https://readthedocs.org/projects/h11/badge/?version=latest
  10. :target: http://h11.readthedocs.io/en/latest/?badge=latest
  11. :alt: Documentation Status
  12. This is a little HTTP/1.1 library written from scratch in Python,
  13. heavily inspired by `hyper-h2 <https://hyper-h2.readthedocs.io/>`_.
  14. It's a "bring-your-own-I/O" library; h11 contains no IO code
  15. whatsoever. This means you can hook h11 up to your favorite network
  16. API, and that could be anything you want: synchronous, threaded,
  17. asynchronous, or your own implementation of `RFC 6214
  18. <https://tools.ietf.org/html/rfc6214>`_ -- h11 won't judge you.
  19. (Compare this to the current state of the art, where every time a `new
  20. network API <https://trio.readthedocs.io/>`_ comes along then someone
  21. gets to start over reimplementing the entire HTTP protocol from
  22. scratch.) Cory Benfield made an `excellent blog post describing the
  23. benefits of this approach
  24. <https://lukasa.co.uk/2015/10/The_New_Hyper/>`_, or if you like video
  25. then here's his `PyCon 2016 talk on the same theme
  26. <https://www.youtube.com/watch?v=7cC3_jGwl_U>`_.
  27. This also means that h11 is not immediately useful out of the box:
  28. it's a toolkit for building programs that speak HTTP, not something
  29. that could directly replace ``requests`` or ``twisted.web`` or
  30. whatever. But h11 makes it much easier to implement something like
  31. ``requests`` or ``twisted.web``.
  32. At a high level, working with h11 goes like this:
  33. 1) First, create an ``h11.Connection`` object to track the state of a
  34. single HTTP/1.1 connection.
  35. 2) When you read data off the network, pass it to
  36. ``conn.receive_data(...)``; you'll get back a list of objects
  37. representing high-level HTTP "events".
  38. 3) When you want to send a high-level HTTP event, create the
  39. corresponding "event" object and pass it to ``conn.send(...)``;
  40. this will give you back some bytes that you can then push out
  41. through the network.
  42. For example, a client might instantiate and then send a
  43. ``h11.Request`` object, then zero or more ``h11.Data`` objects for the
  44. request body (e.g., if this is a POST), and then a
  45. ``h11.EndOfMessage`` to indicate the end of the message. Then the
  46. server would then send back a ``h11.Response``, some ``h11.Data``, and
  47. its own ``h11.EndOfMessage``. If either side violates the protocol,
  48. you'll get a ``h11.ProtocolError`` exception.
  49. h11 is suitable for implementing both servers and clients, and has a
  50. pleasantly symmetric API: the events you send as a client are exactly
  51. the ones that you receive as a server and vice-versa.
  52. `Here's an example of a tiny HTTP client
  53. <https://github.com/python-hyper/h11/blob/master/examples/basic-client.py>`_
  54. It also has `a fine manual <https://h11.readthedocs.io/>`_.
  55. FAQ
  56. ---
  57. *Whyyyyy?*
  58. I wanted to play with HTTP in `Curio
  59. <https://curio.readthedocs.io/en/latest/tutorial.html>`__ and `Trio
  60. <https://trio.readthedocs.io>`__, which at the time didn't have any
  61. HTTP libraries. So I thought, no big deal, Python has, like, a dozen
  62. different implementations of HTTP, surely I can find one that's
  63. reusable. I didn't find one, but I did find Cory's call-to-arms
  64. blog-post. So I figured, well, fine, if I have to implement HTTP from
  65. scratch, at least I can make sure no-one *else* has to ever again.
  66. *Should I use it?*
  67. Maybe. You should be aware that it's a very young project. But, it's
  68. feature complete and has an exhaustive test-suite and complete docs,
  69. so the next step is for people to try using it and see how it goes
  70. :-). If you do then please let us know -- if nothing else we'll want
  71. to talk to you before making any incompatible changes!
  72. *What are the features/limitations?*
  73. Roughly speaking, it's trying to be a robust, complete, and non-hacky
  74. implementation of the first "chapter" of the HTTP/1.1 spec: `RFC 7230:
  75. HTTP/1.1 Message Syntax and Routing
  76. <https://tools.ietf.org/html/rfc7230>`_. That is, it mostly focuses on
  77. implementing HTTP at the level of taking bytes on and off the wire,
  78. and the headers related to that, and tries to be anal about spec
  79. conformance. It doesn't know about higher-level concerns like URL
  80. routing, conditional GETs, cross-origin cookie policies, or content
  81. negotiation. But it does know how to take care of framing,
  82. cross-version differences in keep-alive handling, and the "obsolete
  83. line folding" rule, so you can focus your energies on the hard /
  84. interesting parts for your application, and it tries to support the
  85. full specification in the sense that any useful HTTP/1.1 conformant
  86. application should be able to use h11.
  87. It's pure Python, and has no dependencies outside of the standard
  88. library.
  89. It has a test suite with 100.0% coverage for both statements and
  90. branches.
  91. Currently it supports Python 3 (testing on 3.7-3.10) and PyPy 3.
  92. The last Python 2-compatible version was h11 0.11.x.
  93. (Originally it had a Cython wrapper for `http-parser
  94. <https://github.com/nodejs/http-parser>`_ and a beautiful nested state
  95. machine implemented with ``yield from`` to postprocess the output. But
  96. I had to take these out -- the new *parser* needs fewer lines-of-code
  97. than the old *parser wrapper*, is written in pure Python, uses no
  98. exotic language syntax, and has more features. It's sad, really; that
  99. old state machine was really slick. I just need a few sentences here
  100. to mourn that.)
  101. I don't know how fast it is. I haven't benchmarked or profiled it yet,
  102. so it's probably got a few pointless hot spots, and I've been trying
  103. to err on the side of simplicity and robustness instead of
  104. micro-optimization. But at the architectural level I tried hard to
  105. avoid fundamentally bad decisions, e.g., I believe that all the
  106. parsing algorithms remain linear-time even in the face of pathological
  107. input like slowloris, and there are no byte-by-byte loops. (I also
  108. believe that it maintains bounded memory usage in the face of
  109. arbitrary/pathological input.)
  110. The whole library is ~800 lines-of-code. You can read and understand
  111. the whole thing in less than an hour. Most of the energy invested in
  112. this so far has been spent on trying to keep things simple by
  113. minimizing special-cases and ad hoc state manipulation; even though it
  114. is now quite small and simple, I'm still annoyed that I haven't
  115. figured out how to make it even smaller and simpler. (Unfortunately,
  116. HTTP does not lend itself to simplicity.)
  117. The API is ~feature complete and I don't expect the general outlines
  118. to change much, but you can't judge an API's ergonomics until you
  119. actually document and use it, so I'd expect some changes in the
  120. details.
  121. *How do I try it?*
  122. .. code-block:: sh
  123. $ pip install h11
  124. $ git clone git@github.com:python-hyper/h11
  125. $ cd h11/examples
  126. $ python basic-client.py
  127. and go from there.
  128. *License?*
  129. MIT
  130. *Code of conduct?*
  131. Contributors are requested to follow our `code of conduct
  132. <https://github.com/python-hyper/h11/blob/master/CODE_OF_CONDUCT.md>`_ in
  133. all project spaces.