Skip to content

Commit

Permalink
Added to sendStatus() method
Browse files Browse the repository at this point in the history
  • Loading branch information
vermakhushboo committed Feb 1, 2024
1 parent f437d8f commit d7ca3da
Showing 1 changed file with 171 additions and 1 deletion.
172 changes: 171 additions & 1 deletion src/Swoole/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Utopia\Swoole;

use Exception;
use Swoole\Http\Response as SwooleResponse;
use Utopia\Response as UtopiaResponse;

Expand All @@ -14,6 +15,154 @@ class Response extends UtopiaResponse
*/
protected SwooleResponse $swoole;

/**
* Status Code Reason
*
* @var string
*/
protected string $reason = '';

/**
* HTTP response status codes
*/
public const STATUS_CODE_CONTINUE = 100;

public const STATUS_CODE_SWITCHING_PROTOCOLS = 101;

public const STATUS_CODE_OK = 200;

public const STATUS_CODE_CREATED = 201;

public const STATUS_CODE_ACCEPTED = 202;

public const STATUS_CODE_NON_AUTHORITATIVE_INFORMATION = 203;

public const STATUS_CODE_NOCONTENT = 204;

public const STATUS_CODE_RESETCONTENT = 205;

public const STATUS_CODE_PARTIALCONTENT = 206;

public const STATUS_CODE_MULTIPLE_CHOICES = 300;

public const STATUS_CODE_MOVED_PERMANENTLY = 301;

public const STATUS_CODE_FOUND = 302;

public const STATUS_CODE_SEE_OTHER = 303;

public const STATUS_CODE_NOT_MODIFIED = 304;

public const STATUS_CODE_USE_PROXY = 305;

public const STATUS_CODE_UNUSED = 306;

public const STATUS_CODE_TEMPORARY_REDIRECT = 307;

public const STATUS_CODE_BAD_REQUEST = 400;

public const STATUS_CODE_UNAUTHORIZED = 401;

public const STATUS_CODE_PAYMENT_REQUIRED = 402;

public const STATUS_CODE_FORBIDDEN = 403;

public const STATUS_CODE_NOT_FOUND = 404;

public const STATUS_CODE_METHOD_NOT_ALLOWED = 405;

public const STATUS_CODE_NOT_ACCEPTABLE = 406;

public const STATUS_CODE_PROXY_AUTHENTICATION_REQUIRED = 407;

public const STATUS_CODE_REQUEST_TIMEOUT = 408;

public const STATUS_CODE_CONFLICT = 409;

public const STATUS_CODE_GONE = 410;

public const STATUS_CODE_LENGTH_REQUIRED = 411;

public const STATUS_CODE_PRECONDITION_FAILED = 412;

public const STATUS_CODE_REQUEST_ENTITY_TOO_LARGE = 413;

public const STATUS_CODE_REQUEST_URI_TOO_LONG = 414;

public const STATUS_CODE_UNSUPPORTED_MEDIA_TYPE = 415;

public const STATUS_CODE_REQUESTED_RANGE_NOT_SATISFIABLE = 416;

public const STATUS_CODE_EXPECTATION_FAILED = 417;

public const STATUS_CODE_TOO_EARLY = 425;

public const STATUS_CODE_TOO_MANY_REQUESTS = 429;

public const STATUS_CODE_UNAVAILABLE_FOR_LEGAL_REASONS = 451;

public const STATUS_CODE_INTERNAL_SERVER_ERROR = 500;

public const STATUS_CODE_NOT_IMPLEMENTED = 501;

public const STATUS_CODE_BAD_GATEWAY = 502;

public const STATUS_CODE_SERVICE_UNAVAILABLE = 503;

public const STATUS_CODE_GATEWAY_TIMEOUT = 504;

public const STATUS_CODE_HTTP_VERSION_NOT_SUPPORTED = 505;

/**
* @var array
*/
protected $statusCodes = [
self::STATUS_CODE_CONTINUE => 'Continue',
self::STATUS_CODE_SWITCHING_PROTOCOLS => 'Switching Protocols',
self::STATUS_CODE_OK => 'OK',
self::STATUS_CODE_CREATED => 'Created',
self::STATUS_CODE_ACCEPTED => 'Accepted',
self::STATUS_CODE_NON_AUTHORITATIVE_INFORMATION => 'Non-Authoritative Information',
self::STATUS_CODE_NOCONTENT => 'No Content',
self::STATUS_CODE_RESETCONTENT => 'Reset Content',
self::STATUS_CODE_PARTIALCONTENT => 'Partial Content',
self::STATUS_CODE_MULTIPLE_CHOICES => 'Multiple Choices',
self::STATUS_CODE_MOVED_PERMANENTLY => 'Moved Permanently',
self::STATUS_CODE_FOUND => 'Found',
self::STATUS_CODE_SEE_OTHER => 'See Other',
self::STATUS_CODE_NOT_MODIFIED => 'Not Modified',
self::STATUS_CODE_USE_PROXY => 'Use Proxy',
self::STATUS_CODE_UNUSED => '(Unused)',
self::STATUS_CODE_TEMPORARY_REDIRECT => 'Temporary Redirect',
self::STATUS_CODE_BAD_REQUEST => 'Bad Request',
self::STATUS_CODE_UNAUTHORIZED => 'Unauthorized',
self::STATUS_CODE_PAYMENT_REQUIRED => 'Payment Required',
self::STATUS_CODE_FORBIDDEN => 'Forbidden',
self::STATUS_CODE_NOT_FOUND => 'Not Found',
self::STATUS_CODE_METHOD_NOT_ALLOWED => 'Method Not Allowed',
self::STATUS_CODE_NOT_ACCEPTABLE => 'Not Acceptable',
self::STATUS_CODE_PROXY_AUTHENTICATION_REQUIRED => 'Proxy Authentication Required',
self::STATUS_CODE_REQUEST_TIMEOUT => 'Request Timeout',
self::STATUS_CODE_CONFLICT => 'Conflict',
self::STATUS_CODE_GONE => 'Gone',
self::STATUS_CODE_LENGTH_REQUIRED => 'Length Required',
self::STATUS_CODE_PRECONDITION_FAILED => 'Precondition Failed',
self::STATUS_CODE_REQUEST_ENTITY_TOO_LARGE => 'Request Entity Too Large',
self::STATUS_CODE_REQUEST_URI_TOO_LONG => 'Request-URI Too Long',
self::STATUS_CODE_UNSUPPORTED_MEDIA_TYPE => 'Unsupported Media Type',
self::STATUS_CODE_REQUESTED_RANGE_NOT_SATISFIABLE => 'Requested Range Not Satisfiable',
self::STATUS_CODE_EXPECTATION_FAILED => 'Expectation Failed',
self::STATUS_CODE_TOO_EARLY => 'Too Early',
self::STATUS_CODE_TOO_MANY_REQUESTS => 'Too Many Requests',
self::STATUS_CODE_UNAVAILABLE_FOR_LEGAL_REASONS => 'Unavailable For Legal Reasons',
self::STATUS_CODE_INTERNAL_SERVER_ERROR => 'Internal Server Error',
self::STATUS_CODE_NOT_IMPLEMENTED => 'Not Implemented',
self::STATUS_CODE_BAD_GATEWAY => 'Bad Gateway',
self::STATUS_CODE_SERVICE_UNAVAILABLE => 'Service Unavailable',
self::STATUS_CODE_GATEWAY_TIMEOUT => 'Gateway Timeout',
self::STATUS_CODE_HTTP_VERSION_NOT_SUPPORTED => 'HTTP Version Not Supported',
];

/**
* Response constructor.
*/
Expand Down Expand Up @@ -45,6 +194,26 @@ protected function end(string $content = null): void
$this->swoole->end($content);
}

/**
* Set status code reason
*
* Set HTTP response status code reason between available options. If status code is unknown an exception will be thrown.
*
* @param int $code
*
* @throws Exception
*/
protected function setStatusCodeReason(int $code): static
{
if (!\array_key_exists($code, $this->statusCodes)) {
throw new Exception('Unknown HTTP status code');
}

$this->reason = $this->statusCodes[$code];

return $this;
}

/**
* Send Status Code
*
Expand All @@ -53,7 +222,8 @@ protected function end(string $content = null): void
*/
protected function sendStatus(int $statusCode): void
{
$this->swoole->status((string) $statusCode, (string) $statusCode);
$this->setStatusCodeReason($statusCode);
$this->swoole->status((string) $statusCode, $this->reason);
}

/**
Expand Down

0 comments on commit d7ca3da

Please sign in to comment.