changeset 983:23ec25435b8c

simplify AbstractGenerator
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 16 Oct 2016 22:58:41 -0600
parents dbecd7faa1f5
children 7b0fa315e835
files src/org/eclipse/jetty/http/AbstractGenerator.java src/org/eclipse/jetty/http/HttpGenerator.java src/org/eclipse/jetty/server/AbstractHttpConnection.java src/org/eclipse/jetty/server/HttpOutput.java src/org/eclipse/jetty/server/HttpWriter.java src/org/eclipse/jetty/server/Response.java
diffstat 6 files changed, 59 insertions(+), 227 deletions(-) [+]
line wrap: on
line diff
--- a/src/org/eclipse/jetty/http/AbstractGenerator.java	Sun Oct 16 21:40:27 2016 -0600
+++ b/src/org/eclipse/jetty/http/AbstractGenerator.java	Sun Oct 16 22:58:41 2016 -0600
@@ -38,7 +38,7 @@
  * faster, but will consume more memory.   This option is just for testing and tuning.
  *
  */
-public abstract class AbstractGenerator
+abstract class AbstractGenerator
 {
 	private static final Logger LOG = LoggerFactory.getLogger(AbstractGenerator.class);
 
@@ -46,38 +46,34 @@
 	public static final boolean MORE=false;
 
 	// states
-	public final static int STATE_HEADER = 0;
-	public final static int STATE_CONTENT = 2;
-	public final static int STATE_FLUSHING = 3;
-	public final static int STATE_END = 4;
-
-	public static final byte[] NO_BYTES = {};
+	final static int STATE_HEADER = 0;
+	final static int STATE_CONTENT = 2;
+	final static int STATE_FLUSHING = 3;
+	final static int STATE_END = 4;
 
 	// data
 
-	protected final Buffers _buffers; // source of buffers
-	protected final EndPoint _endp;
+	final Buffers _buffers; // source of buffers
+	final EndPoint _endp;
 
-	protected int _state = STATE_HEADER;
+	int _state = STATE_HEADER;
 
-	protected int _status = 0;
-	protected int _version = HttpVersions.HTTP_1_1_ORDINAL;
-	protected  Buffer _reason;
-	protected  Buffer _method;
-	protected  String _uri;
+	int _status = 0;
+	int _version = HttpVersions.HTTP_1_1_ORDINAL;
+	Buffer _reason;
+	Buffer _method;
+	String _uri;
 
-	protected long _contentWritten = 0;
-	protected long _contentLength = HttpTokens.UNKNOWN_CONTENT;
-	protected boolean _last = false;
-	protected boolean _head = false;
-	protected boolean _noContent = false;
-	protected Boolean _persistent = null;
+	long _contentWritten = 0;
+	long _contentLength = HttpTokens.UNKNOWN_CONTENT;
+	boolean _last = false;
+	boolean _head = false;
+	boolean _noContent = false;
+	Boolean _persistent = null;
 
-	protected Buffer _header; // Buffer for HTTP header (and maybe small _content)
-	protected Buffer _buffer; // Buffer for copy of passed _content
-	protected Buffer _content; // Buffer passed to addContent
-
-	protected Buffer _date;
+	Buffer _header; // Buffer for HTTP header (and maybe small _content)
+	Buffer _buffer; // Buffer for copy of passed _content
+	Buffer _content; // Buffer passed to addContent
 
 
 	/* ------------------------------------------------------------------------------- */
@@ -87,7 +83,7 @@
 	 * @param buffers buffer pool
 	 * @param io the end point
 	 */
-	public AbstractGenerator(Buffers buffers, EndPoint io)
+	AbstractGenerator(Buffers buffers, EndPoint io)
 	{
 		this._buffers = buffers;
 		this._endp = io;
@@ -106,19 +102,13 @@
 	 */
 	public abstract void addContent(Buffer content, boolean last) throws IOException;
 
-	/* ------------------------------------------------------------------------------- */
-	public abstract boolean isRequest();
+	abstract boolean isRequest();
 
-	/* ------------------------------------------------------------------------------- */
-	public abstract boolean isResponse();
-
-	/* ------------------------------------------------------------------------------- */
-	public boolean isOpen()
+	public final boolean isOpen()
 	{
 		return _endp.isOpen();
 	}
 
-	/* ------------------------------------------------------------------------------- */
 	public void reset()
 	{
 		_state = STATE_HEADER;
@@ -131,14 +121,12 @@
 		_persistent = null;
 		_contentWritten = 0;
 		_contentLength = HttpTokens.UNKNOWN_CONTENT;
-		_date = null;
 
 		_content = null;
-		_method=null;
+		_method = null;
 	}
 
-	/* ------------------------------------------------------------------------------- */
-	public void returnBuffers()
+	public final void returnBuffers()
 	{
 		if (_buffer!=null && _buffer.length()==0)
 		{
@@ -153,8 +141,7 @@
 		}
 	}
 
-	/* ------------------------------------------------------------------------------- */
-	public void resetBuffer()
+	public final void resetBuffer()
 	{
 		if(_state>=STATE_FLUSHING)
 			throw new IllegalStateException("Flushed");
@@ -172,77 +159,34 @@
 	/**
 	 * @return Returns the contentBufferSize.
 	 */
-	public int getContentBufferSize()
+	public final int getContentBufferSize()
 	{
 		if (_buffer==null)
 			_buffer=_buffers.getBuffer();
 		return _buffer.capacity();
 	}
 
-	/* ------------------------------------------------------------ */
-	/**
-	 * @param contentBufferSize The contentBufferSize to set.
-	 */
-	public void increaseContentBufferSize(int contentBufferSize)
-	{
-		if (_buffer==null)
-			_buffer=_buffers.getBuffer();
-		if (contentBufferSize > _buffer.capacity())
-		{
-			Buffer nb = _buffers.getBuffer(contentBufferSize);
-			nb.put(_buffer);
-			_buffers.returnBuffer(_buffer);
-			_buffer = nb;
-		}
-	}
-
-	/* ------------------------------------------------------------ */
-	public Buffer getUncheckedBuffer()
+	public final Buffer getUncheckedBuffer()
 	{
 		return _buffer;
 	}
 
-	/* ------------------------------------------------------------ */
-	public int getState()
-	{
-		return _state;
-	}
-
-	/* ------------------------------------------------------------ */
-	public boolean isState(int state)
-	{
-		return _state == state;
-	}
-
-	/* ------------------------------------------------------------ */
-	public boolean isComplete()
+	public final boolean isComplete()
 	{
 		return _state == STATE_END;
 	}
 
-	/* ------------------------------------------------------------ */
-	public boolean isIdle()
+	public final boolean isIdle()
 	{
 		return _state == STATE_HEADER && _method==null && _status==0;
 	}
 
-	/* ------------------------------------------------------------ */
-	public boolean isCommitted()
+	public final boolean isCommitted()
 	{
 		return _state != STATE_HEADER;
 	}
 
-	/* ------------------------------------------------------------ */
-	/**
-	 * @return Returns the head.
-	 */
-	public boolean isHead()
-	{
-		return _head;
-	}
-
-	/* ------------------------------------------------------------ */
-	public void setContentLength(long value)
+	public final void setContentLength(long value)
 	{
 		if (value<0)
 			_contentLength=HttpTokens.UNKNOWN_CONTENT;
@@ -250,11 +194,7 @@
 			_contentLength=value;
 	}
 
-	/* ------------------------------------------------------------ */
-	/**
-	 * @param head The head to set.
-	 */
-	public void setHead(boolean head)
+	public final void setHead(boolean head)
 	{
 		_head = head;
 	}
@@ -264,17 +204,16 @@
 	 * @return <code>false</code> if the connection should be closed after a request has been read,
 	 * <code>true</code> if it should be used for additional requests.
 	 */
-	public boolean isPersistent()
+	public final boolean isPersistent()
 	{
 		return _persistent!=null
 		?_persistent.booleanValue()
 		:(isRequest()?true:_version>HttpVersions.HTTP_1_0_ORDINAL);
 	}
 
-	/* ------------------------------------------------------------ */
-	public void setPersistent(boolean persistent)
+	public final void setPersistent(boolean persistent)
 	{
-		_persistent=persistent;
+		_persistent = persistent;
 	}
 
 	/* ------------------------------------------------------------ */
@@ -282,7 +221,7 @@
 	 * @param version The version of the client the response is being sent to (NB. Not the version
 	 *            in the response, which is the version of the server).
 	 */
-	public void setVersion(int version)
+	public final void setVersion(int version)
 	{
 		if (_state != STATE_HEADER)
 			throw new IllegalStateException("STATE!=START "+_state);
@@ -291,36 +230,12 @@
 			_noContent=true;
 	}
 
-	public int getVersion()
-	{
-		return _version;
-	}
-
-	public void setDate(Buffer timeStampBuffer)
-	{
-		_date=timeStampBuffer;
-	}
-
-	/* ------------------------------------------------------------ */
-	/**
-	 */
-	public void setRequest(String method, String uri)
-	{
-		if (method==null || HttpMethods.GET.equals(method) )
-			_method=HttpMethods.GET_BUFFER;
-		else
-			_method=HttpMethods.CACHE.lookup(method);
-		_uri=uri;
-		if (_version==HttpVersions.HTTP_0_9_ORDINAL)
-			_noContent=true;
-	}
-
 	/* ------------------------------------------------------------ */
 	/**
 	 * @param status The status code to send.
 	 * @param reason the status message to send.
 	 */
-	public void setResponse(int status, String reason)
+	public final void setResponse(int status, String reason)
 	{
 		if (_state != STATE_HEADER) throw new IllegalStateException("STATE!=START");
 		_method=null;
@@ -344,22 +259,7 @@
 		}
 	}
 
-	/* ------------------------------------------------------------ */
-	/** Prepare buffer for unchecked writes.
-	 * Prepare the generator buffer to receive unchecked writes
-	 * @return the available space in the buffer.
-	 * @throws IOException
-	 */
-	public abstract int prepareUncheckedAddContent() throws IOException;
-
-	/* ------------------------------------------------------------ */
-	void uncheckedAddContent(int b)
-	{
-		_buffer.put((byte)b);
-	}
-
-	/* ------------------------------------------------------------ */
-	public void completeUncheckedAddContent()
+	public final void completeUncheckedAddContent()
 	{
 		if (_noContent)
 		{
@@ -374,7 +274,6 @@
 		}
 	}
 
-	/* ------------------------------------------------------------ */
 	public boolean isBufferFull()
 	{
 		if (_buffer != null && _buffer.space()==0)
@@ -387,19 +286,16 @@
 		return _content!=null && _content.length()>0;
 	}
 
-	/* ------------------------------------------------------------ */
-	public boolean isWritten()
+	public final boolean isWritten()
 	{
 		return _contentWritten>0;
 	}
 
-	/* ------------------------------------------------------------ */
-	public boolean isAllContentWritten()
+	public final boolean isAllContentWritten()
 	{
 		return _contentLength>=0 && _contentWritten>=_contentLength;
 	}
 
-	/* ------------------------------------------------------------ */
 	public abstract void completeHeader(HttpFields fields, boolean allContentAdded) throws IOException;
 
 	/* ------------------------------------------------------------ */
@@ -423,12 +319,10 @@
 		}
 	}
 
