guile-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [FEATURE Request] Built-in server should support a thunk as body


From: Nala Ginrut
Subject: Re: [FEATURE Request] Built-in server should support a thunk as body
Date: Mon, 25 Aug 2014 22:52:41 +0800

hi David!

2014年8月25日 下午8:16于 "David Thompson" <address@hidden>写道:
>
> 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


reply via email to

[Prev in Thread] Current Thread [Next in Thread]