Skip to content

Commit 817163a

Browse files
author
Derek Jones
committed
Modified show_error() to allow sending of HTTP server response codes.
Added set_status_header() to the Common functions to allow use when the Output class is unavailable. Fixed a bug where the 400 status header sent with the 'disallowed URI characters' was not compatible with CGI environments.
1 parent 55acc8b commit 817163a

File tree

8 files changed

+121
-79
lines changed

8 files changed

+121
-79
lines changed

system/codeigniter/Common.php

Lines changed: 87 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,10 @@ function config_item($item)
203203
* @access public
204204
* @return void
205205
*/
206-
function show_error($message)
206+
function show_error($message, $status_code = 500)
207207
{
208208
$error =& load_class('Exceptions');
209-
echo $error->show_error('An Error Was Encountered', $message);
209+
echo $error->show_error('An Error Was Encountered', $message, 'error_general', $status_code);
210210
exit;
211211
}
212212

@@ -252,6 +252,91 @@ function log_message($level = 'error', $message, $php_error = FALSE)
252252
$LOG->write_log($level, $message, $php_error);
253253
}
254254

255+
256+
/**
257+
* Set HTTP Status Header
258+
*
259+
* @access public
260+
* @param int the status code
261+
* @param string
262+
* @return void
263+
*/
264+
function set_status_header($code = 200, $text = '')
265+
{
266+
$stati = array(
267+
200 => 'OK',
268+
201 => 'Created',
269+
202 => 'Accepted',
270+
203 => 'Non-Authoritative Information',
271+
204 => 'No Content',
272+
205 => 'Reset Content',
273+
206 => 'Partial Content',
274+
275+
300 => 'Multiple Choices',
276+
301 => 'Moved Permanently',
277+
302 => 'Found',
278+
304 => 'Not Modified',
279+
305 => 'Use Proxy',
280+
307 => 'Temporary Redirect',
281+
282+
400 => 'Bad Request',
283+
401 => 'Unauthorized',
284+
403 => 'Forbidden',
285+
404 => 'Not Found',
286+
405 => 'Method Not Allowed',
287+
406 => 'Not Acceptable',
288+
407 => 'Proxy Authentication Required',
289+
408 => 'Request Timeout',
290+
409 => 'Conflict',
291+
410 => 'Gone',
292+
411 => 'Length Required',
293+
412 => 'Precondition Failed',
294+
413 => 'Request Entity Too Large',
295+
414 => 'Request-URI Too Long',
296+
415 => 'Unsupported Media Type',
297+
416 => 'Requested Range Not Satisfiable',
298+
417 => 'Expectation Failed',
299+
300+
500 => 'Internal Server Error',
301+
501 => 'Not Implemented',
302+
502 => 'Bad Gateway',
303+
503 => 'Service Unavailable',
304+
504 => 'Gateway Timeout',
305+
505 => 'HTTP Version Not Supported'
306+
);
307+
308+
if ($code == '' OR ! is_numeric($code))
309+
{
310+
show_error('Status codes must be numeric', 500);
311+
}
312+
313+
if (isset($stati[$code]) AND $text == '')
314+
{
315+
$text = $stati[$code];
316+
}
317+
318+
if ($text == '')
319+
{
320+
show_error('No status text available. Please check your status code number or supply your own message text.', 500);
321+
}
322+
323+
$server_protocol = (isset($_SERVER['SERVER_PROTOCOL'])) ? $_SERVER['SERVER_PROTOCOL'] : FALSE;
324+
325+
if (substr(php_sapi_name(), 0, 3) == 'cgi')
326+
{
327+
header("Status: {$code} {$text}", TRUE);
328+
}
329+
elseif ($server_protocol == 'HTTP/1.1' OR $server_protocol == 'HTTP/1.0')
330+
{
331+
header($server_protocol." {$code} {$text}", TRUE, $code);
332+
}
333+
else
334+
{
335+
header("HTTP/1.1 {$code} {$text}", TRUE, $code);
336+
}
337+
}
338+
339+
255340
/**
256341
* Exception Handler
257342
*

system/libraries/Exceptions.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,10 @@ function show_404($page = '')
113113
* @param string the template name
114114
* @return string
115115
*/
116-
function show_error($heading, $message, $template = 'error_general')
116+
function show_error($heading, $message, $template = 'error_general', $status_code = 500)
117117
{
118+
set_status_header($status_code);
119+
118120
$message = '<p>'.implode('</p><p>', ( ! is_array($message)) ? array($message) : $message).'</p>';
119121

120122
if (ob_get_level() > $this->ob_level + 1)

system/libraries/Output.php

Lines changed: 3 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -116,85 +116,16 @@ function set_header($header, $replace = TRUE)
116116

117117
/**
118118
* Set HTTP Status Header
119-
*
119+
* moved to Common procedural functions in 1.7.2
120+
*
120121
* @access public
121122
* @param int the status code
122123
* @param string
123124
* @return void
124125
*/
125126
function set_status_header($code = '200', $text = '')
126127
{
127-
$stati = array(
128-
'200' => 'OK',
129-
'201' => 'Created',
130-
'202' => 'Accepted',
131-
'203' => 'Non-Authoritative Information',
132-
'204' => 'No Content',
133-
'205' => 'Reset Content',
134-
'206' => 'Partial Content',
135-
136-
'300' => 'Multiple Choices',
137-
'301' => 'Moved Permanently',
138-
'302' => 'Found',
139-
'304' => 'Not Modified',
140-
'305' => 'Use Proxy',
141-
'307' => 'Temporary Redirect',
142-
143-
'400' => 'Bad Request',
144-
'401' => 'Unauthorized',
145-
'403' => 'Forbidden',
146-
'404' => 'Not Found',
147-
'405' => 'Method Not Allowed',
148-
'406' => 'Not Acceptable',
149-
'407' => 'Proxy Authentication Required',
150-
'408' => 'Request Timeout',
151-
'409' => 'Conflict',
152-
'410' => 'Gone',
153-
'411' => 'Length Required',
154-
'412' => 'Precondition Failed',
155-
'413' => 'Request Entity Too Large',
156-
'414' => 'Request-URI Too Long',
157-
'415' => 'Unsupported Media Type',
158-
'416' => 'Requested Range Not Satisfiable',
159-
'417' => 'Expectation Failed',
160-
161-
'500' => 'Internal Server Error',
162-
'501' => 'Not Implemented',
163-
'502' => 'Bad Gateway',
164-
'503' => 'Service Unavailable',
165-
'504' => 'Gateway Timeout',
166-
'505' => 'HTTP Version Not Supported'
167-
);
168-
169-
if ($code == '' OR ! is_numeric($code))
170-
{
171-
show_error('Status codes must be numeric');
172-
}
173-
174-
if (isset($stati[$code]) AND $text == '')
175-
{
176-
$text = $stati[$code];
177-
}
178-
179-
if ($text == '')
180-
{
181-
show_error('No status text available. Please check your status code number or supply your own message text.');
182-
}
183-
184-
$server_protocol = (isset($_SERVER['SERVER_PROTOCOL'])) ? $_SERVER['SERVER_PROTOCOL'] : FALSE;
185-
186-
if (substr(php_sapi_name(), 0, 3) == 'cgi')
187-
{
188-
header("Status: {$code} {$text}", TRUE);
189-
}
190-
elseif ($server_protocol == 'HTTP/1.1' OR $server_protocol == 'HTTP/1.0')
191-
{
192-
header($server_protocol." {$code} {$text}", TRUE, $code);
193-
}
194-
else
195-
{
196-
header("HTTP/1.1 {$code} {$text}", TRUE, $code);
197-
}
128+
set_status_header($code, $text);
198129
}
199130

200131
// --------------------------------------------------------------------

system/libraries/URI.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,7 @@ function _filter_uri($str)
188188
{
189189
if ( ! preg_match("|^[".preg_quote($this->config->item('permitted_uri_chars'))."]+$|i", $str))
190190
{
191-
header('HTTP/1.1 400 Bad Request');
192-
show_error('The URI you submitted has disallowed characters.');
191+
show_error('The URI you submitted has disallowed characters.', 400);
193192
}
194193
}
195194

user_guide/changelog.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ <h2>Version 1.7.2</h2>
8585
<li>Modified <kbd>directory_map()</kbd> in the <a href="helpers/directory_helper.html">Directory helper</a> to allow the inclusion of hidden files.</li>
8686
</ul>
8787
</li>
88+
<li>General
89+
<ul>
90+
<li>Modified <a href="general/errors.html">show_error()</a> to allow sending of HTTP server response codes.</li>
91+
<li>Added set_status_header() to the <a href="general/common_functions.html">Common functions<a> to allow use when the Output class is unavailable.</li>
92+
</ul>
93+
</li>
8894
</ul>
8995

9096
<h3>Bug fixes for 1.7.2</h3>
@@ -102,6 +108,7 @@ <h3>Bug fixes for 1.7.2</h3>
102108
<li>Fixed a case sensitive string replacement in xss_clean()</li>
103109
<li>Fixed a bug in form_prep() causing it to not preserve entities in the user's original input when called back into a form element</li>
104110
<li>Fixed a bug in _protect_identifiers() where the swap prefix ($swap_pre) was not being observed.</li>
111+
<li>Fixed a bug where the 400 status header sent with the 'disallowed URI characters' was not compatible with CGI environments.</li>
105112
</ul>
106113

107114
<h2>Version 1.7.1</h2>

user_guide/general/common_functions.html

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,11 @@
5858
<h1>Common Functions</h1>
5959

6060
<p>CodeIgniter uses a few functions for its operation that are globally defined, and are available to you at any point. These do not require loading any libraries or helpers.</p>
61+
6162
<h2>is_really_writable('<var>path/to/file</var>')</h2>
63+
6264
<p>is_writable() returns TRUE on Windows servers when you really can't write to the file as the OS reports to PHP as FALSE only if the read-only attribute is marked. This function determines if a file is actually writable by attempting to write to it first. Generally only recommended on platforms where this information may be unreliable.</p>
65+
6366
<code>if (is_really_writable('file.txt'))<br />
6467
{<br />
6568
&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;I could write to this if I wanted to&quot;;<br />
@@ -68,11 +71,25 @@ <h2>is_really_writable('<var>path/to/file</var>')</h2>
6871
{<br />
6972
&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;File is not writable&quot;;<br />
7073
}</code>
74+
7175
<h2>config_item('<var>item_key</var>')</h2>
7276
<p>The <a href="../libraries/config.html">Config library</a> is the preferred way of accessing configuration information, however config_item() can be used to retrieve single keys. See Config library documentation for more information.</p>
77+
7378
<h2>show_error('<var>message</var>'), show_404('<var>page</var>'), log_message('<var>level</var>', '<samp>message</samp>')</h2>
7479
<p>These are each outlined on the <a href="errors.html">Error Handling</a> page.</p>
80+
81+
<h2>set_status_header(<var>code</var>, '<var>text</var>');</h2>
82+
83+
<p>Permits you to manually set a server status header. Example:</p>
84+
85+
<code>set_status_header(401);<br />
86+
// Sets the header as: Unauthorized</code>
87+
88+
<p><a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html">See here</a> for a full list of headers.</p>
89+
7590
</div>
91+
92+
7693
<!-- END CONTENT -->
7794

7895

user_guide/general/errors.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,10 @@ <h1>Error Handling</h1>
7171

7272
<p>The following functions let you generate errors:</p>
7373

74-
<h2>show_error('<var>message</var>')</h2>
74+
<h2>show_error('<var>message</var>' [, int <var>$status_code</var>= 500 ] )</h2>
7575
<p>This function will display the error message supplied to it using the following error template:</p>
7676
<p><dfn>application/errors/</dfn><kbd>error_general.php</kbd></p>
77+
<p>The optional parameter $status_code determines what HTTP status code should be sent with the error.</p>
7778

7879
<h2>show_404('<var>page</var>')</h2>
7980
<p>This function will display the 404 error message supplied to it using the following error template:</p>

user_guide/libraries/output.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ <h2>$this->output->set_header();</h2>
100100
$this->output->set_header("Pragma: no-cache"); </code>
101101

102102

103-
<h2>$this->output->set_status_header();</h2>
103+
<h2>$this->output->set_status_header(<var>code</var>, '<var>text</var>');</h2>
104104

105105
<p>Permits you to manually set a server status header. Example:</p>
106106

0 commit comments

Comments
 (0)