File Coverage

blib/lib/POE/Component/Server/SimpleHTTP/Connection.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 20 90.0


line stmt bran cond sub pod time code
1             # Declare our package
2             package POE::Component::Server::SimpleHTTP::Connection;
3              
4 7     7   1323 use strict;
  7         8  
  7         230  
5 7     7   33 use warnings;
  7         13  
  7         345  
6              
7             our $VERSION = '2.18';
8              
9 7     7   35 use Socket qw( inet_ntoa unpack_sockaddr_in );
  7         9  
  7         354  
10 7     7   35 use POE;
  7         13  
  7         87  
11              
12 7     7   5620 use Moose;
  0            
  0            
13              
14             has dead => (
15             is => 'rw',
16             isa => 'Bool',
17             default => 0,
18             );
19              
20             has ssl => (
21             is => 'rw',
22             isa => 'Bool',
23             default => 0,
24             );
25              
26             has sslcipher => (
27             is => 'rw',
28             default => undef,
29             );
30              
31             has remote_ip => (
32             is => 'ro',
33             );
34              
35             has remote_port => (
36             is => 'ro',
37             );
38              
39             has remote_addr => (
40             is => 'ro',
41             );
42              
43             has local_ip => (
44             is => 'ro',
45             );
46              
47             has local_port => (
48             is => 'ro',
49             );
50              
51             has local_addr => (
52             is => 'ro',
53             );
54              
55             has ID => (
56             is => 'rw',
57             );
58              
59             has OnClose => (
60             is => 'ro',
61             default => sub { { } },
62             );
63              
64             sub BUILDARGS {
65             my $class = shift;
66              
67             my $self = { };
68              
69             my $socket = shift;
70              
71             eval {
72             ( $self->{'remote_port'}, $self->{'remote_addr'} ) =
73             unpack_sockaddr_in( getpeername($socket) );
74             $self->{'remote_ip'} = inet_ntoa( $self->{'remote_addr'} );
75              
76             ( $self->{'local_port'}, $self->{'local_addr'} ) =
77             unpack_sockaddr_in( getsockname($socket) );
78             $self->{'local_ip'} = inet_ntoa( $self->{'local_addr'} );
79             };
80              
81             if ($@) {
82             return undef;
83             }
84              
85             return $self;
86             }
87              
88             sub _on_close {
89             my ( $self, $sessionID, $state, @args ) = @_;
90             if ($state) {
91             $self->OnClose->{$sessionID} = [ $state, @args ];
92             $poe_kernel->refcount_increment( $sessionID, __PACKAGE__ );
93             }
94             else {
95             my $data = delete $self->OnClose->{$sessionID};
96             $poe_kernel->refcount_decrement( $sessionID, __PACKAGE__ ) if $data;
97             }
98             }
99              
100             sub DEMOLISH {
101             my ($self) = @_;
102             while ( my ( $sessionID, $data ) = each %{ $self->OnClose || {} } ) {
103             $poe_kernel->call( $sessionID, @$data );
104             $poe_kernel->refcount_decrement( $sessionID, __PACKAGE__ );
105             }
106             }
107              
108             no Moose;
109              
110             __PACKAGE__->meta->make_immutable;
111              
112             # End of module
113             1;
114              
115             __END__
116              
117             =head1 NAME
118              
119             POE::Component::Server::SimpleHTTP::Connection - Stores connection information for SimpleHTTP
120              
121             =head1 SYNOPSIS
122              
123             use POE::Component::Server::SimpleHTTP::Connection;
124             my $connection = POE::Component::Server::SimpleHTTP::Connection->new( $socket );
125              
126             # Print some stuff
127             print $connection->remote_port;
128              
129             =head1 DESCRIPTION
130              
131             This module simply holds some information from a SimpleHTTP connection.
132              
133             =head2 METHODS
134              
135             my $connection = POE::Component::Server::SimpleHTTP::Connection->new( $socket );
136              
137             $connection->remote_ip(); # Returns remote ip in dotted quad format ( 1.1.1.1 )
138             $connection->remote_port(); # Returns remote port
139             $connection->remote_addr(); # Returns true remote address, consult the L<Socket> POD
140             $connection->local_addr(); # Returns true local address, same as above
141             $connection->local_ip(); # Returns local ip in dotted quad format ( 1.1.1.1 )
142             $connection->local_port(); # Returns local port
143             $connection->dead(); # Returns a boolean value whether the socket is closed or not
144             $connection->ssl(); # Returns a boolean value whether the socket is SSLified or not
145             $connection->sslcipher(); # Returns the SSL Cipher type or undef if not SSL
146             $connection->ID(); # unique ID of this connection
147              
148             =head2 EXPORT
149              
150             Nothing.
151              
152             =head1 SEE ALSO
153              
154             L<POE::Component::Server::SimpleHTTP>,
155             L<POE::Component::Server::SimpleHTTP::Response>
156              
157             =head1 AUTHOR
158              
159             Apocalypse E<lt>apocal@cpan.orgE<gt>
160              
161             Chris C<BinGOs> Williams <chris@bingosnet.co.uk>
162              
163             =head1 COPYRIGHT AND LICENSE
164              
165             Copyright E<copy> Apocalypse and Chris Williams
166              
167             This library is free software; you can redistribute it and/or modify
168             it under the same terms as Perl itself.
169              
170             =cut