Skip to content

Commit

Permalink
fix: Remove default content-length header from MultipartStreamBuilder…
Browse files Browse the repository at this point in the history
… class (#63)

* fix: Remove default content-length header from MultipartStreamBuilder class

---------

Co-authored-by: David Buchmann <david@liip.ch>
  • Loading branch information
chris-lee-lb and dbu authored Sep 1, 2024
1 parent ed56da2 commit 4087940
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

## 1.4.0 - 2024-09-01

- 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

- Added missing mimetype for `.webp` images.
Expand Down
13 changes: 3 additions & 10 deletions src/MultipartStreamBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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)) {
Expand Down
7 changes: 5 additions & 2 deletions tests/FunctionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,16 @@ public function testHeaders()
$this->assertTrue(false === strpos($multipartStream, 'Content-Disposition:'));
}

public function testContentLength()
/**
* 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()
Expand Down

0 comments on commit 4087940

Please sign in to comment.