1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-05-20 11:40:18 +02:00

web: Handle ending CRLF (\r\n) for chunked input and output ports.

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.

Signed-off-by: Ludovic Courtès <ludo@gnu.org>
This commit is contained in:
Christopher Baines 2022-06-30 19:15:53 +01:00 committed by Ludovic Courtès
parent a84d8f6473
commit 9a3353a993
2 changed files with 4 additions and 3 deletions

View file

@ -1987,6 +1987,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)))))
@ -2041,7 +2042,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)))

View file

@ -426,7 +426,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"
@ -502,4 +502,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"))