CGI/Portable/AdapterCGI.pm

filter: Statementstmt 11.3% 12 / 106 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 36 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 30.0% 3 / 10 Podpod 100% 5 / 5 totaltotal 10.0% 20 / 200 CC 28 SCAR 49.6
CC
28
Cov
7%
fetch_user_input
272.0
_send_output
90.0
send_user_output
20.0
CRAP 142.2
« CGI/Portable/AdapterSocket.pm ↑ Summary DemoUsage.pm »
1 =head1 NAME
2  
3 CGI::Portable::AdapterCGI - Run under CGI, Apache::Registry, cmd line
4  
5 =cut
6  
7 ######################################################################
8  
9 package CGI::Portable::AdapterCGI;
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 Apache (when running under mod_perl only)
36  
37 =head2 Nonstandard Modules
38  
39 CGI::Portable 0.50
40  
41 =head1 SYNOPSIS
42  
43 =head2 Content of thin shell "startup_cgi.pl" for CGI or Apache::Registry env:
44  
45 #!/usr/bin/perl
46 use strict;
47 use warnings;
48  
49 require CGI::Portable;
50 my $globals = CGI::Portable->new();
51  
52 use Cwd;
53 $globals->file_path_root( cwd() ); # let us default to current working directory
54 $globals->file_path_delimiter( $^O=~/Mac/i ? ":" : $^O=~/Win/i ? "\\" : "/" );
55  
56 $globals->set_prefs( 'config.pl' );
57 $globals->current_user_path_level( 1 );
58  
59 require CGI::Portable::AdapterCGI;
60 my $io = CGI::Portable::AdapterCGI->new();
61  
62 $io->fetch_user_input( $globals );
63 $globals->call_component( 'DemoAardvark' );
64 $io->send_user_output( $globals );
65  
66 1;
67  
68 =head1 DESCRIPTION
69  
70 This Perl 5 object class is an adapter for CGI::Portable that takes care of the
71 details for gathering user input and sending user output in a CGI environment.
72 Perl scripts running under the CGI protocol communicate with the HTTP server
73 through the global symbols named [%ENV, STDIN, STDOUT]; they read from the first
74 two and write to the third one.
75  
76 The Perl module named Apache::Registry can also simulate a CGI environment while
77 running under mod_perl, with a few caveats. CGI::Portable::AdapterCGI can sense
78 when it is running under Apache::Registry by checking $ENV{'GATEWAY_INTERFACE'}
79 and adjust its behaviour as appropriate. Specifically, the output HTTP headers
80 are sent using an Apache method rather than through STDOUT.
81  
82 If this module does not see a valid $ENV{'REQUEST_METHOD'} value then it will
83 assume the script is being debugged on the command line and will either use
84 existing shell arguments to simulate an HTTP request or it will prompt the user
85 interactively for request details. The response is sent using STDOUT as usual.
86  
87 =head1 SYNTAX
88  
89 This class does not export any functions or methods, so you need to call them
90 using object notation. This means using B<Class-E<gt>function()> for functions
91 and B<$object-E<gt>method()> for methods. If you are inheriting this class for
92 your own modules, then that often means something like B<$self-E<gt>method()>.
93  
94 =head1 FUNCTIONS AND METHODS
95  
96 =head2 new()
97  
98 This function creates a new CGI::Portable::AdapterCGI object and returns it.
99 The new object has no properties, but only methods.
100  
101 =cut
102  
103 ######################################################################
104  
105 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
106 0 my $class = shift( @_ );
107 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
108 0 return( $self );
109 }
110  
111 ######################################################################
112  
113 =head2 fetch_user_input( GLOBALS )
114  
115 This method takes a CGI::Portable object as its first argument, GLOBALS, and
116 feeds it all of the HTTP request and user input details that it can gather.
117 The user_path() is always initialized from $ENV{'PATH_INFO'} by this method;
118 if you want it to be from a query param then you can update it yourself later.
119  
120 =cut
121  
122 ######################################################################
123  
124 0 ▶sub fetch_user_input {
Subroutine fetch_user_input: not called CC=16 SCAR=56.1
Pod documented
125 0 my ($self, $globals) = @_;
126  
127 0 $globals->server_ip( '127.0.0.1' ); # there is no ENV for this, is there?
128 0 ▶ $globals->server_domain( $ENV{'HTTP_HOST'} || $ENV{'SERVER_NAME'} ||
Condition0%0/5
$ENV{'HTTP_HOST'} || $ENV{'SERVER_NAME'} || 'localhost'
l!l
00
$ENV{"HTTP_HOST"} or $ENV{"SERVER_NAME"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$ENV{'HTTP_HOST'} || $ENV{'SERVER_NAME'} || 'localhost'
$ENV{"HTTP_HOST"} $ENV{"SERVER_NAME"}
Truth table0%0/3
A or B
A= $ENV{"HTTP_HOST"}B= $ENV{"SERVER_NAME"}
ABresult
1X1
011
000
129 'localhost' );
130 0 ▶ $globals->server_port( $ENV{'SERVER_PORT'} || 80 );
Condition0%0/2
$ENV{'SERVER_PORT'} || 80
l!l
00
MC/DC0%0/1
$ENV{'SERVER_PORT'} || 80
$ENV{'SERVER_PORT'}
Truth table0%0/2
A
A= $ENV{'SERVER_PORT'}
Aresult
11
00
131 0 ▶ $globals->client_ip( $ENV{'REMOTE_ADDR'} || '127.0.0.1' );
Condition0%0/2
$ENV{'REMOTE_ADDR'} || '127.0.0.1'
l!l
00
MC/DC0%0/1
$ENV{'REMOTE_ADDR'} || '127.0.0.1'
$ENV{'REMOTE_ADDR'}
Truth table0%0/2
A
A= $ENV{'REMOTE_ADDR'}
Aresult
11
00
132 0 ▶ $globals->client_domain( $ENV{'REMOTE_HOST'} || $ENV{'REMOTE_ADDR'} ||
Condition0%0/5
$ENV{'REMOTE_HOST'} || $ENV{'REMOTE_ADDR'} || 'localhost'
l!l
00
$ENV{"REMOTE_HOST"} or $ENV{"REMOTE_ADDR"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$ENV{'REMOTE_HOST'} || $ENV{'REMOTE_ADDR'} || 'localhost'
$ENV{"REMOTE_HOST"} $ENV{"REMOTE_ADDR"}
Truth table0%0/3
A or B
A= $ENV{"REMOTE_HOST"}B= $ENV{"REMOTE_ADDR"}
ABresult
1X1
011
000
133 'localhost' );
134 0 $globals->client_port( $ENV{'REMOTE_PORT'} );
135  
136 0 my $host = $globals->server_domain();
137 0 my $port = $globals->server_port();
138 $globals->url_base( 'http://'.$host.($port != 80 ? ":$port" : '').
139 0 ▶ $self->_uri_unescape( $ENV{'SCRIPT_NAME'} ) );
Branch: 1 branch
BranchTrueFalseTotal
$port != 80 ? : 0 0 0
140  
141 0 ▶ $globals->request_method( $ENV{'REQUEST_METHOD'} || 'GET' );
Condition0%0/2
$ENV{'REQUEST_METHOD'} || 'GET'
l!l
00
MC/DC0%0/1
$ENV{'REQUEST_METHOD'} || 'GET'
$ENV{'REQUEST_METHOD'}
Truth table0%0/2
A
A= $ENV{'REQUEST_METHOD'}
Aresult
11
00
142 0 ▶ $globals->request_uri( $ENV{'REQUEST_URI'} || '/' );
Condition0%0/2
$ENV{'REQUEST_URI'} || '/'
l!l
00
MC/DC0%0/1
$ENV{'REQUEST_URI'} || '/'
$ENV{'REQUEST_URI'}
Truth table0%0/2
A
A= $ENV{'REQUEST_URI'}
Aresult
11
00
143 0 ▶ $globals->request_protocol( $ENV{'SERVER_PROTOCOL'} || 'HTTP/1.0' );
Condition0%0/2
$ENV{'SERVER_PROTOCOL'} || 'HTTP/1.0'
l!l
00
MC/DC0%0/1
$ENV{'SERVER_PROTOCOL'} || 'HTTP/1.0'
$ENV{'SERVER_PROTOCOL'}
Truth table0%0/2
A
A= $ENV{'SERVER_PROTOCOL'}
Aresult
11
00
144  
145 0 $globals->referer( $self->_uri_unescape( $ENV{'HTTP_REFERER'} ) );
146 0 $globals->user_agent( $ENV{'HTTP_USER_AGENT'} );
147  
148 0 my ($path_info, $query, $post, $oversize, $cookies);
149  
150 0 ▶ if( $ENV{'REQUEST_METHOD'} =~ /^(GET|HEAD|POST)$/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ENV{'REQUEST_METHOD'} =~ /^(GET|HEAD|POST)$/) { } 0 0 0
elsif ($ARGV[1]) { } 0 0 0
151 0 $path_info = $self->_uri_unescape( $ENV{'PATH_INFO'} );
152  
153 0 $query = $ENV{'QUERY_STRING'};
154 0 ▶ $query ||= $ENV{'REDIRECT_QUERY_STRING'};
Condition0%0/3
$query ||= $ENV{"REDIRECT_QUERY_STRING"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$query ||= $ENV{"REDIRECT_QUERY_STRING"}
$query $ENV{"REDIRECT_QUERY_STRING"}
Truth table0%0/3
A ||= B
A= $queryB= $ENV{"REDIRECT_QUERY_STRING"}
ABresult
1X1
011
000
155  
156 0 ▶ if( $ENV{'CONTENT_LENGTH'} > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"CONTENT_LENGTH"} > 0) 0 0 0
157 0 read( STDIN, $post, $ENV{'CONTENT_LENGTH'} );
158 0 chomp( $post );
159 }
160  
161 0 ▶ $cookies = $ENV{'HTTP_COOKIE'} || $ENV{'COOKIE'};
Condition0%0/3
$ENV{'HTTP_COOKIE'} || $ENV{'COOKIE'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$ENV{'HTTP_COOKIE'} || $ENV{'COOKIE'}
$ENV{'HTTP_COOKIE'} $ENV{'COOKIE'}
Truth table0%0/3
A || B
A= $ENV{'HTTP_COOKIE'}B= $ENV{'COOKIE'}
ABresult
1X1
011
000
162  
163 } elsif( $ARGV[1] ) { # allow caller to save $ARGV[0] for the http_host
164 0 $path_info = $ARGV[1];
165 0 $query = $ARGV[2];
166 0 $post = $ARGV[3];
167 0 $cookies = $ARGV[4];
168  
169 } else {
170 0 print STDERR "offline mode: enter path_info on standard input\n";
171 0 print STDERR "it must be all on one line\n";
172 0 $path_info = <STDIN>;
173 0 chomp( $path_info );
174  
175 0 print STDERR "offline mode: enter query_string on standard input\n";
176 0 print STDERR "it must be query-escaped and all on one line\n";
177 0 $query = <STDIN>;
178 0 chomp( $query );
179  
180 0 print STDERR "offline mode: enter post_string on standard input\n";
181 0 print STDERR "it must be query-escaped and all on one line\n";
182 0 $post = <STDIN>;
183 0 chomp( $post );
184  
185 0 print STDERR "offline mode: enter cookies_string on standard input\n";
186 0 print STDERR "they must be cookie-escaped and all on one line\n";
187 0 $cookies = <STDIN>;
188 0 chomp( $cookies );
189 }
190  
191 0 $globals->user_path( $path_info );
192 0 $globals->user_query( $query );
193 0 $globals->user_post( $post );
194 0 $globals->user_cookies( $cookies );
195 }
196  
197 # _uri_unescape( STRING )
198 # This private method takes a string in the argument STRING, and returns
199 # an uri-unescaped version of it.
200  
201 0 ▶sub _uri_unescape {
Subroutine _uri_unescape: not called CC=1 SCAR=6.9
202 0 my ($self, $str) = @_;
203 0 $str =~ tr/+/ /;
204 0 $str =~ s/%([0-9a-fA-F]{2})/pack("c",hex($1))/ge;
205 0 return( $str );
206 }
207  
208 ######################################################################
209  
210 =head2 send_user_output( GLOBALS )
211  
212 This method takes a CGI::Portable object as its first argument, GLOBALS, and
213 sends to the user as much of the HTTP response and user output details that it
214 can get from the object.
215  
216 =head2 send_quick_html_response( CONTENT )
217  
218 This method takes a string containing an HTML document as its first argument,
219 CONTENT, and sends an http response appropriate for an HTML document which
220 includes CONTENT as the http body.
221  
222 =head2 send_quick_redirect_response( URL )
223  
224 This method takes a string containing an url as its first argument, URL, and
225 sends an http redirection header to send the client browser to that url.
226  
227 =cut
228  
229 ######################################################################
230  
231 0 ▶sub send_user_output {
Subroutine send_user_output: not called CC=4 SCAR=30.0
Pod documented
232 0 my ($self, $globals) = @_;
233 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
234 0 my $target = $globals->http_window_target();
235 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
236 0 my $url = $globals->http_redirect_url();
237 0 my @cookies = $globals->get_http_cookies();
238 0 my %misc = $globals->get_http_headers();
239 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
240 0 my $binary = $globals->http_body_is_binary();
241 0 $self->_send_output( $status, $type, $url, $target, $content, $binary,
242 \@cookies, \%misc );
243 }
244  
245 0 ▶sub send_quick_html_response {
Subroutine send_quick_html_response: not called CC=1 SCAR=6.9
Pod documented
246 0 my ($self, $content) = @_;
247 0 $self->_send_output( '200 OK', 'text/html', undef, undef, $content );
248 }
249  
250 0 ▶sub send_quick_redirect_response {
Subroutine send_quick_redirect_response: not called CC=1 SCAR=6.9
Pod documented
251 0 my ($self, $url) = @_;
252 0 $self->_send_output( '301 Moved', undef, $url );
253 }
254  
255 # _send_output( STATUS, TYPE, [URL, [TARGET[, CONTENT[, IS_BINARY[,
256 # COOKIES[, MISC]]]]]] )
257 # This private method is used to implement all the send_*() methods above,
258 # and works under both mod_perl and cgi. It currently does not support NPH
259 # responses but that should be added in the future.
260  
261 0 ▶sub _send_output {
Subroutine _send_output: not called CC=9 SCAR=45.0
262 0 my ($self, $status, $type, $url, $target, $content, $is_binary,
263 $cook, $misc) = @_;
264 0 ▶ ref($cook) eq 'ARRAY' or $cook = [];
Branch: 1 branch
BranchTrueFalseTotal
ref $cook eq "ARRAY" or $cook = [] 0 0 0
265 0 ▶ ref($misc) eq 'HASH' or $misc = {};
Branch: 1 branch
BranchTrueFalseTotal
ref $misc eq "HASH" or $misc = {} 0 0 0
266  
267 0 my @header = ("Status: $status");
268 0 ▶ $target and push( @header, "Window-Target: $target" );
Branch: 1 branch
BranchTrueFalseTotal
$target and push @header, "Window-Target: $target" 0 0 0
269 0 ▶ @{$cook} and push( @header, map { "Set-Cookie: $_" } @{$cook} );
Branch: 1 branch
BranchTrueFalseTotal
@{$cook;} and push @header, map({"Set-Cookie: $_";} @{$cook;}) 0 0 0
270 0 ▶ push( @header, $url ? "Location: $url" : "Content-Type: $type" );
Branch: 1 branch
BranchTrueFalseTotal
$url ? : 0 0 0
271 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
272 0 my $endl = "\015\012"; # cr + lf
273 0 my $header = join( $endl, @header ).$endl.$endl;
274  
275 0 ▶ if( $ENV{'GATEWAY_INTERFACE'} =~ /^CGI-Perl/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{'GATEWAY_INTERFACE'} =~ /^CGI-Perl/) { } 0 0 0
276 0 require Apache;
277 0 $| = 1;
278 0 my $req = Apache->request();
279 0 $req->send_cgi_header( $header );
280  
281 } else {
282 0 print STDOUT $header;
283 }
284  
285 0 ▶ $is_binary and binmode( STDOUT );
Branch: 1 branch
BranchTrueFalseTotal
$is_binary and binmode STDOUT 0 0 0
286 0 print STDOUT $content;
287 }
288  
289 ######################################################################
290  
291 1 1;
292 __END__
« CGI/Portable/AdapterSocket.pm ↑ Summary DemoUsage.pm »