Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c915a31

Browse files
author
Ruben Jacobs
committed
Use regex as buffer + tests + isset check
1 parent d221a9e commit c915a31

File tree

6 files changed

+85
-20
lines changed

6 files changed

+85
-20
lines changed

src/Symfony/Component/HttpClient/Response/CurlResponse.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,8 @@ private static function parseHeaderLine($ch, string $data, array &$info, array &
287287
// Regular header line: add it to the list
288288

289289
self::addResponseHeaders([substr($data, 0, -2)], $info, $headers);
290-
if (\array_key_exists('content-type', $headers)) {
291-
self::handleContentType($content, $headers['content-type'][0]);
290+
if (isset($headers['content-type'], $options['buffer']) && is_string($options['buffer'])) {
291+
self::handleContentType($content, $headers['content-type'][0], $options['buffer']);
292292
}
293293

294294
if (0 !== strpos($data, 'HTTP/')) {

src/Symfony/Component/HttpClient/Response/NativeResponse.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ final class NativeResponse implements ResponseInterface
3535
private $inflate;
3636
private $multi;
3737
private $debugBuffer;
38+
private $bufferRegex;
3839

3940
/**
4041
* @internal
@@ -51,6 +52,7 @@ public function __construct(NativeClientState $multi, $context, string $url, $op
5152
$this->resolveRedirect = $resolveRedirect;
5253
$this->onProgress = $onProgress;
5354
$this->content = $options['buffer'] ? fopen('php://temp', 'w+') : null;
55+
$this->bufferRegex = isset($options['buffer']) && is_string($options['buffer']) ? $options['buffer'] : null;
5456

5557
// Temporary resources to dechunk/inflate the response stream
5658
$this->buffer = fopen('php://temp', 'w+');
@@ -130,8 +132,8 @@ private function open(): void
130132
// Send request and follow redirects when needed
131133
$this->handle = $h = fopen($url, 'r', false, $this->context);
132134
self::addResponseHeaders($http_response_header, $this->info, $this->headers, $this->info['debug']);
133-
if (\array_key_exists('content-type', $this->headers)) {
134-
self::handleContentType($this->content, $this->headers['content-type'][0]);
135+
if (null !== $this->bufferRegex && isset($this->headers['content-type'])) {
136+
self::handleContentType($this->content, $this->headers['content-type'][0], $this->bufferRegex);
135137
}
136138

137139
$url = ($this->resolveRedirect)($this->multi, $this->headers['location'][0] ?? null, $this->context);

src/Symfony/Component/HttpClient/Response/ResponseTrait.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,11 +235,10 @@ private static function addResponseHeaders(array $responseHeaders, array &$info,
235235
}
236236
}
237237

238-
private static function handleContentType(&$content, string $contentType)
238+
private static function handleContentType(&$content, string $contentType, string $bufferRegex): void
239239
{
240240
$contentType = substr($contentType, 0, strpos($contentType, ';'));
241-
if (1 === preg_match('/(text\/[a-z]+|[a-z]+\/(json|xml)$)/', $contentType) && null === $content) {
242-
$options['buffer'] = true;
241+
if (null === $content && preg_match($bufferRegex, $contentType)) {
243242
$content = fopen('php://temp', 'w+');
244243
}
245244
}

src/Symfony/Component/HttpClient/Tests/CurlHttpClientTest.php

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
use Psr\Log\AbstractLogger;
1515
use Symfony\Component\HttpClient\CurlHttpClient;
16+
use Symfony\Component\HttpClient\MockHttpClient;
17+
use Symfony\Component\HttpClient\Response\MockResponse;
1618
use Symfony\Contracts\HttpClient\HttpClientInterface;
1719

1820
/**
@@ -72,34 +74,48 @@ public function log($level, $message, array $context = [])
7274
$this->assertSame($expected, $logger->logs);
7375
}
7476

75-
public function testBufferForJson(): void
77+
public function testJsonRegexBuffer(): void
7678
{
77-
$client = new CurlHttpClient(['buffer' => false]);
78-
$response = $client->request('GET', 'https://jsonplaceholder.typicode.com/posts');
79+
$client = new CurlHttpClient(['buffer' => '/([a-z]+\/json$)/']);
80+
$response = $client->request('GET', 'https://jsonplaceholder.typicode.com/users');
81+
82+
$firstContent = $response->getContent();
83+
$secondContent = $response->getContent();
84+
85+
$this->assertSame($firstContent, $secondContent);
86+
}
87+
88+
public function testPdfRegexBuffer(): void
89+
{
90+
$client = new CurlHttpClient(['buffer' => '/application\/pdf$/']);
91+
$response = $client->request('GET', 'http://s2.q4cdn.com/235752014/files/doc_downloads/test.pdf');
92+
93+
$firstContent = $response->getContent();
94+
$secondContent = $response->getContent();
7995

80-
$this->assertSame($response->getContent(false), $response->getContent(false));
96+
$this->assertSame($firstContent, $secondContent);
8197
}
8298

8399
/**
84100
* @expectedException \Symfony\Component\HttpClient\Exception\TransportException
85101
*/
86-
public function testBufferForImagesWithFalseBuffer(): void
102+
public function testFalseBuffer(): void
87103
{
88104
$client = new CurlHttpClient(['buffer' => false]);
89-
$response = $client->request('GET', 'http://s2.q4cdn.com/235752014/files/doc_downloads/test.pdf');
105+
$response = $client->request('GET', 'https://jsonplaceholder.typicode.com/users');
90106

91-
$pdf = $response->getContent(false);
92-
$secondPdf = $response->getContent(false);
107+
$firstPosts = $response->getContent();
108+
$secondPosts = $response->getContent();
93109
}
94110

95-
public function testBufferForImagesWithTrueBuffer(): void
111+
public function testTrueBuffer(): void
96112
{
97113
$client = new CurlHttpClient(['buffer' => true]);
98-
$response = $client->request('GET', 'http://s2.q4cdn.com/235752014/files/doc_downloads/test.pdf');
114+
$response = $client->request('GET', 'https://jsonplaceholder.typicode.com/users');
99115

100-
$pdf = $response->getContent(false);
101-
$secondPdf = $response->getContent(false);
116+
$firstContent = $response->getContent();
117+
$secondContent = $response->getContent();
102118

103-
$this->assertSame($pdf, $secondPdf);
119+
$this->assertSame($firstContent, $secondContent);
104120
}
105121
}

src/Symfony/Component/HttpClient/Tests/MockHttpClientTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ protected function getHttpClient(string $testCase): HttpClientInterface
122122
case 'testUncheckedTimeoutThrows':
123123
$body = ['<1>', '', '<2>'];
124124
$responses[] = new MockResponse($body, ['response_headers' => $headers]);
125+
break;
126+
case 'testBufferRegex':
127+
125128
break;
126129
}
127130

src/Symfony/Component/HttpClient/Tests/NativeHttpClientTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,49 @@ protected function getHttpClient(string $testCase): HttpClientInterface
2020
{
2121
return new NativeHttpClient();
2222
}
23+
24+
public function testJsonRegexBuffer(): void
25+
{
26+
$client = new NativeHttpClient(['buffer' => '/([a-z]+\/json$)/']);
27+
$response = $client->request('GET', 'https://jsonplaceholder.typicode.com/users');
28+
29+
$firstContent = $response->getContent();
30+
$secondContent = $response->getContent();
31+
32+
$this->assertSame($firstContent, $secondContent);
33+
}
34+
35+
public function testPdfRegexBuffer(): void
36+
{
37+
$client = new NativeHttpClient(['buffer' => '/application\/pdf$/']);
38+
$response = $client->request('GET', 'http://s2.q4cdn.com/235752014/files/doc_downloads/test.pdf');
39+
40+
$firstContent = $response->getContent();
41+
$secondContent = $response->getContent();
42+
43+
$this->assertSame($firstContent, $secondContent);
44+
}
45+
46+
/**
47+
* @expectedException \Symfony\Component\HttpClient\Exception\TransportException
48+
*/
49+
public function testFalseBuffer(): void
50+
{
51+
$client = new NativeHttpClient(['buffer' => false]);
52+
$response = $client->request('GET', 'https://jsonplaceholder.typicode.com/users');
53+
54+
$firstPosts = $response->getContent();
55+
$secondPosts = $response->getContent();
56+
}
57+
58+
public function testTrueBuffer(): void
59+
{
60+
$client = new NativeHttpClient(['buffer' => true]);
61+
$response = $client->request('GET', 'https://jsonplaceholder.typicode.com/users');
62+
63+
$firstContent = $response->getContent();
64+
$secondContent = $response->getContent();
65+
66+
$this->assertSame($firstContent, $secondContent);
67+
}
2368
}

0 commit comments

Comments
 (0)