Module std::net::tls

TLS client connections.

Provides encrypted TCP connections using TLS with system root certificates. Suitable for HTTPS clients, secure API calls, and any protocol that runs over TLS.

Examples

import std::net::tls;

fn main() {
    let stream = tls.connect("example.com", 443);
    let request = "GET / HTTP/1.1\r\nHost: example.com\r\nConnection: close\r\n\r\n";
    match tls.try_write(stream, hew_string_to_bytes(request)) {
        Ok(_) => {},
        Err(err) => panic(fs.io_error_message(err)),
    }
    let response = match tls.read(stream, 4096) {
        Ok(data) => data,
        Err(err) => panic(fs.io_error_message(err)),
    };
    tls.close(stream);
}

Contents

Functions

Function connect

pub fn connect(host: string, port: i64) -> TlsStream

Open a TLS connection to a remote host.

Uses system root certificates to verify the server. Returns a TlsStream on success or a zero-value on failure.

Examples

let stream = tls.connect("example.com", 443);

Function last_error

pub fn last_error() -> string

Return the last TLS client error observed on this thread.

Failed connect() calls update this string. Successful connects clear it.

Function write

pub fn write(stream: TlsStream, data: bytes) -> i64

Write raw bytes to a TLS stream.

Returns the number of bytes written, or −1 on error.

Function try_write

pub fn try_write(stream: TlsStream, data: bytes) -> Result<i64, fs.IoError>

Write raw bytes to a TLS stream.

Returns the number of bytes written, or Err(fs.IoError) on failure.

Function read

pub fn read(stream: TlsStream, size: i64) -> Result<bytes, fs.IoError>

Read up to size bytes from a TLS stream.

Returns Ok(bytes) on success or orderly EOF, or Err(fs.IoError) on retryable/TLS/I-O failures. Call last_error() for the precise detail.

Function close

pub fn close(stream: TlsStream)

Close a TLS connection and release resources.

Types

Struct TlsStream

An established TLS connection to a remote host.

Created by tls.connect(host, port). Must be released with close().

Traits

Trait TlsStreamMethods

Methods available on a TlsStream.

Methods

fn write(self: Self, data: bytes) -> i64

Write raw bytes to the TLS stream.

Returns the number of bytes written, or −1 on error.

fn try_write(self: Self, data: bytes) -> Result<i64, fs.IoError>

Write raw bytes to the TLS stream.

Returns the number of bytes written, or Err(fs.IoError) on failure.

fn read(self: Self, size: i64) -> Result<bytes, fs.IoError>

Read up to size bytes from the TLS stream.

Returns Ok(bytes) on success or orderly EOF, or Err(fs.IoError) on retryable/TLS/I-O failures. Use last_error() for the precise class.

fn close(self: Self)

Close the TLS connection and release resources.