Search in sources :

Example 46 with FullHttpResponse

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpResponse in project reactor-netty by reactor.

the class WebsocketClientOperations method onInboundNext.

@Override
@SuppressWarnings("FutureReturnValueIgnored")
public void onInboundNext(ChannelHandlerContext ctx, Object msg) {
    if (msg instanceof FullHttpResponse) {
        started = true;
        channel().pipeline().remove(HttpObjectAggregator.class);
        FullHttpResponse response = (FullHttpResponse) msg;
        setNettyResponse(response);
        if (notRedirected(response)) {
            try {
                handshaker.finishHandshake(channel(), response);
                // This change is needed after the Netty change https://github.com/netty/netty/pull/11966
                ctx.read();
                listener().onStateChange(this, HttpClientState.RESPONSE_RECEIVED);
            } catch (Exception e) {
                onInboundError(e);
                // "FutureReturnValueIgnored" this is deliberate
                ctx.close();
            } finally {
                // Release unused content (101 status)
                response.content().release();
            }
        } else {
            response.content().release();
            listener().onUncaughtException(this, redirecting);
        }
        return;
    }
    if (!this.proxyPing && msg instanceof PingWebSocketFrame) {
        // "FutureReturnValueIgnored" this is deliberate
        ctx.writeAndFlush(new PongWebSocketFrame(((PingWebSocketFrame) msg).content()));
        ctx.read();
        return;
    }
    if (msg instanceof CloseWebSocketFrame && ((CloseWebSocketFrame) msg).isFinalFragment()) {
        if (log.isDebugEnabled()) {
            log.debug(format(channel(), "CloseWebSocketFrame detected. Closing Websocket"));
        }
        CloseWebSocketFrame closeFrame = new CloseWebSocketFrame(true, ((CloseWebSocketFrame) msg).rsv(), ((CloseWebSocketFrame) msg).content());
        if (closeFrame.statusCode() != -1) {
            sendCloseNow(closeFrame);
        } else {
            sendCloseNow(closeFrame, WebSocketCloseStatus.EMPTY);
        }
        onInboundComplete();
    } else if (msg != LastHttpContent.EMPTY_LAST_CONTENT) {
        super.onInboundNext(ctx, msg);
    }
}
Also used : CloseWebSocketFrame(io.netty.handler.codec.http.websocketx.CloseWebSocketFrame) PongWebSocketFrame(io.netty.handler.codec.http.websocketx.PongWebSocketFrame) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) PingWebSocketFrame(io.netty.handler.codec.http.websocketx.PingWebSocketFrame) WebSocketClientHandshakeException(io.netty.handler.codec.http.websocketx.WebSocketClientHandshakeException)

Example 47 with FullHttpResponse

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpResponse in project proxyee-down by monkeyWie.

the class EmbedHttpServer method invoke.

// 根据请求uri找到对应的处理类方法执行
public FullHttpResponse invoke(String uri, Channel channel, FullHttpRequest request) throws Exception {
    if (controllerList != null) {
        for (Object obj : controllerList) {
            Class<?> clazz = obj.getClass();
            RequestMapping mapping = clazz.getAnnotation(RequestMapping.class);
            if (mapping != null) {
                String mappingUri = fixUri(mapping.value()[0]);
                for (Method actionMethod : clazz.getMethods()) {
                    RequestMapping subMapping = actionMethod.getAnnotation(RequestMapping.class);
                    if (subMapping != null) {
                        String subMappingUri = fixUri(subMapping.value()[0]);
                        if (uri.equalsIgnoreCase(mappingUri + subMappingUri)) {
                            return (FullHttpResponse) actionMethod.invoke(obj, channel, request);
                        }
                    }
                }
            }
        }
    }
    return defaultController.handle(channel, request);
}
Also used : FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) Method(java.lang.reflect.Method) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 48 with FullHttpResponse

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpResponse in project proxyee-down by monkeyWie.

the class PacController method build.

@RequestMapping("pdown.pac")
public FullHttpResponse build(Channel channel, FullHttpRequest request) throws Exception {
    Set<String> domains = ExtensionContent.getProxyWildCards();
    String pacContent = PAC_TEMPLATE.replace("{port}", DownApplication.INSTANCE.PROXY_PORT + "");
    if (domains != null && domains.size() > 0) {
        StringBuilder domainsBuilder = new StringBuilder();
        for (String domain : domains) {
            if (domainsBuilder.length() != 0) {
                domainsBuilder.append(",");
            }
            domainsBuilder.append("'" + domain + "'");
        }
        pacContent = pacContent.replace("{domains}", domainsBuilder.toString());
    } else {
        pacContent = pacContent.replace("{domains}", "");
    }
    FullHttpResponse httpResponse = HttpHandlerUtil.buildContent(pacContent, "application/x-ns-proxy-autoconfig");
    httpResponse.headers().set(HttpHeaderNames.CACHE_CONTROL, HttpHeaderValues.NO_CACHE);
    httpResponse.headers().set(HttpHeaderNames.PRAGMA, HttpHeaderValues.NO_CACHE);
    httpResponse.headers().set(HttpHeaderNames.EXPIRES, 0);
    return httpResponse;
}
Also used : FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 49 with FullHttpResponse

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpResponse in project proxyee-down by monkeyWie.

