guile-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] web: Support reading chunked request bodies.


From: Christopher Baines
Subject: [PATCH] web: Support reading chunked request bodies.
Date: Thu, 20 Jul 2023 15:02:38 +0100

Guile already supports chunked response bodies, but chunked request
bodies are allowed as well.  This is useful when you're sending an
unknown amount of data in the request.

* module/web/request.scm (read-request-body): Support the request body
being chunked.
---
 module/web/request.scm | 26 +++++++++++++++++---------
 1 file changed, 17 insertions(+), 9 deletions(-)

diff --git a/module/web/request.scm b/module/web/request.scm
index ff4b94485..b24bc0d77 100644
--- a/module/web/request.scm
+++ b/module/web/request.scm
@@ -221,15 +221,23 @@ on PORT, perhaps using some transfer encoding."
                     (request-headers r) (request-meta r) port)))
 
 (define (read-request-body r)
-  "Reads the request body from R, as a bytevector.  Return ‘#f’
-if there was no request body."
-  (let ((nbytes (request-content-length r)))
-    (and nbytes
-         (let ((bv (get-bytevector-n (request-port r) nbytes)))
-           (if (= (bytevector-length bv) nbytes)
-               bv
-               (bad-request "EOF while reading request body: ~a bytes of ~a"
-                            (bytevector-length bv) nbytes))))))
+  "Reads the request body from R.  If the request body is chunked, a port
+will be returned.  Otherwise, if the request body is present, it will be
+returned as a bytevector or ‘#f’ will be returned if there was no
+request body."
+  (cond
+   ((member '(chunked) (request-transfer-encoding r))
+    (make-chunked-input-port (request-port r)
+                             ;; closing the port is handled elsewhere
+                             #:keep-alive? #t))
+   (else
+    (let ((nbytes (request-content-length r)))
+      (and nbytes
+           (let ((bv (get-bytevector-n (request-port r) nbytes)))
+             (if (= (bytevector-length bv) nbytes)
+                 bv
+                 (bad-request "EOF while reading request body: ~a bytes of ~a"
+                              (bytevector-length bv) nbytes))))))))
 
 (define (write-request-body r bv)
   "Write BV, a bytevector, to the port corresponding to the HTTP
-- 
2.41.0




reply via email to

[Prev in Thread] Current Thread [Next in Thread]