HttpRequest

This commit is contained in:
redkale
2024-10-05 19:27:09 +08:00
parent a0a12bc709
commit 162401ee19
3 changed files with 42 additions and 74 deletions

View File

@@ -6,11 +6,7 @@
package org.redkale.net.http;
import java.nio.channels.CompletionHandler;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.SecureRandom;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.redkale.annotation.ConstructorParameters;
import org.redkale.asm.*;
@@ -55,9 +51,6 @@ public class HttpContext extends Context {
// 不带通配符的mapping url的缓存对象
private final UriPathNode uriPathNode = new UriPathNode();
// 不带通配符的mapping url的缓存对象
private final Map<ByteArray, String>[] uriPathCaches = new Map[100];
public HttpContext(HttpContextConfig config) {
super(config);
this.lazyHeader = config.lazyHeader;
@@ -76,38 +69,11 @@ public class HttpContext extends Context {
}
void addUriPath(final String path, HttpServlet servlet) {
this.uriPathNode.put(path, path, servlet);
byte[] bs = path.getBytes(StandardCharsets.UTF_8);
int index = bs.length >= uriPathCaches.length ? 0 : bs.length;
Map<ByteArray, String> map = uriPathCaches[index];
if (map == null) {
map = new HashMap<>();
uriPathCaches[index] = map;
}
map.put(new ByteArray().put(bs), path);
this.uriPathNode.put(path, servlet);
}
String loadUriPath(ByteArray array, boolean latin1, Charset charset) {
int index = array.length() >= uriPathCaches.length ? 0 : array.length();
Map<ByteArray, String> map = uriPathCaches[index];
String uri = map == null ? null : map.get(array);
if (uri == null) {
uri = array.toString(latin1, charset);
}
return uri;
}
String loadUriPath(ByteArray array, int sublen, boolean latin1, Charset charset) {
int pos = array.length();
array.position(sublen);
int index = array.length() >= uriPathCaches.length ? 0 : array.length();
Map<ByteArray, String> map = uriPathCaches[index];
String uri = map == null ? null : map.get(array);
if (uri == null) {
uri = array.toString(latin1, 0, sublen, charset);
}
array.position(pos);
return uri;
HttpServlet removeUriPath(final String path) {
return this.uriPathNode.remove(path);
}
@Override
@@ -258,33 +224,20 @@ public class HttpContext extends Context {
return (Creator<H>) Creator.create(newClazz);
}
protected static class UriPathNode extends ByteTreeNode<String> {
protected HttpServlet servlet;
protected static class UriPathNode extends ByteTreeNode<HttpServlet> {
protected UriPathNode() {
super();
}
protected UriPathNode(ByteTreeNode<String> parent, int index) {
super(parent, index);
}
protected ByteTreeNode<String> put(String key, String value, HttpServlet servlet) {
UriPathNode n = (UriPathNode) super.put(key, value);
n.servlet = servlet;
return n;
@Override
protected ByteTreeNode<HttpServlet> put(String key, HttpServlet servlet) {
return super.put(key, servlet);
}
@Override
protected ByteTreeNode<String> createNode(ByteTreeNode<String> parent, int index, String key, int subLen) {
UriPathNode n = new UriPathNode(parent, index);
n.value = key.substring(0, subLen);
return n;
}
public HttpServlet getServlet() {
return servlet;
protected HttpServlet remove(String key) {
return super.remove(key);
}
}

View File

@@ -20,7 +20,6 @@ import org.redkale.annotation.Comment;
import org.redkale.convert.*;
import org.redkale.convert.json.JsonConvert;
import org.redkale.net.Request;
import org.redkale.net.http.HttpContext.UriPathNode;
import org.redkale.util.*;
import static org.redkale.util.Utility.isEmpty;
import static org.redkale.util.Utility.isNotEmpty;
@@ -715,7 +714,7 @@ public class HttpRequest extends Request<HttpContext> {
boolean decodeable = false;
boolean latin1 = true;
boolean finding = true;
ByteTreeNode<String> pathNode = context.getUriPathNode();
ByteTreeNode<HttpServlet> pathNode = context.getUriPathNode();
while (remain-- > 0) {
b = buffer.get();
if (b == ' ') {
@@ -726,7 +725,7 @@ public class HttpRequest extends Request<HttpContext> {
finding = false;
}
if (finding) {
ByteTreeNode<String> nextNode = pathNode.getNode(b);
ByteTreeNode<HttpServlet> nextNode = pathNode.getNode(b);
if (nextNode == null) { // not match any path
nextNode = pathNode;
ByteArray tmp = headerBytes.clear();
@@ -763,12 +762,12 @@ public class HttpRequest extends Request<HttpContext> {
size = bytes.length();
if (qst >= 0) { // 带?参数
if (pathNode != null) {
this.requestPath = pathNode.getValue();
this.pathServlet = ((UriPathNode) pathNode).getServlet();
this.requestPath = pathNode.getKey();
this.pathServlet = pathNode.getValue();
} else if (decodeable) { // 需要转义
this.requestPath = toDecodeString(bytes, 0, qst, charset);
} else {
this.requestPath = context.loadUriPath(bytes, qst, latin1, charset);
this.requestPath = bytes.toString(latin1, 0, qst, charset);
}
int qlen = size - qst - 1;
this.queryBytes = bytes.getBytes(qst + 1, qlen);
@@ -781,12 +780,12 @@ public class HttpRequest extends Request<HttpContext> {
}
} else { // 没有带?参数
if (pathNode != null) {
this.requestPath = pathNode.getValue();
this.pathServlet = ((UriPathNode) pathNode).getServlet();
this.requestPath = pathNode.getKey();
this.pathServlet = pathNode.getValue();
} else if (decodeable) { // 需要转义
this.requestPath = toDecodeString(bytes, 0, bytes.length(), charset);
} else {
this.requestPath = context.loadUriPath(bytes, latin1, charset);
this.requestPath = bytes.toString(latin1, charset);
}
this.queryBytes = EMPTY_BYTES;
}

View File

@@ -15,28 +15,31 @@ package org.redkale.util;
*/
public class ByteTreeNode<T> {
protected final ByteTreeNode<T> parent;
protected final byte index;
protected final ByteTreeNode<T> parent;
protected final String key;
protected T value;
protected ByteTreeNode<T>[] nodes = new ByteTreeNode[127];
protected ByteTreeNode() {
this(null, 0);
this(null, 0, "");
}
protected ByteTreeNode(ByteTreeNode<T> parent, int index) {
private ByteTreeNode(ByteTreeNode<T> parent, int index, String key) {
this.parent = parent;
if (index < 0 || index >= nodes.length) {
throw new RedkaleException(index + " is illegal");
}
this.index = (byte) index;
this.key = key;
}
public static <T> ByteTreeNode<T> create() {
return new ByteTreeNode(null, 0);
return new ByteTreeNode(null, 0, "");
}
public ByteTreeNode<T> getNode(byte b) {
@@ -55,6 +58,10 @@ public class ByteTreeNode<T> {
return index;
}
public String getKey() {
return key;
}
public T getValue() {
return value;
}
@@ -70,6 +77,19 @@ public class ByteTreeNode<T> {
return n.value;
}
protected T remove(String key) {
ByteTreeNode<T> n = this;
for (char ch : key.toCharArray()) {
n = n.nodes[ch];
if (n == null) {
return null;
}
}
T rs = n.value;
n.value = null;
return rs;
}
protected ByteTreeNode<T> put(String key, T value) {
ByteTreeNode<T> n = this;
int i = 0;
@@ -80,7 +100,7 @@ public class ByteTreeNode<T> {
i++;
ByteTreeNode<T> s = n.nodes[ch];
if (s == null) {
s = createNode(n, ch, key, i);
s = new ByteTreeNode(n, ch, key.substring(0, i));
n.nodes[ch] = s;
}
n = s;
@@ -89,10 +109,6 @@ public class ByteTreeNode<T> {
return n;
}
protected ByteTreeNode<T> createNode(ByteTreeNode<T> parent, int index, String key, int subLen) {
return new ByteTreeNode(parent, index);
}
@Override
public String toString() {
return "ByteTreeNode{" + "index='" + (char) index + "', value=" + value + '}';