Skip to content

Commit ba7a78a

Browse files
balamuruganaharshavardhana
authored andcommitted
fix: split and add path as segments to HttpUrl.Builder (#506)
okhttp.HttpUrl.Builder.addEncodedPathSegment() does encoding '/' even for encoded path. This patch fixes this by splitting path by '/' and encodes each segment and add them individually. Fixes #504
1 parent 86d75b4 commit ba7a78a

File tree

3 files changed

+20
-22
lines changed

3 files changed

+20
-22
lines changed

api/src/main/java/io/minio/MinioClient.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,9 @@ private Request createRequest(Method method, String bucketName, String objectNam
646646

647647
if (objectName != null) {
648648
// Limitation: OkHttp does not allow to add '.' and '..' as path segment.
649-
urlBuilder.addEncodedPathSegment(S3Escaper.encodePath(objectName));
649+
for (String pathSegment : objectName.split("/")) {
650+
urlBuilder.addEncodedPathSegment(S3Escaper.encode(pathSegment));
651+
}
650652
}
651653

652654
if (queryParamMap != null) {

api/src/main/java/io/minio/S3Escaper.java

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -49,25 +49,4 @@ public static String encode(String str) {
4949
.replaceAll("\\[", "%5B")
5050
.replaceAll("\\]", "%5D");
5151
}
52-
53-
54-
/**
55-
* Returns S3 encoded path.
56-
*/
57-
public static String encodePath(String path) {
58-
StringBuffer encodedPath = new StringBuffer();
59-
60-
// Limitation: Its not allowed to add path segment as '/', '//', '/usr' or 'usr/'.
61-
if (path != null) {
62-
for (String pathSegment : path.split("/")) {
63-
if (encodedPath.length() > 0) {
64-
encodedPath.append("/");
65-
}
66-
67-
encodedPath.append(encode(pathSegment));
68-
}
69-
}
70-
71-
return encodedPath.toString();
72-
}
7352
}

functional/FunctionalTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,22 @@ public static void getObject_test4() throws Exception {
278278
client.removeObject(bucketName, fileName);
279279
}
280280

281+
/**
282+
* Test: getObject(String bucketName, String objectName, String fileName).
283+
* where objectName has multiple path segments.
284+
*/
285+
public static void getObject_test5() throws Exception {
286+
System.out.println("Test: objectName with multiple path segments: "
287+
+ "getObject(String bucketName, String objectName, String fileName)");
288+
String fileName = createFile(3 * MB);
289+
String objectName = "path/to/" + fileName;
290+
client.putObject(bucketName, objectName, fileName);
291+
Files.delete(Paths.get(fileName));
292+
client.getObject(bucketName, objectName, fileName + ".downloaded");
293+
Files.delete(Paths.get(fileName + ".downloaded"));
294+
client.removeObject(bucketName, objectName);
295+
}
296+
281297
/**
282298
* Test: listObjects(final String bucketName).
283299
*/
@@ -849,6 +865,7 @@ public static void main(String[] args) {
849865
getObject_test2();
850866
getObject_test3();
851867
getObject_test4();
868+
getObject_test5();
852869

853870
listObject_test1();
854871
listObject_test2();

0 commit comments

Comments
 (0)