-	/* ------------------------------------------------------------ */
 	public abstract int flushBuffer() throws IOException;
 
 
-	/* ------------------------------------------------------------ */
-	public void flush(long maxIdleTime) throws IOException
+	public final void flush(long maxIdleTime) throws IOException
 	{
 		// block until everything is flushed
 		long now=System.currentTimeMillis();
@@ -458,7 +352,7 @@
 	 * @param close True if the connection should be closed
 	 * @throws IOException if there is a problem flushing the response
 	 */
-	public void sendError(int code, String reason, String content, boolean close) throws IOException
+	public final void sendError(int code, String reason, String content, boolean close) throws IOException
 	{
 		if (close)
 			_persistent=false;
@@ -488,19 +382,13 @@
 		}
 	}
 
-	/* ------------------------------------------------------------ */
-	/**
-	 * @return Returns the contentWritten.
-	 */
-	public long getContentWritten()
+	public final long getContentWritten()
 	{
 		return _contentWritten;
 	}
 
 
-
-	/* ------------------------------------------------------------ */
-	public void  blockForOutput(long maxIdleTime) throws IOException
+	public final void blockForOutput(long maxIdleTime) throws IOException
 	{
 		if (_endp.isBlocking())
 		{
--- a/src/org/eclipse/jetty/http/HttpGenerator.java	Sun Oct 16 21:40:27 2016 -0600
+++ b/src/org/eclipse/jetty/http/HttpGenerator.java	Sun Oct 16 22:58:41 2016 -0600
@@ -47,7 +47,6 @@
 	// Build cache of response lines for status
 	private static class Status
 	{
-		Buffer _reason;
 		Buffer _schemeCode;
 		Buffer _responseLine;
 	}
@@ -75,21 +74,11 @@
 			bytes[versionLength+6+reason.length()]=HttpTokens.LINE_FEED;
 
 			__status[i] = new Status();
-			__status[i]._reason=new ByteArrayBuffer(bytes,versionLength+5,bytes.length-versionLength-7,Buffer.IMMUTABLE);
 			__status[i]._schemeCode=new ByteArrayBuffer(bytes,0,versionLength+5,Buffer.IMMUTABLE);
 			__status[i]._responseLine=new ByteArrayBuffer(bytes,0,bytes.length,Buffer.IMMUTABLE);
 		}
 	}
 
-	/* ------------------------------------------------------------------------------- */
-	public static Buffer getReasonBuffer(int code)
-	{
-		Status status = code<__status.length?__status[code]:null;
-		if (status!=null)
-			return status._reason;
-		return null;
-	}
-
 
 	// common _content
 	private static final byte[] LAST_CHUNK =
@@ -106,25 +95,17 @@
 	private static final int CHUNK_SPACE = 12;
 
 	// data
-	protected boolean _bypass = false; // True if _content buffer can be written directly to endp and bypass the content buffer
+	private boolean _bypass = false; // True if _content buffer can be written directly to endp and bypass the content buffer
 	private boolean _needCRLF = false;
 	private boolean _needEOC = false;
 	private boolean _bufferChunked = false;
 
 
-	/* ------------------------------------------------------------------------------- */
-	/**
-	 * Constructor.
-	 *
-	 * @param buffers buffer pool
-	 * @param io the end point to use
-	 */
 	public HttpGenerator(Buffers buffers, EndPoint io)
 	{
 		super(buffers,io);
 	}
 
-	/* ------------------------------------------------------------------------------- */
 	@Override
 	public void reset()
 	{
@@ -149,7 +130,7 @@
 		_bypass = false;
 		_needCRLF = false;
 		_needEOC = false;
-		_bufferChunked=false;
+		_bufferChunked = false;
 		_method=null;
 		_uri=null;
 		_noContent=false;
@@ -262,7 +243,6 @@
 	 * @return the available space in the buffer.
 	 * @throws IOException
 	 */
-	@Override
 	public int prepareUncheckedAddContent() throws IOException
 	{
 		if (_noContent)
@@ -293,7 +273,6 @@
 		return _buffer.space()-(_contentLength == HttpTokens.CHUNKED_CONTENT?CHUNK_SPACE:0);
 	}
 
-	/* ------------------------------------------------------------ */
 	@Override
 	public boolean isBufferFull()
 	{
@@ -301,7 +280,6 @@
 		return super.isBufferFull() || _bufferChunked || _bypass  || (_contentLength == HttpTokens.CHUNKED_CONTENT && _buffer != null && _buffer.space() < CHUNK_SPACE);
 	}
 
-	/* ------------------------------------------------------------ */
 	public void send1xx(int code) throws IOException
 	{
 		if (_state != STATE_HEADER)
@@ -309,7 +287,7 @@
 
 		if (code<100||code>199)
 			throw new IllegalArgumentException("!1xx");
-		Status status=__status[code];
+		Status status = __status[code];
 		if (status==null)
 			throw new IllegalArgumentException(code+"?");
 
@@ -339,21 +317,17 @@
 		}
 	}
 
-	/* ------------------------------------------------------------ */
 	@Override
-	public boolean isRequest()
+	boolean isRequest()
 	{
 		return _method!=null;
 	}
 
-	/* ------------------------------------------------------------ */
-	@Override
-	public boolean isResponse()
+	private boolean isResponse()
 	{
 		return _method==null;
 	}
 
-	/* ------------------------------------------------------------ */
 	@Override
 	public void completeHeader(HttpFields fields, boolean allContentAdded) throws IOException
 	{
@@ -474,16 +448,6 @@
 				}
 			}
 
-			// Add headers
-			if (_status>=200 && _date!=null)
-			{
-				_header.put(HttpHeaders.DATE_BUFFER);
-				_header.put((byte)':');
-				_header.put((byte)' ');
-				_header.put(_date);
-				_header.put(CRLF);
-			}
-
 			// key field values
 			HttpFields.Field content_length = null;
 			HttpFields.Field transfer_encoding = null;
@@ -791,7 +755,6 @@
 		flushBuffer();
 	}
 
-	/* ------------------------------------------------------------ */
 	@Override
 	public int flushBuffer() throws IOException
 	{
@@ -908,7 +871,6 @@
 		}
 	}
 
-	/* ------------------------------------------------------------ */
 	private int flushMask()
 	{
 		return  ((_header != null && _header.length() > 0)?4:0)
@@ -916,7 +878,6 @@
 		| ((_bypass && _content != null && _content.length() > 0)?1:0);
 	}
 
