#!/usr/bin/perl -w use HTTP::Daemon; use HTTP::Status; use HTTP::Response; use IPC::Open2; use LWP::UserAgent; my $PORT = 8081; if (@ARGV) { $PORT = shift; } =comment sub clean_child { wait; $SIG{'CHLD'} = \&clean_child; } $SIG{'CHLD'} = \&clean_child; =cut my $LOCAL = 'http://(nemesis|aisa)'; my $REMOTE = 'https://is.muni.cz/'; my $daemon = new HTTP::Daemon LocalPort => $PORT, Reuse => 1, Listen => 20; if (not defined $daemon) { die "Cannot bind to $PORT: $@\n"; } my $agent = new LWP::UserAgent; my $myurl = $daemon->url; warn "My URL is $myurl\n"; while (my $connection = $daemon->accept) { my $child = fork(); unless (defined $child) { die "Fork failed: $!\n"; } if ($child) { # Rodič -- nic na práci, půjdeme zase poslouchat warn "Parent: forked child $child, fine. Will go to another accept.\n"; ### $connection->close; next; } $connection->autoflush; my $num_req = 1; # Potomek postupně přebírá požadavky (ovšem se s klientem # nedomluví na keep-alive spojení, bude proveden stejně jen jeden) while (my $request = $connection->get_request) { my $uri = $request->uri; $uri =~ s!^$LOCAL(\.[^:/]+)*(:\d+)*/!$REMOTE/!; print $request->uri, "\n"; $request->uri($uri); my $response = $agent->request($request); if ($response->is_success) { my $body = $response->content; $body =~ s!$REMOTE!$myurl!gso; $response->content($body); } else { warn $response->error_as_HTML; } $connection->send_response($response); } continue { warn "Child $$: finished response, going back to wait.\n"; } warn "Child $$: no more requests, exitting.\n"; ## $connection->close; ## undef $connection; exit 0; } undef $daemon;