Skip to content

Commit 570c50b

Browse files
committed
Merge branch 'master' into request-headers
2 parents 9faea9d + 8be1335 commit 570c50b

File tree

2 files changed

+110
-55
lines changed

2 files changed

+110
-55
lines changed

src/Tonic/Request.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ private function getContentType($options)
109109

110110
private function getData($options)
111111
{
112-
if (isset($_SERVER['CONTENT_LENGTH']) && $_SERVER['CONTENT_LENGTH'] > 0) {
112+
if ($this->getOption($options, 'contentLength') > 0) {
113113
return file_get_contents('php://input');
114114
} elseif (isset($options['data'])) {
115115
return $options['data'];
@@ -127,7 +127,7 @@ private function getURIFromEnvironment($options)
127127
if (!$uri) { // use given URI in config options
128128
if (isset($_SERVER['REDIRECT_URL']) && isset($_SERVER['SCRIPT_NAME'])) { // use redirection URL from Apache environment
129129
$dirname = dirname($_SERVER['SCRIPT_NAME']);
130-
$uri = substr($_SERVER['REDIRECT_URL'], strlen($dirname == '/' ? '' : $dirname));
130+
$uri = substr($_SERVER['REDIRECT_URL'], strlen($dirname == DIRECTORY_SEPARATOR ? '' : $dirname));
131131
} elseif (isset($_SERVER['PHP_SELF']) && isset($_SERVER['SCRIPT_NAME'])) { // use PHP_SELF from Apache environment
132132
$uri = substr($_SERVER['PHP_SELF'], strlen($_SERVER['SCRIPT_NAME']));
133133
} else { // fail

src/Tonic/Response.php

Lines changed: 108 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -7,68 +7,119 @@
77
*/
88
class Response
99
{
10-
public $code, $body;
11-
private $headers = array(
12-
'content-type' => 'text/html'
13-
);
14-
1510
/**
1611
* HTTP response code constant
1712
*/
18-
const OK = 200,
19-
CREATED = 201,
20-
NOCONTENT = 204,
21-
MOVEDPERMANENTLY = 301,
22-
FOUND = 302,
23-
SEEOTHER = 303,
24-
NOTMODIFIED = 304,
25-
TEMPORARYREDIRECT = 307,
26-
BADREQUEST = 400,
27-
UNAUTHORIZED = 401,
28-
FORBIDDEN = 403,
29-
NOTFOUND = 404,
30-
METHODNOTALLOWED = 405,
31-
NOTACCEPTABLE = 406,
32-
GONE = 410,
33-
LENGTHREQUIRED = 411,
34-
PRECONDITIONFAILED = 412,
35-
UNSUPPORTEDMEDIATYPE = 415,
36-
IMATEAPOT = 418,
37-
ENHANCEYOURCALM = 420,
38-
INTERNALSERVERERROR = 500;
13+
const
14+
HTTPCONTINUE = 100,
15+
SWITCHINGPROTOCOLS = 101,
16+
17+
OK = 200,
18+
CREATED = 201,
19+
ACCEPTED = 202,
20+
NONAUTHORATIVEINFORMATION = 203,
21+
NOCONTENT = 204,
22+
RESETCONTENT = 205,
23+
PARTIALCONTENT = 206,
24+
25+
MULTIPLECHOICES = 300,
26+
MOVEDPERMANENTLY = 301,
27+
FOUND = 302,
28+
SEEOTHER = 303,
29+
NOTMODIFIED = 304,
30+
USEPROXY = 305,
31+
TEMPORARYREDIRECT = 307,
32+
33+
BADREQUEST = 400,
34+
UNAUTHORIZED = 401,
35+
PAYMENTREQUIRED = 402,
36+
FORBIDDEN = 403,
37+
NOTFOUND = 404,
38+
METHODNOTALLOWED = 405,
39+
NOTACCEPTABLE = 406,
40+
PROXYAUTHENTICATIONREQUIRED = 407,
41+
REQUESTTIMEOUT = 408,
42+
CONFLICT = 409,
43+
GONE = 410,
44+
LENGTHREQUIRED = 411,
45+
PRECONDITIONFAILED = 412,
46+
REQUESTENTITYTOOLARGE = 413,
47+
REQUESTURITOOLONG = 414,
48+
UNSUPPORTEDMEDIATYPE = 415,
49+
REQUESTEDRANGENOTSATISFIABLE = 416,
50+
EXPECTATIONFAILED = 417,
51+
IMATEAPOT = 418, // RFC2324
52+
53+
INTERNALSERVERERROR = 500,
54+
NOTIMPLEMENTED = 501,
55+
BADGATEWAY = 502,
56+
SERVICEUNAVAILABLE = 503,
57+
GATEWAYTIMEOUT = 504,
58+
HTTPVERSIONNOTSUPPORTED = 505;
3959

4060
/**
4161
* Map of HTTP response codes
42-
* @var str[]
62+
* RFC 2616, 2324
4363
*/
44-
private $codes = array(
45-
Response::OK => 'OK',
46-
Response::CREATED => 'Created',
47-
Response::NOCONTENT => 'No Content',
48-
Response::MOVEDPERMANENTLY => 'Moved Permanently',
49-
Response::FOUND => 'Found',
50-
Response::SEEOTHER => 'See Other',
51-
Response::NOTMODIFIED => 'Not Modified',
52-
Response::TEMPORARYREDIRECT => 'Temporary Redirect',
53-
Response::BADREQUEST => 'Bad Request',
54-
Response::UNAUTHORIZED => 'Unauthorized',
55-
Response::FORBIDDEN => 'Forbidden',
56-
Response::NOTFOUND => 'Not Found',
57-
Response::METHODNOTALLOWED => 'Method Not Allowed',
58-
Response::NOTACCEPTABLE => 'Not Acceptable',
59-
Response::GONE => 'Gone',
60-
Response::LENGTHREQUIRED => 'Length Required',
61-
Response::PRECONDITIONFAILED => 'Precondition Failed',
62-
Response::UNSUPPORTEDMEDIATYPE => 'Unsupported Media Type',
63-
Response::IMATEAPOT => 'I\'m A Teapot',
64-
Response::UNSUPPORTEDMEDIATYPE => 'Enhance Your Calm',
65-
Response::INTERNALSERVERERROR => 'Internal Server Error'
64+
protected $codes = array(
65+
Response::HTTPCONTINUE => 'Continue',
66+
Response::SWITCHINGPROTOCOLS => 'Switching Protocols',
67+
68+
Response::OK => 'OK',
69+
Response::CREATED => 'Created',
70+
Response::ACCEPTED => 'Accepted',
71+
Response::NONAUTHORATIVEINFORMATION => 'Non-Authoritative Information',
72+
Response::NOCONTENT => 'No Content',
73+
Response::RESETCONTENT => 'Reset Content',
74+
Response::PARTIALCONTENT => 'Partial Content',
75+
76+
Response::MULTIPLECHOICES => 'Multiple Choices',
77+
Response::MOVEDPERMANENTLY => 'Moved Permanently',
78+
Response::FOUND => 'Found',
79+
Response::SEEOTHER => 'See Other',
80+
Response::NOTMODIFIED => 'Not Modified',
81+
Response::USEPROXY => 'Use Proxy',
82+
Response::TEMPORARYREDIRECT => 'Temporary Redirect',
83+
84+
Response::BADREQUEST => 'Bad Request',
85+
Response::UNAUTHORIZED => 'Unauthorized',
86+
Response::PAYMENTREQUIRED => 'Payment Required',
87+
Response::FORBIDDEN => 'Forbidden',
88+
Response::NOTFOUND => 'Not Found',
89+
Response::METHODNOTALLOWED => 'Method Not Allowed',
90+
Response::NOTACCEPTABLE => 'Not Acceptable',
91+
Response::PROXYAUTHENTICATIONREQUIRED => 'Proxy Authentication Required',
92+
Response::REQUESTTIMEOUT => 'Request Timeout',
93+
Response::CONFLICT => 'Conflict',
94+
Response::GONE => 'Gone',
95+
Response::LENGTHREQUIRED => 'Length Required',
96+
Response::PRECONDITIONFAILED => 'Precondition Failed',
97+
Response::REQUESTENTITYTOOLARGE => 'Request Entity Too Large',
98+
Response::REQUESTURITOOLONG => 'Request-URI Too Long',
99+
Response::UNSUPPORTEDMEDIATYPE => 'Unsupported Media Type',
100+
Response::REQUESTEDRANGENOTSATISFIABLE => 'Requested Range Not Satisfiable',
101+
Response::EXPECTATIONFAILED => 'Expectation Failed',
102+
Response::IMATEAPOT => 'I\'m a teapot',
103+
104+
Response::INTERNALSERVERERROR => 'Internal Server Error',
105+
Response::NOTIMPLEMENTED => 'Not Implemented',
106+
Response::BADGATEWAY => 'Bad Gateway',
107+
Response::SERVICEUNAVAILABLE => 'Service Unavailable',
108+
Response::GATEWAYTIMEOUT => 'Gateway Timeout',
109+
Response::HTTPVERSIONNOTSUPPORTED => 'HTTP Version Not Supported',
66110
);
67111

68-
public function __construct($code = 204, $body = '')
112+
public
113+
$code = self::NOCONTENT,
114+
$body;
115+
116+
protected
117+
$headers = array('content-type' => 'text/html');
118+
119+
public function __construct($code = null, $body = null)
69120
{
70-
$this->code = $code;
71-
$this->body = $body;
121+
$code and $this->code = $code;
122+
$body and $this->body = $body;
72123
}
73124

74125
/**
@@ -102,12 +153,16 @@ protected function responseMessage()
102153
return isset($this->codes[$this->code]) ? $this->codes[$this->code] : '';
103154
}
104155

156+
protected function responseCode() {
157+
return $this->code;
158+
}
159+
105160
/**
106161
* Output the response
107162
*/
108163
public function output()
109164
{
110-
header('HTTP/1.1 '.$this->code.' '.$this->responseMessage());
165+
header('HTTP/1.1 '.$this->responseCode().' '.$this->responseMessage());
111166
foreach ($this->headers as $name => $value) {
112167
header($name.': '.$value);
113168
}

0 commit comments

Comments
 (0)