Package home | Report new bug | New search | Development Roadmap Status: Open | Feedback | All | Closed Since Version 1.4.4

Request #12335 Add error codes to HTTP_Request
Submitted: 2007-10-24 13:31 UTC Modified: 2007-10-24 14:26 UTC
From: jstump Assigned: avb
Status: Closed Package: HTTP_Request (version 1.4.1)
PHP Version: Irrelevant OS: All
Roadmaps: (Not assigned)    
Subscription  


 [2007-10-24 13:31 UTC] jstump (Joe Stump)
Description: ------------ I added error codes to all of the PEAR::raiseError()'s in HTTP_Request. String comparison for error types makes me a sad panda.

Comments

 [2007-10-24 13:33 UTC] jstump (Joe Stump)
Index: Request.php =================================================================== RCS file: /repository/pear/HTTP_Request/Request.php,v retrieving revision 1.56 diff -u -r1.56 Request.php --- Request.php 14 Aug 2007 18:42:19 -0000 1.56 +++ Request.php 24 Oct 2007 18:23:08 -0000 @@ -70,6 +70,20 @@ /**#@-*/ /**#@+ + * Constants for HTTP request error codes + */ +define('HTTP_REQUEST_ERROR_FILE', 1); +define('HTTP_REQUEST_ERROR_URL', 2); +define('HTTP_REQUEST_ERROR_PROXY', 4); +define('HTTP_REQUEST_ERROR_REDIRECTS', 8); +define('HTTP_REQUEST_ERROR_RESPONSE', 16); +define('HTTP_REQUEST_ERROR_GZIP_METHOD', 32); +define('HTTP_REQUEST_ERROR_GZIP_READ', 64); +define('HTTP_REQUEST_ERROR_GZIP_DATA', 128); +define('HTTP_REQUEST_ERROR_GZIP_CRC', 256); +/**#@-*/ + +/**#@+ * Constants for HTTP protocol versions */ define('HTTP_REQUEST_HTTP_VER_1_0', '1.0', true); @@ -572,11 +586,11 @@ function addFile($inputName, $fileName, $contentType = 'application/octet-stream') { if (!is_array($fileName) && !is_readable($fileName)) { - return PEAR::raiseError("File '{$fileName}' is not readable"); + return PEAR::raiseError("File '{$fileName}' is not readable", HTTP_REQUEST_ERROR_FILE); } elseif (is_array($fileName)) { foreach ($fileName as $name) { if (!is_readable($name)) { - return PEAR::raiseError("File '{$name}' is not readable"); + return PEAR::raiseError("File '{$name}' is not readable", HTTP_REQUEST_ERROR_FILE); } } } @@ -662,7 +676,7 @@ function sendRequest($saveBody = true) { if (!is_a($this->_url, 'Net_URL')) { - return PEAR::raiseError('No URL given.'); + return PEAR::raiseError('No URL given', HTTP_REQUEST_ERROR_URL); } $host = isset($this->_proxy_host) ? $this->_proxy_host : $this->_url->host; @@ -672,7 +686,7 @@ // we running on at least 4.3.0 if (strcasecmp($this->_url->protocol, 'https') == 0 AND function_exists('file_get_contents') AND extension_loaded('openssl')) { if (isset($this->_proxy_host)) { - return PEAR::raiseError('HTTPS proxies are not supported.'); + return PEAR::raiseError('HTTPS proxies are not supported', HTTP_REQUEST_ERROR_PROXY); } $host = 'ssl://' . $host; } @@ -792,7 +806,7 @@ // Too many redirects } elseif ($this->_allowRedirects AND $this->_redirects > $this->_maxRedirects) { - return PEAR::raiseError('Too many redirects'); + return PEAR::raiseError('Too many redirects', HTTP_REQUEST_ERROR_REDIRECTS); } return true; @@ -880,6 +894,10 @@ $path = $this->_url->path . $querystring; $url = $host . $port . $path; + if (!strlen($url)) { + $url = '/'; + } + $request = $this->_method . ' ' . $url . ' HTTP/' . $this->_http . "\r\n"; if (in_array($this->_method, $this->_bodyDisallowed) || @@ -1169,7 +1187,7 @@ do { $line = $this->_sock->readLine(); if (sscanf($line, 'HTTP/%s %s', $http_version, $returncode) != 2) { - return PEAR::raiseError('Malformed response.'); + return PEAR::raiseError('Malformed response', HTTP_REQUEST_ERROR_RESPONSE); } else { $this->_protocol = 'HTTP/' . $http_version; $this->_code = intval($returncode); @@ -1389,11 +1407,11 @@ } $method = ord(substr($data, 2, 1)); if (8 != $method) { - return PEAR::raiseError('_decodeGzip(): unknown compression method'); + return PEAR::raiseError('_decodeGzip(): unknown compression method', HTTP_REQUEST_ERROR_GZIP_METHOD); } $flags = ord(substr($data, 3, 1)); if ($flags & 224) { - return PEAR::raiseError('_decodeGzip(): reserved bits are set'); + return PEAR::raiseError('_decodeGzip(): reserved bits are set', HTTP_REQUEST_ERROR_GZIP_DATA); } // header is 10 bytes minimum. may be longer, though. @@ -1401,45 +1419,45 @@ // extra fields, need to skip 'em if ($flags & 4) { if ($length - $headerLength - 2 < 8) { - return PEAR::raiseError('_decodeGzip(): data too short'); + return PEAR::raiseError('_decodeGzip(): data too short', HTTP_REQUEST_ERROR_GZIP_DATA); } $extraLength = unpack('v', substr($data, 10, 2)); if ($length - $headerLength - 2 - $extraLength[1] < 8) { - return PEAR::raiseError('_decodeGzip(): data too short'); + return PEAR::raiseError('_decodeGzip(): data too short', HTTP_REQUEST_ERROR_GZIP_DATA); } $headerLength += $extraLength[1] + 2; } // file name, need to skip that if ($flags & 8) { if ($length - $headerLength - 1 < 8) { - return PEAR::raiseError('_decodeGzip(): data too short'); + return PEAR::raiseError('_decodeGzip(): data too short', HTTP_REQUEST_ERROR_GZIP_DATA); } $filenameLength = strpos(substr($data, $headerLength), chr(0)); if (false === $filenameLength || $length - $headerLength - $filenameLength - 1 < 8) { - return PEAR::raiseError('_decodeGzip(): data too short'); + return PEAR::raiseError('_decodeGzip(): data too short', HTTP_REQUEST_ERROR_GZIP_DATA); } $headerLength += $filenameLength + 1; } // comment, need to skip that also if ($flags & 16) { if ($length - $headerLength - 1 < 8) { - return PEAR::raiseError('_decodeGzip(): data too short'); + return PEAR::raiseError('_decodeGzip(): data too short', HTTP_REQUEST_ERROR_GZIP_DATA); } $commentLength = strpos(substr($data, $headerLength), chr(0)); if (false === $commentLength || $length - $headerLength - $commentLength - 1 < 8) { - return PEAR::raiseError('_decodeGzip(): data too short'); + return PEAR::raiseError('_decodeGzip(): data too short', HTTP_REQUEST_ERROR_GZIP_DATA); } $headerLength += $commentLength + 1; } // have a CRC for header. let's check if ($flags & 1) { if ($length - $headerLength - 2 < 8) { - return PEAR::raiseError('_decodeGzip(): data too short'); + return PEAR::raiseError('_decodeGzip(): data too short', HTTP_REQUEST_ERROR_GZIP_DATA); } $crcReal = 0xffff & crc32(substr($data, 0, $headerLength)); $crcStored = unpack('v', substr($data, $headerLength, 2)); if ($crcReal != $crcStored[1]) { - return PEAR::raiseError('_decodeGzip(): header CRC check failed'); + return PEAR::raiseError('_decodeGzip(): header CRC check failed', HTTP_REQUEST_ERROR_GZIP_CRC); } $headerLength += 2; } @@ -1451,11 +1469,11 @@ // finally, call the gzinflate() function $unpacked = @gzinflate(substr($data, $headerLength, -8), $dataSize); if (false === $unpacked) { - return PEAR::raiseError('_decodeGzip(): gzinflate() call failed'); + return PEAR::raiseError('_decodeGzip(): gzinflate() call failed', HTTP_REQUEST_ERROR_GZIP_READ); } elseif ($dataSize != strlen($unpacked)) { - return PEAR::raiseError('_decodeGzip(): data size check failed'); + return PEAR::raiseError('_decodeGzip(): data size check failed', HTTP_REQUEST_ERROR_GZIP_READ); } elseif ((0xffffffff & $dataCrc) != (0xffffffff & crc32($unpacked))) { - return PEAR::raiseError('_decodeGzip(): data CRC check failed'); + return PEAR::raiseError('_decodeGzip(): data CRC check failed', HTTP_REQUEST_ERROR_GZIP_CRC); } if (HTTP_REQUEST_MBSTRING) { mb_internal_encoding($oldEncoding);
 [2007-10-24 14:26 UTC] avb (Alexey Borzov)
This bug has been fixed in CVS. If this was a documentation problem, the fix will appear on pear.php.net by the end of next Sunday (CET). If this was a problem with the pear.php.net website, the change should be live shortly. Otherwise, the fix will appear in the package's next release. Thank you for the report and for helping us make PEAR better.