From 36b3dadf8c3f817c80f7edc639b449a2b40b9388 Mon Sep 17 00:00:00 2001 From: Stephen Tomkinson Date: Wed, 10 May 2017 11:41:23 +0100 Subject: [PATCH] Add logic from JCLOUDS-1261 to the other 2 types of Aws4 signer. --- .../org/jclouds/s3/filters/Aws4SignerBase.java | 16 ++++++++++++++++ .../Aws4SignerForAuthorizationHeader.java | 9 +-------- .../s3/filters/Aws4SignerForChunkedUpload.java | 1 + .../s3/filters/Aws4SignerForQueryString.java | 1 + 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/apis/s3/src/main/java/org/jclouds/s3/filters/Aws4SignerBase.java b/apis/s3/src/main/java/org/jclouds/s3/filters/Aws4SignerBase.java index 1348e22348..c30ee46d43 100644 --- a/apis/s3/src/main/java/org/jclouds/s3/filters/Aws4SignerBase.java +++ b/apis/s3/src/main/java/org/jclouds/s3/filters/Aws4SignerBase.java @@ -133,6 +133,22 @@ public abstract class Aws4SignerBase { dateFormat.setTimeZone(GMT); } + protected static String hostHeaderFor(URI endpoint) { + String scheme = endpoint.getScheme(); + String host = endpoint.getHost(); + int port = endpoint.getPort(); + + // if the port is defined and doesn't match the URI scheme + if (port != -1) { + if (("http".equalsIgnoreCase(scheme) && port != 80) || + ("https".equalsIgnoreCase(scheme) && port != 443)) { + host += ":" + port; // append the port number to the hostname + } + } + + return host; // else just use the original hostname + } + protected String getContentType(HttpRequest request) { Payload payload = request.getPayload(); diff --git a/apis/s3/src/main/java/org/jclouds/s3/filters/Aws4SignerForAuthorizationHeader.java b/apis/s3/src/main/java/org/jclouds/s3/filters/Aws4SignerForAuthorizationHeader.java index 9b8bf99153..258c3a700a 100644 --- a/apis/s3/src/main/java/org/jclouds/s3/filters/Aws4SignerForAuthorizationHeader.java +++ b/apis/s3/src/main/java/org/jclouds/s3/filters/Aws4SignerForAuthorizationHeader.java @@ -75,7 +75,6 @@ public class Aws4SignerForAuthorizationHeader extends Aws4SignerBase { // get host & port from request endpoint. String host = request.getEndpoint().getHost(); - int port = request.getEndpoint().getPort(); Date date = timestampProvider.get(); String timestamp = timestampFormat.format(date); @@ -120,13 +119,7 @@ public class Aws4SignerForAuthorizationHeader extends Aws4SignerBase { } // host - // if the port is defined and doesn't match the URI scheme - if (port != -1) { - if (("http".equalsIgnoreCase(request.getEndpoint().getScheme()) && port != 80) || - ("https".equalsIgnoreCase(request.getEndpoint().getScheme()) && port != 443)) { - host += ":" + port; // append the port number to the hostname - } - } + host = hostHeaderFor(request.getEndpoint()); requestBuilder.replaceHeader(HttpHeaders.HOST, host); signedHeadersBuilder.put(HttpHeaders.HOST.toLowerCase(), host); diff --git a/apis/s3/src/main/java/org/jclouds/s3/filters/Aws4SignerForChunkedUpload.java b/apis/s3/src/main/java/org/jclouds/s3/filters/Aws4SignerForChunkedUpload.java index bd455e9648..8729054739 100644 --- a/apis/s3/src/main/java/org/jclouds/s3/filters/Aws4SignerForChunkedUpload.java +++ b/apis/s3/src/main/java/org/jclouds/s3/filters/Aws4SignerForChunkedUpload.java @@ -155,6 +155,7 @@ public class Aws4SignerForChunkedUpload extends Aws4SignerBase { } // host + host = hostHeaderFor(request.getEndpoint()); requestBuilder.replaceHeader(HttpHeaders.HOST, host); signedHeadersBuilder.put(HttpHeaders.HOST.toLowerCase(), host); diff --git a/apis/s3/src/main/java/org/jclouds/s3/filters/Aws4SignerForQueryString.java b/apis/s3/src/main/java/org/jclouds/s3/filters/Aws4SignerForQueryString.java index 2f1abe42fc..537e66c21f 100644 --- a/apis/s3/src/main/java/org/jclouds/s3/filters/Aws4SignerForQueryString.java +++ b/apis/s3/src/main/java/org/jclouds/s3/filters/Aws4SignerForQueryString.java @@ -102,6 +102,7 @@ public class Aws4SignerForQueryString extends Aws4SignerBase { // For added security, you should sign as many headers as possible. // HOST + host = hostHeaderFor(request.getEndpoint()); signedHeadersBuilder.put("host", host); ImmutableMap signedHeaders = signedHeadersBuilder.build();