|
7 | 7 | */ |
8 | 8 | class Response |
9 | 9 | { |
10 | | - public $code, $body; |
11 | | - private $headers = array( |
12 | | - 'content-type' => 'text/html' |
13 | | - ); |
14 | | - |
15 | 10 | /** |
16 | 11 | * HTTP response code constant |
17 | 12 | */ |
18 | | - const OK = 200, |
19 | | - CREATED = 201, |
20 | | - NOCONTENT = 204, |
21 | | - MOVEDPERMANENTLY = 301, |
22 | | - FOUND = 302, |
23 | | - SEEOTHER = 303, |
24 | | - NOTMODIFIED = 304, |
25 | | - TEMPORARYREDIRECT = 307, |
26 | | - BADREQUEST = 400, |
27 | | - UNAUTHORIZED = 401, |
28 | | - FORBIDDEN = 403, |
29 | | - NOTFOUND = 404, |
30 | | - METHODNOTALLOWED = 405, |
31 | | - NOTACCEPTABLE = 406, |
32 | | - GONE = 410, |
33 | | - LENGTHREQUIRED = 411, |
34 | | - PRECONDITIONFAILED = 412, |
35 | | - UNSUPPORTEDMEDIATYPE = 415, |
36 | | - IMATEAPOT = 418, |
37 | | - ENHANCEYOURCALM = 420, |
38 | | - INTERNALSERVERERROR = 500; |
| 13 | + const |
| 14 | + HTTPCONTINUE = 100, |
| 15 | + SWITCHINGPROTOCOLS = 101, |
| 16 | + |
| 17 | + OK = 200, |
| 18 | + CREATED = 201, |
| 19 | + ACCEPTED = 202, |
| 20 | + NONAUTHORATIVEINFORMATION = 203, |
| 21 | + NOCONTENT = 204, |
| 22 | + RESETCONTENT = 205, |
| 23 | + PARTIALCONTENT = 206, |
| 24 | + |
| 25 | + MULTIPLECHOICES = 300, |
| 26 | + MOVEDPERMANENTLY = 301, |
| 27 | + FOUND = 302, |
| 28 | + SEEOTHER = 303, |
| 29 | + NOTMODIFIED = 304, |
| 30 | + USEPROXY = 305, |
| 31 | + TEMPORARYREDIRECT = 307, |
| 32 | + |
| 33 | + BADREQUEST = 400, |
| 34 | + UNAUTHORIZED = 401, |
| 35 | + PAYMENTREQUIRED = 402, |
| 36 | + FORBIDDEN = 403, |
| 37 | + NOTFOUND = 404, |
| 38 | + METHODNOTALLOWED = 405, |
| 39 | + NOTACCEPTABLE = 406, |
| 40 | + PROXYAUTHENTICATIONREQUIRED = 407, |
| 41 | + REQUESTTIMEOUT = 408, |
| 42 | + CONFLICT = 409, |
| 43 | + GONE = 410, |
| 44 | + LENGTHREQUIRED = 411, |
| 45 | + PRECONDITIONFAILED = 412, |
| 46 | + REQUESTENTITYTOOLARGE = 413, |
| 47 | + REQUESTURITOOLONG = 414, |
| 48 | + UNSUPPORTEDMEDIATYPE = 415, |
| 49 | + REQUESTEDRANGENOTSATISFIABLE = 416, |
| 50 | + EXPECTATIONFAILED = 417, |
| 51 | + IMATEAPOT = 418, // RFC2324 |
| 52 | + |
| 53 | + INTERNALSERVERERROR = 500, |
| 54 | + NOTIMPLEMENTED = 501, |
| 55 | + BADGATEWAY = 502, |
| 56 | + SERVICEUNAVAILABLE = 503, |
| 57 | + GATEWAYTIMEOUT = 504, |
| 58 | + HTTPVERSIONNOTSUPPORTED = 505; |
39 | 59 |
|
40 | 60 | /** |
41 | 61 | * Map of HTTP response codes |
42 | | - * @var str[] |
| 62 | + * RFC 2616, 2324 |
43 | 63 | */ |
44 | | - private $codes = array( |
45 | | - Response::OK => 'OK', |
46 | | - Response::CREATED => 'Created', |
47 | | - Response::NOCONTENT => 'No Content', |
48 | | - Response::MOVEDPERMANENTLY => 'Moved Permanently', |
49 | | - Response::FOUND => 'Found', |
50 | | - Response::SEEOTHER => 'See Other', |
51 | | - Response::NOTMODIFIED => 'Not Modified', |
52 | | - Response::TEMPORARYREDIRECT => 'Temporary Redirect', |
53 | | - Response::BADREQUEST => 'Bad Request', |
54 | | - Response::UNAUTHORIZED => 'Unauthorized', |
55 | | - Response::FORBIDDEN => 'Forbidden', |
56 | | - Response::NOTFOUND => 'Not Found', |
57 | | - Response::METHODNOTALLOWED => 'Method Not Allowed', |
58 | | - Response::NOTACCEPTABLE => 'Not Acceptable', |
59 | | - Response::GONE => 'Gone', |
60 | | - Response::LENGTHREQUIRED => 'Length Required', |
61 | | - Response::PRECONDITIONFAILED => 'Precondition Failed', |
62 | | - Response::UNSUPPORTEDMEDIATYPE => 'Unsupported Media Type', |
63 | | - Response::IMATEAPOT => 'I\'m A Teapot', |
64 | | - Response::UNSUPPORTEDMEDIATYPE => 'Enhance Your Calm', |
65 | | - Response::INTERNALSERVERERROR => 'Internal Server Error' |
| 64 | + protected $codes = array( |
| 65 | + Response::HTTPCONTINUE => 'Continue', |
| 66 | + Response::SWITCHINGPROTOCOLS => 'Switching Protocols', |
| 67 | + |
| 68 | + Response::OK => 'OK', |
| 69 | + Response::CREATED => 'Created', |
| 70 | + Response::ACCEPTED => 'Accepted', |
| 71 | + Response::NONAUTHORATIVEINFORMATION => 'Non-Authoritative Information', |
| 72 | + Response::NOCONTENT => 'No Content', |
| 73 | + Response::RESETCONTENT => 'Reset Content', |
| 74 | + Response::PARTIALCONTENT => 'Partial Content', |
| 75 | + |
| 76 | + Response::MULTIPLECHOICES => 'Multiple Choices', |
| 77 | + Response::MOVEDPERMANENTLY => 'Moved Permanently', |
| 78 | + Response::FOUND => 'Found', |
| 79 | + Response::SEEOTHER => 'See Other', |
| 80 | + Response::NOTMODIFIED => 'Not Modified', |
| 81 | + Response::USEPROXY => 'Use Proxy', |
| 82 | + Response::TEMPORARYREDIRECT => 'Temporary Redirect', |
| 83 | + |
| 84 | + Response::BADREQUEST => 'Bad Request', |
| 85 | + Response::UNAUTHORIZED => 'Unauthorized', |
| 86 | + Response::PAYMENTREQUIRED => 'Payment Required', |
| 87 | + Response::FORBIDDEN => 'Forbidden', |
| 88 | + Response::NOTFOUND => 'Not Found', |
| 89 | + Response::METHODNOTALLOWED => 'Method Not Allowed', |
| 90 | + Response::NOTACCEPTABLE => 'Not Acceptable', |
| 91 | + Response::PROXYAUTHENTICATIONREQUIRED => 'Proxy Authentication Required', |
| 92 | + Response::REQUESTTIMEOUT => 'Request Timeout', |
| 93 | + Response::CONFLICT => 'Conflict', |
| 94 | + Response::GONE => 'Gone', |
| 95 | + Response::LENGTHREQUIRED => 'Length Required', |
| 96 | + Response::PRECONDITIONFAILED => 'Precondition Failed', |
| 97 | + Response::REQUESTENTITYTOOLARGE => 'Request Entity Too Large', |
| 98 | + Response::REQUESTURITOOLONG => 'Request-URI Too Long', |
| 99 | + Response::UNSUPPORTEDMEDIATYPE => 'Unsupported Media Type', |
| 100 | + Response::REQUESTEDRANGENOTSATISFIABLE => 'Requested Range Not Satisfiable', |
| 101 | + Response::EXPECTATIONFAILED => 'Expectation Failed', |
| 102 | + Response::IMATEAPOT => 'I\'m a teapot', |
| 103 | + |
| 104 | + Response::INTERNALSERVERERROR => 'Internal Server Error', |
| 105 | + Response::NOTIMPLEMENTED => 'Not Implemented', |
| 106 | + Response::BADGATEWAY => 'Bad Gateway', |
| 107 | + Response::SERVICEUNAVAILABLE => 'Service Unavailable', |
| 108 | + Response::GATEWAYTIMEOUT => 'Gateway Timeout', |
| 109 | + Response::HTTPVERSIONNOTSUPPORTED => 'HTTP Version Not Supported', |
66 | 110 | ); |
67 | 111 |
|
68 | | - public function __construct($code = 204, $body = '') |
| 112 | + public |
| 113 | + $code = self::NOCONTENT, |
| 114 | + $body; |
| 115 | + |
| 116 | + protected |
| 117 | + $headers = array('content-type' => 'text/html'); |
| 118 | + |
| 119 | + public function __construct($code = null, $body = null) |
69 | 120 | { |
70 | | - $this->code = $code; |
71 | | - $this->body = $body; |
| 121 | + $code and $this->code = $code; |
| 122 | + $body and $this->body = $body; |
72 | 123 | } |
73 | 124 |
|
74 | 125 | /** |
@@ -102,12 +153,16 @@ protected function responseMessage() |
102 | 153 | return isset($this->codes[$this->code]) ? $this->codes[$this->code] : ''; |
103 | 154 | } |
104 | 155 |
|
| 156 | + protected function responseCode() { |
| 157 | + return $this->code; |
| 158 | + } |
| 159 | + |
105 | 160 | /** |
106 | 161 | * Output the response |
107 | 162 | */ |
108 | 163 | public function output() |
109 | 164 | { |
110 | | - header('HTTP/1.1 '.$this->code.' '.$this->responseMessage()); |
| 165 | + header('HTTP/1.1 '.$this->responseCode().' '.$this->responseMessage()); |
111 | 166 | foreach ($this->headers as $name => $value) { |
112 | 167 | header($name.': '.$value); |
113 | 168 | } |
|
0 commit comments