Skip to content

Instantly share code, notes, and snippets.

@miyagawa
Created May 11, 2011 04:59
Show Gist options
  • Save miyagawa/965935 to your computer and use it in GitHub Desktop.
Save miyagawa/965935 to your computer and use it in GitHub Desktop.
hijack any LWP stack with your PSGI app - it's on CPAN now http://search.cpan.org/~miyagawa/LWP-Protocol-PSGI-0.01/
use strict;
use LWP::UserAgent;
my $app = do {
use Mojolicious::Lite;
get '/' => sub { shift->render(text => "Hello World") };
get '/search' => sub {
my $self = shift;
my $foo = $self->param('q');
$self->render(text => "You searched $foo");
};
app->start('psgi');
};
package LWP::Protocol::PSGI;
use parent qw(LWP::Protocol);
use HTTP::Message::PSGI;
sub request {
my($self, $request) = @_;
my $env = HTTP::Message::PSGI::req_to_psgi $request;
my $res = $app->($env);
return HTTP::Message::PSGI::res_from_psgi $res;
}
for my $proto ('http', 'https') {
LWP::Protocol::implementor($proto, "LWP::Protocol::PSGI");
}
use XXX -dumper;
my $ua = LWP::UserAgent->new;
my $res = $ua->get("http://google.com/");
warn $res->content;
$res = $ua->get("http://google.com/search?q=foo");
warn $res->content;
use WWW::Mechanize;
my $mech = WWW::Mechanize->new;
$mech->get('http://amazon.com/search?q=bar');
warn $mech->content;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment