Ok here is my problem. Im writing a http proxy that will serve http request. So here is a little background.
The http protocol works on a request response basis clients and servers exchange messages. messages are terminated by the sequence "\r\n\r\n"
some implemenations use the sequence "\n\n" even though the rfc for the protocol explicitly stated the former sequence servers are asked to be tolerant.
The method posted below attempts to read a request from a client. This method works great especially when a client is pipelining request. In such a case its picks up 20 or more request and serve them simultaneously. processing one message at a time is slower. No the problem im having is when a client post data.. That includes a request follwed by the content the client wants to post, The contents gets mixed into the message because this function really checks the end of the stream for a message terminator. which is somewhere in the middle..
Code:
/**
* The method is called on the Selector thread for the object
* handling the channel.
* An asynchronous call from another thread wont cause the selector
* to throw an exception. Rather the wakeup method will be called
* interrupting the selector.
*/
final public void readMessage() {
try {
int r = connection.read(buffer);
if(r > 0) {
buffer.flip();
/*decode the buffer, append it to the request string
*determine if client has made a complete request.
*if so disable read interest in this channel and
*pass the request unto a handler
*/
CoderResult cr = asciiDecoder.decode(buffer, charBuffer, true);
if(cr.isError() || cr.isOverflow()) {
cr.throwException();
}
charBuffer.flip();
request.append(charBuffer);
int l = request.length();
if(request.substring(l-4, l).endsWith(MESSAGETERMINATOR)) {
selector.removeChannelInterest(connection, OP_READ);
threadPool.execute(this);
}
else if(request.length() > MAX_MESSAGE_LENGTH) {
logger.log("message length is too long! recieved from channel "+connection);
close();
}
buffer.clear();
charBuffer.clear();
}
else if(r == -1) { close(); }
}
catch(Exception ex) {
close();
logger.log(ex);
}
}
I need help coming up with and algorithm to avoid the post issue and still be able to process more that one request at a time.. If you don't understand what exactly im trying to do please post your questions and I will help to clarify. Thanks.