1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.mimo.netty.handler.codec.icap;
15
16 import java.nio.charset.Charset;
17
18
19
20
21
22
23
24
25
26 public final class IcapCodecUtil {
27
28
29
30
31
32
33 public static final Byte[] IEOF_SEQUENCE = new Byte[]{48,59,32,105,101,111,102};
34
35 public static final byte[] NATIVE_IEOF_SEQUENCE = new byte[]{48,59,32,105,101,111,102};
36
37 public static final String IEOF_SEQUENCE_STRING = "0; ieof";
38
39
40
41
42 public static final byte SPACE = 32;
43
44
45
46
47 public static final byte CR = 13;
48
49
50
51
52 public static final byte LF = 10;
53
54
55
56
57 public static final byte[] CRLF = new byte[] { CR, LF };
58
59
60
61
62 public static final byte COLON = 58;
63
64
65 public static final Charset ASCII_CHARSET = Charset.forName("ASCII");
66
67 public static final String ENCAPSULATION_ELEMENT_REQHDR = "req-hdr";
68 public static final String ENCAPSULATION_ELEMENT_RESHDR = "res-hdr";
69 public static final String ENCAPSULATION_ELEMENT_REQBODY = "req-body";
70 public static final String ENCAPSULATION_ELEMENT_RESBODY = "res-body";
71 public static final String ENCAPSULATION_ELEMENT_OPTBODY = "opt-body";
72 public static final String ENCAPSULATION_ELEMENT_NULLBODY = "null-body";
73
74 private IcapCodecUtil() {
75 }
76
77
78
79
80
81
82 public static void validateHeaderName(String name) {
83 if (name == null) {
84 throw new NullPointerException("name");
85 }
86 for (int i = 0; i < name.length(); i ++) {
87 char caracter = name.charAt(i);
88 if (caracter > 127) {
89 throw new IllegalArgumentException("name contains non-ascii character: " + name);
90 }
91
92
93 switch (caracter) {
94 case '\t': case '\n': case 0x0b: case '\f': case '\r':
95 case ' ': case ',': case ':': case ';': case '=':
96 throw new IllegalArgumentException("name contains one of the following prohibited characters: =,;: \\t\\r\\n\\v\\f: " + name);
97 }
98 }
99 }
100
101
102
103
104
105
106 public static void validateHeaderValue(String value) {
107 if (value == null) {
108 throw new NullPointerException("value");
109 }
110
111
112
113
114 int state = 0;
115
116 for (int i = 0; i < value.length(); i ++) {
117 final char caracter = value.charAt(i);
118
119
120 if(caracter == 0x0b | caracter == '\f') {
121 throw new IllegalArgumentException("value contains a prohibited character " + caracter + ": " + value);
122 }
123
124
125 if(state == 0) {
126 if(caracter == '\r') {
127 state = 1;
128 }
129 if(caracter == '\n') {
130 state = 2;
131 }
132 } else if(state == 1) {
133 if(caracter == '\n') {
134 state = 2;
135 } else {
136 throw new IllegalArgumentException("Only '\\n' is allowed after '\\r': " + value);
137 }
138 } else if(state == 2) {
139 if(caracter == '\t') {
140 state = 0;
141 } else {
142 throw new IllegalArgumentException("Only ' ' and '\\t' are allowed after '\\n': " + value);
143 }
144 }
145 }
146
147 if (state != 0) {
148 throw new IllegalArgumentException("value must not end with '\\r' or '\\n':" + value);
149 }
150 }
151 }