Home / Class/ PortUnificationServerHandler Class — netty Architecture

PortUnificationServerHandler Class — netty Architecture

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

Entity Profile

Dependency Diagram

graph TD
  0035cd98_439e_cdb1_2619_abb4aba4a268["PortUnificationServerHandler"]
  ff0cb6ff_073c_6c54_4159_5febe3217634["PortUnificationServerHandler.java"]
  0035cd98_439e_cdb1_2619_abb4aba4a268 -->|defined in| ff0cb6ff_073c_6c54_4159_5febe3217634
  501de240_e1cb_94f8_c582_2faaf20d2729["PortUnificationServerHandler()"]
  0035cd98_439e_cdb1_2619_abb4aba4a268 -->|method| 501de240_e1cb_94f8_c582_2faaf20d2729
  46a8a557_e9f2_75bd_144b_c3fb8b8fd5d6["decode()"]
  0035cd98_439e_cdb1_2619_abb4aba4a268 -->|method| 46a8a557_e9f2_75bd_144b_c3fb8b8fd5d6
  8efb1e2d_d4a3_dfe6_6952_9c2c3ed410e3["isSsl()"]
  0035cd98_439e_cdb1_2619_abb4aba4a268 -->|method| 8efb1e2d_d4a3_dfe6_6952_9c2c3ed410e3
  c115e1be_c004_4723_1d8d_ff6eac5b5eca["isGzip()"]
  0035cd98_439e_cdb1_2619_abb4aba4a268 -->|method| c115e1be_c004_4723_1d8d_ff6eac5b5eca
  54693d02_409a_0835_0870_dca0e376bca9["isHttp()"]
  0035cd98_439e_cdb1_2619_abb4aba4a268 -->|method| 54693d02_409a_0835_0870_dca0e376bca9
  99396a68_6df1_d1c5_cc6c_1872bdcc9221["isFactorial()"]
  0035cd98_439e_cdb1_2619_abb4aba4a268 -->|method| 99396a68_6df1_d1c5_cc6c_1872bdcc9221
  f41ecb92_a386_7e45_1870_e2126e8202ac["enableSsl()"]
  0035cd98_439e_cdb1_2619_abb4aba4a268 -->|method| f41ecb92_a386_7e45_1870_e2126e8202ac
  48a9eded_6f9c_3cad_72d3_82ff38ffecf1["enableGzip()"]
  0035cd98_439e_cdb1_2619_abb4aba4a268 -->|method| 48a9eded_6f9c_3cad_72d3_82ff38ffecf1
  640d12e2_2b98_1048_fac3_cf3977c9655d["switchToHttp()"]
  0035cd98_439e_cdb1_2619_abb4aba4a268 -->|method| 640d12e2_2b98_1048_fac3_cf3977c9655d
  20fe5679_2b0a_75ac_82c8_fa37abbd573d["switchToFactorial()"]
  0035cd98_439e_cdb1_2619_abb4aba4a268 -->|method| 20fe5679_2b0a_75ac_82c8_fa37abbd573d

Relationship Graph

Source Code

example/src/main/java/io/netty/example/portunification/PortUnificationServerHandler.java lines 41–147

public class PortUnificationServerHandler extends ByteToMessageDecoder {

    private static final int MAX_CONTENT_LENGTH = 65536;

    private final SslContext sslCtx;
    private final boolean detectSsl;
    private final boolean detectGzip;

    public PortUnificationServerHandler(SslContext sslCtx) {
        this(sslCtx, true, true);
    }

    private PortUnificationServerHandler(SslContext sslCtx, boolean detectSsl, boolean detectGzip) {
        this.sslCtx = sslCtx;
        this.detectSsl = detectSsl;
        this.detectGzip = detectGzip;
    }

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
        // Will use the first five bytes to detect a protocol.
        if (in.readableBytes() < 5) {
            return;
        }

        if (isSsl(in)) {
            enableSsl(ctx);
        } else {
            final int magic1 = in.getUnsignedByte(in.readerIndex());
            final int magic2 = in.getUnsignedByte(in.readerIndex() + 1);
            if (isGzip(magic1, magic2)) {
                enableGzip(ctx);
            } else if (isHttp(magic1, magic2)) {
                switchToHttp(ctx);
            } else if (isFactorial(magic1)) {
                switchToFactorial(ctx);
            } else {
                // Unknown protocol; discard everything and close the connection.
                in.clear();
                ctx.close();
            }
        }
    }

    private boolean isSsl(ByteBuf buf) {
        if (detectSsl) {
            return SslHandler.isEncrypted(buf, false);
        }
        return false;
    }

    private boolean isGzip(int magic1, int magic2) {
        if (detectGzip) {
            return magic1 == 31 && magic2 == 139;
        }
        return false;
    }

    private static boolean isHttp(int magic1, int magic2) {
        return
            magic1 == 'G' && magic2 == 'E' || // GET
            magic1 == 'P' && magic2 == 'O' || // POST
            magic1 == 'P' && magic2 == 'U' || // PUT
            magic1 == 'H' && magic2 == 'E' || // HEAD
            magic1 == 'O' && magic2 == 'P' || // OPTIONS
            magic1 == 'P' && magic2 == 'A' || // PATCH
            magic1 == 'D' && magic2 == 'E' || // DELETE
            magic1 == 'T' && magic2 == 'R' || // TRACE
            magic1 == 'C' && magic2 == 'O';   // CONNECT
    }

    private static boolean isFactorial(int magic1) {
        return magic1 == 'F';
    }

    private void enableSsl(ChannelHandlerContext ctx) {
        ChannelPipeline p = ctx.pipeline();
        p.addLast("ssl", sslCtx.newHandler(ctx.alloc()));
        p.addLast("unificationA", new PortUnificationServerHandler(sslCtx, false, detectGzip));
        p.remove(this);
    }

Frequently Asked Questions

What is the PortUnificationServerHandler class?
PortUnificationServerHandler is a class in the netty codebase, defined in example/src/main/java/io/netty/example/portunification/PortUnificationServerHandler.java.
Where is PortUnificationServerHandler defined?
PortUnificationServerHandler is defined in example/src/main/java/io/netty/example/portunification/PortUnificationServerHandler.java at line 41.

Analyze Your Own Codebase

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

Try Supermodel Free