2016-09-20 21:27:41 +08:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* This file is part of workerman.
|
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* For full copyright and license information, please see the MIT-LICENSE.txt
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
|
|
|
* @author walkor<walkor@workerman.net>
|
|
|
|
* @copyright walkor<walkor@workerman.net>
|
|
|
|
* @link http://www.workerman.net/
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
|
|
|
*/
|
|
|
|
namespace Workerman\Protocols;
|
2015-04-04 21:46:31 +08:00
|
|
|
|
|
|
|
use Workerman\Connection\TcpConnection;
|
2016-09-20 21:27:41 +08:00
|
|
|
use Workerman\Worker;
|
2015-04-04 21:46:31 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* http protocol
|
|
|
|
*/
|
|
|
|
class Http
|
|
|
|
{
|
|
|
|
/**
|
2016-09-20 21:27:41 +08:00
|
|
|
* Check the integrity of the package.
|
|
|
|
*
|
|
|
|
* @param string $recv_buffer
|
2015-04-04 21:46:31 +08:00
|
|
|
* @param TcpConnection $connection
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public static function input($recv_buffer, TcpConnection $connection)
|
|
|
|
{
|
2016-09-20 21:27:41 +08:00
|
|
|
if (!strpos($recv_buffer, "\r\n\r\n")) {
|
|
|
|
// Judge whether the package length exceeds the limit.
|
|
|
|
if (strlen($recv_buffer) >= TcpConnection::$maxPackageSize) {
|
2015-04-04 21:46:31 +08:00
|
|
|
$connection->close();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
|
|
|
|
list($header,) = explode("\r\n\r\n", $recv_buffer, 2);
|
|
|
|
if (0 === strpos($recv_buffer, "POST")) {
|
2015-04-04 21:46:31 +08:00
|
|
|
// find Content-Length
|
|
|
|
$match = array();
|
2016-09-20 21:27:41 +08:00
|
|
|
if (preg_match("/\r\nContent-Length: ?(\d+)/i", $header, $match)) {
|
|
|
|
$content_length = $match[1];
|
|
|
|
return $content_length + strlen($header) + 4;
|
|
|
|
} else {
|
2015-04-04 21:46:31 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
} elseif (0 === strpos($recv_buffer, "GET")) {
|
|
|
|
return strlen($header) + 4;
|
|
|
|
} else {
|
|
|
|
$connection->send("HTTP/1.1 400 Bad Request\r\n\r\n", true);
|
|
|
|
return 0;
|
2015-04-04 21:46:31 +08:00
|
|
|
}
|
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
|
2015-04-04 21:46:31 +08:00
|
|
|
/**
|
2016-09-20 21:27:41 +08:00
|
|
|
* Parse $_POST、$_GET、$_COOKIE.
|
|
|
|
*
|
|
|
|
* @param string $recv_buffer
|
2015-04-04 21:46:31 +08:00
|
|
|
* @param TcpConnection $connection
|
2016-09-20 21:27:41 +08:00
|
|
|
* @return array
|
2015-04-04 21:46:31 +08:00
|
|
|
*/
|
|
|
|
public static function decode($recv_buffer, TcpConnection $connection)
|
|
|
|
{
|
2016-09-20 21:27:41 +08:00
|
|
|
// Init.
|
|
|
|
$_POST = $_GET = $_COOKIE = $_REQUEST = $_SESSION = $_FILES = array();
|
2015-04-04 21:46:31 +08:00
|
|
|
$GLOBALS['HTTP_RAW_POST_DATA'] = '';
|
2016-09-20 21:27:41 +08:00
|
|
|
// Clear cache.
|
|
|
|
HttpCache::$header = array('Connection' => 'Connection: keep-alive');
|
2015-04-04 21:46:31 +08:00
|
|
|
HttpCache::$instance = new HttpCache();
|
2016-09-20 21:27:41 +08:00
|
|
|
// $_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.
|
2015-04-04 21:46:31 +08:00
|
|
|
list($http_header, $http_body) = explode("\r\n\r\n", $recv_buffer, 2);
|
|
|
|
$header_data = explode("\r\n", $http_header);
|
2016-09-20 21:27:41 +08:00
|
|
|
|
|
|
|
list($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI'], $_SERVER['SERVER_PROTOCOL']) = explode(' ',
|
|
|
|
$header_data[0]);
|
|
|
|
|
|
|
|
$http_post_boundary = '';
|
2015-04-04 21:46:31 +08:00
|
|
|
unset($header_data[0]);
|
2016-09-20 21:27:41 +08:00
|
|
|
foreach ($header_data as $content) {
|
2015-04-04 21:46:31 +08:00
|
|
|
// \r\n\r\n
|
2016-09-20 21:27:41 +08:00
|
|
|
if (empty($content)) {
|
2015-04-04 21:46:31 +08:00
|
|
|
continue;
|
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
list($key, $value) = explode(':', $content, 2);
|
|
|
|
$key = str_replace('-', '_', strtoupper($key));
|
|
|
|
$value = trim($value);
|
|
|
|
$_SERVER['HTTP_' . $key] = $value;
|
|
|
|
switch ($key) {
|
2015-04-04 21:46:31 +08:00
|
|
|
// HTTP_HOST
|
2016-09-20 21:27:41 +08:00
|
|
|
case 'HOST':
|
|
|
|
$tmp = explode(':', $value);
|
2015-04-04 21:46:31 +08:00
|
|
|
$_SERVER['SERVER_NAME'] = $tmp[0];
|
2016-09-20 21:27:41 +08:00
|
|
|
if (isset($tmp[1])) {
|
2015-04-04 21:46:31 +08:00
|
|
|
$_SERVER['SERVER_PORT'] = $tmp[1];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
// cookie
|
2016-09-20 21:27:41 +08:00
|
|
|
case 'COOKIE':
|
2015-04-04 21:46:31 +08:00
|
|
|
parse_str(str_replace('; ', '&', $_SERVER['HTTP_COOKIE']), $_COOKIE);
|
|
|
|
break;
|
2016-09-20 21:27:41 +08:00
|
|
|
// content-type
|
|
|
|
case 'CONTENT_TYPE':
|
|
|
|
if (!preg_match('/boundary="?(\S+)"?/', $value, $match)) {
|
2015-04-04 21:46:31 +08:00
|
|
|
$_SERVER['CONTENT_TYPE'] = $value;
|
2016-09-20 21:27:41 +08:00
|
|
|
} else {
|
2015-04-04 21:46:31 +08:00
|
|
|
$_SERVER['CONTENT_TYPE'] = 'multipart/form-data';
|
2016-09-20 21:27:41 +08:00
|
|
|
$http_post_boundary = '--' . $match[1];
|
2015-04-04 21:46:31 +08:00
|
|
|
}
|
|
|
|
break;
|
2016-09-20 21:27:41 +08:00
|
|
|
case 'CONTENT_LENGTH':
|
|
|
|
$_SERVER['CONTENT_LENGTH'] = $value;
|
|
|
|
break;
|
2015-04-04 21:46:31 +08:00
|
|
|
}
|
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
|
|
|
|
// Parse $_POST.
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
|
|
if (isset($_SERVER['CONTENT_TYPE']) && $_SERVER['CONTENT_TYPE'] === 'multipart/form-data') {
|
2015-04-04 21:46:31 +08:00
|
|
|
self::parseUploadFiles($http_body, $http_post_boundary);
|
2016-09-20 21:27:41 +08:00
|
|
|
} else {
|
2015-04-04 21:46:31 +08:00
|
|
|
parse_str($http_body, $_POST);
|
|
|
|
// $GLOBALS['HTTP_RAW_POST_DATA']
|
|
|
|
$GLOBALS['HTTP_RAW_POST_DATA'] = $http_body;
|
|
|
|
}
|
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
|
2015-04-04 21:46:31 +08:00
|
|
|
// QUERY_STRING
|
|
|
|
$_SERVER['QUERY_STRING'] = parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY);
|
2016-09-20 21:27:41 +08:00
|
|
|
if ($_SERVER['QUERY_STRING']) {
|
2015-04-04 21:46:31 +08:00
|
|
|
// $GET
|
|
|
|
parse_str($_SERVER['QUERY_STRING'], $_GET);
|
2016-09-20 21:27:41 +08:00
|
|
|
} else {
|
2015-04-04 21:46:31 +08:00
|
|
|
$_SERVER['QUERY_STRING'] = '';
|
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
|
2015-04-04 21:46:31 +08:00
|
|
|
// REQUEST
|
|
|
|
$_REQUEST = array_merge($_GET, $_POST);
|
2016-09-20 21:27:41 +08:00
|
|
|
|
2015-04-04 21:46:31 +08:00
|
|
|
// REMOTE_ADDR REMOTE_PORT
|
|
|
|
$_SERVER['REMOTE_ADDR'] = $connection->getRemoteIp();
|
|
|
|
$_SERVER['REMOTE_PORT'] = $connection->getRemotePort();
|
2016-09-20 21:27:41 +08:00
|
|
|
|
|
|
|
return array('get' => $_GET, 'post' => $_POST, 'cookie' => $_COOKIE, 'server' => $_SERVER, 'files' => $_FILES);
|
2015-04-04 21:46:31 +08:00
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
|
2015-04-04 21:46:31 +08:00
|
|
|
/**
|
2016-09-20 21:27:41 +08:00
|
|
|
* Http encode.
|
|
|
|
*
|
|
|
|
* @param string $content
|
2015-04-04 21:46:31 +08:00
|
|
|
* @param TcpConnection $connection
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function encode($content, TcpConnection $connection)
|
|
|
|
{
|
2016-09-20 21:27:41 +08:00
|
|
|
// Default http-code.
|
|
|
|
if (!isset(HttpCache::$header['Http-Code'])) {
|
2015-04-04 21:46:31 +08:00
|
|
|
$header = "HTTP/1.1 200 OK\r\n";
|
2016-09-20 21:27:41 +08:00
|
|
|
} else {
|
|
|
|
$header = HttpCache::$header['Http-Code'] . "\r\n";
|
2015-04-04 21:46:31 +08:00
|
|
|
unset(HttpCache::$header['Http-Code']);
|
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
|
2015-04-04 21:46:31 +08:00
|
|
|
// Content-Type
|
2016-09-20 21:27:41 +08:00
|
|
|
if (!isset(HttpCache::$header['Content-Type'])) {
|
2015-04-04 21:46:31 +08:00
|
|
|
$header .= "Content-Type: text/html;charset=utf-8\r\n";
|
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
|
2015-04-04 21:46:31 +08:00
|
|
|
// other headers
|
2016-09-20 21:27:41 +08:00
|
|
|
foreach (HttpCache::$header as $key => $item) {
|
|
|
|
if ('Set-Cookie' === $key && is_array($item)) {
|
|
|
|
foreach ($item as $it) {
|
|
|
|
$header .= $it . "\r\n";
|
2015-04-04 21:46:31 +08:00
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
} else {
|
|
|
|
$header .= $item . "\r\n";
|
2015-04-04 21:46:31 +08:00
|
|
|
}
|
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
|
2015-04-04 21:46:31 +08:00
|
|
|
// header
|
2016-09-20 21:27:41 +08:00
|
|
|
$header .= "Server: workerman/" . Worker::VERSION . "\r\nContent-Length: " . strlen($content) . "\r\n\r\n";
|
|
|
|
|
2015-04-04 21:46:31 +08:00
|
|
|
// save session
|
|
|
|
self::sessionWriteClose();
|
2016-09-20 21:27:41 +08:00
|
|
|
|
2015-04-04 21:46:31 +08:00
|
|
|
// the whole http package
|
2016-09-20 21:27:41 +08:00
|
|
|
return $header . $content;
|
2015-04-04 21:46:31 +08:00
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
|
2015-04-04 21:46:31 +08:00
|
|
|
/**
|
|
|
|
* 设置http头
|
2016-09-20 21:27:41 +08:00
|
|
|
*
|
|
|
|
* @return bool|void
|
2015-04-04 21:46:31 +08:00
|
|
|
*/
|
|
|
|
public static function header($content, $replace = true, $http_response_code = 0)
|
|
|
|
{
|
2016-09-20 21:27:41 +08:00
|
|
|
if (PHP_SAPI != 'cli') {
|
2015-04-04 21:46:31 +08:00
|
|
|
return $http_response_code ? header($content, $replace, $http_response_code) : header($content, $replace);
|
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
if (strpos($content, 'HTTP') === 0) {
|
2015-04-04 21:46:31 +08:00
|
|
|
$key = 'Http-Code';
|
2016-09-20 21:27:41 +08:00
|
|
|
} else {
|
2015-04-04 21:46:31 +08:00
|
|
|
$key = strstr($content, ":", true);
|
2016-09-20 21:27:41 +08:00
|
|
|
if (empty($key)) {
|
2015-04-04 21:46:31 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
|
|
|
|
if ('location' === strtolower($key) && !$http_response_code) {
|
2015-04-04 21:46:31 +08:00
|
|
|
return self::header($content, true, 302);
|
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
|
|
|
|
if (isset(HttpCache::$codes[$http_response_code])) {
|
|
|
|
HttpCache::$header['Http-Code'] = "HTTP/1.1 $http_response_code " . HttpCache::$codes[$http_response_code];
|
|
|
|
if ($key === 'Http-Code') {
|
2015-04-04 21:46:31 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
|
|
|
|
if ($key === 'Set-Cookie') {
|
2015-04-04 21:46:31 +08:00
|
|
|
HttpCache::$header[$key][] = $content;
|
2016-09-20 21:27:41 +08:00
|
|
|
} else {
|
2015-04-04 21:46:31 +08:00
|
|
|
HttpCache::$header[$key] = $content;
|
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
|
2015-04-04 21:46:31 +08:00
|
|
|
return true;
|
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
|
2015-04-04 21:46:31 +08:00
|
|
|
/**
|
2016-09-20 21:27:41 +08:00
|
|
|
* Remove header.
|
|
|
|
*
|
2015-04-04 21:46:31 +08:00
|
|
|
* @param string $name
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function headerRemove($name)
|
|
|
|
{
|
2016-09-20 21:27:41 +08:00
|
|
|
if (PHP_SAPI != 'cli') {
|
|
|
|
header_remove($name);
|
|
|
|
return;
|
2015-04-04 21:46:31 +08:00
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
unset(HttpCache::$header[$name]);
|
2015-04-04 21:46:31 +08:00
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
|
2015-04-04 21:46:31 +08:00
|
|
|
/**
|
2016-09-20 21:27:41 +08:00
|
|
|
* Set cookie.
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @param string $value
|
2015-04-04 21:46:31 +08:00
|
|
|
* @param integer $maxage
|
2016-09-20 21:27:41 +08:00
|
|
|
* @param string $path
|
|
|
|
* @param string $domain
|
|
|
|
* @param bool $secure
|
|
|
|
* @param bool $HTTPOnly
|
|
|
|
* @return bool|void
|
2015-04-04 21:46:31 +08:00
|
|
|
*/
|
2016-09-20 21:27:41 +08:00
|
|
|
public static function setcookie(
|
|
|
|
$name,
|
|
|
|
$value = '',
|
|
|
|
$maxage = 0,
|
|
|
|
$path = '',
|
|
|
|
$domain = '',
|
|
|
|
$secure = false,
|
|
|
|
$HTTPOnly = false
|
|
|
|
) {
|
|
|
|
if (PHP_SAPI != 'cli') {
|
2015-04-04 21:46:31 +08:00
|
|
|
return setcookie($name, $value, $maxage, $path, $domain, $secure, $HTTPOnly);
|
|
|
|
}
|
|
|
|
return self::header(
|
2016-09-20 21:27:41 +08:00
|
|
|
'Set-Cookie: ' . $name . '=' . rawurlencode($value)
|
|
|
|
. (empty($domain) ? '' : '; Domain=' . $domain)
|
|
|
|
. (empty($maxage) ? '' : '; Max-Age=' . $maxage)
|
|
|
|
. (empty($path) ? '' : '; Path=' . $path)
|
|
|
|
. (!$secure ? '' : '; Secure')
|
|
|
|
. (!$HTTPOnly ? '' : '; HttpOnly'), false);
|
2015-04-04 21:46:31 +08:00
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
|
2015-04-04 21:46:31 +08:00
|
|
|
/**
|
|
|
|
* sessionStart
|
2016-09-20 21:27:41 +08:00
|
|
|
*
|
2015-04-04 21:46:31 +08:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function sessionStart()
|
|
|
|
{
|
2016-09-20 21:27:41 +08:00
|
|
|
if (PHP_SAPI != 'cli') {
|
2015-04-04 21:46:31 +08:00
|
|
|
return session_start();
|
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
if (HttpCache::$instance->sessionStarted) {
|
2015-04-04 21:46:31 +08:00
|
|
|
echo "already sessionStarted\nn";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
HttpCache::$instance->sessionStarted = true;
|
2016-09-20 21:27:41 +08:00
|
|
|
// Generate a SID.
|
|
|
|
if (!isset($_COOKIE[HttpCache::$sessionName]) || !is_file(HttpCache::$sessionPath . '/ses' . $_COOKIE[HttpCache::$sessionName])) {
|
|
|
|
$file_name = tempnam(HttpCache::$sessionPath, 'ses');
|
|
|
|
if (!$file_name) {
|
2015-04-04 21:46:31 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
HttpCache::$instance->sessionFile = $file_name;
|
2016-09-20 21:27:41 +08:00
|
|
|
$session_id = substr(basename($file_name), strlen('ses'));
|
2015-04-04 21:46:31 +08:00
|
|
|
return self::setcookie(
|
2016-09-20 21:27:41 +08:00
|
|
|
HttpCache::$sessionName
|
|
|
|
, $session_id
|
|
|
|
, ini_get('session.cookie_lifetime')
|
|
|
|
, ini_get('session.cookie_path')
|
|
|
|
, ini_get('session.cookie_domain')
|
|
|
|
, ini_get('session.cookie_secure')
|
|
|
|
, ini_get('session.cookie_httponly')
|
2015-04-04 21:46:31 +08:00
|
|
|
);
|
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
if (!HttpCache::$instance->sessionFile) {
|
|
|
|
HttpCache::$instance->sessionFile = HttpCache::$sessionPath . '/ses' . $_COOKIE[HttpCache::$sessionName];
|
2015-04-04 21:46:31 +08:00
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
// Read session from session file.
|
|
|
|
if (HttpCache::$instance->sessionFile) {
|
2015-04-04 21:46:31 +08:00
|
|
|
$raw = file_get_contents(HttpCache::$instance->sessionFile);
|
2016-09-20 21:27:41 +08:00
|
|
|
if ($raw) {
|
2015-04-04 21:46:31 +08:00
|
|
|
session_decode($raw);
|
|
|
|
}
|
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
return true;
|
2015-04-04 21:46:31 +08:00
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
|
2015-04-04 21:46:31 +08:00
|
|
|
/**
|
2016-09-20 21:27:41 +08:00
|
|
|
* Save session.
|
|
|
|
*
|
2015-04-04 21:46:31 +08:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function sessionWriteClose()
|
|
|
|
{
|
2016-09-20 21:27:41 +08:00
|
|
|
if (PHP_SAPI != 'cli') {
|
2015-04-04 21:46:31 +08:00
|
|
|
return session_write_close();
|
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
if (!empty(HttpCache::$instance->sessionStarted) && !empty($_SESSION)) {
|
2015-04-04 21:46:31 +08:00
|
|
|
$session_str = session_encode();
|
2016-09-20 21:27:41 +08:00
|
|
|
if ($session_str && HttpCache::$instance->sessionFile) {
|
2015-04-04 21:46:31 +08:00
|
|
|
return file_put_contents(HttpCache::$instance->sessionFile, $session_str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return empty($_SESSION);
|
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
|
2015-04-04 21:46:31 +08:00
|
|
|
/**
|
2016-09-20 21:27:41 +08:00
|
|
|
* End, like call exit in php-fpm.
|
|
|
|
*
|
2015-04-04 21:46:31 +08:00
|
|
|
* @param string $msg
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public static function end($msg = '')
|
|
|
|
{
|
2016-09-20 21:27:41 +08:00
|
|
|
if (PHP_SAPI != 'cli') {
|
2015-04-04 21:46:31 +08:00
|
|
|
exit($msg);
|
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
if ($msg) {
|
2015-04-04 21:46:31 +08:00
|
|
|
echo $msg;
|
|
|
|
}
|
|
|
|
throw new \Exception('jump_exit');
|
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
|
2015-04-04 21:46:31 +08:00
|
|
|
/**
|
2016-09-20 21:27:41 +08:00
|
|
|
* Get mime types.
|
|
|
|
*
|
|
|
|
* @return string
|
2015-04-04 21:46:31 +08:00
|
|
|
*/
|
|
|
|
public static function getMimeTypesFile()
|
|
|
|
{
|
2016-09-20 21:27:41 +08:00
|
|
|
return __DIR__ . '/Http/mime.types';
|
2015-04-04 21:46:31 +08:00
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse $_FILES.
|
|
|
|
*
|
|
|
|
* @param string $http_body
|
|
|
|
* @param string $http_post_boundary
|
|
|
|
* @return void
|
2015-04-04 21:46:31 +08:00
|
|
|
*/
|
2016-09-20 21:27:41 +08:00
|
|
|
protected static function parseUploadFiles($http_body, $http_post_boundary)
|
2015-04-04 21:46:31 +08:00
|
|
|
{
|
2016-09-20 21:27:41 +08:00
|
|
|
$http_body = substr($http_body, 0, strlen($http_body) - (strlen($http_post_boundary) + 4));
|
|
|
|
$boundary_data_array = explode($http_post_boundary . "\r\n", $http_body);
|
|
|
|
if ($boundary_data_array[0] === '') {
|
2015-04-04 21:46:31 +08:00
|
|
|
unset($boundary_data_array[0]);
|
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
foreach ($boundary_data_array as $boundary_data_buffer) {
|
2015-04-04 21:46:31 +08:00
|
|
|
list($boundary_header_buffer, $boundary_value) = explode("\r\n\r\n", $boundary_data_buffer, 2);
|
2016-09-20 21:27:41 +08:00
|
|
|
// Remove \r\n from the end of buffer.
|
2015-04-04 21:46:31 +08:00
|
|
|
$boundary_value = substr($boundary_value, 0, -2);
|
2016-09-20 21:27:41 +08:00
|
|
|
foreach (explode("\r\n", $boundary_header_buffer) as $item) {
|
2015-04-04 21:46:31 +08:00
|
|
|
list($header_key, $header_value) = explode(": ", $item);
|
|
|
|
$header_key = strtolower($header_key);
|
2016-09-20 21:27:41 +08:00
|
|
|
switch ($header_key) {
|
2015-04-04 21:46:31 +08:00
|
|
|
case "content-disposition":
|
2016-09-20 21:27:41 +08:00
|
|
|
// Is file data.
|
|
|
|
if (preg_match('/name=".*?"; filename="(.*?)"$/', $header_value, $match)) {
|
|
|
|
// Parse $_FILES.
|
2015-04-04 21:46:31 +08:00
|
|
|
$_FILES[] = array(
|
|
|
|
'file_name' => $match[1],
|
|
|
|
'file_data' => $boundary_value,
|
|
|
|
'file_size' => strlen($boundary_value),
|
|
|
|
);
|
|
|
|
continue;
|
2016-09-20 21:27:41 +08:00
|
|
|
} // Is post field.
|
|
|
|
else {
|
|
|
|
// Parse $_POST.
|
|
|
|
if (preg_match('/name="(.*?)"$/', $header_value, $match)) {
|
2015-04-04 21:46:31 +08:00
|
|
|
$_POST[$match[1]] = $boundary_value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-09-20 21:27:41 +08:00
|
|
|
* Http cache for the current http response.
|
2015-04-04 21:46:31 +08:00
|
|
|
*/
|
|
|
|
class HttpCache
|
|
|
|
{
|
|
|
|
public static $codes = array(
|
2016-09-20 21:27:41 +08:00
|
|
|
100 => 'Continue',
|
|
|
|
101 => 'Switching Protocols',
|
|
|
|
200 => 'OK',
|
|
|
|
201 => 'Created',
|
|
|
|
202 => 'Accepted',
|
|
|
|
203 => 'Non-Authoritative Information',
|
|
|
|
204 => 'No Content',
|
|
|
|
205 => 'Reset Content',
|
|
|
|
206 => 'Partial Content',
|
|
|
|
300 => 'Multiple Choices',
|
|
|
|
301 => 'Moved Permanently',
|
|
|
|
302 => 'Found',
|
|
|
|
303 => 'See Other',
|
|
|
|
304 => 'Not Modified',
|
|
|
|
305 => 'Use Proxy',
|
|
|
|
306 => '(Unused)',
|
|
|
|
307 => 'Temporary Redirect',
|
|
|
|
400 => 'Bad Request',
|
|
|
|
401 => 'Unauthorized',
|
|
|
|
402 => 'Payment Required',
|
|
|
|
403 => 'Forbidden',
|
|
|
|
404 => 'Not Found',
|
|
|
|
405 => 'Method Not Allowed',
|
|
|
|
406 => 'Not Acceptable',
|
|
|
|
407 => 'Proxy Authentication Required',
|
|
|
|
408 => 'Request Timeout',
|
|
|
|
409 => 'Conflict',
|
|
|
|
410 => 'Gone',
|
|
|
|
411 => 'Length Required',
|
|
|
|
412 => 'Precondition Failed',
|
|
|
|
413 => 'Request Entity Too Large',
|
|
|
|
414 => 'Request-URI Too Long',
|
|
|
|
415 => 'Unsupported Media Type',
|
|
|
|
416 => 'Requested Range Not Satisfiable',
|
|
|
|
417 => 'Expectation Failed',
|
|
|
|
422 => 'Unprocessable Entity',
|
|
|
|
423 => 'Locked',
|
|
|
|
500 => 'Internal Server Error',
|
|
|
|
501 => 'Not Implemented',
|
|
|
|
502 => 'Bad Gateway',
|
|
|
|
503 => 'Service Unavailable',
|
|
|
|
504 => 'Gateway Timeout',
|
|
|
|
505 => 'HTTP Version Not Supported',
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var HttpCache
|
|
|
|
*/
|
2015-04-04 21:46:31 +08:00
|
|
|
public static $instance = null;
|
2016-09-20 21:27:41 +08:00
|
|
|
|
2015-04-04 21:46:31 +08:00
|
|
|
public static $header = array();
|
|
|
|
public static $sessionPath = '';
|
|
|
|
public static $sessionName = '';
|
|
|
|
public $sessionStarted = false;
|
|
|
|
public $sessionFile = '';
|
|
|
|
|
|
|
|
public static function init()
|
|
|
|
{
|
|
|
|
self::$sessionName = ini_get('session.name');
|
|
|
|
self::$sessionPath = session_save_path();
|
2016-09-20 21:27:41 +08:00
|
|
|
if (!self::$sessionPath) {
|
2015-04-04 21:46:31 +08:00
|
|
|
self::$sessionPath = sys_get_temp_dir();
|
|
|
|
}
|
|
|
|
@\session_start();
|
|
|
|
}
|
|
|
|
}
|