diff src/org/eclipse/jetty/http/HttpGenerator.java @ 1066:bbbda7c6e8ec

fix use of HttpGenerator._header
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 09 Nov 2016 05:48:10 -0700
parents 158d1e6ac17f
children 56b515be91e1
line wrap: on
line diff
--- a/src/org/eclipse/jetty/http/HttpGenerator.java	Wed Nov 09 04:36:05 2016 -0700
+++ b/src/org/eclipse/jetty/http/HttpGenerator.java	Wed Nov 09 05:48:10 2016 -0700
@@ -150,19 +150,23 @@
 				if (_bufferChunked)
 				{
 					JBuffer nc = _buffers.getBuffer(_content.remaining()+CHUNK_SPACE+content.remaining());
-					nc.put(_content);
-					nc.put(HttpTokens.CRLF);
+					nc.clear();
+					nc.putQ(_content);
+					nc.putQ(HttpTokens.CRLF);
 					BufferUtil.putHexInt(nc, content.remaining());
-					nc.put(HttpTokens.CRLF);
-					nc.put(content);
-					content=nc;
+					nc.putQ(HttpTokens.CRLF);
+					nc.putQ(content);
+					nc.flip();
+					content = nc;
 				}
 				else
 				{
 					JBuffer nc = _buffers.getBuffer(_content.remaining()+content.remaining());
-					nc.put(_content);
-					nc.put(content);
-					content=nc;
+					nc.clear();
+					nc.putQ(_content);
+					nc.putQ(content);
+					nc.flip();
+					content = nc;
 				}
 			}
 		}
@@ -245,16 +249,13 @@
 		if (status==null)
 			throw new IllegalArgumentException(code+"?");
 
-		// get a header buffer
-		if (_header == null)
-			_header = _buffers.getHeader();
-
-		_header.put(status._responseLine);
-		_header.put(HttpTokens.CRLF);
+		_header.putQ(status._responseLine);
+		_header.putQ(HttpTokens.CRLF);
 
 		try
 		{
 			// nasty semi busy flush!
+			_header.flip();
 			while(_header.remaining()>0)
 			{
 				int len = _endp.flush(_header);
@@ -284,10 +285,6 @@
 			throw new IllegalStateException("last?");
 		_last = _last | allContentAdded;
 
-		// get a header buffer
-		if (_header == null)
-			_header = _buffers.getHeader();
-
 		boolean has_server = false;
 
 		try
@@ -310,31 +307,31 @@
 
 				if (status==null)
 				{
-					_header.put(HttpVersions.HTTP_1_1_BYTES);
-					_header.put((byte) ' ');
-					_header.put((byte) ('0' + _status / 100));
-					_header.put((byte) ('0' + (_status % 100) / 10));
-					_header.put((byte) ('0' + (_status % 10)));
-					_header.put((byte) ' ');
+					_header.putQ(HttpVersions.HTTP_1_1_BYTES);
+					_header.putQ((byte) ' ');
+					_header.putQ((byte) ('0' + _status / 100));
+					_header.putQ((byte) ('0' + (_status % 100) / 10));
+					_header.putQ((byte) ('0' + (_status % 10)));
+					_header.putQ((byte) ' ');
 					if (_reason==null)
 					{
-						_header.put((byte) ('0' + _status / 100));
-						_header.put((byte) ('0' + (_status % 100) / 10));
-						_header.put((byte) ('0' + (_status % 10)));
+						_header.putQ((byte) ('0' + _status / 100));
+						_header.putQ((byte) ('0' + (_status % 100) / 10));
+						_header.putQ((byte) ('0' + (_status % 10)));
 					}
 					else
-						_header.put(_reason);
-					_header.put(HttpTokens.CRLF);
+						_header.putQ(_reason);
+					_header.putQ(HttpTokens.CRLF);
 				}
 				else
 				{
 					if (_reason==null)
-						_header.put(status._responseLine);
+						_header.putQ(status._responseLine);
 					else
 					{
-						_header.put(status._schemeCode);
-						_header.put(_reason);
-						_header.put(HttpTokens.CRLF);
+						_header.putQ(status._schemeCode);
+						_header.putQ(_reason);
+						_header.putQ(HttpTokens.CRLF);
 					}
 				}
 
@@ -348,7 +345,7 @@
 
 					if (_status!=101 )
 					{
-						_header.put(HttpTokens.CRLF);
+						_header.putQ(HttpTokens.CRLF);
 						_state = STATE_CONTENT;
 						return;
 					}
@@ -532,11 +529,11 @@
 						if (content_length == null && !_noContent)
 						{
 							// known length but not actually set.
-							_header.put(HttpHeaders.CONTENT_LENGTH_BYTES);
-							_header.put(HttpTokens.COLON);
-							_header.put((byte) ' ');
+							_header.putQ(HttpHeaders.CONTENT_LENGTH_BYTES);
+							_header.putQ(HttpTokens.COLON);
+							_header.putQ((byte) ' ');
 							BufferUtil.putDecLong(_header, _contentLength);
-							_header.put(HttpTokens.CRLF);
+							_header.putQ(HttpTokens.CRLF);
 						}
 					}
 					else
@@ -548,7 +545,7 @@
 
 				case HttpTokens.NO_CONTENT:
 					if (content_length == null && _status >= 200 && _status != 204 && _status != 304)
-						_header.put(CONTENT_LENGTH_0);
+						_header.putQ(CONTENT_LENGTH_0);
 					break;
 
 				case HttpTokens.EOF_CONTENT:
@@ -576,7 +573,7 @@
 						throw new IllegalArgumentException("BAD TE");
 				}
 				else
