/
Formatter.pm
185 lines (132 loc) Β· 4.39 KB
/
Formatter.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package Raisin::Middleware::Formatter;
use strict;
use warnings;
use parent 'Plack::Middleware';
use HTTP::Status qw(:constants);
use Plack::Request;
use Plack::Response;
use Plack::Util;
use Plack::Util::Accessor qw(
default_format
format
encoder
decoder
);
sub call {
my ($self, $env) = @_;
# Pre-process
my $req = Plack::Request->new($env);
if ($req->content) {
my %media_types_map_flat_hash = $self->decoder->media_types_map_flat_hash;
my $format = $media_types_map_flat_hash{ $req->content_type };
unless ($format) {
Raisin::log(info => "unsupported media type: ${ \$req->content_type }");
return Plack::Response->new(HTTP_UNSUPPORTED_MEDIA_TYPE)->finalize;
}
$env->{'raisinx.decoder'} = $format;
my $d = Plack::Util::load_class($self->decoder->for($format));
$env->{'raisinx.body_params'} = $d->deserialize($req->content);
}
my $format = $self->negotiate_format($req);
unless ($format) {
return Plack::Response->new(HTTP_NOT_ACCEPTABLE)->finalize;
}
$env->{'raisinx.encoder'} = $format;
my $res = $self->app->($env);
# Post-process
Plack::Util::response_cb($res, sub {
# TODO: delayed responses
my $res = shift;
my $r = Plack::Response->new(@$res);
if (ref $r->body) {
my $s = Plack::Util::load_class($self->encoder->for($format));
$r->content_type($s->content_type) unless $r->content_type;
$r->body($s->serialize($r->body));
}
@$res = @{ $r->finalize };
return;
});
}
sub _accept_header_set { length(shift || '') }
sub _path_has_extension {
my $path = shift;
my @chunks = split /\./, $path;
scalar(@chunks) > 1;
}
sub negotiate_format {
my ($self, $req) = @_;
# PRECEDENCE:
# - extension
# - headers
# - default
my @formats = do {
if (_path_has_extension($req->path)) {
$self->format_from_extension($req->path);
}
elsif (_accept_header_set($req->header('Accept'))) {
$self->format_from_header($req->header('Accept'));
}
else {
$self->default_format;
}
};
if ($self->format && !scalar grep { $self->format eq $_ } @formats) {
return;
}
shift @formats;
}
sub format_from_extension {
my ($self, $path) = @_;
my @p = split /\./, $path;
return if scalar @p <= 1;
my %media_types_map_flat_hash = $self->encoder->media_types_map_flat_hash;
my $format = $media_types_map_flat_hash{ $p[-1] };
return unless $format;
$format;
}
sub format_from_header {
my ($self, $accept) = @_;
return unless $accept;
my %media_types_map_flat_hash = $self->encoder->media_types_map_flat_hash;
# Add a default format as a `*/*`
$media_types_map_flat_hash{'*/*'} = $self->default_format;
my @media_types;
for my $type (split /\s*,\s*/, $accept) {
my ($media, $params) = split /;/, $type, 2;
# Cleaning up media type by deleting a Vendor tree
$media =~ s/vnd\.[^+]+\+//g;
next unless my $format = $media_types_map_flat_hash{$media};
my $q = ($params // '') =~ /q=([\d\.]+)/ ? $1 : 1;
push @media_types, { format => $format, q => $q };
}
map { $_->{format} } sort { $b->{q} <=> $a->{q} } @media_types;
}
1;
__END__
=head1 NAME
Raisin::Middleware::Formatter - A parser/formatter middleware for L<Raisin>.
=head1 DESCRIPTION
Parses and formats the data it gets from requests and responses if it's needed.
=head1 METHODS
=head2 negotiate_format
Negotiates a format from path extension, C<Accept> header or using default format.
A precedence is following:
=over
=item * extension;
=item * C<Accept> header;
=item * default;
=back
In other words if an extension exists the framework doesn't look for C<Accept>
header. If the extension is not supported the framework throws an error,
the same is for C<Accept> header. Only if both extension and C<Accept> header
are not specified it fallback to default format.
=head2 format_from_extension
Extracts an extension from a path, and if exists looks for a formatter.
=head2 format_from_header
Parses C<Accept> header for known formatters.
=head1 AUTHOR
Artur Khabibullin - rtkh E<lt>atE<gt> cpan.org
=head1 LICENSE
This module and all the modules in this package are governed by the same license
as Perl itself.
=cut