moonbitlang/core/async/tls does not have a README file

    TlsError

    TLS related error
    impl Show for TlsError

    TlsError::to_string

    fn TlsError::to_string(self : TlsError) -> String

    Tls

    type Tls

    A TLS-encrypted connection
    impl BinaryReader for Tls
    impl BinaryWriter for Tls

    Tls::client

    Create a TLS client that read from write to inner. client will block until TLS handshake to remote server completed.

    trust specifies which servers are trusted and how certificate validation is performed. See TrustedRoot for more details. The default is SystemRoot.

    If host is present, it will be used to verify the peer's certificate.

    If host is present and sni is true (true by default), Server Name Indication (SNI) field of TLS will be set to host. sni=false is currently unsupported on Windows.

    If require_shutdown is true (true by default), the TLS connection will raise an error if the peer closes the underlying transport without performing normal TLS shutdown. If the client is the side that initiates termination, .shutdown() should be called so that the remote peer can reliably detect termination.

    TLS shutdown is necessary to safely detect termination of the TLS stream, however protocols with their own message delimiter, such as HTTP, does not rely on TLS to detect stream termination. In this case, TLS shutdown is not necessary.

    Tls::client_from_pair

    Create a TLS client that read from r and write to w. client_from_pair will block until TLS handshake to remote server completed.

    trust specifies which servers are trusted and how certificate validation is performed. See TrustedRoot for more details. The default is SystemRoot.

    If host is present, it will be used to verify the peer's certificate.

    If host is present and sni is true (true by default), Server Name Indication (SNI) field of TLS will be set to host.

    Tls::close

    fn Tls::close(self : Tls) -> Unit

    Close a TLS connection and release related resource. THIS FUNCTION MUST BE CALLED BEFORE CLOSING UNDERLYING TRANSPORT!!!

    Note that .close() does not provide reliable termination indication for the remote peer. If protected termination is desirable, call .shutdown() before .close().

    Tls::drop_all

    async fn Tls::drop_all(self : Tls) -> Int64 raise
    ReaderError

    Drop all remaining content from a binary reader, until EOF is reached.

    Tls::drop_exactly

    async fn Tls::drop_exactly(self : Tls, len : Int) -> Unit raise
    ReaderError

    reader.drop_exactly(len) drops exactly len bytes from the reader. drop_exactly waits until enough bytes are available. If EOF is reached before enough data is dropped, UnexpectedEof will be raised.

    Tls::flush_partial_data

    async fn Tls::flush_partial_data(self : Tls) -> Unit

    Flush all partial data and ensure they are delivered immediately. For writer transformers such as gzip writer, the underlying transport will be flushed as well. The flush is only best-effort: underlying implementation may choose to still buffer some data.

    flush_partial_data IS NOT used to ensure stream integrity: stream termination such as @tls.Tls::shutdown() is a protocol-specific operation. The only purpose of flush_partial_data is to ensure timely delivery of partial data. If the user only cares about eventual integrity of data, flush_partial_data is always UNNECESSARY.

    flush_partial_data mainly deal with userland buffering. It does not, for example, synchronize data to disk for a file system writer.

    Tls::get_peer_certificate

    fn Tls::get_peer_certificate(self : Tls) -> Bytes? raise

    Get the certificate of remote peer for a TLS connection in DER format. Note that even if nothing goes wrong, the certificate may not exist:

    • although rare, some TLS algorithm choice does not have a certificate
    • for connection without client certificate, the server does not have any peer certificate

    Therefore the return type is Bytes? instead of Bytes.

    Tls::peek

    async fn Tls::peek(self : Tls) -> BytesView? raise
    ReaderError

    reader.peek() fetches and returns a chunk of data from the reader. The returned data will not get consumed from the reader: other reader API can still fetch them later.

    The chunk returned by reader.peek() is a copy-free slice of the reader's internal buffer, and its content will be overwritten by subsequent reader operations. So caller of peek MUST NOT use the returned slice after any other BinaryReader operation is invoked on the same reader. If the data need to be kept persistent, copy it via .to_owned().

    The returned chunk will contain at least one byte. If EOF is reached before any data is fetched, None will be returned.

    reader.peek() is cancellation safe as long as the underlying reader implementation is cancellation safe. When reader.peek() is cancelled, no data will be lost.

    Tls::peek_at_least

    async fn Tls::peek_at_least(self : Tls, min_len : Int) -> BytesView raise
    ReaderError

    reader.peek_at_least(min_len) fetches and returns a chunk containing at least min_len bytes without consuming it. The returned chunk may be larger than min_len.

    The returned view is valid only until another operation is invoked on the same reader. Use .to_owned() to keep the data persistently.

    If EOF is reached before enough data is fetched, UnexpectedEof will be raised. Any shorter trailing data remains buffered and can be fetched by a later reader operation.

    reader.peek_at_least() is cancellation safe as long as the underlying reader implementation is cancellation safe. When it is cancelled, no data will be lost.

    Tls::peek_exactly

    async fn Tls::peek_exactly(self : Tls, len : Int) -> BytesView raise
    ReaderError

    reader.peek_exactly(len) fetches exactly len bytes from the reader. It will wait until enough number of bytes is available. The returned data will not get consumed from the reader: other reader API can still fetch them later.

    The chunk returned by reader.peek_exactly() is a copy-free slice of the reader's internal buffer, and its content will be overwritten by subsequent reader operations. So caller of peek MUST NOT use the returned slice after any other BinaryReader operation is invoked on the same reader. If the data need to be kept persistent, copy it via .to_owned().

    If EOF is reached before enough data is read, UnexpectedEof will be raised. In this case remaining data remain buffered, and can be fetched by other reader operations later.

    reader.peek_exactly() is cancellation safe as long as the underlying reader implementation is cancellation safe. When reader.peek_exactly() is cancelled, no data will be lost.

    Tls::peek_until

    async fn Tls::peek_until(self : Tls, pattern : BytesView, include_delimiter? : Bool) -> BytesView? raise
    ReaderError

    reader.peek_until(pat, include_delimiter? : Bool = false) fetches content from reader until the byte string pat is found in the reader stream. The returned data will not get consumed from the reader: other reader API can still fetch them later.

    The chunk returned by reader.peek_until() is a copy-free slice of the reader's internal buffer, and its content will be overwritten by subsequent reader operations. So caller of peek MUST NOT use the returned slice after any other BinaryReader operation is invoked on the same reader. If the data need to be kept persistent, copy it via .to_owned().

    If pat is found in the input stream, content before that occurrence is returned. If include_delimiter is true, pat itself is included at the end of the result. The returned data may be empty in this case if include_delimiter is false.

    EOF is implicitly treated as a delimiter by reader.peek_until(). If pat is not found in the input stream when EOF is reached, trailing data before EOF will be returned (or None if there is no trailing data). In this case, the returned data contains no trailing delimiter even if include_delimiter=true. This can be used to detect the presence of trailing delimiter in the input stream.

    reader.peek_until() is cancellation safe as long as the underlying reader implementation is cancellation safe. When reader.peek_until() is cancelled, no data will be lost.

    Tls::read

    async fn Tls::read(self : Tls) -> Bytes? raise
    ReaderError

    reader.read() fetches and returns a chunk of data from the reader. It will return as fast as possible when new data become available.

    The returned chunk will contain at least one byte. If EOF is reached before any data is fetched, None will be returned.

    reader.read() is cancellation safe as long as the underlying reader implementation is cancellation safe. When reader.read() is cancelled, no data will be lost.

    Tls::read_all

    async fn Tls::read_all(self : Tls) -> Bytes raise
    ReaderError

    Read all remaining content from a binary reader, until EOF is reached.

    Tls::read_at_least

    async fn Tls::read_at_least(self : Tls, min_len : Int) -> Bytes raise
    ReaderError

    reader.read_at_least(min_len) fetches and returns an owned chunk containing at least min_len bytes. The returned chunk may be larger than min_len.

    If EOF is reached before enough data is fetched, UnexpectedEof will be raised. Any shorter trailing data remains buffered and can be fetched through another reader operation later.

    reader.read_at_least() is cancellation safe as long as the underlying reader implementation is cancellation safe. When it is cancelled, no data will be lost.

    Tls::read_exactly

    async fn Tls::read_exactly(self : Tls, len : Int) -> Bytes raise
    ReaderError

    reader.read_exactly(len) reads exactly len bytes from the reader. It will wait until enough number of bytes is available. If EOF is reached before enough data is read, UnexpectedEof will be raised.

    Tls::read_into

    async fn Tls::read_into(self : Tls, dst : MutArrayView[Byte]) -> Int raise
    ReaderError

    reader.read_into(dst) reads at most dst.length() bytes from the reader into dst. The number of bytes actually read is returned. Note that the return value may be smaller than dst.length() if not enough data is immediately available. If EOF is reached and no more data is available, zero will be returned.

    Tls::read_until

    async fn Tls::read_until(self : Tls, pattern : BytesView, include_delimiter? : Bool) -> Bytes? raise
    ReaderError

    reader.read_until(pat, include_delimiter? : Bool = false) reads content from reader until the byte string pat is found in the reader stream.

    If pat is found in the input stream, content before that occurrence is returned and the delimiter is consumed. If include_delimiter is true, pat itself is included at the end of the result. The returned data may be empty in this case if include_delimiter is false.

    EOF is implicitly treated as a delimiter by reader.read_until(). If pat is not found in the input stream when EOF is reached, trailing data before EOF will be returned (or None if there is no trailing data). In this case, the returned data contains no trailing delimiter even if include_delimiter=true. This can be used to detect the presence of trailing delimiter in the input stream.

    reader.read_until() is cancellation safe as long as the underlying reader implementation is cancellation safe. When reader.read_until() is cancelled, no data will be lost.

    Tls::server_endpoint_channel_binding

    fn Tls::server_endpoint_channel_binding(self : Tls) -> Bytes raise

    Return tls-server-endpoint type of channel binding data for this TLS connection, according to RFC 5929. Not all TLS connection has such thing as a server certificate, so tls-unique is the more recommended approach when available.

    Tls::shutdown

    async fn Tls::shutdown(self : Tls) -> Unit

    Shutdown a TLS connection gracefully. This function MUST be called before close, and MUST NOT be called if the TLS connection fail with other error.

    shutdown is used to initiate the closure of a TLS connection. So there is no need to call shutdown if you receive EOF from the peer.

    When calling shutdown, there may still be pending data sent by the peer on the wire. So to close a TLS connection cleanly, make sure you read from the connection until EOF after calling shutdown.

    Note that the main purpose of TLS shutdown is to ensure integrity before closing the underlying transport. So if your application protocol has its own way of ensuring integrity (e.g. Content-Length in HTTP/1.1), it is not necessary to call shutdown.

    Tls::unique_channel_binding

    fn Tls::unique_channel_binding(self : Tls) -> Bytes raise

    Return tls-unique type of channel binding data for this TLS connection, according to RFC 5929

    Tls::write

    async fn Tls::write(self : Tls, data : BytesView) -> Unit raise
    WriterError

    writer.write(data) writes a chunk of data into the writer. write will ensure that all content of data has been written before returning. However, write only guarantees that all data is committed to the underlying writer object; it does not guarantee writer-specific properties, such as written data being flushed to persistent storage for a file stream.

    Tls::write_reader

    async fn Tls::write_reader(self : Tls, reader : &
    BinaryReader
    ) -> Unit

    writer.write_reader(reader) continuously reads from input stream reader and writes all its content to writer until EOF is reached.

    write_reader only raises two kinds of error: @io.ReaderError from the reader, or @io.WriterError from the writer.

    TrustedRoot

    pub(all) enum TrustedRoot {
    NoVerification
    SystemRoot
    CustomPemFile(String)
    }

    Specify the trusted root when performing TLS certificate validation.

    • NoVerification: disable certificate validation. This destroys the whole purpose of using TLS and should only be used for testing purpose.

    • SystemRoot: use the default trusted root of the system

    • CustomPemFile(name): use the certificates in the PEM file name exclusively as trusted root. Useful for connecting to services whose certificates are issued by a private or self-signed root.