the class HttpHandlerUtil method buildJson.

public static FullHttpResponse buildJson(Object obj, Include include) {
    FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
    response.headers().set(HttpHeaderNames.CONTENT_TYPE, AsciiString.cached("application/json; charset=utf-8"));
    if (obj != null) {
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            if (include != null) {
                objectMapper.setSerializationInclusion(include);
            }
            String content = objectMapper.writeValueAsString(obj);
            response.content().writeBytes(content.getBytes(Charset.forName("utf-8")));
        } catch (JsonProcessingException e) {
            response.setStatus(HttpResponseStatus.SERVICE_UNAVAILABLE);
        }
    }
    response.headers().set(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes());
    return response;
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) AsciiString(io.netty.util.AsciiString) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 50 with FullHttpResponse

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpResponse in project ballerina by ballerina-lang.

the class WebSocketClientHandler method channelRead0.

@Override
public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
    Channel ch = ctx.channel();
    if (!handshaker.isHandshakeComplete()) {
        handshaker.finishHandshake(ch, (FullHttpResponse) msg);
        logger.info("WebSocket Client connected!");
        handshakeFuture.setSuccess();
        isOpen = true;
        return;
    }
    if (msg instanceof FullHttpResponse) {
        FullHttpResponse response = (FullHttpResponse) msg;
        throw new IllegalStateException("Unexpected FullHttpResponse (getStatus=" + response.status() + ", content=" + response.content().toString(CharsetUtil.UTF_8) + ')');
    }
    WebSocketFrame frame = (WebSocketFrame) msg;
    if (frame instanceof TextWebSocketFrame) {
        TextWebSocketFrame textFrame = (TextWebSocketFrame) frame;
        textReceived = textFrame.text();
    } else if (frame instanceof BinaryWebSocketFrame) {
        BinaryWebSocketFrame binaryFrame = (BinaryWebSocketFrame) frame;
        bufferReceived = binaryFrame.content().nioBuffer();
    } else if (frame instanceof PingWebSocketFrame) {
        PingWebSocketFrame pingFrame = (PingWebSocketFrame) frame;
        isPing = true;
        bufferReceived = pingFrame.content().nioBuffer();
    } else if (frame instanceof PongWebSocketFrame) {
        PongWebSocketFrame pongFrame = (PongWebSocketFrame) frame;
        isPong = true;
        bufferReceived = pongFrame.content().nioBuffer();
    } else if (frame instanceof CloseWebSocketFrame) {
        ch.close().sync();
        isOpen = false;
    }
}
Also used : CloseWebSocketFrame(io.netty.handler.codec.http.websocketx.CloseWebSocketFrame) PongWebSocketFrame(io.netty.handler.codec.http.websocketx.PongWebSocketFrame) Channel(io.netty.channel.Channel) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) BinaryWebSocketFrame(io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) WebSocketFrame(io.netty.handler.codec.http.websocketx.WebSocketFrame) PingWebSocketFrame(io.netty.handler.codec.http.websocketx.PingWebSocketFrame) BinaryWebSocketFrame(io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) PongWebSocketFrame(io.netty.handler.codec.http.websocketx.PongWebSocketFrame) CloseWebSocketFrame(io.netty.handler.codec.http.websocketx.CloseWebSocketFrame) PingWebSocketFrame(io.netty.handler.codec.http.websocketx.PingWebSocketFrame)

Aggregations

FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)261 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)174 ByteBuf (io.netty.buffer.ByteBuf)53 Test (org.junit.Test)50 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)38 HttpRequest (io.netty.handler.codec.http.HttpRequest)34 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)24 HttpObject (io.netty.handler.codec.http.HttpObject)23 IOException (java.io.IOException)23 HttpTrade (org.jocean.http.server.HttpServerBuilder.HttpTrade)23 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)22 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)21 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)21 Test (org.junit.jupiter.api.Test)21 ChannelFuture (io.netty.channel.ChannelFuture)20 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)20 HttpResponse (io.netty.handler.codec.http.HttpResponse)20 HttpInitiator (org.jocean.http.client.HttpClient.HttpInitiator)20 Subscription (rx.Subscription)20 LocalAddress (io.netty.channel.local.LocalAddress)19