-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
[HttpClient] Fix seeking in not-yet-initialized requests #47808
Conversation
Oh ! Going to test this ASAP ! Thanks @nicolas-grekas ! |
029bc14
to
5046e85
Compare
5046e85
to
f4d4da3
Compare
I tested it and it works, however, there is still an issue in our app. This is because of PsrHttpFactory.php#L130. I have the feeling that it also need some adaptations. |
I made further test, but I'm still unable to get the body of a response twice. I'm also unable to reproduce the issue in a few lines of code. I made a screenshot after adding some /**
* {@inheritdoc}
*/
public function sendRequest(RequestInterface $request): ResponseInterface
{
try {
$body = $request->getBody();
if ($body->isSeekable()) {
$body->seek(0);
}
$response = $this->client->request($request->getMethod(), (string) $request->getUri(), [
'headers' => $request->getHeaders(),
'body' => $body->getContents(),
'http_version' => '1.0' === $request->getProtocolVersion() ? '1.0' : null,
]);
$psrResponse = $this->responseFactory->createResponse($response->getStatusCode());
foreach ($response->getHeaders(false) as $name => $values) {
foreach ($values as $value) {
try {
$psrResponse = $psrResponse->withAddedHeader($name, $value);
} catch (\InvalidArgumentException $e) {
// ignore invalid header
}
}
}
if ($response instanceof StreamableInterface) {
dump('Response is StreamableInterface', $response);
$body = $response->toStream(false);
} else {
dump('Response is not StreamableInterface', $response);
$body = StreamWrapper::createResource($response, $this->client);
}
$body = $this->streamFactory->createStreamFromResource($body);
$r = $psrResponse->withBody($body);
dump((string)$r->getBody());
dd((string)$r->getBody());
return $r;
} catch (TransportExceptionInterface $e) {
if ($e instanceof \InvalidArgumentException) {
throw new Psr18RequestException($e, $request);
}
throw new Psr18NetworkException($e, $request);
}
} And here's the result. Sorry for the pixelisation of the information, but they are not relevant here. |
Please provide a reproducing script if you can, I don't be able to
understand without one.
|
I've spent the whole evening trying to reproduce it, without luck. I'll try again this weekend. |
What I propose is to merge this PR, then I'll open another issue for the rest. WDYT ? |
Reported on Slack by @drupol