11
11
import jakarta .annotation .Nullable ;
12
12
import java .io .IOException ;
13
13
import java .net .URI ;
14
+ import org .apache .hc .client5 .http .CircularRedirectException ;
14
15
import org .apache .hc .client5 .http .ClientProtocolException ;
15
16
import org .apache .hc .client5 .http .classic .ExecChain ;
16
17
import org .apache .hc .client5 .http .classic .ExecChain .Scope ;
@@ -105,7 +106,8 @@ private boolean pendingRedirect(
105
106
HttpClientContext httpContext ,
106
107
HttpRequest request ,
107
108
ApacheHttpClient5Request instrumenterRequest ,
108
- @ Nullable ClassicHttpResponse response ) {
109
+ @ Nullable ClassicHttpResponse response )
110
+ throws HttpException {
109
111
if (response == null ) {
110
112
return false ;
111
113
}
@@ -130,17 +132,18 @@ private boolean pendingRedirect(
130
132
// a circular redirect, which happens before exec decorators run.
131
133
RedirectLocations redirectLocations =
132
134
(RedirectLocations ) httpContext .getAttribute (HttpClientContext .REDIRECT_LOCATIONS );
133
- if (redirectLocations != null ) {
135
+ if (! redirectLocations . getAll (). isEmpty () ) {
134
136
RedirectLocations copy = new RedirectLocations ();
135
137
for (URI uri : redirectLocations .getAll ()) {
136
138
copy .add (uri );
137
139
}
138
140
139
141
try {
140
- DefaultRedirectStrategy . INSTANCE . getLocationURI (request , response , httpContext );
141
- } catch (HttpException e ) {
142
+ getLocationUri (request , response , httpContext , copy );
143
+ } catch (ProtocolException e ) {
142
144
// We will not be returning to the Exec, finish the span.
143
- instrumenter .end (context , instrumenterRequest , response , new ClientProtocolException (e ));
145
+ instrumenter .end (
146
+ context , instrumenterRequest , response , new ClientProtocolException (e .getMessage (), e ));
144
147
return true ;
145
148
} finally {
146
149
httpContext .setAttribute (HttpClientContext .REDIRECT_LOCATIONS , copy );
@@ -156,6 +159,21 @@ private boolean pendingRedirect(
156
159
return true ;
157
160
}
158
161
162
+ private static URI getLocationUri (
163
+ HttpRequest request ,
164
+ HttpResponse response ,
165
+ HttpClientContext httpContext ,
166
+ RedirectLocations redirectLocations )
167
+ throws HttpException {
168
+ URI redirectUri =
169
+ DefaultRedirectStrategy .INSTANCE .getLocationURI (request , response , httpContext );
170
+ if (!httpContext .getRequestConfig ().isCircularRedirectsAllowed ()
171
+ && redirectLocations .contains (redirectUri )) {
172
+ throw new CircularRedirectException ("Circular redirect to '" + redirectUri + "'" );
173
+ }
174
+ return DefaultRedirectStrategy .INSTANCE .getLocationURI (request , response , httpContext );
175
+ }
176
+
159
177
private static ApacheHttpClient5Request getApacheHttpClient5Request (
160
178
ClassicHttpRequest request , Scope scope ) {
161
179
HttpHost host = null ;
0 commit comments