diff src/org/eclipse/jetty/http/HttpFields.java @ 1020:6be43ef1eb96

HttpHeaderValues uses StringCache
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 31 Oct 2016 22:24:41 -0600
parents f126d30e04a4
children e350c11242be
line wrap: on
line diff
--- a/src/org/eclipse/jetty/http/HttpFields.java	Mon Oct 31 03:33:42 2016 -0600
+++ b/src/org/eclipse/jetty/http/HttpFields.java	Mon Oct 31 22:24:41 2016 -0600
@@ -277,46 +277,19 @@
 
 	private final ArrayList<Field> _fields = new ArrayList<Field>(20);
 	private final HashMap<Buffer,Field> _names = new HashMap<Buffer,Field>(32);
-	
-	/* ------------------------------------------------------------ */
-	/**
-	 * Constructor.
-	 */
-	public HttpFields()
-	{
-	}
-
-	// TODO externalize this cache so it can be configurable
-	private static ConcurrentMap<String, Buffer> __cache = new ConcurrentHashMap<String, Buffer>();
-	private static int __cacheSize = Integer.getInteger("org.eclipse.jetty.http.HttpFields.CACHE",2000);
-	
-	/* -------------------------------------------------------------- */
+/*
 	private Buffer convertValue(String value)
 	{
-		Buffer buffer = __cache.get(value);
-		if (buffer!=null)
-			return buffer;
-		
 		try
 		{   
-			buffer = new ByteArrayBuffer(value,StringUtil.__ISO_8859_1);
-			
-			if (__cacheSize>0)
-			{
-				if (__cache.size()>__cacheSize)
-					__cache.clear();
-				Buffer b=__cache.putIfAbsent(value,buffer);
-				if (b!=null)
-					buffer=b;
-			}
-			
-			return buffer;
+			return new ByteArrayBuffer(value,StringUtil.__ISO_8859_1);
 		}
 		catch (UnsupportedEncodingException e)
 		{
 			throw new RuntimeException(e);
 		}
 	}
+*/
 	
 	/* -------------------------------------------------------------- */
 	/**
@@ -423,19 +396,6 @@
 
 	/* -------------------------------------------------------------- */
 	/**
-	 * @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 Buffer get(Buffer name)
-	{
-		Field field = getField(name);
-		return field==null?null:field._value;
-	}
-
-
-	/* -------------------------------------------------------------- */
-	/**
 	 * Get multi headers
 	 * 
 	 * @return Enumeration of the values, or null if no such header.
@@ -444,17 +404,17 @@
 	public Collection<String> getValuesCollection(String name)
 	{
 		Field field = getField(name);
-	if (field==null)
-		return null;
-
-		final List<String> list = new ArrayList<String>();
-
-	while(field!=null)
-	{
-		list.add(field.getValue());
-		field=field._next;
-	}
-	return list;
+		if (field==null)
+			return null;
+	
+			final List<String> list = new ArrayList<String>();
+	
+		while(field!=null)
+		{
+			list.add(field.getValue());
+			field=field._next;
+		}
+		return list;
 	}
 
 	/* -------------------------------------------------------------- */
@@ -584,8 +544,7 @@
 		else
 		{
 			Buffer n = HttpHeaders.CACHE.lookup(name);
-			Buffer v = convertValue(value);
-			put(n, v);
+			put(n, value);
 		}
 	}
 
@@ -598,9 +557,23 @@
 	 */
 	public void put(Buffer name, String value)
 	{
+/*
 		Buffer n = HttpHeaders.CACHE.lookup(name);
 		Buffer v = convertValue(value);
 		put(n, v);
+*/
+		remove(name);
+		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);
 	}
 
 	/* -------------------------------------------------------------- */
