NAME

Apache::Config::Preproc - Preprocess Apache configuration files

SYNOPSIS

    use Apache::Config::Preproc;
    
    $x = new Apache::Config::Preproc '/path/to/httpd.conf',
             -expand => [ qw(include compact macro ifmodule ifdefine) ] 
             or die $Apache::Admin::Config::ERROR;

DESCRIPTION

Apache::Config::Preproc reads and parses Apache configuration file, expanding the syntactic constructs selected by the -expand option. In the simplest case, the argument to that option is a reference to the list of names. Each name in the list identifies a module responsible for processing specific Apache configuration keywords. For convenience, most modules are named after the keyword they process, so that, e.g. include is responsible for inclusion of the files listed with Include and IncludeOptional statements. The list of built-in module names follows:

compact

Removes empty lines and comments.

include

Expands Include and IncludeOptional statements by replacing them with the content of the corresponding files.

ifmodule

Expands the <IfModule> statements.

ifdefine

Expands the <IfDefine> statements.

macro

Expands the <Macro> statements.

See the section MODULES for a detailed description of these modules.

More expansions can be easily implemented by supplying a corresponding expansion module (see the section MODULE INTERNALS below).

If the -expand argument is not supplied, the following default is used:

    [ 'include' ]
    

The rest of methods is inherited from Apache::Admin::Config.

CONSTRUCTOR

new

    $obj = new Apache::Config::Preproc $file,
          [-expand => $modlist],
          [-indent => $integer], ['-create'], ['-no-comment-grouping'],
          ['-no-blank-grouping']

Reads the Apache configuration file from $file and preprocesses it. The $file argument can be either the file name or file handle.

The keyword arguments are:

-expand => $arrayref

Define what expansions are to be performed. $arrayref is a reference to array of module names with optional arguments. To supply arguments, use either a list reference where the first element is the module name and rest of elements are arguments, or a hash reference with the name of the module as key and a reference to the list of arguments as its value. Consider, for example:

    -expand => [ 'include', { ifmodule => { probe => 1 } } ]

    -expand => [ 'include', [ 'ifmodule', { probe => 1 } ] ]

Both constructs load the include module with no specific arguments, and the ifmodule module with the arguments probe => 1.

See the MODULES section for a discussion of built-in modules and allowed arguments.

A missing -expand argument is equivalent to

    -expand => [ 'include' ]
    

Rest of arguments is the same as for the Apache::Admin::Config constructor:

-indent => $n

Enables reindentation of the configuration content. The $n argument is the indenting amount per level of nesting. Negative value means indent with tab characters.

-create

If present $file is a pathname of unexisting file, don't return an error.

-no-comment-grouping

Disables grouping of successive comments into one comment item. Useless if the compact expansion is enabled.

-no-blank-grouping

Disables grouping of successive empty lines into one blank item. Useless if the compact expansion is enabled.

METHODS

All methods are inherited from Apache::Admin::Config.

MODULES

The preprocessing phases to be performed on the parsed configuration text are defined by the -expand argument. Internally, each name in its argument list causes loading of a Perl module responsible for this particular phase. Arguments to the constructor can be supplied using any of the following constructs:

       { NAME => [ ARG, ...] }

or

       [ NAME, ARG, ... ]

    

This section describes the built-in modules and their arguments.

compact

The compact module eliminates empty and comment lines. The constructor takes no arguments.

include

Processes Include and IncludeOptional statements and replaces them with the contents of the files supplied in their argument. If the latter is not an absolute file name, it is searched in the server root directory.

The following keyword arguments can be used to set the default server root directory:

server_root => DIR

Sets default server root value to DIR.

probe => LISTREF | 1

Determines the default server root value by analyzing the output of httpd -V. If LISTREF is given, it contains alternative pathnames of the Apache httpd binary. Otherwise,

    probe => 1

is a shorthand for

    probe => [qw(/usr/sbin/httpd /usr/sbin/apache2)]
        

When the ServerRoot statement is seen, its value overwrites any previously set server root directory.

ifmodule

Processes IfModule statements. If the statement's argument evaluates to true, it is replaced by the statements inside it. Otherwise, it is removed. Nested statements are allowed. The LoadModule statements are examined in order to evaluate the argument.

The constructor understands the following arguments:

preloaded => LISTREF

Supplies a list of preloaded module names. You can use this argument to pass a list of modules linked statically in your version of httpd.

probe => LISTREF | 1

Provides an alternative way of handling statically linked Apache modules. If LISTREF is given, each its element is treated as the pathname of the Apache httpd binary. The first of them that is found is run with the -l option to list the statically linked modules, and its output is parsed.

The option

    probe => 1

is a shorthand for

    probe => [qw(/usr/sbin/httpd /usr/sbin/apache2)]

ifdefine

Eliminates the Define and UnDefine statements and expands the <IfDefine> statements in the Apache configuration parse tree. Optional arguments to the constructor are treated as the names of symbols to define (similar to the httpd -D options). Example:

    -expand => [ { ifdefine => [ qw(SSL FOREGROUND) ] } ]
    

macro

Processes Macro and Use statements (see mod_macro). Macro statements are removed. Each Use statement is replaced by the expansion of the macro named in its argument.

The constructor accepts the following arguments:

keep => $listref

List of macro names to exclude from expanding. Each <Macro> and Use statement with a name from $listref as its first argument will be retained in the parse tree.

As a syntactic sugar, $listref can also be a scalar value. This is convenient when a single macro name is to be retained.

MODULE INTERNALS

Each keyword phase listed in the -expand array causes loading of the module Apache::Config::Preproc::phase. This module must provide the following methods:

new($conf, ...)

Class constructor. The $conf argument is the configuration file object (Apache::Config::Preproc). Rest are constructor arguments provided with the module name in the -expand list.

expand

This method must perform actual expansion of a subtree of the parse tree. It is called as:

    $phase->expand($subtree, $repl)

Its arguments are:

$subtree

The subtree to be processed.

$repl

A reference to array of items (of the same type as $subtree, i.e. Apache::Admin::Config or Apache::Admin::Config::Tree) where expansion is to be stored.

The function returns true if it did process the $subtree. In this case, the subtree will be removed from the parse tree and the items from @$repl will be inserted in its place. Thus, to simply remove the $subtree the expand method must return true and not touch $repl. For example, the following is the expand method definition from the Apache::Config::Preproc::compact module:

    sub expand {
        my ($self, $d, $repl) = @_;
        return $d->type eq 'blank' || $d->type eq 'comment';
    }

Notice, that expand does not need to recurse into section objects. This is taken care of by the caller.

EXAMPLE

    my $obj = new Apache::Config::Preproc('/etc/httpd/httpd.conf',
                   -expand => [qw(compact include ifmodule macro)],
                   -indent => 4) or die $Apache::Admin::Config::ERROR;
    print $obj->dump_raw

This snippet loads the Apache configuration from file /etc/httpd/httpd.conf, performs all the built-in expansions, and prints the result on standard output, using 4 character indent for each additional level of nesting.

SEE ALSO

Apache::Admin::Config(3).