connection 0.3.0 → 0.3.1
raw patch · 3 files changed
+64/−56 lines, 3 filesdep ~networkdep ~tlsPVP ok
version bump matches the API change (PVP)
Dependency ranges changed: network, tls
API changes (from Hackage documentation)
Files
- Network/Connection.hs +6/−0
- README.md +56/−54
- connection.cabal +2/−2
Network/Connection.hs view
@@ -1,3 +1,4 @@+{-# LANGUAGE CPP #-} {-# LANGUAGE BangPatterns #-} {-# LANGUAGE DeriveDataTypeable #-} {-# LANGUAGE ScopedTypeVariables #-}@@ -64,6 +65,7 @@ import Network.Socket import qualified Network.Socket.ByteString as N +import Data.Tuple (swap) import Data.Default.Class import Data.Data import Data.ByteString (ByteString)@@ -100,6 +102,10 @@ , TLS.sessionEstablish = \sessionID sessionData -> modifyMVar_ mvar (return . M.insert sessionID sessionData) , TLS.sessionInvalidate = \sessionID -> modifyMVar_ mvar (return . M.delete sessionID)+#if MIN_VERSION_tls(1,5,0)+ , TLS.sessionResumeOnlyOnce = \sessionID ->+ modifyMVar mvar (pure . swap . M.updateLookupWithKey (\_ _ -> Nothing) sessionID)+#endif } -- | Initialize the library with shared parameters between connection.
README.md view
@@ -14,68 +14,70 @@ Connect to www.example.com on port 4567 (without socks or tls), then send a byte, receive a single byte, print it, and close the connection:-- import qualified Data.ByteString as B- import Network.Connection- import Data.Default-- main = do- ctx <- initConnectionContext- con <- connectTo ctx $ ConnectionParams- { connectionHostname = "www.example.com"- , connectionPort = 4567- , connectionUseSecure = Nothing- , connectionUseSocks = Nothing- }- connectionPut con (B.singleton 0xa)- r <- connectionGet con 1- putStrLn $ show r- connectionClose con+```haskell+import qualified Data.ByteString as B+import Network.Connection+import Data.Default +main = do+ ctx <- initConnectionContext+ con <- connectTo ctx $ ConnectionParams+ { connectionHostname = "www.example.com"+ , connectionPort = 4567+ , connectionUseSecure = Nothing+ , connectionUseSocks = Nothing+ }+ connectionPut con (B.singleton 0xa)+ r <- connectionGet con 1+ putStrLn $ show r+ connectionClose con+``` Using a socks proxy is easy, we just need replacing the connectionSocks parameter, for example connecting to the same host, but using a socks proxy at localhost:1080:-- con <- connectTo ctx $ ConnectionParams- { connectionHostname = "www.example.com"- , connectionPort = 4567- , connectionUseSecure = Nothing- , connectionUseSocks = Just $ SockSettingsSimple "localhost" 1080- }-+```haskell+con <- connectTo ctx $ ConnectionParams+ { connectionHostname = "www.example.com"+ , connectionPort = 4567+ , connectionUseSecure = Nothing+ , connectionUseSocks = Just $ SockSettingsSimple "localhost" 1080+ }+``` Connecting to a SSL style socket is equally easy, and need to set the UseSecure fields in ConnectionParams:-- con <- connectTo ctx $ ConnectionParams- { connectionHostname = "www.example.com"- , connectionPort = 4567- , connectionUseSecure = Just def- , connectionUseSocks = Nothing- }-+```haskell+con <- connectTo ctx $ ConnectionParams+ { connectionHostname = "www.example.com"+ , connectionPort = 4567+ , connectionUseSecure = Just def+ , connectionUseSocks = Nothing+ }+``` And finally, you can start TLS in the middle of an insecure connection. This is great for protocol using STARTTLS (e.g. IMAP, SMTP): - {-# LANGUAGE OverloadedStrings #-}- import qualified Data.ByteString as B- import Data.ByteString.Char8 ()- import Network.Connection- import Data.Default+```haskell+{-# LANGUAGE OverloadedStrings #-}+import qualified Data.ByteString as B+import Data.ByteString.Char8 ()+import Network.Connection+import Data.Default - main = do- ctx <- initConnectionContext- con <- connectTo ctx $ ConnectionParams- { connectionHostname = "www.example.com"- , connectionPort = 4567- , connectionUseSecure = Nothing- , connectionUseSocks = Nothing- }- -- talk to the other side with no TLS: says hello and starttls- connectionPut con "HELLO\n"- connectionPut con "STARTTLS\n"+main = do+ ctx <- initConnectionContext+ con <- connectTo ctx $ ConnectionParams+ { connectionHostname = "www.example.com"+ , connectionPort = 4567+ , connectionUseSecure = Nothing+ , connectionUseSocks = Nothing+ }+ -- talk to the other side with no TLS: says hello and starttls+ connectionPut con "HELLO\n"+ connectionPut con "STARTTLS\n" - -- switch to TLS- connectionSetSecure ctx con def+ -- switch to TLS+ connectionSetSecure ctx con def - -- the connection is from now on using TLS, we can send secret for example- connectionPut con "PASSWORD 123\n"- connectionClose con+ -- the connection is from now on using TLS, we can send secret for example+ connectionPut con "PASSWORD 123\n"+ connectionClose con+```
connection.cabal view
@@ -1,5 +1,5 @@ Name: connection-Version: 0.3.0+Version: 0.3.1 Description: Simple network library for all your connection need. .@@ -27,7 +27,7 @@ , bytestring , containers , data-default-class- , network >= 2.6+ , network >= 2.6.3 , tls >= 1.4 , socks >= 0.6 , x509 >= 1.5