Skip to content

Commit

Permalink
Close channels when normal complete or error (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
ruhan1 authored Jul 8, 2024
1 parent 2b0abb4 commit efbf93c
Showing 1 changed file with 20 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

import static java.lang.Integer.parseInt;
import static org.apache.commons.codec.digest.DigestUtils.sha256Hex;
import static org.apache.commons.io.IOUtils.closeQuietly;
import static org.commonjava.indy.model.core.ArtifactStore.TRACKING_ID;
import static org.commonjava.indy.service.httprox.util.ApplicationHeader.proxy_authenticate;
import static org.commonjava.indy.service.httprox.util.ApplicationStatus.PROXY_AUTHENTICATION_REQUIRED;
Expand Down Expand Up @@ -115,7 +116,6 @@ public void handleEvent(final ConduitStreamSinkChannel channel) {

private void doHandleEvent(final ConduitStreamSinkChannel sinkChannel)
{

if ( directed )
{
return;
Expand All @@ -131,14 +131,10 @@ private void doHandleEvent(final ConduitStreamSinkChannel sinkChannel)
logger.debug("Handling error from request reader: " + error.getMessage(), error);
handleError(error, http);
} else {
logger.debug("Invalid state (no error or request) from request reader. Sending 400.");
try {
http.writeStatus(ApplicationStatus.BAD_REQUEST);
} catch (final IOException e) {
logger.error("Failed to write BAD REQUEST for missing HTTP first-line to response channel.", e);
}
handleBadRequest(http);
}

closeQuietly(sinkChannel);
closeQuietly(sourceChannel);
return;
}

Expand Down Expand Up @@ -302,7 +298,6 @@ private void doHandleEvent(final ConduitStreamSinkChannel sinkChannel)
}
}
}

logger.debug("Response complete.");
} catch (final Throwable e) {
error = e;
Expand All @@ -313,22 +308,25 @@ private void doHandleEvent(final ConduitStreamSinkChannel sinkChannel)
handleError(error, http);
}

try
if ( directed )
{
if ( directed )
{
// do not close sink channel
}
else
{
http.close();
}
// do not close sink channel
}
catch (final IOException e)
else
{
logger.error("Failed to shutdown response", e);
closeQuietly( http );
closeQuietly( sinkChannel );
closeQuietly( sourceChannel );
}
}

private void handleBadRequest(HttpConduitWrapper http) {
logger.warn("Invalid state (no error or request) from request reader. Sending 400.");
try {
http.writeStatus(ApplicationStatus.BAD_REQUEST);
} catch (IOException e) {
logger.warn("Failed to write BAD_REQUEST", e);
}
}

private String generateAuthCacheKey( UserPass proxyUserPass )
Expand All @@ -342,11 +340,10 @@ private void handleError(final Throwable error, final HttpWrapper http) {
if (http.isOpen()) {
http.writeStatus(ApplicationStatus.SERVER_ERROR);
http.writeError(error);

logger.debug("Response error complete.");
}
} catch (final IOException closeException) {
logger.error("Failed to close httprox request: " + error.getMessage(), error);
} catch (final IOException e) {
logger.warn("Failed to write error: " + error.getMessage(), error);
}
}

Expand Down

0 comments on commit efbf93c

Please sign in to comment.