1 <?php
2
3 /**
4 * ArangoDB PHP client: HTTP response
5 *
6 * @package triagens\ArangoDb
7 * @author Jan Steemann
8 * @copyright Copyright 2012, triagens GmbH, Cologne, Germany
9 */
10
11 namespace triagens\ArangoDb;
12
13 /**
14 * Container class for HTTP responses
15 *
16 * <br>
17 *
18 * @package triagens\ArangoDb
19 * @since 0.2
20 */
21 class HttpResponse
22 {
23 /**
24 * The header retrieved
25 *
26 * @var string
27 */
28 private $_header = '';
29
30 /**
31 * The body retrieved
32 *
33 * @var string
34 */
35 private $_body = '';
36
37 /**
38 * All headers retrieved as an assoc array
39 *
40 * @var array
41 */
42 private $_headers = [];
43
44 /**
45 * The result status-line (first line of HTTP response header)
46 *
47 * @var string
48 */
49 private $_result = '';
50
51 /**
52 * The HTTP status code of the response
53 *
54 * @var int
55 */
56 private $_httpCode;
57
58 /**
59 * Whether or not the response is for an async request without a response body
60 *
61 * @var bool
62 */
63 private $_wasAsync = false;
64
65 /**
66 * Whether or not the response is for an async request without a response body
67 *
68 * @var Batchpart
69 */
70 private $batchPart;
71
72 /**
73 * HTTP location header
74 */
75 const HEADER_LOCATION = 'location';
76
77 /**
78 * Set up the response
79 *
80 *
81 * @param string $responseString - the complete HTTP response as supplied by the server
82 * @param string $originUrl The original URL the response is coming from
83 * @param string $originMethod The HTTP method that was used when sending data to the origin URL
84 *
85 * @param bool $wasAsync
86 *
87 * @throws ClientException
88 */
89 public function __construct($responseString, $originUrl = null, $originMethod = null, $wasAsync = false)
90 {
91 $this->_wasAsync = $wasAsync;
92
93 list($this->_header, $this->_body) = HttpHelper::parseHttpMessage($responseString, $originUrl, $originMethod);
94 list($this->_httpCode, $this->_result, $this->_headers) = HttpHelper::parseHeaders($this->_header);
95
96 if ($this->_body === null &&
97 ($this->_httpCode !== 204 && $this->_httpCode !== 304 && !$wasAsync)
98 ) {
99 // got no response body!
100 if ($originUrl !== null && $originMethod !== null) {
101 if ($responseString === '') {
102 throw new ClientException(
103 'Got no response from the server after request to '
104 . $originMethod . ' ' . $originUrl . ' - Note: this may be a timeout issue'
105 );
106 }
107 throw new ClientException(
108 'Got an invalid response from the server after request to '
109 . $originMethod . ' ' . $originUrl
110 );
111 }
112
113 throw new ClientException('Got an invalid response from the server');
114 }
115 }
116
117 /**
118 * Return the HTTP status code of the response
119 *
120 * @return int - HTTP status code of response
121 */
122 public function getHttpCode()
123 {
124 return $this->_httpCode;
125 }
126
127 /**
128 * Return an individual HTTP headers of the response
129 *
130 * @param string $name - name of header
131 *
132 * @return string - header value, NULL if header wasn't set in response
133 */
134 public function getHeader($name)
135 {
136 assert(is_string($name));
137
138 $name = strtolower($name);
139
140 if (isset($this->_headers[$name])) {
141 return $this->_headers[$name];
142 }
143
144 return null;
145 }
146
147 /**
148 * Return the HTTP headers of the response
149 *
150 * @return array - array of all headers with values
151 */
152 public function getHeaders()
153 {
154 return $this->_headers;
155 }
156
157 /**
158 * Return the location HTTP header of the response
159 *
160 * @return string - header value, NULL is header wasn't set in response
161 */
162 public function getLocationHeader()
163 {
164 return $this->getHeader(self::HEADER_LOCATION);
165 }
166
167 /**
168 * Return the body of the response
169 *
170 * @return string - body of the response
171 */
172 public function getBody()
173 {
174 return $this->_body;
175 }
176
177 /**
178 * Return the result line (first header line) of the response
179 *
180 * @return string - the result line (first line of header)
181 */
182 public function getResult()
183 {
184 return $this->_result;
185 }
186
187 /**
188 * Return the data from the JSON-encoded body
189 *
190 * @throws ClientException
191 * @return array - array of values from the JSON-encoded response body
192 */
193 public function getJson()
194 {
195 $body = $this->getBody();
196 $json = json_decode($body, true);
197
198 if (!is_array($json)) {
199 if ($this->_wasAsync) {
200 return [];
201 }
202
203 // should be an array, fail otherwise
204 throw new ClientException('Got a malformed result from the server');
205 }
206
207 return $json;
208 }
209
210 /**
211 * @param Batchpart $batchPart
212 *
213 * @return HttpResponse
214 */
215 public function setBatchPart($batchPart)
216 {
217 $this->batchPart = $batchPart;
218
219 return $this;
220 }
221
222 /**
223 * @return Batchpart
224 */
225 public function getBatchPart()
226 {
227 return $this->batchPart;
228 }
229
230 }
231