1 package Web::Dispatch::ParamParser;
4 use warnings FATAL => 'all';
6 sub UNPACKED_QUERY () { __PACKAGE__.'.unpacked_query' }
7 sub UNPACKED_BODY () { __PACKAGE__.'.unpacked_body' }
8 sub UNPACKED_BODY_OBJECT () { __PACKAGE__.'.unpacked_body_object' }
9 sub UNPACKED_UPLOADS () { __PACKAGE__.'.unpacked_uploads' }
11 sub get_unpacked_query_from {
12 return $_[0]->{+UNPACKED_QUERY} ||= do {
13 _unpack_params($_[0]->{QUERY_STRING})
17 sub get_unpacked_body_from {
18 return $_[0]->{+UNPACKED_BODY} ||= do {
19 my $ct = lc($_[0]->{CONTENT_TYPE}||'');
20 if (!$_[0]->{CONTENT_LENGTH}) {
22 } elsif (index($ct, 'application/x-www-form-urlencoded') >= 0) {
23 $_[0]->{'psgi.input'}->read(my $buf, $_[0]->{CONTENT_LENGTH});
25 } elsif (index($ct, 'multipart/form-data') >= 0) {
26 my $p = get_unpacked_body_object_from($_[0])->param;
27 # forcible arrayification
29 map +(ref($p->{$_}) eq 'ARRAY'
31 : ($_ => [ $p->{$_} ])
40 sub get_unpacked_body_object_from {
41 # we may have no object at all - so use a single element arrayref for ||=
42 return ($_[0]->{+UNPACKED_BODY_OBJECT} ||= do {
43 if (!$_[0]->{CONTENT_LENGTH}) {
45 } elsif (index(lc($_[0]->{CONTENT_TYPE}||''),'multipart/form-data')==-1) {
48 [ _make_http_body($_[0]) ]
53 sub get_unpacked_uploads_from {
54 $_[0]->{+UNPACKED_UPLOADS} ||= do {
55 require Web::Dispatch::Upload; require HTTP::Headers;
56 my ($final, $reason) = (
57 {}, "field %s exists with value %s but body was not multipart/form-data"
59 if (my $body = get_unpacked_body_object_from($_[0])) {
60 my $u = $body->upload;
61 $reason = "field %s exists with value %s but was not an upload";
62 foreach my $k (keys %$u) {
63 foreach my $v (ref($u->{$k}) eq 'ARRAY' ? @{$u->{$k}} : $u->{$k}) {
64 push(@{$final->{$k}||=[]}, Web::Dispatch::Upload->new(
66 headers => HTTP::Headers->new($v->{headers})
71 my $b = get_unpacked_body_from($_[0]);
72 foreach my $k (keys %$b) {
74 foreach my $v (@{$b->{$k}}) {
76 push(@{$final->{$k}||=[]}, Web::Dispatch::NotAnUpload->new(
78 reason => sprintf($reason, $k, $v)
87 # shamelessly stolen from HTTP::Body::UrlEncoded by Christian Hansen
89 my $DECODE = qr/%([0-9a-fA-F]{2})/;
93 foreach my $num ( 0 .. 255 ) {
94 my $h = sprintf "%02X", $num;
95 $hex_chr{ lc $h } = $hex_chr{ uc $h } = chr $num;
100 (my $params = $_[0]) =~ s/\+/ /g;
102 foreach my $pair (split(/[&;](?:\s+)?/, $params)) {
103 next unless (($name, $value) = split(/=/, $pair, 2)) == 2;
105 s/$DECODE/$hex_chr{$1}/gs for ($name, $value);
107 push(@{$unpack{$name}||=[]}, $value);
114 # shamelessly stolen from Plack::Request by miyagawa
116 sub _make_http_body {
118 # Can't actually do this yet, since Plack::Request deletes the
119 # header structure out of the uploads in its copy of the body.
120 # I suspect I need to supply miyagawa with a failing test.
122 #if (my $plack_body = $_[0]->{'plack.request.http.body'}) {
123 # # Plack already constructed one; probably wasteful to do it again
124 # return $plack_body;
128 my $body = HTTP::Body->new(@{$_[0]}{qw(CONTENT_TYPE CONTENT_LENGTH)});
131 my $input = $_[0]->{'psgi.input'};
132 my $cl = $_[0]->{CONTENT_LENGTH};
134 $input->read(my $chunk, $cl < 8192 ? $cl : 8192);
135 my $read = length $chunk;
139 if ($read == 0 && $spin++ > 2000) {
141 Carp::croak("Bad Content-Length: maybe client disconnect? ($cl bytes remaining)");