diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2017-12-06 00:27:12 +0200 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2017-12-06 00:27:12 +0200 |
commit | fd07891cd3757bba51b09b55e5671f01809de220 (patch) | |
tree | 1fd4fe0d381c501c437b5a6005c083910fd4d7a3 | |
download | acpp-fd07891cd3757bba51b09b55e5671f01809de220.tar.gz acpp-fd07891cd3757bba51b09b55e5671f01809de220.tar.bz2 |
Initial commit
-rw-r--r-- | .gitignore | 13 | ||||
-rw-r--r-- | MANIFEST.SKIP | 60 | ||||
-rw-r--r-- | Makefile.PL | 16 | ||||
-rw-r--r-- | lib/Apache/Admin/Config/Expand.pm | 134 | ||||
-rw-r--r-- | t/inc.t | 91 |
5 files changed, 314 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0e4670f --- /dev/null +++ b/.gitignore @@ -0,0 +1,13 @@ +*~ +\#*# +.#* +*.bak +*.tar* +.emacs.* +/tmp/ +/buildreq/ +/debug.sh +core +/MANIFEST +/MYMETA.* +/Makefile diff --git a/MANIFEST.SKIP b/MANIFEST.SKIP new file mode 100644 index 0000000..859ca13 --- /dev/null +++ b/MANIFEST.SKIP @@ -0,0 +1,60 @@ +# Avoid version control files. +\bRCS\b +\bCVS\b +\bSCCS\b +,v$ +\B\.svn\b +\B\.git\b +\B\.gitignore\b +\b_darcs\b +\B\.cvsignore$ + +# Avoid VMS specific MakeMaker generated files +\bDescrip.MMS$ +\bDESCRIP.MMS$ +\bdescrip.mms$ + +# Avoid Makemaker generated and utility files. +\bMANIFEST\.bak +\bMakefile$ +\bblib/ +\bMakeMaker-\d +\bpm_to_blib\.ts$ +\bpm_to_blib$ +\bblibdirs\.ts$ # 6.18 through 6.25 generated this + +# Avoid Module::Build generated and utility files. +\bBuild$ +\b_build/ +\bBuild.bat$ +\bBuild.COM$ +\bBUILD.COM$ +\bbuild.com$ + +# Avoid temp and backup files. +~$ +\.old$ +\#$ +\b\.# +\.bak$ +\.tmp$ +\.# +\.rej$ + +# Avoid OS-specific files/dirs +# Mac OSX metadata +\B\.DS_Store +# Mac OSX SMB mount metadata files +\B\._ + +# Avoid Devel::Cover and Devel::CoverX::Covered files. +\bcover_db\b +\bcovered\b + +# Avoid MYMETA files +^MYMETA\. + +^debug.sh +^tmp +^buildreq +^\.emacs\.*
\ No newline at end of file diff --git a/Makefile.PL b/Makefile.PL new file mode 100644 index 0000000..0edde9e --- /dev/null +++ b/Makefile.PL @@ -0,0 +1,16 @@ +use strict; +use ExtUtils::MakeMaker; +use Module::Metadata; + +WriteMakefile(NAME => 'Apache::Admin::Config::Expand', + VERSION_FROM => 'lib/Apache/Admin/Config/Expand.pm', + AUTHOR => 'Sergey Poznyakoff <gray@gnu.org>', + LICENSE => 'gpl_3', + ABSTRACT_FROM => 'lib/Apache/Admin/Config/Expand.pm', + PREREQ_PM => { + 'Apache::Admin::Config' => '0.95', + 'File::Spec' => '3.39_02' + }, + MIN_PERL_VERSION => 5.016001 +); + diff --git a/lib/Apache/Admin/Config/Expand.pm b/lib/Apache/Admin/Config/Expand.pm new file mode 100644 index 0000000..c4fb2eb --- /dev/null +++ b/lib/Apache/Admin/Config/Expand.pm @@ -0,0 +1,134 @@ +package Apache::Admin::Config::Expand; +use parent 'Apache::Admin::Config'; +use strict; +use warnings; +use Carp; +use File::Spec; + +our $VERSION = '1.0'; + +sub new { + my $class = shift; + my $file = shift; + my $compact = Apache::Admin::Config::Tree::_get_arg(\@_, '-compact!'); + my $self = bless $class->SUPER::new($file, @_), $class; + $self->{_file} = $file; + $self->{_options} = \@_; + return unless $self->_preproc; + $self->_compact if $compact; + return $self; +} + +sub _preproc { + my $self = shift; + + for (my $i = 0; my $d = $self->select(-which => $i); $i++) { + if ($d->type eq 'directive' && $d->name =~ /^include(optional)?$/i) { + return unless $self->_include($d, $1); + $d->unlink; + } + } + return 1; +} + +sub _dequote { + my ($str) = @_; + if ($str =~ s/^"(.*)"$/$1/) { + $str =~ s/\\"/"/g; + } + return $str; +} + +sub _include { + my ($self, $d, $opt) = @_; + my $pat = _dequote($d->value); + unless (File::Spec->file_name_is_absolute($pat)) { + if (my $d = $self->directive('ServerRoot')) { + $pat = File::Spec->catfile(_dequote($d->value), $pat); + } + } + my @filelist = glob $pat; + if (@filelist) { + foreach my $file (@filelist) { + if (my $inc = new Apache::Admin::Config($file, + @{$self->{_options}})) { + while (my $obj = $inc->select(-which => 0)) { + $obj->move($self, -after => $d); + $d = $obj; + } + } else { + return 0; + } + } + } elsif (!$opt) { + return $self->_set_error("no files matching $pat"); + } + return 1; +} + +sub _compact { + my $self = shift; + for (my $i = 0; my $d = $self->select(-which => $i); ) { + if ($d->type eq 'blank' || $d->type eq 'comment') { + $d->delete; + } else { + if ($d->type eq 'section') { + _compact($d); + } + $i++; + } + } +} + +1; +__END__ + +=head1 NAME + +Apache::Admin::Config::Expand - Preprocess Apache configuration files + +=head1 SYNOPSIS + + use Apache::Admin::Config::Expand; + + $x = new Apache::Admin::Config::Expand '/path/to/httpd.conf', '-compact' + or die $Apache::Admin::Config::ERROR; + +=head1 DESCRIPTION + +B<Apache::Admin::Config::Expand> reads and parses Apache configuration +file, expanding any B<Include> and B<IncludeOptional> statements. Optionally, +whitespace and comments are eliminated. Apart from this, it provides the +same functionality as B<Apache::Admin::Config>, from which it inherits all +methods. + +=head1 METHODS + +=head2 new + + $obj = new Apache::Admin::Config::Expand [/path/to/file|handle], + [-compact], + [-indent => $integer], ['-create'], ['-no-comment-grouping'], + ['-no-blank-grouping'] + +Reads an Apache configuration file. The B<-compact> argument, if specified +causes any comment and empty lines to be removed from the configuration. +Rest of arguments and flags have the same meaning as in +B<Apache::Admin::Config>. + +Rest of methods is inherited from B<Apache::Admin::Config>. + +=head1 EXAMPLE + +Print on standard output only statements from the configuration file + + use Apache::Admin::Config::Expand; + $conf = new Apache::Admin::Config::Expand '/etc/http/httpd.conf', + '-compact' + or die $Apache::Admin::Config::ERROR; + print $conf->dump_raw; + +=cut + + + @@ -0,0 +1,91 @@ +use lib qw(t lib); +use strict; +use Test; +plan test => 2; + +use Apache::Admin::Config::Expand; +use File::Temp qw(tempfile); +use File::Basename; + +my $tmpl = 'httpconf.XXXXXX'; +my ($conffd, $confname) = tempfile($tmpl, UNLINK => 1); +my ($incfd, $incname) = tempfile($tmpl, UNLINK => 1); +my ($inc2fd, $inc2name) = tempfile($tmpl, UNLINK => 1); + +my $server_root = dirname($confname); + +print $conffd <<EOT; +# Test configuration file + +# +# ServerType is either inetd, or standalone. Inetd mode is only supported on +# Unix platforms. +# +ServerType standalone + +# +# ServerRoot: The top of the directory tree under which the server's +# configuration, error, and log files are kept. +# +ServerRoot "$server_root" + +Include "$incname" + +PidFile logs/httpd.pid +EOT +; +close $conffd; + +print $incfd <<EOT; +# First-level include file + +# +# Timeout: The number of seconds before receives and sends time out. +# +Timeout 300 + +Include "$inc2name" + +# +# KeepAlive: Whether or not to allow persistent connections (more than +# one request per connection). Set to "Off" to deactivate. +# +KeepAlive On +EOT +; +close $incfd; + +print $inc2fd <<EOT; +# Second-level include file + +# +# MaxKeepAliveRequests: The maximum number of requests to allow +# during a persistent connection. Set to 0 to allow an unlimited amount. +# We recommend you leave this number high, for maximum performance. +# +MaxKeepAliveRequests 100 + +# +# KeepAliveTimeout: Number of seconds to wait for the next request from the +# same client on the same connection. +# +KeepAliveTimeout 15 +EOT +; +close $inc2fd; + +my $text = <<EOT +ServerType standalone +ServerRoot "$server_root" +Timeout 300 +MaxKeepAliveRequests 100 +KeepAliveTimeout 15 +KeepAlive On +PidFile logs/httpd.pid +EOT +; + +my $conf = new Apache::Admin::Config::Expand($confname, '-compact'); +ok(defined $conf); +ok($conf->dump_raw, $text); + |