Skip to content

Commit

Permalink
Merge pull request #547 from cakofony/http_client_connection_safety
Browse files Browse the repository at this point in the history
HttpClientConnection catches failures more broadly
  • Loading branch information
stuartwdouglas committed Aug 24, 2017
2 parents 085e95b + 640e3ce commit 1842b27
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ public void handleEvent(StreamSourceConduit channel) {
private static final int UPGRADE_REQUESTED = 1 << 29;
private static final int CLOSE_REQ = 1 << 30;
private static final int CLOSED = 1 << 31;
private int count = 0;

private int state;
private final ChannelListener.SimpleSetter<HttpClientConnection> closeSetter = new ChannelListener.SimpleSetter<>();
Expand Down Expand Up @@ -331,7 +330,6 @@ public void sendRequest(final ClientRequest request, final ClientCallback<Client
http2Delegate.sendRequest(request, clientCallback);
return;
}
count++;
if (anyAreSet(state, UPGRADE_REQUESTED | UPGRADED | CLOSE_REQ | CLOSED)) {
clientCallback.failed(UndertowClientMessages.MESSAGES.invalidConnectionState());
return;
Expand Down Expand Up @@ -400,7 +398,7 @@ private void initiateRequest(HttpClientExchange httpClientExchange) {
conduit = new ClientFixedLengthStreamSinkConduit(conduit, length, false, false, currentRequest);
hasContent = length != 0;
} catch (NumberFormatException e) {
handleError(new IOException(e));
handleError(e);
return;
}
} else if (transferEncodingString != null) {
Expand Down Expand Up @@ -430,12 +428,19 @@ public void handleException(ConduitStreamSinkChannel channel, IOException except
}));
sinkChannel.resumeWrites();
}
} catch (IOException e) {
handleError(e);
} catch (Throwable t) {
handleError(t);
}
}
}

private void handleError(Throwable exception) {
if (exception instanceof IOException) {
handleError((IOException) exception);
} else {
handleError(new IOException(exception));
}
}
private void handleError(IOException exception) {
UndertowLogger.REQUEST_IO_LOGGER.ioException(exception);
safeClose(connection);
Expand Down Expand Up @@ -655,11 +660,11 @@ public void handleEvent(StreamSourceChannel channel) {
}


} catch (Exception e) {
UndertowLogger.CLIENT_LOGGER.exceptionProcessingRequest(e);
} catch (Throwable t) {
UndertowLogger.CLIENT_LOGGER.exceptionProcessingRequest(t);
safeClose(connection);
if(currentRequest != null) {
currentRequest.setFailed(new IOException(e));
currentRequest.setFailed(new IOException(t));
}
} finally {
if (free) {
Expand Down Expand Up @@ -708,7 +713,7 @@ private void prepareResponseChannel(ClientResponse response, ClientExchange exch
long contentLength = Long.parseLong(length);
connection.getSourceChannel().setConduit(new FixedLengthStreamSourceConduit(connection.getSourceChannel().getConduit(), contentLength, responseFinishedListener));
} catch (NumberFormatException e) {
handleError(new IOException(e));
handleError(e);
throw e;
}
} else if (response.getProtocol().equals(Protocols.HTTP_1_1) && !Connectors.isEntityBodyAllowed(response.getResponseCode())) {
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/io/undertow/util/ConnectionUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void handleException(ConduitStreamSinkChannel channel, IOException except
doDrain(connection, additional);
}

} catch (Exception e) {
} catch (Throwable e) {
if (e instanceof IOException) {
UndertowLogger.REQUEST_IO_LOGGER.ioException((IOException) e);
} else {
Expand Down Expand Up @@ -130,7 +130,7 @@ public void handleEvent(ConduitStreamSourceChannel channel) {
IoUtils.safeClose(connection);
IoUtils.safeClose(additional);
}
} catch (Exception e) {
} catch (Throwable e) {
if (e instanceof IOException) {
UndertowLogger.REQUEST_IO_LOGGER.ioException((IOException) e);
} else {
Expand Down

0 comments on commit 1842b27

Please sign in to comment.