blib/lib/LWP/MediaTypes.pm

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