From 372a33ef59915eda7df05b593b83705befe36280 Mon Sep 17 00:00:00 2001 From: Aliaksandr Stsiapanay Date: Wed, 20 Mar 2024 11:37:11 +0300 Subject: [PATCH] JCLOUDS-1632: GCP BlobStore fails to put a blob if blob name contains non ASCII characters --- .../jclouds/io/payloads/MultipartForm.java | 2 +- .../io/payloads/MultipartFormTest.java | 32 +++++++++++++------ 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/core/src/main/java/org/jclouds/io/payloads/MultipartForm.java b/core/src/main/java/org/jclouds/io/payloads/MultipartForm.java index 94d4112cf45..c148de402bc 100644 --- a/core/src/main/java/org/jclouds/io/payloads/MultipartForm.java +++ b/core/src/main/java/org/jclouds/io/payloads/MultipartForm.java @@ -50,7 +50,7 @@ public MultipartForm(String boundary, Iterable content) { if (!part.isRepeatable()) isRepeatable = false; contentLength += part.getContentMetadata().getContentLength() - + createHeaders(boundaryrn, part).length() + + createHeaders(boundaryrn, part).getBytes().length + createRn().length(); } contentLength += createFooter(boundary).length(); diff --git a/core/src/test/java/org/jclouds/io/payloads/MultipartFormTest.java b/core/src/test/java/org/jclouds/io/payloads/MultipartFormTest.java index 3a673e47120..9f90d06a41a 100644 --- a/core/src/test/java/org/jclouds/io/payloads/MultipartFormTest.java +++ b/core/src/test/java/org/jclouds/io/payloads/MultipartFormTest.java @@ -41,17 +41,31 @@ public class MultipartFormTest { public void testSinglePart() throws IOException { StringBuilder builder = new StringBuilder(); - addData(boundary, "hello", builder); + addData(boundary, "file", "hello", builder); builder.append("--").append(boundary).append("--").append("\r\n"); String expects = builder.toString(); assertEquals(expects.length(), 199); - MultipartForm multipartForm = new MultipartForm(boundary, newPart("hello")); + MultipartForm multipartForm = new MultipartForm(boundary, newPart("file", "hello")); assertEquals(Strings2.toStringAndClose(multipartForm.openStream()), expects); assertEquals(multipartForm.getContentMetadata().getContentLength(), Long.valueOf(199)); } + public void testLengthIsCorrectPerUTF8() throws IOException { + + StringBuilder builder = new StringBuilder(); + addData(boundary, "unic₪de", "hello", builder); + builder.append("--").append(boundary).append("--").append("\r\n"); + String expects = builder.toString(); + assertEquals(expects.getBytes().length, 204); + + MultipartForm multipartForm = new MultipartForm(boundary, newPart("unic₪de", "hello")); + + assertEquals(Strings2.toStringAndClose(multipartForm.openStream()), expects); + assertEquals(multipartForm.getContentMetadata().getContentLength(), Long.valueOf(204)); + } + public static class MockFilePayload extends FilePayload { private final StringPayload realPayload; @@ -81,14 +95,14 @@ public boolean isRepeatable() { } } - private Part newPart(String data) { - return Part.create("file", new MockFilePayload(data), + private Part newPart(String name, String data) { + return Part.create(name, new MockFilePayload(data), new PartOptions().contentType(PLAIN_TEXT_UTF_8.withoutParameters().toString())); } - private void addData(String boundary, String data, StringBuilder builder) { + private void addData(String boundary, String name, String data, StringBuilder builder) { builder.append("--").append(boundary).append("\r\n"); - builder.append("Content-Disposition").append(": ").append("form-data; name=\"file\"; filename=\"testfile.txt\"") + builder.append("Content-Disposition").append(": ").append("form-data; name=\"").append(name).append("\"; filename=\"testfile.txt\"") .append("\r\n"); builder.append("Content-Type").append(": ").append("text/plain").append("\r\n"); builder.append("\r\n"); @@ -98,15 +112,15 @@ private void addData(String boundary, String data, StringBuilder builder) { public void testMultipleParts() throws IOException { StringBuilder builder = new StringBuilder(); - addData(boundary, "hello", builder); - addData(boundary, "goodbye", builder); + addData(boundary, "file", "hello", builder); + addData(boundary, "file", "goodbye", builder); builder.append("--").append(boundary).append("--").append("\r\n"); String expects = builder.toString(); assertEquals(expects.length(), 352); - MultipartForm multipartForm = new MultipartForm(boundary, newPart("hello"), newPart("goodbye")); + MultipartForm multipartForm = new MultipartForm(boundary, newPart("file", "hello"), newPart("file", "goodbye")); assertEquals(Strings2.toStringAndClose(multipartForm.openStream()), expects);