| 1 | package LWP::MediaTypes; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | require Exporter; | |||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | @ISA = qw(Exporter); | |||||||||||||||||||||||||||||||||||||||||
| 5 | 1 | @EXPORT = qw(guess_media_type media_suffix); | |||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | @EXPORT_OK = qw(add_type add_encoding read_media_types); | |||||||||||||||||||||||||||||||||||||||||
| 7 | 1 | our $VERSION = '6.05'; | |||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | # note: These hashes will also be filled with the entries found in | ||||||||||||||||||||||||||||||||||||||||||
| 14 | # the 'media.types' file. | ||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 1 | my %suffixType = ( | |||||||||||||||||||||||||||||||||||||||||
| 17 | 'txt' => 'text/plain', | ||||||||||||||||||||||||||||||||||||||||||
| 18 | 'html' => 'text/html', | ||||||||||||||||||||||||||||||||||||||||||
| 19 | 'gif' => 'image/gif', | ||||||||||||||||||||||||||||||||||||||||||
| 20 | 'jpg' => 'image/jpeg', | ||||||||||||||||||||||||||||||||||||||||||
| 21 | 'xml' => 'text/xml', | ||||||||||||||||||||||||||||||||||||||||||
| 22 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | 1 | my %suffixExt = ( | |||||||||||||||||||||||||||||||||||||||||
| 25 | 'text/plain' => 'txt', | ||||||||||||||||||||||||||||||||||||||||||
| 26 | 'text/html' => 'html', | ||||||||||||||||||||||||||||||||||||||||||
| 27 | 'image/gif' => 'gif', | ||||||||||||||||||||||||||||||||||||||||||
| 28 | 'image/jpeg' => 'jpg', | ||||||||||||||||||||||||||||||||||||||||||
| 29 | 'text/xml' => 'xml', | ||||||||||||||||||||||||||||||||||||||||||
| 30 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | #XXX: there should be some way to define this in the media.types files. | ||||||||||||||||||||||||||||||||||||||||||
| 33 | 1 | my %suffixEncoding = ( | |||||||||||||||||||||||||||||||||||||||||
| 34 | 'Z' => 'compress', | ||||||||||||||||||||||||||||||||||||||||||
| 35 | 'gz' => 'gzip', | ||||||||||||||||||||||||||||||||||||||||||
| 36 | 'hqx' => 'x-hqx', | ||||||||||||||||||||||||||||||||||||||||||
| 37 | 'uu' => 'x-uuencode', | ||||||||||||||||||||||||||||||||||||||||||
| 38 | 'z' => 'x-pack', | ||||||||||||||||||||||||||||||||||||||||||
| 39 | 'bz2' => 'x-bzip2', | ||||||||||||||||||||||||||||||||||||||||||
| 40 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | 1 | read_media_types(); | |||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | sub guess_media_type | ||||||||||||||||||||||||||||||||||||||||||
| 47 | 29 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
guess_media_type: called CC=17 SCAR=28.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | 29 | my($file, $header) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 49 | 29 | ▶ | return undef unless defined $file; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | 29 | my $fullname; | |||||||||||||||||||||||||||||||||||||||||
| 52 | 29 | ▶ | if (ref $file) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | 9 | ▶ | croak("Unable to determine filetype on unblessed refs") unless blessed($file); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | 8 | ▶ | if ($file->can('path')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 4 | $file = $file->path; | |||||||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||||||||
| 57 | elsif ($file->can('filename')) { | ||||||||||||||||||||||||||||||||||||||||||
| 58 | 2 | $fullname = $file->filename; | |||||||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||||||
| 60 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 61 | 2 | $fullname = "" . $file; | |||||||||||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||||||||||
| 64 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 65 | 20 | $fullname = $file; # enable peek at actual file | |||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | 28 | my @encoding = (); | |||||||||||||||||||||||||||||||||||||||||
| 69 | 28 | my $ct = undef; | |||||||||||||||||||||||||||||||||||||||||
| 70 | 28 | for (file_exts($file)) { | |||||||||||||||||||||||||||||||||||||||||
| 71 | # first check this dot part as encoding spec | ||||||||||||||||||||||||||||||||||||||||||
| 72 | 28 | ▶ | if (exists $suffixEncoding{$_}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | 10 | unshift(@encoding, $suffixEncoding{$_}); | |||||||||||||||||||||||||||||||||||||||||
| 74 | 10 | next; | |||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||
| 76 | 18 | ▶ | if (exists $suffixEncoding{lc $_}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | 2 | unshift(@encoding, $suffixEncoding{lc $_}); | |||||||||||||||||||||||||||||||||||||||||
| 78 | 2 | next; | |||||||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | # check content-type | ||||||||||||||||||||||||||||||||||||||||||
| 82 | 16 | ▶ | if (exists $suffixType{$_}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | 14 | $ct = $suffixType{$_}; | |||||||||||||||||||||||||||||||||||||||||
| 84 | 14 | last; | |||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||
| 86 | 2 | ▶ | if (exists $suffixType{lc $_}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | $ct = $suffixType{lc $_}; | |||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | last; | |||||||||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | # don't know nothing about this dot part, bail out | ||||||||||||||||||||||||||||||||||||||||||
| 92 | 2 | last; | |||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||
| 94 | 28 | ▶ | unless (defined $ct) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | # Take a look at the file | ||||||||||||||||||||||||||||||||||||||||||
| 96 | 14 | ▶ | if (defined $fullname) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | 12 | ▶ | $ct = (-T $fullname) ? "text/plain" : "application/octet-stream"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||||||
| 99 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 100 | 2 | $ct = "application/octet-stream"; | |||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | 28 | ▶ | if ($header) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | 1 | $header->header('Content-Type' => $ct); | |||||||||||||||||||||||||||||||||||||||||
| 106 | 1 | ▶ | $header->header('Content-Encoding' => \@encoding) if @encoding; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | 28 | ▶ | wantarray ? ($ct, @encoding) : $ct; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||
| 113 | 3 | ▶ | sub media_suffix { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
media_suffix: called CC=11 SCAR=27.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | 3 | ▶ | if (!wantarray && @_ == 1 && $_[0] !~ /\*/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
not wantarray and @_ == 1 and not $_[0] =~ /\*/
not wantarray and @_ == 1
MC/DC33%1/3
not wantarray and @_ == 1 and not $_[0] =~ /\*/
not wantarray
@_ == 1
not $_[0] =~ /\*/
Truth table50%2/4
not(A) and B and not(C)
A= not wantarrayB= @_ == 1C= not $_[0] =~ /\*/
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | 1 | return $suffixExt{lc $_[0]}; | |||||||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||||||
| 117 | 2 | my(@type) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 118 | 2 | my(@suffix, $ext, $type); | |||||||||||||||||||||||||||||||||||||||||
| 119 | 2 | foreach (@type) { | |||||||||||||||||||||||||||||||||||||||||
| 120 | 2 | ▶ | if (s/\*/.*/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 121 | 2 | ▶ | while(($ext,$type) = each(%suffixType)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | 1766 | ▶ | push(@suffix, $ext) if $type =~ /^$_$/i; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||||||||
| 125 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | my $ltype = lc $_; | |||||||||||||||||||||||||||||||||||||||||
| 127 | 0 | ▶ | while(($ext,$type) = each(%suffixType)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 128 | 0 | ▶ | push(@suffix, $ext) if lc $type eq $ltype; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||||||||||||
| 132 | 2 | ▶ | wantarray ? @suffix : $suffix[0]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||
| 136 | sub file_exts | ||||||||||||||||||||||||||||||||||||||||||
| 137 | 28 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
file_exts: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 138 | 28 | require File::Basename; | |||||||||||||||||||||||||||||||||||||||||
| 139 | 28 | my @parts = reverse split(/\./, File::Basename::basename($_[0])); | |||||||||||||||||||||||||||||||||||||||||
| 140 | 28 | pop(@parts); # never consider first part | |||||||||||||||||||||||||||||||||||||||||
| 141 | 28 | @parts; | |||||||||||||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | sub add_type | ||||||||||||||||||||||||||||||||||||||||||
| 146 | 2065 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_type: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 147 | 2065 | my($type, @exts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 148 | 2065 | for my $ext (@exts) { | |||||||||||||||||||||||||||||||||||||||||
| 149 | 2651 | $ext =~ s/^\.//; | |||||||||||||||||||||||||||||||||||||||||
| 150 | 2651 | $suffixType{$ext} = $type; | |||||||||||||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||||||||||||
| 152 | 2065 | ▶ | $suffixExt{lc $type} = $exts[0] if @exts; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||
| 156 | sub add_encoding | ||||||||||||||||||||||||||||||||||||||||||
| 157 | 2 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_encoding: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 158 | 2 | my($type, @exts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 159 | 2 | for my $ext (@exts) { | |||||||||||||||||||||||||||||||||||||||||
| 160 | 2 | $ext =~ s/^\.//; | |||||||||||||||||||||||||||||||||||||||||
| 161 | 2 | $suffixEncoding{$ext} = $type; | |||||||||||||||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||
| 166 | sub read_media_types | ||||||||||||||||||||||||||||||||||||||||||
| 167 | 1 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
read_media_types: called CC=8 SCAR=20.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 168 | 1 | my(@files) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||
| 170 | 1 | local($/, $_) = ("\n", undef); # ensure correct $INPUT_RECORD_SEPARATOR | |||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||
| 172 | 1 | my @priv_files = (); | |||||||||||||||||||||||||||||||||||||||||
| 173 | push(@priv_files, "$ENV{HOME}/.media.types", "$ENV{HOME}/.mime.types") | ||||||||||||||||||||||||||||||||||||||||||
| 174 | 1 | ▶ | if defined $ENV{HOME}; # Some doesn't have a home (for instance Win32) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||
| 176 | # Try to locate "media.types" file, and initialize %suffixType from it | ||||||||||||||||||||||||||||||||||||||||||
| 177 | 1 | my $typefile; | |||||||||||||||||||||||||||||||||||||||||
| 178 | 1 | ▶ | unless (@files) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 179 | 1 | @files = map {"$_/LWP/media.types"} @INC; | |||||||||||||||||||||||||||||||||||||||||
| 180 | 1 | push @files, @priv_files; | |||||||||||||||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||||||||||||||||
| 182 | 1 | for $typefile (@files) { | |||||||||||||||||||||||||||||||||||||||||
| 183 | 10 | local(*TYPE); | |||||||||||||||||||||||||||||||||||||||||
| 184 | 10 | ▶ | open(TYPE, $typefile) || next; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 185 | 3 | ▶ | while (<TYPE>) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 186 | 4437 | ▶ | next if /^\s*#/; # comment line | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 187 | 2064 | ▶ | next if /^\s*$/; # blank line | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 188 | 2064 | s/#.*//; # remove end-of-line comments | |||||||||||||||||||||||||||||||||||||||||
| 189 | 2064 | my($type, @exts) = split(' ', $_); | |||||||||||||||||||||||||||||||||||||||||
| 190 | 2064 | add_type($type, @exts); | |||||||||||||||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||||||||||||||||
| 192 | 3 | close(TYPE); | |||||||||||||||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||||||
| 196 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||||
| 199 | __END__ | ||||||||||||||||||||||||||||||||||||||||||