Skip to content

Commit

Permalink
Merge pull request #5102 from eclipse/jetty-9.4.x-3916-multipart-outp…
Browse files Browse the repository at this point in the history
…ut-boundary

Issue #3916 - Fix whitespace between boundary and part headers
  • Loading branch information
joakime authored Jul 30, 2020
2 parents f37f828 + 784fabf commit 8b52d7d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,28 @@ public void testMultipleRangeRequests() throws Exception
String boundary = body.substring(0, body.indexOf("\r\n"));
assertResponseContains("206 Partial", response);
assertResponseContains("Content-Type: multipart/byteranges; boundary=", response);
assertResponseContains("Content-Range: bytes 0-9/80", response);
assertResponseContains("Content-Range: bytes 20-29/80", response);
assertResponseContains("Content-Range: bytes 40-49/80", response);
assertResponseContains(DATA.substring(0, 10), response);
assertResponseContains(DATA.substring(20, 30), response);
assertResponseContains(DATA.substring(40, 50), response);

String section1 = boundary + "\r\n" +
"Content-Type: text/plain\r\n" +
"Content-Range: bytes 0-9/80\r\n" +
"\r\n" +
DATA.substring(0, 10) + "\r\n";
assertResponseContains(section1, response);

String section2 = boundary + "\r\n" +
"Content-Type: text/plain\r\n" +
"Content-Range: bytes 20-29/80\r\n" +
"\r\n" +
DATA.substring(20, 30) + "\r\n";
assertResponseContains(section2, response);

String section3 = boundary + "\r\n" +
"Content-Type: text/plain\r\n" +
"Content-Range: bytes 40-49/80\r\n" +
"\r\n" +
DATA.substring(40, 50) + "\r\n";
assertResponseContains(section3, response);

assertTrue(body.endsWith(boundary + "--\r\n"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
public class MultiPartOutputStream extends FilterOutputStream
{

private static final byte[] __CRLF = {'\r', '\n'};
private static final byte[] __DASHDASH = {'-', '-'};
private static final byte[] CRLF = {'\r', '\n'};
private static final byte[] DASHDASH = {'-', '-'};

public static final String MULTIPART_MIXED = "multipart/mixed";
public static final String MULTIPART_X_MIXED_REPLACE = "multipart/x-mixed-replace";
Expand Down Expand Up @@ -71,11 +71,11 @@ public void close()
try
{
if (inPart)
out.write(__CRLF);
out.write(__DASHDASH);
out.write(CRLF);
out.write(DASHDASH);
out.write(boundaryBytes);
out.write(__DASHDASH);
out.write(__CRLF);
out.write(DASHDASH);
out.write(CRLF);
inPart = false;
}
finally
Expand Down Expand Up @@ -104,15 +104,19 @@ public void startPart(String contentType)
throws IOException
{
if (inPart)
out.write(__CRLF);
{
out.write(CRLF);
}
inPart = true;
out.write(__DASHDASH);
out.write(DASHDASH);
out.write(boundaryBytes);
out.write(__CRLF);
out.write(CRLF);
if (contentType != null)
{
out.write(("Content-Type: " + contentType).getBytes(StandardCharsets.ISO_8859_1));
out.write(__CRLF);
out.write(__CRLF);
out.write(CRLF);
}
out.write(CRLF);
}

/**
Expand All @@ -126,20 +130,22 @@ public void startPart(String contentType, String[] headers)
throws IOException
{
if (inPart)
out.write(__CRLF);
out.write(CRLF);
inPart = true;
out.write(__DASHDASH);
out.write(DASHDASH);
out.write(boundaryBytes);
out.write(__CRLF);
out.write(CRLF);
if (contentType != null)
{
out.write(("Content-Type: " + contentType).getBytes(StandardCharsets.ISO_8859_1));
out.write(__CRLF);
out.write(CRLF);
}
for (int i = 0; headers != null && i < headers.length; i++)
{
out.write(headers[i].getBytes(StandardCharsets.ISO_8859_1));
out.write(__CRLF);
out.write(CRLF);
}
out.write(__CRLF);
out.write(CRLF);
}

@Override
Expand All @@ -148,7 +154,3 @@ public void write(byte[] b, int off, int len) throws IOException
out.write(b, off, len);
}
}




0 comments on commit 8b52d7d

Please sign in to comment.