2
# $Id: MediaTypes.pm,v 1.27 1999/11/16 14:36:24 gisle Exp $
4
1
package LWP::MediaTypes;
8
LWP::MediaTypes - guess media type for a file or a URL
12
use LWP::MediaTypes qw(guess_media_type);
13
$type = guess_media_type("/tmp/foo.gif");
17
This module provides functions for handling media (also known as
18
MIME) types and encodings. The mapping from file extentions to media
19
types is defined by the F<media.types> file. If the F<~/.media.types>
20
file exists it is used instead.
21
For backwards compatability we will also look for F<~/.mime.types>.
23
The following functions are exported by default:
29
####################################################################
3
# $Id: MediaTypes.pm,v 1.32 2004/11/17 11:04:09 gisle Exp $
32
6
@ISA = qw(Exporter);
33
7
@EXPORT = qw(guess_media_type media_suffix);
34
8
@EXPORT_OK = qw(add_type add_encoding read_media_types);
35
$VERSION = sprintf("%d.%02d", q$Revision: 1.27 $ =~ /(\d+)\.(\d+)/);
9
$VERSION = sprintf("%d.%02d", q$Revision: 1.32 $ =~ /(\d+)\.(\d+)/);
37
11
require LWP::Debug;
64
38
'bz2' => 'x-bzip2',
68
46
require Data::Dumper;
69
47
Data::Dumper->new([\%suffixType, \%suffixExt, \%suffixEncoding],
70
48
[qw(*suffixType *suffixExt *suffixEncoding)])->Dump;
77
=item guess_media_type($filename_or_url, [$header_to_modify])
79
This function tries to guess media type and encoding for a file or url.
80
It returns the content-type, which is a string like C<"text/html">.
81
In array context it also returns any content-encodings applied (in the
82
order used to encode the file). You can pass a URI object
83
reference, instead of the file name.
85
If the type can not be deduced from looking at the file name,
86
then guess_media_type() will let the C<-T> Perl operator take a look.
87
If this works (and C<-T> returns a TRUE value) then we return
88
I<text/plain> as the type, otherwise we return
89
I<application/octet-stream> as the type.
91
The optional second argument should be a reference to a HTTP::Headers
92
object or any object that implements the $obj->header method in a
93
similar way. When it is present the values of the
94
'Content-Type' and 'Content-Encoding' will be set for this header.
98
52
sub guess_media_type
156
=item media_suffix($type,...)
158
This function will return all suffixes that can be used to denote the
159
specified media type(s). Wildcard types can be used. In a scalar
160
context it will return the first suffix found.
164
@suffixes = media_suffix('image/*', 'audio/basic');
165
$suffix = media_suffix('text/html');
169
112
sub media_suffix {
170
113
if (!wantarray && @_ == 1 && $_[0] !~ /\*/) {
171
114
return $suffixExt{$_[0]};
213
LWP::MediaTypes - guess media type for a file or a URL
217
use LWP::MediaTypes qw(guess_media_type);
218
$type = guess_media_type("/tmp/foo.gif");
222
This module provides functions for handling media (also known as
223
MIME) types and encodings. The mapping from file extensions to media
224
types is defined by the F<media.types> file. If the F<~/.media.types>
225
file exists it is used instead.
226
For backwards compatibility we will also look for F<~/.mime.types>.
228
The following functions are exported by default:
232
=item guess_media_type( $filename )
234
=item guess_media_type( $uri )
236
=item guess_media_type( $filename_or_uri, $header_to_modify )
238
This function tries to guess media type and encoding for a file or a URI.
239
It returns the content type, which is a string like C<"text/html">.
240
In array context it also returns any content encodings applied (in the
241
order used to encode the file). You can pass a URI object
242
reference, instead of the file name.
244
If the type can not be deduced from looking at the file name,
245
then guess_media_type() will let the C<-T> Perl operator take a look.
246
If this works (and C<-T> returns a TRUE value) then we return
247
I<text/plain> as the type, otherwise we return
248
I<application/octet-stream> as the type.
250
The optional second argument should be a reference to a HTTP::Headers
251
object or any object that implements the $obj->header method in a
252
similar way. When it is present the values of the
253
'Content-Type' and 'Content-Encoding' will be set for this header.
255
=item media_suffix( $type, ... )
257
This function will return all suffixes that can be used to denote the
258
specified media type(s). Wildcard types can be used. In a scalar
259
context it will return the first suffix found. Examples:
261
@suffixes = media_suffix('image/*', 'audio/basic');
262
$suffix = media_suffix('text/html');
266
The following functions are only exported by explicit request:
270
=item add_type( $type, @exts )
272
Associate a list of file extensions with the given media type.
275
add_type("x-world/x-vrml" => qw(wrl vrml));
277
=item add_encoding( $type, @ext )
279
Associate a list of file extensions with an encoding type.
282
add_encoding("x-gzip" => "gz");
284
=item read_media_types( @files )
286
Parse media types files and add the type mappings found there.
289
read_media_types("conf/mime.types");