From eff53feca4ec0c753fee52c5267a5c09373452b4 Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Wed, 27 Mar 2024 16:47:51 +0100 Subject: [PATCH 1/3] prevent generating values that will be accepted --- tests/Header/ContentType/BoundaryTest.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/Header/ContentType/BoundaryTest.php b/tests/Header/ContentType/BoundaryTest.php index 4e0b5e5..377bda8 100644 --- a/tests/Header/ContentType/BoundaryTest.php +++ b/tests/Header/ContentType/BoundaryTest.php @@ -35,7 +35,9 @@ public function testOf() public function testThrowWhenRandomString() { $this - ->forAll(Set\Unicode::strings()) + ->forAll(Set\Unicode::strings()->filter( + static fn($string) => !\preg_match('~^[a-zA-Z0-9 \'()+_,-./:=?]{1,70}$~', $string), + )) ->then(function($random) { try { Boundary::of($random); From e8d67147e22de84bb8f2f99d37019dbfc01640e4 Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Thu, 27 Jun 2024 11:16:49 +0200 Subject: [PATCH 2/3] derive laziness of message bodies from source bodies --- composer.json | 2 +- src/Content/Multipart.php | 7 ++++--- src/Content/Multipart/File.php | 10 +++++----- src/Request/Stringable.php | 13 ++++++++----- src/Response/Stringable.php | 13 ++++++++----- src/ServerRequest/Stringable.php | 11 +++++++---- 6 files changed, 33 insertions(+), 23 deletions(-) diff --git a/composer.json b/composer.json index df6a047..ed815f2 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,7 @@ "require": { "php": "~8.2", "innmind/url": "~4.0", - "innmind/immutable": "~4.15|~5.0", + "innmind/immutable": "~5.7", "innmind/filesystem": "~7.0", "innmind/io": "~2.2", "innmind/time-continuum": "~3.0", diff --git a/src/Content/Multipart.php b/src/Content/Multipart.php index 485adfa..5fc10b4 100644 --- a/src/Content/Multipart.php +++ b/src/Content/Multipart.php @@ -73,7 +73,7 @@ public function asContent(): Content private function chunks(): Sequence { $boundary = $this->boundaryStr(); - $boundaryLine = Sequence::lazyStartingWith($boundary->append("\r\n")); + $boundaryLine = Sequence::of($boundary->append("\r\n")); if ($this->parts->empty()) { return $boundaryLine->add($boundary->append('--')); @@ -81,8 +81,9 @@ private function chunks(): Sequence return $this ->parts - ->flatMap(static fn($part) => $boundaryLine - ->append($part->chunks()) + ->flatMap(static fn($part) => $part + ->chunks() + ->prepend($boundaryLine) ->add(Str::of("\r\n")), ) ->add($boundary->append('--')); diff --git a/src/Content/Multipart/File.php b/src/Content/Multipart/File.php index afb0855..8fca449 100644 --- a/src/Content/Multipart/File.php +++ b/src/Content/Multipart/File.php @@ -31,8 +31,10 @@ public function __construct(string $name, Binary $file) public function chunks(): Sequence { return $this - ->headers() - ->append($this->file->content()->chunks()); + ->file + ->content() + ->chunks() + ->prepend($this->headers()); } /** @@ -61,9 +63,7 @@ private function headers(): Sequence { $name = $this->file->name()->toString(); $mediaType = $this->file->mediaType()->toString(); - // Use a lazy Sequence here to prevent loading the whole file in memory - // when the the chunks are appended to the headers in self::chunks() - $headers = Sequence::lazyStartingWith( + $headers = Sequence::of( Str::of("Content-Disposition: form-data; name=\"{$this->name}\"; filename=\"$name\"\r\n"), Str::of("Content-Type: $mediaType\r\n"), ); diff --git a/src/Request/Stringable.php b/src/Request/Stringable.php index 45793b0..520c66b 100644 --- a/src/Request/Stringable.php +++ b/src/Request/Stringable.php @@ -32,13 +32,16 @@ public function __invoke(Request $request): Content ->map(static fn($header) => $header->toString()) ->map(Str::of(...)) ->map(static fn($header) => $header->append("\n")); - $body = $request->body()->chunks(); return Content::ofChunks( - Sequence::lazyStartingWith($status) - ->append($headers) - ->add(Str::of("\n")) - ->append($body), + $request + ->body() + ->chunks() + ->prepend( + Sequence::of($status) + ->append($headers) + ->add(Str::of("\n")), + ), ); } diff --git a/src/Response/Stringable.php b/src/Response/Stringable.php index 01a465f..8c09f28 100644 --- a/src/Response/Stringable.php +++ b/src/Response/Stringable.php @@ -32,13 +32,16 @@ public function __invoke(Response $response): Content ->map(static fn($header) => $header->toString()) ->map(Str::of(...)) ->map(static fn($header) => $header->append("\n")); - $body = $response->body()->chunks(); return Content::ofChunks( - Sequence::lazyStartingWith($status) - ->append($headers) - ->add(Str::of("\n")) - ->append($body), + $response + ->body() + ->chunks() + ->prepend( + Sequence::of($status) + ->append($headers) + ->add(Str::of("\n")), + ), ); } diff --git a/src/ServerRequest/Stringable.php b/src/ServerRequest/Stringable.php index abe640b..ded46ed 100644 --- a/src/ServerRequest/Stringable.php +++ b/src/ServerRequest/Stringable.php @@ -35,10 +35,13 @@ public function __invoke(ServerRequest $request): Content ->map(static fn($header) => $header->append("\n")); return Content::ofChunks( - Sequence::lazyStartingWith($status) - ->append($headers) - ->add(Str::of("\n")) - ->append($this->bodyChunks($request)), + $this + ->bodyChunks($request) + ->prepend( + Sequence::of($status) + ->append($headers) + ->add(Str::of("\n")), + ), ); } From c7ffa2dda36bba4aa75e2d229a9289a4b1bb5a8d Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Thu, 27 Jun 2024 11:27:02 +0200 Subject: [PATCH 3/3] update changelog --- CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1e91676..c774fc9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +## 7.1.0 - 2024-06-27 + +### Changed + +- Requires `innmind/immutable:~5.7` +- Derive bodies laziness from source bodies + ## 7.0.1 - 2024-03-27 ### Fixed