-
-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathResponse.php
167 lines (141 loc) · 4.11 KB
/
Response.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
namespace Office365\Runtime\Http;
class Response
{
public function __construct($content, $curlInfo, RequestOptions $requestOptions)
{
$this->Content = $content;
$this->CurlInfo = $curlInfo;
$this->RequestOptions = $requestOptions;
// validate the individual responses,
// so we can catch an instance where there is an unauthorized response.
$this->validate();
}
/**
* returns a Array including all response headers of
* this request. when RequestOptions::IncludeHeaders was not
* set to true this will be empty.
* @return array
*/
public function getHeaders()
{
if (!empty($this->Headers)) {
return $this->Headers;
}
// Headers where not included into the response.
if (!$this->RequestOptions->IncludeHeaders) {
return [];
}
$endOfHeaderPos = strpos($this->Content, "\r\n\r\n");
$lines = [];
if ($endOfHeaderPos === false) {
$lines = explode("\r\n", $this->Content);
} else {
$lines = explode("\r\n", substr($this->Content, 0, $endOfHeaderPos));
}
$this->Headers = [];
foreach ($lines as $line){
if($line){
list($k, $v) = preg_split("/[ :]/", $line, 2);
$this->Headers[strtolower(trim($k))] = trim($v);
}
}
return $this->Headers;
}
/**
* Returns a specific response header or null when he was not set.
* @param String $key
* @return String|null
*/
public function getHeader($key) {
$headers = $this->getHeaders();
return $headers[strtolower($key)] ?? null;
}
/**
* Returns the body of the response without the header part.
* @return String
*/
public function getBody() {
if ($this->RequestOptions->IncludeHeaders) {
// return body without header
$endOfHeaderPos = strpos($this->getContent(), "\r\n\r\n");
if ($endOfHeaderPos !== false) {
return substr($this->getContent(), $endOfHeaderPos + 4);
}
}
return $this->getContent();
}
/**
*
* @throws RequestException
*/
public function validate()
{
if ($this->getStatusCode() >= 400) {
if ($this->getHeader('Content-Length') !== null && (int)$this->getHeader('Content-Length') === 0) {
if ($this->getHeader('X-MSDAVEXT_Error')) {
$this->Content = urldecode($this->getHeader('X-MSDAVEXT_Error'));
}
throw new RequestException($this->Content, $this->getStatusCode());
}
}
}
/**
* returns the Content-Type of the response or null when not provided.
* @return String|null
*/
public function getContentType()
{
return $this->CurlInfo['content_type'] ?? null;
}
/**
* returns the array of curl_getinfo.
* Check https://www.php.net/manual/function.curl-getinfo.php for the available keys.
* @return array
*/
public function getCurlInfo()
{
return $this->CurlInfo ?? array();
}
/**
* returns the HTTP status code of the response
* @return Int|null
*/
public function getStatusCode()
{
return isset($this->CurlInfo['http_code']) ? (int)$this->CurlInfo['http_code'] : null;
}
/**
* returns the RequestOptions object which was used to perform the request.
* @return RequestOptions
*/
public function getRequestOptions()
{
return $this->RequestOptions;
}
/**
* returns the content of the response. When IncludeHeaders was set to true
* this will contain header and body.
* @return String
*/
public function getContent()
{
return $this->Content;
}
/**
* @var RequestOptions
*/
protected $RequestOptions;
/**
* @var array
*/
protected $CurlInfo;
/**
* @var mixed
*/
protected $Content;
/**
* @var array The parsed headers.
*/
protected $Headers = [];
}