Skip to content

Commit

Permalink
Merge pull request #2137 from DedunuKarunarathne/fix-http-code
Browse files Browse the repository at this point in the history
Fix incorrect HTTP status code when an HTTP request with synapse unsupported HTTP Method is received
  • Loading branch information
DedunuKarunarathne authored Feb 1, 2024
2 parents c008566 + 42739df commit f70908a
Showing 1 changed file with 45 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.HttpVersion;
import org.apache.http.MethodNotSupportedException;
import org.apache.http.message.BasicHttpResponse;
import org.apache.http.nio.ContentDecoder;
import org.apache.http.nio.ContentEncoder;
Expand Down Expand Up @@ -895,42 +896,12 @@ public void exception(NHttpServerConnection conn, Exception ex) {
isFault = true;
SourceContext.updateState(conn, ProtocolState.CLOSED);
sourceConfiguration.getSourceConnections().shutDownConnection(conn, true);
} else if (ex instanceof MethodNotSupportedException) {
isFault = generateHTTPErrorResponse(conn, ex, HttpVersion.HTTP_1_1, HttpStatus.SC_NOT_IMPLEMENTED,
"Not Implemented");
} else if (ex instanceof HttpException) {
log.error("HttpException occurred ", ex);
if (PassThroughCorrelationConfigDataHolder.isEnable()) {
logHttpRequestErrorInCorrelationLog(conn, "HTTP Exception");
}
try {
if (conn.isResponseSubmitted()) {
sourceConfiguration.getSourceConnections().shutDownConnection(conn, true);
return;
}
HttpContext httpContext = conn.getContext();

HttpResponse response = new BasicHttpResponse(
HttpVersion.HTTP_1_1, HttpStatus.SC_BAD_REQUEST, "Bad request");
response.setParams(
new DefaultedHttpParams(sourceConfiguration.getHttpParams(),
response.getParams()));
response.addHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_CLOSE);

// Pre-process HTTP request
httpContext.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
httpContext.setAttribute(ExecutionContext.HTTP_REQUEST, null);
httpContext.setAttribute(ExecutionContext.HTTP_RESPONSE, response);

sourceConfiguration.getHttpProcessor().process(response, httpContext);

conn.submitResponse(response);
SourceContext.updateState(conn, ProtocolState.CLOSED);
informWriterError(conn);
conn.close();
} catch (Exception ex1) {
log.error(ex1.getMessage(), ex1);
SourceContext.updateState(conn, ProtocolState.CLOSED);
sourceConfiguration.getSourceConnections().shutDownConnection(conn, true);
isFault = true;
}
isFault = generateHTTPErrorResponse(conn, ex, HttpVersion.HTTP_1_1, HttpStatus.SC_BAD_REQUEST,
"Bad request");
} else {
log.error("Unexpected error: " + ex.getMessage(), ex);
SourceContext.updateState(conn, ProtocolState.CLOSED);
Expand All @@ -943,6 +914,45 @@ public void exception(NHttpServerConnection conn, Exception ex) {
}
}

private boolean generateHTTPErrorResponse (NHttpServerConnection conn, Exception ex, HttpVersion version,
int statusCode, String reason) {
log.error("HttpException occurred ", ex);
boolean isFault = false;
if (PassThroughCorrelationConfigDataHolder.isEnable()) {
logHttpRequestErrorInCorrelationLog(conn, "HTTP Exception");
}
try {
if (conn.isResponseSubmitted()) {
sourceConfiguration.getSourceConnections().shutDownConnection(conn, true);
return false;
}
HttpContext httpContext = conn.getContext();

HttpResponse response = new BasicHttpResponse(version, statusCode, reason);
response.setParams(
new DefaultedHttpParams(sourceConfiguration.getHttpParams(), response.getParams()));
response.addHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_CLOSE);

// Pre-process HTTP request
httpContext.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
httpContext.setAttribute(ExecutionContext.HTTP_REQUEST, null);
httpContext.setAttribute(ExecutionContext.HTTP_RESPONSE, response);

sourceConfiguration.getHttpProcessor().process(response, httpContext);

conn.submitResponse(response);
SourceContext.updateState(conn, ProtocolState.CLOSED);
informWriterError(conn);
conn.close();
} catch (Exception ex1) {
log.error(ex1.getMessage(), ex1);
SourceContext.updateState(conn, ProtocolState.CLOSED);
sourceConfiguration.getSourceConnections().shutDownConnection(conn, true);
isFault = true;
}
return isFault;
}

private Map<String, String> getLoggingInfo(NHttpServerConnection conn, ProtocolState state) {
HashMap<String, String> logDetails = new HashMap<>();
SourceContext sourceContext = SourceContext.get(conn);
Expand Down

0 comments on commit f70908a

Please sign in to comment.