001/* 002 * Copyright (C) 2009 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 005 * in compliance with the License. You may obtain a copy of the License at 006 * 007 * http://www.apache.org/licenses/LICENSE-2.0 008 * 009 * Unless required by applicable law or agreed to in writing, software distributed under the License 010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 011 * or implied. See the License for the specific language governing permissions and limitations under 012 * the License. 013 */ 014 015package com.google.common.net; 016 017import com.google.common.annotations.GwtIncompatible; 018import com.google.common.annotations.J2ktIncompatible; 019import com.google.common.base.Preconditions; 020import com.google.errorprone.annotations.CanIgnoreReturnValue; 021import java.net.InetAddress; 022import java.text.ParseException; 023import org.jspecify.annotations.Nullable; 024 025/** 026 * A syntactically valid host specifier, suitable for use in a URI. This may be either a numeric IP 027 * address in IPv4 or IPv6 notation, or a domain name. 028 * 029 * <p>Because this class is intended to represent host specifiers which can reasonably be used in a 030 * URI, the domain name case is further restricted to include only those domain names which end in a 031 * recognized public suffix; see {@link InternetDomainName#isPublicSuffix()} for details. 032 * 033 * <p>Note that no network lookups are performed by any {@code HostSpecifier} methods. No attempt is 034 * made to verify that a provided specifier corresponds to a real or accessible host. Only syntactic 035 * and pattern-based checks are performed. 036 * 037 * <p>If you know that a given string represents a numeric IP address, use {@link InetAddresses} to 038 * obtain and manipulate a {@link java.net.InetAddress} instance from it rather than using this 039 * class. Similarly, if you know that a given string represents a domain name, use {@link 040 * InternetDomainName} rather than this class. 041 * 042 * @author Craig Berry 043 * @since 5.0 044 */ 045@J2ktIncompatible 046@GwtIncompatible 047public final class HostSpecifier { 048 049 private final String canonicalForm; 050 051 private HostSpecifier(String canonicalForm) { 052 this.canonicalForm = canonicalForm; 053 } 054 055 /** 056 * Returns a {@code HostSpecifier} built from the provided {@code specifier}, which is already 057 * known to be valid. If the {@code specifier} might be invalid, use {@link #from(String)} 058 * instead. 059 * 060 * <p>The specifier must be in one of these formats: 061 * 062 * <ul> 063 * <li>A domain name, like {@code google.com} 064 * <li>A IPv4 address string, like {@code 127.0.0.1} 065 * <li>An IPv6 address string with or without brackets, like {@code [2001:db8::1]} or {@code 066 * 2001:db8::1} 067 * </ul> 068 * 069 * @throws IllegalArgumentException if the specifier is not valid. 070 */ 071 public static HostSpecifier fromValid(String specifier) { 072 // Verify that no port was specified, and strip optional brackets from 073 // IPv6 literals. 074 HostAndPort parsedHost = HostAndPort.fromString(specifier); 075 Preconditions.checkArgument(!parsedHost.hasPort()); 076 String host = parsedHost.getHost(); 077 078 // Try to interpret the specifier as an IP address. Note we build 079 // the address rather than using the .is* methods because we want to 080 // use InetAddresses.toUriString to convert the result to a string in 081 // canonical form. 082 InetAddress addr = null; 083 try { 084 addr = InetAddresses.forString(host); 085 } catch (IllegalArgumentException e) { 086 // It is not an IPv4 or IPv6 literal 087 } 088 089 if (addr != null) { 090 return new HostSpecifier(InetAddresses.toUriString(addr)); 091 } 092 093 // It is not any kind of IP address; must be a domain name or invalid. 094 095 // TODO(user): different versions of this for different factories? 096 InternetDomainName domain = InternetDomainName.from(host); 097 098 if (domain.hasPublicSuffix()) { 099 return new HostSpecifier(domain.toString()); 100 } 101 102 throw new IllegalArgumentException( 103 "Domain name does not have a recognized public suffix: " + host); 104 } 105 106 /** 107 * Attempts to return a {@code HostSpecifier} for the given string, throwing an exception if 108 * parsing fails. Always use this method in preference to {@link #fromValid(String)} for a 109 * specifier that is not already known to be valid. 110 * 111 * @throws ParseException if the specifier is not valid. 112 */ 113 @CanIgnoreReturnValue // TODO(b/219820829): consider removing 114 public static HostSpecifier from(String specifier) throws ParseException { 115 try { 116 return fromValid(specifier); 117 } catch (IllegalArgumentException e) { 118 // Since the IAE can originate at several different points inside 119 // fromValid(), we implement this method in terms of that one rather 120 // than the reverse. 121 122 ParseException parseException = new ParseException("Invalid host specifier: " + specifier, 0); 123 parseException.initCause(e); 124 throw parseException; 125 } 126 } 127 128 /** 129 * Determines whether {@code specifier} represents a valid {@link HostSpecifier} as described in 130 * the documentation for {@link #fromValid(String)}. 131 */ 132 public static boolean isValid(String specifier) { 133 try { 134 HostSpecifier unused = fromValid(specifier); 135 return true; 136 } catch (IllegalArgumentException e) { 137 return false; 138 } 139 } 140 141 @Override 142 public boolean equals(@Nullable Object other) { 143 if (this == other) { 144 return true; 145 } 146 147 if (other instanceof HostSpecifier) { 148 HostSpecifier that = (HostSpecifier) other; 149 return this.canonicalForm.equals(that.canonicalForm); 150 } 151 152 return false; 153 } 154 155 @Override 156 public int hashCode() { 157 return canonicalForm.hashCode(); 158 } 159 160 /** 161 * Returns a string representation of the host specifier suitable for inclusion in a URI. If the 162 * host specifier is a domain name, the string will be normalized to all lower case. If the 163 * specifier was an IPv6 address without brackets, brackets are added so that the result will be 164 * usable in the host part of a URI. 165 */ 166 @Override 167 public String toString() { 168 return canonicalForm; 169 } 170}