改名
This commit is contained in:
@@ -16,88 +16,100 @@ namespace HttpServer\Http;
|
|||||||
trait HttpHeaders
|
trait HttpHeaders
|
||||||
{
|
{
|
||||||
|
|
||||||
private array $_headers = [];
|
private array $_headers = [];
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $headers
|
* @param array $headers
|
||||||
*/
|
*/
|
||||||
public function setHeaders(array $headers): void
|
public function setHeaders(array $headers): void
|
||||||
{
|
{
|
||||||
$this->_headers = $headers;
|
$this->_headers = $headers;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function toArray(): array
|
public function toArray(): array
|
||||||
{
|
{
|
||||||
return $this->_headers;
|
return $this->_headers;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $name
|
* @param $name
|
||||||
* @return mixed|null
|
* @param null $default
|
||||||
*/
|
* @return mixed
|
||||||
public function getHeader($name, $default = null): mixed
|
*/
|
||||||
{
|
public function getHeader($name, $default = null): mixed
|
||||||
return $this->_headers[$name] ?? $default;
|
{
|
||||||
}
|
return $this->_headers[$name] ?? $default;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @param $name
|
||||||
*/
|
* @param $default
|
||||||
public function getContentType(): string
|
* @return mixed
|
||||||
{
|
*/
|
||||||
return $this->_headers['content-type'] ?? '';
|
public function header($name, $default = null): mixed
|
||||||
}
|
{
|
||||||
|
return $this->getHeader($name, $default);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string|null
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getRequestUri(): ?string
|
public function getContentType(): string
|
||||||
{
|
{
|
||||||
return $this->_headers['request_uri'];
|
return $this->_headers['content-type'] ?? '';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string|null
|
* @return string|null
|
||||||
*/
|
*/
|
||||||
public function getRequestMethod(): ?string
|
public function getRequestUri(): ?string
|
||||||
{
|
{
|
||||||
return $this->_headers['request_method'];
|
return $this->_headers['request_uri'];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return mixed
|
* @return string|null
|
||||||
*/
|
*/
|
||||||
public function getAgent(): mixed
|
public function getRequestMethod(): ?string
|
||||||
{
|
{
|
||||||
return $this->_headers['user-agent'];
|
return $this->_headers['request_method'];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $name
|
* @return mixed
|
||||||
* @return bool
|
*/
|
||||||
*/
|
public function getAgent(): mixed
|
||||||
public function exists($name): bool
|
{
|
||||||
{
|
return $this->_headers['user-agent'];
|
||||||
return $this->_headers[$name] ?? null === null;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array
|
* @param $name
|
||||||
*/
|
* @return bool
|
||||||
public function getHeaders(): array
|
*/
|
||||||
{
|
public function exists($name): bool
|
||||||
return $this->_headers;
|
{
|
||||||
}
|
return $this->_headers[$name] ?? null === null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getHeaders(): array
|
||||||
|
{
|
||||||
|
return $this->_headers;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ use HttpServer\Http\Context;
|
|||||||
use HttpServer\Http\Request;
|
use HttpServer\Http\Request;
|
||||||
use HttpServer\Http\Response;
|
use HttpServer\Http\Response;
|
||||||
use HttpServer\IInterface\Middleware;
|
use HttpServer\IInterface\Middleware;
|
||||||
|
use Server\RequestInterface;
|
||||||
use Snowflake\Snowflake;
|
use Snowflake\Snowflake;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -32,15 +33,13 @@ class CoreMiddleware implements Middleware
|
|||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function onHandler(Request $request, Closure $next): mixed
|
public function onHandler(RequestInterface $request, Closure $next): mixed
|
||||||
{
|
{
|
||||||
$headers = $request->headers;
|
|
||||||
|
|
||||||
/** @var Response $response */
|
/** @var Response $response */
|
||||||
$response = Snowflake::getApp('response');
|
$response = Snowflake::getApp('response');
|
||||||
$response->addHeader('Access-Control-Allow-Origin', '*');
|
$response->addHeader('Access-Control-Allow-Origin', '*');
|
||||||
$response->addHeader('Access-Control-Allow-Headers', $headers->get('access-control-request-headers'));
|
$response->addHeader('Access-Control-Allow-Headers', $request->header('access-control-request-headers'));
|
||||||
$response->addHeader('Access-Control-Request-Method', $headers->get('access-control-request-method'));
|
$response->addHeader('Access-Control-Request-Method', $request->header('access-control-request-method'));
|
||||||
|
|
||||||
return $next($request);
|
return $next($request);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user