Skip to content

Commit

Permalink
🐛 fixed connection reset by peer
Browse files Browse the repository at this point in the history
  • Loading branch information
hellokaton committed Jun 15, 2018
1 parent 1018463 commit 1cce172
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 46 deletions.
113 changes: 67 additions & 46 deletions src/main/java/com/blade/server/netty/HttpHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,14 @@ protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest req) {

String remoteAddress = ctx.channel().remoteAddress().toString();

boolean isStatic = false;
boolean keepAlive = isKeepAlive(req);

Instant start = Instant.now();

Request request = HttpRequest.build(req, remoteAddress);
Response response = new HttpResponse();

boolean keepAlive = isKeepAlive(req);

// route signature
Signature signature = Signature.builder().request(request).response(response).build();
// request uri
String uri = request.uri();
String cleanUri = uri;
Expand All @@ -100,60 +99,80 @@ protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest req) {
try {
if (isStaticFile(cleanUri)) {
staticFileHandler.handle(ctx, request, response);
isStatic = true;
return;
}

Route route = routeMatcher.lookupRoute(request.method(), cleanUri);
if (null == route) {
log404(log, method, uri);
throw new NotFoundException(uri);
}

request.initPathParams(route);

// get method parameters
signature.setRoute(route);

// middleware
if (hasMiddleware && !invokeMiddleware(routeMatcher.getMiddleware(), signature)) {
response.body(EmptyBody.empty());
handleResponse(response, ctx, keepAlive);
return;
}

// web hook before
if (hasBeforeHook && !invokeHook(routeMatcher.getBefore(cleanUri), signature)) {
response.body(EmptyBody.empty());
handleResponse(response, ctx, keepAlive);
// route signature
Signature signature = Signature.builder().request(request).response(response).build();
if (execution(ctx, keepAlive, signature, cleanUri)) {
return;
}

// execute
this.routeHandle(signature);

// webHook
if (hasAfterHook) {
this.invokeHook(routeMatcher.getAfter(cleanUri), signature);
}

long cost = log200(log, start, method, uri);
request.attribute(REQUEST_COST_TIME, cost);
} catch (Exception e) {
if (e instanceof BladeException) {
} else {
log500(log, method, uri);
}
if (null != exceptionHandler) {
exceptionHandler.handle(e);
} else {
log.error("Request execution error", e);
}
this.exceptionCaught(uri, method, e);
} finally {
this.handleResponse(response, ctx, keepAlive);
if (!isStatic) {
this.handleResponse(response, ctx, keepAlive);
}
WebContext.remove();
}
}

private void exceptionCaught(String uri, String method, Exception e) {
if (e instanceof BladeException) {
} else {
log500(log, method, uri);
}
if (null != exceptionHandler) {
exceptionHandler.handle(e);
} else {
log.error("Request execution error", e);
}
}

private boolean execution(ChannelHandlerContext ctx, boolean keepAlive, Signature signature, String cleanUri) throws Exception {
Request request = signature.request();

Route route = routeMatcher.lookupRoute(request.method(), cleanUri);
if (null == route) {
log404(log, request.method(), request.uri());
throw new NotFoundException(request.uri());
}

Response response = signature.response();

request.initPathParams(route);

// get method parameters
signature.setRoute(route);

// middleware
if (hasMiddleware && !invokeMiddleware(routeMatcher.getMiddleware(), signature)) {
signature.response().body(EmptyBody.empty());
handleResponse(response, ctx, keepAlive);
return true;
}

// web hook before
if (hasBeforeHook && !invokeHook(routeMatcher.getBefore(cleanUri), signature)) {
response.body(EmptyBody.empty());
handleResponse(response, ctx, keepAlive);
return true;
}

// execute
this.routeHandle(signature);

// webHook
if (hasAfterHook) {
this.invokeHook(routeMatcher.getAfter(cleanUri), signature);
}
return false;
}

@Override
public void channelReadComplete(ChannelHandlerContext ctx) {
ctx.flush();
Expand Down Expand Up @@ -368,9 +387,11 @@ public boolean invokeHook(Signature routeSignature, Route hookRoute) throws Exce
Object returnParam;
if (len > 0) {
if (len == 1) {
returnParam = ReflectKit.invokeMethod(target, hookMethod, routeSignature);
MethodAccess methodAccess = BladeCache.getMethodAccess(target.getClass());
returnParam = methodAccess.invoke(target, hookMethod.getName(), new Object[]{routeSignature});
} else if (len == 2) {
returnParam = ReflectKit.invokeMethod(target, hookMethod, routeSignature.request(), routeSignature.response());
MethodAccess methodAccess = BladeCache.getMethodAccess(target.getClass());
returnParam = methodAccess.invoke(target, hookMethod.getName(), new Object[]{routeSignature.request(), routeSignature.response()});
} else {
throw new InternalErrorException("Bad web hook structure");
}
Expand Down
3 changes: 3 additions & 0 deletions src/test/java/netty_hello/Hello.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public static void main(String[] args) {
e.printStackTrace();
}
})
.before("/*", (request, response) -> {
System.out.println("Before...");
})
.get("/rand", ((request, response) -> {
try {
TimeUnit.MILLISECONDS.sleep(new Random().nextInt(1000));
Expand Down

0 comments on commit 1cce172

Please sign in to comment.