[web-devel] Content-Length on sendFile

Max Cantor mxcantor at gmail.com
Wed Jun 15 02:57:52 CEST 2011


A simple, can be done immediately would be to put a note in the Haddocks for
sendFile: "warning, does not set the content-length http header which will
break some proxies such as amazons ELB.  if you are using a proxy, please
set the header manually.

Does hFileSize in System.IO not work on windows?  I'm not at the office now,
but IIRC, this is how we fixed it:

sendFileWithLength ct fp = do
  hSize <- liftIO $ withFile fp ReadMode hFileSize  -- All System.IO
  setHeader "Content-Length" (fromString $ show hSize)
  sendFile ct fp

i think this could be combined into one line if you wanna be slick, but dont
have GHC here to check..

sendFileWithLength ct fp =
   (liftIO $ withFile fp ReadMode hFileSize) >>= setHeader "Content-Length"
. (fromString . show) >> sendFile ct fp


I know i'm kind of abusing fromString here, but it was 2 am and we were
tired..


On Wed, Jun 15, 2011 at 3:10 AM, David Pollak <feeder.of.the.bears at gmail.com
> wrote:

>
>
> On Tue, Jun 14, 2011 at 12:06 PM, Michael Snoyman <michael at snoyman.com>wrote:
>
>> On Tue, Jun 14, 2011 at 9:55 PM, David Pollak
>> <feeder.of.the.bears at gmail.com> wrote:
>> >
>> >
>> > On Tue, Jun 14, 2011 at 11:50 AM, Michael Snoyman <michael at snoyman.com>
>> > wrote:
>> >>
>> >> I think the reason we didn't include the header is because, when I
>> >> wrote it, I didn't know of a cross-platform way to get file sizes. Now
>> >> that I know about unix-compat, this seems like a reasonable thing to
>> >> add. Anyone know a reason we *shouldn't* do this?
>> >
>> > In Lift, we do not automatically populate the Content-Length for generic
>> > BLOB responses because if the response is streaming, then we have to
>> > strictly evaluate the stream to determine the length.  For returning
>> large
>> > items (e.g., a 2 GB AVI), this can cause issues.  We break the Response
>> > types into "In Memory" which are strict and we include the
>> Content-Length
>> > header, but for responses that are not strict, it's up to the coder.
>> >
>> > My 2 cents.
>>
>> Nubis was referring specifically to sending files. Yesod has a similar
>> breakdown where some datatypes (strict ByteStrings for instance) have
>> a content-length attached, while others (lazy ByteStrings) do not. We
>> achieve this via the optional Int argument in the ContentBuilder
>> constructor[1]. Various instances of ToContent simply handle this
>> differently.
>>
>> In the case of files, we would either use the length specified by the
>> FilePart parameter, or simply get the file size from the file system.
>>
>
> Sorry for the newbie mistake and thanks for correction me!
>
> Rock on.
>
>
>>
>> Michael
>>
>> [1]
>> http://hackage.haskell.org/packages/archive/yesod-core/0.8.2/doc/html/Yesod-Content.html
>>
>> >>
>> >> Michael
>> >>
>> >> On Tue, Jun 14, 2011 at 8:16 PM, Nubis <nubis at woobiz.com.ar> wrote:
>> >> > Hi guys,
>> >> > Just wanted to give you a heads up that yesod's sendFile is not
>> setting
>> >> > the
>> >> > Content-Length header, which 'breaks' some proxies and load
>> balancers.
>> >> > (in
>> >> > our case, amazon elastic load balancer).
>> >> > I don't know if that's by design, I'm not 100% sure but not setting
>> the
>> >> > header would be only useful for 'long pooling' of binary files for
>> doing
>> >> > some sort of hackish http streaming, which I don't think is yesod's
>> >> > purpose
>> >> > anyways :)
>> >> > A workaround is setting the Content-Length header before calling
>> >> > sendFile, I
>> >> > guess it may be something easy to add, I was going to propose a patch
>> >> > that
>> >> > just wraps the current implementation in a do block that sets the
>> >> > header,
>> >> > but then I noticed a number of optimizations being made that hint
>> there
>> >> > should be a 'better' way.
>> >> >
>> >> > cheers!
>> >> > ----nubis :)
>> >> >
>> >> > _______________________________________________
>> >> > web-devel mailing list
>> >> > web-devel at haskell.org
>> >> > http://www.haskell.org/mailman/listinfo/web-devel
>> >> >
>> >> >
>> >>
>> >> _______________________________________________
>> >> web-devel mailing list
>> >> web-devel at haskell.org
>> >> http://www.haskell.org/mailman/listinfo/web-devel
>> >
>> >
>> >
>> > --
>> > Lift, the simply functional web framework http://liftweb.net
>> > Simply Lift http://simply.liftweb.net
>> > Follow me: http://twitter.com/dpp
>> > Blog: http://goodstuff.im
>> >
>> >
>>
>
>
>
> --
> Lift, the simply functional web framework http://liftweb.net
> Simply Lift http://simply.liftweb.net
> Follow me: http://twitter.com/dpp
> Blog: http://goodstuff.im
>
>
> _______________________________________________
> web-devel mailing list
> web-devel at haskell.org
> http://www.haskell.org/mailman/listinfo/web-devel
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/web-devel/attachments/20110615/be812dec/attachment.htm>


More information about the web-devel mailing list