This commit is contained in:
2021-08-01 15:03:49 +08:00
parent 57e5bd000a
commit 3d314cc76f
3 changed files with 135 additions and 155 deletions
+23 -33
View File
@@ -17,12 +17,15 @@ class HttpHeaders
{
private array $_headers = [];
/**
* @param string $uri
*/
public function setRequestUri(string $uri)
{
$this->replace('request_uri', $uri);
$this->_headers['request_uri'] = $uri;
}
@@ -31,7 +34,7 @@ class HttpHeaders
*/
public function setRequestMethod(string $method)
{
$this->replace('request_method', $method);
$this->_headers['request_method'] = $method;
}
@@ -41,7 +44,7 @@ class HttpHeaders
*/
public function replace($name, $value)
{
$this->addHeaders([$name => $value]);
$this->_headers[$name] = $value;
}
/**
@@ -50,7 +53,7 @@ class HttpHeaders
*/
public function addHeader($name, $value)
{
$this->addHeaders([$name => $value]);
$this->_headers[$name] = $value;
}
/**
@@ -59,14 +62,9 @@ class HttpHeaders
*/
public function addHeaders(array $headers): static
{
if (empty($headers)) {
return $this;
foreach ($headers as $key => $header) {
$this->_headers[$key] = $header;
}
$request = Context::getContext('request');
if (!empty($request->headers)) {
$headers = array_merge($request->headers, $headers);
}
$request->headers = $headers;
return $this;
}
@@ -75,7 +73,7 @@ class HttpHeaders
*/
public function toArray(): array
{
return $this->___call();
return $this->_headers;
}
/**
@@ -84,11 +82,7 @@ class HttpHeaders
*/
public function getHeader($name): ?string
{
$headers = $this->___call();
if (!isset($headers[$name])) {
return null;
}
return $headers[$name];
return $this->_headers[$name] ?? null;
}
@@ -99,10 +93,16 @@ class HttpHeaders
*/
public function get($name, $default = null): mixed
{
if (($value = $this->getHeader($name)) === null) {
return $default;
}
return $value;
return $this->_headers[$name] ?? $default;
}
/**
* @return string
*/
public function getContentType(): string
{
return $this->getHeader('content-type');
}
@@ -112,8 +112,7 @@ class HttpHeaders
*/
public function exists($name): bool
{
$headers = $this->___call();
return isset($headers[$name]) && $headers[$name] != null;
return isset($this->_headers[$name]) && $this->_headers[$name] != null;
}
@@ -122,16 +121,7 @@ class HttpHeaders
*/
public function getHeaders(): array
{
return $this->___call();
}
/**
* @return mixed
*/
private function ___call(): array
{
return Context::getContext('request')->header ?? [];
return $this->_headers;
}
+59 -65
View File
@@ -14,6 +14,7 @@ use HttpServer\Exception\RequestException;
use JetBrains\PhpStorm\Pure;
use ReflectionException;
use Snowflake\Core\Json;
use Snowflake\Core\Xml;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake;
@@ -24,15 +25,17 @@ use Snowflake\Snowflake;
class HttpParams
{
private string|array|null $body = [];
/** @var array */
private array $_gets = [];
private array $_posts = [];
/** @var array */
private array $gets = [];
private array $_files = [];
/** @var array */
private array $files = [];
private array $socket = [];
private mixed $_rawContent = [];
/**
@@ -40,7 +43,7 @@ class HttpParams
*/
public function getRawContent(): mixed
{
return $this->getRequest()->rawContent();
return $this->_rawContent;
}
/**
@@ -61,67 +64,58 @@ class HttpParams
return;
}
foreach ($data as $key => $vla) {
$this->body[$key] = $vla;
$this->_posts[$key] = $vla;
}
}
/**
* 删除参数
* @param array $files
*/
public function clearBody()
public function setFiles(array $files): void
{
$this->body = [];
$this->_files = $files;
}
/**
* 删除参数
* @param array $gets
*/
public function clearGet()
public function setGets(array $gets): void
{
$this->gets = [];
$this->_gets = $gets;
}
/**
* 清空文件上传信息
* @param mixed $content
* @param Request $contextType
*/
public function clearFile()
public function setRawContent(mixed $content, Request $contextType)
{
$this->files = [];
if (empty($content)) {
return;
}
$context_type = $contextType->headers->getContentType();
if (str_contains($context_type, 'json')) {
$this->setPosts(json_decode($content));
} else if (str_contains($context_type, 'xml')) {
$this->setPosts(Xml::toArray($content));
} else {
$this->_rawContent = $content;
}
}
/**
* @return mixed
* @return array
*/
public function getBody(): mixed
public function getBody(): array
{
return $this->body;
return $this->_posts;
}
/**
* @return mixed
*/
public function getBodyAndClear(): mixed
{
$data = $this->body['body'];
$this->clearBody();
return $data;
}
/**
* @param string $key
* @param string $value
*/
public function addGetParam(string $key, string $value)
{
$this->gets[$key] = $value;
}
/**
* @return int
*/
@@ -130,6 +124,18 @@ class HttpParams
return (int)$this->get('page', 1);
}
/**
* @param $name
* @param null $default
* @return mixed
*/
public function query($name, $default = null): mixed
{
return $this->_gets[$name] ?? $default;
}
/**
* @return int
*/
@@ -147,7 +153,7 @@ class HttpParams
*/
public function get($name, $defaultValue = null, $call = null): mixed
{
return $this->getRequest()->get[$name] ?? $defaultValue;
return $this->_gets[$name] ?? $defaultValue;
}
/**
@@ -158,7 +164,7 @@ class HttpParams
*/
public function post($name, $defaultValue = null, $call = null): mixed
{
$data = $this->getRequest()->post[$name] ?? $defaultValue;
$data = $this->_posts[$name] ?? $defaultValue;
if ($call !== null) {
$data = call_user_func($call, $data);
}
@@ -186,26 +192,23 @@ class HttpParams
*/
public function gets(): array
{
return $this->getRequest()->get;
return $this->_gets;
}
/**
* @return array
*/
#[Pure] public function params(): array
public function params(): array
{
$request = $this->getRequest();
return array_merge($request->post ?? [], $request->files ?? []);
return array_merge($this->_posts ?? [], $this->_files ?? []);
}
/**
* @return array
*/
#[Pure] public function load(): array
public function load(): array
{
$request = $this->getRequest();
return array_merge($request->files ?? [], $request->post ?? [], $request->gets ?? []);
return array_merge($this->_files ?? [], $this->_posts ?? [], $this->_gets ?? []);
}
/**
@@ -215,7 +218,7 @@ class HttpParams
*/
public function array($name, array $defaultValue = []): mixed
{
return $this->getRequest()->post[$name] ?? $defaultValue;
return $this->_posts[$name] ?? $defaultValue;
}
/**
@@ -226,12 +229,12 @@ class HttpParams
*/
public function file($name): File|null
{
$param = $this->getRequest()->files[$name] ?? null;
if (empty($param)) {
return null;
$param = $this->_files[$name] ?? null;
if (!empty($param)) {
$param['class'] = File::class;
return Snowflake::createObject($param);
}
$param['class'] = File::class;
return Snowflake::createObject($param);
return null;
}
/**
@@ -242,7 +245,7 @@ class HttpParams
*/
private function required(string $name, bool $isNeed = false): mixed
{
$int = $this->getRequest()->post[$name] ?? NULL;
$int = $this->_posts[$name] ?? NULL;
if (is_null($int) && $isNeed === true) {
throw new RequestException("You need to add request parameter $name");
}
@@ -418,13 +421,4 @@ class HttpParams
return $load[$name] ?? null;
}
/**
* @return mixed
*/
private function getRequest(): \Swoole\Http\Request
{
return Context::getContext('request');
}
}
+53 -57
View File
@@ -3,16 +3,13 @@ declare(strict_types=1);
namespace HttpServer\Http;
use Annotation\Inject;
use Exception;
use HttpServer\Abstracts\HttpService;
use HttpServer\IInterface\AuthIdentity;
use JetBrains\PhpStorm\Pure;
use ReflectionException;
use Server\ServerManager;
use Snowflake\Core\Json;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake;
use function router;
defined('REQUEST_OK') or define('REQUEST_OK', 0);
defined('REQUEST_FAIL') or define('REQUEST_FAIL', 500);
@@ -38,25 +35,21 @@ class Request extends HttpService
public int $fd = 0;
/**
* @var HttpParams|null
*/
#[Inject(HttpParams::class)]
public ?HttpParams $params = null;
/**
* @var HttpHeaders|null
*/
#[Inject(HttpHeaders::class)]
public ?HttpHeaders $headers = null;
public bool $isCli = FALSE;
public float $startTime;
public ?array $clientInfo;
public string $uri = '';
private string $_method = '';
private string $_uri = '';
public int $statusCode = 200;
@@ -76,6 +69,10 @@ class Request extends HttpService
const HTTP_SOCKET = 'sw::socket';
private string $_platform = '';
/**
* @var AuthIdentity|null
*/
@@ -117,9 +114,9 @@ class Request extends HttpService
/**
* @return bool
*/
public function isFavicon(): bool
#[Pure] public function isFavicon(): bool
{
return $this->getUri() === 'favicon.ico';
return $this->_uri === 'favicon.ico';
}
/**
@@ -135,7 +132,7 @@ class Request extends HttpService
*/
public function isHead(): bool
{
$result = $this->headers->getHeader('request_method') == 'HEAD';
$result = $this->_method == 'HEAD';
if ($result) {
$this->setStatus(101);
} else {
@@ -166,7 +163,7 @@ class Request extends HttpService
*/
public function getIsPackage(): bool
{
return $this->headers->getHeader('request_method') == 'package';
return $this->_method == 'package';
}
/**
@@ -174,7 +171,7 @@ class Request extends HttpService
*/
public function getIsReceive(): bool
{
return $this->headers->getHeader('request_method') == 'receive';
return $this->_method == 'receive';
}
@@ -196,22 +193,6 @@ class Request extends HttpService
}
/**
* @return string
*/
public function parseUri(): string
{
$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[]
*/
@@ -234,31 +215,34 @@ class Request extends HttpService
*/
public function getUri(): string
{
return $this->headers->getHeader('request_uri');
return $this->_uri;
}
/**
* @return string|null
*/
public function getPlatform(): ?string
{
if (!empty($this->_platform)) {
return $this->_platform;
}
$user = $this->headers->getHeader('user-agent');
$match = preg_match('/\(.*\)?/', $user, $output);
if (!$match || count($output) < 1) {
return null;
return $this->_platform = 'unknown';
}
$output = strtolower(array_shift($output));
if (strpos('mac', $output)) {
return 'mac';
return $this->_platform = 'mac';
} else if (strpos('iphone', $output)) {
return 'iphone';
return $this->_platform = 'iphone';
} else if (strpos('android', $output)) {
return 'android';
return $this->_platform = 'android';
} else if (strpos('windows', $output)) {
return 'windows';
return $this->_platform = 'windows';
} else {
return $this->_platform = 'unknown';
}
return null;
}
/**
@@ -298,7 +282,7 @@ class Request extends HttpService
*/
public function getIsPost(): bool
{
return $this->getMethod() == 'POST';
return $this->_method == 'POST';
}
/**
@@ -315,7 +299,7 @@ class Request extends HttpService
*/
public function getIsOption(): bool
{
return $this->getMethod() == 'OPTIONS';
return $this->_method == 'OPTIONS';
}
/**
@@ -323,7 +307,7 @@ class Request extends HttpService
*/
public function getIsGet(): bool
{
return $this->getMethod() == 'GET';
return $this->_method == 'GET';
}
/**
@@ -331,7 +315,7 @@ class Request extends HttpService
*/
public function getIsDelete(): bool
{
return $this->getMethod() == 'DELETE';
return $this->_method == 'DELETE';
}
/**
@@ -341,7 +325,7 @@ class Request extends HttpService
*/
public function getMethod(): string
{
$method = $this->headers->get('request_method');
$method = $this->_method;
if (empty($method)) {
return 'GET';
}
@@ -421,9 +405,9 @@ class Request extends HttpService
* @param $router
* @return bool
*/
public function is($router): bool
#[Pure] public function is($router): bool
{
return $this->getUri() == $router;
return $this->_uri == $router;
}
/**
@@ -431,23 +415,35 @@ class Request extends HttpService
*/
public function isNotFound(): bool
{
return Json::to(404, 'Page ' . $this->getUri() . ' not found.');
return Json::to(404, 'Page ' . $this->_uri . ' not found.');
}
/**
* @param \Swoole\Http\Request $request
* @return Request
* @throws ReflectionException
* @throws NotFindClassException
* @throws Exception
*/
public static function create(\Swoole\Http\Request $request): Request
{
$request->header = array_merge($request->header, $request->server);
Context::setContext('request', $request);
/** @var Request $sRequest */
return Snowflake::app()->get('request');
$httpRequest = new Request();
$httpRequest->fd = $request->fd;
$server = ServerManager::getContext()->getServer();
$httpRequest->clientInfo = $server->getClientInfo($request->fd);
$httpRequest->headers = new HttpHeaders();
$httpRequest->headers->addHeaders(array_merge($request->header, $request->server));
$httpRequest->_uri = $httpRequest->headers->get('request_uri');
$httpRequest->_method = $httpRequest->headers->get('request_method');
$httpRequest->params = new HttpParams();
$httpRequest->params->setPosts($request->post);
$httpRequest->params->setFiles($request->files);
$httpRequest->params->setGets($request->get);
$httpRequest->params->setRawContent($request->rawContent(), $httpRequest);
return Context::setContext('request', $httpRequest);
}