diff --git a/jumpapp/classes/Site.php b/jumpapp/classes/Site.php index d60f146..2deedfe 100644 --- a/jumpapp/classes/Site.php +++ b/jumpapp/classes/Site.php @@ -47,6 +47,7 @@ public function __construct(private Config $config, array $sitearray, private ar $this->iconname = $sitearray['icon'] ?? null; $this->tags = $sitearray['tags'] ?? $this->tags; $this->description = isset($sitearray['description']) ? $sitearray['description'] : $sitearray['name']; + $this->status = $sitearray['status'] ?? null; } /** diff --git a/jumpapp/classes/Status.php b/jumpapp/classes/Status.php index 33fa5c8..a7363f1 100644 --- a/jumpapp/classes/Status.php +++ b/jumpapp/classes/Status.php @@ -65,15 +65,25 @@ public function get_status(): string { * @return string */ private function do_request(): string { + // Grab some details if they exist from the site options. + $url = $this->site->status->url ?? $this->site->url; + $method = !in_array(($this->site->status->request_method ?? null), ['HEAD', 'GET']) ? 'HEAD' : $this->site->status->request_method; + // Try to make a request and see what we get back. try { - if ($this->client->request('HEAD', $this->site->url)) { + if ($this->client->request($method, $url)) { return self::STATUS_ONLINE; } } catch (\GuzzleHttp\Exception\ConnectException) { // Catch instances where we cant connect. return self::STATUS_OFFLINE; - } catch (\GuzzleHttp\Exception\BadResponseException) { - // Catch 4xx and 5xx errors. + } catch (\GuzzleHttp\Exception\BadResponseException $e) { + // This exception is thrown on 4xx and 5xx errors, however we want to ensure we dont + // show an error status in the UI if the response code is in the list of allowed codes. + // E.g. the server response with "418 I'm a teapot". + $status = $e->getResponse()->getStatusCode(); + if (in_array($status, ((array)$this->site->status->allowed_status_codes ?? []))) { + return self::STATUS_ONLINE; + } return self::STATUS_ERROR; } catch (\Exception) { // If anything went wrong or we had some other status code. diff --git a/jumpapp/sites/sites.json b/jumpapp/sites/sites.json index 215de26..06b7f1f 100644 --- a/jumpapp/sites/sites.json +++ b/jumpapp/sites/sites.json @@ -51,7 +51,18 @@ "name": "Pi-hole", "url" : "https://pi-hole.net/", "nofollow": false, - "tags": ["stuff", "things"] + "tags": ["home", "things"] + }, + { + "name": "Teapot", + "url" : "https://www.google.com/pagedoesnotexist", + "nofollow": false, + "tags": ["stuff", "things"], + "status": { + "allowed_status_codes": [418], + "request_method": "GET", + "url": "https://www.google.com/teapot" + } } ] }