Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Help with converting HTTP::Request to Plack::Request

by knox (Sexton)
on Feb 10, 2020 at 05:52 UTC ( #11112715=perlquestion: print w/replies, xml ) Need Help??

knox has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks

Your wisdom is politely requested to set me on the right path. I am looking for a way to convert the code below to Plack. The snippet below requests a remote url that redirects, and then captures the new redirected URL. I have recently converted most of project over to Plack, but I am stuck on this bit. I will acknowledge that Plack::Request may not the solution here, and I still looking around cpan for other solutions.

Thank you for any guidance, also any code samples would also be really helpful!

$request = HTTP::Request->new(GET => $url); my $user_agent = LWP::UserAgent->new; my $response = $user_agent->request($request); if ( $response->is_success and $response->previous ) { ($scheme, $auth, $path, $query, $frag) = uri_split($respon +se->request->uri); }

Replies are listed 'Best First'.
Re: Help with converting HTTP::Request to Plack::Request
by Your Mother (Bishop) on Feb 10, 2020 at 07:56 UTC

    I echo anonymonkís request for clarification. Plack/PSGI is server side. It doesnít, so far, make sense to answer your question with Plack code. Here is some more complete stuff for anyone to play with. I would suggest that the URI objectís methods are superior to URI::Split, so I include them after the split. One thing you might consider, and who knows, might be leading to your problem, the server doesnít get the fragment (I mention it partly because it was an issue for me once and you present it in code dealing with server responses). Only the client knows about fragments; you can scan the YAML dump and see everything the server code gets.

    # Server plackup -MYAML -e 'sub { warn Dump(+shift), $/; [ 200, [], [ "OHAI" ]] + }' HTTP::Server::PSGI: Accepting connections at http://0:5000/
    # Client use 5.16.2; use strict; use HTTP::Request; use LWP::UserAgent; use URI::Split "uri_split"; my $uri = 'http://user:pass@localhost:5000/some/path?o=hai#frag'; my $request = HTTP::Request->new( GET => $uri ); my $user_agent = LWP::UserAgent->new; my $response = $user_agent->request($request); if ( $response->is_success ) { my $uri = $response->request->uri; my ( $scheme, $auth, $path, $query, $frag ) = uri_split($uri); say join " + ", $scheme, $auth, $path, $query, $frag; for my $method (qw/ authority host port path fragment query /) { printf "%12s -> %s\n", $method, $uri->$method; } }

      The context: I had a working local FCGI script that i was converting to Plack step by step. It was going smoothly until it got to the posted code. For some reason I couldn't get an error code or line number, just: "failed (111: Connection refused) while connecting to upstream, client: 127.0.0.1, server: polyneura.local, request: "POST /index.cgi HTTP/1.1", upstream: "http://127.0.0.1:5000". I should have posted it here.

      Plack/PSGI is server side. It doesnít, so far, make sense to answer your question with Plack code.

      This was really helpful, it wasn't obvious to me until you mentioned it. While it didn't directly solve my problem, it was a light bulb moment for me as I thought that every type of request had to wrapped up in some sort of Plack handler.

      Only the client knows about fragments; you can scan the YAML dump and see everything the server code gets.

      This is cool, thanks! After further testing, I found that LWP::Protocol::https was not installed. This confused me, because it was working on my local fcgi script (I'll check this later). So I went through the process of installing LWP::Protocol::https which yielded more errors:

      ! Installing the dependencies failed: Module 'Net::SSLeay' is not inst +alled ! Bailing out the installation for IO-Socket-SSL-2.066.

      ! tail /home/<userhome>/.cpanm/work/1581360095.6546/build.log ^ rm -f blib/arch/auto/Net/SSLeay/SSLeay.so LD_RUN_PATH="/usr/lib/x86_64-linux-gnu:/lib/x86_64-linux-gnu" cc -sha +red -O2 -L/usr -L/usr/lib -L/usr/local/lib -fstack-protector-strong +SSLeay.o -o blib/arch/auto/Net/SSLeay/SSLeay.so \ -L/usr -L/usr/lib -lssl -lcrypto -lz \ /usr/bin/ld: cannot find -lz collect2: error: ld returned 1 exit status Makefile:493: recipe for target 'blib/arch/auto/Net/SSLeay/SSLeay.so' +failed make: *** [blib/arch/auto/Net/SSLeay/SSLeay.so] Error 1 -> FAIL Installing Net::SSLeay failed. See /home/<userhome>/.cpanm/wor +k/1581360095.6546/build.log for details. Retry with --force to force +install it.

      I found this: https://stackoverflow.com/questions/3373995/usr-bin-ld-cannot-find-lz and installed sudo apt-get install zlib1g-dev. I haven't got around to changing to URI yet, but I will make some changes soon. Thanks for that. Apologies if this was long winded, but I figure someone out there could learn from my mistakes.

      A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Help with converting HTTP::Request to Plack::Request
by Anonymous Monk on Feb 10, 2020 at 06:51 UTC

    What is the context?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://11112715]
Approved by marto
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2020-08-09 20:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which rocket would you take to Mars?










    Results (55 votes). Check out past polls.

    Notices?