Skip to content

Commit b2eae62

Browse files
ClientRequestContext thorws a exception when host, authoriy is null.
1 parent c5b3f19 commit b2eae62

File tree

4 files changed

+48
-17
lines changed

4 files changed

+48
-17
lines changed

core/src/main/java/com/linecorp/armeria/client/ClientRequestContext.java

-4
Original file line numberDiff line numberDiff line change
@@ -290,15 +290,11 @@ ClientRequestContext newDerivedContext(RequestId id, @Nullable HttpRequest req,
290290
* <li>{@link Endpoint#authority()}.</li>
291291
* </ol>
292292
*/
293-
@Nullable
294-
@UnstableApi
295293
String authority();
296294

297295
/**
298296
* Returns the host part of {@link #authority()}, without a port number.
299297
*/
300-
@Nullable
301-
@UnstableApi
302298
String host();
303299

304300
/**

core/src/main/java/com/linecorp/armeria/client/ClientRequestContextWrapper.java

-2
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,11 @@ public String fragment() {
7070
return unwrap().fragment();
7171
}
7272

73-
@Nullable
7473
@Override
7574
public String authority() {
7675
return unwrap().authority();
7776
}
7877

79-
@Nullable
8078
@Override
8179
public String host() {
8280
return unwrap().host();

core/src/main/java/com/linecorp/armeria/internal/client/DefaultClientRequestContext.java

+20-11
Original file line numberDiff line numberDiff line change
@@ -483,15 +483,20 @@ private void failEarly(Throwable cause) {
483483

484484
// TODO(ikhoon): Consider moving the logic for filling authority to `HttpClientDelegate.exceute()`.
485485
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+
}
494497
}
498+
} catch (IllegalStateException e) {
499+
// Just pass, because it's normal condition.
495500
}
496501

497502
final HttpHeadersBuilder headersBuilder = internalRequestHeaders.toBuilder();
@@ -750,7 +755,6 @@ public String fragment() {
750755
return requestTarget().fragment();
751756
}
752757

753-
@Nullable
754758
@Override
755759
public String authority() {
756760
final HttpHeaders additionalRequestHeaders = this.additionalRequestHeaders;
@@ -774,6 +778,11 @@ public String authority() {
774778
if (authority == null) {
775779
authority = internalRequestHeaders.get(HttpHeaderNames.HOST);
776780
}
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+
}
777786
return authority;
778787
}
779788

@@ -794,12 +803,12 @@ private String origin() {
794803
return origin;
795804
}
796805

797-
@Nullable
798806
@Override
799807
public String host() {
800808
final String authority = authority();
801809
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");
803812
}
804813
return SchemeAndAuthority.of(null, authority).host();
805814
}

core/src/test/java/com/linecorp/armeria/client/ClientRequestContextTest.java

+28
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import static org.assertj.core.api.Assertions.assertThat;
1919
import static org.assertj.core.api.Assertions.assertThatThrownBy;
2020

21+
import java.util.concurrent.CompletableFuture;
22+
import java.util.concurrent.CountDownLatch;
2123
import java.util.function.Function;
2224
import java.util.stream.Stream;
2325

@@ -29,6 +31,7 @@
2931
import org.junit.jupiter.params.provider.ArgumentsSource;
3032
import org.junit.jupiter.params.provider.ValueSource;
3133

34+
import com.linecorp.armeria.common.AggregatedHttpResponse;
3235
import com.linecorp.armeria.common.HttpMethod;
3336
import com.linecorp.armeria.common.HttpRequest;
3437
import com.linecorp.armeria.common.RequestContext;
@@ -269,6 +272,31 @@ void hasOwnAttr() {
269272
}
270273
}
271274

275+
@Test
276+
void callAuthorityShouldBeThrownDuringPartiallyInit() {
277+
final CountDownLatch countDownLatch = new CountDownLatch(1);
278+
final WebClient client = WebClient
279+
.builder("http://localhost:8080")
280+
.contextCustomizer(ctx -> {
281+
countDownLatch.countDown();
282+
assertThatThrownBy(ctx::host)
283+
.isInstanceOf(IllegalStateException.class)
284+
.hasMessage(
285+
"ClientRequestContext may be in the process of initialization." +
286+
"In this case, host() or authority() could be null");
287+
assertThatThrownBy(ctx::authority)
288+
.isInstanceOf(IllegalStateException.class)
289+
.hasMessage(
290+
"ClientRequestContext may be in the process of initialization." +
291+
"In this case, host() or authority() could be null"
292+
);
293+
}).build();
294+
295+
final CompletableFuture<AggregatedHttpResponse> aggregate = client.get("/").aggregate();
296+
assertThat(countDownLatch.getCount()).isEqualTo(0);
297+
aggregate.cancel(true);
298+
}
299+
272300
@ParameterizedTest
273301
@ValueSource(strings = {"%", "http:///", "http://foo.com/bar"})
274302
void updateRequestWithInvalidPath(String path) {

0 commit comments

Comments
 (0)