unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: Christopher Baines <mail@cbaines.net>
To: guile-devel@gnu.org
Subject: [PATCH 1/2] web: Handle ending CRLF (\r\n) for chunked input and output ports.
Date: Thu, 30 Jun 2022 19:15:53 +0100	[thread overview]
Message-ID: <20220630181554.25772-1-mail@cbaines.net> (raw)

The chunked transfer encoding specifies the chunked body ends with
CRLF. This is in addition to the CRLF at the end of the last chunk, so
there should be CRLF twice at the end of the chunked body:

  https://datatracker.ietf.org/doc/html/rfc2616#section-3.6.1

* module/web/http.scm (make-chunked-input-port): Read two extra bytes at
the end of the chunked input.
(make-chunked-output-port): Write the missing \r\n when closing the
port.
* test-suite/tests/web-http.test (chunked encoding): Add missing \r\n to
test data.
---
 module/web/http.scm            | 3 ++-
 test-suite/tests/web-http.test | 4 ++--
 2 files changed, 4 insertions(+), 3 deletions(-)

diff --git a/module/web/http.scm b/module/web/http.scm
index 4276e1744..163c52176 100644
--- a/module/web/http.scm
+++ b/module/web/http.scm
@@ -1977,6 +1977,7 @@ closed it will also close PORT, unless the KEEP-ALIVE? is true."
                (cond
                 ((zero? size)
                  (set! finished? #t)
+                 (get-bytevector-n port 2) ; \r\n follows the last chunk
                  num-read)
                 (else
                  (loop to-read num-read)))))
@@ -2031,7 +2032,7 @@ KEEP-ALIVE? is true."
         (put-string port "\r\n"))))
   (define (close)
     (flush)
-    (put-string port "0\r\n")
+    (put-string port "0\r\n\r\n")
     (force-output port)
     (unless keep-alive?
       (close-port port)))
diff --git a/test-suite/tests/web-http.test b/test-suite/tests/web-http.test
index 63377349c..86fbc0e1a 100644
--- a/test-suite/tests/web-http.test
+++ b/test-suite/tests/web-http.test
@@ -421,7 +421,7 @@
                  '((basic (realm . "guile")))))
 
 (with-test-prefix "chunked encoding"
-  (let* ((s "5\r\nFirst\r\nA\r\n line\n Sec\r\n8\r\nond line\r\n0\r\n")
+  (let* ((s "5\r\nFirst\r\nA\r\n line\n Sec\r\n8\r\nond line\r\n0\r\n\r\n")
          (p (make-chunked-input-port (open-input-string s))))
     (pass-if-equal
         "First line\n Second line"
@@ -497,4 +497,4 @@
            (force-output out-chunked)
            (display "Third chunk" out-chunked)
            (close-port out-chunked))))
-      "b\r\nFirst chunk\r\nc\r\nSecond chunk\r\nb\r\nThird chunk\r\n0\r\n"))
+      "b\r\nFirst chunk\r\nc\r\nSecond chunk\r\nb\r\nThird chunk\r\n0\r\n\r\n"))
-- 
2.36.1




             reply	other threads:[~2022-06-30 18:15 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-30 18:15 Christopher Baines [this message]
2022-06-30 18:15 ` [PATCH 2/2] web: Don't hide missing data in the chunked input port Christopher Baines
2022-07-04  9:54 ` [PATCH 1/2] web: Handle ending CRLF (\r\n) for chunked input and output ports Ludovic Courtès
2022-07-04 11:37   ` Christopher Baines

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220630181554.25772-1-mail@cbaines.net \
    --to=mail@cbaines.net \
    --cc=guile-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).