From 9da964657db67c39525f2dc24cd6b0f2b246f5ea Mon Sep 17 00:00:00 2001 From: redkale Date: Sat, 7 Sep 2024 23:33:10 +0800 Subject: [PATCH] Context.maxHeader --- .../java/META-INF/application-template.xml | 5 ++- src/main/java/org/redkale/net/Context.java | 13 ++++++ src/main/java/org/redkale/net/Server.java | 45 ++++++++++++------- .../org/redkale/net/http/HttpRequest.java | 3 ++ .../org/redkale/net/sncp/SncpRequest.java | 8 ++++ 5 files changed, 57 insertions(+), 17 deletions(-) diff --git a/src/main/java/META-INF/application-template.xml b/src/main/java/META-INF/application-template.xml index 5e4eb47e8..fed3f6919 100644 --- a/src/main/java/META-INF/application-template.xml +++ b/src/main/java/META-INF/application-template.xml @@ -155,8 +155,9 @@ serviceid1_name1 serviceid1_name2 serviceid2_name1 serviceid2_name2 lib: server额外的class目录, 默认为${APP_HOME}/libs/*; charset: 文本编码, 默认: UTF-8 backlog: 默认10K - maxconns: 最大连接数, 小于1表示无限制, 默认: 0 - maxbody: request.body最大值, 默认: 256K + maxConns: 最大连接数, 小于1表示无限制, 默认: 0 + maxHeader: request.header最大值, 默认: 16K + maxBody: request.body最大值, 默认: 256K bufferCapacity: ByteBuffer的初始化大小, TCP默认: 32K; (HTTP 2.0、WebSocket,必须要16k以上); UDP默认: 8K bufferPoolSize: ByteBuffer池的大小,默认: 线程数*4 responsePoolSize: Response池的大小,默认: 1024 diff --git a/src/main/java/org/redkale/net/Context.java b/src/main/java/org/redkale/net/Context.java index 3084895a4..aec148749 100644 --- a/src/main/java/org/redkale/net/Context.java +++ b/src/main/java/org/redkale/net/Context.java @@ -60,6 +60,9 @@ public class Context { // 最大连接数, 为0表示没限制 protected int maxConns; + // 请求头的大小上限, 默认16K + protected int maxHeader; + // 请求内容的大小上限, 默认64K protected int maxBody; @@ -88,6 +91,7 @@ public class Context { config.sslContext, config.bufferCapacity, config.maxConns, + config.maxHeader, config.maxBody, config.charset, config.serverAddress, @@ -107,6 +111,7 @@ public class Context { SSLContext sslContext, int bufferCapacity, final int maxConns, + final int maxHeader, final int maxBody, Charset charset, InetSocketAddress address, @@ -123,6 +128,7 @@ public class Context { this.sslContext = sslContext; this.bufferCapacity = bufferCapacity; this.maxConns = maxConns; + this.maxHeader = maxHeader; this.maxBody = maxBody; this.charset = StandardCharsets.UTF_8.equals(charset) ? null : charset; this.serverAddress = address; @@ -193,6 +199,10 @@ public class Context { return maxConns; } + public int getMaxHeader() { + return maxHeader; + } + public int getMaxBody() { return maxBody; } @@ -270,6 +280,9 @@ public class Context { // 字符集 public Charset charset; + // 请求头的大小上限, 默认16K + public int maxHeader; + // 请求内容的大小上限, 默认64K public int maxBody; diff --git a/src/main/java/org/redkale/net/Server.java b/src/main/java/org/redkale/net/Server.java index 4a3f38683..925588c8d 100644 --- a/src/main/java/org/redkale/net/Server.java +++ b/src/main/java/org/redkale/net/Server.java @@ -101,6 +101,9 @@ public abstract class Server< // 最大连接数, 为0表示没限制 protected int maxConns; + // 请求头大小的上限,单位:字节 + protected int maxHeader; + // 请求包大小的上限,单位:字节 protected int maxBody; @@ -131,14 +134,15 @@ public abstract class Server< Objects.requireNonNull(config); this.config = config; this.address = new InetSocketAddress(config.getValue("host", "0.0.0.0"), config.getIntValue("port", 80)); - this.charset = Charset.forName(config.getValue("charset", "UTF-8")); - this.maxConns = config.getIntValue("maxconns", 0); this.aliveTimeoutSeconds = config.getIntValue("aliveTimeoutSeconds", 30); this.readTimeoutSeconds = config.getIntValue("readTimeoutSeconds", 0); this.writeTimeoutSeconds = config.getIntValue("writeTimeoutSeconds", 0); this.backlog = parseLenth(config.getValue("backlog"), 1024); + this.charset = Charset.forName(config.getValue("charset", "UTF-8")); + this.maxConns = config.getIntValue("maxConns", 0); + this.maxHeader = parseLenth(config.getValue("maxHeader"), 16 * 1024); this.maxBody = - parseLenth(config.getValue("maxbody"), "UDP".equalsIgnoreCase(netprotocol) ? 16 * 1024 : 256 * 1024); + parseLenth(config.getValue("maxBody"), "UDP".equalsIgnoreCase(netprotocol) ? 16 * 1024 : 256 * 1024); int bufCapacity = parseLenth( config.getValue("bufferCapacity"), "UDP".equalsIgnoreCase(netprotocol) ? UDP_BUFFER_CAPACITY : 32 * 1024); @@ -298,10 +302,6 @@ public abstract class Server< return responsePoolSize; } - public int getMaxBody() { - return maxBody; - } - public int getAliveTimeoutSeconds() { return aliveTimeoutSeconds; } @@ -318,6 +318,14 @@ public abstract class Server< return maxConns; } + public int getMaxHeader() { + return maxHeader; + } + + public int getMaxBody() { + return maxBody; + } + @SuppressWarnings("unchecked") public void addServlet(S servlet, final Object attachment, AnyValue conf, K... mappings) { this.dispatcher.addServlet(servlet, attachment, conf, mappings); @@ -402,6 +410,13 @@ public abstract class Server< } } + public void changeCharset(final Charset newCharset) { + this.charset = newCharset; + if (this.context != null) { + this.context.charset = newCharset; + } + } + public void changeMaxconns(final int newMaxConns) { this.maxConns = newMaxConns; if (this.context != null) { @@ -412,17 +427,17 @@ public abstract class Server< } } - public void changeCharset(final Charset newcharset) { - this.charset = newcharset; + public void changeMaxHeader(final int newMaxHeader) { + this.maxHeader = newMaxHeader; if (this.context != null) { - this.context.charset = newcharset; + this.context.maxHeader = newMaxHeader; } } - public void changeMaxbody(final int newmaxbody) { - this.maxBody = newmaxbody; + public void changeMaxBody(final int newMaxBody) { + this.maxBody = newMaxBody; if (this.context != null) { - this.context.maxBody = newmaxbody; + this.context.maxBody = newMaxBody; } } @@ -460,6 +475,7 @@ public abstract class Server< contextConfig.sslContext = this.sslContext; contextConfig.bufferCapacity = this.bufferCapacity; contextConfig.maxConns = this.maxConns; + contextConfig.maxHeader = this.maxHeader; contextConfig.maxBody = this.maxBody; contextConfig.charset = this.charset; contextConfig.serverAddress = this.address; @@ -522,11 +538,10 @@ public abstract class Server< /** * 判断是否存在Filter * - * @param 泛型 * @param filterClassName Filter类 * @return boolean */ - public boolean containsFilter(String filterClassName) { + public boolean containsFilter(String filterClassName) { return this.dispatcher.containsFilter(filterClassName); } diff --git a/src/main/java/org/redkale/net/http/HttpRequest.java b/src/main/java/org/redkale/net/http/HttpRequest.java index 89a0d92cc..753f76e58 100644 --- a/src/main/java/org/redkale/net/http/HttpRequest.java +++ b/src/main/java/org/redkale/net/http/HttpRequest.java @@ -392,6 +392,9 @@ public class HttpRequest extends Request { this.headerHalfLen = this.headerLength; } bytes.clear(); + if (this.headerLength > context.getMaxHeader()) { + return -1; + } if (this.contentType != null && this.contentType.contains("boundary=")) { this.boundary = true; } diff --git a/src/main/java/org/redkale/net/sncp/SncpRequest.java b/src/main/java/org/redkale/net/sncp/SncpRequest.java index f0968f853..29d804b88 100644 --- a/src/main/java/org/redkale/net/sncp/SncpRequest.java +++ b/src/main/java/org/redkale/net/sncp/SncpRequest.java @@ -81,6 +81,14 @@ public class SncpRequest extends Request { + this.headerSize); return -1; } + if (this.headerSize > context.getMaxHeader()) { + context.getLogger() + .log( + Level.WARNING, + "sncp buffer header.length must lower " + context.getMaxHeader() + ", but " + + this.headerSize); + return -1; + } this.readState = READ_STATE_HEADER; } // ---------------------head----------------------------------