blib/lib/Query/Param.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 CC 1 SCAR 0.7
CC
1
Cov
83%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package Query::Param;
2  
3 # A lightweight form and query string variable parser...
4  
5 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 ▶use URI::Escape qw(uri_unescape);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 0 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=2.6
10 use Data::Dumper;
11  
12 our $VERSION = '1.0.0';
13  
14 ########################################################################
15 sub new {
16 ########################################################################
17 my ( $class, $query_string, $content_type ) = @_;
18  
19 $content_type //= 'application/x-www-form-urlencoded';
20  
21 my $self = bless {
22 _raw => {}, # raw key => [raw_value, ...]
23 _decoded => {}, # decoded key => scalar or arrayref
24 _type => $content_type,
25 }, $class;
26  
27 $query_string //= q{};
28  
29 if ( $content_type eq 'application/json' ) {
30 $self->{_raw} = $query_string;
31 $self->{_decoded} = decode_json($query_string);
32 }
33 elsif ( $content_type =~ /multipart\/form\-data/xsm ) {
34 $self->{_raw} = $query_string;
35 $self->{_decoded} = $self->parse_multipart_form_data( $content_type, $query_string );
36 }
37 else {
38 while ( $query_string =~ /([^&=]+)=?([^&]*)/xsmg ) {
39 my ( $key, $val ) = ( $1, $2 );
40 push @{ $self->{_raw}{$key} }, $val;
41 }
42 }
43  
44 return $self;
45 }
46  
47 ########################################################################
48 sub new_from_request {
49 ########################################################################
50 my ($class) = @_;
51  
52 my ( $content_type, $content_length, $method ) = @ENV{qw(CONTENT_TYPE CONTENT_LENGTH REQUEST_METHOD)};
53  
54 die "ERROR: no request method found\n"
55 if !$ENV{REQUEST_METHOD};
56  
57 return $class->new( $ENV{QUERY_STRING} )
58 if $ENV{REQUEST_METHOD} eq 'GET';
59  
60 return
61 if $ENV{REQUEST_METHOD} ne 'POST';
62  
63 my $fh = *STDIN;
64  
65 if ( !$content_length ) {
66 $content_length = 4096;
67 }
68  
69 my $content = q{};
70 my $buffer;
71  
72 while ( my $bytes_read = read $fh, $buffer, $content_length ) {
73 $content .= $buffer;
74 }
75  
76 return $class->new( $content, $content_type )
77 if $content_type !~ /multipart\/form\-data/xsm;
78  
79 return $class->_new_from_multipart_form_data( $content_type, $content );
80 }
81  
82 ########################################################################
83 sub _new_from_multipart_form_data {
84 ########################################################################
85 my ( $class, $content_type, $content ) = @_;
86  
87 my $params = $class->parse_multipart_form_data( $content_type, $content );
88  
89 my $self = bless {
90 _raw => {},
91 _decoded => {},
92 _type => $content_type,
93 }, $class;
94  
95 foreach ( keys %{$params} ) {
96 $self->{_raw}->{$_} = $self->{_decoded}->{$_} = $params->{$_};
97 }
98  
99 return $self;
100 }
101  
102 ########################################################################
103 sub Vars {
104 ########################################################################
105 my ($self) = @_;
106 my %vars;
107  
108 for my $key ( $self->keys ) {
109 my $val = $self->get($key);
110 $vars{$key} = ref $val eq 'ARRAY' ? $val->[-1] : $val;
111 }
112  
113 return \%vars;
114 }
115  
116 ########################################################################
117 sub parse_multipart_form_data {
118 ########################################################################
119 my ( $self, $content_type, $post_data ) = @_;
120  
121 my %params;
122  
123 my ($boundary) = $content_type =~ /boundary=([^;]+)/xsm;
124  
125 while ( $post_data =~ /Content\-Disposition:\sform\-data;\sname="([^"]+)"\r?\n\r?\n(.*?)\r?\n--$boundary/sg ) {
126  
127 my ( $name, $value ) = ( $1, $2 );
128 $value =~ s/\s+$//xsm;
129  
130 if ( exists $params{$name} ) {
131 if ( !ref $params{$name} ) {
132 $params{$name} = [ $params{$name}, $value ];
133 }
134 else {
135 push @{ $params{$name} }, $value;
136 }
137 }
138 else {
139 $params{$name} = $value;
140 }
141 }
142  
143 return \%params;
144 }
145  
146 ########################################################################
147 sub param {
148 ########################################################################
149 my ( $self, $key ) = @_;
150  
151 return $self->keys if !defined $key;
152 return $self->get($key);
153 }
154  
155 ########################################################################
156 sub params {
157 ########################################################################
158 my ($self) = @_;
159 my %out;
160  
161 for my $key ( $self->keys ) {
162 my $val = $self->get($key);
163 $out{$key} = $val;
164 }
165  
166 return \%out;
167 }
168  
169 ########################################################################
170 sub get {
171 ########################################################################
172 my ( $self, $key ) = @_;
173  
174 return $self->{_decoded}{$key}
175 if exists $self->{_decoded}{$key};
176  
177 return
178 if !exists $self->{_raw}{$key};
179  
180 my @values = map { uri_unescape( $_ =~ tr/+/ /r ) } @{ $self->{_raw}{$key} };
181  
182 $self->{_decoded}{$key} = @values > 1 ? \@values : $values[0];
183  
184 return $self->{_decoded}{$key};
185 }
186  
187 ########################################################################
188 sub set {
189 ########################################################################
190 my ( $self, $key, $val ) = @_;
191  
192 delete $self->{_raw}{$key};
193 $self->{_decoded}{$key} = $val;
194  
195 return;
196 }
197  
198 ########################################################################
199 sub has {
200 ########################################################################
201 my ( $self, $key ) = @_;
202 return exists $self->{_raw}{$key} || exists $self->{_decoded}{$key};
203 }
204  
205 ########################################################################
206 sub keys {
207 ########################################################################
208 my ($self) = @_;
209  
210 return
211 if $self->{_type} ne 'application/x-www-form-urlencoded';
212  
213 my %seen;
214  
215 return grep { !$seen{$_}++ } ( keys %{ $self->{_raw} }, keys %{ $self->{_decoded} } );
216 }
217  
218 ########################################################################
219 sub to_string {
220 ########################################################################
221 my ($self) = @_;
222  
223 return $self->{_raw}
224 if $self->{_type} ne 'application/x-www-form-urlencoded';
225  
226 my @pairs;
227  
228 for my $key ( $self->keys ) {
229 if ( exists $self->{_raw}{$key} ) {
230 push @pairs, map {"$key=$_"} @{ $self->{_raw}{$key} };
231 }
232 elsif ( exists $self->{_decoded}{$key} ) {
233 my @vals
234 = ref $self->{_decoded}{$key} eq 'ARRAY'
235 ? @{ $self->{_decoded}{$key} }
236 : ( $self->{_decoded}{$key} );
237  
238 for my $v (@vals) {
239 my $escaped_key = _form_escape($key);
240 my $escaped_val = _form_escape($v);
241 push @pairs, "$escaped_key=$escaped_val";
242 }
243 }
244 }
245  
246 return join q{&}, @pairs;
247 }
248  
249 ########################################################################
250 sub _form_escape {
251 ########################################################################
252 my ($s) = @_;
253 $s =~ s/([^\w\-\.\~ ])/sprintf("%%%02X", ord($1))/eg;
254 $s =~ s/ /+/g;
255  
256 return $s;
257 }
258  
259 ########################################################################
260 sub pairs {
261 ########################################################################
262 my ($self) = @_;
263 my @kv;
264  
265 for my $key ( $self->keys ) {
266 my $val = $self->get($key);
267  
268 if ( ref $val eq 'ARRAY' ) {
269 push @kv, map { ( $key => $_ ) } @$val;
270 }
271 else {
272 push @kv, ( $key => $val );
273 }
274 }
275  
276 return @kv;
277 }
278  
279 1;
280  
281 __END__
↑ Summary