Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Remove default content-length header from MultipartStreamBuilder class #63

Merged
merged 3 commits into from
Sep 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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']
dbu marked this conversation as resolved.
Show resolved Hide resolved
* @var string $filename
dbu marked this conversation as resolved.
Show resolved Hide resolved
* }
dbu marked this conversation as resolved.
Show resolved Hide resolved
*
* @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()
dbu marked this conversation as resolved.
Show resolved Hide resolved
{
$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
Loading