skyzyx
3/30/2011 - 4:55 PM

Apply to AWS SDK for PHP 1.3.1 to resolve issues with CloudWatch gzip encoding.

Apply to AWS SDK for PHP 1.3.1 to resolve issues with CloudWatch gzip encoding.

--- production/sdk.class.php	2011-03-25 15:17:27.000000000 -0700
+++ development/sdk.class.php	2011-03-30 08:44:35.000000000 -0700
@@ -1195,34 +1195,61 @@
 			return $response;
 		}

 		// Decompress gzipped content
 		if (isset($headers['content-encoding']))
 		{
 			switch (strtolower(trim($headers['content-encoding'], "\x09\x0A\x0D\x20")))
 			{
 				case 'gzip':
 				case 'x-gzip':
+					if (strpos($headers['_info']['url'], 'monitoring.') !== false)
+					{
+						// CloudWatch incorrectly uses the deflate algorithm when they say gzip.
+						if (($uncompressed = gzuncompress($body)) !== false)
+						{
+							$body = $uncompressed;
+						}
+						elseif (($uncompressed = gzinflate($body)) !== false)
+						{
+							$body = $uncompressed;
+						}
+						break;
+					}
+					else
+					{
+						// Everyone else uses gzip correctly.
 					$decoder = new CFGzipDecode($body);
 					if ($decoder->parse())
 					{
 						$body = $decoder->data;
 					}
 					break;
+					}

 				case 'deflate':
-					if (($body = gzuncompress($body)) === false)
-					{
-						if (($body = gzinflate($body)) === false)
+					if (strpos($headers['_info']['url'], 'monitoring.') !== false)
 						{
+						// CloudWatch incorrectly does nothing when they say deflate.
 							continue;
 						}
+					else
+					{
+						// Everyone else uses deflate correctly.
+						if (($uncompressed = gzuncompress($body)) !== false)
+						{
+							$body = $uncompressed;
+						}
+						elseif (($uncompressed = gzinflate($body)) !== false)
+						{
+							$body = $uncompressed;
+						}
 					}
 					break;
 			}
 		}

 		// Look for XML cues
 		if (
 			(isset($headers['content-type']) && ($headers['content-type'] === 'text/xml' || $headers['content-type'] === 'application/xml')) || // We know it's XML
 			(!isset($headers['content-type']) && (stripos($body, '<?xml') === 0 || strpos($body, '<Error>') === 0) || preg_match('/^<(\w*) xmlns="http(s?):\/\/(\w*).amazon(aws)?.com/im', $body)) // Sniff for XML
 		)