From: rjung Date: Tue, 3 May 2011 12:12:35 +0000 (+0000) Subject: Add a script to check web.xml and httpd mime.types X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=0d93383fdbfcbcbdb9b5c6d48dae857135f97aa7;p=tomcat7.0 Add a script to check web.xml and httpd mime.types for differences. git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1099032 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/res/scripts/check-mime.pl b/res/scripts/check-mime.pl new file mode 100755 index 000000000..f5ce06bce --- /dev/null +++ b/res/scripts/check-mime.pl @@ -0,0 +1,410 @@ +#!/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. +# +# $Id$ +# ----------------------------------------------------------------------------- + +# The script uses two mime type lists to describe +# the merging between httpd and Tomcat mime types. +# +# 1) %TOMCAT_ONLY: Additional extensions for Tomcat that do not exist in httpd +# 2) %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 1) and 2) and generates a new web.xml: +# +# A) Additional extensions in Tomcat which are not part of 1) +# are logged. They will be removed in the generated new web.xml. +# If you want to keep them, add them to the list 1) 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 the list 2) are logged. +# They will be overwritten wit the httpd definition in the generated +# new web.xml. If you want to keep their Tomcat definition, add them +# to the list 1) 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, update web.xml with the +# generated new web.xml. +# D) If the extensions are not sorted alphabetically, a message is logged. +# The generated web.xml will be always be sorted alphabetically. +# If you want to fix the sort order, update web.xml with 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" +$main::VERSION = '1.0'; + +# 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 + flac audio/flac + gz application/x-gzip + hqx application/mac-binhex40 + htc text/x-component + jsf text/plain + jspf text/plain + m4a audio/mp4 + 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 + z application/x-compress + 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 + 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 @cols; +my $extension; +my $type; +my $comment; +my $commented; +my $check; +my $msg; + + +# 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); + +# Read and parse httpd mime.types, build up hash extension->mime-type +open(MIMETYPES, "<$opt_m") or die "Could not open file '$opt_m' for read - Aborting!"; +while () { + chomp($_); + $line = $_; + $line =~ s/#.*//; + $line =~ s/^\s+//; + if ($line ne '') { + @cols = split(/\s+/, $line); + if ($#cols > 0) { + for ($i=1; $i <= $#cols; $i++) { + $httpd{$cols[$i]} = $cols[0]; + } + } else { + print STDERR "WARN mime.types line ignored: $_\n"; + } + } +} +close(MIMETYPES); + +# Read and parse web.xml, build up hash extension->mime-type +# and store the file parts form before and after mime mappings. +open(WEBXML, "<$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 () { + if ($_ !~ //) { + $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*\s*$/) { + $comment = $1; + $_ = ; + chomp($_); + } + if ($_ =~ /^\s*([^<]*)<\/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 ...', got '$_' - Aborting!\n"; + close(WEBXML); + exit 2; + } + $_ = ; + chomp($_); + if ($_ =~ /^\s*([^<]*)<\/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 ...', got '$_' - Aborting!\n"; + close(WEBXML); + exit 3; + } + $_ = ; + chomp($_); + if ($_ !~ /^\s*<\/mime-mapping>\s*$/) { + print STDERR "ERROR Parse error in Tomcat mime-mapping line $.\n"; + print STDERR "ERROR Expected '', got '$_' - Aborting!\n"; + close(WEBXML); + exit 4; + } + $_ = ; + # Check for comment closure + if ($commented && $_ =~ /^[^<]*-->\s*$/) { + $commented = 0; + $_ = ; + } + # Check for comment opening + if ($_ =~ /^\s*\n"; + } + print NEW " $extension\n"; + print NEW " $httpd{$extension}\n"; + print NEW " \n"; + if (exists($tomcat_commented{$extension})) { + print NEW " -->\n"; + } +} +print NEW $tomcat_post; +close(NEW); +print "New file '$opt_o' has been written.\n"; +