packages feed

wai-middleware-static (empty) → 0.0.1

raw patch · 4 files changed

+179/−0 lines, 4 filesdep +basedep +bytestringdep +containerssetup-changed

Dependencies added: base, bytestring, containers, directory, http-types, mtl, system-filepath, text, wai

Files

+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c)2012, Andrew Farmer <anfarmer@ku.edu>++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++    * Redistributions of source code must retain the above copyright+      notice, this list of conditions and the following disclaimer.++    * Redistributions in binary form must reproduce the above+      copyright notice, this list of conditions and the following+      disclaimer in the documentation and/or other materials provided+      with the distribution.++    * Neither the name of Andrew Farmer <anfarmer@ku.edu> nor the names of other+      contributors may be used to endorse or promote products derived+      from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Network/Wai/Middleware/Static.hs view
@@ -0,0 +1,107 @@+{-# LANGUAGE OverloadedStrings #-}+module Network.Wai.Middleware.Static (static, staticRoot) where++import Control.Monad.Trans (liftIO)+import Data.List (isInfixOf)+import qualified Data.Map as M+import Data.Maybe (fromMaybe)+import qualified Data.ByteString as B+import qualified Data.Text as T+import qualified Data.Text.Encoding as E++import qualified Filesystem.Path.CurrentOS as F+import Network.HTTP.Types (status200)+import System.Directory (doesFileExist)++import Network.Wai++-- | Serve static files out of the application root (current directory).+-- If file is found, it is streamed to the client and no further middleware is run.+static :: Middleware+static = staticRoot ""++-- | Like 'static', but only looks for static files in the given directory.+-- Supplied path may be relative or absolute and is prepended to the requested path.+--+-- > static = staticRoot ""+staticRoot :: T.Text -> Middleware+staticRoot base app req =+    if ".." `isInfixOf` fStr+      then app req+      else do exists <- liftIO $ doesFileExist fStr+              if exists+                then return $ ResponseFile status200 [("Content-Type", getMimeType fp)] fStr Nothing+                else app req+  where fp = F.collapse $ F.fromText $ T.dropWhile (=='/') $ E.decodeUtf8 $ rawPathInfo req+        fStr = F.encodeString $ F.fromText base F.</> fp++getMimeType :: F.FilePath -> B.ByteString+getMimeType = go . map E.encodeUtf8 . F.extensions+    where go [] = defaultMimeType+          go exts = fromMaybe (go $ tail exts) $ M.lookup (B.intercalate "." exts) defaultMimeTypes++type MimeMap = M.Map B.ByteString B.ByteString++defaultMimeType :: B.ByteString+defaultMimeType = "application/octet-stream"++-- This list taken from snap-core's Snap.Util.FileServe+defaultMimeTypes :: MimeMap+defaultMimeTypes = M.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"                   ),+  ( "json"    , "application/json"                  ),+  ( "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"          ),+  ( "svg"     , "image/svg+xml"                     ),+  ( "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"          ),+  ( "ttf"     , "application/x-font-truetype"       ),+  ( "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"                   ) ]
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ wai-middleware-static.cabal view
@@ -0,0 +1,40 @@+Name:                wai-middleware-static+Version:             0.0.1+Synopsis:            WAI middleware that intercepts requests to static files.+Homepage:            https://github.com/xich/scotty+Bug-reports:         https://github.com/xich/scotty/issues+License:             BSD3+License-file:        LICENSE+Author:              Andrew Farmer <anfarmer@ku.edu>+Maintainer:          Andrew Farmer <anfarmer@ku.edu>+Copyright:           (c) 2012 Andrew Farmer+Category:            Web+Stability:           experimental+Build-type:          Simple+Cabal-version:       >= 1.10+Description:+  WAI middleware that intercepts requests to static files and serves them+  if they exist.+  .+  [WAI] <http://hackage.haskell.org/package/wai>++-- Extra-source-files:++Library+  Exposed-modules:     Network.Wai.Middleware.Static+  default-language:    Haskell2010+  Build-depends:       base             >= 4.3.1 && < 5,+                       bytestring       >= 0.9.1,+                       containers       >= 0.4,+                       directory        >= 1.1,+                       http-types       >= 0.6.8,+                       mtl              >= 2.0.1,+                       system-filepath  >= 0.4.4,+                       text             >= 0.11.1,+                       wai              >= 1.0.0++  GHC-options: -Wall -fno-warn-orphans++source-repository head+  type:     git+  location: git://github.com/xich/scotty.git