@@ -70,9 +70,8 @@ class JdkClientHttpRequest extends AbstractStreamingClientHttpRequest {
70
70
71
71
private final @ Nullable Duration timeout ;
72
72
73
-
74
73
public JdkClientHttpRequest (HttpClient httpClient , URI uri , HttpMethod method , Executor executor ,
75
- @ Nullable Duration readTimeout ) {
74
+ @ Nullable Duration readTimeout ) {
76
75
77
76
this .httpClient = httpClient ;
78
77
this .uri = uri ;
@@ -81,7 +80,6 @@ public JdkClientHttpRequest(HttpClient httpClient, URI uri, HttpMethod method, E
81
80
this .timeout = readTimeout ;
82
81
}
83
82
84
-
85
83
@ Override
86
84
public HttpMethod getMethod () {
87
85
return this .method ;
@@ -92,16 +90,15 @@ public URI getURI() {
92
90
return this .uri ;
93
91
}
94
92
95
-
96
93
@ Override
97
94
protected ClientHttpResponse executeInternal (HttpHeaders headers , @ Nullable Body body ) throws IOException {
98
95
CompletableFuture <HttpResponse <InputStream >> responseFuture = null ;
96
+ TimeoutHandler timeoutHandler = null ;
99
97
try {
100
98
HttpRequest request = buildRequest (headers , body );
101
99
responseFuture = this .httpClient .sendAsync (request , HttpResponse .BodyHandlers .ofInputStream ());
102
-
103
100
if (this .timeout != null ) {
104
- TimeoutHandler timeoutHandler = new TimeoutHandler (responseFuture , this .timeout );
101
+ timeoutHandler = new TimeoutHandler (responseFuture , this .timeout );
105
102
HttpResponse <InputStream > response = responseFuture .get ();
106
103
InputStream inputStream = timeoutHandler .wrapInputStream (response );
107
104
return new JdkClientHttpResponse (response , inputStream );
@@ -136,6 +133,13 @@ else if (cause instanceof IOException ioEx) {
136
133
throw (message == null ? new IOException (cause ) : new IOException (message , cause ));
137
134
}
138
135
}
136
+ catch (CancellationException ex ) {
137
+ if (timeoutHandler != null && timeoutHandler .isTimeout ){
138
+ throw new HttpTimeoutException ("Request timed out" );
139
+ }
140
+ Throwable cause = ex .getCause ();
141
+ throw new IOException (cause .getMessage (), cause );
142
+ }
139
143
}
140
144
141
145
private HttpRequest buildRequest (HttpHeaders headers , @ Nullable Body body ) {
@@ -224,6 +228,7 @@ public ByteBuffer map(byte[] b, int off, int len) {
224
228
private static final class TimeoutHandler {
225
229
226
230
private final CompletableFuture <Void > timeoutFuture ;
231
+ boolean isTimeout =false ;
227
232
228
233
private TimeoutHandler (CompletableFuture <HttpResponse <InputStream >> future , Duration timeout ) {
229
234
@@ -232,6 +237,7 @@ private TimeoutHandler(CompletableFuture<HttpResponse<InputStream>> future, Dura
232
237
233
238
this .timeoutFuture .thenRun (() -> {
234
239
if (future .cancel (true ) || future .isCompletedExceptionally () || !future .isDone ()) {
240
+ this .isTimeout = true ;
235
241
return ;
236
242
}
237
243
try {
0 commit comments