Skip to content

Add IPv6 support in RestTemplate #358

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

Merged
merged 1 commit into from
Sep 25, 2013

Conversation

bclozel
Copy link
Member

@bclozel bclozel commented Sep 20, 2013

Prior to this commit, RestTemplate would not would
not accept IPv6 raw addresses in URLs because UriComponentsBuilder
would not parse/encode the Host part correctly.

The UriComponentsBuilder now parses and encode raw IPv6 addresses
in the "[1abc:2abc:3abc::5ABC:6abc]" format and also supports the
use of IPv6 scope_ids (see JDK8 java.net.Inet6Address),
like "[1abc:2abc:3abc::5ABC:6abc%eth0]".

Issue: SPR-10539

@ghost ghost assigned rstoyanchev Sep 20, 2013
@rstoyanchev
Copy link
Contributor

This looks very good overall. A couple of things I noticed while experimenting.

If an invalid IPv6 format is used (e.g. omit the closing ] or use non-alphanumeric char) the result is a UriComponents where the host is tacked on to the path, so it looks like it got parsed but the path is invalid. Since the IPv6 format is quite specific we should do something to guard against such cases. Maybe check if there is a scheme but no host, or if the path starts with [ could be another giveaway?

HierarchicalUriComponents.Type.HOST now allows [ ] : for any host value. Since HierarchicalUriComponents can only be created through UriComponentsBuilder, in theory a regular host should never contain [ : but it could end up containing ] (e.g. this is now allowed http://example.com] even if highly unlikely). Perhaps what we need is an additional HOST_IPV6 enum that would be used instead when the host is an IPv6 address?

@bclozel
Copy link
Member Author

bclozel commented Sep 23, 2013

Good catch. I'll modify my PR and add more tests on edge cases.
There are also IPv6 compatible IPv4 addresses like ::192.168.1.1.

@bclozel
Copy link
Member Author

bclozel commented Sep 24, 2013

I've changed a couple of things in this PR.

I've splitted the host regexp in 2; the match process is getting more complex and I wonder if we should use named groups to make it more readable/manageable.

On the first point, I've added a "scheme but no host" check in the fromHttpUrl method, because some URIs (emails) do verify that condition but are still valid.

I agree, IPv4 and IPv6 are different enough to create a separate HOST_IPV6 type.

I'm not really happy with the condition check to decide on wich HierarchicalUriComponents.Type we should use (checking if the host part starts with [); IMHO, the only alternative would be to use named groups in the regexp and some kind of flag saying if the host is a IPv6 address or not.

Prior to this commit, RestTemplate would not would
not accept IPv6 raw addresses in URLs because UriComponentsBuilder
would not parse/encode the Host part correctly.

The UriComponentsBuilder now parses and encode raw IPv6 addresses
in the "[1abc:2abc:3abc::5ABC:6abc]" format and also supports the
use of IPv6 scope_ids (see JDK8 java.net.Inet6Address),
like "[1abc:2abc:3abc::5ABC:6abc%eth0]".

Issue: SPR-10539
rstoyanchev added a commit that referenced this pull request Sep 25, 2013
* bclozel-SPR-10539:
  Polish
  Add IPv6 support in RestTemplate
@rstoyanchev rstoyanchev merged commit 2dd4480 into spring-projects:master Sep 25, 2013
@rstoyanchev
Copy link
Contributor

This is now merged.

I'm not really happy with the condition check to decide on wich HierarchicalUriComponents.Type we should use (checking if the host part starts with [); IMHO, the only alternative would be to use named groups in the regexp and some kind of flag saying if the host is a IPv6 address or not.

There are few ways to make it a little nicer but since it is a private matter within HierarchicalUriComponents, the current solution is fine. I did a little polish, just to cover the call to verify(), but the approach is the same.

sbrannen added a commit to sbrannen/spring-framework that referenced this pull request Jun 29, 2020
PR spring-projectsgh-358 introduced a "scheme but no host" check in the fromHttpUrl()
method in UriComponentsBuilder, but a similar check was not added to
fromUriString() at that time.

This commit introduces a "scheme but no host" check in fromUriString()
to align with the functionality in fromHttpUrl().

Note, however that the regular expressions used to match against the
hostname or IP address are inexact and still permit invalid host names
or IP addresses. True validation of the host portion of the URI is out
of scope for this commit.

Closes spring-projectsgh-25334
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants