@@ -14,7 +14,8 @@ import Data.Maybe (fromMaybe)
1414import Data.String (fromString )
1515import Data.Text.Encoding (encodeUtf8 )
1616import Network.Socket (getPeerName )
17- import Network.Wai (Application )
17+ import Network.Wai (Application , Request (.. ))
18+ import Network.Wai.Application.Static (StaticSettings (.. ))
1819import qualified Network.Wai.Application.Static as S
1920import qualified Network.Wai.Handler.Warp as W
2021import qualified Network.Wai.Handler.Warp.Internal as WI
@@ -31,12 +32,13 @@ import System.Directory (createDirectoryIfMissing)
3132import System.FilePath
3233import UnliftIO.Concurrent (forkFinally )
3334import UnliftIO.Exception (bracket , finally )
35+ import qualified WaiAppStatic.Types as WAT
3436
3537serveStaticFiles :: EmbeddedWebParams -> IO ()
3638serveStaticFiles EmbeddedWebParams {webStaticPath, webHttpPort, webHttpsParams} = do
3739 forM_ webHttpPort $ \ port -> flip forkFinally (\ e -> logError $ " HTTP server crashed: " <> tshow e) $ do
3840 logInfo $ " Serving static site on port " <> tshow port
39- W. runSettings (mkSettings port) ( S. staticApp $ S. defaultFileServerSettings webStaticPath)
41+ W. runSettings (mkSettings port) app
4042 forM_ webHttpsParams $ \ WebHttpsParams {port, cert, key} -> flip forkFinally (\ e -> logError $ " HTTPS server crashed: " <> tshow e) $ do
4143 logInfo $ " Serving static site on port " <> tshow port <> " (TLS)"
4244 WT. runTLS (WT. tlsSettings cert key) (mkSettings port) app
@@ -72,18 +74,30 @@ warpSettings :: W.Settings
7274warpSettings = W. setGracefulShutdownTimeout (Just 1 ) W. defaultSettings
7375
7476staticFiles :: FilePath -> Application
75- staticFiles root = S. staticApp settings
77+ staticFiles root = S. staticApp settings . changeWellKnownPath
7678 where
77- settings = (S. defaultFileServerSettings root)
78- { S. ssListing = Nothing
79- }
79+ settings = defSettings {ssListing = Nothing , ssGetMimeType = getMimeType}
80+ defSettings = S. defaultFileServerSettings root
81+ getMimeType f
82+ | WAT. fromPiece (WAT. fileName f) == " apple-app-site-association" = pure " application/json"
83+ | otherwise = (ssGetMimeType defSettings) f
84+ changeWellKnownPath req = case pathInfo req of
85+ " .well-known" : rest ->
86+ req
87+ { pathInfo = " well-known" : rest,
88+ rawPathInfo = " /well-known/" <> B. drop pfxLen (rawPathInfo req)
89+ }
90+ _ -> req
91+ pfxLen = B. length " /.well-known/"
8092
8193generateSite :: ServerInformation -> Maybe TransportHost -> FilePath -> IO ()
8294generateSite si onionHost sitePath = do
8395 createDirectoryIfMissing True sitePath
8496 B. writeFile (sitePath </> " index.html" ) $ serverInformation si onionHost
8597 copyDir " media" E. mediaContent
86- copyDir " .well-known" E. wellKnown
98+ -- `.well-known` path is re-written in changeWellKnownPath,
99+ -- staticApp does not allow hidden folders.
100+ copyDir " well-known" E. wellKnown
87101 createLinkPage " contact"
88102 createLinkPage " invitation"
89103 createLinkPage " a"
@@ -94,9 +108,7 @@ generateSite si onionHost sitePath = do
94108 where
95109 copyDir dir content = do
96110 createDirectoryIfMissing True $ sitePath </> dir
97- forM_ content $ \ (path, s) -> do
98- createDirectoryIfMissing True $ sitePath </> dir </> takeDirectory path
99- B. writeFile (sitePath </> dir </> path) s
111+ forM_ content $ \ (path, s) -> B. writeFile (sitePath </> dir </> path) s
100112 createLinkPage path = do
101113 createDirectoryIfMissing True $ sitePath </> path
102114 B. writeFile (sitePath </> path </> " index.html" ) E. linkHtml
0 commit comments