diff --git a/src/main/java/org/redkale/net/AsyncIOGroup.java b/src/main/java/org/redkale/net/AsyncIOGroup.java index 60503cfd1..d23324f7c 100644 --- a/src/main/java/org/redkale/net/AsyncIOGroup.java +++ b/src/main/java/org/redkale/net/AsyncIOGroup.java @@ -27,7 +27,7 @@ import org.redkale.util.*; @ResourceType(AsyncGroup.class) public class AsyncIOGroup extends AsyncGroup { - private boolean started; + private final AtomicBoolean started = new AtomicBoolean(); private boolean skipClose; @@ -108,20 +108,18 @@ public class AsyncIOGroup extends AsyncGroup { @Override public AsyncGroup start() { - if (started) { - return this; - } if (closed.get()) { throw new RedkaleException("group is closed"); } - for (int i = 0; i < this.ioReadThreads.length; i++) { - this.ioReadThreads[i].start(); - if (this.ioWriteThreads[i] != this.ioReadThreads[i]) { - this.ioWriteThreads[i].start(); + if (started.compareAndSet(false, true)) { + for (int i = 0; i < this.ioReadThreads.length; i++) { + this.ioReadThreads[i].start(); + if (this.ioWriteThreads[i] != this.ioReadThreads[i]) { + this.ioWriteThreads[i].start(); + } } + //connectThread用时才初始化 } - //connectThread用时才初始化 - started = true; return this; } diff --git a/src/main/java/org/redkale/net/http/HttpHeaders.java b/src/main/java/org/redkale/net/http/HttpHeaders.java index 8c8908c81..382d46aee 100644 --- a/src/main/java/org/redkale/net/http/HttpHeaders.java +++ b/src/main/java/org/redkale/net/http/HttpHeaders.java @@ -12,6 +12,7 @@ import java.util.List; import java.util.Map; import java.util.Set; import java.util.function.BiConsumer; +import java.util.function.Predicate; import org.redkale.convert.TextConvert; import org.redkale.convert.json.JsonConvert; import org.redkale.util.RedkaleException; @@ -103,14 +104,21 @@ public class HttpHeaders implements RestHeaders, Serializable { @Override public void forEach(BiConsumer consumer) { + forEach((Predicate) null, consumer); + } + + @Override + public void forEach(Predicate filter, BiConsumer consumer) { if (map != null) { map.forEach((k, v) -> { - if (v instanceof Collection) { - for (Object item : (Collection) v) { - consumer.accept(k, item == null ? null : item.toString()); + if (filter == null || filter.test(k)) { + if (v instanceof Collection) { + for (Object item : (Collection) v) { + consumer.accept(k, item == null ? null : item.toString()); + } + } else { + consumer.accept(k, v == null ? null : v.toString()); } - } else { - consumer.accept(k, v == null ? null : v.toString()); } }); } diff --git a/src/main/java/org/redkale/net/http/HttpSimpleClient.java b/src/main/java/org/redkale/net/http/HttpSimpleClient.java index d79dfd42f..800381cb4 100644 --- a/src/main/java/org/redkale/net/http/HttpSimpleClient.java +++ b/src/main/java/org/redkale/net/http/HttpSimpleClient.java @@ -246,18 +246,17 @@ public class HttpSimpleClient extends Client 0 ? url.substring(urlpos) : "/") + " HTTP/1.1\r\n").getBytes(StandardCharsets.UTF_8)); array.put(("Host: " + uri.getHost() + "\r\n").getBytes(StandardCharsets.UTF_8)); - array.put(("Content-Length: " + (body == null ? 0 : body.length) + "\r\n").getBytes(StandardCharsets.UTF_8)); + array.put(HttpSimpleRequest.contentLengthBytes(body)); if (headers == null || !headers.containsIgnoreCase("User-Agent")) { array.put(header_bytes_useragent); } - if (headers == null || !headers.containsIgnoreCase("Connection")) { - array.put(header_bytes_connclose); - } + array.put(header_bytes_connclose); if (headers == null || !headers.containsIgnoreCase(Rest.REST_HEADER_TRACEID)) { array.put((Rest.REST_HEADER_TRACEID + ": " + traceid + "\r\n").getBytes(StandardCharsets.UTF_8)); } if (headers != null) { - headers.forEach((k, v) -> array.put((k + ": " + v + "\r\n").getBytes(StandardCharsets.UTF_8))); + headers.forEach(k -> !k.equalsIgnoreCase("Connection") && !k.equalsIgnoreCase("Content-Length"), + (k, v) -> array.put((k + ": " + v + "\r\n").getBytes(StandardCharsets.UTF_8))); } array.put((byte) '\r', (byte) '\n'); if (body != null) { @@ -336,7 +335,7 @@ public class HttpSimpleClient extends Client handler) { this.channel.readInIOThread(handler); } - + public void write(ByteTuple array, CompletionHandler handler) { this.channel.write(array, handler); } diff --git a/src/main/java/org/redkale/net/http/HttpSimpleRequest.java b/src/main/java/org/redkale/net/http/HttpSimpleRequest.java index 8ad98873e..ebf36fb6b 100644 --- a/src/main/java/org/redkale/net/http/HttpSimpleRequest.java +++ b/src/main/java/org/redkale/net/http/HttpSimpleRequest.java @@ -147,7 +147,8 @@ public class HttpSimpleRequest extends ClientRequest implements java.io.Serializ array.put(("Content-Type: " + contentType0 + "\r\n").getBytes(StandardCharsets.UTF_8)); array.put(contentLengthBytes(clientBody)); if (headers != null) { - headers.forEach((k, v) -> array.put((k + ": " + v + "\r\n").getBytes(StandardCharsets.UTF_8))); + headers.forEach(k -> !k.equalsIgnoreCase("Content-Type") && !k.equalsIgnoreCase("Content-Length"), + (k, v) -> array.put((k + ": " + v + "\r\n").getBytes(StandardCharsets.UTF_8))); } array.put((byte) '\r', (byte) '\n'); //写body @@ -160,7 +161,7 @@ public class HttpSimpleRequest extends ClientRequest implements java.io.Serializ return headers != null && headers.containsIgnoreCase(name); } - protected byte[] contentLengthBytes(byte[] clientBody) { + protected static byte[] contentLengthBytes(byte[] clientBody) { int len = clientBody == null ? 0 : clientBody.length; if (len < contentLengthArray.length) { return contentLengthArray[len]; diff --git a/src/main/java/org/redkale/net/http/RestHeaders.java b/src/main/java/org/redkale/net/http/RestHeaders.java index 1023c42a6..ddb555353 100644 --- a/src/main/java/org/redkale/net/http/RestHeaders.java +++ b/src/main/java/org/redkale/net/http/RestHeaders.java @@ -9,6 +9,7 @@ import java.io.Serializable; import java.util.List; import java.util.Map; import java.util.function.BiConsumer; +import java.util.function.Predicate; /** * 用于RestService类的方法的参数获取HttpHeaders @@ -30,6 +31,8 @@ public interface RestHeaders { public void forEach(BiConsumer consumer); + public void forEach(Predicate filter, BiConsumer consumer); + public String[] names(); public boolean contains(String name);