From af423e5406b09b7cd717483fc26c6332c3cb1418 Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Fri, 7 Sep 2018 14:42:04 -0400 Subject: [PATCH] Update WireHttp to support more HTTP response codes (the success ones, in addition to the existing error codes) --- wire/core/PageFinder.php | 2 +- wire/core/WireHttp.php | 71 +++++++++++++++++++++++++++++++++++----- 2 files changed, 63 insertions(+), 10 deletions(-) diff --git a/wire/core/PageFinder.php b/wire/core/PageFinder.php index 568388ff..13576edb 100644 --- a/wire/core/PageFinder.php +++ b/wire/core/PageFinder.php @@ -1932,7 +1932,7 @@ class PageFinder extends Wire { $query->join($sql); } else { - $query->join("pages AS rootparent ON ($alias.parent_id=rootparent.id AND rootparent.id=1)"); + $query->join("pages AS rootparent$n ON ($alias.parent_id=rootparent$n.id AND rootparent$n.id=1)"); } $lastAlias = $alias; } diff --git a/wire/core/WireHttp.php b/wire/core/WireHttp.php index 2333b2a0..4d3a21cb 100644 --- a/wire/core/WireHttp.php +++ b/wire/core/WireHttp.php @@ -81,7 +81,29 @@ class WireHttp extends Wire { * @var array * */ - protected $errorCodes = array( + protected $httpCodes = array( + 100 => 'Continue', + 101 => 'Switching Protocols', + 102 => 'Processing (WebDAV; RFC 2518)', + 200 => 'OK', + 201 => 'Created', + 202 => 'Accepted', + 203 => 'Non-Authoritative Information', + 204 => 'No Content', + 205 => 'Reset Content', + 206 => 'Partial Content', + 207 => 'Multi-Status (WebDAV; RFC 4918)', + 208 => 'Already Reported (WebDAV; RFC 5842)', + 226 => 'IM Used (RFC 3229)', + 300 => 'Multiple Choices', + 301 => 'Moved Permanently', + 302 => 'Found', + 303 => 'See Other', + 304 => 'Not Modified', + 305 => 'Use Proxy', + 306 => 'Switch Proxy', + 307 => 'Temporary Redirect', + 308 => 'Permanent Redirect', 400 => 'Bad Request', 401 => 'Unauthorized', 402 => 'Payment Required', @@ -319,8 +341,9 @@ class WireHttp extends Wire { * */ public function head($url, $data = array()) { - $responseHeader = $this->send($url, $data, 'HEAD'); - return is_array($responseHeader) ? $responseHeader : false; + $this->send($url, $data, 'HEAD'); + $responseHeaders = $this->getResponseHeaders(); + return is_array($responseHeaders) ? $responseHeaders : false; } /** @@ -489,7 +512,7 @@ class WireHttp extends Wire { } else { $code = $this->getHttpCode(); - if($code && isset($this->errorCodes[$code])) { + if($code && $code >= 400 && isset($this->httpCodes[$code])) { // known http error code, no need to fallback to sockets $result = false; } else if($code && $code >= 200 && $code < 300) { @@ -713,7 +736,7 @@ class WireHttp extends Wire { fclose($fp); $methods = implode(", ", $triedMethods); - if(count($this->error) || isset($this->errorCodes[$this->httpCode])) { + if(count($this->error) || ($this->httpCode >= 400 && isset($this->httpCodes[$this->httpCode]))) { unlink($toFile); $error = $this->_('File could not be downloaded') . ' ' . htmlentities("($fromURL) ") . $this->getError() . " (tried: $methods)"; throw new WireException($error); @@ -919,7 +942,9 @@ class WireHttp extends Wire { $this->httpCode = (int) $httpCode; $this->httpCodeText = $httpText; - if(isset($this->errorCodes[$this->httpCode])) $this->error[] = $this->errorCodes[$this->httpCode]; + if($this->httpCode >= 400 && isset($this->httpCodes[$this->httpCode])) { + $this->error[] = $this->httpCodes[$this->httpCode]; + } // parsed version $this->responseHeaders = array(); @@ -1080,8 +1105,8 @@ class WireHttp extends Wire { */ public function getError($getArray = false) { $error = $getArray ? $this->error : implode(', ', $this->error); - if(isset($this->errorCodes[$this->httpCode])) { - $httpError = "$this->httpCode " . $this->errorCodes[$this->httpCode]; + if($this->httpCode >= 400 && isset($this->httpCodes[$this->httpCode])) { + $httpError = "$this->httpCode " . $this->httpCodes[$this->httpCode]; if($getArray) { array_unshift($error, $httpError); } else { @@ -1103,6 +1128,30 @@ class WireHttp extends Wire { return $this->httpCode; } + /** + * Return array of all possible HTTP codes as (code => description) + * + * @return array + * + */ + public function getHttpCodes() { + return $this->httpCodes; + } + + /** + * Return array of all possible HTTP success codes as (code => description) + * + * @return array + * + */ + public function getSuccessCodes() { + $codes = array(); + foreach($this->httpCodes as $code => $text) { + if($code < 400) $codes[$code] = $text; + } + return $codes; + } + /** * Return array of all possible HTTP error codes as (code => description) * @@ -1110,7 +1159,11 @@ class WireHttp extends Wire { * */ public function getErrorCodes() { - return $this->errorCodes; + $errorCodes = array(); + foreach($this->httpCodes as $code => $text) { + if($code >= 400) $errorCodes[$code] = $text; + } + return $errorCodes; } /**