view src/org/eclipse/jetty/server/BlockingHttpConnection.java @ 1020:6be43ef1eb96

HttpHeaderValues uses StringCache
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 31 Oct 2016 22:24:41 -0600
parents dbecd7faa1f5
children 2ea54e6117c3
line wrap: on
line source

//
//  ========================================================================
//  Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
//  ------------------------------------------------------------------------
//  All rights reserved. This program and the accompanying materials
//  are made available under the terms of the Eclipse Public License v1.0
//  and Apache License v2.0 which accompanies this distribution.
//
//      The Eclipse Public License is available at
//      http://www.eclipse.org/legal/epl-v10.html
//
//      The Apache License v2.0 is available at
//      http://www.opensource.org/licenses/apache2.0.php
//
//  You may elect to redistribute this code under either of these licenses.
//  ========================================================================
//

package org.eclipse.jetty.server;

import java.io.IOException;

import org.eclipse.jetty.http.HttpException;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.io.EndPoint;
import org.eclipse.jetty.io.nio.ChannelEndPoint;
import org.eclipse.jetty.server.nio.BlockingChannelConnector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/* ------------------------------------------------------------ */
/** Blocking Server HTTP Connection
 */
public final class BlockingHttpConnection extends AbstractHttpConnection
{
	private static final Logger LOG = LoggerFactory.getLogger(BlockingHttpConnection.class);

	public BlockingHttpConnection(BlockingChannelConnector connector, ChannelEndPoint endpoint)
	{
		super(connector,endpoint);
	}

	@Override
	public void handle() throws IOException
	{
		try
		{
			// do while the endpoint is open
			// AND the connection has not changed
			while (_endp.isOpen())
			{
				try
				{
					// If we are not ended then parse available
					if (!_parser.isComplete() && !_endp.isInputShutdown())
						_parser.parseAvailable();

					// Do we have more generating to do?
					// Loop here because some writes may take multiple steps and
					// we need to flush them all before potentially blocking in the
					// next loop.
					if (_generator.isCommitted() && !_generator.isComplete() && !_endp.isOutputShutdown())
						_generator.flushBuffer();

					// Flush buffers
					_endp.flush();
				}
				catch (HttpException e)
				{
					if (LOG.isDebugEnabled())
					{
						LOG.debug("uri="+_uri);
						LOG.debug("fields="+_requestFields);
						LOG.debug("",e);
					}
					_generator.sendError(e.getStatus(), e.getReason(), null, true);
					_parser.reset();
					_endp.shutdownOutput();
				}
				finally
				{
					//  Is this request/response round complete and are fully flushed?
					if (_parser.isComplete() && _generator.isComplete())
					{
						// Reset the parser/generator
						reset();

						// TODO Is this required?
						if (!_generator.isPersistent() && !_endp.isOutputShutdown())
						{
							LOG.warn("Safety net oshut!!! Please open a bugzilla");
							_endp.shutdownOutput();
						}
					}
					
					// If we don't have a committed response and we are not suspended
					if (_endp.isInputShutdown() && _generator.isIdle())
					{
						// then no more can happen, so close.
						_endp.close();
					}
				}
			}
		}
		finally
		{
			_parser.returnBuffers();
			_generator.returnBuffers();
		}
	}
}