📄 mhttprequest.java
字号:
}
break;
}
}
}
}
//-------------------------------------------------------------------------------
private void recvHeader()
throws IOException
{
bufferedInputStream.mark( Integer.MAX_VALUE );
bufferedInputStream.reset();
MLineReader reader = getLineReader();
reader.setInputStream( bufferedInputStream );
//1st line of HTTP request header
String line = reader.readLine();
if( line == null || line.equals( "" ) )
{
throw new MHttpIOException( "Couldn't receive HTTP request" );
}
splitRequestLine( line );
headerLength += line.length() + reader.getLastDelimiterSize();
readHeaderFields( reader );
}
//--------------------------------------------------------------------------------
private void splitRequestLine( String line )
throws IOException
{
//block "GET / HTTP/1.0 "
if( line.charAt( line.length() -1 ) == ' ' )
{
throw new MHttpIOException( "Invalid request line:" + line );
}
String[] requestLineArray = line.split( " " );
if( requestLineArray.length != 3 )
{
throw new MHttpIOException( "Invalid request line:" + line );
}
setMethod( requestLineArray[ 0 ] );
uri = requestLineArray[ 1 ];
version = requestLineArray[ 2 ];
}
//-------------------------------------------------------------------------------
public int getMethodType()
{
return methodType;
}
//-------------------------------------------------------------------------------
public byte[] getHeader()
{
ByteArrayOutputStream bufferStream = null;
try
{
// request line
bufferStream = new ByteArrayOutputStream( HEADER_BUFSIZE );
bufferStream.write( method.getBytes( MCharset.CS_ISO_8859_1 ) );
bufferStream.write( (byte)0x20 );
bufferStream.write( uri.getBytes( MCharset.CS_ISO_8859_1 ) );
bufferStream.write( (byte)0x20 );
bufferStream.write( version.getBytes( MCharset.CS_ISO_8859_1 ) );
bufferStream.write( CRLF );
// fields
int count = headerList.size();
for( int i = 0; i < count; ++i )
{
bufferStream.write( ( ( String )headerList.get( i ) ).getBytes( MCharset.CS_ISO_8859_1 ) );
bufferStream.write( CRLF );
}
// blank line
bufferStream.write( CRLF );
}
catch( IOException e )
{
//MLogger.getInstance().Log( e.toString() );
e.printStackTrace();
}
return bufferStream.toByteArray();
}
//-------------------------------------------------------------------------------
public String getUri()
{
return uri;
}
//-------------------------------------------------------------------------------
public void setVersion( String in_version )
{
version = in_version;
}
//-------------------------------------------------------------------------------
public String getVersion()
{
return version;
}
//-------------------------------------------------------------------------------
public int getHeaderLength()
{
return headerLength;
}
//-------------------------------------------------------------------------------
public void setUri(String uri)
{
this.uri = uri;
parseParameters();
}
//-------------------------------------------------------------------------------
public void setMethod( String in_method )
{
method = in_method;
if( method.equals( "GET" ) )
{
methodType = GET;
}
else if( method.equals( "POST" ) )
{
methodType = POST;
}
else if( method.equals( "HEAD" ) )
{
methodType = HEAD;
}
else if( method.equals( "PUT" ) )
{
methodType = PUT;
}
else if( method.equals( "DELETE" ) )
{
methodType = DELETE;
}
else if( method.equals( "TRACE" ) )
{
methodType = TRACE;
}
else if( method.equals( "CONNECT" ) )
{
methodType = CONNECT;
}
else if( method.equals( "OPTIONS" ) )
{
methodType = OPTIONS;
}
else
{
methodType = OTHER;
}
}
// --------------------------------------------------------------------------------
private void setHasBodyFlag()
throws MHttpIOException
{
if( headerExists( "Transfer-Encoding" ) )
{
hasBodyFlag = true;
return;
}
int contentLength = getContentLength();
hasBodyFlag = ( contentLength > 0 );
}
//-------------------------------------------------------------------------------
public String getRequestLine()
{
StringBuffer strBuf = new StringBuffer( 100 );
strBuf.append( method );
strBuf.append( " " );
strBuf.append( uri );
strBuf.append( " " );
strBuf.append( version );
return strBuf.toString();
}
//--------------------------------------------------------------------------------
public boolean isKeepAliveRequest()
{
boolean keepAlive;
if( version.equals( "HTTP/1.1" ) )
{
keepAlive = true;
if( headerExists( "Connection" )
&& getHeaderValue( "Connection" ).equalsIgnoreCase( "close" )
)
{
keepAlive = false;
}
}
else
{
keepAlive = false;
if( headerExists( "Connection" )
&& getHeaderValue( "Connection" ).equalsIgnoreCase( "Keep-Alive" )
)
{
keepAlive = true;
}
}
return keepAlive;
}
//-------------------------------------------------------------------------------
public int getState()
{
return state;
}
//-------------------------------------------------------------------------------
public String getAddr()
{
return addr;
}
//-------------------------------------------------------------------------------
public int getPort()
{
return port;
}
//-------------------------------------------------------------------------------
public void setPort( int in_port )
{
port = in_port;
}
//-------------------------------------------------------------------------------
public void setAddr( String in_addr )
{
addr = in_addr;
}
//--------------------------------------------------------------------------------
public String getMethod()
{
return method;
}
//-------------------------------------------------------------------------------
protected void recvBodyUntilDisconnected()
throws IOException
{
throw new MHttpException( 411, "Length Required" );
}
// --------------------------------------------------------------------------------
public List getCookieValueList( String name )
{
if( cookieNameSet1.contains( name ) )
{
List l = new ArrayList();
l.add( cookieMap1.get( name ) );
return l;
}
else if( cookieNameSet2.contains( name ) )
{
return ( List )cookieMap2.get( name );
}
else
{
return new ArrayList();
}
}
// --------------------------------------------------------------------------------
public String getCookieValue( String name )
{
if( cookieNameSet1.contains( name ) )
{
return ( String )cookieMap1.get( name );
}
else if( cookieNameSet2.contains( name ) )
{
return ( String )( ( List )cookieMap2.get( name ) ).get( 0 );
}
else
{
return "";
}
}
// --------------------------------------------------------------------------------
public List getParameterNameList()
{
List l = new ArrayList();
l.addAll( parameterNameSet1 );
l.addAll( parameterNameSet2 );
return l;
}
// --------------------------------------------------------------------------------
public List getParameterValueList( String name )
{
if( parameterNameSet1.contains( name ) )
{
List l = new ArrayList();
l.add( parameterMap1.get( name ) );
return l;
}
else if( parameterNameSet2.contains( name ) )
{
return ( List )parameterMap2.get( name );
}
else
{
return new ArrayList();
}
}
// --------------------------------------------------------------------------------
public String getParameterValue( String name )
{
if( parameterNameSet1.contains( name ) )
{
return ( String )parameterMap1.get( name );
}
else if( parameterNameSet2.contains( name ) )
{
return ( String )( ( List )parameterMap2.get( name ) ).get( 0 );
}
else
{
return "";
}
}
// --------------------------------------------------------------------------------
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -