From 81e2bfd0c7edee70274daf5e309d4861c483a6c8 Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Wed, 26 Oct 2016 11:19:47 +1100 Subject: [PATCH] Improve HttpField preencoding #1031 --- .../jetty/http/PreEncodedHttpField.java | 51 ++++++++++++++----- 1 file changed, 39 insertions(+), 12 deletions(-) diff --git a/jetty-http/src/main/java/org/eclipse/jetty/http/PreEncodedHttpField.java b/jetty-http/src/main/java/org/eclipse/jetty/http/PreEncodedHttpField.java index 6c81a914693..8a49a986660 100644 --- a/jetty-http/src/main/java/org/eclipse/jetty/http/PreEncodedHttpField.java +++ b/jetty-http/src/main/java/org/eclipse/jetty/http/PreEncodedHttpField.java @@ -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()); + } + + // Always support HTTP1 + if (__encoders[0]==null) + __encoders[0] = new Http1FieldPreEncoder(); } - private final byte[][] _encodedField=new byte[2][]; + 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)]); } } \ No newline at end of file