1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package io.netty.handler.codec.haproxy;
18
19 import io.netty.buffer.ByteBuf;
20 import io.netty.buffer.DefaultByteBufHolder;
21 import io.netty.util.internal.StringUtil;
22
23 import static io.netty.util.internal.ObjectUtil.*;
24
25
26
27
28
29
30
31 public class HAProxyTLV extends DefaultByteBufHolder {
32
33 private final Type type;
34 private final byte typeByteValue;
35
36
37
38
39
40 int totalNumBytes() {
41 return 3 + contentNumBytes();
42 }
43
44 int contentNumBytes() {
45 return content().readableBytes();
46 }
47
48
49
50
51 public enum Type {
52 PP2_TYPE_ALPN,
53 PP2_TYPE_AUTHORITY,
54 PP2_TYPE_SSL,
55 PP2_TYPE_SSL_VERSION,
56 PP2_TYPE_SSL_CN,
57 PP2_TYPE_NETNS,
58
59
60
61 OTHER;
62
63
64
65
66
67
68
69
70
71
72 public static Type typeForByteValue(byte byteValue) {
73 switch (byteValue) {
74 case 0x01:
75 return PP2_TYPE_ALPN;
76 case 0x02:
77 return PP2_TYPE_AUTHORITY;
78 case 0x20:
79 return PP2_TYPE_SSL;
80 case 0x21:
81 return PP2_TYPE_SSL_VERSION;
82 case 0x22:
83 return PP2_TYPE_SSL_CN;
84 case 0x30:
85 return PP2_TYPE_NETNS;
86 default:
87 return OTHER;
88 }
89 }
90
91
92
93
94
95
96
97
98 public static byte byteValueForType(Type type) {
99 switch (type) {
100 case PP2_TYPE_ALPN:
101 return 0x01;
102 case PP2_TYPE_AUTHORITY:
103 return 0x02;
104 case PP2_TYPE_SSL:
105 return 0x20;
106 case PP2_TYPE_SSL_VERSION:
107 return 0x21;
108 case PP2_TYPE_SSL_CN:
109 return 0x22;
110 case PP2_TYPE_NETNS:
111 return 0x30;
112 default:
113 throw new IllegalArgumentException("unknown type: " + type);
114 }
115 }
116 }
117
118
119
120
121
122
123
124 public HAProxyTLV(byte typeByteValue, ByteBuf content) {
125 this(Type.typeForByteValue(typeByteValue), typeByteValue, content);
126 }
127
128
129
130
131
132
133
134 public HAProxyTLV(Type type, ByteBuf content) {
135 this(type, Type.byteValueForType(type), content);
136 }
137
138
139
140
141
142
143
144
145 HAProxyTLV(final Type type, final byte typeByteValue, final ByteBuf content) {
146 super(content);
147 this.type = checkNotNull(type, "type");
148 this.typeByteValue = typeByteValue;
149 }
150
151
152
153
154 public Type type() {
155 return type;
156 }
157
158
159
160
161 public byte typeByteValue() {
162 return typeByteValue;
163 }
164
165 @Override
166 public HAProxyTLV copy() {
167 return replace(content().copy());
168 }
169
170 @Override
171 public HAProxyTLV duplicate() {
172 return replace(content().duplicate());
173 }
174
175 @Override
176 public HAProxyTLV retainedDuplicate() {
177 return replace(content().retainedDuplicate());
178 }
179
180 @Override
181 public HAProxyTLV replace(ByteBuf content) {
182 return new HAProxyTLV(type, typeByteValue, content);
183 }
184
185 @Override
186 public HAProxyTLV retain() {
187 super.retain();
188 return this;
189 }
190
191 @Override
192 public HAProxyTLV retain(int increment) {
193 super.retain(increment);
194 return this;
195 }
196
197 @Override
198 public HAProxyTLV touch() {
199 super.touch();
200 return this;
201 }
202
203 @Override
204 public HAProxyTLV touch(Object hint) {
205 super.touch(hint);
206 return this;
207 }
208
209 @Override
210 public String toString() {
211 return StringUtil.simpleClassName(this) +
212 "(type: " + type() +
213 ", typeByteValue: " + typeByteValue() +
214 ", content: " + contentToString() + ')';
215 }
216 }