From 55597357c715484494bc7ef58ee36a6d0e5b63f6 Mon Sep 17 00:00:00 2001 From: Christian Schneider Date: Thu, 17 Oct 2019 16:46:36 +0200 Subject: Fix ->is_reachable to use get() instead of fopen to use HTTP/1.1 and add tests --- it_url.class | 15 +++------------ test/it_url.t | 5 +++++ 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/it_url.class b/it_url.class index d03ad07..c8fb796 100644 --- a/it_url.class +++ b/it_url.class @@ -71,19 +71,10 @@ function __construct($url = null) */ function is_reachable($timeout = 5) { - $result = false; - - if ($fp = @fsockopen($this->realhostname, $this->port, $dummy_errno, $dummy_errstr, $timeout)) - { - fputs($fp, "GET /$this->path HTTP/1.0\r\nHost: $this->realhostname\r\nUser-Agent: ITools\r\n\r\n"); - $line = fgets($fp, 128); - fclose($fp); + $url = new it_url($this->url); + @$url->get(['method' => 'HEAD', 'totaltimeout' => $timeout]); - #debug("it_url::is_reachable($this->rawurl: $line"); - $result = preg_match("#^$this->protocol/[^ ]+ +[23]#i", $line); - } - - return $result; + return $url->result >= 200 && $url->result < 400; } /** diff --git a/test/it_url.t b/test/it_url.t index 624b4bf..b265afa 100755 --- a/test/it_url.t +++ b/test/it_url.t @@ -118,13 +118,18 @@ is( $url = new it_url('http://www.gna.ch/'); +is((bool)$url->is_reachable(), true, "is_reachable($url->url)"); $page = $url->get(); ok( strpos($page, ''), # UTF8SAFE '$url->get with url in constructor' ); +$url = new it_url('http://www.search.ch/not_found'); +is((bool)$url->is_reachable(), false, "is_reachable($url->url)"); + $url = new it_url('http://bogus.url'); +is((bool)$url->is_reachable(), false, "is_reachable($url->url)"); $page = $url->get('http://www.gna.ch/'); ok( strpos($page, ''), # UTF8SAFE -- cgit v1.2.3