use of io.netty.handler.codec.http.DefaultFullHttpResponse in project netty by netty.
the class HelloWorldHttp1Handler method channelRead0.
@Override
public void channelRead0(ChannelHandlerContext ctx, FullHttpRequest req) throws Exception {
if (HttpUtil.is100ContinueExpected(req)) {
ctx.write(new DefaultFullHttpResponse(HTTP_1_1, CONTINUE));
}
boolean keepAlive = HttpUtil.isKeepAlive(req);
ByteBuf content = ctx.alloc().buffer();
content.writeBytes(HelloWorldHttp2Handler.RESPONSE_BYTES.duplicate());
ByteBufUtil.writeAscii(content, " - via " + req.protocolVersion() + " (" + establishApproach + ")");
FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, content);
response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8");
response.headers().setInt(CONTENT_LENGTH, response.content().readableBytes());
if (!keepAlive) {
ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
} else {
response.headers().set(CONNECTION, HttpHeaderValues.KEEP_ALIVE);
ctx.writeAndFlush(response);
}
}
use of io.netty.handler.codec.http.DefaultFullHttpResponse in project netty by netty.
the class RtspEncoderTest method testSend200OkResponseWithBody.
/**
* Test of a 200 OK response, with body.
*/
@Test
public void testSend200OkResponseWithBody() {
String expected = "RTSP/1.0 200 OK\r\n" + "server: Testserver\r\n" + "session: 2547019973447939919\r\n" + "content-type: text/parameters\r\n" + "content-length: 50\r\n" + "cseq: 3\r\n" + "\r\n" + "position: 24\r\n" + "stream_state: playing\r\n" + "scale: 1.00\r\n";
byte[] content = ("position: 24\r\n" + "stream_state: playing\r\n" + "scale: 1.00\r\n").getBytes(CharsetUtil.UTF_8);
FullHttpResponse response = new DefaultFullHttpResponse(RtspVersions.RTSP_1_0, RtspResponseStatuses.OK);
response.headers().add(RtspHeaderNames.SERVER, "Testserver");
response.headers().add(RtspHeaderNames.SESSION, "2547019973447939919");
response.headers().add(RtspHeaderNames.CONTENT_TYPE, "text/parameters");
response.headers().add(RtspHeaderNames.CONTENT_LENGTH, "" + content.length);
response.headers().add(RtspHeaderNames.CSEQ, "3");
response.content().writeBytes(content);
EmbeddedChannel ch = new EmbeddedChannel(new RtspEncoder());
ch.writeOutbound(response);
ByteBuf buf = ch.readOutbound();
String actual = buf.toString(CharsetUtil.UTF_8);
buf.release();
assertEquals(expected, actual);
}
use of io.netty.handler.codec.http.DefaultFullHttpResponse in project netty by netty.
the class CorsHandler method handlePreflight.
private void handlePreflight(final ChannelHandlerContext ctx, final HttpRequest request) {
final HttpResponse response = new DefaultFullHttpResponse(request.protocolVersion(), OK, true, true);
if (setOrigin(response)) {
setAllowMethods(response);
setAllowHeaders(response);
setAllowCredentials(response);
setMaxAge(response);
setPreflightHeaders(response);
}
release(request);
respond(ctx, request, response);
}
use of io.netty.handler.codec.http.DefaultFullHttpResponse in project netty by netty.
the class CorsHandler method forbidden.
private static void forbidden(final ChannelHandlerContext ctx, final HttpRequest request) {
release(request);
respond(ctx, request, new DefaultFullHttpResponse(request.protocolVersion(), FORBIDDEN));
}
use of io.netty.handler.codec.http.DefaultFullHttpResponse in project netty by netty.
the class HttpSnoopServerHandler method writeResponse.
private boolean writeResponse(HttpObject currentObj, ChannelHandlerContext ctx) {
// Decide whether to close the connection or not.
boolean keepAlive = HttpUtil.isKeepAlive(request);
// Build the response object.
FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, currentObj.decoderResult().isSuccess() ? OK : BAD_REQUEST, Unpooled.copiedBuffer(buf.toString(), CharsetUtil.UTF_8));
response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain; charset=UTF-8");
if (keepAlive) {
// Add 'Content-Length' header only for a keep-alive connection.
response.headers().setInt(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes());
// Add keep alive header as per:
// - http://www.w3.org/Protocols/HTTP/1.1/draft-ietf-http-v11-spec-01.html#Connection
response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
}
// Encode the cookie.
String cookieString = request.headers().get(HttpHeaderNames.COOKIE);
if (cookieString != null) {
Set<Cookie> cookies = ServerCookieDecoder.STRICT.decode(cookieString);
if (!cookies.isEmpty()) {
// Reset the cookies if necessary.
for (Cookie cookie : cookies) {
response.headers().add(HttpHeaderNames.SET_COOKIE, ServerCookieEncoder.STRICT.encode(cookie));
}
}
} else {
// Browser sent no cookie. Add some.
response.headers().add(HttpHeaderNames.SET_COOKIE, ServerCookieEncoder.STRICT.encode("key1", "value1"));
response.headers().add(HttpHeaderNames.SET_COOKIE, ServerCookieEncoder.STRICT.encode("key2", "value2"));
}
// Write the response.
ctx.write(response);
return keepAlive;
}
Aggregations