Search in sources :

Example 96 with FullHttpResponse

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

the class SpdyHttpDecoder method createHttpResponse.

private static FullHttpResponse createHttpResponse(SpdyHeadersFrame responseFrame, ByteBufAllocator alloc, boolean validateHeaders) throws Exception {
    // Create the first line of the response from the name/value pairs
    SpdyHeaders headers = responseFrame.headers();
    HttpResponseStatus status = HttpResponseStatus.parseLine(headers.get(STATUS));
    HttpVersion version = HttpVersion.valueOf(headers.getAsString(VERSION));
    headers.remove(STATUS);
    headers.remove(VERSION);
    boolean release = true;
    ByteBuf buffer = alloc.buffer();
    try {
        FullHttpResponse res = new DefaultFullHttpResponse(version, status, buffer, validateHeaders);
        for (Map.Entry<CharSequence, CharSequence> e : responseFrame.headers()) {
            res.headers().add(e.getKey(), e.getValue());
        }
        // The Connection and Keep-Alive headers are no longer valid
        HttpUtil.setKeepAlive(res, true);
        // Transfer-Encoding header is not valid
        res.headers().remove(HttpHeaderNames.TRANSFER_ENCODING);
        res.headers().remove(HttpHeaderNames.TRAILER);
        release = false;
        return res;
    } finally {
        if (release) {
            buffer.release();
        }
    }
}
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) ByteBuf(io.netty.buffer.ByteBuf) HttpVersion(io.netty.handler.codec.http.HttpVersion) HashMap(java.util.HashMap) Map(java.util.Map)

Example 97 with FullHttpResponse

use of io.netty.handler.codec.http.FullHttpResponse in project zuul by Netflix.

the class PushMessageSender method sendHttpResponse.

private void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest request, HttpResponseStatus status, PushUserAuth userAuth) {
    final FullHttpResponse resp = new DefaultFullHttpResponse(HTTP_1_1, status);
    resp.headers().add("Content-Length", "0");
    final ChannelFuture cf = ctx.channel().writeAndFlush(resp);
    if (!HttpUtil.isKeepAlive(request)) {
        cf.addListener(ChannelFutureListener.CLOSE);
    }
    logPushEvent(request, status, userAuth);
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse)

Example 98 with FullHttpResponse

use of io.netty.handler.codec.http.FullHttpResponse in project wildfly by wildfly.

the class TestingOcspServer method getHttpResponse.

public HttpResponse getHttpResponse(HttpRequest request, HttpOcspServlet servlet) {
    byte[] body;
    HttpMethod method;
    if (request.getBody() == null) {
        method = HttpMethod.GET;
        body = request.getPath().getValue().split("/ocsp/", 2)[1].getBytes(UTF_8);
    } else {
        method = HttpMethod.POST;
        body = request.getBody().getRawBytes();
    }
    ByteBuf buffer = Unpooled.wrappedBuffer(body);
    FullHttpRequest nettyRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_0, method, request.getPath().getValue(), buffer);
    for (Header header : request.getHeaderList()) {
        for (NottableString value : header.getValues()) {
            nettyRequest.headers().add(header.getName().getValue(), value.getValue());
        }
    }
    FullHttpResponse nettyResponse;
    try {
        nettyResponse = servlet.service(nettyRequest, new ServletURI(request.getPath().getValue()), null, SslReverseProxyMode.NONE);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    HttpResponse response = response().withStatusCode(nettyResponse.status().code()).withBody(nettyResponse.content().array());
    for (Map.Entry<String, String> header : nettyResponse.headers()) {
        response.withHeader(header.getKey(), header.getValue());
    }
    return response;
}
Also used : ServletURI(org.xipki.http.servlet.ServletURI) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) HttpResponse(org.mockserver.model.HttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) NottableString(org.mockserver.model.NottableString) ByteBuf(io.netty.buffer.ByteBuf) SQLException(java.sql.SQLException) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Header(org.mockserver.model.Header) NottableString(org.mockserver.model.NottableString) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) Map(java.util.Map) HttpMethod(io.netty.handler.codec.http.HttpMethod)

Example 99 with FullHttpResponse

use of io.netty.handler.codec.http.FullHttpResponse in project dubbo by alibaba.

the class HttpProcessHandler method channelRead0.

@Override
protected void channelRead0(ChannelHandlerContext ctx, HttpRequest msg) throws Exception {
    CommandContext commandContext = HttpCommandDecoder.decode(msg);
    // return 404 when fail to construct command context
    FullHttpResponse response;
    if (commandContext == null) {
        log.warn("can not found commandContext url: " + msg.getUri());
        response = http404();
    } else {
        commandContext.setRemote(ctx.channel());
        try {
            String result = commandExecutor.execute(commandContext);
            response = http200(result);
        } catch (NoSuchCommandException ex) {
            log.error("can not find commandContext: " + commandContext, ex);
            response = http404();
        } catch (Exception qosEx) {
            log.error("execute commandContext: " + commandContext + " got exception", qosEx);
            response = http500(qosEx.getMessage());
        }
    }
    ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}
Also used : CommandContext(org.apache.dubbo.qos.command.CommandContext) NoSuchCommandException(org.apache.dubbo.qos.command.NoSuchCommandException) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) NoSuchCommandException(org.apache.dubbo.qos.command.NoSuchCommandException)

Example 100 with FullHttpResponse

use of io.netty.handler.codec.http.FullHttpResponse in project blade by biezhi.

the class DefaultExceptionHandler method renderPage.

protected void renderPage(Response response, ModelAndView modelAndView) {
    var sw = new StringWriter();
    try {
        WebContext.blade().templateEngine().render(modelAndView, sw);
        ByteBuf buffer = Unpooled.wrappedBuffer(sw.toString().getBytes("utf-8"));
        FullHttpResponse fullHttpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.valueOf(response.statusCode()), buffer);
        response.body(new RawBody(fullHttpResponse));
    } catch (Exception e) {
        log.error("Render view error", e);
    }
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) StringWriter(java.io.StringWriter) lombok.var(lombok.var) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) RawBody(com.blade.mvc.http.RawBody) ByteBuf(io.netty.buffer.ByteBuf)

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