File Serving functions
- type MimeMap = Map String String
- mimeTypes :: MimeMap
- asContentType :: Monad m => String -> FilePath -> m String
- guessContentType :: MimeMap -> FilePath -> Maybe String
- guessContentTypeM :: Monad m => MimeMap -> FilePath -> m String
- sendFileResponse :: String -> FilePath -> Maybe (CalendarTime, Request) -> Integer -> Integer -> Response
- lazyByteStringResponse :: String -> ByteString -> Maybe (CalendarTime, Request) -> Integer -> Integer -> Response
- strictByteStringResponse :: String -> ByteString -> Maybe (CalendarTime, Request) -> Integer -> Integer -> Response
- filePathSendFile :: (ServerMonad m, MonadIO m) => String -> FilePath -> m Response
- filePathLazy :: (ServerMonad m, MonadIO m) => String -> FilePath -> m Response
- filePathStrict :: (ServerMonad m, MonadIO m) => String -> FilePath -> m Response
- serveFile :: (ServerMonad m, FilterMonad Response m, MonadIO m, MonadPlus m) => (FilePath -> m String) -> FilePath -> m Response
- serveFileUsing :: (ServerMonad m, FilterMonad Response m, MonadIO m, MonadPlus m) => (String -> FilePath -> m Response) -> (FilePath -> m String) -> FilePath -> m Response
- fileServe' :: (WebMonad Response m, ServerMonad m, FilterMonad Response m, MonadIO m, MonadPlus m) => (String -> FilePath -> m Response) -> (FilePath -> m String) -> [FilePath] -> FilePath -> m Response
- fileServe :: (WebMonad Response m, ServerMonad m, FilterMonad Response m, MonadIO m, MonadPlus m) => [FilePath] -> FilePath -> m Response
- fileServeLazy :: (WebMonad Response m, ServerMonad m, FilterMonad Response m, MonadIO m, MonadPlus m) => [FilePath] -> FilePath -> m Response
- fileServeStrict :: (WebMonad Response m, ServerMonad m, FilterMonad Response m, MonadIO m, MonadPlus m) => [FilePath] -> FilePath -> m Response
- blockDotFiles :: (Request -> IO Response) -> Request -> IO Response
- defaultIxFiles :: [String]
- doIndex :: (ServerMonad m, FilterMonad Response m, MonadIO m, MonadPlus m) => [String] -> MimeMap -> String -> m Response
- doIndex' :: (ServerMonad m, FilterMonad Response m, MonadIO m, MonadPlus m) => (String -> FilePath -> m Response) -> (FilePath -> m String) -> [String] -> String -> m Response
- doIndexLazy :: (ServerMonad m, FilterMonad Response m, MonadIO m, MonadPlus m) => [String] -> MimeMap -> String -> m Response
- doIndexStrict :: (ServerMonad m, FilterMonad Response m, MonadIO m, MonadPlus m) => [String] -> MimeMap -> String -> m Response
- errorwrapper :: (MonadIO m, MonadPlus m, FilterMonad Response m) => String -> String -> m Response
- fileNotFound :: (Monad m, FilterMonad Response m) => FilePath -> m Response
- isDot :: String -> Bool
Content-Type / Mime-Type
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
:: 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
:: String | content-type string (e.g. |
-> 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
:: String | content-type string (e.g. |
-> 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
:: (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.
:: (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.
:: (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
serveFile :: (ServerMonad m, FilterMonad Response m, MonadIO m, MonadPlus m) => (FilePath -> m String) -> FilePath -> m ResponseSource
Alias for serveFileUsing
filePathSendFile
:: (ServerMonad m, FilterMonad Response m, MonadIO m, MonadPlus m) | |
=> (String -> FilePath -> m Response) | typically |
-> (FilePath -> m String) | function for determining content-type of file. Typically |
-> 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
:: (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 |
-> (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:
- a low-level function which takes a content-type and
FilePath
and generates a Response 2. a function which determines the content-type from theFilePath
3. a list of all the default index files
NOTE: unlike fileServe, there are no index files by default. See defaultIxFiles
.
:: (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.
:: (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.
:: (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
doIndex :: (ServerMonad m, FilterMonad Response m, MonadIO m, MonadPlus m) => [String] -> MimeMap -> String -> m ResponseSource
doIndex' :: (ServerMonad m, FilterMonad Response m, MonadIO m, MonadPlus m) => (String -> FilePath -> m Response) -> (FilePath -> m String) -> [String] -> String -> m ResponseSource
doIndexLazy :: (ServerMonad m, FilterMonad Response m, MonadIO m, MonadPlus m) => [String] -> MimeMap -> String -> m ResponseSource
doIndexStrict :: (ServerMonad m, FilterMonad Response m, MonadIO m, MonadPlus m) => [String] -> MimeMap -> String -> m ResponseSource
errorwrapper :: (MonadIO m, MonadPlus m, FilterMonad Response m) => String -> String -> m ResponseSource
fileNotFound :: (Monad m, FilterMonad Response m) => FilePath -> m ResponseSource
return a simple File not found 404 page.