ilce
ilce

Reputation: 1046

LWP::UserAgent - determining source of response code - PERL Modules

When using the LWP::UserAgent module, one makes a request to a URL and receives an HTTP::Response object which contains the response code (hopefully 200!) and a status line.

My problem is that I can't figure out how to determine whether the response code was returned from the webserver or from LWP::UserAgent. For example, I believe that if the domain name doesn't resolve or you simply cannot connect to the host, LWP::UserAgent reports this in the form a 500 code, which is indistinguisable from a 500 "Internal Server Error" code reported from the actual web server that's up but experiencing some issues.

The problem is further amplified when going through a proxy server, as there are now three possible "sources" of an error message:

How is one supposed to know if the 500 code means a) the server is up but unhappy, b) the proxy could not connect to the server, or c) LWP::UserAgent could not connect to the proxy?

I posted the same question here also: http://www.justskins.com/forums/lwp-useragent-determining-source-43810.html

Upvotes: 3

Views: 994

Answers (2)

ilce
ilce

Reputation: 1046

#!/usr/bin/perl
use strict;

use LWP::UserAgent;
use HTTP::Request;
use IO::Socket::SSL;

my $ua = LWP::UserAgent->new(
   ssl_opts => {
      SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE
   }
);

my $request = HTTP::Request->new(GET => "www.example.com");
my $response = $ua->request($request);

my $clientWarning = $response->header("Client-Warning");
if(defined $clientWarning and length($clientWarning) != 0) {
    if($clientWarning =~ /Internal response/) {
       print "$server UNAVAILABLE";
    }
 } else {
   print "server AVAILABLE";
 }

Upvotes: 1

Oleg V. Volkov
Oleg V. Volkov

Reputation: 22421

Error responses that LWP generates internally will have the "Client-Warning" header set to the value "Internal response". If you need to differentiate these internal responses from responses that a remote server actually generates, you need to test this header value.

(from LWP::UserAgent -> REQUEST-METHODS)

Upvotes: 2

Related Questions