| 1 | # | ||
| 2 | # This file is part of WebDyne. | ||
| 3 | # | ||
| 4 | # This software is copyright (c) 2026 by Andrew Speer <andrew.speer@isolutions.com.au>. | ||
| 5 | # | ||
| 6 | # This is free software; you can redistribute it and/or modify it under | ||
| 7 | # the same terms as the Perl 5 programming language system itself. | ||
| 8 | # | ||
| 9 | # Full license text is available at: | ||
| 10 | # | ||
| 11 | # <http://dev.perl.org/licenses/> | ||
| 12 | # | ||
| 13 | |||
| 14 | package WebDyne::Request::PSGI; | ||
| 15 | |||
| 16 | |||
| 17 | # Compiler Pragma | ||
| 18 | # | ||
| 19 | 1 | ▶ | use strict qw(vars); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 20 | 1 | ▶ | use vars qw($VERSION @ISA); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 21 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 22 | 1 | ▶ | no warnings qw(uninitialized); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 23 | |||
| 24 | |||
| 25 | # External modules | ||
| 26 | # | ||
| 27 | 1 | ▶ | use File::Spec; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 28 | 1 | ▶ | use File::Spec::Unix; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 29 | 1 | ▶ | use HTTP::Status qw(status_message HTTP_OK HTTP_NOT_FOUND HTTP_FOUND); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 30 | 1 | ▶ | use URI; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 31 | 1 | ▶ | use Data::Dumper; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 32 | 0 | ▶ | use Plack::Request; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 33 | use Plack::Response; | ||
| 34 | |||
| 35 | |||
| 36 | # WebDyne modules | ||
| 37 | # | ||
| 38 | use WebDyne::Util; | ||
| 39 | use WebDyne::Constant; | ||
| 40 | use WebDyne::PSGI::Constant; | ||
| 41 | use WebDyne::Request::Common qw(handler_methods_init); | ||
| 42 | use WebDyne::Request::Fake; | ||
| 43 | |||
| 44 | |||
| 45 | # Version information | ||
| 46 | # | ||
| 47 | $VERSION='3.029'; | ||
| 48 | |||
| 49 | |||
| 50 | # Debug load | ||
| 51 | # | ||
| 52 | debug("Loading %s version $VERSION", __PACKAGE__); | ||
| 53 | |||
| 54 | |||
| 55 | # Init | ||
| 56 | # | ||
| 57 | &init() unless defined(&method); | ||
| 58 | |||
| 59 | |||
| 60 | # All done. Positive return | ||
| 61 | # | ||
| 62 | 1; | ||
| 63 | |||
| 64 | |||
| 65 | #================================================================================================== | ||
| 66 | |||
| 67 | |||
| 68 | sub init { | ||
| 69 | |||
| 70 | # Setup pass through methods | ||
| 71 | # | ||
| 72 | my %method=( | ||
| 73 | accept_encoding => undef, | ||
| 74 | accept_language => undef, | ||
| 75 | accept => undef, | ||
| 76 | args => undef, | ||
| 77 | as_string => undef, | ||
| 78 | authority => undef, | ||
| 79 | authorization => undef, | ||
| 80 | auth_type => undef, | ||
| 81 | base => 'base', | ||
| 82 | base_url => 'base', | ||
| 83 | body_handle => 'input', | ||
| 84 | #body => sub { my $r=shift(); if (my $input_or=$r->input) { unless (exists $r->{'body'}) { $input_or->read($r->{'body'}, $r->content_length()) }; return $r->{'body'} } }, | ||
| 85 | body => \&body, | ||
| 86 | cache_control => undef, | ||
| 87 | charset => undef, | ||
| 88 | cleanup_register => undef, | ||
| 89 | client_address => 'address', | ||
| 90 | content => \&body, | ||
| 91 | content_encoding => 'content_encoding', | ||
| 92 | #content_length => 'content_length', | ||
| 93 | #content_type => 'content_type', | ||
| 94 | content_length => undef, | ||
| 95 | content_type => undef, | ||
| 96 | cookies => 'cookies', | ||
| 97 | cookie => 'cookies', | ||
| 98 | custom_response => undef, | ||
| 99 | cwd => undef, | ||
| 100 | dir_config => undef, | ||
| 101 | document_root => undef, | ||
| 102 | env => 'env', | ||
| 103 | etag => undef, | ||
| 104 | filename => undef, | ||
| 105 | finalize => undef, | ||
| 106 | finfo => undef, | ||
| 107 | form_parameters => 'body_parameters', | ||
| 108 | forwarded_for => undef, | ||
| 109 | fragment => undef, | ||
| 110 | handler => undef, | ||
| 111 | header => sub { shift()->{'req'}->headers->header(@_) }, | ||
| 112 | headers => undef, | ||
| 113 | header_only => undef, | ||
| 114 | headers_in => \&headers_in, | ||
| 115 | headers_out => undef, | ||
| 116 | hostname => undef, | ||
| 117 | host => undef, | ||
| 118 | https => undef, | ||
| 119 | http_version => 'protocol', | ||
| 120 | id => sub { shift()->{'env'}->{'psgi.request_id'} }, | ||
| 121 | if_modified_since => undef, | ||
| 122 | if_none_match => undef, | ||
| 123 | input => sub { shift()->{'env'}->{'psgi.input'} }, | ||
| 124 | is_ajax => undef, | ||
| 125 | is_main => undef, | ||
| 126 | location => undef, | ||
| 127 | log_error => undef, | ||
| 128 | lookup_file => undef, | ||
| 129 | lookup_uri => undef, | ||
| 130 | main => undef, | ||
| 131 | media_type => undef, | ||
| 132 | method => 'method', | ||
| 133 | mtime => undef, | ||
| 134 | multipart_parameters=> 'body_parameters', | ||
| 135 | next => undef, | ||
| 136 | notes => undef, | ||
| 137 | origin => undef, | ||
| 138 | output_filters => undef, | ||
| 139 | path_info => 'path_info', | ||
| 140 | path_parameters => undef, | ||
| 141 | path => 'path_info', | ||
| 142 | pool => undef, | ||
| 143 | preferred_charset => undef, | ||
| 144 | preferred_encoding => undef, | ||
| 145 | preferred_language => undef, | ||
| 146 | preferred_media_type=> undef, | ||
| 147 | prev => undef, | ||
| 148 | print => undef, | ||
| 149 | protocol => 'protocol', | ||
| 150 | query_parameters => 'query_parameters', | ||
| 151 | query_string => 'query_string', | ||
| 152 | redirect => undef, | ||
| 153 | referer => 'referer', | ||
| 154 | register_cleanup => undef, | ||
| 155 | remote_address => 'address', | ||
| 156 | remote_host => sub { my $r=shift(); $r->{'req'}->remote_host || $r->{'env'}->{'REMOTE_ADDR'} }, | ||
| 157 | remote_port => undef, | ||
| 158 | remote_user => 'user', | ||
| 159 | request_time => sub { shift()->{'env'}->{'psgi.start_time'} }, | ||
| 160 | route => undef, | ||
| 161 | run => undef, | ||
| 162 | scheme => 'scheme', | ||
| 163 | script_name => 'script_name', | ||
| 164 | secure => 'secure', | ||
| 165 | sendfile => undef, | ||
| 166 | send_http_header => undef, | ||
| 167 | server_name => undef, | ||
| 168 | server_port => undef, | ||
| 169 | session_id => undef, | ||
| 170 | session => undef, | ||
| 171 | set_handlers => undef, | ||
| 172 | status_line => undef, | ||
| 173 | status => undef, | ||
| 174 | unparsed_uri => sub { shift()->{'env'}->{'REQUEST_URI'} }, | ||
| 175 | uploads => 'uploads', | ||
| 176 | uri => 'uri', | ||
| 177 | url => 'uri', | ||
| 178 | user_agent => 'user_agent', | ||
| 179 | user => undef, | ||
| 180 | write => undef, | ||
| 181 | err_html => undef, | ||
| 182 | DESTROY => undef, | ||
| 183 | ); | ||
| 184 | |||
| 185 | |||
| 186 | # Setup handlers and return | ||
| 187 | # | ||
| 188 | foreach my $handler (qw(req res)) { | ||
| 189 | *{$handler}=sub { return shift()->{$handler} }; | ||
| 190 | } | ||
| 191 | return handler_methods_init(__PACKAGE__, 'Plack::Request', \%method); | ||
| 192 | |||
| 193 | } | ||
| 194 | |||
| 195 | |||
| 196 | sub new { | ||
| 197 | |||
| 198 | |||
| 199 | # New PSGI request | ||
| 200 | # | ||
| 201 | my ($class, %r)=@_; | ||
| 202 | debug("$class, r: %s, calller:%s", Dumper(\%r, [caller(0)])); | ||
| 203 | |||
| 204 | |||
| 205 | # Get PSGI env var | ||
| 206 | # | ||
| 207 | my $env_hr=$r{'env'} || | ||
| 208 | return err('no PSGI env supplied'); | ||
| 209 | |||
| 210 | |||
| 211 | # Try to figure out filename user wants | ||
| 212 | # | ||
| 213 | unless ($r{'filename'}) { | ||
| 214 | |||
| 215 | |||
| 216 | # Not supplied - need to work out | ||
| 217 | # | ||
| 218 | debug('filename not supplied, determining from request'); | ||
| 219 | |||
| 220 | |||
| 221 | # Iterate through options. If *not* supplied by SCRIPT_FILENAME keep going. | ||
| 222 | # | ||
| 223 | my $fn; | ||
| 224 | my $invalid_path; | ||
| 225 | unless (($fn=$env_hr->{'SCRIPT_FILENAME'}) && !$r{'uri'}) { | ||
| 226 | |||
| 227 | |||
| 228 | # Need to calc from document root in PSGI environment | ||
| 229 | # | ||
| 230 | debug('not supplied in SCRIPT_FILENAME or uri param. calculating'); | ||
| 231 | if (my $dn=($r{'document_root'} || $ENV{'DOCUMENT_ROOT'} || $DOCUMENT_ROOT)) { | ||
| 232 | |||
| 233 | # Get from URI and location | ||
| 234 | # | ||
| 235 | my $uri=$r{'uri'} || $env_hr->{'PATH_INFO'}; | ||
| 236 | my @uri_part=grep { length($_) } split(m{/+}, $uri); | ||
| 237 | if (grep { $_ eq '..' } @uri_part) { | ||
| 238 | debug("rejecting path traversal attempt in uri: $uri"); | ||
| 239 | $fn=undef; | ||
| 240 | $invalid_path=1; | ||
| 241 | } | ||
| 242 | else { | ||
| 243 | $fn=File::Spec->rel2abs(File::Spec->catfile($dn, @uri_part)); | ||
| 244 | } | ||
| 245 | debug("fn: $fn from dn: $dn, uri: $uri"); | ||
| 246 | |||
| 247 | } | ||
| 248 | |||
| 249 | |||
| 250 | # Need to add default psp file ? | ||
| 251 | # | ||
| 252 | #unless ($fn=~/\.psp$/) { # fastest | ||
| 253 | unless ($invalid_path || ($fn=~WEBDYNE_PSP_EXT_RE)) { # fastest | ||
| 254 | |||
| 255 | # Is it a directory that exists ? Only append default document if that is the case, else let the api code | ||
| 256 | # handle it | ||
| 257 | # | ||
| 258 | debug("no .psp extenstion on fn: $fn, looking at options"); | ||
| 259 | if (($fn=~/\/$/) || ((-d $fn) || !$fn)) { | ||
| 260 | |||
| 261 | |||
| 262 | # Append default doc to path, which appears at moment to be a directory ? | ||
| 263 | # | ||
| 264 | my $document_default=$r{'document_default'} || $DOCUMENT_DEFAULT; | ||
| 265 | debug("appending document default $document_default to fn:$fn"); | ||
| 266 | |||
| 267 | # If absolute path just use it | ||
| 268 | # | ||
| 269 | if (File::Spec->file_name_is_absolute($document_default)) { | ||
| 270 | |||
| 271 | # Yep - absolute path | ||
| 272 | # | ||
| 273 | $fn=$document_default | ||
| 274 | } | ||
| 275 | else { | ||
| 276 | |||
| 277 | # Otherwise append to existing path | ||
| 278 | # | ||
| 279 | $fn=File::Spec->catfile($fn, split m{/+}, $document_default); #/ | ||
| 280 | $fn=File::Spec->rel2abs($fn); | ||
| 281 | } | ||
| 282 | } | ||
| 283 | else { | ||
| 284 | |||
| 285 | # Not .psp file, do not want | ||
| 286 | # | ||
| 287 | debug("fn: $fn does not end with /, leaving undef"); | ||
| 288 | $fn=undef; | ||
| 289 | } | ||
| 290 | } | ||
| 291 | } | ||
| 292 | |||
| 293 | |||
| 294 | # Final sanity check | ||
| 295 | # | ||
| 296 | debug("final fn: $fn"); | ||
| 297 | $r{'filename'}=$fn; | ||
| 298 | |||
| 299 | } | ||
| 300 | |||
| 301 | |||
| 302 | # Finished, pass back | ||
| 303 | # | ||
| 304 | return bless \%r, $class; | ||
| 305 | |||
| 306 | } | ||
| 307 | |||
| 308 | sub body { | ||
| 309 | |||
| 310 | my $self=shift(); | ||
| 311 | return ${$self->{'env'}{'webdyne.psgi.body'}} if exists $self->{'env'}{'webdyne.psgi.body'}; | ||
| 312 | return $self->{'body'} if exists $self->{'body'}; | ||
| 313 | |||
| 314 | my $length=$self->content_length(); | ||
| 315 | $self->{'body_status'}=400; | ||
| 316 | die "invalid Content-Length\n" if defined($length) && $length !~ /\A[0-9]+\z/; | ||
| 317 | if (defined($length) && $length > $WEBDYNE_CGI_POST_MAX) { | ||
| 318 | $self->{'body_status'}=413; | ||
| 319 | die "request body exceeds upload limit\n"; | ||
| 320 | } | ||
| 321 | |||
| 322 | # PSGI input may return short reads. Stop at the declared length when | ||
| 323 | # present, or EOF otherwise, and reject before retaining excess bytes. | ||
| 324 | # | ||
| 325 | my $input_or=$self->input(); | ||
| 326 | my $body=''; | ||
| 327 | while (!defined($length) || length($body) < $length) { | ||
| 328 | my $remaining=defined($length) ? $length-length($body) : $WEBDYNE_CGI_POST_MAX-length($body)+1; | ||
| 329 | my $chunk; | ||
| 330 | my $read=$input_or->read($chunk, $remaining > 8192 ? 8192 : $remaining); | ||
| 331 | die "unable to read request body\n" unless defined($read); | ||
| 332 | unless ($read) { | ||
| 333 | die "incomplete request body\n" if defined($length); | ||
| 334 | last; | ||
| 335 | } | ||
| 336 | if (length($body)+length($chunk) > $WEBDYNE_CGI_POST_MAX) { | ||
| 337 | $self->{'body_status'}=413; | ||
| 338 | die "request body exceeds upload limit\n"; | ||
| 339 | } | ||
| 340 | $body.=$chunk; | ||
| 341 | } | ||
| 342 | delete $self->{'body_status'}; | ||
| 343 | return $self->{'body'}=$body; | ||
| 344 | } | ||
| 345 | |||
| 346 | |||
| 347 | sub headers_in { | ||
| 348 | my $r=shift(); | ||
| 349 | if (@_) { | ||
| 350 | return $r->{'req'}->headers()->header(@_); | ||
| 351 | } | ||
| 352 | else { | ||
| 353 | return $r->{'req'}->headers(); | ||
| 354 | } | ||
| 355 | } | ||
| 356 | |||
| 357 | 1; | ||
| 358 | |||
| 359 | __END__ | ||