CGI/Portable/AdapterSocket.pm

filter: Statementstmt 15.1% 15 / 99 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 47 MC/DCmcdc 0.0% 0 / 27 Subroutinesub 36.3% 4 / 11 Podpod 100% 5 / 5 totaltotal 10.9% 24 / 219 CC 36 SCAR 59.1
CC
36
Cov
9%
fetch_user_input
650.0
_send_output
72.0
send_user_output
20.0
CRAP 368.0
↑ Summary CGI/Portable/AdapterCGI.pm »
1 =head1 NAME
2  
3 CGI::Portable::AdapterSocket - Run under IO::Socket-based Perl server
4  
5 =cut
6  
7 ######################################################################
8  
9 package CGI::Portable::AdapterSocket;
10 1 require 5.004;
11  
12 # Copyright (c) 1999-2004, Darren R. Duncan. All rights reserved. This module
13 # is free software; you can redistribute it and/or modify it under the same terms
14 # as Perl itself. However, I do request that this copyright information and
15 # credits remain attached to the file. If you modify this module and
16 # redistribute a changed version then please attach a note listing the
17 # modifications. This module is available "as-is" and the author can not be held
18 # accountable for any problems resulting from its use.
19  
20 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 $VERSION = '0.50';
24  
25 ######################################################################
26  
27 =head1 DEPENDENCIES
28  
29 =head2 Perl Version
30  
31 5.004
32  
33 =head2 Standard Modules
34  
35 IO::Socket -- IO::Socket::INET built in
36  
37 =head2 Nonstandard Modules
38  
39 CGI::Portable 0.50
40  
41 =cut
42  
43 ######################################################################
44  
45 1 ▶use IO::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 ######################################################################
48  
49 =head1 SYNOPSIS
50  
51 =head2 Content of thin shell "startup_socket.pl" for IO::Socket::INET:
52  
53 #!/usr/bin/perl
54 use strict;
55 use warnings;
56  
57 print "[Server $0 starting up]\n";
58  
59 require CGI::Portable;
60 my $globals = CGI::Portable->new();
61  
62 use Cwd;
63 $globals->file_path_root( cwd() ); # let us default to current working directory
64 $globals->file_path_delimiter( $^O=~/Mac/i ? ":" : $^O=~/Win/i ? "\\" : "/" );
65  
66 $globals->set_prefs( 'config.pl' );
67 $globals->current_user_path_level( 1 );
68  
69 require CGI::Portable::AdapterSocket;
70 my $io = CGI::Portable::AdapterSocket->new();
71  
72 use IO::Socket;
73 my $server = IO::Socket::INET->new(
74 Listen => SOMAXCONN,
75 LocalAddr => '127.0.0.1',
76 LocalPort => 1984,
77 Proto => 'tcp'
78 );
79 die "[Error: can't setup server $0]" unless $server;
80  
81 print "[Server $0 accepting clients]\n";
82  
83 while( my $client = $server->accept() ) {
84 printf "%s: [Connect from %s]\n", scalar localtime, $client->peerhost;
85  
86 my $content = $globals->make_new_context();
87  
88 $io->fetch_user_input( $content, $client );
89 $content->call_component( 'DemoAardvark' );
90 $io->send_user_output( $content, $client );
91  
92 close $client;
93  
94 printf "%s http://%s:%s%s %s\n", $content->request_method,
95 $content->server_domain, $content->server_port,
96 $content->user_path_string, $content->http_status_code;
97 }
98  
99 1;
100  
101 =head1 DESCRIPTION
102  
103 This Perl 5 object class is an adapter for CGI::Portable that takes care of the
104 details for gathering user input and sending user output when this Perl script
105 is the HTTP server itself, and IO::Socket (IO::Socket::INET) is being used for
106 networking with the HTTP client.
107  
108 =head1 SYNTAX
109  
110 This class does not export any functions or methods, so you need to call them
111 using object notation. This means using B<Class-E<gt>function()> for functions
112 and B<$object-E<gt>method()> for methods. If you are inheriting this class for
113 your own modules, then that often means something like B<$self-E<gt>method()>.
114  
115 =head1 FUNCTIONS AND METHODS
116  
117 =head2 new()
118  
119 This function creates a new CGI::Portable::AdapterSocket object and returns it.
120 The new object has no properties, but only methods.
121  
122 =cut
123  
124 ######################################################################
125  
126 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
127 0 my $class = shift( @_ );
128 0 ▶ my $self = bless( {}, ref($class) || $class );
Condition0%0/3
ref $class || $class
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table0%0/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
129 0 return( $self );
130 }
131  
132 ######################################################################
133  
134 =head2 fetch_user_input( GLOBALS, CLIENT )
135  
136 This method takes a CGI::Portable object as its first argument, GLOBALS, and
137 feeds it all of the HTTP request and user input details that it can gather.
138 The second argument, CLIENT, is an IO::Socket::INET object which is the client
139 we will be getting our input from.
140 The user_path() is always initialized from the requested uri by this method;
141 if you want it to be from a query param then you can update it yourself later.
142 For debugging purposes, this method returns two strings containing the
143 un-parsed HTTP request headers and body respectively, should you want to inspect
144 them for yourself later.
145  
146 =cut
147  
148 ######################################################################
149  
150 0 ▶sub fetch_user_input {
Subroutine fetch_user_input: not called CC=25 SCAR=64.8
Pod documented
151 0 my ($self, $globals, $client) = @_;
152  
153 0 ▶ $globals->server_ip( $client->sockhost() || '127.0.0.1' ); # number
Condition0%0/2
$client->sockhost || '127.0.0.1'
l!l
00
MC/DC0%0/1
$client->sockhost || '127.0.0.1'
$client->sockhost
Truth table0%0/2
A
A= $client->sockhost
Aresult
11
00
154 0 ▶ $globals->server_domain( $client->sockaddr() || 'localhost' ); # domain
Condition0%0/2
$client->sockaddr || 'localhost'
l!l
00
MC/DC0%0/1
$client->sockaddr || 'localhost'
$client->sockaddr
Truth table0%0/2
A
A= $client->sockaddr
Aresult
11
00
155 0 ▶ $globals->server_port( $client->sockport() || 80 );
Condition0%0/2
$client->sockport || 80
l!l
00
MC/DC0%0/1
$client->sockport || 80
$client->sockport
Truth table0%0/2
A
A= $client->sockport
Aresult
11
00
156 0 ▶ $globals->client_ip( $client->peerhost() || '127.0.0.1' ); # number
Condition0%0/2
$client->peerhost || '127.0.0.1'
l!l
00
MC/DC0%0/1
$client->peerhost || '127.0.0.1'
$client->peerhost
Truth table0%0/2
A
A= $client->peerhost
Aresult
11
00
157 0 ▶ $globals->client_domain( $client->peeraddr() || $client->peerhost() ||
Condition0%0/5
$client->peeraddr || $client->peerhost || 'localhost'
l!l
00
$client->peeraddr or $client->peerhost
l!l&&r!l&&!r
000
MC/DC0%0/2
$client->peeraddr || $client->peerhost || 'localhost'
$client->peeraddr $client->peerhost
Truth table0%0/3
A or B
A= $client->peeraddrB= $client->peerhost
ABresult
1X1
011
000
158 'localhost' ); # domain
159 0 $globals->client_port( $client->peerport() );
160  
161 0 my $host = $globals->server_domain();
162 0 my $port = $globals->server_port();
163 0 ▶ $globals->url_base( 'http://'.$host.($port != 80 ? ":$port" : '') );
Branch: 1 branch
BranchTrueFalseTotal
$port != 80 ? : 0 0 0
164  
165 0 my $endl = "\015\012"; # cr + lf
166 0 local $\ = $endl.$endl;
167 0 local $/ = $endl.$endl;
168 0 my $raw_http_headers = <$client>;
169 0 my ($request, @headers_in) = grep( /\w/, split( $endl, $raw_http_headers ) );
170  
171 0 my ($method, $uri, $protocol) = grep( /\S/, split( /\s/, $request ) );
172 0 ▶ $globals->request_method( $method || 'GET' );
Condition0%0/2
$method || 'GET'
l!l
00
MC/DC0%0/1
$method || 'GET'
$method
Truth table0%0/2
A
A= $method
Aresult
11
00
173 0 ▶ $globals->request_uri( $uri || '/' );
Condition0%0/2
$uri || '/'
l!l
00
MC/DC0%0/1
$uri || '/'
$uri
Truth table0%0/2
A
A= $uri
Aresult
11
00
174 0 ▶ $globals->request_protocol( $protocol || 'HTTP/1.0' );
Condition0%0/2
$protocol || 'HTTP/1.0'
l!l
00
MC/DC0%0/1
$protocol || 'HTTP/1.0'
$protocol
Truth table0%0/2
A
A= $protocol
Aresult
11
00
175 0 my ($path, $query) = split( /\?/, $uri );
176 0 $globals->user_path( $self->_uri_unescape( $path ) );
177 0 $globals->user_query( $query );
178  
179 0 my $content_length = 0;
180 0 foreach my $header_in (@headers_in) {
181 0 my ($key, $value) = split( ": ", $header_in );
182 0 $key = lc( $key );
183 0 ▶ $key eq 'host' and do {
Branch: 1 branch
BranchTrueFalseTotal
$key eq "host" and do { () = split(/:/, $value, 3); $hdomain and $globals->server_domain($hdomain); $hport and $globals->server_port($hport) } and next 0 0 0
Condition0%0/3
$key eq "host" and do { () = split(/:/, $value, 3); $hdomain and $globals->server_domain($hdomain); $hport and $globals->server_port($hport) }
!ll&&!rl&&r
000
MC/DC0%0/2
$key eq "host" and do { () = split(/:/, $value, 3); $hdomain and $globals->server_domain($hdomain); $hport and $globals->server_port($hport) }
$key eq "host" do { () = split(/:/, $value, 3); $hdomain and $globals->server_domain($hdomain); $hport and $globals->server_port($hport) }
Truth table0%0/3
A and B
A= $key eq "host"B= do { () = split(/:/, $value, 3); $hdomain and $globals->server_domain($hdomain); $hport and $globals->server_port($hport) }
ABresult
0X0
100
111
184 0 my ($hdomain, $hport) = split( ":", $value );
185 0 ▶ $hdomain and $globals->server_domain( $hdomain );
Branch: 1 branch
BranchTrueFalseTotal
$hdomain and $globals->server_domain($hdomain) 0 0 0
186 0 ▶ $hport and $globals->server_port( $hport );
Condition0%0/3
$hport && $globals->server_port($hport)
!ll&&!rl&&r
000
MC/DC0%0/2
$hport && $globals->server_port($hport)
$hport $globals->server_port($hport)
Truth table0%0/3
A && B
A= $hportB= $globals->server_port($hport)
ABresult
0X0
100
111
187 } and next;
188 0 ▶ $key eq 'referer' and
Branch: 1 branch
BranchTrueFalseTotal
$key eq "referer" and $globals->referer($self->_uri_unescape($value)) and next 0 0 0
Condition0%0/3
$key eq "referer" and $globals->referer($self->_uri_unescape($value))
!ll&&!rl&&r
000
MC/DC0%0/2
$key eq "referer" and $globals->referer($self->_uri_unescape($value))
$key eq "referer" $globals->referer($self->_uri_unescape($value))
Truth table0%0/3
A and B
A= $key eq "referer"B= $globals->referer($self->_uri_unescape($value))
ABresult
0X0
100
111
189 $globals->referer( $self->_uri_unescape( $value ) ) and next;
190 0 ▶ $key eq 'user-agent' and $globals->user_agent( $value ) and next;
Branch: 1 branch
BranchTrueFalseTotal
$key eq "user-agent" and $globals->user_agent($value) and next 0 0 0
Condition0%0/3
$key eq "user-agent" and $globals->user_agent($value)
!ll&&!rl&&r
000
MC/DC0%0/2
$key eq "user-agent" and $globals->user_agent($value)
$key eq "user-agent" $globals->user_agent($value)
Truth table0%0/3
A and B
A= $key eq "user-agent"B= $globals->user_agent($value)
ABresult
0X0
100
111
191 0 ▶ $key eq 'cookie' and $globals->user_cookies( $value ) and next;
Branch: 1 branch
BranchTrueFalseTotal
$key eq "cookie" and $globals->user_cookies($value) and next 0 0 0
Condition0%0/3
$key eq "cookie" and $globals->user_cookies($value)
!ll&&!rl&&r
000
MC/DC0%0/2
$key eq "cookie" and $globals->user_cookies($value)
$key eq "cookie" $globals->user_cookies($value)
Truth table0%0/3
A and B
A= $key eq "cookie"B= $globals->user_cookies($value)
ABresult
0X0
100
111
192 0 ▶ $key =~ /length/ and $content_length = $value and next;
Branch: 1 branch
BranchTrueFalseTotal
$key =~ /length/ and $content_length = $value and next 0 0 0
Condition0%0/3
$key =~ /length/ and $content_length = $value
!ll&&!rl&&r
000
MC/DC0%0/2
$key =~ /length/ and $content_length = $value
$key =~ /length/ $content_length = $value
Truth table0%0/3
A and B
A= $key =~ /length/B= $content_length = $value
ABresult
0X0
100
111
193 }
194  
195 0 my $raw_http_body = '';
196 0 ▶ if( $content_length > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content_length > 0) 0 0 0
197 0 my $raw_http_body = '';
198 0 read( $client, $raw_http_body, $content_length );
199 0 chomp( $raw_http_body );
200 0 $globals->user_post( $raw_http_body );
201 }
202  
203 0 return( $raw_http_headers, $raw_http_body ); # for debugging
204 }
205  
206 # _uri_unescape( STRING )
207 # This private method takes a string in the argument STRING, and returns
208 # an uri-unescaped version of it.
209  
210 0 ▶sub _uri_unescape {
Subroutine _uri_unescape: not called CC=1 SCAR=6.9
211 0 my ($self, $str) = @_;
212 0 $str =~ tr/+/ /;
213 0 $str =~ s/%([0-9a-fA-F]{2})/pack("c",hex($1))/ge;
214 0 return( $str );
215 }
216  
217 ######################################################################
218  
219 =head2 send_user_output( GLOBALS, CLIENT )
220  
221 This method takes a CGI::Portable object as its first argument, GLOBALS, and
222 sends to the user as much of the HTTP response and user output details that it
223 can get from the object.
224 The second argument, CLIENT, is an IO::Socket::INET object which is the client
225 we will be sending our output to.
226  
227 =head2 send_quick_html_response( CONTENT, CLIENT )
228  
229 This method takes a string containing an HTML document as its first argument,
230 CONTENT, and sends an http response appropriate for an HTML document which
231 includes CONTENT as the http body.
232 The second argument, CLIENT, is an IO::Socket::INET object which is the client
233 we will be sending our output to.
234  
235 =head2 send_quick_redirect_response( URL, CLIENT )
236  
237 This method takes a string containing an url as its first argument, URL, and
238 sends an http redirection header to send the client browser to that url.
239 The second argument, CLIENT, is an IO::Socket::INET object which is the client
240 we will be sending our output to.
241  
242 =cut
243  
244 ######################################################################
245  
246 0 ▶sub send_user_output {
Subroutine send_user_output: not called CC=4 SCAR=30.0
Pod documented
247 0 my ($self, $globals, $client) = @_;
248 0 ▶ my $status = $globals->http_status_code() || '200 OK';
Condition0%0/2
$globals->http_status_code || '200 OK'
l!l
00
MC/DC0%0/1
$globals->http_status_code || '200 OK'
$globals->http_status_code
Truth table0%0/2
A
A= $globals->http_status_code
Aresult
11
00
249 0 my $target = $globals->http_window_target();
250 0 ▶ my $type = $globals->http_content_type() || 'text/html';
Condition0%0/2
$globals->http_content_type || 'text/html'
l!l
00
MC/DC0%0/1
$globals->http_content_type || 'text/html'
$globals->http_content_type
Truth table0%0/2
A
A= $globals->http_content_type
Aresult
11
00
251 0 my $url = $globals->http_redirect_url();
252 0 my @cookies = $globals->get_http_cookies();
253 0 my %misc = $globals->get_http_headers();
254 0 ▶ my $content = $globals->http_body() || $globals->page_as_string();
Condition0%0/3
$globals->http_body || $globals->page_as_string
l!l&&r!l&&!r
000
MC/DC0%0/2
$globals->http_body || $globals->page_as_string
$globals->http_body $globals->page_as_string
Truth table0%0/3
A || B
A= $globals->http_bodyB= $globals->page_as_string
ABresult
1X1
011
000
255 0 my $binary = $globals->http_body_is_binary();
256 0 $self->_send_output( $client, $status, $type, $url, $target, $content,
257 $binary, \@cookies, \%misc );
258 }
259  
260 0 ▶sub send_quick_html_response {
Subroutine send_quick_html_response: not called CC=1 SCAR=6.9
Pod documented
261 0 my ($self, $content, $client) = @_;
262 0 $self->_send_output( $client, '200 OK', 'text/html', undef, undef, $content );
263 }
264  
265 0 ▶sub send_quick_redirect_response {
Subroutine send_quick_redirect_response: not called CC=1 SCAR=6.9
Pod documented
266 0 my ($self, $url, $client) = @_;
267 0 $self->_send_output( $client, '301 Moved', undef, $url );
268 }
269  
270 # _send_output( CLIENT, STATUS, TYPE, [URL, [TARGET[, CONTENT[, IS_BINARY[,
271 # COOKIES[, MISC]]]]]] )
272 # This private method is used to implement all the send_*() methods above,
273 # and works under both mod_perl and cgi. It currently does not support NPH
274 # responses but that should be added in the future.
275  
276 0 ▶sub _send_output {
Subroutine _send_output: not called CC=8 SCAR=42.8
277 0 my ($self, $client, $status, $type, $url, $target, $content, $is_binary,
278 $cook, $misc) = @_;
279 0 ▶ ref($cook) eq 'ARRAY' or $cook = [];
Branch: 1 branch
BranchTrueFalseTotal
ref $cook eq "ARRAY" or $cook = [] 0 0 0
280 0 ▶ ref($misc) eq 'HASH' or $misc = {};
Branch: 1 branch
BranchTrueFalseTotal
ref $misc eq "HASH" or $misc = {} 0 0 0
281  
282 0 my @header = ("Status: $status");
283 0 ▶ $target and push( @header, "Window-Target: $target" );
Branch: 1 branch
BranchTrueFalseTotal
$target and push @header, "Window-Target: $target" 0 0 0
284 0 ▶ @{$cook} and push( @header, map { "Set-Cookie: $_" } @{$cook} );
Branch: 1 branch
BranchTrueFalseTotal
@{$cook;} and push @header, map({"Set-Cookie: $_";} @{$cook;}) 0 0 0
285 0 ▶ push( @header, $url ? "Location: $url" : "Content-Type: $type" );
Branch: 1 branch
BranchTrueFalseTotal
$url ? : 0 0 0
286 0 ▶ %{$misc} and push( @header, map { "$_: $misc->{$_}" } sort keys %{$misc} );
Branch: 1 branch
BranchTrueFalseTotal
%{$misc;} and push @header, map({"${_}: $misc->{$_}";} sort(keys %{$misc;})) 0 0 0
287 0 unshift( @header, "HTTP/1.0 $status" );
288  
289 0 my $endl = "\015\012"; # cr + lf
290 0 my $header = join( $endl, @header ).$endl.$endl;
291  
292 0 $client->autoflush(1);
293 0 print $client $header;
294 0 ▶ $is_binary and binmode( $client );
Branch: 1 branch
BranchTrueFalseTotal
$is_binary and binmode $client 0 0 0
295 0 print $client $content;
296 }
297  
298 ######################################################################
299  
300 1 1;
301 __END__
↑ Summary CGI/Portable/AdapterCGI.pm »