handle empty query parameters
Matt S Trout [Fri, 14 Aug 2015 15:01:49 +0000 (15:01 +0000)]
Changes
lib/Web/Dispatch/ParamParser.pm
lib/Web/Simple.pm
t/param_parser.t

diff --git a/Changes b/Changes
index 30b28aa..b1fc756 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1,5 +1,6 @@
 Revision history for Web-Simple
 
+  - handle empty query parameters
   - produce a sensible error for (GET => undef) on 5.8
 
 0.030 - 2014-08-07
index 3caf160..e55c312 100644 (file)
@@ -104,7 +104,7 @@ sub get_unpacked_uploads_from {
     (my $params = $_[0]) =~ s/\+/ /g;
     my ($name, $value);
     foreach my $pair (split(/[&;](?:\s+)?/, $params)) {
-      next unless (($name, $value) = split(/=/, $pair, 2)) == 2;
+      $value = 1 unless (($name, $value) = split(/=/, $pair, 2)) == 2;
 
       s/$DECODE/$hex_chr{$1}/gs for ($name, $value);
       $_ = decode_utf8 $_ for ($name, $value);
index 8685d39..134c7ae 100644 (file)
@@ -526,7 +526,9 @@ The param spec is elements of one of the following forms:
 
 separated by the C<&> character. The arguments added to the request are
 one per non-C<:>/C<*> parameter (scalar for normal, arrayref for multiple),
-plus if any C<:>/C<*> specs exist a hashref containing those values.
+plus if any C<:>/C<*> specs exist a hashref containing those values. If a
+parameter has no value, i.e. appears as '?foo&', a value of 1 will be
+captured.
 
 Please note that if you specify a multiple type parameter match, you are
 ensured of getting an arrayref for the value, EVEN if the current incoming
index 7a35bac..d539b1d 100644 (file)
@@ -5,7 +5,7 @@ use Test::More qw(no_plan);
 
 use Web::Dispatch::ParamParser;
 
-my $param_sample = 'foo=bar&baz=quux&foo=%2F';
+my $param_sample = 'foo=bar&baz=quux&foo=%2F&xyzzy';
 my $unpacked = {
   baz => [
     "quux"
@@ -13,11 +13,14 @@ my $unpacked = {
   foo => [
     "bar",
     "/"
+  ],
+  xyzzy => [
+    1
   ]
 };
 
 is_deeply(
-  Web::Dispatch::ParamParser::_unpack_params('foo=bar&baz=quux&foo=%2F'),
+  Web::Dispatch::ParamParser::_unpack_params('foo=bar&baz=quux&foo=%2F&xyzzy'),
   $unpacked,
   'Simple unpack ok'
 );
@@ -36,7 +39,7 @@ is_deeply(
   'Unpack cached ok'
 );
 
-sub FakeBody::param { { baz => "quux", foo => [ "bar", "/" ] } }
+sub FakeBody::param { { baz => "quux", foo => [ "bar", "/" ], xyzzy => [ 1 ] } }
 
 my $body_env = {
   CONTENT_TYPE   => "multipart/form-data",