home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / perl_ste.zip / LWP / MediaTypes.pm < prev    next >
Text File  |  1997-05-25  |  5KB  |  211 lines

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