Parse mime-types, match against media ranges
Perl
Latest commit 9be1940 Dec 24, 2015 @ap take control of prereqs
Permalink
Failed to load latest commit information.
lib/Parse reflow and de-boilerplate the docs Jan 5, 2015
t clean up test suite Jan 5, 2015
.gitignore switch to Dist::Zilla Aug 3, 2012
Changes release 1.003 Jan 10, 2015
README.pod release 1.003 Jan 10, 2015
cpanfile take control of prereqs Dec 24, 2015
dist.ini take control of prereqs Dec 24, 2015

README.pod

NAME

Parse::MIME - Parse mime-types, match against media ranges

VERSION

version 1.003

SYNOPSIS

 use Parse::MIME qw( best_match );
 print best_match( [ qw( application/xbel+xml text/xml ) ], 'text/*;q=0.5,*/*; q=0.1' );
 # text/xml

DESCRIPTION

This module provides basic functions for handling mime-types. It can handle matching mime-types against a list of media-ranges. See section 14.1 of the HTTP specification [RFC 2616] for a complete explanation: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1

INTERFACE

None of the following functions are exported by default. You can use the :all tag to import all of them into your package:

 use Parse::MIME ':all';

parse_mime_type

Parses a mime-type into its component parts and returns type, subtype and params, where params is a reference to a hash of all the parameters for the media range:

 parse_mime_type 'application/xhtml;q=0.5'
 # ( 'application', 'xhtml', { q => 0.5 } )

parse_media_range

Media-ranges are mime-types with wild-cards and a q quality parameter. This function works just like "parse_mime_type", but also guarantees that there is a value for q in the params hash, supplying the default value if necessary.

 parse_media_range 'application/xhtml'
 # ( 'application', 'xhtml', { q => 1 } )

parse_media_range_list

Media-range lists are comma-separated lists of media ranges. This function works just like "parse_media_range", but accepts a list of media ranges and returns for all of media-ranges.

 my @l = parse_media_range_list 'application/xhtml, text/html;q=0.7'
 # ( 'application', 'xhtml', { q => 1 }, 'text', 'html', { q => 0.7 } )

fitness_and_quality_parsed

Find the best match for a given mime-type (passed as the first parameter) against a list of media ranges that have already been parsed by "parse_media_range" (passed as a flat list). Returns the fitness value and the value of the q quality parameter of the best match, or ( -1, 0 ) if no match was found.

 # for @l see above
 fitness_and_quality_parsed( 'text/html', @l )
 # ( 110, 0.7 )

quality

Determines the quality (q) of a mime-type (passed as the first parameter) when compared against a media-range list string. F.ex.:

 quality( 'text/html', 'text/*;q=0.3, text/html;q=0.7, text/html;level=1, text/html;level=2;q=0.4, */*;q=0.5' )
 # 0.7

quality_parsed

Just like "quality", except the second parameter must be pre-parsed by "parse_media_range_list".

best_match

Choose the mime-type with the highest quality (q) from a list of candidates. Takes an array of supported mime-types as the first parameter and finds the best match for all the media-ranges listed in header, which is passed as the second parameter. The value of header must be a string that conforms to the format of the HTTP Accept header. F.ex.:

 best_match( [ qw( application/xbel+xml text/xml ) ], 'text/*;q=0.5,*/*; q=0.1' )
 # 'text/xml'

AUTHOR

Aristotle Pagaltzis <pagaltzis@gmx.de>

COPYRIGHT AND LICENSE

This software is copyright (c) 2015 by Aristotle Pagaltzis.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.