Skip to content

Commit

Permalink
Refactor SimpleHTTPClient to read body as-is rather than via readLine()
Browse files Browse the repository at this point in the history
Update impacted tests so they still pass
  • Loading branch information
markt-asf committed Jun 22, 2023
1 parent 4e5ffbe commit 00e7d79
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion test/org/apache/catalina/core/TestStandardContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ protected void service(HttpServletRequest req, HttpServletResponse resp)

PrintWriter out = resp.getWriter();

out.println("parts=" + (null == req.getParts()
out.print("parts=" + (null == req.getParts()
? "null"
: Integer.valueOf(req.getParts().size())));
}
Expand Down
16 changes: 12 additions & 4 deletions test/org/apache/catalina/startup/SimpleHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,13 @@ public String getResponseLine() {
return responseLine;
}

public int getStatusCode() {
if (responseLine.length() < 13) {
throw new IllegalStateException();
}
return Integer.parseInt(responseLine.substring(9, 12));
}

public List<String> getResponseHeaders() {
return responseHeaders;
}
Expand Down Expand Up @@ -326,11 +333,12 @@ private void processBody(boolean wantBody) throws IOException {
builder.append(body, 0 , read);
Assert.assertEquals(contentLength, builder.toString().getBytes(responseBodyEncoding).length);
} else {
// not using content length, so just read it line by line
String line = null;
// Not using content length, so just read until EOF
char[] buf = new char[1024];
int read;
try {
while ((line = readLine()) != null) {
builder.append(line);
while ((read = reader.read(buf)) != -1) {
builder.append(buf, 0, read);
}
} catch (SocketException e) {
// Ignore
Expand Down
2 changes: 1 addition & 1 deletion test/org/apache/coyote/http11/TestHttp11InputBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se
private void processHeaders(String header, HttpServletRequest req, PrintWriter out) {
Enumeration<String> values = req.getHeaders(header);
while (values.hasMoreElements()) {
out.println(values.nextElement());
out.print(values.nextElement());
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/org/apache/coyote/http11/TestHttp11Processor.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public void testResponseWithErrorChunked() throws Exception {
// There should not be an end chunk
Assert.assertFalse(client.getResponseBody().endsWith("0"));
// The last portion of text should be there
Assert.assertTrue(client.getResponseBody().endsWith("line03"));
Assert.assertTrue(client.getResponseBody().endsWith("line03" + SimpleHttpClient.CRLF));
}

private static class ResponseWithErrorServlet extends HttpServlet {
Expand Down

0 comments on commit 00e7d79

Please sign in to comment.