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

Skip to content

Commit 755f411

Browse files
minor #30831 [HttpClient][Contracts] rename "raw_headers" to "response_headers" (nicolas-grekas)
This PR was merged into the 4.3-dev branch. Discussion ---------- [HttpClient][Contracts] rename "raw_headers" to "response_headers" | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - A preliminary step before adding the `request_headers` info on responses to ease debugging. Commits ------- 0b21268 [HttpClient][Contracts] rename "raw_headers" to "response_headers"
2 parents 50a5dfd + 0b21268 commit 755f411

15 files changed

+65
-65
lines changed

src/Symfony/Component/BrowserKit/Tests/HttpBrowserTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function __construct(array $server = [], History $history = null, CookieJ
3636

3737
return new MockResponse($this->nextResponse->getContent(), [
3838
'http_code' => $this->nextResponse->getStatusCode(),
39-
'raw_headers' => $this->nextResponse->getHeaders(),
39+
'response_headers' => $this->nextResponse->getHeaders(),
4040
]);
4141
});
4242
parent::__construct($client);

src/Symfony/Component/HttpClient/CachingHttpClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public function request(string $method, string $url, array $options = []): Respo
9797
$response = $this->cache->handle($request);
9898
$response = new MockResponse($response->getContent(), [
9999
'http_code' => $response->getStatusCode(),
100-
'raw_headers' => $response->headers->allPreserveCase(),
100+
'response_headers' => $response->headers->allPreserveCase(),
101101
]);
102102

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

src/Symfony/Component/HttpClient/CurlHttpClient.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ public function __construct(array $defaultOptions = [], int $maxHostConnections
7474
return;
7575
}
7676

77-
curl_multi_setopt($mh, CURLMOPT_PUSHFUNCTION, static function ($parent, $pushed, array $rawHeaders) use ($multi) {
78-
return self::handlePush($parent, $pushed, $rawHeaders, $multi);
77+
curl_multi_setopt($mh, CURLMOPT_PUSHFUNCTION, static function ($parent, $pushed, array $requestHeaders) use ($multi) {
78+
return self::handlePush($parent, $pushed, $requestHeaders, $multi);
7979
});
8080
}
8181

@@ -191,7 +191,7 @@ public function request(string $method, string $url, array $options = []): Respo
191191
$curlopts[CURLOPT_ENCODING] = ''; // Enable HTTP compression
192192
}
193193

