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();
}
}
}
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);
}
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;
}
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);
}
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);
}
}
Aggregations