use of io.netty.handler.codec.http.HttpResponse in project elasticsearch by elastic.
the class Netty4HttpChannelTests method testCorsEnabledWithoutAllowOrigins.
public void testCorsEnabledWithoutAllowOrigins() {
// Set up a HTTP transport with only the CORS enabled setting
Settings settings = Settings.builder().put(HttpTransportSettings.SETTING_CORS_ENABLED.getKey(), true).build();
HttpResponse response = executeRequest(settings, "remote-host", "request-host");
// inspect response and validate
assertThat(response.headers().get(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN), nullValue());
}
use of io.netty.handler.codec.http.HttpResponse in project jersey by jersey.
the class JerseyClientHandler method channelRead0.
@Override
public void channelRead0(ChannelHandlerContext ctx, HttpObject msg) {
if (msg instanceof HttpResponse) {
final HttpResponse response = (HttpResponse) msg;
final ClientResponse jerseyResponse = new ClientResponse(new Response.StatusType() {
@Override
public int getStatusCode() {
return response.status().code();
}
@Override
public Response.Status.Family getFamily() {
return Response.Status.Family.familyOf(response.status().code());
}
@Override
public String getReasonPhrase() {
return response.status().reasonPhrase();
}
}, jerseyRequest);
for (Map.Entry<String, String> entry : response.headers().entries()) {
jerseyResponse.getHeaders().add(entry.getKey(), entry.getValue());
}
// request entity handling.
if ((response.headers().contains(HttpHeaderNames.CONTENT_LENGTH) && HttpUtil.getContentLength(response) > 0) || HttpUtil.isTransferEncodingChunked(response)) {
ctx.channel().closeFuture().addListener(new GenericFutureListener<Future<? super Void>>() {
@Override
public void operationComplete(Future<? super Void> future) throws Exception {
isList.add(NettyInputStream.END_OF_INPUT_ERROR);
}
});
jerseyResponse.setEntityStream(new NettyInputStream(isList));
} else {
jerseyResponse.setEntityStream(new InputStream() {
@Override
public int read() throws IOException {
return -1;
}
});
}
if (asyncConnectorCallback != null) {
connector.executorService.execute(new Runnable() {
@Override
public void run() {
asyncConnectorCallback.response(jerseyResponse);
future.complete(jerseyResponse);
}
});
}
}
if (msg instanceof HttpContent) {
HttpContent httpContent = (HttpContent) msg;
ByteBuf content = httpContent.content();
if (content.isReadable()) {
// copy bytes - when netty reads last chunk, it automatically closes the channel, which invalidates all
// relates ByteBuffs.
byte[] bytes = new byte[content.readableBytes()];
content.getBytes(content.readerIndex(), bytes);
isList.add(new ByteArrayInputStream(bytes));
}
if (msg instanceof LastHttpContent) {
isList.add(NettyInputStream.END_OF_INPUT);
}
}
}
use of io.netty.handler.codec.http.HttpResponse in project netty-socketio by mrniko.
the class EncoderHandler method write.
private void write(XHROptionsMessage msg, ChannelHandlerContext ctx, ChannelPromise promise) {
HttpResponse res = new DefaultHttpResponse(HTTP_1_1, HttpResponseStatus.OK);
res.headers().add(HttpHeaderNames.SET_COOKIE, "io=" + msg.getSessionId()).add(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE).add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_HEADERS, HttpHeaderNames.CONTENT_TYPE);
String origin = ctx.channel().attr(ORIGIN).get();
addOriginHeaders(origin, res);
ByteBuf out = encoder.allocateBuffer(ctx.alloc());
sendMessage(msg, ctx.channel(), out, res, promise);
}
use of io.netty.handler.codec.http.HttpResponse in project netty-socketio by mrniko.
the class EncoderHandler method sendMessage.
private void sendMessage(HttpMessage msg, Channel channel, ByteBuf out, String type, ChannelPromise promise, HttpResponseStatus status) {
HttpResponse res = new DefaultHttpResponse(HTTP_1_1, status);
res.headers().add(HttpHeaderNames.CONTENT_TYPE, type).add(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
if (msg.getSessionId() != null) {
res.headers().add(HttpHeaderNames.SET_COOKIE, "io=" + msg.getSessionId());
}
String origin = channel.attr(ORIGIN).get();
addOriginHeaders(origin, res);
HttpUtil.setContentLength(res, out.readableBytes());
// prevent XSS warnings on IE
// https://github.com/LearnBoost/socket.io/pull/1333
String userAgent = channel.attr(EncoderHandler.USER_AGENT).get();
if (userAgent != null && (userAgent.contains(";MSIE") || userAgent.contains("Trident/"))) {
res.headers().add("X-XSS-Protection", "0");
}
sendMessage(msg, channel, out, res, promise);
}
use of io.netty.handler.codec.http.HttpResponse in project netty-socketio by mrniko.
the class PollingTransport method sendError.
private void sendError(ChannelHandlerContext ctx) {
HttpResponse res = new DefaultHttpResponse(HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR);
ctx.channel().writeAndFlush(res).addListener(ChannelFutureListener.CLOSE);
}
Aggregations