1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.handler.codec.http;
17
18 import io.netty.buffer.ByteBufUtil;
19 import io.netty.util.CharsetUtil;
20 import io.netty.util.internal.ObjectUtil;
21 import io.netty.util.internal.StringUtil;
22
23 import java.net.URI;
24 import java.net.URISyntaxException;
25 import java.net.URLEncoder;
26 import java.nio.charset.Charset;
27
28
29
30
31
32
33
34
35
36
37
38
39
40 public class QueryStringEncoder {
41
42 private final Charset charset;
43 private final StringBuilder uriBuilder;
44 private boolean hasParams;
45 private static final byte WRITE_UTF_UNKNOWN = (byte) '?';
46 private static final char[] CHAR_MAP = "0123456789ABCDEF".toCharArray();
47
48
49
50
51
52 public QueryStringEncoder(String uri) {
53 this(uri, HttpConstants.DEFAULT_CHARSET);
54 }
55
56
57
58
59
60 public QueryStringEncoder(String uri, Charset charset) {
61 ObjectUtil.checkNotNull(charset, "charset");
62 uriBuilder = new StringBuilder(uri);
63 this.charset = CharsetUtil.UTF_8.equals(charset) ? null : charset;
64 }
65
66
67
68
69 public void addParam(String name, String value) {
70 ObjectUtil.checkNotNull(name, "name");
71 if (hasParams) {
72 uriBuilder.append('&');
73 } else {
74 uriBuilder.append('?');
75 hasParams = true;
76 }
77
78 encodeComponent(name);
79 if (value != null) {
80 uriBuilder.append('=');
81 encodeComponent(value);
82 }
83 }
84
85 private void encodeComponent(CharSequence s) {
86 if (charset == null) {
87 encodeUtf8Component(s);
88 } else {
89 encodeNonUtf8Component(s);
90 }
91 }
92
93
94
95
96
97
98 public URI toUri() throws URISyntaxException {
99 return new URI(toString());
100 }
101
102
103
104
105
106
107 @Override
108 public String toString() {
109 return uriBuilder.toString();
110 }
111
112
113
114
115
116
117
118
119
120
121
122 private void encodeNonUtf8Component(CharSequence s) {
123
124 char[] buf = null;
125
126 for (int i = 0, len = s.length(); i < len;) {
127 char c = s.charAt(i);
128 if (dontNeedEncoding(c)) {
129 uriBuilder.append(c);
130 i++;
131 } else {
132 int index = 0;
133 if (buf == null) {
134 buf = new char[s.length() - i];
135 }
136
137 do {
138 buf[index] = c;
139 index++;
140 i++;
141 } while (i < s.length() && !dontNeedEncoding(c = s.charAt(i)));
142
143 byte[] bytes = new String(buf, 0, index).getBytes(charset);
144
145 for (byte b : bytes) {
146 appendEncoded(b);
147 }
148 }
149 }
150 }
151
152
153
154
155 private void encodeUtf8Component(CharSequence s) {
156 for (int i = 0, len = s.length(); i < len; i++) {
157 char c = s.charAt(i);
158 if (!dontNeedEncoding(c)) {
159 encodeUtf8Component(s, i, len);
160 return;
161 }
162 }
163 uriBuilder.append(s);
164 }
165
166 private void encodeUtf8Component(CharSequence s, int encodingStart, int len) {
167 if (encodingStart > 0) {
168
169 uriBuilder.append(s, 0, encodingStart);
170 }
171 encodeUtf8ComponentSlow(s, encodingStart, len);
172 }
173
174 private void encodeUtf8ComponentSlow(CharSequence s, int start, int len) {
175 for (int i = start; i < len; i++) {
176 char c = s.charAt(i);
177 if (c < 0x80) {
178 if (dontNeedEncoding(c)) {
179 uriBuilder.append(c);
180 } else {
181 appendEncoded(c);
182 }
183 } else if (c < 0x800) {
184 appendEncoded(0xc0 | (c >> 6));
185 appendEncoded(0x80 | (c & 0x3f));
186 } else if (StringUtil.isSurrogate(c)) {
187 if (!Character.isHighSurrogate(c)) {
188 appendEncoded(WRITE_UTF_UNKNOWN);
189 continue;
190 }
191
192 if (++i == s.length()) {
193 appendEncoded(WRITE_UTF_UNKNOWN);
194 break;
195 }
196
197 writeUtf8Surrogate(c, s.charAt(i));
198 } else {
199 appendEncoded(0xe0 | (c >> 12));
200 appendEncoded(0x80 | ((c >> 6) & 0x3f));
201 appendEncoded(0x80 | (c & 0x3f));
202 }
203 }
204 }
205
206 private void writeUtf8Surrogate(char c, char c2) {
207 if (!Character.isLowSurrogate(c2)) {
208 appendEncoded(WRITE_UTF_UNKNOWN);
209 appendEncoded(Character.isHighSurrogate(c2) ? WRITE_UTF_UNKNOWN : c2);
210 return;
211 }
212 int codePoint = Character.toCodePoint(c, c2);
213
214 appendEncoded(0xf0 | (codePoint >> 18));
215 appendEncoded(0x80 | ((codePoint >> 12) & 0x3f));
216 appendEncoded(0x80 | ((codePoint >> 6) & 0x3f));
217 appendEncoded(0x80 | (codePoint & 0x3f));
218 }
219
220 private void appendEncoded(int b) {
221 uriBuilder.append('%').append(forDigit(b >> 4)).append(forDigit(b));
222 }
223
224
225
226
227
228
229
230
231 private static char forDigit(int digit) {
232 return CHAR_MAP[digit & 0xF];
233 }
234
235
236
237
238
239
240
241
242
243
244
245
246 private static boolean dontNeedEncoding(char ch) {
247 return ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z' || ch >= '0' && ch <= '9'
248 || ch == '-' || ch == '_' || ch == '.' || ch == '*' || ch == '~';
249 }
250 }