From: Nala Ginrut <nalaginrut@gmail.com>
To: David Thompson <dthompson2@worcester.edu>
Cc: guile-devel <guile-devel@gnu.org>
Subject: Re: [FEATURE Request] Built-in server should support a thunk as body
Date: Mon, 25 Aug 2014 22:52:41 +0800 [thread overview]
Message-ID: <CAPjoZoegrbM2Q5c-s0T95BVv_to75QNkoVdkn0DbWNP2CDdg3w@mail.gmail.com> (raw)
In-Reply-To: <877g1wsr10.fsf@izanagi.i-did-not-set--mail-host-address--so-tickle-me>
[-- Attachment #1: Type: text/plain, Size: 2196 bytes --]
hi David!
2014年8月25日 下午8:16于 "David Thompson" <dthompson2@worcester.edu>写道:
>
> Hi Nala,
>
> I'm currently writing a web application using Guile's built-in HTTP
> server. To serve static files, I build a response like:
>
> (values `((content-type . (text/css)))
> (call-with-input-file file-name get-bytevector-all))
>
> Since the response body can be a string, bytevector, or lambda, I tried
> using sendfile:
>
> (values `((content-type . (text/css)))
> (lambda (output)
> (call-with-input-file file-name
> (lambda (input)
> (sendfile output input file-size)))))
>
> However, it didn't work because 'output' is a string port, but sendfile
> requires file ports.
>
You can't use sendfile here because the lambda you passed in will be
expected to output a string.
But we don't need any content string, since the content should be handled
by sendfile automatically.
> Does your proposal give us access to a file port that we can write to?
> I'm still learning to use Guile's HTTP modules and serving static files
> was something that confused me for awhile.
>
My proposal is to pass a thunk, say, a lambda with no arguments.
E.g:
(define (your-func req filename)
(define in (open-input-file filename))
(define size (stat:size (stat filename)))
(define my-response ...)
(values my-response
(lambda ()
(sendfile
(request-port req)
in size))))
This func could be called in handler of run-server.
One have to detect file size with stat and pass it as Content-Length in
#:headers of my-request, I ignored it here, but I think you could
understand.(Sorry but I'm typing these code on my smart phone).
The thunk will be called in http write handler in my patch.
With this feature, we can use sendfile for static file without breaking the
interface.
PS: If you want to build web app quickly, I'll recommend Artanis :-)
> --
> David Thompson
> Web Developer - Free Software Foundation - http://fsf.org
> GPG Key: 0FF1D807
> Support the FSF: https://fsf.org/donate
[-- Attachment #2: Type: text/html, Size: 2874 bytes --]
next prev parent reply other threads:[~2014-08-25 14:52 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-08-25 7:25 [FEATURE Request] Built-in server should support a thunk as body Nala Ginrut
2014-08-25 12:16 ` David Thompson
2014-08-25 14:52 ` Nala Ginrut [this message]
2014-08-26 5:30 ` Nala Ginrut
2014-08-26 6:38 ` Nala Ginrut
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=CAPjoZoegrbM2Q5c-s0T95BVv_to75QNkoVdkn0DbWNP2CDdg3w@mail.gmail.com \
--to=nalaginrut@gmail.com \
--cc=dthompson2@worcester.edu \
--cc=guile-devel@gnu.org \
/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).