Skip to content

Commit

Permalink
Merge pull request #1713 from dushaniw/master-port-fix
Browse files Browse the repository at this point in the history
Fix netty limitation for length URIs exceeding 4096 bytes
  • Loading branch information
dushaniw authored May 8, 2024
2 parents 7e25bcb + 56c8970 commit e0fec83
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,14 @@ public WebSocketClientHandler cacheNewConnection(final String tenantDomain,
handler.setDispatchSequence(responseDispatchSequence);
handler.setDispatchErrorSequence(responseErrorSequence);
}
int maxInitLength = getMaxValueOrDefault(
transportOut.getParameter(WebsocketConstants.WEBSOCKET_MAX_HTTP_CODEC_INIT_LENGTH), 4096);
int maxHeaderSize = getMaxValueOrDefault(
transportOut.getParameter(WebsocketConstants.WEBSOCKET_MAX_HTTP_CODEC_HEADER_SIZE), 8192);
int maxChunkSize = getMaxValueOrDefault(
transportOut.getParameter(WebsocketConstants.WEBSOCKET_MAX_HTTP_CODEC_CHUNK_SIZE), 8192);
int maxContentLength = getMaxValueOrDefault(
transportOut.getParameter(WebsocketConstants.WEBSOCKET_MAX_HTTP_AGGREGATOR_CONTENT_LENGTH), 8192);
Bootstrap b = new Bootstrap();
b.group(group).channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
Expand All @@ -275,7 +283,8 @@ protected void initChannel(SocketChannel ch) {
sslEngine.setSSLParameters(sslParams);
}
p.addLast(sslHandler); }
p.addLast(new HttpClientCodec(), new HttpObjectAggregator(8192),
p.addLast(new HttpClientCodec(maxInitLength, maxHeaderSize, maxChunkSize),
new HttpObjectAggregator(maxContentLength),
new WebSocketFrameAggregator(Integer.MAX_VALUE), handler);
}
});
Expand Down Expand Up @@ -381,5 +390,11 @@ public void removeChannelHandler(String sourceIdentifier,
}
}

private int getMaxValueOrDefault(Parameter parameter, int defaultValue) {

if (parameter != null) {
return Integer.parseInt(parameter.getParameterElement().getText());
}
return defaultValue;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ public class WebsocketConstants {
public static final String TRUST_STORE_PASSWORD = "ws.trust.store.Password";
public static final String TRUST_STORE_CONFIG_ELEMENT = "ws.trust.store";
public static final String WEBSOCKET_MAX_FRAME_PAYLOAD_LENGTH = "wsMaxFrameLength";
public static final String WEBSOCKET_MAX_HTTP_CODEC_INIT_LENGTH = "wsMaxHttpCodecInitLength";
public static final String WEBSOCKET_MAX_HTTP_CODEC_HEADER_SIZE = "wsMaxHttpCodecHeaderSize";
public static final String WEBSOCKET_MAX_HTTP_CODEC_CHUNK_SIZE = "wsMaxHttpCodecChunkSize";
public static final String WEBSOCKET_MAX_HTTP_AGGREGATOR_CONTENT_LENGTH = "wsMaxHttpAggregateContentLength";

public static final String SYNAPSE_SUBPROTOCOL_PREFIX = "synapse";
public static final String WEBSOCKET_SUBSCRIBER_PATH = "websocket.subscriber.path";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,12 @@ public class Constants {
public static final String INTERNAL_APIS_FILE = "internal-apis.xml";
public static final String PREFIX_TO_ENABLE_INTERNAL_APIS = "enable";
public static final String WEBSOCKET_TRANSPORT_MAX_FRAME_PAYLOAD_LENGTH = "ws.transport.max.frame.payload.length";

public static final String WEBSOCKET_TRANSPORT_MAX_HTTP_CODEC_INIT_LENGTH
= "ws.transport.max.http.codec.init.length";
public static final String WEBSOCKET_TRANSPORT_MAX_HTTP_CODEC_HEADER_SIZE
= "ws.transport.max.http.codec.header.size";
public static final String WEBSOCKET_TRANSPORT_MAX_HTTP_CODEC_CHUNK_SIZE
= "ws.transport.max.http.codec.chunk.size";
public static final String WEBSOCKET_TRANSPORT_MAX_HTTP_AGGREGATOR_CONTENT_LENGTH
= "ws.transport.max.http.aggregator.content.length";
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@
import io.netty.handler.codec.http.websocketx.WebSocketFrameAggregator;
import io.netty.handler.ssl.SslHandler;
import io.netty.handler.timeout.IdleStateHandler;
import org.apache.synapse.config.SynapsePropertiesLoader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wso2.carbon.inbound.endpoint.internal.http.api.Constants;
import org.wso2.carbon.inbound.endpoint.protocol.websocket.ssl.InboundWebsocketSSLConfiguration;
import org.wso2.carbon.inbound.endpoint.protocol.websocket.ssl.SSLHandlerFactory;

Expand Down Expand Up @@ -99,8 +101,16 @@ protected void initChannel(SocketChannel websocketChannel) throws Exception {
}

ChannelPipeline p = websocketChannel.pipeline();
p.addLast("codec", new HttpServerCodec());
p.addLast("aggregator", new HttpObjectAggregator(65536));
int maxInitLength = Integer.parseInt(SynapsePropertiesLoader.getPropertyValue(
Constants.WEBSOCKET_TRANSPORT_MAX_HTTP_CODEC_INIT_LENGTH, "4096"));
int maxHeaderSize = Integer.parseInt(SynapsePropertiesLoader.getPropertyValue(
Constants.WEBSOCKET_TRANSPORT_MAX_HTTP_CODEC_HEADER_SIZE, "8192"));
int maxChunkSize = Integer.parseInt(SynapsePropertiesLoader.getPropertyValue(
Constants.WEBSOCKET_TRANSPORT_MAX_HTTP_CODEC_CHUNK_SIZE, "8192"));
int maxContentLength = Integer.parseInt(SynapsePropertiesLoader.getPropertyValue(
Constants.WEBSOCKET_TRANSPORT_MAX_HTTP_AGGREGATOR_CONTENT_LENGTH, "65536"));
p.addLast("codec", new HttpServerCodec(maxInitLength, maxHeaderSize, maxChunkSize));
p.addLast("aggregator", new HttpObjectAggregator(maxContentLength));
p.addLast("frameAggregator", new WebSocketFrameAggregator(Integer.MAX_VALUE));
p.addLast("idleStateHandler", new IdleStateHandler(inflowIdleTime, outflowIdleTime, 0));
InboundWebsocketSourceHandler sourceHandler = new InboundWebsocketSourceHandler();
Expand Down

0 comments on commit e0fec83

Please sign in to comment.