1
0
mirror of https://github.com/guzzle/guzzle.git synced 2025-01-17 21:38:16 +01:00

Merge pull request #2804 from Nyholm/head-request-fix

Make sure the Request always has a body object
This commit is contained in:
Tobias Nyholm 2020-10-09 15:37:55 +02:00 committed by GitHub
commit 17428d7501
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 60 additions and 24 deletions

View File

@ -405,30 +405,27 @@ class CurlFactory implements CurlFactoryInterface
}
}
// Do not connect a sink for HEAD requests.
if ($easy->request->getMethod() !== 'HEAD') {
if (!isset($options['sink'])) {
// Use a default temp stream if no sink was set.
$options['sink'] = \fopen('php://temp', 'w+');
}
$sink = $options['sink'];
if (!\is_string($sink)) {
$sink = \GuzzleHttp\Psr7\stream_for($sink);
} elseif (!\is_dir(\dirname($sink))) {
// Ensure that the directory exists before failing in curl.
throw new \RuntimeException(\sprintf(
'Directory %s does not exist for sink value of %s',
\dirname($sink),
$sink
));
} else {
$sink = new LazyOpenStream($sink, 'w+');
}
$easy->sink = $sink;
$conf[\CURLOPT_WRITEFUNCTION] = static function ($ch, $write) use ($sink): int {
return $sink->write($write);
};
if (!isset($options['sink'])) {
// Use a default temp stream if no sink was set.
$options['sink'] = \fopen('php://temp', 'w+');
}
$sink = $options['sink'];
if (!\is_string($sink)) {
$sink = \GuzzleHttp\Psr7\stream_for($sink);
} elseif (!\is_dir(\dirname($sink))) {
// Ensure that the directory exists before failing in curl.
throw new \RuntimeException(\sprintf(
'Directory %s does not exist for sink value of %s',
\dirname($sink),
$sink
));
} else {
$sink = new LazyOpenStream($sink, 'w+');
}
$easy->sink = $sink;
$conf[\CURLOPT_WRITEFUNCTION] = static function ($ch, $write) use ($sink): int {
return $sink->write($write);
};
$timeoutRequiresNoSignal = false;
if (isset($options['timeout'])) {

View File

@ -89,7 +89,7 @@ class CurlFactoryTest extends TestCase
$response = $a(new Psr7\Request('HEAD', Server::$url), []);
$response->wait();
self::assertTrue($_SERVER['_curl'][\CURLOPT_NOBODY]);
$checks = [\CURLOPT_WRITEFUNCTION, \CURLOPT_READFUNCTION, \CURLOPT_FILE, \CURLOPT_INFILE];
$checks = [\CURLOPT_READFUNCTION, \CURLOPT_FILE, \CURLOPT_INFILE];
foreach ($checks as $check) {
self::assertArrayNotHasKey($check, $_SERVER['_curl']);
}
@ -358,6 +358,27 @@ class CurlFactoryTest extends TestCase
);
}
/**
* https://github.com/guzzle/guzzle/issues/2799
*/
public function testDecodesGzippedResponsesWithHeaderForHeadRequest()
{
$this->addDecodeResponse();
$handler = new Handler\CurlMultiHandler();
$request = new Psr7\Request('HEAD', Server::$url, ['Accept-Encoding' => 'gzip']);
$response = $handler($request, ['decode_content' => true]);
$response = $response->wait();
self::assertEquals('gzip', $_SERVER['_curl'][\CURLOPT_ENCODING]);
$sent = Server::received()[0];
self::assertEquals('gzip', $sent->getHeaderLine('Accept-Encoding'));
// Verify that the content-length matches the encoded size.
self::assertTrue(
!$response->hasHeader('content-length') ||
$response->getHeaderLine('content-length') == \strlen(\gzencode('test'))
);
}
public function testDoesNotForceDecode()
{
$content = $this->addDecodeResponse();

View File

@ -201,6 +201,24 @@ class StreamHandlerTest extends TestCase
self::assertTrue(!$response->hasHeader('content-length') || $response->getHeaderLine('content-length') == $response->getBody()->getSize());
}
public function testAutomaticallyDecompressGzipHead()
{
Server::flush();
$content = \gzencode('test');
Server::enqueue([
new Response(200, [
'Content-Encoding' => 'gzip',
'Content-Length' => \strlen($content),
], $content)
]);
$handler = new StreamHandler();
$request = new Request('HEAD', Server::$url);
$response = $handler($request, ['decode_content' => true])->wait();
// Verify that the content-length matches the encoded size.
self::assertTrue(!$response->hasHeader('content-length') || $response->getHeaderLine('content-length') == \strlen($content));
}
public function testReportsOriginalSizeAndContentEncodingAfterDecoding()
{
Server::flush();