-					_header.put(TRANSFER_ENCODING_CHUNKED);
+					_header.putQ(TRANSFER_ENCODING_CHUNKED);
 			}
 
 			// Handle connection if need be
@@ -588,44 +585,44 @@
 
 			if (!_persistent && (close || _version > HttpVersions.HTTP_1_0_ORDINAL))
 			{
-				_header.put(CONNECTION_CLOSE);
+				_header.putQ(CONNECTION_CLOSE);
 				if (connection!=null)
 				{
-					_header.setPutIndex(_header.putIndex()-2);
-					_header.put((byte)',');
-					_header.put(connection.toString().getBytes());
-					_header.put(CRLF);
+					_header.position(_header.position()-2);
+					_header.putQ((byte)',');
+					_header.putQ(connection.toString().getBytes());
+					_header.putQ(CRLF);
 				}
 			}
 			else if (keep_alive)
 			{
-				_header.put(CONNECTION_KEEP_ALIVE);
+				_header.putQ(CONNECTION_KEEP_ALIVE);
 				if (connection!=null)
 				{
-					_header.setPutIndex(_header.putIndex()-2);
-					_header.put((byte)',');
-					_header.put(connection.toString().getBytes());
-					_header.put(CRLF);
+					_header.position(_header.position()-2);
+					_header.putQ((byte)',');
+					_header.putQ(connection.toString().getBytes());
+					_header.putQ(CRLF);
 				}
 			}
 			else if (connection!=null)
 			{
-				_header.put(CONNECTION_);
-				_header.put(connection.toString().getBytes());
-				_header.put(CRLF);
+				_header.putQ(CONNECTION_);
+				_header.putQ(connection.toString().getBytes());
+				_header.putQ(CRLF);
 			}
 
 			if (!has_server && _status>199)
-				_header.put(SERVER);
+				_header.putQ(SERVER);
 
 			// end the header.
-			_header.put(HttpTokens.CRLF);
+			_header.putQ(HttpTokens.CRLF);
 			_state = STATE_CONTENT;
 
 		}
 		catch(ArrayIndexOutOfBoundsException e)
 		{
-			throw new RuntimeException("Header>"+_header.capacity(),e);
+			throw new RuntimeException("Header>"+_header.remaining(),e);
 		}
 	}
 
