From 2bd1029d1bf29a728cdb244f70a3cc7c58dd7030 Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Fri, 3 Feb 2017 10:29:27 -0700 Subject: [PATCH] Fixing InsufficientBytes test cases + Because of issues #1045 and #1185 insufficient bytes on a response results in a closed connection --- .../org/eclipse/jetty/http/HttpTester.java | 56 ++++++++++++------- .../jetty/server/AbstractHttpTest.java | 7 ++- .../server/HttpManyWaysToCommitTest.java | 42 ++++---------- 3 files changed, 52 insertions(+), 53 deletions(-) diff --git a/jetty-http/src/test/java/org/eclipse/jetty/http/HttpTester.java b/jetty-http/src/test/java/org/eclipse/jetty/http/HttpTester.java index 40c4c99b2d8..2137da99cfd 100644 --- a/jetty-http/src/test/java/org/eclipse/jetty/http/HttpTester.java +++ b/jetty-http/src/test/java/org/eclipse/jetty/http/HttpTester.java @@ -118,7 +118,7 @@ public class HttpTester parser.parseNext(ByteBuffer.wrap(contentStream.toByteArray())); return r; } - + public abstract static class Input { final ByteBuffer _buffer; @@ -216,27 +216,41 @@ public class HttpTester } public static Response parseResponse(Input in) throws IOException - { - return parseResponse(in, false); - } - - public static Response parsePartialResponse(Input in) throws IOException - { - return parseResponse(in, true); - } - - private static Response parseResponse(Input in, boolean allowIncomplete) throws IOException { Response r; HttpParser parser=in.takeHttpParser(); if (parser==null) { r=new Response(); - parser =new HttpParser(r); + parser = new HttpParser(r); } else r=(Response)parser.getHandler(); + parseResponse(in, parser, r); + + if(r.isComplete()) + return r; + + in.setHttpParser(parser); + return null; + } + + public static void parseResponse(Input in, Response response) throws IOException + { + HttpParser parser = in.takeHttpParser(); + if (parser == null) + { + parser = new HttpParser(response); + } + parseResponse(in, parser, response); + + if (!response.isComplete()) + in.setHttpParser(parser); + } + + private static void parseResponse(Input in, HttpParser parser, Response r) throws IOException + { ByteBuffer buffer = in.getBuffer(); while(true) @@ -255,18 +269,12 @@ public class HttpTester } } - if (allowIncomplete || r.isComplete()) - return r; - - LOG.info("Incomplete Response: (parser={}) {}", parser, r); - - in.setHttpParser(parser); - return null; + System.out.printf("parseResponse() parser=%s%n%s%n", parser, r.toString()); } - public abstract static class Message extends HttpFields implements HttpParser.HttpHandler { + boolean _earlyEOF; boolean _complete=false; ByteArrayOutputStream _content; HttpVersion _version=HttpVersion.HTTP_1_0; @@ -379,8 +387,14 @@ public class HttpTester @Override public void earlyEOF() { + _earlyEOF = true; } - + + public boolean isEarlyEOF() + { + return _earlyEOF; + } + @Override public boolean content(ByteBuffer ref) { diff --git a/jetty-server/src/test/java/org/eclipse/jetty/server/AbstractHttpTest.java b/jetty-server/src/test/java/org/eclipse/jetty/server/AbstractHttpTest.java index 66d20d92bc4..7db394abf7a 100644 --- a/jetty-server/src/test/java/org/eclipse/jetty/server/AbstractHttpTest.java +++ b/jetty-server/src/test/java/org/eclipse/jetty/server/AbstractHttpTest.java @@ -66,7 +66,7 @@ public abstract class AbstractHttpTest { server = new Server(); connector = new ServerConnector(server,null,null,new ArrayByteBufferPool(64,2048,64*1024),1,1,new HttpConnectionFactory()); - connector.setIdleTimeout(10000); + connector.setIdleTimeout(100000); server.addConnector(connector); stacklessChannelLogging =new StacklessLogging(HttpChannel.class); @@ -92,9 +92,12 @@ public abstract class AbstractHttpTest writer.write("\r\n"); writer.flush(); + HttpTester.Response response = new HttpTester.Response(); HttpTester.Input input = HttpTester.from(socket.getInputStream()); - HttpTester.Response response = HttpTester.parsePartialResponse(input); + HttpTester.parseResponse(input, response); + if ("HTTP/1.1".equals(httpVersion) + && response.isComplete() && response.get("content-length") == null && response.get("transfer-encoding") == null && !__noBodyCodes.contains(response.getStatus())) diff --git a/jetty-server/src/test/java/org/eclipse/jetty/server/HttpManyWaysToCommitTest.java b/jetty-server/src/test/java/org/eclipse/jetty/server/HttpManyWaysToCommitTest.java index c4e0b90f911..9cb27c4d48e 100644 --- a/jetty-server/src/test/java/org/eclipse/jetty/server/HttpManyWaysToCommitTest.java +++ b/jetty-server/src/test/java/org/eclipse/jetty/server/HttpManyWaysToCommitTest.java @@ -21,10 +21,9 @@ package org.eclipse.jetty.server; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.not; import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; -import java.io.EOFException; import java.io.IOException; -import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.Collection; @@ -426,20 +425,14 @@ public class HttpManyWaysToCommitTest extends AbstractHttpTest { server.setHandler(new SetContentLengthAndWriteInsufficientBytesHandler(true)); server.start(); - try - { - HttpTester.Response response = executeRequest(); - assertThat("response code", response.getStatus(), is(200)); - assertHeader(response, "content-length", "6"); - byte content[] = response.getContentBytes(); - assertThat("content bytes", content.length, is(6)); - String contentStr = new String(content, StandardCharsets.UTF_8); - assertThat("content bytes as string", contentStr, is("foo")); - } - catch(EOFException e) - { - // possible good response - } + + HttpTester.Response response = executeRequest(); + System.out.println(response.toString()); + assertThat("response code", response.getStatus(), is(200)); + assertHeader(response, "content-length", "6"); + byte content[] = response.getContentBytes(); + assertThat("content bytes", content.length, is(0)); + assertTrue("response eof", response.isEarlyEOF()); } @Test @@ -448,20 +441,9 @@ public class HttpManyWaysToCommitTest extends AbstractHttpTest server.setHandler(new SetContentLengthAndWriteInsufficientBytesHandler(false)); server.start(); - try - { - HttpTester.Response response = executeRequest(); - assertThat("response code is 200", response.getStatus(), is(200)); - assertHeader(response, "content-length", "6"); - byte content[] = response.getContentBytes(); - assertThat("content bytes", content.length, is(3)); - String contentStr = new String(content, StandardCharsets.UTF_8); - assertThat("content bytes as string", contentStr, is("foo")); - } - catch(EOFException e) - { - // expected - } + HttpTester.Response response = executeRequest(); + assertThat("response has no status", response.getStatus(), is(0)); + assertTrue("response eof", response.isEarlyEOF()); } @Test