Files
kiri-core/HttpServer/Http/HttpHeaders.php
T

123 lines
1.8 KiB
PHP
Raw Normal View History

2020-08-31 01:27:08 +08:00
<?php
/**
* Created by PhpStorm.
* User: admin
* Date: 2019-03-18
* Time: 14:54
*/
2020-10-29 18:17:25 +08:00
declare(strict_types=1);
2020-12-25 15:57:52 +08:00
2020-08-31 01:27:08 +08:00
namespace HttpServer\Http;
/**
* Class HttpHeaders
2020-08-31 12:38:32 +08:00
* @package Snowflake\Snowflake\Http
2020-08-31 01:27:08 +08:00
*/
class HttpHeaders
{
2021-04-07 14:07:18 +08:00
/**
2021-08-02 11:07:15 +08:00
* @return array
2021-04-07 14:07:18 +08:00
*/
2021-08-02 11:07:15 +08:00
public function toArray(): array
2021-04-07 14:07:18 +08:00
{
2021-08-02 11:07:15 +08:00
return $this->__handler__();
2021-04-07 14:07:18 +08:00
}
/**
2021-08-02 11:07:15 +08:00
* @param $name
* @return mixed|null
2021-04-07 14:07:18 +08:00
*/
2021-08-02 11:07:15 +08:00
public function getHeader($name): ?string
2021-04-07 14:07:18 +08:00
{
2021-08-02 11:07:15 +08:00
return $this->__handler__($name);
2021-04-07 14:07:18 +08:00
}
2020-08-31 01:27:08 +08:00
/**
* @param $name
2021-08-02 11:07:15 +08:00
* @param null $default
* @return mixed
2020-08-31 01:27:08 +08:00
*/
2021-08-02 11:07:15 +08:00
public function get($name, $default = null): mixed
2020-08-31 01:27:08 +08:00
{
2021-08-02 11:07:15 +08:00
return $this->__handler__($name, $default);
2020-08-31 01:27:08 +08:00
}
/**
2021-08-02 11:07:15 +08:00
* @return string
2020-08-31 01:27:08 +08:00
*/
2021-08-02 11:07:15 +08:00
public function getContentType(): string
2020-08-31 01:27:08 +08:00
{
2021-08-02 11:07:15 +08:00
return $this->__handler__('content-type');
2020-08-31 01:27:08 +08:00
}
2021-08-01 15:16:50 +08:00
/**
2021-08-02 11:07:15 +08:00
* @return string|null
2021-08-01 15:16:50 +08:00
*/
2021-08-02 11:07:15 +08:00
public function getRequestUri(): ?string
2021-08-01 15:16:50 +08:00
{
2021-08-04 18:09:37 +08:00
$uri = $this->__handler__('request_uri', '/');
if (empty($uri)) {
return '/';
}
return $uri;
2021-08-01 15:16:50 +08:00
}
2020-09-02 14:28:21 +08:00
/**
2021-08-02 11:07:15 +08:00
* @return string|null
2020-09-02 14:28:21 +08:00
*/
2021-08-02 11:07:15 +08:00
public function getRequestMethod(): ?string
2020-09-02 14:28:21 +08:00
{
2021-08-02 11:07:15 +08:00
return $this->__handler__('request_method');
2020-08-31 01:27:08 +08:00
}
/**
2020-12-25 15:57:52 +08:00
* @return mixed
2020-08-31 01:27:08 +08:00
*/
2021-08-02 11:07:15 +08:00
public function getAgent(): mixed
2020-08-31 01:27:08 +08:00
{
2021-08-02 11:07:15 +08:00
return $this->__handler__('user-agent');
2020-08-31 01:27:08 +08:00
}
/**
2021-08-01 15:03:49 +08:00
* @param $name
* @return bool
2020-08-31 01:27:08 +08:00
*/
2021-08-01 15:03:49 +08:00
public function exists($name): bool
2020-08-31 01:27:08 +08:00
{
2021-08-02 11:07:15 +08:00
return $this->__handler__($name) === null;
2020-08-31 01:27:08 +08:00
}
2021-07-27 18:21:30 +08:00
/**
2021-08-01 15:03:49 +08:00
* @return array
2021-07-27 18:21:30 +08:00
*/
2021-08-01 15:03:49 +08:00
public function getHeaders(): array
2021-07-27 18:21:30 +08:00
{
2021-08-02 11:07:15 +08:00
return $this->__handler__();
2021-07-27 18:21:30 +08:00
}
2021-08-02 11:07:15 +08:00
/**
* @param null $name
* @param null $default
* @return mixed
*/
private function __handler__($name = null, $default = null): mixed
{
/** @var \Swoole\Http\Request $context */
$context = Context::getContext(\Swoole\Http\Request::class);
if (!empty($name)) {
return $context->header[$name] ?? $default;
}
return $context->header;
}
2020-08-31 01:27:08 +08:00
}