• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

Network/H02-Sep-2019-529327

CHANGELOG.mdH A D02-Sep-2019349 86

LICENSEH A D02-Sep-20191.5 KiB2824

README.mdH A D02-Sep-20192.8 KiB8473

Setup.hsH A D02-Sep-201946 32

connection.cabalH A D03-May-20221.5 KiB4542

README.md

1haskell Connection library
2==========================
3
4Simple network library for all your connection need.
5
6Features:
7
8- Really simple to use
9- SSL/TLS
10- SOCKS
11
12Usage
13-----
14
15Connect to www.example.com on port 4567 (without socks or tls), then send a
16byte, receive a single byte, print it, and close the connection:
17```haskell
18import qualified Data.ByteString as B
19import Network.Connection
20import Data.Default
21
22main = do
23    ctx <- initConnectionContext
24    con <- connectTo ctx $ ConnectionParams
25                              { connectionHostname  = "www.example.com"
26                              , connectionPort      = 4567
27                              , connectionUseSecure = Nothing
28                              , connectionUseSocks  = Nothing
29                              }
30    connectionPut con (B.singleton 0xa)
31    r <- connectionGet con 1
32    putStrLn $ show r
33    connectionClose con
34```
35Using a socks proxy is easy, we just need replacing the connectionSocks
36parameter, for example connecting to the same host, but using a socks
37proxy at localhost:1080:
38```haskell
39con <- connectTo ctx $ ConnectionParams
40                       { connectionHostname  = "www.example.com"
41                       , connectionPort      = 4567
42                       , connectionUseSecure = Nothing
43                       , connectionUseSocks  = Just $ SockSettingsSimple "localhost" 1080
44                       }
45```
46Connecting to a SSL style socket is equally easy, and need to set the UseSecure fields in ConnectionParams:
47```haskell
48con <- connectTo ctx $ ConnectionParams
49                       { connectionHostname  = "www.example.com"
50                       , connectionPort      = 4567
51                       , connectionUseSecure = Just def
52                       , connectionUseSocks  = Nothing
53                       }
54```
55And finally, you can start TLS in the middle of an insecure connection. This is great for
56protocol using STARTTLS (e.g. IMAP, SMTP):
57
58```haskell
59{-# LANGUAGE OverloadedStrings #-}
60import qualified Data.ByteString as B
61import Data.ByteString.Char8 ()
62import Network.Connection
63import Data.Default
64
65main = do
66    ctx <- initConnectionContext
67    con <- connectTo ctx $ ConnectionParams
68                              { connectionHostname  = "www.example.com"
69                              , connectionPort      = 4567
70                              , connectionUseSecure = Nothing
71                              , connectionUseSocks  = Nothing
72                              }
73    -- talk to the other side with no TLS: says hello and starttls
74    connectionPut con "HELLO\n"
75    connectionPut con "STARTTLS\n"
76
77    -- switch to TLS
78    connectionSetSecure ctx con def
79
80    -- the connection is from now on using TLS, we can send secret for example
81    connectionPut con "PASSWORD 123\n"
82    connectionClose con
83```
84