Skip to content

Commit fcc2fcc

Browse files
[MRESOLVER-703] Expose redirect config for http transport
1 parent 2830727 commit fcc2fcc

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

maven-resolver-api/src/main/java/org/eclipse/aether/ConfigurationProperties.java

+28
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,34 @@ public final class ConfigurationProperties {
306306
*/
307307
public static final boolean DEFAULT_PERSISTED_CHECKSUMS = true;
308308

309+
/**
310+
* If enabled, the HTTP transport will follow HTTP redirects.
311+
*
312+
* @since 1.9.23
313+
*/
314+
public static final String HTTP_FOLLOW_REDIRECTS = PREFIX_CONNECTOR + "http.followRedirects";
315+
316+
/**
317+
* The default HTTP redirect mode if {@link #HTTP_FOLLOW_REDIRECTS} isn't set.
318+
*
319+
* @since 1.9.23
320+
*/
321+
public static final boolean DEFAULT_FOLLOW_REDIRECTS = true;
322+
323+
/**
324+
* The max redirect count to follow for the HTTP transport.
325+
*
326+
* @since 1.9.23
327+
*/
328+
public static final String HTTP_MAX_REDIRECTS = PREFIX_CONNECTOR + "http.maxRedirects";
329+
330+
/**
331+
* The default max redirect count to follow for the HTTP transport.
332+
*
333+
* @since 1.9.23
334+
*/
335+
public static final int DEFAULT_HTTP_MAX_REDIRECTS = 5;
336+
309337
private ConfigurationProperties() {
310338
// hide constructor
311339
}

maven-resolver-transport-http/src/main/java/org/eclipse/aether/transport/http/HttpTransporter.java

+17-4
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
import org.apache.http.impl.client.CloseableHttpClient;
8080
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
8181
import org.apache.http.impl.client.HttpClientBuilder;
82+
import org.apache.http.impl.client.LaxRedirectStrategy;
8283
import org.apache.http.impl.client.StandardHttpRequestRetryHandler;
8384
import org.apache.http.protocol.HttpContext;
8485
import org.apache.http.util.EntityUtils;
@@ -263,6 +264,16 @@ final class HttpTransporter extends AbstractTransporter {
263264
HTTP_RETRY_HANDLER_REQUEST_SENT_ENABLED);
264265
String userAgent = ConfigUtils.getString(
265266
session, ConfigurationProperties.DEFAULT_USER_AGENT, ConfigurationProperties.USER_AGENT);
267+
int maxRedirects = ConfigUtils.getInteger(
268+
session,
269+
ConfigurationProperties.DEFAULT_HTTP_MAX_REDIRECTS,
270+
ConfigurationProperties.HTTP_MAX_REDIRECTS + "." + repository.getId(),
271+
ConfigurationProperties.HTTP_MAX_REDIRECTS);
272+
boolean followRedirects = ConfigUtils.getBoolean(
273+
session,
274+
ConfigurationProperties.DEFAULT_FOLLOW_REDIRECTS,
275+
ConfigurationProperties.HTTP_FOLLOW_REDIRECTS + "." + repository.getId(),
276+
ConfigurationProperties.HTTP_FOLLOW_REDIRECTS);
266277

267278
Charset credentialsCharset = Charset.forName(credentialEncoding);
268279
Registry<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider>create()
@@ -275,6 +286,8 @@ final class HttpTransporter extends AbstractTransporter {
275286
SocketConfig socketConfig =
276287
SocketConfig.custom().setSoTimeout(requestTimeout).build();
277288
RequestConfig requestConfig = RequestConfig.custom()
289+
.setMaxRedirects(maxRedirects)
290+
.setRedirectsEnabled(followRedirects)
278291
.setConnectTimeout(connectTimeout)
279292
.setConnectionRequestTimeout(connectTimeout)
280293
.setLocalAddress(getBindAddress(session, repository))
@@ -306,6 +319,7 @@ final class HttpTransporter extends AbstractTransporter {
306319

307320
HttpClientBuilder builder = HttpClientBuilder.create()
308321
.setUserAgent(userAgent)
322+
.setRedirectStrategy(LaxRedirectStrategy.INSTANCE)
309323
.setDefaultSocketConfig(socketConfig)
310324
.setDefaultRequestConfig(requestConfig)
311325
.setServiceUnavailableRetryStrategy(serviceUnavailableRetryStrategy)
@@ -601,7 +615,7 @@ private <T extends HttpUriRequest> T commonHeaders(T request) {
601615
}
602616

603617
@SuppressWarnings("checkstyle:magicnumber")
604-
private <T extends HttpUriRequest> T resume(T request, GetTask task) {
618+
private <T extends HttpUriRequest> void resume(T request, GetTask task) {
605619
long resumeOffset = task.getResumeOffset();
606620
if (resumeOffset > 0L && task.getDataFile() != null) {
607621
request.setHeader(HttpHeaders.RANGE, "bytes=" + resumeOffset + '-');
@@ -610,7 +624,6 @@ private <T extends HttpUriRequest> T resume(T request, GetTask task) {
610624
DateUtils.formatDate(new Date(task.getDataFile().lastModified() - 60L * 1000L)));
611625
request.setHeader(HttpHeaders.ACCEPT_ENCODING, "identity");
612626
}
613-
return request;
614627
}
615628

616629
@SuppressWarnings("checkstyle:magicnumber")
@@ -787,7 +800,7 @@ public boolean retryRequest(HttpResponse response, int executionCount, HttpConte
787800
&& (serviceUnavailableHttpCodes.contains(
788801
response.getStatusLine().getStatusCode()));
789802
if (retry) {
790-
Long retryInterval = retryInterval(response, executionCount, context);
803+
Long retryInterval = retryInterval(response, executionCount);
791804
if (retryInterval != null) {
792805
RETRY_INTERVAL_HOLDER.set(retryInterval);
793806
return true;
@@ -804,7 +817,7 @@ public boolean retryRequest(HttpResponse response, int executionCount, HttpConte
804817
*
805818
* @return Long representing the retry interval as millis, or {@code null} if the request should be failed.
806819
*/
807-
private Long retryInterval(HttpResponse httpResponse, int executionCount, HttpContext httpContext) {
820+
private Long retryInterval(HttpResponse httpResponse, int executionCount) {
808821
Long result = null;
809822
Header header = httpResponse.getFirstHeader(HttpHeaders.RETRY_AFTER);
810823
if (header != null && header.getValue() != null) {

src/site/markdown/configuration.md

+2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ Option | Type | Description | Default Value | Supports Repo ID Suffix
5252
`aether.connector.http.reuseConnections` | boolean | Should HTTP client reuse connections (in other words, pool connections) or not? | `true` | yes
5353
`aether.connector.http.supportWebDav` | boolean | If enabled, transport makes best effort to deploy to WebDAV server. This mode is not recommended, better use real Maven Repository Manager instead. | `false` | yes
5454
`aether.connector.http.useSystemProperties` | boolean | If enabled, underlying Apache HttpClient will use system properties as well to configure itself (typically used to set up HTTP Proxy via Java system properties). See <a href="https://hc.apache.org/httpcomponents-client-4.5.x/current/httpclient/apidocs/org/apache/http/impl/client/HttpClientBuilder.html">HttpClientBuilder</a> for used properties. This mode is **not recommended**, better use documented ways of configuration instead. | `false` | yes
55+
`aether.connector.http.followRedirects` | boolean | If enabled, the HTTP transport will follow HTTP redirects | `true` | yes
56+
`aether.connector.http.maxRedirects` | int | The max redirect count to follow for the HTTP transport | `5` | yes
5557
`aether.connector.https.cipherSuites` | String | Comma-separated list of [Cipher Suites](https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#ciphersuites) which are enabled for HTTPS connections. | - (no restriction) | no
5658
`aether.connector.https.securityMode` | String | Using this flag resolver may set the "security mode" of HTTPS connector. Any other mode than 'default' is NOT MEANT for production, as it is inherently not secure. Accepted values: "default", "insecure" (ignore any kind of certificate validation errors and hostname validation checks). | `"default"` | yes
5759
`aether.connector.https.protocols` | String | Comma-separated list of [Protocols](https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#jssenames) which are enabled for HTTPS connections. | - (no restriction) | no

0 commit comments

Comments
 (0)