Issue #346 HttpParser RFC2616 Compliance mode

Reimplmented HTTP/0.9 support in RFC2616 compliance mode
This commit is contained in:
Greg Wilkins 2016-02-23 17:33:53 +01:00
parent 76689dd303
commit 3c671aa8b6
14 changed files with 191 additions and 40 deletions

View File

@ -53,6 +53,7 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.client.ssl.SslBytesTest.TLSRecord.Type;
import org.eclipse.jetty.http.HttpCompliance;
import org.eclipse.jetty.http.HttpParser;
import org.eclipse.jetty.io.Connection;
import org.eclipse.jetty.io.EndPoint;
@ -114,7 +115,7 @@ public class SslBytesServerTest extends SslBytesTest
return configure(new HttpConnection(getHttpConfiguration(), connector, endPoint,getHttpCompliance())
{
@Override
protected HttpParser newHttpParser(HttpParser.Compliance compliance)
protected HttpParser newHttpParser(HttpCompliance compliance)
{
return new HttpParser(newRequestHandler(), getHttpConfiguration().getRequestHeaderSize(),compliance)
{

View File

@ -0,0 +1,31 @@
//
// ========================================================================
// 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.http;
/**
* HTTP compliance modes:
* <dl>
* <dt>RFC7230</dt><dd>(default) Compliance with RFC7230</dd>
* <dt>RFC2616</dt><dd>Wrapped/Continued headers and HTTP/0.9 supported</dd>
* <dt>LEGACY</dt><dd>(aka STRICT) Adherence to Servlet Specification requirement for
* exact case of header names, bypassing the header caches, which are case insensitive,
* otherwise equivalent to RFC2616</dd>
*/
public enum HttpCompliance { LEGACY, RFC2616, RFC7230 }

View File

@ -359,7 +359,12 @@ public class HttpGenerator
break;
default:
throw new IllegalArgumentException(version+" not supported");
_persistent = false;
_endOfContent=EndOfContent.EOF_CONTENT;
if (BufferUtil.hasContent(content))
_contentPrepared+=content.remaining();
_state = last?State.COMPLETING:State.COMMITTED;
return Result.FLUSH;
}
// Do we need a response header

View File

@ -77,8 +77,8 @@ import static org.eclipse.jetty.http.HttpTokens.TAB;
* The parser can work in varying compliance modes:
* <dl>
* <dt>RFC7230</dt><dd>(default) Compliance with RFC7230</dd>
* <dt>RFC2616</dt><dd>Wrapped headers supported</dd>
* <dt>STRICT</dt><dd>(misnomer) Adherence to Servlet Specification requirement for
* <dt>RFC2616</dt><dd>Wrapped headers and HTTP/0.9 supported</dd>
* <dt>LEGACY</dt><dd>(aka STRICT) Adherence to Servlet Specification requirement for
* exact case of header names, bypassing the header caches, which are case insensitive,
* otherwise equivalent to RFC2616</dd>
* </p>
@ -106,9 +106,6 @@ public class HttpParser
*/
public final static Trie<HttpField> CACHE = new ArrayTrie<>(2048);
// Compliance
public enum Compliance { STRICT, RFC2616, RFC7230 };
// States
public enum State
{
@ -146,7 +143,7 @@ public class HttpParser
private final RequestHandler _requestHandler;
private final ResponseHandler _responseHandler;
private final int _maxHeaderBytes;
private final Compliance _compliance;
private final HttpCompliance _compliance;
private HttpField _field;
private HttpHeader _header;
private String _headerString;
@ -226,10 +223,10 @@ public class HttpParser
CACHE.put(new HttpField(HttpHeader.COOKIE,(String)null));
}
private static Compliance compliance()
private static HttpCompliance compliance()
{
Boolean strict = Boolean.getBoolean(__STRICT);
return strict?Compliance.STRICT:Compliance.RFC7230;
return strict?HttpCompliance.LEGACY:HttpCompliance.RFC7230;
}
/* ------------------------------------------------------------------------------- */
@ -260,18 +257,24 @@ public class HttpParser
@Deprecated
public HttpParser(RequestHandler handler,int maxHeaderBytes,boolean strict)
{
this(handler,maxHeaderBytes,strict?Compliance.STRICT:compliance());
this(handler,maxHeaderBytes,strict?HttpCompliance.LEGACY:compliance());
}
/* ------------------------------------------------------------------------------- */
@Deprecated
public HttpParser(ResponseHandler handler,int maxHeaderBytes,boolean strict)
{
this(handler,maxHeaderBytes,strict?Compliance.STRICT:compliance());
this(handler,maxHeaderBytes,strict?HttpCompliance.LEGACY:compliance());
}
/* ------------------------------------------------------------------------------- */
public HttpParser(RequestHandler handler,int maxHeaderBytes,Compliance compliance)
public HttpParser(RequestHandler handler,HttpCompliance compliance)
{
this(handler,-1,compliance);
}
/* ------------------------------------------------------------------------------- */
public HttpParser(RequestHandler handler,int maxHeaderBytes,HttpCompliance compliance)
{
_handler=handler;
_requestHandler=handler;
@ -281,7 +284,7 @@ public class HttpParser
}
/* ------------------------------------------------------------------------------- */
public HttpParser(ResponseHandler handler,int maxHeaderBytes,Compliance compliance)
public HttpParser(ResponseHandler handler,int maxHeaderBytes,HttpCompliance compliance)
{
_handler=handler;
_requestHandler=null;
@ -583,7 +586,7 @@ public class HttpParser
_length=_string.length();
_methodString=takeString();
HttpMethod method=HttpMethod.CACHE.get(_methodString);
if (method!=null && _compliance!=Compliance.STRICT)
if (method!=null && _compliance!=HttpCompliance.LEGACY)
_methodString=method.asString();
setState(State.SPACE1);
}
@ -685,7 +688,15 @@ public class HttpParser
else if (ch < HttpTokens.SPACE && ch>=0)
{
// HTTP/0.9
throw new BadMessageException("HTTP/0.9 not supported");
if (_compliance.ordinal()>=HttpCompliance.RFC7230.ordinal())
throw new BadMessageException("HTTP/0.9 not supported");
handle=_requestHandler.startRequest(_methodString,_uri.toString(), HttpVersion.HTTP_0_9);
setState(State.END);
BufferUtil.clear(buffer);
handle=_handler.headerComplete()||handle;
handle=_handler.messageComplete()||handle;
return handle;
}
else
{
@ -747,7 +758,15 @@ public class HttpParser
else
{
// HTTP/0.9
throw new BadMessageException("HTTP/0.9 not supported");
if (_compliance.ordinal()>=HttpCompliance.RFC7230.ordinal())
throw new BadMessageException("HTTP/0.9 not supported");
handle=_requestHandler.startRequest(_methodString,_uri.toString(), HttpVersion.HTTP_0_9);
setState(State.END);
BufferUtil.clear(buffer);
handle=_handler.headerComplete()||handle;
handle=_handler.messageComplete()||handle;
return handle;
}
}
else if (ch<0)
@ -859,7 +878,7 @@ public class HttpParser
_host=true;
if (!(_field instanceof HostPortHttpField))
{
_field=new HostPortHttpField(_header,_compliance==Compliance.STRICT?_headerString:_header.asString(),_valueString);
_field=new HostPortHttpField(_header,_compliance==HttpCompliance.LEGACY?_headerString:_header.asString(),_valueString);
add_to_connection_trie=_connectionFields!=null;
}
break;
@ -888,7 +907,7 @@ public class HttpParser
if (add_to_connection_trie && !_connectionFields.isFull() && _header!=null && _valueString!=null)
{
if (_field==null)
_field=new HttpField(_header,_compliance==Compliance.STRICT?_headerString:_header.asString(),_valueString);
_field=new HttpField(_header,_compliance==HttpCompliance.LEGACY?_headerString:_header.asString(),_valueString);
_connectionFields.put(_field);
}
}
@ -933,7 +952,7 @@ public class HttpParser
case HttpTokens.SPACE:
case HttpTokens.TAB:
{
if (_compliance.ordinal()>=Compliance.RFC7230.ordinal())
if (_compliance.ordinal()>=HttpCompliance.RFC7230.ordinal())
throw new BadMessageException(HttpStatus.BAD_REQUEST_400,"Bad Continuation");
// header value without name - continuation?
@ -1035,7 +1054,7 @@ public class HttpParser
final String n;
final String v;
if (_compliance==Compliance.STRICT)
if (_compliance==HttpCompliance.LEGACY)
{
// Have to get the fields exactly from the buffer to match case
String fn=field.getName();

View File

@ -32,6 +32,41 @@ import org.junit.Test;
public class HttpGeneratorServerTest
{
@Test
public void test_0_9() throws Exception
{
ByteBuffer header = BufferUtil.allocate(8096);
ByteBuffer content = BufferUtil.toBuffer("0123456789");
HttpGenerator gen = new HttpGenerator();
HttpGenerator.Result result = gen.generateResponse(null, null, null, content, true);
assertEquals(HttpGenerator.Result.NEED_INFO, result);
assertEquals(HttpGenerator.State.START, gen.getState());
MetaData.Response info = new MetaData.Response(HttpVersion.HTTP_0_9, 200, null, new HttpFields(), 10);
info.getFields().add("Content-Type", "test/data");
info.getFields().add("Last-Modified", DateGenerator.__01Jan1970);
result = gen.generateResponse(info, null, null, content, true);
assertEquals(HttpGenerator.Result.FLUSH, result);
assertEquals(HttpGenerator.State.COMPLETING, gen.getState());
String response = BufferUtil.toString(header);
BufferUtil.clear(header);
response += BufferUtil.toString(content);
BufferUtil.clear(content);
result = gen.generateResponse(null, null, null, content, false);
assertEquals(HttpGenerator.Result.SHUTDOWN_OUT, result);
assertEquals(HttpGenerator.State.END, gen.getState());
assertEquals(10, gen.getContentPrepared());
assertThat(response, not(containsString("200 OK")));
assertThat(response, not(containsString("Last-Modified: Thu, 01 Jan 1970 00:00:00 GMT")));
assertThat(response, not(containsString("Content-Length: 10")));
assertThat(response, containsString("0123456789"));
}
@Test
public void testSimple() throws Exception

View File

@ -114,19 +114,49 @@ public class HttpParserTest
assertEquals(-1, _headers);
}
@Test
public void testLineParse1_RFC2616() throws Exception
{
ByteBuffer buffer= BufferUtil.toBuffer("GET /999\015\012");
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);
}
@Test
public void testLineParse1() throws Exception
{
ByteBuffer buffer= BufferUtil.toBuffer("GET /999\015\012");
_versionOrReason= null;
HttpParser.RequestHandler handler = new Handler();
HttpParser parser= new HttpParser(handler);
parseAll(parser,buffer);
assertEquals("HTTP/0.9 not supported", _bad);
}
@Test
public void testLineParse2_RFC2616() throws Exception
{
ByteBuffer buffer= BufferUtil.toBuffer("POST /222 \015\012");
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);
}
@Test
public void testLineParse2() throws Exception
{
@ -136,7 +166,7 @@ public class HttpParserTest
HttpParser.RequestHandler handler = new Handler();
HttpParser parser= new HttpParser(handler);
parseAll(parser,buffer);
assertEquals("HTTP/0.9 not supported", _bad);
assertEquals("HTTP/0.9 not supported", _bad);
}
@Test
@ -230,7 +260,7 @@ public class HttpParserTest
"\015\012");
HttpParser.RequestHandler handler = new Handler();
HttpParser parser= new HttpParser(handler,4096,HttpParser.Compliance.RFC2616);
HttpParser parser= new HttpParser(handler,4096,HttpCompliance.RFC2616);
parseAll(parser,buffer);
Assert.assertThat(_bad,Matchers.nullValue());
@ -252,7 +282,7 @@ public class HttpParserTest
"\015\012");
HttpParser.RequestHandler handler = new Handler();
HttpParser parser= new HttpParser(handler,4096,HttpParser.Compliance.RFC7230);
HttpParser parser= new HttpParser(handler,4096,HttpCompliance.RFC7230);
parseAll(parser,buffer);
Assert.assertThat(_bad,Matchers.notNullValue());
@ -602,7 +632,7 @@ public class HttpParserTest
"cOnNeCtIoN: ClOsE\015\012"+
"\015\012");
HttpParser.RequestHandler handler = new Handler();
HttpParser parser= new HttpParser(handler,-1,HttpParser.Compliance.RFC7230);
HttpParser parser= new HttpParser(handler,-1,HttpCompliance.RFC7230);
parseAll(parser,buffer);
assertNull(_bad);
assertEquals("GET", _methodOrVersion);
@ -624,7 +654,7 @@ public class HttpParserTest
"cOnNeCtIoN: ClOsE\015\012"+
"\015\012");
HttpParser.RequestHandler handler = new Handler();
HttpParser parser= new HttpParser(handler,-1,HttpParser.Compliance.STRICT);
HttpParser parser= new HttpParser(handler,-1,HttpCompliance.LEGACY);
parseAll(parser,buffer);
assertNull(_bad);
assertEquals("gEt", _methodOrVersion);

