orbit/src/Orbit/Response.php
2020-08-27 02:46:11 -05:00

99 lines
2.4 KiB
PHP

<?php
namespace Orbit;
class Response
{
const STATUS_INPUT = 10;
const STATUS_SENSITIVE_INPUT = 11;
const STATUS_SUCCESS = 20;
const STATUS_REDIRECT_TEMPORARY = 30;
const STATUS_REDIRECT_PERMANENT = 31;
const STATUS_TEMPORARY_FAILURE = 40;
const STATUS_SERVER_UNAVAILABLE = 41;
const STATUS_CGI_ERROR = 42;
const STATUS_PROXY_ERROR = 43;
const STATUS_SLOW_DOWN = 44;
const STATUS_PERMANENT_FAILURE = 50;
const STATUS_NOT_FOUND = 51;
const STATUS_GONE = 52;
const STATUS_PROXY_REQUEST_REFUSED = 53;
const STATUS_BAD_REQUEST = 59;
const STATUS_CLIENT_CERTIFICATE_REQUIRED = 60;
const STATUS_CERTIFICATE_NOT_AUTHORISED = 61;
const STATUS_CERTIFICATE_NOT_VALID = 62;
public $status = "";
public $meta = "";
public $body = "";
public $filepath;
public function __construct($status = "", $meta = "")
{
$this->status = $status;
$this->meta = $meta;
}
public function getHeader()
{
return sprintf("%s %s\r\n", $this->status, $this->meta);
}
public function send($client)
{
fwrite($client, $this->getHeader());
if ($this->filepath) {
$size = filesize($this->filepath);
$fp = fopen($this->filepath, "rb");
if (false === $fp) {
throw new \Exception("Error reading file '{$this->filepath}'");
}
$result = 1;
while (!feof($fp) && $result) {
// If the client cancels, bail out before trying large files
// So, result will be 0 if the client cancels (broken socket)
$result = fwrite($client, fread($fp, 8192));
}
return $size;
} else {
$body = $this->getBody();
fwrite($client, $body);
return mb_strlen($body);
}
}
public function setBody($body = '')
{
$this->body = $body;
return $this;
}
public function getBody()
{
if ($this->filepath) {
$this->body = file_get_contents($this->filepath);
}
return $this->body;
}
public function setStaticFile($filepath)
{
$this->filepath = $filepath;
return $this;
}
public function setStatus($status)
{
$this->status = $status;
return $this;
}
public function setMeta($meta)
{
$this->meta = $meta;
return $this;
}
}