📄 httpconnection.java
字号:
//========================================================================//$Id: HttpConnection.java,v 1.13 2005/11/25 21:01:45 gregwilkins Exp $//Copyright 2004-2005 Mort Bay Consulting Pty. Ltd.//------------------------------------------------------------------------//Licensed under the Apache License, Version 2.0 (the "License");//you may not use this file except in compliance with the License.//You may obtain a copy of the License at//http://www.apache.org/licenses/LICENSE-2.0//Unless required by applicable law or agreed to in writing, software//distributed under the License is distributed on an "AS IS" BASIS,//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.//See the License for the specific language governing permissions and//limitations under the License.//========================================================================package org.mortbay.jetty;import java.io.IOException;import java.io.InputStream;import java.io.PrintWriter;import javax.servlet.ServletInputStream;import javax.servlet.ServletOutputStream;import javax.servlet.http.HttpServletResponse;import org.mortbay.io.Buffer;import org.mortbay.io.Connection;import org.mortbay.io.EndPoint;import org.mortbay.io.BufferCache.CachedBuffer;import org.mortbay.io.nio.SelectChannelEndPoint;import org.mortbay.log.Log;import org.mortbay.resource.Resource;import org.mortbay.util.QuotedStringTokenizer;import org.mortbay.util.StringUtil;import org.mortbay.util.URIUtil;import org.mortbay.util.ajax.Continuation;/** * <p> * A HttpConnection represents the connection of a HTTP client to the server and * is created by an instance of a {@link Connector}. It's prime function is to * associate {@link Request} and {@link Response} instances with a * {@link EndPoint}. * </p> * <p> * A connection is also the prime mechanism used by jetty to recycle objects * without pooling. The {@link Request},{@link Response}, {@link HttpParser}, * {@link HttpGenerator} and {@link HttpFields} instances are all recycled for * the duraction of a connection. Where appropriate, allocated buffers are also * kept associated with the connection via the parser and/or generator. * </p> * * * @author gregw * */public class HttpConnection implements Connection{ private static int UNKNOWN = -2; private static ThreadLocal __currentConnection = new ThreadLocal(); private long _timeStamp = System.currentTimeMillis(); private int _requests; private boolean _handling; private boolean _destroy; protected final Connector _connector; protected final EndPoint _endp; protected final Server _server; protected final HttpURI _uri; protected final Parser _parser; protected final HttpFields _requestFields; protected final Request _request; protected ServletInputStream _in; protected final Generator _generator; protected final HttpFields _responseFields; protected final Response _response; protected Output _out; protected OutputWriter _writer; protected PrintWriter _printWriter; int _include; private Object _associatedObject; // associated object private transient int _expect = UNKNOWN; private transient int _version = UNKNOWN; private transient boolean _head = false; private transient boolean _host = false; private transient boolean _delayedHandling = false; /* ------------------------------------------------------------ */ public static HttpConnection getCurrentConnection() { return (HttpConnection)__currentConnection.get(); } /* ------------------------------------------------------------ */ protected static void setCurrentConnection(HttpConnection connection) { __currentConnection.set(connection); } /* ------------------------------------------------------------ */ /** * Constructor * */ public HttpConnection(Connector connector, EndPoint endpoint, Server server) { _uri = URIUtil.__CHARSET==StringUtil.__UTF8?new HttpURI():new EncodedHttpURI(URIUtil.__CHARSET); _connector = connector; _endp = endpoint; _parser = new HttpParser(_connector,endpoint,new RequestHandler(),_connector.getHeaderBufferSize(),_connector.getRequestBufferSize()); _requestFields = new HttpFields(); _responseFields = new HttpFields(); _request = new Request(this); _response = new Response(this); _generator = new HttpGenerator(_connector,_endp,_connector.getHeaderBufferSize(),_connector.getResponseBufferSize()); _generator.setSendServerVersion(server.getSendServerVersion()); _server = server; } protected HttpConnection(Connector connector, EndPoint endpoint, Server server, Parser parser, Generator generator, Request request) { _uri = URIUtil.__CHARSET==StringUtil.__UTF8?new HttpURI():new EncodedHttpURI(URIUtil.__CHARSET); _connector = connector; _endp = endpoint; _parser = parser; _requestFields = new HttpFields(); _responseFields = new HttpFields(); _request = request; _response = new Response(this); _generator = generator; _generator.setSendServerVersion(server.getSendServerVersion()); _server = server; } /* ------------------------------------------------------------ */ public void destroy() { synchronized (this) { _destroy = true; if (!_handling) { if (_parser != null) _parser.reset(true); if (_generator != null) _generator.reset(true); if (_requestFields != null) _requestFields.destroy(); if (_responseFields != null) _responseFields.destroy(); } } } /* ------------------------------------------------------------ */ /** * @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; } /* ------------------------------------------------------------ */ /** * @return The time this connection was established. */ public long getTimeStamp() { return _timeStamp; } /* ------------------------------------------------------------ */ /** * @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() { return _connector; } /* ------------------------------------------------------------ */ /** * @return Returns the requestFields. */ public HttpFields getRequestFields() { return _requestFields; } /* ------------------------------------------------------------ */ /** * @return Returns the responseFields. */ public HttpFields getResponseFields() { return _responseFields; } /* ------------------------------------------------------------ */ /** * @return The result of calling {@link #getConnector}. * {@link Connector#isConfidential(Request) isCondidential} * (request), or false if there is no connector. */ public boolean isConfidential(Request request) { if (_connector != null) return _connector.isConfidential(request); return false; } /* ------------------------------------------------------------ */ /** * Find out if the request is INTEGRAL security. * * @param request * @return <code>true</code> if there is a {@link #getConnector() connector} * and it considers <code>request</code> to be * {@link Connector#isIntegral(Request) integral} */ public boolean isIntegral(Request request) { if (_connector != null) return _connector.isIntegral(request); return false; } /* ------------------------------------------------------------ */ /** * @return The {@link EndPoint} for this connection. */ public EndPoint getEndPoint() { return _endp; } /* ------------------------------------------------------------ */ /** * @return <code>false</code> (this method is not yet implemented) */ public boolean getResolveNames() { return _connector.getResolveNames(); } /* ------------------------------------------------------------ */ /** * @return Returns the request. */ public Request getRequest() { return _request; } /* ------------------------------------------------------------ */ /** * @return Returns the response. */ public Response getResponse() { return _response; } /* ------------------------------------------------------------ */ /** * @return The input stream for this connection. The stream will be created * if it does not already exist. */ public ServletInputStream getInputStream() { if (_in == null) _in = new HttpParser.Input(((HttpParser)_parser),_connector.getMaxIdleTime()); return _in; } /* ------------------------------------------------------------ */ /** * @return The output stream for this connection. The stream will be created * if it does not already exist. */ public ServletOutputStream getOutputStream() { if (_out == null) _out = new Output(); return _out; } /* ------------------------------------------------------------ */ /** * @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) { getOutputStream(); if (_writer == null) { _writer = new OutputWriter(); _printWriter = new PrintWriter(_writer) { /* ------------------------------------------------------------ */ /* * @see java.io.PrintWriter#close() */ public void close() { try { out.close(); } catch (IOException e) { Log.debug(e); setError(); } } }; } _writer.setCharacterEncoding(encoding); return _printWriter; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -