1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
|
#!/usr/bin/perl
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# -----------------------------------------------------------------------------
# Merge the MIME type definitions contained in the
# file mime.types from the httpd project into Tomcat web.xml.
# -----------------------------------------------------------------------------
# The script uses two mime type lists to describe
# the merging between httpd and Tomcat mime types.
#
# - %TOMCAT_ONLY: Additional extensions for Tomcat that do not exist in httpd
# - %TOMCAT_KEEP: Mime type differences for common extensions where we stick to
# the Tomcat definition
# The script checks consistency between Tomcat and httpd according
# to the lists TOMCAT_ONLY and TOMCAT_KEEP and generates a new web.xml:
#
# A) Additional extensions in Tomcat which are not part of TOMCAT_ONLY
# are logged. They will be removed in the generated new web.xml.
# If you want to keep them, add them to TOMCAT_ONLY and run the
# script again. If you want to remove them, commit the generated
# new web.xml.
# B) Mime type differences for the same extension between httpd
# and Tomcat that are not part of TOMCAT_KEEP are logged.
# They will be overwritten with the httpd definition in the generated
# new web.xml. If you want to keep their Tomcat definition, add them
# to TOMCAT_KEEP and run the script again. If you want to use the
# definitions from httpd, commit the generated new web.xml.
# C) Additional extensions in httpd are logged. The script outputs a
# merged web.xml, which already includes all those additional
# extensions. If you want to keep them, commit the generated
# new web.xml.
# D) If the extensions are not sorted alphabetically, a message is logged.
# The generated web.xml will always be sorted alphabetically.
# If you want to keep the alphabetical sort order, commit the generated
# new web.xml.
use strict;
use locale;
use POSIX qw(locale_h);
use Getopt::Std;
################### BEGIN VARIABLES WHICH MUST BE MAINTAINED #####################
# Script version, printed via getopts with "--version"
our $VERSION = '1.1';
# Locale used via LC_COLLATE when sorting extensions
my $LOCALE = 'en.UTF-8';
# Mime types that are part of the Tomcat
# configuration, but missing from httpd
my %TOMCAT_ONLY = qw(
abs audio/x-mpeg
aim application/x-aim
anx application/annodex
art image/x-jg
avx video/x-rad-screenplay
axa audio/annodex
axv video/annodex
body text/html
dib image/bmp
dv video/x-dv
gz application/x-gzip
htc text/x-component
jsf text/plain
jspf text/plain
m4b audio/mp4
m4r audio/mp4
mp1 audio/mpeg
mpa audio/mpeg
mac image/x-macpaint
mpega audio/x-mpeg
mpv2 video/mpeg2
pict image/pict
pnt image/x-macpaint
qti image/x-quicktime
qtif image/x-quicktime
shtml text/x-server-parsed-html
ulw audio/basic
wasm application/wasm
z application/x-compress
);
# Mime types, that are defined differently
# in Tomcat than in httpd
my %TOMCAT_KEEP = qw(
cdf application/x-cdf
class application/java
exe application/octet-stream
flac audio/flac
m4v video/mp4
mif application/x-mif
pct image/pict
pic image/pict
pls audio/x-scpls
);
################### END VARIABLES WHICH MUST BE MAINTAINED #####################
# Global data variables
# Mime type definitions from httpd
my %httpd;
# Mime type definitions from Tomcat
my %tomcat;
# Comments found when parsing mime type definitions
my %tomcat_comments;
# Is the whole mime type commented out?
my %tomcat_commented;
# List of extensions found in the original order
my @tomcat_extensions;
# Text in web.xml before and after the mime-type definitions
my $tomcat_pre; my $tomcat_post;
# Helper variables
my $i;
my $line;
my $mimetype;
my @extensions;
my $extension;
my $type;
my $comment;
my $commented;
my $msg;
my $previous;
my $current;
# File handles
my $mimetypes_fh;
my $webxml_fh;
my $output_fh;
# Usage/Help
sub HELP_MESSAGE {
my $fh = shift;
print $fh "Usage:: $0 -m MIMEFILE -i INPUTFILE -o OUTPUTFILE\n";
print $fh " MIMEFILE: path to mime.types from the httpd project\n";
print $fh " INPUTFILE: path to existing web.xml, which will be checked\n";
print $fh " OUTPUTFILE: path to the new (generated) web.xml. Any existing\n";
print $fh " file will be overwritten.\n";
}
# Parse arguments:
# -m: mime.types file (httpd) to use
# -i: input web.xml file to check
# -o: output web.xml file (gets generated and overwritten)
$Getopt::Std::STANDARD_HELP_VERSION = 1;
our ($opt_m, $opt_i, $opt_o);
getopts('m:i:o:');
# Check whether mandatory arguments are given
if ($opt_m eq '' || $opt_i eq '' || $opt_o eq '') {
HELP_MESSAGE(*STDOUT);
exit 1;
}
# Switch locale for alphabetical ordering
setlocale(LC_COLLATE, $LOCALE);
# Check whether TOMCAT_ONLY and TOMCAT_KEEP are disjoint
for $extension (sort keys %TOMCAT_ONLY) {
if (exists($TOMCAT_KEEP{$extension})) {
push(@extensions, ($extension));
}
}
if (@extensions > 0) {
print STDERR "FATAL Lists TOMCAT_ONLY and TOMCAT_KEEP must be disjoint.\n";
print STDERR "FATAL Common entries are: " . join(', ', @extensions) . " - Aborting!\n";
exit 6;
}
# Read and parse httpd mime.types, build up hash extension->mime-type
open($mimetypes_fh, '<', $opt_m) or die "Could not open file '$opt_m' for read - Aborting!";
while (<$mimetypes_fh>) {
chomp($_);
$line = $_;
$line =~ s/#.*//;
$line =~ s/^\s+//;
if ($line ne '') {
($mimetype, @extensions) = split(/\s+/, $line);
if (@extensions > 0) {
for $extension (@extensions) {
$httpd{$extension} = $mimetype;
}
} else {
print STDERR "WARN mime.types line ignored: $_\n";
}
}
}
close($mimetypes_fh);
# Read and parse web.xml, build up hash extension->mime-type
# and store the file parts form before and after mime mappings.
open($webxml_fh, '<', $opt_i) or die "Could not open file '$opt_i' for read - Aborting!";
# Skip and record all lines before the first mime type definition.
# Because of comment handling we need to read one line ahead.
$line = '';
while (<$webxml_fh>) {
if ($_ !~ /<mime-mapping>/) {
$tomcat_pre .= $line;
} else {
last;
}
$line = $_;
}
$commented = 0;
# If the previous line was start of a comment
# set marker, else add it to pre.
if ($line =~ /^\s*<!--[^>]*$/) {
$commented = 1;
} else {
$tomcat_pre .= $line;
}
# Now we parse blocks of the form:
# <mime-mapping>
# <extension>abs</extension>
# <mime-type>audio/x-mpeg</mime-type>
# </mime-mapping>
# Optional single comment lines directly after "<mime-mapping>"
# are allowed. The whole block is also allowed to be commented out.
while ($_ =~ /^\s*<mime-mapping>\s*$/) {
$_ = <$webxml_fh>;
chomp($_);
$comment = '';
if ($_ =~ /^\s*<!--([^>]*)-->\s*$/) {
$comment = $1;
$_ = <$webxml_fh>;
chomp($_);
}
if ($_ =~ /^\s*<extension>([^<]*)<\/extension>\s*$/ ) {
$extension = $1;
$extension =~ s/^\s+//;
$extension =~ s/\s+$//;
} else {
print STDERR "ERROR Parse error in Tomcat mime-mapping line $.\n";
print STDERR "ERROR Expected <extension>...</extension>', got '$_' - Aborting!\n";
close($webxml_fh);
exit 2;
}
$_ = <$webxml_fh>;
chomp($_);
if ($_ =~ /^\s*<mime-type>([^<]*)<\/mime-type>\s*$/ ) {
$type = $1;
$type =~ s/^\s+//;
$type =~ s/\s+$//;
if (exists($tomcat{$extension}) && $tomcat{$extension} ne $type) {
print STDERR "WARN MIME mapping redefinition detected!\n";
print STDERR "WARN Kept '$extension' -> '$tomcat{$extension}'\n";
print STDERR "WARN Ignored '$extension' -> '$type'\n";
} else {
$tomcat{$extension} = $type;
if ($comment ne '') {
$tomcat_comments{$extension} = $comment;
}
if ($commented) {
$tomcat_commented{$extension} = 1;
}
push(@tomcat_extensions, $extension);
}
} else {
print STDERR "ERROR Parse error in Tomcat mime-mapping line $.\n";
print STDERR "ERROR Expected <mime-type>...</mime-type>', got '$_' - Aborting!\n";
close($webxml_fh);
exit 3;
}
$_ = <$webxml_fh>;
chomp($_);
if ($_ !~ /^\s*<\/mime-mapping>\s*$/) {
print STDERR "ERROR Parse error in Tomcat mime-mapping line $.\n";
print STDERR "ERROR Expected '</mime-mapping>', got '$_' - Aborting!\n";
close($webxml_fh);
exit 4;
}
$_ = <$webxml_fh>;
# Check for comment closure
if ($commented && $_ =~ /^[^<]*-->\s*$/) {
$commented = 0;
$_ = <$webxml_fh>;
}
# Check for comment opening
if ($_ =~ /^\s*<!--[^>]*$/) {
$commented = 1;
$line = $_;
$_ = <$webxml_fh>;
}
}
# Add back the last comment line already digested
if ($commented) {
$tomcat_post = $line;
}
# Read and record the remaining lines
$tomcat_post .= $_;
while (<$webxml_fh>) {
if ($_ =~ /<mime-mapping>/) {
print STDERR "ERROR mime-mapping blocks are not consecutive\n";
print STDERR "ERROR See line $. in $opt_i - Aborting!\n";
close($webxml_fh);
exit 5;
}
$tomcat_post .= $_;
}
close($webxml_fh);
# Look for extensions in TOMCAT_ONLY.
# Abort if it already exists in mime.types.
# Warn if they are no longer existing in web.xml.
for $extension (sort keys %TOMCAT_ONLY) {
if (exists($httpd{$extension})) {
if ($httpd{$extension} eq $TOMCAT_ONLY{$extension}) {
print STDERR "FATAL Consistent definition for '$extension' -> '$TOMCAT_ONLY{$extension}' exists in mime.types.\n";
print STDERR "FATAL You must remove '$extension' from the TOMCAT_ONLY list - Aborting!\n";
exit 7;
} else {
print STDERR "FATAL Definition '$extension' -> '$httpd{$extension}' exists in mime.types but\n";
print STDERR "FATAL differs from '$extension' -> '$TOMCAT_ONLY{$extension}' in TOMCAT_ONLY.\n";
print STDERR "FATAL You must either remove '$extension' from the TOMCAT_ONLY list to keep the mime.types variant,\n";
print STDERR "FATAL or move it to TOMCAT_KEEP to overwrite the mime.types variant - Aborting!\n";
exit 8;
}
}
if (!exists($tomcat{$extension})) {
print STDERR "WARN Additional extension '$extension' allowed by TOMCAT_ONLY list, but not found in web.xml\n";
print STDERR "WARN Definition '$extension' -> '$TOMCAT_ONLY{$extension}' will be added again to generated web.xml.\n";
print STDERR "WARN Consider removing it from TOMCAT_ONLY if you do not want to add back this extension.\n";
}
}
# Look for extensions in TOMCAT_KEEP.
# Abort if they do not exist in mime.types or have the same definition there..
# Warn if they are no longer existing in web.xml.
for $extension (sort keys %TOMCAT_KEEP) {
if (exists($httpd{$extension})) {
if ($httpd{$extension} eq $TOMCAT_KEEP{$extension}) {
print STDERR "FATAL Consistent definition for '$extension' -> '$TOMCAT_KEEP{$extension}' exists in mime.types.\n";
print STDERR "FATAL You must remove '$extension' from the TOMCAT_KEEP list - Aborting!\n";
exit 9;
}
} else {
print STDERR "WARN Definition '$extension' -> '$TOMCAT_KEEP{$extension}' does not exist in mime.types,\n";
print STDERR "FATAL so you must move it from TOMCAT_KEEP to TOMCAT_ONLY - Aborting!\n";
exit 10;
}
if (!exists($tomcat{$extension})) {
print STDERR "WARN Additional extension '$extension' allowed by TOMCAT_KEEP list, but not found in web.xml\n";
print STDERR "WARN Definition '$extension' -> '$TOMCAT_KEEP{$extension}' will be added again to generated web.xml.\n";
print STDERR "WARN Consider removing it from TOMCAT_KEEP if you do not want to add back this extension.\n";
}
}
# Look for extensions existing for Tomcat but not for httpd.
# Log them if they are not in TOMCAT_ONLY
for $extension (@tomcat_extensions) {
if (!exists($httpd{$extension})) {
if (!exists($TOMCAT_ONLY{$extension})) {
print STDERR "WARN Extension '$extension' found in web.xml but not in mime.types is missing from TOMCAT_ONLY list.\n";
print STDERR "WARN Definition '$extension' -> '$tomcat{$extension}' will be removed from generated web.xml.\n";
} elsif ($tomcat{$extension} ne $TOMCAT_ONLY{$extension}) {
print STDERR "WARN Additional extension '$extension' allowed by TOMCAT_ONLY list, but has new definition.\n";
print STDERR "WARN Definition '$extension' -> '$tomcat{$extension}' will be replaced" .
" by '$extension' -> '$TOMCAT_ONLY{$extension}' in generated web.xml.\n";
}
}
}
# Look for extensions with inconsistent mime types for Tomcat and httpd.
# Log them if they are not in TOMCAT_KEEP
for $extension (@tomcat_extensions) {
if (exists($httpd{$extension}) && $tomcat{$extension} ne $httpd{$extension}) {
if (!exists($TOMCAT_KEEP{$extension})) {
print STDERR "WARN Mapping '$extension' inconsistency is missing from TOMCAT_KEEP list.\n";
print STDERR "WARN Definition '$extension' -> '$tomcat{$extension}' will be replaced" .
" by '$extension' -> '$httpd{$extension}' in generated web.xml.\n";
} elsif ($tomcat{$extension} ne $TOMCAT_KEEP{$extension}) {
print STDERR "WARN Extension '$extension' inconsistency allowed by TOMCAT_KEEP list, but has new definition.\n";
print STDERR "WARN Definition '$extension' -> '$tomcat{$extension}' will be replaced" .
" by '$extension' -> '$TOMCAT_KEEP{$extension}' in generated web.xml.\n";
}
}
}
# Log if extensions in web.xml are not sorted alphabetically.
$msg = '';
$previous = '';
for $current (@tomcat_extensions) {
if ($previous ge $current) {
$msg .= "WARN Extension '$previous' defined before '$current'\n";
}
$previous = $current;
}
if ($msg ne '') {
print STDERR "WARN MIME type definitions in web.xml were not sorted alphabetically by extension\n";
print STDERR $msg;
print STDERR "WARN This will be fixed in the new generated web.xml file '$opt_o'.\n";
}
# Log all extensions defined for httpd but not for Tomcat
for $extension (sort keys %httpd) {
if (!exists($tomcat{$extension})) {
print STDERR "INFO Extension '$extension' found for httpd, but not for Tomcat.\n";
print STDERR "INFO Definition '$extension' -> '$httpd{$extension}' will be added" .
" to the generated web.xml.\n";
}
}
# Generate new web.xml:
# - Use definitions from httpd
# - Add TOMCAT_ONLY
# - Fix TOMCAT_KEEP
# - output tomcat_pre, sorted mime-mappings, tomcat_post.
while (($extension, $mimetype) = each %TOMCAT_ONLY) {
$httpd{$extension} = $mimetype;
}
while (($extension, $mimetype) = each %TOMCAT_KEEP) {
$httpd{$extension} = $mimetype;
}
open ($output_fh, '>', $opt_o) or die "Could not open file '$opt_o' for write - Aborting!";
print $output_fh $tomcat_pre;
for $extension (sort keys %httpd) {
if (exists($tomcat_commented{$extension})) {
print $output_fh " <!--\n";
}
print $output_fh " <mime-mapping>\n";
if (exists($tomcat_comments{$extension})) {
print $output_fh " <!--$tomcat_comments{$extension}-->\n";
}
print $output_fh " <extension>$extension</extension>\n";
print $output_fh " <mime-type>$httpd{$extension}</mime-type>\n";
print $output_fh " </mime-mapping>\n";
if (exists($tomcat_commented{$extension})) {
print $output_fh " -->\n";
}
}
print $output_fh $tomcat_post;
close($output_fh);
print "New file '$opt_o' has been written.\n";
|