Snap.Util.FileServe

snap-core-0.2.16: Snap: A Haskell Web Framework (Core)

Snap.Util.FileServe

Description

Contains web handlers to serve files from a directory.

Synopsis

- getSafePath :: Snap FilePath
- fileServe :: FilePath -> Snap ()
- fileServe' :: MimeMap -> FilePath -> Snap ()
- fileServeSingle :: FilePath -> Snap ()
- fileServeSingle' :: ByteString -> FilePath -> Snap ()
- defaultMimeTypes :: MimeMap
- type MimeMap = Map FilePath ByteString

# Documentation

getSafePath :: Snap FilePath

Gets a path from the `Request` using `rqPathInfo` and makes sure it is safe to use for opening files. A path is safe if it is a relative path and has no .. elements to escape the intended directory structure.

fileServe

Arguments

:: FilePath | root directory
-> Snap () |

Serves files out of the given directory. The relative path given in `rqPathInfo` is searched for the given file, and the file is served with the appropriate mime type if it is found. Absolute paths and "`..`" are prohibited to prevent files from being served from outside the sandbox.

Uses `defaultMimeTypes` to determine the `Content-Type` based on the file's extension.

fileServe'

Arguments

:: MimeMap | MIME type mapping
-> FilePath | root directory
-> Snap () |

Same as `fileServe`, with control over the MIME mapping used.

fileServeSingle

Arguments

:: FilePath | path to file
-> Snap () |

Serves a single file specified by a full or relative path. The path restrictions on fileServe don't apply to this function since the path is not being supplied by the user.

fileServeSingle'

Arguments

:: ByteString | MIME type mapping
-> FilePath | path to file
-> Snap () |

Same as `fileServeSingle`, with control over the MIME mapping used.

defaultMimeTypes :: MimeMap

The default set of mime type mappings we use when serving files. Its value:

     Map.fromList [
       ( ".asc"     , "text/plain"                        ),
       ( ".asf"     , "video/x-ms-asf"                    ),
       ( ".asx"     , "video/x-ms-asf"                    ),
       ( ".avi"     , "video/x-msvideo"                   ),
       ( ".bz2"     , "application/x-bzip"                ),
       ( ".c"       , "text/plain"                        ),
       ( ".class"   , "application/octet-stream"          ),
       ( ".conf"    , "text/plain"                        ),
       ( ".cpp"     , "text/plain"                        ),
       ( ".css"     , "text/css"                          ),
       ( ".cxx"     , "text/plain"                        ),
       ( ".dtd"     , "text/xml"                          ),
       ( ".dvi"     , "application/x-dvi"                 ),
       ( ".gif"     , "image/gif"                         ),
       ( ".gz"      , "application/x-gzip"                ),
       ( ".hs"      , "text/plain"                        ),
       ( ".htm"     , "text/html"                         ),
       ( ".html"    , "text/html"                         ),
       ( ".jar"     , "application/x-java-archive"        ),
       ( ".jpeg"    , "image/jpeg"                        ),
       ( ".jpg"     , "image/jpeg"                        ),
       ( ".js"      , "text/javascript"                   ),
       ( ".log"     , "text/plain"                        ),
       ( ".m3u"     , "audio/x-mpegurl"                   ),
       ( ".mov"     , "video/quicktime"                   ),
       ( ".mp3"     , "audio/mpeg"                        ),
       ( ".mpeg"    , "video/mpeg"                        ),
       ( ".mpg"     , "video/mpeg"                        ),
       ( ".ogg"     , "application/ogg"                   ),
       ( ".pac"     , "application/x-ns-proxy-autoconfig" ),
       ( ".pdf"     , "application/pdf"                   ),
       ( ".png"     , "image/png"                         ),
       ( ".ps"      , "application/postscript"            ),
       ( ".qt"      , "video/quicktime"                   ),
       ( ".sig"     , "application/pgp-signature"         ),
       ( ".spl"     , "application/futuresplash"          ),
       ( ".swf"     , "application/x-shockwave-flash"     ),
       ( ".tar"     , "application/x-tar"                 ),
       ( ".tar.bz2" , "application/x-bzip-compressed-tar" ),
       ( ".tar.gz"  , "application/x-tgz"                 ),
       ( ".tbz"     , "application/x-bzip-compressed-tar" ),
       ( ".text"    , "text/plain"                        ),
       ( ".tgz"     , "application/x-tgz"                 ),
       ( ".torrent" , "application/x-bittorrent"          ),
       ( ".txt"     , "text/plain"                        ),
       ( ".wav"     , "audio/x-wav"                       ),
       ( ".wax"     , "audio/x-ms-wax"                    ),
       ( ".wma"     , "audio/x-ms-wma"                    ),
       ( ".wmv"     , "video/x-ms-wmv"                    ),
       ( ".xbm"     , "image/x-xbitmap"                   ),
       ( ".xml"     , "text/xml"                          ),
       ( ".xpm"     , "image/x-xpixmap"                   ),
       ( ".xwd"     , "image/x-xwindowdump"               ),
       ( ".zip"     , "application/zip"                   ) ]

type MimeMap = Map FilePath ByteString

A type alias for MIME type