@@ -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 =
0 commit comments