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

Safe Haskell | None
Language | Haskell2010

Happstack.Server.FileServe

Contents

- Serving Functions
- Content-Type / Mime-Type
- Index Files
- Deprecated

Description

functions for serving static files from the disk

Synopsis

- data Browsing
  - = EnableBrowsing
  - | DisableBrowsing
- serveDirectory :: (WebMonad Response m, ServerMonad m, FilterMonad Response m, MonadIO m, MonadPlus m) => Browsing -> [FilePath] -> FilePath -> m Response
- serveFile :: (ServerMonad m, FilterMonad Response m, MonadIO m, MonadPlus m) => (FilePath -> m String) -> FilePath -> m Response
- serveFileFrom :: (ServerMonad m, FilterMonad Response m, MonadIO m, MonadPlus m) => FilePath -> (FilePath -> m String) -> FilePath -> m Response
- type MimeMap = Map String String
- mimeTypes :: MimeMap
- asContentType :: Monad m => String -> FilePath -> m String
- guessContentTypeM :: Monad m => MimeMap -> FilePath -> m String
- defaultIxFiles :: [FilePath]
- fileServe :: (WebMonad Response m, ServerMonad m, FilterMonad Response m, MonadIO m, MonadPlus m) => [FilePath] -> FilePath -> m Response

# Serving Functions

data Browsing Source #

see `serveDirectory`

Constructors

EnableBrowsing |
DisableBrowsing |

#### Instances

Instances details

Data Browsing Source # |

Instance details

Defined in Happstack.Server.FileServe.BuildingBlocks

Methods

gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> Browsing -> c Browsing Source #

gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c Browsing Source #

toConstr :: Browsing -> Constr Source #

dataTypeOf :: Browsing -> DataType Source #

dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c Browsing) Source #

dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Browsing) Source #

gmapT :: (forall b. Data b => b -> b) -> Browsing -> Browsing Source #

gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Browsing -> r Source #

gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Browsing -> r Source #

gmapQ :: (forall d. Data d => d -> u) -> Browsing -> [u] Source #

gmapQi :: Int -> (forall d. Data d => d -> u) -> Browsing -> u Source #

gmapM :: Monad m => (forall d. Data d => d -> m d) -> Browsing -> m Browsing Source #

gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Browsing -> m Browsing Source #

gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Browsing -> m Browsing Source #

Enum Browsing Source # |

Instance details

Defined in Happstack.Server.FileServe.BuildingBlocks

Methods

succ :: Browsing -> Browsing Source #

pred :: Browsing -> Browsing Source #

toEnum :: Int -> Browsing Source #

fromEnum :: Browsing -> Int Source #

enumFrom :: Browsing -> [Browsing] Source #

enumFromThen :: Browsing -> Browsing -> [Browsing] Source #

enumFromTo :: Browsing -> Browsing -> [Browsing] Source #

enumFromThenTo :: Browsing -> Browsing -> Browsing -> [Browsing] Source #

Read Browsing Source # |
Instance details Defined in Happstack.Server.FileServe.BuildingBlocks Methods readsPrec :: Int -> ReadS Browsing Source # readList :: ReadS [Browsing] Source # readPrec :: ReadPrec Browsing Source # readListPrec :: ReadPrec [Browsing] Source #
Show Browsing Source # |
Instance details Defined in Happstack.Server.FileServe.BuildingBlocks Methods showsPrec :: Int -> Browsing -> ShowS Source # show :: Browsing -> String Source # showList :: [Browsing] -> ShowS Source #
Eq Browsing Source # |
Instance details Defined in Happstack.Server.FileServe.BuildingBlocks Methods (==) :: Browsing -> Browsing -> Bool Source # (/=) :: Browsing -> Browsing -> Bool Source #
Ord Browsing Source # |

Instance details

Defined in Happstack.Server.FileServe.BuildingBlocks

Methods

compare :: Browsing -> Browsing -> Ordering Source #

(<) :: Browsing -> Browsing -> Bool Source #

(<=) :: Browsing -> Browsing -> Bool Source #

(>) :: Browsing -> Browsing -> Bool Source #

(>=) :: Browsing -> Browsing -> Bool Source #

max :: Browsing -> Browsing -> Browsing Source #

min :: Browsing -> Browsing -> Browsing Source #

serveDirectory Source #

Arguments

