HTTP-Promise

 view release on metacpan or  search on metacpan

lib/HTTP/Promise/IO.pm  view on Meta::CPAN

        unless( exists( $ssl_opts->{SSL_verifycn_scheme} ) )
        {
            $ssl_opts->{SSL_verifycn_scheme} = 'www'
        }
    }
    if( $ssl_opts->{SSL_verify_mode} )
    {
        unless( exists( $ssl_opts->{SSL_ca_file} ) || exists( $ssl_opts->{SSL_ca_path} ) )
        {
            $self->_load_class( 'Mozilla::CA' ) || return( $self->pass_error );
            $ssl_opts->{SSL_ca_file} = Mozilla::CA::SSL_ca_file();
        }
    }
    return( $ssl_opts );
}

sub FREEZE
{
    my $self = CORE::shift( @_ );
    my $serialiser = CORE::shift( @_ ) // '';
    my $class = CORE::ref( $self );
    my %hash  = %$self;
    CORE::delete( @hash{ qw( _fh ) } );
    if( CORE::exists( $hash{stop_if} ) && 
        CORE::defined( $hash{stop_if} ) && 
        CORE::ref( $hash{stop_if} ) )
    {
        require B::Deparse;
        my $deparse = B::Deparse->new( '-p', '-sC' );
        my $code = $deparse->coderef2text( CORE::delete( $hash{stop_if} ) );
        $hash{stop_if_code} = $code;
    }
    # Return an array reference rather than a list so this works with Sereal and CBOR
    CORE::return( [$class, \%hash] ) if( $serialiser eq 'Sereal' && Sereal::Encoder->VERSION <= version->parse( '4.023' ) );
    # But Storable want a list with the first element being the serialised element
    CORE::return( $class, \%hash );
}

sub STORABLE_freeze { CORE::return( CORE::shift->FREEZE( @_ ) ); }

sub STORABLE_thaw { CORE::return( CORE::shift->THAW( @_ ) ); }

sub THAW
{
    my( $self, undef, @args ) = @_;
    my $ref = ( CORE::scalar( @args ) == 1 && CORE::ref( $args[0] ) eq 'ARRAY' ) ? CORE::shift( @args ) : \@args;
    my $class = ( CORE::defined( $ref ) && CORE::ref( $ref ) eq 'ARRAY' && CORE::scalar( @$ref ) > 1 ) ? CORE::shift( @$ref ) : ( CORE::ref( $self ) || $self );
    my $hash = CORE::ref( $ref ) eq 'ARRAY' ? CORE::shift( @$ref ) : {};
    my $new;
    # Storable pattern requires to modify the object it created rather than returning a new one
    if( CORE::ref( $self ) )
    {
        foreach( CORE::keys( %$hash ) )
        {
            $self->{ $_ } = CORE::delete( $hash->{ $_ } );
        }
        $new = $self;
    }
    else
    {
        $new = bless( $hash => $class );
    }
    if( CORE::exists( $hash->{stop_if_code} ) && 
        CORE::defined( $hash->{stop_if_code} ) && 
        CORE::length( $hash->{stop_if_code} ) )
    {
        my $code = CORE::delete( $hash->{stop_if_code} );
        my $saved = $@;
        # "if you want to eval the result, you should prepend "sub subname ", or "sub " for an anonymous function constructor."
        # <https://metacpan.org/pod/B::Deparse#coderef2text>
        my $ref;
        {
            no strict;
            $ref = eval( "sub{ $code }" );
        }
        if( $@ )
        {
            $@ =~ s/ at .*\n//;
            die( $@ );
        }
        $@ = $saved;
        $new->{stop_if} = $ref;
    }
    CORE::return( $new );
}

1;
# NOTE: POD
__END__

=encoding utf-8

=head1 NAME

HTTP::Promise::IO - I/O Handling Class for HTTP::Promise

=head1 SYNOPSIS

    use HTTP::Promise::IO;
    my $this = HTTP::Promise::IO->new( $fh ) || 
        die( HTTP::Promise::IO->error, "\n" );

=head1 VERSION

    v0.1.0

=head1 DESCRIPTION

This class implements a filehandle reader and writer with a twist.

First off, it does not rely on lines, since data stream or in general data from HTTP requests and responses do not necessarily always contain lines. Binary data are sent without necessarily any line at all.

Second, it is easy on memory by implementing L</read>, which uses a shared L</buffer>, and you can use L</unread> to return data to it (they would be prepended).

Last, but not least, it implements 2 methods to read in chunks of data from the filehandle until some string pattern specified is found: L</read_until> and L</read_until_in_memory>

=head1 CONSTRUCTOR

=head2 new

This takes a proper filehandle and will ensure the C<O_NONBLOCK> bit is set, so that it can timeout if there is no more data streamed from the filehandle.

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 0.654 second using v1.00-cache-2.02-grep-82fe00e-cpan-2c419f77a38b )