| 1: | <?php |
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | namespace SimpleID\Protocols; |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | class HTTPResponse { |
| 30: | |
| 31: | private $isNetworkError = false; |
| 32: | |
| 33: | private $isHTTPError = false; |
| 34: | |
| 35: | |
| 36: | private $version; |
| 37: | |
| 38: | |
| 39: | private $responseCode = null; |
| 40: | |
| 41: | |
| 42: | private $body; |
| 43: | |
| 44: | |
| 45: | private $headers = []; |
| 46: | |
| 47: | |
| 48: | |
| 49: | |
| 50: | |
| 51: | |
| 52: | |
| 53: | |
| 54: | public function __construct($response) { |
| 55: | if ($response === false) { |
| 56: | $this->isNetworkError = true; |
| 57: | $this->isHTTPError = true; |
| 58: | return; |
| 59: | } |
| 60: | |
| 61: | $this->body = $response['body']; |
| 62: | $this->readHeaders($response['headers']); |
| 63: | } |
| 64: | |
| 65: | |
| 66: | |
| 67: | |
| 68: | |
| 69: | private function readHeaders($headers) { |
| 70: | |
| 71: | $status = array_shift($headers); |
| 72: | |
| 73: | |
| 74: | list($protocol, $code) = explode(' ', trim($status), 3); |
| 75: | $this->version = substr($protocol, strpos($protocol, '/') + 1); |
| 76: | $this->responseCode = $code; |
| 77: | |
| 78: | $valid_codes = [ |
| 79: | 100, 101, |
| 80: | 200, 201, 202, 203, 204, 205, 206, |
| 81: | 300, 301, 302, 303, 304, 305, 307, |
| 82: | 400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 414, 415, 416, 417, |
| 83: | 500, 501, 502, 503, 504, 505 |
| 84: | ]; |
| 85: | |
| 86: | |
| 87: | |
| 88: | if (!in_array($code, $valid_codes)) { |
| 89: | $this->responseCode = strval(floor(intval($code) / 100) * 100); |
| 90: | } |
| 91: | |
| 92: | $this->isHTTPError = !in_array($this->responseCode, [200, 304]); |
| 93: | |
| 94: | while ($headers) { |
| 95: | |
| 96: | |
| 97: | if (preg_match('@^HTTP/\d+(\.\d+)? \d{3}@', $headers[0])) { |
| 98: | $this->headers = []; |
| 99: | $this->readHeaders($headers); |
| 100: | return; |
| 101: | } |
| 102: | $field = array_shift($headers); |
| 103: | list($header, $value) = explode(':', trim($field), 2); |
| 104: | |
| 105: | |
| 106: | $header = self::httpCase($header); |
| 107: | |
| 108: | if (isset($this->headers[$header])) { |
| 109: | |
| 110: | |
| 111: | |
| 112: | $this->headers[$header] .= ','. trim($value); |
| 113: | } else { |
| 114: | $this->headers[$header] = trim($value); |
| 115: | } |
| 116: | } |
| 117: | } |
| 118: | |
| 119: | |
| 120: | |
| 121: | |
| 122: | |
| 123: | |
| 124: | public function isNetworkError() { |
| 125: | return $this->isNetworkError; |
| 126: | } |
| 127: | |
| 128: | |
| 129: | |
| 130: | |
| 131: | |
| 132: | |
| 133: | public function isHTTPError() { |
| 134: | return $this->isHTTPError; |
| 135: | } |
| 136: | |
| 137: | |
| 138: | |
| 139: | |
| 140: | |
| 141: | |
| 142: | public function getBody() { |
| 143: | return $this->body; |
| 144: | } |
| 145: | |
| 146: | |
| 147: | |
| 148: | |
| 149: | |
| 150: | |
| 151: | public function getResponseCode() { |
| 152: | return $this->responseCode; |
| 153: | } |
| 154: | |
| 155: | |
| 156: | |
| 157: | |
| 158: | |
| 159: | |
| 160: | public function getVersion() { |
| 161: | return $this->version; |
| 162: | } |
| 163: | |
| 164: | |
| 165: | |
| 166: | |
| 167: | |
| 168: | |
| 169: | |
| 170: | public function getHeader($header) { |
| 171: | return $this->headers[self::httpCase($header)]; |
| 172: | } |
| 173: | |
| 174: | |
| 175: | |
| 176: | |
| 177: | |
| 178: | |
| 179: | |
| 180: | public function hasHeader($header) { |
| 181: | return array_key_exists(self::httpCase($header), $this->headers); |
| 182: | } |
| 183: | |
| 184: | |
| 185: | |
| 186: | |
| 187: | |
| 188: | |
| 189: | |
| 190: | |
| 191: | |
| 192: | static public function httpCase($str) { |
| 193: | return implode('-', array_map('ucfirst', explode('-', strtolower($str)))); |
| 194: | } |
| 195: | } |
| 196: | |
| 197: | ?> |