home *** CD-ROM | disk | FTP | other *** search
/ Acorn User 10 / AU_CD10.iso / Archived / Updates / Perl / libwww_for_perl_109 / site_perl / LWP / MediaTypes.pm < prev    next >
Text File  |  1997-12-02  |  6KB  |  221 lines

  1. #
  2. # $Id: MediaTypes.pm,v 1.19 1997/12/02 13:22:52 aas Exp $
  3.  
  4. package LWP::MediaTypes;
  5.  
  6. =head1 NAME
  7.  
  8. LWP::MediaTypes - guess media type for a file or a URL.
  9.  
  10. =head1 SYNOPSIS
  11.  
  12.  use LWP::MediaTypes qw(guess_media_type);
  13.  $type = guess_media_type("/tmp/foo.gif");
  14.  
  15. =head1 DESCRIPTION
  16.  
  17. This module provides functions for handling of 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 exist it is used as a replacement.
  21. For backwards compatability we will also look for F<~/.mime.types>.
  22.  
  23. The following functions are available (and exported by default):
  24.  
  25. =over 4
  26.  
  27. =cut
  28.  
  29. ####################################################################
  30.  
  31. require Exporter;
  32. @ISA = qw(Exporter);
  33. @EXPORT = qw(guess_media_type media_suffix);
  34.  
  35. require LWP::Debug;
  36. use strict;
  37.  
  38. # note: These hashes will also be filled with the entries found in
  39. # the 'media.types' file.
  40.  
  41. my %suffixType = (
  42.     'txt'   => 'text/plain',
  43.     'html'  => 'text/html',
  44.     'gif'   => 'image/gif',
  45.     'jpg'   => 'image/jpeg',
  46. );
  47.  
  48. my %suffixExt = (
  49.     'text/plain' => 'txt',
  50.     'text/html'  => 'h',
  51.     'image/gif'  => 'gif',
  52.     'image/jpeg' => 'jpg',
  53. );
  54.  
  55. #XXX: there should be some way to define this in the media.types files.
  56. my %suffixEncoding = (
  57.     'Z'   => 'compress',
  58.     'gz'  => 'gzip',
  59.     'hqx' => 'x-hqx',
  60.     'uu'  => 'x-uuencode',
  61.     'z'   => 'x-pack'
  62. );
  63.  
  64. local($/, $_) = ("\n", undef);  # ensure correct $INPUT_RECORD_SEPARATOR
  65.  
  66. my @priv_files = ();
  67. push(@priv_files, "$ENV{HOME}/.media.types", "$ENV{HOME}/.mime.types")
  68.   if defined $ENV{HOME};  # Some does not have a home (for instance Win32)
  69.  
  70. # Try to locate "media.types" file, and initialize %suffixType from it
  71. my $typefile;
  72. for $typefile ((map {"$_/LWP/media.types"} @INC), @priv_files) {
  73.     local(*TYPE);
  74.     open(TYPE, $typefile) || next;
  75.     LWP::Debug::debug("Reading media types from $typefile");
  76.     while (<TYPE>) {
  77.     next if /^\s*#/; # comment line
  78.     next if /^\s*$/; # blank line
  79.     s/#.*//;         # remove end-of-line comments
  80.     my($type, @exts) = split(' ', $_);
  81.     $suffixExt{$type} = $exts[0] if @exts;
  82.     my $ext;
  83.     for $ext (@exts) {
  84.         $suffixType{$ext} = $type;
  85.     }
  86.     }
  87.     close(TYPE);
  88. }
  89.  
  90.  
  91. ####################################################################
  92.  
  93. =item guess_media_type($filename_or_url, [$header_to_modify])
  94.  
  95. This function tries to guess media type and encoding for given file.
  96. In scalar context it returns only the content-type.  In array context
  97. it returns an array consisting of content-type followed by any
  98. content-encodings applied.
  99.  
  100. The guess_media_type function also accepts a URI::URL object as argument.
  101.  
  102. If the type can not be deduced from looking at the file name only,
  103. then guess_media_type() will take a look at the actual file using the
  104. C<-T> perl operator in order to determine if this is a text file
  105. (text/plain).  If this does not work it will return
  106. I<application/octet-stream> as the type.
  107.  
  108. The optional second argument should be a reference to a HTTP::Headers
  109. object (or some HTTP::Message object).  When present this function
  110. will set the value of the 'Content-Type' and 'Content-Encoding' for
  111. this header.
  112.  
  113. =cut
  114.  
  115. sub guess_media_type
  116. {
  117.     my($file, $header) = @_;
  118.     return undef unless defined $file;
  119.  
  120.     my $fullname;
  121.     if (ref($file)) {
  122.     # assume URI::URL object
  123.     $file = $file->path;
  124.     #XXX should handle non http:, file: or ftp: URLs differently
  125.     } else {
  126.     $fullname = $file;  # enable peek at actual file
  127.     }
  128.     $file =~ s,.*/,,;   # only basename left
  129.     my @parts = reverse split(/\./, $file);
  130.     pop(@parts);        # never concider first part
  131.  
  132.     my @encoding = ();
  133.     my $ct = undef;
  134.     for (@parts) {
  135.     # first check this dot part as encoding spec
  136.     if (exists $suffixEncoding{$_}) {
  137.         unshift(@encoding, $suffixEncoding{$_});
  138.         next;
  139.     }
  140.     if (exists $suffixEncoding{lc $_}) {
  141.         unshift(@encoding, $suffixEncoding{lc $_});
  142.         next;
  143.     }
  144.  
  145.     # check content-type
  146.     if (exists $suffixType{$_}) {
  147.         $ct = $suffixType{$_};
  148.         last;
  149.     }
  150.     if (exists $suffixType{lc $_}) {
  151.         $ct = $suffixType{lc $_};
  152.         last;
  153.     }
  154.  
  155.     # don't know nothing about this dot part, bail out
  156.     last;
  157.     }
  158.     unless (defined $ct) {
  159.     # Take a look at the file
  160.     if (defined $fullname) {
  161.         $ct = (-T $fullname) ? "text/plain" : "application/octet-stream";
  162.     } else {
  163.         $ct = "application/octet-stream";
  164.     }
  165.     }
  166.  
  167.     if ($header) {
  168.     $header->header('Content-Type' => $ct);
  169.     $header->header('Content-Encoding' => \@encoding) if @encoding;
  170.     }
  171.  
  172.     wantarray ? ($ct, @encoding) : $ct;
  173. }
  174.  
  175.  
  176. =item media_suffix($type,...)
  177.  
  178. This function will return all suffixes that can be used to denote the
  179. specified media type(s).  Wildcard types can be used.  In scalar
  180. context it will return the first suffix found.
  181.  
  182. Examples:
  183.  
  184.   @suffixes = media_suffix('image/*', 'audio/basic');
  185.   $suffix = media_suffix('text/html');
  186.  
  187. =cut
  188.  
  189. sub media_suffix {
  190.     if (!wantarray && @_ == 1 && $_[0] !~ /\*/) {
  191.     return $suffixExt{$_[0]};
  192.     }
  193.     my(@type) = @_;
  194.     my(@suffix, $ext, $type);
  195.     foreach (@type) {
  196.     if (s/\*/.*/) {
  197.         while(($ext,$type) = each(%suffixType)) {
  198.         push(@suffix, $ext) if $type =~ /^$_$/;
  199.         }
  200.     } else {
  201.         while(($ext,$type) = each(%suffixType)) {
  202.         push(@suffix, $ext) if $type eq $_;
  203.         }
  204.     }
  205.     }
  206.     wantarray ? @suffix : $suffix[0];
  207. }
  208.  
  209. 1;
  210.  
  211. =back 
  212.  
  213. =head1 COPYRIGHT
  214.  
  215. Copyright 1995-1997 Gisle Aas.
  216.  
  217. This library is free software; you can redistribute it and/or
  218. modify it under the same terms as Perl itself.
  219.  
  220. =cut
  221.