File: Connection.pm

package info (click to toggle)
libpoe-component-server-simplehttp-perl 2.30-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 324 kB
  • sloc: perl: 1,324; makefile: 7
file content (203 lines) | stat: -rw-r--r-- 4,586 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# Declare our package
package POE::Component::Server::SimpleHTTP::Connection;
$POE::Component::Server::SimpleHTTP::Connection::VERSION = '2.30';
#ABSTRACT: Stores connection information for SimpleHTTP

use strict;
use warnings;

use Socket (qw( AF_INET AF_INET6 AF_UNIX inet_ntop sockaddr_family
   unpack_sockaddr_in unpack_sockaddr_in6 unpack_sockaddr_un ));
use POE;

use Moose;

has dead => (
  is => 'rw',
  isa => 'Bool',
  default => 0,
);

has ssl => (
  is => 'rw',
  isa => 'Bool',
  default => 0,
);

has sslcipher => (
  is => 'rw',
  default => undef,
);

has remote_ip => (
  is => 'ro',
);

has remote_port => (
  is => 'ro',
);

has remote_addr => (
  is => 'ro',
);

has local_ip => (
  is => 'ro',
);

has local_port => (
  is => 'ro',
);

has local_addr => (
  is => 'ro',
);

has ID => (
  is => 'rw',
);

has OnClose => (
  is => 'ro',
  default => sub { { } },
);

sub BUILDARGS {
   my $class = shift;

   my $self = { };

   my $socket = shift;

   eval {
      my $family;
      ( $family, $self->{'remote_port'}, $self->{'remote_addr'},
         $self->{'remote_ip'}
      ) = $class->get_sockaddr_info( getpeername($socket) );

      ( $family, $self->{'local_port'}, $self->{'local_addr'},
         $self->{'local_ip'}
      ) = $class->get_sockaddr_info( getsockname($socket) );
   };

   if ($@) {
      return undef;
   }

   return $self;
}

sub _on_close {
   my ( $self, $sessionID, $state, @args ) = @_;
   if ($state) {
      $self->OnClose->{$sessionID} = [ $state, @args ];
      $poe_kernel->refcount_increment( $sessionID, __PACKAGE__ );
   }
   else {
      my $data = delete $self->OnClose->{$sessionID};
      $poe_kernel->refcount_decrement( $sessionID, __PACKAGE__ ) if $data;
   }
}

sub DEMOLISH {
   my ($self) = @_;
   while ( my ( $sessionID, $data ) = each %{ $self->OnClose || {} } ) {
      $poe_kernel->call( $sessionID, @$data );
      $poe_kernel->refcount_decrement( $sessionID, __PACKAGE__ );
   }
}

sub get_sockaddr_info {
   my $class = shift;
   my $sockaddr = shift;

   my $family = sockaddr_family( $sockaddr );
   my ( $port, $address, $straddress );
   if ( $family == AF_INET ) {
      ( $port, $address ) = unpack_sockaddr_in( $sockaddr );
      $straddress = inet_ntop( $family, $address );
   } elsif ( $family == AF_INET6 ) {
      ( $port, $address ) = unpack_sockaddr_in6( $sockaddr );
      $straddress = inet_ntop( $family, $address );
   } elsif ( $family == AF_UNIX ) {
      $address = unpack_sockaddr_un( $sockaddr );
      $straddress = $address // '<local>';
      $port = undef;
   } else {
      $address = $port = undef;
      $straddress = '<unknown>';
   }
   return ( $family, $address, $port, $straddress );
}

no Moose;

__PACKAGE__->meta->make_immutable;

# End of module
1;

__END__

=pod

=encoding UTF-8

=head1 NAME

POE::Component::Server::SimpleHTTP::Connection - Stores connection information for SimpleHTTP

=head1 VERSION

version 2.30

=head1 SYNOPSIS

	use POE::Component::Server::SimpleHTTP::Connection;
	my $connection = POE::Component::Server::SimpleHTTP::Connection->new( $socket );

	# Print some stuff
	print $connection->remote_port;

=head1 DESCRIPTION

	This module simply holds some information from a SimpleHTTP connection.

=head2 METHODS

	my $connection = POE::Component::Server::SimpleHTTP::Connection->new( $socket );

	$connection->remote_ip();	# Returns remote address as a string ( 1.1.1.1 or 2000::1 )
	$connection->remote_port();	# Returns remote port
	$connection->remote_addr();	# Returns true remote address, consult the L<Socket> POD
	$connection->local_addr();	# Returns true local address, same as above
	$connection->local_ip();	# Returns remote address as a string ( 1.1.1.1 or 2000::1 )
	$connection->local_port();	# Returns local port
	$connection->dead();		# Returns a boolean value whether the socket is closed or not
	$connection->ssl();		# Returns a boolean value whether the socket is SSLified or not
	$connection->sslcipher();	# Returns the SSL Cipher type or undef if not SSL
	$connection->ID();          # unique ID of this connection

=head2 EXPORT

Nothing.

=for Pod::Coverage DEMOLISH get_sockaddr_info

=head1 SEE ALSO

L<POE::Component::Server::SimpleHTTP>,
L<POE::Component::Server::SimpleHTTP::Response>

=head1 AUTHOR

Apocalypse <APOCAL@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2023 by Apocalypse, Chris Williams, Eriam Schaffter, Marlon Bailey and Philip Gwyn.

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

=cut