Search in sources :

Example 61 with FullHttpResponse

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);
    }
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) WebSocketServerHandshakerFactory(io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory)

Example 62 with FullHttpResponse

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);
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HashMap(java.util.HashMap) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse)

Example 63 with FullHttpResponse

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;
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) ByteBuf(io.netty.buffer.ByteBuf)

Example 64 with FullHttpResponse

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();
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) BadRequestException(info.xiancloud.nettyhttpserver.http.bean.BadRequestException) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse)

Example 65 with FullHttpResponse

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);
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) ResponseWrapper(info.xiancloud.nettyhttpserver.http.bean.ResponseWrapper) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse)

Aggregations

FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)256 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)172 ByteBuf (io.netty.buffer.ByteBuf)53 Test (org.junit.Test)50 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)38 HttpRequest (io.netty.handler.codec.http.HttpRequest)34 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)24 HttpObject (io.netty.handler.codec.http.HttpObject)23 IOException (java.io.IOException)23 HttpTrade (org.jocean.http.server.HttpServerBuilder.HttpTrade)23 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)21 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)21 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)21 Test (org.junit.jupiter.api.Test)21 ChannelFuture (io.netty.channel.ChannelFuture)20 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)20 HttpInitiator (org.jocean.http.client.HttpClient.HttpInitiator)20 Subscription (rx.Subscription)20 LocalAddress (io.netty.channel.local.LocalAddress)19 HttpResponse (io.netty.handler.codec.http.HttpResponse)19