Skip to content

Commit c2c4730

Browse files
authored
smp server: serve .well-known folder via server pages (#1514)
1 parent ccdd8e1 commit c2c4730

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

apps/smp-server/static/.well-known/apple-app-site-association/index.json renamed to apps/smp-server/static/.well-known/apple-app-site-association

File renamed without changes.

apps/smp-server/web/Static.hs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ import Data.Maybe (fromMaybe)
1414
import Data.String (fromString)
1515
import Data.Text.Encoding (encodeUtf8)
1616
import Network.Socket (getPeerName)
17-
import Network.Wai (Application)
17+
import Network.Wai (Application, Request (..))
18+
import Network.Wai.Application.Static (StaticSettings (..))
1819
import qualified Network.Wai.Application.Static as S
1920
import qualified Network.Wai.Handler.Warp as W
2021
import qualified Network.Wai.Handler.Warp.Internal as WI
@@ -31,12 +32,13 @@ import System.Directory (createDirectoryIfMissing)
3132
import System.FilePath
3233
import UnliftIO.Concurrent (forkFinally)
3334
import UnliftIO.Exception (bracket, finally)
35+
import qualified WaiAppStatic.Types as WAT
3436

3537
serveStaticFiles :: EmbeddedWebParams -> IO ()
3638
serveStaticFiles 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
7274
warpSettings = W.setGracefulShutdownTimeout (Just 1) W.defaultSettings
7375

7476
staticFiles :: 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

8193
generateSite :: ServerInformation -> Maybe TransportHost -> FilePath -> IO ()
8294
generateSite 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

Comments
 (0)