Skip to content

Commit

Permalink
JCLOUDS-1632: GCP BlobStore fails to put a blob if blob name contains…
Browse files Browse the repository at this point in the history
… non ASCII characters
  • Loading branch information
astsiapanay authored and gaul committed Mar 20, 2024
1 parent 4c7fb2c commit b379c17
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public MultipartForm(String boundary, Iterable<? extends Part> 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();
Expand Down
32 changes: 23 additions & 9 deletions core/src/test/java/org/jclouds/io/payloads/MultipartFormTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand All @@ -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);

Expand Down

0 comments on commit b379c17

Please sign in to comment.