1# Declare our package 2package POE::Component::Server::SimpleHTTP::Connection; 3$POE::Component::Server::SimpleHTTP::Connection::VERSION = '2.28'; 4#ABSTRACT: Stores connection information for SimpleHTTP 5 6use strict; 7use warnings; 8 9use Socket (qw( AF_INET AF_INET6 AF_UNIX inet_ntop sockaddr_family 10 unpack_sockaddr_in unpack_sockaddr_in6 unpack_sockaddr_un )); 11use POE; 12 13use Moose; 14 15has dead => ( 16 is => 'rw', 17 isa => 'Bool', 18 default => 0, 19); 20 21has ssl => ( 22 is => 'rw', 23 isa => 'Bool', 24 default => 0, 25); 26 27has sslcipher => ( 28 is => 'rw', 29 default => undef, 30); 31 32has remote_ip => ( 33 is => 'ro', 34); 35 36has remote_port => ( 37 is => 'ro', 38); 39 40has remote_addr => ( 41 is => 'ro', 42); 43 44has local_ip => ( 45 is => 'ro', 46); 47 48has local_port => ( 49 is => 'ro', 50); 51 52has local_addr => ( 53 is => 'ro', 54); 55 56has ID => ( 57 is => 'rw', 58); 59 60has OnClose => ( 61 is => 'ro', 62 default => sub { { } }, 63); 64 65sub BUILDARGS { 66 my $class = shift; 67 68 my $self = { }; 69 70 my $socket = shift; 71 72 eval { 73 my $family; 74 ( $family, $self->{'remote_port'}, $self->{'remote_addr'}, 75 $self->{'remote_ip'} 76 ) = $class->get_sockaddr_info( getpeername($socket) ); 77 78 ( $family, $self->{'local_port'}, $self->{'local_addr'}, 79 $self->{'local_ip'} 80 ) = $class->get_sockaddr_info( getsockname($socket) ); 81 }; 82 83 if ($@) { 84 return undef; 85 } 86 87 return $self; 88} 89 90sub _on_close { 91 my ( $self, $sessionID, $state, @args ) = @_; 92 if ($state) { 93 $self->OnClose->{$sessionID} = [ $state, @args ]; 94 $poe_kernel->refcount_increment( $sessionID, __PACKAGE__ ); 95 } 96 else { 97 my $data = delete $self->OnClose->{$sessionID}; 98 $poe_kernel->refcount_decrement( $sessionID, __PACKAGE__ ) if $data; 99 } 100} 101 102sub DEMOLISH { 103 my ($self) = @_; 104 while ( my ( $sessionID, $data ) = each %{ $self->OnClose || {} } ) { 105 $poe_kernel->call( $sessionID, @$data ); 106 $poe_kernel->refcount_decrement( $sessionID, __PACKAGE__ ); 107 } 108} 109 110sub get_sockaddr_info { 111 my $class = shift; 112 my $sockaddr = shift; 113 114 my $family = sockaddr_family( $sockaddr ); 115 my ( $port, $address, $straddress ); 116 if ( $family == AF_INET ) { 117 ( $port, $address ) = unpack_sockaddr_in( $sockaddr ); 118 $straddress = inet_ntop( $family, $address ); 119 } elsif ( $family == AF_INET6 ) { 120 ( $port, $address ) = unpack_sockaddr_in6( $sockaddr ); 121 $straddress = inet_ntop( $family, $address ); 122 } elsif ( $family == AF_UNIX ) { 123 $address = unpack_sockaddr_un( $sockaddr ); 124 $straddress = $address // '<local>'; 125 $port = undef; 126 } else { 127 $address = $port = undef; 128 $straddress = '<unknown>'; 129 } 130 return ( $family, $address, $port, $straddress ); 131} 132 133no Moose; 134 135__PACKAGE__->meta->make_immutable; 136 137# End of module 1381; 139 140__END__ 141 142=pod 143 144=encoding UTF-8 145 146=head1 NAME 147 148POE::Component::Server::SimpleHTTP::Connection - Stores connection information for SimpleHTTP 149 150=head1 VERSION 151 152version 2.28 153 154=head1 SYNOPSIS 155 156 use POE::Component::Server::SimpleHTTP::Connection; 157 my $connection = POE::Component::Server::SimpleHTTP::Connection->new( $socket ); 158 159 # Print some stuff 160 print $connection->remote_port; 161 162=head1 DESCRIPTION 163 164 This module simply holds some information from a SimpleHTTP connection. 165 166=head2 METHODS 167 168 my $connection = POE::Component::Server::SimpleHTTP::Connection->new( $socket ); 169 170 $connection->remote_ip(); # Returns remote address as a string ( 1.1.1.1 or 2000::1 ) 171 $connection->remote_port(); # Returns remote port 172 $connection->remote_addr(); # Returns true remote address, consult the L<Socket> POD 173 $connection->local_addr(); # Returns true local address, same as above 174 $connection->local_ip(); # Returns remote address as a string ( 1.1.1.1 or 2000::1 ) 175 $connection->local_port(); # Returns local port 176 $connection->dead(); # Returns a boolean value whether the socket is closed or not 177 $connection->ssl(); # Returns a boolean value whether the socket is SSLified or not 178 $connection->sslcipher(); # Returns the SSL Cipher type or undef if not SSL 179 $connection->ID(); # unique ID of this connection 180 181=head2 EXPORT 182 183Nothing. 184 185=for Pod::Coverage DEMOLISH get_sockaddr_info 186 187=head1 SEE ALSO 188 189L<POE::Component::Server::SimpleHTTP>, 190L<POE::Component::Server::SimpleHTTP::Response> 191 192=head1 AUTHOR 193 194Apocalypse <APOCAL@cpan.org> 195 196=head1 COPYRIGHT AND LICENSE 197 198This software is copyright (c) 2018 by Apocalypse, Chris Williams, Eriam Schaffter, Marlon Bailey and Philip Gwyn. 199 200This is free software; you can redistribute it and/or modify it under 201the same terms as the Perl 5 programming language system itself. 202 203=cut 204