abstracthttprequest.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 2,711 行 · 第 1/4 页
JAVA
2,711 行
/* * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved * * This file is part of Resin(R) Open Source * * Each copy or derived work must preserve the copyright notice and this * notice unmodified. * * Resin Open Source is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Resin Open Source is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty * of NON-INFRINGEMENT. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License * along with Resin Open Source; if not, write to the * * Free Software Foundation, Inc. * 59 Temple Place, Suite 330 * Boston, MA 02111-1307 USA * * @author Scott Ferguson */package com.caucho.server.connection;import com.caucho.i18n.CharacterEncoding;import com.caucho.security.SecurityContext;import com.caucho.security.SecurityContextProvider;import com.caucho.server.dispatch.DispatchServer;import com.caucho.server.dispatch.Invocation;import com.caucho.server.port.TcpConnection;import com.caucho.server.security.AbstractAuthenticator;import com.caucho.server.security.AbstractLogin;import com.caucho.server.session.SessionImpl;import com.caucho.server.session.SessionManager;import com.caucho.server.webapp.WebApp;import com.caucho.util.*;import com.caucho.vfs.BufferedReaderAdapter;import com.caucho.vfs.Encoding;import com.caucho.vfs.Path;import com.caucho.vfs.ReadStream;import javax.servlet.RequestDispatcher;import javax.servlet.ServletContext;import javax.servlet.ServletException;import javax.servlet.ServletInputStream;import javax.servlet.ServletRequestAttributeEvent;import javax.servlet.ServletRequestAttributeListener;import javax.servlet.ServletResponse;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import java.io.BufferedReader;import java.io.IOException;import java.io.UnsupportedEncodingException;import java.net.InetAddress;import java.security.Principal;import java.security.cert.X509Certificate;import java.util.ArrayList;import java.util.Collections;import java.util.Enumeration;import java.util.HashMap;import java.util.Locale;import java.util.Map;import java.util.logging.Level;import java.util.logging.Logger;/** * Abstract request implementing methods common to the different * request implementations. */public abstract class AbstractHttpRequest implements CauchoRequest, SecurityContextProvider{ protected static final Logger log = Logger.getLogger(AbstractHttpRequest.class.getName()); static final L10N L = new L10N(AbstractHttpRequest.class); protected static final CaseInsensitiveIntMap _headerCodes; public static final String REQUEST_URI = "javax.servlet.include.request_uri"; public static final String CONTEXT_PATH = "javax.servlet.include.context_path"; public static final String SERVLET_PATH = "javax.servlet.include.servlet_path"; public static final String PATH_INFO = "javax.servlet.include.path_info"; public static final String QUERY_STRING = "javax.servlet.include.query_string"; public static final String STATUS_CODE = "javax.servlet.error.status_code"; public static final String EXCEPTION_TYPE = "javax.servlet.error.exception_type"; public static final String MESSAGE = "javax.servlet.error.message"; public static final String EXCEPTION = "javax.servlet.error.exception"; public static final String ERROR_URI = "javax.servlet.error.request_uri"; public static final String SERVLET_NAME = "javax.servlet.error.servlet_name"; public static final String JSP_EXCEPTION = "javax.servlet.jsp.jspException"; public static final String SHUTDOWN = "com.caucho.shutdown"; private static final String CHAR_ENCODING = "resin.form.character.encoding"; private static final String FORM_LOCALE = "resin.form.local"; private static final String CAUCHO_CHAR_ENCODING = "caucho.form.character.encoding"; private static final char []CONNECTION = "connection".toCharArray(); private static final char []COOKIE = "cookie".toCharArray(); private static final char []CONTENT_LENGTH = "content-length".toCharArray(); private static final char []EXPECT = "expect".toCharArray(); private static final char []HOST = "host".toCharArray(); private static final char []CONTINUE_100 = "100-continue".toCharArray(); private static final char []CLOSE = "close".toCharArray(); private static final boolean []TOKEN; private static final boolean []VALUE; private static final ServletRequestAttributeListener []NULL_LISTENERS = new ServletRequestAttributeListener[0]; private static final Cookie []NULL_COOKIES = new Cookie[0]; protected final DispatchServer _server; protected final Connection _conn; protected final TcpConnection _tcpConn; protected AbstractHttpResponse _response; protected Invocation _invocation; private SecurityContextProvider _oldProvider; private String _runAs; private boolean _keepalive; private long _startTime; protected CharSegment _hostHeader; protected boolean _expect100Continue; private Cookie []_cookiesIn; private ArrayList<Cookie> _cookies = new ArrayList<Cookie>(); // True if the page depends on cookies private boolean _varyCookies; // The cookie the page depends on private String _varyCookie; private boolean _hasCookie; private boolean _isSessionIdFromCookie; protected int _sessionGroup; private long _contentLength; private boolean _sessionIsLoaded; private SessionImpl _session; // Connection stream protected final ReadStream _rawRead; // Stream for reading post contents protected final ReadStream _readStream; // True if the post stream has been initialized protected boolean _hasReadStream; // Servlet input stream for post contents private final ServletInputStreamImpl _is = new ServletInputStreamImpl(); // character incoding for a Post private String _readEncoding; // Reader for post contents private final BufferedReaderAdapter _bufferedReader; private boolean _hasReader; private boolean _hasInputStream; // HttpServletRequest stuff private final Form _formParser = new Form(); private final HashMapImpl<String,String[]> _form = new HashMapImpl<String,String[]>(); private HashMapImpl<String,String[]> _filledForm; private final HashMapImpl<String,Object> _attributes = new HashMapImpl<String,Object>(); private ArrayList<Locale> _locales = new ArrayList<Locale>(); private ArrayList<Path> _closeOnExit = new ArrayList<Path>(); // Efficient date class for printing date headers protected final QDate _calendar = new QDate(); private final CharBuffer _cbName = new CharBuffer(); private final CharBuffer _cbValue = new CharBuffer(); protected final CharBuffer _cb = new CharBuffer(); // private final ArrayList<CharSegment> _arrayList = new ArrayList<CharSegment>(); private final byte []_address = new byte[256]; private final byte []_logBuffer = new byte[1024]; private ServletRequestAttributeListener []_attributeListeners; /** * Create a new Request. Because the actual initialization occurs with * the start() method, this just allocates statics. * * @param server the parent server */ protected AbstractHttpRequest(DispatchServer server, Connection conn) { _server = server; _conn = conn; if (conn != null) _rawRead = conn.getReadStream(); else _rawRead = null; if (conn instanceof TcpConnection) _tcpConn = (TcpConnection) conn; else _tcpConn = null; _readStream = new ReadStream(); _readStream.setReuseBuffer(true); _bufferedReader = new BufferedReaderAdapter(_readStream); } /** * Initialization. */ public void init() { } /** * Returns the connection. */ public final Connection getConnection() { return _conn; } /** * returns the dispatch server. */ public final DispatchServer getDispatchServer() { return _server; } /** * Called when the connection starts */ public void startConnection() { } /** * Prepare the Request object for a new request. * * @param s the raw connection stream */ protected void start() throws IOException { startResume(); _invocation = null; _varyCookies = false; _varyCookie = null; _hasCookie = false; _hostHeader = null; _expect100Continue = false; _cookiesIn = null; _cookies.clear(); _contentLength = -1; _sessionGroup = -1; _session = null; _sessionIsLoaded = false; _hasReadStream = false; _hasReader = false; _hasInputStream = false; _filledForm = null; _locales.clear(); _readEncoding = null; _keepalive = true; _isSessionIdFromCookie = false; _oldProvider = null; _runAs = null; _attributeListeners = NULL_LISTENERS; if (_attributes.size() > 0) _attributes.clear(); } /** * Prepare the Request object for a new request. * * @param s the raw connection stream */ protected void startResume() throws IOException { _oldProvider = SecurityContext.setProvider(this); if (_tcpConn != null) _tcpConn.beginActive(); else _startTime = Alarm.getCurrentTime(); } /** * Returns true if client disconnects should be ignored. */ public boolean isIgnoreClientDisconnect() { // server/183c Invocation invocation = _invocation; if (invocation == null) return true; else { WebApp webApp = invocation.getWebApp(); if (webApp != null) return webApp.isIgnoreClientDisconnect(); else return true; } } /** * Returns the response for this request. */ public CauchoResponse getResponse() { return _response; } /** * Returns the local server name. */ public String getServerName() { String host = _conn.getVirtualHost(); /* if (host == null && _invocation != null) host = _invocation.getHostName(); */ CharSequence rawHost; if (host == null && (rawHost = getHost()) != null) { if (rawHost instanceof CharSegment) { CharSegment cb = (CharSegment) rawHost; char []buffer = cb.getBuffer(); int offset = cb.getOffset(); int length = cb.getLength(); for (int i = length - 1; i >= 0; i--) { char ch = buffer[i + offset]; if ('A' <= ch && ch <= 'Z') buffer[i + offset] = (char) (ch + 'a' - 'A'); } host = new String(buffer, offset, length); } else return rawHost.toString().toLowerCase(); } if (host == null) { InetAddress addr = _conn.getLocalAddress(); return addr.getHostName(); } int p1 = host.lastIndexOf('/'); if (p1 < 0) p1 = 0; int p = host.lastIndexOf(':'); if (p >= 0 && p1 < p) return host.substring(p1, p); else return host; } protected CharSequence getHost() { return null; } /** * Returns the server's port. */ public int getServerPort() { String host = _conn.getVirtualHost(); CharSequence rawHost; if (host == null && (rawHost = getHost()) != null) { int length = rawHost.length(); int i; for (i = length - 1; i >= 0; i--) { if (rawHost.charAt(i) == ':') { int port = 0; for (i++; i < length; i++) { char ch = rawHost.charAt(i); if ('0' <= ch && ch <= '9') port = 10 * port + ch - '0'; } return port; } } return isSecure() ? 443 : 80; } if (host == null) return _conn.getLocalPort(); int p1 = host.lastIndexOf(':'); if (p1 < 0) return isSecure() ? 443 : 80; else { int length = host.length(); int port = 0; for (int i = p1 + 1; i < length; i++) { char ch = host.charAt(i); if ('0' <= ch && ch <= '9') port = 10 * port + ch - '0'; } return port; } } /** * Returns the local port. */ public int getLocalPort() { return _conn.getLocalPort(); } /** * Returns the server's address. */ public String getLocalAddr() { return _conn.getLocalAddress().getHostAddress(); } /** * Returns the server's address. */ public String getLocalName() { return _conn.getLocalAddress().getHostAddress(); } public String getRemoteAddr() { return _conn.getRemoteHost(); } public int printRemoteAddr(byte []buffer, int offset) throws IOException { int len = _conn.getRemoteAddress(buffer, offset, buffer.length - offset); return offset + len; } public String getRemoteHost() { return _conn.getRemoteHost(); } /** * Returns the local port. */ public int getRemotePort() { return _conn.getRemotePort(); } /** * Returns the request's scheme. */ public String getScheme() { return isSecure() ? "https" : "http"; } abstract public String getProtocol(); abstract public String getMethod(); /** * Returns the URI for the request */ public String getRequestURI() { if (_invocation != null) return _invocation.getRawURI(); else return ""; } /** * Returns the URI for the page. getPageURI and getRequestURI differ * for included files. getPageURI gets the URI for the included page. * getRequestURI returns the original URI. */ public String getPageURI() { return _invocation.getRawURI(); } public abstract byte []getUriBuffer(); public abstract int getUriLength(); /** * Returns the context part of the uri. The context part is the part * that maps to an webApp. */ public String getContextPath() { return _invocation.getContextPath(); } /** * Returns the context part of the uri. For included files, this will * return the included context-path. */ public String getPageContextPath() { return getContextPath(); } /** * Returns the portion of the uri mapped to the servlet for the original * request. */ public String getServletPath() { return _invocation.getServletPath(); } /** * Returns the portion of the uri mapped to the servlet for the current * page. */ public String getPageServletPath() { return _invocation.getServletPath(); } /** * Returns the portion of the uri after the servlet path for the original * request. */ public String getPathInfo() { return _invocation.getPathInfo(); } /** * Returns the portion of the uri after the servlet path for the current * page. */ public String getPagePathInfo() { return _invocation.getPathInfo(); } /** * Returns the URL for the request */ public StringBuffer getRequestURL() { StringBuffer sb = new StringBuffer(); sb.append(getScheme()); sb.append("://"); sb.append(getServerName()); int port = getServerPort(); if (port > 0 && port != 80 && port != 443) { sb.append(":"); sb.append(port); } sb.append(getRequestURI()); return sb; } /** * @deprecated As of JSDK 2.1 */ public String getRealPath(String path) { if (path == null) return null; if (path.length() > 0 && path.charAt(0) == '/') return _invocation.getWebApp().getRealPath(path); String uri = getPageURI(); String context = getPageContextPath(); if (context != null) uri = uri.substring(context.length()); int p = uri.lastIndexOf('/'); if (p >= 0) path = uri.substring(0, p + 1) + path; return _invocation.getWebApp().getRealPath(path); } /** * Returns the real path of pathInfo. */ public String getPathTranslated() { String pathInfo = getPathInfo(); if (pathInfo == null) return null; else return getRealPath(pathInfo); } /** * Returns the current page's query string. */ public String getQueryString() { if (_invocation != null) return _invocation.getQueryString(); else return null; } /** * Returns the current page's query string. */ public String getPageQueryString() {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?