You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jclouds.apache.org by ga...@apache.org on 2015/09/09 00:35:19 UTC

[04/10] jclouds git commit: JCLOUDS-217: Fix encoding handling in S3.

JCLOUDS-217: Fix encoding handling in S3.

When constructing the query path, S3 does not properly handle encoded
paths. For example, if a blob named %20 is to be placed into the blob
store, S3 would end up placing blob named " " (what %20 represents).
This occurs because the S3 provider examines the URI's path portion
(which is presented in a decoded fasion to the caller). After
examining the path, it is not encoded again. Instead, we should call
getRawPath() to avoid this issue.

There are two issues on the decoding path:
1. Given a blob named " ", S3 API will throw a RuntimeException due to
a NULL check -- the key that it uses is NULL to represent the XML
content " " corresponding to the blob name.
2. Given a blob named "%20 ", S3 API will generate a URI for a blob
named "%20%20", which is also incorrect. The correct URI would be
"%2520%20" (escaping the first "%" and " " characters).

The first issue is due to the currentOrNull() helper, which calls
trim() on the string and then compares the string to an empty string.
This means that a blob named " " will be parsed as "" and then
converted to NULL as the result of that method. Passing "null" as the
key then fails in a number of places (notably, appendPath()).

The second issue is due to the appendPath() method in the jclouds Uris
class. The issue here is that appendPath() calls urlDecode() and
passes the result to path(). The path() method, in turn, also calls
urlDecode(). After these transformations, a properly encoded blob of
the form %2520%20 turns into "%20 " and then "  " (two spaces). After
these transformations the path is encoded again, resulting in "%20%20"
(which is wrong).


Project: http://git-wip-us.apache.org/repos/asf/jclouds/repo
Commit: http://git-wip-us.apache.org/repos/asf/jclouds/commit/a0b95ea7
Tree: http://git-wip-us.apache.org/repos/asf/jclouds/tree/a0b95ea7
Diff: http://git-wip-us.apache.org/repos/asf/jclouds/diff/a0b95ea7

Branch: refs/heads/master
Commit: a0b95ea7a3df01399c95e57117b63e2a9c22a37b
Parents: aaa1641
Author: Timur Alperovich <ti...@gmail.com>
Authored: Sat Aug 22 00:36:15 2015 -0700
Committer: Timur Alperovich <ti...@gmail.com>
Committed: Tue Sep 8 15:19:02 2015 -0700

----------------------------------------------------------------------
 .../jclouds/s3/binders/BindAsHostPrefixIfConfigured.java |  2 +-
 .../main/java/org/jclouds/s3/xml/ListBucketHandler.java  | 11 ++++++++---
 .../java/org/jclouds/rest/binders/BindAsHostPrefix.java  |  5 ++++-
 3 files changed, 13 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/jclouds/blob/a0b95ea7/apis/s3/src/main/java/org/jclouds/s3/binders/BindAsHostPrefixIfConfigured.java
----------------------------------------------------------------------
diff --git a/apis/s3/src/main/java/org/jclouds/s3/binders/BindAsHostPrefixIfConfigured.java b/apis/s3/src/main/java/org/jclouds/s3/binders/BindAsHostPrefixIfConfigured.java
index 0c1e567..4fb18ac 100644
--- a/apis/s3/src/main/java/org/jclouds/s3/binders/BindAsHostPrefixIfConfigured.java
+++ b/apis/s3/src/main/java/org/jclouds/s3/binders/BindAsHostPrefixIfConfigured.java
@@ -63,7 +63,7 @@ public class BindAsHostPrefixIfConfigured implements Binder {
          }
          return (R) request.toBuilder().replaceHeader(HttpHeaders.HOST, host).build();
       } else {
-         StringBuilder path = new StringBuilder(request.getEndpoint().getPath());
+         StringBuilder path = new StringBuilder(request.getEndpoint().getRawPath());
          if (servicePath.equals("/")) {
             if (path.toString().equals("/"))
                path.append(payloadAsString);

http://git-wip-us.apache.org/repos/asf/jclouds/blob/a0b95ea7/apis/s3/src/main/java/org/jclouds/s3/xml/ListBucketHandler.java
----------------------------------------------------------------------
diff --git a/apis/s3/src/main/java/org/jclouds/s3/xml/ListBucketHandler.java b/apis/s3/src/main/java/org/jclouds/s3/xml/ListBucketHandler.java
index c5fdb73..27d0a94 100644
--- a/apis/s3/src/main/java/org/jclouds/s3/xml/ListBucketHandler.java
+++ b/apis/s3/src/main/java/org/jclouds/s3/xml/ListBucketHandler.java
@@ -32,6 +32,7 @@ import org.jclouds.s3.domain.ListBucketResponse;
 import org.jclouds.s3.domain.ObjectMetadata;
 import org.jclouds.s3.domain.ObjectMetadataBuilder;
 import org.jclouds.s3.domain.internal.ListBucketResponseImpl;
+import org.jclouds.util.Strings2;
 import org.xml.sax.Attributes;
 
 import com.google.common.collect.ImmutableSet;
@@ -81,6 +82,7 @@ public class ListBucketHandler extends ParseSax.HandlerWithResult<ListBucketResp
       if (qName.equals("CommonPrefixes")) {
          inCommonPrefixes = true;
       }
+      currentText.setLength(0);
    }
 
    public void endElement(String uri, String name, String qName) {
@@ -89,9 +91,13 @@ public class ListBucketHandler extends ParseSax.HandlerWithResult<ListBucketResp
       } else if (qName.equals("DisplayName")) {
          currentOwner.setDisplayName(currentOrNull(currentText));
       } else if (qName.equals("Key")) { // content stuff
-         currentKey = currentOrNull(currentText);
+         if (currentText.length() == 0) {
+            throw new RuntimeException("Object store returned empty key name");
+         }
+         currentKey = currentText.toString();
          builder.key(currentKey);
-         builder.uri(uriBuilder(getRequest().getEndpoint()).clearQuery().appendPath(currentKey).build());
+         builder.uri(uriBuilder(getRequest().getEndpoint()).clearQuery().appendPath(Strings2.urlEncode(currentKey))
+               .build());
       } else if (qName.equals("LastModified")) {
          builder.lastModified(dateParser
                .iso8601DateOrSecondsDateParse(currentOrNull(currentText)));
@@ -132,7 +138,6 @@ public class ListBucketHandler extends ParseSax.HandlerWithResult<ListBucketResp
       } else if (qName.equals("IsTruncated")) {
          this.isTruncated = Boolean.parseBoolean(currentOrNull(currentText));
       }
-      currentText.setLength(0);
    }
 
    public void characters(char ch[], int start, int length) {

http://git-wip-us.apache.org/repos/asf/jclouds/blob/a0b95ea7/core/src/main/java/org/jclouds/rest/binders/BindAsHostPrefix.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/jclouds/rest/binders/BindAsHostPrefix.java b/core/src/main/java/org/jclouds/rest/binders/BindAsHostPrefix.java
index 8c8ec7f..f88460d 100644
--- a/core/src/main/java/org/jclouds/rest/binders/BindAsHostPrefix.java
+++ b/core/src/main/java/org/jclouds/rest/binders/BindAsHostPrefix.java
@@ -22,6 +22,8 @@ import static com.google.common.net.InternetDomainName.from;
 import static com.google.common.net.InternetDomainName.isValid;
 import static org.jclouds.http.Uris.uriBuilder;
 
+import java.net.URI;
+
 import javax.inject.Singleton;
 
 import org.jclouds.http.HttpRequest;
@@ -38,6 +40,7 @@ public class BindAsHostPrefix implements Binder {
       checkNotNull(payload, "hostprefix");
       checkArgument(isValid(request.getEndpoint().getHost()), "this is only valid for hostnames: " + request);
       InternetDomainName name = from(request.getEndpoint().getHost()).child(payload.toString());
-      return (R) request.toBuilder().endpoint(uriBuilder(request.getEndpoint()).host(name.toString()).build()).build();
+      URI newEndpoint = uriBuilder(request.getEndpoint()).host(name.toString()).build();
+      return (R) request.toBuilder().endpoint(newEndpoint).replacePath(request.getEndpoint().getRawPath()).build();
    }
 }