View File

@ -29,6 +29,7 @@
<Item>
<New class="org.eclipse.jetty.server.HttpConnectionFactory">
<Arg name="config"><Ref refid="httpConfig" /></Arg>
<Arg name="compliance"><Call class="org.eclipse.jetty.http.HttpCompliance" name="valueOf"><Arg><Property name="jetty.http.compliance" default="RFC7230"/></Arg></Call></Arg>
</New>
</Item>
</Array>

View File

@ -21,6 +21,7 @@
<Arg>
<New class="org.eclipse.jetty.server.HttpConnectionFactory">
<Arg name="config"><Ref refid="sslHttpConfig" /></Arg>
<Arg name="compliance"><Call class="org.eclipse.jetty.http.HttpCompliance" name="valueOf"><Arg><Property name="jetty.http.compliance" default="RFC7230"/></Arg></Call></Arg>
</New>
</Arg>
</Call>

View File

@ -34,3 +34,6 @@ etc/jetty-http.xml
## Thread priority delta to give to acceptor threads
# jetty.http.acceptorPriorityDelta=0
## HTTP Compliance: RFC7230, RFC2616, LEGACY
# jetty.http.compliance=RFC7230

View File

@ -260,6 +260,11 @@ class HttpChannelOverHttp extends HttpChannel implements HttpParser.RequestHandl
switch (_metadata.getVersion())
{
case HTTP_0_9:
{
persistent=false;
break;
}
case HTTP_1_0:
{
if (getHttpConfiguration().isPersistentConnectionsEnabled())
@ -337,7 +342,7 @@ class HttpChannelOverHttp extends HttpChannel implements HttpParser.RequestHandl
default:
{
throw new IllegalStateException();
throw new IllegalStateException("unsupported version "+_metadata.getVersion());
}
}

View File

@ -24,6 +24,7 @@ import java.nio.channels.WritePendingException;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.atomic.AtomicInteger;
import org.eclipse.jetty.http.HttpCompliance;
import org.eclipse.jetty.http.HttpField;
import org.eclipse.jetty.http.HttpGenerator;
import org.eclipse.jetty.http.HttpHeader;
@ -90,7 +91,7 @@ public class HttpConnection extends AbstractConnection implements Runnable, Http
return last;
}
public HttpConnection(HttpConfiguration config, Connector connector, EndPoint endPoint, HttpParser.Compliance compliance)
public HttpConnection(HttpConfiguration config, Connector connector, EndPoint endPoint, HttpCompliance compliance)
{
super(endPoint, connector.getExecutor());
_config = config;
@ -119,7 +120,7 @@ public class HttpConnection extends AbstractConnection implements Runnable, Http
return new HttpChannelOverHttp(this, _connector, _config, getEndPoint(), this);
}
protected HttpParser newHttpParser(HttpParser.Compliance compliance)
protected HttpParser newHttpParser(HttpCompliance compliance)
{
return new HttpParser(newRequestHandler(), getHttpConfiguration().getRequestHeaderSize(), compliance);
}

