Context.maxHeader

This commit is contained in:
redkale
2024-09-07 23:33:10 +08:00
parent 50c10f31fa
commit 9da964657d
5 changed files with 57 additions and 17 deletions

View File

@@ -155,8 +155,9 @@ serviceid1_name1 serviceid1_name2 serviceid2_name1 serviceid2_name2
lib: server额外的class目录 默认为${APP_HOME}/libs/*; lib: server额外的class目录 默认为${APP_HOME}/libs/*;
charset: 文本编码, 默认: UTF-8 charset: 文本编码, 默认: UTF-8
backlog: 默认10K backlog: 默认10K
maxconns 最大连接数, 小于1表示无限制 默认: 0 maxConns 最大连接数, 小于1表示无限制 默认: 0
maxbody: request.body最大值, 默认: 256K maxHeader: request.header最大值, 默认: 16K
maxBody: request.body最大值 默认: 256K
bufferCapacity: ByteBuffer的初始化大小 TCP默认: 32K; (HTTP 2.0、WebSocket必须要16k以上); UDP默认: 8K bufferCapacity: ByteBuffer的初始化大小 TCP默认: 32K; (HTTP 2.0、WebSocket必须要16k以上); UDP默认: 8K
bufferPoolSize ByteBuffer池的大小默认: 线程数*4 bufferPoolSize ByteBuffer池的大小默认: 线程数*4
responsePoolSize Response池的大小默认: 1024 responsePoolSize Response池的大小默认: 1024

View File

@@ -60,6 +60,9 @@ public class Context {
// 最大连接数, 为0表示没限制 // 最大连接数, 为0表示没限制
protected int maxConns; protected int maxConns;
// 请求头的大小上限, 默认16K
protected int maxHeader;
// 请求内容的大小上限, 默认64K // 请求内容的大小上限, 默认64K
protected int maxBody; protected int maxBody;
@@ -88,6 +91,7 @@ public class Context {
config.sslContext, config.sslContext,
config.bufferCapacity, config.bufferCapacity,
config.maxConns, config.maxConns,
config.maxHeader,
config.maxBody, config.maxBody,
config.charset, config.charset,
config.serverAddress, config.serverAddress,
@@ -107,6 +111,7 @@ public class Context {
SSLContext sslContext, SSLContext sslContext,
int bufferCapacity, int bufferCapacity,
final int maxConns, final int maxConns,
final int maxHeader,
final int maxBody, final int maxBody,
Charset charset, Charset charset,
InetSocketAddress address, InetSocketAddress address,
@@ -123,6 +128,7 @@ public class Context {
this.sslContext = sslContext; this.sslContext = sslContext;
this.bufferCapacity = bufferCapacity; this.bufferCapacity = bufferCapacity;
this.maxConns = maxConns; this.maxConns = maxConns;
this.maxHeader = maxHeader;
this.maxBody = maxBody; this.maxBody = maxBody;
this.charset = StandardCharsets.UTF_8.equals(charset) ? null : charset; this.charset = StandardCharsets.UTF_8.equals(charset) ? null : charset;
this.serverAddress = address; this.serverAddress = address;
@@ -193,6 +199,10 @@ public class Context {
return maxConns; return maxConns;
} }
public int getMaxHeader() {
return maxHeader;
}
public int getMaxBody() { public int getMaxBody() {
return maxBody; return maxBody;
} }
@@ -270,6 +280,9 @@ public class Context {
// 字符集 // 字符集
public Charset charset; public Charset charset;
// 请求头的大小上限, 默认16K
public int maxHeader;
// 请求内容的大小上限, 默认64K // 请求内容的大小上限, 默认64K
public int maxBody; public int maxBody;

View File

@@ -101,6 +101,9 @@ public abstract class Server<
// 最大连接数, 为0表示没限制 // 最大连接数, 为0表示没限制
protected int maxConns; protected int maxConns;
// 请求头大小的上限,单位:字节
protected int maxHeader;
// 请求包大小的上限,单位:字节 // 请求包大小的上限,单位:字节
protected int maxBody; protected int maxBody;
@@ -131,14 +134,15 @@ public abstract class Server<
Objects.requireNonNull(config); Objects.requireNonNull(config);
this.config = config; this.config = config;
this.address = new InetSocketAddress(config.getValue("host", "0.0.0.0"), config.getIntValue("port", 80)); 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.aliveTimeoutSeconds = config.getIntValue("aliveTimeoutSeconds", 30);
this.readTimeoutSeconds = config.getIntValue("readTimeoutSeconds", 0); this.readTimeoutSeconds = config.getIntValue("readTimeoutSeconds", 0);
this.writeTimeoutSeconds = config.getIntValue("writeTimeoutSeconds", 0); this.writeTimeoutSeconds = config.getIntValue("writeTimeoutSeconds", 0);
this.backlog = parseLenth(config.getValue("backlog"), 1024); 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 = 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( int bufCapacity = parseLenth(
config.getValue("bufferCapacity"), config.getValue("bufferCapacity"),
"UDP".equalsIgnoreCase(netprotocol) ? UDP_BUFFER_CAPACITY : 32 * 1024); "UDP".equalsIgnoreCase(netprotocol) ? UDP_BUFFER_CAPACITY : 32 * 1024);
@@ -298,10 +302,6 @@ public abstract class Server<
return responsePoolSize; return responsePoolSize;
} }
public int getMaxBody() {
return maxBody;
}
public int getAliveTimeoutSeconds() { public int getAliveTimeoutSeconds() {
return aliveTimeoutSeconds; return aliveTimeoutSeconds;
} }
@@ -318,6 +318,14 @@ public abstract class Server<
return maxConns; return maxConns;
} }
public int getMaxHeader() {
return maxHeader;
}
public int getMaxBody() {
return maxBody;
}
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void addServlet(S servlet, final Object attachment, AnyValue conf, K... mappings) { public void addServlet(S servlet, final Object attachment, AnyValue conf, K... mappings) {
this.dispatcher.addServlet(servlet, attachment, conf, 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) { public void changeMaxconns(final int newMaxConns) {
this.maxConns = newMaxConns; this.maxConns = newMaxConns;
if (this.context != null) { if (this.context != null) {
@@ -412,17 +427,17 @@ public abstract class Server<
} }
} }
public void changeCharset(final Charset newcharset) { public void changeMaxHeader(final int newMaxHeader) {
this.charset = newcharset; this.maxHeader = newMaxHeader;
if (this.context != null) { if (this.context != null) {
this.context.charset = newcharset; this.context.maxHeader = newMaxHeader;
} }
} }
public void changeMaxbody(final int newmaxbody) { public void changeMaxBody(final int newMaxBody) {
this.maxBody = newmaxbody; this.maxBody = newMaxBody;
if (this.context != null) { 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.sslContext = this.sslContext;
contextConfig.bufferCapacity = this.bufferCapacity; contextConfig.bufferCapacity = this.bufferCapacity;
contextConfig.maxConns = this.maxConns; contextConfig.maxConns = this.maxConns;
contextConfig.maxHeader = this.maxHeader;
contextConfig.maxBody = this.maxBody; contextConfig.maxBody = this.maxBody;
contextConfig.charset = this.charset; contextConfig.charset = this.charset;
contextConfig.serverAddress = this.address; contextConfig.serverAddress = this.address;
@@ -522,11 +538,10 @@ public abstract class Server<
/** /**
* 判断是否存在Filter * 判断是否存在Filter
* *
* @param <T> 泛型
* @param filterClassName Filter类 * @param filterClassName Filter类
* @return boolean * @return boolean
*/ */
public <T extends Filter> boolean containsFilter(String filterClassName) { public boolean containsFilter(String filterClassName) {
return this.dispatcher.containsFilter(filterClassName); return this.dispatcher.containsFilter(filterClassName);
} }

View File

@@ -392,6 +392,9 @@ public class HttpRequest extends Request<HttpContext> {
this.headerHalfLen = this.headerLength; this.headerHalfLen = this.headerLength;
} }
bytes.clear(); bytes.clear();
if (this.headerLength > context.getMaxHeader()) {
return -1;
}
if (this.contentType != null && this.contentType.contains("boundary=")) { if (this.contentType != null && this.contentType.contains("boundary=")) {
this.boundary = true; this.boundary = true;
} }

View File

@@ -81,6 +81,14 @@ public class SncpRequest extends Request<SncpContext> {
+ this.headerSize); + this.headerSize);
return -1; 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; this.readState = READ_STATE_HEADER;
} }
// ---------------------head---------------------------------- // ---------------------head----------------------------------