Skip to content

Commit

Permalink
update HttpRequestHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
flzj-kl committed Oct 15, 2024
1 parent fa75320 commit 93030de
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.*;

import java.net.URI;
import java.net.URISyntaxException;

/**
* @description: HttpRequestHandler
* @author:flzjkl
Expand All @@ -18,8 +21,8 @@ public class HttpRequestHandler extends SimpleChannelInboundHandler<FullHttpRequ


@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) {
String uri = request.uri();
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) throws Exception {
String path = new URI(request.uri()).getPath();;
HttpMethod method = request.method();
FullHttpResponse resp = null;

Expand All @@ -28,7 +31,7 @@ protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request)
}

if (HttpMethod.POST.equals(method)) {
if ("/api/native-agent".equals(uri)) {
if ("/api/native-agent".equals(path)) {
resp = httpNativeAgentHandler.handle(ctx, request);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URI;

/**
* @description: HttpRequestHandler
* @author:flzjkl
Expand All @@ -25,13 +27,16 @@ public class HttpRequestHandler extends SimpleChannelInboundHandler<FullHttpRequ


@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) {
String path = request.uri();
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) throws Exception {
String path = new URI(request.uri()).getPath();
HttpMethod method = request.method();
FullHttpResponse resp = null;

if (HttpMethod.GET.equals(method)) {
resp = httpResourcesHandler.handlerResources(path);
if ("/".equals(path)) {
path = "/index.html";
}
resp = httpResourcesHandler.handlerResources(request, path);
}

if (HttpMethod.OPTIONS.equals(method)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,8 @@ public class HttpResourcesHandler {
private static final Logger logger = LoggerFactory.getLogger(HttpResourcesHandler.class);

private static final String RESOURCES_PATH = "native-agent";
public FullHttpResponse handlerResources (String path) {
public FullHttpResponse handlerResources (FullHttpRequest request, String path) {
FullHttpResponse resp = null;
if ("/".equals(path)) {
path = "/index.html";
}
if (path.contains(".html") || path.contains(".css") || path.contains(".js") || path.contains(".ico") || path.contains(".png")) {
if (path.contains("?")) {
path = path.split("\\?")[0];
Expand All @@ -32,15 +29,15 @@ public FullHttpResponse handlerResources (String path) {
if (is != null) {
try {
ByteBuf content = readInputStream(is);
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, content);
FullHttpResponse response = new DefaultFullHttpResponse(request.getProtocolVersion(), HttpResponseStatus.OK, content);
HttpHeaders headers = response.headers();
headers.set(HttpHeaderNames.CONTENT_TYPE, getContentType(path));
headers.set(HttpHeaderNames.CONTENT_LENGTH, content.readableBytes());
headers.set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
resp = response;
} catch (IOException e) {
logger.error("find resources error:" + e.getMessage());
resp = new DefaultFullHttpResponse(resp.getProtocolVersion(), HttpResponseStatus.NOT_FOUND);
resp = new DefaultFullHttpResponse(request.getProtocolVersion(), HttpResponseStatus.NOT_FOUND);
resp.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/html; charset=utf-8");
} finally {
if (is != null) {
Expand Down

0 comments on commit 93030de

Please sign in to comment.