@@ -483,20 +483,15 @@ private void failEarly(Throwable cause) {
483
483
484
484
// TODO(ikhoon): Consider moving the logic for filling authority to `HttpClientDelegate.exceute()`.
485
485
private void autoFillSchemeAuthorityAndOrigin () {
486
-
487
- try {
488
- final String authority = authority ();
489
- if (endpoint != null && endpoint .isIpAddrOnly ()) {
490
- // The connection will be established with the IP address but `host` set to the `Endpoint`
491
- // could be used for SNI. It would make users send HTTPS requests
492
- // with CSLB or configure a reverse proxy based on an authority.
493
- final String host = SchemeAndAuthority .of (null , authority ).host ();
494
- if (!NetUtil .isValidIpV4Address (host ) && !NetUtil .isValidIpV6Address (host )) {
495
- endpoint = endpoint .withHost (host );
496
- }
486
+ final String authority = authorityOrNull ();
487
+ if (authority != null && endpoint != null && endpoint .isIpAddrOnly ()) {
488
+ // The connection will be established with the IP address but `host` set to the `Endpoint`
489
+ // could be used for SNI. It would make users send HTTPS requests with CSLB or configure a reverse
490
+ // proxy based on an authority.
491
+ final String host = SchemeAndAuthority .of (null , authority ).host ();
492
+ if (!NetUtil .isValidIpV4Address (host ) && !NetUtil .isValidIpV6Address (host )) {
493
+ endpoint = endpoint .withHost (host );
497
494
}
498
- } catch (IllegalStateException e ) {
499
- // Just pass, because it's normal condition.
500
495
}
501
496
502
497
final HttpHeadersBuilder headersBuilder = internalRequestHeaders .toBuilder ();
@@ -757,6 +752,17 @@ public String fragment() {
757
752
758
753
@ Override
759
754
public String authority () {
755
+ final String authority = authorityOrNull ();
756
+ if (authority == null ) {
757
+ throw new IllegalStateException (
758
+ "ClientRequestContext may be in the process of initialization." +
759
+ "In this case, host() or authority() could be null" );
760
+ }
761
+ return authority ;
762
+ }
763
+
764
+ @ Nullable
765
+ private String authorityOrNull () {
760
766
final HttpHeaders additionalRequestHeaders = this .additionalRequestHeaders ;
761
767
String authority = additionalRequestHeaders .get (HttpHeaderNames .AUTHORITY );
762
768
if (authority == null ) {
@@ -778,11 +784,7 @@ public String authority() {
778
784
if (authority == null ) {
779
785
authority = internalRequestHeaders .get (HttpHeaderNames .HOST );
780
786
}
781
- if (authority == null ) {
782
- throw new IllegalStateException (
783
- "ClientRequestContext may be in the process of initialization." +
784
- "In this case, host() or authority() could be null" );
785
- }
787
+
786
788
return authority ;
787
789
}
788
790
@@ -812,16 +814,16 @@ public String host() {
812
814
@ Override
813
815
public URI uri () {
814
816
final String scheme = getScheme (sessionProtocol ());
817
+ final String authority = authorityOrNull ();
815
818
final String path = path ();
816
819
final String query = query ();
817
820
final String fragment = fragment ();
818
821
try (TemporaryThreadLocals tmp = TemporaryThreadLocals .acquire ()) {
819
822
final StringBuilder buf = tmp .stringBuilder ();
820
823
buf .append (scheme );
821
- try {
822
- final String authority = authority ();
824
+ if (authority != null ) {
823
825
buf .append ("://" ).append (authority );
824
- } catch ( IllegalStateException e ) {
826
+ } else {
825
827
buf .append (':' );
826
828
}
827
829
buf .append (path );
0 commit comments