Refactor UrlParser (#7294)
I thought I was going to need this for #7293, but it seems like still a good change, removes a bit of duplication across getHost and getPort, and could be useful in the future if we want logic to grab both host and port in a "single pass"
This commit is contained in:
parent
01f313a136
commit
16272753cc
|
@ -12,41 +12,52 @@ class UrlParser {
|
||||||
@Nullable
|
@Nullable
|
||||||
static String getHost(String url) {
|
static String getHost(String url) {
|
||||||
|
|
||||||
int schemeEndIndex = url.indexOf(':');
|
int startIndex = getHostStartIndex(url);
|
||||||
if (schemeEndIndex == -1) {
|
if (startIndex == -1) {
|
||||||
// not a valid url
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
int len = url.length();
|
int endIndexExclusive = getHostEndIndexExclusive(url, startIndex);
|
||||||
if (len <= schemeEndIndex + 2
|
if (endIndexExclusive == startIndex) {
|
||||||
|| url.charAt(schemeEndIndex + 1) != '/'
|
|
||||||
|| url.charAt(schemeEndIndex + 2) != '/') {
|
|
||||||
// has no authority component
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// look for the end of the host:
|
return url.substring(startIndex, endIndexExclusive);
|
||||||
// ':' ==> start of port, or
|
|
||||||
// '/', '?', '#' ==> start of path
|
|
||||||
int index;
|
|
||||||
for (index = schemeEndIndex + 3; index < len; index++) {
|
|
||||||
char c = url.charAt(index);
|
|
||||||
if (c == ':' || c == '/' || c == '?' || c == '#') {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
String host = url.substring(schemeEndIndex + 3, index);
|
|
||||||
return host.isEmpty() ? null : host;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
static Integer getPort(String url) {
|
static Integer getPort(String url) {
|
||||||
|
|
||||||
|
int hostStartIndex = getHostStartIndex(url);
|
||||||
|
if (hostStartIndex == -1) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
int hostEndIndexExclusive = getHostEndIndexExclusive(url, hostStartIndex);
|
||||||
|
if (hostEndIndexExclusive == hostStartIndex) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (hostEndIndexExclusive < url.length() && url.charAt(hostEndIndexExclusive) != ':') {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
int portStartIndex = hostEndIndexExclusive + 1;
|
||||||
|
|
||||||
|
int portEndIndexExclusive = getPortEndIndexExclusive(url, portStartIndex);
|
||||||
|
if (portEndIndexExclusive == portStartIndex) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return safeParse(url.substring(portStartIndex, portEndIndexExclusive));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int getHostStartIndex(String url) {
|
||||||
|
|
||||||
int schemeEndIndex = url.indexOf(':');
|
int schemeEndIndex = url.indexOf(':');
|
||||||
if (schemeEndIndex == -1) {
|
if (schemeEndIndex == -1) {
|
||||||
// not a valid url
|
// not a valid url
|
||||||
return null;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int len = url.length();
|
int len = url.length();
|
||||||
|
@ -54,39 +65,39 @@ class UrlParser {
|
||||||
|| url.charAt(schemeEndIndex + 1) != '/'
|
|| url.charAt(schemeEndIndex + 1) != '/'
|
||||||
|| url.charAt(schemeEndIndex + 2) != '/') {
|
|| url.charAt(schemeEndIndex + 2) != '/') {
|
||||||
// has no authority component
|
// has no authority component
|
||||||
return null;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return schemeEndIndex + 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int getHostEndIndexExclusive(String url, int startIndex) {
|
||||||
// look for the end of the host:
|
// look for the end of the host:
|
||||||
// ':' ==> start of port, or
|
// ':' ==> start of port, or
|
||||||
// '/', '?', '#' ==> start of path
|
// '/', '?', '#' ==> start of path
|
||||||
int index;
|
int index;
|
||||||
int portIndex = -1;
|
int len = url.length();
|
||||||
for (index = schemeEndIndex + 3; index < len; index++) {
|
for (index = startIndex; index < len; index++) {
|
||||||
char c = url.charAt(index);
|
char c = url.charAt(index);
|
||||||
if (c == ':') {
|
if (c == ':' || c == '/' || c == '?' || c == '#') {
|
||||||
portIndex = index + 1;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (c == '/' || c == '?' || c == '#') {
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return index;
|
||||||
if (portIndex == -1) {
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static int getPortEndIndexExclusive(String url, int startIndex) {
|
||||||
// look for the end of the port:
|
// look for the end of the port:
|
||||||
// '/', '?', '#' ==> start of path
|
// '/', '?', '#' ==> start of path
|
||||||
for (index = portIndex; index < len; index++) {
|
int index;
|
||||||
|
int len = url.length();
|
||||||
|
for (index = startIndex; index < len; index++) {
|
||||||
char c = url.charAt(index);
|
char c = url.charAt(index);
|
||||||
if (c == '/' || c == '?' || c == '#') {
|
if (c == '/' || c == '?' || c == '#') {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
String port = url.substring(portIndex, index);
|
return index;
|
||||||
return port.isEmpty() ? null : safeParse(port);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
|
|
Loading…
Reference in New Issue