diff src/org/eclipse/jetty/http/HttpFields.java @ 1022:3718afd99988

HttpHeaders uses StringCache
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 01 Nov 2016 01:04:46 -0600
parents e350c11242be
children eca26899c4bc
line wrap: on
line diff
--- a/src/org/eclipse/jetty/http/HttpFields.java	Mon Oct 31 22:49:25 2016 -0600
+++ b/src/org/eclipse/jetty/http/HttpFields.java	Tue Nov 01 01:04:46 2016 -0600
@@ -40,8 +40,6 @@
 import java.util.concurrent.ConcurrentMap;
 
 import org.eclipse.jetty.io.Buffer;
-import org.eclipse.jetty.io.BufferCache;
-import org.eclipse.jetty.io.BufferCache.CachedBuffer;
 import org.eclipse.jetty.io.BufferUtil;
 import org.eclipse.jetty.io.ByteArrayBuffer;
 import org.eclipse.jetty.util.LazyList;
@@ -267,7 +265,6 @@
 	};
 
 	private final static String __01Jan1970 = formatDate(0);
-	private final static Buffer __01Jan1970_BUFFER = new ByteArrayBuffer(__01Jan1970);
 	private final static String __01Jan1970_COOKIE = formatCookieDate(0).trim();
 
 
@@ -275,21 +272,9 @@
 
 
 
-	private final ArrayList<Field> _fields = new ArrayList<Field>(20);
-	private final HashMap<Buffer,Field> _names = new HashMap<Buffer,Field>(32);
-/*
-	private Buffer convertValue(String value)
-	{
-		try
-		{   
-			return new ByteArrayBuffer(value,StringUtil.__ISO_8859_1);
-		}
-		catch (UnsupportedEncodingException e)
-		{
-			throw new RuntimeException(e);
-		}
-	}
-*/
+	private final ArrayList<Field> _fields = new ArrayList<Field>();
+	private final HashMap<String,Field> _names = new HashMap<String,Field>();
+
 	
 	/* -------------------------------------------------------------- */
 	/**
@@ -346,28 +331,14 @@
 		return _fields.get(i);
 	}
 
-	/* ------------------------------------------------------------ */
 	private Field getField(String name)
 	{
-		return _names.get(HttpHeaders.CACHE.lookup(name));
+		return _names.get(name.toLowerCase());
 	}
 
-	/* ------------------------------------------------------------ */
-	private Field getField(Buffer name)
-	{
-		return _names.get(HttpHeaders.CACHE.lookup(name));
-	}
-
-	/* ------------------------------------------------------------ */
-	public boolean containsKey(Buffer name)
-	{
-		return _names.containsKey(HttpHeaders.CACHE.lookup(name));
-	}
-
-	/* ------------------------------------------------------------ */
 	public boolean containsKey(String name)
 	{
-		return _names.containsKey(HttpHeaders.CACHE.lookup(name));
+		return _names.containsKey(name.toLowerCase());
 	}
 
 	/* -------------------------------------------------------------- */
@@ -379,19 +350,7 @@
 	public String getStringField(String name)
 	{
 		Field field = getField(name);
-		return field==null?null:field.getValue();
-	}
-
-	/* -------------------------------------------------------------- */
-	/**
-	 * @return the value of a field, or null if not found. For multiple fields of the same name,
-	 *         only the first is returned.
-	 * @param name the case-insensitive field name
-	 */
-	public String getStringField(Buffer name)
-	{
-		Field field = getField(name);
-		return field==null?null:field.getValue();
+		return field==null ? null : field.getValue();
 	}
 
 	/* -------------------------------------------------------------- */
