Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
* develop:
  update changelog
  derive laziness of message bodies from source bodies
  prevent generating values that will be accepted
  • Loading branch information
Baptouuuu committed Jun 27, 2024
2 parents f301457 + c7ffa2d commit 942fffe
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 24 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 4 additions & 3 deletions src/Content/Multipart.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,17 @@ 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('--'));
}

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('--'));
Expand Down
10 changes: 5 additions & 5 deletions src/Content/Multipart/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

/**
Expand Down Expand Up @@ -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"),
);
Expand Down
13 changes: 8 additions & 5 deletions src/Request/Stringable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
),
);
}

Expand Down
13 changes: 8 additions & 5 deletions src/Response/Stringable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
),
);
}

Expand Down
11 changes: 7 additions & 4 deletions src/ServerRequest/Stringable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
),
);
}

Expand Down
4 changes: 3 additions & 1 deletion tests/Header/ContentType/BoundaryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 942fffe

Please sign in to comment.