Skip to content

Commit bdd8f5e

Browse files
merge functions
1 parent cbec5a2 commit bdd8f5e

5 files changed

Lines changed: 24 additions & 65 deletions

File tree

apps/smp-server/Main.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module Main where
33
import Control.Logger.Simple
44
import Simplex.Messaging.Server.CLI (getEnvPath)
55
import Simplex.Messaging.Server.Main (smpServerCLI_)
6-
import Simplex.Messaging.Server.Web (serveStaticFiles, attachStaticFilesWithWS)
6+
import Simplex.Messaging.Server.Web (serveStaticFiles, attachStaticAndWS)
77
import SMPWeb (smpGenerateSite)
88

99
defaultCfgPath :: FilePath
@@ -19,4 +19,4 @@ main :: IO ()
1919
main = do
2020
cfgPath <- getEnvPath "SMP_SERVER_CFG_PATH" defaultCfgPath
2121
logPath <- getEnvPath "SMP_SERVER_LOG_PATH" defaultLogPath
22-
withGlobalLogging logCfg $ smpServerCLI_ smpGenerateSite serveStaticFiles attachStaticFilesWithWS cfgPath logPath
22+
withGlobalLogging logCfg $ smpServerCLI_ smpGenerateSite serveStaticFiles attachStaticAndWS cfgPath logPath

src/Simplex/Messaging/Server/Main.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ import System.Directory (renameFile)
106106
#endif
107107

108108
smpServerCLI :: FilePath -> FilePath -> IO ()
109-
smpServerCLI = smpServerCLI_ (\_ _ _ -> pure ()) (\_ -> pure ()) (\_ -> error "attachStaticFiles not available")
109+
smpServerCLI = smpServerCLI_ (\_ _ _ -> pure ()) (\_ -> pure ()) (\_ -> error "attachStaticAndWS not available")
110110

111111
smpServerCLI_ ::
112112
(ServerInformation -> Maybe TransportHost -> FilePath -> IO ()) ->
@@ -115,7 +115,7 @@ smpServerCLI_ ::
115115
FilePath ->
116116
FilePath ->
117117
IO ()
118-
smpServerCLI_ generateSite serveStaticFiles attachStaticFiles cfgPath logPath =
118+
smpServerCLI_ generateSite serveStaticFiles attachStaticAndWS cfgPath logPath =
119119
getCliCommand' (cliCommandP cfgPath logPath iniFile) serverVersion >>= \case
120120
Init opts ->
121121
doesFileExist iniFile >>= \case
@@ -489,7 +489,7 @@ smpServerCLI_ generateSite serveStaticFiles attachStaticFiles cfgPath logPath =
489489
case webStaticPath' of
490490
Just path | sharedHTTP -> do
491491
runWebServer path Nothing ServerInformation {config, information}
492-
attachStaticFiles path $ \attachHTTP -> do
492+
attachStaticAndWS path $ \attachHTTP -> do
493493
logDebug "Allocated web server resources"
494494
runSMPServer cfg (Just attachHTTP) `finally` logDebug "Releasing web server resources..."
495495
Just path -> do

src/Simplex/Messaging/Server/Web.hs

Lines changed: 15 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ module Simplex.Messaging.Server.Web
99
WebHttpsParams (..),
1010
EmbeddedContent (..),
1111
serveStaticFiles,
12-
attachStaticFiles,
13-
attachStaticFilesWithWS,
12+
attachStaticAndWS,
1413
serveStaticPageH2,
1514
generateSite,
1615
serverInfoSubsts,
@@ -92,46 +91,17 @@ serveStaticFiles EmbeddedWebParams {webStaticPath, webHttpPort, webHttpsParams}
9291

