1
0
mirror of https://github.com/guzzle/guzzle.git synced 2025-02-13 03:45:22 +01:00

[Http] Making the Content-MD5 validation plugin more readable and adding more test coverage

This commit is contained in:
Michael Dowling 2011-04-07 23:27:01 -05:00
parent 85ed138b78
commit 6bff5cbd83
2 changed files with 54 additions and 37 deletions

View File

@ -55,46 +55,53 @@ class Md5ValidatorPlugin implements Observer
public function update(Subject $subject, $event, $context = null)
{
/* @var $subject EntityEnclosingRequest */
if ($event == 'request.complete' && $subject instanceof RequestInterface) {
$contentMd5 = $context->getContentMd5();
if ($contentMd5) {
$contentEncoding = $context->getContentEncoding();
if (!$contentEncoding || $this->contentEncoded) {
// Make sure that the request's size is under the cutoff size
$size = $context->getContentLength() ?: $context->getBody()->getSize();
if ($size && $size < $this->contentLengthCutoff) {
if ($event != 'request.complete' || !($subject instanceof RequestInterface)) {
return;
}
switch ($contentEncoding) {
case 'gzip':
$context->getBody()->compress('zlib.deflate');
$hash = $context->getBody()->getContentMd5();
$context->getBody()->uncompress();
break;
case 'compress':
$context->getBody()->compress('bzip2.compress');
$hash = $context->getBody()->getContentMd5();
$context->getBody()->uncompress();
break;
default:
if ($contentEncoding) {
return;
}
$hash = $context->getBody()->getContentMd5();
break;
}
$contentMd5 = $context->getContentMd5();
if (!$contentMd5) {
return;
}
if ($contentMd5 !== $hash) {
throw new \UnexpectedValueException(sprintf(
'The response entity body may have been '
. 'modified over the wire. The Content-MD5 '
. 'received (%s) did not match the calculated '
. 'MD5 hash (%s).',
$contentMd5, $hash
));
}
}
$contentEncoding = $context->getContentEncoding();
if ($contentEncoding && !$this->contentEncoded) {
return false;
}
// Make sure that the request's size is under the cutoff size
$size = $context->getContentLength() ?: $context->getBody()->getSize();
if (!$size || $size > $this->contentLengthCutoff) {
return;
}
switch ($contentEncoding) {
case 'gzip':
$context->getBody()->compress('zlib.deflate');
$hash = $context->getBody()->getContentMd5();
$context->getBody()->uncompress();
break;
case 'compress':
$context->getBody()->compress('bzip2.compress');
$hash = $context->getBody()->getContentMd5();
$context->getBody()->uncompress();
break;
default:
if ($contentEncoding) {
return;
}
}
$hash = $context->getBody()->getContentMd5();
break;
}
if ($contentMd5 !== $hash) {
throw new \UnexpectedValueException(sprintf(
'The response entity body may have been '
. 'modified over the wire. The Content-MD5 '
. 'received (%s) did not match the calculated '
. 'MD5 hash (%s).',
$contentMd5, $hash
));
}
}
}

View File

@ -33,6 +33,10 @@ class Md5ValidatorPluginTest extends \Guzzle\Tests\GuzzleTestCase
), 'abc');
$request->getEventManager()->notify('request.complete', $response);
// Try again with no Content-MD5
$response->removeHeader('Content-MD5');
$request->getEventManager()->notify('request.complete', $response);
}
/**
@ -108,5 +112,11 @@ class Md5ValidatorPluginTest extends \Guzzle\Tests\GuzzleTestCase
'Content-Encoding' => 'compress'
), 'abc');
$request->getEventManager()->notify('request.complete', $response);
// Try again with encoding and disabled content-encoding checks
$request->getEventManager()->detach($plugin);
$plugin = new Md5ValidatorPlugin(false);
$request->getEventManager()->attach($plugin);
$request->getEventManager()->notify('request.complete', $response);
}
}