publish: Factorize 'content-length' addition.
authorLudovic Courtès <ludo@gnu.org>
Sat, 3 Dec 2016 23:38:30 +0000 (00:38 +0100)
committerLudovic Courtès <ludo@gnu.org>
Sun, 4 Dec 2016 21:32:26 +0000 (22:32 +0100)
* guix/scripts/publish.scm (with-content-length): New procedure.
(http-write) <application/octet-stream>: Use it.

guix/scripts/publish.scm

index 1b32f63..33a7b3b 100644 (file)
@@ -365,6 +365,14 @@ example: \"/foo/bar\" yields '(\"foo\" \"bar\")."
                            (response-headers response)
                            eq?)))
 
+(define (with-content-length response length)
+  "Return RESPONSE with a 'content-length' header set to LENGTH."
+  (set-field response (response-headers)
+             (alist-cons 'content-length length
+                         (alist-delete 'content-length
+                                       (response-headers response)
+                                       eq?))))
+
 (define-syntax-rule (swallow-EPIPE exp ...)
   "Swallow EPIPE errors raised by EXP..."
   (catch 'system-error
@@ -432,13 +440,8 @@ blocking."
             (call-with-input-file (utf8->string body)
               (lambda (input)
                 (let* ((size     (stat:size (stat input)))
-                       (headers  (alist-cons 'content-length size
-                                             (alist-delete 'content-length
-                                                           (response-headers response)
-                                                           eq?)))
-                       (response (write-response (set-field response
-                                                            (response-headers)
-                                                            headers)
+                       (response (write-response (with-content-length response
+                                                                      size)
                                                  client))
                        (output   (response-port response)))
                   (dump-port input output)