unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Ricardo Wurmus <rekado@elephly.net>
To: Zelphir Kaltstahl <zelphirkaltstahl@posteo.de>
Cc: guile-user@gnu.org
Subject: Re: Web development
Date: Fri, 04 Sep 2020 22:25:43 +0200	[thread overview]
Message-ID: <87y2lpe1bs.fsf@elephly.net> (raw)
In-Reply-To: <28851dc6-d0a2-4b17-cf0c-c14571f02110@posteo.de>


Zelphir Kaltstahl <zelphirkaltstahl@posteo.de> writes:

>>> (2) What do you use to serve static files (securely)? If you use Guile's
>>> web server, how exactly do you do it? Do you have the code somewhere?
>> I configure an assets directory and define a procedure that sanitizes
>> the requested file name to serve it from that directory.  I don’t do
>> much with files so I don’t usually do anything other than
>>
>>     (call-with-input-file file-name get-bytevector-all)
>>
>> for the body of the response.  But if I had to send large files I’d use
>> “sendfile” directly.
>
> Don't you need to also check what kind of file type it is and select the
> appropriate MIME type for answering the request?

Yes, you need to send along the correct MIME type.  In my projects I
know the MIME type of all files I’ll send ahead of time, so I can get
away with looking up the file extension in an alist of extensions to
MIME types.

Something like that:

--8<---------------cut here---------------start------------->8---
(define (render-static-file root path)
  ;; PATH is a list of path components
  (let ((file-name (string-join (cons* root path) "/")))
    (if (and (not (any (cut string-contains <> "..") path))
             (file-exists? file-name)
             (not (directory? file-name)))
        (list `((content-type . ,(assoc-ref file-mime-types
                                            (file-extension file-name))))
              (call-with-input-file file-name get-bytevector-all))
        (not-found (build-uri 'http
                              #:host (assoc-ref %config 'host)
                              #:port (assoc-ref %config 'port)
                              #:path (string-join path "/" 'prefix))))))
--8<---------------cut here---------------end--------------->8---

There’s probably a better way and a fail-safe way to do this, but this
has worked fine for me.

> And how do you use sendfile directly? (Code example for this would also
> be great to see.)

I’ve got no example for that in my projects, but “guix publish” provides
an implementation of “http-write” that uses sendfile.  See
guix/scripts/publish.scm in the Guix source tree.

> Is it still necessary to sanitize the requested file names, when an HTTP
> server handles requests for assets before the Guile application is hit?

I’d still do that because I’d want the application to never do the wrong
thing, even when the deployment details change.

-- 
Ricardo



  reply	other threads:[~2020-09-04 20:25 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-09-04 15:43 Web development Zelphir Kaltstahl
2020-09-04 15:56 ` [EXT] " Thompson, David
2020-09-04 20:55   ` Zelphir Kaltstahl
2020-09-05  6:29     ` Joshua Branson via General Guile related discussions
2020-09-04 16:44 ` Ricardo Wurmus
2020-09-04 20:09   ` Zelphir Kaltstahl
2020-09-04 20:25     ` Ricardo Wurmus [this message]
  -- strict thread matches above, loose matches on Subject: below --
2020-09-07  9:35 tantalum
2020-09-07 19:53 ` Zelphir Kaltstahl
2019-10-20  6:10 Diversification [ branched from Re: conflicts in the gnu project now affect guile] Todor Kondić
2019-10-20  6:14 ` John Cowan
2019-10-23  6:16   ` Amirouche Boubekki
2019-10-23  6:48     ` pelzflorian (Florian Pelz)
2019-10-23 10:37       ` Chris Vine
2019-10-23 11:25         ` pelzflorian (Florian Pelz)
2019-10-23 12:33           ` pelzflorian (Florian Pelz)
2019-10-23 19:19             ` Zelphir Kaltstahl
2020-09-05  6:15               ` Joshua Branson via General Guile related discussions
2020-09-05 11:50                 ` Web development Zelphir Kaltstahl
2020-09-05 13:09                   ` Ricardo Wurmus

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87y2lpe1bs.fsf@elephly.net \
    --to=rekado@elephly.net \
    --cc=guile-user@gnu.org \
    --cc=zelphirkaltstahl@posteo.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).