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

📄 httputils.java

📁 java写的一个很小但是实用的http server
💻 JAVA
字号:
// IMPORTANT   IMPORTANT   IMPORTANT   IMPORTANT   IMPORTANT   IMPORTANT
//
// This class must only be used for TINI until all of the stuff
// required by javax.servlet.http.HttpUtils is supported by TINI.
//
// The missing elements are:
//
// java.text.MessageFormat
//
// HttpUtils.java - TINI version of javax.servlet.http.HttpUtils.
//
// Copyright (C) 1999-2002  Smart Software Consulting
//
// This program 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.
//
// This program 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.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//
// Smart Software Consulting
// 1688 Silverwood Court
// Danville, CA  94526-3079
// USA
//
// http://www.smartsc.com
//

package javax.servlet.http;

import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.StringTokenizer;
import java.util.Vector;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletRequest;

public class HttpUtils
{
	public HttpUtils()
	{
	}

	public static StringBuffer getRequestURL( HttpServletRequest httpservletrequest)
	{
		StringBuffer stringbuffer = new StringBuffer();
		String s = httpservletrequest.getScheme();
		int i = httpservletrequest.getServerPort();
		String s1 = httpservletrequest.getServletPath();
		String s2 = httpservletrequest.getPathInfo();
		stringbuffer.append( s);
		stringbuffer.append( "://");
		stringbuffer.append( httpservletrequest.getServerName());
		if( s.equals( "http") && i != 80 || s.equals( "https") && i != 443)
		{
			stringbuffer.append( ':');
			stringbuffer.append( httpservletrequest.getServerPort());
		}
		if( s1 != null)
			stringbuffer.append( s1);
		if( s2 != null)
			stringbuffer.append( s2);
		return stringbuffer;
	}

	private static String parseName( String s, StringBuffer stringbuffer)
	{
		stringbuffer.setLength( 0);
		for( int i = 0; i < s.length(); i++)
		{
			char c = s.charAt( i);
			switch( c)
			{
			case '+':
				stringbuffer.append( ' ');
				break;

			case '%':
				try
				{
					stringbuffer.append( (char)Integer.parseInt( s.substring( i + 1, i + 3), 16));
					i += 2;
					break;
				}
				catch( NumberFormatException _nfe)
				{
					throw new IllegalArgumentException();
				}
				catch( StringIndexOutOfBoundsException sioobe)
				{
					String s1 = s.substring( i);
					stringbuffer.append( s1);
					if( s1.length() == 2)
						i++;
				}
				break;

			default:
				stringbuffer.append( c);
				break;
			}
		}

		return stringbuffer.toString();
	}

	public static Hashtable parsePostData( int i, ServletInputStream servletinputstream)
	{
		byte abyte0[] = null;
		if( i <= 0)
			return null;
		try
		{
			abyte0 = new byte[i];
			int k = 0;
			do
			{
				int j = servletinputstream.read( abyte0, k, i - k);
				if( j <= 0)
				{
					throw new IOException( "Short Read");
				}
				k += j;

			} while( i - k > 0);
		}
		catch( IOException ioe)
		{
			return nullHashtable;
		}
		String s = new String( abyte0, 0, i);
		return parseQueryString( s);
	}

	public static Hashtable parseQueryString( String s)
	{
		if( s == null)
			throw new IllegalArgumentException();
		Hashtable hashtable = new Hashtable();
		StringBuffer stringbuffer = new StringBuffer();
		Vector v;
		String key;
		StringTokenizer stringtokenizer = new StringTokenizer( s, "&");
		while( stringtokenizer.hasMoreTokens())
		{
			String s1 = stringtokenizer.nextToken();
			int i = s1.indexOf( '=');
			if( i == -1)
				throw new IllegalArgumentException();
			key = parseName( s1.substring( 0, i), stringbuffer);
			String value = parseName( s1.substring( i + 1, s1.length()), stringbuffer);
			v = (Vector)hashtable.get( key);
			if( v == null)
			{
				v = new Vector();
				hashtable.put( key, v);
			}
			v.addElement( value);
		}

		Enumeration e = hashtable.keys();
		while( e.hasMoreElements())
		{
			key = (String)e.nextElement();
			v = (Vector)hashtable.get( key);
			int len = v.size();
			String[] values = new String[len];
			for( int i = 0; i < len; ++i)
			{
				values[i] = (String)v.elementAt( i);
			}
			hashtable.put( key, values);
		}

		return hashtable;
	}

	static Hashtable nullHashtable = new Hashtable();
}

⌨️ 快捷键说明

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