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

The next subroutine:

sub check_link { my $url = $_[0]; my $realm = 'Privacy Guard'; my $user = 'Admin'; my $pass = '********'; $url =~ s/http:\/\///; $url = $url.':80'; my $ua = LWP::UserAgent->new; $ua->credentials( $url, $realm, $user, $pass ); my $res = $ua->head($url); if ($res->is_error) { return $res->status_line; } else { return $url; } }

gives a: "400 URL must be absolute" error.

if (!$res->is_error) {

gives: 88.159.57.235/roots/lines/wielinga/wie-aaa/wie-sle/wie-hee/wie-bru/index.htm:80

What goes wrong?

Replies are listed 'Best First'.
Re: 400 URL must be absolute
by cavac (Prior) on Apr 04, 2022 at 22:03 UTC

    That URL is wrong on so many levels.

    First of all, LWP::UserAgent expects the protocol name at the beginning of the URL. In your case, that would be "http://". (This is insecure, and you should really be using https, but whatever).

    The port number would go after the hostname, before the rest of the URI (e.g. "the path on the server"). So that would be http://hostname:80/foo/bart/baz

    Since you are using HTTP and the default port for HTTP is 80, you don't need to specify the port number.

    So the URI you want to use, i assume is:

    http://88.159.57.235/roots/lines/wielinga/wie-aaa/wie-sle/wie-hee/wie-bru/index.htm

    Since you use basic auth, you should be able to just paste the URI into your browser and get the authentication prompt to verify. Works for me.

    More information:

    perl -e 'use Crypt::Digest::SHA256 qw[sha256_hex]; print substr(sha256_hex("the Answer To Life, The Universe And Everything"), 6, 2), "\n";'

      Thanks a lot, it helps me with the "400 URL must be absolute" error. I'm not a programmer, let alone a "PerlMonk". I have to get my knowledge from what I can find on the internet and that is not always clear.

      But... now i get a "401 Unauthorized" error. Because I know now the URL must be OK and the user and password are OK, the problem must be the realm (?). I got the in de sub given realm with the script:

      lwp-request -e http://88.159.57.235/roots/lines/wielinga/wie-aaa/wie-s +le/wie-hee/wie-bru/pien.htm

      with the response:

      401 Unauthorized Connection: close Date: Tue, 05 Apr 2022 09:21:04 GMT Accept-Ranges: bytes ETag: "8b2-5d3194c961ede" Server: Apache/2.4.41 (Ubuntu) WWW-Authenticate: Basic realm="Maintenance Guard" Content-Length: 2226 Content-Type: text/html Last-Modified: Tue, 14 Dec 2021 11:14:56 GMT Client-Date: Tue, 05 Apr 2022 09:21:04 GMT Client-Peer: 88.159.57.235:80 Client-Response-Num: 1 Title: Customized Error 401 X-Meta-Author: W.J.Nijs X-Meta-Robots: noindex

      Is there an other way to get the right realm?

        Your code in the original post say real='Privacy Guard', but the lwp-request headers ask for realm='Maintenance Guard'.

        So the correct realm on your code would be 'Maintenance Guard', i guess.

        Also make sure you get upper/lowercase of all authentication fields correct (username, password, realm).

        I can't really test your setup. For that, i would need username+password, but i will never ask for that unless you are a paying customer of my employer due to all sorts of legal and insurance reasons. So do not post the authentication data. The only thing i can advise you to do is to check if your authentication data is correct by using your browser to log in.

        perl -e 'use Crypt::Digest::SHA256 qw[sha256_hex]; print substr(sha256_hex("the Answer To Life, The Universe And Everything"), 6, 2), "\n";'

        Or have i use the URI instead of the URL for $url with the user and pass in the URI? Is that save?

Re: 400 URL must be absolute
by LanX (Saint) on Apr 04, 2022 at 19:13 UTC
    my guess: your regex is deleting the leading "http://" hence making $url relative

    update

    > ... wie-bru/index.htm:80

    ehm ... please inform yourself about URL-syntax and the position of port numbers, they are supposed to come before the file path.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

      Sorry, i am not only not a "PerlMonk" but also not a "URL/URI Monk", but i will learn. Thanks...

        To avoid the problem with the realm I used an URI with the user info included. Next subroutine works perfect:

        #----------------------- # Subroutine check_link #----------------------- sub check_link { my $url = $_[0]; my $user = 'Admin'; my $pass = '********'; my $UriInfo = 'http://'.$user.':'.$pass.'@'; $url =~ s/http:\/\//$UriInfo/; my $ua = LWP::UserAgent->new; my $res = $ua->head($url); if ($res->is_error) { return '<FONT COLOR="#FF0000">X</FONT>'; } else { return '<FONT COLOR="#008800">V</FONT>'; } }

        Thank you all for the very meaningful contributions.