uHTML/request.pm untested

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