Skip to content

Fix Content-Range response when body size equals expected range length#28

Open
ajgarlag wants to merge 1 commit intolaminas:2.2.xfrom
ajgarlag:patch-1
Open

Fix Content-Range response when body size equals expected range length#28
ajgarlag wants to merge 1 commit intolaminas:2.2.xfrom
ajgarlag:patch-1

Conversation

@ajgarlag
Copy link
Copy Markdown

Signed-off-by: Antonio J. García Lagar ajgarcia@c17.net

Q A
Documentation no
Bugfix yes
BC Break no
New Feature no
RFC no
QA no

Description

When the response body has already been limited to the requested range, the emitter should ignore the Content-Range header and emit the full response body.

This is an alternative to #23 to fix #22

Signed-off-by: Antonio J. García Lagar <ajgarcia@c17.net>
@Ocramius Ocramius added the Bug Something isn't working label Apr 21, 2022
@Ocramius Ocramius requested a review from weierophinney April 21, 2022 10:24
@Ocramius Ocramius added this to the 2.2.0 milestone Apr 21, 2022
@boesing
Copy link
Copy Markdown
Member

boesing commented Apr 21, 2022

This looks pretty good. It does not modify existing behavior I guess (at least not a behavior which is covered by tests).
I guess we can supersede #23 for this?


if ($body->isSeekable()) {
$body->seek($first);
$body->seek($body->getSize() === $length ? 0 : $first);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be ===, or <=? In other words, what hapepns when the body size is shorter than the requested length?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case, I think that the code should throw an exception but I preferred to make the minimal edition to fix #22 without modifying any other execution path.

We can fix that case in another PR.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After thinking about this, definitely, we should throw an exception in that case.

When the emitter receives a response object with a Content-Range header, we only have two options here:

  1. The response producer computes the Content-Range header and delegates to the emitter the responsibility of fixing the body content length.
  2. The response producer computes the Content-Range header and fixes the response body content so the received body size equals the length defined in the Content-Range header. (option fixed by this PR).

In the first case, the body size can be greater, equal, or lesser than the length defined in the Content-Range header. If the given body size is lesser than the length defined in the Content-Range header, the response producer failed at computing the Content-Range header and we should throw an exception.

Anyway, as I wrote previously, this PR only fixes option 2 but does not modify any behavior related to option 1, so I think it can be merged safely, and fix the problem with shorter body size in other PR.

@weierophinney What do you think?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

About exceptions: would that leave the door open for DDoS attacks? Having an app crash (on purpose) via generic header manipulation can be problematic.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right: throwing an exception could be a bad idea here. Maybe we can return a 502 response instead because the emitter got an invalid response.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we're trying to solve the problem at the wrong level.
The root of the problem is that the emitter doesn't have the full context to provide the correct range split (this comes on the request only).

If approaching this from a generic PoV (as we currently do), a middleware would be more suitable for parsing the requested ranges and converting the headers/body to provide support for single and multiple ranges.

Trying to make the emitter limit the body but also ignore things if the body was already truncated sounds unnecessary. To me, the emitter should focus on doing a single job: stream the response.

I'd suggest we create a new SapiStreamEmitter that doesn't do anything with Content-Range and deprecate the current one. We should also ask ourselves if the responsibility of parsing ranges and limiting the body belongs to this library.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree 💯 with @lcobucci here, and would gladly accept a PR that:

  • Creates a new SapiStreamEmitter implementation that doesn't do anything with the Content-Range header.
  • Deprecates the current SapiStreamEmitter.
  • Creates middleware for managing ranges.

@Ocramius Ocramius removed this from the 2.2.0 milestone Sep 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Awaiting Author Updates Bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SapiStreamEmitter that does not consider Content-Range

5 participants