Perlで簡単なHTTPプロキシを作成するにはどうすればよいですか?

StackOverflow https://stackoverflow.com/questions/273718

  •  07-07-2019
  •  | 
  •  

質問

HTTP :: Proxyパッケージを使用したくないのは、いくつかのリクエストをダンプしたいからです。私の1つのライナーは次のように見えますが、ヘッダーを渡そうとすると壊れます:

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