Пример #1
0
 /**
  * Do the handshake.
  *
  * @param   Swoole\Request $request
  * @param   Swoole\Response $response
  * @throws   \Exception
  * @return  bool
  */
 public function doHandshake(Swoole\Request $request, Swoole\Response $response)
 {
     if (!isset($request->head['Sec-WebSocket-Key'])) {
         $this->log('Bad protocol implementation: it is not RFC6455.');
         return false;
     }
     $key = $request->head['Sec-WebSocket-Key'];
     if (0 === preg_match('#^[+/0-9A-Za-z]{21}[AQgw]==$#', $key) || 16 !== strlen(base64_decode($key))) {
         $this->log('Header Sec-WebSocket-Key: $key is illegal.');
         return false;
     }
     /**
      * @TODO
      *   ? Origin;
      *   ? Sec-WebSocket-Protocol;
      *   ? Sec-WebSocket-Extensions.
      */
     $response->send_http_status(101);
     $response->addHeader(array('Upgrade' => 'websocket', 'Connection' => 'Upgrade', 'Sec-WebSocket-Accept' => base64_encode(sha1($key . static::GUID, true)), 'Sec-WebSocket-Version' => self::WEBSOCKET_VERSION));
     return true;
 }
 /**
  * 动态请求
  * @param $request
  * @param $response
  * @return unknown_type
  */
 function processDynamic(Swoole\Request $request, Swoole\Response $response)
 {
     $path = $this->document_root . '/' . $request->meta['path'];
     if (is_file($path)) {
         $request->setGlobal();
         $response->head['Content-Type'] = 'text/html';
         ob_start();
         try {
             include $path;
         } catch (\Exception $e) {
             $response->send_http_status(404);
             $response->body = $e->getMessage() . '!<br /><h1>' . self::SOFTWARE . '</h1>';
         }
         $response->body = ob_get_contents();
         ob_end_clean();
     } else {
         $this->httpError(404, $response, "页面不存在({$request->meta['path']})!");
     }
 }
Пример #3
0
 /**
  * 静态请求
  * @param $request
  * @param $response
  * @return unknown_type
  */
 function process_static($request, Swoole\Response $response)
 {
     $path = $this->document_root . '/' . $request->meta['path'];
     if (is_file($path)) {
         $read_file = true;
         if ($this->expire) {
             $expire = intval($this->config['server']['expire_time']);
             $fstat = stat($path);
             //过期控制信息
             if (isset($request->head['If-Modified-Since'])) {
                 $lastModifiedSince = strtotime($request->head['If-Modified-Since']);
                 if ($lastModifiedSince and $fstat['mtime'] <= $lastModifiedSince) {
                     //不需要读文件了
                     $read_file = false;
                     $response->send_http_status(304);
                 }
             } else {
                 $response->head['Cache-Control'] = "max-age={$expire}";
                 $response->head['Pragma'] = "max-age={$expire}";
                 $response->head['Last-Modified'] = date(self::DATE_FORMAT_HTTP, $fstat['mtime']);
                 $response->head['Expires'] = "max-age={$expire}";
             }
         }
         $ext_name = \Upload::file_ext($request->meta['path']);
         if ($read_file) {
             $response->head['Content-Type'] = $this->mime_types[$ext_name];
             $response->body = file_get_contents($path);
         }
         return true;
     } else {
         return false;
     }
 }