Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 142 additions & 0 deletions src/Symfony/Component/HttpClient/CachingHttpClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\HttpClient;

use Psr\Log\LoggerInterface;
use Symfony\Component\HttpClient\Response\MockResponse;
use Symfony\Component\HttpClient\Response\ResponseStream;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpCache\HttpCache;
use Symfony\Component\HttpKernel\HttpCache\StoreInterface;
use Symfony\Component\HttpKernel\HttpClientKernel;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
use Symfony\Contracts\HttpClient\ResponseStreamInterface;

/**
* Adds caching on top of an HTTP client.
*
* The implementation buffers responses in memory and doesn't stream directly from the network.
* You can disable/enable this layer by setting option "no_cache" under "extra" to true/false.
* By default, caching is enabled unless the "buffer" option is set to false.
*
* @author Nicolas Grekas <[email protected]>
*/
class CachingHttpClient implements HttpClientInterface
{
use HttpClientTrait;

private $client;
private $cache;
private $defaultOptions = self::OPTIONS_DEFAULTS;

public function __construct(HttpClientInterface $client, StoreInterface $store, array $defaultOptions = [], LoggerInterface $logger = null)
{
if (!class_exists(HttpClientKernel::class)) {
throw new \LogicException(sprintf('Using "%s" requires that the HttpKernel component version 4.3 or higher is installed, try running "composer require symfony/http-kernel:^4.3".', __CLASS__));
}

$this->client = $client;
$kernel = new HttpClientKernel($client, $logger);
$this->cache = new HttpCache($kernel, $store, null, $defaultOptions);

unset($defaultOptions['debug']);
unset($defaultOptions['default_ttl']);
unset($defaultOptions['private_headers']);
unset($defaultOptions['allow_reload']);
unset($defaultOptions['allow_revalidate']);
unset($defaultOptions['stale_while_revalidate']);
unset($defaultOptions['stale_if_error']);

if ($defaultOptions) {
[, $this->defaultOptions] = self::prepareRequest(null, null, $defaultOptions, $this->defaultOptions);
}
}

/**
* {@inheritdoc}
*/
public function request(string $method, string $url, array $options = []): ResponseInterface
{
[$url, $options] = $this->prepareRequest($method, $url, $options, $this->defaultOptions, true);
$url = implode('', $url);
$options['extra']['no_cache'] = $options['extra']['no_cache'] ?? !$options['buffer'];

if ($options['extra']['no_cache'] || !empty($options['body']) || !\in_array($method, ['GET', 'HEAD', 'OPTIONS'])) {
return $this->client->request($method, $url, $options);
}

$request = Request::create($url, $method);
$request->attributes->set('http_client_options', $options);

foreach ($options['headers'] as $name => $values) {
if ('cookie' !== $name) {
$request->headers->set($name, $values);
continue;
}

foreach ($values as $cookies) {
foreach (explode('; ', $cookies) as $cookie) {
if ('' !== $cookie) {
$cookie = explode('=', $cookie, 2);
$request->cookies->set($cookie[0], $cookie[1] ?? null);
}
}
}
}

$response = $this->cache->handle($request);
$response = new MockResponse($response->getContent(), [
'http_code' => $response->getStatusCode(),
'raw_headers' => $response->headers->allPreserveCase(),
]);

return MockResponse::fromRequest($method, $url, $options, $response);
}

/**
* {@inheritdoc}
*/
public function stream($responses, float $timeout = null): ResponseStreamInterface
{
if ($responses instanceof ResponseInterface) {
$responses = [$responses];
} elseif (!\is_iterable($responses)) {
throw new \TypeError(sprintf('%s() expects parameter 1 to be an iterable of ResponseInterface objects, %s given.', __METHOD__, \is_object($responses) ? \get_class($responses) : \gettype($responses)));
}

$mockResponses = [];
$clientResponses = [];

foreach ($responses as $response) {
if ($response instanceof MockResponse) {
$mockResponses[] = $response;
} else {
$clientResponses[] = $response;
}
}

if (!$mockResponses) {
return $this->client->stream($clientResponses, $timeout);
}

if (!$clientResponses) {
return new ResponseStream(MockResponse::stream($mockResponses, $timeout));
}

return new ResponseStream((function () use ($mockResponses, $clientResponses, $timeout) {
yield from MockResponse::stream($mockResponses, $timeout);
yield $this->client->stream($clientResponses, $timeout);
})());
}
}
4 changes: 4 additions & 0 deletions src/Symfony/Component/HttpClient/HttpClientTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ private static function mergeDefaultOptions(array $options, array $defaultOption
$options[$k] = $options[$k] ?? $v;
}

