view src/org/eclipse/jetty/server/BlockingHttpConnection.java @ 971:f997df37cec1

minor
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 14 Oct 2016 13:06:06 -0600
parents 866f2e801618
children 5ee36654b383
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.Generator;
import org.eclipse.jetty.http.HttpException;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.http.Parser;
import org.eclipse.jetty.io.Connection;
import org.eclipse.jetty.io.EndPoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


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

	public BlockingHttpConnection(Connector connector, EndPoint endpoint, Server server)
	{
		super(connector,endpoint,server);
	}

	public BlockingHttpConnection(Connector connector, EndPoint endpoint, Server server, Parser parser, Generator generator, Request request)
	{
		super(connector,endpoint,server,parser,generator,request);
	}

	@Override
	protected void handleRequest() throws IOException
	{
		super.handleRequest();
	}

	@Override
	public void handle() throws IOException
	{
		try
		{
			setCurrentConnection(this);

			// 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
		{
			setCurrentConnection(null);
			_parser.returnBuffers();
			_generator.returnBuffers();
		}
	}
}