Issue #423 (Duplicate Content-Length header not handled correctly)
Refactored tests into existing test class.
This commit is contained in:
parent
5f2e2820f4
commit
0b82129cd8
|
@ -18,12 +18,6 @@
|
||||||
|
|
||||||
package org.eclipse.jetty.http;
|
package org.eclipse.jetty.http;
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
@ -36,12 +30,14 @@ import org.junit.Assert;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
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
|
public class HttpParserTest
|
||||||
{
|
{
|
||||||
/* ------------------------------------------------------------------------------- */
|
|
||||||
/**
|
/**
|
||||||
* Parse until {@link State#END} state.
|
* Parse until {@link State#END} state.
|
||||||
* If the parser is already in the END state, then it is {@link HttpParser#reset()} and re-parsed.
|
* If the parser is already in the END state, then it is {@link HttpParser#reset()} and re-parsed.
|
||||||
|
@ -462,8 +458,6 @@ public class HttpParserTest
|
||||||
assertEquals(9, _headers);
|
assertEquals(9, _headers);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testHeaderParseLF() throws Exception
|
public void testHeaderParseLF() throws Exception
|
||||||
{
|
{
|
||||||
|
@ -561,8 +555,6 @@ public class HttpParserTest
|
||||||
assertEquals(null,_bad);
|
assertEquals(null,_bad);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testBadMethodEncoding() throws Exception
|
public void testBadMethodEncoding() throws Exception
|
||||||
{
|
{
|
||||||
|
@ -587,7 +579,6 @@ public class HttpParserTest
|
||||||
assertThat(_bad,Matchers.notNullValue());
|
assertThat(_bad,Matchers.notNullValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testBadHeaderEncoding() throws Exception
|
public void testBadHeaderEncoding() throws Exception
|
||||||
{
|
{
|
||||||
|
@ -720,7 +711,6 @@ public class HttpParserTest
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testChunkParse() throws Exception
|
public void testChunkParse() throws Exception
|
||||||
{
|
{
|
||||||
|
@ -811,7 +801,6 @@ public class HttpParserTest
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMultiParse() throws Exception
|
public void testMultiParse() throws Exception
|
||||||
{
|
{
|
||||||
|
@ -877,7 +866,6 @@ public class HttpParserTest
|
||||||
assertEquals("0123456789", _content);
|
assertEquals("0123456789", _content);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMultiParseEarlyEOF() throws Exception
|
public void testMultiParseEarlyEOF() throws Exception
|
||||||
{
|
{
|
||||||
|
@ -1021,7 +1009,6 @@ public class HttpParserTest
|
||||||
assertTrue(_messageCompleted);
|
assertTrue(_messageCompleted);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testResponseParse3() throws Exception
|
public void testResponseParse3() throws Exception
|
||||||
{
|
{
|
||||||
|
@ -1158,8 +1145,6 @@ public class HttpParserTest
|
||||||
assertEquals(HttpParser.State.CLOSED,parser.getState());
|
assertEquals(HttpParser.State.CLOSED,parser.getState());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testNoURI() throws Exception
|
public void testNoURI() throws Exception
|
||||||
{
|
{
|
||||||
|
@ -1182,7 +1167,6 @@ public class HttpParserTest
|
||||||
assertEquals(HttpParser.State.CLOSED,parser.getState());
|
assertEquals(HttpParser.State.CLOSED,parser.getState());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testNoURI2() throws Exception
|
public void testNoURI2() throws Exception
|
||||||
{
|
{
|
||||||
|
@ -1351,9 +1335,6 @@ public class HttpParserTest
|
||||||
assertEquals(HttpParser.State.CLOSED,parser.getState());
|
assertEquals(HttpParser.State.CLOSED,parser.getState());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testBadContentLength0() throws Exception
|
public void testBadContentLength0() throws Exception
|
||||||
{
|
{
|
||||||
|
@ -1368,7 +1349,7 @@ public class HttpParserTest
|
||||||
|
|
||||||
parser.parseNext(buffer);
|
parser.parseNext(buffer);
|
||||||
assertEquals("GET",_methodOrVersion);
|
assertEquals("GET",_methodOrVersion);
|
||||||
assertEquals("Bad Content-Length",_bad);
|
assertEquals("Invalid Content-Length Value",_bad);
|
||||||
assertFalse(buffer.hasRemaining());
|
assertFalse(buffer.hasRemaining());
|
||||||
assertEquals(HttpParser.State.CLOSE,parser.getState());
|
assertEquals(HttpParser.State.CLOSE,parser.getState());
|
||||||
parser.atEOF();
|
parser.atEOF();
|
||||||
|
@ -1390,7 +1371,7 @@ public class HttpParserTest
|
||||||
|
|
||||||
parser.parseNext(buffer);
|
parser.parseNext(buffer);
|
||||||
assertEquals("GET",_methodOrVersion);
|
assertEquals("GET",_methodOrVersion);
|
||||||
assertEquals("Bad Content-Length",_bad);
|
assertEquals("Invalid Content-Length Value",_bad);
|
||||||
assertFalse(buffer.hasRemaining());
|
assertFalse(buffer.hasRemaining());
|
||||||
assertEquals(HttpParser.State.CLOSE,parser.getState());
|
assertEquals(HttpParser.State.CLOSE,parser.getState());
|
||||||
parser.atEOF();
|
parser.atEOF();
|
||||||
|
@ -1412,7 +1393,7 @@ public class HttpParserTest
|
||||||
|
|
||||||
parser.parseNext(buffer);
|
parser.parseNext(buffer);
|
||||||
assertEquals("GET",_methodOrVersion);
|
assertEquals("GET",_methodOrVersion);
|
||||||
assertEquals("Bad Content-Length",_bad);
|
assertEquals("Invalid Content-Length Value",_bad);
|
||||||
assertFalse(buffer.hasRemaining());
|
assertFalse(buffer.hasRemaining());
|
||||||
assertEquals(HttpParser.State.CLOSE,parser.getState());
|
assertEquals(HttpParser.State.CLOSE,parser.getState());
|
||||||
parser.atEOF();
|
parser.atEOF();
|
||||||
|
@ -1420,6 +1401,108 @@ public class HttpParserTest
|
||||||
assertEquals(HttpParser.State.CLOSED,parser.getState());
|
assertEquals(HttpParser.State.CLOSED,parser.getState());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDuplicateContentLengthWithLargerThenCorrectValue()
|
||||||
|
{
|
||||||
|
ByteBuffer buffer= BufferUtil.toBuffer(
|
||||||
|
"POST / HTTP/1.1\015\012"
|
||||||
|
+ "Content-Length: 2\015\012"
|
||||||
|
+ "Content-Length: 1\015\012"
|
||||||
|
+ "Connection: close\015\012"
|
||||||
|
+ "\015\012"
|
||||||
|
+ "X");
|
||||||
|
|
||||||
|
HttpParser.RequestHandler handler = new Handler();
|
||||||
|
HttpParser parser=new HttpParser(handler);
|
||||||
|
|
||||||
|
parser.parseNext(buffer);
|
||||||
|
assertEquals("POST",_methodOrVersion);
|
||||||
|
assertEquals("Duplicate Content-Length",_bad);
|
||||||
|
assertFalse(buffer.hasRemaining());
|
||||||
|
assertEquals(HttpParser.State.CLOSE,parser.getState());
|
||||||
|
parser.atEOF();
|
||||||
|
parser.parseNext(BufferUtil.EMPTY_BUFFER);
|
||||||
|
assertEquals(HttpParser.State.CLOSED,parser.getState());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDuplicateContentLengthWithCorrectThenLargerValue()
|
||||||
|
{
|
||||||
|
ByteBuffer buffer= BufferUtil.toBuffer(
|
||||||
|
"POST / HTTP/1.1\015\012"
|
||||||
|
+ "Content-Length: 1\015\012"
|
||||||
|
+ "Content-Length: 2\015\012"
|
||||||
|
+ "Connection: close\015\012"
|
||||||
|
+ "\015\012"
|
||||||
|
+ "X");
|
||||||
|
|
||||||
|
HttpParser.RequestHandler handler = new Handler();
|
||||||
|
HttpParser parser=new HttpParser(handler);
|
||||||
|
|
||||||
|
parser.parseNext(buffer);
|
||||||
|
assertEquals("POST",_methodOrVersion);
|
||||||
|
assertEquals("Duplicate Content-Length",_bad);
|
||||||
|
assertFalse(buffer.hasRemaining());
|
||||||
|
assertEquals(HttpParser.State.CLOSE,parser.getState());
|
||||||
|
parser.atEOF();
|
||||||
|
parser.parseNext(BufferUtil.EMPTY_BUFFER);
|
||||||
|
assertEquals(HttpParser.State.CLOSED,parser.getState());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testTransferEncodingChunkedThenContentLength()
|
||||||
|
{
|
||||||
|
ByteBuffer buffer= BufferUtil.toBuffer(
|
||||||
|
"POST /chunk HTTP/1.1\015\012"
|
||||||
|
+ "Host: localhost\015\012"
|
||||||
|
+ "Transfer-Encoding: chunked\015\012"
|
||||||
|
+ "Content-Length: 1\015\012"
|
||||||
|
+ "\015\012"
|
||||||
|
+ "1\015\012"
|
||||||
|
+ "X\015\012"
|
||||||
|
+ "0\015\012"
|
||||||
|
+ "\015\012");
|
||||||
|
|
||||||
|
HttpParser.RequestHandler handler = new Handler();
|
||||||
|
HttpParser parser=new HttpParser(handler);
|
||||||
|
parseAll(parser,buffer);
|
||||||
|
|
||||||
|
assertEquals("POST", _methodOrVersion);
|
||||||
|
assertEquals("/chunk", _uriOrStatus);
|
||||||
|
assertEquals("HTTP/1.1", _versionOrReason);
|
||||||
|
assertEquals("X", _content);
|
||||||
|
|
||||||
|
assertTrue(_headerCompleted);
|
||||||
|
assertTrue(_messageCompleted);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testContentLengthThenTransferEncodingChunked()
|
||||||
|
{
|
||||||
|
ByteBuffer buffer= BufferUtil.toBuffer(
|
||||||
|
"POST /chunk HTTP/1.1\015\012"
|
||||||
|
+ "Host: localhost\015\012"
|
||||||
|
+ "Content-Length: 1\015\012"
|
||||||
|
+ "Transfer-Encoding: chunked\015\012"
|
||||||
|
+ "\015\012"
|
||||||
|
+ "1\015\012"
|
||||||
|
+ "X\015\012"
|
||||||
|
+ "0\015\012"
|
||||||
|
+ "\015\012");
|
||||||
|
|
||||||
|
HttpParser.RequestHandler handler = new Handler();
|
||||||
|
HttpParser parser=new HttpParser(handler);
|
||||||
|
parseAll(parser,buffer);
|
||||||
|
|
||||||
|
assertEquals("POST", _methodOrVersion);
|
||||||
|
assertEquals("/chunk", _uriOrStatus);
|
||||||
|
assertEquals("HTTP/1.1", _versionOrReason);
|
||||||
|
assertEquals("X", _content);
|
||||||
|
|
||||||
|
assertTrue(_headerCompleted);
|
||||||
|
assertTrue(_messageCompleted);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testHost() throws Exception
|
public void testHost() throws Exception
|
||||||
{
|
{
|
||||||
|
@ -1642,7 +1725,6 @@ public class HttpParserTest
|
||||||
assertEquals("unknown",_val[4]);
|
assertEquals("unknown",_val[4]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testHTTP2Preface() throws Exception
|
public void testHTTP2Preface() throws Exception
|
||||||
{
|
{
|
||||||
|
@ -1665,7 +1747,6 @@ public class HttpParserTest
|
||||||
assertEquals(null, _bad);
|
assertEquals(null, _bad);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void init()
|
public void init()
|
||||||
{
|
{
|
||||||
|
@ -1692,7 +1773,6 @@ public class HttpParserTest
|
||||||
private String[] _hdr;
|
private String[] _hdr;
|
||||||
private String[] _val;
|
private String[] _val;
|
||||||
private int _headers;
|
private int _headers;
|
||||||
|
|
||||||
private boolean _early;
|
private boolean _early;
|
||||||
private boolean _headerCompleted;
|
private boolean _headerCompleted;
|
||||||
private boolean _messageCompleted;
|
private boolean _messageCompleted;
|
||||||
|
|
|
@ -1,158 +0,0 @@
|
||||||
//
|
|
||||||
// ========================================================================
|
|
||||||
// Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
|
|
||||||
// ------------------------------------------------------------------------
|
|
||||||
// All rights reserved. This program and the accompanying materials
|
|
||||||
// are made available under the terms of the Eclipse Public License v1.0
|
|
||||||
// and Apache License v2.0 which accompanies this distribution.
|
|
||||||
//
|
|
||||||
// The Eclipse Public License is available at
|
|
||||||
// http://www.eclipse.org/legal/epl-v10.html
|
|
||||||
//
|
|
||||||
// The Apache License v2.0 is available at
|
|
||||||
// http://www.opensource.org/licenses/apache2.0.php
|
|
||||||
//
|
|
||||||
// You may elect to redistribute this code under either of these licenses.
|
|
||||||
// ========================================================================
|
|
||||||
//
|
|
||||||
|
|
||||||
package org.eclipse.jetty.server;
|
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.io.OutputStream;
|
|
||||||
import java.net.Socket;
|
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
|
|
||||||
import javax.servlet.ServletException;
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
|
||||||
import javax.servlet.http.HttpServletResponse;
|
|
||||||
|
|
||||||
import org.eclipse.jetty.http.HttpStatus;
|
|
||||||
import org.eclipse.jetty.server.handler.AbstractHandler;
|
|
||||||
import org.eclipse.jetty.toolchain.test.http.SimpleHttpParser;
|
|
||||||
import org.eclipse.jetty.toolchain.test.http.SimpleHttpResponse;
|
|
||||||
import org.eclipse.jetty.util.IO;
|
|
||||||
import org.junit.After;
|
|
||||||
import org.junit.Assert;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
public class ContentLengthTest
|
|
||||||
{
|
|
||||||
private Server server;
|
|
||||||
private ServerConnector connector;
|
|
||||||
|
|
||||||
private void startServer(Handler handler) throws Exception
|
|
||||||
{
|
|
||||||
server = new Server();
|
|
||||||
connector = new ServerConnector(server);
|
|
||||||
server.addConnector(connector);
|
|
||||||
server.setHandler(handler);
|
|
||||||
server.start();
|
|
||||||
}
|
|
||||||
|
|
||||||
@After
|
|
||||||
public void dispose() throws Exception
|
|
||||||
{
|
|
||||||
if (server != null)
|
|
||||||
server.stop();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testDuplicateContentLengthWithLargerAndCorrectValue() throws Exception
|
|
||||||
{
|
|
||||||
String content = "hello_world";
|
|
||||||
testDuplicateContentLength(content, 2 * content.length(), content.length());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testDuplicateContentLengthWithCorrectAndLargerValue() throws Exception
|
|
||||||
{
|
|
||||||
String content = "hello_world";
|
|
||||||
testDuplicateContentLength(content, content.length(), 2 * content.length());
|
|
||||||
}
|
|
||||||
|
|
||||||
private void testDuplicateContentLength(String content, long length1, long length2) throws Exception
|
|
||||||
{
|
|
||||||
startServer(new AbstractHandler()
|
|
||||||
{
|
|
||||||
@Override
|
|
||||||
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
|
|
||||||
{
|
|
||||||
baseRequest.setHandled(true);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
try (Socket client = new Socket("localhost", connector.getLocalPort()))
|
|
||||||
{
|
|
||||||
String request = "" +
|
|
||||||
"POST / HTTP/1.1\r\n" +
|
|
||||||
"Host: localhost\r\n" +
|
|
||||||
"Content-Length: " + length1 + "\r\n" +
|
|
||||||
"Content-Length: " + length2 + "\r\n" +
|
|
||||||
"\r\n" +
|
|
||||||
content;
|
|
||||||
OutputStream output = client.getOutputStream();
|
|
||||||
output.write(request.getBytes(StandardCharsets.UTF_8));
|
|
||||||
output.flush();
|
|
||||||
|
|
||||||
SimpleHttpParser parser = new SimpleHttpParser();
|
|
||||||
BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream(), StandardCharsets.UTF_8));
|
|
||||||
SimpleHttpResponse response = parser.readResponse(reader);
|
|
||||||
|
|
||||||
Assert.assertEquals(HttpStatus.BAD_REQUEST_400, Integer.parseInt(response.getCode()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testTransferEncodingChunkedBeforeContentLength() throws Exception
|
|
||||||
{
|
|
||||||
String content = "hello_world";
|
|
||||||
testTransferEncodingChunkedAndContentLength(content, "Transfer-Encoding: chunked", "Content-Length: " + content.length());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testContentLengthBeforeTransferEncodingChunked() throws Exception
|
|
||||||
{
|
|
||||||
String content = "hello_world";
|
|
||||||
testTransferEncodingChunkedAndContentLength(content, "Content-Length: " + content.length(), "Transfer-Encoding: chunked");
|
|
||||||
}
|
|
||||||
|
|
||||||
private void testTransferEncodingChunkedAndContentLength(String content, String header1, String header2) throws Exception
|
|
||||||
{
|
|
||||||
startServer(new AbstractHandler()
|
|
||||||
{
|
|
||||||
@Override
|
|
||||||
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
|
|
||||||
{
|
|
||||||
baseRequest.setHandled(true);
|
|
||||||
String body = IO.toString(request.getInputStream());
|
|
||||||
Assert.assertEquals(content, body);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
try (Socket client = new Socket("localhost", connector.getLocalPort()))
|
|
||||||
{
|
|
||||||
String request = "" +
|
|
||||||
"POST / HTTP/1.1\r\n" +
|
|
||||||
"Host: localhost\r\n" +
|
|
||||||
header1 + "\r\n" +
|
|
||||||
header2 + "\r\n" +
|
|
||||||
"\r\n" +
|
|
||||||
Integer.toHexString(content.length()) + "\r\n" +
|
|
||||||
content +
|
|
||||||
"0\r\n" +
|
|
||||||
"\r\n";
|
|
||||||
OutputStream output = client.getOutputStream();
|
|
||||||
output.write(request.getBytes(StandardCharsets.UTF_8));
|
|
||||||
output.flush();
|
|
||||||
|
|
||||||
SimpleHttpParser parser = new SimpleHttpParser();
|
|
||||||
BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream(), StandardCharsets.UTF_8));
|
|
||||||
SimpleHttpResponse response = parser.readResponse(reader);
|
|
||||||
|
|
||||||
Assert.assertEquals(HttpStatus.OK_200, Integer.parseInt(response.getCode()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue