Skip to content

Commit

Permalink
Clean up code per coding standards
Browse files Browse the repository at this point in the history
  • Loading branch information
rmccue committed Oct 15, 2015
1 parent 0fbed0f commit 3626938
Show file tree
Hide file tree
Showing 10 changed files with 1,107 additions and 1,073 deletions.
65 changes: 36 additions & 29 deletions library/Requests.php
Expand Up @@ -176,8 +176,9 @@ protected static function get_transport($capabilities = array()) {

// Find us a working transport
foreach (self::$transports as $class) {
if (!class_exists($class))
if (!class_exists($class)) {
continue;
}

$result = call_user_func(array($class, 'test'), $capabilities);
if ($result) {
Expand All @@ -188,7 +189,7 @@ protected static function get_transport($capabilities = array()) {
if (self::$transport[$cap_string] === null) {
throw new Requests_Exception('No working transports found', 'notransport', self::$transports);
}

return new self::$transport[$cap_string]();
}

Expand Down Expand Up @@ -326,7 +327,8 @@ public static function request($url, $headers = array(), $data = array(), $type
if (is_string($options['transport'])) {
$transport = new $transport();
}
} else {
}
else {
$need_ssl = (0 === stripos($url, 'https://'));
$capabilities = array('ssl' => $need_ssl);
$transport = self::get_transport($capabilities);
Expand Down Expand Up @@ -472,7 +474,7 @@ protected static function get_default_options($multirequest = false) {
'idn' => true,
'hooks' => null,
'transport' => null,
'verify' => dirname( __FILE__ ) . '/Requests/Transport/cacert.pem',
'verify' => dirname(__FILE__) . '/Requests/Transport/cacert.pem',
'verifyname' => true,
);
if ($multirequest !== false) {
Expand Down Expand Up @@ -608,7 +610,7 @@ protected static function parse_response($headers, $url, $req_headers, $req_data
}
$options['redirected']++;
$location = $return->headers['location'];
if (strpos ($location, 'http://') !== 0 && strpos ($location, 'https://') !== 0) {
if (strpos($location, 'http://') !== 0 && strpos($location, 'https://') !== 0) {
// relative redirect, for compatibility make it absolute
$location = Requests_IRI::absolutize($url, $location);
$location = $location->uri;
Expand Down Expand Up @@ -667,7 +669,7 @@ protected static function decode_chunked($data) {
$encoded = $data;

while (true) {
$is_chunked = (bool) preg_match( '/^([0-9a-f]+)[^\r\n]*\r\n/i', $encoded, $matches );
$is_chunked = (bool) preg_match('/^([0-9a-f]+)[^\r\n]*\r\n/i', $encoded, $matches);
if (!$is_chunked) {
// Looks like it's not chunked after all
return $data;
Expand Down Expand Up @@ -773,23 +775,26 @@ public static function decompress($data) {
public static function compatible_gzinflate($gzData) {
// Compressed data might contain a full zlib header, if so strip it for
// gzinflate()
if ( substr($gzData, 0, 3) == "\x1f\x8b\x08" ) {
if (substr($gzData, 0, 3) == "\x1f\x8b\x08") {
$i = 10;
$flg = ord( substr($gzData, 3, 1) );
if ( $flg > 0 ) {
if ( $flg & 4 ) {
list($xlen) = unpack('v', substr($gzData, $i, 2) );
$flg = ord(substr($gzData, 3, 1));
if ($flg > 0) {
if ($flg & 4) {
list($xlen) = unpack('v', substr($gzData, $i, 2));
$i = $i + 2 + $xlen;
}
if ( $flg & 8 )
if ($flg & 8) {
$i = strpos($gzData, "\0", $i) + 1;
if ( $flg & 16 )
}
if ($flg & 16) {
$i = strpos($gzData, "\0", $i) + 1;
if ( $flg & 2 )
}
if ($flg & 2) {
$i = $i + 2;
}
}
$decompressed = self::compatible_gzinflate( substr( $gzData, $i ) );
if ( false !== $decompressed ) {
$decompressed = self::compatible_gzinflate(substr($gzData, $i));
if (false !== $decompressed) {
return $decompressed;
}
}
Expand All @@ -805,55 +810,57 @@ public static function compatible_gzinflate($gzData) {
$huffman_encoded = false;

// low nibble of first byte should be 0x08
list( , $first_nibble ) = unpack( 'h', $gzData );
list(, $first_nibble) = unpack('h', $gzData);

// First 2 bytes should be divisible by 0x1F
list( , $first_two_bytes ) = unpack( 'n', $gzData );
list(, $first_two_bytes) = unpack('n', $gzData);

if ( 0x08 == $first_nibble && 0 == ( $first_two_bytes % 0x1F ) )
if (0x08 == $first_nibble && 0 == ($first_two_bytes % 0x1F)) {
$huffman_encoded = true;
}

if ( $huffman_encoded ) {
if ( false !== ( $decompressed = @gzinflate( substr( $gzData, 2 ) ) ) )
if ($huffman_encoded) {
if (false !== ($decompressed = @gzinflate(substr($gzData, 2)))) {
return $decompressed;
}
}

if ( "\x50\x4b\x03\x04" == substr( $gzData, 0, 4 ) ) {
if ("\x50\x4b\x03\x04" == substr($gzData, 0, 4)) {
// ZIP file format header
// Offset 6: 2 bytes, General-purpose field
// Offset 26: 2 bytes, filename length
// Offset 28: 2 bytes, optional field length
// Offset 30: Filename field, followed by optional field, followed
// immediately by data
list( , $general_purpose_flag ) = unpack( 'v', substr( $gzData, 6, 2 ) );
list(, $general_purpose_flag) = unpack('v', substr($gzData, 6, 2));

// If the file has been compressed on the fly, 0x08 bit is set of
// the general purpose field. We can use this to differentiate
// between a compressed document, and a ZIP file
$zip_compressed_on_the_fly = ( 0x08 == (0x08 & $general_purpose_flag ) );
$zip_compressed_on_the_fly = (0x08 == (0x08 & $general_purpose_flag));

if ( ! $zip_compressed_on_the_fly ) {
if (!$zip_compressed_on_the_fly) {
// Don't attempt to decode a compressed zip file
return $gzData;
}

// Determine the first byte of data, based on the above ZIP header
// offsets:
$first_file_start = array_sum( unpack( 'v2', substr( $gzData, 26, 4 ) ) );
if ( false !== ( $decompressed = @gzinflate( substr( $gzData, 30 + $first_file_start ) ) ) ) {
$first_file_start = array_sum(unpack('v2', substr($gzData, 26, 4)));
if (false !== ($decompressed = @gzinflate(substr($gzData, 30 + $first_file_start)))) {
return $decompressed;
}
return false;
}

// Finally fall back to straight gzinflate
if ( false !== ( $decompressed = @gzinflate( $gzData ) ) ) {
if (false !== ($decompressed = @gzinflate($gzData))) {
return $decompressed;
}

// Fallback for all above failing, not expected, but included for
// debugging and preventing regressions and to track stats
if ( false !== ( $decompressed = @gzinflate( substr( $gzData, 2 ) ) ) ) {
if (false !== ($decompressed = @gzinflate(substr($gzData, 2)))) {
return $decompressed;
}

Expand Down
9 changes: 5 additions & 4 deletions library/Requests/Cookie/Jar.php
Expand Up @@ -70,8 +70,9 @@ public function offsetExists($key) {
* @return string Item value
*/
public function offsetGet($key) {
if (!isset($this->cookies[$key]))
if (!isset($this->cookies[$key])) {
return null;
}

return $this->cookies[$key];
}
Expand Down Expand Up @@ -132,7 +133,7 @@ public function register(Requests_Hooker $hooks) {
* @param array $options
*/
public function before_request($url, &$headers, &$data, &$type, &$options) {
if ( ! $url instanceof Requests_IRI ) {
if (!$url instanceof Requests_IRI) {
$url = new Requests_IRI($url);
}

Expand All @@ -146,7 +147,7 @@ public function before_request($url, &$headers, &$data, &$type, &$options) {
continue;
}

if ( $cookie->domain_matches( $url->host ) ) {
if ($cookie->domain_matches($url->host)) {
$cookies[] = $cookie->format_for_header();
}
}
Expand All @@ -162,7 +163,7 @@ public function before_request($url, &$headers, &$data, &$type, &$options) {
*/
public function before_redirect_check(Requests_Response &$return) {
$url = $return->url;
if ( ! $url instanceof Requests_IRI ) {
if (!$url instanceof Requests_IRI) {
$url = new Requests_IRI($url);
}

Expand Down

0 comments on commit 3626938

Please sign in to comment.