uHTML/request.pm untested

filter: Statementstmt 0.0% 0 / 350 Branchbran 0.0% 0 / 218 Conditioncond 0.0% 0 / 42 Subroutinesub 0.0% 0 / 35 Podpod 0.0% 0 / 27 totaltotal 0.0% 0 / 672 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.83" ;
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 the cookie
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 my $SameSite = shift or 'strict' ; # Google's Same Site Policy": Strict, Lax or None
369  
370 $path = '/' unless defined $path ;
371 $host = $self->{'ENV'}->{'SERVER_NAME'} unless defined $host ;
372 $self->{'SendCookie'}->{$Name} = "$Name=$Value;" ;
373 if( $expire )
374 {
375 my @Date = gmtime( 0 ) ;
376 $expire =~ m/(\d*)([shdmy]?)/ ;
377 if( $1+0 )
378 {
379 my $F = $2 ? $2 : 's' ;
380 my @Lgt = ( 60,60,24,30,12 ) ;
381 my %Fld = ( 's' => 0, 'h' => 2, 'd' => 3, 'm' => 4, 'y' => 5 ) ;
382 @Date = localtime( time ) ;
383 $Date[$Fld{$F}] += $1 ;
384 $Date[$_] >= $Lgt[$_] and $Date[$_+1] += int($Date[$_]/$Lgt[$_]), $Date[$_] %= $Lgt[$_] foreach 0,1,2,4 ;
385 @Date = gmtime( timelocal_nocheck( @Date[0 .. 5] ) ) ;
386 }
387 $self->{'SendCookie'}->{$Name} .= strftime( " Expires=%a, %d %b %Y %T UTC;",@Date ) ;
388 }
389 $self->{'SendCookie'}->{$Name} .= " Path=$path;" if $path ;
390 $self->{'SendCookie'}->{$Name} .= " Domain=$host;" ;
391 $self->{'SendCookie'}->{$Name} .= " sameSite=$SameSite;" ;
392 $self->{'SendCookie'}->{$Name} .= " HttpOnly" unless $JS ;
393 return $self->{'RecvCookie'}->{$Name} ;
394 }
395  
396 sub ContentType
397 {
398 my $self = shift ;
399  
400  
401 $self->{'contentType'} = shift if @_ ;
402 return $self->{'contentType'} if $self->{'contentType'} ;
403  
404 if( not $self->File() or $self->File() =~ /\.u?html?$/i ) { return( $self->{'contentType'} = 'text/html' ) }
405 elsif( $self->File() =~ /\.u?css$/i ) { return( $self->{'contentType'} = 'text/css' ) }
406 elsif( $self->File() =~ /\.u?js$/i ) { return( $self->{'contentType'} = 'text/js' ) }
407 elsif( $self->File() =~ /\.u?xml$/i ) { return( $self->{'contentType'} = 'text/xml' ) }
408 elsif( $self->File() =~ /\.u?rss$/i ) { return( $self->{'contentType'} = 'application/rss+xml' ) }
409  
410 return( $self->{'contentType'} = guess_media_type( $self->File() ) || 'text/html' ) ;
411 }
412  
413 sub Headers
414 {
415 my $self = shift ;
416 my $length = shift ;
417  
418 return $self->{'Headers'} if defined $self->{'Headers'} ;
419  
420 $self->codeFile() unless $self->{'length'} ; # process uHTML to HTML
421 $self->Finish() unless $self->{'FinishRequest'} ; # Close the request, call all closing functions
422  
423 push @{$self->{'Headers'}}, 'x-powered-by', 'uHTML' ,
424 'Content-Length',$length || $self->{'length'} ,
425 'Content-Type', $self->ContentType() . "; charset=$self->{'charset'}" ;
426  
427 push @{$self->{'Headers'}},'Set-Cookie',$self->{'SendCookie'}->{$_} foreach keys %{$self->{'SendCookie'}} ;
428  
429 return $self->{'Headers'} ;
430 }
431  
432 sub setHeader
433 {
434 my( $self,$name,$value ) = @_ ;
435  
436 $value = ' ' if $value eq '' ;
437 push @{$self->{'Headers'}},$name,$value if $name ne '' ;
438 }
439  
440 sub putHeader
441 {
442 my $self = shift ;
443 my $len = shift ;
444 my $H = $self->Headers($len) ;
445  
446 for( my $i=0; $i<$#{$H}; $i+=2 ) { printf "%s: %s\n",$H->[$i],$H->[$i+1] }
447 print "\n" ;
448 }
449  
450 sub FileData
451 {
452 my $self = shift ;
453  
454 unless( $self->{'FileData'} )
455 {
456 my $FILE ;
457 open $FILE,$self->file() or die "uHTML::request: $self->{'file'} not found" ;
458 read $FILE,$self->{'FileData'},-s $FILE ;
459 }
460 return \$self->{'FileData'} ;
461 }
462  
463 sub codeFile
464 {
465 my $self = shift ;
466  
467 $self->Init() unless $self->{'InitRequest'} ; # Initialise the request and call all init functions
468 $self->FileData() unless $self->{'FileData'} ;
469 uHTML::fileStart( "$self->{'path'}/$self->{'File'}" ) ;
470 $self->{'HTML'} = uHTML::recodedList( $self->{'FileData'},$self->{'ENV'} ) ;
471 $self->{'length'} += length foreach @{$self->{'HTML'}} ;
472 uHTML::fileEnd() ;
473 }
474  
475 sub HTML
476 {
477 my $self = shift ;
478 my $string = shift ;
479  
480 $self->codeFile() unless $self->{'length'} ; # process uHTML to HTML
481 return( $string ? join( '',@{$self->{'HTML'}} ) : $self->{'HTML'} ) ;
482 }
483  
484 sub putFile
485 {
486 my $self = shift ;
487  
488 $self->FileData() unless $self->{'FileData'} ;
489 $self->codeFile() unless $self->{'length'} ; # process uHTML to HTML
490 print foreach $self->{'HTML'} ? @{$self->{'HTML'}} : $self->{'FileData'} ;
491 }
492  
493 sub process
494 {
495 my $self = shift ;
496 my $env = shift ;
497  
498 $self = uHTML::request->new( $env ) unless ref $self eq 'uHTML::request'; # get a new uHTML::request if needed
499 # $self->Init() ; # Initialise the request and call all init functions
500 # $self->codeFile() ; # process uHTML to HTML
501 # $self->Finish() ; #Close the request call all closing functions
502 $self->putHeader() ; # Send out headers
503 $self->putFile() ; # Send out HTML
504 }
505  
506 sub home
507 {
508 my $self = shift ;
509  
510 return $self->{'home'} ;
511 }
512  
513  
514 sub file
515 {
516 my $self = shift ;
517  
518 return $self->{'file'} ;
519 }
520  
521 sub File
522 {
523 my $self = shift ;
524 my $fname = shift ;
525  
526 if( $fname )
527 {
528 $fname = "/${\($self->Path())}/$fname" unless $fname =~ m%^/% ;
529 if( -f "${\($self->Root())}$fname" )
530 {
531 my $p0 = $self->{'Path'}->[0] ;
532 $self->{'Path'} = [ split m%/+%,$fname,-1 ] ;
533 $self->{'File'} = pop @{$self->{'Path'}} ;
534 $self->{'BaseName'} = $self->{'File'} =~ s/\.[^\.]*$//ir ;
535 $self->{'path'} = join '/',@{$self->{'Path'}} ;
536 $self->{'file'} = "${\($self->Root())}$self->{'path'}/$self->{'File'}" ;
537 $self->{'Path'}->[0] = $p0 ;
538 }
539 }
540  
541 return $self->{'File'} ;
542 }
543  
544 sub BaseName
545 {
546 my $self = shift ;
547 return $self->{'BaseName'} ;
548 }
549  
550 sub RequestName
551 {
552 my $self = shift ;
553 return $self->{'RequestName'} ;
554 }
555  
556 sub Path
557 {
558 my $self = shift ;
559 my $idx = shift ;
560  
561 return $self->{'Path'}->[$idx] if $idx =~ m/^\d+$/ ;
562 return (wantarray ? @{$self->{'Path'}}[$1 .. $2] : join '/',@{$self->{'Path'}}[$1 .. $2]) if $idx =~ m/^(\d+)(?:-|\.\.)(\d+)$/ ;
563 return (wantarray ? @{$self->{'Path'}} : $self->{'path'}) ;
564 }
565  
566 sub RequestPath
567 {
568 my $self = shift ;
569 my $idx = shift ;
570  
571 return $self->{'Request'}->[$idx] if $idx =~ m/^\d+$/ ;
572 return (wantarray ? @{$self->{'Request'}}[$1 .. $2] : join '/',@{$self->{'Request'}}[$1 .. $2]) if $idx =~ m/^(\d+)(?:-|\.\.)(\d+)$/ ;
573 return (wantarray ? @{$self->{'Request'}} : $self->{'request'}) ;
574 }
575  
576 sub Root
577 {
578 my $self = shift ;
579  
580 $self->{'Root'} = shift if @_ ;
581 return $self->{'Root'} ;
582 }
583  
584 sub ServerPath
585 {
586 my $self = shift ;
587 my $Path = shift ;
588  
589 return $self->Root() unless $Path ne '' ;
590 return( $Path=~s/^#// ? ($self->{'home'} && $Path !~ m%^/% ? "$self->{'home'}/$Path" : $Path) :
591 ($self->{'Root'}.( $Path !~ m%^/% ? "$self->{'path'}/$Path" : $Path)) ) ;
592 }
593  
594  
595 1;
596  
597 __END__