@@ -612,6 +585,7 @@
 	 */
 	public void put(Buffer name, Buffer value)
 	{
+/*
 		remove(name);
 		if (value == null)
 			return;
@@ -619,46 +593,15 @@
 		if (!(name instanceof BufferCache.CachedBuffer)) 
 			name = HttpHeaders.CACHE.lookup(name);
 		if (!(value instanceof CachedBuffer))
-			value= HttpHeaderValues.CACHE.lookup(value).asImmutableBuffer();
+			value = HttpHeaderValues.CACHE.lookup(value).asImmutableBuffer();
 		
 		// new value;
 		Field field = new Field(name, value);
 		_fields.add(field);
 		_names.put(name, field);
-	}
-
-	/* -------------------------------------------------------------- */
-	/**
-	 * Set a field.
-	 * 
-	 * @param name the name of the field
-	 * @param list the List value of the field. If null the field is cleared.
-	 */
-	public void put(String name, List<?> list)
-	{
-		if (list == null || list.size() == 0)
-		{
-			remove(name);
-			return;
-		}
-		Buffer n = HttpHeaders.CACHE.lookup(name);
-
-		Object v = list.get(0);
-		if (v != null)
-			put(n, HttpHeaderValues.CACHE.lookup(v.toString()));
-		else
-			remove(n);
-
-		if (list.size() > 1)
-		{
-			java.util.Iterator<?> iter = list.iterator();
-			iter.next();
-			while (iter.hasNext())
-			{
-				v = iter.next();
-				if (v != null) put(n, HttpHeaderValues.CACHE.lookup(v.toString()));
-			}
-		}
+*/
+		String s = value==null ? null : value.toString();
+		put(name,s);
 	}
 
 	/* -------------------------------------------------------------- */
@@ -676,8 +619,7 @@
 		if (value==null)
 			return;
 		Buffer n = HttpHeaders.CACHE.lookup(name);
-		Buffer v = convertValue(value);
-		add(n, v);
+		add(n, value);
 	}
 
 	/* -------------------------------------------------------------- */
@@ -690,18 +632,18 @@
 	 * @exception IllegalArgumentException If the name is a single valued field and already has a
 	 *                value.
 	 */
-	public void add(Buffer name, Buffer value) throws IllegalArgumentException
+	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);
 		Field last = null;
 		while (field != null)
@@ -711,7 +653,8 @@
 		}
 
 		// create the field
-		field = new Field(name, value);
+		int valueOrdinal = HttpHeaderValues.CACHE.getOrdinal(value);
+		field = new Field(name, value, valueOrdinal);
 		_fields.add(field);
 
 		// look for chain to add too
@@ -791,7 +734,7 @@
 		if (field == null) 
 			return -1;
 
-		String val = valueParameters(BufferUtil.to8859_1_String(field._value), null);
+		String val = valueParameters(field._value, null);
 		if (val == null) 
 			return -1;
 
@@ -810,7 +753,8 @@
 	 */
 	public void putLongField(Buffer name, long value)
 	{
-		Buffer v = BufferUtil.toBuffer(value);
+//		Buffer v = BufferUtil.toBuffer(value);
+		String v = Long.toString(value);
 		put(name, v);
 	}
 
@@ -824,7 +768,8 @@
 	public void putLongField(String name, long value)
 	{
 		Buffer n = HttpHeaders.CACHE.lookup(name);
-		Buffer v = BufferUtil.toBuffer(value);
+//		Buffer v = BufferUtil.toBuffer(value);
+		String v = Long.toString(value);
 		put(n, v);
 	}
 
@@ -838,7 +783,8 @@
 	public void addLongField(String name, long value)
 	{
 		Buffer n = HttpHeaders.CACHE.lookup(name);
-		Buffer v = BufferUtil.toBuffer(value);
+//		Buffer v = BufferUtil.toBuffer(value);
+		String v = Long.toString(value);
 		add(n, v);
 	}
 
@@ -851,7 +797,8 @@
 	 */
 	public void addLongField(Buffer name, long value)
 	{
-		Buffer v = BufferUtil.toBuffer(value);
+//		Buffer v = BufferUtil.toBuffer(value);
+		String v = Long.toString(value);
 		add(name, v);
 	}
 
