e
This commit is contained in:
@@ -0,0 +1,196 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace HttpServer\Http;
|
||||
|
||||
use HttpServer\Abstracts\BaseContext;
|
||||
use Swoole\Coroutine;
|
||||
|
||||
/**
|
||||
* Class Context
|
||||
* @package Yoc\http
|
||||
*/
|
||||
class Context extends BaseContext
|
||||
{
|
||||
|
||||
protected static $_requests = [];
|
||||
|
||||
protected static $_response = [];
|
||||
|
||||
|
||||
/**
|
||||
* @param $id
|
||||
* @param $context
|
||||
* @param null $key
|
||||
* @return mixed
|
||||
*/
|
||||
public static function setContext($id, $context, $key = null)
|
||||
{
|
||||
if (static::inCoroutine()) {
|
||||
return self::setCoroutine($id, $context, $key);
|
||||
} else {
|
||||
return self::setStatic($id, $context, $key);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $id
|
||||
* @param $context
|
||||
* @param null $key
|
||||
* @return mixed
|
||||
*/
|
||||
private static function setStatic($id, $context, $key = null)
|
||||
{
|
||||
if (!empty($key)) {
|
||||
if (!is_array(static::$_requests[$id])) {
|
||||
static::$_requests[$id] = [$key => $context];
|
||||
} else {
|
||||
static::$_requests[$id][$key] = $context;
|
||||
}
|
||||
} else {
|
||||
static::$_requests[$id] = $context;
|
||||
}
|
||||
return $context;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $id
|
||||
* @param $context
|
||||
* @param null $key
|
||||
* @return
|
||||
*/
|
||||
private static function setCoroutine($id, $context, $key = null)
|
||||
{
|
||||
if (!static::hasContext($id)) {
|
||||
Coroutine::getContext()[$id] = [];
|
||||
}
|
||||
if (!empty($key)) {
|
||||
if (!is_array(Coroutine::getContext()[$id])) {
|
||||
Coroutine::getContext()[$id] = [$key => $context];
|
||||
} else {
|
||||
Coroutine::getContext()[$id][$key] = $context;
|
||||
}
|
||||
} else {
|
||||
Coroutine::getContext()[$id] = $context;
|
||||
}
|
||||
return $context;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $id
|
||||
* @param null $key
|
||||
* @return false|mixed
|
||||
*/
|
||||
public static function autoIncr($id, $key = null)
|
||||
{
|
||||
if (!static::inCoroutine()) {
|
||||
return false;
|
||||
}
|
||||
if (!isset(Coroutine::getContext()[$id][$key])) {
|
||||
return false;
|
||||
}
|
||||
return Coroutine::getContext()[$id][$key] += 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $id
|
||||
* @param null $key
|
||||
* @return false|mixed
|
||||
*/
|
||||
public static function autoDecr($id, $key = null)
|
||||
{
|
||||
if (!static::inCoroutine()) {
|
||||
return false;
|
||||
}
|
||||
if (!isset(Coroutine::getContext()[$id][$key])) {
|
||||
return false;
|
||||
}
|
||||
return Coroutine::getContext()[$id][$key] -= 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $id
|
||||
* @param null $key
|
||||
* @return mixed
|
||||
*/
|
||||
public static function getContext($id, $key = null)
|
||||
{
|
||||
if (static::inCoroutine()) {
|
||||
$array = Coroutine::getContext()[$id] ?? null;
|
||||
} else {
|
||||
$array = static::$_requests[$id] ?? null;
|
||||
}
|
||||
if (empty($key) || !is_array($array)) {
|
||||
return $array;
|
||||
}
|
||||
return $array[$key];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public static function getAllContext()
|
||||
{
|
||||
if (static::inCoroutine()) {
|
||||
return Coroutine::getContext() ?? [];
|
||||
} else {
|
||||
return static::$_requests ?? [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $id
|
||||
* @param null $key
|
||||
*/
|
||||
public static function deleteId($id, $key = null)
|
||||
{
|
||||
if (!static::hasContext($id, $key)) {
|
||||
return;
|
||||
}
|
||||
if (static::inCoroutine()) {
|
||||
if (!empty($key)) {
|
||||
Coroutine::getContext()[$id][$key] = null;
|
||||
} else {
|
||||
Coroutine::getContext()[$id] = null;
|
||||
}
|
||||
} else {
|
||||
unset(static::$_requests[$id]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $id
|
||||
* @param null $key
|
||||
* @return mixed
|
||||
*/
|
||||
public static function hasContext($id, $key = null)
|
||||
{
|
||||
if (static::inCoroutine()) {
|
||||
$data = Coroutine::getContext()[$id] ?? null;
|
||||
} else {
|
||||
$data = static::$_requests[$id] ?? null;
|
||||
}
|
||||
if (empty($data)) {
|
||||
return false;
|
||||
}
|
||||
if (empty($key)) {
|
||||
return true;
|
||||
} else if (!is_array($data)) {
|
||||
return false;
|
||||
}
|
||||
return isset($data[$key]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public static function inCoroutine()
|
||||
{
|
||||
return Coroutine::getCid() > 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace HttpServer\Http;
|
||||
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* Class File
|
||||
* @package BeReborn\Http
|
||||
*/
|
||||
class File
|
||||
{
|
||||
|
||||
public $name = '';
|
||||
public $tmp_name = '';
|
||||
public $error = '';
|
||||
public $type = '';
|
||||
public $size = '';
|
||||
|
||||
private $newName = '';
|
||||
private $errorInfo = [
|
||||
0 => 'UPLOAD_ERR_OK.',
|
||||
1 => 'The uploaded file exceeds the upload_max_filesize directive in php.ini.',
|
||||
2 => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.',
|
||||
3 => 'The uploaded file was only partially uploaded.',
|
||||
4 => 'No file was uploaded.',
|
||||
6 => 'Missing a temporary folder.',
|
||||
7 => 'Failed to write file to disk.',
|
||||
8 => 'A PHP extension stopped the file upload.'
|
||||
];
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
* @return bool
|
||||
* @throws Exception
|
||||
*/
|
||||
public function saveTo(string $path)
|
||||
{
|
||||
if ($this->hasError()) {
|
||||
throw new Exception($this->getErrorInfo());
|
||||
}
|
||||
|
||||
@move_uploaded_file($this->tmp_name, $path);
|
||||
if (!file_exists($path)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function rename()
|
||||
{
|
||||
if (!empty($this->newName)) {
|
||||
return $this->newName;
|
||||
}
|
||||
$param = ['tmp_name' => $this->getTmpPath()];
|
||||
$this->newName = \BeReborn::rename($param);
|
||||
return $this->newName;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getTmpPath()
|
||||
{
|
||||
return $this->tmp_name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*
|
||||
* check file have error
|
||||
*/
|
||||
public function hasError()
|
||||
{
|
||||
return $this->error !== 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*
|
||||
* get upload error info
|
||||
*/
|
||||
public function getErrorInfo()
|
||||
{
|
||||
if (!isset($this->errorInfo[$this->error])) {
|
||||
return 'Unknown upload error.';
|
||||
}
|
||||
return $this->errorInfo[$this->error];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: whwyy
|
||||
* Date: 2018/4/8 0008
|
||||
* Time: 17:51
|
||||
*/
|
||||
|
||||
namespace HttpServer\Http\Formatter;
|
||||
|
||||
|
||||
use BeReborn\Core\JSON;
|
||||
use HttpServer\Application;
|
||||
use Swoole\Http\Response;
|
||||
use HttpServer\IInterface\IFormatter;
|
||||
|
||||
/**
|
||||
* Class HtmlFormatter
|
||||
* @package BeReborn\Http\Formatter
|
||||
*/
|
||||
class HtmlFormatter extends Application implements IFormatter
|
||||
{
|
||||
|
||||
public $data;
|
||||
|
||||
/** @var Response */
|
||||
public $status;
|
||||
|
||||
public $header = [];
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
* @return $this
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function send($data)
|
||||
{
|
||||
if (!is_string($data)) {
|
||||
$data = JSON::encode($data);
|
||||
}
|
||||
$this->data = $data;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getData()
|
||||
{
|
||||
$data = $this->data;
|
||||
$this->clear();
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function clear()
|
||||
{
|
||||
unset($this->data);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: whwyy
|
||||
* Date: 2018/4/8 0008
|
||||
* Time: 17:18
|
||||
*/
|
||||
|
||||
namespace HttpServer\Http\Formatter;
|
||||
|
||||
use Exception;
|
||||
use HttpServer\Application;
|
||||
use HttpServer\IInterface\IFormatter;
|
||||
|
||||
/**
|
||||
* Class JsonFormatter
|
||||
* @package BeReborn\Http\Formatter
|
||||
*/
|
||||
class JsonFormatter extends Application implements IFormatter
|
||||
{
|
||||
public $data;
|
||||
|
||||
public $status = 200;
|
||||
|
||||
public $header = [];
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
* @return $this|IFormatter
|
||||
* @throws Exception
|
||||
*/
|
||||
public function send($data)
|
||||
{
|
||||
if (!is_string($data)) {
|
||||
$data = json_encode($data);
|
||||
}
|
||||
$this->data = $data;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getData()
|
||||
{
|
||||
$data = $this->data;
|
||||
$this->clear();
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
||||
public function clear()
|
||||
{
|
||||
unset($this->data);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: whwyy
|
||||
* Date: 2018/4/8 0008
|
||||
* Time: 17:29
|
||||
*/
|
||||
|
||||
namespace HttpServer\Http\Formatter;
|
||||
|
||||
|
||||
use HttpServer\Application;
|
||||
use SimpleXMLElement;
|
||||
use Swoole\Http\Response;
|
||||
use HttpServer\IInterface\IFormatter;
|
||||
|
||||
|
||||
/**
|
||||
* Class XmlFormatter
|
||||
* @package BeReborn\Http\Formatter
|
||||
*/
|
||||
class XmlFormatter extends Application implements IFormatter
|
||||
{
|
||||
|
||||
public $data = '';
|
||||
|
||||
/** @var Response */
|
||||
public $status;
|
||||
|
||||
public $header = [];
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
* @return $this
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function send($data)
|
||||
{
|
||||
if (!is_string($data)) {
|
||||
// TODO: Implement send() method.
|
||||
$dom = new SimpleXMLElement('<xml/>');
|
||||
|
||||
$this->toXml($dom, $data);
|
||||
|
||||
$this->data = $dom->saveXML();
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getData()
|
||||
{
|
||||
$data = $this->data;
|
||||
$this->clear();
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param SimpleXMLElement $dom
|
||||
* @param $data
|
||||
*/
|
||||
public function toXml($dom, $data)
|
||||
{
|
||||
foreach ($data as $key => $val) {
|
||||
if (is_numeric($key)) {
|
||||
$key = 'item' . $key;
|
||||
}
|
||||
if (is_array($val)) {
|
||||
$node = $dom->addChild($key);
|
||||
$this->toXml($node, $val);
|
||||
} else if (is_object($val)) {
|
||||
$val = get_object_vars($val);
|
||||
$node = $dom->addChild($key);
|
||||
$this->toXml($node, $val);
|
||||
} else {
|
||||
$dom->addChild($key, htmlspecialchars($val));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function clear()
|
||||
{
|
||||
unset($this->data);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: admin
|
||||
* Date: 2019-03-18
|
||||
* Time: 14:54
|
||||
*/
|
||||
|
||||
namespace HttpServer\Http;
|
||||
|
||||
/**
|
||||
* Class HttpHeaders
|
||||
* @package BeReborn\Http
|
||||
*/
|
||||
class HttpHeaders
|
||||
{
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
private $headers = [];
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
private $response = [];
|
||||
|
||||
/**
|
||||
* HttpHeaders constructor.
|
||||
* @param $headers
|
||||
*/
|
||||
public function __construct($headers)
|
||||
{
|
||||
$this->headers = $headers;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param $value
|
||||
*/
|
||||
public function setHeader($name, $value)
|
||||
{
|
||||
$this->response[$name] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $headers
|
||||
*/
|
||||
public function setHeaders(array $headers)
|
||||
{
|
||||
foreach ($headers as $key => $val) {
|
||||
$this->response[$key] = $val;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param $value
|
||||
*/
|
||||
public function replace($name, $value)
|
||||
{
|
||||
$this->headers[$name] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param $value
|
||||
*/
|
||||
public function addHeader($name, $value)
|
||||
{
|
||||
$this->headers[$name] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $headers
|
||||
* @return $this
|
||||
*/
|
||||
public function addHeaders(array $headers)
|
||||
{
|
||||
if (empty($headers)) {
|
||||
return $this;
|
||||
}
|
||||
if (!empty($this->headers)) {
|
||||
$headers = array_merge($this->headers, $headers);
|
||||
}
|
||||
$this->headers = $headers;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getResponseHeaders()
|
||||
{
|
||||
return $this->response;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @return mixed|null
|
||||
*/
|
||||
public function getHeader($name)
|
||||
{
|
||||
return $this->headers[$name] ?? null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @return mixed|string|null
|
||||
*/
|
||||
public function get($name)
|
||||
{
|
||||
return $this->getHeader($name);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @return bool
|
||||
*/
|
||||
public function exists($name)
|
||||
{
|
||||
return isset($this->headers[$name]) && $this->headers[$name] != null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getHeaders()
|
||||
{
|
||||
return $this->headers;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,404 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: admin
|
||||
* Date: 2019-03-18
|
||||
* Time: 14:54
|
||||
*/
|
||||
|
||||
namespace HttpServer\Http;
|
||||
|
||||
use BeReborn\Core\JSON;
|
||||
use Exception;
|
||||
use HttpServer\Exception\RequestException;
|
||||
|
||||
/**
|
||||
* Class HttpParams
|
||||
* @package BeReborn\Http
|
||||
*/
|
||||
class HttpParams
|
||||
{
|
||||
|
||||
/** @var array */
|
||||
private $body = [];
|
||||
|
||||
/** @var array */
|
||||
private $gets = [];
|
||||
|
||||
/** @var array */
|
||||
private $files = [];
|
||||
|
||||
/**
|
||||
* HttpParams constructor.
|
||||
* @param $body
|
||||
* @param $get
|
||||
* @param $files
|
||||
*/
|
||||
public function __construct($body, $get, $files)
|
||||
{
|
||||
$this->body = $body;
|
||||
$this->gets = $get ?? [];
|
||||
$this->files = $files ?? [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function offset()
|
||||
{
|
||||
return ($this->page() - 1) * $this->size();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
* 批量添加数据
|
||||
*/
|
||||
public function setPosts($data)
|
||||
{
|
||||
if (!is_array($data)) {
|
||||
return;
|
||||
}
|
||||
foreach ($data as $key => $vla) {
|
||||
$this->body[$key] = $vla;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @param string $value
|
||||
*/
|
||||
public function addGetParam(string $key, string $value)
|
||||
{
|
||||
$this->gets[$key] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
private function page()
|
||||
{
|
||||
return (int)$this->get('page', 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function size()
|
||||
{
|
||||
return (int)$this->get('size', 20);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param $defaultValue
|
||||
* @param $call
|
||||
* @return mixed|null
|
||||
*/
|
||||
public function get($name, $defaultValue = null, $call = null)
|
||||
{
|
||||
return $this->gets[$name] ?? $defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param null $defaultValue
|
||||
* @param $call
|
||||
* @return mixed|null
|
||||
*/
|
||||
public function post($name, $defaultValue = null, $call = null)
|
||||
{
|
||||
$data = $this->body[$name] ?? $defaultValue;
|
||||
if ($call !== null) {
|
||||
$data = call_user_func($call, $data);
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @return false|string
|
||||
* @throws Exception
|
||||
*/
|
||||
public function json($name)
|
||||
{
|
||||
$data = $this->array($name);
|
||||
if (empty($data)) {
|
||||
return JSON::encode([]);
|
||||
} else if (!is_array($data)) {
|
||||
return JSON::encode([]);
|
||||
}
|
||||
return JSON::encode($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function gets()
|
||||
{
|
||||
return $this->gets;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function params()
|
||||
{
|
||||
return array_merge($this->body ?? [], $this->files ?? []);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function load()
|
||||
{
|
||||
return array_merge($this->files, $this->body, $this->gets);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param array $defaultValue
|
||||
* @return array|mixed
|
||||
*/
|
||||
public function array($name, $defaultValue = [])
|
||||
{
|
||||
return $this->body[$name] ?? $defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @return mixed|File|null
|
||||
* @throws Exception
|
||||
*/
|
||||
public function file($name)
|
||||
{
|
||||
if (!isset($this->files[$name])) {
|
||||
return null;
|
||||
}
|
||||
$param = $this->files[$name];
|
||||
$param['class'] = File::class;
|
||||
return \BeReborn::createObject($param);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param bool $isNeed
|
||||
* @return mixed|null
|
||||
* @throws RequestException
|
||||
*/
|
||||
private function required($name, $isNeed = false)
|
||||
{
|
||||
$int = $this->body[$name] ?? NULL;
|
||||
if (is_null($int) && $isNeed === true) {
|
||||
throw new RequestException("You need to add request parameter $name");
|
||||
}
|
||||
return $int;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param bool $isNeed
|
||||
* @param null $min
|
||||
* @param null $max
|
||||
* @return int
|
||||
* @throws Exception
|
||||
*/
|
||||
public function int($name, $isNeed = FALSE, $min = NULL, $max = NULL)
|
||||
{
|
||||
$int = $this->required($name, $isNeed);
|
||||
if ($int === null) return null;
|
||||
if (is_array($min)) {
|
||||
list($min, $max) = $min;
|
||||
}
|
||||
if (is_null($int)) {
|
||||
$length = 0;
|
||||
} else {
|
||||
$length = strlen(floatval($int));
|
||||
}
|
||||
if (!is_numeric($int) || intval($int) != $int) {
|
||||
throw new RequestException("The request parameter $name must integer.");
|
||||
}
|
||||
$this->between($length, $min, $max);
|
||||
return (int)$int;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param bool $isNeed
|
||||
* @param int $round
|
||||
* @return float
|
||||
* @throws Exception
|
||||
*/
|
||||
public function float($name, $isNeed = FALSE, $round = 0)
|
||||
{
|
||||
$int = $this->required($name, $isNeed);
|
||||
if ($int === null) {
|
||||
return null;
|
||||
}
|
||||
if ($round > 0) {
|
||||
return round(floatval($int), $round);
|
||||
} else {
|
||||
return floatval($int);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param bool $isNeed
|
||||
* @param null $length
|
||||
*
|
||||
* @return string
|
||||
* @throws
|
||||
*/
|
||||
public function string($name, $isNeed = FALSE, $length = NULL)
|
||||
{
|
||||
$string = $this->required($name, $isNeed);
|
||||
if ($string === null || $length === null) {
|
||||
return $string;
|
||||
}
|
||||
if (!is_string($string)) {
|
||||
$string = json_encode($string, JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
$_length = strlen($string);
|
||||
if (is_array($length)) {
|
||||
if (count($length) < 2) {
|
||||
array_unshift($length, 0);
|
||||
}
|
||||
$this->between($_length, ...$length);
|
||||
} else if (is_numeric($length) && $_length != $length) {
|
||||
throw new RequestException("The length of the string must be $length characters");
|
||||
}
|
||||
return $string;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $_length
|
||||
* @param $min
|
||||
* @param $max
|
||||
* @throws RequestException
|
||||
*/
|
||||
private function between($_length, $min, $max)
|
||||
{
|
||||
if ($min !== NULL && $_length < $min) {
|
||||
throw new RequestException("The minimum value cannot be lower than $min");
|
||||
}
|
||||
if ($max !== NULL && $_length > $max) {
|
||||
throw new RequestException("Maximum cannot exceed $max, has length " . $_length);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param bool $isNeed
|
||||
*
|
||||
* @return string
|
||||
* @throws RequestException
|
||||
*/
|
||||
public function email($name, $isNeed = FALSE)
|
||||
{
|
||||
$email = $this->required($name, $isNeed);
|
||||
if ($email === null) {
|
||||
return null;
|
||||
}
|
||||
if (!preg_match('/^\w+([.-_]\w+)+@\w+(\.\w+)+$/', $email)) {
|
||||
throw new RequestException("Request parameter $name is in the wrong format", 4001);
|
||||
}
|
||||
return $email;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param bool $isNeed
|
||||
*
|
||||
* @return string
|
||||
* @throws RequestException
|
||||
*/
|
||||
public function bool($name, $isNeed = FALSE)
|
||||
{
|
||||
$email = $this->required($name, $isNeed);
|
||||
if ($email === null) {
|
||||
return false;
|
||||
}
|
||||
return (bool)$email;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param null $default
|
||||
*
|
||||
* @return mixed|null
|
||||
* @throws RequestException
|
||||
*/
|
||||
public function timestamp($name, $default = NULL)
|
||||
{
|
||||
$value = $this->required($name, false);
|
||||
if ($value === null) {
|
||||
return $default;
|
||||
}
|
||||
if (!is_numeric($value)) {
|
||||
throw new RequestException('The request param :attribute not is a timestamp value');
|
||||
}
|
||||
if (strlen((string)$value) != 10) {
|
||||
throw new RequestException('The request param :attribute not is a timestamp value');
|
||||
}
|
||||
if (!date('YmdHis', $value)) {
|
||||
throw new RequestException('The request param :attribute format error', 4001);
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param null $default
|
||||
*
|
||||
* @return mixed|null
|
||||
* @throws RequestException
|
||||
*/
|
||||
public function datetime($name, $default = NULL)
|
||||
{
|
||||
$value = $this->required($name, false);
|
||||
if ($value === null) {
|
||||
return $default;
|
||||
}
|
||||
$match = '/^\d{4}.*?([1-12]).*([1-31]).*?[0-23].*?[0-59].*?[0-59].*?$/';
|
||||
$match = preg_match($match, $value, $result);
|
||||
if (!$match || $result[0] != $value) {
|
||||
throw new RequestException('The request param :attribute format error', 4001);
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param null $default
|
||||
* @return mixed|null
|
||||
* @throws RequestException
|
||||
*/
|
||||
public function ip($name, $default = NULL)
|
||||
{
|
||||
$value = $this->required($name, false);
|
||||
if ($value == NULL) {
|
||||
return $default;
|
||||
}
|
||||
$match = preg_match('/^\d{1,3}(\.\d{1,3}){3}$/', $value, $result);
|
||||
if (!$match || $result[0] != $value) {
|
||||
throw new RequestException('The request param :attribute format error', 4001);
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @return mixed|null
|
||||
*/
|
||||
public function __get($name)
|
||||
{
|
||||
$load = $this->load();
|
||||
|
||||
return $load[$name] ?? null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,424 @@
|
||||
<?php
|
||||
|
||||
namespace HttpServer\Http;
|
||||
|
||||
use Snowflake\Core\Help;
|
||||
use Exception;
|
||||
use HttpServer\Application;
|
||||
use HttpServer\IInterface\AuthIdentity;
|
||||
|
||||
defined('REQUEST_OK') or define('REQUEST_OK', 0);
|
||||
defined('REQUEST_FAIL') or define('REQUEST_FAIL', 500);
|
||||
|
||||
/**
|
||||
* Class HttpRequest
|
||||
*
|
||||
* @package BeReborn\HttpRequest
|
||||
*
|
||||
* @property-read $isPost
|
||||
* @property-read $isGet
|
||||
* @property-read $isOption
|
||||
* @property-read $isDelete
|
||||
* @property-read $isHttp
|
||||
* @property-read $method
|
||||
* @property-read $identity
|
||||
* @property-read $isPackage
|
||||
* @property-read $isReceive
|
||||
*/
|
||||
class Request extends Application
|
||||
{
|
||||
|
||||
/** @var int $fd */
|
||||
public $fd = 0;
|
||||
|
||||
/** @var HttpParams */
|
||||
public $params;
|
||||
|
||||
/** @var HttpHeaders */
|
||||
public $headers;
|
||||
|
||||
/** @var bool */
|
||||
public $isCli = FALSE;
|
||||
|
||||
/** @var float */
|
||||
public $startTime;
|
||||
|
||||
public $uri = '';
|
||||
|
||||
public $statusCode = 200;
|
||||
|
||||
/** @var string[] */
|
||||
private $explode = [];
|
||||
|
||||
const PLATFORM_MAC_OX = 'mac';
|
||||
const PLATFORM_IPHONE = 'iphone';
|
||||
const PLATFORM_ANDROID = 'android';
|
||||
const PLATFORM_WINDOWS = 'windows';
|
||||
|
||||
|
||||
/**
|
||||
* @var AuthIdentity|null
|
||||
*/
|
||||
private $_grant = null;
|
||||
|
||||
|
||||
/**
|
||||
* @param $fd
|
||||
*/
|
||||
public function setFd($fd)
|
||||
{
|
||||
$this->fd = $fd;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isFavicon()
|
||||
{
|
||||
return $this->getUri() === 'favicon.ico';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getIdentity()
|
||||
{
|
||||
return $this->_grant;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isHead()
|
||||
{
|
||||
$result = $this->headers->getHeader('request_method') == 'head';
|
||||
if ($result) {
|
||||
$this->setStatus(101);
|
||||
} else {
|
||||
$this->setStatus(200);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $status
|
||||
* @return mixed
|
||||
*/
|
||||
public function setStatus($status)
|
||||
{
|
||||
return $this->statusCode = $status;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getStatus()
|
||||
{
|
||||
return $this->statusCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function getIsPackage()
|
||||
{
|
||||
return $this->headers->getHeader('request_method') == 'package';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function getIsReceive()
|
||||
{
|
||||
return $this->headers->getHeader('request_method') == 'receive';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $value
|
||||
*/
|
||||
public function setGrantAuthorization($value)
|
||||
{
|
||||
$this->_grant = $value;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function hasGrant()
|
||||
{
|
||||
return $this->_grant !== null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function parseUri()
|
||||
{
|
||||
$array = [];
|
||||
$explode = explode('/', $this->headers->getHeader('request_uri'));
|
||||
foreach ($explode as $item) {
|
||||
if (empty($item)) {
|
||||
continue;
|
||||
}
|
||||
$array[] = $item;
|
||||
}
|
||||
return $this->uri = implode('/', ($this->explode = $array));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getExplode()
|
||||
{
|
||||
return $this->explode;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function getCurrent()
|
||||
{
|
||||
return current($this->explode);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getUri()
|
||||
{
|
||||
if (!$this->headers) {
|
||||
return 'command exec.';
|
||||
}
|
||||
if (!empty($this->uri)) {
|
||||
return $this->uri;
|
||||
}
|
||||
$uri = $this->headers->getHeader('request_uri');
|
||||
$uri = ltrim($uri, '/');
|
||||
if (empty($uri)) return '/';
|
||||
return $uri;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return mixed|string
|
||||
* @throws Exception
|
||||
*/
|
||||
public function adapter()
|
||||
{
|
||||
if (!$this->isHead()) {
|
||||
return router()->runHandler();
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function getPlatform()
|
||||
{
|
||||
$user = $this->headers->getHeader('user-agent');
|
||||
$match = preg_match('/\(.*\)?/', $user, $output);
|
||||
if (!$match || count($output) < 1) {
|
||||
return null;
|
||||
}
|
||||
$output = strtolower(array_shift($output));
|
||||
if (strpos('mac', $output)) {
|
||||
return 'mac';
|
||||
} else if (strpos('iphone', $output)) {
|
||||
return 'iphone';
|
||||
} else if (strpos('android', $output)) {
|
||||
return 'android';
|
||||
} else if (strpos('windows', $output)) {
|
||||
return 'windows';
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isIos()
|
||||
{
|
||||
return $this->getPlatform() == static::PLATFORM_IPHONE;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isAndroid()
|
||||
{
|
||||
return $this->getPlatform() == static::PLATFORM_ANDROID;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isMacOs()
|
||||
{
|
||||
return $this->getPlatform() == static::PLATFORM_MAC_OX;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isWindows()
|
||||
{
|
||||
return $this->getPlatform() == static::PLATFORM_WINDOWS;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function getIsPost()
|
||||
{
|
||||
return $this->getMethod() == 'post';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getIsHttp()
|
||||
{
|
||||
if (!app()->has('socket')) {
|
||||
return false;
|
||||
}
|
||||
$socket = \BeReborn::$app->getSocket()->getServer();
|
||||
if (empty($this->fd)) {
|
||||
return false;
|
||||
}
|
||||
return $socket->exist($this->fd) && !$socket->isEstablished($this->fd);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function getIsOption()
|
||||
{
|
||||
return $this->getMethod() == 'options';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function getIsGet()
|
||||
{
|
||||
return $this->getMethod() == 'get';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function getIsDelete()
|
||||
{
|
||||
return $this->getMethod() == 'delete';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*
|
||||
* 获取请求类型
|
||||
*/
|
||||
public function getMethod()
|
||||
{
|
||||
$head = $this->headers->getHeader('request_method');
|
||||
return strtolower($head);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function getIsCli()
|
||||
{
|
||||
return $this->isCli === TRUE;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param $value
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public function __set($name, $value)
|
||||
{
|
||||
$method = 'set' . ucfirst($name);
|
||||
if (method_exists($this, $method)) {
|
||||
$this->$method($value);
|
||||
} else {
|
||||
parent::__set($name, $value); // TODO: Change the autogenerated stub
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed|null
|
||||
*/
|
||||
public function getIp()
|
||||
{
|
||||
$headers = $this->headers->getHeaders();
|
||||
if (!empty($headers['x-forwarded-for'])) return $headers['x-forwarded-for'];
|
||||
if (!empty($headers['request-ip'])) return $headers['request-ip'];
|
||||
if (!empty($headers['remote_addr'])) return $headers['remote_addr'];
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getRuntime()
|
||||
{
|
||||
return sprintf('%.5f', microtime(TRUE) - $this->startTime);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getDebug()
|
||||
{
|
||||
$mainstay = sprintf("%.6f", microtime(true)); // 带毫秒的时间戳
|
||||
|
||||
$timestamp = floor($mainstay); // 时间戳
|
||||
$milliseconds = round(($mainstay - $timestamp) * 1000); // 毫秒
|
||||
|
||||
$datetime = date("Y-m-d H:i:s", $timestamp) . '.' . $milliseconds;
|
||||
|
||||
$tmp = [
|
||||
'[Debug ' . $datetime . '] ',
|
||||
$this->getIp(),
|
||||
$this->getUri(),
|
||||
'`' . $this->headers->getHeader('user-agent') . '`',
|
||||
$this->getRuntime()
|
||||
];
|
||||
|
||||
return implode(' ', $tmp);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $request
|
||||
* @return Request
|
||||
*/
|
||||
public static function create($request)
|
||||
{
|
||||
$sRequest = new Request();
|
||||
$sRequest->fd = $request->fd;
|
||||
$sRequest->startTime = microtime(true);
|
||||
$sRequest->params = new HttpParams(Help::toArray($request->rawContent()), $request->get, $request->files);
|
||||
if (!empty($request->post)) {
|
||||
$sRequest->params->setPosts($request->post ?? []);
|
||||
}
|
||||
$headers = $request->server;
|
||||
if (!empty($request->header)) {
|
||||
$headers = array_merge($headers, $request->header);
|
||||
}
|
||||
$sRequest->headers = new HttpHeaders($headers);
|
||||
$sRequest->parseUri();
|
||||
return $sRequest;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,226 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: whwyy
|
||||
* Date: 2018/4/24 0024
|
||||
* Time: 19:39
|
||||
*/
|
||||
|
||||
namespace HttpServer\Http;
|
||||
|
||||
use HttpServer\Application;
|
||||
use HttpServer\Http\Formatter\HtmlFormatter;
|
||||
use HttpServer\Http\Formatter\JsonFormatter;
|
||||
use HttpServer\Http\Formatter\XmlFormatter;
|
||||
use Exception;
|
||||
use Snowflake\Core\Help;
|
||||
use Snowflake\Exception\ComponentException;
|
||||
use Snowflake\Snowflake;
|
||||
use Swoole\Http\Response as SResponse;
|
||||
|
||||
/**
|
||||
* Class Response
|
||||
* @package BeReborn\Http
|
||||
*/
|
||||
class Response extends Application
|
||||
{
|
||||
|
||||
const JSON = 'json';
|
||||
const XML = 'xml';
|
||||
const HTML = 'html';
|
||||
|
||||
/** @var string */
|
||||
public $format = null;
|
||||
|
||||
/** @var int */
|
||||
public $statusCode = 200;
|
||||
|
||||
/** @var SResponse */
|
||||
public $response;
|
||||
public $isWebSocket = false;
|
||||
public $headers = [];
|
||||
|
||||
private $startTime = 0;
|
||||
|
||||
private $_format_maps = [
|
||||
self::JSON => JsonFormatter::class,
|
||||
self::XML => XmlFormatter::class,
|
||||
self::HTML => HtmlFormatter::class
|
||||
];
|
||||
|
||||
public $fd = 0;
|
||||
|
||||
/**
|
||||
* @param $format
|
||||
* @return $this
|
||||
*/
|
||||
public function setFormat($format)
|
||||
{
|
||||
$this->format = $format;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 清理无用数据
|
||||
*/
|
||||
public function clear()
|
||||
{
|
||||
$this->fd = 0;
|
||||
$this->isWebSocket = false;
|
||||
$this->format = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getContentType()
|
||||
{
|
||||
if ($this->format == null || $this->format == static::JSON) {
|
||||
return 'application/json;charset=utf-8';
|
||||
} else if ($this->format == static::XML) {
|
||||
return 'application/xml;charset=utf-8';
|
||||
} else {
|
||||
return 'text/html;charset=utf-8';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function sender()
|
||||
{
|
||||
return $this->send(func_get_args());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @param $value
|
||||
*/
|
||||
public function addHeader($key, $value)
|
||||
{
|
||||
$response = Context::getContext('response');
|
||||
$response->header($key, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $context
|
||||
* @param int $statusCode
|
||||
* @param null $response
|
||||
* @return bool
|
||||
* @throws Exception
|
||||
*/
|
||||
public function send($context = '', $statusCode = 200, $response = null)
|
||||
{
|
||||
$sendData = $this->parseData($context);
|
||||
if ($response instanceof SResponse) {
|
||||
$this->response = $response;
|
||||
}
|
||||
if ($this->response instanceof SResponse) {
|
||||
return $this->sendData($this->response, $sendData, $statusCode);
|
||||
} else {
|
||||
return $this->printResult($sendData);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $context
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
private function parseData($context)
|
||||
{
|
||||
if (isset($this->_format_maps[$this->format])) {
|
||||
$config['class'] = $this->_format_maps[$this->format];
|
||||
} else {
|
||||
$config['class'] = HtmlFormatter::class;
|
||||
}
|
||||
$formatter = Snowflake::createObject($config);
|
||||
return $formatter->send($context)->getData();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $result
|
||||
* @return string
|
||||
* @throws Exception
|
||||
*/
|
||||
private function printResult($result)
|
||||
{
|
||||
$result = Help::toString($result);
|
||||
|
||||
$string = 'Command Result: ' . PHP_EOL;
|
||||
$string .= empty($result) ? 'success!' : $result . PHP_EOL;
|
||||
$string .= 'Command Success!' . PHP_EOL;
|
||||
echo $string;
|
||||
|
||||
$event = Snowflake::get()->event;
|
||||
$event->trigger('CONSOLE_END');
|
||||
|
||||
return 'ok';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $response
|
||||
* @param $sendData
|
||||
* @param $status
|
||||
* @return mixed
|
||||
*/
|
||||
private function sendData($response, $sendData, $status)
|
||||
{
|
||||
$response->status($status);
|
||||
$response->header('Content-Type', $this->getContentType());
|
||||
$response->header('Access-Control-Allow-Origin', '*');
|
||||
$response->header('Run-Time', $this->getRuntime());
|
||||
return $response->end($sendData);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $url
|
||||
* @param array $param
|
||||
* @return int
|
||||
*/
|
||||
public function redirect($url, array $param = [])
|
||||
{
|
||||
if (!empty($param)) {
|
||||
$url .= '?' . http_build_query($param);
|
||||
}
|
||||
$url = ltrim($url, '/');
|
||||
if (!preg_match('/^http/', $url)) {
|
||||
$url = '/' . $url;
|
||||
}
|
||||
return $this->response->redirect($url);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param null $response
|
||||
* @return mixed
|
||||
* @throws ComponentException
|
||||
*/
|
||||
public static function create($response = null)
|
||||
{
|
||||
$ciResponse = Snowflake::get()->clone('response');
|
||||
$ciResponse->response = $response;
|
||||
$ciResponse->startTime = microtime(true);
|
||||
$ciResponse->format = self::JSON;
|
||||
return $ciResponse;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function sendNotFind()
|
||||
{
|
||||
$this->format = static::HTML;
|
||||
$this->send('', 404);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getRuntime()
|
||||
{
|
||||
return sprintf('%.5f', microtime(TRUE) - $this->startTime);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user