TlsStream

TlsStream

meta-stub

a

is a TLS-encrypted TCP stream using OpenSSL.

Usage

(match (TlsStream.connect "example.com" 443)
  (Result.Success s)
    (do
      (ignore (TlsStream.send &s "GET / HTTP/1.1
Host: example.com
Connection: close

"))
      (match (TlsStream.read &s)
        (Result.Success msg) (println* &msg)
        (Result.Error e) (IO.errorln &e))
      (TlsStream.close s))
  (Result.Error e) (IO.errorln &e))

close

external

(Fn [TlsStream] ())

closes the TLS stream, consuming it. Performs SSL shutdown.

close!

external

(Fn [(Ref TlsStream a)] ())

closes the TLS stream by reference. Sets fd to -1 to prevent double-close.

connect

defn

(Fn [(Ref String a), Int] (Result TlsStream String))

                        (connect host port)
                    

connects to a remote host over TLS. Returns (Result TlsStream String).

copy

external

(Fn [(Ref TlsStream a)] TlsStream)

read

defn

(Fn [(Ref TlsStream a)] (Result String b))

                        (read stream)
                    

reads up to 4096 bytes from the TLS stream. Returns the data as a string. Returns an empty string on connection close.

read-append

defn

(Fn [(Ref TlsStream a), (Ref (Array Byte) b)] (Result Int String))

                        (read-append stream buf)
                    

reads from the TLS stream and appends to an existing byte buffer. Returns bytes read (0 = connection closed), or an error.

read-bytes

defn

(Fn [(Ref TlsStream a)] (Result (Array Byte) b))

                        (read-bytes stream)
                    

reads up to 4096 bytes as a byte array.

send

defn

(Fn [(Ref TlsStream a), (Ref String b)] (Result Int String))

                        (send stream msg)
                    

sends a string over the TLS stream. Returns bytes sent or an error.

send-bytes

defn

(Fn [(Ref TlsStream a), (Ref (Array Byte) b)] (Result Int String))

                        (send-bytes stream data)
                    

sends binary data over the TLS stream. Returns bytes sent or an error.

set-timeout

external

(Fn [(Ref TlsStream a), Int] ())

sets read and write timeouts in seconds.

with-stream

macro

Macro

                        (with-stream name host port :rest forms)
                    

connects over TLS, executes forms, then closes the stream.