Tweak behaviour when the response body is a procedure

Newer versions of Guile Fibers will now use chunked encoding when a procedure
is used (and no content length is set). This is good, but not always what is
wanted, and there's also an issue with the port encoding.

This commit switches to responding with a string/bytevector when more
appropriate, plus explicitly setting the port encoding where that's needed.
This commit is contained in:
Christopher Baines 2023-02-09 10:39:24 +00:00
parent 3ba8418656
commit 0ce5af2c59
2 changed files with 29 additions and 28 deletions

View file

@ -278,8 +278,9 @@
(list (build-response (list (build-response
#:code 200 #:code 200
#:headers '((content-type . (text/plain)))) #:headers '((content-type . (text/plain))))
(call-with-output-string
(lambda (port) (lambda (port)
(write-metrics registry port))))))) (write-metrics registry port))))))))
(define (render-derivation derivation-file-name) (define (render-derivation derivation-file-name)
(letpar& ((derivation (letpar& ((derivation

View file

@ -112,6 +112,7 @@
#:code 200 #:code 200
#:headers '((content-type . (application/x-nix-archive #:headers '((content-type . (application/x-nix-archive
(charset . "ISO-8859-1"))))) (charset . "ISO-8859-1")))))
(call-with-output-bytevector
(lambda (port) (lambda (port)
(write-file-tree (write-file-tree
file-name file-name
@ -122,7 +123,7 @@
(bytevector-length derivation-bytevector))) (bytevector-length derivation-bytevector)))
#:file-port #:file-port
(lambda (file) (lambda (file)
(open-bytevector-input-port derivation-bytevector)))))))) (open-bytevector-input-port derivation-bytevector)))))))))
(not-found (request-uri request)))) (not-found (request-uri request))))
(define (render-lzip-nar request (define (render-lzip-nar request
@ -137,9 +138,12 @@
(lambda (data) (lambda (data)
(list (build-response (list (build-response
#:code 200 #:code 200
#:headers '((content-type . (application/x-nix-archive #:headers `((content-type . (application/x-nix-archive
(charset . "ISO-8859-1"))))) (charset . "ISO-8859-1")))
(content-length . ,(bytevector-length data))))
(lambda (port) (lambda (port)
;; TODO: This should probably be handled by guile-fibers
(set-port-encoding! port "ISO-8859-1")
(put-bytevector port data))))) (put-bytevector port data)))))
(not-found (request-uri request)))) (not-found (request-uri request))))
@ -188,11 +192,9 @@
(lambda (file) (lambda (file)
(open-bytevector-input-port derivation-bytevector))) (open-bytevector-input-port derivation-bytevector)))
(get-bytevector))))) (get-bytevector)))))
(lambda (port) (narinfo-string derivation-file-name
(display (narinfo-string derivation-file-name
nar-bytevector nar-bytevector
derivation-references) derivation-references)))))))
port))))))))
(and=> (parallel-via-thread-pool-channel (and=> (parallel-via-thread-pool-channel
(with-thread-postgresql-connection (with-thread-postgresql-connection
(lambda (conn) (lambda (conn)
@ -204,14 +206,12 @@
(list (build-response (list (build-response
#:code 200 #:code 200
#:headers '((content-type . (application/x-narinfo)))) #:headers '((content-type . (application/x-narinfo))))
(lambda (port) (derivation-source-file-narinfo-string store-path
(display (derivation-source-file-narinfo-string store-path
compression compression
compressed-size compressed-size
hash-algorithm hash-algorithm
hash hash
uncompressed-size) uncompressed-size)))))
port))))))
(not-found (request-uri request)))) (not-found (request-uri request))))