Always close Guzzle response body after reading it
authorjoshuaruesweg <ruesweg@woltlab.com>
Wed, 21 Apr 2021 09:20:27 +0000 (11:20 +0200)
committerjoshuaruesweg <ruesweg@woltlab.com>
Wed, 21 Apr 2021 09:20:27 +0000 (11:20 +0200)
wcfsetup/install/files/lib/util/HTTPRequest.class.php

index efcc294c20c076623623e598ead5c7e35f7baef8..2cb85ab0678e97d0a86925250aa158a5cba4d659 100644 (file)
@@ -284,22 +284,27 @@ final class HTTPRequest {
                }
                
                if ($this->replyBody === null) {
-                       $bodyLength = 0;
-                       while (!$this->response->getBody()->eof()) {
-                               $toRead = 8192;
-                               if (isset($this->options['maxLength'])) {
-                                       $toRead = min($toRead, $this->options['maxLength'] - $bodyLength);
-                               }
-                               
-                               $data = $this->response->getBody()->read($toRead);
-                               $this->replyBody .= $data;
-                               $bodyLength += strlen($data);
-                               
-                               if (isset($this->options['maxLength']) && $bodyLength >= $this->options['maxLength']) {
-                                       $this->response->getBody()->close();
-                                       break;
-                               }
+                       try {
+                               $bodyLength = 0;
+                               while (!$this->response->getBody()->eof()) {
+                                       $toRead = 8192;
+                                       if (isset($this->options['maxLength'])) {
+                                               $toRead = min($toRead, $this->options['maxLength'] - $bodyLength);
+                                       }
+                                       
+                                       $data = $this->response->getBody()->read($toRead);
+                                       $this->replyBody .= $data;
+                                       $bodyLength += strlen($data);
+                                       
+                                       if (isset($this->options['maxLength']) && $bodyLength >= $this->options['maxLength']) {
+                                               break;
+                                       }
+                               }       
                        }
+                       finally {
+                               $this->response->getBody()->close();
+                       }
+                       
                        if (isset($this->options['maxLength'])) {
                                $this->replyBody = substr($this->replyBody, 0, $this->options['maxLength']);
                        }