Package proposal: HTTP_Status
| From: | Marshall Roch | Date: | Tue, 26 Aug 2003 06:33:06 +0000 |
| Subject: | Package proposal: HTTP_Status | ||
| Groups: | php.pear.dev | ||
| Request: | Send a blank email to pear-dev+get-20509@lists.php.net to get a copy of this message | ||
I've created a class called HTTP_Status that translates HTTP (including WebDAV) status codes (e.g. 200, 404, 304) into human-readable form (respectively "OK", "Not Found", "Not Modified"). It also has methods to check whether a given status code is "informational," "success," "redirection," "client error," or "server error." There are a number of classes already in PEAR that could make use of this.
HTTP_Header defines almost all of the status codes as constants, but it has a number of problems:
1) The constants can't be used by other packages.
2) The constants are only used for one method (a lot of code for a very specific gain), and the developer (Wolfram) seems to want to make it cleaner:
function sendStatusCode ( $code )
{
if ( is_int($code) ) {
// if the $code is an int we get the constant here
// is there an easier way to build a constant dynamically?
eval("\$code = HTTP_HEADER_STATUS_".$code.';');
}
...
}
With HTTP_Status, it would be:
...
// eval("\$code = HTTP_HEADER_STATUS_".$code.';');
$status = $code . HTTP_Status::getStatusMessage($code);
...
In HTTP_Client, the following is used to check for redirection:
if ( ... in_array($code, array(300, 301, 302, 303, 307))) {
... do stuff because it's a redirection ...} With HTTP_Status, it would be: if ( ... HTTP_Status::isRedirection($code)) {
... do stuff because it's a redirection ...} If more redirection codes were added in a new version of HTTP, HTTP_Client, or any other class that makes use of HTTP status codes, would continue working fine. Also in HTTP_Client, the following code is used: if (400 >= $code) {
$this->_notify('httpSuccess');
...
}
This is saying that all status codes greater than 399 (redirection codes are 300 - 399) are not redirects, so it's successful. This isn't the case: 100 - 199 are informational; 200 - 299 show success. With HTTP_Status:
if (!HTTP_Status::isRedirection($code)) {
$this->_notify('httpSuccess');
...
}
The same principal applies to each group of status codes (informational, success, redirection, client error, and server error).
Source: http://www.exclupen.com/misc/php/pear_patches/HTTP_Status/Status.phps
Package.xml:
http://www.exclupen.com/misc/php/pear_patches/HTTP_Status/package.xml
Package:
http://www.exclupen.com/misc/php/pear_patches/HTTP_Status/HTTP_Status-1.0.tgz
--
Marshall Roch