jigsawhttpservletrequest.java

来自「很棒的web服务器源代码」· Java 代码 · 共 1,199 行 · 第 1/3 页

JAVA
1,199
字号
// JigsawHttpServletRequest.java// $Id: JigsawHttpServletRequest.java,v 1.65 2003/04/15 14:51:52 ylafon Exp $// (c) COPYRIGHT MIT and INRIA, 1996.// Please first read the full copyright statement in file COPYRIGHT.htmlpackage org.w3c.jigsaw.servlet;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.PipedInputStream;import java.io.Reader;import java.io.StringReader;import java.io.UnsupportedEncodingException;import java.net.URL;import java.net.MalformedURLException;import java.util.Enumeration;import java.util.Hashtable;import java.util.Locale;import java.util.Map;import java.util.Vector;import java.security.Principal;import javax.servlet.RequestDispatcher;import javax.servlet.Servlet;import javax.servlet.ServletContext;import javax.servlet.ServletInputStream;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpSession;import org.w3c.util.ArrayEnumeration;import org.w3c.util.EmptyEnumeration;import org.w3c.util.ObservableProperties;import org.w3c.jigsaw.http.Client;import org.w3c.jigsaw.http.Request;import org.w3c.jigsaw.http.httpd;import org.w3c.jigsaw.forms.URLDecoder;import org.w3c.jigsaw.forms.URLDecoderException;import org.w3c.jigsaw.resources.VirtualHostResource;import org.w3c.www.http.ContentLengthInputStream;import org.w3c.www.http.HeaderDescription;import org.w3c.www.http.HeaderValue;import org.w3c.www.http.HttpAcceptLanguage;import org.w3c.www.http.HttpCookie;import org.w3c.www.http.HttpCookieList;import org.w3c.www.http.HttpEntityMessage;import org.w3c.www.http.HttpMessage;import org.w3c.www.http.HttpRequestMessage;import org.w3c.www.mime.MimeType;import org.w3c.jigsaw.auth.AuthFilter; // for auth infos accessimport org.w3c.tools.resources.InvalidResourceException;import org.w3c.tools.resources.Resource;import org.w3c.tools.resources.ResourceReference;class HeaderNames implements Enumeration {    // The HeaderDescription enumeration    Enumeration e = null;    public boolean hasMoreElements() {	return e.hasMoreElements();    }    public Object nextElement() {	HeaderDescription d = (HeaderDescription) e.nextElement();	return d.getName();    }    HeaderNames(Enumeration e) {	this.e = e ;    }}/** *  @author Alexandre Rafalovitch <alex@access.com.au> *  @author Anselm Baird-Smith <abaird@w3.org> *  @author Benoit Mahe <bmahe@sophia.inria.fr> *  @author Yves Lafon <ylafon@w3.org> */public class JigsawHttpServletRequest implements HttpServletRequest {    // for null encoding    private static final String nullEnc = "null".intern();    /**     * The InputStream state codes.     */    /**     * The initial state of the request InputStream      */    private final static int STREAM_STATE_INITIAL = 0;    /**     * One reader has been created and is probably used.     */    private final static int STREAM_READER_USED = 1;    /**     * The input stream is used     */    private final static int INPUT_STREAM_USED = 2;    /**     * The inputstream state     */    private int stream_state = STREAM_STATE_INITIAL;    public final static 	String STATE_PARAMETERS = "org.w3c.jigsaw.servlet.stateParam";    private static MimeType type = MimeType.APPLICATION_X_WWW_FORM_URLENCODED ;    /**      * The initial request.     */    private Request request = null;    /**     * The attached servlet.     */    private Servlet servlet = null;    /**     * The attached servlet context.     */    private JigsawServletContext servletContext = null;    /**     * The lazyly computed queryParameters hashtable.     */    private Hashtable queryParameters = null;    protected JigsawHttpServletResponse response = null;    protected JigsawHttpSession httpSession = null;    protected JigsawHttpSessionContext sessionContext = null;    protected String requestedSessionID = null;    protected String encoding = null;    private Hashtable convertParameters(Hashtable source) {	if (source != null) {	    Enumeration e = source.keys();	    while (e.hasMoreElements()) {		Object name = e.nextElement();		Object value = source.get(name);		if (value instanceof String) {		    String _newval[] = new String[1];		    _newval[0] = (String) value;		    source.put(name, _newval);		}	    }	    return source;	}	return null;    }    private Hashtable mergeParameters(Hashtable source, Hashtable dest) {	if (source == null)	    return dest;	if (dest != null) {	    Enumeration e = source.keys();	    while (e.hasMoreElements()) {		String name = (String)e.nextElement();		Object value = dest.get(name);		if (value == null)		    dest.put(name, source.get(name));		else  if (value instanceof String[]) {		    String oldValues [] = (String[])value;		    String newValues [] = new String[oldValues.length+1];		    System.arraycopy(oldValues,0,newValues,0,				     oldValues.length);		    newValues[oldValues.length] = (String)source.get(name);		    dest.put(name,newValues);		} else {		    String newValues [] = new String[2];		    newValues[0] = (String)source.get(name);		    newValues[1] = (String)value;		    dest.put(name,newValues);		}	    }	    return dest;	} else {	    return source;	}    }    private synchronized void prepareQueryParameters() {	if( queryParameters != null )	    return;	Hashtable postParameters = null;	// What kinf of parameters are we expecting ?	if ( request.getMethod().equals("POST") ) {	    // POSTed parameters, check content type:	    if ((! request.hasContentType())		|| (type.match(request.getContentType()) < 0) ) 		return;	    postParameters = new Hashtable(2);	    // Get and decode the request entity:	    URLDecoder dec = null;	    try {		InputStream in = request.getInputStream() ;		// Notify the client that we are willing to continue		String exp = request.getExpect();		if (exp != null && (exp.equalsIgnoreCase("100-continue"))) {		    Client client = request.getClient();		    if ( client != null ) {			client.sendContinue();		    }		}		dec = new URLDecoder (in, false);		postParameters = dec.parse() ;	    } catch (URLDecoderException e) {		postParameters = null;	    } catch (IOException ex) {		postParameters = null;	    }	}	// URL encoded parameters:	String query = getQueryString();	if (query != null) {	    Reader qis = new StringReader(query);	    try {		queryParameters = new URLDecoder(qis, false).parse();	    } catch (Exception ex) {		throw new RuntimeException("Java implementation bug.");	    }	}	queryParameters = mergeParameters(postParameters, queryParameters);	// state parameters	Hashtable param = (Hashtable)request.getState(STATE_PARAMETERS);	queryParameters = mergeParameters(param, queryParameters);	convertParameters(queryParameters);    }    protected String getURLParameter(String name) {	Hashtable urlParameters = null;	String query = getQueryString();	if (query != null) {	    Reader qis = new StringReader(query);	    try {		urlParameters = new URLDecoder(qis, false).parse();		return (String) urlParameters.get(name);	    } catch (Exception ex) {		throw new RuntimeException("Java implementation bug.");	    }	}	return null;    }    /**     * Return the Charset parameter of content type     * @return A String instance     */    public String getCharacterEncoding() {	if (encoding == null) {	    org.w3c.www.mime.MimeType type = request.getContentType();	    if ((type != null) && (type.hasParameter("charset"))) {		encoding = type.getParameterValue("charset");	    } else {		encoding = nullEnc;	    }	}	if (encoding == nullEnc) {	    return null;	}	return encoding;    }    /**     * Overrides the name of the character encoding used in the body of this     * request     * ServletRequest implementation - version 2.3     * @param enc, a <code>String</code> specifying the encoding String     */    public void setCharacterEncoding(String enc) 	throws java.io.UnsupportedEncodingException    {	// a hack to see if the character encoding is supported 	InputStreamReader isr = new InputStreamReader(new PipedInputStream(),						      enc);	encoding = enc;    }    /**     * ServletRequest implementation - Get the length of request data.     * @return An int, or <strong>-1</strong>.     */    public int getContentLength() {	return request.getContentLength();    }    /**     * ServletRequest implementation - Get the type of the request's body.     * @return A String encoded mime type, or <strong>null</strong>.     */    public String getContentType() {	org.w3c.www.mime.MimeType t = request.getContentType();	return (t == null) ? null : t.toString();    }    /**     * ServletRequest implementation - Get the protocol of that request.     * @return A String encoded version of the protocol.     */    public String getProtocol() {	return request.getVersion();    }    protected httpd getServer() {	return request.getClient().getServer();    }   /**    * ServletRequest implementation - Get the name of queried server.    * @return Name of server, as a String.    */   public String getServerName() {       String host = request.getHost();       if (host != null) {	   int idx = host.lastIndexOf(':');	   if (idx != -1) {	       return host.substring(0, host.lastIndexOf(':'));	   } else {	       return host;	   }       } else {	   return getServer().getHost();       }   }   /**    * ServletRequest implementation - Get the port of queried server.    * @return A port number (int).    */   public int getServerPort() {       if (request.isProxy()) {	   String host = request.getHost();	   if (host != null) {	       int idx = host.lastIndexOf(':');	       if (idx == -1)		   return 80;	       return Integer.parseInt(host.substring(idx+1));	   }       }       return getServer().getLocalPort();   }   /**    * ServletRequest implementation - Get the IP address of requests's sender.    * @return Numeric IP address, as a String.    */    public String getRemoteAddr() {	return request.getClient().getInetAddress().getHostAddress();    }    /**     * ServletRequest implementation - FQDN of request's sender.     * @return Name of client's machine (FQDN).     */    public String getRemoteHost() {	return request.getClient().getInetAddress().getHostName();    }    /**     * ServletRequest implementation - Get real path.     * Jigsaw realy has no notion of <em>translation</em> stricto     * sensu (it has much better in fact ;-). This is a pain here.     * @return the real path.     * @deprecated since jsdk1.2     */    public String getRealPath(String name) {	httpd             server  = getServer();	ResourceReference rr_root = server.getRootReference();	return JigsawServletContext.getRealPath(name, 						rr_root, 						request.getTargetResource());    }    protected ServletInputStream is = null;    /**

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?