Merge pull request #3452 from eclipse/jetty-9.4.x-3444-httpfields-add-preencoded

Jetty 9.4.x 3444 httpfields add preencoded
This commit is contained in:
Greg Wilkins 2019-03-13 11:06:12 +11:00 committed by GitHub
commit 3b7338888b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 54 additions and 14 deletions

View File

@ -19,10 +19,10 @@
package org.eclipse.jetty.http;
import static java.nio.charset.StandardCharsets.ISO_8859_1;
import java.util.Arrays;
import static java.nio.charset.StandardCharsets.ISO_8859_1;
/* ------------------------------------------------------------ */
/**
@ -60,7 +60,8 @@ public class Http1FieldPreEncoder implements HttpFieldPreEncoder
byte[] v=value.getBytes(ISO_8859_1);
byte[] bytes=Arrays.copyOf(n,n.length+2+v.length+2);
bytes[n.length]=(byte)':';
bytes[n.length]=(byte)' ';
bytes[n.length+1]=(byte)' ';
System.arraycopy(v, 0, bytes, n.length+2, v.length);
bytes[bytes.length-2]=(byte)'\r';
bytes[bytes.length-1]=(byte)'\n';

View File

@ -21,7 +21,6 @@ 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;
@ -73,7 +72,7 @@ public class PreEncodedHttpField extends HttpField
else
LOG.warn("multiple PreEncoders for "+e.getHttpVersion());
}
// Always support HTTP1
if (__encoders[0]==null)
__encoders[0] = new Http1FieldPreEncoder();

View File

@ -18,15 +18,6 @@
package org.eclipse.jetty.http;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.nio.ByteBuffer;
import java.util.Collections;
import java.util.Enumeration;
@ -40,6 +31,15 @@ import org.eclipse.jetty.util.BufferUtil;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class HttpFieldsTest
{
@Test
@ -299,6 +299,46 @@ public class HttpFieldsTest
assertEquals(false, e.hasMoreElements());
}
@Test
public void testPreEncodedField()
{
ByteBuffer buffer = BufferUtil.allocate(1024);
PreEncodedHttpField known = new PreEncodedHttpField(HttpHeader.CONNECTION, HttpHeaderValue.CLOSE.asString());
BufferUtil.clearToFill(buffer);
known.putTo(buffer,HttpVersion.HTTP_1_1);
BufferUtil.flipToFlush(buffer,0);
assertThat(BufferUtil.toString(buffer),is("Connection: close\r\n"));
PreEncodedHttpField unknown = new PreEncodedHttpField(null, "Header", "Value");
BufferUtil.clearToFill(buffer);
unknown.putTo(buffer,HttpVersion.HTTP_1_1);
BufferUtil.flipToFlush(buffer,0);
assertThat(BufferUtil.toString(buffer),is("Header: Value\r\n"));
}
@Test
public void testAddPreEncodedField()
{
final PreEncodedHttpField X_XSS_PROTECTION_FIELD = new PreEncodedHttpField("X-XSS-Protection", "1; mode=block");
HttpFields fields = new HttpFields();
fields.add(X_XSS_PROTECTION_FIELD);
assertThat("Fields output", fields.toString(), containsString("X-XSS-Protection: 1; mode=block"));
}
@Test
public void testAddFinalHttpField()
{
final HttpField X_XSS_PROTECTION_FIELD = new HttpField("X-XSS-Protection", "1; mode=block");
HttpFields fields = new HttpFields();
fields.add(X_XSS_PROTECTION_FIELD);
assertThat("Fields output", fields.toString(), containsString("X-XSS-Protection: 1; mode=block"));
}
@Test
public void testGetValues() throws Exception
{