-- Hoogle documentation, generated by Haddock
-- See Hoogle, http://www.haskell.org/hoogle/

-- | WAI application for static serving
--   
--   API docs and the README are available at
--   http://www.stackage.org/package/wai-app-static.
@package wai-app-static
@version 3.1.9

module WaiAppStatic.Types

-- | An individual component of a path, or of a filepath.
--   
--   This is the core type used by wai-app-static for doing lookups. It
--   provides a smart constructor to avoid the possibility of constructing
--   unsafe path segments (though unsafeToPiece can get around
--   that as necessary).
--   
--   Individual file lookup backends must know how to convert from a
--   Piece to their storage system.
data Piece

-- | Smart constructor for a Piece. Won't allow unsafe components,
--   such as pieces beginning with a period or containing a slash. This
--   will, however, allow null pieces.
toPiece :: Text -> Maybe Piece
fromPiece :: Piece -> Text

-- | Construct a Piece without input validation.
unsafeToPiece :: Text -> Piece

-- | Request coming from a user. Corresponds to pathInfo.
--   
--   The root path is the empty list.
type Pieces = [Piece]

-- | Call toPiece on a list.
--   
--   
--   toPieces = mapM toPiece
--   
toPieces :: [Text] -> Maybe Pieces

-- | Values for the max-age component of the cache-control response header.
data MaxAge

-- | no cache-control set
NoMaxAge :: MaxAge

-- | set to the given number of seconds
MaxAgeSeconds :: Int -> MaxAge

-- | essentially infinite caching; in reality, probably one year
MaxAgeForever :: MaxAge

-- | set cache-control to no-store @since 3.1.8
NoStore :: MaxAge

-- | set cache-control to no-cache @since 3.1.9
NoCache :: MaxAge

-- | Just the name of a folder.
type FolderName = Piece

-- | Represent contents of a single folder, which can be itself either a
--   file or a folder.
data Folder
Folder :: [Either FolderName File] -> Folder
[folderContents] :: Folder -> [Either FolderName File]

-- | Information on an individual file.
data File
File :: Integer -> (Status -> ResponseHeaders -> Response) -> Piece -> IO (Maybe ByteString) -> Maybe EpochTime -> File

-- | Size of file in bytes
[fileGetSize] :: File -> Integer

-- | How to construct a WAI response for this file. Some files are stored
--   on the filesystem and can use ResponseFile, while others are
--   stored in memory and should use ResponseBuilder.
[fileToResponse] :: File -> Status -> ResponseHeaders -> Response

-- | Last component of the filename.
[fileName] :: File -> Piece

-- | Calculate a hash of the contents of this file, such as for etag.
[fileGetHash] :: File -> IO (Maybe ByteString)

-- | Last modified time, used for both display in listings and
--   if-modified-since.
[fileGetModified] :: File -> Maybe EpochTime

-- | Result of looking up a file in some storage backend.
--   
--   The lookup is either a file or folder, or does not exist.
data LookupResult
LRFile :: File -> LookupResult
LRFolder :: Folder -> LookupResult
LRNotFound :: LookupResult

-- | How to construct a directory listing page for the given request path
--   and the resulting folder.
type Listing = Pieces -> Folder -> IO Builder

-- | All of the settings available to users for tweaking wai-app-static.
--   
--   Note that you should use the settings type approach for modifying
--   values. See http://www.yesodweb.com/book/settings-types for
--   more information.
data StaticSettings
StaticSettings :: (Pieces -> IO LookupResult) -> (File -> IO MimeType) -> [Piece] -> Maybe Listing -> MaxAge -> (Pieces -> ByteString -> ByteString) -> Bool -> Bool -> Bool -> Maybe Application -> StaticSettings

-- | Lookup a single file or folder. This is how you can control storage
--   backend (filesystem, embedded, etc) and where to lookup.
[ssLookupFile] :: StaticSettings -> Pieces -> IO LookupResult

-- | Determine the mime type of the given file. Note that this function
--   lives in IO in case you want to perform more complicated
--   mimetype analysis, such as via the file utility.
[ssGetMimeType] :: StaticSettings -> File -> IO MimeType

-- | Ordered list of filenames to be used for indices. If the user requests
--   a folder, and a file with the given name is found in that folder, that
--   file is served. This supercedes any directory listing.
[ssIndices] :: StaticSettings -> [Piece]

-- | How to perform a directory listing. Optional. Will be used when the
--   user requested a folder.
[ssListing] :: StaticSettings -> Maybe Listing

-- | Value to provide for max age in the cache-control.
[ssMaxAge] :: StaticSettings -> MaxAge

-- | Given a requested path and a new destination, construct a string that
--   will go there. Default implementation will use relative paths.
[ssMkRedirect] :: StaticSettings -> Pieces -> ByteString -> ByteString

