-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
refactor: deduplicate host resolution logic and observations #4288
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,37 @@ | ||
| module PostgREST.Network | ||
| ( resolveHost | ||
| ( resolveSocketToAddress | ||
| ) where | ||
|
|
||
| import Data.IP (fromHostAddress, fromHostAddress6) | ||
| import Data.String (IsString (..)) | ||
| import qualified Network.Socket as NS | ||
|
|
||
| import Protolude | ||
|
|
||
| resolveHost :: NS.Socket -> IO (Maybe Text) | ||
| resolveHost sock = do | ||
| -- | Resolves the socket to an address depending on the socket type. The Show | ||
| -- instance of the socket types automatically resolves it to the correct | ||
| -- address. Example resolution: | ||
| -- ----------------------------------------------------- | ||
| -- | IPv4 | IPv6 | Unix | | ||
| -- ----------------------------------------------------- | ||
| -- | 127.0.0.1:80 | [2001:db8::1]:80 | /tmp/pgrst.sock | | ||
| -- ----------------------------------------------------- | ||
| resolveSocketToAddress :: NS.Socket -> IO Text | ||
| resolveSocketToAddress sock = do | ||
| sn <- NS.getSocketName sock | ||
| case sn of | ||
| NS.SockAddrInet _ hostAddr -> pure $ Just $ fromString $ show $ fromHostAddress hostAddr | ||
| -- The IPv6 addresses are wrapped in [] brackets. This is done in accordance | ||
| -- to RFC 3986 (https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.2). | ||
| -- In short, we did this to have a clear separation between the port and host | ||
| -- because the components of an IPv6 are separated with the ':' character. | ||
| NS.SockAddrInet6 _ _ hostAddr6 _ -> pure $ Just $ fromString $ "[" ++ show (fromHostAddress6 hostAddr6) ++ "]" | ||
| _ -> pure Nothing | ||
| return $ showSocketAddr sn | ||
|
|
||
| -- | | ||
| -- >>> let addr_ipv4 = NS.SockAddrInet 80 (NS.tupleToHostAddress (127,0,0,1)) | ||
| -- >>> let addr_ipv6 = NS.SockAddrInet6 80 0 (0,0,0,1) 0 | ||
| -- >>> let addr_unix = NS.SockAddrUnix "/tmp/pgrst.sock" | ||
| -- | ||
| -- >>> showSocketAddr addr_ipv4 | ||
| -- "127.0.0.1:80" | ||
|
|
||
| -- >>> showSocketAddr addr_ipv6 | ||
| -- "[::1]:80" | ||
| -- | ||
| -- >>> showSocketAddr addr_unix | ||
| -- "/tmp/pgrst.sock" | ||
| showSocketAddr :: NS.SockAddr -> Text | ||
| showSocketAddr = fromString . show |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.