ref: d7740736da8f2ca830d558cdedd89266b9019375
parent: f5175435a735fa8cdcd28995d5f3e32faf792945
author: Frank Smit <frank@61924.nl>
date: Tue Aug 17 16:16:53 EDT 2021
Use correct HTTP headers for response encoding.
--- a/lib/http/server.myr
+++ b/lib/http/server.myr
@@ -43,8 +43,11 @@
s = mksrvsession(fd)
while !srv.quit
match parsereq(s)
- | `std.Ok req: fn(srv, s, req)
- | `std.Err e: break
+ | `std.Ok req:
+ fn(srv, s, req)
+ freereq(s)
+ | `std.Err e:
+ break
;;
;;
std.close(fd)
@@ -56,11 +59,23 @@
sb = std.mksb()
ioput(s, "HTTP/1.1 {} {}\r\n", resp.status, statusstr(resp.status))
- ioput(s, "Content-Length: {}\r\n", resp.body.len)
- ioput(s, "Encoding: {}\r\n", resp.enc)
+
+ if resp.enc != `Chunked
+ ioput(s, "Content-Length: {}\r\n", resp.body.len)
+ ;;
+
+ match resp.enc
+ | `Length: /* noop */
+ | `Chunked: ioput(s, "Transfer-Encoding: chunked\r\n")
+ | `Compress: ioput(s, "Transfer-Encoding: compress\r\n")
+ | `Deflate: ioput(s, "Transfer-Encoding: deflate\r\n")
+ | `Gzip: ioput(s, "Transfer-Encoding: gzip\r\n")
+ ;;
+
for (k, v) : resp.hdrs
ioput(s, "{}: {}\r\n", k, v)
;;
+
ioput(s, "\r\n")
iowrite(s, resp.body)
ioflush(s)