blib/lib/Apache/ProxyRewrite.pm untested

filter: Statementstmt 0.0% 0 / 273 Branchbran 0.0% 0 / 86 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 407 CC 44 SCAR 46.9
CC
44
Cov
0%
dealwithtag
240.0
fetch
110.0
respond
110.0
CRAP 109.3
1 # $Id: ProxyRewrite.pm,v 1.14 2002/01/16 15:49:48 cgilmore Exp $
2 #
3 # Author : Christian Gilmore
4 # Created On : Nov 10 12:04:00 CDT 2000
5 # Status : Functional
6 #
7 # PURPOSE
8 # Proxy requests and rewrite embedded URLs according to configuration
9 #
10 ###############################################################################
11 #
12 # IBM Public License Version 1.0
13 #
14 # THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS IBM
15 # PUBLIC LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR
16 # DISTRIBUTION OF THE PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE OF
17 # THIS AGREEMENT.
18 #
19 # 1. DEFINITIONS
20 #
21 # "Contribution" means:
22 #
23 # a) in the case of International Business Machines Corporation
24 # ("IBM"), the Original Program, and
25 #
26 # b) in the case of each Contributor,
27 #
28 # i) changes to the Program, and
29 #
30 # ii) additions to the Program;
31 #
32 # where such changes and/or additions to the Program originate from
33 # and are distributed by that particular Contributor. A Contribution
34 # 'originates' from a Contributor if it was added to the Program by
35 # such Contributor itself or anyone acting on such Contributor's
36 # behalf. Contributions do not include additions to the Program
37 # which: (i) are separate modules of software distributed in
38 # conjunction with the Program under their own license agreement,
39 # and (ii) are not derivative works of the Program.
40 #
41 # "Contributor" means IBM and any other entity that distributes the
42 # Program.
43 #
44 # "Licensed Patents " mean patent claims licensable by a Contributor
45 # which are necessarily infringed by the use or sale of its
46 # Contribution alone or when combined with the Program.
47 #
48 # "Original Program" means the original version of the software
49 # accompanying this Agreement as released by IBM, including source
50 # code, object code and documentation, if any.
51 #
52 # "Program" means the Original Program and Contributions.
53 #
54 # "Recipient" means anyone who receives the Program under this
55 # Agreement, including all Contributors.
56 #
57 # 2. GRANT OF RIGHTS
58 #
59 # a) Subject to the terms of this Agreement, each Contributor hereby
60 # grants Recipient a non-exclusive, worldwide, royalty-free
61 # copyright license to reproduce, prepare derivative works of,
62 # publicly display, publicly perform, distribute and sublicense the
63 # Contribution of such Contributor, if any, and such derivative
64 # works, in source code and object code form.
65 #
66 # b) Subject to the terms of this Agreement, each Contributor hereby
67 # grants Recipient a non-exclusive, worldwide, royalty-free patent
68 # license under Licensed Patents to make, use, sell, offer to sell,
69 # import and otherwise transfer the Contribution of such
70 # Contributor, if any, in source code and object code form. This
71 # patent license shall apply to the combination of the Contribution
72 # and the Program if, at the time the Contribution is added by the
73 # Contributor, such addition of the Contribution causes such
74 # combination to be covered by the Licensed Patents. The patent
75 # license shall not apply to any other combinations which include
76 # the Contribution. No hardware per se is licensed hereunder.
77 #
78 # c) Recipient understands that although each Contributor grants the
79 # licenses to its Contributions set forth herein, no assurances are
80 # provided by any Contributor that the Program does not infringe the
81 # patent or other intellectual property rights of any other entity.
82 # Each Contributor disclaims any liability to Recipient for claims
83 # brought by any other entity based on infringement of intellectual
84 # property rights or otherwise. As a condition to exercising the
85 # rights and licenses granted hereunder, each Recipient hereby
86 # assumes sole responsibility to secure any other intellectual
87 # property rights needed, if any. For example, if a third party
88 # patent license is required to allow Recipient to distribute the
89 # Program, it is Recipient's responsibility to acquire that license
90 # before distributing the Program.
91 #
92 # d) Each Contributor represents that to its knowledge it has
93 # sufficient copyright rights in its Contribution, if any, to grant
94 # the copyright license set forth in this Agreement.
95 #
96 # 3. REQUIREMENTS
97 #
98 # A Contributor may choose to distribute the Program in object code
99 # form under its own license agreement, provided that:
100 #
101 # a) it complies with the terms and conditions of this Agreement;
102 #
103 # and
104 #
105 # b) its license agreement:
106 #
107 # i) effectively disclaims on behalf of all Contributors all
108 # warranties and conditions, express and implied, including
109 # warranties or conditions of title and non-infringement, and
110 # implied warranties or conditions of merchantability and fitness
111 # for a particular purpose;
112 #
113 # ii) effectively excludes on behalf of all Contributors all
114 # liability for damages, including direct, indirect, special,
115 # incidental and consequential damages, such as lost profits;
116 # iii) states that any provisions which differ from this Agreement
117 # are offered by that Contributor alone and not by any other party;
118 # and
119 #
120 # iv) states that source code for the Program is available from such
121 # Contributor, and informs licensees how to obtain it in a
122 # reasonable manner on or through a medium customarily used for
123 # software exchange.
124 #
125 # When the Program is made available in source code form:
126 #
127 # a) it must be made available under this Agreement; and
128 #
129 # b) a copy of this Agreement must be included with each copy of the
130 # Program.
131 #
132 # Each Contributor must include the following in a conspicuous
133 # location in the Program:
134 #
135 # Copyright © {date here}, International Business Machines
136 # Corporation and others. All Rights Reserved.
137 #
138 # In addition, each Contributor must identify itself as the originator
139 # of its Contribution, if any, in a manner that reasonably allows
140 # subsequent Recipients to identify the originator of the
141 # Contribution.
142 #
143 # 4. COMMERCIAL DISTRIBUTION
144 #
145 # Commercial distributors of software may accept certain
146 # responsibilities with respect to end users, business partners and
147 # the like. While this license is intended to facilitate the
148 # commercial use of the Program, the Contributor who includes the
149 # Program in a commercial product offering should do so in a manner
150 # which does not create potential liability for other Contributors.
151 # Therefore, if a Contributor includes the Program in a commercial
152 # product offering, such Contributor ("Commercial Contributor") hereby
153 # agrees to defend and indemnify every other Contributor ("Indemnified
154 # Contributor") against any losses, damages and costs (collectively
155 # "Losses") arising from claims, lawsuits and other legal actions
156 # brought by a third party against the Indemnified Contributor to the
157 # extent caused by the acts or omissions of such Commercial
158 # Contributor in connection with its distribution of the Program in a
159 # commercial product offering. The obligations in this section do not
160 # apply to any claims or Losses relating to any actual or alleged
161 # intellectual property infringement. In order to qualify, an
162 # Indemnified Contributor must: a) promptly notify the Commercial
163 # Contributor in writing of such claim, and b) allow the Commercial
164 # Contributor to control, and cooperate with the Commercial
165 # Contributor in, the defense and any related settlement negotiations.
166 # The Indemnified Contributor may participate in any such claim at its
167 # own expense.
168 #
169 # For example, a Contributor might include the Program in a commercial
170 # product offering, Product X. That Contributor is then a Commercial
171 # Contributor. If that Commercial Contributor then makes performance
172 # claims, or offers warranties related to Product X, those performance
173 # claims and warranties are such Commercial Contributor's
174 # responsibility alone. Under this section, the Commercial Contributor
175 # would have to defend claims against the other Contributors related
176 # to those performance claims and warranties, and if a court requires
177 # any other Contributor to pay any damages as a result, the Commercial
178 # Contributor must pay those damages.
179 #
180 # 5. NO WARRANTY
181 #
182 # EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS
183 # PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
184 # ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION,
185 # ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
186 # MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient
187 # is solely responsible for determining the appropriateness of using
188 # and distributing the Program and assumes all risks associated with
189 # its exercise of rights under this Agreement, including but not
190 # limited to the risks and costs of program errors, compliance with
191 # applicable laws, damage to or loss of data, programs or equipment,
192 # and unavailability or interruption of operations.
193 #
194 # 6. DISCLAIMER OF LIABILITY
195 #
196 # EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT
197 # NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT,
198 # INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
199 # (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND ON
200 # ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
201 # TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
202 # THE USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS
203 # GRANTED HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
204 # DAMAGES.
205 #
206 # 7. GENERAL
207 #
208 # If any provision of this Agreement is invalid or unenforceable under
209 # applicable law, it shall not affect the validity or enforceability
210 # of the remainder of the terms of this Agreement, and without further
211 # action by the parties hereto, such provision shall be reformed to
212 # the minimum extent necessary to make such provision valid and
213 # enforceable.
214 #
215 # If Recipient institutes patent litigation against a Contributor with
216 # respect to a patent applicable to software (including a cross-claim
217 # or counterclaim in a lawsuit), then any patent licenses granted by
218 # that Contributor to such Recipient under this Agreement shall
219 # terminate as of the date such litigation is filed. In addition, If
220 # Recipient institutes patent litigation against any entity (including
221 # a cross-claim or counterclaim in a lawsuit) alleging that the
222 # Program itself (excluding combinations of the Program with other
223 # software or hardware) infringes such Recipient's patent(s), then
224 # such Recipient's rights granted under Section 2(b) shall terminate
225 # as of the date such litigation is filed.
226 #
227 # All Recipient's rights under this Agreement shall terminate if it
228 # fails to comply with any of the material terms or conditions of this
229 # Agreement and does not cure such failure in a reasonable period of
230 # time after becoming aware of such noncompliance. If all Recipient's
231 # rights under this Agreement terminate, Recipient agrees to cease use
232 # and distribution of the Program as soon as reasonably practicable.
233 # However, Recipient's obligations under this Agreement and any
234 # licenses granted by Recipient relating to the Program shall continue
235 # and survive.
236 #
237 # IBM may publish new versions (including revisions) of this Agreement
238 # from time to time. Each new version of the Agreement will be given a
239 # distinguishing version number. The Program (including Contributions)
240 # may always be distributed subject to the version of the Agreement
241 # under which it was received. In addition, after a new version of the
242 # Agreement is published, Contributor may elect to distribute the
243 # Program (including its Contributions) under the new version. No one
244 # other than IBM has the right to modify this Agreement. Except as
245 # expressly stated in Sections 2(a) and 2(b) above, Recipient receives
246 # no rights or licenses to the intellectual property of any
247 # Contributor under this Agreement, whether expressly, by implication,
248 # estoppel or otherwise. All rights in the Program not expressly
249 # granted under this Agreement are reserved.
250 #
251 # This Agreement is governed by the laws of the State of New York and
252 # the intellectual property laws of the United States of America. No
253 # party to this Agreement will bring a legal action under this
254 # Agreement more than one year after the cause of action arose. Each
255 # party waives its rights to a jury trial in any resulting litigation.
256 #
257 ###############################################################################
258  
259  
260 # Package name
261 package Apache::ProxyRewrite;
262  
263  
264 # Required libraries
265 use strict;
266 use Apache;
267 use Apache::Constants qw(OK AUTH_REQUIRED DECLINED DONE);
268 use Apache::Log;
269 use Apache::URI;
270 use LWP::UserAgent;
271 use Socket;
272 use URI::Escape qw(uri_unescape);
273  
274  
275 # Global variables
276 $Apache::ProxyRewrite::VERSION = '0.17';
277 $Apache::ProxyRewrite::PRODUCT = 'ProxyRewrite/' .
278 $Apache::ProxyRewrite::VERSION;
279 my %LINK_ELEMENTS =
280 ( # These represent all the possible valid tags that have links in them
281 'a' => 'href',
282 'applet' => {
283 'archive' => 1,
284 'code' => 1,
285 'codebase' => 1,
286 },
287 'area' => 'href',
288 'base' => 'href',
289 'body' => 'background',
290 'embed' => 'src',
291 'form' => 'action',
292 'frame' => 'src',
293 'img' => {
294 'src' => 1,
295 'lowsrc' => 1,
296 'usemap' => 1,
297 },
298 'input' => 'src',
299 'isindex' => 'action',
300 'link' => {
301 'href' => 1,
302 'src' => 1,
303 },
304 'meta' => {
305 'content' => 1,
306 'http-equiv' => 1,
307 },
308 'object' => {
309 'classid' => 1,
310 'codebase' => 1,
311 'data' => 1,
312 'name' => 1,
313 'usemap' => 1,
314 },
315 'script' => 'src',
316 'td' => 'background',
317 'th' => 'background',
318 'tr' => 'background',
319 );
320  
321  
322 ###############################################################################
323 ###############################################################################
324 # handler: hook into Apache/mod_perl API
325 ###############################################################################
326 ###############################################################################
327 sub handler {
328 my $r = shift;
329 my %mappings = ();
330 my ($auth_info, $auth_redirect, $remote_location) = undef;
331  
332 %mappings = split(/\s*(?:=>|,)\s*/, $r->dir_config('ProxyRewrite'));
333 $auth_info = $r->dir_config('ProxyAuthInfo');
334 $auth_redirect = $r->dir_config('ProxyAuthRedirect') || 'Off';
335 if ($r->dir_config('ProxyTo')) {
336 $remote_location = $r->dir_config('ProxyTo');
337 } else {
338 $r->log->error("ProxyRewrite::handler: ProxyTo directive must be defined");
339 return DECLINED;
340 }
341  
342 # Automatically add a mapping for the remote relative URI and the
343 # current location. Also capture remote site information.
344 $remote_location =~ m!^([^:]+://[^/]+)(/?.*)!;
345 my $remote_site = $1;
346 if ($2) {
347 $mappings{$2} = $r->location;
348 } elsif ($r->location eq '/') {
349 $mappings{'/'} = $r->location;
350 } else {
351 $mappings{'/'} = $r->location . '/';
352 }
353  
354 $r->log->debug("handler: Remote Site - $remote_site");
355 $r->log->debug("handler: Remote Location - $remote_location");
356 $r->log->debug("handler: Auth Info - $auth_info");
357 foreach my $map (keys(%mappings)) {
358 # Standardize host on lowercase
359 if ($map =~ m!([^:]+://)([^/]+)(.*)!) {
360 my $mapping = $mappings{$map};
361 delete $mappings{$map};
362 my ($protocol, $url_host, $uri) = ($1, $2, $3);
363 $url_host =~ tr/A-Z/a-z/;
364 $map = $protocol . $url_host . $uri;
365 $mappings{$map} = $mapping;
366 }
367 $r->log->debug("handler: Mapping $map to $mappings{$map}");
368 }
369  
370 # fetch URL
371 $r->log->info("ProxyRewrite: Preparing to fetch ", $r->uri,
372 " at time ", time);
373 my $response = &fetch($r, $remote_location, $remote_site,
374 $auth_info, \%mappings);
375  
376 # rewrite response URIs as needed
377 $r->log->info("ProxyRewrite: Preparing to rewrite URIs for ", $r->uri,
378 " at time ", time);
379 if ($response->header('Content-type') =~ m!^text/html!) {
380 &parse($r, $remote_site, $response, \%mappings);
381 }
382  
383 # respond to client
384 $r->log->info("ProxyRewrite: Preparing to respond for ", $r->uri,
385 " at time ", time);
386 &respond($r, $remote_site, $remote_location, $auth_redirect,
387 $response, \%mappings);
388  
389 return OK;
390 }
391  
392 ###############################################################################
393 ###############################################################################
394 # fetch: fetch the remote URL and return a reference to the response object
395 ###############################################################################
396 ###############################################################################
397 sub fetch {
398 my ($r, $remote_location, $remote_site, $auth_info, $mapref) = @_;
399 my $client_agent = '';
400 my $my_uri = '';
401 my ($k, $v);
402 my $base = $r->location();
403 my $args = $r->args();
404 if ($base ne '/') {
405 ($my_uri = $r->uri) =~ s/^$base//;
406 } else {
407 $my_uri = $r->uri;
408 }
409 $my_uri = $remote_location . $my_uri;
410 $my_uri .= '?' . $r->args() if $args;
411  
412 my $request = HTTP::Request->new($r->method, $my_uri);
413  
414 $r->log->info("ProxyRewrite::fetch: Time proxy request method created: ", time);
415 $r->log->debug("fetch: Base URI (aka location section): $base");
416 $r->log->info("ProxyRewrite::fetch: Request for $my_uri with method ", $r->method);
417  
418 my(%headers_in) = $r->headers_in;
419 while(($k,$v) = each %headers_in) {
420 # HACK to force no Keep-Alives on the connection between proxy
421 # and remote server
422 $r->log->debug("fetch: IN $k: $v");
423 if ($k =~ /Connection/) {
424 $v = "Close";
425 } elsif ($k =~ /Host/) {
426 ($v) = ($remote_location =~ m!://([^/]+)!);
427 } elsif ($k =~ /User-Agent/) {
428 $client_agent = $v;
429 }
430 $v = uri_unescape($v);
431 $request->header($k,$v);
432 $r->log->debug("fetch: IN-MOD $k: $v");
433 }
434  
435 # If we have authorization information and it isn't already filled in
436 if ($auth_info && !$request->authorization()) {
437 $request->authorization($auth_info);
438 }
439  
440 if ($r->method eq "POST") {
441 my $content;
442 if ($r->headers_in->{'Content-type'} eq 'application/x-www-form-urlencoded') {
443 $content = $r->content;
444 } else {
445 $r->read($content, $r->headers_in->{'Content-length'});
446 }
447 $request->content($content);
448 $r->log->debug("fetch: Request type: ", $r->method);
449 $r->log->debug("fetch: Request content type: ",
450 $r->headers_in->{'Content-type'});
451 $r->log->debug("fetch: Request content: $content");
452 }
453  
454 $r->log->debug("fetch: Product: $Apache::ProxyRewrite::PRODUCT");
455 my $ua = new LWP::UserAgent;
456 if ($client_agent ne '') {
457 $ua->agent("$client_agent; $Apache::ProxyRewrite::PRODUCT");
458 } else {
459 $ua->agent("$Apache::ProxyRewrite::PRODUCT");
460 }
461 my $res = $ua->simple_request($request);
462 $r->log->info("ProxyRewrite::fetch: Time proxy got document: ", time);
463 $r->log->info("ProxyRewrite::fetch: Original document size: ",
464 length($res->content));
465  
466 return($res);
467 }
468  
469 ###############################################################################
470 ###############################################################################
471 # parse: parse HTML and find all embedded URLs
472 ###############################################################################
473 ###############################################################################
474 sub parse {
475 my ($r, $remote_site, $response, $mapref) = @_;
476 my $buf = $response->content;
477 my ($lessthanpos, $greaterthanpos, $prediff, $diff,
478 $preblock, $tagblock, $lastblock);
479 my $pos = 0;
480 my $newbuf = '';
481 my $iscomment = 0;
482 my $buflen = length($buf);
483  
484 while (($lessthanpos = index($buf, "<", $pos)) > -1) {
485 # Make a special case out of the comment in case there
486 # are nested tags within the comment, such as javascript code
487 # fragments. Not necessarily our problem, but it doesn't hurt much
488 # to deal with it.
489 if (substr($buf, $lessthanpos + 1, 3) eq '!--') {
490 $greaterthanpos = index($buf, "-->", $lessthanpos);
491 $iscomment = 1;
492 } else {
493 $greaterthanpos = index($buf, ">", $lessthanpos);
494 }
495 $prediff = $lessthanpos - $pos;
496 $diff = $greaterthanpos - $lessthanpos - 1;
497 $preblock = substr($buf, $pos, $prediff + 1);
498 $tagblock = substr($buf, $lessthanpos + 1, $diff);
499 if ($iscomment == 0) {
500 $r->log->debug("parse: Dealing with tag block: $tagblock");
501 &dealwithtag($r, $remote_site, \$tagblock, $mapref);
502 $r->log->debug("parse: Edited tag block: $tagblock");
503 } else {
504 $r->log->debug("parse: Skipped comment tag block");
505 $iscomment = 0;
506 }
507 $newbuf .= "$preblock$tagblock";
508 $pos = $greaterthanpos;
509 # If a tag isn't properly closed at the end of a document, we need to
510 # force an end to the loop.
511 last if ($pos == -1);
512 }
513 $lastblock = substr($buf, $pos, $buflen);
514 $newbuf .= "$lastblock";
515  
516 $response->content($newbuf);
517 }
518  
519 ###############################################################################
520 ###############################################################################
521 # dealwithtag: decides if there a URL in a tag and sends it to be rewritten
522 ###############################################################################
523 ###############################################################################
524 sub dealwithtag {
525 my ($r, $remote_site, $tagblock, $mapref) = @_;
526 my @blocks;
527 my ($tag, $lctag, $key, $lckey, $value, $lcvalue, $delay, $tmp, $i);
528 my $done = 0;
529 my $refresh = 0;
530  
531 # Remove spaces around equal signs, eg 'src = bar' becomes 'src=bar'
532 $$tagblock =~ s/\s*(=)\s*/$1/g;
533 # Remove all other forms of whitespace in block
534 $$tagblock =~ s/(\f|\n|\r|\t)+/ /g;
535 # Remove leading spaces in block, eg < img ...> becomes <img ...>
536 $$tagblock =~ s/^\s+//;
537 # Remove leading and trailing whitespace within quotes
538 $$tagblock =~ s/(=[\"\'])\s*/$1/g;
539 $$tagblock =~ s/\s*([\"\'])/$1/g;
540 @blocks = split(/\s+/, $$tagblock);
541 $tag = shift(@blocks);
542 $lctag = lc($tag);
543 if (exists($LINK_ELEMENTS{$lctag})) {
544 $$tagblock = $tag;
545 for ($i = 0; $i < @blocks; $i++) {
546 if ($blocks[$i] =~ /=/) {
547 ($key, $value) = split(/=/, $blocks[$i], 2);
548 $lckey = lc($key);
549 if ($lctag =~ /(applet|img|link|meta|object)/) {
550 if (exists($LINK_ELEMENTS{$lctag}{$lckey})) {
551 $value =~ s/(\"|\')//g;
552 if ($lctag eq 'meta') {
553 $lcvalue = lc($value);
554 if ($lckey eq 'http-equiv') {
555 if ($lcvalue eq 'refresh') {
556 $refresh = 1;
557 }
558 $$tagblock .= " $key=\"$value\"";
559 next;
560 } else {
561 # Must be a content key
562 while (!$done && $i < @blocks) {
563 $value .= " $blocks[++$i]";
564 if (1 == ($value =~ s/\"//g)) {
565 $done = 1;
566 }
567 }
568 $done = 0;
569 if ($refresh) {
570 $tmp = $value;
571 $value =~ /(\d)+\;\s*url=([^;\s]+)/i;
572 $delay = $1;
573 $value = $2;
574 } else {
575 $$tagblock .= " $key=\"$value\"";
576 next;
577 }
578 }
579 }
580 # deal with potential codebase issues
581 if ($lctag eq 'applet' || $lctag eq 'object') {
582 # Must deal with later
583 }
584 &rewrite_url($r, $remote_site, \$value, $mapref);
585 if ($lctag eq 'meta' && $refresh) {
586 $refresh = 0;
587 $r->headers_out->{'Refresh'} = "$delay; $value";
588 $tmp =~ s/(url=)[^;\s]+/$1$value/i;
589 $value = $tmp;
590 }
591 # Handle the special case of when the value begins with a port
592 if ($value =~ /^:/) {
593 $$tagblock .= " $key=\"$remote_site$value\"";
594 } else {
595 $$tagblock .= " $key=\"$value\"";
596 }
597 } else {
598 $$tagblock .= " $blocks[$i]";
599 }
600 } elsif ($lckey eq $LINK_ELEMENTS{$lctag}) {
601 $value =~ s/(\"|\')//g;
602 &rewrite_url($r, $remote_site, \$value, $mapref);
603 # Handle the special case of when the value begins with a port
604 if ($value =~ /^:/) {
605 $$tagblock .= " $key=\"$remote_site$value\"";
606 } else {
607 $$tagblock .= " $key=\"$value\"";
608 }
609 } else {
610 $$tagblock .= " $blocks[$i]";
611 }
612 } else {
613 $$tagblock .= " $blocks[$i]";
614 }
615 }
616 }
617 }
618  
619 ###############################################################################
620 ###############################################################################
621 # rewrite_url: rewrite URLs as per the mappings hash
622 ###############################################################################
623 ###############################################################################
624 sub rewrite_url {
625 my ($r, $remote_site, $url, $mapref) = @_;
626  
627 $r->log->debug("rewrite_url: Looking at rewriting $$url");
628 $r->log->debug("rewrite_url: remote_site: $remote_site");
629  
630 # Remove remote_site from URI to get just the relative-from-root information
631 if ($$url =~ s/^$remote_site//) {
632 $r->log->debug("rewrite_url: Shrunk to $$url");
633 }
634  
635 # Standardize host on lowercase
636 if ($$url =~ m!([^:]+://)([^/]+)(.*)!) {
637 my ($protocol, $url_host, $uri) = ($1, $2, $3);
638 $url_host =~ tr/A-Z/a-z/;
639 $$url = $protocol . $url_host . $uri;
640 }
641  
642 # Ensure we go from most to least specific rewrite
643 foreach my $mapping (sort { $b cmp $a } keys(%$mapref)) {
644 $r->log->debug("rewrite_url: Testing match of $mapping ",
645 "($$mapref{$mapping})");
646 last if ($$url =~ s/^$mapping/$$mapref{$mapping}/);
647 }
648 }
649  
650 ###############################################################################
651 ###############################################################################
652 # respond: respond to the client
653 ###############################################################################
654 ###############################################################################
655 sub respond {
656 my ($r, $remote_site, $remote_location, $auth_redirect,
657 $response, $mapref) = @_;
658 my $parsed_uri = Apache::URI->parse($r);
659  
660 $r->log->debug("respond: URI: ", $r->uri);
661 $r->log->debug("respond: Parsed hostinfo: ", $parsed_uri->hostinfo());
662  
663 # feed reponse back into our request_record
664 $response->scan(sub {
665 my ($header, $value) = @_;
666 $r->log->debug("respond: OUT $header: $value");
667 if ($header =~ /^Set-Cookie/i) {
668 $value =~ /path=([^;]+)/i;
669 my $cookie_path = $1;
670 &rewrite_url($r, $remote_site, \$cookie_path, $mapref);
671 # Handle the special case of when the value
672 # begins with a port
673 if ($cookie_path =~ /^:/) {
674 $value =~
675 s/(path=)([^;]+)/$1$remote_site$cookie_path/i;
676 } else {
677 $value =~ s/(path=)([^;]+)/$1$cookie_path/i;
678 }
679 } elsif ($header =~/^Client-Peer/i) {
680 my $local_addr = $r->connection->local_addr;
681 my ($port, $ip) =
682 Socket::unpack_sockaddr_in($local_addr);
683 $ip = Socket::inet_ntoa($ip);
684 $value = "$ip:$port";
685 }
686 $r->log->debug("respond: OUT-MOD $header: $value");
687 $r->headers_out->{$header} = $value;
688 });
689 $r->content_type($response->header('Content-type'));
690 $r->status($response->code);
691 $r->status_line(join " ", $response->code, $response->message);
692  
693 # deal with redirects
694 if ($r->status =~ /(301|302)/) {
695 my $location = $response->header('Location');
696 &rewrite_url($r, $remote_site, \$location, $mapref);
697 # Only modify location if rewritten URL is relative
698 unless ($location =~ m!://!) {
699 if ($location =~ m!^/!) {
700 $location = $parsed_uri->scheme . '://' . $parsed_uri->hostinfo .
701 $location;
702 } else {
703 my $base = $r->uri;
704 $base =~ s!(/)[^/]+$!$1!;
705 $location = $parsed_uri->scheme . '://' . $parsed_uri->hostinfo .
706 $base . $location;
707 }
708 }
709 $r->log->debug("respond: Location: $location");
710 $r->headers_out->{'Location'} = $location;
711 }
712  
713 # deal with auth required redirects
714 if ($r->status == 401 && $auth_redirect =~ /^on$/i) {
715 my $base = $r->location();
716 my $location = '';
717 if ($base ne '/') {
718 ($location = $r->uri) =~ s/^$base//;
719 } else {
720 $location = $r->uri;
721 }
722 $location = $remote_location . $location;
723 $r->status('302');
724 $r->status_line(join " ", '302', 'Moved Temporarily');
725 $r->log->debug("respond: Location: $location");
726 $r->headers_out->{'Location'} = $location;
727 $response->content(undef);
728 }
729  
730 if (length($response->content) != 0) {
731 $r->headers_out->{'Content-length'} = length($response->content);
732 } else {
733 # HEAD request, must populate with what backend said
734 $r->headers_out->{'Content-length'} = length($response->content);
735 }
736  
737 $r->log->debug("respond: Status: ", $r->status);
738 $r->log->debug("respond: Status Line: ", $r->status_line);
739  
740 $r->send_http_header();
741 $r->print($response->content);
742 }
743  
744 1;
745  
746 __END__