views:

1284

answers:

2

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-&gt;uri,$r-&gt;headers,$r-&gt;content);$c-&gt;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?

+3  A: 

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
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
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
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
I didn't even notice that the GET string was not right. Thanks.
dlamblin
+1  A: 

Why are you trying so hard to write a one-liner? Is there some reason you can't save the program in a file? I'm just curious what the situation is.

brian d foy
That's a fair question; I have made a file now. A one-liner would ideally make more sense and could be whipped up for other situations quickly. It might read like: perl -MMagic::Package -e '$d=M::P->new(1999,"http://localhost:1996");while($x=$d->except("/uploaded")){Dumper($x);}'
dlamblin