Beispiel #1
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/' . Worker::VERSION, '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 = str_replace('-', '_', strtoupper($key));
         $value = trim($value);
         $_SERVER['HTTP_' . $key] = $value;
         switch ($key) {
             // HTTP_HOST
             case 'HOST':
                 $tmp = explode(':', $value);
                 $_SERVER['SERVER_NAME'] = $tmp[0];
                 if (isset($tmp[1])) {
                     $_SERVER['SERVER_PORT'] = $tmp[1];
                 }
                 break;
                 // cookie
             // cookie
             case 'COOKIE':
                 parse_str(str_replace('; ', '&', $_SERVER['HTTP_COOKIE']), $_COOKIE);
                 break;
                 // content-type
             // content-type
             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;
             case 'CONTENT_LENGTH':
                 $_SERVER['CONTENT_LENGTH'] = $value;
                 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);
 }
Beispiel #2
0
 /**
  * 从http数据包中解析$_POST、$_GET、$_COOKIE等 
  * @param string $recv_buffer
  * @param TcpConnection $connection
  * @return void
  */
 public static function decode($recv_buffer, TcpConnection $connection)
 {
     // 初始化
     $_POST = $_GET = $_COOKIE = $_REQUEST = $_SESSION = $_FILES = array();
     $GLOBALS['HTTP_RAW_POST_DATA'] = '';
     // 清空上次的数据
     HttpCache::$header = array('Connection' => 'Connection: keep-alive');
     HttpCache::$instance = new HttpCache();
     // 需要设置的变量名
     $_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');
     // 将header分割成数组
     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]);
     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;
         }
     }
     // 需要解析$_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);
 }
Beispiel #3
0
 /**
  * 从http数据包中解析$_POST、$_GET、$_COOKIE等
  *
  * @param string        $recv_buffer
  * @param TcpConnection $connection
  * @return array
  */
 public static function decode($recv_buffer, TcpConnection $connection)
 {
     // 初始化
     Base::getLog()->debug(__METHOD__ . ' clean global variables');
     $_POST = $_GET = $_COOKIE = $_REQUEST = $_SESSION = $_FILES = [];
     $GLOBALS['HTTP_RAW_POST_DATA'] = '';
     $userInfo = posix_getpwuid(posix_getuid());
     $_SERVER = ['USER' => Arr::get($userInfo, 'name', ''), 'HOME' => '', 'QUERY_STRING' => '', 'REQUEST_METHOD' => '', 'REQUEST_URI' => '', 'SERVER_PROTOCOL' => '', 'SERVER_SOFTWARE' => 'tourze/' . Base::VERSION, 'SERVER_NAME' => '', 'SCRIPT_FILENAME' => '', 'HTTP_HOST' => '', 'HTTP_USER_AGENT' => '', 'HTTP_ACCEPT' => '', 'HTTP_ACCEPT_LANGUAGE' => '', 'HTTP_ACCEPT_ENCODING' => '', 'HTTP_COOKIE' => '', 'HTTP_CONNECTION' => '', 'REMOTE_ADDR' => '', 'REMOTE_PORT' => '0', 'REQUEST_TIME' => time()];
     $_SERVER['REQUEST_TIME_FLOAT'] = $_SERVER['REQUEST_TIME'] . substr((string) microtime(), 1, 5);
     Base::getLog()->debug(__METHOD__ . ' clean previous headers');
     // 清空上次的数据
     HttpCache::$header = ['Connection: keep-alive'];
     HttpCache::$instance = new HttpCache();
     // 将header分割成数组
     list($httpHeader, $httpBody) = explode("\r\n\r\n", $recv_buffer, 2);
     $headerData = explode("\r\n", $httpHeader);
     // 第一行为比较重要的一行
     $firstLine = array_shift($headerData);
     list($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI'], $_SERVER['SERVER_PROTOCOL']) = explode(' ', $firstLine);
     $_SERVER['PHP_SELF'] = $_SERVER['SCRIPT_NAME'] = $_SERVER['DOCUMENT_URI'] = $_SERVER['REQUEST_URI'];
     Base::getLog()->debug(__METHOD__ . ' receive http request', ['method' => $_SERVER['REQUEST_METHOD'], 'uri' => $_SERVER['REQUEST_URI'], 'protocol' => $_SERVER['SERVER_PROTOCOL']]);
     $httpPostBoundary = '';
     foreach ($headerData 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';
                     $httpPostBoundary = '--' . $match[1];
                 }
                 break;
         }
     }
     // 需要解析$_POST
     if ($_SERVER['REQUEST_METHOD'] === 'POST') {
         if (isset($_SERVER['CONTENT_TYPE']) && $_SERVER['CONTENT_TYPE'] === 'multipart/form-data') {
             self::parseUploadFiles($httpBody, $httpPostBoundary);
         } else {
             parse_str($httpBody, $_POST);
             $GLOBALS['HTTP_RAW_POST_DATA'] = $httpBody;
         }
     }
     // 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();
     // 这两处要怎么读取呢?
     $_SERVER['SERVER_ADDR'] = null;
     $_SERVER['SERVER_PORT'] = null;
     $result = ['get' => $_GET, 'post' => $_POST, 'cookie' => $_COOKIE, 'server' => $_SERVER, 'files' => $_FILES];
     Base::getLog()->debug(__METHOD__ . ' get request data', $result);
     return $result;
 }