happstack-server-0.5.0.2: Web related tools and services.

Happstack.Server.HTTP.FileServe

Contents

Description

File Serving functions

Synopsis

Content-Type / Mime-Type

mimeTypes :: MimeMapSource

Ready collection of common mime types. Except for the first two entries, the mappings come from an Ubuntu 8.04 etcmime.types file.

Low-Level

sendFileResponseSource

Arguments

:: String

content-type string

-> FilePath

file path for content to send

-> Maybe (CalendarTime, Request)

mod-time for the handle (MUST NOT be later than server's time of message origination), incoming request (used to check for if-modified-since header)

-> Integer

offset into Handle

-> Integer

number of bytes to send

-> Response 

Use sendFile to send the contents of a Handle

lazyByteStringResponseSource

Arguments

:: String

content-type string (e.g. "text/plain; charset=utf-8")

-> ByteString

lazy bytestring content to send

-> Maybe (CalendarTime, Request)

mod-time for the bytestring, incoming request (used to check for if-modified-since header)

-> Integer

offset into the bytestring

-> Integer

number of bytes to send (offset + count must be less than or equal to the length of the bytestring)

-> Response 

Send the contents of a Lazy ByteString

strictByteStringResponseSource

Arguments

:: String

content-type string (e.g. "text/plain; charset=utf-8")

-> ByteString

lazy bytestring content to send

-> Maybe (CalendarTime, Request)

mod-time for the bytestring, incoming request (used to check for if-modified-since header)

-> Integer

offset into the bytestring

-> Integer

number of bytes to send (offset + count must be less than or equal to the length of the bytestring)

-> Response 

Send the contents of a Lazy ByteString

filePathSendFileSource

Arguments

:: (ServerMonad m, MonadIO m) 
=> String

content-type string

-> FilePath

path to file on disk

-> m Response 

Send the specified file with the specified mime-type using sendFile()

NOTE: assumes file exists and is readable by the server. See serveFileUsing.

WARNING: No security checks are performed.

filePathLazySource

Arguments

:: (ServerMonad m, MonadIO m) 
=> String

content-type string

-> FilePath

path to file on disk

-> m Response 

Send the specified file with the specified mime-type using Lazy ByteStrings

NOTE: assumes file exists and is readable by the server. See serveFileUsing.

WARNING: No security checks are performed.

filePathStrictSource

Arguments

:: (ServerMonad m, MonadIO m) 
=> String

content-type string

-> FilePath

path to file on disk

-> m Response 

Send the specified file with the specified mime-type using Lazy ByteStrings

NOTE: assumes file exists and is readable by the server. See serveFileUsing.

WARNING: No security checks are performed.

High-Level

Serving a single file

serveFileUsingSource

Arguments

:: (ServerMonad m, FilterMonad Response m, MonadIO m, MonadPlus m) 
=> (String -> FilePath -> m Response)

typically filePathSendFile, filePathLazy, or filePathStrict

-> (FilePath -> m String)

function for determining content-type of file. Typically asContentType or guessContentTypeM

-> FilePath

path to the file to serve

-> m Response 

Serve a single, specified file.

example 1:

Serve using sendfile() and the specified content-type

 serveFileUsing filePathSendFile (asContentType "image/jpeg") "/srv/data/image.jpg"

example 2:

Serve using a lazy ByteString and the guess the content-type from the extension

 serveFileUsing filePathLazy (guessContentTypeM mimeTypes) "/srv/data/image.jpg"

WARNING: No security checks are performed.

Serving files from a directory

fileServe'Source

Arguments

:: (WebMonad Response m, ServerMonad m, FilterMonad Response m, MonadIO m, MonadPlus m) 
=> (String -> FilePath -> m Response)

function which takes a content-type and filepath and generates a response (typically filePathSendFile, filePathLazy, or filePathStrict)

-> (FilePath -> m String)

function which returns the mime-type for FilePath

-> [FilePath]

index file names, in case the requested path is a directory

-> FilePath

file/directory to serve

-> m Response 

Serve files from a directory and its subdirectories (parameterizable version)

Parameterize this function to create functions like, fileServe, fileServeLazy, and fileServeStrict

You supply:

  1. a low-level function which takes a content-type and FilePath and generates a Response 2. a function which determines the content-type from the FilePath 3. a list of all the default index files

NOTE: unlike fileServe, there are no index files by default. See defaultIxFiles.

fileServeSource

Arguments

:: (WebMonad Response m, ServerMonad m, FilterMonad Response m, MonadIO m, MonadPlus m) 
=> [FilePath]

index file names, in case the requested path is a directory

-> FilePath

file/directory to serve

-> m Response 

Serve files from a directory and its subdirectories (sendFile version). Should perform much better than its predecessors.

fileServeLazySource

Arguments

:: (WebMonad Response m, ServerMonad m, FilterMonad Response m, MonadIO m, MonadPlus m) 
=> [FilePath]

index file names, in case the requested path is a directory

-> FilePath

file/directory to serve

-> m Response 

Serve files from a directory and its subdirectories (lazy ByteString version).

May leak file handles.

fileServeStrictSource

Arguments

:: (WebMonad Response m, ServerMonad m, FilterMonad Response m, MonadIO m, MonadPlus m) 
=> [FilePath]

index file names, in case the next argument is a directory

-> FilePath

file/directory to serve

-> m Response 

Serve files from a directory and its subdirectories (strict ByteString version).

Other

blockDotFiles :: (Request -> IO Response) -> Request -> IO ResponseSource

Prevents files of the form '.foo' or 'bar/.foo' from being served

isDot :: String -> BoolSource

Returns True if the given String either starts with a . or is of the form foo/.bar, e.g. the typical *nix convention for hidden files.