diff options
author | Yorhel <git@yorhel.nl> | 2015-09-20 10:33:24 +0200 |
---|---|---|
committer | Yorhel <git@yorhel.nl> | 2015-09-20 10:45:17 +0200 |
commit | 55cbbb319a135dbddfbfdd989bc0cb364edef81d (patch) | |
tree | 3d34057c92084dbbb1fbb2b732c762602f568743 | |
parent | 874b11d68ea251ffc6fd21b9464bff3b7946066b (diff) |
TUWF::Request: Add reqQuery() + include slash in reqPath()
!!!BACKWARDS INCOMPATIBLE CHANGE!!!
reqPath() used to not include the leading slash. So it would return
'some/page' instead of '/some/page'. This leading slash turned out to be
useful, so it's been added.
-rw-r--r-- | lib/TUWF.pm | 2 | ||||
-rw-r--r-- | lib/TUWF.pod | 8 | ||||
-rw-r--r-- | lib/TUWF/Request.pm | 15 | ||||
-rw-r--r-- | lib/TUWF/Request.pod | 9 |
4 files changed, 22 insertions, 12 deletions
diff --git a/lib/TUWF.pm b/lib/TUWF.pm index 5fad91c..1954ceb 100644 --- a/lib/TUWF.pm +++ b/lib/TUWF.pm @@ -232,7 +232,7 @@ sub _handle_request { return 1 if $self->{_TUWF}{pre_request_handler} && !$self->{_TUWF}{pre_request_handler}->($self); # find the handler - my $loc = $self->reqPath; + (my $loc = $self->reqPath) =~ s/^\///; study $loc; my $han = $self->{_TUWF}{error_404_handler}; my @args; diff --git a/lib/TUWF.pod b/lib/TUWF.pod index cc73ea1..69ac6e4 100644 --- a/lib/TUWF.pod +++ b/lib/TUWF.pod @@ -240,10 +240,10 @@ Note that all submodules must be in the same parent directory in C<@INC>. Maps a URI to a function. The I<regex> is matched to L<reqPath()|TUWF::Request> and must match the path from the begin to the end. (That is, the regex is used -between C<^> and C<$> marks). Since C<reqPath()> does not contain a leading -C</> character, these should also be omitted in the regexes. It is common to -use the C<qr{}> operator to quote the regex, which prevents you from having to -escape slashes in the path as would be required with C<qr//>. +between C<^> and C<$> marks). The leading slash of the path is removed before +matching, so these should be omitted in the regexes. It is common to use the +C<qr{}> operator to quote the regex, which prevents you from having to escape +slashes in the path as would be required with C<qr//>. All registered regexes are matched against any incoming URI, and the subroutine corresponding to the first matched regex will be called to handle the request. diff --git a/lib/TUWF/Request.pm b/lib/TUWF/Request.pm index 8caee9b..2ba8f7a 100644 --- a/lib/TUWF/Request.pm +++ b/lib/TUWF/Request.pm @@ -10,7 +10,7 @@ use Carp 'croak'; our $VERSION = '0.2'; our @EXPORT = qw| reqInit reqGet reqPost reqParam reqUploadMIME reqUploadRaw reqSaveUpload - reqCookie reqMethod reqHeader reqPath reqBaseURI reqURI reqHost reqIP + reqCookie reqMethod reqHeader reqPath reqQuery reqBaseURI reqURI reqHost reqIP |; @@ -255,9 +255,9 @@ sub reqHeader { } -# returns the path part of the current URI, excluding the leading slash +# returns the path part of the current URI, including the leading slash sub reqPath { - (my $u = ($ENV{REQUEST_URI}||'')) =~ s{^/+}{}; + (my $u = ($ENV{REQUEST_URI}||'')) =~ s{\?.*$}{}; return decode_utf8 $u, 1; } @@ -268,10 +268,15 @@ sub reqBaseURI { } +sub reqQuery { + my $u = $ENV{QUERY_STRING} ? '?'.$ENV{QUERY_STRING} : ''; + return decode_utf8 $u, 1; +} + + sub reqURI { my $s = shift; - my $u = $ENV{QUERY_STRING} ? '?'.$ENV{QUERY_STRING} : ''; - return $s->reqBaseURI().'/'.$s->reqPath().decode_utf8($u, 1); + return $s->reqBaseURI().$s->reqPath().$s->reqQuery(); } diff --git a/lib/TUWF/Request.pod b/lib/TUWF/Request.pod index cae7869..a2b283e 100644 --- a/lib/TUWF/Request.pod +++ b/lib/TUWF/Request.pod @@ -128,14 +128,19 @@ have been sent by the client. =head2 reqPath() -Returns the path part of the current page, relative to the I<base URI>. Does -not include a leading slash. +Returns the path part of the current page, relative to the I<base URI>. +Includes a leading slash. =head2 reqBaseURI() Returns the I<base URI> of the current page. That is, C<http(s)://> plus hostname. Does not include a trailing slash. +=head2 reqQuery() + +Returns the query string part of the current page, including leading question +mark. Returns an empty string if the current page does not have a query part. + =head2 reqURI() Returns the full URI of the current page, including C<http(s)://> and query |