Search in sources :

Example 61 with HttpResponseStatus

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponseStatus in project netty by netty.

the class WebSocketServerHandler method sendHttpResponse.

private static void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) {
    // Generate an error page if response getStatus code is not OK (200).
    HttpResponseStatus responseStatus = res.status();
    if (responseStatus.code() != 200) {
        ByteBufUtil.writeUtf8(res.content(), responseStatus.toString());
        HttpUtil.setContentLength(res, res.content().readableBytes());
    }
    // Send the response and close the connection if necessary.
    boolean keepAlive = HttpUtil.isKeepAlive(req) && responseStatus.code() == 200;
    HttpUtil.setKeepAlive(res, keepAlive);
    // Flushed in channelReadComplete()
    ChannelFuture future = ctx.write(res);
    if (!keepAlive) {
        future.addListener(ChannelFutureListener.CLOSE);
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus)

Example 62 with HttpResponseStatus

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponseStatus in project knotx by Cognifide.

the class RequestProcessorKnotProxyImpl method processError.

@Override
protected KnotContext processError(KnotContext knotContext, Throwable error) {
    HttpResponseStatus statusCode;
    if (error instanceof NoSuchElementException) {
        statusCode = HttpResponseStatus.NOT_FOUND;
    } else {
        statusCode = HttpResponseStatus.INTERNAL_SERVER_ERROR;
    }
    knotContext.getClientResponse().setStatusCode(statusCode.code());
    return knotContext;
}
Also used : HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) NoSuchElementException(java.util.NoSuchElementException)

Example 63 with HttpResponseStatus

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponseStatus in project knotx by Cognifide.

the class GatewayKnotProxyImpl method processError.

@Override
protected KnotContext processError(KnotContext knotContext, Throwable error) {
    HttpResponseStatus statusCode;
    if (error instanceof NoSuchElementException) {
        statusCode = HttpResponseStatus.NOT_FOUND;
    } else {
        statusCode = HttpResponseStatus.INTERNAL_SERVER_ERROR;
    }
    knotContext.getClientResponse().setStatusCode(statusCode.code());
    return knotContext;
}
Also used : HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) NoSuchElementException(java.util.NoSuchElementException)

Example 64 with HttpResponseStatus

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponseStatus in project zuul by Netflix.

the class PushMessageSender method channelRead0.

@Override
protected void channelRead0(final ChannelHandlerContext ctx, final FullHttpRequest request) throws Exception {
    if (!request.decoderResult().isSuccess()) {
        sendHttpResponse(ctx, request, BAD_REQUEST, null);
        return;
    }
    final String path = request.uri();
    if (path == null) {
        sendHttpResponse(ctx, request, BAD_REQUEST, null);
        return;
    }
    if (path.endsWith("/push")) {
        logPushAttempt();
        final HttpMethod method = request.method();
        if ((method != HttpMethod.POST) && (method != HttpMethod.GET)) {
            sendHttpResponse(ctx, request, METHOD_NOT_ALLOWED, null);
            return;
        }
        final PushUserAuth userAuth = getPushUserAuth(request);
        if (!userAuth.isSuccess()) {
            sendHttpResponse(ctx, request, UNAUTHORIZED, userAuth);
            logNoIdentity();
            return;
        }
        final PushConnection pushConn = pushConnectionRegistry.get(userAuth.getClientIdentity());
        if (pushConn == null) {
            sendHttpResponse(ctx, request, NOT_FOUND, userAuth);
            logClientNotConnected();
            return;
        }
        if (!verifySecureToken(request, pushConn)) {
            sendHttpResponse(ctx, request, FORBIDDEN, userAuth);
            logSecurityTokenVerificationFail();
            return;
        }
        if (method == HttpMethod.GET) {
            // client only checking if particular CID + ESN is connected to this instance
            sendHttpResponse(ctx, request, OK, userAuth);
            return;
        }
        final ByteBuf body = request.content().retain();
        if (body.readableBytes() <= 0) {
            sendHttpResponse(ctx, request, NO_CONTENT, userAuth);
            return;
        }
        if (pushConn.isRateLimited()) {
            sendHttpResponse(ctx, request, HttpResponseStatus.SERVICE_UNAVAILABLE, userAuth);
            logRateLimited();
            return;
        }
        final ChannelFuture clientFuture = pushConn.sendPushMessage(body);
        clientFuture.addListener(cf -> {
            HttpResponseStatus status;
            if (cf.isSuccess()) {
                logPushSuccess();
                status = OK;
            } else {
                logPushError(cf.cause());
                status = INTERNAL_SERVER_ERROR;
            }
            sendHttpResponse(ctx, request, status, userAuth);
        });
    } else {
        // Last handler in the chain
        sendHttpResponse(ctx, request, BAD_REQUEST, null);
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) ByteBuf(io.netty.buffer.ByteBuf) HttpMethod(io.netty.handler.codec.http.HttpMethod)

Example 65 with HttpResponseStatus

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponseStatus in project flink by apache.

the class HandlerRedirectUtils method getResponse.

public static HttpResponse getResponse(HttpResponseStatus status, @Nullable String message) {
    ByteBuf messageByteBuf = message == null ? Unpooled.buffer(0) : Unpooled.wrappedBuffer(message.getBytes(ENCODING));
    FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status, messageByteBuf);
    response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/plain; charset=" + ENCODING.name());
    response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes());
    return response;
}
Also used : DefaultFullHttpResponse(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultFullHttpResponse) FullHttpResponse(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultFullHttpResponse) ByteBuf(org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf)

Aggregations

HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)73 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)17 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)16 ByteBuf (io.netty.buffer.ByteBuf)15 HttpMethod (io.netty.handler.codec.http.HttpMethod)11 IOException (java.io.IOException)11 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)10 HttpResponse (io.netty.handler.codec.http.HttpResponse)10 HttpVersion (io.netty.handler.codec.http.HttpVersion)9 Map (java.util.Map)8 URI (java.net.URI)7 Test (org.junit.Test)7 URISyntaxException (java.net.URISyntaxException)6 Test (org.junit.jupiter.api.Test)6 Channel (io.netty.channel.Channel)5 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)5 HttpHeaderNames (io.netty.handler.codec.http.HttpHeaderNames)5 Duration (java.time.Duration)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4