@@ -893,8 +840,8 @@
 	{
 		String d=formatDate(date);
 		Buffer n = HttpHeaders.CACHE.lookup(name);
-		Buffer v = new ByteArrayBuffer(d);
-		add(n, v);
+//		Buffer v = new ByteArrayBuffer(d);
+		add(n, d);
 	}
 
 	/* ------------------------------------------------------------ */
@@ -1028,25 +975,13 @@
 			field=field._next;
 		}
 
-		add(HttpHeaders.SET_COOKIE_BUFFER, new ByteArrayBuffer(name_value_params));
+		add(HttpHeaders.SET_COOKIE_BUFFER, name_value_params);
 		
 		// Expire responses with set-cookie headers so they do not get cached.
 		put(HttpHeaders.EXPIRES_BUFFER, __01Jan1970_BUFFER);
 	}
 
-	/* -------------------------------------------------------------- */
-	public void putTo(Buffer buffer) throws IOException
-	{
-		for (int i = 0; i < _fields.size(); i++)
-		{
-			Field field = _fields.get(i);
-			if (field != null) 
-				field.putTo(buffer);
-		}
-		BufferUtil.putCRLF(buffer);
-	}
-
-	/* -------------------------------------------------------------- */
+	@Override
 	public String toString()
 	{
 		try
@@ -1221,24 +1156,27 @@
 		return vl;
 	}
 
-	/* ------------------------------------------------------------ */
-	/* ------------------------------------------------------------ */
-	/* ------------------------------------------------------------ */
+
 	public static final class Field
 	{
-		private Buffer _name;
-		private Buffer _value;
+		private final Buffer _name;
+		private final String _value;
+		private final int _valueOrdinal;
 		private Field _next;
 
-		/* ------------------------------------------------------------ */
-		private Field(Buffer name, Buffer value)
+		private Field(Buffer name, String value, int valueOrdinal)
 		{
 			_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;
@@ -1266,16 +1204,13 @@
 			buffer.put((byte) ':');
 			buffer.put((byte) ' ');
 			
-			o=(_value instanceof CachedBuffer)?((CachedBuffer)_value).getOrdinal():-1;
+			o = _valueOrdinal;
+			byte[] valueBytes = StringUtil.getBytes(_value);
 			if (o>=0)
-				buffer.put(_value);
+				buffer.put(valueBytes);
 			else
 			{
-				int s=_value.getIndex();
-				int e=_value.putIndex();
-				while (s<e)
-				{
-					byte b=_value.peek(s++);
+				for( byte b : valueBytes ) {
 					switch(b)
 					{
 						case '\r':
@@ -1290,55 +1225,37 @@
 			BufferUtil.putCRLF(buffer);
 		}
 
-		/* ------------------------------------------------------------ */
-		public String getName()
+		private String getName()
 		{
 			return BufferUtil.to8859_1_String(_name);
 		}
 
-		/* ------------------------------------------------------------ */
-		Buffer getNameBuffer()
-		{
-			return _name;
-		}
-
-		/* ------------------------------------------------------------ */
 		public int getNameOrdinal()
 		{
 			return HttpHeaders.CACHE.getOrdinal(_name);
 		}
 
-		/* ------------------------------------------------------------ */
 		public String getValue()
 		{
-			return BufferUtil.to8859_1_String(_value);
-		}
-
-		/* ------------------------------------------------------------ */
-		public Buffer getValueBuffer()
-		{
 			return _value;
 		}
 
-		/* ------------------------------------------------------------ */
 		public int getValueOrdinal()
 		{
-			return HttpHeaderValues.CACHE.getOrdinal(_value);
+			return _valueOrdinal;
 		}
 
-		/* ------------------------------------------------------------ */
 		public int getIntValue()
 		{
 			return (int) getLongValue();
 		}
 
-		/* ------------------------------------------------------------ */
 		public long getLongValue()
 		{
 			return BufferUtil.toLong(_value);
 		}
 
-		/* ------------------------------------------------------------ */
+		@Override
 		public String toString()
 		{
 			return ("[" + getName() + "=" + _value + (_next == null ? "" : "->") + "]");