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 22b519c commit 2619a09
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
27 changes: 18 additions & 9 deletions src/main/java/com/blade/mvc/handler/DefaultExceptionHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import io.netty.handler.codec.http.HttpVersion;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Optional;
Expand All @@ -36,15 +37,17 @@ public class DefaultExceptionHandler implements ExceptionHandler {

@Override
public void handle(Exception e) {
Response response = WebContext.response();
Request request = WebContext.request();

if (e instanceof BladeException) {
this.handleBladeException((BladeException) e, request, response);
} else if (ValidatorException.class.isInstance(e)) {
this.handleValidators(ValidatorException.class.cast(e), request, response);
} else {
this.handleException(e, request, response);
if (!isResetByPeer(e)) {
Response response = WebContext.response();
Request request = WebContext.request();

if (e instanceof BladeException) {
this.handleBladeException((BladeException) e, request, response);
} else if (ValidatorException.class.isInstance(e)) {
this.handleValidators(ValidatorException.class.cast(e), request, response);
} else {
this.handleException(e, request, response);
}
}
}

Expand Down Expand Up @@ -146,4 +149,10 @@ protected String getStackTrace(Throwable exception) {
return errors.toString();
}

private boolean isResetByPeer(Exception e) {
if (IOException.class.isInstance(e) && "Connection reset by peer".equals(e.getMessage())) {
return true;
}
return false;
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/blade/mvc/http/HttpRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public class HttpRequest implements Request {
private void init(FullHttpRequest fullHttpRequest) {
// headers
HttpHeaders httpHeaders = fullHttpRequest.headers();
if (httpHeaders.isEmpty()) {
if (!httpHeaders.isEmpty()) {
this.headers = new HashMap<>(httpHeaders.size());
Iterator<Map.Entry<String, String>> entryIterator = httpHeaders.iteratorAsString();
while (entryIterator.hasNext()) {
Expand Down
13 changes: 7 additions & 6 deletions src/main/java/com/blade/mvc/http/HttpResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -218,13 +218,14 @@ public void send(@NonNull FullHttpResponse response) {
response.headers().set(HttpConst.CONTENT_LENGTH, String.valueOf(response.content().readableBytes()));
}

if (!keepAlive) {
ctx.write(response).addListener(ChannelFutureListener.CLOSE);
} else {
response.headers().set(HttpConst.CONNECTION, KEEP_ALIVE);
ctx.write(response, ctx.voidPromise());
if (ctx.channel().isWritable() && ctx.channel().isOpen()) {
if (!keepAlive) {
ctx.write(response).addListener(ChannelFutureListener.CLOSE);
} else {
response.headers().set(HttpConst.CONNECTION, KEEP_ALIVE);
ctx.write(response, ctx.voidPromise());
}
}

}

@Override
Expand Down

0 comments on commit 2619a09

Please sign in to comment.