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

Response isn't ready to read #42

Open
mindplay-dk opened this issue Apr 16, 2024 · 0 comments
Open

Response isn't ready to read #42

mindplay-dk opened this issue Apr 16, 2024 · 0 comments

Comments

@mindplay-dk
Copy link

The returned ResponseInterface instance contains a StreamInterface instance that isn't ready to read.

The issue is here:

private function createResponseFromCurlHandle($curlHandle) : ResponseInterface
{
/** @var int */
$statusCode = curl_getinfo($curlHandle, CURLINFO_RESPONSE_CODE);
$response = $this->responseFactory->createResponse($statusCode);
/** @var float */
$requestTime = curl_getinfo($curlHandle, CURLINFO_TOTAL_TIME);
$response = $response->withAddedHeader('X-Request-Time', sprintf('%.3f ms', $requestTime * 1000));
/** @var ?string */
$message = curl_multi_getcontent($curlHandle);
if ($message === null) {
return $response;
}
/** @var int */
$headerSize = curl_getinfo($curlHandle, CURLINFO_HEADER_SIZE);
$header = substr($message, 0, $headerSize);
$response = $this->populateResponseWithHeaderFields($response, $header);
$body = substr($message, $headerSize);
$response->getBody()->write($body);
return $response;
}

The response gets returned after writing to the stream, which means the stream pointer will be at the end of the stream.

After writing to the response body stream (in line 228) you need to rewind the stream, e.g.:

        $response->getBody()->write($body);
        $response->getBody()->rewind(); // 👈

I'm using nyholm/psr7, but that shouldn't matter - if your StreamInterface implementation is somehow rewinding the body stream automatically after write (or isn't moving the stream pointer ahead in the first place) it isn't following the PSR-7 spec.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant