use of io.netty.handler.codec.http.FullHttpResponse in project ballerina by ballerina-lang.
the class VMDebugServerHandler method handleHttpRequest.
private void handleHttpRequest(ChannelHandlerContext ctx, FullHttpRequest req) {
// Handle a bad request.
if (!req.decoderResult().isSuccess()) {
sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, BAD_REQUEST));
return;
}
// Allow only GET methods.
if (req.method() != GET) {
sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, FORBIDDEN));
return;
}
if (!DebugConstants.DEBUG_WEBSOCKET_PATH.equals(req.uri())) {
FullHttpResponse res = new DefaultFullHttpResponse(HTTP_1_1, NOT_FOUND);
sendHttpResponse(ctx, req, res);
return;
}
// Handshake
WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(getWebSocketLocation(req), null, true);
handshaker = wsFactory.newHandshaker(req);
if (handshaker == null) {
WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel());
} else {
try {
debugManager.addDebugSession(ctx.channel());
} catch (DebugException e) {
FullHttpResponse res = new DefaultFullHttpResponse(HTTP_1_1, TOO_MANY_REQUESTS);
sendHttpResponse(ctx, req, res);
return;
}
handshaker.handshake(ctx.channel(), req);
}
}
use of io.netty.handler.codec.http.FullHttpResponse in project modules-extra by CubeEngine.
the class HttpRequestHandler method error.
private void error(ChannelHandlerContext context, RequestStatus error, ApiRequestException e) {
Map<String, Object> data = new HashMap<>();
data.put("id", error.getCode());
data.put("desc", error.getDescription());
if (e != null) {
Map<String, Object> reason = new HashMap<>();
reason.put("id", e.getCode());
reason.put("desc", e.getMessage());
data.put("reason", reason);
}
FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, error.getRepsonseStatus(), Unpooled.copiedBuffer(this.serialize(data), this.UTF8));
response.headers().set(CONTENT_TYPE, MimeType.JSON.toString());
context.writeAndFlush(response).addListener(CLOSE).addListener(CLOSE_ON_FAILURE);
}
use of io.netty.handler.codec.http.FullHttpResponse in project xian by happyyangyuan.
the class ResponseBuilder method createResponse.
public static FullHttpResponse createResponse(HttpResponseStatus status, String message) {
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status);
if (message != null) {
ByteBuf buf = Unpooled.copiedBuffer(message, CharsetUtil.UTF_8);
response = response.replace(buf);
response.headers().set(HttpHeaderNames.CONTENT_LENGTH, buf.writerIndex());
} else {
response.headers().set(HttpHeaderNames.CONTENT_LENGTH, 0);
}
response.headers().set(HttpHeaderNames.CONTENT_TYPE, APPLICATION_JSON);
response.headers().set(HttpHeaderNames.CACHE_CONTROL, HttpHeaderValues.NO_STORE);
response.headers().set(HttpHeaderNames.PRAGMA, HttpHeaderValues.NO_CACHE);
return response;
}
use of io.netty.handler.codec.http.FullHttpResponse in project xian by happyyangyuan.
the class DefaultExceptionHandler method exceptionCaught.
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
LOG.error("Exception caught", cause);
HttpResponseStatus status = (cause instanceof BadRequestException) ? BAD_REQUEST : INTERNAL_SERVER_ERROR;
String content = StringUtil.getExceptionStacktrace(cause);
FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, status, Unpooled.copiedBuffer(content, CharsetUtil.UTF_8));
response.headers().set(CONTENT_TYPE, Config.getContentType());
response.headers().set(CONTENT_LENGTH, response.content().readableBytes());
/*ctx.writeAndFlush(response); todo 爆了异常之后,这里根本写不进去,直接查日志吧!*/
ctx.close();
LOG.warn("TODO: 16/9/10 目前是出现了异常则直接关闭,这样对长连接稳定性好像不太有利");
MsgIdHolder.clear();
}
use of io.netty.handler.codec.http.FullHttpResponse in project xian by happyyangyuan.
the class HttpResponseBuilder method write.
@Override
public void write(ChannelHandlerContext ctx, Object responseMsg, ChannelPromise promise) throws Exception {
ResponseWrapper responseWrapper = (ResponseWrapper) responseMsg;
FullHttpResponse httpResponse = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.copiedBuffer(responseWrapper.getResPayload().toString(), Config.defaultUtf8()));
httpResponse.headers().set(CONTENT_TYPE, contentType(responseWrapper));
httpResponse.headers().set(CONTENT_LENGTH, httpResponse.content().readableBytes());
LOG.debug("我们不适用http1.1协议默认的keep-alive = true以及请求的header中的keep-alive来决定连接是否keep-alvie而是使用自定义的header: LONG_CONNECTION");
Boolean keepAlive = ctx.channel().attr(ClearingHandler.LONG_CONNECTION).get();
if (keepAlive) {
httpResponse.headers().set(CONNECTION, HttpHeaderValues.KEEP_ALIVE);
} else {
httpResponse.headers().set(CONNECTION, HttpHeaderValues.CLOSE);
}
/*LOG.info("<<<<<<<<<<<< netty 返回 HttpResponse: \r\n" + httpResponse);*/
super.write(ctx, httpResponse, promise);
}
Aggregations