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 {
private boolean isSecure = false;
private boolean isDelivered;
private static final String CONNECTION_CLOSED = "connection closed";
/**
* Constructs an HTTP Connection.
*
......@@ -50,7 +52,7 @@ public class HttpConnection {
}
try {
deliverBody(null);
deliverBody(CONNECTION_CLOSED);
}
catch (HttpConnectionClosedException e) {
/* Shouldn't happen */
......@@ -91,6 +93,9 @@ public class HttpConnection {
* a deliverable to forward to the client
*/
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.
if (isClosed) {
throw new HttpConnectionClosedException("The http connection is no longer " +
......@@ -172,6 +177,9 @@ public class HttpConnection {
if (deliverable == null) {
throw new HttpBindTimeoutException();
}
else if(CONNECTION_CLOSED.equals(deliverable)) {
return null;
}
return deliverable;
}
this.isDelivered = true;
......
......@@ -330,9 +330,6 @@ public class HttpSession extends ClientSession {
public String getResponse(long requestID) throws HttpBindException {
for (HttpConnection connection : connectionQueue) {
if (connection.getRequestId() == requestID) {
if (requestID > lastRequestID) {
lastRequestID = connection.getRequestId();
}
String response = getResponse(connection);
// connection needs to be removed after response is returned to maintain idempotence
......@@ -346,12 +343,20 @@ public class HttpSession extends ClientSession {
throw new InternalError("Could not locate connection: " + requestID);
}
private String getResponse(HttpConnection connection) {
String response;
private String getResponse(HttpConnection connection) throws HttpBindException {
String response = null;
try {
response = connection.getResponse();
}
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();
}
return response;
......@@ -448,10 +453,15 @@ public class HttpSession extends ClientSession {
// Number of current connections open plus the current one tells us how many that
// we need to close.
int connectionsToClose = (connectionQueue.size() + 1) - hold;
for (int i = 0; i < connectionsToClose; i++) {
int connectionsToClose = (getOpenConnectionCount() + 1) - hold;
int closed = 0;
for (int i = 0; i < connectionQueue.size() && closed < connectionsToClose; i++) {
HttpConnection toClose = connectionQueue.get(i);
toClose.close();
if (!toClose.isClosed()) {
lastRequestID = toClose.getRequestId();
toClose.close();
closed++;
}
}
}
connectionQueue.add(connection);
......@@ -459,6 +469,16 @@ public class HttpSession extends ClientSession {
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)
throws HttpConnectionClosedException {
connection.deliverBody(createDeliverable(deliverable));
......@@ -502,7 +522,8 @@ public class HttpSession extends ClientSession {
boolean delivered = false;
for (HttpConnection connection : connectionQueue) {
try {
if (connection.getRequestId() <= lastRequestID + 1) {
if (connection.getRequestId() == lastRequestID + 1) {
lastRequestID = connection.getRequestId();
deliver(connection, deliverable);
delivered = true;
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