{-# LANGUAGE OverloadedStrings #-}moduleNetwork.Wai.Middleware.Static(static,staticRoot)whereimportControl.Monad.Trans(liftIO)importData.List(isInfixOf)importqualifiedData.MapasMimportData.Maybe(fromMaybe)importqualifiedData.ByteStringasBimportqualifiedData.TextasTimportqualifiedData.Text.EncodingasEimportqualifiedFilesystem.Path.CurrentOSasFimportNetwork.HTTP.Types(status200)importSystem.Directory(doesFileExist)importNetwork.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::Middlewarestatic=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->MiddlewarestaticRootbaseappreq=if".."`isInfixOf`fStrthenappreqelsedoexists<-liftIO$doesFileExistfStrifexiststhenreturn$ResponseFilestatus200[("Content-Type",getMimeTypefp)]fStrNothingelseappreqwherefp=F.collapse$F.fromText$T.dropWhile(=='/')$E.decodeUtf8$rawPathInforeqfStr=F.encodeString$F.fromTextbaseF.</>fpgetMimeType::F.FilePath->B.ByteStringgetMimeType=go.mapE.encodeUtf8.F.extensionswherego[]=defaultMimeTypegoexts=fromMaybe(go$tailexts)$M.lookup(B.intercalate"."exts)defaultMimeTypestypeMimeMap=M.MapB.ByteStringB.ByteStringdefaultMimeType::B.ByteStringdefaultMimeType="application/octet-stream"-- This list taken from snap-core's Snap.Util.FileServedefaultMimeTypes::MimeMapdefaultMimeTypes=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")]