{-# OPTIONS_HADDOCK not-home #-}
module MagicWormhole.Internal.WebSockets
( WebSocketEndpoint(..)
, parseWebSocketEndpoint
, uriToWebSocketEndpoint
, Hostname
, Port
, Path
) where
import Protolude
import Data.String (String)
import Network.URI (URI(..), URIAuth(..), parseURI)
data WebSocketEndpoint = WebSocketEndpoint Hostname Port Path deriving (Eq, Show)
type Hostname = String
type Port = Int
type Path = String
uriToWebSocketEndpoint :: URI -> Maybe WebSocketEndpoint
uriToWebSocketEndpoint uri = do
authority <- uriAuthority uri
port <- case uriPort authority of
"" -> empty
_:rest -> readMaybe rest
pure $ WebSocketEndpoint (uriRegName authority) port (uriPath uri)
parseWebSocketEndpoint :: String -> Maybe WebSocketEndpoint
parseWebSocketEndpoint = uriToWebSocketEndpoint <=< parseURI