Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
fzdwx committed Jun 24, 2022
2 parents 09337dd + f7bc73b commit 9c57a9f
Show file tree
Hide file tree
Showing 12 changed files with 157 additions and 49 deletions.
30 changes: 15 additions & 15 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<lambda.version>0.10.9-dev</lambda.version>
<lambda.version>0.10.10-dev</lambda.version>
<sky.version>0.11.4-dev</sky.version>


Expand Down Expand Up @@ -182,20 +182,20 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- <plugin>-->
<!-- <groupId>org.apache.maven.plugins</groupId>-->
<!-- <artifactId>maven-gpg-plugin</artifactId>-->
<!-- <version>1.5</version>-->
<!-- <executions>-->
<!-- <execution>-->
<!-- <id>sign-artifacts</id>-->
<!-- <phase>verify</phase>-->
<!-- <goals>-->
<!-- <goal>sign</goal>-->
<!-- </goals>-->
<!-- </execution>-->
<!-- </executions>-->
<!-- </plugin>-->
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions sky-infrastructure/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ It encapsulates a layer on netty, which is easier to use

```xml
<dependency>
<groupId>io.github.fzdwx</groupId>
<artifactId>sky-infrastructure</artifactId>
<version>0.11.4-dev</version>
<groupId>io.github.fzdwx</groupId>
<artifactId>sky-infrastructure</artifactId>
<version>0.11.4-dev</version>
</dependency>
```

Expand Down
12 changes: 11 additions & 1 deletion sky-infrastructure/src/main/java/core/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,18 @@ public class Server implements core.Transport<Server> {
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;
Expand Down
6 changes: 4 additions & 2 deletions sky-infrastructure/src/main/java/core/http/HttpServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -127,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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -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)
*/
Expand Down Expand Up @@ -268,5 +290,4 @@ default ChannelFuture writeFlush(String s) {
default ChannelFuture writeFlush(byte[] bytes) {
return sendAndFlush(bytes).then();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -44,6 +45,7 @@
* @date 2022/3/18 19:58
* @since 0.06
*/
@Slf4j
public class HttpServerRequestImpl implements HttpServerRequest {

private final ChannelHandlerContext ctx;
Expand All @@ -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;
Expand All @@ -62,22 +65,40 @@ public class HttpServerRequestImpl implements HttpServerRequest {
private KvMap formAttributes;
private Map<String, FileUpload> 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) {
Expand Down Expand Up @@ -215,8 +236,7 @@ public void upgradeToWebSocket(Hooks<WebSocket> 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();
Expand Down
14 changes: 14 additions & 0 deletions sky-infrastructure/src/main/java/util/Netty.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading

0 comments on commit 9c57a9f

Please sign in to comment.