Improve HttpField preencoding #1031

This commit is contained in:
Greg Wilkins 2016-10-26 11:19:47 +11:00
parent 1151964d74
commit 81e2bfd0c7
1 changed files with 39 additions and 12 deletions

View File

@ -21,6 +21,7 @@ package org.eclipse.jetty.http;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.ServiceLoader;
@ -51,30 +52,56 @@ public class PreEncodedHttpField extends HttpField
{
try
{
encoders.add(iter.next());
HttpFieldPreEncoder encoder = iter.next();
if (index(encoder.getHttpVersion())>=0)
encoders.add(encoder);
}
catch(Error|RuntimeException e)
{
LOG.debug(e);
}
}
// TODO avoid needing this catch all
if (encoders.size()==0)
encoders.add(new Http1FieldPreEncoder());
LOG.debug("HttpField encoders loaded: {}",encoders);
__encoders = encoders.toArray(new HttpFieldPreEncoder[encoders.size()]);
int size=encoders.size();
__encoders = new HttpFieldPreEncoder[size==0?1:size];
for (HttpFieldPreEncoder e:encoders)
{
int i = index(e.getHttpVersion());
if (__encoders[i]==null)
__encoders[i] = e;
else
LOG.warn("multiple PreEncoders for "+e.getHttpVersion());
}
private final byte[][] _encodedField=new byte[2][];
// Always support HTTP1
if (__encoders[0]==null)
__encoders[0] = new Http1FieldPreEncoder();
}
private static int index(HttpVersion version)
{
switch (version)
{
case HTTP_1_0:
case HTTP_1_1:
return 0;
case HTTP_2:
return 1;
default:
return -1;
}
}
private final byte[][] _encodedField=new byte[__encoders.length][];
public PreEncodedHttpField(HttpHeader header,String name,String value)
{
super(header,name, value);
for (HttpFieldPreEncoder e:__encoders)
{
_encodedField[e.getHttpVersion()==HttpVersion.HTTP_2?1:0]=e.getEncodedField(header,header.asString(),value);
}
for (int i=0;i<__encoders.length;i++)
_encodedField[i]=__encoders[i].getEncodedField(header,header.asString(),value);
}
public PreEncodedHttpField(HttpHeader header,String value)
@ -89,6 +116,6 @@ public class PreEncodedHttpField extends HttpField
public void putTo(ByteBuffer bufferInFillMode, HttpVersion version)
{
bufferInFillMode.put(_encodedField[version==HttpVersion.HTTP_2?1:0]);
bufferInFillMode.put(_encodedField[index(version)]);
}
}