Add support for trailers
Discussed in https://github.com/encode/uvicorn/discussions/1593
Originally posted by sam-kleiner August 4, 2022 I would like to add support for HTTP trailers.
This is already supported in the h11 library so adding this is fairly easy.
I wrote a POC with tests
Uses for this in include returning hashes for validation, expire info, errors during stream, etc.
Here is the output (using my code) of a curl command getting streaming json where an error happens before all json is sent to the client
$ curl -v -N http://localhost:8000 --raw
* Trying 127.0.0.1:8000...
* Connected to localhost (127.0.0.1) port 8000 (#0)
> GET / HTTP/1.1
> Host: localhost:8000
> User-Agent: curl/7.79.1
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< date: Wed, 03 Aug 2022 22:37:47 GMT
< server: uvicorn
< content-type: application/json
< trailer: X-Error
< Transfer-Encoding: chunked
<
c
{"message":[
8
"bEHWA",
8
"xuyBF",
8
"14vBZ",
8
"f4Jyp",
8
"uadVV",
8
"oTbyL",
8
"aZcZr",
0
x-error: Exception message here
* Connection #0 to host localhost left intact
I do not remember that ASGI supports trailers, so is this an extension to the ASGI specification?
Related: https://github.com/django/asgiref/issues/141
This may be an asgi extension, I think. By the way, can we use a uniform name for it?You have just named it http.response.trailers, but mine is http.trailingheaders.send. ~~Maybe your's better?~~
I've implemented this under uvicorn-trailers.
The code for it can be found on https://github.com/Kludex/uvicorn-extensions/blob/main/src/python/uvicorn-trailers/uvicorn_trailers/httptools_impl.py
I'll periodically check the downloads of that package, but if you are interested on this extension, please add a message on this issue explaining why you need it, and like this message.