1 | <?php |
||
4 | class Response |
||
5 | { |
||
6 | public $http_protocol = 'HTTP/1.1'; |
||
7 | public $http_status = 200; |
||
8 | |||
9 | public $header; |
||
10 | public $cookie; |
||
11 | public $content = ''; |
||
12 | |||
13 | public $isFastCgi = false; |
||
14 | |||
15 | static $HTTP_HEADERS = array( |
||
|
|||
16 | 100 => 'Continue', |
||
17 | 101 => 'Switching Protocols', |
||
18 | 102 => 'Processing', // RFC2518 |
||
19 | 200 => 'OK', |
||
20 | 201 => 'Created', |
||
21 | 202 => 'Accepted', |
||
22 | 203 => 'Non-Authoritative Information', |
||
23 | 204 => 'No Content', |
||
24 | 205 => 'Reset Content', |
||
25 | 206 => 'Partial Content', |
||
26 | 207 => 'Multi-Status', // RFC4918 |
||
27 | 208 => 'Already Reported', // RFC5842 |
||
28 | 226 => 'IM Used', // RFC3229 |
||
29 | 300 => 'Multiple Choices', |
||
30 | 301 => 'Moved Permanently', |
||
31 | 302 => 'Found', |
||
32 | 303 => 'See Other', |
||
33 | 304 => 'Not Modified', |
||
34 | 305 => 'Use Proxy', |
||
35 | 306 => 'Reserved', |
||
36 | 307 => 'Temporary Redirect', |
||
37 | 308 => 'Permanent Redirect', // RFC7238 |
||
38 | 400 => 'Bad Request', |
||
39 | 401 => 'Unauthorized', |
||
40 | 402 => 'Payment Required', |
||
41 | 403 => 'Forbidden', |
||
42 | 404 => 'Not Found', |
||
43 | 405 => 'Method Not Allowed', |
||
44 | 406 => 'Not Acceptable', |
||
45 | 407 => 'Proxy Authentication Required', |
||
46 | 408 => 'Request Timeout', |
||
47 | 409 => 'Conflict', |
||
48 | 410 => 'Gone', |
||
49 | 411 => 'Length Required', |
||
50 | 412 => 'Precondition Failed', |
||
51 | 413 => 'Request Entity Too Large', |
||
52 | 414 => 'Request-URI Too Long', |
||
53 | 415 => 'Unsupported Media Type', |
||
54 | 416 => 'Requested Range Not Satisfiable', |
||
55 | 417 => 'Expectation Failed', |
||
56 | 418 => 'I\'m a teapot', // RFC2324 |
||
57 | 422 => 'Unprocessable Entity', // RFC4918 |
||
58 | 423 => 'Locked', // RFC4918 |
||
59 | 424 => 'Failed Dependency', // RFC4918 |
||
60 | 425 => 'Reserved for WebDAV advanced collections expired proposal', // RFC2817 |
||
61 | 426 => 'Upgrade Required', // RFC2817 |
||
62 | 428 => 'Precondition Required', // RFC6585 |
||
63 | 429 => 'Too Many Requests', // RFC6585 |
||
64 | 431 => 'Request Header Fields Too Large', // RFC6585 |
||
65 | 500 => 'Internal Server Error', |
||
66 | 501 => 'Not Implemented', |
||
67 | 502 => 'Bad Gateway', |
||
68 | 503 => 'Service Unavailable', |
||
69 | 504 => 'Gateway Timeout', |
||
70 | 505 => 'HTTP Version Not Supported', |
||
71 | 506 => 'Variant Also Negotiates (Experimental)', // RFC2295 |
||
72 | 507 => 'Insufficient Storage', // RFC4918 |
||
73 | 508 => 'Loop Detected', // RFC5842 |
||
74 | 510 => 'Not Extended', // RFC2774 |
||
75 | 511 => 'Network Authentication Required', // RFC6585 |
||
76 | ); |
||
77 | |||
78 | protected $protocol; |
||
79 | public $request; |
||
80 | protected $gzip_level = false; |
||
81 | |||
82 | 8 | public function __construct($protocol, $request, $isFastCgi = false) |
|
83 | { |
||
84 | 8 | $this->protocol = $protocol; |
|
85 | 8 | $this->request = $request; |
|
86 | 8 | $this->request->response = $this; |
|
87 | 8 | $this->header = [ |
|
88 | 4 | 'Content-Type' => 'text/html', |
|
89 | ]; |
||
90 | 8 | } |
|
91 | |||
92 | /** |
||
93 | * 设置Http状态 |
||
94 | * @param $code |
||
95 | */ |
||
96 | 8 | public function status($code) |
|
100 | |||
101 | /** |
||
102 | * 设置Http头信息 |
||
103 | * @param $key |
||
104 | * @param $value |
||
105 | */ |
||
106 | 8 | public function header($key, $value) |
|
110 | |||
111 | /** |
||
112 | * 设置COOKIE |
||
113 | * @param $name |
||
114 | * @param null $value |
||
115 | * @param null $expire |
||
116 | * @param string $path |
||
117 | * @param null $domain |
||
118 | * @param null $secure |
||
119 | * @param null $httponly |
||
120 | */ |
||
121 | 4 | public function rawcookie($name, $value = null, $expire = null, $path = '/', $domain = null, $secure = null, $httponly = null) |
|
122 | { |
||
123 | 4 | $cookie = []; |
|
124 | 4 | foreach (['name', 'value', 'expire', 'path', 'domain', 'secure', 'httponly'] as $key) { |
|
125 | 4 | $cookie[$key] = $$key; |
|
126 | 2 | } |
|
127 | 4 | $this->cookie[] = $cookie; |
|
128 | 4 | } |
|
129 | |||
130 | /** |
||
131 | * 添加http header |
||
132 | * @param $header |
||
133 | */ |
||
134 | public function addHeaders(array $header) |
||
138 | |||
139 | 4 | public function getHeader() |
|
140 | { |
||
141 | 4 | $out = ''; |
|
142 | 4 | if ($this->isFastCgi) { |
|
143 | $status = isset(static::$HTTP_HEADERS[$this->http_status]) ? static::$HTTP_HEADERS[$this->http_status] : 'Undefined Status Code'; |
||
144 | $out .= 'Status: ' . $this->http_status . ' ' . $status . "\r\n"; |
||
145 | } else { |
||
146 | //Protocol |
||
147 | 4 | if (isset($this->header[0])) { |
|
148 | $out .= $this->header[0] . "\r\n"; |
||
149 | unset($this->header[0]); |
||
150 | } else { |
||
151 | 4 | $out = "HTTP/1.1 200 OK\r\n"; |
|
152 | } |
||
153 | } |
||
154 | 4 | if (!isset($this->header['Content-Length'])) { |
|
155 | $this->header['Content-Length'] = strlen($this->content); |
||
156 | } |
||
157 | |||
158 | //Headers |
||
159 | 4 | foreach ($this->header as $k => $v) { |
|
160 | 4 | $out .= $k . ': ' . $v . "\r\n"; |
|
161 | 2 | } |
|
162 | //Cookies |
||
163 | 4 | if (!empty($this->cookie) and is_array($this->cookie)) { |
|
164 | 4 | foreach ($this->cookie as $cookie) { |
|
165 | |||
166 | 4 | if ($cookie['value'] == null) { |
|
167 | $cookie['value'] = 'deleted'; |
||
168 | } |
||
169 | 4 | $value = "{$cookie['name']}={$cookie['value']}"; |
|
170 | 4 | if ($cookie['expire']) { |
|
171 | 4 | $value .= "; expires=" . date("D, d-M-Y H:i:s T", $cookie['expire']); |
|
172 | 2 | } |
|
173 | 4 | if ($cookie['path']) { |
|
174 | 4 | $value .= "; path={$cookie['path']}"; |
|
175 | 2 | } |
|
176 | 4 | if ($cookie['secure']) { |
|
177 | $value .= "; secure"; |
||
178 | } |
||
179 | 4 | if ($cookie['domain']) { |
|
180 | $value .= "; domain={$cookie['domain']}"; |
||
181 | } |
||
182 | 4 | if ($cookie['httponly']) { |
|
183 | 4 | $value .= '; httponly'; |
|
184 | 2 | } |
|
185 | |||
186 | 4 | $out .= "Set-Cookie: $value\r\n"; |
|
187 | 2 | } |
|
188 | 2 | } |
|
189 | //End |
||
190 | 4 | $out .= "\r\n"; |
|
191 | 4 | return $out; |
|
192 | } |
||
193 | |||
194 | public function noCache() |
||
199 | |||
200 | 4 | public function gzip($level) |
|
204 | |||
205 | 4 | public function getRawContent($content = '') |
|
206 | { |
||
207 | 4 | if($content) { |
|
221 | } |
||
222 |
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.