diff options
author | Yorhel <git@yorhel.nl> | 2018-02-05 16:59:43 +0100 |
---|---|---|
committer | Yorhel <git@yorhel.nl> | 2018-02-05 16:59:45 +0100 |
commit | 39ec19f50ed1e99e7ea174bbbd7f47bda53367eb (patch) | |
tree | b35191eedef4bdc6e443cb6355d91681791c7cd3 /lib/TUWF | |
parent | 4d3c301576d41149b820821a790670abc4aba374 (diff) |
Add tuwf->done and tuwf->pass, change error & before hook handling
This changes the way that before hooks signal whether to continue
processing or not, and is a breaking change for code that uses before
hooks with a false return value. This change does not affect the
pre_request_handler, so only code using the git version of TUWF is
affected.
This more generic control flow handling now also permits request
handlers for overlapping URL regexes, and tuwf->pass can be used to pass
control to subsequent handlers.
Diffstat (limited to 'lib/TUWF')
-rw-r--r-- | lib/TUWF/Request.pm | 23 | ||||
-rw-r--r-- | lib/TUWF/Response.pod | 3 |
2 files changed, 11 insertions, 15 deletions
diff --git a/lib/TUWF/Request.pm b/lib/TUWF/Request.pm index d75a2f5..f77042c 100644 --- a/lib/TUWF/Request.pm +++ b/lib/TUWF/Request.pm @@ -25,29 +25,29 @@ sub reqInit { if ($ENV{REQUEST_URI}||'') =~ /\?/; } - my $err = eval { + my $ok = eval { $self->{_TUWF}{Req}{Cookies} = _parse_cookies($self, $ENV{HTTP_COOKIE} || $ENV{COOKIE}); $self->{_TUWF}{Req}{GET} = _parse_urlencoded($ENV{QUERY_STRING}); $self->reqPath(); # let it croak when the path isn't valid UTF-8 1; }; - return 'utf8' if !$err && $@ && $@ =~ /does not map to Unicode/; # <- UGLY! + die TUWF::Exception->new('utf8') if !$ok && $@ && $@ =~ /does not map to Unicode/; # <- UGLY! # re-throw if it wasn't a UTF-8 problem. I don't expect this to happen - die $@ if !$err; + die $@ if !$ok; my $meth = $self->reqMethod; - return 'method' if $meth !~ /^(GET|POST|HEAD|DEL|OPTIONS|PUT|PATCH)$/; + die TUWF::Exception->new('method') if $meth !~ /^(GET|POST|HEAD|DEL|OPTIONS|PUT|PATCH)$/; if($meth =~ /^(POST|PUT|PATCH)$/ && $ENV{CONTENT_LENGTH}) { - return 'maxpost' if $self->{_TUWF}{max_post_body} && $ENV{CONTENT_LENGTH} > $self->{_TUWF}{max_post_body}; + die TUWF::Exception->new('maxpost') if $self->{_TUWF}{max_post_body} && $ENV{CONTENT_LENGTH} > $self->{_TUWF}{max_post_body}; my $data; die "Couldn't read all request data.\n" if $ENV{CONTENT_LENGTH} > read STDIN, $data, $ENV{CONTENT_LENGTH}, 0; - $err = eval { + $ok = eval { if(($ENV{'CONTENT_TYPE'}||'') =~ m{^application/json(?:;.*)?$}) { $self->{_TUWF}{Req}{JSON} = _parse_json($data); - return 'json' if !$self->{_TUWF}{Req}{JSON}; + die TUWF::Exception->new('json') if !$self->{_TUWF}{Req}{JSON}; } elsif(($ENV{'CONTENT_TYPE'}||'') =~ m{^multipart/form-data; boundary=(.+)$}) { _parse_multipart($self, $data, $1); } else { @@ -55,18 +55,15 @@ sub reqInit { } 1; }; - return 'utf8' if !$err && $@ && $@ =~ /does not map to Unicode/; - die $@ if !$err; + die TUWF::Exception->new('utf8') if !$ok && $@ && $@ =~ /does not map to Unicode/; + die $@ if !$ok; } - - return ''; } sub _check_control { # Disallow any control codes, except for x09 (tab), x0a (newline) and x0d (carriage return) - # The error message is a hack to trigger the 'utf8' error code. - die "Illegal control code (does not map to Unicode)" if $_[0] =~ /[\x00-\x08\x0b\x0c\x0e-\x1f]/; + die TUWF::Exception->new('controlchar') if $_[0] =~ /[\x00-\x08\x0b\x0c\x0e-\x1f]/; $_[0] } diff --git a/lib/TUWF/Response.pod b/lib/TUWF/Response.pod index fab404c..a6ef6ad 100644 --- a/lib/TUWF/Response.pod +++ b/lib/TUWF/Response.pod @@ -210,8 +210,7 @@ Examples: # Serve a file in '/webroot/public' if it exists, # otherwise handle the request as usual. TUWF::hook before => sub { - return 0 if tuwf->resFile('/webroot/public', tuwf->reqPath); - return 1; + tuwf->done if tuwf->resFile('/webroot/public', tuwf->reqPath); }; You might also want to set proper caching headers if the static files don't |