-	/* ------------------------------------------------------------ */
 	private void prepareBuffers()
 	{
 		// if we are not flushing an existing chunk
@@ -955,7 +916,7 @@
 					_header.put(HttpTokens.CRLF);
 
 					// Need a CRLF after the content
-					_needCRLF=true;
+					_needCRLF = true;
 				}
 				else if (_buffer!=null)
 				{
@@ -1055,20 +1016,6 @@
 
 	}
 
-	public int getBytesBuffered()
-	{
-		return(_header==null?0:_header.length())+
-		(_buffer==null?0:_buffer.length())+
-		(_content==null?0:_content.length());
-	}
-
-	public boolean isEmpty()
-	{
-		return (_header==null||_header.length()==0) &&
-		(_buffer==null||_buffer.length()==0) &&
-		(_content==null||_content.length()==0);
-	}
-
 	@Override
 	public String toString()
 	{
--- a/src/org/eclipse/jetty/server/AbstractHttpConnection.java	Sun Oct 16 21:40:27 2016 -0600
+++ b/src/org/eclipse/jetty/server/AbstractHttpConnection.java	Sun Oct 16 22:58:41 2016 -0600
@@ -768,7 +768,7 @@
 
 		public void sendResponse(Buffer response) throws IOException
 		{
-			((HttpGenerator)super._generator).sendResponse(response);
+			super._generator.sendResponse(response);
 		}
 
 		public void sendContent(Object content) throws IOException
--- a/src/org/eclipse/jetty/server/HttpOutput.java	Sun Oct 16 21:40:27 2016 -0600
+++ b/src/org/eclipse/jetty/server/HttpOutput.java	Sun Oct 16 22:58:41 2016 -0600
@@ -23,7 +23,6 @@
 
 import javax.servlet.ServletOutputStream;
 
-import org.eclipse.jetty.http.AbstractGenerator;
 import org.eclipse.jetty.http.HttpGenerator;
 import org.eclipse.jetty.io.Buffer;
 import org.eclipse.jetty.io.ByteArrayBuffer;
@@ -36,13 +35,13 @@
  * Implements  {@link javax.servlet.ServletOutputStream} from the <code>javax.servlet</code> package.   
  * </p>
  * A {@link ServletOutputStream} implementation that writes content
- * to a {@link AbstractGenerator}.   The class is designed to be reused
+ * to a {@link HttpGenerator}.   The class is designed to be reused
  * and can be reopened after a close.
  */
 public class HttpOutput extends ServletOutputStream 
 {
 	protected final AbstractHttpConnection _connection;
-	protected final AbstractGenerator _generator;
+	protected final HttpGenerator _generator;
 	private boolean _closed;
 	private ByteArrayBuffer _onebyte;
 	
--- a/src/org/eclipse/jetty/server/HttpWriter.java	Sun Oct 16 21:40:27 2016 -0600
+++ b/src/org/eclipse/jetty/server/HttpWriter.java	Sun Oct 16 22:58:41 2016 -0600
@@ -22,7 +22,7 @@
 import java.io.OutputStreamWriter;
 import java.io.Writer;
 
-import org.eclipse.jetty.http.AbstractGenerator;
+import org.eclipse.jetty.http.HttpGenerator;
 import org.eclipse.jetty.util.ByteArrayOutputStream2;
 import org.eclipse.jetty.util.StringUtil;
 
@@ -43,7 +43,7 @@
     private static final int WRITE_UTF8 = 2;
     
     final HttpOutput _out;
-    final AbstractGenerator _generator;
+    final HttpGenerator _generator;
     int _writeMode;
     int _surrogate;
 
--- a/src/org/eclipse/jetty/server/Response.java	Sun Oct 16 21:40:27 2016 -0600
+++ b/src/org/eclipse/jetty/server/Response.java	Sun Oct 16 22:58:41 2016 -0600
@@ -890,9 +890,7 @@
 	 */
 	public void setBufferSize(int size)
 	{
-		if (isCommitted() || getContentCount()>0)
-			throw new IllegalStateException("Committed or content written");
-		_connection._generator.increaseContentBufferSize(size);
+		throw new UnsupportedOperationException();
 	}
 
 	/* ------------------------------------------------------------ */