guile-user
[Top][All Lists]
Advanced

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

Re: Learning Guile web. Stuck on returning an image.


From: Neil Jerram
Subject: Re: Learning Guile web. Stuck on returning an image.
Date: Mon, 26 May 2014 10:05:17 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.4 (gnu/linux)

Martyn Smith <address@hidden> writes:

> Getting to the point of the email -- I am a bit confused how I would return a
> jpg image. For example: -
> <img src="/load-image?id=1234" />
>
> The "1234" tells me which file to load and return.
>
> Being primarily a .NET developer, I am struggling to understand how to achieve
> such goal in guile. I have tried things like (read-file "/location/to/
> image.jpg" "r") but no luck. Also tried to understand converting to bytes in
> the documentation but again... no luck. Always getting a 500 error. Yes, I 
> have
> included (content-type . (image/jpg)) etc.
>
> Can anyone give me a heads up on how to do such thing? How would I open the 
> jpg
> file? How would it be returned?

I recently had the same problem, and eventually decided that it would be
better to use Guile only for my dynamic content, with a standard web
server for the static content.  For the latter I chose lighttpd, and I
can provide more details of my setup if that would be helpful.

But what specifically was the problem?  My handler for an image file
(and other static content) looked like this:

  (lambda (request request-body uri)
    (let* ((fn (apply string-append
                      static-root
                      (map (lambda (name)
                             (string-append "/" name))
                           uri)))
           (content (with-input-from-file fn read-string))
           (content-type (cond ((string-match "\\.svg" fn)
                                '(image/svg+xml))
                               ((string-match "\\.html?" fn)
                                '(text/html))
                               ((string-match "\\.jpe?g" fn)
                                '(image/jpeg))
                               ((string-match "\\.png" fn)
                                '(image/png))
                               ((string-match "\\.css" fn)
                                '(text/css))
                               ((string-match "\\.js" fn)
                                '(application/javascript))
                               (else
                                '(text/plain)))))
      (trc 'content-type content-type 'length (string-length content))
      (values `((content-type . ,content-type))
              content)))

The problem here that 'content' contains binary data that can't be
returned as is in an HTTP response.  It needs to be encoded in one of
the HTTP-supported encodings, and a corresponding Content-Encoding
header added to the response.

I think I investigated a little whether Guile's (web ...) modules could
do this, or could easily be enhanced, but decided instead on the
solution described above.

Regards,
        Neil



reply via email to

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