Browse files

Merge branch 'master' of github.com:peej/tonic

  • Loading branch information...
2 parents 8de049d + e51ef99 commit 8be1335ee91bb5ded8816624ed977b8a7ae999a8 @peej committed Jul 28, 2012
Showing with 109 additions and 54 deletions.
  1. +1 −1 src/Tonic/Request.php
  2. +108 −53 src/Tonic/Response.php
View
2 src/Tonic/Request.php
@@ -98,7 +98,7 @@ private function getContentType($options)
private function getData($options)
{
- if (isset($_SERVER['CONTENT_LENGTH']) && $_SERVER['CONTENT_LENGTH'] > 0) {
+ if ($this->getOption($options, 'contentLength', array('CONTENT_LENGTH', 'HTTP_CONTENT_LENGTH')) > 0) {
return file_get_contents('php://input');
} elseif (isset($options['data'])) {
return $options['data'];
View
161 src/Tonic/Response.php
@@ -7,68 +7,119 @@
*/
class Response
{
- public $code, $body;
- private $headers = array(
- 'content-type' => 'text/html'
- );
-
/**
* HTTP response code constant
*/
- const OK = 200,
- CREATED = 201,
- NOCONTENT = 204,
- MOVEDPERMANENTLY = 301,
- FOUND = 302,
- SEEOTHER = 303,
- NOTMODIFIED = 304,
- TEMPORARYREDIRECT = 307,
- BADREQUEST = 400,
- UNAUTHORIZED = 401,
- FORBIDDEN = 403,
- NOTFOUND = 404,
- METHODNOTALLOWED = 405,
- NOTACCEPTABLE = 406,
- GONE = 410,
- LENGTHREQUIRED = 411,
- PRECONDITIONFAILED = 412,
- UNSUPPORTEDMEDIATYPE = 415,
- IMATEAPOT = 418,
- ENHANCEYOURCALM = 420,
- INTERNALSERVERERROR = 500;
+ const
+ HTTPCONTINUE = 100,
+ SWITCHINGPROTOCOLS = 101,
+
+ OK = 200,
+ CREATED = 201,
+ ACCEPTED = 202,
+ NONAUTHORATIVEINFORMATION = 203,
+ NOCONTENT = 204,
+ RESETCONTENT = 205,
+ PARTIALCONTENT = 206,
+
+ MULTIPLECHOICES = 300,
+ MOVEDPERMANENTLY = 301,
+ FOUND = 302,
+ SEEOTHER = 303,
+ NOTMODIFIED = 304,
+ USEPROXY = 305,
+ TEMPORARYREDIRECT = 307,
+
+ BADREQUEST = 400,
+ UNAUTHORIZED = 401,
+ PAYMENTREQUIRED = 402,
+ FORBIDDEN = 403,
+ NOTFOUND = 404,
+ METHODNOTALLOWED = 405,
+ NOTACCEPTABLE = 406,
+ PROXYAUTHENTICATIONREQUIRED = 407,
+ REQUESTTIMEOUT = 408,
+ CONFLICT = 409,
+ GONE = 410,
+ LENGTHREQUIRED = 411,
+ PRECONDITIONFAILED = 412,
+ REQUESTENTITYTOOLARGE = 413,
+ REQUESTURITOOLONG = 414,
+ UNSUPPORTEDMEDIATYPE = 415,
+ REQUESTEDRANGENOTSATISFIABLE = 416,
+ EXPECTATIONFAILED = 417,
+ IMATEAPOT = 418, // RFC2324
+
+ INTERNALSERVERERROR = 500,
+ NOTIMPLEMENTED = 501,
+ BADGATEWAY = 502,
+ SERVICEUNAVAILABLE = 503,
+ GATEWAYTIMEOUT = 504,
+ HTTPVERSIONNOTSUPPORTED = 505;
/**
* Map of HTTP response codes
- * @var str[]
+ * RFC 2616, 2324
*/
- private $codes = array(
- Response::OK => 'OK',
- Response::CREATED => 'Created',
- Response::NOCONTENT => 'No Content',
- Response::MOVEDPERMANENTLY => 'Moved Permanently',
- Response::FOUND => 'Found',
- Response::SEEOTHER => 'See Other',
- Response::NOTMODIFIED => 'Not Modified',
- Response::TEMPORARYREDIRECT => 'Temporary Redirect',
- Response::BADREQUEST => 'Bad Request',
- Response::UNAUTHORIZED => 'Unauthorized',
- Response::FORBIDDEN => 'Forbidden',
- Response::NOTFOUND => 'Not Found',
- Response::METHODNOTALLOWED => 'Method Not Allowed',
- Response::NOTACCEPTABLE => 'Not Acceptable',
- Response::GONE => 'Gone',
- Response::LENGTHREQUIRED => 'Length Required',
- Response::PRECONDITIONFAILED => 'Precondition Failed',
- Response::UNSUPPORTEDMEDIATYPE => 'Unsupported Media Type',
- Response::IMATEAPOT => 'I\'m A Teapot',
- Response::UNSUPPORTEDMEDIATYPE => 'Enhance Your Calm',
- Response::INTERNALSERVERERROR => 'Internal Server Error'
+ protected $codes = array(
+ Response::HTTPCONTINUE => 'Continue',
+ Response::SWITCHINGPROTOCOLS => 'Switching Protocols',
+
+ Response::OK => 'OK',
+ Response::CREATED => 'Created',
+ Response::ACCEPTED => 'Accepted',
+ Response::NONAUTHORATIVEINFORMATION => 'Non-Authoritative Information',
+ Response::NOCONTENT => 'No Content',
+ Response::RESETCONTENT => 'Reset Content',
+ Response::PARTIALCONTENT => 'Partial Content',
+
+ Response::MULTIPLECHOICES => 'Multiple Choices',
+ Response::MOVEDPERMANENTLY => 'Moved Permanently',
+ Response::FOUND => 'Found',
+ Response::SEEOTHER => 'See Other',
+ Response::NOTMODIFIED => 'Not Modified',
+ Response::USEPROXY => 'Use Proxy',
+ Response::TEMPORARYREDIRECT => 'Temporary Redirect',
+
+ Response::BADREQUEST => 'Bad Request',
+ Response::UNAUTHORIZED => 'Unauthorized',
+ Response::PAYMENTREQUIRED => 'Payment Required',
+ Response::FORBIDDEN => 'Forbidden',
+ Response::NOTFOUND => 'Not Found',
+ Response::METHODNOTALLOWED => 'Method Not Allowed',
+ Response::NOTACCEPTABLE => 'Not Acceptable',
+ Response::PROXYAUTHENTICATIONREQUIRED => 'Proxy Authentication Required',
+ Response::REQUESTTIMEOUT => 'Request Timeout',
+ Response::CONFLICT => 'Conflict',
+ Response::GONE => 'Gone',
+ Response::LENGTHREQUIRED => 'Length Required',
+ Response::PRECONDITIONFAILED => 'Precondition Failed',
+ Response::REQUESTENTITYTOOLARGE => 'Request Entity Too Large',
+ Response::REQUESTURITOOLONG => 'Request-URI Too Long',
+ Response::UNSUPPORTEDMEDIATYPE => 'Unsupported Media Type',
+ Response::REQUESTEDRANGENOTSATISFIABLE => 'Requested Range Not Satisfiable',
+ Response::EXPECTATIONFAILED => 'Expectation Failed',
+ Response::IMATEAPOT => 'I\'m a teapot',
+
+ Response::INTERNALSERVERERROR => 'Internal Server Error',
+ Response::NOTIMPLEMENTED => 'Not Implemented',
+ Response::BADGATEWAY => 'Bad Gateway',
+ Response::SERVICEUNAVAILABLE => 'Service Unavailable',
+ Response::GATEWAYTIMEOUT => 'Gateway Timeout',
+ Response::HTTPVERSIONNOTSUPPORTED => 'HTTP Version Not Supported',
);
- public function __construct($code = 204, $body = '')
+ public
+ $code = self::NOCONTENT,
+ $body;
+
+ protected
+ $headers = array('content-type' => 'text/html');
+
+ public function __construct($code = null, $body = null)
{
- $this->code = $code;
- $this->body = $body;
+ $code and $this->code = $code;
+ $body and $this->body = $body;
}
/**
@@ -102,12 +153,16 @@ protected function responseMessage()
return isset($this->codes[$this->code]) ? $this->codes[$this->code] : '';
}
+ protected function responseCode() {
+ return $this->code;
+ }
+
/**
* Output the response
*/
public function output()
{
- header('HTTP/1.1 '.$this->code.' '.$this->responseMessage());
+ header('HTTP/1.1 '.$this->responseCode().' '.$this->responseMessage());
foreach ($this->headers as $name => $value) {
header($name.': '.$value);
}

0 comments on commit 8be1335

Please sign in to comment.