| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Apache::ProxyStuff; |
|
2
|
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
1179
|
use strict; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
50
|
|
|
4
|
1
|
|
|
1
|
|
5
|
use vars qw(@ISA $VERSION); |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
112
|
|
|
5
|
1
|
|
|
1
|
|
5875
|
use Apache::Constants qw(:common); |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
use Apache::Log; |
|
7
|
|
|
|
|
|
|
use Apache::Table; |
|
8
|
|
|
|
|
|
|
use HTML::TokeParser; |
|
9
|
|
|
|
|
|
|
use LWP::UserAgent; |
|
10
|
|
|
|
|
|
|
use Data::Dumper; |
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
@ISA = qw(LWP::UserAgent); |
|
13
|
|
|
|
|
|
|
$VERSION = '0.10'; |
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
my $UA = __PACKAGE__->new; |
|
16
|
|
|
|
|
|
|
$UA->agent(join "/", __PACKAGE__, $VERSION); |
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
# Override Methods |
|
19
|
|
|
|
|
|
|
sub redirect_ok {return 0} |
|
20
|
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
# Helper Subs |
|
22
|
|
|
|
|
|
|
sub set_headers { |
|
23
|
|
|
|
|
|
|
my ($req, %headers) = @_; |
|
24
|
|
|
|
|
|
|
foreach my $header (keys %headers) { |
|
25
|
|
|
|
|
|
|
next if $header eq 'Connection'; # Don't want to pass Keep-Alive |
|
26
|
|
|
|
|
|
|
$req->push_header($header => $headers{$header}); |
|
27
|
|
|
|
|
|
|
} # End foreach |
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
# Set REMOTE_ADDR, REMOTE_HOST, REMOTE_USER |
|
30
|
|
|
|
|
|
|
$req->push_header('REMOTE_ADDR' => $ENV{'REMOTE_ADDR'}); |
|
31
|
|
|
|
|
|
|
$req->push_header('REMOTE_HOST' => $ENV{'REMOTE_HOST'} || $ENV{'REMOTE_ADDR'}); |
|
32
|
|
|
|
|
|
|
$req->push_header('REMOTE_USER' => $ENV{'REMOTE_USER'}); |
|
33
|
|
|
|
|
|
|
return $req; |
|
34
|
|
|
|
|
|
|
} # End set_headers |
|
35
|
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub open_head { |
|
37
|
|
|
|
|
|
|
my ($token, $r, $meta_description, $meta_content) = @_; |
|
38
|
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
# Print tag |
|
40
|
|
|
|
|
|
|
print qq($token->[-1]\n); |
|
41
|
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
# Print meta tags |
|
43
|
|
|
|
|
|
|
print $meta_description; |
|
44
|
|
|
|
|
|
|
print $meta_content; |
|
45
|
|
|
|
|
|
|
} # End open_head() |
|
46
|
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub open_body { |
|
48
|
|
|
|
|
|
|
my ($token, $r, $header, $body_attributes) = @_; |
|
49
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
# Print body tag |
|
51
|
|
|
|
|
|
|
print q(
|
|
52
|
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
# Replace existing body attributes with new ones if necessary |
|
54
|
|
|
|
|
|
|
if ($body_attributes) { |
|
55
|
|
|
|
|
|
|
foreach my $pair (split /\s+/, $body_attributes) { |
|
56
|
|
|
|
|
|
|
my ($attr, $value) = split /=/, $pair; |
|
57
|
|
|
|
|
|
|
$token->[2]->{lc $attr} = $value; # Keys are lowercase |
|
58
|
|
|
|
|
|
|
} # End foreach |
|
59
|
|
|
|
|
|
|
} # End if |
|
60
|
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
# Print attributes |
|
62
|
|
|
|
|
|
|
my $atts = join(' ', map({"$_=$token->[2]->{$_}"} keys %{$token->[2]})); |
|
63
|
|
|
|
|
|
|
print qq( $atts) if $atts; |
|
64
|
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
# Close body tag |
|
66
|
|
|
|
|
|
|
print qq(>); |
|
67
|
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
# Send the header |
|
69
|
|
|
|
|
|
|
print $header; |
|
70
|
|
|
|
|
|
|
} # End open_body() |
|
71
|
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub close_body { |
|
73
|
|
|
|
|
|
|
my ($token, $r, $footer) = @_; |
|
74
|
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
# Send the footer |
|
76
|
|
|
|
|
|
|
print $footer; |
|
77
|
|
|
|
|
|
|
print $token->[-1]; |
|
78
|
|
|
|
|
|
|
} # End close_body() |
|
79
|
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
sub a_href { |
|
81
|
|
|
|
|
|
|
my ($token, $r, $add_host2href) = @_; |
|
82
|
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
# Open tag |
|
84
|
|
|
|
|
|
|
print qq(
|
|
85
|
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
# Modify href |
|
87
|
|
|
|
|
|
|
if ($token->[2]->{'href'} =~ /^\//) { # Modify if absolute URI |
|
88
|
|
|
|
|
|
|
$token->[2]->{'href'} = qq(/$add_host2href) . $token->[2]->{'href'}; |
|
89
|
|
|
|
|
|
|
} # End unless |
|
90
|
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
# Print attributes |
|
92
|
|
|
|
|
|
|
my $atts = join(' ', map({"$_=$token->[2]->{$_}"} keys %{$token->[2]})); |
|
93
|
|
|
|
|
|
|
print qq( $atts) if $atts; |
|
94
|
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
# Close tag |
|
96
|
|
|
|
|
|
|
print qq(>); |
|
97
|
|
|
|
|
|
|
} # End a_href() |
|
98
|
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
sub img_src { |
|
100
|
|
|
|
|
|
|
my ($token, $r, $add_host2img_src) = @_; |
|
101
|
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
# Open tag |
|
103
|
|
|
|
|
|
|
print qq(
|
|
104
|
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
# Modify src |
|
106
|
|
|
|
|
|
|
if ($token->[2]->{'src'} =~ /^\//) { # Modify is absolute URI |
|
107
|
|
|
|
|
|
|
$token->[2]->{'src'} = qq(/$add_host2img_src) . $token->[2]->{'src'}; |
|
108
|
|
|
|
|
|
|
} # End unless |
|
109
|
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
# Print attributes |
|
111
|
|
|
|
|
|
|
my $atts = join(' ', map({"$_=$token->[2]->{$_}"} keys %{$token->[2]})); |
|
112
|
|
|
|
|
|
|
print qq( $atts) if $atts; |
|
113
|
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
# Close tag |
|
115
|
|
|
|
|
|
|
print qq(>); |
|
116
|
|
|
|
|
|
|
} # End a_href() |
|
117
|
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
sub form_action { |
|
119
|
|
|
|
|
|
|
my ($token, $r, $add_host2form_action) = @_; |
|
120
|
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
# Open tag |
|
122
|
|
|
|
|
|
|
print qq( |
|
123
|
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
# Modify action |
|
125
|
|
|
|
|
|
|
if ($token->[2]->{'action'} =~ /^\//) { # Modify is absolute URI |
|
126
|
|
|
|
|
|
|
$token->[2]->{'action'} = qq(/$add_host2form_action) . $token->[2]->{'action'}; |
|
127
|
|
|
|
|
|
|
} # End unless |
|
128
|
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
# Print attributes |
|
130
|
|
|
|
|
|
|
my $atts = join(' ', map({"$_=$token->[2]->{$_}"} keys %{$token->[2]})); |
|
131
|
|
|
|
|
|
|
print qq( $atts) if $atts; |
|
132
|
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
# Close tag |
|
134
|
|
|
|
|
|
|
print qq(>); |
|
135
|
|
|
|
|
|
|
} # End a_href() |
|
136
|
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
sub process_text { |
|
138
|
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
my ($content, $r, $header, $footer, $meta_description, $meta_content, $body_attributes, |
|
140
|
|
|
|
|
|
|
$add_host2href, $add_host2img_src, $add_host2form_action) = @_; |
|
141
|
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
# Parse the document |
|
143
|
|
|
|
|
|
|
my $parser = new HTML::TokeParser($content); |
|
144
|
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
# Pre-process the beginning of document so we can appropriately handled bad documents |
|
146
|
|
|
|
|
|
|
# that are missing the tag |
|
147
|
|
|
|
|
|
|
my ($found_html, @tokens_up_to_html, $found_head, @tokens_up_to_head, $found_body, @tokens, |
|
148
|
|
|
|
|
|
|
$found_close_body, @tokens_before_close_html, $found_close_html); |
|
149
|
|
|
|
|
|
|
while (my $token = $parser->get_token) { |
|
150
|
|
|
|
|
|
|
# If it's save the previous tokens and this one in a seperate array |
|
151
|
|
|
|
|
|
|
if ($token->[0] eq 'S' and $token->[1] eq 'html') { |
|
152
|
|
|
|
|
|
|
$found_html++; |
|
153
|
|
|
|
|
|
|
push @tokens_up_to_html, @tokens, $token; |
|
154
|
|
|
|
|
|
|
$r->log->debug("HTML Tokens: ", Dumper(@tokens_up_to_html), qq(\n\n)); |
|
155
|
|
|
|
|
|
|
@tokens = (); |
|
156
|
|
|
|
|
|
|
} # End if |
|
157
|
|
|
|
|
|
|
# If it's save the previous tokens and this one in a seperate array |
|
158
|
|
|
|
|
|
|
elsif ($token->[0] eq 'E' and $token->[1] eq 'head') { |
|
159
|
|
|
|
|
|
|
$found_head++; |
|
160
|
|
|
|
|
|
|
push @tokens_up_to_head, @tokens, $token; |
|
161
|
|
|
|
|
|
|
$r->log->debug(" Tokens: ", Dumper(@tokens_up_to_head), qq(\n\n)); |
|
162
|
|
|
|
|
|
|
@tokens = (); |
|
163
|
|
|
|
|
|
|
} # End elsif |
|
164
|
|
|
|
|
|
|
# If it's add this one to the stack and set a flag |
|
165
|
|
|
|
|
|
|
elsif ($token->[0] eq 'S' and $token->[1] eq 'body') { |
|
166
|
|
|
|
|
|
|
$found_body++; |
|
167
|
|
|
|
|
|
|
push @tokens, $token; |
|
168
|
|
|
|
|
|
|
} # End elsif |
|
169
|
|
|
|
|
|
|
# If it's add this one to the stack and set a flag |
|
170
|
|
|
|
|
|
|
elsif ($token->[0] eq 'E' and $token->[1] eq 'body') { |
|
171
|
|
|
|
|
|
|
$found_close_body++; |
|
172
|
|
|
|
|
|
|
push @tokens, $token; |
|
173
|
|
|
|
|
|
|
} # End elsif |
|
174
|
|
|
|
|
|
|
# If it's save the previous tokens in a seperate array |
|
175
|
|
|
|
|
|
|
elsif ($token->[0] eq 'E' and $token->[1] eq 'html') { |
|
176
|
|
|
|
|
|
|
$found_close_html++; |
|
177
|
|
|
|
|
|
|
push @tokens_before_close_html, @tokens; |
|
178
|
|
|
|
|
|
|
@tokens = $token; |
|
179
|
|
|
|
|
|
|
} # End elsif |
|
180
|
|
|
|
|
|
|
# Otherwise just save up the tokens |
|
181
|
|
|
|
|
|
|
else {push @tokens, $token} |
|
182
|
|
|
|
|
|
|
} # End while |
|
183
|
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
# Build our body tags in case we need them |
|
185
|
|
|
|
|
|
|
my $body_tag = ['S', 'body', {}, [], '']; |
|
186
|
|
|
|
|
|
|
my $close_body_tag = ['E', 'body', {}, [], '']; |
|
187
|
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
# Rebuild the master array of tokens |
|
189
|
|
|
|
|
|
|
# If we found just make one big of array of the tokens we saved |
|
190
|
|
|
|
|
|
|
my @all_tokens; |
|
191
|
|
|
|
|
|
|
if ($found_body) {@all_tokens = (@tokens_up_to_html, @tokens_up_to_head)} |
|
192
|
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
# If we found but no add after the |
|
194
|
|
|
|
|
|
|
elsif ($found_head) {@all_tokens = (@tokens_up_to_html, @tokens_up_to_head, $body_tag)} |
|
195
|
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
# If we found but no and no add after |
|
197
|
|
|
|
|
|
|
elsif ($found_html) {@all_tokens = (@tokens_up_to_html, $body_tag)} |
|
198
|
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
# We didn't find , and so add to the beginning |
|
200
|
|
|
|
|
|
|
else {@all_tokens = ($body_tag)} |
|
201
|
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
# If we found just add the rest onto the end |
|
203
|
|
|
|
|
|
|
if ($found_close_body) {push @all_tokens, @tokens_before_close_html, @tokens} |
|
204
|
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
# If we found but no insert after |
|
206
|
|
|
|
|
|
|
elsif ($found_close_html) {push @all_tokens, @tokens_before_close_html, $close_body_tag, @tokens} |
|
207
|
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
# We didn't find or |