Make more web server encoding tweaks

This commit is contained in:
Christopher Baines 2025-02-07 16:09:42 +00:00
parent b0bb65dbed
commit cb2085b684
2 changed files with 33 additions and 12 deletions

View file

@ -57,8 +57,7 @@
sock))
(define* (make-chunked-output-port/knots port #:key (keep-alive? #f)
(buffering 1200)
(encoding "ISO-8859-1"))
(buffering 1200))
"Returns a new port which translates non-encoded data into a HTTP
chunked transfer encoded data and writes this to PORT. Data written to
this port is buffered until the port is flushed, at which point it is
@ -83,7 +82,6 @@ KEEP-ALIVE? is true."
(define ret
(make-custom-binary-output-port "chunked http" write! #f #f close))
(set-port-encoding! ret encoding)
(setvbuf ret 'block buffering)
ret)
@ -284,10 +282,7 @@ on the procedure being called at any particular time."
(lambda ()
(write-response response client)
(let* ((change-client-port-encoding?
(and (procedure? body)
(not (response-content-length response))))
(body-written?
(let ((body-written?
(if (procedure? body)
(let* ((type (response-content-type response
'(text/plain)))
@ -298,10 +293,8 @@ on the procedure being called at any particular time."
client
(make-chunked-output-port/knots
client
#:keep-alive? #t
#:encoding charset))))
(when change-client-port-encoding?
(set-port-encoding! client charset))
#:keep-alive? #t))))
(set-port-encoding! body-port charset)
(let ((body-written?
(with-exception-handler
(lambda (exn)
@ -324,7 +317,7 @@ on the procedure being called at any particular time."
(if body-written?
(begin
(force-output client)
(when change-client-port-encoding?
(when (procedure? body)
(set-port-encoding! client "ISO-8859-1"))
(keep-alive? response))
#f)))

View file

@ -81,6 +81,34 @@
"☺"
body)))))
(run-fibers-for-tests
(lambda ()
(let* ((web-server
(run-knots-web-server
(lambda (request)
(values '((content-type . (text/plain
(charset . "utf-8")))
(content-length . 3))
(lambda (port)
(display "☺" port))))
#:port 0)) ;; Bind to any port
(port
(web-server-port web-server))
(uri
(build-uri 'http #:host "127.0.0.1" #:port port)))
(let ((response
body
(http-get
uri
;; TODO Remove once using Guile 3.0.10
#:streaming? #t
#:port (non-blocking-open-socket-for-uri uri))))
(assert-equal
"☺"
(utf8->string
(get-bytevector-n body 3)))))))
;; Test hanlding of exceptions when writing the response to a client
(run-fibers-for-tests
(lambda ()