/** * 处理受到的数据 * @param TcpConnection $connection * @return void */ public function onConnect($connection) { $socket = $connection->getSocket(); $t_socket = new TSocket(); $t_socket->setHandle($socket); $transport_name = '\\Thrift\\Transport\\' . $this->thriftTransport; $transport = new $transport_name($t_socket); $protocol_name = '\\Thrift\\Protocol\\' . $this->thriftProtocol; $protocol = new $protocol_name($transport); // 执行处理 try { // 先初始化一个 $protocol->fname == 'none'; // 统计开始时间 StatisticClient::tick(); // 业务处理 $this->processor->process($protocol, $protocol); StatisticClient::report($this->name, $protocol->fname, 1, 0, '', $this->statisticAddress); } catch (\Exception $e) { StatisticClient::report($this->name, $protocol->fname, 0, $e->getCode(), $e, $this->statisticAddress); ThriftWorker::log('CODE:' . $e->getCode() . ' MESSAGE:' . $e->getMessage() . "\n" . $e->getTraceAsString() . "\nCLIENT_IP:" . $connection->getRemoteIp() . "\n"); $connection->send($e->getMessage()); } }
/** * 当接收到完整的http请求后的处理逻辑 * 1、如果请求的是以php为后缀的文件,则尝试加载 * 2、如果请求的url没有后缀,则尝试加载对应目录的index.php * 3、如果请求的是非php为后缀的文件,尝试读取原始数据并发送 * 4、如果请求的文件不存在,则返回404 * @param TcpConnection $connection * @param mixed $data * @return void */ public function onMessage($connection, $data) { // 请求的文件 $url_info = parse_url($_SERVER['REQUEST_URI']); if (!$url_info) { Http::header('HTTP/1.1 400 Bad Request'); return $connection->close('<h1>400 Bad Request</h1>'); } $path = $url_info['path']; $path_info = pathinfo($path); $extension = isset($path_info['extension']) ? $path_info['extension'] : ''; if ($extension === '') { $path = ($len = strlen($path)) && $path[$len - 1] === '/' ? $path . 'index.php' : $path . '/index.php'; $extension = 'php'; } $root_dir = isset($this->serverRoot[$_SERVER['HTTP_HOST']]) ? $this->serverRoot[$_SERVER['HTTP_HOST']] : current($this->serverRoot); $file = "{$root_dir}/{$path}"; // 对应的php文件不存在则直接使用根目录的index.php if ($extension === 'php' && !is_file($file)) { $file = "{$root_dir}/index.php"; if (!is_file($file)) { $file = "{$root_dir}/index.html"; $extension = 'html'; } } // 请求的文件存在 if (is_file($file)) { // 判断是否是站点目录里的文件 if (!($request_realpath = realpath($file)) || !($root_dir_realpath = realpath($root_dir)) || 0 !== strpos($request_realpath, $root_dir_realpath)) { Http::header('HTTP/1.1 400 Bad Request'); return $connection->close('<h1>400 Bad Request</h1>'); } $file = realpath($file); // 如果请求的是php文件 if ($extension === 'php') { $cwd = getcwd(); chdir($root_dir); ini_set('display_errors', 'off'); // 缓冲输出 ob_start(); // 载入php文件 try { // $_SERVER变量 $_SERVER['REMOTE_ADDR'] = $connection->getRemoteIp(); $_SERVER['REMOTE_PORT'] = $connection->getRemotePort(); include $file; } catch (\Exception $e) { // 如果不是exit if ($e->getMessage() != 'jump_exit') { echo $e; } } $content = ob_get_clean(); ini_set('display_errors', 'on'); $connection->close($content); chdir($cwd); return; } // 请求的是静态资源文件 if (isset(self::$mimeTypeMap[$extension])) { Http::header('Content-Type: ' . self::$mimeTypeMap[$extension]); } else { Http::header('Content-Type: ' . self::$defaultMimeType); } // 获取文件信息 $info = stat($file); $modified_time = $info ? date('D, d M Y H:i:s', $info['mtime']) . ' GMT' : ''; // 如果有$_SERVER['HTTP_IF_MODIFIED_SINCE'] if (!empty($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $info) { // 文件没有更改则直接304 if ($modified_time === $_SERVER['HTTP_IF_MODIFIED_SINCE']) { // 304 Http::header('HTTP/1.1 304 Not Modified'); // 发送给客户端 return $connection->close(''); } } if ($modified_time) { Http::header("Last-Modified: {$modified_time}"); } // 发送给客户端 return $connection->close(file_get_contents($file)); } else { // 404 Http::header("HTTP/1.1 404 Not Found"); return $connection->close('<html><head><title>404 页面不存在</title></head><body><center><h3>404 Not Found</h3></center></body></html>'); } }
/** * 当worker通过内部通讯端口连接到gateway时 * @param TcpConnection $connection */ public function onWorkerConnect($connection) { $connection->remoteAddress = $connection->getRemoteIp() . ':' . $connection->getRemotePort(); $this->_workerConnections[$connection->remoteAddress] = $connection; }
/** * Emit when http message coming. * @param TcpConnection $connection * @param mixed $data * @return void */ public function onMessage($connection, $data) { // REQUEST_URI. $url_info = parse_url($_SERVER['REQUEST_URI']); if (!$url_info) { Http::header('HTTP/1.1 400 Bad Request'); return $connection->close('<h1>400 Bad Request</h1>'); } $path = $url_info['path']; $path_info = pathinfo($path); $extension = isset($path_info['extension']) ? $path_info['extension'] : ''; if ($extension === '') { $path = ($len = strlen($path)) && $path[$len - 1] === '/' ? $path . 'index.php' : $path . '/index.php'; $extension = 'php'; } $root_dir = isset($this->serverRoot[$_SERVER['HTTP_HOST']]) ? $this->serverRoot[$_SERVER['HTTP_HOST']] : current($this->serverRoot); $file = "{$root_dir}/{$path}"; if ($extension === 'php' && !is_file($file)) { $file = "{$root_dir}/index.php"; if (!is_file($file)) { $file = "{$root_dir}/index.html"; $extension = 'html'; } } // File exsits. if (is_file($file)) { // Security check. if (!($request_realpath = realpath($file)) || !($root_dir_realpath = realpath($root_dir)) || 0 !== strpos($request_realpath, $root_dir_realpath)) { Http::header('HTTP/1.1 400 Bad Request'); return $connection->close('<h1>400 Bad Request</h1>'); } $file = realpath($file); // Request php file. if ($extension === 'php') { $cwd = getcwd(); chdir($root_dir); ini_set('display_errors', 'off'); ob_start(); // Try to include php file. try { // $_SERVER. $_SERVER['REMOTE_ADDR'] = $connection->getRemoteIp(); $_SERVER['REMOTE_PORT'] = $connection->getRemotePort(); include $file; } catch (\Exception $e) { // Jump_exit? if ($e->getMessage() != 'jump_exit') { echo $e; } } $content = ob_get_clean(); ini_set('display_errors', 'on'); $connection->close($content); chdir($cwd); return; } // Static resource file request. if (isset(self::$mimeTypeMap[$extension])) { Http::header('Content-Type: ' . self::$mimeTypeMap[$extension]); } else { Http::header('Content-Type: ' . self::$defaultMimeType); } // Get file stat. $info = stat($file); $modified_time = $info ? date('D, d M Y H:i:s', $info['mtime']) . ' GMT' : ''; if (!empty($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $info) { // Http 304. if ($modified_time === $_SERVER['HTTP_IF_MODIFIED_SINCE']) { // 304 Http::header('HTTP/1.1 304 Not Modified'); // Send nothing but http headers.. return $connection->close(''); } } if ($modified_time) { Http::header("Last-Modified: {$modified_time}"); } // Send to client. return $connection->close(file_get_contents($file)); } else { // 404 Http::header("HTTP/1.1 404 Not Found"); return $connection->close('<html><head><title>404 File not found</title></head><body><center><h3>404 Not Found</h3></center></body></html>'); } }