Ejemplo n.º 1
0
 /**
  * HTTP 接口请求处理的方法
  *
  * @param \Swoole\Http\Request $request
  * @param \Swoole\Http\Response $response
  */
 public function onRequest($request, $response)
 {
     $this->request = $request;
     $this->response = $response;
     $response->header('Content-Type', 'application/json');
     $response->end('{"status":"error","code":0,"msg":"verify fail."}');
     try {
         if (!$this->verify($request)) {
             $response->status(401);
         }
         $uri = $this->uri();
         $file = __DIR__ . '/../../../../api/' . $uri . (substr($uri, -1) === '/' ? 'index' : '') . '.php';
         $this->debug("request api: {$file}");
         if (!is_file($file)) {
             throw new \Exception('can not found api', 1);
         }
         $rs = (include $file);
         if (!$rs) {
             throw new \Exception('api result empty', 2);
         } elseif (is_string($rs)) {
             $rs = ['data' => $rs, 'status' => 'success'];
         } elseif (!is_array($rs)) {
             $rs = (array) $rs;
         }
         if (!isset($rs['status'])) {
             $rs['status'] = 'success';
         }
         $response->end(json_encode($rs, JSON_UNESCAPED_UNICODE));
     } catch (\Exception $e) {
         $response->status(500);
         $response->end(json_encode(['status' => 'error', 'code' => -$e->getCode(), 'msg' => $e->getMessage()], JSON_UNESCAPED_UNICODE));
     }
     $this->request = null;
     $this->response = null;
 }
Ejemplo n.º 2
0
 /**
  * HTTP 接口请求处理的方法
  *
  * @param \Swoole\Http\Request $request
  * @param \Swoole\Http\Response $response
  */
 public function onRequest($request, $response)
 {
     # 发送一个头信息
     $response->header('Server', self::$config['server']['name'] ?: 'MQSRV');
     if (isset(self::$workers['API'])) {
         /**
          * @var WorkerAPI $worker
          */
         $worker = self::$workers['API'];
         if ($worker->isApi($request)) {
             $worker->onRequest($request, $response);
             return;
         }
     }
     if (isset(self::$workers['Manager'])) {
         /**
          * @var WorkerManager $worker
          */
         $worker = self::$workers['Manager'];
         if ($worker->isManager($request)) {
             $worker->onRequest($request, $response);
             return;
         }
     }
     self::$worker->onRequest($request, $response);
 }
Ejemplo n.º 3
0
 /**
  * 发送响应
  * @param $request Request
  * @param $response Response
  * @return bool
  */
 protected function response(Request $request, Response $response)
 {
     if (!isset($response->head['Date'])) {
         $response->head['Date'] = gmdate("D, d M Y H:i:s T");
     }
     if (!isset($response->head['Connection'])) {
         //keepalive
         if ($this->keepalive && (isset($request->head['Connection']) && strtolower($request->head['Connection']) == 'keep-alive')) {
             $response->head['KeepAlive'] = 'on';
             $response->head['Connection'] = 'keep-alive';
         } else {
             $response->head['KeepAlive'] = 'off';
             $response->head['Connection'] = 'close';
         }
     }
     //过期命中
     if ($this->expire and $response->http_status == 304) {
         $out = $response->getHeader();
         return $this->sw->send($request->fd, $out);
     }
     //压缩
     if ($this->gzip) {
         $response->head['Content-Encoding'] = 'deflate';
         $response->body = gzdeflate($response->body, (int) $this->config->get('server.gzip_level', 1));
     }
     $out = $response->getHeader() . $response->body;
     $ret = $this->sw->send($request->fd, $out);
     $this->afterResponse($request, $response);
     return $ret;
 }
Ejemplo n.º 4
0
 /**
  * 发生了http错误
  * @param                 $code
  * @param Swoole\Http\Response $response
  * @param string          $content
  */
 function httpError($code, Swoole\Http\Response $response, $content = '')
 {
     $response->send_http_status($code);
     $response->head['Content-Type'] = 'text/html';
     $response->body = Swoole\Error::info(Swoole\Http\Response::$HTTP_HEADERS[$code], "<p>{$content}</p><hr><address>" . self::SOFTWARE . " at {$this->server->host} Port {$this->server->port}</address>");
 }
Ejemplo n.º 5
0
 /**
  * 输出静态文件
  *
  * @param $uri
  * @param \Swoole\Http\Response $response
  */
 protected function assets($uri, $response)
 {
     $uri = str_replace(['\\', '../'], ['/', '/'], $uri);
     $rPos = strrpos($uri, '.');
     if (false === $rPos) {
         # 没有任何后缀
         $response->status(404);
         $response->end('assets not found');
         return;
     }
     $type = strtolower(substr($uri, $rPos + 1));
     $header = ['js' => 'application/x-javascript', 'css' => 'text/css', 'png' => 'image/png', 'jpg' => 'image/jpeg', 'jpeg' => 'image/jpeg', 'gif' => 'image/gif', 'json' => 'application/json', 'svg' => 'image/svg+xml', 'woff' => 'application/font-woff', 'woff2' => 'application/font-woff2', 'ttf' => 'application/x-font-ttf', 'eot' => 'application/vnd.ms-fontobject', 'html' => 'text/html'];
     if (isset($header[$type])) {
         $response->header('Content-Type', $header[$type]);
     }
     $file = __DIR__ . '/../../../../assets/' . $uri;
     if (is_file($file)) {
         # 设置缓存头信息
         $time = 86400;
         $response->header('Cache-Control', 'max-age=' . $time);
         $response->header('Pragma', 'cache');
         $response->header('Last-Modified', date('D, d M Y H:i:s \\G\\M\\T', filemtime($file)));
         $response->header('Expires', date('D, d M Y H:i:s \\G\\M\\T', time() + $time));
         # 直接发送文件
         $response->sendfile($file);
     } else {
         $response->status(404);
         $response->end('assets not found');
     }
 }