1 package LWP::MediaTypes;
5 @EXPORT = qw(guess_media_type media_suffix);
6 @EXPORT_OK = qw(add_type add_encoding read_media_types);
11 # note: These hashes will also be filled with the entries found in
12 # the 'media.types' file.
15 'txt' => 'text/plain',
16 'html' => 'text/html',
18 'jpg' => 'image/jpeg',
23 'text/plain' => 'txt',
24 'text/html' => 'html',
26 'image/jpeg' => 'jpg',
30 #XXX: there should be some way to define this in the media.types files.
31 my %suffixEncoding = (
46 Data::Dumper->new([\%suffixType, \%suffixExt, \%suffixEncoding],
47 [qw(*suffixType *suffixExt *suffixEncoding)])->Dump;
53 my($file, $header) = @_;
54 return undef unless defined $file;
60 #XXX should handle non http:, file: or ftp: URIs differently
63 $fullname = $file; # enable peek at actual file
68 for (file_exts($file)) {
69 # first check this dot part as encoding spec
70 if (exists $suffixEncoding{$_}) {
71 unshift(@encoding, $suffixEncoding{$_});
74 if (exists $suffixEncoding{lc $_}) {
75 unshift(@encoding, $suffixEncoding{lc $_});
80 if (exists $suffixType{$_}) {
81 $ct = $suffixType{$_};
84 if (exists $suffixType{lc $_}) {
85 $ct = $suffixType{lc $_};
89 # don't know nothing about this dot part, bail out
92 unless (defined $ct) {
93 # Take a look at the file
94 if (defined $fullname) {
95 $ct = (-T $fullname) ? "text/plain" : "application/octet-stream";
98 $ct = "application/octet-stream";
103 $header->header('Content-Type' => $ct);
104 $header->header('Content-Encoding' => \@encoding) if @encoding;
107 wantarray ? ($ct, @encoding) : $ct;
112 if (!wantarray && @_ == 1 && $_[0] !~ /\*/) {
113 return $suffixExt{$_[0]};
116 my(@suffix, $ext, $type);
119 while(($ext,$type) = each(%suffixType)) {
120 push(@suffix, $ext) if $type =~ /^$_$/;
124 while(($ext,$type) = each(%suffixType)) {
125 push(@suffix, $ext) if $type eq $_;
129 wantarray ? @suffix : $suffix[0];
135 require File::Basename;
136 my @parts = reverse split(/\./, File::Basename::basename($_[0]));
137 pop(@parts); # never consider first part
144 my($type, @exts) = @_;
145 for my $ext (@exts) {
147 $suffixType{$ext} = $type;
149 $suffixExt{$type} = $exts[0] if @exts;
155 my($type, @exts) = @_;
156 for my $ext (@exts) {
158 $suffixEncoding{$ext} = $type;
167 local($/, $_) = ("\n", undef); # ensure correct $INPUT_RECORD_SEPARATOR
171 push(@priv_files, "$ENV{HOME}:media.types", "$ENV{HOME}:mime.types")
172 if defined $ENV{HOME}; # Some does not have a home (for instance Win32)
175 push(@priv_files, "$ENV{HOME}/.media.types", "$ENV{HOME}/.mime.types")
176 if defined $ENV{HOME}; # Some doesn't have a home (for instance Win32)
179 # Try to locate "media.types" file, and initialize %suffixType from it
183 @files = map {$_."LWP:media.types"} @INC;
186 @files = map {"$_/LWP/media.types"} @INC;
188 push @files, @priv_files;
190 for $typefile (@files) {
192 open(TYPE, $typefile) || next;
194 next if /^\s*#/; # comment line
195 next if /^\s*$/; # blank line
196 s/#.*//; # remove end-of-line comments
197 my($type, @exts) = split(' ', $_);
198 add_type($type, @exts);
211 LWP::MediaTypes - guess media type for a file or a URL
215 use LWP::MediaTypes qw(guess_media_type);
216 $type = guess_media_type("/tmp/foo.gif");
220 This module provides functions for handling media (also known as
221 MIME) types and encodings. The mapping from file extensions to media
222 types is defined by the F<media.types> file. If the F<~/.media.types>
223 file exists it is used instead.
224 For backwards compatibility we will also look for F<~/.mime.types>.
226 The following functions are exported by default:
230 =item guess_media_type( $filename )
232 =item guess_media_type( $uri )
234 =item guess_media_type( $filename_or_uri, $header_to_modify )
236 This function tries to guess media type and encoding for a file or a URI.
237 It returns the content type, which is a string like C<"text/html">.
238 In array context it also returns any content encodings applied (in the
239 order used to encode the file). You can pass a URI object
240 reference, instead of the file name.
242 If the type can not be deduced from looking at the file name,
243 then guess_media_type() will let the C<-T> Perl operator take a look.
244 If this works (and C<-T> returns a TRUE value) then we return
245 I<text/plain> as the type, otherwise we return
246 I<application/octet-stream> as the type.
248 The optional second argument should be a reference to a HTTP::Headers
249 object or any object that implements the $obj->header method in a
250 similar way. When it is present the values of the
251 'Content-Type' and 'Content-Encoding' will be set for this header.
253 =item media_suffix( $type, ... )
255 This function will return all suffixes that can be used to denote the
256 specified media type(s). Wildcard types can be used. In a scalar
257 context it will return the first suffix found. Examples:
259 @suffixes = media_suffix('image/*', 'audio/basic');
260 $suffix = media_suffix('text/html');
264 The following functions are only exported by explicit request:
268 =item add_type( $type, @exts )
270 Associate a list of file extensions with the given media type.
273 add_type("x-world/x-vrml" => qw(wrl vrml));
275 =item add_encoding( $type, @ext )
277 Associate a list of file extensions with an encoding type.
280 add_encoding("x-gzip" => "gz");
282 =item read_media_types( @files )
284 Parse media types files and add the type mappings found there.
287 read_media_types("conf/mime.types");
293 Copyright 1995-1999 Gisle Aas.
295 This library is free software; you can redistribute it and/or
296 modify it under the same terms as Perl itself.