📄 request.java
字号:
//========================================================================//$Id: Request.java,v 1.15 2005/11/16 22:02:40 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.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.UnsupportedEncodingException;import java.net.InetAddress;import java.nio.ByteBuffer;import java.security.Principal;import java.util.Collection;import java.util.Collections;import java.util.Enumeration;import java.util.EventListener;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Locale;import java.util.Map;import javax.servlet.RequestDispatcher;import javax.servlet.ServletContext;import javax.servlet.ServletInputStream;import javax.servlet.ServletRequestAttributeEvent;import javax.servlet.ServletRequestAttributeListener;import javax.servlet.ServletRequestListener;import javax.servlet.ServletRequestWrapper;import javax.servlet.ServletResponse;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpSession;import org.mortbay.io.Buffer;import org.mortbay.io.BufferUtil;import org.mortbay.io.EndPoint;import org.mortbay.io.Portable;import org.mortbay.io.nio.DirectNIOBuffer;import org.mortbay.io.nio.IndirectNIOBuffer;import org.mortbay.io.nio.NIOBuffer;import org.mortbay.jetty.handler.ContextHandler;import org.mortbay.jetty.handler.ContextHandler.SContext;import org.mortbay.jetty.security.Authenticator;import org.mortbay.jetty.security.SecurityHandler;import org.mortbay.jetty.security.UserRealm;import org.mortbay.log.Log;import org.mortbay.util.Attributes;import org.mortbay.util.AttributesMap;import org.mortbay.util.LazyList;import org.mortbay.util.MultiMap;import org.mortbay.util.StringUtil;import org.mortbay.util.URIUtil;import org.mortbay.util.UrlEncoded;import org.mortbay.util.ajax.Continuation;/* ------------------------------------------------------------ *//** Jetty Request. * <p> * Implements {@link javax.servlet.HttpServletRequest} from the {@link javax.servlet} package. * </p> * <p> * The standard interface of mostly getters, * is extended with setters so that the request is mutable by the handlers that it is * passed to. This allows the request object to be as lightweight as possible and not * actually implement any significant behaviour. For example<ul> * * <li>The {@link getContextPath} method will return null, until the requeset has been * passed to a {@link ContextHandler} which matches the {@link getPathInfo} with a context * path and calls {@link setContextPath} as a result.</li> * * <li>the HTTP session methods * will all return null sessions until such time as a request has been passed to * a {@link org.mortbay.jetty.servlet.SessionHandler} which checks for session cookies * and enables the ability to create new sessions.</li> * * <li>The {@link getServletPath} method will return null until the request has been * passed to a {@link org.mortbay.jetty.servlet.ServletHandler} and the pathInfo matched * against the servlet URL patterns and {@link setServletPath} called as a result.</li> * </ul> * * A request instance is created for each {@link HttpConnection} accepted by the server * and recycled for each HTTP request received via that connection. An effort is made * to avoid reparsing headers and cookies that are likely to be the same for * requests from the same connection. * * @author gregw * */public class Request implements HttpServletRequest{ private static final byte STATE_DELIMITER = 1; private static final byte STATE_NAME = 2; private static final byte STATE_VALUE = 4; private static final byte STATE_QUOTED_VALUE = 8; private static final byte STATE_UNQUOTED_VALUE = 16; private static final Collection __defaultLocale = Collections.singleton(Locale.getDefault()); private static final int __NONE=0, _STREAM=1, __READER=2; private boolean _handled =false; private HttpConnection _connection; private EndPoint _endp; private Map _roleMap; private Attributes _attributes; private String _authType; private String _characterEncoding; private String _queryEncoding; private String _serverName; private String _remoteAddr; private String _remoteHost; private String _method; private String _pathInfo; private int _port; private String _protocol=HttpVersions.HTTP_1_1; private String _queryString; private String _requestedSessionId; private boolean _requestedSessionIdFromCookie=false; private String _requestURI; private String _scheme=URIUtil.HTTP; private String _contextPath; private String _servletPath; private String _servletName; private HttpURI _uri; private Principal _userPrincipal; private MultiMap _parameters; private MultiMap _baseParameters; private boolean _paramsExtracted; private int _inputState=__NONE; private BufferedReader _reader; private String _readerEncoding; private boolean _dns=false; private ContextHandler.SContext _context; private HttpSession _session; private SessionManager _sessionManager; private boolean _cookiesExtracted=false; private Cookie[] _cookies; private String[] _lastCookies; private long _timeStamp; private Buffer _timeStampBuffer; private Continuation _continuation; private Object _requestAttributeListeners; private Object _requestListeners; private Map _savedNewSessions; private UserRealm _userRealm; /* ------------------------------------------------------------ */ /** * */ public Request() { } /* ------------------------------------------------------------ */ /** * */ public Request(HttpConnection connection) { _connection=connection; _endp=connection.getEndPoint(); _dns=_connection.getResolveNames(); } /* ------------------------------------------------------------ */ protected void setConnection(HttpConnection connection) { _connection=connection; _endp=connection.getEndPoint(); _dns=connection.getResolveNames(); } /* ------------------------------------------------------------ */ protected void recycle() { _handled=false; if (_context!=null) throw new IllegalStateException("Request in context!"); if(_attributes!=null) _attributes.clearAttributes(); _authType=null; _characterEncoding=null; _queryEncoding=null; _context=null; _serverName=null; _method=null; _pathInfo=null; _port=0; _protocol=HttpVersions.HTTP_1_1; _queryString=null; _requestedSessionId=null; _requestedSessionIdFromCookie=false; _session=null; _requestURI=null; _scheme=URIUtil.HTTP; _servletPath=null; _timeStamp=0; _timeStampBuffer=null; _uri=null; _userPrincipal=null; if (_baseParameters!=null) _baseParameters.clear(); _parameters=null; _paramsExtracted=false; _inputState=__NONE; _cookiesExtracted=false; if (_savedNewSessions!=null) _savedNewSessions.clear(); _savedNewSessions=null; if (_continuation!=null && _continuation.isPending()) _continuation.reset(); } /* ------------------------------------------------------------ */ /** * Get Request TimeStamp * * @return The time that the request was received. */ public Buffer getTimeStampBuffer() { if (_timeStampBuffer == null && _timeStamp > 0) _timeStampBuffer = HttpFields.__dateCache.formatBuffer(_timeStamp); return _timeStampBuffer; } /* ------------------------------------------------------------ */ /** * Get Request TimeStamp * * @return The time that the request was received. */ public long getTimeStamp() { return _timeStamp; } /* ------------------------------------------------------------ */ public void setTimeStamp(long ts) { _timeStamp = ts; } /* ------------------------------------------------------------ */ public boolean isHandled() { return _handled; } /* ------------------------------------------------------------ */ public void setHandled(boolean h) { _handled=h; } /* ------------------------------------------------------------ */ /* * @see javax.servlet.ServletRequest#getAttribute(java.lang.String) */ public Object getAttribute(String name) { if ("org.mortbay.jetty.ajax.Continuation".equals(name)) return getContinuation(true); if (_attributes==null) return null; return _attributes.getAttribute(name); } /* ------------------------------------------------------------ */ /* * @see javax.servlet.ServletRequest#getAttributeNames() */ public Enumeration getAttributeNames() { if (_attributes==null) return Collections.enumeration(Collections.EMPTY_LIST); return AttributesMap.getAttributeNamesCopy(_attributes); } /* ------------------------------------------------------------ */ /* * @see javax.servlet.http.HttpServletRequest#getAuthType() */ public String getAuthType() { return _authType; } /* ------------------------------------------------------------ */ /* * @see javax.servlet.ServletRequest#getCharacterEncoding() */ public String getCharacterEncoding() { return _characterEncoding; } public long getContentRead() { if (_connection==null || _connection.getParser()==null) return -1; return ((HttpParser)_connection.getParser()).getContentRead(); } /* ------------------------------------------------------------ */ /* * @see javax.servlet.ServletRequest#getContentLength() */ public int getContentLength() { return (int)_connection.getRequestFields().getLongField(HttpHeaders.CONTENT_LENGTH_BUFFER); } /* ------------------------------------------------------------ */ /* * @see javax.servlet.ServletRequest#getContentType() */ public String getContentType() { return _connection.getRequestFields().getStringField(HttpHeaders.CONTENT_TYPE_BUFFER); } /* ------------------------------------------------------------ */ /* * @see javax.servlet.ServletRequest#getContentType() */ public void setContentType(String contentType) { _connection.getRequestFields().put(HttpHeaders.CONTENT_TYPE_BUFFER,contentType); } /* ------------------------------------------------------------ */ /* * @see javax.servlet.http.HttpServletRequest#getContextPath() */ public String getContextPath() { return _contextPath; } /* ------------------------------------------------------------ */ /* * @see javax.servlet.http.HttpServletRequest#getCookies() */ public Cookie[] getCookies() { if (_cookiesExtracted) return _cookies; try { // Handle no cookies if (!_connection.getRequestFields().containsKey(HttpHeaders.COOKIE_BUFFER)) { _cookies = null; _cookiesExtracted = true; _lastCookies = null; return _cookies; } // Check if cookie headers match last cookies if (_lastCookies != null) { int last = 0; Enumeration enm = _connection.getRequestFields().getValues(HttpHeaders.COOKIE_BUFFER); while (enm.hasMoreElements()) { String c = (String)enm.nextElement(); if (last >= _lastCookies.length || !c.equals(_lastCookies[last])) { _lastCookies = null; break; } last++; } if (_lastCookies != null && _lastCookies.length==last) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -