From d512d5c105a6203c7868192c71376110195a9bbe Mon Sep 17 00:00:00 2001 From: Taimoor Zaeem Date: Fri, 5 Sep 2025 12:10:50 +0500 Subject: [PATCH] refactor: move escapeHostName to Network.hs Signed-off-by: Taimoor Zaeem --- src/PostgREST/Network.hs | 14 ++++++++++++++ src/PostgREST/Response/OpenAPI.hs | 12 ++---------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/PostgREST/Network.hs b/src/PostgREST/Network.hs index fc14d62b4..9d7d3898e 100644 --- a/src/PostgREST/Network.hs +++ b/src/PostgREST/Network.hs @@ -1,5 +1,6 @@ module PostgREST.Network ( resolveSocketToAddress + , escapeHostName ) where import Data.String (IsString (..)) @@ -35,3 +36,16 @@ resolveSocketToAddress sock = do -- "/tmp/pgrst.sock" showSocketAddr :: NS.SockAddr -> Text showSocketAddr = fromString . show + +-- | When printing special addresses like !4 or *6, we use the following mapping. +-- These special addresses come from: +-- https://hackage.haskell.org/package/streaming-commons-0.2.3.0/docs/\ +-- Data-Streaming-Network.html#t:HostPreference +-- TODO: "!6" should not be printed as "0.0.0.0" address. +escapeHostName :: Text -> Text +escapeHostName "*" = "0.0.0.0" +escapeHostName "*4" = "0.0.0.0" +escapeHostName "!4" = "0.0.0.0" +escapeHostName "*6" = "0.0.0.0" +escapeHostName "!6" = "0.0.0.0" +escapeHostName h = h diff --git a/src/PostgREST/Response/OpenAPI.hs b/src/PostgREST/Response/OpenAPI.hs index 880106a8c..948e6843f 100644 --- a/src/PostgREST/Response/OpenAPI.hs +++ b/src/PostgREST/Response/OpenAPI.hs @@ -25,6 +25,8 @@ import Data.Swagger import PostgREST.Config (AppConfig (..), Proxy (..), isMalformedProxyUri, toURI) +import PostgREST.MediaType +import PostgREST.Network (escapeHostName) import PostgREST.SchemaCache (SchemaCache (..)) import PostgREST.SchemaCache.Identifiers (QualifiedIdentifier (..)) import PostgREST.SchemaCache.Relationship (Cardinality (..), @@ -37,8 +39,6 @@ import PostgREST.SchemaCache.Table (Column (..), Table (..), TablesMap, tableColumnsList) -import PostgREST.MediaType - import Protolude hiding (Proxy, get) encode :: (Text, Text) -> AppConfig -> SchemaCache -> TablesMap -> HM.HashMap k [Routine] -> Maybe Text -> LBS.ByteString @@ -384,14 +384,6 @@ makeSecurityDefinitions secName allow secSchType = SecuritySchemeApiKey (ApiKeyParams "Authorization" ApiKeyHeader) secSchDescription = Just "Add the token prepending \"Bearer \" (without quotes) to it" -escapeHostName :: Text -> Text -escapeHostName "*" = "0.0.0.0" -escapeHostName "*4" = "0.0.0.0" -escapeHostName "!4" = "0.0.0.0" -escapeHostName "*6" = "0.0.0.0" -escapeHostName "!6" = "0.0.0.0" -escapeHostName h = h - postgrestSpec :: (Text, Text) -> RelationshipsMap -> [Routine] -> [Table] -> (Text, Text, Integer, Text) -> Maybe Text -> Bool -> Swagger postgrestSpec (prettyVersion, docsVersion) rels pds ti (s, h, p, b) sd allowSecurityDef = (mempty :: Swagger) & basePath ?~ T.unpack b