~amirouche/arew-http11

HTTP/1.1 reader and writer for Chez Scheme
a4375939 — Amirouche 7 months ago
Add support for chunked encoding for request and response read
f903ea39 — Amirouche 7 months ago
my name is progress.
b123d294 — Amirouche 7 months ago
cleanup

refs

main
browse  log 

clone

read-only
https://git.sr.ht/~amirouche/arew-http11
read/write
git@git.sr.ht:~amirouche/arew-http11

You can also use your local clone with git send-email.

#arew-http11

by Amirouche A. BOUBEKKI.

#Abstract

HTTP/1.1 is a widespread protocol to communicate on the Internet, and the basis of the web. This library offers procedures to read and write HTTP/1.1 and HTTP/1.0 network payload and does not require or assume a particular implementation of network sockets. arew-http11 library can also be described as HTTP sans io.

#Issues

  • The generators / accumulators might raise connection close or in the case of the generator return eof. Is that a problem?

#Example

#Request:

GET / HTTP/1.1
Host: hyper.dev
User-Agent: curl/7.68.0
Accept: */*

#Response

HTTP/1.1 200 OK
Server: nginx/1.18.0 (Ubuntu)
Date: Fri, 08 Apr 2022 16:09:16 GMT
Content-Type: text/html
Content-Length: 2952
Last-Modified: Sun, 03 Apr 2022 08:19:12 GMT
Connection: keep-alive
ETag: "62495880-b88"
Accept-Ranges: bytes

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="/static/normalize.css">
    <link rel="stylesheet" href="/static/styles.css">
    <link rel="stylesheet" href="/static/highlight.css">
    <title>hyper.dev</title>
  </head>
  <body>
    <div id="container">
      <div><h2><a href="https://hyper.dev">https://hyper.dev</a></h2>
<p>Let's agree that <em>serif</emfonts do not always carry boring stuff. And
have a taste of it:</p>
<blockquote><p>If a system must serve the creative spirit, it must be entirely
comprehensible by a single individual.</p>
</blockquote>
<p>Another bite:</p>
<blockquote><p>Programming languages should be designed not by piling feature on
top of feature, but by removing the weaknesses and restrictions that
make additional features appear necessary.  — <a href="http://r7rs.org/">Revised<sup>7</sup>
Report on the Algorithmic Language Scheme</a>,
Introduction.</p>
</blockquote>
<h3><a href="/discourse/">Discourse</a></h3>
<h3>Timeline</h3>
<ol>
<li><p>2022-04-01:</p>
<ul>
<li><a href="/n/compendium-of-web-patterns/">compendium of web patterns</a>;</li>
<li><a href="/n/defragmentation/">defragmentation</a>;</li>
<li><a href="/n/gambit-scheme-pawns/">gambit scheme pawns</a>;</li>
<li><a href="/n/flow/">flow</a>;</li>
<li><a href="/n/cocktail-text-search-algorithm/">cocktail: a work-in-progress text search algorithm</a>.</li>
</ul>
</li>
</ol>
<h3><a href="/blog/">Archive</a></h3>
<h3>Other formats</h3>
<p>There is available a <a href="hyper.dev.md">single markdown file</a>, a <a href="hyper.dev.html">single
<code>.html</codefile</a> and a <a href="hyper.dev.pdf"><code>.pdf</code></a>;</p>
<h3>LICENSE</h3>
<p>Copyright © Amirouche Amazigh BOUBEKKI, and contributors (2012-2022).</p>
<p>Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:</p>
<p>The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.</p>
<p>THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.</p>
</div>
    </div>
  </body>
</html>

#Documentation

#(http-error? obj)

Returns #t if OBJ is an HTTP error. Otherwise, it returns #f.

#(http-error-message obj)

Return a human readable string describing the error.

#(http-error-payload obj)

Return a machine-readable object that can help understand the error or possibly workaround it.

#(http-request-read generator)

Read a HTTP request from GENERATOR that yields one byte at a time, and returns five values: method, uri, version, headers and body.

The only argument GENERATOR must yield one byte at a time. It may raise errors, but those are unspecified, and should not be handled by http-request-read.

The five returned values are the following:

  • method: a string describing the HTTP method, it should not be checked, in other words according to this library any method is valid;

  • uri: a string describing the uri from the request line;

  • version: a pair of numbers describing the HTTP version;

  • headers: an association list where both key and values are strings;

  • body: a generator that yields bytevectors from the request body.

#(http-request-write accumulator method uri version headers body)

Write a HTTP request to ACCUMULATOR based on METHOD, URI, VERSION, HEADERS, and BODY.

The argument ACCUMULATOR must accept bytevectors as argument. The other arguments are specified as follows:

  • METHOD a string;

  • URI a string;

  • VERSION a pair of positive integers;

  • HEADERS an association list of string key and value pairs;

  • BODY a generator that yields a byte at a time;

#(http-response-read generator)

Read an HTTP request from GENERATOR, and returns version, code, reason, headers and the body.

The only argument GENERATOR should yield bytevectors. The returned values are specified as follows:

  • version: a pair of positive integers;

  • code: a positive integer;

  • reason: a string;

  • headers: an association list where both keys, and values are strings;

  • body: a generator that yields a bytevectors.

#(http-response-write accumulator version code reason headers body)

Write a HTTP response to ACCUMULATOR based on VERSION, CODE, REASON, HEADERS, and BODY.

The argument ACCUMULATOR must accept one byte at a time as argument. The other arguments are specified as follows:

  • VERSION a pair of positive integer;

  • CODE a positive integer;

  • REASON a string;

  • HEADERS an association list of string key and value pairs;

  • BODY a generator that yields a byte at a time;