CurlHttpClient.php
1 year ago
HttpClientInterface.php
1 year ago
Request.php
1 year ago
Response.php
1 year ago
Response.php
279 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WonderPush\Net; |
| 4 | |
| 5 | if (count(get_included_files()) === 1) { http_response_code(403); exit(); } // Prevent direct access |
| 6 | |
| 7 | /** |
| 8 | * Represents an HTTP response, with JSON parsing facility. |
| 9 | */ |
| 10 | class Response extends \WonderPush\Obj\BaseObject { |
| 11 | |
| 12 | /** |
| 13 | * The request |
| 14 | * @var Request |
| 15 | */ |
| 16 | private $request; |
| 17 | /** |
| 18 | * HTTP Status code. |
| 19 | * @var integer |
| 20 | */ |
| 21 | private $statusCode; |
| 22 | |
| 23 | /** |
| 24 | * HTTP headers. |
| 25 | * @var string[] |
| 26 | */ |
| 27 | private $headers; |
| 28 | |
| 29 | /** |
| 30 | * Raw HTTP body. |
| 31 | * @var string |
| 32 | */ |
| 33 | private $rawBody; |
| 34 | |
| 35 | /** |
| 36 | * Whether parsing has already taken place. |
| 37 | * @var boolean |
| 38 | */ |
| 39 | private $isParsed; |
| 40 | |
| 41 | /** |
| 42 | * JSON parsed body, or `null` on parsing errors. |
| 43 | * |
| 44 | * Uses objects instead of associative arrays to preserve `{}`/`[]` distinctions. |
| 45 | * |
| 46 | * @var object|null |
| 47 | */ |
| 48 | private $parsedBody; |
| 49 | |
| 50 | /** |
| 51 | * @var \WonderPush\Errors\Base |
| 52 | */ |
| 53 | private $parseError; |
| 54 | |
| 55 | /** |
| 56 | * @return Request |
| 57 | */ |
| 58 | public function getRequest() { |
| 59 | return $this->request; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * @param Request $request |
| 64 | * @return Response |
| 65 | */ |
| 66 | public function setRequest($request) { |
| 67 | $this->request = $request; |
| 68 | return $this; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * The HTTP Status code. |
| 73 | * @return integer |
| 74 | */ |
| 75 | public function getStatusCode() { |
| 76 | return $this->statusCode; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Set the HTTP Status code. |
| 81 | * @param integer $statusCode |
| 82 | * @return $this |
| 83 | */ |
| 84 | public function setStatusCode($statusCode) { |
| 85 | $this->statusCode = $statusCode; |
| 86 | return $this; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * The HTTP headers. |
| 91 | * @return string[] |
| 92 | */ |
| 93 | public function getHeaders() { |
| 94 | return $this->headers; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Set the HTTP headers. |
| 99 | * @param string[] $headers |
| 100 | * @return $this |
| 101 | */ |
| 102 | public function setHeaders($headers) { |
| 103 | $this->headers = $headers; |
| 104 | return $this; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * The raw HTTP body. |
| 109 | * @return string |
| 110 | */ |
| 111 | public function getRawBody() { |
| 112 | return $this->rawBody; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Set the raw HTTP body. |
| 117 | * @param string $rawBody |
| 118 | * @return $this |
| 119 | */ |
| 120 | public function setRawBody($rawBody) { |
| 121 | $this->rawBody = $rawBody; |
| 122 | |
| 123 | // Reset parsing info |
| 124 | $this->isParsed = null; |
| 125 | $this->parsedBody = null; |
| 126 | $this->parseError = null; |
| 127 | |
| 128 | return $this; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Parses the raw HTML body into JSON, and notes any parsing error. |
| 133 | */ |
| 134 | private function parseBody() { |
| 135 | if ($this->isParsed) { |
| 136 | return; |
| 137 | } |
| 138 | |
| 139 | // false body means no answer from server |
| 140 | if ($this->rawBody === false) { |
| 141 | $this->parsedBody = null; |
| 142 | $this->parseError = new \WonderPush\Errors\Network($this->request, $this); |
| 143 | $this->isParsed = true; |
| 144 | return; |
| 145 | } |
| 146 | |
| 147 | // Try json_decode |
| 148 | $this->parsedBody = json_decode($this->rawBody, false); |
| 149 | |
| 150 | // Manage json parsing errors |
| 151 | $jsonParseError = json_last_error(); |
| 152 | if (function_exists('json_last_error_msg')) { |
| 153 | // @codingStandardsIgnoreLine |
| 154 | $jsonParseErrorMsg = json_last_error_msg(); |
| 155 | } else { |
| 156 | switch ($this->parseError) { |
| 157 | case JSON_ERROR_NONE: |
| 158 | // https://github.com/php/php-src/blob/6053987bc27e8dede37f437193a5cad448f99bce/ext/json/tests/007.phpt |
| 159 | $jsonParseErrorMsg = 'No error'; |
| 160 | break; |
| 161 | case JSON_ERROR_DEPTH: |
| 162 | // https://github.com/php/php-src/blob/6053987bc27e8dede37f437193a5cad448f99bce/ext/json/tests/007.phpt |
| 163 | $jsonParseErrorMsg = 'Maximum stack depth exceeded'; |
| 164 | break; |
| 165 | case JSON_ERROR_STATE_MISMATCH: |
| 166 | // https://github.com/php/php-src/blob/6053987bc27e8dede37f437193a5cad448f99bce/ext/json/tests/007.phpt |
| 167 | $jsonParseErrorMsg = 'State mismatch (invalid or malformed JSON)'; |
| 168 | break; |
| 169 | case JSON_ERROR_CTRL_CHAR: |
| 170 | // https://github.com/php/php-src/blob/6053987bc27e8dede37f437193a5cad448f99bce/ext/json/tests/007.phpt |
| 171 | $jsonParseErrorMsg = 'Control character error, possibly incorrectly encoded'; |
| 172 | break; |
| 173 | case JSON_ERROR_SYNTAX: |
| 174 | // https://github.com/php/php-src/blob/6053987bc27e8dede37f437193a5cad448f99bce/ext/json/tests/007.phpt |
| 175 | $jsonParseErrorMsg = 'Syntax error'; |
| 176 | break; |
| 177 | // @codingStandardsIgnoreLine |
| 178 | case JSON_ERROR_UTF8: |
| 179 | // https://github.com/php/php-src/blob/6053987bc27e8dede37f437193a5cad448f99bce/ext/json/tests/bug54058.phpt |
| 180 | $jsonParseErrorMsg = 'Malformed UTF-8 characters, possibly incorrectly encoded'; |
| 181 | break; |
| 182 | default: |
| 183 | // On PHP 5.5.0 there were other codes, but fortunately, the json_last_error_msg() function exists |
| 184 | // so we won't end up here. |
| 185 | $jsonParseErrorMsg = 'Unknown error'; |
| 186 | break; |
| 187 | } |
| 188 | } |
| 189 | if ($jsonParseError !== JSON_ERROR_NONE) { |
| 190 | $this->parseError = new \WonderPush\Errors\Parsing($jsonParseError, $jsonParseErrorMsg . ' body:' . ($this->rawBody ?: '') . ' status:' . $this->statusCode . ' headers:' . ($this->headers ? json_encode($this->headers) : '')); |
| 191 | } |
| 192 | $this->isParsed = true; |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * The error code encountered while parsing the body, if any. |
| 197 | * @return \WonderPush\Errors\Base |
| 198 | */ |
| 199 | public function parseError() { |
| 200 | $this->parseBody(); |
| 201 | return $this->parseError; |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * The parsed body, or a {@link \WonderPush\Errors\Parsing}. |
| 206 | * @return mixed |
| 207 | */ |
| 208 | public function parsedBody() { |
| 209 | $this->parseBody(); |
| 210 | return $this->parsedBody; |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Returns an exception when there was an error making the call, or if the server returned an error response. |
| 215 | * @return null|\WonderPush\Errors\Base |
| 216 | */ |
| 217 | public function exception() { |
| 218 | $this->parseBody(); |
| 219 | |
| 220 | if ($this->parseError) { |
| 221 | return $this->parseError; |
| 222 | } |
| 223 | |
| 224 | $statusCode = $this->getStatusCode(); |
| 225 | $body = $this->parsedBody(); |
| 226 | |
| 227 | $error = false; // false if no exception, true to return one, or an actual \Exception for chaining |
| 228 | $errorMessage = null; |
| 229 | $errorCode = null; |
| 230 | |
| 231 | if (isset($body->error) && is_object($body->error)) { |
| 232 | |
| 233 | $error = true; |
| 234 | if (isset($body->error->message)) { |
| 235 | $errorMessage = $body->error->message; |
| 236 | } |
| 237 | if (isset($body->error->code )) { |
| 238 | $errorCode = $body->error->code; |
| 239 | } |
| 240 | //if (isset($body->error->status )) { |
| 241 | // $statusCode = $body->error->status; |
| 242 | //} |
| 243 | |
| 244 | } |
| 245 | |
| 246 | if ($statusCode < 200 || $statusCode >= 300) { |
| 247 | $error = true; |
| 248 | if ($errorMessage === null) { |
| 249 | $errorMessage = 'Non 2xx status code'; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | if ($error !== false) { |
| 254 | if ($error instanceof \Exception) { |
| 255 | return new \WonderPush\Errors\Server($this->request, $this, $errorMessage, $errorCode, $error); |
| 256 | } |
| 257 | return new \WonderPush\Errors\Server($this->request, $this, $errorMessage, $errorCode); |
| 258 | } |
| 259 | return null; |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * Returns the result instantiated with provided $cls or throws when request had error. |
| 264 | * @param $cls |
| 265 | * @return mixed |
| 266 | * @throws \WonderPush\Errors\Base |
| 267 | */ |
| 268 | public function checkedResult($cls, $key = null) { |
| 269 | $exception = $this->exception(); |
| 270 | if ($exception) { |
| 271 | throw $exception; |
| 272 | } |
| 273 | $this->parseBody(); |
| 274 | $body = $this->parsedBody(); |
| 275 | return new $cls($key ? $body->{$key} : $body); |
| 276 | } |
| 277 | |
| 278 | } |
| 279 |