1 |
commit: a8b0ba36853eea23c0e07bcc680bdbe877dcd68e |
2 |
Author: Robin H. Johnson <robbat2 <AT> gentoo <DOT> org> |
3 |
AuthorDate: Thu Apr 30 06:29:53 2020 +0000 |
4 |
Commit: Robin H. Johnson <robbat2 <AT> gentoo <DOT> org> |
5 |
CommitDate: Thu Apr 30 06:29:53 2020 +0000 |
6 |
URL: https://gitweb.gentoo.org/proj/gentoo-mirrorstats.git/commit/?id=a8b0ba36 |
7 |
|
8 |
probe-mirmon: use libcurl instead of exec wget |
9 |
|
10 |
Signed-off-by: Robin H. Johnson <robbat2 <AT> gentoo.org> |
11 |
|
12 |
probe-mirmon | 38 +++++++++++++++++++++++++++++++++++++- |
13 |
1 file changed, 37 insertions(+), 1 deletion(-) |
14 |
|
15 |
diff --git a/probe-mirmon b/probe-mirmon |
16 |
index 8c57691..6d67fbf 100755 |
17 |
--- a/probe-mirmon |
18 |
+++ b/probe-mirmon |
19 |
@@ -21,6 +21,7 @@ use strict; |
20 |
use warnings; |
21 |
use Date::Parse (); # dev-perl/TimeDate |
22 |
use File::Tempdir; # dev-perl/File-Tempdir |
23 |
+use WWW::Curl::Easy; |
24 |
|
25 |
sub main { |
26 |
my ( $timeout, $url ) = @_; |
27 |
@@ -28,10 +29,45 @@ sub main { |
28 |
handle_rsync( $timeout, $url ); |
29 |
} |
30 |
else { |
31 |
- handle_wget( $timeout, $url ); |
32 |
+ handle_libcurl( $timeout, $url ); |
33 |
} |
34 |
} |
35 |
|
36 |
+sub handle_libcurl { |
37 |
+ my ( $timeout, $url ) = @_; |
38 |
+ |
39 |
+ my $curl = WWW::Curl::Easy->new; |
40 |
+ |
41 |
+ $curl->setopt(CURLOPT_HEADER, 0); |
42 |
+ $curl->setopt(CURLOPT_CONNECTTIMEOUT, $timeout); |
43 |
+ $curl->setopt(CURLOPT_TIMEOUT, $timeout); |
44 |
+ $curl->setopt(CURLOPT_FTP_USE_EPSV, 1); |
45 |
+ $curl->setopt(CURLOPT_URL, $url); |
46 |
+ |
47 |
+ # A filehandle, reference to a scalar or reference to a typeglob can be used here. |
48 |
+ my $response_body; |
49 |
+ $curl->setopt(CURLOPT_WRITEDATA,\$response_body); |
50 |
+ |
51 |
+ # Starts the actual request |
52 |
+ my $retcode = $curl->perform; |
53 |
+ |
54 |
+ # Looking at the results... |
55 |
+ if ($retcode == 0) { |
56 |
+ #print("Transfer went ok\n"); |
57 |
+ my $response_code = $curl->getinfo(CURLINFO_HTTP_CODE); |
58 |
+ # judge result and next action based on $response_code |
59 |
+ #print("Received response: $response_code $response_body\n"); |
60 |
+ chomp $response_body; |
61 |
+ #print("s='$response_body'\n"); |
62 |
+ print(munge_date($response_body), "\n"); |
63 |
+ } else { |
64 |
+ # Error code, type of error, error message |
65 |
+ #print("An error happened: $retcode ".$curl->strerror($retcode)." ".$curl->errbuf."\n"); |
66 |
+ exit 800; |
67 |
+ } |
68 |
+ |
69 |
+} |
70 |
+ |
71 |
sub handle_wget { |
72 |
my ( $timeout, $url ) = @_; |
73 |
# TODO: replace this with native HTTP |