Home / Class/ HttpProxyServer Class — netty Architecture

HttpProxyServer Class — netty Architecture

Architecture documentation for the HttpProxyServer class in HttpProxyServer.java from the netty codebase.

Entity Profile

Dependency Diagram

graph TD
  7398b183_c32e_55ec_e1e5_f9d148e814ea["HttpProxyServer"]
  28810160_ce2b_65e9_e2bc_c0ffa3db9787["HttpProxyServer.java"]
  7398b183_c32e_55ec_e1e5_f9d148e814ea -->|defined in| 28810160_ce2b_65e9_e2bc_c0ffa3db9787
  c4bd6483_fb4f_8761_5174_bb31e0c06d04["HttpProxyServer()"]
  7398b183_c32e_55ec_e1e5_f9d148e814ea -->|method| c4bd6483_fb4f_8761_5174_bb31e0c06d04
  7b2546f4_db7a_8eb1_30b8_38389fbb8e7c["configure()"]
  7398b183_c32e_55ec_e1e5_f9d148e814ea -->|method| 7b2546f4_db7a_8eb1_30b8_38389fbb8e7c
  53be49a0_9a3d_011c_81ec_41cafe0e8353["authenticate()"]
  7398b183_c32e_55ec_e1e5_f9d148e814ea -->|method| 53be49a0_9a3d_011c_81ec_41cafe0e8353

Relationship Graph

Source Code

handler-proxy/src/test/java/io/netty/handler/proxy/HttpProxyServer.java lines 44–167

final class HttpProxyServer extends ProxyServer {

    HttpProxyServer(boolean useSsl, TestMode testMode, InetSocketAddress destination) {
        super(useSsl, testMode, destination);
    }

    HttpProxyServer(
            boolean useSsl, TestMode testMode, InetSocketAddress destination, String username, String password) {
        super(useSsl, testMode, destination, username, password);
    }

    @Override
    protected void configure(SocketChannel ch) throws Exception {
        ChannelPipeline p = ch.pipeline();
        switch (testMode) {
        case INTERMEDIARY:
            p.addLast(new HttpServerCodec());
            p.addLast(new HttpObjectAggregator(1));
            p.addLast(new HttpIntermediaryHandler());
            break;
        case TERMINAL:
            p.addLast(new HttpServerCodec());
            p.addLast(new HttpObjectAggregator(1));
            p.addLast(new HttpTerminalHandler());
            break;
        case UNRESPONSIVE:
            p.addLast(UnresponsiveHandler.INSTANCE);
            break;
        }
    }

    private boolean authenticate(ChannelHandlerContext ctx, FullHttpRequest req) {
        assertEquals(HttpMethod.CONNECT, req.method());

        if (testMode != TestMode.INTERMEDIARY) {
            ctx.pipeline().addBefore(ctx.name(), "lineDecoder", new LineBasedFrameDecoder(64, false, true));
        }

        ctx.pipeline().remove(HttpObjectAggregator.class);
        ctx.pipeline().get(HttpServerCodec.class).removeInboundHandler();

        boolean authzSuccess = false;
        if (username != null) {
            CharSequence authz = req.headers().get(HttpHeaderNames.PROXY_AUTHORIZATION);
            if (authz != null) {
                String[] authzParts = authz.toString().split(" ", 2);
                ByteBuf authzBuf64 = Unpooled.copiedBuffer(authzParts[1], CharsetUtil.US_ASCII);
                ByteBuf authzBuf = Base64.decode(authzBuf64);

                String expectedAuthz = username + ':' + password;
                authzSuccess = "Basic".equals(authzParts[0]) &&
                               expectedAuthz.equals(authzBuf.toString(CharsetUtil.US_ASCII));

                authzBuf64.release();
                authzBuf.release();
            }
        } else {
            authzSuccess = true;
        }

        return authzSuccess;
    }

    private final class HttpIntermediaryHandler extends IntermediaryHandler {

        private SocketAddress intermediaryDestination;

        @Override
        protected boolean handleProxyProtocol(ChannelHandlerContext ctx, Object msg) throws Exception {
            FullHttpRequest req = (FullHttpRequest) msg;
            FullHttpResponse res;
            if (!authenticate(ctx, req)) {
                res = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.UNAUTHORIZED);
                res.headers().set(HttpHeaderNames.CONTENT_LENGTH, 0);
            } else {
                res = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
                String uri = req.uri();
                int lastColonPos = uri.lastIndexOf(':');
                assertThat(lastColonPos).isGreaterThan(0);
                intermediaryDestination = SocketUtils.socketAddress(
                        uri.substring(0, lastColonPos), Integer.parseInt(uri.substring(lastColonPos + 1)));

Frequently Asked Questions

What is the HttpProxyServer class?
HttpProxyServer is a class in the netty codebase, defined in handler-proxy/src/test/java/io/netty/handler/proxy/HttpProxyServer.java.
Where is HttpProxyServer defined?
HttpProxyServer is defined in handler-proxy/src/test/java/io/netty/handler/proxy/HttpProxyServer.java at line 44.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free