diff --git a/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/passthru/util/PassThroughTransportUtils.java b/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/passthru/util/PassThroughTransportUtils.java index c711ae59a..1671cbd63 100644 --- a/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/passthru/util/PassThroughTransportUtils.java +++ b/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/passthru/util/PassThroughTransportUtils.java @@ -44,6 +44,7 @@ import org.apache.synapse.transport.utils.conn.SynapseNHttpClientConnection; import java.net.InetAddress; +import java.util.ArrayList; import java.util.Map; import java.util.Iterator; @@ -117,7 +118,8 @@ public static EndpointReference getDestinationEPR(MessageContext msgContext) { */ public static void removeUnwantedHeaders(MessageContext msgContext, TargetConfiguration targetConfiguration) { Map headers = (Map) msgContext.getProperty(MessageContext.TRANSPORT_HEADERS); - Map excessHeaders = (Map) msgContext.getProperty(NhttpConstants.EXCESS_TRANSPORT_HEADERS); + + removeUnwantedExcessHeaders(msgContext); if (headers == null || headers.isEmpty()) { return; @@ -159,6 +161,39 @@ public static void removeUnwantedHeaders(MessageContext msgContext, TargetConfig } + /** + * Headers that frame the message body on the connection, or that control the connection + * itself. These are dictated by the transport and must never be carried over from an inbound + * message onto the outbound one. + * + * @param headerName the http header name to test + * @return true if the header frames the message or controls the connection + */ + public static boolean isConnectionFramingHeader(String headerName) { + return HTTP.TRANSFER_ENCODING.equalsIgnoreCase(headerName) + || HTTP.CONTENT_LEN.equalsIgnoreCase(headerName) + || HTTP.CONN_DIRECTIVE.equalsIgnoreCase(headerName); + } + + /** + * Remove the connection framing headers from EXCESS_TRANSPORT_HEADERS. + * + * @param msgContext the Axis2 Message context holding the excess header map + */ + private static void removeUnwantedExcessHeaders(MessageContext msgContext) { + Map excessHeaders = (Map) msgContext.getProperty(NhttpConstants.EXCESS_TRANSPORT_HEADERS); + + if (excessHeaders == null || excessHeaders.isEmpty()) { + return; + } + + for (Object headerName : new ArrayList(excessHeaders.keySet())) { + if (headerName instanceof String && isConnectionFramingHeader((String) headerName)) { + excessHeaders.remove(headerName); + } + } + } + /** * Determine the Http Status Code depending on the message type processed
* (normal response versus fault response) as well as Axis2 message context properties set diff --git a/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/passthru/util/SourceResponseFactory.java b/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/passthru/util/SourceResponseFactory.java index 0738a496c..68c7d3ab1 100644 --- a/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/passthru/util/SourceResponseFactory.java +++ b/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/passthru/util/SourceResponseFactory.java @@ -79,6 +79,10 @@ public static SourceResponse create(MessageContext msgContext, if (excessHeaders != null) { for (Iterator iterator = excessHeaders.keySet().iterator(); iterator.hasNext();) { String key = (String) iterator.next(); + if (PassThroughTransportUtils.isConnectionFramingHeader(key)) { + // A duplicate framing header must never be relayed onto the outbound message + continue; + } for (String excessVal : (Collection) excessHeaders.get(key)) { sourceResponse.addHeader(key, (String) excessVal); } diff --git a/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/passthru/util/TargetRequestFactory.java b/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/passthru/util/TargetRequestFactory.java index 4b8802020..a8c075f3d 100644 --- a/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/passthru/util/TargetRequestFactory.java +++ b/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/passthru/util/TargetRequestFactory.java @@ -133,6 +133,10 @@ public static TargetRequest create(MessageContext msgContext, if (excessHeaders != null) { for (Iterator iterator = excessHeaders.keySet().iterator(); iterator.hasNext();) { String key = (String) iterator.next(); + if (PassThroughTransportUtils.isConnectionFramingHeader(key)) { + // A duplicate framing header must never be relayed onto the outbound message + continue; + } for (String excessVal : (Collection) excessHeaders.get(key)) { request.addHeader(key, (String) excessVal); }