Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

As someone mentioned, the easiest/shortest way is with a proxy; e.g., Plack::App::Proxy

Plack::App::Proxy->new(remote => 'http://perl.org')->to_app;

That could be wrapped up in a proper script to control URIs and capturing data and what gets proxied and what doesn't, etc.

Because I was curious, I tried whipping up a Plack::Request/HTTP::Response based version. Turns out it's quite easy. Though it seems to work, this code is a simplistic first stab and I have no idea how fit it is for your, or any, actual task.

The code is shown after the usage… I admit it's confusing, but it was more fun/terse to have the code display itself.

Run it

I stripped some headers (Host/Agent/Accept) from the output to save space.

plackup /home/moo/post-echo.psgi -r Watching /home/moo/lib /home/moo/post-echo.psgi for file updates. HTTP::Server::PSGI: Accepting connections at http://0:5000/

Use it

moo@cow[2258]~>curl http://0:5000/ OHAI GET moo@cow[2259]~>curl http://0:5000/ -X HEAD OHAI HEAD moo@cow[2260]~>curl http://0:5000/ -X POST POST http://example.org/ User-Agent: ...snip... moo@cow[2261]~>curl http://0:5000/ -d "internet=cats in tubes" POST http://example.org/ Content-Length: 22 Content-Type: application/x-www-form-urlencoded internet=cats in tubes

Have it echo its own code :P

Demonstrate that file uploads (apparently) work fine too.

curl http://0:5000/ -F code=@/home/moo/post-echo.psgi POST http://example.org/ Expect: 100-continue Content-Length: 2408 Content-Type: multipart/form-data; boundary=-------------------------- +--2db0025e93ba ------------------------------2db0025e93ba Content-Disposition: form-data; name="code"; filename="post-echo.psgi" Content-Type: application/octet-stream #!/usr/bin/env perl # filename: post-echo.psgi use strictures; use Plack::Request; use HTTP::Request; sub { my $env = shift; my $request = Plack::Request->new($env); my $response = $request->new_response(200); $response->content_type("text/plain; charset=UTF-8"); if ( $request->method eq "POST" ) { $request->input->read( my $buffer, $request->header("Content-L +ength"), 0 ); my $rerequest = HTTP::Request ->new( "POST", "http://example.org/" , $request->headers, +$buffer ); $response->body( $rerequest->as_string ); } else { $response->body("OHAI " . $request->method . "\n"); } $response->finalize; };



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-03-29 10:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found