From patchwork Fri May 1 13:35:50 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mathieu Othacehe X-Patchwork-Id: 21789 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id 92F9527BBE4; Fri, 1 May 2020 14:37:54 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED,FREEMAIL_FROM,MAILING_LIST_MULTI,T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.2 Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:470:142::17]) by mira.cbaines.net (Postfix) with ESMTP id 140A827BBE1 for ; Fri, 1 May 2020 14:37:54 +0100 (BST) Received: from localhost ([::1]:45258 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jUVrV-00032L-Jd for patchwork@mira.cbaines.net; Fri, 01 May 2020 09:37:53 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:53650) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jUVqg-0002OG-RJ for guix-patches@gnu.org; Fri, 01 May 2020 09:37:03 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.90_1) (envelope-from ) id 1jUVqg-0001sg-5B for guix-patches@gnu.org; Fri, 01 May 2020 09:37:02 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:36906) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1jUVqf-0001qp-NV for guix-patches@gnu.org; Fri, 01 May 2020 09:37:01 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1jUVqf-0007Os-Jr for guix-patches@gnu.org; Fri, 01 May 2020 09:37:01 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#40993] cuirass: Add build products download support. Resent-From: Mathieu Othacehe Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Fri, 01 May 2020 13:37:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 40993 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: To: Danny Milosavljevic Cc: ludo@gnu.org, 40993@debbugs.gnu.org Received: via spool by 40993-submit@debbugs.gnu.org id=B40993.158834016228351 (code B ref 40993); Fri, 01 May 2020 13:37:01 +0000 Received: (at 40993) by debbugs.gnu.org; 1 May 2020 13:36:02 +0000 Received: from localhost ([127.0.0.1]:48452 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1jUVph-0007N7-L3 for submit@debbugs.gnu.org; Fri, 01 May 2020 09:36:02 -0400 Received: from mail-wm1-f42.google.com ([209.85.128.42]:52595) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1jUVpe-0007Md-H1 for 40993@debbugs.gnu.org; Fri, 01 May 2020 09:35:59 -0400 Received: by mail-wm1-f42.google.com with SMTP id 188so5971677wmc.2 for <40993@debbugs.gnu.org>; Fri, 01 May 2020 06:35:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:references:date:in-reply-to:message-id :user-agent:mime-version; bh=nPx3f8qRTuuusvdvoSKde1n43sJ99f97tSp4VCb8pMU=; b=MJ3I3o1UzIsbQoWYOjdv4lhJNPfq6b0XwD9AsfqS//BFnMqjAEnfwYvzQVeXC2gsQo Qwq4s0NMBmv7zQvyc89PoY8DqMLPz/qQIS+nB59EW85+EyNGoxC7qwucx2w4OJkCyliM ppdKWJVN/TTnVlWdAMPprr5Nv7E5IhuWwhFfFb4rJDmvNDHisBkE84nZbLSifOHEB83F DNQ8AsY+BMyRkswRmi9uKFshjhuI8Jb3j0+0l7ZPFIriaVrqT5OVvcSM49WPB3Dq1Ykf WeYc8lHAoRr2rGHkfSbNYzYBMVpLUhm8ZnkDsjwEWcSrDbOC6B3dCWu7RS9iLq6vcATg YUoA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:references:date:in-reply-to :message-id:user-agent:mime-version; bh=nPx3f8qRTuuusvdvoSKde1n43sJ99f97tSp4VCb8pMU=; b=Nlr1p2UowmLwiANBBiJvEG7DAHjTIDhTwX1BJ3CQsQpCeSN8eoNAvpFAmV9ywZ8law wwDKfCEcdfct5BGXxVBItJQaHfBdvkj2aGcH6zjWzbXN5uI18YkNlT2PcVA7H4m+9iMt Ksxp2KX7kIsSVLK6qwbAmXUpA+42LLz9wKEJyQByenXHV5nmc8vawHjpZRUH4hq7nR34 axMbTSVhusIughL83Nb5jYLf5IcMaw++w3izccfDR7zcHikGN/qHFeFVh1DE9y7mzH63 INSCvpnwQDt0mS4Z6hcZ6D/LADacTaSfm+u5mXaSRvJcj5/4Ey9bC/K2XMLtrkEWzfPw R4MA== X-Gm-Message-State: AGi0PubPc69e/O/PqcE4mt/EFOteHxcBQ/T8j4oJZC/Y5lLfdKKHu3mI xhcGBDa5HGqMaykV9MbmBnE= X-Google-Smtp-Source: APiQypIJz8gvWh68Cqe5Fnv7arBByLp374a8OWcyac2Iug40B8+xZrzMKEdiJI4wg6MIBsqZBFhwLg== X-Received: by 2002:a05:600c:2314:: with SMTP id 20mr4294486wmo.118.1588340152544; Fri, 01 May 2020 06:35:52 -0700 (PDT) Received: from meru ([2a01:cb18:832e:5f00:5142:5b8b:1d3f:ecc5]) by smtp.gmail.com with ESMTPSA id r2sm3896324wmg.2.2020.05.01.06.35.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 May 2020 06:35:51 -0700 (PDT) From: Mathieu Othacehe References: <87ees4uja7.fsf@gmail.com> <20200501120914.606ffe02@scratchpost.org> Date: Fri, 01 May 2020 15:35:50 +0200 In-Reply-To: <20200501120914.606ffe02@scratchpost.org> (Danny Milosavljevic's message of "Fri, 1 May 2020 12:09:14 +0200") Message-ID: <874ksz4w21.fsf@gmail.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.3 (gnu/linux) MIME-Version: 1.0 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-Received-From: 209.51.188.43 X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: "Guix-patches" X-getmail-retrieved-from-mailbox: Patches Hey Danny, > very cool! Thanks :) > Though I agree using sendfile would be much better, especially since the user > can download 800 MB image files there. > > The guile (web server) module allows passing a procedure as the #:body, but > then it makes a bytevector out of the result and hard-codes the content-type :P. > > Eventually (web server http) http-write is reached, which only supports encoding > bytevectors and #f, that's it. No files. > > So we'd have to overwrite http-write. > > But we are using our own (web server fiberized) impl already. > > So our impl chould be extended to be able to get and process FDs. > > client-loop there has > > (lambda (response body) > (write-response response client) > (when body > (put-bytevector client body)) > > which means the "when body" part should be extended to also handle files, not just bytevectors. The problem is that even with our fiberized implementation, what we pass as "body" is checked in "sanitize-response" procedure of Guile's (web server) module. With the (very) hacky patch attached, I fool sanitize-response, by sending the file name as a bytevector. This allows me to save gigabytes of RAM when downloading disk images. WDYT? Thanks, Mathieu From 0c5e91c170639d50d1cc339fa0b0e68ea4fba68c Mon Sep 17 00:00:00 2001 From: Mathieu Othacehe Date: Fri, 1 May 2020 15:03:12 +0200 Subject: [PATCH] cuirass: Use sendfiles instead of raw copies. * src/cuirass/http.scm (respond-file): Send the file name as an UTF8 bytevector, instead of the raw file content, (respond-gzipped-file): ditto. Also set 'content-disposition header. * src/web/server/fiberized.scm (client-loop): Check if 'content-disposition is set. If it's the case, assume that the bytevector is the file name, and use sendfiles to send it. Otherwise, keep the existing behaviour and send directly the received bytevector. --- src/cuirass/http.scm | 25 ++++++++++--------------- src/web/server/fiberized.scm | 21 +++++++++++++++++++-- 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/src/cuirass/http.scm b/src/cuirass/http.scm index 79fa246..bdc780c 100644 --- a/src/cuirass/http.scm +++ b/src/cuirass/http.scm @@ -40,7 +40,8 @@ #:use-module (web uri) #:use-module (fibers) #:use-module (fibers channels) - #:use-module ((rnrs bytevectors) #:select (utf8->string)) + #:use-module ((rnrs bytevectors) #:select (utf8->string + string->utf8)) #:use-module (sxml simple) #:use-module (cuirass templates) #:use-module (guix utils) @@ -246,19 +247,14 @@ Hydra format." "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd") (sxml->xml body port)))) - (define* (respond-file file - #:key name) + (define* (respond-file file) (let ((content-type (or (assoc-ref %file-mime-types (file-extension file)) '(application/octet-stream)))) (respond `((content-type . ,content-type) - ,@(if name - `((content-disposition - . (form-data (filename . ,name)))) - '())) - ;; FIXME: FILE is potentially big so it'd be better to not load - ;; it in memory and instead 'sendfile' it. - #:body (call-with-input-file file get-bytevector-all)))) + (content-disposition + . (form-data (filename . ,(basename file))))) + #:body (string->utf8 file)))) (define (respond-static-file path) ;; PATH is a list of path components @@ -273,10 +269,9 @@ Hydra format." (define (respond-gzipped-file file) ;; Return FILE with 'gzip' content-encoding. (respond `((content-type . (text/plain (charset . "UTF-8"))) - (content-encoding . (gzip))) - ;; FIXME: FILE is potentially big so it'd be better to not load - ;; it in memory and instead 'sendfile' it. - #:body (call-with-input-file file get-bytevector-all))) + (content-encoding . (gzip)) + (content-disposition . (form-data (filename . ,file)))) + #:body (string->utf8 file))) (define (respond-build-not-found build-id) (respond-json-with-error @@ -521,7 +516,7 @@ Hydra format." (('GET "download" id) (let ((path (db-get-build-product-path id))) - (respond-file path #:name (basename path)))) + (respond-file path))) (('GET "static" path ...) (respond-static-file path)) diff --git a/src/web/server/fiberized.scm b/src/web/server/fiberized.scm index 308b642..68ae132 100644 --- a/src/web/server/fiberized.scm +++ b/src/web/server/fiberized.scm @@ -37,6 +37,7 @@ #:use-module (web request) #:use-module (web response) #:use-module (web server) + #:use-module ((rnrs bytevectors) #:select (utf8->string)) #:use-module (ice-9 binary-ports) #:use-module (ice-9 match) #:use-module (fibers) @@ -92,6 +93,8 @@ ((0) (memq 'keep-alive (response-connection response))))) (else #f))))) +(define extend-response (@@ (web server) extend-response)) + (define (client-loop client have-request) ;; Always disable Nagle's algorithm, as we handle buffering ;; ourselves. @@ -119,9 +122,23 @@ #:headers '((content-length . 0))) #vu8())))) (lambda (response body) - (write-response response client) (when body - (put-bytevector client body)) + (let* ((headers (response-headers response)) + (file? (assq-ref headers 'content-disposition)) + (file (and file? (utf8->string body))) + (file-size (and file? (stat:size (stat file))))) + (cond + (file? + (call-with-input-file file + (lambda (port) + (write-response + (extend-response response 'content-length + file-size) + client) + (sendfile client port file-size)))) + (else + (write-response response client) + (put-bytevector client body))))) (force-output client) (if (and (keep-alive? response) (not (eof-object? (peek-char client)))) -- 2.26.0