@@ -673,16 +670,23 @@
 				last_flush = to_flush;
 				switch (to_flush)
 				{
+//qqq
 					case 7:
 						throw new IllegalStateException(); // should never happen!
 					case 6:
+						_header.flip();
 						len = _endp.flush(_header, _buffer, null);
+						_header.compact();
 						break;
 					case 5:
+						_header.flip();
 						len = _endp.flush(_header, _content, null);
+						_header.compact();
 						break;
 					case 4:
+						_header.flip();
 						len = _endp.flush(_header);
+						_header.compact();
 						break;
 					case 3:
 						len = _endp.flush(_buffer, _content, null);
@@ -697,8 +701,7 @@
 					{
 						len=0;
 						// Nothing more we can write now.
-						if (_header != null)
-							_header.clearJ();
+						_header.clear();
 
 						_bypass = false;
 						_bufferChunked = false;
@@ -754,7 +757,7 @@
 
 	private int flushMask()
 	{
-		return  ((_header != null && _header.remaining() > 0)?4:0)
+		return  ((_header.position() > 0)?4:0)
 		| ((_buffer != null && _buffer.remaining() > 0)?2:0)
 		| ((_bypass && _content != null && _content.remaining() > 0)?1:0);
 	}
@@ -782,19 +785,16 @@
 					int size = _content.remaining();
 					_bufferChunked = true;
 
-					if (_header == null)
-						_header = _buffers.getHeader();
-
 					// if we need CRLF add this to header
 					if (_needCRLF)
 					{
-						if (_header.remaining() > 0) throw new IllegalStateException("EOC");
-						_header.put(HttpTokens.CRLF);
+						if (_header.position() > 0) throw new IllegalStateException("EOC");
+						_header.putQ(HttpTokens.CRLF);
 						_needCRLF = false;
 					}
 					// Add the chunk size to the header
 					BufferUtil.putHexInt(_header, size);
-					_header.put(HttpTokens.CRLF);
+					_header.putQ(HttpTokens.CRLF);
 
 					// Need a CRLF after the content
 					_needCRLF = true;
@@ -807,17 +807,14 @@
 						// Prepare a chunk!
 						_bufferChunked = true;
 
-						if (_header == null)
-							_header = _buffers.getHeader();
-
 						if (_needCRLF)
 						{
-							if (_header.remaining() > 0) throw new IllegalStateException("EOC");
-							_header.put(HttpTokens.CRLF);
+							if (_header.position() > 0) throw new IllegalStateException("EOC");
+							_header.putQ(HttpTokens.CRLF);
 							_needCRLF = false;
 						}
 						BufferUtil.putHexInt(_header, size);
-						_header.put(HttpTokens.CRLF);
+						_header.putQ(HttpTokens.CRLF);
 
 						// Add end chunk trailer.
 						if (_buffer.space() >= 2)
@@ -830,14 +827,11 @@
 				// If we need EOC and everything written
 				if (_needEOC && (_content == null || _content.remaining() == 0))
 				{
-					if (_header == null && _buffer == null)
-						_header = _buffers.getHeader();
-
 					if (_needCRLF)
 					{
-						if (_buffer == null && _header != null && _header.space() >= HttpTokens.CRLF.length)
+						if (_buffer == null && _header.remaining() >= HttpTokens.CRLF.length)
 						{
-							_header.put(HttpTokens.CRLF);
+							_header.putQ(HttpTokens.CRLF);
 							_needCRLF = false;
 						}
 						else if (_buffer!=null && _buffer.space() >= HttpTokens.CRLF.length)
@@ -849,11 +843,11 @@
 
 					if (!_needCRLF && _needEOC)
 					{
-						if (_buffer == null && _header != null && _header.space() >= LAST_CHUNK.length)
+						if (_buffer == null && _header.remaining() >= LAST_CHUNK.length)
 						{
 							if (!_head)
 							{
-								_header.put(LAST_CHUNK);
+								_header.putQ(LAST_CHUNK);
 								_bufferChunked=true;
 							}
 							_needEOC = false;
@@ -880,13 +874,13 @@
 	@Override
 	public String toString()
 	{
-		JBuffer header=_header;
+		JBuffer header = _header;
 		JBuffer buffer=_buffer;
 		JBuffer content=_content;
 		return String.format("%s{s=%d,h=%d,b=%d,c=%d}",
 				getClass().getSimpleName(),
 				_state,
-				header == null ? -1 : header.remaining(),
+				header == null ? -1 : header.position(),
 				buffer == null ? -1 : buffer.remaining(),
 				content == null ? -1 : content.remaining());
 	}
@@ -931,7 +925,7 @@
 	private boolean _noContent = false;
 	private Boolean _persistent = null;
 
-	private JBuffer _header; // JBuffer for HTTP header (and maybe small _content)
+	private final JBuffer _header; // JBuffer for HTTP header (and maybe small _content)
 	private JBuffer _buffer; // JBuffer for copy of passed _content
 	private JBuffer _content; // JBuffer passed to addContent
 
@@ -940,6 +934,8 @@
 	{
 		this._buffers = buffers;
 		this._endp = io;
+		_header = _buffers.getHeader();
+		_header.clear();
 	}
 
 	public final boolean isOpen()