Skip to content

Commit

Permalink
Merge pull request snapframework#71 from jaspervdj/master
Browse files Browse the repository at this point in the history
Add preServeHook to DirectoryConfig
  • Loading branch information
gregorycollins committed May 27, 2011
2 parents 9c386ea + 2926149 commit 5f88abb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
29 changes: 19 additions & 10 deletions src/Snap/Util/FileServe.hs
Expand Up @@ -221,7 +221,11 @@ data DirectoryConfig m = DirectoryConfig {
dynamicHandlers :: HandlerMap m,

-- | MIME type map to look up content types.
mimeTypes :: MimeMap
mimeTypes :: MimeMap,

-- | Handler that is called before a file is served. It will only be
-- called when a file is actually found, not for generated index pages.
preServeHook :: FilePath -> m ()
}


Expand Down Expand Up @@ -328,36 +332,39 @@ defaultIndexGenerator mm styles d = do
------------------------------------------------------------------------------
-- | A very simple configuration for directory serving. This configuration
-- uses built-in MIME types from 'defaultMimeTypes', and has no index files,
-- index generator, or dynamic file handlers.
-- index generator, dynamic file handlers, or 'preServeHook'.
simpleDirectoryConfig :: MonadSnap m => DirectoryConfig m
simpleDirectoryConfig = DirectoryConfig {
indexFiles = [],
indexGenerator = const pass,
dynamicHandlers = Map.empty,
mimeTypes = defaultMimeTypes
mimeTypes = defaultMimeTypes,
preServeHook = const $ return ()
}


------------------------------------------------------------------------------
-- | A reasonable default configuration for directory serving. This
-- configuration uses built-in MIME types from 'defaultMimeTypes', serves
-- common index files @index.html@ and @index.htm@, but does not autogenerate
-- directory indexes, nor have any dynamic file handlers.
-- directory indexes, nor have any dynamic file handlers. The 'preServeHook'
-- will not do anything.
defaultDirectoryConfig :: MonadSnap m => DirectoryConfig m
defaultDirectoryConfig = DirectoryConfig {
indexFiles = ["index.html", "index.htm"],
indexGenerator = const pass,
dynamicHandlers = Map.empty,
mimeTypes = defaultMimeTypes
mimeTypes = defaultMimeTypes,
preServeHook = const $ return ()
}


------------------------------------------------------------------------------
-- | A more elaborate configuration for file serving. This configuration
-- uses built-in MIME types from 'defaultMimeTypes', serves common index files
-- @index.html@ and @index.htm@, and autogenerates directory indexes with a
-- Snap-like feel. It still has no dynamic file handlers, which should be
-- added as needed.
-- Snap-like feel. It still has no dynamic file handlers, nor 'preServeHook',
-- which should be added as needed.
--
-- Files recognized as indexes include @index.html@, @index.htm@,
-- @default.html@, @default.htm@, @home.html@
Expand All @@ -366,7 +373,8 @@ fancyDirectoryConfig = DirectoryConfig {
indexFiles = ["index.html", "index.htm"],
indexGenerator = defaultIndexGenerator defaultMimeTypes snapIndexStyles,
dynamicHandlers = Map.empty,
mimeTypes = defaultMimeTypes
mimeTypes = defaultMimeTypes,
preServeHook = const $ return ()
}


Expand Down Expand Up @@ -401,12 +409,13 @@ serveDirectoryWith cfg base = do
generate = indexGenerator cfg
mimes = mimeTypes cfg
dyns = dynamicHandlers cfg
pshook = preServeHook cfg

-- Serves a file if it exists; passes if not
serve f = do
liftIO (doesFileExist f) >>= flip unless pass
let fname = takeFileName f
let staticServe = do serveFileAs (fileType mimes fname)
let fname = takeFileName f
let staticServe f' = pshook f >> serveFileAs (fileType mimes fname) f'
lookupExt staticServe dyns fname f >> return True <|> return False

-- Serves a directory via indices if available. Returns True on success,
Expand Down
12 changes: 8 additions & 4 deletions test/suite/Snap/Util/FileServe/Tests.hs
Expand Up @@ -224,7 +224,8 @@ testFsCfg = testCase "fileServe/Cfg" $ do
indexFiles = [],
indexGenerator = const pass,
dynamicHandlers = Map.empty,
mimeTypes = defaultMimeTypes
mimeTypes = defaultMimeTypes,
preServeHook = const $ return ()
}

-- Named file in the root directory
Expand Down Expand Up @@ -266,7 +267,8 @@ testFsCfg = testCase "fileServe/Cfg" $ do
indexFiles = ["index.txt", "altindex.html"],
indexGenerator = const pass,
dynamicHandlers = Map.empty,
mimeTypes = defaultMimeTypes
mimeTypes = defaultMimeTypes,
preServeHook = const $ return ()
}

-- Request for root directory with index
Expand Down Expand Up @@ -296,7 +298,8 @@ testFsCfg = testCase "fileServe/Cfg" $ do
indexFiles = ["index.txt", "altindex.html"],
indexGenerator = printName,
dynamicHandlers = Map.empty,
mimeTypes = defaultMimeTypes
mimeTypes = defaultMimeTypes,
preServeHook = const $ return ()
}

-- Request for root directory with index
Expand All @@ -318,7 +321,8 @@ testFsCfg = testCase "fileServe/Cfg" $ do
indexFiles = [],
indexGenerator = const pass,
dynamicHandlers = Map.fromList [ (".txt", printName) ],
mimeTypes = defaultMimeTypes
mimeTypes = defaultMimeTypes,
preServeHook = const $ return ()
}

-- Request for file with dynamic handler
Expand Down

0 comments on commit 5f88abb

Please sign in to comment.