Overview

Namespaces

  • Contrib
    • Bundle
      • CoverallsBundle
        • Console
        • Entity
      • CoverallsV1Bundle
        • Api
        • Collector
        • Command
        • Config
        • Entity
          • Git
    • Component
      • File
      • Log
      • System
        • Git
  • Guzzle
    • Batch
      • Exception
    • Cache
    • Common
      • Exception
    • Http
      • Curl
      • Exception
      • Message
      • QueryAggregator
    • Inflection
    • Iterator
    • Log
    • Parser
      • Cookie
      • Message
      • UriTemplate
      • Url
    • Plugin
      • Async
      • Backoff
      • Cache
      • Cookie
        • CookieJar
        • Exception
      • CurlAuth
      • ErrorResponse
        • Exception
      • History
      • Log
      • Md5
      • Mock
      • Oauth
    • Service
      • Builder
      • Command
        • Factory
        • LocationVisitor
          • Request
          • Response
      • Description
      • Exception
      • Resource
    • Stream
  • PHP
  • Psr
    • Log
  • Symfony
    • Component
      • Config
        • Definition
          • Builder
          • Exception
        • Exception
        • Loader
        • Resource
        • Util
      • Console
        • Command
        • Formatter
        • Helper
        • Input
        • Output
        • Tester
      • EventDispatcher
        • Debug
      • Finder
        • Adapter
        • Comparator
        • Exception
        • Expression
        • Iterator
        • Shell
      • Stopwatch
      • Yaml
        • Exception

Classes

  • CommandContentMd5Plugin
  • Md5ValidatorPlugin
  • Overview
  • Namespace
  • Class
  • Tree
  • Todo
 1: <?php
 2: 
 3: namespace Guzzle\Plugin\Md5;
 4: 
 5: use Guzzle\Common\Event;
 6: use Guzzle\Common\Exception\UnexpectedValueException;
 7: use Guzzle\Http\Message\Response;
 8: use Symfony\Component\EventDispatcher\EventSubscriberInterface;
 9: 
10: /**
11:  * Ensures that an the MD5 hash of an entity body matches the Content-MD5
12:  * header (if set) of an HTTP response.  An exception is thrown if the
13:  * calculated MD5 does not match the expected MD5.
14:  */
15: class Md5ValidatorPlugin implements EventSubscriberInterface
16: {
17:     /**
18:      * @var int Maximum Content-Length in bytes to validate
19:      */
20:     protected $contentLengthCutoff;
21: 
22:     /**
23:      * @var bool Whether or not to compare when a Content-Encoding is present
24:      */
25:     protected $contentEncoded;
26: 
27:     /**
28:      * Constructor
29:      *
30:      * @param bool     $contentEncoded      Calculating the MD5 hash of an entity body where a Content-Encoding was
31:      *                                      applied is a more expensive comparison because the entity body will need to
32:      *                                      be compressed in order to get the correct hash.  Set to FALSE to not
33:      *                                      validate the MD5 hash of an entity body with an applied Content-Encoding.
34:      * @param bool|int $contentLengthCutoff Maximum Content-Length (bytes) in which a MD5 hash will be validated. Any
35:      *                                      response with a Content-Length greater than this value will not be validated
36:      *                                      because it will be deemed too memory intensive.
37:      */
38:     public function __construct($contentEncoded = true, $contentLengthCutoff = false)
39:     {
40:         $this->contentLengthCutoff = $contentLengthCutoff;
41:         $this->contentEncoded = $contentEncoded;
42:     }
43: 
44:     /**
45:      * {@inheritdoc}
46:      */
47:     public static function getSubscribedEvents()
48:     {
49:         return array('request.complete' => array('onRequestComplete', 255));
50:     }
51: 
52:     /**
53:      * {@inheritdoc}
54:      * @throws UnexpectedValueException
55:      */
56:     public function onRequestComplete(Event $event)
57:     {
58:         $response = $event['response'];
59: 
60:         if (!$contentMd5 = $response->getContentMd5()) {
61:             return;
62:         }
63: 
64:         $contentEncoding = $response->getContentEncoding();
65:         if ($contentEncoding && !$this->contentEncoded) {
66:             return false;
67:         }
68: 
69:         // Make sure that the size of the request is under the cutoff size
70:         if ($this->contentLengthCutoff) {
71:             $size = $response->getContentLength() ?: $response->getBody()->getSize();
72:             if (!$size || $size > $this->contentLengthCutoff) {
73:                 return;
74:             }
75:         }
76: 
77:         if (!$contentEncoding) {
78:             $hash = $response->getBody()->getContentMd5();
79:         } elseif ($contentEncoding == 'gzip') {
80:             $response->getBody()->compress('zlib.deflate');
81:             $hash = $response->getBody()->getContentMd5();
82:             $response->getBody()->uncompress();
83:         } elseif ($contentEncoding == 'compress') {
84:             $response->getBody()->compress('bzip2.compress');
85:             $hash = $response->getBody()->getContentMd5();
86:             $response->getBody()->uncompress();
87:         } else {
88:             return;
89:         }
90: 
91:         if ($contentMd5 !== $hash) {
92:             throw new UnexpectedValueException(
93:                 "The response entity body may have been modified over the wire.  The Content-MD5 "
94:                 . "received ({$contentMd5}) did not match the calculated MD5 hash ({$hash})."
95:             );
96:         }
97:     }
98: }
99: 
php-coveralls API documentation generated by ApiGen 2.8.0