2014-04-23 04:25:10 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2014 The Netty Project
|
|
|
|
*
|
|
|
|
* The Netty Project licenses this file to you under the Apache License,
|
|
|
|
* version 2.0 (the "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at:
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*/
|
|
|
|
package io.netty.handler.codec.haproxy;
|
|
|
|
|
2014-06-21 06:54:18 +02:00
|
|
|
import static io.netty.handler.codec.haproxy.HAProxyConstants.*;
|
|
|
|
|
2014-04-23 04:25:10 +02:00
|
|
|
/**
|
2014-06-21 06:54:18 +02:00
|
|
|
* The HAProxy proxy protocol specification version.
|
2014-04-23 04:25:10 +02:00
|
|
|
*/
|
2014-06-21 06:54:18 +02:00
|
|
|
public enum HAProxyProtocolVersion {
|
2014-04-23 04:25:10 +02:00
|
|
|
/**
|
2014-06-21 06:54:18 +02:00
|
|
|
* The ONE proxy protocol version represents a version 1 (human-readable) header.
|
2014-04-23 04:25:10 +02:00
|
|
|
*/
|
2014-06-21 06:54:18 +02:00
|
|
|
V1(VERSION_ONE_BYTE),
|
2014-04-23 04:25:10 +02:00
|
|
|
/**
|
2014-06-21 06:54:18 +02:00
|
|
|
* The TWO proxy protocol version represents a version 2 (binary) header.
|
2014-04-23 04:25:10 +02:00
|
|
|
*/
|
2014-06-21 06:54:18 +02:00
|
|
|
V2(VERSION_TWO_BYTE);
|
2014-04-23 04:25:10 +02:00
|
|
|
|
|
|
|
/**
|
2014-06-21 06:54:18 +02:00
|
|
|
* The highest 4 bits of the protocol version and command byte contain the version
|
2014-04-23 04:25:10 +02:00
|
|
|
*/
|
2014-06-21 06:54:18 +02:00
|
|
|
private static final byte VERSION_MASK = (byte) 0xf0;
|
2014-04-23 04:25:10 +02:00
|
|
|
|
2014-06-21 06:54:18 +02:00
|
|
|
private final byte byteValue;
|
2014-04-23 04:25:10 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new instance
|
|
|
|
*/
|
2014-06-21 06:54:18 +02:00
|
|
|
HAProxyProtocolVersion(byte byteValue) {
|
|
|
|
this.byteValue = byteValue;
|
2014-04-23 04:25:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-06-21 06:54:18 +02:00
|
|
|
* Returns the {@link HAProxyProtocolVersion} represented by the higest 4 bits of the specified byte.
|
2014-04-23 04:25:10 +02:00
|
|
|
*
|
2014-06-21 06:54:18 +02:00
|
|
|
* @param verCmdByte protocol version and command byte
|
2014-04-23 04:25:10 +02:00
|
|
|
*/
|
|
|
|
public static HAProxyProtocolVersion valueOf(byte verCmdByte) {
|
2014-06-21 06:54:18 +02:00
|
|
|
int version = verCmdByte & VERSION_MASK;
|
|
|
|
switch ((byte) version) {
|
|
|
|
case VERSION_TWO_BYTE:
|
|
|
|
return V2;
|
|
|
|
case VERSION_ONE_BYTE:
|
|
|
|
return V1;
|
2014-04-23 04:25:10 +02:00
|
|
|
default:
|
2014-06-21 06:54:18 +02:00
|
|
|
throw new IllegalArgumentException("unknown version: " + version);
|
2014-04-23 04:25:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-06-21 06:54:18 +02:00
|
|
|
* Returns the byte value of this version.
|
2014-04-23 04:25:10 +02:00
|
|
|
*/
|
|
|
|
public byte byteValue() {
|
2014-06-21 06:54:18 +02:00
|
|
|
return byteValue;
|
2014-04-23 04:25:10 +02:00
|
|
|
}
|
|
|
|
}
|