Network.TCP

HTTP-4000.4.1: A library for client-side HTTP

Copyright | See LICENSE file
License | BSD
Maintainer | Ganesh Sittampalam <ganesh@earth.li>
Stability | experimental
Portability | non-portable (not tested)
Safe Haskell | None
Language | Haskell98

Network.TCP

Description

Some utility functions for working with the Haskell `network` package. Mostly for internal use by the `Network.HTTP` code.

Synopsis

- data Connection
- data EndPoint = EndPoint {
  - epHost :: String
  - epPort :: Int
  }
- openTCPPort :: String -> Int -> IO Connection
- isConnectedTo :: Connection -> EndPoint -> IO Bool
- openTCPConnection :: BufferType ty => String -> Int -> IO (HandleStream ty)
- socketConnection :: BufferType ty => String -> Int -> Socket -> IO (HandleStream ty)
- isTCPConnectedTo :: HandleStream ty -> EndPoint -> IO Bool
- data HandleStream a
- class BufferType bufType => HStream bufType where
  - openStream :: String -> Int -> IO (HandleStream bufType)
  - openSocketStream :: String -> Int -> Socket -> IO (HandleStream bufType)
  - readLine :: HandleStream bufType -> IO (Result bufType)
  - readBlock :: HandleStream bufType -> Int -> IO (Result bufType)
  - writeBlock :: HandleStream bufType -> bufType -> IO (Result ())
  - close :: HandleStream bufType -> IO ()
  - closeQuick :: HandleStream bufType -> IO ()
  - closeOnEnd :: HandleStream bufType -> Bool -> IO ()
- data StreamHooks ty = StreamHooks {
  - hook_readLine :: (ty -> String) -> Result ty -> IO ()
  - hook_readBlock :: (ty -> String) -> Int -> Result ty -> IO ()
  - hook_writeBlock :: (ty -> String) -> ty -> Result () -> IO ()
  - hook_close :: IO ()
  - hook_name :: String
  }
- nullHooks :: StreamHooks ty
- setStreamHooks :: HandleStream ty -> StreamHooks ty -> IO ()
- getStreamHooks :: HandleStream ty -> IO (Maybe (StreamHooks ty))
- hstreamToConnection :: HandleStream String -> Connection

# Documentation

data Connection #

The `Connection` newtype is a wrapper that allows us to make connections an instance of the Stream class, without GHC extensions. While this looks sort of like a generic reference to the transport layer it is actually TCP specific, which can be seen in the implementation of the 'Stream Connection' instance.

#### Instances

Instances details

Stream Connection # |
Instance details Defined in Network.TCP Methods readLine :: Connection -> IO (Result String) # readBlock :: Connection -> Int -> IO (Result String) # writeBlock :: Connection -> String -> IO (Result ()) # close :: Connection -> IO () # closeOnEnd :: Connection -> Bool -> IO () #

data EndPoint #

Constructors

EndPoint |
Fields epHost :: String epPort :: Int

#### Instances

Instances details

Eq EndPoint # |
Instance details Defined in Network.TCP Methods (==) :: EndPoint -> EndPoint -> Bool # (/=) :: EndPoint -> EndPoint -> Bool #

openTCPPort :: String -> Int -> IO Connection #

`openTCPPort uri port` establishes a connection to a remote host, using `getHostByName` which possibly queries the DNS system, hence may trigger a network connection.

isConnectedTo :: Connection -> EndPoint -> IO Bool #

Checks both that the underlying Socket is connected and that the connection peer matches the given host name (which is recorded locally).

openTCPConnection :: BufferType ty => String -> Int -> IO (HandleStream ty) #

socketConnection :: BufferType ty => String -> Int -> Socket -> IO (HandleStream ty) #

`socketConnection`, like `openConnection` but using a pre-existing `Socket`.

isTCPConnectedTo :: HandleStream ty -> EndPoint -> IO Bool #

data HandleStream a #

class BufferType bufType => HStream bufType where #

`HStream` overloads the use of `HandleStream`s, letting you overload the handle operations over the type that is communicated across the handle. It comes in handy for `Network.HTTP` `Request` and `Response`s as the payload representation isn't fixed, but overloaded.

The library comes with instances for `ByteString`s and `String`, but should you want to plug in your own payload representation, defining your own `HStream` instance _should_ be all that it takes.

Methods

openStream :: String -> Int -> IO (HandleStream bufType) #

openSocketStream :: String -> Int -> Socket -> IO (HandleStream bufType) #

readLine :: HandleStream bufType -> IO (Result bufType) #

readBlock :: HandleStream bufType -> Int -> IO (Result bufType) #

writeBlock :: HandleStream bufType -> bufType -> IO (Result ()) #

close :: HandleStream bufType -> IO () #

closeQuick :: HandleStream bufType -> IO () #

closeOnEnd :: HandleStream bufType -> Bool -> IO () #

#### Instances

Instances details

HStream ByteString # |
Instance details Defined in Network.TCP Methods openStream :: String -> Int -> IO (HandleStream ByteString) # openSocketStream :: String -> Int -> Socket -> IO (HandleStream ByteString) # readLine :: HandleStream ByteString -> IO (Result ByteString) # readBlock :: HandleStream ByteString -> Int -> IO (Result ByteString) # writeBlock :: HandleStream ByteString -> ByteString -> IO (Result ()) # close :: HandleStream ByteString -> IO () # closeQuick :: HandleStream ByteString -> IO () # closeOnEnd :: HandleStream ByteString -> Bool -> IO () #
HStream ByteString # |
Instance details Defined in Network.TCP Methods openStream :: String -> Int -> IO (HandleStream ByteString) # openSocketStream :: String -> Int -> Socket -> IO (HandleStream ByteString) # readLine :: HandleStream ByteString -> IO (Result ByteString) # readBlock :: HandleStream ByteString -> Int -> IO (Result ByteString) # writeBlock :: HandleStream ByteString -> ByteString -> IO (Result ()) # close :: HandleStream ByteString -> IO () # closeQuick :: HandleStream ByteString -> IO () # closeOnEnd :: HandleStream ByteString -> Bool -> IO () #
HStream String # |
Instance details Defined in Network.TCP Methods openStream :: String -> Int -> IO (HandleStream String) # openSocketStream :: String -> Int -> Socket -> IO (HandleStream String) # readLine :: HandleStream String -> IO (Result String) # readBlock :: HandleStream String -> Int -> IO (Result String) # writeBlock :: HandleStream String -> String -> IO (Result ()) # close :: HandleStream String -> IO () # closeQuick :: HandleStream String -> IO () # closeOnEnd :: HandleStream String -> Bool -> IO () #

data StreamHooks ty #

Constructors

StreamHooks |
Fields hook_readLine :: (ty -> String) -> Result ty -> IO () hook_readBlock :: (ty -> String) -> Int -> Result ty -> IO () hook_writeBlock :: (ty -> String) -> ty -> Result () -> IO () hook_close :: IO () hook_name :: String

#### Instances

Instances details

Eq ty => Eq (StreamHooks ty) # |
Instance details Defined in Network.TCP Methods (==) :: StreamHooks ty -> StreamHooks ty -> Bool # (/=) :: StreamHooks ty -> StreamHooks ty -> Bool #

nullHooks :: StreamHooks ty #

setStreamHooks :: HandleStream ty -> StreamHooks ty -> IO () #

getStreamHooks :: HandleStream ty -> IO (Maybe (StreamHooks ty)) #

hstreamToConnection :: HandleStream String -> Connection #