📄 msession.java.20040219
字号:
{
MHttpDataLogger.log( response, clientAddr, logId + "_" + MHttpDataLogger.RES_LOG_SUFFIX );
}
//-----------------------------------------------------------------------------------
private void saveRequest( String logId )
throws IOException
{
MHttpDataLogger.log( request, clientAddr, logId + "_" + MHttpDataLogger.REQ_LOG_SUFFIX );
}
//-----------------------------------------------------------------------------------
private String getLogId()
{
StringBuffer strBuf = new StringBuffer( LOGFILENAME_BUFSIZE );
strBuf.append( System.currentTimeMillis() );
strBuf.append( "_" );
strBuf.append( clientPort );
return strBuf.toString();
}
//-----------------------------------------------------------------------------------
private void execCommand( String command, String ruleName, String logId )
//throws IOException
{
command = command.replaceAll( "%name", ruleName );
command = command.replaceAll( "%addr", clientAddr );
command = command.replaceAll( "%port", String.valueOf( clientPort ) );
command = command.replaceAll( "%logId", logId );
command = command.replaceAll( "%logDir", MHttpDataLogger.getAlertLogDirName() );
// %protocol
if( isHttps() )
{
command = command.replaceAll( "%protocol", HTTPS );
}
else
{
command = command.replaceAll( "%protocol", HTTP );
}
//Runtime.getRuntime().exec( command );
MCommandExecuter.getInstance().addCommand( command );
}
//-----------------------------------------------------------------------------------
public static void setAdditionalResponseHeaderMap( Map in_additionalReponseHeaderMap )
{
additionalResponseHeaderMap = in_additionalReponseHeaderMap;
}
//-----------------------------------------------------------------------------------
public static void setAdditionalRequestHeaderMap( Map in_additionalRequestHeaderMap )
{
additionalRequestHeaderMap = in_additionalRequestHeaderMap;
}
//-----------------------------------------------------------------------------------
public static void setRequestFilter( MHttpFilter in_requestFilter )
{
requestFilter = in_requestFilter;
}
//-----------------------------------------------------------------------------------
public static void setResponseFilter( MHttpFilter in_responseFilter )
{
responseFilter = in_responseFilter;
}
//-----------------------------------------------------------------------------------
private void filterRequest()
{
if( requestFilter != null )
{
requestFilter.doFilter( request );
}
}
//-----------------------------------------------------------------------------------
private void filterResponse()
{
if( responseFilter != null )
{
responseFilter.doFilter( response );
}
}
//-----------------------------------------------------------------------------------
private void connect()
throws IOException
{
if( serverSocket == null )
{
doConnect();
}
}
//-----------------------------------------------------------------------------------
private void doConnect()
throws IOException
{
//serverSocket = new Socket( targetHost, targetPort );
SocketAddress sockAddr = new InetSocketAddress( targetHost, targetPort );
serverSocket = new Socket();
serverSocket.connect( sockAddr, connectTimeOut );
serverInputStream = new BufferedInputStream( serverSocket.getInputStream() );
serverOutputStream = serverSocket.getOutputStream();
}
//-----------------------------------------------------------------------------------
private void recvRequest()
throws Exception
{
request = new MHttpRequest();
request.setAddr( clientAddr );
request.setPort( clientPort );
request.init( clientInputStream );
String version = request.getVersion();
if( version.equals( "HTTP/1.1" ) )
{
keepAlive = true;
if( request.headerExists( "Connection" )
&& request.getHeaderValue( "Connection" ).equalsIgnoreCase( "close" )
)
{
keepAlive = false;
}
}
else
{
keepAlive = false;
if( request.headerExists( "Connection" )
&& request.getHeaderValue( "Connection" ).equalsIgnoreCase( "Keep-Alive" )
)
{
keepAlive = true;
}
}
}
//-----------------------------------------------------------------------------------
private void modifyRequest()
{
if( replaceHostField == true )
{
request.setHeaderValue( "Host", MSession.targetPeer );
}
// add additionalHeader
if( additionalRequestHeaderMap != null )
{
Iterator i = additionalRequestHeaderMap.keySet().iterator();
while( i.hasNext() )
{
String key = ( String )i.next();
String value = ( String )additionalRequestHeaderMap.get( key );
value = replaceSpecialString( value );
request.setHeaderValue( key, value );
}
}
}
//-----------------------------------------------------------------------------------
private String replaceSpecialString( String in )
{
String tmpStr = in;
// %addr
tmpStr = tmpStr.replaceAll( "%addr", clientAddr );
// %protocol
if( isHttps() )
{
tmpStr = tmpStr.replaceAll( "%protocol", HTTPS );
}
else
{
tmpStr = tmpStr.replaceAll( "%protocol", HTTP );
}
return tmpStr;
}
//-----------------------------------------------------------------------------------
private boolean isHttps()
{
return isHttpsFlag;
}
//-----------------------------------------------------------------------------------
private void sendRequest()
throws IOException
{
serverOutputStream.write( request.getHeader() );
if( request.hasBody() )
{
MStreamUtil.connectStream( request.getBodyInputStream(), serverOutputStream );
}
}
//-----------------------------------------------------------------------------------
private void recvResponse()
throws Exception
{
if( request.getMethodType() == MHttpRequest.HEAD )
{
response = new MHttpResponse( serverInputStream, true );
}
else
{
response = new MHttpResponse( serverInputStream );
}
// 100 continue
if( response.getStatusCode() == 100 )
{
response = new MHttpResponse( serverInputStream );
}
if( response.headerExists( "Connection" )
&& response.getHeaderValue( "Connection" ).equalsIgnoreCase( "close" )
)
{
keepAlive = false;
}
}
//-----------------------------------------------------------------------------------
private void sendResponse()
throws IOException
{
clientOutputStream.write( response.getHeader() );
if( response.hasBody() )
{
MStreamUtil.connectStream( response.getBodyInputStream(), clientOutputStream );
}
}
//-----------------------------------------------------------------------------------
private void modifyResponse()
{
/*
// check Location field
if( replaceLocationField )
{
if( response.headerExists( "Location" ) )
{
String location = response.getHeaderValue( "Location" );
if( location.indexOf( targetPeer ) == 7 )
{
if( isHttps() )
{
location = location.replaceFirst( "^http", "https" );
response.setHeaderValue( "Location", location );
}
}
}
}
*/
// add additionalHeader
if( additionalResponseHeaderMap != null )
{
Iterator i = additionalResponseHeaderMap.keySet().iterator();
while( i.hasNext() )
{
String key = ( String )i.next();
String value = ( String )additionalResponseHeaderMap.get( key );
response.setHeaderValue( key, value );
}
}
}
//-----------------------------------------------------------------------------------
public final void breakCommand()
{
closeConnection();
}
//-----------------------------------------------------------------------------------
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 )
{
e.printStackTrace();
}
}
//-----------------------------------------------------------------------------------
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 )
{
e.printStackTrace();
}
}
//-----------------------------------------------------------------------------------
public static void setResponseRuleList( List in_responseRuleList )
{
MSession.responseRuleList = in_responseRuleList;
}
//-----------------------------------------------------------------------------------
public static void setRequestRuleList( List in_requestRuleList )
{
MSession.requestRuleList = in_requestRuleList;
}
//-----------------------------------------------------------------------------------
public void update()
{
time++;
if( request != null )
{
int state = request.getState();
if( state == MHttpRequest.RECV_REQUEST_HEADER )
{
if( time > MSession.requestHeaderTimeOut )
{
doTimeOut();
}
}
else if( state == MHttpRequest.RECV_REQUEST_BODY )
{
if( time > MSession.requestBodyTimeOut )
{
doTimeOut();
}
}
}
}
//-----------------------------------------------------------------------------------
private void doTimeOut()
{
if( timedOut == true )
{
return;
}
else
{
timedOut = true;
logTimeOut();
closeConnection();
}
}
//-----------------------------------------------------------------------------------
private void logTimeOut()
{
StringBuffer strBuf = new StringBuffer( LOG_BUFSIZE );
strBuf.append( clientSocket.getInetAddress().getHostAddress() );
strBuf.append( ":" );
strBuf.append( clientSocket.getPort() );
strBuf.append( " " );
strBuf.append( "Session timed out." );
MLogger.getInstance().Log( strBuf.toString() );
}
//-----------------------------------------------------------------------------------
public static void setTimer( MTimer in_timer )
{
timer = in_timer;
}
//--------------------------------------------------------------------------------
public static void setConnectTimeOut( int i )
{
connectTimeOut = i;
}
//-----------------------------------------------------------------------------------
public static void setRequestBodyTimeOut( int in_requestBodyTimeOut )
{
requestBodyTimeOut = in_requestBodyTimeOut;
}
//-----------------------------------------------------------------------------------
public static void setRequestHeaderTimeOut( int in_requestHeaderTimeOut )
{
requestHeaderTimeOut = in_requestHeaderTimeOut;
}
//-----------------------------------------------------------------------------------
public static void setTargetPeer( String in_targetHost, int in_targetPort )
{
targetHost = in_targetHost;
targetPort = in_targetPort;
if( targetPort == 80 )
{
targetPeer = targetHost;
}
else
{
targetPeer = targetHost + ":" + String.valueOf( targetPort );
}
}
//-----------------------------------------------------------------------------------
public static void setReplaceHostField(boolean b)
{
replaceHostField = b;
}
//-----------------------------------------------------------------------------------
public static void setReplaceLocationField(boolean b)
{
replaceLocationField = b;
}
//-----------------------------------------------------------------------------------
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -