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);
}
use of io.netty.handler.codec.http.HttpResponse in project async-http-client by AsyncHttpClient.
the class BasicHttpsTest method multipleConcurrentPostRequestsOverHttpsWithDisabledKeepAliveStrategy.
@Test
public void multipleConcurrentPostRequestsOverHttpsWithDisabledKeepAliveStrategy() throws Throwable {
logger.debug(">>> multipleConcurrentPostRequestsOverHttpsWithDisabledKeepAliveStrategy");
KeepAliveStrategy keepAliveStrategy = new KeepAliveStrategy() {
@Override
public boolean keepAlive(Request ahcRequest, HttpRequest nettyRequest, HttpResponse nettyResponse) {
return !ahcRequest.getUri().isSecured();
}
};
withClient(config().setSslEngineFactory(createSslEngineFactory()).setKeepAliveStrategy(keepAliveStrategy)).run(client -> {
withServer(server).run(server -> {
server.enqueueEcho();
server.enqueueEcho();
server.enqueueEcho();
String body = "hello there";
client.preparePost(getTargetUrl()).setBody(body).setHeader(CONTENT_TYPE, "text/html").execute();
client.preparePost(getTargetUrl()).setBody(body).setHeader(CONTENT_TYPE, "text/html").execute();
Response response = client.preparePost(getTargetUrl()).setBody(body).setHeader(CONTENT_TYPE, "text/html").execute().get();
assertEquals(response.getResponseBody(), body);
});
});
logger.debug("<<< multipleConcurrentPostRequestsOverHttpsWithDisabledKeepAliveStrategy");
}
use of io.netty.handler.codec.http.HttpResponse in project async-http-client by AsyncHttpClient.
the class HttpHandler method handleRead.
@Override
public void handleRead(final Channel channel, final NettyResponseFuture<?> future, final Object e) throws Exception {
// future is already done because of an exception or a timeout
if (future.isDone()) {
// FIXME isn't the channel already properly closed?
channelManager.closeChannel(channel);
return;
}
AsyncHandler<?> handler = future.getAsyncHandler();
try {
if (e instanceof HttpObject) {
HttpObject object = (HttpObject) e;
Throwable t = object.decoderResult().cause();
if (t != null) {
readFailed(channel, future, t);
return;
}
}
if (e instanceof HttpResponse) {
handleHttpResponse((HttpResponse) e, channel, future, handler);
} else if (e instanceof HttpContent) {
handleChunk((HttpContent) e, channel, future, handler);
}
} catch (Exception t) {
// next request
if (//
hasIOExceptionFilters && //
t instanceof IOException && requestSender.applyIoExceptionFiltersAndReplayRequest(future, IOException.class.cast(t), channel)) {
return;
}
readFailed(channel, future, t);
throw t;
}
}
Aggregations