in servicetalk-http-utils/src/main/java/io/servicetalk/http/utils/HttpRequestUriUtils.java [180:251]
private static String buildEffectiveRequestUri(@Nullable final ConnectionContext ctx,
final HttpRequestMetaData metaData,
@Nullable final String fixedScheme,
@Nullable final String fixedAuthority,
@Nullable String path,
@Nullable String query,
final boolean includeUserInfo,
final boolean checkAuthorityOrAsteriskForm) {
if (fixedScheme != null && !"http".equalsIgnoreCase(fixedScheme) && !"https".equalsIgnoreCase(fixedScheme)) {
throw new IllegalArgumentException("Unsupported scheme: " + fixedScheme);
}
if (ctx == null && (fixedScheme == null || fixedAuthority == null)) {
throw new IllegalArgumentException("Context required without scheme and authority");
}
// https://tools.ietf.org/html/rfc7230#section-5.5
// If the request-target is in authority-form or asterisk-form, the effective request URI's combined
// path and query component is empty.
if (checkAuthorityOrAsteriskForm && (CONNECT.equals(metaData.method()) || OPTIONS.equals(metaData.method()))) {
path = query = null;
}
final String metadataScheme = metaData.scheme();
if (metadataScheme != null) {
// absolute form
return buildRequestUri(
metadataScheme,
includeUserInfo ? metaData.userInfo() : null,
metaData.host(),
metaData.port(),
path,
query);
}
final String scheme = fixedScheme != null ? fixedScheme.toLowerCase() :
(ctx.sslSession() != null ? "https" : "http");
final HostAndPort effectiveHostAndPort;
if (fixedAuthority != null) {
return buildRequestUri(
scheme,
fixedAuthority,
path,
query);
} else if ((effectiveHostAndPort = metaData.effectiveHostAndPort()) != null) {
return buildRequestUri(
scheme,
includeUserInfo ? metaData.userInfo() : null,
effectiveHostAndPort.hostName(),
effectiveHostAndPort.port(),
path,
query);
} else {
if (!(ctx.localAddress() instanceof InetSocketAddress)) {
throw new IllegalArgumentException("ConnectionContext#getLocalAddress is not an InetSocketAddress: "
+ ctx.localAddress());
}
final InetSocketAddress localAddress = (InetSocketAddress) ctx.localAddress();
final boolean defaultPort = ("http".equals(scheme) && localAddress.getPort() == 80)
|| ("https".equals(scheme) && localAddress.getPort() == 443);
return buildRequestUri(
scheme,
null,
localAddress.getHostName(),
defaultPort ? -1 : localAddress.getPort(),
path,
query);
}
}