Added debug output showing the message header for a HTTP message that failed to be read.

This commit is contained in:
wcrisman
2014-09-17 09:49:04 -07:00
parent b48e81bfe0
commit 46df41d433

View File

@@ -285,8 +285,8 @@ public class WebServer {
private abstract class AbstractSocketContext extends ChannelContext {
/** The key that represents the connection between the channel (socket) and the selector used to multiplex the listener. */
public SelectionKey key = null;
// /** The SelectionKey flags that will be used when the NetworkListener thread finishes processing the input/output of a message. The thread can safely change these flags without synchronizing. */
// private int flags = 0;
/** The SelectionKey flags that will be used when the NetworkListener thread finishes processing the input/output of a message. The thread can safely change these flags without synchronizing. */
private int flags = 0;
/** A socket context related to this one (when two are tied together such that data from one immediately is sent to the other). */
protected AbstractSocketContext relatedSocketContext = null;
@@ -326,6 +326,12 @@ public class WebServer {
*/
protected void flagWrite(boolean requiresWrite) {
synchronized(key) {
boolean hasWrite = (flags & SelectionKey.OP_WRITE) != 0;
if(hasWrite != requiresWrite) {
flags ^= SelectionKey.OP_WRITE;
}//if//
/*
if(key.isValid()) {
int ops = key.interestOps();
boolean hasWrite = (ops & SelectionKey.OP_WRITE) != 0;
@@ -341,6 +347,7 @@ public class WebServer {
}//if//
}//else//
}//if//
*/
}//synchronized//
}//flagWrite()//
/**
@@ -349,6 +356,12 @@ public class WebServer {
*/
protected void flagRead(boolean requiresRead) {
synchronized(key) {
boolean hasRead = (flags & SelectionKey.OP_READ) != 0;
if(hasRead != requiresRead) {
flags ^= SelectionKey.OP_READ;
}//if//
/*
if(key.isValid()) {
int ops = key.interestOps();
boolean hasRead = (ops & SelectionKey.OP_READ) != 0;
@@ -364,10 +377,22 @@ public class WebServer {
}//if//
}//else//
}//if//
*/
}//synchronized//
}//flagWrite()//
protected void flagReadWrite(boolean requiresRead, boolean requiresWrite) {
synchronized(key) {
boolean hasWrite = (flags & SelectionKey.OP_WRITE) != 0;
boolean hasRead = (flags & SelectionKey.OP_READ) != 0;
if(hasRead != requiresRead) {
flags ^= SelectionKey.OP_READ;
}//if//
if(hasWrite != requiresWrite) {
flags ^= SelectionKey.OP_WRITE;
}//if//
/*
if(key.isValid()) {
int ops = key.interestOps();
boolean hasRead = (ops & SelectionKey.OP_READ) != 0;
@@ -397,21 +422,25 @@ public class WebServer {
key.interestOps(ops);
}//if//
*/
}//synchronized//
}//flagReadWrite()//
protected void flagReadWrite() {
synchronized(key) {
if(key.isValid()) key.interestOps(SelectionKey.OP_READ | SelectionKey.OP_WRITE);
//if(key.isValid()) key.interestOps(SelectionKey.OP_READ | SelectionKey.OP_WRITE);
flags = SelectionKey.OP_READ | SelectionKey.OP_WRITE;
}//synchronized//
}//flagReadWrite()//
protected void flagWriteOnly() {
synchronized(key) {
if(key.isValid()) key.interestOps(SelectionKey.OP_WRITE);
//if(key.isValid()) key.interestOps(SelectionKey.OP_WRITE);
flags = SelectionKey.OP_WRITE;
}//synchronized//
}//flagWriteOnly()//
protected void flagReadOnly() {
synchronized(key) {
if(key.isValid()) key.interestOps(SelectionKey.OP_READ);
//if(key.isValid()) key.interestOps(SelectionKey.OP_READ);
flags = SelectionKey.OP_READ;
}//synchronized//
}//flagReadOnly()//
}//AbstractSocketContext//
@@ -1856,6 +1885,7 @@ public class WebServer {
final boolean isWrite = key.isWritable();
final ChannelContext context = (ChannelContext) key.attachment();
final SelectableChannel channel = key.channel();
final SelectionKey selectionKey = key;
if(channel instanceof ServerSocketChannel) {
try {
@@ -1897,9 +1927,12 @@ public class WebServer {
//Truely enabling Speedy would require a thread to read which when finished would flag read again BEFORE processing the message and BEFORE sending a response.
//For now (so we don't have to require jdk7 yet) we will simply allow Speedy to queue up messages, but only read, process, and then write them one at a time. Most of the speed loss is in the waiting for the WRITE to finish before handling the next request (due to it being broken into packets and the mechanics of TCP), and that is generally minimal (speed lose) since usually the bottleneck in speed is the browser's connection to the internet (most of us haven't got Gigabit Ethernet at home). Anyone with enough home juice to have this be a problem would only notice the difference for really porky websites (which is a problem in and of its self).
key.interestOps(key.interestOps() ^ (isWrite ? SelectionKey.OP_WRITE : SelectionKey.OP_READ));
//Not allowing either reads or writes to continue until all processing of this message is done.//
// key.interestOps(0);
((AbstractSocketContext) context).flags = key.interestOps();
key.interestOps(0);
//The problem with this is that we'd have to use AsynchronousSocketChannel which would appear to require a complete rewrite of everything since it operates completely differently.//
// key.interestOps(key.interestOps() ^ (isWrite ? SelectionKey.OP_WRITE : SelectionKey.OP_READ));
}//synchronized//
if(((SocketChannel) channel).isOpen()) {
@@ -1937,7 +1970,19 @@ public class WebServer {
}//catch//
finally {
if(channel != null && !socketClosed && channel.isOpen() && context != null) {
try {
//Set the new ops for the selection key and notify the selector that ops have changed.//
synchronized(selectionKey) {
if(selectionKey.isValid()) {
selectionKey.interestOps(((AbstractSocketContext) context).flags);
}//if//
}//synchronized//
selector.wakeup();
}//try//
catch(Throwable e) {
Debug.log(e);
}//catch//
}//if//
else if(channel != null && (!channel.isOpen() || socketClosed) && channel instanceof SocketChannel && context instanceof SocketContext) {
cleanupClientChannel((SocketContext) context, (SocketChannel) channel);
@@ -2716,6 +2761,11 @@ private boolean processClientRequest(SocketContext context, ByteBuffer fragment,
}//if//
}//try//
catch(IOException e) {
Debug.log(" ===== Begin exception output. =====");
Debug.log("Processing:\r\n" + context.messageHeaderFragment.toString());
Debug.log(e);
Debug.log("The following IOException is a result of this exception.");
Debug.log(" ===== End exception output. =====");
throw e;
}//catch//
catch(Throwable e) {