Perl에 간단한 HTTP 프록시를 어떻게 작성할 수 있습니까?
문제
몇 가지 요청을 버리고 싶기 때문에 http :: 프록시 패키지를 사용하고 싶지 않습니다. 내 하나의 라이너는 이렇게 보이지만 헤더를 통과하려고 시도하는 것을 깨뜨립니다.
perl -MData::Dumper -MHTTP::Daemon -MHTTP::Status -MLWP::UserAgent -e 'my $ua = LWP::UserAgent->new;my $d=new HTTP::Daemon(LocalPort=>1999);print "Please contact me at: <", $d->url, ">\n";while (my $c = $d->accept) {while (my $r = $c->get_request) {if ($r->method eq 'GET' and $r->url->path eq "/uploader") {$c->send_response("whatever.");print Dumper($r);}else{$response=$ua->request($r->method,"http://localhost:1996".$r->uri,$r->headers,$r->content);$c->send_response($response);}}}'
포맷 된 것입니다.
#perl -e '
use Data::Dumper;
use HTTP::Daemon;
use HTTP::Status;
use LWP::UserAgent;
my $ua = LWP::UserAgent->new;
my $d=new HTTP::Daemon(LocalPort=>1999);
print "Please contact me at: < ", $d->url, " >\n";
while (my $c = $d->accept) {
while (my $r = $c->get_request) {
if ($r->method eq 'GET' and $r->url->path eq "/uploaded") {
$c->send_response("whatever.");
print Dumper($r);
} else {
$response = $ua -> request(
$r->method,
"http://localhost:1996" . $r->uri,
$r->headers,
$r->content);
$c->send_response($response);
}
}
}#'
따라서 호스트를 변경해야하기 때문에 요청을 전달할 수는 없으며 헤더를 전달할 수는 없습니다. 그래서 짧게 유지하려면 어떻게해야합니까?
그렇다면 누구든지 이것을 더 나은 원 라이너로 만들 수 있습니까?
해결책
Aw shoot, 나는 이것으로 그것을 고쳤다 :
#perl -e '
use Data::Dumper;
use HTTP::Daemon;
use HTTP::Status;
use LWP::UserAgent;
my $ua = LWP::UserAgent->new;
my $d=new HTTP::Daemon(LocalPort=>1999);
print "Please contact me at: < ", $d->url, " >\n";
while (my $c = $d->accept) {
while (my $r = $c->get_request) {
if ($r->method eq "GET" and $r->url->path eq "/uploaded") {
$c->send_response("whatever.");
print Dumper($r);
} else {
$response = $ua -> request( HTTP::Request->new(
$r->method,
"http://localhost:1996" . $r->uri,
$r->headers,
$r->content));
$c->send_response($response);
}
}
}#'
참고 HTTP::Request->new
예 ... 작동합니다. 약간 느립니다. 하지만 괜찮습니다
다른 팁
한 라이너를 쓰려고하는 이유는 무엇입니까? 파일에 프로그램을 저장할 수없는 이유가 있습니까? 상황이 무엇인지 궁금합니다.
제휴하지 않습니다 StackOverflow