| 1 | # /=====================================================================\ # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | # | LaTeXML::Util::WWW | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | # | Web Utilities for LaTeXML | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | # |=====================================================================| # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | # | Part of LaTeXML: | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | # | Public domain software, produced as part of work done by the | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | # | United States Government & not subject to copyright in the US. | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | # |---------------------------------------------------------------------| # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | # | Deyan Ginev (d.ginev@jacobs-university.de) #_# | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | # | http://dlmf.nist.gov/LaTeXML/ (o o) | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | # \=========================================================ooo==U==ooo=/ # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | package LaTeXML::Util::WWW; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 5 | ▶ | use LaTeXML::Common::Error; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 5 | ▶ | use LaTeXML::Global; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 5 | ▶ | use URI; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 5 | ▶ | use LWP; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 5 | ▶ | use LWP::Simple; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 5 | ▶ | use base qw(Exporter); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 5 | our @EXPORT = qw(&auth_get &url_find &url_split); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 0 | ▶ | sub auth_get { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
auth_get: not called CC=13 SCAR=52.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 0 | my ($url, $authlist) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 0 | my $browser; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 0 | ▶ | if ($url =~ /^https/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 0 | $browser = LWP::UserAgent->new(ssl_opts => { verify_hostname => 0 }); } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 0 | $browser = LWP::UserAgent->new; } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 0 | my $response = $browser->get($url); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 0 | my $realm = $response->www_authenticate; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 0 | ▶ | if ($realm) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | ▶ | if ($realm =~ /^Basic realm="([^"]+)"$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | $realm = $1; } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | # Prompt for username pass for this location: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 0 | my $req; my $tries = 2; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 0 | ▶ | my ($uname, $pass) = ($$authlist{$realm} ? @{ $$authlist{$realm} } : (undef, undef)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 0 | ▶ | while (!($response && $response->is_success) && $tries > 0) { # 2 tries | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
not $response && $response->is_success and $tries > 0
$response && $response->is_success
MC/DC0%0/3
not $response && $response->is_success and $tries > 0
$response
$response->is_success
$tries > 0
Truth table0%0/5
not(A && B) and C
A= $responseB= $response->is_successC= $tries > 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 0 | $tries--; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 0 | ▶ | if (!$uname) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | $req = HTTP::Request->new(GET => $url); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 0 | $req->authorization_basic($uname, $pass); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 0 | $response = $browser->request($req); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | ▶ | $$authlist{$realm} = [$uname, $pass] if $response->is_success; } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | $req = HTTP::Request->new(GET => $url); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | $req->authorization_basic($uname, $pass); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | $response = $browser->request($req); } } } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | ▶ | return auth_get("$url.tex", $authlist) if ((!$response->is_success) && $url !~ /\.tex$/); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$response->is_success or $url =~ /\.tex$/
MC/DC0%0/2
$response->is_success or $url =~ /\.tex$/
$response->is_success
$url =~ /\.tex$/
Truth table0%0/3
A or B
A= $response->is_successB= $url =~ /\.tex$/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | ▶ | Error('www', 'get', $url, 'HTTP GET failed with: "' . $response->message . '"') unless ($response->is_success); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | return $response->content; } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | ▶ | sub url_find { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
url_find: not called CC=8 SCAR=42.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | my ($relative_url, %options) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | return if !($options{urlbase} && $relative_url && | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | ▶ | length($options{urlbase}) > 0 && length($relative_url) > 0); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/9
$options{"urlbase"} and $relative_url and length $options{"urlbase"} > 0 and length $relative_url > 0
$options{"urlbase"} and $relative_url and length $options{"urlbase"} > 0
$options{"urlbase"} and $relative_url
MC/DC0%0/4
$options{"urlbase"} and $relative_url and length $options{"urlbase"} > 0 and length $relative_url > 0
$options{"urlbase"}
$relative_url
length $options{"urlbase"} > 0
length $relative_url > 0
Truth table0%0/5
A and B and C and D
A= $options{"urlbase"}B= $relative_urlC= length $options{"urlbase"} > 0D= length $relative_url > 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | $options{urlbase} =~ s/(\/+)$//g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | $options{urlbase} .= '/'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | my $absolute_url = URI->new_abs($relative_url, $options{urlbase}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | my $browser; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | ▶ | if ($absolute_url =~ /^https/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | $browser = LWP::UserAgent->new(ssl_opts => { verify_hostname => 0 }); } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | $browser = LWP::UserAgent->new; } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | my $response = $browser->head($absolute_url); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | ▶ | if ($response->is_success) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | return $absolute_url->as_string } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | # Only 404 is expected, anythign else is an error: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | ▶ | Error("http_fail", $response->code, undef, " HTTP GET on $absolute_url failed. Reason: " . $response->message) if ($response->code != 404); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | return; } } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | ▶ | sub url_split { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
url_split: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | my ($url) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | ▶ | return ($url =~ /^(.+)\/([^\/]+)$/ ? ($1, $2) : ($url, 'index.tex')); } # Well, what???? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | #====================================================================== | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 5 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||