File: startup.pm

package info (click to toggle)
libapache2-mod-perl2 2.0.13-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 12,016 kB
  • sloc: perl: 97,771; ansic: 14,493; makefile: 51; sh: 18
file content (135 lines) | stat: -rw-r--r-- 3,782 bytes parent folder | download | duplicates (7)
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
# please insert nothing before this line: -*- mode: cperl; cperl-indent-level: 4; cperl-continued-statement-offset: 4; indent-tabs-mode: nil -*-
package TestHooks::startup;

# test PerlPostConfigHandler and PerlOpenLogsHandler phases
# also test that we can run things on vhost entries from these phases

use strict;
use warnings FATAL => 'all';

use Apache::TestUtil;
use Apache::Test;
use Apache::TestTrace;

use APR::Table;
use Apache2::ServerRec ();
use Apache2::ServerUtil ();
use Apache2::RequestRec ();
use Apache2::RequestIO ();

use File::Spec::Functions qw(catfile catdir);
use File::Path qw(mkpath);

use Apache2::Const -compile => 'OK';

my $dir = catdir Apache::Test::vars("documentroot"), 'hooks', 'startup';

sub open_logs {
    my ($conf_pool, $log_pool, $temp_pool, $s) = @_;

    # main server
    run("open_logs", $s);

    for (my $vhost_s = $s->next; $vhost_s; $vhost_s = $vhost_s->next) {
        my $port = $vhost_s->port;
        my $val = $vhost_s->dir_config->{PostConfig};
        # we have one vhost that we want to run open_logs for
        next unless $val && $val eq 'VHost';
        run("open_logs", $vhost_s);
    }

    Apache2::Const::OK;
}

sub post_config {
    my ($conf_pool, $log_pool, $temp_pool, $s) = @_;

    # main server
    run("post_config", $s);

    for (my $vhost_s = $s->next; $vhost_s; $vhost_s = $vhost_s->next) {
        my $port = $vhost_s->port;
        my $val = $vhost_s->dir_config->{PostConfig};
        # we have one vhost that we want to run post_config for
        next unless $val && $val eq 'VHost';
        run("post_config", $vhost_s);
    }

    Apache2::Const::OK;
}

sub run {
    my ($phase, $s) = @_;

    my $val = $s->dir_config->{PostConfig} or die "Can't read PostConfig var";

    # make sure that these are set at the earliest possible time
    die '$ENV{MOD_PERL} not set!' unless $ENV{MOD_PERL};
    die '$ENV{MOD_PERL_API_VERSION} not set!'
        unless $ENV{MOD_PERL_API_VERSION} == 2;

    my $port = $s->port;
    my $file = catfile $dir, "$phase-$port";

    mkpath $dir, 0, 0755;
    open my $fh, ">$file" or die "can't open $file: $!";
    print $fh $val;
    close $fh;

    debug "Phase $phase is completed for server at port $port";
}

sub handler {
    my $r = shift;

    $r->content_type('text/plain');

    my $s = $r->server;
    my $expected = $s->dir_config->{PostConfig}
        or die "Can't read PostConfig var";
    my $port = $s->port;

    for my $phase (qw(open_logs post_config)) {
        my $file = catfile $dir, "$phase-$port";
        open my $fh, "$file" or die "can't open $file: $!";
        my $received = <$fh> || '';
        close $fh;

        # can't cleanup the file here, because t/SMOKE may run this
        # test more than once, so we cleanup on startup in modperl_extra.pl
        # unlink $file;

        if ($expected eq $received) {
            $r->print("$phase ok\n");
        } else {
            warn "phase: $phase\n";
            warn "port: $port\n";
            warn "expected: $expected\n";
            warn "received: $received\n";
        }
    }
    Apache2::Const::OK;
}

1;
__DATA__
<NoAutoConfig>
<VirtualHost TestHooks::startup>
    PerlSetVar PostConfig VHost
    PerlModule TestHooks::startup
    PerlPostConfigHandler TestHooks::startup::post_config
    PerlOpenLogsHandler   TestHooks::startup::open_logs
    <Location /TestHooks__startup>
        SetHandler modperl
        PerlResponseHandler TestHooks::startup
    </Location>
</VirtualHost>
PerlSetVar PostConfig Main
PerlModule TestHooks::startup
PerlPostConfigHandler TestHooks::startup::post_config
PerlOpenLogsHandler   TestHooks::startup::open_logs
<Location /TestHooks__startup>
    SetHandler modperl
    PerlResponseHandler TestHooks::startup
</Location>
</NoAutoConfig>