Code Monkey home page Code Monkey logo

parse-mime's Introduction

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 <[email protected]>

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.

parse-mime's People

Contributors

ap avatar

Watchers

 avatar Karen Etheridge avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.