我需要实现一些支持SSL的http代理。 SSL工作正常,我需要将incomming消息解析为HTTP请求或其他。
我认为它可以起作用:
1. in messageReceived I should check I will check if first "maximum-length
of HTTP request method name length" amout of bytes converted to string
(new String(byte[]) starts with any of HTTP method name (e.g. GET,POST..)
2a) If so, let's tread this session as HTTP request, grap whole request and
send it to another HTTP server, wait for response, return the response and
close the connection.
2b) Otherwise, this is binary tcp connection with data, I want to keep this
connection alive and play with incoming data/send data to it.
问题是,在messageReceived()回调中,我有时会收到完整的,有时是不完整的HTTP请求数据,并且会多次调用它。例如。当我对端口运行HTTP请求时,它会被调用,例如一次只有第一个http行+标题,然后用POST数据调用它。
我已经使用mina设置了TCP监听:
IoAcceptor acceptor = new NioSocketAcceptor();
int port = 5555; //whatever
DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();
// ssl support
SslFilter sslFilter = new SslFilter(MySslContextFactory.getInstance(true));
chain.addFirst("sslFilter", sslFilter);
// handling
acceptor.setHandler( new TCPHandler() );
// listening at 0.0.0.0:5555
acceptor.bind( new InetSocketAddress(port) );
TCPHandler.java messageReceived代码段如下所示:
@Override
public void messageReceived( IoSession session, Object message ) throws Exception
IoBuffer msgBuffer = (IoBuffer)message;
byte[] msgBytes = msgBuffer.array();
msgBytes = Arrays.copyOfRange(msgBytes, 0, msgBuffer.limit());
}
我不知道如何/何时检查我是否已阅读HTTP请求的所有数据。它通常包含一串数据(当检查msgBytes变量时)