line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
2
|
|
|
2
|
|
124813
|
use strict; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
174
|
|
2
|
|
|
|
|
|
|
package Plack::Middleware::Rewrite::Query; |
3
|
|
|
|
|
|
|
#ABSTRACT: Safely modify the QUERY_STRING of a PSGI request |
4
|
|
|
|
|
|
|
our $VERSION = '0.1.1'; #VERSION |
5
|
|
|
|
|
|
|
|
6
|
2
|
|
|
2
|
|
12
|
use parent qw(Plack::Middleware Exporter); |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
20
|
|
7
|
2
|
|
|
2
|
|
168
|
use Plack::Util::Accessor qw(map modify); |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
30
|
|
8
|
2
|
|
|
2
|
|
2242
|
use Plack::Request (); |
|
2
|
|
|
|
|
111202
|
|
|
2
|
|
|
|
|
54
|
|
9
|
2
|
|
|
2
|
|
22
|
use URI::Escape (); |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
34
|
|
10
|
2
|
|
|
2
|
|
12
|
use Hash::MultiValue; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
883
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our @EXPORT_OK = qw(rewrite_query query_string); |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub call { |
15
|
5
|
|
|
5
|
1
|
35438
|
my ($self, $env) = @_; |
16
|
5
|
|
|
|
|
23
|
rewrite_query($env, modify => $self->modify, map => $self->map); |
17
|
5
|
|
|
|
|
24
|
$self->app->($env); |
18
|
|
|
|
|
|
|
} |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub rewrite_query { |
21
|
6
|
|
|
6
|
1
|
38423
|
my ($env, %config) = @_; |
22
|
|
|
|
|
|
|
|
23
|
6
|
|
|
|
|
44
|
my $query = Plack::Request->new($env)->query_parameters; |
24
|
6
|
|
|
|
|
649
|
my $map = $config{map}; |
25
|
6
|
|
|
|
|
12
|
my $modify = $config{modify}; |
26
|
|
|
|
|
|
|
|
27
|
6
|
100
|
|
|
|
19
|
if ($map) { |
28
|
2
|
|
|
|
|
9
|
my @keys = $query->keys; |
29
|
2
|
|
|
|
|
34
|
my @values = $query->values; |
30
|
2
|
|
|
|
|
21
|
$query->clear; |
31
|
6
|
|
|
|
|
37
|
$query->merge_flat( |
32
|
2
|
|
|
|
|
26
|
map { $map->($keys[$_], $values[$_]) } 0 .. $#keys |
33
|
|
|
|
|
|
|
); |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
6
|
100
|
|
|
|
81
|
if ($modify) { |
38
|
5
|
|
|
|
|
8
|
map { $modify->($_) } $query; # alias $_ to $query |
|
5
|
|
|
|
|
18
|
|
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
# rebuild QUERY_STRING |
42
|
6
|
|
|
|
|
115
|
$env->{QUERY_STRING} = query_string($query); |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
# this has become invalid if it existed |
45
|
6
|
|
|
|
|
29
|
delete $env->{'plack.request.merged'}; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub query_string { |
49
|
6
|
|
|
6
|
1
|
11
|
my ($query) = @_; |
50
|
6
|
|
|
|
|
21
|
my @values = map { URI::Escape::uri_escape($_) } $query->values; |
|
17
|
|
|
|
|
232
|
|
51
|
17
|
|
|
|
|
115
|
join '&', map { |
52
|
17
|
|
|
|
|
157
|
$_ . '=' . shift @values |
53
|
6
|
|
|
|
|
85
|
} map { URI::Escape::uri_escape($_) } $query->keys; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
1; |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
__END__ |