Skip to content

Invalid Content-Length header added when body size is unknown #327

Closed

Description

@phpbg

When response body has unknown size, an empty Content-Length header is added, which is invalid.
RFC2616 states:

Any Content-Length greater than or equal to zero is a valid value.

And StreamInterface says:

    /**
     * Get the size of the stream if known.
     *
     * @return int|null Returns the size in bytes if known, or null if unknown.
     */
    public function getSize();

In StreamingServer.php, (string)$body->getSize() should be checked before inserting the header:

elseif (!$body instanceof HttpBodyStream) {
            // assign Content-Length header when using a "normal" buffered body string
            $response = $response->withHeader('Content-Length', (string)$body->getSize())->withoutHeader('Transfer-Encoding');
}

Activity

  1. changed the title [-]Invalid Content-Length header added when body size is inknown[/-] [+]Invalid Content-Length header added when body size is unknown[/+] on Nov 11, 2018
  2. WyriHaximus commented on Nov 11, 2018

    @WyriHaximus
    Member

    As always we're open for PR's fixing bugs like these. Would you be interested in doing that?

  3. clue commented on Nov 12, 2018

    @clue
    Member

    @phpbg Thanks for reporting, I understand where you're coming from!

    This project does follow HTTP specs and adds these headers automatically under certain circumstances. In this instance, it looks like there's indeed a small defect when not using the built-in Response class but a third-party one that does indeed follow valid PSR-7 semantics but does not behave as this library expects.

    That being said, it looks like this may not be cause of the issue you're seeing. In your case, it sounds like you want to stream a response body. This project being an event-driven, non-blocking implementation and PSR-7 being a definition for synchronous environments means we've had to implement a number of work-arounds that result in some known limitations. If you want to return a streaming response, this will have to be wrapped in a ReadableStreamInterface. This should be covered in the documentation: https://github.com/reactphp/http#streaming-response

  4. phpbg commented on Nov 12, 2018

    @phpbg
    Author

    You were too fast doing the PR :-)

  5. added this to the v0.8.4 milestone on Jan 16, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions