Search in sources :

Example 41 with HttpResponseStatus

use of io.netty.handler.codec.http.HttpResponseStatus in project netty by netty.

the class WebSocketIndexPageHandler 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);
    ChannelFuture future = ctx.writeAndFlush(res);
    if (!keepAlive) {
        future.addListener(ChannelFutureListener.CLOSE);
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus)

Example 42 with HttpResponseStatus

use of io.netty.handler.codec.http.HttpResponseStatus in project netty by netty.

the class HttpConversionUtil method toHttpResponse.

/**
 * Create a new object to contain the response data.
 *
 * @param streamId The stream associated with the response
 * @param http2Headers The initial set of HTTP/2 headers to create the response with
 * @param validateHttpHeaders <ul>
 *        <li>{@code true} to validate HTTP headers in the http-codec</li>
 *        <li>{@code false} not to validate HTTP headers in the http-codec</li>
 *        </ul>
 * @return A new response object which represents headers for a chunked response
 * @throws Http2Exception see {@link #addHttp2ToHttpHeaders(int, Http2Headers,
 *         HttpHeaders, HttpVersion, boolean, boolean)}
 */
public static HttpResponse toHttpResponse(final int streamId, final Http2Headers http2Headers, final boolean validateHttpHeaders) throws Http2Exception {
    final HttpResponseStatus status = parseStatus(http2Headers.status());
    // HTTP/2 does not define a way to carry the version or reason phrase that is included in an
    // HTTP/1.1 status line.
    final HttpResponse msg = new DefaultHttpResponse(HttpVersion.HTTP_1_1, status, validateHttpHeaders);
    try {
        addHttp2ToHttpHeaders(streamId, http2Headers, msg.headers(), msg.protocolVersion(), false, false);
    } catch (final Http2Exception e) {
        throw e;
    } catch (final Throwable t) {
        throw streamError(streamId, PROTOCOL_ERROR, t, "HTTP/2 to HTTP/1.x headers conversion error");
    }
    return msg;
}
Also used : HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse)

Example 43 with HttpResponseStatus

use of io.netty.handler.codec.http.HttpResponseStatus in project netty by netty.

the class HttpConversionUtil method toFullHttpResponse.

/**
 * Create a new object to contain the response data
 *
 * @param streamId The stream associated with the response
 * @param http2Headers The initial set of HTTP/2 headers to create the response with
 * @param content {@link ByteBuf} content to put in {@link FullHttpResponse}
 * @param validateHttpHeaders <ul>
 *        <li>{@code true} to validate HTTP headers in the http-codec</li>
 *        <li>{@code false} not to validate HTTP headers in the http-codec</li>
 *        </ul>
 * @return A new response object which represents headers/data
 * @throws Http2Exception see {@link #addHttp2ToHttpHeaders(int, Http2Headers, FullHttpMessage, boolean)}
 */
public static FullHttpResponse toFullHttpResponse(int streamId, Http2Headers http2Headers, ByteBuf content, boolean validateHttpHeaders) throws Http2Exception {
    HttpResponseStatus status = parseStatus(http2Headers.status());
    // HTTP/2 does not define a way to carry the version or reason phrase that is included in an
    // HTTP/1.1 status line.
    FullHttpResponse msg = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status, content, validateHttpHeaders);
    try {
        addHttp2ToHttpHeaders(streamId, http2Headers, msg, false);
    } catch (Http2Exception e) {
        msg.release();
        throw e;
    } catch (Throwable t) {
        msg.release();
        throw streamError(streamId, PROTOCOL_ERROR, t, "HTTP/2 to HTTP/1.x headers conversion error");
    }
    return msg;
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse)

Example 44 with HttpResponseStatus

use of 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 45 with HttpResponseStatus

use of 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)

Aggregations

HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)53 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)16 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)15 ByteBuf (io.netty.buffer.ByteBuf)14 HttpResponse (io.netty.handler.codec.http.HttpResponse)9 HttpVersion (io.netty.handler.codec.http.HttpVersion)8 IOException (java.io.IOException)8 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)7 HttpMethod (io.netty.handler.codec.http.HttpMethod)7 Test (org.junit.Test)7 Map (java.util.Map)5 DefaultHttpResponse (io.netty.handler.codec.http.DefaultHttpResponse)4 HttpRequest (io.netty.handler.codec.http.HttpRequest)4 NoSuchElementException (java.util.NoSuchElementException)4 ChannelFuture (io.netty.channel.ChannelFuture)3 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)3 DefaultHttpContent (io.netty.handler.codec.http.DefaultHttpContent)3 PrintWriter (java.io.PrintWriter)3 List (java.util.List)3 Unpooled (io.netty.buffer.Unpooled)2