:: (WebMonad Response m, ServerMonad m, FilterMonad Response m, MonadIO m, MonadPlus m) |
=> Browsing | allow directory browsing
-> [FilePath] | index file names, in case the requested path is a directory
-> FilePath | file/directory to serve
-> m Response |

Serve files and directories from a directory and its subdirectories using `sendFile`.

Usage:

    serveDirectory EnableBrowsing ["index.html"] "path/to/files/on/disk"

If the requested path does not match a file or directory on the disk, then `serveDirectory` calls `mzero`.

If the requested path is a file then the file is served normally.

If the requested path is a directory, then the result depends on what the first two arguments to the function are.

The first argument controls whether directory browsing is enabled.

The second argument is a list of index files (such as index.html).

When a directory is requested, `serveDirectory` will first try to find one of the index files (in the order they are listed). If that fails, it will show a directory listing if `EnableBrowsing` is set, otherwise it will return `forbidden "Directory index forbidden"`.

Here is an explicit list of all the possible outcomes when the argument is a (valid) directory:

``DisableBrowsing`, empty index file list`

This will always return, forbidden "Directory index forbidden"

``DisableBrowsing`, non-empty index file list`

1. If an index file is found it will be shown.
2. Otherwise returns, forbidden "Directory index forbidden"

``EnableBrowsing`, empty index file list`

Always shows a directory index.

``EnableBrowsing`, non-empty index file list`

1. If an index file is found it will be shown
2. Otherwise shows a directory index

see also: `defaultIxFiles`, `serveFile`

serveFile Source #

Arguments

:: (ServerMonad m, FilterMonad Response m, MonadIO m, MonadPlus m) |
=> (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. The name of the file being served is specified explicity. It is not derived automatically from the `Request` url.

example 1:

Serve as a specific content-type:

    serveFile (asContentType "image/jpeg") "/srv/data/image.jpg"

example 2:

Serve guessing the content-type from the extension:

    serveFile (guessContentTypeM mimeTypes) "/srv/data/image.jpg"

If the specified path does not exist or is not a file, this function will return `mzero`.

WARNING: No security checks are performed.

NOTE: alias for `serveFileUsing` `filePathSendFile`

serveFileFrom Source #

Arguments

:: (ServerMonad m, FilterMonad Response m, MonadIO m, MonadPlus m) |
=> FilePath | directory wherein served files must be contained
-> (FilePath -> m String) | function for determining content-type of file. Typically `asContentType` or `guessContentTypeM`
-> FilePath | path to the file to serve
-> m Response |

Like `serveFile`, but uses `combineSafe` to prevent directory traversal attacks when the path to the file is supplied by the user.

# Content-Type / Mime-Type

type MimeMap = Map String String Source #

a `Map` from file extensions to content-types

example:

    myMimeMap :: MimeMap
    myMimeMap = Map.fromList [("gz","application/x-gzip"), ... ]

see also: `mimeTypes`

mimeTypes :: MimeMap Source #

Ready collection of common mime types. Except for the first two entries, the mappings come from http://svn.apache.org/viewvc/httpd/httpd/branches/2.4.x/docs/conf/mime.types?view=co

asContentType Source #

Arguments

:: Monad m |
=> String | the content-type to return
-> FilePath |
-> m String |

returns a specific content type, completely ignoring the `FilePath` argument.

Use this with `serveFile` if you want to explicitly specify the content-type.

see also: `guessContentTypeM`, `serveFile`

guessContentTypeM :: Monad m => MimeMap -> FilePath -> m String Source #

try to guess the content-type of a file based on its extension

defaults to "application/octet-stream" if no match was found.

Useful as an argument to `serveFile`

see also: `guessContentType`, `serveFile`

# Index Files

defaultIxFiles :: [FilePath] Source #

a list of common index files. Specifically: `index.html`, `index.xml`, `index.gif`

Typically used as an argumebnt to `serveDiretory`.

# Deprecated

fileServe Source #

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 |

Deprecated: use serveDirectory instead.

Serve files from a directory and its subdirectories using `sendFile`.

Usage:

    fileServe ["index.html"] "path/to/files/on/disk"

`fileServe` does not support directory browsing. See `serveDirectory`

DEPRECATED: use `serveDirectory` instead.

Note:

The list of index files `["index.html"]` is only used to determine what file to show if the user requests a directory. You *do not* need to explicitly list all the files you want to serve.