@@ -454,41 +413,6 @@
 
 	/* -------------------------------------------------------------- */
 	/**
-	 * Get multi headers
-	 * 
-	 * @return Enumeration of the value Strings
-	 * @param name the case-insensitive field name
-	 */
-	public Enumeration<String> getValues(Buffer name)
-	{
-		final Field field = getField(name);
-		if (field == null) 
-		{
-			List<String> empty=Collections.emptyList();
-			return Collections.enumeration(empty);
-		}
-
-		return new Enumeration<String>()
-		{
-			Field f = field;
-
-			public boolean hasMoreElements()
-			{
-				return f != null;
-			}
-
-			public String nextElement() throws NoSuchElementException
-			{
-				if (f == null) throw new NoSuchElementException();
-				Field n = f;
-				f = f._next;
-				return n.getValue();
-			}
-		};
-	}
-
-	/* -------------------------------------------------------------- */
-	/**
 	 * Get multi field values with separator. The multiple values can be represented as separate
 	 * headers of the same name, or by a single header using the separator(s), or a combination of
 	 * both. Separators may be quoted.
@@ -539,69 +463,15 @@
 	 */
 	public void put(String name, String value)
 	{
-		if (value==null)
-			remove(name);
-		else
-		{
-			Buffer n = HttpHeaders.CACHE.lookup(name);
-			put(n, value);
-		}
-	}
-
-	/* -------------------------------------------------------------- */
-	/**
-	 * Set a field.
-	 * 
-	 * @param name the name of the field
-	 * @param value the value of the field. If null the field is cleared.
-	 */
-	public void put(Buffer name, String value)
-	{
-/*
-		Buffer n = HttpHeaders.CACHE.lookup(name);
-		Buffer v = convertValue(value);
-		put(n, v);
-*/
-		remove(name);
+		String nameLower = name.toLowerCase();
+		removeLower(nameLower);
 		if (value == null)
 			return;
 
-		if (!(name instanceof BufferCache.CachedBuffer)) 
-			name = HttpHeaders.CACHE.lookup(name);
-		int valueOrdinal = HttpHeaderValues.CACHE.getOrdinal(value);
-		
-		// new value;
-		Field field = new Field(name, value, valueOrdinal);
-		_fields.add(field);
-		_names.put(name, field);
-	}
-
-	/* -------------------------------------------------------------- */
-	/**
-	 * Set a field.
-	 * 
-	 * @param name the name of the field
-	 * @param value the value of the field. If null the field is cleared.
-	 */
-	public void put(Buffer name, Buffer value)
-	{
-/*
-		remove(name);
-		if (value == null)
-			return;
-
-		if (!(name instanceof BufferCache.CachedBuffer)) 
-			name = HttpHeaders.CACHE.lookup(name);
-		if (!(value instanceof CachedBuffer))
-			value = HttpHeaderValues.CACHE.lookup(value).asImmutableBuffer();
-		
 		// new value;
 		Field field = new Field(name, value);
 		_fields.add(field);
-		_names.put(name, field);
-*/
-		String s = value==null ? null : value.toString();
-		put(name,s);
+		_names.put(nameLower, field);
 	}
 
 	/* -------------------------------------------------------------- */
