Search in sources :

Example 46 with HttpResponse

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);
}
Also used : DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse) ByteBuf(io.netty.buffer.ByteBuf)

Example 47 with HttpResponse

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);
}
Also used : DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse)

Example 48 with HttpResponse

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);
}
Also used : DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse)

Example 49 with HttpResponse

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");
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse) HttpRequest(io.netty.handler.codec.http.HttpRequest) HttpResponse(io.netty.handler.codec.http.HttpResponse) KeepAliveStrategy(org.asynchttpclient.channel.KeepAliveStrategy) Test(org.testng.annotations.Test) HttpTest(org.asynchttpclient.testserver.HttpTest)

Example 50 with HttpResponse

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;
    }
}
Also used : HttpObject(io.netty.handler.codec.http.HttpObject) HttpResponse(io.netty.handler.codec.http.HttpResponse) IOException(java.io.IOException) HttpContent(io.netty.handler.codec.http.HttpContent) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) IOException(java.io.IOException)

Aggregations

HttpResponse (io.netty.handler.codec.http.HttpResponse)127 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)57 Test (org.junit.Test)50 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)30 HttpRequest (io.netty.handler.codec.http.HttpRequest)27 DefaultHttpResponse (io.netty.handler.codec.http.DefaultHttpResponse)26 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)24 HttpContent (io.netty.handler.codec.http.HttpContent)18 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)18 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)14 ChannelFuture (io.netty.channel.ChannelFuture)12 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)12 ByteBuf (io.netty.buffer.ByteBuf)10 AsciiString (io.netty.util.AsciiString)9 HttpObject (io.netty.handler.codec.http.HttpObject)7 Map (java.util.Map)7 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)7 WebSocketExtensionData (io.netty.handler.codec.http.websocketx.extensions.WebSocketExtensionData)6 IOException (java.io.IOException)6 Settings (org.elasticsearch.common.settings.Settings)6