Nile/HTTP/PSGI.pm untested

filter: Statementstmt 0.0% 0 / 124 Branchbran 0.0% 0 / 66 Conditioncond 0.0% 0 / 49 MC/DCmcdc 0.0% 0 / 28 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 289 CC 30 SCAR 54.1
CC
30
Cov
0%
psgi_header
420.0
url
132.0
new
2.0
CRAP 224.5
1 # Copyright Infomation
2 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3 # Author : Dr. Ahmed Amin Elsheshtawy, Ph.D.
4 # Website: https://github.com/mewsoft/Nile, http://www.mewsoft.com
5 # Email : mewsoft@cpan.org, support@mewsoft.com
6 # Copyrights (c) 2014-2015 Mewsoft Corp. All rights reserved.
7 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 package Nile::HTTP::PSGI;
9  
10 use strict;
11 use 5.008_001;
12  
13 our $VERSION = '0.55';
14 our $AUTHORITY = 'cpan:MEWSOFT';
15  
16 use base qw(CGI::Simple);
17  
18 if ($CGI::Simple::VERSION lt '1.111') {
19 no warnings 'redefine';
20 *CGI::Simple::_internal_read = sub($\$;$) {
21 my ($self, $buffer, $len) = @_;
22 $len = 4096 if !defined $len;
23 if (exists $self->{psgi_env}->{'psgi.input'}) {
24 $self->{psgi_env}->{'psgi.input'}->read($$buffer, $len);
25 }
26 elsif ( $self->{'.mod_perl'} ) {
27 my $r = $self->_mod_perl_request();
28 $r->read( $$buffer, $len );
29 }
30 else {
31 read STDIN, $$buffer, $len;
32 }
33 };
34 }
35  
36 sub new {
37 my ($class, $env) = @_;
38  
39 my $self = bless {
40 psgi_env => $env,
41 use_tempfile => 1,
42 }, $class;
43  
44 local *ENV = $env;
45  
46 $self->_initialize_globals;
47 $self->_store_globals;
48
49 no strict 'refs';
50 $self->_read_parse($self->{psgi_env}->{'psgi.input'});
51  
52 $self;
53 }
54  
55 sub _mod_perl { return 0 }
56  
57 sub env {
58 $_[0]->{psgi_env};
59 }
60  
61 # copied and rearanged from CGI::Simple::header
62 sub psgi_header {
63 my($self, @p) = @_;
64 require CGI::Simple::Util;
65 my @header;
66 my(
67 $type, $status, $cookie, $target, $expires, $nph, $charset,
68 $attachment, $p3p, @other
69 ) = CGI::Simple::Util::rearrange([
70 ['TYPE', 'CONTENT_TYPE', 'CONTENT-TYPE'],
71 'STATUS', ['COOKIE', 'COOKIES'], 'TARGET',
72 'EXPIRES', 'NPH', 'CHARSET',
73 'ATTACHMENT','P3P',
74 ], @p);
75  
76 $type ||= 'text/html' unless defined($type);
77 if (defined $charset) {
78 $self->charset($charset);
79 } else {
80 $charset = $self->charset if $type =~ /^text\//;
81 }
82 $charset ||= '';
83  
84 # rearrange() was designed for the HTML portion, so we
85 # need to fix it up a little.
86 my @other_headers;
87 for (@other) {
88 # Don't use \s because of perl bug 21951
89 next unless my($header,$value) = /([^ \r\n\t=]+)=\"?(.+?)\"?$/;
90 $header =~ s/^(\w)(.*)/"\u$1\L$2"/e;
91 push @other_headers, $header, $self->unescapeHTML($value);
92 }
93  
94 $type .= "; charset=$charset"
95 if $type ne ''
96 and $type !~ /\bcharset\b/
97 and defined $charset
98 and $charset ne '';
99  
100 # Maybe future compatibility. Maybe not.
101 my $protocol = $self->{psgi_env}->{SERVER_PROTOCOL} || 'HTTP/1.0';
102  
103 push @header, "Status", $status if $status;
104 push @header, "Window-Target", $target if $target;
105 if ($p3p) {
106 $p3p = join ' ',@$p3p if ref $p3p eq 'ARRAY';
107 push @header, "P3P", qq{policyref="/w3c/p3p.xml", CP="$p3p"};
108 }
109  
110 # push all the cookies -- there may be several
111 if ($cookie) {
112 my(@cookie) = ref $cookie eq 'ARRAY' ? @{$cookie} : $cookie;
113 for (@cookie) {
114 my $cs = eval{ $_->can('as_string') } ? $_->as_string : "$_";
115 push @header, "Set-Cookie", $cs if $cs ne '';
116 }
117 }
118 # if the user indicates an expiration time, then we need
119 # both an Expires and a Date header (so that the browser is
120 # uses OUR clock)
121 $expires = 'now'
122 if $self->no_cache; # encourage no caching via expires now
123 push @header, 'Expires', CGI::Simple::Util::expires($expires, 'http')
124 if $expires;
125 push @header, 'Date', CGI::Simple::Util::expires(0, 'http')
126 if defined $expires || $cookie || $nph;
127 push @header, 'Pragma', 'no-cache' if $self->cache or $self->no_cache;
128 push @header, 'Content-Disposition', "attachment; filename=\"$attachment\""
129 if $attachment;
130 push @header, @other;
131 push @header, 'Content-Type', $type if $type;
132  
133 $status ||= "200";
134 $status =~ s/\D*$//;
135  
136 return $status, \@header;
137 }
138  
139 # The list is auto generated and modified with:
140 # perl -nle '/^sub (\w+)/ and $sub=$1; \
141 # /^}\s*$/ and do { print $sub if $code{$sub} =~ /([\%\$]ENV|http\()/; undef $sub };\
142 # $code{$sub} .= "$_\n" if $sub; \
143 # /^\s*package [^C]/ and exit' \
144 # `perldoc -l CGI`
145 for my $method (qw(
146 url_param
147 upload
148 upload_info
149 parse_query_string
150 cookie
151 raw_cookie
152 header
153 MyFullUrl
154 PrintEnv
155 auth_type
156 content_length
157 content_type
158 document_root
159 gateway_interface
160 path_translated
161 referer
162 remote_addr
163 remote_host
164 remote_ident
165 remote_user
166 request_method
167 #script_name
168 server_name
169 server_port
170 server_protocol
171 server_software
172 user_name
173 user_agent
174 virtual_host
175 path_info
176 accept
177 http
178 https
179 protocol
180 #url
181 )) {
182 no strict 'refs';
183 *$method = sub {
184 my $self = shift;
185 my $super = "SUPER::$method";
186 local *ENV = $self->{psgi_env};
187 return $self->$super(@_);
188 };
189 }
190  
191 sub script_name {
192 my ($self) = @_;
193 #$ENV{'SCRIPT_NAME'} || $0 || ''
194 $self->env->{SCRIPT_NAME} || ''
195 }
196  
197 sub url {
198 my ( $self, @p ) = @_;
199 use CGI::Simple::Util 'rearrange';
200 my ( $relative, $absolute, $full, $path_info, $query, $base )
201 = rearrange(
202 [
203 'RELATIVE', 'ABSOLUTE', 'FULL',
204 [ 'PATH', 'PATH_INFO' ],
205 [ 'QUERY', 'QUERY_STRING' ], 'BASE'
206 ],
207 @p
208 );
209  
210 my $url;
211  
212 local *ENV = $self->{psgi_env};
213  
214 $full++ if $base || !( $relative || $absolute );
215  
216 my $path = $self->path_info;
217  
218 my $script_name = $self->script_name;
219  
220 if ($full) {
221 my $protocol = $self->protocol();
222 $url = "$protocol://";
223 my $vh = $self->http( 'host' );
224  
225 if ($vh) {
226 $url .= $vh;
227 }
228 else {
229 $url .= server_name();
230 my $port = $self->server_port;
231 $url .= ":" . $port
232 unless ( lc( $protocol ) eq 'http' && $port == 80 )
233 or ( lc( $protocol ) eq 'https' && $port == 443 );
234 }
235  
236 return $url if $base;
237 #$url .= $script_name;
238 #$url .= $path;
239 }
240 elsif ($relative) {
241 ($url) = $script_name =~ m#([^/]+)$#;
242 }
243 elsif ($absolute) {
244 #$url = $script_name;
245 $url = $path;
246 }
247  
248 $url .= $path if $path_info and defined $path;
249 $url .= "?" . $self->query_string if $query and $self->query_string;
250 $url = '' unless defined $url;
251  
252 $url =~ s/([^a-zA-Z0-9_.%;&?\/\\:+=~-])/uc sprintf("%%%02x",ord($1))/eg;
253  
254 return $url;
255 }
256  
257 sub DESTROY {
258 my $self = shift;
259 CGI::Simple::_initialize_globals();
260 }
261  
262 1;
263  
264 __END__