194-
foreach ($options['raw_headers'] as $header) {
194+
foreach ($options['request_headers'] as $header) {
195195
if (':' === $header[-2] && \strlen($header) - 2 === strpos($header, ': ')) {
196196
// curl requires a special syntax to send empty headers
197197
$curlopts[CURLOPT_HTTPHEADER][] = substr_replace($header, ';', -2);
@@ -282,11 +282,11 @@ public function __destruct()
282282
}
283283
}
284284

285-
private static function handlePush($parent, $pushed, array $rawHeaders, \stdClass $multi): int
285+
private static function handlePush($parent, $pushed, array $requestHeaders, \stdClass $multi): int
286286
{
287287
$headers = [];
288288

289-
foreach ($rawHeaders as $h) {
289+
foreach ($requestHeaders as $h) {
290290
if (false !== $i = strpos($h, ':', 1)) {
291291
$headers[substr($h, 0, $i)] = substr($h, 1 + $i);
292292
}
@@ -348,21 +348,21 @@ private static function createRedirectResolver(array $options, string $host): \C
348348
$redirectHeaders = [];
349349
if (0 < $options['max_redirects']) {
350350
$redirectHeaders['host'] = $host;
351-
$redirectHeaders['with_auth'] = $redirectHeaders['no_auth'] = array_filter($options['raw_headers'], static function ($h) {
351+
$redirectHeaders['with_auth'] = $redirectHeaders['no_auth'] = array_filter($options['request_headers'], static function ($h) {
352352
return 0 !== stripos($h, 'Host:');
353353
});
354354

355355
if (isset($options['headers']['authorization']) || isset($options['headers']['cookie'])) {
356-
$redirectHeaders['no_auth'] = array_filter($options['raw_headers'], static function ($h) {
356+
$redirectHeaders['no_auth'] = array_filter($options['request_headers'], static function ($h) {
357357
return 0 !== stripos($h, 'Authorization:') && 0 !== stripos($h, 'Cookie:');
358358
});
359359
}
360360
}
361361

362362
return static function ($ch, string $location) use ($redirectHeaders) {
363363
if ($redirectHeaders && $host = parse_url($location, PHP_URL_HOST)) {
364-
$rawHeaders = $redirectHeaders['host'] === $host ? $redirectHeaders['with_auth'] : $redirectHeaders['no_auth'];
365-
curl_setopt($ch, CURLOPT_HTTPHEADER, $rawHeaders);
364+
$requestHeaders = $redirectHeaders['host'] === $host ? $redirectHeaders['with_auth'] : $redirectHeaders['no_auth'];
365+
curl_setopt($ch, CURLOPT_HTTPHEADER, $requestHeaders);
366366
}
367367

368368
$url = self::parseUrl(curl_getinfo($ch, CURLINFO_EFFECTIVE_URL));

src/Symfony/Component/HttpClient/Exception/HttpExceptionTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function __construct(ResponseInterface $response)
3131

3232
$httpCodeFound = false;
3333
$isJson = false;
34-
foreach (array_reverse($response->getInfo('raw_headers')) as $h) {
34+
foreach (array_reverse($response->getInfo('response_headers')) as $h) {
3535
if (0 === strpos($h, 'HTTP/')) {
3636
if ($httpCodeFound) {
3737
break;

src/Symfony/Component/HttpClient/HttpClientTrait.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ private static function prepareRequest(?string $method, ?string $url, array $opt
5353
$options['peer_fingerprint'] = self::normalizePeerFingerprint($options['peer_fingerprint']);
5454
}
5555

56-
// Compute raw headers
57-
$rawHeaders = $headers = [];
56+
// Compute request headers
57+
$requestHeaders = $headers = [];
5858

5959
foreach ($options['headers'] as $name => $values) {
6060
foreach ($values as $value) {
61-
$rawHeaders[] = $name.': '.$headers[$name][] = $value = (string) $value;
61+
$requestHeaders[] = $name.': '.$headers[$name][] = $value = (string) $value;
6262

6363
if (\strlen($value) !== strcspn($value, "\r\n\0")) {
6464
throw new InvalidArgumentException(sprintf('Invalid header value: CR/LF/NUL found in "%s".', $value));
@@ -95,14 +95,14 @@ private static function prepareRequest(?string $method, ?string $url, array $opt
9595
if (null !== $url) {
9696
// Merge auth with headers
9797
if (($options['auth_basic'] ?? false) && !($headers['authorization'] ?? false)) {
98-
$rawHeaders[] = 'authorization: '.$headers['authorization'][] = 'Basic '.base64_encode($options['auth_basic']);
98+
$requestHeaders[] = 'authorization: '.$headers['authorization'][] = 'Basic '.base64_encode($options['auth_basic']);
9999
}
100100
// Merge bearer with headers
101101
if (($options['auth_bearer'] ?? false) && !($headers['authorization'] ?? false)) {
102-
$rawHeaders[] = 'authorization: '.$headers['authorization'][] = 'Bearer '.$options['auth_bearer'];
102+
$requestHeaders[] = 'authorization: '.$headers['authorization'][] = 'Bearer '.$options['auth_bearer'];
103103
}
104104

105-
$options['raw_headers'] = $rawHeaders;
105+
$options['request_headers'] = $requestHeaders;
106106
unset($options['auth_basic'], $options['auth_bearer']);
107107

108108
// Parse base URI
@@ -128,7 +128,7 @@ private static function prepareRequest(?string $method, ?string $url, array $opt
128128
*/
129129
private static function mergeDefaultOptions(array $options, array $defaultOptions, bool $allowExtraOptions = false): array
130130
{
131-
unset($options['raw_headers'], $defaultOptions['raw_headers']);
131+
unset($options['request_headers'], $defaultOptions['request_headers']);
132132

133133
$options['headers'] = self::normalizeHeaders($options['headers'] ?? []);
134134

src/Symfony/Component/HttpClient/NativeHttpClient.php

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,12 @@ public function request(string $method, string $url, array $options = []): Respo
7777
$options['body'] = self::getBodyAsString($options['body']);
7878

7979
if ('' !== $options['body'] && 'POST' === $method && !isset($options['headers']['content-type'])) {
80-
$options['raw_headers'][] = 'content-type: application/x-www-form-urlencoded';
80+
$options['request_headers'][] = 'content-type: application/x-www-form-urlencoded';
8181
}
8282

8383
if ($gzipEnabled = \extension_loaded('zlib') && !isset($options['headers']['accept-encoding'])) {
8484
// gzip is the most widely available algo, no need to deal with deflate
85-
$options['raw_headers'][] = 'accept-encoding: gzip';
85+
$options['request_headers'][] = 'accept-encoding: gzip';
8686
}
8787

8888
if ($options['peer_fingerprint']) {
@@ -94,7 +94,7 @@ public function request(string $method, string $url, array $options = []): Respo
9494
}
9595

9696
$info = [
97-
'raw_headers' => [],
97+
'response_headers' => [],
9898
'url' => $url,
9999
'error' => null,
100100
'http_method' => $method,
@@ -159,7 +159,7 @@ public function request(string $method, string $url, array $options = []): Respo
159159
[$host, $port, $url['authority']] = self::dnsResolve($url, $this->multi, $info, $onProgress);
160160

161161
if (!isset($options['headers']['host'])) {
162-
$options['raw_headers'][] = 'host: '.$host.$port;
162+
$options['request_headers'][] = 'host: '.$host.$port;
163163
}
164164

165165
$context = [
@@ -203,7 +203,7 @@ public function request(string $method, string $url, array $options = []): Respo
203203

204204
$resolveRedirect = self::createRedirectResolver($options, $host, $proxy, $noProxy, $info, $onProgress);
205205
$context = stream_context_create($context, ['notification' => $notification]);
206-
self::configureHeadersAndProxy($context, $host, $options['raw_headers'], $proxy, $noProxy);
206+
self::configureHeadersAndProxy($context, $host, $options['request_headers'], $proxy, $noProxy);
207207

208208
return new NativeResponse($this->multi, $context, implode('', $url), $options, $gzipEnabled, $info, $resolveRedirect, $onProgress);
209209
}
@@ -326,12 +326,12 @@ private static function createRedirectResolver(array $options, string $host, ?ar
326326
$redirectHeaders = [];
327327
if (0 < $maxRedirects = $options['max_redirects']) {
328328
$redirectHeaders = ['host' => $host];
329-
$redirectHeaders['with_auth'] = $redirectHeaders['no_auth'] = array_filter($options['raw_headers'], static function ($h) {
329+
$redirectHeaders['with_auth'] = $redirectHeaders['no_auth'] = array_filter($options['request_headers'], static function ($h) {
330330
return 0 !== stripos($h, 'Host:');
331331
});
332332

333333
if (isset($options['headers']['authorization']) || isset($options['headers']['cookie'])) {
334-
$redirectHeaders['no_auth'] = array_filter($options['raw_headers'], static function ($h) {
334+
$redirectHeaders['no_auth'] = array_filter($options['request_headers'], static function ($h) {
335335
return 0 !== stripos($h, 'Authorization:') && 0 !== stripos($h, 'Cookie:');
336336
});
337337
}
@@ -376,46 +376,46 @@ private static function createRedirectResolver(array $options, string $host, ?ar
376376

377377
if (false !== (parse_url($location, PHP_URL_HOST) ?? false)) {
378378
// Authorization and Cookie headers MUST NOT follow except for the initial host name
379-
$rawHeaders = $redirectHeaders['host'] === $host ? $redirectHeaders['with_auth'] : $redirectHeaders['no_auth'];
380-
$rawHeaders[] = 'host: '.$host.$port;
381-
self::configureHeadersAndProxy($context, $host, $rawHeaders, $proxy, $noProxy);
379+
$requestHeaders = $redirectHeaders['host'] === $host ? $redirectHeaders['with_auth'] : $redirectHeaders['no_auth'];
380+
$requestHeaders[] = 'host: '.$host.$port;
381+
self::configureHeadersAndProxy($context, $host, $requestHeaders, $proxy, $noProxy);
382382
}
383383

384384
return implode('', $url);
385385
};
386386
}
387387

388-
private static function configureHeadersAndProxy($context, string $host, array $rawHeaders, ?array $proxy, array $noProxy)
388+
private static function configureHeadersAndProxy($context, string $host, array $requestHeaders, ?array $proxy, array $noProxy)
389389
{
390390
if (null === $proxy) {
391-
return stream_context_set_option($context, 'http', 'header', $rawHeaders);
391+
return stream_context_set_option($context, 'http', 'header', $requestHeaders);
392392
}
393393

394394
// Matching "no_proxy" should follow the behavior of curl
395395

396396
foreach ($noProxy as $rule) {
397397
if ('*' === $rule) {
398-
return stream_context_set_option($context, 'http', 'header', $rawHeaders);
398+
return stream_context_set_option($context, 'http', 'header', $requestHeaders);
399399
}
400400

401401
if ($host === $rule) {
402-
return stream_context_set_option($context, 'http', 'header', $rawHeaders);
402+
return stream_context_set_option($context, 'http', 'header', $requestHeaders);
403403
}
404404

405405
$rule = '.'.ltrim($rule, '.');
406406

407407
if (substr($host, -\strlen($rule)) === $rule) {
408-
return stream_context_set_option($context, 'http', 'header', $rawHeaders);
408+
return stream_context_set_option($context, 'http', 'header', $requestHeaders);
409409
}
410410
}
411411

412412
stream_context_set_option($context, 'http', 'proxy', $proxy['url']);
413413
stream_context_set_option($context, 'http', 'request_fulluri', true);
414414

415415
if (null !== $proxy['auth']) {
416-
$rawHeaders[] = 'Proxy-Authorization: '.$proxy['auth'];
416+
$requestHeaders[] = 'Proxy-Authorization: '.$proxy['auth'];
417417
}
418418

419-
return stream_context_set_option($context, 'http', 'header', $rawHeaders);
419+
return stream_context_set_option($context, 'http', 'header', $requestHeaders);
420420
}
421421
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function __construct(\stdClass $multi, $ch, array $options = null, string
4848
$info = &$this->info;
4949
$headers = &$this->headers;
5050

51-
if (!$info['raw_headers']) {
51+
if (!$info['response_headers']) {
5252
// Used to keep track of what we're waiting for
5353
curl_setopt($ch, CURLOPT_PRIVATE, 'headers');
5454
}
@@ -257,7 +257,7 @@ private static function parseHeaderLine($ch, string $data, array &$info, array &
257257

258258
if ("\r\n" !== $data) {
259259
// Regular header line: add it to the list
260-
self::addRawHeaders([substr($data, 0, -2)], $info, $headers);
260+
self::addResponseHeaders([substr($data, 0, -2)], $info, $headers);
261261

262262
if (0 === strpos($data, 'HTTP') && 300 <= $info['http_code'] && $info['http_code'] < 400) {
263263
if (curl_getinfo($ch, CURLINFO_REDIRECT_COUNT) === $options['max_redirects']) {

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,26 +37,26 @@ class MockResponse implements ResponseInterface
3737
* yielding an empty string simulates a timeout,
3838
* exceptions are turned to TransportException
3939
*
40-
* @see ResponseInterface::getInfo() for possible info, e.g. "raw_headers"
40+
* @see ResponseInterface::getInfo() for possible info, e.g. "response_headers"
4141
*/
4242
public function __construct($body = '', array $info = [])
4343
{
4444
$this->body = \is_iterable($body) ? $body : (string) $body;
4545
$this->info = $info + $this->info;
4646

47-
if (!isset($info['raw_headers'])) {
47+
if (!isset($info['response_headers'])) {
4848
return;
4949
}
5050

51-
$rawHeaders = [];
51+
$responseHeaders = [];
5252

53-
foreach ($info['raw_headers'] as $k => $v) {
53+
foreach ($info['response_headers'] as $k => $v) {
5454
foreach ((array) $v as $v) {
55-
$rawHeaders[] = (\is_string($k) ? $k.': ' : '').$v;
55+
$responseHeaders[] = (\is_string($k) ? $k.': ' : '').$v;
5656
}
5757
}
5858

59-
$this->info['raw_headers'] = $rawHeaders;
59+
$this->info['response_headers'] = $responseHeaders;
6060
}
6161

6262
/**
@@ -239,7 +239,7 @@ private static function readResponse(self $response, array $options, ResponseInt
239239
// populate info related to headers
240240
$info = $mock->getInfo() ?: [];
241241
$response->info['http_code'] = ($info['http_code'] ?? 0) ?: $mock->getStatusCode(false) ?: 200;
242-
$response->addRawHeaders($info['raw_headers'] ?? [], $response->info, $response->headers);
242+
$response->addResponseHeaders($info['response_headers'] ?? [], $response->info, $response->headers);
243243
$dlSize = (int) ($response->headers['content-length'][0] ?? 0);
244244

245245
$response->info = [

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ private function open(): void
111111
// Send request and follow redirects when needed
112112
$this->info['fopen_time'] = microtime(true);
113113
$this->handle = $h = fopen($url, 'r', false, $this->context);
114-
self::addRawHeaders($http_response_header, $this->info, $this->headers);
114+
self::addResponseHeaders($http_response_header, $this->info, $this->headers);
115115
$url = ($this->resolveRedirect)($this->multi, $this->headers['location'][0] ?? null, $this->context);
116116
} while (null !== $url);
117117
} catch (\Throwable $e) {

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ trait ResponseTrait
4343
private $content;
4444

4545
private $info = [
46-
'raw_headers' => [],
46+
'response_headers' => [],
4747
'http_code' => 0,
4848
'error' => null,
4949
];
@@ -187,17 +187,17 @@ abstract protected static function perform(\stdClass $multi, array &$responses):
187187
*/
188188
abstract protected static function select(\stdClass $multi, float $timeout): int;
189189

190-
private static function addRawHeaders(array $rawHeaders, array &$info, array &$headers): void
190+
private static function addResponseHeaders(array $responseHeaders, array &$info, array &$headers): void
191191
{
192-
foreach ($rawHeaders as $h) {
192+
foreach ($responseHeaders as $h) {
193193
if (11 <= \strlen($h) && '/' === $h[4] && preg_match('#^HTTP/\d+(?:\.\d+)? ([12345]\d\d) .*#', $h, $m)) {
194194
$headers = [];
195195
$info['http_code'] = (int) $m[1];
196196
} elseif (2 === \count($m = explode(':', $h, 2))) {
197197
$headers[strtolower($m[0])][] = ltrim($m[1]);
198198
}
199199

200-
$info['raw_headers'][] = $h;
200+
$info['response_headers'][] = $h;
201201
}
202202

203203
if (!$info['http_code']) {

src/Symfony/Component/HttpClient/Tests/Exception/HttpExceptionTraitTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function testParseError(string $mimeType, string $json): void
3838
->will($this->returnValueMap([
3939
['http_code', 400],
4040
['url', 'http://example.com'],
41-
['raw_headers', [
41+
['response_headers', [
4242
'HTTP/1.1 400 Bad Request',
4343
'Content-Type: '.$mimeType,
4444
]],

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public function testAuthBearerOption()
169169
{
170170
[, $options] = self::prepareRequest('POST', 'http://example.com', ['auth_bearer' => 'foobar'], HttpClientInterface::OPTIONS_DEFAULTS);
171171
$this->assertSame('Bearer foobar', $options['headers']['authorization'][0]);
172-
$this->assertSame('authorization: Bearer foobar', $options['raw_headers'][0]);
172+
$this->assertSame('authorization: Bearer foobar', $options['request_headers'][0]);
173173
}
174174

175175
/**

0 commit comments

Comments
 (0)