1
0
Fork 0
mirror of synced 2024-06-03 03:14:50 +12:00
appwrite/src/Appwrite/Swoole/Request.php

339 lines
8.3 KiB
PHP
Raw Normal View History

2020-06-27 00:27:58 +12:00
<?php
2020-07-07 16:40:18 +12:00
namespace Appwrite\Swoole;
2020-06-27 00:27:58 +12:00
use Utopia\Request as UtopiaRequest;
use Swoole\Http\Request as SwooleRequest;
class Request extends UtopiaRequest
{
/**
* Swoole Request Object
*
* @var SwooleRequest
*/
protected $swoole;
2020-06-27 00:27:58 +12:00
/**
* Request constructor.
*/
public function __construct(SwooleRequest $request)
{
$this->swoole = $request;
}
/**
* Get Param
*
* Get param by current method name
*
* @param string $key
* @param mixed $default
* @return mixed
*/
public function getParam(string $key, $default = null)
{
2020-07-05 09:50:55 +12:00
switch($this->getMethod()) {
2020-06-27 00:27:58 +12:00
case self::METHOD_GET:
return $this->getQuery($key, $default);
break;
case self::METHOD_POST:
case self::METHOD_PUT:
case self::METHOD_PATCH:
case self::METHOD_DELETE:
return $this->getPayload($key, $default);
break;
default:
return $this->getQuery($key, $default);
}
}
/**
* Get Params
*
* Get all params of current method
*
* @return array
*/
public function getParams(): array
{
2020-07-03 05:37:24 +12:00
switch($this->getMethod()) {
2020-06-27 00:27:58 +12:00
case self::METHOD_GET:
return (!empty($this->swoole->get)) ? $this->swoole->get : [];
break;
case self::METHOD_POST:
case self::METHOD_PUT:
case self::METHOD_PATCH:
2020-07-09 01:01:42 +12:00
case self::METHOD_DELETE:
2020-06-27 00:27:58 +12:00
return $this->generateInput();
break;
default:
return (!empty($this->swoole->get)) ? $this->swoole->get : [];
}
return [];
}
/**
* Get Query
*
* Method for querying HTTP GET request parameters. If $key is not found $default value will be returned.
*
* @param string $key
* @param mixed $default
* @return mixed
*/
public function getQuery(string $key, $default = null)
{
return (isset($this->swoole->get[$key])) ? $this->swoole->get[$key] : $default;
}
/**
* Get payload
*
* Method for querying HTTP request payload parameters. If $key is not found $default value will be returned.
*
* @param string $key
* @param mixed $default
* @return mixed
*/
public function getPayload(string $key, $default = null)
{
$payload = $this->generateInput();
return (isset($payload[$key])) ? $payload[$key] : $default;
}
/**
* Get server
*
* Method for querying server parameters. If $key is not found $default value will be returned.
*
* @param string $key
* @param mixed $default
* @return mixed
*/
public function getServer(string $key, $default = null)
{
return (isset($this->swoole->server) && isset($this->swoole->server[$key])) ? $this->swoole->server[$key] : $default;
}
/**
* Get IP
*
* Returns users IP address.
* Support HTTP_X_FORWARDED_FOR header usually return
* from different proxy servers or PHP default REMOTE_ADDR
*/
public function getIP(): string
{
2020-07-03 05:37:24 +12:00
return $this->getHeader('x-forwarded-for', $this->getServer('remote_addr', '0.0.0.0'));
}
/**
* Get Protocol
*
* Returns request protocol.
* Support HTTP_X_FORWARDED_PROTO header usually return
* from different proxy servers or PHP default REQUEST_SCHEME
*
* @return string
*/
public function getProtocol(): string
{
2020-07-13 04:51:50 +12:00
$protocol = $this->getHeader('x-forwarded-proto', $this->getServer('server_protocol', 'https'));
if($protocol === 'HTTP/1.1') {
2020-07-03 05:37:24 +12:00
return 'http';
}
2020-07-13 04:51:50 +12:00
return $protocol;
2020-07-03 05:37:24 +12:00
}
/**
* Get Port
*
* Returns request port.
*
* @return string
*/
public function getPort(): string
{
2020-07-03 05:58:24 +12:00
return $this->getHeader('x-forwarded-port', (string)\parse_url($this->getProtocol().'://'.$this->getHeader('x-forwarded-host', $this->getHeader('host')), PHP_URL_PORT));
2020-07-03 05:37:24 +12:00
}
/**
* Get Hostname
*
* Returns request hostname.
*
* @return string
*/
public function getHostname(): string
{
return \parse_url($this->getProtocol().'://'.$this->getHeader('x-forwarded-host', $this->getHeader('host')), PHP_URL_HOST);
2020-06-27 00:27:58 +12:00
}
/**
* Get Method
*
* Return HTTP request method
*
* @return string
*/
2020-07-03 09:48:02 +12:00
public function getMethod(): string
2020-06-27 00:27:58 +12:00
{
return $this->getServer('request_method', 'UNKNOWN');
}
2020-07-05 09:50:55 +12:00
/**
* Get URI
*
* Return HTTP request URI
*
* @return string
*/
public function getURI(): string
{
return $this->getServer('request_uri', '');
}
2020-07-03 09:48:02 +12:00
/**
* Get Referer
*
* Return HTTP referer header
*
* @return string
*/
public function getReferer(string $default = ''): string
{
return $this->getHeader('referer', '');
}
/**
* Get Origin
*
* Return HTTP origin header
*
* @return string
*/
public function getOrigin(string $default = ''): string
{
return $this->getHeader('origin', $default);
}
2020-07-04 03:14:51 +12:00
/**
* Get User Agent
*
* Return HTTP user agent header
*
* @return string
*/
public function getUserAgent(string $default = ''): string
{
return $this->getHeader('user-agent', $default);
}
/**
* Get Accept
*
* Return HTTP accept header
*
* @return string
*/
public function getAccept(string $default = ''): string
{
return $this->getHeader('accept', $default);
}
2020-06-27 00:27:58 +12:00
/**
* Get files
*
* Method for querying upload files data. If $key is not found empty array will be returned.
*
* @param string $key
* @return array
*/
public function getFiles($key): array
{
2020-07-01 20:55:14 +12:00
$key = strtolower($key);
2020-06-27 00:27:58 +12:00
return (isset($this->swoole->files[$key])) ? $this->swoole->files[$key] : [];
}
/**
* Get cookie
*
* Method for querying HTTP cookie parameters. If $key is not found $default value will be returned.
*
* @param string $key
* @param string $default
*
* @return string
2020-06-27 00:27:58 +12:00
*/
public function getCookie(string $key, string $default = ''): string
{
2020-07-01 20:55:14 +12:00
$key = strtolower($key);
2020-06-27 00:27:58 +12:00
return (isset($this->swoole->cookie[$key])) ? $this->swoole->cookie[$key] : $default;
}
/**
* Get header
*
* Method for querying HTTP header parameters. If $key is not found $default value will be returned.
*
* @param string $key
* @param string $default
* @return string
*/
public function getHeader(string $key, string $default = ''): string
{
2020-07-01 20:55:14 +12:00
return (isset($this->swoole->header[$key])) ? $this->swoole->header[$key] : $default;
2020-06-27 00:27:58 +12:00
}
/**
* Generate input
*
* Generate PHP input stream and parse it as an array in order to handle different content type of requests
*
* @return array
*/
protected function generateInput(): array
{
if (null === $this->payload) {
$contentType = $this->getHeader('content-type');
// Get content-type without the charset
$length = strpos($contentType, ';');
$length = (empty($length)) ? strlen($contentType) : $length;
$contentType = substr($contentType, 0, $length);
switch ($contentType) {
case 'application/json':
2020-07-03 02:32:34 +12:00
$this->payload = json_decode($this->swoole->rawContent(), true);
2020-06-27 00:27:58 +12:00
break;
default:
2020-07-09 02:02:14 +12:00
$this->payload = $this->swoole->post;
2020-06-27 00:27:58 +12:00
break;
}
if(empty($this->payload)) { // Make sure we return same data type even if json payload is empty or failed
$this->payload = [];
}
}
return $this->payload;
}
/**
* Generate headers
*
* Parse request headers as an array for easy querying using the getHeader method
*
* @return array
*/
protected function generateHeaders(): array
{
return $this->swoole->header;
}
}