- 本章继续学习函数 Response::getInstance()->sendHeaders()
// 源文件 /var/Typecho/Response.php
177 /**
178 * send all headers
179 */
180 public function sendHeaders()
181 {
182 if ($this->sandbox) {
183 return;
184 }
185
186 $sentHeaders = [];
187 foreach (headers_list() as $header) {
188 [$key] = explode(':', $header, 2);
189 $sentHeaders[] = strtolower(trim($key));
190 }
191
192 header('HTTP/1.1 ' . $this->status . ' ' . self::HTTP_CODE[$this->status], true, $this->status);
193
194 // set header
195 foreach ($this->headers as $name => $value) {
196 if (!in_array(strtolower($name), $sentHeaders)) {
197 header($name . ': ' . $value, true);
198 }
199 }
200
201 // set cookie
202 foreach ($this->cookies as $cookie) {
203 [$key, $value, $timeout, $path, $domain, $secure, $httponly] = $cookie;
204
205 if ($timeout > 0) {
206 $now = time();
207 $timeout += $timeout > $now - 86400 ? 0 : $now;
208 } elseif ($timeout < 0) {
209 $timeout = 1;
210 }
211
212 setrawcookie($key, rawurlencode($value), $timeout, $path, $domain, $secure, $httponly);
213 }
214 }
- 第182~184行:如果 $this->sandbox 的值为 true 则返回,否则继续执行函数体。
// 源文件 /var/Typecho/Response.php
112 /**
113 * @var bool
114 */
115 private $sandbox = false;
- 第115行:定义变量 $sandbox 默认值为 false 函数继续执行。
第186行:定义变量 $sentHeaders 默认值为一个空数组。第187~190行:foreach 循环,headers_list() 返回已发送的 HTTP 响应头(或准备发送的),explode(':', $header, 2)用 ':' 分割 $header 成为含有两个元素的数组并赋值给 [$key],strtolower(trim($key)) 把 $key 中的元素两边的空格修剪掉并且转换为小写字母然后赋值给 $sentHeaders[]第192行:header() 发送原生 HTTP 头,header("HTTP/1.1 200 OK",true,200)
第二个参数 true 表明是否用后面的头替换前面相同类型的头,第三个参数强制指定 HTTP 响应的值为 200 // 源文件 /var/Typecho/Response.php
102 /**
103 * @var int
104 */
105 private $status = 200;
- 第105行:定义变量 $status 默认值为 200
// 源文件 /var/Typecho/Response.php
/**
* http code
*
* @access private
* @var array
*/
23 private const HTTP_CODE = [
100 => 'Continue',
101 => 'Switching Protocols',
26 200 => 'OK',
201 => 'Created',
202 => 'Accepted',
203 => 'Non-Authoritative Information',
204 => 'No Content',
205 => 'Reset Content',
206 => 'Partial Content',
300 => 'Multiple Choices',
301 => 'Moved Permanently',
302 => 'Found',
303 => 'See Other',
304 => 'Not Modified',
305 => 'Use Proxy',
307 => 'Temporary Redirect',
400 => 'Bad Request',
401 => 'Unauthorized',
402 => 'Payment Required',
403 => 'Forbidden',
404 => 'Not Found',
405 => 'Method Not Allowed',
406 => 'Not Acceptable',
407 => 'Proxy Authentication Required',
408 => 'Request Timeout',
409 => 'Conflict',
410 => 'Gone',
411 => 'Length Required',
412 => 'Precondition Failed',
413 => 'Request Entity Too Large',
414 => 'Request-URI Too Long',
415 => 'Unsupported Media Type',
416 => 'Requested Range Not Satisfiable',
417 => 'Expectation Failed',
500 => 'Internal Server Error',
501 => 'Not Implemented',
502 => 'Bad Gateway',
503 => 'Service Unavailable',
504 => 'Gateway Timeout',
505 => 'HTTP Version Not Supported'
64 ];
- 第23-64行:定义了 self::HTTP_CODE[200] 的值为 'OK'
第195~199行:foreach 循环,in_array - 检查数组中是否存在某个值,代码实现了=>检查$this->headers数组中不存在$sentHeaders数组中的元素并且用header()方法进行发送。第202~213行:foreach 循环,对 cookie 的超时时间进行处理,然后发送未经 URL 编码的 cookie ,但是 cookie 中的值按照 RFC 3986 对 URL 进行编码。本章到此结束,下一章继续学习源文件 install.php