9392
-- | Prepare context and prepare HTTP handler for TLS connections that already passed TLS.handshake and ALPN check.
9493
-- This version does not support WebSocket upgrade (passes Nothing for wsHandler).
95-
attachStaticFiles :: FilePath -> (AttachHTTP -> IO a) -> IO a
96-
attachStaticFiles path action =
97-
-- Initialize global internal state for http server.
98-
WI.withII warpSettings $ \ii -> do
99-
action $ \socket cxt _wsHandler -> do
100-
-- Initialize internal per-connection resources.
101-
addr <- getPeerName socket
102-
withConnection addr cxt $ \(conn, transport) ->
103-
withTimeout ii conn $ \th ->
104-
-- Run Warp connection handler to process HTTP requests for static files.
105-
WI.serveConnection conn ii th addr transport warpSettings app
106-
where
107-
app = staticFiles path
108-
-- from warp-tls
109-
withConnection socket cxt = bracket (WT.attachConn socket cxt) (terminate . fst)
110-
-- from warp
111-
withTimeout ii conn =
112-
bracket
113-
(WI.registerKillThread (WI.timeoutManager ii) (WI.connClose conn))
114-
WI.cancel
115-
-- shared clean up
116-
terminate conn = WI.connClose conn `finally` (readIORef (WI.connWriteBuffer conn) >>= WI.bufFree)
117-
118-
-- | Like 'attachStaticFiles' but with WebSocket upgrade support for SMP.
119-
-- When wsHandler is provided via AttachHTTP, WebSocket connections are handed off to it.
120-
-- When wsHandler is Nothing, WebSocket upgrade requests are rejected (falls through to static files).
121-
attachStaticFilesWithWS :: FilePath -> (AttachHTTP -> IO a) -> IO a
122-
attachStaticFilesWithWS path action =
94+
attachStaticAndWS :: FilePath -> (AttachHTTP -> IO a) -> IO a
95+
attachStaticAndWS path action =
12396
WI.withII warpSettings $ \ii -> do
12497
action $ \socket cxt wsHandler_ -> do
125-
-- Capture TLS info BEFORE Warp takes over
126-
tlsUniq <- getTlsUnique cxt
127-
wsALPN <- TLS.getNegotiatedProtocol cxt
128-
let peerCert = X.CertificateChain [] -- Client certs not used for web widget
129-
130-
-- Create combined WAI app: WebSocket -> SMP (if handler provided), HTTP -> static files
131-
let app = case wsHandler_ of
132-
Just wsHandler -> WaiWS.websocketsOr wsOpts (handleWebSocket wsHandler tlsUniq wsALPN peerCert) (staticFiles path)
133-
Nothing -> staticFiles path
134-
98+
app <- case wsHandler_ of
99+
Just wsHandler -> do
100+
tlsUniq <- getTlsUnique cxt
101+
wsALPN <- TLS.getNegotiatedProtocol cxt
102+
let peerCert = X.CertificateChain []
103+
pure $ WaiWS.websocketsOr wsOpts (handleWebSocket wsHandler tlsUniq wsALPN peerCert) (staticFiles path)
104+
Nothing -> pure $ staticFiles path
135105
addr <- getPeerName socket
136106
withConnection addr cxt $ \(conn, transport) ->
137107
withTimeout ii conn $ \th ->
@@ -145,33 +115,22 @@ attachStaticFilesWithWS path action =
145115
handleWebSocket :: WSHandler -> ByteString -> Maybe ByteString -> X.CertificateChain -> PendingConnection -> IO ()
146116
handleWebSocket wsHandler tlsUniq wsALPN peerCert pending = do
147117
wsConn <- acceptRequest pending
148-
-- Create a dummy stream for the WS type. In wai-websockets context,
149-
-- connection lifecycle is managed externally, so this stream just
150-
-- provides the interface for closeConnection.
151-
dummyStream <- makeDummyStream
118+
dummyStream <- WSS.makeStream (pure Nothing) (\_ -> pure ())
152119
let ws = WS
153-
{ tlsUniq = tlsUniq,
154-
wsALPN = wsALPN,
120+
{ tlsUniq,
121+
wsALPN,
155122
wsStream = dummyStream,
156123
wsConnection = wsConn,
157-
wsTransportConfig = defaultTransportConfig,
124+
wsTransportConfig = TransportConfig {logTLSErrors = True, transportTimeout = Nothing},
158125
wsCertSent = False,
159126
wsPeerCert = peerCert
160127
}
161128
wsHandler ws
162129

163-
-- Create a minimal stream that just returns EOF on read and ignores writes.
164-
-- Close is a no-op since wai-websockets manages the connection lifecycle.
165-
makeDummyStream :: IO Stream
166-
makeDummyStream = WSS.makeStream (pure Nothing) (\_ -> pure ())
167-
168-
defaultTransportConfig = TransportConfig {logTLSErrors = True, transportTimeout = Nothing}
169-
170-
-- Get TLS unique value (used for channel binding)
171130
getTlsUnique :: TLS.Context -> IO ByteString
172131
getTlsUnique cxt = TLS.getPeerFinished cxt >>= maybe (fail "TLS not finished") pure
173132

174-
-- from warp-tls (socket is actually SockAddr here, matching original pattern)
133+
-- from warp-tls
175134
withConnection socket cxt = bracket (WT.attachConn socket cxt) (terminate . fst)
176135
-- from warp
177136
withTimeout ii conn =

tests/CLITests.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import qualified Simplex.Messaging.Transport.HTTP2.Client as HC
3131
import Simplex.Messaging.Transport.Server (loadFileFingerprint)
3232
import Simplex.Messaging.Util (catchAll_)
3333
import qualified SMPWeb
34-
import Simplex.Messaging.Server.Web (serveStaticFiles, attachStaticFilesWithWS)
34+
import Simplex.Messaging.Server.Web (serveStaticFiles, attachStaticAndWS)
3535
import System.Directory (doesFileExist)
3636
import System.Environment (withArgs)
3737
import System.FilePath ((</>))
@@ -152,7 +152,7 @@ smpServerTestStatic = do
152152
Right ini_ <- readIniFile iniFile
153153
lookupValue "WEB" "https" ini_ `shouldBe` Right "5223"
154154

155-
let smpServerCLI' = smpServerCLI_ SMPWeb.smpGenerateSite serveStaticFiles attachStaticFilesWithWS
155+
let smpServerCLI' = smpServerCLI_ SMPWeb.smpGenerateSite serveStaticFiles attachStaticAndWS
156156
let server = capture_ (withArgs ["start"] $ smpServerCLI' cfgPath logPath `catchAny` print)
157157
bracket (async server) cancel $ \_t -> do
158158
threadDelay 1000000

tests/ServerTests.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ import Simplex.Messaging.Transport
5555
import Simplex.Messaging.Transport.Client (TransportClientConfig (..), defaultTransportClientConfig, runTLSTransportClient)
5656
import Simplex.Messaging.Transport.WebSockets (WS)
5757
import Simplex.Messaging.Transport.Server (ServerCredentials (..), loadFileFingerprint)
58-
import Simplex.Messaging.Server.Web (attachStaticFilesWithWS)
58+
import Simplex.Messaging.Server.Web (attachStaticAndWS)
5959
import Data.X509.Validation (Fingerprint (..))
6060
import Simplex.Messaging.Util (whenM)
6161
import Simplex.Messaging.Version (mkVersionRange)
@@ -1500,7 +1500,7 @@ testWebSocketAndTLS =
15001500
it "native TLS and WebSocket clients work on same port" $ \(_t, msType) -> do
15011501
Fingerprint fpHTTP <- loadFileFingerprint "tests/fixtures/web_ca.crt"
15021502
let httpKeyHash = C.KeyHash fpHTTP
1503-
attachStaticFilesWithWS "tests/fixtures" $ \attachHTTP ->
1503+
attachStaticAndWS "tests/fixtures" $ \attachHTTP ->
15041504
withSmpServerConfig (cfgWebOn msType testPort) (Just attachHTTP) $ \_ -> do
15051505
g <- C.newRandom
15061506
(rPub, rKey) <- atomically $ C.generateAuthKeyPair C.SEd25519 g

0 commit comments

Comments
 (0)