From 6be113f99d52cc284ecd0ed07fc88df5ea7bf718 Mon Sep 17 00:00:00 2001 From: Christopher Baines Date: Thu, 9 Feb 2023 11:43:42 +0000 Subject: [PATCH] Adjust render procedures to not use procedures for responses The newer Guile Fibers web server will use the chunked transfer encoding when a procedure is used and the content length is unspecified. This is good for large responses, but unnecessary here. Also, there's a bug with the charset so these changes to respond with correctly encoded bytevectors to avoid that. --- guix-data-service/web/render.scm | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/guix-data-service/web/render.scm b/guix-data-service/web/render.scm index 081399a..744c66c 100644 --- a/guix-data-service/web/render.scm +++ b/guix-data-service/web/render.scm @@ -24,6 +24,7 @@ #:use-module (srfi srfi-19) #:use-module (srfi srfi-26) #:use-module (ice-9 ftw) + #:use-module (ice-9 iconv) #:use-module (ice-9 binary-ports) #:use-module (web request) #:use-module (web response) @@ -142,30 +143,39 @@ (list (build-response #:code code #:headers (append extra-headers - '((content-type . (text/html)) + '((content-type . (text/html + (charset . "utf-8"))) (vary . (accept))))) - (lambda (port) - (sxml->html sxml port)))) + (call-with-encoded-output-string + "utf-8" + (lambda (port) + (sxml->html sxml port))))) (define* (render-json json #:key (extra-headers '()) (code 200)) (list (build-response #:code code #:headers (append extra-headers - '((content-type . (application/json)) + '((content-type . (application/json + (charset . "utf-8"))) (vary . (accept))))) - (lambda (port) - (scm->json json port)))) + (call-with-encoded-output-string + "utf-8" + (lambda (port) + (scm->json json port))))) (define* (render-text text #:key (extra-headers '()) (code 200)) (list (build-response #:code code #:headers (append extra-headers - '((content-type . (text/plain)) + '((content-type . (text/plain + (charset . "utf-8"))) (vary . (accept))))) - (lambda (port) - (display text port)))) + (call-with-encoded-output-string + "utf-8" + (lambda (port) + (display text port))))) (define (not-found uri) (list (build-response #:code 404)