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