From bbec986c221c7d44655c59008936a57ef12ee382 Mon Sep 17 00:00:00 2001 From: beu Date: Thu, 14 Apr 2022 12:42:30 +0200 Subject: [PATCH] Add support of response headers --- core/Files/AsyncHttpRequest.php | 6 ++++-- libs/curl-easy/cURL/Response.php | 26 ++++++++++++++++++++++++-- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/core/Files/AsyncHttpRequest.php b/core/Files/AsyncHttpRequest.php index 92623040..ae188552 100644 --- a/core/Files/AsyncHttpRequest.php +++ b/core/Files/AsyncHttpRequest.php @@ -57,7 +57,8 @@ class AsyncHttpRequest implements UsageInformationAble { ->set(CURLOPT_USERAGENT, 'ManiaControl v' . ManiaControl::VERSION)// user-agent ->set(CURLOPT_RETURNTRANSFER, true)// ->set(CURLOPT_FOLLOWLOCATION, true)// support redirect - ->set(CURLOPT_SSL_VERIFYPEER, false); + ->set(CURLOPT_SSL_VERIFYPEER, false) + ->set(CURLOPT_HEADER, true); return $request; } @@ -121,8 +122,9 @@ class AsyncHttpRequest implements UsageInformationAble { $error = $event->response->getError()->getMessage(); } else { $content = $event->response->getContent(); + $headers = $event->response->getHeaders(); } - call_user_func($this->function, $content, $error); + call_user_func($this->function, $content, $error, $headers); }); $fileReader = $this->maniaControl->getFileReader(); diff --git a/libs/curl-easy/cURL/Response.php b/libs/curl-easy/cURL/Response.php index aa02af19..7c2f6d51 100644 --- a/libs/curl-easy/cURL/Response.php +++ b/libs/curl-easy/cURL/Response.php @@ -6,6 +6,7 @@ class Response protected $ch; protected $error; protected $content = null; + protected $headers; /** * Constructs response @@ -17,8 +18,19 @@ class Response { $this->ch = $request->getHandle(); - if (is_string($content)) { - $this->content = $content; + if ($content != null) { + $header_size = $this->getInfo(CURLINFO_HEADER_SIZE); + + foreach (explode("\r\n", substr($content, 0, $header_size)) as $value) { + if(false !== ($matches = explode(':', $value, 2))) { + if (count($matches) === 2) { + $headers_arr["{$matches[0]}"] = trim($matches[1]); + } + } + } + $this->headers = $headers_arr; + + $this->content = substr($content, $header_size);; } } @@ -76,4 +88,14 @@ class Response { return isset($this->error); } + + /** + * Returns headers of request + * + * @return array Headers + */ + public function getHeaders() + { + return $this->headers; + } }