NAME

Future::IO::TLS - A TLS interface for Future::IO

VERSION

version 0.001

SYNOPSIS

use Future::IO;
Future::IO->load_best_impl;
use Future::AsyncAwait;

use Future::IO::TLS;
use Future::IO::Resolver;

async sub main($hostname, $secure) {
  my $port = $secure ? 'https' : 'http';
  my ($address) = await Future::IO::Resolver->getaddrinfo(host => $hostname, service => $port) or die;

  socket my $connection, $address->{family}, $address->{socktype}, $address->{protocol} or die;
  await Future::IO->connect($connection, $address->{addr});
  my $ssl = $secure ? await Future::IO::TLS->start_TLS($connection, hostname => $hostname) : 'Future::IO';

  await $ssl->write($connection, "GET / HTTP/1.1\r\nHost: $hostname\r\n\r\n");
  my $response = await $ssl->read($connection, 2048);

  say $response;
};

my $main = main('www.google.com', 1);
$main->get;

DESCRIPTION

This is a fully asynchronous TLS implementation for Future::IO, based on Crypt::OpenSSL3.

METHODS

start_TLS

my $tls = Future::IO::TLS->start_TLS($fh, %options);

This initiates a TLS handshake to upgrade the connection to using TLS. It will return a TLS connection object that should be used as invocant instead of Future::IO when calling read or write.

It takes the following optional named arguments:

  • server

    If true the connection will take the accepting role in the handshake, otherwise it will take the connecting role.

  • context

    An TLS Context used to base connections on.

  • hostname

    The hostname of the other side of the connection. Typically used for client connections.

  • private_key_file

    The location of the private key file. Typically used for server connections.

  • certificate_chain_file

    The location of the certificate chain file. Typically used for server connections.

connect

my $tls = Future::IO::TLS->connect($fh, $sockaddr, %options);

This combines Future::IO->connect with Future::IO::TLS->start_TLS. You probably want to pass this a hostname parameter, otherwise the peer's identity can't be verified.

accept

my $tls = Future::IO::TLS->accept($fh, $sockaddr, %options);

This combines Future::IO->accept with Future::IO::TLS->start_TLS. You probably want to pass this the private_key_file and certificate_chain_file arguments.

read

my $data = await $io->read($fh, $size);

Read $size bytes from $fh using TLS.

write

my $written = await $io->write($fh, $data);

Write $data to $fh using TLS.

AUTHOR

Leon Timmermans <fawaka@gmail.com>

COPYRIGHT AND LICENSE

This software is copyright (c) 2026 by Leon Timmermans.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.