| 1 | ## no critic (RequireUseStrict) | ||||||||||||||||||||||||||||
| 2 | package Plack::Middleware::SetAccept; | ||||||||||||||||||||||||||||
| 3 | 1 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | $Plack::Middleware::SetAccept::VERSION = '0.01'; | |||||||||||||||||||||||||||
| 5 | } | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | ## use critic (RequireUseStrict) | ||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use parent 'Plack::Middleware'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use List::MoreUtils qw(any); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use URI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use URI::QueryParam; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 0 | ▶ | sub prepare_app { | ||||||||||||||||||||||||||
|
Subroutine
prepare_app: not called CC=11 SCAR=48.8
Pod
documented
| |||||||||||||||||||||||||||||
| 18 | 0 | my ( $self ) = @_; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 0 | my ( $from, $mapping, $param ) = @{$self}{qw/from mapping param/}; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 0 | ▶ | unless($from) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | 0 | croak "'from' parameter is required"; | |||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | 0 | ▶ | unless($mapping) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 0 | croak "'mapping' parameter is required"; | |||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | 0 | ▶ | $from = [ $from ] unless ref($from); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 0 | ▶ | unless(@$from) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | 0 | croak "'from' parameter cannot be an empty array reference"; | |||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | 0 | ▶ | if(my ( $bad ) = grep { $_ ne 'suffix' && $_ ne 'param' } @$from) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$_ ne 'suffix' && $_ ne 'param'
MC/DC0%0/2
$_ ne 'suffix' && $_ ne 'param'
$_ ne 'suffix'
$_ ne 'param'
Truth table0%0/3
A && B
A= $_ ne 'suffix'B= $_ ne 'param'
| |||||||||||||||||||||||||||||
| 33 | 0 | croak "'$bad' is not a valid value for the 'from' parameter"; | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | 0 | ▶ | if(grep { $_ eq 'param' } @$from) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 0 | ▶ | unless($param) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 0 | croak "'param' parameter is required when using 'param' for from"; | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | 0 | ▶ | unless(exists $self->{'tolerant'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 0 | $self->{'tolerant'} = 1; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 0 | ▶ | unless(ref($mapping) eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 0 | croak "'mapping' parameter must be a hash reference"; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 0 | ▶ | sub get_uri { | ||||||||||||||||||||||||||
|
Subroutine
get_uri: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 50 | 0 | my ( $self, $env ) = @_; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 0 | my $host; | |||||||||||||||||||||||||||
| 53 | 0 | ▶ | unless($host = $env->{'HTTP_HOST'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 0 | $host = $env->{'SERVER_NAME'}; | |||||||||||||||||||||||||||
| 55 | 0 | ▶ | unless($env->{'SERVER_PORT'} == 80) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 0 | $host .= ':' . $env->{'SERVER_PORT'}; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | return URI->new( | ||||||||||||||||||||||||||||
| 61 | $env->{'psgi.url_scheme'} . '://' . | ||||||||||||||||||||||||||||
| 62 | $host . | ||||||||||||||||||||||||||||
| 63 | 0 | $env->{'REQUEST_URI'} | |||||||||||||||||||||||||||
| 64 | ); | ||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 0 | ▶ | sub extract_format { | ||||||||||||||||||||||||||
|
Subroutine
extract_format: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||||||||||||||||||||
| 68 | 0 | my ( $self, $env ) = @_; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 0 | my @format; | |||||||||||||||||||||||||||
| 71 | 0 | my $from = $self->{'from'}; | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 0 | ▶ | $from = [ $from ] unless ref $from; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 0 | my @reasons; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 0 | my $uri = $self->get_uri($env); | |||||||||||||||||||||||||||
| 78 | 0 | foreach (@$from) { | |||||||||||||||||||||||||||
| 79 | 0 | ▶ | if($_ eq 'suffix') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 80 | 0 | my $path = $uri->path; | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 0 | ▶ | if($path =~ /\.([^.]+)$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 0 | push @format, $1; | |||||||||||||||||||||||||||
| 84 | 0 | $path = $`; | |||||||||||||||||||||||||||
| 85 | 0 | $uri->path($path); | |||||||||||||||||||||||||||
| 86 | 0 | push @reasons, 'suffix'; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | } elsif($_ eq 'param') { | ||||||||||||||||||||||||||||
| 89 | 0 | my @values = $uri->query_param_delete($self->{'param'}); | |||||||||||||||||||||||||||
| 90 | 0 | ▶ | if(@values) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 0 | push @format, @values; | |||||||||||||||||||||||||||
| 92 | 0 | push @reasons, 'param'; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | 0 | ▶ | if(@reasons) { # if there has been any modification | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 0 | $env->{'PATH_INFO'} = $uri->path; | |||||||||||||||||||||||||||
| 98 | 0 | $env->{'REQUEST_URI'} = $uri->path_query; | |||||||||||||||||||||||||||
| 99 | 0 | $env->{'QUERY_STRING'} = $uri->query; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | 0 | return ( \@format, \@reasons ); | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 0 | ▶ | sub acceptable { | ||||||||||||||||||||||||||
|
Subroutine
acceptable: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 105 | 0 | my ( $self, $accept ) = @_; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 0 | my %acceptable = map { s/;.*$//; $_ => 1 } split /\s*,\s*/, $accept; | |||||||||||||||||||||||||||
| 108 | 0 | return grep { $acceptable{$_} } values %{ $self->{'mapping'} }; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 0 | ▶ | sub unacceptable { | ||||||||||||||||||||||||||
|
Subroutine
unacceptable: not called CC=11 SCAR=48.8
Pod
documented
| |||||||||||||||||||||||||||||
| 112 | 0 | my ( $self, $env, $reasons ) = @_; | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 0 | ▶ | if($self->{'tolerant'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 0 | return $self->app->($env); | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 0 | my $host; | |||||||||||||||||||||||||||
| 119 | 0 | ▶ | unless($host = $env->{'HTTP_HOST'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 0 | $host = $env->{'SERVER_NAME'}; | |||||||||||||||||||||||||||
| 121 | 0 | ▶ | unless($env->{'SERVER_PORT'} == 80) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 0 | $host .= ':' . $env->{'SERVER_PORT'}; | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | 0 | my $path = $env->{'PATH_INFO'}; | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 0 | my $content; | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 0 | ▶ | if($env->{'REQUEST_METHOD'} eq 'GET') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | 0 | $content = '<html xmlns="http://www.w3.org/1999/xhtml"><body><ul>'; | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 0 | my $from; | |||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 0 | ▶ | if(@$reasons) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | 0 | $from = $reasons->[0]; | |||||||||||||||||||||||||||
| 136 | } else { | ||||||||||||||||||||||||||||
| 137 | 0 | $from = $self->{'from'}; | |||||||||||||||||||||||||||
| 138 | 0 | ▶ | $from = $from->[0] if ref $from; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 0 | ▶ | if($from eq 'suffix') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 142 | 0 | foreach my $format (sort keys %{$self->{'mapping'}}) { | |||||||||||||||||||||||||||
| 143 | 0 | my $type = $self->{'mapping'}{$format}; | |||||||||||||||||||||||||||
| 144 | 0 | $content .= "<li><a href='http://$host$path.$format'>$type</a></li>"; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | } elsif($from eq 'param') { | ||||||||||||||||||||||||||||
| 147 | 0 | my $param = $self->{'param'}; | |||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 0 | foreach my $format (sort keys %{$self->{'mapping'}}) { | |||||||||||||||||||||||||||
| 150 | 0 | my $type = $self->{'mapping'}{$format}; | |||||||||||||||||||||||||||
| 151 | 0 | $content .= "<li><a href='http://$host$path?$param=$format'>$type</a></li>"; | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | 0 | $content .= '</ul></body></html>'; | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | return [ | ||||||||||||||||||||||||||||
| 157 | 0 | 406, | |||||||||||||||||||||||||||
| 158 | ['Content-Type' => 'application/xhtml+xml'], | ||||||||||||||||||||||||||||
| 159 | [$content], | ||||||||||||||||||||||||||||
| 160 | ]; | ||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 0 | ▶ | sub call { | ||||||||||||||||||||||||||
|
Subroutine
call: not called CC=17 SCAR=57.2
Pod
documented
| |||||||||||||||||||||||||||||
| 164 | 0 | my ( $self, $env ) = @_; | |||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 0 | my $method = $env->{'REQUEST_METHOD'}; | |||||||||||||||||||||||||||
| 167 | 0 | ▶ | if($method eq 'GET' || $method eq 'HEAD') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$method eq "GET" or $method eq "HEAD"
MC/DC0%0/2
$method eq "GET" or $method eq "HEAD"
$method eq "GET"
$method eq "HEAD"
Truth table0%0/3
A or B
A= $method eq "GET"B= $method eq "HEAD"
| |||||||||||||||||||||||||||||
| 168 | 0 | my ( $format, $reasons ) = $self->extract_format($env); | |||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 0 | ▶ | if(@$format) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | 0 | ▶ | my $accept = $env->{'HTTP_ACCEPT'} || ''; | ||||||||||||||||||||||||||
|
Condition0%0/2
$env->{'HTTP_ACCEPT'} || ''
MC/DC0%0/1
$env->{'HTTP_ACCEPT'} || ''
$env->{'HTTP_ACCEPT'}
Truth table0%0/2
A
A= $env->{'HTTP_ACCEPT'}
| |||||||||||||||||||||||||||||
| 172 | 0 | ▶ | if((any { exists $self->{'mapping'}{$_} } @$format) || $self->acceptable($accept)) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
Condition0%0/3
&any(sub {
exists $self->{"mapping"}{$_};
}
, @$format) or $self->acceptable($accept)
MC/DC0%0/2
&any(sub {
exists $self->{"mapping"}{$_};
}
, @$format) or $self->acceptable($accept)
&any(sub {
exists $self->{"mapping"}{$_};
}
, @$format)
$self->acceptable($accept)
Truth table0%0/3
A or B
A= &any(sub {
exists $self->{"mapping"}{$_};
}
, @$format)B= $self->acceptable($accept)
| |||||||||||||||||||||||||||||
| 173 | 0 | @$format = grep { exists $self->{'mapping'}{$_} } @$format; | |||||||||||||||||||||||||||
| 174 | } else { | ||||||||||||||||||||||||||||
| 175 | 0 | return $self->unacceptable($env, $reasons); | |||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 0 | my @accept = split /\s*,\s*/, $accept; | |||||||||||||||||||||||||||
| 179 | 0 | foreach my $f (@$format) { | |||||||||||||||||||||||||||
| 180 | 0 | my $mapping = $self->{'mapping'}{$f}; | |||||||||||||||||||||||||||
| 181 | 0 | my $mapping_noparams = $mapping; | |||||||||||||||||||||||||||
| 182 | 0 | $mapping_noparams =~ s/;.*$//; | |||||||||||||||||||||||||||
| 183 | 0 | my ( $mapping_type ) = split /\//, $mapping; | |||||||||||||||||||||||||||
| 184 | 0 | foreach my $accept (@accept) { | |||||||||||||||||||||||||||
| 185 | 0 | my $accept_noparams = $accept; | |||||||||||||||||||||||||||
| 186 | 0 | $accept_noparams =~ s/;.*$//; | |||||||||||||||||||||||||||
| 187 | 0 | ▶ | if($accept_noparams eq $mapping_noparams) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 188 | 0 | undef $accept; | |||||||||||||||||||||||||||
| 189 | 0 | last; | |||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | 0 | ▶ | next unless defined($accept) && $accept =~ /\*/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $accept and $accept =~ /\*/
MC/DC0%0/2
defined $accept and $accept =~ /\*/
defined $accept
$accept =~ /\*/
Truth table0%0/3
A and B
A= defined $acceptB= $accept =~ /\*/
| |||||||||||||||||||||||||||||
| 192 | 0 | my ( $type ) = split /\//, $accept; | |||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 0 | ▶ | if($type eq '*' || $type eq $mapping_type) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$type eq "*" or $type eq $mapping_type
MC/DC0%0/2
$type eq "*" or $type eq $mapping_type
$type eq "*"
$type eq $mapping_type
Truth table0%0/3
A or B
A= $type eq "*"B= $type eq $mapping_type
| |||||||||||||||||||||||||||||
| 195 | 0 | undef $accept; | |||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||
| 198 | 0 | ▶ | push @accept, $mapping if defined $mapping; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | 0 | $env->{'HTTP_ACCEPT'} = join(', ', grep { defined } @accept); | |||||||||||||||||||||||||||
| 201 | } else { | ||||||||||||||||||||||||||||
| 202 | 0 | ▶ | if(exists $env->{'HTTP_ACCEPT'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 203 | 0 | my $accept = $env->{'HTTP_ACCEPT'}; | |||||||||||||||||||||||||||
| 204 | 0 | ▶ | unless($self->acceptable($accept)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 205 | 0 | return $self->unacceptable($env, $reasons); | |||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||
| 207 | } else { | ||||||||||||||||||||||||||||
| 208 | 0 | $env->{'HTTP_ACCEPT'} = '*/*' | |||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||
| 212 | 0 | return $self->app->($env); | |||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | 1 | 1; | |||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | =pod | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | =head1 NAME | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | Plack::Middleware::SetAccept - Sets the Accept header based on the suffix or query params of a request | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | =head1 VERSION | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | version 0.01 | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | use Plack::Builder; | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | my %map = ( | ||||||||||||||||||||||||||||
| 234 | json => 'application/json', | ||||||||||||||||||||||||||||
| 235 | xml => 'application/xml', | ||||||||||||||||||||||||||||
| 236 | ); | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | builder { | ||||||||||||||||||||||||||||
| 239 | enable 'SetAccept', from => 'suffix', mapping => \%map; | ||||||||||||||||||||||||||||
| 240 | $app; | ||||||||||||||||||||||||||||
| 241 | }; | ||||||||||||||||||||||||||||
| 242 | # now /foo.json behaves as /foo, with Accept: application/json | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | # or | ||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | builder { | ||||||||||||||||||||||||||||
| 247 | enable 'SetAccept', from => 'param', param => 'format', mapping => \%map; | ||||||||||||||||||||||||||||
| 248 | $app; | ||||||||||||||||||||||||||||
| 249 | }; | ||||||||||||||||||||||||||||
| 250 | # now /foo?format=xml behaves as /foo, with Accept: application/xml | ||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | # or | ||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | builder { | ||||||||||||||||||||||||||||
| 255 | enable 'SetAccept', from => ['suffix', 'param'], param => 'format', mapping => \%map; | ||||||||||||||||||||||||||||
| 256 | $app; | ||||||||||||||||||||||||||||
| 257 | }; | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | This middleware sets the Accept header by extracting a piece of the request | ||||||||||||||||||||||||||||
| 262 | URI. It can extract from either the suffix of the path (ex. /foo.json) or | ||||||||||||||||||||||||||||
| 263 | from the query string (ex. /foo?format=json) for HEAD and GET requests. The | ||||||||||||||||||||||||||||
| 264 | value is looked up in a mapping table and is added to the Accept header. | ||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | =head1 PARAMETERS | ||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | =head2 from | ||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | Specifies from where the middleware is to extract the accept string. Valid | ||||||||||||||||||||||||||||
| 271 | values for this are 'suffix', 'param', or an array reference containing | ||||||||||||||||||||||||||||
| 272 | either/both of those values. The order in the array reference doesn't really | ||||||||||||||||||||||||||||
| 273 | matter, except for when the middleware generates XHTML links on a 406 error. | ||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | =head2 param | ||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | Only required when using 'param' for from. Specifies the query string | ||||||||||||||||||||||||||||
| 278 | parameter that specifies the lookup value for the mapping table. | ||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | =head2 mapping | ||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||
| 282 | A hash table containing Accept mappings. The keys should be the possible | ||||||||||||||||||||||||||||
| 283 | values extracted from the URI, and the values should be the mime types | ||||||||||||||||||||||||||||
| 284 | associated with the keys. | ||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | =head2 tolerant | ||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||
| 288 | If this option is falsy (defaults to 1), a 406 response code will be | ||||||||||||||||||||||||||||
| 289 | generated for "unacceptable" values. The body of the response will | ||||||||||||||||||||||||||||
| 290 | contain an XHTML document with a list of alternative links. | ||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | L<Plack>, L<Plack::Middleware> | ||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | =begin comment | ||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | =over | ||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | =item prepare_app | ||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | =item get_uri | ||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | =item extract_format | ||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | =item acceptable | ||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | =item unacceptable | ||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | =item call | ||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | =back | ||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | =end comment | ||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | Rob Hoelz <rob@hoelz.ro> | ||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||
| 320 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | This software is copyright (c) 2011 by Rob Hoelz. | ||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||
| 324 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 325 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | =head1 BUGS | ||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||
| 329 | Please report any bugs or feature requests on the bugtracker website | ||||||||||||||||||||||||||||
| 330 | http://github.com/hoelzro/plack-middleware-setaccept/issues | ||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | When submitting a bug or request, please include a test-file or a | ||||||||||||||||||||||||||||
| 333 | patch to an existing test-file that illustrates the bug or desired | ||||||||||||||||||||||||||||
| 334 | feature. | ||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | =cut | ||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | __END__ | ||||||||||||||||||||||||||||