View File

@ -18,7 +18,7 @@
package org.eclipse.jetty.server;
import org.eclipse.jetty.http.HttpParser;
import org.eclipse.jetty.http.HttpCompliance;
import org.eclipse.jetty.http.HttpVersion;
import org.eclipse.jetty.io.Connection;
import org.eclipse.jetty.io.EndPoint;
@ -32,17 +32,23 @@ import org.eclipse.jetty.util.annotation.Name;
public class HttpConnectionFactory extends AbstractConnectionFactory implements HttpConfiguration.ConnectionFactory
{
private final HttpConfiguration _config;
private HttpParser.Compliance _httpCompliance=HttpParser.Compliance.RFC7230;
private HttpCompliance _httpCompliance;
public HttpConnectionFactory()
{
this(new HttpConfiguration());
}
public HttpConnectionFactory(@Name("config") HttpConfiguration config)
{
this(config,null);
}
public HttpConnectionFactory(@Name("config") HttpConfiguration config, @Name("compliance") HttpCompliance compliance)
{
super(HttpVersion.HTTP_1_1.asString());
_config=config;
_httpCompliance=compliance==null?HttpCompliance.RFC7230:compliance;
if (config==null)
throw new IllegalArgumentException("Null HttpConfiguration");
addBean(_config);
@ -54,15 +60,15 @@ public class HttpConnectionFactory extends AbstractConnectionFactory implements
return _config;
}
public HttpParser.Compliance getHttpCompliance()
public HttpCompliance getHttpCompliance()
{
return _httpCompliance;
}
/**
* @param httpCompliance String value of {@link HttpParser.Compliance}
* @param httpCompliance String value of {@link HttpCompliance}
*/
public void setHttpCompliance(HttpParser.Compliance httpCompliance)
public void setHttpCompliance(HttpCompliance httpCompliance)
{
_httpCompliance = httpCompliance;
}

View File

@ -29,7 +29,7 @@ import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.http.HttpParser;
import org.eclipse.jetty.http.HttpCompliance;
import org.eclipse.jetty.http.HttpVersion;
import org.eclipse.jetty.io.Connection;
import org.eclipse.jetty.io.EndPoint;
@ -93,7 +93,7 @@ public class ExtendedServerTest extends HttpServerTestBase
private static class ExtendedHttpConnection extends HttpConnection
{
public ExtendedHttpConnection(HttpConfiguration config, Connector connector, EndPoint endPoint, HttpParser.Compliance compliance)
public ExtendedHttpConnection(HttpConfiguration config, Connector connector, EndPoint endPoint, HttpCompliance compliance)
{
super(config,connector,endPoint,compliance);
}

View File

@ -42,6 +42,7 @@ import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.http.HttpCompliance;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.http.HttpParser;
import org.eclipse.jetty.http.MimeTypes;
@ -236,6 +237,18 @@ public class HttpConnectionTest
checkContains(response,0,"HTTP/1.1 400 Bad URI");
}
@Test
public void test_0_9() throws Exception
{
connector.getConnectionFactory(HttpConnectionFactory.class).setHttpCompliance(HttpCompliance.RFC2616);
String response=connector.getResponses("GET /R1\n");
int offset=0;
checkNotContained(response,offset,"HTTP/1.1");
checkNotContained(response,offset,"200");
checkContains(response,offset,"pathInfo=/R1");
}
@Test
public void testSimple() throws Exception
{