1

I don't want to use the HTTP::Proxy package because I want to dump out a couple requests. My one liner looks like this, but breaks on trying to pass the header in:

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

formatted, that's:

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

So I can't just pass in the request, because I need to change the host, and I can't just pass in the headers it seems... so what should I do to keep it short.

So can anyone make this a better one-liner?

brian d foy
  • 129,424
  • 31
  • 207
  • 592
dlamblin
  • 43,965
  • 20
  • 101
  • 140

1 Answers1

7

Aw shoot, I fixed it with this:

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

note the HTTP::Request->new yeah... so it works, it's a tad slow. but that's okay

dlamblin
  • 43,965
  • 20
  • 101
  • 140
  • You probably don't want to use single quotes around GET. It works in this case, but not for the reasons you think it works (try turning on strict mode to see what I mean). – Leon Timmermans Nov 07 '08 at 22:07
  • I'm lost on why not to use single quotes. The single quotes simply mean "no variable interpolation." In fact, I would recommend changing the other strings to single-quotes just to be consistent. – Max Lybbert Nov 07 '08 at 23:39
  • 1
    Because he is also using them on the command line. Due to luck this works in this case, but in other cases it won't. – Leon Timmermans Nov 08 '08 at 01:52
  • I didn't even notice that the GET string was not right. Thanks. – dlamblin Nov 17 '08 at 17:39