Commit 0ab8be05 authored by Alex Wenckus's avatar Alex Wenckus Committed by alex

http-bind request fixes: JM-986

git-svn-id: http://svn.igniterealtime.org/svn/repos/wildfire/trunk@7200 b35dd754-fafc-0310-a699-88a17e54d16e
parent f284466c
...@@ -29,6 +29,8 @@ public class HttpConnection { ...@@ -29,6 +29,8 @@ public class HttpConnection {
private boolean isSecure = false; private boolean isSecure = false;
private boolean isDelivered; private boolean isDelivered;
private static final String CONNECTION_CLOSED = "connection closed";
/** /**
* Constructs an HTTP Connection. * Constructs an HTTP Connection.
* *
...@@ -50,7 +52,7 @@ public class HttpConnection { ...@@ -50,7 +52,7 @@ public class HttpConnection {
} }
try { try {
deliverBody(null); deliverBody(CONNECTION_CLOSED);
} }
catch (HttpConnectionClosedException e) { catch (HttpConnectionClosedException e) {
/* Shouldn't happen */ /* Shouldn't happen */
...@@ -91,6 +93,9 @@ public class HttpConnection { ...@@ -91,6 +93,9 @@ public class HttpConnection {
* a deliverable to forward to the client * a deliverable to forward to the client
*/ */
public void deliverBody(String body) throws HttpConnectionClosedException { public void deliverBody(String body) throws HttpConnectionClosedException {
if(body == null) {
throw new IllegalArgumentException("Body cannot be null!");
}
// We only want to use this function once so we will close it when the body is delivered. // We only want to use this function once so we will close it when the body is delivered.
if (isClosed) { if (isClosed) {
throw new HttpConnectionClosedException("The http connection is no longer " + throw new HttpConnectionClosedException("The http connection is no longer " +
...@@ -172,6 +177,9 @@ public class HttpConnection { ...@@ -172,6 +177,9 @@ public class HttpConnection {
if (deliverable == null) { if (deliverable == null) {
throw new HttpBindTimeoutException(); throw new HttpBindTimeoutException();
} }
else if(CONNECTION_CLOSED.equals(deliverable)) {
return null;
}
return deliverable; return deliverable;
} }
this.isDelivered = true; this.isDelivered = true;
......
...@@ -330,9 +330,6 @@ public class HttpSession extends ClientSession { ...@@ -330,9 +330,6 @@ public class HttpSession extends ClientSession {
public String getResponse(long requestID) throws HttpBindException { public String getResponse(long requestID) throws HttpBindException {
for (HttpConnection connection : connectionQueue) { for (HttpConnection connection : connectionQueue) {
if (connection.getRequestId() == requestID) { if (connection.getRequestId() == requestID) {
if (requestID > lastRequestID) {
lastRequestID = connection.getRequestId();
}
String response = getResponse(connection); String response = getResponse(connection);
// connection needs to be removed after response is returned to maintain idempotence // connection needs to be removed after response is returned to maintain idempotence
...@@ -346,12 +343,20 @@ public class HttpSession extends ClientSession { ...@@ -346,12 +343,20 @@ public class HttpSession extends ClientSession {
throw new InternalError("Could not locate connection: " + requestID); throw new InternalError("Could not locate connection: " + requestID);
} }
private String getResponse(HttpConnection connection) { private String getResponse(HttpConnection connection) throws HttpBindException {
String response; String response = null;
try { try {
response = connection.getResponse(); response = connection.getResponse();
} }
catch (HttpBindTimeoutException e) { catch (HttpBindTimeoutException e) {
// This connection timed out we need to increment the request count
if(connection.getRequestId() != lastRequestID + 1) {
Log.warn("Connection timed out: " + connection.getRequestId() + ". " + e.getMessage());
throw new HttpBindException("Unexpected RID error.", true, 404);
}
lastRequestID = connection.getRequestId();
}
if(response == null) {
response = createEmptyBody(); response = createEmptyBody();
} }
return response; return response;
...@@ -448,10 +453,15 @@ public class HttpSession extends ClientSession { ...@@ -448,10 +453,15 @@ public class HttpSession extends ClientSession {
// Number of current connections open plus the current one tells us how many that // Number of current connections open plus the current one tells us how many that
// we need to close. // we need to close.
int connectionsToClose = (connectionQueue.size() + 1) - hold; int connectionsToClose = (getOpenConnectionCount() + 1) - hold;
for (int i = 0; i < connectionsToClose; i++) { int closed = 0;
for (int i = 0; i < connectionQueue.size() && closed < connectionsToClose; i++) {
HttpConnection toClose = connectionQueue.get(i); HttpConnection toClose = connectionQueue.get(i);
toClose.close(); if (!toClose.isClosed()) {
lastRequestID = toClose.getRequestId();
toClose.close();
closed++;
}
} }
} }
connectionQueue.add(connection); connectionQueue.add(connection);
...@@ -459,6 +469,16 @@ public class HttpSession extends ClientSession { ...@@ -459,6 +469,16 @@ public class HttpSession extends ClientSession {
fireConnectionOpened(connection); fireConnectionOpened(connection);
} }
private int getOpenConnectionCount() {
int count = 0;
for(HttpConnection connection : connectionQueue) {
if(!connection.isClosed()) {
count++;
}
}
return count;
}
private void deliver(HttpConnection connection, Collection<Deliverable> deliverable) private void deliver(HttpConnection connection, Collection<Deliverable> deliverable)
throws HttpConnectionClosedException { throws HttpConnectionClosedException {
connection.deliverBody(createDeliverable(deliverable)); connection.deliverBody(createDeliverable(deliverable));
...@@ -502,7 +522,8 @@ public class HttpSession extends ClientSession { ...@@ -502,7 +522,8 @@ public class HttpSession extends ClientSession {
boolean delivered = false; boolean delivered = false;
for (HttpConnection connection : connectionQueue) { for (HttpConnection connection : connectionQueue) {
try { try {
if (connection.getRequestId() <= lastRequestID + 1) { if (connection.getRequestId() == lastRequestID + 1) {
lastRequestID = connection.getRequestId();
deliver(connection, deliverable); deliver(connection, deliverable);
delivered = true; delivered = true;
break; break;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment