From 9d9974b8f8ae57124c052d427ea6a0b5502574e4 Mon Sep 17 00:00:00 2001 From: ProtossChrisTW Date: Sat, 10 Aug 2024 18:09:25 +0800 Subject: [PATCH 1/3] fix: Remove default content-length header from MultipartStreamBuilder class --- CHANGELOG.md | 4 ++++ src/MultipartStreamBuilder.php | 13 +++---------- tests/FunctionTest.php | 4 ++-- 3 files changed, 9 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 30431e2..1962b37 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Change Log +## 1.3.2 - 2024-08-10 + +- Remove default `Content-Length` header from MultipartStreamBuilder class. + ## 1.3.1 - 2024-06-10 - Added missing mimetype for `.webp` images. diff --git a/src/MultipartStreamBuilder.php b/src/MultipartStreamBuilder.php index a3bcd7b..ca54c05 100644 --- a/src/MultipartStreamBuilder.php +++ b/src/MultipartStreamBuilder.php @@ -95,9 +95,9 @@ public function addData($resource, array $headers = []) * @param string|resource|StreamInterface $resource * @param array $options { * - * @var array $headers additional headers ['header-name' => 'header-value'] - * @var string $filename - * } + * @var array $headers additional headers ['header-name' => 'header-value'] + * @var string $filename + * } * * @return MultipartStreamBuilder */ @@ -185,13 +185,6 @@ private function prepareHeaders($name, StreamInterface $stream, $filename, array } } - // Set a default content-length header if one was not provided - if (!$this->hasHeader($headers, 'content-length')) { - if ($length = $stream->getSize()) { - $headers['Content-Length'] = (string) $length; - } - } - // Set a default Content-Type if one was not provided if (!$this->hasHeader($headers, 'content-type') && $hasFilename) { if ($type = $this->getMimetypeHelper()->getMimetypeFromFilename($filename)) { diff --git a/tests/FunctionTest.php b/tests/FunctionTest.php index 64b31a6..2d210e8 100644 --- a/tests/FunctionTest.php +++ b/tests/FunctionTest.php @@ -88,13 +88,13 @@ public function testHeaders() $this->assertTrue(false === strpos($multipartStream, 'Content-Disposition:')); } - public function testContentLength() + public function testShouldNotContainContentLength() { $builder = new MultipartStreamBuilder(); $builder->addResource('foobar', 'stream contents'); $multipartStream = (string) $builder->build(); - $this->assertTrue(false !== strpos($multipartStream, 'Content-Length: 15')); + $this->assertTrue(false === strpos($multipartStream, 'Content-Length: 15')); } public function testFormName() From 17794b4d8776043c3542691739d1ae25fcc850ba Mon Sep 17 00:00:00 2001 From: David Buchmann Date: Sun, 1 Sep 2024 12:56:33 +0200 Subject: [PATCH 2/3] Apply suggestions from code review --- CHANGELOG.md | 4 ++-- src/MultipartStreamBuilder.php | 6 +++--- tests/FunctionTest.php | 5 ++++- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1962b37..6bb487d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,8 @@ # Change Log -## 1.3.2 - 2024-08-10 +## 1.4.0 - 2024-09-01 -- Remove default `Content-Length` header from MultipartStreamBuilder class. +- No longer automatically add a `Content-Length` header for each part in MultipartStreamBuilder class to comply with RFC 7578 section 4.8. ## 1.3.1 - 2024-06-10 diff --git a/src/MultipartStreamBuilder.php b/src/MultipartStreamBuilder.php index ca54c05..0ba62de 100644 --- a/src/MultipartStreamBuilder.php +++ b/src/MultipartStreamBuilder.php @@ -95,9 +95,9 @@ public function addData($resource, array $headers = []) * @param string|resource|StreamInterface $resource * @param array $options { * - * @var array $headers additional headers ['header-name' => 'header-value'] - * @var string $filename - * } + * @var array $headers additional headers ['header-name' => 'header-value'] + * @var string $filename + * } * * @return MultipartStreamBuilder */ diff --git a/tests/FunctionTest.php b/tests/FunctionTest.php index 2d210e8..52e84ea 100644 --- a/tests/FunctionTest.php +++ b/tests/FunctionTest.php @@ -88,13 +88,16 @@ public function testHeaders() $this->assertTrue(false === strpos($multipartStream, 'Content-Disposition:')); } + /** + * Comply with RFC 7578 section 4.8 + */ public function testShouldNotContainContentLength() { $builder = new MultipartStreamBuilder(); $builder->addResource('foobar', 'stream contents'); $multipartStream = (string) $builder->build(); - $this->assertTrue(false === strpos($multipartStream, 'Content-Length: 15')); + $this->assertTrue(false === strpos($multipartStream, 'Content-Length:')); } public function testFormName() From c63d9894f3b28e334fdf86058faa7cf738a9da53 Mon Sep 17 00:00:00 2001 From: David Buchmann Date: Sun, 1 Sep 2024 12:59:17 +0200 Subject: [PATCH 3/3] styleci --- src/MultipartStreamBuilder.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/MultipartStreamBuilder.php b/src/MultipartStreamBuilder.php index 0ba62de..ca54c05 100644 --- a/src/MultipartStreamBuilder.php +++ b/src/MultipartStreamBuilder.php @@ -95,9 +95,9 @@ public function addData($resource, array $headers = []) * @param string|resource|StreamInterface $resource * @param array $options { * - * @var array $headers additional headers ['header-name' => 'header-value'] - * @var string $filename - * } + * @var array $headers additional headers ['header-name' => 'header-value'] + * @var string $filename + * } * * @return MultipartStreamBuilder */