Skip to content

Commit

Permalink
feature #33973 [HttpClient] add HttpClient::createForBaseUri() (nicol…
Browse files Browse the repository at this point in the history
…as-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()
  • Loading branch information
nicolas-grekas committed Oct 14, 2019
2 parents ff3e32c + 1aa9a11 commit 46e94d9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Symfony/Component/HttpClient/CHANGELOG.md
Expand Up @@ -4,10 +4,11 @@ CHANGELOG
4.4.0
-----

* added `StreamWrapper`
* added `HttpClient::createForBaseUri()`
* added `HttplugClient` with support for sync and async requests
* added `max_duration` option
* added support for NTLM authentication
* added `StreamWrapper` to cast any `ResponseInterface` instances to PHP streams.
* added `$response->toStream()` to cast responses to regular PHP streams
* made `Psr18Client` implement relevant PSR-17 factories and have streaming responses
* added `TraceableHttpClient`, `HttpClientDataCollector` and `HttpClientPass` to integrate with the web profiler
Expand Down
10 changes: 10 additions & 0 deletions src/Symfony/Component/HttpClient/HttpClient.php
Expand Up @@ -39,4 +39,14 @@ public static function create(array $defaultOptions = [], int $maxHostConnection

return new NativeHttpClient($defaultOptions, $maxHostConnections);
}

/**
* Creates a client that adds options (e.g. authentication headers) only when the request URL matches the provided base URI.
*/
public static function createForBaseUri(string $baseUri, array $defaultOptions = [], int $maxHostConnections = 6, int $maxPendingPushes = 50): HttpClientInterface
{
$client = self::create([], $maxHostConnections, $maxPendingPushes);

return ScopingHttpClient::forBaseUri($client, $baseUri, $defaultOptions);
}
}

0 comments on commit 46e94d9

Please sign in to comment.