-- | If True, send a redirect to the user when a folder is
--   requested and an index page should be displayed. When False,
--   display the content immediately.
[ssRedirectToIndex] :: StaticSettings -> Bool

-- | Prefer usage of etag caching to last-modified caching.
[ssUseHash] :: StaticSettings -> Bool

-- | Force a trailing slash at the end of directories
[ssAddTrailingSlash] :: StaticSettings -> Bool

-- | Optional Application to be used in case of 404 errors
--   
--   Since 3.1.3
[ss404Handler] :: StaticSettings -> Maybe Application
instance GHC.Classes.Eq WaiAppStatic.Types.Piece
instance GHC.Classes.Ord WaiAppStatic.Types.Piece
instance GHC.Internal.Show.Show WaiAppStatic.Types.Piece

module WaiAppStatic.Listing

-- | Provides a default directory listing, suitable for most apps.
--   
--   Code below taken from Happstack:
--   https://github.com/Happstack/happstack-server/blob/87e6c01a65c687d06c61345430a112fc9a444a95/src/Happstack/Server/FileServe/BuildingBlocks.hs
defaultListing :: Listing

-- | Access files on the filesystem.
module WaiAppStatic.Storage.Filesystem

-- | How to calculate etags. Can perform filesystem reads on each call, or
--   use some caching mechanism.
type ETagLookup = FilePath -> IO Maybe ByteString

-- | Settings optimized for a web application. Files will have aggressive
--   caching applied and hashes calculated, and indices and listings are
--   disabled.
defaultWebAppSettings :: FilePath -> StaticSettings

-- | Settings optimized for a file server. More conservative caching will
--   be applied, and indices and listings are enabled.
defaultFileServerSettings :: FilePath -> StaticSettings

-- | Same as defaultWebAppSettings, but additionally uses a
--   specialized ETagLookup in place of the standard one. This can
--   allow you to cache your hash values, or even precompute them.
webAppSettingsWithLookup :: FilePath -> ETagLookup -> StaticSettings

module WaiAppStatic.Storage.Embedded

-- | Serve the list of path/content pairs directly from memory.
embeddedSettings :: [(FilePath, ByteString)] -> StaticSettings

-- | An Etag is used to return 304 Not Modified responses so the client
--   does not need to download resources a second time. Usually the etag is
--   built from a hash of the content. To disable Etags, you can pass the
--   empty string. This will cause the content to be redownloaded on every
--   request.
type Etag = Text

-- | Used at compile time to hold data about an entry to embed into the
--   compiled executable.
data EmbeddableEntry
EmbeddableEntry :: Text -> MimeType -> Either (Etag, ByteString) ExpQ -> EmbeddableEntry

-- | The location where this resource should be served from. The location
--   can contain forward slashes (/) to simulate directories, but must not
--   end with a forward slash.
[eLocation] :: EmbeddableEntry -> Text

-- | The mime type.
[eMimeType] :: EmbeddableEntry -> MimeType

-- | The content itself. The content can be given as a tag and bytestring,
--   in which case the content will be embedded directly into the
--   execuatble. Alternatively, the content can be given as a template
--   haskell expression returning IO (Etag,
--   ByteString) in which case this action will be executed on
--   every request to reload the content (this is useful for a debugging
--   mode).
[eContent] :: EmbeddableEntry -> Either (Etag, ByteString) ExpQ

