我不想使用HTTP :: Proxy包,因为我想要转储几个请求。我的一个班轮看起来像这样,但在尝试传递标题时打破了:

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);
    }
  }
}#'

所以我不能只传递请求,因为我需要更改主机,而且我不能只传入它看起来的标题......所以我应该做些什么来保持它简短。

那么有人能让这个更好的单行吗?

有帮助吗?

解决方案

拍摄很棒,我用它修好了:

#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-&gt; new 是的......所以它有效,它有点慢。但那没关系

其他提示

为什么你这么努力写一个单行程?是否有某些原因无法将程序保存在文件中?我只是好奇情况是什么。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top