if (isset($defaultOptions['extra'])) {
$options['extra'] += $defaultOptions['extra'];
}

if ($defaultOptions['resolve'] ?? false) {
$options['resolve'] += array_change_key_case($defaultOptions['resolve']);
}
Expand Down
10 changes: 10 additions & 0 deletions src/Symfony/Component/HttpClient/HttpOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,4 +309,14 @@ public function capturePeerCertChain(bool $capture)

return $this;
}

/**
* @return $this
*/
public function setExtra(string $name, $value)
{
$this->options['extra'][$name] = $value;

return $this;
}
}
31 changes: 29 additions & 2 deletions src/Symfony/Component/HttpClient/Response/MockResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class MockResponse implements ResponseInterface
use ResponseTrait;

private $body;
private $requestOptions = [];

private static $mainMulti;
private static $idSequence = 0;
Expand All @@ -42,6 +43,31 @@ public function __construct($body = '', array $info = [])
{
$this->body = \is_iterable($body) ? $body : (string) $body;
$this->info = $info + $this->info;

if (!isset($info['raw_headers'])) {
return;
}

$rawHeaders = [];
foreach ($this->info['raw_headers'] as $k => $v) {
if (\is_array($v)) {
$rawHeaders[] = ucwords($k, ' -') . ': ' . implode(', ', $v);
} else {
foreach ((array) $v as $v) {
$rawHeaders[] = (\is_string($k) ? $k . ': ' : '') . $v;
}
}
}

$this->info['raw_headers'] = $rawHeaders;
}

/**
* Returns the options used when doing the request.
*/
public function getRequestOptions(): array
{
return $this->requestOptions;
}

/**
Expand All @@ -66,6 +92,7 @@ protected function close(): void
public static function fromRequest(string $method, string $url, array $options, ResponseInterface $mock): self
{
$response = new self([]);
$response->requestOptions = $options;
$response->id = ++self::$idSequence;
$response->content = ($options['buffer'] ?? true) ? fopen('php://temp', 'w+') : null;
$response->initializer = static function (self $response) {
Expand Down Expand Up @@ -218,11 +245,11 @@ private static function readResponse(self $response, array $options, ResponseInt
$response->addRawHeaders($info['raw_headers'] ?? [], $response->info, $response->headers);
$dlSize = (int) ($response->headers['content-length'][0] ?? 0);

$response->info = [
$response->info = $response->info + $info + [
'start_time' => $response->info['start_time'],
'user_data' => $response->info['user_data'],
'http_code' => $response->info['http_code'],
] + $info + $response->info;
];

if (isset($response->info['total_time'])) {
$response->info['total_time'] = microtime(true) - $response->info['start_time'];
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/HttpClient/Response/ResponseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,10 @@ abstract protected static function select(\stdClass $multi, float $timeout): int

private static function addRawHeaders(array $rawHeaders, array &$info, array &$headers): void
{
$hasStatusLine = false;
foreach ($rawHeaders as $h) {
if (11 <= \strlen($h) && '/' === $h[4] && preg_match('#^HTTP/\d+(?:\.\d+)? ([12345]\d\d) .*#', $h, $m)) {
$hasStatusLine = true;
$headers = [];
$info['http_code'] = (int) $m[1];
} elseif (2 === \count($m = explode(':', $h, 2))) {
Expand All @@ -199,6 +201,9 @@ private static function addRawHeaders(array $rawHeaders, array &$info, array &$h
$info['raw_headers'][] = $h;
}

if (!$hasStatusLine && !empty($info['http_code'])) {
array_unshift($info['raw_headers'], sprintf('HTTP/1.1 %s OK', $info['http_code']));
}
if (!$info['http_code']) {
throw new TransportException('Invalid or missing HTTP status line.');
}
Expand Down
Loading