uHTML/request.pm untested

filter: Statementstmt 0.0% 0 / 348 Branchbran 0.0% 0 / 218 Conditioncond 0.0% 0 / 38 Subroutinesub 0.0% 0 / 35 Podpod 0.0% 0 / 27 totaltotal 0.0% 0 / 666 CC 110 SCAR 46.6
CC
110
Cov
0%
_getMultiPartData
342.0
PostData
240.0
Cookie
156.0
CRAP 105.9
1  
2 #
3 # Author: Roland Mosler - Roland@Place.Ug
4 #
5 # Das ist eine uHTML-Bibliothek von Place.Ug
6 # Es ist erlaubt dieses Paket unter der aktuellen GNU LGPL zu nutzen
7 # Bei Weiterentwicklungen ist die Ursprungsbibliothek zu nennen
8 #
9 # Fehler und Verbesserungen bitte an uHTML@Place.Ug
10 #
11 # This is a uHTML library from Place.Ug
12 # It is allowed to use this library under the actual GNU LGPL
13 # The name of this library is to be named in all derivations
14 #
15 # Please report errors to uHTML@Place.Ug
16 #
17 # © Roland Mosler, Place.Ug
18 #
19  
20  
21 use strict ;
22  
23 package uHTML::request ;
24  
25 use version ; our $VERSION = "1.81" ;
26  
27 require uHTML ;
28 use LWP::MediaTypes ;
29 use URI::Escape ;
30 use IO::Handle ;
31 use Time::Local( 'timegm','timelocal_nocheck' ) ;
32 use POSIX qw( strftime setlocale LC_TIME ) ;
33  
34 my( @initRequest, @finishRequest ) ;
35  
36 my @Empty = () ;
37  
38 sub new
39 {
40 my $class = shift ;
41 my $env = shift ;
42 my $req ;
43  
44 unless( ref $env eq 'HASH' )
45 {
46 my %E = %ENV ;
47 $env = \%E ;
48 }
49  
50 $env->{'PATH_INFO'} =~ s%/*$%% ;
51 $req->{'ENV'} = $env ;
52 $req->{'charset'} = 'utf-8' ;
53 $req->{'home'} = $env->{'DATA_ROOT'} ? $env->{'DATA_ROOT'} : $env->{'SCRIPT_ROOT'} ;
54 $req->{'Root'} = $env->{'DOCUMENT_ROOT'} ;
55 $req->{'Root'} .= $env->{'REDIRECT_ROOT'} if $env->{'REDIRECT_ROOT'} ;
56  
57 my $ru = $env->{'REQUEST_URI'} =~ s/\?.*//r ;
58 $req->{'Request'} = [ split m%/+%,$ru,-1 ] ;
59 $req->{'Path'} = [ split m%/+%,$env->{'PATH_INFO'},-1 ] ;
60 $req->{'file'} = $req->{'Root'} . $env->{'PATH_INFO'} ;
61  
62 if( -d $req->{'file'} )
63 {
64 $req->{'file'} .= '/index.uhtml' ;
65 $req->{'File'} = 'index.uhtml' ;
66 $req->{'RequestFile'} = 'index.uhtml';
67 }
68 else
69 {
70 $req->{'File'} = pop @{$req->{'Path'}} ;
71 $req->{'RequestFile'} = pop @{$req->{'Request'}} ;
72 }
73  
74 $req->{'BaseName'} = $req->{'File'} =~ s/\.[^\.]*$//ir ;
75 $req->{'RequestName'} = $req->{'RequestFile'} =~ s/\.[^\.]*$//ir ;
76 $req->{'path'} = join '/',@{$req->{'Path'}} ;
77 $req->{'Path'}->[0] = $env->{'HOST_NAME'} ? $env->{'HOST_NAME'} : $env->{'SERVER_NAME'} ;
78 $req->{'request'} = join '/'.@{$req->{'Request'}} ;
79 $req->{'Request'}->[0] = $env->{'HTTP_HOST'} ;
80  
81 return( $env->{'uRequest'} = bless( $req,$class ) ) ;
82 }
83  
84 sub Init
85 {
86 my $self = shift ;
87  
88 if( @_ )
89 {
90 my $func ;
91  
92 foreach $func( @_ )
93 {
94 next if ref $func ne 'CODE' ;
95 next if grep $func == $_,@initRequest ;
96 push @initRequest,$func ;
97 }
98 }
99 elsif( ref $self eq 'uHTML::request' and not $self->{'InitRequest'} )
100 {
101 uHTML::loadModules( $self->{'ENV'} ) ;
102 my @icopy = @initRequest ; # side effect protection
103 $_->($self) foreach @icopy ;
104 $self->{'InitRequest'} = 1 ;
105 }
106 return $self ;
107 }
108  
109 sub Finish
110 {
111 my $self = shift ;
112  
113 if( @_ )
114 {
115 my $func ;
116  
117 foreach $func( @_ )
118 {
119 next if ref $func ne 'CODE' ;
120 next if grep $func == $_,@finishRequest ;
121 push @finishRequest,$func ;
122 }
123 }
124 elsif( ref $self eq 'uHTML::request' and not $self->{'FinishRequest'} )
125 {
126 my @icopy = @finishRequest ; # side effect protection
127 $_->($self) foreach @icopy ;
128 $self->{'FinishRequest'} = 1 ;
129 }
130 return $self ;
131 }
132  
133 sub ENV
134 {
135 my $self = shift ;
136  
137 $self->{'ENV'} = shift if @_ ;
138 return $self->{'ENV'} ;
139 }
140  
141 sub QueryData
142 {
143 my $self = shift ;
144 my $Name = shift ;
145 my( $n,$v ) ;
146  
147 unless( $self->{'Query'} )
148 {
149 my %Q ;
150  
151 $self->{'Query'} = \%Q ;
152 foreach( split /\&/,$self->{'ENV'}->{'QUERY_STRING'} )
153 {
154 ( $n,$v ) = m/([^\=]+)(?:\=(.*))?/ ;
155 if( $n )
156 {
157 if( $v ne '' )
158 {
159 $v =~ s/\+/ /g ;
160 push @{$self->{'Query'}->{uri_unescape($n)}},uri_unescape($v) ;
161 }
162 elsif( $n =~ m/^(\d+)(\w+)/ )
163 {
164 push @{$self->{'Query'}->{uri_unescape($2)}},$1 ;
165 }
166 else
167 {
168 $self->{'Query'}->{uri_unescape($n)} = \@Empty ;
169 }
170 }
171 }
172 }
173 return $self->{'Query'} unless $Name ;
174 return (wantarray ? @Empty : '') unless $self->{'Query'}->{$Name} ;
175 return (wantarray ? @{$self->{'Query'}->{$Name}} : $self->{'Query'}->{$Name}->[0]) ;
176 }
177  
178 # RFC2046
179 my $BounduaryTest = qr%[\dA-Za-z'()+_,-./:=?]{1,70}% ;
180 my $CRLF = qr/\r\n/ ;
181  
182 sub _getMultiPartData
183 {
184 my( $Struct,$Data,$bounduary,$length ) = @_ ;
185 my( $line,$bound,$end,$status,$Record,$handling ) ;
186  
187 my( $cnt ) ;
188  
189 return 0 unless ref $Struct eq 'HASH' ;
190 $bound = qr/^--$bounduary/ ;
191 $end = "--$bounduary--" ;
192  
193  
194 for( undef $status;;$cnt++ )
195 {
196 last unless $Data =~ m/(.*?)\r\n/sgc ;
197  
198 unless( $line = $1 or $status eq 'data' )
199 {
200 $status = 'data' if $status eq 'head' and ref $Record eq 'HASH' ;
201 next ;
202 }
203  
204 if( $line =~ $bound )
205 {
206 if( ref $Record eq 'HASH' and $Record->{'label'} )
207 {
208 if( $Record->{'name'} )
209 {
210 $Record->{'length'} = length $Record->{'data'} ;
211 push( @{$Struct->{$Record->{'label'}}},$Record ) ;
212 }
213 elsif( $Record->{'type'} eq 'multipart/form-data' and $Record->{'boundary'} )
214 {
215 _getMultiPartData( $Struct,$Record->{'data'},$Record->{'boundary'},length $Record->{'data'} ) ;
216 }
217 else
218 {
219 push( @{$Struct->{$Record->{'label'}}},$Record->{'data'} ) if $Record->{'data'} ;
220 }
221 }
222 $status = 'complete', last if $line eq $end ;
223  
224 my %H ; $Record = \%H ;
225 $status = 'head' ;
226 next ;
227 }
228  
229 next unless ref $Record eq 'HASH' ;
230  
231 if( $status eq 'data' )
232 {
233 $Record->{'data'} .= "\r\n" if $Record->{'data'} ;
234 $Record->{'data'} .= $line ;
235 next ;
236 }
237 elsif( $status eq 'head' )
238 {
239 $handling = ($line =~ m/^Content-([\w-]+): +/gs)[0] ;
240 if( $handling eq 'Disposition' )
241 {
242 ( $Record->{'label'},$Record->{'name'} ) = ($line =~ m/form-data; +name="((?:[^"\\]|\\.?)+)"(?:; +filename="((?:[^"\\]|\\.?)+)")?/cgs ) ;
243 }
244 elsif( $handling eq 'Type' )
245 {
246 ( $Record->{'type'},$Record->{'boundary'} ) = ($line =~ m%([\w-/]+) *; boundary="?($BounduaryTest)"?%cgs) ;
247 }
248 elsif( $handling eq 'Transfer-Encoding' )
249 {
250 $Record->{'encoding'} = ($line =~ m/([^;]+);/cgs)[0] ;
251 }
252 }
253 }
254 return $status ;
255 }
256  
257 sub PostData
258 {
259 my $self = shift ;
260 my $Name = shift ;
261  
262 unless( $self->{'Post'} )
263 {
264 return \@Empty unless $self->{'ENV'}->{'REQUEST_METHOD'} eq 'POST' and $self->{'ENV'}->{'CONTENT_LENGTH'} > 0 ;
265  
266 my( $n,$v,$PostData,$sep ) ;
267 my( %P,$IO ) ;
268  
269 $self->{'Post'} = \%P ;
270 $self->{'PostL'} = $self->{'ENV'}->{'CONTENT_LENGTH'} ;
271 $self->{'PostR'} = 0 ;
272 unless( $IO = $self->{'ENV'}->{'psgi.input'} )
273 {
274 $IO = IO::Handle->new() ;
275 undef $IO unless $IO->fdopen( fileno( STDIN ),"r" ) ;
276 }
277  
278 if( $IO )
279 {
280 while( $self->{'PostL'}>$self->{'PostR'} )
281 {
282 $self->{'PostR'} += $IO->read( $PostData,$self->{'PostL'}-$self->{'PostR'},$self->{'PostR'} ) ;
283 }
284 }
285  
286 return \@Empty unless $self->{'PostR'} > 0 ;
287  
288  
289 if( $sep = ($self->{'ENV'}->{'CONTENT_TYPE'} =~ m%^multipart/form-data *; *boundary="?($BounduaryTest)"?%)[0] )
290 {
291 _getMultiPartData( $self->{'Post'},$PostData,$sep,$self->{'PostL'} ) ;
292 }
293 else
294 {
295 foreach( split /\&/,$PostData )
296 {
297 ( $n,$v ) = m/([^\=]+)(?:\=(.*))?/ ;
298 if( $n )
299 {
300 if( $v ne '' )
301 {
302 $v =~ s/\+/ /g ;
303 push @{$self->{'Post'}->{uri_unescape($n)}},uri_unescape($v) ;
304 }
305 elsif( $n =~ m/^(\d+)(\w+)/ )
306 {
307 push @{$self->{'Post'}->{uri_unescape($2)}},$1 ;
308 }
309 else
310 {
311 $self->{'Post'}->{uri_unescape($n)} = \@Empty ;
312 }
313 }
314 }
315 }
316 }
317 return $self->{'Post'} unless $Name ;
318 return (wantarray ? @Empty : '') unless $self->{'Post'}->{$Name} ;
319 return (wantarray ? @{$self->{'Post'}->{$Name}} : $self->{'Post'}->{$Name}->[0]) ;
320 }
321  
322 sub Data
323 {
324 my $self = shift ;
325 my $Name = shift ;
326  
327 unless( $self->{'Data'} )
328 {
329 my %D ;
330  
331 $self->{'Data'} = \%D ;
332 $self->PostData('') ;
333 $self->QueryData('') ;
334 foreach( keys %{$self->{'Post'}} )
335 {
336 push @{$self->{'Data'}->{$_}},@{$self->{'Post'}->{$_}} if $self->{'Post'}->{$_} ;
337 }
338 foreach( keys %{$self->{'Query'}} )
339 {
340 push @{$self->{'Data'}->{$_}},@{$self->{'Query'}->{$_}} if $self->{'Query'}->{$_} ;
341 }
342  
343 }
344  
345 return $self->{'Data'} unless $Name ;
346 return (wantarray ? @Empty : '') unless exists $self->{'Data'}->{$Name} ;
347 return (wantarray ? @{$self->{'Data'}->{$Name}} : (ref $self->{'Data'}->{$Name} eq 'HASH' ? $self->{'Data'}->{$Name} : $self->{'Data'}->{$Name}->[0])) ;
348 }
349  
350 sub Cookie
351 {
352 my $self = shift ;
353 my $Name = uri_escape_utf8( shift ) ; # cookie name
354 my $Value = uri_escape_utf8( shift ) ; # cookie value
355  
356 return $self->{'ENV'}->{'HTTP_COOKIE'} unless $Name ;
357 unless( defined $self->{'RecvCookie'} or not $self->{'ENV'}->{'HTTP_COOKIE'} )
358 {
359 my %C = map uri_unescape($_),map split( /=/ ),split /;\s+/,$self->{'ENV'}->{'HTTP_COOKIE'} ;
360 $self->{'RecvCookie'} = \%C ;
361 }
362 return( defined $self->{'RecvCookie'} ? $self->{'RecvCookie'}->{$Name} : undef ) unless defined $Value ;
363  
364 my $expire = shift ; # expiration in seconds, hours, days, months, or years (suffix none or s, h, d, m, y), 0 deletes
365 my $path = shift ; # cookie path ('/' if not defined)
366 my $host = shift ; # host ($ENV{'SERVER_NAME'} if not defined)
367 my $JS = shift ; # if true, then the cookie is available via JavaScript
368  
369 $path = '/' unless defined $path ;
370 $host = $self->{'ENV'}->{'SERVER_NAME'} unless defined $host ;
371 $self->{'SendCookie'}->{$Name} = "$Name=$Value;" ;
372 if( $expire )
373 {
374 my @Date = gmtime( 0 ) ;
375 $expire =~ m/(\d*)([shdmy]?)/ ;
376 if( $1+0 )
377 {
378 my $F = $2 ? $2 : 's' ;
379 my @Lgt = ( 60,60,24,30,12 ) ;
380 my %Fld = ( 's' => 0, 'h' => 2, 'd' => 3, 'm' => 4, 'y' => 5 ) ;
381 @Date = localtime( time ) ;
382 $Date[$Fld{$F}] += $1 ;
383 $Date[$_] >= $Lgt[$_] and $Date[$_+1] += int($Date[$_]/$Lgt[$_]), $Date[$_] %= $Lgt[$_] foreach 0,1,2,4 ;
384 @Date = gmtime( timelocal_nocheck( @Date[0 .. 5] ) ) ;
385 }
386 $self->{'SendCookie'}->{$Name} .= strftime( " Expires=%a, %d %b %Y %T UTC;",@Date ) ;
387 }
388 $self->{'SendCookie'}->{$Name} .= " Path=$path;" if $path ;
389 $self->{'SendCookie'}->{$Name} .= " Domain=$host;" ;
390 $self->{'SendCookie'}->{$Name} .= " HttpOnly" unless $JS ;
391 return $self->{'RecvCookie'}->{$Name} ;
392 }
393  
394 sub ContentType
395 {
396 my $self = shift ;
397  
398  
399 $self->{'contentType'} = shift if @_ ;
400 return $self->{'contentType'} if $self->{'contentType'} ;
401  
402 if( not $self->File() or $self->File() =~ /\.u?html?$/i ) { return( $self->{'contentType'} = 'text/html' ) }
403 elsif( $self->File() =~ /\.u?css$/i ) { return( $self->{'contentType'} = 'text/css' ) }
404 elsif( $self->File() =~ /\.u?js$/i ) { return( $self->{'contentType'} = 'text/js' ) }
405 elsif( $self->File() =~ /\.u?xml$/i ) { return( $self->{'contentType'} = 'text/xml' ) }
406 elsif( $self->File() =~ /\.u?rss$/i ) { return( $self->{'contentType'} = 'application/rss+xml' ) }
407  
408 return( $self->{'contentType'} = guess_media_type( $self->File() ) ) ;
409 }
410  
411 sub Headers
412 {
413 my $self = shift ;
414 my $length = shift ;
415  
416 return $self->{'Headers'} if defined $self->{'Headers'} ;
417  
418 $self->codeFile() unless $self->{'length'} ; # process uHTML to HTML
419 $self->Finish() unless $self->{'FinishRequest'} ; # Close the request, call all closing functions
420  
421 push @{$self->{'Headers'}}, 'x-powered-by', 'uHTML' ,
422 'Content-Length',$length || $self->{'length'} ,
423 'Content-Type', $self->ContentType() . "; charset=$self->{'charset'}" ;
424  
425 push @{$self->{'Headers'}},'Set-Cookie',$self->{'SendCookie'}->{$_} foreach keys %{$self->{'SendCookie'}} ;
426  
427 return $self->{'Headers'} ;
428 }
429  
430 sub setHeader
431 {
432 my( $self,$name,$value ) = @_ ;
433  
434 $value = ' ' if $value eq '' ;
435 push @{$self->{'Headers'}},$name,$value if $name ne '' ;
436 }
437  
438 sub putHeader
439 {
440 my $self = shift ;
441 my $len = shift ;
442 my $H = $self->Headers($len) ;
443  
444 for( my $i=0; $i<$#{$H}; $i+=2 ) { printf "%s: %s\n",$H->[$i],$H->[$i+1] }
445 print "\n" ;
446 }
447  
448 sub FileData
449 {
450 my $self = shift ;
451  
452 unless( $self->{'FileData'} )
453 {
454 my $FILE ;
455 open $FILE,$self->file() or die "UHTML::request: $self->{'file'} not found" ;
456 read $FILE,$self->{'FileData'},-s $FILE ;
457 }
458 return \$self->{'FileData'} ;
459 }
460  
461 sub codeFile
462 {
463 my $self = shift ;
464  
465 $self->Init() unless $self->{'InitRequest'} ; # Initialise the request and call all init functions
466 $self->FileData() unless $self->{'FileData'} ;
467 uHTML::fileStart( "$self->{'path'}/$self->{'File'}" ) ;
468 $self->{'HTML'} = uHTML::recodedList( $self->{'FileData'},$self->{'ENV'} ) ;
469 $self->{'length'} += length foreach @{$self->{'HTML'}} ;
470 uHTML::fileEnd() ;
471 }
472  
473 sub HTML
474 {
475 my $self = shift ;
476 my $string = shift ;
477  
478 $self->codeFile() unless $self->{'length'} ; # process uHTML to HTML
479 return( $string ? join( '',@{$self->{'HTML'}} ) : $self->{'HTML'} ) ;
480 }
481  
482 sub putFile
483 {
484 my $self = shift ;
485  
486 $self->FileData() unless $self->{'FileData'} ;
487 $self->codeFile() unless $self->{'length'} ; # process uHTML to HTML
488 print foreach $self->{'HTML'} ? @{$self->{'HTML'}} : $self->{'FileData'} ;
489 }
490  
491 sub process
492 {
493 my $self = shift ;
494 my $env = shift ;
495  
496 $self = uHTML::request->new( $env ) unless ref $self eq 'uHTML::request'; # get a new uHTML::request if needed
497 # $self->Init() ; # Initialise the request and call all init functions
498 # $self->codeFile() ; # process uHTML to HTML
499 # $self->Finish() ; #Close the request call all closing functions
500 $self->putHeader() ; # Send out headers
501 $self->putFile() ; # Send out HTML
502 }
503  
504 sub home
505 {
506 my $self = shift ;
507  
508 return $self->{'home'} ;
509 }
510  
511  
512 sub file
513 {
514 my $self = shift ;
515  
516 return $self->{'file'} ;
517 }
518  
519 sub File
520 {
521 my $self = shift ;
522 my $fname = shift ;
523  
524 if( $fname )
525 {
526 $fname = "/${\($self->Path())}/$fname" unless $fname =~ m%^/% ;
527 if( -f "${\($self->Root())}$fname" )
528 {
529 my $p0 = $self->{'Path'}->[0] ;
530 $self->{'Path'} = [ split m%/+%,$fname,-1 ] ;
531 $self->{'File'} = pop @{$self->{'Path'}} ;
532 $self->{'BaseName'} = $self->{'File'} =~ s/\.[^\.]*$//ir ;
533 $self->{'path'} = join '/',@{$self->{'Path'}} ;
534 $self->{'file'} = "${\($self->Root())}$self->{'path'}/$self->{'File'}" ;
535 $self->{'Path'}->[0] = $p0 ;
536 }
537 }
538  
539 return $self->{'File'} ;
540 }
541  
542 sub BaseName
543 {
544 my $self = shift ;
545 return $self->{'BaseName'} ;
546 }
547  
548 sub RequestName
549 {
550 my $self = shift ;
551 return $self->{'RequestName'} ;
552 }
553  
554 sub Path
555 {
556 my $self = shift ;
557 my $idx = shift ;
558  
559 return $self->{'Path'}->[$idx] if $idx =~ m/^\d+$/ ;
560 return (wantarray ? @{$self->{'Path'}}[$1 .. $2] : join '/',@{$self->{'Path'}}[$1 .. $2]) if $idx =~ m/^(\d+)(?:-|\.\.)(\d+)$/ ;
561 return (wantarray ? @{$self->{'Path'}} : $self->{'path'}) ;
562 }
563  
564 sub RequestPath
565 {
566 my $self = shift ;
567 my $idx = shift ;
568  
569 return $self->{'Request'}->[$idx] if $idx =~ m/^\d+$/ ;
570 return (wantarray ? @{$self->{'Request'}}[$1 .. $2] : join '/',@{$self->{'Request'}}[$1 .. $2]) if $idx =~ m/^(\d+)(?:-|\.\.)(\d+)$/ ;
571 return (wantarray ? @{$self->{'Request'}} : $self->{'request'}) ;
572 }
573  
574 sub Root
575 {
576 my $self = shift ;
577  
578 $self->{'Root'} = shift if @_ ;
579 return $self->{'Root'} ;
580 }
581  
582 sub ServerPath
583 {
584 my $self = shift ;
585 my $Path = shift ;
586  
587 return $self->Root() unless $Path ne '' ;
588 return( $Path=~s/^#// ? ($self->{'home'} && $Path !~ m%^/% ? "$self->{'home'}/$Path" : $Path) :
589 ($self->{'Root'}.( $Path !~ m%^/% ? "$self->{'path'}/$Path" : $Path)) ) ;
590 }
591  
592  
593 1;
594  
595 __END__