diff src/org/eclipse/jetty/server/AbstractHttpConnection.java @ 972:5ee36654b383

simplify AbstractHttpConnection
author Franklin Schmidt <fschmidt@gmail.com>
date Sat, 15 Oct 2016 22:42:05 -0600
parents 866f2e801618
children 7422ca1ae146
line wrap: on
line diff
--- a/src/org/eclipse/jetty/server/AbstractHttpConnection.java	Fri Oct 14 13:06:06 2016 -0600
+++ b/src/org/eclipse/jetty/server/AbstractHttpConnection.java	Sat Oct 15 22:42:05 2016 -0600
@@ -48,7 +48,6 @@
 import org.eclipse.jetty.io.Buffer;
 import org.eclipse.jetty.io.BufferCache.CachedBuffer;
 import org.eclipse.jetty.io.Buffers;
-import org.eclipse.jetty.io.Connection;
 import org.eclipse.jetty.io.EndPoint;
 import org.eclipse.jetty.io.EofException;
 import org.eclipse.jetty.io.RuntimeIOException;
@@ -94,32 +93,27 @@
 {
 	private static final Logger LOG = LoggerFactory.getLogger(AbstractHttpConnection.class);
 
-	private static final int UNKNOWN = -2;
 	private static final ThreadLocal<AbstractHttpConnection> __currentConnection = new ThreadLocal<AbstractHttpConnection>();
 
 	private int _requests;
 
-	protected final Connector _connector;
-	protected final Server _server;
-	protected final HttpURI _uri;
+	private final Connector _connector;
+	private final Server _server;
+	protected final HttpURI _uri = StringUtil.__UTF8.equals(URIUtil.__CHARSET)?new HttpURI():new EncodedHttpURI(URIUtil.__CHARSET);
 
-	protected final Parser _parser;
-	protected final HttpFields _requestFields;
-	protected final Request _request;
-	protected volatile ServletInputStream _in;
+	protected final HttpParser _parser;
+	protected final HttpFields _requestFields = new HttpFields();
+	private final Request _request;
+	private volatile ServletInputStream _in;
 
-	protected final Generator _generator;
-	protected final HttpFields _responseFields;
+	protected final HttpGenerator _generator;
+	final HttpFields _responseFields = new HttpFields();
 	protected final Response _response;
-	protected volatile Output _out;
-	protected volatile OutputWriter _writer;
-	protected volatile PrintWriter _printWriter;
+	private volatile Output _out;
+	private volatile HttpWriter _writer;
+	private volatile PrintWriter _printWriter;
 
-	int _include;
-
-	private Object _associatedObject; // associated object
-
-	private int _version = UNKNOWN;
+	private int _version = -2;  // UNKNOWN
 
 	private String _charset;
 	private boolean _expect = false;
@@ -127,159 +121,42 @@
 	private boolean _expect102Processing = false;
 	private boolean _head = false;
 	private boolean _host = false;
-	private boolean _delayedHandling=false;
+	private boolean _delayedHandling = false;
 	private boolean _earlyEOF = false;
 
-	/* ------------------------------------------------------------ */
 	public static AbstractHttpConnection getCurrentConnection()
 	{
 		return __currentConnection.get();
 	}
 
-	/* ------------------------------------------------------------ */
 	protected static void setCurrentConnection(AbstractHttpConnection connection)
 	{
 		__currentConnection.set(connection);
 	}
 
-	/* ------------------------------------------------------------ */
-	public AbstractHttpConnection(Connector connector, EndPoint endpoint, Server server)
-	{
-		super(endpoint);
-		_uri = StringUtil.__UTF8.equals(URIUtil.__CHARSET)?new HttpURI():new EncodedHttpURI(URIUtil.__CHARSET);
-		_connector = connector;
-		HttpBuffers ab = (HttpBuffers)_connector;
-		_parser = newHttpParser(ab.getRequestBuffers(), endpoint, new RequestHandler());
-		_requestFields = new HttpFields();
-		_responseFields = new HttpFields();
-		_request = new Request(this);
-		_response = new Response(this);
-		_generator = newHttpGenerator(ab.getResponseBuffers(), endpoint);
-		_server = server;
-	}
-
-	/* ------------------------------------------------------------ */
-	protected AbstractHttpConnection(Connector connector, EndPoint endpoint, Server server,
-			Parser parser, Generator generator, Request request)
+	protected AbstractHttpConnection(Connector connector, EndPoint endpoint)
 	{
 		super(endpoint);
-
-		_uri = URIUtil.__CHARSET.equals(StringUtil.__UTF8)?new HttpURI():new EncodedHttpURI(URIUtil.__CHARSET);
 		_connector = connector;
-		_parser = parser;
-		_requestFields = new HttpFields();
-		_responseFields = new HttpFields();
-		_request = request;
+		HttpBuffers ab = _connector;
+		_parser = new HttpParser(ab.getRequestBuffers(), endpoint, new RequestHandler());
+		_request = new Request(this);
 		_response = new Response(this);
-		_generator = generator;
-		_server = server;
-	}
-
-	protected HttpParser newHttpParser(Buffers requestBuffers, EndPoint endpoint, HttpParser.EventHandler requestHandler)
-	{
-		return new HttpParser(requestBuffers, endpoint, requestHandler);
-	}
-
-	protected HttpGenerator newHttpGenerator(Buffers responseBuffers, EndPoint endPoint)
-	{
-		return new HttpGenerator(responseBuffers, endPoint);
+		_generator = new HttpGenerator(ab.getResponseBuffers(), endpoint);
+		_server = connector.server;
 	}
 
-	/* ------------------------------------------------------------ */
-	/**
-	 * @return the parser used by this connection
-	 */
-	public Parser getParser()
-	{
-		return _parser;
-	}
-
-	/* ------------------------------------------------------------ */
-	/**
-	 * @return the number of requests handled by this connection
-	 */
-	public int getRequests()
-	{
-		return _requests;
-	}
-
-	/* ------------------------------------------------------------ */
-	public Server getServer()
-	{
-		return _server;
-	}
-
-	/* ------------------------------------------------------------ */
-	/**
-	 * @return Returns the associatedObject.
-	 */
-	public Object getAssociatedObject()
-	{
-		return _associatedObject;
-	}
-
-	/* ------------------------------------------------------------ */
-	/**
-	 * @param associatedObject The associatedObject to set.
-	 */
-	public void setAssociatedObject(Object associatedObject)
-	{
-		_associatedObject = associatedObject;
-	}
-
-	/* ------------------------------------------------------------ */
-	/**
-	 * @return Returns the connector.
-	 */
-	public Connector getConnector()
+	public final Connector getConnector()
 	{
 		return _connector;
 	}
 
-	/* ------------------------------------------------------------ */
-	/**
-	 * @return Returns the requestFields.
-	 */
-	public HttpFields getRequestFields()
-	{
-		return _requestFields;
-	}
-
-	/* ------------------------------------------------------------ */
-	/**
-	 * @return Returns the responseFields.
-	 */
-	public HttpFields getResponseFields()
-	{
-		return _responseFields;
-	}
-
-	/* ------------------------------------------------------------ */
-	/**
-	 * Find out if the request supports CONFIDENTIAL security.
-	 * @param request the incoming HTTP request
-	 * @return the result of calling {@link Connector#isConfidential(Request)}, or false
-	 * if there is no connector
-	 */
-	public boolean isConfidential(Request request)
-	{
-		return _connector != null && _connector.isConfidential(request);
-	}
-
-	/* ------------------------------------------------------------ */
-	/**
-	 * @return Returns the request.
-	 */
-	public Request getRequest()
+	public final Request getRequest()
 	{
 		return _request;
 	}
 
-	/* ------------------------------------------------------------ */
-	/**
-	 * @return Returns the response.
-	 */
-	public Response getResponse()
+	public final Response getResponse()
 	{
 		return _response;
 	}
@@ -296,18 +173,18 @@
 	 * The stream will be created if it does not already exist.
 	 * @throws IOException if the input stream cannot be retrieved
 	 */
-	public ServletInputStream getInputStream() throws IOException
+	public final ServletInputStream getInputStream() throws IOException
 	{
 		// If the client is expecting 100 CONTINUE, then send it now.
 		if (_expect100Continue)
 		{
 			// is content missing?
-			if (((HttpParser)_parser).getHeaderBuffer()==null || ((HttpParser)_parser).getHeaderBuffer().length()<2)
+			if (_parser.getHeaderBuffer()==null || _parser.getHeaderBuffer().length()<2)
 			{
 				if (_generator.isCommitted())
 					throw new IllegalStateException("Committed before 100 Continues");
 
-				((HttpGenerator)_generator).send1xx(HttpStatus.CONTINUE_100);
+				_generator.send1xx(HttpStatus.CONTINUE_100);
 			}
 			_expect100Continue=false;
 		}
@@ -321,7 +198,7 @@
 	/**
 	 * @return The output stream for this connection. The stream will be created if it does not already exist.
 	 */
-	public ServletOutputStream getOutputStream()
+	public final ServletOutputStream getOutputStream()
 	{
 		if (_out == null)
 			_out = new Output();
@@ -334,12 +211,12 @@
 	 * @return A {@link PrintWriter} wrapping the {@link #getOutputStream output stream}. The writer is created if it
 	 *    does not already exist.
 	 */
-	public PrintWriter getPrintWriter(String encoding)
+	public final PrintWriter getPrintWriter(String encoding)
 	{
 		getOutputStream();
 		if (_writer==null)
 		{
-			_writer=new OutputWriter();
+			_writer = new HttpWriter(_out);
 			_printWriter = new PrintWriter(_writer)
 			{
 				public void close()
@@ -362,20 +239,12 @@
 		return _printWriter;
 	}
 
-	/* ------------------------------------------------------------ */
-	public boolean isResponseCommitted()
-	{
-		return _generator.isCommitted();
-	}
-
-	/* ------------------------------------------------------------ */
-	public boolean isEarlyEOF()
+	public final boolean isEarlyEOF()
 	{
 		return _earlyEOF;
 	}
 
-	/* ------------------------------------------------------------ */
-	public void reset()
+	protected void reset()
 	{
 		_parser.reset();
 		_parser.returnBuffers(); // TODO maybe only on unhandle
@@ -386,11 +255,11 @@
 		_responseFields.clear();
 		_response.recycle();
 		_uri.clear();
-		_writer=null;
+		_writer = null;
 		_earlyEOF = false;
 	}
 
-	protected void handleRequest() throws IOException
+	private void handleRequest() throws IOException
 	{
 		boolean error = false;
 
@@ -513,7 +382,7 @@
 	}
 
 
-	public void commitResponse(boolean last) throws IOException
+	public final void commitResponse(boolean last) throws IOException
 	{
 		if (!_generator.isCommitted())
 		{
@@ -543,8 +412,7 @@
 			_generator.complete();
 	}
 
-	/* ------------------------------------------------------------ */
-	public void completeResponse() throws IOException
+	public final void completeResponse() throws IOException
 	{
 		if (!_generator.isCommitted())
 		{
@@ -570,8 +438,7 @@
 		_generator.complete();
 	}
 
-	/* ------------------------------------------------------------ */
-	public void flushResponse() throws IOException
+	public final void flushResponse() throws IOException
 	{
 		try
 		{
@@ -584,57 +451,17 @@
 		}
 	}
 
-	/* ------------------------------------------------------------ */
-	public Generator getGenerator()
-	{
-		return _generator;
-	}
-
-	/* ------------------------------------------------------------ */
-	public boolean isIncluding()
-	{
-		return _include>0;
-	}
-
-	/* ------------------------------------------------------------ */
-	public void include()
-	{
-		_include++;
-	}
-
-	/* ------------------------------------------------------------ */
-	public void included()
-	{
-		_include--;
-		if (_out!=null)
-			_out.reopen();
-	}
-
-	public boolean isIdle()
+	public final boolean isIdle()
 	{
 		return _generator.isIdle() && (_parser.isIdle() || _delayedHandling);
 	}
 
-	/* ------------------------------------------------------------ */
-	/**
-	 * @see org.eclipse.jetty.io.Connection#isSuspended()
-	 */
-	public boolean isSuspended()
-	{
-		return false;
-	}
-
-	public boolean isExpecting100Continues()
-	{
-		return _expect100Continue;
-	}
-
-	public boolean isExpecting102Processing()
+	public final boolean isExpecting102Processing()
 	{
 		return _expect102Processing;
 	}
 
-	public int getMaxIdleTime()
+	public final int getMaxIdleTime()
 	{
 		if (_connector.isLowResources() && _endp.getMaxIdleTime()==_connector.getMaxIdleTime())
 			return 0;
@@ -643,6 +470,7 @@
 		return _connector.getMaxIdleTime();
 	}
 
+	@Override
 	public String toString()
 	{
 		return String.format("%s,g=%s,p=%s,r=%d",
@@ -652,14 +480,13 @@
 				_requests);
 	}
 
-	/* ------------------------------------------------------------ */
-	protected void startRequest(Buffer method, Buffer uri, Buffer version) throws IOException
+	private void startRequest(Buffer method, Buffer uri, Buffer version) throws IOException
 	{
 		uri=uri.asImmutableBuffer();
 
 		_host = false;
 		_expect = false;
-		_expect100Continue=false;
+		_expect100Continue = false;
 		_expect102Processing=false;
 		_delayedHandling=false;
 		_charset=null;
@@ -670,7 +497,7 @@
 
 		try
 		{
-			_head=false;
+			_head = false;
 			switch (HttpMethods.CACHE.getOrdinal(method))
 			{
 			  case HttpMethods.CONNECT_ORDINAL:
@@ -678,7 +505,7 @@
 				  break;
 
 			  case HttpMethods.HEAD_ORDINAL:
-				  _head=true;
+				  _head = true;
 				  _uri.parse(uri.array(), uri.getIndex(), uri.length());
 				  break;
 
@@ -691,11 +518,11 @@
 			if (version==null)
 			{
 				_request.setProtocol(HttpVersions.HTTP_0_9);
-				_version=HttpVersions.HTTP_0_9_ORDINAL;
+				_version = HttpVersions.HTTP_0_9_ORDINAL;
 			}
 			else
 			{
-				version= HttpVersions.CACHE.get(version);
+				version = HttpVersions.CACHE.get(version);
 				if (version==null)
 					throw new HttpException(HttpStatus.BAD_REQUEST_400,null);
 				_version = HttpVersions.CACHE.getOrdinal(version);
@@ -712,8 +539,7 @@
 		}
 	}
 
-	/* ------------------------------------------------------------ */
-	protected void parsedHeader(Buffer name, Buffer value) throws IOException
+	private void parsedHeader(Buffer name, Buffer value) throws IOException
 	{
 		int ho = HttpHeaders.CACHE.getOrdinal(name);
 		switch (ho)
@@ -730,11 +556,11 @@
 					switch(HttpHeaderValues.CACHE.getOrdinal(value))
 					{
 						case HttpHeaderValues.CONTINUE_ORDINAL:
-							_expect100Continue=_generator instanceof HttpGenerator;
+							_expect100Continue = true;
 							break;
 
 						case HttpHeaderValues.PROCESSING_ORDINAL:
-							_expect102Processing=_generator instanceof HttpGenerator;
+							_expect102Processing = true;
 							break;
 
 						default:
@@ -743,19 +569,19 @@
 							{
 								CachedBuffer cb=HttpHeaderValues.CACHE.get(values[i].trim());
 								if (cb==null)
-									_expect=true;
+									_expect = true;
 								else
 								{
 									switch(cb.getOrdinal())
 									{
 										case HttpHeaderValues.CONTINUE_ORDINAL:
-											_expect100Continue=_generator instanceof HttpGenerator;
+											_expect100Continue = true;
 											break;
 										case HttpHeaderValues.PROCESSING_ORDINAL:
-											_expect102Processing=_generator instanceof HttpGenerator;
+											_expect102Processing = true;
 											break;
 										default:
-											_expect=true;
+											_expect = true;
 									}
 								}
 							}
@@ -777,8 +603,7 @@
 		_requestFields.add(name, value);
 	}
 
-	/* ------------------------------------------------------------ */
-	protected void headerComplete() throws IOException
+	private void headerComplete() throws IOException
 	{
 		// Handle idle race
 		if (_endp.isOutputShutdown())
@@ -844,14 +669,13 @@
 			_request.setCharacterEncodingUnchecked(_charset);
 
 		// Either handle now or wait for first content
-		if ((((HttpParser)_parser).getContentLength()<=0 && !((HttpParser)_parser).isChunking())||_expect100Continue)
+		if ((_parser.getContentLength()<=0 && !_parser.isChunking())||_expect100Continue)
 			handleRequest();
 		else
-			_delayedHandling=true;
+			_delayedHandling = true;
 	}
 
-	/* ------------------------------------------------------------ */
-	protected void content(Buffer buffer) throws IOException
+	private void content(Buffer buffer) throws IOException
 	{
 		if (_delayedHandling)
 		{
@@ -860,8 +684,7 @@
 		}
 	}
 
-	/* ------------------------------------------------------------ */
-	public void messageComplete(long contentLength) throws IOException
+	private void messageComplete(long contentLength) throws IOException
 	{
 		if (_delayedHandling)
 		{
@@ -870,75 +693,44 @@
 		}
 	}
 
-	/* ------------------------------------------------------------ */
-	public void earlyEOF()
+	private void earlyEOF()
 	{
 		_earlyEOF = true;
 	}
 
-	/* ------------------------------------------------------------ */
-	/* ------------------------------------------------------------ */
-	/* ------------------------------------------------------------ */
+
 	private class RequestHandler extends HttpParser.EventHandler
 	{
-		/*
-		 *
-		 * @see org.eclipse.jetty.server.server.HttpParser.EventHandler#startRequest(org.eclipse.io.Buffer,
-		 *      org.eclipse.io.Buffer, org.eclipse.io.Buffer)
-		 */
 		@Override
 		public void startRequest(Buffer method, Buffer uri, Buffer version) throws IOException
 		{
 			AbstractHttpConnection.this.startRequest(method, uri, version);
 		}
 
-		/*
-		 * @see org.eclipse.jetty.server.server.HttpParser.EventHandler#parsedHeaderValue(org.eclipse.io.Buffer)
-		 */
 		@Override
 		public void parsedHeader(Buffer name, Buffer value) throws IOException
 		{
 			AbstractHttpConnection.this.parsedHeader(name, value);
 		}
 
-		/*
-		 * @see org.eclipse.jetty.server.server.HttpParser.EventHandler#headerComplete()
-		 */
 		@Override
 		public void headerComplete() throws IOException
 		{
 			AbstractHttpConnection.this.headerComplete();
 		}
 
-		/* ------------------------------------------------------------ */
-		/*
-		 * @see org.eclipse.jetty.server.server.HttpParser.EventHandler#content(int, org.eclipse.io.Buffer)
-		 */
 		@Override
 		public void content(Buffer ref) throws IOException
 		{
 			AbstractHttpConnection.this.content(ref);
 		}
 
-		/* ------------------------------------------------------------ */
-		/*
-		 * (non-Javadoc)
-		 *
-		 * @see org.eclipse.jetty.server.server.HttpParser.EventHandler#messageComplete(int)
-		 */
 		@Override
 		public void messageComplete(long contentLength) throws IOException
 		{
 			AbstractHttpConnection.this.messageComplete(contentLength);
 		}
 
-		/* ------------------------------------------------------------ */
-		/*
-		 * (non-Javadoc)
-		 *
-		 * @see org.eclipse.jetty.server.server.HttpParser.EventHandler#startResponse(org.eclipse.io.Buffer, int,
-		 *      org.eclipse.io.Buffer)
-		 */
 		@Override
 		public void startResponse(Buffer version, int status, Buffer reason)
 		{
@@ -946,12 +738,6 @@
 				LOG.debug("Bad request!: "+version+" "+status+" "+reason);
 		}
 
-		/* ------------------------------------------------------------ */
-		/*
-		 * (non-Javadoc)
-		 *
-		 * @see org.eclipse.jetty.server.server.HttpParser.EventHandler#earlyEOF()
-		 */
 		@Override
 		public void earlyEOF()
 		{
@@ -959,10 +745,8 @@
 		}
 	}
 
-	/* ------------------------------------------------------------ */
-	/* ------------------------------------------------------------ */
-	/* ------------------------------------------------------------ */
-	public class Output extends HttpOutput
+
+	public final class Output extends HttpOutput
 	{
 		Output()
 		{
@@ -979,7 +763,7 @@
 			if (isClosed())
 				return;
 
-			if (!isIncluding() && !super._generator.isCommitted())
+			if (!super._generator.isCommitted())
 				commitResponse(Generator.LAST);
 			else
 				flushResponse();
@@ -1081,16 +865,4 @@
 		}
 	}
 
-	/* ------------------------------------------------------------ */
-	/* ------------------------------------------------------------ */
-	/* ------------------------------------------------------------ */
-	public class OutputWriter extends HttpWriter
-	{
-		OutputWriter()
-		{
-			super(AbstractHttpConnection.this._out);
-		}
-	}
-
-
 }