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

View file

@ -81,6 +81,34 @@
"☺" "☺"
body))))) 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 ;; Test hanlding of exceptions when writing the response to a client
(run-fibers-for-tests (run-fibers-for-tests
(lambda () (lambda ()