From a3fae7c6dc93e61570eabc2242d8fefb9e737a41 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Tue, 24 May 2016 16:35:57 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9BAppe=E7=B1=BB=20Request?= =?UTF-8?q?=E5=92=8CRoute=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- base.php | 9 ---- library/think/App.php | 19 +++++--- library/think/Request.php | 74 +++++++++++++++++++++++++++++-- library/think/Response.php | 4 ++ library/think/Route.php | 42 ++++++------------ library/think/controller/Rest.php | 2 +- 6 files changed, 103 insertions(+), 47 deletions(-) diff --git a/base.php b/base.php index 41bdd7dd..118d3ac6 100644 --- a/base.php +++ b/base.php @@ -48,14 +48,5 @@ defined('CLASS_APPEND_SUFFIX') or define('CLASS_APPEND_SUFFIX', false); // 是 defined('APP_MODE') or define('APP_MODE', function_exists('saeAutoLoader') ? 'sae' : 'common'); // 环境常量 -define('IS_CGI', strpos(PHP_SAPI, 'cgi') === 0 ? 1 : 0); define('IS_WIN', strstr(PHP_OS, 'WIN') ? 1 : 0); define('IS_MAC', strstr(PHP_OS, 'Darwin') ? 1 : 0); -define('IS_CLI', PHP_SAPI == 'cli' ? 1 : 0); -define('IS_AJAX', (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') ? true : false); -define('NOW_TIME', $_SERVER['REQUEST_TIME']); -define('REQUEST_METHOD', IS_CLI ? 'GET' : $_SERVER['REQUEST_METHOD']); -define('IS_GET', REQUEST_METHOD == 'GET' ? true : false); -define('IS_POST', REQUEST_METHOD == 'POST' ? true : false); -define('IS_PUT', REQUEST_METHOD == 'PUT' ? true : false); -define('IS_DELETE', REQUEST_METHOD == 'DELETE' ? true : false); diff --git a/library/think/App.php b/library/think/App.php index 6120a7b0..6111d8cf 100644 --- a/library/think/App.php +++ b/library/think/App.php @@ -28,7 +28,6 @@ use think\Route; */ class App { - /** * 执行应用程序 * @access public @@ -40,6 +39,18 @@ class App { is_null($request) && $request = Request::instance(); + define('NOW_TIME', $request->time()); + define('REQUEST_METHOD', $request->method()); + define('HTTP_HOST', $request->host()); + define('IS_GET', REQUEST_METHOD == 'GET' ? true : false); + define('IS_POST', REQUEST_METHOD == 'POST' ? true : false); + define('IS_PUT', REQUEST_METHOD == 'PUT' ? true : false); + define('IS_DELETE', REQUEST_METHOD == 'DELETE' ? true : false); + define('IS_AJAX', $request->isAjax()); + define('__EXT__', $request->ext()); + define('IS_CGI', $request->isCgi()); + define('IS_CLI', $request->isCli()); + // 初始化应用(公共模块) $config = self::initModule(COMMON_MODULE, Config::get()); @@ -343,12 +354,8 @@ class App */ public static function route($request, array $config) { - - define('__INFO__', $request->pathinfo()); - define('__EXT__', $request->ext()); - // 检测URL禁用后缀 - if ($config['url_deny_suffix'] && preg_match('/\.(' . $config['url_deny_suffix'] . ')$/i', __INFO__)) { + if ($config['url_deny_suffix'] && preg_match('/\.(' . $config['url_deny_suffix'] . ')$/i', $request->pathinfo())) { throw new Exception('url suffix deny'); } diff --git a/library/think/Request.php b/library/think/Request.php index 3aff5d89..a63493fc 100644 --- a/library/think/Request.php +++ b/library/think/Request.php @@ -21,6 +21,7 @@ class Request */ protected static $instance; + protected $method; /** * @var string 域名 */ @@ -406,11 +407,78 @@ class Request /** * 当前的请求类型 * @access public + * @param string $method 请求类型 * @return string */ - public function method() + public function method($method = '') { - return IS_CLI ? 'GET' : (isset($this->server['REQUEST_METHOD']) ? $this->server['REQUEST_METHOD'] : $_SERVER['REQUEST_METHOD']); + if ($method) { + $this->method = $method; + return; + } elseif (!$this->method) { + $this->method = IS_CLI ? 'GET' : (isset($this->server['REQUEST_METHOD']) ? $this->server['REQUEST_METHOD'] : $_SERVER['REQUEST_METHOD']); + } + return $this->method; + } + + /** + * 是否为GET请求 + * @access public + * @return bool + */ + public function isGet() + { + return $this->method() == 'GET'; + } + + /** + * 是否为POST请求 + * @access public + * @return bool + */ + public function isPost() + { + return $this->method() == 'POST'; + } + + /** + * 是否为PUT请求 + * @access public + * @return bool + */ + public function isPut() + { + return $this->method() == 'PUT'; + } + + /** + * 是否为DELTE请求 + * @access public + * @return bool + */ + public function isDelete() + { + return $this->method() == 'DELETE'; + } + + /** + * 是否为cli + * @access public + * @return bool + */ + public function isCli() + { + return PHP_SAPI == 'cli'; + } + + /** + * 是否为cgi + * @access public + * @return bool + */ + public function isCgi() + { + return strpos(PHP_SAPI, 'cgi') === 0; } /** @@ -649,7 +717,7 @@ class Request */ public function host() { - return $this->server('SERVER_NAME'); + return $this->server('HTTP_HOST'); } /** diff --git a/library/think/Response.php b/library/think/Response.php index be0146aa..57aa426e 100644 --- a/library/think/Response.php +++ b/library/think/Response.php @@ -99,6 +99,10 @@ class Response // 处理输出数据 $data = $this->output($data); + + // 监听response_data + APP_HOOK && Hook::listen('response_data', $data); + // 发送头部信息 if (!headers_sent() && !empty($this->header)) { // 发送状态码 diff --git a/library/think/Route.php b/library/think/Route.php index a9c86a3f..55ca38ff 100644 --- a/library/think/Route.php +++ b/library/think/Route.php @@ -252,7 +252,7 @@ class Route * 注册路由分组 * @access public * @param string|array $name 分组名称或者参数 - * @param array $routes 路由地址 + * @param array|\Closure $routes 路由地址 * @param array $option 路由参数 * @param string $type 请求类型 * @param array $pattern 变量规则 @@ -493,16 +493,16 @@ class Route $rules = self::$domain; // 开启子域名部署 支持二级和三级域名 if (!empty($rules)) { - if (isset($rules[$_SERVER['HTTP_HOST']])) { + if (isset($rules[HTTP_HOST])) { // 完整域名或者IP配置 - $rule = $rules[$_SERVER['HTTP_HOST']]; + $rule = $rules[HTTP_HOST]; } else { $rootDomain = Config::get('url_domain_root'); if ($rootDomain) { // 配置域名根 例如 thinkphp.cn 163.com.cn 如果是国家级域名 com.cn net.cn 之类的域名需要配置 - $domain = explode('.', rtrim(stristr($_SERVER['HTTP_HOST'], $rootDomain, true), '.')); + $domain = explode('.', rtrim(stristr(HTTP_HOST, $rootDomain, true), '.')); } else { - $domain = explode('.', $_SERVER['HTTP_HOST'], -2); + $domain = explode('.', HTTP_HOST, -2); } // 子域名配置 if (!empty($domain)) { @@ -586,7 +586,7 @@ class Route { // 检测域名部署 if ($checkDomain) { - self::checkDomain(); + self::checkDomain($request); } // 分隔符替换 确保路由定义使用统一的分隔符 @@ -625,7 +625,7 @@ class Route $pattern = isset($val['pattern']) ? $val['pattern'] : []; // 参数有效性检查 - if (!self::checkOption($option, $url)) { + if (!self::checkOption($option, $url, $request)) { continue; } @@ -645,12 +645,12 @@ class Route $key = array_shift($route); } - $key = $rule . '/' . $key; + $key = $rule . ($key ? '/' . $key : ''); // 检查规则路由 if (is_array($route)) { $option1 = $route[1]; // 检查参数有效性 - if (!self::checkOption($option1, $url)) { + if (!self::checkOption($option1, $url, $rquest)) { continue; } $pattern = array_merge($pattern, isset($route[2]) ? $route[2] : []); @@ -683,7 +683,7 @@ class Route // 执行闭包 return ['type' => 'function', 'function' => $miss, 'params' => []]; } - if (self::checkOption($miss['option'], $url)) { + if (self::checkOption($miss['option'], $url, $request)) { return self::parseRule('', $miss['route'], $url, []); } } @@ -741,15 +741,16 @@ class Route * @access private * @param array $option 路由参数 * @param string $url URL地址 + * @param \think\Request $request Request对象 * @return bool */ - private static function checkOption($option, $url) + private static function checkOption($option, $url, $request) { // 请求类型检测 if ((isset($option['method']) && false === stripos($option['method'], REQUEST_METHOD)) || (isset($option['ext']) && false === stripos($option['ext'], __EXT__)) // 伪静态后缀检测 - || (isset($option['domain']) && !in_array($option['domain'], [$_SERVER['HTTP_HOST'], self::$subDomain])) // 域名检测 - || (!empty($option['https']) && !self::isSsl()) // https检测 + || (isset($option['domain']) && !in_array($option['domain'], [HTTP_HOST, self::$subDomain])) // 域名检测 + || (!empty($option['https']) && !$request->isSsl()) // https检测 || (!empty($option['before_behavior']) && false === Hook::exec($option['before_behavior'], $url)) // 行为检测 || (!empty($option['callback']) && is_callable($option['callback']) && false === call_user_func($option['callback'])) // 自定义检测 ) { @@ -817,21 +818,6 @@ class Route return false; } - /** - * 判断是否SSL协议 - * @return boolean - */ - public static function isSsl() - { - if (isset($_SERVER['HTTPS']) && ('1' == $_SERVER['HTTPS'] || 'on' == strtolower($_SERVER['HTTPS']))) { - return true; - } elseif (isset($_SERVER['SERVER_PORT']) && ('443' == $_SERVER['SERVER_PORT'])) { - return true; - } else { - return false; - } - } - /** * 解析模块的URL地址 [模块/控制器/操作?]参数1=值1&参数2=值2... * @access public diff --git a/library/think/controller/Rest.php b/library/think/controller/Rest.php index 0a614e4a..e88826d9 100644 --- a/library/think/controller/Rest.php +++ b/library/think/controller/Rest.php @@ -46,7 +46,7 @@ abstract class Rest $this->_type = __EXT__; } // 请求方式检测 - $method = strtolower($_SERVER['REQUEST_METHOD']); + $method = strtolower(REQUEST_METHOD); if (false === stripos($this->restMethodList, $method)) { // 请求方式非法 则用默认请求方法 $method = $this->restDefaultMethod;