From 818b78a42c7cef8c2a78527cf5e3cd06a5ef0667 Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Fri, 24 Jun 2022 13:30:22 -0400 Subject: [PATCH] Fix issue in WireHttp where it didn't reset the HTTP code description text (like "OK" or "Page Not Found" or "Internal Server Error" between multiple requests. --- wire/core/WireHttp.php | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/wire/core/WireHttp.php b/wire/core/WireHttp.php index 40bba0be..587087de 100644 --- a/wire/core/WireHttp.php +++ b/wire/core/WireHttp.php @@ -778,10 +778,10 @@ class WireHttp extends Wire { if($result === false) { $this->error[] = curl_error($curl); - $this->httpCode = 0; + $this->setHttpCode(0, ''); } else { $this->setResponseHeaderValues($responseHeaders); - $this->httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); + $this->setHttpCode(curl_getinfo($curl, CURLINFO_HTTP_CODE)); } curl_close($curl); @@ -1027,7 +1027,9 @@ class WireHttp extends Wire { } $result = curl_exec($curl); - if($result) $this->httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); + if($result) { + $this->setHttpCode(curl_getinfo($curl, CURLINFO_HTTP_CODE)); + } if($result === false) $this->error[] = curl_error($curl); curl_close($curl); @@ -1342,9 +1344,8 @@ class WireHttp extends Wire { $httpCode = (int) $httpCode; if(strlen($httpText)) $httpText = preg_replace('/[^-_.;() a-zA-Z0-9]/', ' ', $httpText); } - - $this->httpCode = (int) $httpCode; - $this->httpCodeText = $httpText; + + $this->setHttpCode((int) $httpCode, $httpText); if($this->httpCode >= 400 && isset($this->httpCodes[$this->httpCode])) { $this->error[] = $this->httpCodes[$this->httpCode]; @@ -1745,6 +1746,7 @@ class WireHttp extends Wire { $this->responseHeader = array(); $this->responseHeaders = array(); $this->httpCode = 0; + $this->httpCodeText = ''; $this->error = array(); } @@ -1792,6 +1794,19 @@ class WireHttp extends Wire { return $this->httpCode; } + /** + * Set http response code and text + * + * @param int $code + * @param string $text + * + */ + protected function setHttpCode($code, $text = '') { + if(empty($text)) $text = isset($this->httpCodes[$code]) ? $this->httpCodes[$code] : '?'; + $this->httpCode = $code; + $this->httpCodeText = $text; + } + /** * Return array of all possible HTTP codes as (code => description) *