Skip to content

Commit 46e94d9

Browse files
feature #33973 [HttpClient] add HttpClient::createForBaseUri() (nicolas-grekas)
This PR was merged into the 4.4 branch. Discussion ---------- [HttpClient] add HttpClient::createForBaseUri() | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - I've seen ppl use `HttpClient::create()` with default `base_uri` & `auth_bearer`. That's a security risk as the bearer would be sent to any hosts that the client requests. Instead, ppl should use `ScopingHttpClient`. The new method should help to discover and use it. Commits ------- 1aa9a11 [HttpClient] add HttpClient::createForBaseUri()
2 parents ff3e32c + 1aa9a11 commit 46e94d9

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/Symfony/Component/HttpClient/CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ CHANGELOG
44
4.4.0
55
-----
66

7-
* added `StreamWrapper`
7+
* added `HttpClient::createForBaseUri()`
88
* added `HttplugClient` with support for sync and async requests
99
* added `max_duration` option
1010
* added support for NTLM authentication
11+
* added `StreamWrapper` to cast any `ResponseInterface` instances to PHP streams.
1112
* added `$response->toStream()` to cast responses to regular PHP streams
1213
* made `Psr18Client` implement relevant PSR-17 factories and have streaming responses
1314
* added `TraceableHttpClient`, `HttpClientDataCollector` and `HttpClientPass` to integrate with the web profiler

src/Symfony/Component/HttpClient/HttpClient.php

+10
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,14 @@ public static function create(array $defaultOptions = [], int $maxHostConnection
3939

4040
return new NativeHttpClient($defaultOptions, $maxHostConnections);
4141
}
42+
43+
/**
44+
* Creates a client that adds options (e.g. authentication headers) only when the request URL matches the provided base URI.
45+
*/
46+
public static function createForBaseUri(string $baseUri, array $defaultOptions = [], int $maxHostConnections = 6, int $maxPendingPushes = 50): HttpClientInterface
47+
{
48+
$client = self::create([], $maxHostConnections, $maxPendingPushes);
49+
50+
return ScopingHttpClient::forBaseUri($client, $baseUri, $defaultOptions);
51+
}
4252
}

0 commit comments

Comments
 (0)