Fixed bug in MessageBuffer to close the buffer instead of just setting buffer = null. Modified MessageBuffer to close the response if closed its self. Modified SocketContext to get rid of the response linked list, and close the outbound message instead of the response (no more references to Response). Modified SocketContext to immediately create a MessageBuffer from a Response that is ready to be sent, and set it as the currentOutboundMessage (no list or linked list used currently). Modified writeSessionCookies() and prepareResponse() in SocketContext to take the Response as a parameter.
This commit is contained in:
@@ -78,6 +78,11 @@ class MessageBuffer {
|
|||||||
*/
|
*/
|
||||||
public void close() {
|
public void close() {
|
||||||
this.buffer = null;
|
this.buffer = null;
|
||||||
|
|
||||||
|
if(response != null) {
|
||||||
|
response.close();
|
||||||
|
response = null;
|
||||||
|
}//if//
|
||||||
}//close()//
|
}//close()//
|
||||||
/**
|
/**
|
||||||
* Gets the byte buffer containing the current portion of the message to be sent.
|
* Gets the byte buffer containing the current portion of the message to be sent.
|
||||||
@@ -102,14 +107,14 @@ class MessageBuffer {
|
|||||||
result = false; //Should never occur currently: See StreamedContent's javadocs.//
|
result = false; //Should never occur currently: See StreamedContent's javadocs.//
|
||||||
}//if//
|
}//if//
|
||||||
else if(getResult == IContent.CONTENT_END) {
|
else if(getResult == IContent.CONTENT_END) {
|
||||||
buffer = null;
|
close();
|
||||||
}//else if//
|
}//else if//
|
||||||
|
|
||||||
if(buffer != null && buffer.position() != 0) buffer.flip();
|
if(buffer != null && buffer.position() != 0) buffer.flip();
|
||||||
}//if//
|
}//if//
|
||||||
else if(!buffer.hasRemaining()) {
|
else if(!buffer.hasRemaining()) {
|
||||||
//Clear the buffer pointer indicating the message buffer is done.//
|
//Clear the buffer pointer indicating the message buffer is done.//
|
||||||
buffer = null;
|
close();
|
||||||
result = false;
|
result = false;
|
||||||
}//else if//
|
}//else if//
|
||||||
}//if//
|
}//if//
|
||||||
|
|||||||
@@ -95,9 +95,9 @@ public class SocketContext extends AbstractSocketContext implements IWebApplicat
|
|||||||
/** The last used request number which identifies the sequence for the requests. */
|
/** The last used request number which identifies the sequence for the requests. */
|
||||||
private int lastRequestNumber = 0;
|
private int lastRequestNumber = 0;
|
||||||
/** The response we are currently processing. */
|
/** The response we are currently processing. */
|
||||||
private Response currentResponse = null;
|
// private Response currentResponse = null;
|
||||||
/** The response we are will process last. */
|
// /** The response we are will process last. */
|
||||||
private Response lastResponse = null;
|
// private Response lastResponse = null;
|
||||||
/** Tracks the number of bytes sent from the current response. This is only used when debugging. */
|
/** Tracks the number of bytes sent from the current response. This is only used when debugging. */
|
||||||
private int sentBytes = 0;
|
private int sentBytes = 0;
|
||||||
/** Tracks the getWebServer().debug output for the current request/response cycle. This is only used when debugging. */
|
/** Tracks the getWebServer().debug output for the current request/response cycle. This is only used when debugging. */
|
||||||
@@ -189,7 +189,8 @@ protected synchronized void close() {
|
|||||||
try {if(key != null) key.cancel();} catch(Throwable e) {}
|
try {if(key != null) key.cancel();} catch(Throwable e) {}
|
||||||
//Clean up after the response and request.//
|
//Clean up after the response and request.//
|
||||||
//try {while(currentOutboundMessage != null) {currentOutboundMessage.close(); currentOutboundMessage = currentOutboundMessage.getNext();}} catch(Throwable e2) {}
|
//try {while(currentOutboundMessage != null) {currentOutboundMessage.close(); currentOutboundMessage = currentOutboundMessage.getNext();}} catch(Throwable e2) {}
|
||||||
try {if(currentResponse != null) currentResponse.close();} catch(Throwable e2) {}
|
// try {if(currentResponse != null) currentResponse.close();} catch(Throwable e2) {}
|
||||||
|
try {if(currentOutboundMessage != null) currentOutboundMessage.close();} catch(Throwable e2) {}
|
||||||
|
|
||||||
if(getPassThroughSocketContext() != null) {
|
if(getPassThroughSocketContext() != null) {
|
||||||
getPassThroughSocketContext().close();
|
getPassThroughSocketContext().close();
|
||||||
@@ -246,8 +247,7 @@ private void queueOutboundClientMessage(MessageBuffer messageBuffer) {
|
|||||||
notifyListenerOfPendingWrite();
|
notifyListenerOfPendingWrite();
|
||||||
}//if//
|
}//if//
|
||||||
}//queueOutboundClientMessage()//
|
}//queueOutboundClientMessage()//
|
||||||
private void writeSessionCookies(PrintStream pout) {
|
private void writeSessionCookies(Response response, PrintStream pout) {
|
||||||
Response response = currentResponse;
|
|
||||||
ISession session = response.getSession();
|
ISession session = response.getSession();
|
||||||
|
|
||||||
if(session != null) {
|
if(session != null) {
|
||||||
@@ -271,8 +271,7 @@ private void writeSessionCookies(PrintStream pout) {
|
|||||||
* <p>Note: The caller must synchronize on this context to prevent multiple threads from accessing the context at the same time.</p>
|
* <p>Note: The caller must synchronize on this context to prevent multiple threads from accessing the context at the same time.</p>
|
||||||
* @result Whether request is in a receive state. Will be false if the request generated a response that could not be completely transmitted.
|
* @result Whether request is in a receive state. Will be false if the request generated a response that could not be completely transmitted.
|
||||||
*/
|
*/
|
||||||
private void prepareResponse() {
|
private void prepareResponse(Response response) {
|
||||||
Response response = currentResponse;
|
|
||||||
Request request = (Request) response.getRequest();
|
Request request = (Request) response.getRequest();
|
||||||
byte[] headerBytes = null;
|
byte[] headerBytes = null;
|
||||||
IContent content = null;
|
IContent content = null;
|
||||||
@@ -308,7 +307,7 @@ private void prepareResponse() {
|
|||||||
}//for//
|
}//for//
|
||||||
|
|
||||||
//Write out any cookies necessary to retain our session.//
|
//Write out any cookies necessary to retain our session.//
|
||||||
writeSessionCookies(pout);
|
writeSessionCookies(response, pout);
|
||||||
|
|
||||||
//End the header.//
|
//End the header.//
|
||||||
pout.print("\r\n");
|
pout.print("\r\n");
|
||||||
@@ -331,7 +330,7 @@ private void prepareResponse() {
|
|||||||
pout.print("HTTP/1.1 302 Moved Temporarily\r\n");
|
pout.print("HTTP/1.1 302 Moved Temporarily\r\n");
|
||||||
//}//else//
|
//}//else//
|
||||||
|
|
||||||
writeSessionCookies(pout);
|
writeSessionCookies(response, pout);
|
||||||
|
|
||||||
pout.print("Location: " + response.getForwardUri() + "\r\n");
|
pout.print("Location: " + response.getForwardUri() + "\r\n");
|
||||||
|
|
||||||
@@ -423,7 +422,7 @@ private void prepareResponse() {
|
|||||||
}//if//
|
}//if//
|
||||||
}//if//
|
}//if//
|
||||||
|
|
||||||
writeSessionCookies(pout);
|
writeSessionCookies(response, pout);
|
||||||
|
|
||||||
pout.print("Server: DE/1.0\r\n");
|
pout.print("Server: DE/1.0\r\n");
|
||||||
//TODO: IE has a problem with caching and forwarding/redirecting. A page that redirects to another page that was previously cached does not result in IE sending a request for the forwarded content.//
|
//TODO: IE has a problem with caching and forwarding/redirecting. A page that redirects to another page that was previously cached does not result in IE sending a request for the forwarded content.//
|
||||||
@@ -487,7 +486,7 @@ private void prepareResponse() {
|
|||||||
pout.print("HTTP/1.1 200 OK\r\n");
|
pout.print("HTTP/1.1 200 OK\r\n");
|
||||||
}//else//
|
}//else//
|
||||||
|
|
||||||
writeSessionCookies(pout);
|
writeSessionCookies(response, pout);
|
||||||
pout.print("Content-Length: 0\r\n");
|
pout.print("Content-Length: 0\r\n");
|
||||||
pout.print("Server: DE/1.0\r\n");
|
pout.print("Server: DE/1.0\r\n");
|
||||||
pout.print("\r\n");
|
pout.print("\r\n");
|
||||||
@@ -534,8 +533,8 @@ private void prepareResponse() {
|
|||||||
*/
|
*/
|
||||||
public synchronized boolean sendHttpResponse(Response response) {
|
public synchronized boolean sendHttpResponse(Response response) {
|
||||||
//Short circuit the response linked list since it shouldn't be being used.//
|
//Short circuit the response linked list since it shouldn't be being used.//
|
||||||
lastResponse = currentResponse = response;
|
// lastResponse = currentResponse = response;
|
||||||
prepareResponse();
|
prepareResponse(response);
|
||||||
// if(currentResponse != null) {
|
// if(currentResponse != null) {
|
||||||
// lastResponse.setNextResponse(response);
|
// lastResponse.setNextResponse(response);
|
||||||
// lastResponse = response;
|
// lastResponse = response;
|
||||||
@@ -730,7 +729,7 @@ private synchronized void internalProcessResponses() {
|
|||||||
boolean finishedSending = true;
|
boolean finishedSending = true;
|
||||||
|
|
||||||
//Keep sending responses while the buffers are not full and there is another response to send.//
|
//Keep sending responses while the buffers are not full and there is another response to send.//
|
||||||
while(finishedSending && currentResponse != null) {
|
while(finishedSending) {
|
||||||
//If the socket is open then send the next buffer of data.//
|
//If the socket is open then send the next buffer of data.//
|
||||||
if(key.channel().isOpen()) {
|
if(key.channel().isOpen()) {
|
||||||
//Send the pending response object's prepared buffer of data.//
|
//Send the pending response object's prepared buffer of data.//
|
||||||
@@ -739,32 +738,52 @@ private synchronized void internalProcessResponses() {
|
|||||||
|
|
||||||
//Close the response if successfully sent, or if the socket is closed.//
|
//Close the response if successfully sent, or if the socket is closed.//
|
||||||
if(finishedSending || !key.channel().isOpen()) {
|
if(finishedSending || !key.channel().isOpen()) {
|
||||||
try {currentResponse.close();} catch(Throwable e) {}
|
try {currentOutboundMessage.close();} catch(Throwable e) {}
|
||||||
}//if//
|
}//if//
|
||||||
|
|
||||||
//If we finished sending the current response then load the next one.//
|
//If we finished sending the current response then load the next one.//
|
||||||
if(finishedSending) {
|
if(finishedSending) {
|
||||||
currentResponse = currentResponse.getNextResponse();
|
//TODO: Queue up the next outbound message.
|
||||||
|
currentOutboundMessage = null;
|
||||||
if(currentResponse == null) {
|
|
||||||
lastResponse = null;
|
|
||||||
}//if//
|
|
||||||
else if(key.channel().isOpen()) {
|
|
||||||
//Prep the next response object for sending.//
|
|
||||||
prepareResponse();
|
|
||||||
}//else//
|
|
||||||
else {
|
|
||||||
//Clean up after all the left over responses.//
|
|
||||||
while(currentResponse != null) {
|
|
||||||
currentResponse.close();
|
|
||||||
currentResponse = currentResponse.getNextResponse();
|
|
||||||
}//while//
|
|
||||||
|
|
||||||
currentResponse = null;
|
|
||||||
lastResponse = null;
|
|
||||||
}//else//
|
|
||||||
}//if//
|
}//if//
|
||||||
}//while//
|
}//while//
|
||||||
|
|
||||||
|
// //Keep sending responses while the buffers are not full and there is another response to send.//
|
||||||
|
// while(finishedSending && currentResponse != null) {
|
||||||
|
// //If the socket is open then send the next buffer of data.//
|
||||||
|
// if(key.channel().isOpen()) {
|
||||||
|
// //Send the pending response object's prepared buffer of data.//
|
||||||
|
// finishedSending = writeClientBoundMessage();
|
||||||
|
// }//if//
|
||||||
|
//
|
||||||
|
// //Close the response if successfully sent, or if the socket is closed.//
|
||||||
|
// if(finishedSending || !key.channel().isOpen()) {
|
||||||
|
// try {currentResponse.close();} catch(Throwable e) {}
|
||||||
|
// }//if//
|
||||||
|
//
|
||||||
|
// //If we finished sending the current response then load the next one.//
|
||||||
|
// if(finishedSending) {
|
||||||
|
// currentResponse = currentResponse.getNextResponse();
|
||||||
|
//
|
||||||
|
// if(currentResponse == null) {
|
||||||
|
// lastResponse = null;
|
||||||
|
// }//if//
|
||||||
|
// else if(key.channel().isOpen()) {
|
||||||
|
// //Prep the next response object for sending.//
|
||||||
|
// prepareResponse();
|
||||||
|
// }//else//
|
||||||
|
// else {
|
||||||
|
// //Clean up after all the left over responses.//
|
||||||
|
// while(currentResponse != null) {
|
||||||
|
// currentResponse.close();
|
||||||
|
// currentResponse = currentResponse.getNextResponse();
|
||||||
|
// }//while//
|
||||||
|
//
|
||||||
|
// currentResponse = null;
|
||||||
|
// lastResponse = null;
|
||||||
|
// }//else//
|
||||||
|
// }//if//
|
||||||
|
// }//while//
|
||||||
}//processCurrentResponse()//
|
}//processCurrentResponse()//
|
||||||
/**
|
/**
|
||||||
* Sends a response to the client.
|
* Sends a response to the client.
|
||||||
|
|||||||
Reference in New Issue
Block a user