8000 [HttpClient] URL with colon is not correctly parsed · Issue #54332 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[HttpClient] URL with colon is not correctly parsed #54332

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
garak opened this issue Mar 19, 2024 · 5 comments
Closed

[HttpClient] URL with colon is not correctly parsed #54332

garak opened this issue Mar 19, 2024 · 5 comments

Comments

@garak
Copy link
Contributor
garak commented Mar 19, 2024

Symfony version(s) affected

6.4 and 7.0

Description

I'm using Google API for address validation. As referenced in their documentation, the URL to call is /v1:validateAddress

The problem is that HTTP client is trying to parse the URL without including the domain, so the part before the colon is interpreted as a schema, resulting in a "Unsupported scheme" HTTPException.

How to reproduce

Define an HTTP client like this:

framework:
    http_client:
        scoped_clients:
            map_address.client:
                base_uri: 'https://addressvalidation.googleapis.com/'
                headers:
                    content-type: 'application/json'
                    accept: 'application/json'

then inject your client as HttpClientInterface $mapAddressClient and try to use it:

$response = $this->mapAddressClient->request('POST', 'v1:validateAddress?key='.$myKey, [/* some data */]);

You'll get a Symfony\Component\HttpKernel\Exception\ HttpException Unsupported scheme in "v1:validateAddress?key=<redacted>

Possible Solution

I guess that the parsing of the URL done in

private static function parseUrl(string $url, array $query = [], array $allowedSchemes = ['http' => 80, 'https' => 443]): array
should be always done using the full URL, including the schema and the domain, so basically including the configured "base_uri" option.

Additional Context

No response

@MatTheCat
Copy link
Contributor

: is a colon; , is a comma 😁

According to the RFC 3986 this is the correct implementation (your case is the first “normal example”).

Prefix your path with / to prevent it to be parsed as a scheme.

@garak garak changed the title [HttpClient] URL with comma is not correctly parsed [HttpClient] URL with colon is not correctly parsed Mar 20, 2024
@garak
Copy link
Contributor Author
garak commented Mar 20, 2024

The proposed solution works, so I close.
I edited the title/content of the issue, so someone looking for the same problem will be able to find it.
Thanks @MatTheCat

@garak garak closed this as completed Mar 20, 2024
@xabbuh xabbuh closed this as not planned Won't fix, can't repro, duplicate, stale Mar 20, 2024
@atomiix
Copy link
atomiix commented Sep 9, 2024

Hi!
I’ve been facing the same issue but adding only a / (/test:1234) didn’t solve the issue, I got:

[MyApi.php] on line 35:
[Symfony\Component\HttpClient\Exception\InvalidArgumentException] {#830 ▼
  #message: "Malformed URL "/test:124"."
  #code: 0

However, when I add a ? at the end of the url in addition to the / at the beginning (/test:1234?), it works!

I think it should not behave differently depending on the presence or not of a ?.

@nicolas-grekas
Copy link
Member

This is due to php/php-src#12703
We might need to work around...

@nicolas-grekas
8000 Copy link
Member

Support for /foo:123 should be fixed by #58218

nicolas-grekas added a commit that referenced this issue Sep 11, 2024
This PR was merged into the 5.4 branch.

Discussion
----------

Work around `parse_url()` bug

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Issues        | Fix #52579, #54332
| License       | MIT

Works around
* php/php-src#12703

Commits
-------

fb76e53 Work around parse_url() bug
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants
0