@@ -616,35 +486,11 @@
 	 */
 	public void add(String name, String value) throws IllegalArgumentException
 	{
-		if (value==null)
-			return;
-		Buffer n = HttpHeaders.CACHE.lookup(name);
-		add(n, value);
-	}
-
-	/* -------------------------------------------------------------- */
-	/**
-	 * Add to or set a field. If the field is allowed to have multiple values, add will add multiple
-	 * headers of the same name.
-	 * 
-	 * @param name the name of the field
-	 * @param value the value of the field.
-	 * @exception IllegalArgumentException If the name is a single valued field and already has a
-	 *                value.
-	 */
-	public void add(Buffer name, String value) throws IllegalArgumentException
-	{   
 		if (value == null) throw new IllegalArgumentException("null value");
 
-		if (!(name instanceof CachedBuffer))
-			name = HttpHeaders.CACHE.lookup(name);
-		name=name.asImmutableBuffer();
-/*
-		if (!(value instanceof CachedBuffer) && HttpHeaderValues.hasKnownValues(HttpHeaders.CACHE.getOrdinal(name)))
-			value= HttpHeaderValues.CACHE.lookup(value);
-		value=value.asImmutableBuffer();
-*/
-		Field field = _names.get(name);
+		String nameLower = name.toLowerCase();
+
+		Field field = _names.get(nameLower);
 		Field last = null;
 		while (field != null)
 		{
@@ -653,15 +499,14 @@
 		}
 
 		// create the field
-		int valueOrdinal = HttpHeaderValues.CACHE.getOrdinal(value);
-		field = new Field(name, value, valueOrdinal);
+		field = new Field(name, value);
 		_fields.add(field);
 
 		// look for chain to add too
 		if (last != null)
 			last._next = field;
 		else
-			_names.put(name, field);
+			_names.put(nameLower, field);
 	}
 
 	/* ------------------------------------------------------------ */
@@ -672,20 +517,11 @@
 	 */
 	public void remove(String name)
 	{
-		remove(HttpHeaders.CACHE.lookup(name));
+		removeLower(name.toLowerCase());
 	}
 
