1 package LWP::MediaTypes;
5 @EXPORT = qw(guess_media_type media_suffix);
6 @EXPORT_OK = qw(add_type add_encoding read_media_types);
12 # note: These hashes will also be filled with the entries found in
13 # the 'media.types' file.
16 'txt' => 'text/plain',
17 'html' => 'text/html',
19 'jpg' => 'image/jpeg',
24 'text/plain' => 'txt',
25 'text/html' => 'html',
27 'image/jpeg' => 'jpg',
31 #XXX: there should be some way to define this in the media.types files.
32 my %suffixEncoding = (
47 Data::Dumper->new([\%suffixType, \%suffixExt, \%suffixEncoding],
48 [qw(*suffixType *suffixExt *suffixEncoding)])->Dump;
54 my($file, $header) = @_;
55 return undef unless defined $file;
61 #XXX should handle non http:, file: or ftp: URIs differently
64 $fullname = $file; # enable peek at actual file
69 for (file_exts($file)) {
70 # first check this dot part as encoding spec
71 if (exists $suffixEncoding{$_}) {
72 unshift(@encoding, $suffixEncoding{$_});
75 if (exists $suffixEncoding{lc $_}) {
76 unshift(@encoding, $suffixEncoding{lc $_});
81 if (exists $suffixType{$_}) {
82 $ct = $suffixType{$_};
85 if (exists $suffixType{lc $_}) {
86 $ct = $suffixType{lc $_};
90 # don't know nothing about this dot part, bail out
93 unless (defined $ct) {
94 # Take a look at the file
95 if (defined $fullname) {
96 $ct = (-T $fullname) ? "text/plain" : "application/octet-stream";
99 $ct = "application/octet-stream";
104 $header->header('Content-Type' => $ct);
105 $header->header('Content-Encoding' => \@encoding) if @encoding;
108 wantarray ? ($ct, @encoding) : $ct;
113 if (!wantarray && @_ == 1 && $_[0] !~ /\*/) {
114 return $suffixExt{$_[0]};
117 my(@suffix, $ext, $type);
120 while(($ext,$type) = each(%suffixType)) {
121 push(@suffix, $ext) if $type =~ /^$_$/;
125 while(($ext,$type) = each(%suffixType)) {
126 push(@suffix, $ext) if $type eq $_;
130 wantarray ? @suffix : $suffix[0];
136 require File::Basename;
137 my @parts = reverse split(/\./, File::Basename::basename($_[0]));
138 pop(@parts); # never consider first part
145 my($type, @exts) = @_;
146 for my $ext (@exts) {
148 $suffixType{$ext} = $type;
150 $suffixExt{$type} = $exts[0] if @exts;
156 my($type, @exts) = @_;
157 for my $ext (@exts) {
159 $suffixEncoding{$ext} = $type;
168 local($/, $_) = ("\n", undef); # ensure correct $INPUT_RECORD_SEPARATOR
172 push(@priv_files, "$ENV{HOME}:media.types", "$ENV{HOME}:mime.types")
173 if defined $ENV{HOME}; # Some does not have a home (for instance Win32)
176 push(@priv_files, "$ENV{HOME}/.media.types", "$ENV{HOME}/.mime.types")
177 if defined $ENV{HOME}; # Some doesn't have a home (for instance Win32)
180 # Try to locate "media.types" file, and initialize %suffixType from it
184 @files = map {$_."LWP:media.types"} @INC;
187 @files = map {"$_/LWP/media.types"} @INC;
189 push @files, @priv_files;
191 for $typefile (@files) {
193 open(TYPE, $typefile) || next;
194 LWP::Debug::debug("Reading media types from $typefile");
196 next if /^\s*#/; # comment line
197 next if /^\s*$/; # blank line
198 s/#.*//; # remove end-of-line comments
199 my($type, @exts) = split(' ', $_);
200 add_type($type, @exts);
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");
295 Copyright 1995-1999 Gisle Aas.
297 This library is free software; you can redistribute it and/or
298 modify it under the same terms as Perl itself.