LWP::P...l::PSGI(3pm)

πŸ“¦
version 0.11 Β· 6 versions
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– LWP::Protocol::PSGI πŸ’–

okay bestie, so this is the page for LWP::Protocol::PSGI (a 3pm manual page, if you're keeping score) β€” and it's literally the module that lets you hijack LWP's HTTP/HTTPS backend and swap in your own PSGI application. no cap. πŸ¦‹


✨ NAME ✨

LWP::Protocol::PSGI β€” Override LWP's HTTP/HTTPS backend with your own PSGI application


🌸 SYNOPSIS 🌸

use LWP::UserAgent;
use LWP::Protocol::PSGI;

# $app can be any PSGI application: Mojolicious, Catalyst or your own
my $app = do {
    use Dancer;
    set apphandler => 'PSGI';
    get '/search' => sub {
        return 'searching for ' . params->{q};
    };
    dance;
};

# Register the $app to handle all LWP requests
LWP::Protocol::PSGI->register($app);

# can hijack any code or module that uses LWP::UserAgent underneath, with no changes
my $ua  = LWP::UserAgent->new;
my $res = $ua->get("http://www.google.com/search?q=bar");
print $res->content; # "searching for bar"

# Only hijacks specific host (and port)
LWP::Protocol::PSGI->register($psgi_app, host => 'localhost:3000');

my $ua = LWP::UserAgent->new;
$ua->get("http://localhost:3000/app"); # this routes $app
$ua->get("http://google.com/api");     # this doesn't - handled with actual HTTP requests

πŸŽ€ DESCRIPTION πŸŽ€

LWP::Protocol::PSGI is a module that hijacks any code using LWP::UserAgent underneath, so that any HTTP or HTTPS requests can be routed straight into your own PSGI application. it's giving interception but make it cute. πŸ’…

and because it works with literally any code that uses LWP, you can override all your WWW::*, Net::* or WebService::* modules β€” yes, WWW::Mechanize included β€” without touching the calling code or its internals. obsessed. 🫢

use WWW::Mechanize;
use LWP::Protocol::PSGI;

LWP::Protocol::PSGI->register($my_psgi_app);

my $mech = WWW::Mechanize->new;
$mech->get("http://amazon.com/"); # $my_psgi_app runs

🧚 TESTING 🧚

this module is extremely handy if you have tests that run HTTP requests against your application and you want them to work with both internal and external instances. main character testing energy, fr fr.

# in your .t file
use Test::More;
use LWP::UserAgent;

unless ($ENV{TEST_LIVE}) {
    require LWP::Protocol::PSGI;
    my $app = Plack::Util::load_psgi("app.psgi");
    LWP::Protocol::PSGI->register($app);
}

my $ua = LWP::UserAgent->new;
my $res = $ua->get("http://myapp.example.com/");
is $res->code, 200;
like $res->content, qr/Hello/;

so this test script will, by default, route all HTTP requests to your own PSGI app defined in $app β€” but with the environment variable TEST_LIVE set, it runs the requests against the live server instead. best of both worlds, lowkey genius. ✨

you can also combine Plack::App::Proxy with LWP::Protocol::PSGI to route all requests made in your test against a specific server:

use LWP::Protocol::PSGI;
use Plack::App::Proxy;

my $app = Plack::App::Proxy->new(remote => "http://testapp.local:3000")->to_app;
LWP::Protocol::PSGI->register($app);

my $ua = LWP::UserAgent->new;
my $res = $ua->request("http://testapp.com"); # this hits testapp.local:3000

πŸ‘‘ METHODS πŸ‘‘

πŸ’œ register

LWP::Protocol::PSGI->register($app, %options);
my $guard = LWP::Protocol::PSGI->register($app, %options);

Registers an override hook to hijack HTTP requests. and if you call it in a non-void context, it returns a guard object that automatically resets the override when it goes out of context. so clean. πŸŽ€

{
    my $guard = LWP::Protocol::PSGI->register($app);
    # hijack the code using LWP with $app
}

# now LWP uses the original HTTP implementations

when %options is specified, the option limits which URL and hosts this handler overrides. you can either pass host or uri to match requests, and if it doesn't match, the handler falls back to the original LWP HTTP protocol implementor. no drama, no fallouts. 🌈

LWP::Protocol::PSGI->register($app, host => 'www.google.com');
LWP::Protocol::PSGI->register($app, host => qr/\.google\.com$/);
LWP::Protocol::PSGI->register($app, uri => sub { my $uri = shift; ... });

the options can take either:

  • a string, where it does a complete match
  • a regular expression, or
  • a subroutine reference that returns boolean given the value of host (only the hostname) or uri (the whole URI, including query parameters)

πŸ’œ unregister

LWP::Protocol::PSGI->unregister;

Resets all the overrides for LWP. and if you use the guard interface described above, it will be automatically called for you. bye bye override πŸ’…


πŸ¦‹ DIFFERENCES WITH OTHER MODULES πŸ¦‹

🌸 Mock vs Protocol handlers

there are similar modules on CPAN that let you emulate LWP requests and responses. most of them are implemented as a mock library, which means it doesn't go through the LWP guts and just gives you a wrapper for receiving HTTP::Request and returning HTTP::Response back.

LWP::Protocol::PSGI is implemented as an LWP protocol handler instead, and it allows you to use most of the LWP extensions to add capabilities such as manipulating headers and parsing cookies. it's giving depth, not just vibes. πŸ”₯

🌸 Test::LWP::UserAgent

Test::LWP::UserAgent has the similar concept of overriding LWP request method with particular PSGI applications. It has more features and options such as passing through the requests to the native LWP handler, while LWP::Protocol::PSGI only allows one to map certain hosts and ports.

Test::LWP::UserAgent requires you to change the instantiation of UserAgent from LWP::UserAgent->new to Test::LWP::UserAgent->new somehow, and it's your responsibility to do so. This mechanism gives you more control which requests should go through the PSGI app, and it might not be difficult if the creation is done in one place in your code base. However it might be hard or even impossible when you are dealing with third party modules that calls LWP::UserAgent inside.

LWP::Protocol::PSGI affects the LWP calling code more globally, while having an option to enable it only in a specific block, thus there's no need to change the UserAgent object manually, whether it is in your code or CPAN modules. periodt. πŸ‘‘


🌟 AUTHOR 🌟

Tatsuhiko Miyagawa <miyagawa@bulknews.net>


Copyright 2011- Tatsuhiko Miyagawa


🌟 LICENSE 🌟

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


⭐ SEE ALSO ⭐

  • Plack::Client
  • LWP::UserAgent

πŸ’– and that's the whole page bestie, you ate πŸ’–

okay so that's the tea: perl v5.34.0, dated 2022-06-30, and this lil manual page lives under LWP::P...l::PSGI(3pm) in the User Contributed Perl Documentation. slay, go hijack some requests responsibly 🫢✨

πŸ“¦ where this page comes from