how do i "work out the full url"
update: You only have to do this if HTML::SimpleLinkExtor doesn't do it for you -- and it apparently will do if for you if there is a <BASE HREF> tag in the page that you fetch. For that matter, when you happen to fetch a page that has no such tag, you could just put one in before passing the page to LinkExtor:
s{(</head>)}{<base href="$current_docroot">$1}i;
See below about determining the doc-root from a url.
(end of update)
I kinda thought you were already doing that (or trying to) in the code you posted -- but maybe you haven't had a chance to test that part yet (or you did test it, and it didn't work :P).
I'll admit that it's not something I've tried myself, but I think my first impulse would be to do something similar to what you have (this is also not tested):
# $link_target and $url_just_fetched are known...
my $full_target;
if ( $link_target =~ m{^/} ) {
# initial "/" means "relative to doc-root":
my $docroot = $url_just_fetched;
$docroot =~ s{(?<=[^/]/)[^/].*}{}; # delete everything after first
+single slash
$full_target = $docroot . $link_target;
}
elsif ( $link_target !~ /^http/ ) {
# it's presumably relative to the url just fetched, so:
if ( $url_just_fetched =~ m{/$} ) {
$full_target = $url_just_fetched . $link_target;
}
# this is the tricky part:
elsif ( $url_just_fetched =~ /[?&=;:]|\.htm/ ) # probably not a di
+rectory name...
{
my $last_slash = rindex( $url_just_fetched, "/" ) + 1;
$full_target = substr( $url_just_fetched, 0, $last_slash ) . $li
+nk_target;
}
else # assume its a directory name {
$full_target = join "/", $url_just_fetched, $link_target;
}
}
# (if $link_target does start with "http", then it's probably complete
+ already)
# last step:
$full_target =~ s/\#.*//; # in case the link target is a named anchor
+ within a page
(I did do a quick "super search" for SoPW nodes that discuss "resolve relative href", and only found chromatic saying "there is no such module" -- maybe he was thinking of the question in a different context...)
No doubt there'll be some tweaking to do -- especially the part that tries to guess if the last chunk of a url looks like a directory -- but that should get you started. You probably want to make that a separate subroutine (maybe someday it'll be a module). |