Exemple #1
0
 /**
  * Check the integrity of the package.
  *
  * @param string        $buffer
  * @param TcpConnection $connection
  * @return int
  */
 public static function input($buffer, TcpConnection $connection)
 {
     // Judge whether the package length exceeds the limit.
     if (strlen($buffer) >= TcpConnection::$maxPackageSize) {
         $connection->close();
         return 0;
     }
     //  Find the position of  "\n".
     $pos = strpos($buffer, "\n");
     // No "\n", packet length is unknown, continue to wait for the data so return 0.
     if ($pos === false) {
         return 0;
     }
     // Return the current package length.
     return $pos + 1;
 }
Exemple #2
0
 /**
  * Parse $_POST、$_GET、$_COOKIE.
  *
  * @param string        $recv_buffer
  * @param TcpConnection $connection
  * @return array
  */
 public static function decode($recv_buffer, TcpConnection $connection)
 {
     // Init.
     $_POST = $_GET = $_COOKIE = $_REQUEST = $_SESSION = $_FILES = array();
     $GLOBALS['HTTP_RAW_POST_DATA'] = '';
     // Clear cache.
     HttpCache::$header = array('Connection' => 'Connection: keep-alive');
     HttpCache::$instance = new HttpCache();
     // $_SERVER
     $_SERVER = array('QUERY_STRING' => '', 'REQUEST_METHOD' => '', 'REQUEST_URI' => '', 'SERVER_PROTOCOL' => '', 'SERVER_SOFTWARE' => 'workerman/3.0', 'SERVER_NAME' => '', 'HTTP_HOST' => '', 'HTTP_USER_AGENT' => '', 'HTTP_ACCEPT' => '', 'HTTP_ACCEPT_LANGUAGE' => '', 'HTTP_ACCEPT_ENCODING' => '', 'HTTP_COOKIE' => '', 'HTTP_CONNECTION' => '', 'REMOTE_ADDR' => '', 'REMOTE_PORT' => '0');
     // Parse headers.
     list($http_header, $http_body) = explode("\r\n\r\n", $recv_buffer, 2);
     $header_data = explode("\r\n", $http_header);
     list($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI'], $_SERVER['SERVER_PROTOCOL']) = explode(' ', $header_data[0]);
     $http_post_boundary = '';
     unset($header_data[0]);
     foreach ($header_data as $content) {
         // \r\n\r\n
         if (empty($content)) {
             continue;
         }
         list($key, $value) = explode(':', $content, 2);
         $key = strtolower($key);
         $value = trim($value);
         switch ($key) {
             // HTTP_HOST
             case 'host':
                 $_SERVER['HTTP_HOST'] = $value;
                 $tmp = explode(':', $value);
                 $_SERVER['SERVER_NAME'] = $tmp[0];
                 if (isset($tmp[1])) {
                     $_SERVER['SERVER_PORT'] = $tmp[1];
                 }
                 break;
                 // cookie
             // cookie
             case 'cookie':
                 $_SERVER['HTTP_COOKIE'] = $value;
                 parse_str(str_replace('; ', '&', $_SERVER['HTTP_COOKIE']), $_COOKIE);
                 break;
                 // user-agent
             // user-agent
             case 'user-agent':
                 $_SERVER['HTTP_USER_AGENT'] = $value;
                 break;
                 // accept
             // accept
             case 'accept':
                 $_SERVER['HTTP_ACCEPT'] = $value;
                 break;
                 // accept-language
             // accept-language
             case 'accept-language':
                 $_SERVER['HTTP_ACCEPT_LANGUAGE'] = $value;
                 break;
                 // accept-encoding
             // accept-encoding
             case 'accept-encoding':
                 $_SERVER['HTTP_ACCEPT_ENCODING'] = $value;
                 break;
                 // connection
             // connection
             case 'connection':
                 $_SERVER['HTTP_CONNECTION'] = $value;
                 break;
             case 'referer':
                 $_SERVER['HTTP_REFERER'] = $value;
                 break;
             case 'if-modified-since':
                 $_SERVER['HTTP_IF_MODIFIED_SINCE'] = $value;
                 break;
             case 'if-none-match':
                 $_SERVER['HTTP_IF_NONE_MATCH'] = $value;
                 break;
             case 'content-type':
                 if (!preg_match('/boundary="?(\\S+)"?/', $value, $match)) {
                     $_SERVER['CONTENT_TYPE'] = $value;
                 } else {
                     $_SERVER['CONTENT_TYPE'] = 'multipart/form-data';
                     $http_post_boundary = '--' . $match[1];
                 }
                 break;
         }
     }
     // Parse $_POST.
     if ($_SERVER['REQUEST_METHOD'] === 'POST') {
         if (isset($_SERVER['CONTENT_TYPE']) && $_SERVER['CONTENT_TYPE'] === 'multipart/form-data') {
             self::parseUploadFiles($http_body, $http_post_boundary);
         } else {
             parse_str($http_body, $_POST);
             // $GLOBALS['HTTP_RAW_POST_DATA']
             $GLOBALS['HTTP_RAW_POST_DATA'] = $http_body;
         }
     }
     // QUERY_STRING
     $_SERVER['QUERY_STRING'] = parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY);
     if ($_SERVER['QUERY_STRING']) {
         // $GET
         parse_str($_SERVER['QUERY_STRING'], $_GET);
     } else {
         $_SERVER['QUERY_STRING'] = '';
     }
     // REQUEST
     $_REQUEST = array_merge($_GET, $_POST);
     // REMOTE_ADDR REMOTE_PORT
     $_SERVER['REMOTE_ADDR'] = $connection->getRemoteIp();
     $_SERVER['REMOTE_PORT'] = $connection->getRemotePort();
     return array('get' => $_GET, 'post' => $_POST, 'cookie' => $_COOKIE, 'server' => $_SERVER, 'files' => $_FILES);
 }
 /**
  * This method pulls all the data out of a readable stream, and writes it to the supplied destination.
  *
  * @param TcpConnection $dest
  * @return void
  */
 public function pipe($dest)
 {
     $source = $this;
     $this->onMessage = function ($source, $data) use($dest) {
         $dest->send($data);
     };
     $this->onClose = function ($source) use($dest) {
         $dest->destroy();
     };
     $dest->onBufferFull = function ($dest) use($source) {
         $source->pauseRecv();
     };
     $dest->onBufferDrain = function ($dest) use($source) {
         $source->resumeRecv();
     };
 }