From c583054a173cb6ebdc657b78db019b0ce96db7dd Mon Sep 17 00:00:00 2001 From: Simone Bordet Date: Mon, 14 Mar 2016 12:26:11 +0100 Subject: [PATCH 1/2] Code cleanup. --- .../eclipse/jetty/http/HttpParserTest.java | 2184 ++++++++--------- 1 file changed, 1082 insertions(+), 1102 deletions(-) diff --git a/jetty-http/src/test/java/org/eclipse/jetty/http/HttpParserTest.java b/jetty-http/src/test/java/org/eclipse/jetty/http/HttpParserTest.java index fc3c3dcb6d1..e09ff61aad4 100644 --- a/jetty-http/src/test/java/org/eclipse/jetty/http/HttpParserTest.java +++ b/jetty-http/src/test/java/org/eclipse/jetty/http/HttpParserTest.java @@ -30,17 +30,12 @@ import org.junit.Assert; import org.junit.Before; import org.junit.Test; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; - public class HttpParserTest { /** * Parse until {@link State#END} state. * If the parser is already in the END state, then it is {@link HttpParser#reset()} and re-parsed. + * * @param parser The parser to test * @param buffer the buffer to parse * @throws IllegalStateException If the buffers have already been partially parsed. @@ -53,13 +48,13 @@ public class HttpParserTest throw new IllegalStateException("!START"); // continue parsing - int remaining=buffer.remaining(); - while (!parser.isState(State.END) && remaining>0) + int remaining = buffer.remaining(); + while (!parser.isState(State.END) && remaining > 0) { - int was_remaining=remaining; + int was_remaining = remaining; parser.parseNext(buffer); - remaining=buffer.remaining(); - if (remaining==was_remaining) + remaining = buffer.remaining(); + if (remaining == was_remaining) break; } } @@ -67,401 +62,399 @@ public class HttpParserTest @Test public void HttpMethodTest() { - assertNull(HttpMethod.lookAheadGet(BufferUtil.toBuffer("Wibble "))); - assertNull(HttpMethod.lookAheadGet(BufferUtil.toBuffer("GET"))); - assertNull(HttpMethod.lookAheadGet(BufferUtil.toBuffer("MO"))); + Assert.assertNull(HttpMethod.lookAheadGet(BufferUtil.toBuffer("Wibble "))); + Assert.assertNull(HttpMethod.lookAheadGet(BufferUtil.toBuffer("GET"))); + Assert.assertNull(HttpMethod.lookAheadGet(BufferUtil.toBuffer("MO"))); - assertEquals(HttpMethod.GET,HttpMethod.lookAheadGet(BufferUtil.toBuffer("GET "))); - assertEquals(HttpMethod.MOVE,HttpMethod.lookAheadGet(BufferUtil.toBuffer("MOVE "))); + Assert.assertEquals(HttpMethod.GET, HttpMethod.lookAheadGet(BufferUtil.toBuffer("GET "))); + Assert.assertEquals(HttpMethod.MOVE, HttpMethod.lookAheadGet(BufferUtil.toBuffer("MOVE "))); ByteBuffer b = BufferUtil.allocateDirect(128); - BufferUtil.append(b,BufferUtil.toBuffer("GET")); - assertNull(HttpMethod.lookAheadGet(b)); + BufferUtil.append(b, BufferUtil.toBuffer("GET")); + Assert.assertNull(HttpMethod.lookAheadGet(b)); - BufferUtil.append(b,BufferUtil.toBuffer(" ")); - assertEquals(HttpMethod.GET,HttpMethod.lookAheadGet(b)); + BufferUtil.append(b, BufferUtil.toBuffer(" ")); + Assert.assertEquals(HttpMethod.GET, HttpMethod.lookAheadGet(b)); } @Test public void testLineParse_Mock_IP() throws Exception { - ByteBuffer buffer= BufferUtil.toBuffer("POST /mock/127.0.0.1 HTTP/1.1\015\012" + "\015\012"); + ByteBuffer buffer = BufferUtil.toBuffer("POST /mock/127.0.0.1 HTTP/1.1\r\n" + "\r\n"); - HttpParser.RequestHandler handler = new Handler(); - HttpParser parser= new HttpParser(handler); - parseAll(parser,buffer); - assertEquals("POST", _methodOrVersion); - assertEquals("/mock/127.0.0.1", _uriOrStatus); - assertEquals("HTTP/1.1", _versionOrReason); - assertEquals(-1, _headers); + HttpParser.RequestHandler handler = new Handler(); + HttpParser parser = new HttpParser(handler); + parseAll(parser, buffer); + Assert.assertEquals("POST", _methodOrVersion); + Assert.assertEquals("/mock/127.0.0.1", _uriOrStatus); + Assert.assertEquals("HTTP/1.1", _versionOrReason); + Assert.assertEquals(-1, _headers); } @Test public void testLineParse0() throws Exception { - ByteBuffer buffer= BufferUtil.toBuffer("POST /foo HTTP/1.0\015\012" + "\015\012"); + ByteBuffer buffer = BufferUtil.toBuffer("POST /foo HTTP/1.0\r\n" + "\r\n"); - HttpParser.RequestHandler handler = new Handler(); - HttpParser parser= new HttpParser(handler); - parseAll(parser,buffer); - assertEquals("POST", _methodOrVersion); - assertEquals("/foo", _uriOrStatus); - assertEquals("HTTP/1.0", _versionOrReason); - assertEquals(-1, _headers); + HttpParser.RequestHandler handler = new Handler(); + HttpParser parser = new HttpParser(handler); + parseAll(parser, buffer); + Assert.assertEquals("POST", _methodOrVersion); + Assert.assertEquals("/foo", _uriOrStatus); + Assert.assertEquals("HTTP/1.0", _versionOrReason); + Assert.assertEquals(-1, _headers); } @Test public void testLineParse1_RFC2616() throws Exception { - ByteBuffer buffer= BufferUtil.toBuffer("GET /999\015\012"); + ByteBuffer buffer = BufferUtil.toBuffer("GET /999\r\n"); - HttpParser.RequestHandler handler = new Handler(); - HttpParser parser= new HttpParser(handler,HttpCompliance.RFC2616); - parseAll(parser,buffer); + HttpParser.RequestHandler handler = new Handler(); + HttpParser parser = new HttpParser(handler, HttpCompliance.RFC2616); + parseAll(parser, buffer); - assertNull(_bad); - assertEquals("GET", _methodOrVersion); - assertEquals("/999", _uriOrStatus); - assertEquals("HTTP/0.9", _versionOrReason); - assertEquals(-1, _headers); + Assert.assertNull(_bad); + Assert.assertEquals("GET", _methodOrVersion); + Assert.assertEquals("/999", _uriOrStatus); + Assert.assertEquals("HTTP/0.9", _versionOrReason); + Assert.assertEquals(-1, _headers); } @Test public void testLineParse1() throws Exception { - ByteBuffer buffer= BufferUtil.toBuffer("GET /999\015\012"); + ByteBuffer buffer = BufferUtil.toBuffer("GET /999\r\n"); - HttpParser.RequestHandler handler = new Handler(); - HttpParser parser= new HttpParser(handler); - parseAll(parser,buffer); - assertEquals("HTTP/0.9 not supported", _bad); + HttpParser.RequestHandler handler = new Handler(); + HttpParser parser = new HttpParser(handler); + parseAll(parser, buffer); + Assert.assertEquals("HTTP/0.9 not supported", _bad); } @Test public void testLineParse2_RFC2616() throws Exception { - ByteBuffer buffer= BufferUtil.toBuffer("POST /222 \015\012"); + ByteBuffer buffer = BufferUtil.toBuffer("POST /222 \r\n"); - HttpParser.RequestHandler handler = new Handler(); - HttpParser parser= new HttpParser(handler,HttpCompliance.RFC2616); - parseAll(parser,buffer); + HttpParser.RequestHandler handler = new Handler(); + HttpParser parser = new HttpParser(handler, HttpCompliance.RFC2616); + parseAll(parser, buffer); - assertNull(_bad); - assertEquals("POST", _methodOrVersion); - assertEquals("/222", _uriOrStatus); - assertEquals("HTTP/0.9", _versionOrReason); - assertEquals(-1, _headers); + Assert.assertNull(_bad); + Assert.assertEquals("POST", _methodOrVersion); + Assert.assertEquals("/222", _uriOrStatus); + Assert.assertEquals("HTTP/0.9", _versionOrReason); + Assert.assertEquals(-1, _headers); } @Test public void testLineParse2() throws Exception { - ByteBuffer buffer= BufferUtil.toBuffer("POST /222 \015\012"); + ByteBuffer buffer = BufferUtil.toBuffer("POST /222 \r\n"); - _versionOrReason= null; - HttpParser.RequestHandler handler = new Handler(); - HttpParser parser= new HttpParser(handler); - parseAll(parser,buffer); - assertEquals("HTTP/0.9 not supported", _bad); + _versionOrReason = null; + HttpParser.RequestHandler handler = new Handler(); + HttpParser parser = new HttpParser(handler); + parseAll(parser, buffer); + Assert.assertEquals("HTTP/0.9 not supported", _bad); } @Test public void testLineParse3() throws Exception { - ByteBuffer buffer= BufferUtil.toBuffer("POST /fo\u0690 HTTP/1.0\015\012" + "\015\012",StandardCharsets.UTF_8); + ByteBuffer buffer = BufferUtil.toBuffer("POST /fo\u0690 HTTP/1.0\r\n" + "\r\n", StandardCharsets.UTF_8); - HttpParser.RequestHandler handler = new Handler(); - HttpParser parser= new HttpParser(handler); - parseAll(parser,buffer); - assertEquals("POST", _methodOrVersion); - assertEquals("/fo\u0690", _uriOrStatus); - assertEquals("HTTP/1.0", _versionOrReason); - assertEquals(-1, _headers); + HttpParser.RequestHandler handler = new Handler(); + HttpParser parser = new HttpParser(handler); + parseAll(parser, buffer); + Assert.assertEquals("POST", _methodOrVersion); + Assert.assertEquals("/fo\u0690", _uriOrStatus); + Assert.assertEquals("HTTP/1.0", _versionOrReason); + Assert.assertEquals(-1, _headers); } @Test public void testLineParse4() throws Exception { - ByteBuffer buffer= BufferUtil.toBuffer("POST /foo?param=\u0690 HTTP/1.0\015\012" + "\015\012",StandardCharsets.UTF_8); + ByteBuffer buffer = BufferUtil.toBuffer("POST /foo?param=\u0690 HTTP/1.0\r\n" + "\r\n", StandardCharsets.UTF_8); - HttpParser.RequestHandler handler = new Handler(); - HttpParser parser= new HttpParser(handler); - parseAll(parser,buffer); - assertEquals("POST", _methodOrVersion); - assertEquals("/foo?param=\u0690", _uriOrStatus); - assertEquals("HTTP/1.0", _versionOrReason); - assertEquals(-1, _headers); + HttpParser.RequestHandler handler = new Handler(); + HttpParser parser = new HttpParser(handler); + parseAll(parser, buffer); + Assert.assertEquals("POST", _methodOrVersion); + Assert.assertEquals("/foo?param=\u0690", _uriOrStatus); + Assert.assertEquals("HTTP/1.0", _versionOrReason); + Assert.assertEquals(-1, _headers); } @Test public void testLongURLParse() throws Exception { - ByteBuffer buffer= BufferUtil.toBuffer("POST /123456789abcdef/123456789abcdef/123456789abcdef/123456789abcdef/123456789abcdef/123456789abcdef/123456789abcdef/123456789abcdef/123456789abcdef/123456789abcdef/123456789abcdef/123456789abcdef/123456789abcdef/123456789abcdef/123456789abcdef/123456789abcdef/ HTTP/1.0\015\012" + "\015\012"); + ByteBuffer buffer = BufferUtil.toBuffer("POST /123456789abcdef/123456789abcdef/123456789abcdef/123456789abcdef/123456789abcdef/123456789abcdef/123456789abcdef/123456789abcdef/123456789abcdef/123456789abcdef/123456789abcdef/123456789abcdef/123456789abcdef/123456789abcdef/123456789abcdef/123456789abcdef/ HTTP/1.0\r\n" + "\r\n"); - HttpParser.RequestHandler handler = new Handler(); - HttpParser parser= new HttpParser(handler); - parseAll(parser,buffer); - assertEquals("POST", _methodOrVersion); - assertEquals("/123456789abcdef/123456789abcdef/123456789abcdef/123456789abcdef/123456789abcdef/123456789abcdef/123456789abcdef/123456789abcdef/123456789abcdef/123456789abcdef/123456789abcdef/123456789abcdef/123456789abcdef/123456789abcdef/123456789abcdef/123456789abcdef/", _uriOrStatus); - assertEquals("HTTP/1.0", _versionOrReason); - assertEquals(-1, _headers); + HttpParser.RequestHandler handler = new Handler(); + HttpParser parser = new HttpParser(handler); + parseAll(parser, buffer); + Assert.assertEquals("POST", _methodOrVersion); + Assert.assertEquals("/123456789abcdef/123456789abcdef/123456789abcdef/123456789abcdef/123456789abcdef/123456789abcdef/123456789abcdef/123456789abcdef/123456789abcdef/123456789abcdef/123456789abcdef/123456789abcdef/123456789abcdef/123456789abcdef/123456789abcdef/123456789abcdef/", _uriOrStatus); + Assert.assertEquals("HTTP/1.0", _versionOrReason); + Assert.assertEquals(-1, _headers); } @Test public void testConnect() throws Exception { - ByteBuffer buffer= BufferUtil.toBuffer("CONNECT 192.168.1.2:80 HTTP/1.1\015\012" + "\015\012"); - HttpParser.RequestHandler handler = new Handler(); - HttpParser parser= new HttpParser(handler); - parseAll(parser,buffer); - assertEquals("CONNECT", _methodOrVersion); - assertEquals("192.168.1.2:80", _uriOrStatus); - assertEquals("HTTP/1.1", _versionOrReason); - assertEquals(-1, _headers); + ByteBuffer buffer = BufferUtil.toBuffer("CONNECT 192.168.1.2:80 HTTP/1.1\r\n" + "\r\n"); + HttpParser.RequestHandler handler = new Handler(); + HttpParser parser = new HttpParser(handler); + parseAll(parser, buffer); + Assert.assertEquals("CONNECT", _methodOrVersion); + Assert.assertEquals("192.168.1.2:80", _uriOrStatus); + Assert.assertEquals("HTTP/1.1", _versionOrReason); + Assert.assertEquals(-1, _headers); } @Test public void testSimple() throws Exception { - ByteBuffer buffer= BufferUtil.toBuffer( - "GET / HTTP/1.0\015\012" + - "Host: localhost\015\012" + - "Connection: close\015\012" + - "\015\012"); + ByteBuffer buffer = BufferUtil.toBuffer( + "GET / HTTP/1.0\r\n" + + "Host: localhost\r\n" + + "Connection: close\r\n" + + "\r\n"); - HttpParser.RequestHandler handler = new Handler(); - HttpParser parser= new HttpParser(handler); - parseAll(parser,buffer); + HttpParser.RequestHandler handler = new Handler(); + HttpParser parser = new HttpParser(handler); + parseAll(parser, buffer); - assertTrue(_headerCompleted); - assertTrue(_messageCompleted); - assertEquals("GET", _methodOrVersion); - assertEquals("/", _uriOrStatus); - assertEquals("HTTP/1.0", _versionOrReason); - assertEquals("Host", _hdr[0]); - assertEquals("localhost", _val[0]); - assertEquals("Connection", _hdr[1]); - assertEquals("close", _val[1]); - assertEquals(1, _headers); + Assert.assertTrue(_headerCompleted); + Assert.assertTrue(_messageCompleted); + Assert.assertEquals("GET", _methodOrVersion); + Assert.assertEquals("/", _uriOrStatus); + Assert.assertEquals("HTTP/1.0", _versionOrReason); + Assert.assertEquals("Host", _hdr[0]); + Assert.assertEquals("localhost", _val[0]); + Assert.assertEquals("Connection", _hdr[1]); + Assert.assertEquals("close", _val[1]); + Assert.assertEquals(1, _headers); } @Test public void test2616Continuations() throws Exception { - ByteBuffer buffer= BufferUtil.toBuffer( - "GET / HTTP/1.0\015\012" + - "Host: localhost\015\012" + - "Name: value\015\012" + - " extra\015\012" + - "\015\012"); + ByteBuffer buffer = BufferUtil.toBuffer( + "GET / HTTP/1.0\r\n" + + "Host: localhost\r\n" + + "Name: value\r\n" + + " extra\r\n" + + "\r\n"); - HttpParser.RequestHandler handler = new Handler(); - HttpParser parser= new HttpParser(handler,HttpCompliance.RFC2616); - parseAll(parser,buffer); + HttpParser.RequestHandler handler = new Handler(); + HttpParser parser = new HttpParser(handler, HttpCompliance.RFC2616); + parseAll(parser, buffer); - Assert.assertThat(_bad,Matchers.nullValue()); - assertEquals("Host", _hdr[0]); - assertEquals("localhost", _val[0]); - assertEquals("Name", _hdr[1]); - assertEquals("value extra", _val[1]); - assertEquals(1, _headers); + Assert.assertThat(_bad, Matchers.nullValue()); + Assert.assertEquals("Host", _hdr[0]); + Assert.assertEquals("localhost", _val[0]); + Assert.assertEquals("Name", _hdr[1]); + Assert.assertEquals("value extra", _val[1]); + Assert.assertEquals(1, _headers); } @Test public void test7230NoContinuations() throws Exception { - ByteBuffer buffer= BufferUtil.toBuffer( - "GET / HTTP/1.0\015\012" + - "Host: localhost\015\012" + - "Name: value\015\012" + - " extra\015\012" + - "\015\012"); + ByteBuffer buffer = BufferUtil.toBuffer( + "GET / HTTP/1.0\r\n" + + "Host: localhost\r\n" + + "Name: value\r\n" + + " extra\r\n" + + "\r\n"); - HttpParser.RequestHandler handler = new Handler(); - HttpParser parser= new HttpParser(handler,4096,HttpCompliance.RFC7230); - parseAll(parser,buffer); + HttpParser.RequestHandler handler = new Handler(); + HttpParser parser = new HttpParser(handler, 4096, HttpCompliance.RFC7230); + parseAll(parser, buffer); - Assert.assertThat(_bad,Matchers.notNullValue()); - Assert.assertThat(_bad,Matchers.containsString("Bad Continuation")); + Assert.assertThat(_bad, Matchers.notNullValue()); + Assert.assertThat(_bad, Matchers.containsString("Bad Continuation")); } @Test public void test7230NoWhiteSpaceInName() throws Exception { - ByteBuffer buffer= BufferUtil.toBuffer( - "GET / HTTP/1.0\015\012" + - "Host: localhost\015\012" + - " Name: value\015\012" + - "\015\012"); + ByteBuffer buffer = BufferUtil.toBuffer( + "GET / HTTP/1.0\r\n" + + "Host: localhost\r\n" + + " Name: value\r\n" + + "\r\n"); - HttpParser.RequestHandler handler = new Handler(); - HttpParser parser= new HttpParser(handler); - parseAll(parser,buffer); + HttpParser.RequestHandler handler = new Handler(); + HttpParser parser = new HttpParser(handler, HttpCompliance.RFC7230); + parseAll(parser, buffer); - Assert.assertThat(_bad,Matchers.notNullValue()); - Assert.assertThat(_bad,Matchers.containsString("Bad")); + Assert.assertThat(_bad, Matchers.notNullValue()); + Assert.assertThat(_bad, Matchers.containsString("Bad")); init(); - buffer= BufferUtil.toBuffer( - "GET / HTTP/1.0\015\012" + - "Host: localhost\015\012" + - "N ame: value\015\012" + - "\015\012"); + buffer = BufferUtil.toBuffer( + "GET / HTTP/1.0\r\n" + + "Host: localhost\r\n" + + "N ame: value\r\n" + + "\r\n"); - handler = new Handler(); - parser= new HttpParser(handler); - parseAll(parser,buffer); - - Assert.assertThat(_bad,Matchers.containsString("Illegal character")); + handler = new Handler(); + parser = new HttpParser(handler); + parseAll(parser, buffer); + Assert.assertThat(_bad, Matchers.containsString("Illegal character")); init(); - buffer= BufferUtil.toBuffer( - "GET / HTTP/1.0\015\012" + - "Host: localhost\015\012" + - "Name : value\015\012" + - "\015\012"); + buffer = BufferUtil.toBuffer( + "GET / HTTP/1.0\r\n" + + "Host: localhost\r\n" + + "Name : value\r\n" + + "\r\n"); - handler = new Handler(); - parser= new HttpParser(handler); - parseAll(parser,buffer); - - Assert.assertThat(_bad,Matchers.containsString("Illegal character")); + handler = new Handler(); + parser = new HttpParser(handler); + parseAll(parser, buffer); + Assert.assertThat(_bad, Matchers.containsString("Illegal character")); } @Test public void testNoValue() throws Exception { - ByteBuffer buffer= BufferUtil.toBuffer( - "GET / HTTP/1.0\015\012" + - "Host: localhost\015\012" + - "Name0: \015\012"+ - "Name1: \015\012"+ - "Connection: close\015\012" + - "\015\012"); + ByteBuffer buffer = BufferUtil.toBuffer( + "GET / HTTP/1.0\r\n" + + "Host: localhost\r\n" + + "Name0: \r\n" + + "Name1: \r\n" + + "Connection: close\r\n" + + "\r\n"); - HttpParser.RequestHandler handler = new Handler(); - HttpParser parser= new HttpParser(handler,HttpCompliance.RFC2616); - parseAll(parser,buffer); + HttpParser.RequestHandler handler = new Handler(); + HttpParser parser = new HttpParser(handler, HttpCompliance.RFC2616); + parseAll(parser, buffer); - assertTrue(_headerCompleted); - assertTrue(_messageCompleted); - assertEquals("GET", _methodOrVersion); - assertEquals("/", _uriOrStatus); - assertEquals("HTTP/1.0", _versionOrReason); - assertEquals("Host", _hdr[0]); - assertEquals("localhost", _val[0]); - assertEquals("Name0", _hdr[1]); - assertEquals("", _val[1]); - assertEquals("Name1", _hdr[2]); - assertEquals("", _val[2]); - assertEquals("Connection", _hdr[3]); - assertEquals("close", _val[3]); - assertEquals(3, _headers); + Assert.assertTrue(_headerCompleted); + Assert.assertTrue(_messageCompleted); + Assert.assertEquals("GET", _methodOrVersion); + Assert.assertEquals("/", _uriOrStatus); + Assert.assertEquals("HTTP/1.0", _versionOrReason); + Assert.assertEquals("Host", _hdr[0]); + Assert.assertEquals("localhost", _val[0]); + Assert.assertEquals("Name0", _hdr[1]); + Assert.assertEquals("", _val[1]); + Assert.assertEquals("Name1", _hdr[2]); + Assert.assertEquals("", _val[2]); + Assert.assertEquals("Connection", _hdr[3]); + Assert.assertEquals("close", _val[3]); + Assert.assertEquals(3, _headers); } @Test public void testHeaderParseDirect() throws Exception { - ByteBuffer b0= BufferUtil.toBuffer( - "GET / HTTP/1.0\015\012" + - "Host: localhost\015\012" + - "Header1: value1\015\012" + - "Header2: value 2a \015\012" + - "Header3: 3\015\012" + - "Header4:value4\015\012" + - "Server5: notServer\015\012" + - "HostHeader: notHost\015\012" + - "Connection: close\015\012" + - "Accept-Encoding: gzip, deflated\015\012" + - "Accept: unknown\015\012" + - "\015\012"); + ByteBuffer b0 = BufferUtil.toBuffer( + "GET / HTTP/1.0\r\n" + + "Host: localhost\r\n" + + "Header1: value1\r\n" + + "Header2: value 2a \r\n" + + "Header3: 3\r\n" + + "Header4:value4\r\n" + + "Server5: notServer\r\n" + + "HostHeader: notHost\r\n" + + "Connection: close\r\n" + + "Accept-Encoding: gzip, deflated\r\n" + + "Accept: unknown\r\n" + + "\r\n"); ByteBuffer buffer = BufferUtil.allocateDirect(b0.capacity()); - int pos=BufferUtil.flipToFill(buffer); - BufferUtil.put(b0,buffer); - BufferUtil.flipToFlush(buffer,pos); + int pos = BufferUtil.flipToFill(buffer); + BufferUtil.put(b0, buffer); + BufferUtil.flipToFlush(buffer, pos); - HttpParser.RequestHandler handler = new Handler(); - HttpParser parser= new HttpParser(handler); - parseAll(parser,buffer); + HttpParser.RequestHandler handler = new Handler(); + HttpParser parser = new HttpParser(handler); + parseAll(parser, buffer); - assertEquals("GET", _methodOrVersion); - assertEquals("/", _uriOrStatus); - assertEquals("HTTP/1.0", _versionOrReason); - assertEquals("Host", _hdr[0]); - assertEquals("localhost", _val[0]); - assertEquals("Header1", _hdr[1]); - assertEquals("value1", _val[1]); - assertEquals("Header2", _hdr[2]); - assertEquals("value 2a", _val[2]); - assertEquals("Header3", _hdr[3]); - assertEquals("3", _val[3]); - assertEquals("Header4", _hdr[4]); - assertEquals("value4", _val[4]); - assertEquals("Server5", _hdr[5]); - assertEquals("notServer", _val[5]); - assertEquals("HostHeader", _hdr[6]); - assertEquals("notHost", _val[6]); - assertEquals("Connection", _hdr[7]); - assertEquals("close", _val[7]); - assertEquals("Accept-Encoding", _hdr[8]); - assertEquals("gzip, deflated", _val[8]); - assertEquals("Accept", _hdr[9]); - assertEquals("unknown", _val[9]); - assertEquals(9, _headers); + Assert.assertEquals("GET", _methodOrVersion); + Assert.assertEquals("/", _uriOrStatus); + Assert.assertEquals("HTTP/1.0", _versionOrReason); + Assert.assertEquals("Host", _hdr[0]); + Assert.assertEquals("localhost", _val[0]); + Assert.assertEquals("Header1", _hdr[1]); + Assert.assertEquals("value1", _val[1]); + Assert.assertEquals("Header2", _hdr[2]); + Assert.assertEquals("value 2a", _val[2]); + Assert.assertEquals("Header3", _hdr[3]); + Assert.assertEquals("3", _val[3]); + Assert.assertEquals("Header4", _hdr[4]); + Assert.assertEquals("value4", _val[4]); + Assert.assertEquals("Server5", _hdr[5]); + Assert.assertEquals("notServer", _val[5]); + Assert.assertEquals("HostHeader", _hdr[6]); + Assert.assertEquals("notHost", _val[6]); + Assert.assertEquals("Connection", _hdr[7]); + Assert.assertEquals("close", _val[7]); + Assert.assertEquals("Accept-Encoding", _hdr[8]); + Assert.assertEquals("gzip, deflated", _val[8]); + Assert.assertEquals("Accept", _hdr[9]); + Assert.assertEquals("unknown", _val[9]); + Assert.assertEquals(9, _headers); } @Test public void testHeaderParseCRLF() throws Exception { - ByteBuffer buffer= BufferUtil.toBuffer( - "GET / HTTP/1.0\015\012" + - "Host: localhost\015\012" + - "Header1: value1\015\012" + - "Header2: value 2a \015\012" + - "Header3: 3\015\012" + - "Header4:value4\015\012" + - "Server5: notServer\015\012" + - "HostHeader: notHost\015\012" + - "Connection: close\015\012" + - "Accept-Encoding: gzip, deflated\015\012" + - "Accept: unknown\015\012" + - "\015\012"); - HttpParser.RequestHandler handler = new Handler(); - HttpParser parser= new HttpParser(handler); - parseAll(parser,buffer); + ByteBuffer buffer = BufferUtil.toBuffer( + "GET / HTTP/1.0\r\n" + + "Host: localhost\r\n" + + "Header1: value1\r\n" + + "Header2: value 2a \r\n" + + "Header3: 3\r\n" + + "Header4:value4\r\n" + + "Server5: notServer\r\n" + + "HostHeader: notHost\r\n" + + "Connection: close\r\n" + + "Accept-Encoding: gzip, deflated\r\n" + + "Accept: unknown\r\n" + + "\r\n"); + HttpParser.RequestHandler handler = new Handler(); + HttpParser parser = new HttpParser(handler); + parseAll(parser, buffer); - assertEquals("GET", _methodOrVersion); - assertEquals("/", _uriOrStatus); - assertEquals("HTTP/1.0", _versionOrReason); - assertEquals("Host", _hdr[0]); - assertEquals("localhost", _val[0]); - assertEquals("Header1", _hdr[1]); - assertEquals("value1", _val[1]); - assertEquals("Header2", _hdr[2]); - assertEquals("value 2a", _val[2]); - assertEquals("Header3", _hdr[3]); - assertEquals("3", _val[3]); - assertEquals("Header4", _hdr[4]); - assertEquals("value4", _val[4]); - assertEquals("Server5", _hdr[5]); - assertEquals("notServer", _val[5]); - assertEquals("HostHeader", _hdr[6]); - assertEquals("notHost", _val[6]); - assertEquals("Connection", _hdr[7]); - assertEquals("close", _val[7]); - assertEquals("Accept-Encoding", _hdr[8]); - assertEquals("gzip, deflated", _val[8]); - assertEquals("Accept", _hdr[9]); - assertEquals("unknown", _val[9]); - assertEquals(9, _headers); + Assert.assertEquals("GET", _methodOrVersion); + Assert.assertEquals("/", _uriOrStatus); + Assert.assertEquals("HTTP/1.0", _versionOrReason); + Assert.assertEquals("Host", _hdr[0]); + Assert.assertEquals("localhost", _val[0]); + Assert.assertEquals("Header1", _hdr[1]); + Assert.assertEquals("value1", _val[1]); + Assert.assertEquals("Header2", _hdr[2]); + Assert.assertEquals("value 2a", _val[2]); + Assert.assertEquals("Header3", _hdr[3]); + Assert.assertEquals("3", _val[3]); + Assert.assertEquals("Header4", _hdr[4]); + Assert.assertEquals("value4", _val[4]); + Assert.assertEquals("Server5", _hdr[5]); + Assert.assertEquals("notServer", _val[5]); + Assert.assertEquals("HostHeader", _hdr[6]); + Assert.assertEquals("notHost", _val[6]); + Assert.assertEquals("Connection", _hdr[7]); + Assert.assertEquals("close", _val[7]); + Assert.assertEquals("Accept-Encoding", _hdr[8]); + Assert.assertEquals("gzip, deflated", _val[8]); + Assert.assertEquals("Accept", _hdr[9]); + Assert.assertEquals("unknown", _val[9]); + Assert.assertEquals(9, _headers); } @Test public void testHeaderParseLF() throws Exception { - ByteBuffer buffer= BufferUtil.toBuffer( + ByteBuffer buffer = BufferUtil.toBuffer( "GET / HTTP/1.0\n" + "Host: localhost\n" + "Header1: value1\n" + @@ -473,1227 +466,1219 @@ public class HttpParserTest "Connection: close\n" + "Accept-Encoding: gzip, deflated\n" + "Accept: unknown\n" + - "\n"); - HttpParser.RequestHandler handler = new Handler(); - HttpParser parser= new HttpParser(handler); - parseAll(parser,buffer); + "\n"); + HttpParser.RequestHandler handler = new Handler(); + HttpParser parser = new HttpParser(handler); + parseAll(parser, buffer); - assertEquals("GET", _methodOrVersion); - assertEquals("/", _uriOrStatus); - assertEquals("HTTP/1.0", _versionOrReason); - assertEquals("Host", _hdr[0]); - assertEquals("localhost", _val[0]); - assertEquals("Header1", _hdr[1]); - assertEquals("value1", _val[1]); - assertEquals("Header2", _hdr[2]); - assertEquals("value 2a value 2b", _val[2]); - assertEquals("Header3", _hdr[3]); - assertEquals("3", _val[3]); - assertEquals("Header4", _hdr[4]); - assertEquals("value4", _val[4]); - assertEquals("Server5", _hdr[5]); - assertEquals("notServer", _val[5]); - assertEquals("HostHeader", _hdr[6]); - assertEquals("notHost", _val[6]); - assertEquals("Connection", _hdr[7]); - assertEquals("close", _val[7]); - assertEquals("Accept-Encoding", _hdr[8]); - assertEquals("gzip, deflated", _val[8]); - assertEquals("Accept", _hdr[9]); - assertEquals("unknown", _val[9]); - assertEquals(9, _headers); + Assert.assertEquals("GET", _methodOrVersion); + Assert.assertEquals("/", _uriOrStatus); + Assert.assertEquals("HTTP/1.0", _versionOrReason); + Assert.assertEquals("Host", _hdr[0]); + Assert.assertEquals("localhost", _val[0]); + Assert.assertEquals("Header1", _hdr[1]); + Assert.assertEquals("value1", _val[1]); + Assert.assertEquals("Header2", _hdr[2]); + Assert.assertEquals("value 2a value 2b", _val[2]); + Assert.assertEquals("Header3", _hdr[3]); + Assert.assertEquals("3", _val[3]); + Assert.assertEquals("Header4", _hdr[4]); + Assert.assertEquals("value4", _val[4]); + Assert.assertEquals("Server5", _hdr[5]); + Assert.assertEquals("notServer", _val[5]); + Assert.assertEquals("HostHeader", _hdr[6]); + Assert.assertEquals("notHost", _val[6]); + Assert.assertEquals("Connection", _hdr[7]); + Assert.assertEquals("close", _val[7]); + Assert.assertEquals("Accept-Encoding", _hdr[8]); + Assert.assertEquals("gzip, deflated", _val[8]); + Assert.assertEquals("Accept", _hdr[9]); + Assert.assertEquals("unknown", _val[9]); + Assert.assertEquals(9, _headers); } @Test public void testQuoted() throws Exception { - ByteBuffer buffer= BufferUtil.toBuffer( + ByteBuffer buffer = BufferUtil.toBuffer( "GET / HTTP/1.0\n" + "Name0: \"value0\"\t\n" + "Name1: \"value\t1\"\n" + "Name2: \"value\t2A\",\"value,2B\"\t\n" + - "\n"); - HttpParser.RequestHandler handler = new Handler(); - HttpParser parser= new HttpParser(handler); - parseAll(parser,buffer); + "\n"); + HttpParser.RequestHandler handler = new Handler(); + HttpParser parser = new HttpParser(handler); + parseAll(parser, buffer); - assertEquals("GET", _methodOrVersion); - assertEquals("/", _uriOrStatus); - assertEquals("HTTP/1.0", _versionOrReason); - assertEquals("Name0", _hdr[0]); - assertEquals("\"value0\"", _val[0]); - assertEquals("Name1", _hdr[1]); - assertEquals("\"value\t1\"", _val[1]); - assertEquals("Name2", _hdr[2]); - assertEquals("\"value\t2A\",\"value,2B\"", _val[2]); - assertEquals(2, _headers); + Assert.assertEquals("GET", _methodOrVersion); + Assert.assertEquals("/", _uriOrStatus); + Assert.assertEquals("HTTP/1.0", _versionOrReason); + Assert.assertEquals("Name0", _hdr[0]); + Assert.assertEquals("\"value0\"", _val[0]); + Assert.assertEquals("Name1", _hdr[1]); + Assert.assertEquals("\"value\t1\"", _val[1]); + Assert.assertEquals("Name2", _hdr[2]); + Assert.assertEquals("\"value\t2A\",\"value,2B\"", _val[2]); + Assert.assertEquals(2, _headers); } @Test public void testEncodedHeader() throws Exception { - ByteBuffer buffer=BufferUtil.allocate(4096); + ByteBuffer buffer = BufferUtil.allocate(4096); BufferUtil.flipToFill(buffer); - BufferUtil.put(BufferUtil.toBuffer("GET "),buffer); + BufferUtil.put(BufferUtil.toBuffer("GET "), buffer); buffer.put("/foo/\u0690/".getBytes(StandardCharsets.UTF_8)); - BufferUtil.put(BufferUtil.toBuffer(" HTTP/1.0\r\n"),buffer); - BufferUtil.put(BufferUtil.toBuffer("Header1: "),buffer); + BufferUtil.put(BufferUtil.toBuffer(" HTTP/1.0\r\n"), buffer); + BufferUtil.put(BufferUtil.toBuffer("Header1: "), buffer); buffer.put("\u00e6 \u00e6".getBytes(StandardCharsets.ISO_8859_1)); - BufferUtil.put(BufferUtil.toBuffer(" \r\n\r\n"),buffer); - BufferUtil.flipToFlush(buffer,0); + BufferUtil.put(BufferUtil.toBuffer(" \r\n\r\n"), buffer); + BufferUtil.flipToFlush(buffer, 0); - HttpParser.RequestHandler handler = new Handler(); - HttpParser parser= new HttpParser(handler); - parseAll(parser,buffer); + HttpParser.RequestHandler handler = new Handler(); + HttpParser parser = new HttpParser(handler); + parseAll(parser, buffer); - assertEquals("GET", _methodOrVersion); - assertEquals("/foo/\u0690/", _uriOrStatus); - assertEquals("HTTP/1.0", _versionOrReason); - assertEquals("Header1", _hdr[0]); - assertEquals("\u00e6 \u00e6", _val[0]); - assertEquals(0, _headers); - assertEquals(null,_bad); + Assert.assertEquals("GET", _methodOrVersion); + Assert.assertEquals("/foo/\u0690/", _uriOrStatus); + Assert.assertEquals("HTTP/1.0", _versionOrReason); + Assert.assertEquals("Header1", _hdr[0]); + Assert.assertEquals("\u00e6 \u00e6", _val[0]); + Assert.assertEquals(0, _headers); + Assert.assertEquals(null, _bad); } @Test public void testBadMethodEncoding() throws Exception { - ByteBuffer buffer= BufferUtil.toBuffer( - "G\u00e6T / HTTP/1.0\r\nHeader0: value0\r\n\n\n"); + ByteBuffer buffer = BufferUtil.toBuffer( + "G\u00e6T / HTTP/1.0\r\nHeader0: value0\r\n\n\n"); - HttpParser.RequestHandler handler = new Handler(); - HttpParser parser= new HttpParser(handler); - parseAll(parser,buffer); - assertThat(_bad,Matchers.notNullValue()); + HttpParser.RequestHandler handler = new Handler(); + HttpParser parser = new HttpParser(handler); + parseAll(parser, buffer); + Assert.assertThat(_bad, Matchers.notNullValue()); } @Test public void testBadVersionEncoding() throws Exception { - ByteBuffer buffer= BufferUtil.toBuffer( - "GET / H\u00e6P/1.0\r\nHeader0: value0\r\n\n\n"); + ByteBuffer buffer = BufferUtil.toBuffer( + "GET / H\u00e6P/1.0\r\nHeader0: value0\r\n\n\n"); - HttpParser.RequestHandler handler = new Handler(); - HttpParser parser= new HttpParser(handler); - parseAll(parser,buffer); - assertThat(_bad,Matchers.notNullValue()); + HttpParser.RequestHandler handler = new Handler(); + HttpParser parser = new HttpParser(handler); + parseAll(parser, buffer); + Assert.assertThat(_bad, Matchers.notNullValue()); } @Test public void testBadHeaderEncoding() throws Exception { - ByteBuffer buffer= BufferUtil.toBuffer( - "GET / HTTP/1.0\r\nH\u00e6der0: value0\r\n\n\n"); + ByteBuffer buffer = BufferUtil.toBuffer( + "GET / HTTP/1.0\r\nH\u00e6der0: value0\r\n\n\n"); - HttpParser.RequestHandler handler = new Handler(); - HttpParser parser= new HttpParser(handler); - parseAll(parser,buffer); - assertThat(_bad,Matchers.notNullValue()); + HttpParser.RequestHandler handler = new Handler(); + HttpParser parser = new HttpParser(handler); + parseAll(parser, buffer); + Assert.assertThat(_bad, Matchers.notNullValue()); } @Test public void testHeaderTab() throws Exception { - ByteBuffer buffer= BufferUtil.toBuffer( - "GET / HTTP/1.1\r\n" + - "Host: localhost\r\n" + - "Header: value\talternate\r\n" + - "\n\n"); + ByteBuffer buffer = BufferUtil.toBuffer( + "GET / HTTP/1.1\r\n" + + "Host: localhost\r\n" + + "Header: value\talternate\r\n" + + "\n\n"); - HttpParser.RequestHandler handler = new Handler(); - HttpParser parser= new HttpParser(handler); - parseAll(parser,buffer); + HttpParser.RequestHandler handler = new Handler(); + HttpParser parser = new HttpParser(handler); + parseAll(parser, buffer); - assertEquals("GET", _methodOrVersion); - assertEquals("/", _uriOrStatus); - assertEquals("HTTP/1.1", _versionOrReason); - assertEquals("Host", _hdr[0]); - assertEquals("localhost", _val[0]); - assertEquals("Header", _hdr[1]); - assertEquals("value\talternate", _val[1]); + Assert.assertEquals("GET", _methodOrVersion); + Assert.assertEquals("/", _uriOrStatus); + Assert.assertEquals("HTTP/1.1", _versionOrReason); + Assert.assertEquals("Host", _hdr[0]); + Assert.assertEquals("localhost", _val[0]); + Assert.assertEquals("Header", _hdr[1]); + Assert.assertEquals("value\talternate", _val[1]); } @Test public void testNonStrict() throws Exception { - ByteBuffer buffer= BufferUtil.toBuffer( - "get / http/1.0\015\012" + - "HOST: localhost\015\012" + - "cOnNeCtIoN: ClOsE\015\012"+ - "\015\012"); - HttpParser.RequestHandler handler = new Handler(); - HttpParser parser= new HttpParser(handler,-1,HttpCompliance.RFC7230); - parseAll(parser,buffer); - assertNull(_bad); - assertEquals("GET", _methodOrVersion); - assertEquals("/", _uriOrStatus); - assertEquals("HTTP/1.0", _versionOrReason); - assertEquals("Host", _hdr[0]); - assertEquals("localhost", _val[0]); - assertEquals("Connection", _hdr[1]); - assertEquals("close", _val[1]); - assertEquals(1, _headers); + ByteBuffer buffer = BufferUtil.toBuffer( + "get / http/1.0\r\n" + + "HOST: localhost\r\n" + + "cOnNeCtIoN: ClOsE\r\n" + + "\r\n"); + HttpParser.RequestHandler handler = new Handler(); + HttpParser parser = new HttpParser(handler, -1, HttpCompliance.RFC7230); + parseAll(parser, buffer); + Assert.assertNull(_bad); + Assert.assertEquals("GET", _methodOrVersion); + Assert.assertEquals("/", _uriOrStatus); + Assert.assertEquals("HTTP/1.0", _versionOrReason); + Assert.assertEquals("Host", _hdr[0]); + Assert.assertEquals("localhost", _val[0]); + Assert.assertEquals("Connection", _hdr[1]); + Assert.assertEquals("close", _val[1]); + Assert.assertEquals(1, _headers); } @Test public void testStrict() throws Exception { - ByteBuffer buffer= BufferUtil.toBuffer( - "gEt / http/1.0\015\012" + - "HOST: localhost\015\012" + - "cOnNeCtIoN: ClOsE\015\012"+ - "\015\012"); - HttpParser.RequestHandler handler = new Handler(); - HttpParser parser= new HttpParser(handler,-1,HttpCompliance.LEGACY); - parseAll(parser,buffer); - assertNull(_bad); - assertEquals("gEt", _methodOrVersion); - assertEquals("/", _uriOrStatus); - assertEquals("HTTP/1.0", _versionOrReason); - assertEquals("HOST", _hdr[0]); - assertEquals("localhost", _val[0]); - assertEquals("cOnNeCtIoN", _hdr[1]); - assertEquals("ClOsE", _val[1]); - assertEquals(1, _headers); + ByteBuffer buffer = BufferUtil.toBuffer( + "gEt / http/1.0\r\n" + + "HOST: localhost\r\n" + + "cOnNeCtIoN: ClOsE\r\n" + + "\r\n"); + HttpParser.RequestHandler handler = new Handler(); + HttpParser parser = new HttpParser(handler, -1, HttpCompliance.LEGACY); + parseAll(parser, buffer); + Assert.assertNull(_bad); + Assert.assertEquals("gEt", _methodOrVersion); + Assert.assertEquals("/", _uriOrStatus); + Assert.assertEquals("HTTP/1.0", _versionOrReason); + Assert.assertEquals("HOST", _hdr[0]); + Assert.assertEquals("localhost", _val[0]); + Assert.assertEquals("cOnNeCtIoN", _hdr[1]); + Assert.assertEquals("ClOsE", _val[1]); + Assert.assertEquals(1, _headers); } @Test public void testSplitHeaderParse() throws Exception { - ByteBuffer buffer= BufferUtil.toBuffer( - "XXXXSPLIT / HTTP/1.0\015\012" + - "Host: localhost\015\012" + - "Header1: value1\015\012" + - "Header2: value 2a \015\012" + - "Header3: 3\015\012" + - "Header4:value4\015\012" + - "Server5: notServer\015\012" + - "\015\012ZZZZ"); + ByteBuffer buffer = BufferUtil.toBuffer( + "XXXXSPLIT / HTTP/1.0\r\n" + + "Host: localhost\r\n" + + "Header1: value1\r\n" + + "Header2: value 2a \r\n" + + "Header3: 3\r\n" + + "Header4:value4\r\n" + + "Server5: notServer\r\n" + + "\r\nZZZZ"); buffer.position(2); - buffer.limit(buffer.capacity()-2); - buffer=buffer.slice(); + buffer.limit(buffer.capacity() - 2); + buffer = buffer.slice(); - for (int i=0;i _fields=new ArrayList<>(); + private List _fields = new ArrayList<>(); private String[] _hdr; private String[] _val; private int _headers; @@ -1780,16 +1765,14 @@ public class HttpParserTest private class Handler implements HttpParser.RequestHandler, HttpParser.ResponseHandler { private HttpFields fields; - String _proxy; @Override public boolean content(ByteBuffer ref) { - if (_content==null) - _content=""; - String c = BufferUtil.toString(ref,StandardCharsets.UTF_8); - //System.err.println("content '"+c+"'"); - _content= _content + c; + if (_content == null) + _content = ""; + String c = BufferUtil.toString(ref, StandardCharsets.UTF_8); + _content = _content + c; ref.position(ref.limit()); return false; } @@ -1798,17 +1781,17 @@ public class HttpParserTest public boolean startRequest(String method, String uri, HttpVersion version) { _fields.clear(); - _headers= -1; - _hdr= new String[10]; - _val= new String[10]; - _methodOrVersion= method; - _uriOrStatus= uri.toString(); - _versionOrReason= version==null?null:version.asString(); + _headers = -1; + _hdr = new String[10]; + _val = new String[10]; + _methodOrVersion = method; + _uriOrStatus = uri; + _versionOrReason = version == null ? null : version.asString(); - fields=new HttpFields(); + fields = new HttpFields(); _messageCompleted = false; _headerCompleted = false; - _early=false; + _early = false; return false; } @@ -1816,25 +1799,23 @@ public class HttpParserTest public void parsedHeader(HttpField field) { _fields.add(field); - //System.err.println("header "+name+": "+value); - _hdr[++_headers]= field.getName(); - _val[_headers]= field.getValue(); + _hdr[++_headers] = field.getName(); + _val[_headers] = field.getValue(); if (field instanceof HostPortHttpField) { HostPortHttpField hpfield = (HostPortHttpField)field; - _host=hpfield.getHost(); - _port=hpfield.getPort(); + _host = hpfield.getHost(); + _port = hpfield.getPort(); } } @Override public boolean headerComplete() { - //System.err.println("headerComplete"); - _content= null; - String s0=fields.toString(); - String s1=fields.toString(); + _content = null; + String s0 = fields.toString(); + String s1 = fields.toString(); if (!s0.equals(s1)) { throw new IllegalStateException(); @@ -1847,7 +1828,6 @@ public class HttpParserTest @Override public boolean messageComplete() { - //System.err.println("messageComplete"); _messageCompleted = true; return true; } @@ -1855,7 +1835,7 @@ public class HttpParserTest @Override public void badMessage(int status, String reason) { - _bad=reason==null?(""+status):reason; + _bad = reason == null ? ("" + status) : reason; } @Override @@ -1866,9 +1846,9 @@ public class HttpParserTest _uriOrStatus = Integer.toString(status); _versionOrReason = reason; - fields=new HttpFields(); - _hdr= new String[9]; - _val= new String[9]; + fields = new HttpFields(); + _hdr = new String[9]; + _val = new String[9]; _messageCompleted = false; _headerCompleted = false; @@ -1878,7 +1858,7 @@ public class HttpParserTest @Override public void earlyEOF() { - _early=true; + _early = true; } @Override From 838b5e4df23b5da9403dcf3f95207665b3518e5a Mon Sep 17 00:00:00 2001 From: Simone Bordet Date: Mon, 14 Mar 2016 12:31:12 +0100 Subject: [PATCH 2/2] Added test for newlines in header value. --- .../eclipse/jetty/http/HttpParserTest.java | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/jetty-http/src/test/java/org/eclipse/jetty/http/HttpParserTest.java b/jetty-http/src/test/java/org/eclipse/jetty/http/HttpParserTest.java index e09ff61aad4..c03280f863f 100644 --- a/jetty-http/src/test/java/org/eclipse/jetty/http/HttpParserTest.java +++ b/jetty-http/src/test/java/org/eclipse/jetty/http/HttpParserTest.java @@ -1732,6 +1732,44 @@ public class HttpParserTest Assert.assertEquals(null, _bad); } + @Test(expected = BadMessageException.class) + public void test7230HeaderValueWithNewLine() throws Exception + { + testHeaderValueWithNewLine(HttpCompliance.RFC7230); + } + + @Test + public void test2616HeaderValueWithNewLine() throws Exception + { + testHeaderValueWithNewLine(HttpCompliance.RFC2616); + } + + private void testHeaderValueWithNewLine(HttpCompliance compliance) throws Exception + { + ByteBuffer buffer= BufferUtil.toBuffer( + "GET / HTTP/1.1\r\n" + + "Host: localhost\r\n" + + "Header: va\r\n\tlue\r\n"+ + "\r\n"); + + HttpParser.RequestHandler handler = new Handler(); + HttpParser parser= new HttpParser(handler,compliance); + parseAll(parser,buffer); + + if (_bad != null) + throw new BadMessageException(_bad); + + Assert.assertTrue(_headerCompleted); + Assert.assertTrue(_messageCompleted); + Assert.assertEquals("GET", _methodOrVersion); + Assert.assertEquals("/", _uriOrStatus); + Assert.assertEquals("HTTP/1.1", _versionOrReason); + Assert.assertEquals("Host",_hdr[0]); + Assert.assertEquals("localhost",_val[0]); + Assert.assertEquals("Header",_hdr[1]); + Assert.assertEquals("va lue",_val[1]); + } + @Before public void init() {