-- | Create a StaticSettings at compile time that embeds resources
--   directly into the compiled executable. The embedded resources are
--   precompressed (depending on mime type) so that during runtime the
--   resource can be served very quickly.
--   
--   Because of GHC Template Haskell stage restrictions, you must define
--   the entries in a different module than where you create the
--   StaticSettings. For example,
--   
--   
--   {-# LANGUAGE TemplateHaskell, QuasiQuotes, OverloadedStrings #-}
--   module A (mkEmbedded) where
--   
--   import WaiAppStatic.Storage.Embedded
--   import Crypto.Hash.MD5 (hashlazy)
--   import qualified Data.ByteString.Lazy as BL
--   import qualified Data.ByteString.Base64 as B64
--   import qualified Data.Text as T
--   import qualified Data.Text.Encoding as T
--   
--   hash :: BL.ByteString -> T.Text
--   hash = T.take 8 . T.decodeUtf8 . B64.encode . hashlazy
--   
--   mkEmbedded :: IO [EmbeddableEntry]
--   mkEmbedded = do
--       file <- BL.readFile "test.css"
--       let emb = EmbeddableEntry {
--                     eLocation = "somedir/test.css"
--                   , eMimeType = "text/css"
--                   , eContent  = Left (hash file, file)
--                   }
--   
--       let reload = EmbeddableEntry {
--                        eLocation = "anotherdir/test2.txt"
--                      , eMimeType = "text/plain"
--                      , eContent  = Right [| BL.readFile "test2.txt" >>= \c -> return (hash c, c) |]
--                      }
--   
--       return [emb, reload]
--   
--   
--   The above mkEmbedded will be executed at compile time. It
--   loads the contents of test.css and computes the hash of test.css for
--   the etag. The content will be available at the URL somedir/test.css.
--   Internally, embedApp below will attempt to compress the
--   content at compile time. The compression will only happen if the
--   compressed content is shorter than the original and the mime type is
--   either text or javascript. If the content is compressed, at runtime
--   the precomputed compressed content will be served with the appropriate
--   HTTP header. If embedApp decides not to compress the content,
--   it will be served directly.
--   
--   Secondly, mkEmbedded creates a reloadable entry. This will be
--   available at the URL anotherdir/test2.txt. Whenver a request comes in
--   for anotherdir/test2.txt, the action inside the quasiquote in eContent
--   will be executed. This will re-read the test2.txt file and recompute
--   its hash.
--   
--   Finally, here is a module which uses the above action to create a
--   Application.
--   
--   
--   {-# LANGUAGE TemplateHaskell #-}
--   module B where
--   
--   import A
--   import Network.Wai (Application)
--   import Network.Wai.Application.Static (staticApp)
--   import WaiAppStatic.Storage.Embedded
--   import Network.Wai.Handler.Warp (run)
--   
--   myApp :: Application
--   myApp = staticApp $(mkSettings mkEmbedded)
--   
--   main :: IO ()
--   main = run 3000 myApp
--   
mkSettings :: IO [EmbeddableEntry] -> ExpQ

-- | Static file serving for WAI.
module Network.Wai.Application.Static

-- | Turn a StaticSettings into a WAI application.
staticApp :: StaticSettings -> Application

-- | Settings optimized for a web application. Files will have aggressive
--   caching applied and hashes calculated, and indices and listings are
--   disabled.
defaultWebAppSettings :: FilePath -> StaticSettings

-- | Same as defaultWebAppSettings, but additionally uses a
--   specialized ETagLookup in place of the standard one. This can
--   allow you to cache your hash values, or even precompute them.
webAppSettingsWithLookup :: FilePath -> ETagLookup -> StaticSettings

-- | Settings optimized for a file server. More conservative caching will
--   be applied, and indices and listings are enabled.
defaultFileServerSettings :: FilePath -> StaticSettings

-- | Serve the list of path/content pairs directly from memory.
embeddedSettings :: [(FilePath, ByteString)] -> StaticSettings

-- | All of the settings available to users for tweaking wai-app-static.
--   
--   Note that you should use the settings type approach for modifying
--   values. See http://www.yesodweb.com/book/settings-types for
--   more information.
data StaticSettings

-- | Lookup a single file or folder. This is how you can control storage
--   backend (filesystem, embedded, etc) and where to lookup.
ssLookupFile :: StaticSettings -> Pieces -> IO LookupResult

-- | Given a requested path and a new destination, construct a string that
--   will go there. Default implementation will use relative paths.
ssMkRedirect :: StaticSettings -> Pieces -> ByteString -> ByteString

-- | Determine the mime type of the given file. Note that this function
--   lives in IO in case you want to perform more complicated
--   mimetype analysis, such as via the file utility.
ssGetMimeType :: StaticSettings -> File -> IO MimeType

-- | How to perform a directory listing. Optional. Will be used when the
--   user requested a folder.
ssListing :: StaticSettings -> Maybe Listing

-- | Ordered list of filenames to be used for indices. If the user requests
--   a folder, and a file with the given name is found in that folder, that
--   file is served. This supercedes any directory listing.
ssIndices :: StaticSettings -> [Piece]

-- | Value to provide for max age in the cache-control.
ssMaxAge :: StaticSettings -> MaxAge

-- | If True, send a redirect to the user when a folder is
--   requested and an index page should be displayed. When False,
--   display the content immediately.
ssRedirectToIndex :: StaticSettings -> Bool

-- | Force a trailing slash at the end of directories
ssAddTrailingSlash :: StaticSettings -> Bool

-- | Optional Application to be used in case of 404 errors
--   
--   Since 3.1.3
ss404Handler :: StaticSettings -> Maybe Application

-- | Command line version of wai-app-static, used for the warp-static
--   server.
module WaiAppStatic.CmdLine

-- | Run with the given middleware and parsing options from the command
--   line.
--   
--   Since 2.0.1
runCommandLine :: (Args -> Middleware) -> IO ()
data Args
Args :: FilePath -> [FilePath] -> Int -> Bool -> Bool -> Bool -> [(String, String)] -> String -> Args
[docroot] :: Args -> FilePath
[index] :: Args -> [FilePath]
[port] :: Args -> Int
[noindex] :: Args -> Bool
[quiet] :: Args -> Bool
[verbose] :: Args -> Bool
[mime] :: Args -> [(String, String)]
[host] :: Args -> String