-	/* ------------------------------------------------------------ */
-	/**
-	 * Remove a field.
-	 * 
-	 * @param name
-	 */
-	public void remove(Buffer name)
-	{
-		if (!(name instanceof BufferCache.CachedBuffer)) 
-			name = HttpHeaders.CACHE.lookup(name);
-		Field field = _names.remove(name);
+	private void removeLower(String nameLower) {
+		Field field = _names.remove(nameLower);
 		while (field != null)
 		{
 			_fields.remove(field);
@@ -709,20 +545,6 @@
 
 	/* -------------------------------------------------------------- */
 	/**
-	 * Get a header as an long value. Returns the value of an integer field or -1 if not found. The
-	 * case of the field name is ignored.
-	 * 
-	 * @param name the case-insensitive field name
-	 * @exception NumberFormatException If bad long found
-	 */
-	public long getLongField(Buffer name) throws NumberFormatException
-	{
-		Field field = getField(name);
-		return field==null?-1L:field.getLongValue();
-	}
-
-	/* -------------------------------------------------------------- */
-	/**
 	 * Get a header as a date value. Returns the value of a date field, or -1 if not found. The case
 	 * of the field name is ignored.
 	 * 
@@ -751,9 +573,8 @@
 	 * @param name the field name
 	 * @param value the field long value
 	 */
-	public void putLongField(Buffer name, long value)
+	public void putLongField(String name, long value)
 	{
-//		Buffer v = BufferUtil.toBuffer(value);
 		String v = Long.toString(value);
 		put(name, v);
 	}
@@ -765,39 +586,8 @@
 	 * @param name the field name
 	 * @param value the field long value
 	 */
-	public void putLongField(String name, long value)
-	{
-		Buffer n = HttpHeaders.CACHE.lookup(name);
-//		Buffer v = BufferUtil.toBuffer(value);
-		String v = Long.toString(value);
-		put(n, v);
-	}
-
-	/* -------------------------------------------------------------- */
-	/**
-	 * Sets the value of an long field.
-	 * 
-	 * @param name the field name
-	 * @param value the field long value
-	 */
 	public void addLongField(String name, long value)
 	{
-		Buffer n = HttpHeaders.CACHE.lookup(name);
-//		Buffer v = BufferUtil.toBuffer(value);
-		String v = Long.toString(value);
-		add(n, v);
-	}
-
-	/* -------------------------------------------------------------- */
-	/**
-	 * Sets the value of an long field.
-	 * 
-	 * @param name the field name
-	 * @param value the field long value
-	 */
-	public void addLongField(Buffer name, long value)
-	{
-//		Buffer v = BufferUtil.toBuffer(value);
 		String v = Long.toString(value);
 		add(name, v);
 	}
@@ -809,24 +599,10 @@
 	 * @param name the field name
 	 * @param date the field date value
 	 */
-	public void putDateField(Buffer name, long date)
-	{
-		String d=formatDate(date);
-		Buffer v = new ByteArrayBuffer(d);
-		put(name, v);
-	}
-
-	/* -------------------------------------------------------------- */
-	/**
-	 * Sets the value of a date field.
-	 * 
-	 * @param name the field name
-	 * @param date the field date value
-	 */
 	public void putDateField(String name, long date)
 	{
-		Buffer n = HttpHeaders.CACHE.lookup(name);
-		putDateField(n,date);
+		String d = formatDate(date);
+		put(name, d);
 	}
 
 	/* -------------------------------------------------------------- */
@@ -838,10 +614,8 @@
 	 */
 	public void addDateField(String name, long date)
 	{
-		String d=formatDate(date);
-		Buffer n = HttpHeaders.CACHE.lookup(name);
-//		Buffer v = new ByteArrayBuffer(d);
-		add(n, d);
+		String d = formatDate(date);
+		add(name, d);
 	}
 
 	/* ------------------------------------------------------------ */
@@ -965,7 +739,7 @@
 				{
 					_fields.remove(field);
 					if (last==null)
-						_names.put(HttpHeaders.SET_COOKIE_BUFFER,field._next);
+						_names.put(HttpHeaders.SET_COOKIE,field._next);
 					else
 						last._next=field._next;
 					break;
@@ -975,10 +749,10 @@
 			field=field._next;
 		}
 
-		add(HttpHeaders.SET_COOKIE_BUFFER, name_value_params);
+		add(HttpHeaders.SET_COOKIE, name_value_params);
 		
 		// Expire responses with set-cookie headers so they do not get cached.
-		put(HttpHeaders.EXPIRES_BUFFER, __01Jan1970_BUFFER);
+		put(HttpHeaders.EXPIRES, __01Jan1970);
 	}
 
 	@Override
@@ -1159,36 +933,25 @@
 
 	public static final class Field
 	{
-		private final Buffer _name;
+		private final String _name;
 		private final String _value;
-		private final int _valueOrdinal;
 		private Field _next;
 
-		private Field(Buffer name, String value, int valueOrdinal)
+		private Field(String name, String value)
 		{
 			_name = name;
 			_value = value;
-			_valueOrdinal = valueOrdinal;
 			_next = null;
 		}
-/*
-		private Field(Buffer name, Buffer value)
-		{
-			this(name,value.toString(),HttpHeaderValues.CACHE.getOrdinal(value));
-		}
-*/
+
 		public void putTo(Buffer buffer) throws IOException
 		{
-			int o=(_name instanceof CachedBuffer)?((CachedBuffer)_name).getOrdinal():-1;
-			if (o>=0)
-				buffer.put(_name);
+			byte[] nameBytes = StringUtil.getBytes(_name);
+			if (getNameOrdinal() >=0 )
+				buffer.put(nameBytes);
 			else
 			{
-				int s=_name.getIndex();
-				int e=_name.putIndex();
-				while (s<e)
-				{
-					byte b=_name.peek(s++);
+				for( byte b : nameBytes ) {
 					switch(b)
 					{
 						case '\r':
@@ -1204,9 +967,8 @@
 			buffer.put((byte) ':');
 			buffer.put((byte) ' ');
 			
-			o = _valueOrdinal;
 			byte[] valueBytes = StringUtil.getBytes(_value);
-			if (o>=0)
+			if (getValueOrdinal() >= 0)
 				buffer.put(valueBytes);
 			else
 			{
@@ -1242,7 +1004,7 @@
 
 		public int getValueOrdinal()
 		{
-			return _valueOrdinal;
+			return HttpHeaderValues.CACHE.getOrdinal(_value);
 		}
 
 		public int getIntValue()