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

📄 mhttprequest.java

📁 httptunnel.jar httptunnel java 源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package net.jumperz.net;

import java.io.*;
import java.net.*;
import java.nio.charset.UnsupportedCharsetException;
import java.util.*;
import net.jumperz.io.*;
import net.jumperz.util.*;
import net.jumperz.net.exception.*;

public final class MHttpRequest
extends MHttpData
{
private int state;
private int methodType;
private String method;
private String uri;
private String version;
//private String requestLine;
private String addr;
private int port;
private Set parameterNameSet1;
private Set parameterNameSet2;
private Map parameterMap1;
private Map parameterMap2;
private Set cookieNameSet1 = new HashSet();
private Set cookieNameSet2 = new HashSet();
private Map cookieMap1 = new HashMap();
private Map cookieMap2 = new HashMap();

public static final int GET	=  0;
public static final int POST	=  1;
public static final int HEAD	=  2;
public static final int PUT	=  3;
public static final int DELETE	=  4;
public static final int TRACE	=  5;
public static final int CONNECT	=  6;
public static final int OPTIONS	=  7;

public static final int OTHER	= -1;

public static final int RECV_REQUEST_HEADER	= 0;
public static final int RECV_REQUEST_BODY	= 1;
public static final int REQUEST_COMPLETE	= 2;
public static final int SESSIONTYPE_QUERY	= 0;
public static final int SESSIONTYPE_PARAMETER	= 1;
public static final int SESSIONTYPE_COOKIE	= 2;

public static final int DEFAULT_METHOD_TYPE	= GET;
public static final String DEFAULT_METHOD	= "GET";
public static final String DEFAULT_URI		= "/";
public static final String DEFAULT_VERSION	= "HTTP/1.0";
//public static final String DEFAULT_REQUEST_LINE	= "GET / HTTP/1.0";

//--------------------------------------------------------------------------------
public MHttpRequest()
{
methodType	= DEFAULT_METHOD_TYPE;
method		= DEFAULT_METHOD;
uri		= DEFAULT_URI;
version		= DEFAULT_VERSION;
}
//--------------------------------------------------------------------------------
public MHttpRequest( String s )
throws IOException
{
BufferedInputStream i = new BufferedInputStream( new ByteArrayInputStream( s.getBytes( MCharset.CS_ISO_8859_1 ) ) );
init( i );
}
// --------------------------------------------------------------------------------
public MHttpRequest( byte[] buffer )
throws IOException
{
BufferedInputStream i = new BufferedInputStream( new ByteArrayInputStream( buffer ) );
init( i );
}
//--------------------------------------------------------------------------------
public MHttpRequest( BufferedInputStream in_bufferedInputStream )
throws IOException
{
init( in_bufferedInputStream );
}
//-------------------------------------------------------------------------------
public void init( BufferedInputStream in_bufferedInputStream )
throws IOException
{
state = MHttpRequest.RECV_REQUEST_HEADER;
bufferedInputStream = in_bufferedInputStream;
recvHeader();
setHasBodyFlag();

if( hasBodyFlag )
	{
	state = MHttpRequest.RECV_REQUEST_BODY;
	recvBody();
	}
parseParameters();
parseCookies();
state = MHttpRequest.REQUEST_COMPLETE;
}
// --------------------------------------------------------------------------------
public String getSessionId( int sessionType, String sessionIdName )
throws IOException
{
if( sessionType == SESSIONTYPE_QUERY )
	{
	if( parameterNameSet1.contains( sessionIdName ) )
		{
		return ( String )parameterMap1.get( sessionIdName );
		}
	else if( parameterNameSet2.contains( sessionIdName ) )
		{
		throw new IOException( "Two or more session id found." );
		}
	else
		{
		return null;
		}
	}
else if( sessionType == SESSIONTYPE_PARAMETER )
	{
	MRequestUri ru = new MRequestUri( uri );
	String params = ru.getParams();
	if( params.indexOf( sessionIdName ) == 0 )
		{
		return params.substring( sessionIdName.length() + 1 );
		}
	}
else if( sessionType == SESSIONTYPE_COOKIE )
	{
	if( cookieNameSet1.contains( sessionIdName ) )
		{
		return ( String )cookieMap1.get( sessionIdName );
		}
	else if( cookieNameSet2.contains( sessionIdName ) )
		{
		throw new IOException( "Two or more session id found." );
		}
	else
		{
		return null;
		}
	}
return null;
}
// --------------------------------------------------------------------------------
private void parseCookies()
{
if( !headerExists( "Cookie" ) )
	{
	return;
	}

String cookie = getHeaderValue( "Cookie" );
String[] pairList = cookie.split( "[ ]*;[ ]*" );
for( int i = 0; i < pairList.length; ++i )
	{
	int index = pairList[ i ].indexOf( '=' );
	if( index > 0 )
		{
		String name  = pairList[ i ].substring( 0, index );
		String value = pairList[ i ].substring( index + 1 );
		if( cookieNameSet1.contains( name ) )
			{
			List valueList = new ArrayList();
			valueList.add( cookieMap1.get( name ) );
			valueList.add( value );
			
			cookieNameSet1.remove( name );
			cookieMap1.remove( name );
			
			cookieNameSet2.add( name );
			cookieMap2.put( name, valueList );
			}
		else if( cookieNameSet2.contains( name ) )
			{
			List valueList = ( List )cookieMap2.get( name );
			valueList.add( value );
			}
		else
			{
			cookieNameSet1.add( name );
			cookieMap1.put( name, value );
			}
		}
	}
}
// --------------------------------------------------------------------------------
private void parseParameters2( String s )
{
String[] pairList = s.split( "&" );
for( int i = 0; i < pairList.length; ++i )
	{
	int index = pairList[ i ].indexOf( '=' );
	if( index > 0 )
		{
		String name  = pairList[ i ].substring( 0, index );
		String value = "";
		try
			{
			value = MStringUtil.urlDecode( pairList[ i ].substring( index + 1 ) );		
			}
		catch( IllegalArgumentException ignored )
			{
			}
		parameterFound( name, value );
		}
	}
}
// --------------------------------------------------------------------------------
private void parameterFound( String name, String value )
{
if( parameterNameSet1.contains( name ) )
	{
	List valueList = new ArrayList();
	valueList.add( parameterMap1.get( name ) );
	valueList.add( value );
	
	parameterNameSet1.remove( name );
	parameterMap1.remove( name );
	
	parameterNameSet2.add( name );
	parameterMap2.put( name, valueList );
	}
else if( parameterNameSet2.contains( name ) )
	{
	List valueList = ( List )parameterMap2.get( name );
	valueList.add( value );
	}
else
	{
	parameterNameSet1.add( name );
	parameterMap1.put( name, value );
	}
}
// --------------------------------------------------------------------------------
private void parseParameters()
{
parameterNameSet1 = new HashSet();
parameterNameSet2 = new HashSet();
parameterMap1 = new HashMap();
parameterMap2 = new HashMap();

	//QueryString
parseParameters2( new MRequestUri( uri ).getQuery() );

	//Body
if( hasBody() )
	{
	if( headerExists( "Content-Type" ) )
		{
		String contentType = getHeaderValue( "Content-Type" );
		if( contentType.equals( "application/x-www-form-urlencoded" ) )
			{
			try
				{
				String bodyStr = MStreamUtil.streamToString( getBodyInputStream() );
				parseParameters2( bodyStr );
				}
			catch( IOException e )
				{
				e.printStackTrace();
				}		
			}
		else if( contentType.indexOf( "multipart/form-data" ) > -1 )
			{
			String boundary = MRegEx.getMatch( "boundary=(.*)", contentType );
			try
				{
				parseParameters3( boundary );
				}
			catch( IOException e )
				{
				e.printStackTrace();
				}
			}
		}
	}
/*
System.out.println( parameterNameSet1 );
System.out.println( parameterNameSet2 );
System.out.println( parameterMap1 );
System.out.println( parameterMap2 );
*/
}
// --------------------------------------------------------------------------------
private void parseParameters3( String boundary )
throws IOException
{
String bodyStr = MStreamUtil.streamToString( getBodyInputStream() );
String[] array = bodyStr.split( "--" + boundary );
for( int i = 0; i < array.length; ++i )
	{
	if( array[ i ].indexOf( "\r\n" ) == 0 )
		{
		array[ i ] = array[ i ].substring( 2 );	
		}
	String paramName = null;
	while( true )
		{
		int index = array[ i ].indexOf( "\r\n" );
		if( index == -1 )
			{
			break;
			}
		String line = array[ i ].substring( 0, index );
		array[ i ] = array[ i ].substring( index + 2 );
		if( line.indexOf( "Content-Disposition" ) == 0 )
			{
			if( paramName != null )
				{
				throw new IOException( "Duplicate Content-Dispotision." );
				}
			paramName = MRegEx.getMatch( "^Content-Disposition:[ ]*form-data;[ ]*name=\"([^\"]*)\"", line );		
			}
		else if( line.length() == 0 ) // end of header
			{
				// get content
			String paramValue = array[ i ].substring( 0, array[ i ].length() - 2 );
			if( paramName != null )
				{
				parameterFound( paramName, paramValue );

⌨️ 快捷键说明

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