<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">##########################################################
# example HTTPS server using nonblocking sockets
# requires Event::Lib
# at the moment the response consists only of the HTTP
# request, send back as text/plain
##########################################################

use strict;
use IO::Socket;
use IO::Socket::SSL;
use Event::Lib;
use Errno ':POSIX';

#$Net::SSLeay::trace=3;

eval 'use Debug';
*{DEBUG} = sub {} if !defined(&amp;DEBUG);

# create server socket
my $server = IO::Socket::INET-&gt;new(
    LocalAddr =&gt; '0.0.0.0:9000',
    Listen =&gt; 10,
    Reuse =&gt; 1,
    Blocking =&gt; 0,
) || die $!;

event_new( $server, EV_READ|EV_PERSIST, \&amp;_s_accept )-&gt;add();
event_mainloop;

##########################################################
### accept new client on server socket
##########################################################
sub _s_accept {
    my $fds = shift-&gt;fh;
    my $fdc = $fds-&gt;accept || return;
    DEBUG( "new client" );

    $fdc = IO::Socket::SSL-&gt;start_SSL( $fdc,
	SSL_startHandshake =&gt; 0,
	SSL_server =&gt; 1,
    ) || die $!;

    $fdc-&gt;blocking(0);
    _ssl_accept( undef,$fdc );
}

##########################################################
### ssl handshake with client
### called again and again until the handshake is done
### this is called first from _s_accept w/o an event
### and later enters itself as new event until the 
### handshake is done
### if the handshake is done it inits the buffers for the 
### client socket and adds an event for reading the HTTP header
##########################################################
sub _ssl_accept {
    my ($event,$fdc) = @_;
    $fdc ||= $event-&gt;fh;
    if ( $fdc-&gt;accept_SSL ) {
	DEBUG( "new client ssl handshake done" );
	# setup the client
	${*$fdc}{rbuf} =  ${*$fdc}{wbuf} = '';
	event_new( $fdc, EV_READ, \&amp;_client_read_header )-&gt;add;
    } elsif ( $! != EWOULDBLOCK &amp;&amp; $! != EAGAIN ) {
	die "new client failed: $!|$SSL_ERROR";
    } else {
	DEBUG( "new client need to retry accept: $SSL_ERROR" );
	my $what = 
	    $SSL_ERROR == SSL_WANT_READ  ? EV_READ  :
	    $SSL_ERROR == SSL_WANT_WRITE ? EV_WRITE :
	    die "unknown error";
	event_new( $fdc, $what,  \&amp;_ssl_accept )-&gt;add;
    }
}

    
##########################################################
### read http header
### this will re-add itself as an event until the full
### http header was read
### after reading the header it will setup the response
### which will for now just send the header back as text/plain
##########################################################
sub _client_read_header {
    my $event = shift;
    my $fdc = $event-&gt;fh;
    DEBUG( "reading header" );
    my $rbuf_ref = \${*$fdc}{rbuf};
    my $n = sysread( $fdc,$$rbuf_ref,16384,length($$rbuf_ref));
    if ( !defined($n)) {
	die $! if $! != EWOULDBLOCK &amp;&amp; $! != EAGAIN;
	DEBUG( $SSL_ERROR );
	if ( $SSL_ERROR == SSL_WANT_WRITE ) {
	    # retry read once I can write
	    event_new( $fdc, EV_WRITE, \&amp;_client_read_header )-&gt;add;
	} else {
	    $event-&gt;add; # retry
	}
    } elsif ( $n == 0 ) {
	DEBUG( "connection closed" );
	close($fdc);
    } else {
	# check if we have the whole http header
	my $i = index( $$rbuf_ref,"\r\n\r\n" );   # check \r\n\r\n
	$i = index( $$rbuf_ref,"\n\n" ) if $i&lt;0;  # bad clients send \n\n only
	if ( $i&lt;0 ) {
	    $event-&gt;add; # read more from header
	    return;
	}

	# got full header, send request back (we don't serve real pages yet)
	my $header = substr( $$rbuf_ref,0,$i,'' );
	DEBUG( "got header:\n$header" );
	my $wbuf_ref = \${*$fdc}{wbuf};
	$$wbuf_ref = "HTTP/1.0 200 Ok\r\nContent-type: text/plain\r\n\r\n".$header;
	DEBUG( "will send $$wbuf_ref" );
	event_new( $fdc, EV_WRITE, \&amp;_client_write_response )-&gt;add;
    }
}

##########################################################
### this is called to write the response to the client
### this will re-add itself as an event as until the full
### response was send
### if it's done it will just close the socket
##########################################################
sub _client_write_response {
    my $event = shift;
    DEBUG( "writing response" );
    my $fdc = $event-&gt;fh;
    my $wbuf_ref = \${*$fdc}{wbuf};
    my $n = syswrite( $fdc,$$wbuf_ref );
    if ( !defined($n) &amp;&amp; ( $! == EWOULDBLOCK || $! == EAGAIN ) ) {
	# retry
	DEBUG( $SSL_ERROR );
	if ( $SSL_ERROR == SSL_WANT_READ ) {
	    # retry write once we can read
	    event_new( $fdc, EV_READ, \&amp;_client_write_response )-&gt;add;
	} else {
	    $event-&gt;add; # retry again
	}
    } elsif ( $n == 0 ) {
	DEBUG( "connection closed: $!" );
	close($fdc);
    } else {
	DEBUG( "wrote $n bytes" );
	substr($$wbuf_ref,0,$n,'' );
	if ($$wbuf_ref eq '') {
	    DEBUG( "done" );
	    close($fdc);
	} else {
	    # send more
	    $event-&gt;add
	}
    }
}

</pre></body></html>