Skip to content

Commit

Permalink
Fix #11640. Handling of 301 redirect response with no Location header…
Browse files Browse the repository at this point in the history
… in response.
  • Loading branch information
dkocher committed Apr 16, 2021
1 parent c9fb47b commit e5f8881
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
Expand Up @@ -27,11 +27,14 @@
import ch.cyberduck.core.preferences.PreferencesFactory;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.ProtocolException;
import org.apache.http.client.RedirectException;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.conn.util.InetAddressUtils;
import org.apache.http.impl.client.DefaultRedirectStrategy;
import org.apache.http.impl.client.HttpClientBuilder;
Expand Down Expand Up @@ -120,16 +123,27 @@ public RequestEntityRestStorageService(final S3Session session, final HttpClient
this.properties = this.getJetS3tProperties();
// Client configuration
configuration.disableContentCompression();
configuration.setRetryHandler(new S3HttpRequestRetryHandler(this, preferences.getInteger("http.connections.retry")));
final RequestEntityRestStorageService authorizer = this;
configuration.setRetryHandler(new S3HttpRequestRetryHandler(authorizer, preferences.getInteger("http.connections.retry")));
configuration.setRedirectStrategy(new DefaultRedirectStrategy() {
@Override
public HttpUriRequest getRedirect(final HttpRequest request, final HttpResponse response, final HttpContext context) throws ProtocolException {
if(response.containsHeader("x-amz-bucket-region")) {
final String host = ((HttpUriRequest) request).getURI().getHost();
if(!StringUtils.equals(session.getHost().getHostname(), host)) {
regionEndpointCache.putRegionForBucketName(
StringUtils.split(StringUtils.removeEnd(((HttpUriRequest) request).getURI().getHost(), session.getHost().getHostname()), ".")[0],
response.getFirstHeader("x-amz-bucket-region").getValue());
final Header header = response.getFirstHeader("x-amz-bucket-region");
log.warn(String.format("Received redirect response %s with %s", response, header));
String uri = request.getRequestLine().getUri();
for(Location.Name region : session.getHost().getProtocol().getRegions()) {
if(StringUtils.contains(uri, region.getIdentifier())) {
log.warn(String.format("Retry request with URI %s", uri));
final HttpUriRequest uriRequest = RequestBuilder.copy(request).setUri(StringUtils.replace(uri, region.getIdentifier(), header.getValue())).build();
try {
authorizer.authorizeHttpRequest(uriRequest, context, null);
}
catch(ServiceException e) {
throw new RedirectException(e.getMessage(), e);
}
return uriRequest;
}
}
}
return super.getRedirect(request, response, context);
Expand Down
Expand Up @@ -216,4 +216,14 @@ public void testFindCommonPrefix() throws Exception {
// Expected
}
}

@Test
public void testFindProfile() throws Exception {
final Path container = new Path("profiles.cyberduck.io", EnumSet.of(Path.Type.directory, Path.Type.volume));
final Path test = new Path(container, "S3 (HTTP).cyberduckprofile", EnumSet.of(Path.Type.file),
new PathAttributes().withVersionId("4ajsLHgDubdGpoOjd1XCY1m4K5IUOfMY"));
final S3AttributesFinderFeature f = new S3AttributesFinderFeature(session);
final PathAttributes attributes = f.find(test);
assertEquals("30298e0b4a1bd3ce954289281347c6ad", attributes.getChecksum().hash);
}
}

0 comments on commit e5f8881

Please sign in to comment.