Add support of response headers

This commit is contained in:
Beu 2022-04-14 12:42:30 +02:00
parent 7b10300713
commit bbec986c22
2 changed files with 28 additions and 4 deletions

View File

@ -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();

View File

@ -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;
}
}