📄 msession.java.backup
字号:
package net.jumperz.app.MGuardian;
import net.jumperz.util.*;
import net.jumperz.net.*;
import java.io.*;
import java.net.*;
public final class MSession
extends MSubject
implements MCommand
{
public static String targetHost;
public static String Host;
public static int port;
public static int targetPort;
public static boolean replaceHostField = false;
public static boolean replaceLocationField = false;
public static final int ERROR = -1;
public static final int CONNECTING = 0;
public static final int RECEIVING_REQUEST = 1;
public static final int SENDING_REQUEST = 2;
public static final int RECEIVING_RESPONSE = 3;
public static final int SENDING_RESPONSE = 4;
private int state;
private Socket clientSocket;
private Socket serverSocket;
private InputStream clientInputStream;
private OutputStream clientOutputStream;
private InputStream serverInputStream;
private OutputStream serverOutputStream;
private boolean keepAlive = false;
private MHttpRequest request;
private MHttpResponse response;
//-----------------------------------------------------------------------------------
public MSession( Socket in_socket )
throws Exception
{
clientSocket = in_socket;
clientInputStream = clientSocket.getInputStream();
clientOutputStream = clientSocket.getOutputStream();
}
//-----------------------------------------------------------------------------------
public final void execute()
{
try
{
connect();
recvRequest();
sendRequest();
recvResponse();
sendResponse();
}
catch( Exception e )
{
e.printStackTrace();
MLogger.getInstance().Log( e.toString() );
state = ERROR;
}
closeConnection();
sendNotify();
}
//-----------------------------------------------------------------------------------
private void connect()
throws IOException
{
state = CONNECTING;
sendNotify();
serverSocket = new Socket( targetHost, targetPort );
serverInputStream = serverSocket.getInputStream();
serverOutputStream = serverSocket.getOutputStream();
}
//-----------------------------------------------------------------------------------
private void recvRequest()
throws Exception
{
request = new MHttpRequest( clientInputStream );
// persistent connection
//checkConnectionField( request );
}
//-----------------------------------------------------------------------------------
private void sendRequest()
throws Exception
{
if( replaceHostField )
{
request.setHeaderValue( "Host", MSession.targetHost );
}
request.setHeaderValue( "Connection", "Close" );
if( request.getMethodType()== MHttpRequest.POST )
{
request.setVersion( "HTTP/1.0" );
}
serverOutputStream.write( request.getHeader() );
int methodType = request.getMethodType();
if( methodType == MHttpRequest.POST )
{
serverOutputStream.write( request.getBody() );
}
}
//-----------------------------------------------------------------------------------
private void recvResponse()
throws Exception
{
response = new MHttpResponse( serverInputStream );
/* HTTP/1.1
if( keepAlive )
{
checkConnectionField( response );
}
else
{
// if the keepAlive was set to false by client,
// we do not need to check the Connection field of the response.
}
*/
}
//-----------------------------------------------------------------------------------
private void sendResponse()
throws Exception
{
// check Location field
if( replaceLocationField )
{
if( response.headerExists( "Location" ) )
{
String location = response.getHeaderValue( "Location" );
String thisPeer = Host;
if( port != 80 )
{
thisPeer += ":" + String.valueOf( port );
}
if( location.indexOf( targetHost + ":" + port ) > 0 )
{
response.setHeaderValue( "Location", location.replaceFirst( targetHost + ":" + targetPort, thisPeer ) );
}
else
{
response.setHeaderValue( "Location", location.replaceFirst( targetHost, thisPeer ) );
}
}
}
// persistent connection
/*
if( !keepAlive )
{
response.setHeaderValue( "Connection", "Close" );
}
*/
// normal connection
response.setHeaderValue( "Connection", "Close" );
clientOutputStream.write( response.getHeader() );
clientOutputStream.write( response.getBody() );
}
//-----------------------------------------------------------------------------------
public final void breakCommand()
{
closeConnection();
}
//-----------------------------------------------------------------------------------
/*
private void checkConnectionField( MHttpData data )
{
// get connection info
String connection = data.getHeaderValue( "Connection" );
if( connection == null )
{
String version = request.getVersion();
if( !version.equalsIgnoreCase( "HTTP/0.9" )
&& !version.equalsIgnoreCase( "HTTP/1.0" )
)
{
// HTTP/1.1 or higher.
// keep-alive is the default
keepAlive = true;
}
else
{
keepAlive = false;
}
}
else if( connection.equalsIgnoreCase( "Keep-Alive" ) )
{
keepAlive = true;
}
else if( connection.equalsIgnoreCase( "Close" ) )
{
keepAlive = false;
}
}
*/
//-----------------------------------------------------------------------------------
public final void closeConnection()
{
closeClientConnection();
closeServerConnection();
}
//-----------------------------------------------------------------------------------
public final void closeClientConnection()
{
try
{
if( clientInputStream != null )
{
clientInputStream.close();
}
if( clientOutputStream != null )
{
clientOutputStream.close();
}
if( !clientSocket.isClosed() )
{
clientSocket.close();
}
}
catch( IOException e )
{
}
}
//-----------------------------------------------------------------------------------
public final void closeServerConnection()
{
try
{
if( serverInputStream != null )
{
serverInputStream.close();
}
if( serverOutputStream != null )
{
serverOutputStream.close();
}
if( serverSocket != null
&& !serverSocket.isClosed() )
{
serverSocket.close();
}
}
catch( IOException e )
{
}
}
//-----------------------------------------------------------------------------------
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -