1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
<?php
/**
* This class represents a response returned by a controller that was asked to
* perform for a given request. A Trails_Response contains the body, status and
* additional headers which can be renderer back to the client.
*
* @package trails
*
* @author mlunzena
* @copyright (c) Authors
* @version $Id: trails.php 7001 2008-04-04 11:20:27Z mlunzena $
*/
class Trails_Response {
/**
* @ignore
*/
public
$body = '',
$status,
$reason,
$headers = array();
/**
* Constructor.
*
* @param string the body of the response defaulting to ''
* @param array an array of additional headers defaulting to an
* empty array
* @param integer the status code of the response defaulting to a
* regular 200
* @param string the descriptional reason for a status code defaulting to
* the standard reason phrases defined in RFC 2616
*
* @return void
*/
function __construct($body = '', $headers = array(),
$status = NULL, $reason = NULL) {
$this->set_body($body);
$this->headers = $headers;
if (isset($status)) {
$this->set_status($status, $reason);
}
}
/**
* Sets the body of the response.
*
* @param string the body
*
* @return mixed this response object. Useful for cascading method calls.
*/
function set_body($body) {
$this->body = $body;
return $this;
}
/**
* Sets the status code and an optional custom reason. If none is given, the
* standard reason phrase as of RFC 2616 is used.
*
* @param integer the status code
* @param string the custom reason, defaulting to the one given in RFC 2616
*
* @return mixed this response object. Useful for cascading method calls.
*/
function set_status($status, $reason = NULL) {
$this->status = $status;
$this->reason = isset($reason) ? $reason : self::get_reason($status);
return $this;
}
/**
* Returns the reason phrase of this response according to RFC2616.
*
* @param int the response's status
*
* @return string the reason phrase for this response's status
*/
public static function get_reason($status) {
$reason = array(
100 => 'Continue', 'Switching Protocols',
200 => 'OK', 'Created', 'Accepted', 'Non-Authoritative Information',
'No Content', 'Reset Content', 'Partial Content',
300 => 'Multiple Choices', 'Moved Permanently', 'Found', 'See Other',
'Not Modified', 'Use Proxy', '(Unused)', 'Temporary Redirect',
400 => 'Bad Request', 'Unauthorized', 'Payment Required','Forbidden',
'Not Found', 'Method Not Allowed', 'Not Acceptable',
'Proxy Authentication Required', 'Request Timeout', 'Conflict',
'Gone', 'Length Required', 'Precondition Failed',
'Request Entity Too Large', 'Request-URI Too Long',
'Unsupported Media Type', 'Requested Range Not Satisfiable',
'Expectation Failed',
500 => 'Internal Server Error', 'Not Implemented', 'Bad Gateway',
'Service Unavailable', 'Gateway Timeout',
'HTTP Version Not Supported');
return isset($reason[$status]) ? $reason[$status] : '';
}
/**
* Adds an additional header to the response.
*
* @param string the left hand key part
* @param string the right hand value part
*
* @return mixed this response object. Useful for cascading method calls.
*/
function add_header($key, $value) {
$this->headers[$key] = $value;
return $this;
}
/**
* Outputs this response to the client using "echo" and "header".
*
* @return void
*/
function output() {
if (isset($this->status)) {
$this->send_header(sprintf('HTTP/1.1 %d %s',
$this->status, $this->reason),
TRUE,
$this->status);
}
foreach ($this->headers as $k => $v) {
$this->send_header("$k: $v");
}
echo $this->body;
}
/**
* Internally used function to actually send headers
*
* @param string the HTTP header
* @param bool optional; TRUE if previously sent header should be
* replaced - FALSE otherwise (default)
* @param integer optional; the HTTP response code
*
* @return void
*/
function send_header($header, $replace = FALSE, $status = NULL) {
if (isset($status)) {
header($header, $replace, $status);
}
else {
header($header, $replace);
}
}
}
|