From 47fd4e3ab8790aa8690e191c03888856dbdbf806 Mon Sep 17 00:00:00 2001 From: fzdwx Date: Sat, 4 Jun 2022 16:38:25 +0800 Subject: [PATCH 1/4] fix README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index a74ec57..c61c1b3 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,7 @@ import http.HttpServerRequest; @SpringBootApplication @RestController +@UseSkyWebServer public class BurstServerApplication { public static void main(String[] args) { From 85b7fdaee78f65a7b6e0e4033fce0b40344c637f Mon Sep 17 00:00:00 2001 From: weiZhen Date: Wed, 15 Jun 2022 14:23:10 +0800 Subject: [PATCH 2/4] fix #32 --- README.md | 2 +- pom.xml | 4 ++-- samples/pom.xml | 2 +- sky-http-springboot-starter/pom.xml | 2 +- .../src/main/java/sky/starter/util/SkyBanner.java | 2 +- sky-infrastructure/README.md | 2 +- sky-infrastructure/pom.xml | 2 +- sky-infrastructure/src/main/java/core/Server.java | 12 +++++++++++- .../src/main/java/core/http/HttpServer.java | 4 +++- 9 files changed, 22 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 7187c77..07d5dde 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ io.github.fzdwx sky-http-springboot-starter - 0.11.3 + 0.11.3.1 ``` diff --git a/pom.xml b/pom.xml index b0665e6..427aa9f 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ io.github.fzdwx sky pom - 0.11.3 + 0.11.3.1 sky-infrastructure sky-http-springboot-starter @@ -19,7 +19,7 @@ UTF-8 0.10.7 - 0.11.3 + 0.11.3.1 4.1.77.Final diff --git a/samples/pom.xml b/samples/pom.xml index d60282e..d14ef7c 100644 --- a/samples/pom.xml +++ b/samples/pom.xml @@ -17,7 +17,7 @@ 1.8 1.8 - 0.11.3 + 0.11.3.1 2020.0.4 2021.1 2.5.7 diff --git a/sky-http-springboot-starter/pom.xml b/sky-http-springboot-starter/pom.xml index d417598..16560db 100644 --- a/sky-http-springboot-starter/pom.xml +++ b/sky-http-springboot-starter/pom.xml @@ -5,7 +5,7 @@ sky io.github.fzdwx - 0.11.3 + 0.11.3.1 4.0.0 diff --git a/sky-http-springboot-starter/src/main/java/sky/starter/util/SkyBanner.java b/sky-http-springboot-starter/src/main/java/sky/starter/util/SkyBanner.java index 8d5236d..78200c2 100644 --- a/sky-http-springboot-starter/src/main/java/sky/starter/util/SkyBanner.java +++ b/sky-http-springboot-starter/src/main/java/sky/starter/util/SkyBanner.java @@ -10,7 +10,7 @@ @Slf4j public class SkyBanner { - private final static String version = "0.11.3"; + private final static String version = "0.11.3.1"; public static void print() { log.info(Console.banner() + Console.cyan(" [ Sky Web Server ] ") + Console.ANSI_RESET + " (v{})", version); diff --git a/sky-infrastructure/README.md b/sky-infrastructure/README.md index 116e593..bc53ffa 100644 --- a/sky-infrastructure/README.md +++ b/sky-infrastructure/README.md @@ -8,7 +8,7 @@ It encapsulates a layer on netty, which is easier to use io.github.fzdwx sky-infrastructure - 0.11.3 + 0.11.3.1 ``` diff --git a/sky-infrastructure/pom.xml b/sky-infrastructure/pom.xml index de5ce86..c07441d 100644 --- a/sky-infrastructure/pom.xml +++ b/sky-infrastructure/pom.xml @@ -5,7 +5,7 @@ sky io.github.fzdwx - 0.11.3 + 0.11.3.1 4.0.0 diff --git a/sky-infrastructure/src/main/java/core/Server.java b/sky-infrastructure/src/main/java/core/Server.java index 6aaa6d1..409d149 100644 --- a/sky-infrastructure/src/main/java/core/Server.java +++ b/sky-infrastructure/src/main/java/core/Server.java @@ -72,8 +72,18 @@ public class Server implements core.Transport { protected ServerBootstrap serverBootstrap; public Server() { + this(Epoll.isAvailable()); + } + + /** + * new server + * + * @param useEpoll 使用epoll + * @apiNote Epoll.isAvailable(); + */ + public Server(boolean useEpoll) { this.serverBootstrap = new ServerBootstrap(); - this.enableEpoll = Epoll.isAvailable(); + this.enableEpoll = useEpoll; if (enableEpoll) { this.channelType = EpollServerSocketChannel.class; diff --git a/sky-infrastructure/src/main/java/core/http/HttpServer.java b/sky-infrastructure/src/main/java/core/http/HttpServer.java index 09a4a45..eee4cca 100644 --- a/sky-infrastructure/src/main/java/core/http/HttpServer.java +++ b/sky-infrastructure/src/main/java/core/http/HttpServer.java @@ -91,7 +91,9 @@ public Disposer listen() { * start server */ public Disposer listen(final int port) { - return this.listen(new InetSocketAddress(Netty.localhost, port)); + // fix https://github.com/fzdwx/sky/issues/32 + // new InetSocketAddress(Netty.localhost, port) + return this.listen(new InetSocketAddress(port)); } @Override From 574ce663ea18df6974bce81d6e4b619608c5582e Mon Sep 17 00:00:00 2001 From: weiZhen Date: Fri, 17 Jun 2022 14:24:14 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E2=9C=A8=20support=20get=20http=20url?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bean/SkyResponseRequestResultHandler.java | 14 +------- .../src/main/java/core/http/HttpServer.java | 2 +- .../java/core/http/ext/HttpServerRequest.java | 27 ++++++++++++++-- .../core/http/ext/HttpServerResponse.java | 31 +++++++++++++++--- .../core/http/handler/HttpServerHandler.java | 7 ++-- .../core/http/inter/AggHttpServerRequest.java | 15 +++++++++ .../http/inter/HttpServerRequestImpl.java | 32 +++++++++++++++---- .../src/main/java/util/Netty.java | 14 ++++++++ .../src/test/java/http/HttpServerTest.java | 12 +++++++ 9 files changed, 125 insertions(+), 29 deletions(-) diff --git a/sky-http-springboot-starter/src/main/java/sky/starter/bean/SkyResponseRequestResultHandler.java b/sky-http-springboot-starter/src/main/java/sky/starter/bean/SkyResponseRequestResultHandler.java index b33192e..03d23a6 100644 --- a/sky-http-springboot-starter/src/main/java/sky/starter/bean/SkyResponseRequestResultHandler.java +++ b/sky-http-springboot-starter/src/main/java/sky/starter/bean/SkyResponseRequestResultHandler.java @@ -23,19 +23,7 @@ public boolean support(final Object result, final SkyRouteDefinition definition) @Override public void apply(final Object result, final SkyRouteDefinition definition, final HttpServerResponse response) { final HttpResponse httpResponse = (HttpResponse) result; - - response.status(httpResponse.status()); - - int type = httpResponse.type(); - if (type == HttpResponse.Type.TO_STRING) { - if (httpResponse.body() == null) { - response.end(); - } else { - response.end(httpResponse.body().toString()); - } - } else if (type == HttpResponse.Type.JSON) { - response.json(httpResponse.body()); - } + response.end(httpResponse); } @Override diff --git a/sky-infrastructure/src/main/java/core/http/HttpServer.java b/sky-infrastructure/src/main/java/core/http/HttpServer.java index eee4cca..bee52bd 100644 --- a/sky-infrastructure/src/main/java/core/http/HttpServer.java +++ b/sky-infrastructure/src/main/java/core/http/HttpServer.java @@ -129,7 +129,7 @@ public Disposer listen(final InetSocketAddress address) { } p.addLast(new HttpObjectAggregator(maxContentLength)); - p.addLast(new HttpServerHandler(httpHandler, exceptionHandler, sslFlag, jsonSerializer())); + p.addLast(new HttpServerHandler(httpHandler, exceptionHandler, sslFlag, jsonSerializer(), serverOrigin)); }) .listen(address); diff --git a/sky-infrastructure/src/main/java/core/http/ext/HttpServerRequest.java b/sky-infrastructure/src/main/java/core/http/ext/HttpServerRequest.java index aa129e8..4dc6a8c 100644 --- a/sky-infrastructure/src/main/java/core/http/ext/HttpServerRequest.java +++ b/sky-infrastructure/src/main/java/core/http/ext/HttpServerRequest.java @@ -29,6 +29,25 @@ */ public interface HttpServerRequest { + /** + * get headers Host or :authority + * + * @return {@link String } + */ + String host(); + + /** + * get full request url + * + * @return {@link String } + */ + String url(); + + /** + * @return the scheme of the request + */ + String scheme(); + /** * Release resources and end the life cycle */ @@ -184,7 +203,11 @@ default String bodyToString() { */ String contentType(); - static HttpServerRequest create(final ChannelHandlerContext ctx, final boolean ssl, AggHttpServerRequest msg, final JsonSerializer serializer) { - return new HttpServerRequestImpl(ctx, ssl, msg, serializer); + static HttpServerRequest create(final ChannelHandlerContext ctx, + final boolean ssl, + final AggHttpServerRequest msg, + final JsonSerializer serializer, + final String serverOrigin) { + return new HttpServerRequestImpl(ctx, ssl, msg, serializer, serverOrigin); } } \ No newline at end of file diff --git a/sky-infrastructure/src/main/java/core/http/ext/HttpServerResponse.java b/sky-infrastructure/src/main/java/core/http/ext/HttpServerResponse.java index 1e103f0..94e60a3 100644 --- a/sky-infrastructure/src/main/java/core/http/ext/HttpServerResponse.java +++ b/sky-infrastructure/src/main/java/core/http/ext/HttpServerResponse.java @@ -2,6 +2,7 @@ import core.common.Outbound; import core.http.inter.HttpServerResponseImpl; +import core.http.response.HttpResponse; import core.serializer.JsonSerializer; import io.github.fzdwx.lambada.Assert; import io.github.fzdwx.lambada.Io; @@ -115,16 +116,14 @@ static HttpServerResponseImpl create(Channel channel, final HttpServerRequest ht * return 404 response and write message */ default ChannelFuture notFound(final String message) { - return this.status(HttpResponseStatus.NOT_FOUND) - .end(message); + return this.status(HttpResponseStatus.NOT_FOUND).end(message); } /** * return 404 response */ default ChannelFuture notFound() { - return this.status(HttpResponseStatus.NOT_FOUND) - .end(); + return this.status(HttpResponseStatus.NOT_FOUND).end(); } /** @@ -181,6 +180,29 @@ default ChannelFuture sendFile(String filePath) throws IllegalArgumentException return sendFile(file); } + + /** + * end of http response. + */ + default ChannelFuture end(HttpResponse body) { + this.status(body.status()); + + final ChannelFuture cf; + int type = body.type(); + if (type == HttpResponse.Type.TO_STRING) { + if (body.body() == null) { + cf = this.end(); + } else { + cf = this.end(body.body().toString()); + } + } else if (type == HttpResponse.Type.JSON) { + cf = this.json(body.body()); + } else { + cf = channel().newFailedFuture(new UnknownError()); + } + return cf; + } + /** * @see #end(ByteBuf) */ @@ -268,5 +290,4 @@ default ChannelFuture writeFlush(String s) { default ChannelFuture writeFlush(byte[] bytes) { return sendAndFlush(bytes).then(); } - } \ No newline at end of file diff --git a/sky-infrastructure/src/main/java/core/http/handler/HttpServerHandler.java b/sky-infrastructure/src/main/java/core/http/handler/HttpServerHandler.java index 25f1002..12cc65f 100644 --- a/sky-infrastructure/src/main/java/core/http/handler/HttpServerHandler.java +++ b/sky-infrastructure/src/main/java/core/http/handler/HttpServerHandler.java @@ -24,16 +24,19 @@ public class HttpServerHandler extends ChannelInboundHandlerAdapter { private final boolean ssl; private final HttpExceptionHandler exceptionHandler; private final JsonSerializer serializer; + private final String serverOrigin; public HttpServerHandler( final HttpHandler httpHandler, final HttpExceptionHandler exceptionHandler, final Boolean ssl, - final JsonSerializer serializer) { + final JsonSerializer serializer, + final String serverOrigin) { this.ssl = ssl; this.httpHandler = httpHandler; this.exceptionHandler = HttpExceptionHandler.defaultExceptionHandler(exceptionHandler); this.serializer = serializer == null ? JsonSerializer.codec : serializer; + this.serverOrigin = serverOrigin; } @Override @@ -83,7 +86,7 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws E public boolean handleRequest(final ChannelHandlerContext ctx, final Object msg) { if (msg instanceof FullHttpRequest) { - final HttpServerRequest request = HttpServerRequest.create(ctx, ssl, ((AggHttpServerRequest) msg), serializer); + final HttpServerRequest request = HttpServerRequest.create(ctx, ssl, ((AggHttpServerRequest) msg), serializer, serverOrigin); final HttpServerResponse response = HttpServerResponse.create(ctx.channel(), request); try { diff --git a/sky-infrastructure/src/main/java/core/http/inter/AggHttpServerRequest.java b/sky-infrastructure/src/main/java/core/http/inter/AggHttpServerRequest.java index 5ea8a57..db0951a 100644 --- a/sky-infrastructure/src/main/java/core/http/inter/AggHttpServerRequest.java +++ b/sky-infrastructure/src/main/java/core/http/inter/AggHttpServerRequest.java @@ -91,6 +91,21 @@ public InterfaceHttpPostRequestDecoder bodyDecoder() { return this.postRequestDecoder; } + @Override + public String host() { + return null; + } + + @Override + public String url() { + return null; + } + + @Override + public String scheme() { + return ssl() ? "https" : "http"; + } + @Override public void destroy() { if (this.postRequestDecoder != null) { diff --git a/sky-infrastructure/src/main/java/core/http/inter/HttpServerRequestImpl.java b/sky-infrastructure/src/main/java/core/http/inter/HttpServerRequestImpl.java index 6014346..112ccd8 100644 --- a/sky-infrastructure/src/main/java/core/http/inter/HttpServerRequestImpl.java +++ b/sky-infrastructure/src/main/java/core/http/inter/HttpServerRequestImpl.java @@ -28,6 +28,7 @@ import io.netty.handler.codec.http.websocketx.CloseWebSocketFrame; import io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker; import io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory; +import lombok.extern.slf4j.Slf4j; import util.Netty; import java.io.IOException; @@ -44,6 +45,7 @@ * @date 2022/3/18 19:58 * @since 0.06 */ +@Slf4j public class HttpServerRequestImpl implements HttpServerRequest { private final ChannelHandlerContext ctx; @@ -54,6 +56,7 @@ public class HttpServerRequestImpl implements HttpServerRequest { private final Headers headers; private final boolean multipartFlag; private final boolean formUrlEncoderFlag; + private final String host; private HttpMethod methodType; private String path; private String query; @@ -62,22 +65,40 @@ public class HttpServerRequestImpl implements HttpServerRequest { private KvMap formAttributes; private Map uploadFiles; private boolean websocketFlag; + private String url; - public HttpServerRequestImpl(final ChannelHandlerContext ctx, - final boolean ssl, - final AggHttpServerRequest msg, - final JsonSerializer serializer) { + public HttpServerRequestImpl(final ChannelHandlerContext ctx, final boolean ssl, final AggHttpServerRequest msg, final JsonSerializer serializer, + final String serverOrigin) { this.ctx = ctx; this.channel = ctx.channel(); this.nettyRequest = msg; this.headers = msg.headers(); this.ssl = ssl; this.serializer = serializer; + this.host = Netty.getHost(msg.headers(), serverOrigin); this.bodyDecoder = msg.bodyDecoder(); this.multipartFlag = msg.multipart(); this.formUrlEncoderFlag = msg.formUrlEncoder(); } + @Override + public String host() { + return this.host; + } + + @Override + public String url() { + if (this.url == null) { + this.url = this.scheme() + "://" + host + uri(); + } + return this.url; + } + + @Override + public String scheme() { + return ssl() ? "https" : "http"; + } + @Override public void destroy() { if (this.nettyRequest.refCnt() > 0) { @@ -215,8 +236,7 @@ public void upgradeToWebSocket(Hooks h) { } //endregion - final WebSocketServerHandshaker handShaker = - new WebSocketServerHandshakerFactory(getWebSocketLocation(webSocket, nettyRequest), subProtocols, true).newHandshaker(nettyRequest); + final WebSocketServerHandshaker handShaker = new WebSocketServerHandshakerFactory(getWebSocketLocation(webSocket, nettyRequest), subProtocols, true).newHandshaker(nettyRequest); if (handShaker != null) { final ChannelPipeline pipeline = ctx.pipeline(); diff --git a/sky-infrastructure/src/main/java/util/Netty.java b/sky-infrastructure/src/main/java/util/Netty.java index d874881..2ad18ed 100644 --- a/sky-infrastructure/src/main/java/util/Netty.java +++ b/sky-infrastructure/src/main/java/util/Netty.java @@ -832,6 +832,20 @@ public static String query(final String uri) { } } + public static String getHost(final Headers headers, final String serverOrigin) { + String host = headers.get(":authority") != null ? headers.get(":authority") : null; + + if (host == null) { + host = headers.get("Host") != null ? headers.get("Host") : null; + } + + if (host == null) { + int idx = serverOrigin.indexOf("://"); + host = serverOrigin.substring(idx + 3); + } + return host; + } + static StringBuilder appendRequest(StringBuilder buf, HttpRequest req) { appendCommon(buf, req); appendInitialLine(buf, req); diff --git a/sky-infrastructure/src/test/java/http/HttpServerTest.java b/sky-infrastructure/src/test/java/http/HttpServerTest.java index 748910e..9bb4bea 100644 --- a/sky-infrastructure/src/test/java/http/HttpServerTest.java +++ b/sky-infrastructure/src/test/java/http/HttpServerTest.java @@ -4,6 +4,7 @@ import com.alibaba.fastjson2.JSONObject; import core.http.HttpServer; import core.http.ext.HttpHandler; +import core.http.response.HttpResponse; import core.http.route.Router; import io.github.fzdwx.lambada.Seq; import io.github.fzdwx.lambada.Threads; @@ -32,6 +33,17 @@ void test22() { .dispose(); } + @Test + void testHost() { + HttpServer.create() + .requestHandler(((request, response) -> { + response.end(HttpResponse.json("host", request.host()) + .put("url", request.url())); + })) + .listen(8888) + .dispose(); + } + @Test void test_websocket() { HttpServer.create() From f7bc73b1f9aa27c70d591583fb46bc931288ce2c Mon Sep 17 00:00:00 2001 From: weiZhen Date: Sat, 18 Jun 2022 11:42:46 +0800 Subject: [PATCH 4/4] Upgrade lambda --- pom.xml | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/pom.xml b/pom.xml index 509b289..f10a3d3 100644 --- a/pom.xml +++ b/pom.xml @@ -18,7 +18,7 @@ 1.8 UTF-8 - 0.10.9-dev + 0.10.10-dev 0.11.4-dev @@ -182,20 +182,20 @@ - - org.apache.maven.plugins - maven-gpg-plugin - 1.5 - - - sign-artifacts - verify - - sign - - - - + + + + + + + + + + + + + +