@@ -483,15 +483,20 @@ 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
- final String authority = authority ();
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 );
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
+ }
494
497
}
498
+ } catch (IllegalStateException e ) {
499
+ // Just pass, because it's normal condition.
495
500
}
496
501
497
502
final HttpHeadersBuilder headersBuilder = internalRequestHeaders .toBuilder ();
@@ -750,7 +755,6 @@ public String fragment() {
750
755
return requestTarget ().fragment ();
751
756
}
752
757
753
- @ Nullable
754
758
@ Override
755
759
public String authority () {
756
760
final HttpHeaders additionalRequestHeaders = this .additionalRequestHeaders ;
@@ -774,6 +778,11 @@ public String authority() {
774
778
if (authority == null ) {
775
779
authority = internalRequestHeaders .get (HttpHeaderNames .HOST );
776
780
}
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
+ }
777
786
return authority ;
778
787
}
779
788
@@ -794,12 +803,12 @@ private String origin() {
794
803
return origin ;
795
804
}
796
805
797
- @ Nullable
798
806
@ Override
799
807
public String host () {
800
808
final String authority = authority ();
801
809
if (authority == null ) {
802
- return null ;
810
+ throw new IllegalStateException ("ClientRequestContext may be in the process of initialization." +
811
+ "In this case, host() or authority() could be null" );
803
812
}
804
813
return SchemeAndAuthority .of (null , authority ).host ();
805
814
}
0 commit comments