Base URL of the Mokholichesky answer

The module HTTP::Responsehas a method basethat returns the base URL used when converting relative URLs to message content absolute. It extracts it from the body of the message (as an element <base>inside HTML <head>), the HTTP header Content-Location(or deprecated Content-Base), or the requested URL (from the most recent redirect, if any)

I don’t see any way to Mojo::Message::Responseprovide the same information, and I don’t want to encode it myself if it’s already there somewhere

Can anyone help me out?

+4
source share
1 answer

, base LWP


content_location Mojo::Headers. Mojo::Message::Response Mojo::Message Mojo::Headers, ,

my $res = Mojo::Message::Response->new;
$res->parse("HTTP/1.0 200 OK\x0d\x0a");
$res->parse("Content-Length: 12\x0d\x0a");
$res->parse("Content-Type: text/plain\x0d\x0a\x0d\x0a");
$res->parse('Hello World!');
say $res->code;
say $res->headers->content_type;
say $res->body;

say $res->headers->content_location // 'location not defined';  # /
$res->headers->content_location('set_some_location');
say $res->headers->content_location // 'location not defined';

, . ? , .

, . LWP HTTP::Response, base. (v5.16) Mojo::UA . .

URL- DOM,

use Mojo::UserAgent;
my $ua = Mojo::UserAgent->new;
my $base_url = $ua->get($url)
    ->res->dom
    ->at('head')->at('base')->attr('href');

dom Mojo::Message, base attr Mojo::DOM.

$dom->at('head > base[href]');

hashref { href => URL }, URL-.

at undef , .

, <head>.


. Mojo:: Content, Single Mojo::Message::content Mojo:: UserAgent:: Transactor redirect, Mojo:: URL. . , URL.

, base LWP .

+4

Source: https://habr.com/ru/post/1673820/


All Articles