⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 httprequest.java

📁 java写的一个很小但是实用的http server
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		}

		return decoded.toString();
	}

	public static final String FORM_URLENCODED =
		"application/x-www-form-urlencoded";

	private HttpServer server;
	private Socket socket;

	private String method;

	private String verbatimRequestPath; // Actual text in HTTP request
	protected String getVerbatimRequestPath() { return verbatimRequestPath; }

	/**
	 * This is the complete request path (decoded) including any query string
	 * and fragment specifier.
	 */
	protected String requestPath;
	public String getRequestPath() { return requestPath; }
	/**
	 * This is the request URI portion of the request path (decoded).
	 * Does not contain any query string or fragment specifier.
	 */
	protected String requestURI;
	/**
	 * This is the part of the path the specifies which servlet to run.
	 */
	protected String[] servletPathAndName = new String[2];
	protected String pathInfo;
	protected String queryString;

	/**
	 * Parses the request path into its various elememnts.  The elements
	 * include <ol>
	 * <li>context path (always "" for TiniHttpServer)
	 * <li>servlet path (always starts with '/')
	 * <li>path info (rest of path that is not part of context or servlet path)
	 * <li>query string (everything following a '?' or null if not present)
	 * </ol>
	 */
	protected void parseRequestPath( String path )
	{
		// Decode path and save
		requestPath = httpDecode( path );

		// Create fragment-free local copy of request path
		int fragment = path.indexOf( '#' );
		if( fragment != -1 )
		{
			path = path.substring( 0, fragment );
		}
		requestURI = httpDecode( path );

		// Get path info and query string
		int queryIdx = requestURI.indexOf( '?' );
		if( queryIdx != -1 )
		{
			queryString = requestURI.substring( queryIdx + 1 );

			// Parse query string
			if( queryString.length() > 0 )
			{
				new NVPairParser( queryString, "&", "=")
				{
					public void forEachNVPair( String name, String value)
					{
						setParameter( httpDecode( name), httpDecode( value));
					}
				}.parse();
			}

			// Eliminate query string from requestURI
			requestURI = requestURI.substring( 0, queryIdx );
		}

		// Get servlet path portion of request path
		server.getServletPathAndName( requestURI, servletPathAndName );
		int servletPathLength = servletPathAndName[0].length();

		// Get path info portion of request path
		if( servletPathLength < requestURI.length() )
		{
			pathInfo = requestURI.substring( servletPathLength );
		}
	}

	private String protocol;

	private CaselessHashtable headers = new CaselessHashtable();
	private Hashtable cookies    = new Hashtable();
	private Hashtable parameters = new Hashtable();
	private Hashtable attributes = new Hashtable();
	private String    authType = null;
	private String    remoteUser = null;

	// Session variables
	static Hashtable sessions = new Hashtable();
	private com.smartsc.http.HttpSession session;
	private String sessionName;
	private String reqSessionId;
	private String sessionId;
	private boolean reqSessionIdFromCookie;
	private boolean reqSessionIdFromURL;
	private boolean reqSessionIdValid;

	private int nextChar = -1;
	private BufferedInputStream is;
	private boolean getISCalled = false;
	private BufferedReader ir = null;

	private boolean postDataParsed;

	// Create session reaper thread
	private static Thread reaperThread = new HttpSessionReaper();

	// From javax.servlet.ServletResponse
	public Object getAttribute( String name)
	{
		return attributes.get( name);
	}
	public Enumeration getAttributeNames()
	{
		return attributes.keys();
	}
	public String getCharacterEncoding()
	{
		// TODO
		return "";
	}
	public int getContentLength()
	{
		return getIntHeader( CONTENT_LENGTH);
	}
	public String getContentType()
	{
		return (String)headers.get( CONTENT_TYPE);
	}
	public ServletInputStream getInputStream()
	throws IOException
	{
		// If Reader has already been obtained
		if( ir != null)
		{
			throw new IllegalStateException(
				"getReader() was called first.");
		}
		getISCalled = true;
		return new com.smartsc.http.ServletInputStream( is);
	}
	public Locale getLocale()
	{
		return Locale.getDefault();
	}
	public Enumeration getLocales()
	{
		Vector v = new Vector( 1);
		v.addElement( Locale.getDefault());
		return v.elements();
	}
	public String getParameter( String name)
	{
		try { parsePostData(); }
		catch( IOException ioe ) { server.log( ioe ); }
		String param = null;
		Vector v = (Vector)parameters.get( name);
		if( v != null)
			param = (String)v.firstElement();
		return param;
	}
	public Enumeration getParameterNames()
	{
		try { parsePostData(); }
		catch( IOException ioe ) { server.log( ioe ); }
		return parameters.keys();
	}
	public String[] getParameterValues( String name)
	{
		try { parsePostData(); }
		catch( IOException ioe ) { server.log( ioe ); }
		Vector v = (Vector)parameters.get( name);
		if( v == null)
			return null;
		String[] values = new String[v.size()];
		v.copyInto( values);
		return values;
	}
	public String getProtocol()
	{
		return protocol;
	}
	public BufferedReader getReader()
	throws IOException
	{
		if( getISCalled)
		{
			throw new IllegalStateException(
				"getInputStream() was called first.");
		}
		if( ir == null)
		{
			ir = new BufferedReader(
				new InputStreamReader( is) );
		}
		return ir;
	}
	/** @deprecated */
	public String getRealPath( String path)
	{
		return server.getRealPath( path);
	}
	public String getRemoteAddr()
	{
		return socket.getInetAddress().getHostAddress();
	}
	public String getRemoteHost()
	{
		return socket.getInetAddress().getHostName();
	}
	public RequestDispatcher getRequestDispatcher( String urlPath)
	{
		// Sanity check
		if( urlPath == null) return null;

		// Resolve relative path
		if( !urlPath.startsWith( "/" ))
		{
			String uri = getRequestURI();
			int lastSlashIdx = uri.lastIndexOf( '/' );
			if( lastSlashIdx > 0 )
			{
				uri = uri.substring( 0, lastSlashIdx + 1 );
			}
			urlPath = uri + urlPath;
		}

		return server.getRequestDispatcher( urlPath );
	}
	public String getScheme()
	{
		// TODO Support https?
		return server.DEFAULT_SCHEME;
	}
	public String getServerName()
	{
		String serverName;
		String hostColonPort = getHeader( "Host");
		if( hostColonPort != null)
		{
			int colonIdx = hostColonPort.indexOf( ':');
			if( colonIdx != -1)
			{
				serverName = hostColonPort.substring( 0, colonIdx);
			}
			else
			{
				serverName = hostColonPort;
			}
		}
		else
		{
			serverName = socket.getLocalAddress().getHostName();
		}
		return serverName;
	}
	public int getServerPort()
	{
		int port = socket.getLocalPort();
		String hostColonPort = getHeader( "Host");
		if( hostColonPort != null)
		{
			int colonIdx = hostColonPort.indexOf( ':');
			if( colonIdx != -1)
			{
				try
				{
					port = Integer.parseInt(
						hostColonPort.substring( colonIdx + 1));
				}
				catch( NumberFormatException nfe) {}
			}
			else
			{
				port = HttpServer.DEFAULT_PORT;
			}
		}
		return port;
	}
	public boolean isSecure()
	{
		return false;
	}
	public void removeAttribute( String name)
	{
		attributes.remove( name);
	}
	public void setAttribute( String name, Object o)
	{
		attributes.put( name, o);
	}

	// From HttpServletRequest
	public String getAuthType()
	{
		return authType;
	}
	public String getContextPath()
	{
		return "";
	}
	public Cookie[] getCookies()
	{
		Cookie[] cookieArray = new Cookie[cookies.size()];
		Enumeration e = cookies.elements();
		for( int i = 0; e.hasMoreElements(); ++i)
		{
			cookieArray[i] = (Cookie)e.nextElement();
		}
		return cookieArray;
	}
	public long getDateHeader( String name)
	{
		long date = -1;
		String value = (String)headers.get( name);
		if( value != null)
		{
			try { date = Date.parse( value); }
			catch( IllegalArgumentException iae) {}
		}
		return date;
	}
	public String getHeader( String name)
	{
		return (String)headers.get( name);
	}
	public Enumeration getHeaders( String name)
	{
		return headers.getValues( name);
	}
	public Enumeration getHeaderNames()
	{
		return headers.keys();
	}
	public int getIntHeader( String name)
	{
		String value = (String)headers.get( name);
		if( value == null)
			return -1;
		else
			return Integer.parseInt( value);
	}
	public String getMethod()
	{
		return method;
	}
	public String getPathInfo()
	{
		return pathInfo;
	}
	public String getPathTranslated()
	{
		return server.getRealPath( getPathInfo());
	}
	public String getQueryString()
	{
		return queryString;
	}
	public String getRemoteUser()
	{
		return remoteUser;
	}
	public String getRequestURI()
	{
		return requestURI;
	}
	public String getRequestedSessionId()
	{
		return reqSessionId;
	}
	public String getServletPath()
	{
		return servletPathAndName[0];
	}
	protected String getServletName()
	{
		return servletPathAndName[1];
	}
	public HttpSession getSession()
	{
		return getSession( true);
	}
	public HttpSession getSession( boolean create)
	{
		if( session == null && create)
			createSession();

		return session;

		/*
		// If not creating new session
		// and requested session not valid
		if( !create && !reqSessionIdValid)
			return null;
		// Else, if we have a session
		// (regardless of create flag or whether it was valid as requested)
		else if( session != null)
			// Return requested session
			return session;
		// Else (Creating and requested session not valid)
		else // if( create and !reqSessionIdValid)
			// Return new session
			return createSession();
		*/
	}
	public java.security.Principal getUserPrincipal()
	{
		return null;
	}
	public boolean isRequestedSessionIdFromCookie()
	{
		return reqSessionIdFromCookie;
	}
	public boolean isRequestedSessionIdFromURL()
	{
		return reqSessionIdFromURL;
	}
	/** @deprecated */
	public boolean isRequestedSessionIdFromUrl()
	{
		return isRequestedSessionIdFromURL();
	}
	public boolean isRequestedSessionIdValid()
	{
		return reqSessionIdValid;
	}
	public boolean isUserInRole( String role)
	{
		return false;
	}
}

⌨️ 快捷键说明

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