unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
* [PATCH] web: Support reading chunked request bodies.
@ 2023-07-20 14:02 Christopher Baines
  2023-07-24 21:03 ` Maxime Devos
  0 siblings, 1 reply; 2+ messages in thread
From: Christopher Baines @ 2023-07-20 14:02 UTC (permalink / raw)
  To: guile-devel

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




^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-07-24 21:03 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-07-20 14:02 [PATCH] web: Support reading chunked request bodies Christopher Baines
2023-07-24 21:03 ` Maxime Devos

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).