REST-Utils
view release on metacpan or search on metacpan
lib/REST/Utils.pm view on Meta::CPAN
}
return $media_type;
}
=head3 parse_media_range($range)
Carves up a media range and returns a list of the C<($type, $subtype,\%params)>
where %params is a hash of all the parameters for the media range.
For example, the media range 'application/*;q=0.5' would get
parsed into:
('application', '*', { q => 0.5 })
In addition this function also guarantees that there is a value for 'q' in the
%params hash, filling it in with a proper default if necessary.
=cut
sub parse_media_range {
my ($range) = @_;
my ( $type, $subtype, $params ) = parse_mime_type($range);
if ( !exists $params->{q}
|| !$params->{q}
|| !looks_like_number( $params->{q} )
|| $params->{q} > 1
|| $params->{q} < 0 )
{
$params->{q} = 1;
}
return $type, $subtype, $params;
}
=head3 parse_mime_type($mime_type)
Carves up a MIME type and returns a list of the ($type, $subtype,
\%params) where %params is a hash of all the parameters for the media range.
For example, the media range 'application/xhtml;q=0.5' would get parsed into:
('application', 'xhtml', { q => 0.5 })
=cut
sub parse_mime_type {
my ($mime_type) = @_;
my @parts = split /;/msx, $mime_type;
my %params =
map { _strip($_) } map { split /=/msx, $_, 2 } @parts[ 1 .. $#parts ];
my $full_type = _strip( $parts[0] );
# Java URLConnection class sends an Accept header that includes a single
# "*" Turn it into a legal wildcard.
if ( $full_type eq q{*} ) {
$full_type = q{*/*};
}
my ( $type, $subtype ) = split m{/}msx, $full_type;
return _strip($type), _strip($subtype), \%params;
}
=head3 quality($mime_type, $ranges)
Returns the quality 'q' of a MIME type when compared against the
media-ranges in $ranges. For example:
print quality('text/html', 'text/*;q=0.3, text/html;q=0.7, text/html;level
# 0.7
=cut
sub quality {
my ( $mime_type, $ranges ) = @_;
my @parsed_ranges = map { [ parse_media_range($_) ] } split /,/msx, $ranges;
return quality_parsed( $mime_type, @parsed_ranges );
}
=head3 quality_parsed($mime_type, @parsed_ranges)
Find the best match for a given MIME type against a list of media_ranges
that have already been parsed by parse_media_range(). Returns the 'q'
quality parameter of the best match, 0 if no match was found. This
function behaves the same as quality() except that @parsed_ranges must
be a list of parsed media ranges.
=cut
sub quality_parsed {
my (@args) = @_;
return ( fitness_and_quality_parsed(@args) )[1];
}
=head3 request_method($cgi)
This function returns the query's HTTP request method.
Example 1:
my $method = request_method($cgi);
This function takes a L<CGI|CGI> or compatible object as its first parameter.
Because many web sites don't allow the full set of HTTP methods needed
for REST, you can "tunnel" methods through C<GET> or C<POST> requests in
the following ways:
In the query with the C<_method> parameter. This will work even with C<POST>
requests where parameters are usually passed in the request body.
Example 2:
http://localhost/index.cgi?_method=DELETE
Or with the C<X-HTTP-Method-Override> HTTP header.
Example 3:
( run in 2.357 seconds using v1.01-cache-2.11-cpan-71847e10f99 )