1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.http;
16
17 import java.nio.ByteBuffer;
18 import java.nio.CharBuffer;
19 import java.nio.charset.Charset;
20 import java.nio.charset.StandardCharsets;
21 import java.time.Instant;
22 import java.time.ZoneId;
23 import java.time.format.DateTimeFormatter;
24 import java.time.format.DateTimeFormatterBuilder;
25 import java.time.format.DateTimeParseException;
26 import java.util.ArrayList;
27 import java.util.BitSet;
28 import java.util.Date;
29 import java.util.List;
30 import java.util.Locale;
31
32 import org.htmlunit.util.NameValuePair;
33
34
35
36
37
38
39 public final class HttpUtils {
40
41
42
43
44
45 private static final BitSet URLENCODER = new BitSet(256);
46
47 static {
48 for (int i = 'a'; i <= 'z'; i++) {
49 URLENCODER.set(i);
50 }
51 for (int i = 'A'; i <= 'Z'; i++) {
52 URLENCODER.set(i);
53 }
54
55 for (int i = '0'; i <= '9'; i++) {
56 URLENCODER.set(i);
57 }
58 URLENCODER.set('_');
59 URLENCODER.set('-');
60 URLENCODER.set('.');
61 URLENCODER.set('*');
62 }
63
64
65
66
67 private static final String PATTERN_RFC1123 = "EEE, dd MMM yyyy HH:mm:ss zzz";
68
69
70 private static final DateTimeFormatter FORMATTER_RFC1123 = new DateTimeFormatterBuilder()
71 .parseLenient()
72 .parseCaseInsensitive()
73 .appendPattern(PATTERN_RFC1123)
74 .toFormatter(Locale.ENGLISH);
75
76
77
78
79 public static final String PATTERN_RFC1036 = "EEE, dd-MMM-yy HH:mm:ss zzz";
80
81
82 private static final DateTimeFormatter FORMATTER_RFC1036 = new DateTimeFormatterBuilder()
83 .parseLenient()
84 .parseCaseInsensitive()
85 .appendPattern(PATTERN_RFC1036)
86 .toFormatter(Locale.ENGLISH);
87
88
89
90
91
92 private static final String PATTERN_ASCTIME = "EEE MMM d HH:mm:ss yyyy";
93
94
95 private static final DateTimeFormatter FORMATTER_ASCTIME = new DateTimeFormatterBuilder()
96 .parseLenient()
97 .parseCaseInsensitive()
98 .appendPattern(PATTERN_ASCTIME)
99 .toFormatter(Locale.ENGLISH);
100
101
102
103
104 private static final DateTimeFormatter[] STANDARD_PATTERNS = {
105 FORMATTER_RFC1123,
106 FORMATTER_RFC1036,
107 FORMATTER_ASCTIME
108 };
109
110 private static final ZoneId GMT_ID = ZoneId.of("GMT");
111
112
113
114
115
116
117
118
119
120 public static Date parseDate(final String dateValue) {
121 if (dateValue == null) {
122 return null;
123 }
124
125 String v = dateValue;
126
127 if (v.length() > 1 && v.startsWith("'") && v.endsWith("'")) {
128 v = v.substring(1, v.length() - 1);
129 }
130
131 for (final DateTimeFormatter dateFormatter : STANDARD_PATTERNS) {
132 try {
133 return new Date(Instant.from(dateFormatter.parse(v)).toEpochMilli());
134 }
135 catch (final DateTimeParseException ignored) {
136
137 }
138 }
139 return null;
140 }
141
142
143
144
145
146
147
148
149
150 public static String formatDate(final Date date) {
151 final Instant instant = Instant.ofEpochMilli(date.getTime());
152 return FORMATTER_RFC1123.format(instant.atZone(GMT_ID));
153 }
154
155
156
157
158
159
160
161
162
163 public static List<NameValuePair> parseUrlQuery(final String s, final Charset charset) {
164 if (s == null) {
165 return new ArrayList<>(0);
166 }
167
168 final BitSet delimSet = new BitSet();
169 delimSet.set('&');
170 delimSet.set(';');
171
172 final ParseRange cursor = new ParseRange(0, s.length());
173 final List<NameValuePair> list = new ArrayList<>();
174 while (!cursor.atEnd()) {
175 delimSet.set('=');
176 final String name = parseToken(s, cursor, delimSet);
177 String value = null;
178 if (!cursor.atEnd()) {
179 final int delim = s.charAt(cursor.getPos());
180 cursor.updatePos(cursor.getPos() + 1);
181 if (delim == '=') {
182 delimSet.clear('=');
183 value = parseToken(s, cursor, delimSet);
184 if (!cursor.atEnd()) {
185 cursor.updatePos(cursor.getPos() + 1);
186 }
187 }
188 }
189 if (!name.isEmpty()) {
190 list.add(new NameValuePair(
191 decodeFormFields(name, charset),
192 decodeFormFields(value, charset)));
193 }
194 }
195 return list;
196 }
197
198 private static String decodeFormFields(final String content, final Charset charset) {
199 if (content == null) {
200 return null;
201 }
202
203 final ByteBuffer bb = ByteBuffer.allocate(content.length());
204 final CharBuffer cb = CharBuffer.wrap(content);
205 while (cb.hasRemaining()) {
206 final char c = cb.get();
207 if (c == '%' && cb.remaining() >= 2) {
208 final char uc = cb.get();
209 final char lc = cb.get();
210 final int u = Character.digit(uc, 16);
211 final int l = Character.digit(lc, 16);
212 if (u != -1 && l != -1) {
213 bb.put((byte) ((u << 4) + l));
214 }
215 else {
216 bb.put((byte) '%');
217 bb.put((byte) uc);
218 bb.put((byte) lc);
219 }
220 }
221 else if (c == '+') {
222 bb.put((byte) ' ');
223 }
224 else {
225 bb.put((byte) c);
226 }
227 }
228 bb.flip();
229 return (charset == null ? StandardCharsets.UTF_8 : charset).decode(bb).toString();
230 }
231
232
233
234
235
236
237
238
239 public static String toQueryFormFields(final Iterable<? extends NameValuePair> parameters, final Charset charset) {
240 final StringBuilder result = new StringBuilder();
241 for (final NameValuePair parameter : parameters) {
242 final String encodedName = encodeFormFields(parameter.getName(), charset);
243 final String encodedValue = encodeFormFields(parameter.getValue(), charset);
244 if (result.length() > 0) {
245 result.append('&');
246 }
247 result.append(encodedName);
248 if (encodedValue != null) {
249 result.append('=').append(encodedValue);
250 }
251 }
252 return result.toString();
253 }
254
255 private static String encodeFormFields(final String content, final Charset charset) {
256 if (content == null) {
257 return null;
258 }
259
260 final StringBuilder buf = new StringBuilder();
261 final ByteBuffer bb = (charset == null ? StandardCharsets.UTF_8 : charset).encode(content);
262 while (bb.hasRemaining()) {
263 final int b = bb.get() & 0xff;
264 if (URLENCODER.get(b)) {
265 buf.append((char) b);
266 }
267 else if (b == ' ') {
268 buf.append('+');
269 }
270 else {
271 buf.append('%');
272 final char hex1 = Character.toUpperCase(Character.forDigit((b >> 4) & 0xF, 16));
273 final char hex2 = Character.toUpperCase(Character.forDigit(b & 0xF, 16));
274 buf.append(hex1).append(hex2);
275 }
276 }
277 return buf.toString();
278 }
279
280 private HttpUtils() {
281
282 }
283
284
285
286
287
288
289
290
291
292
293 private static String parseToken(final String buf, final ParseRange range, final BitSet delimiters) {
294 final StringBuilder dst = new StringBuilder();
295 boolean whitespace = false;
296 while (!range.atEnd()) {
297 final char current = buf.charAt(range.getPos());
298 if (delimiters.get(current)) {
299 break;
300 }
301 else if (isWhitespace(current)) {
302 skipWhiteSpace(buf, range);
303 whitespace = true;
304 }
305 else {
306 if (whitespace && dst.length() > 0) {
307 dst.append(' ');
308 }
309 copyContent(buf, range, delimiters, dst);
310 whitespace = false;
311 }
312 }
313 return dst.toString();
314 }
315
316
317
318
319
320
321
322
323 private static void skipWhiteSpace(final String buf, final ParseRange range) {
324 int pos = range.getPos();
325 final int indexTo = range.getUpperBound();
326
327 for (int i = pos; i < indexTo; i++) {
328 if (!isWhitespace(buf.charAt(i))) {
329 break;
330 }
331 pos++;
332 }
333 range.updatePos(pos);
334 }
335
336
337
338
339
340
341
342
343
344
345
346 private static void copyContent(final String buf, final ParseRange range,
347 final BitSet delimiters, final StringBuilder dst) {
348 int pos = range.getPos();
349 final int indexTo = range.getUpperBound();
350
351 for (int i = pos; i < indexTo; i++) {
352 final char current = buf.charAt(i);
353 if (delimiters.get(current) || isWhitespace(current)) {
354 break;
355 }
356 pos++;
357 dst.append(current);
358 }
359
360 range.updatePos(pos);
361 }
362
363 private static boolean isWhitespace(final char ch) {
364 return ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n';
365 }
366
367 private static final class ParseRange {
368 private final int upperBound_;
369 private int pos_;
370
371 ParseRange(final int pos, final int upperBound) {
372 upperBound_ = upperBound;
373 pos_ = pos;
374 }
375
376 int getPos() {
377 return pos_;
378 }
379
380 int getUpperBound() {
381 return upperBound_;
382 }
383
384 void updatePos(final int pos) {
385 pos_ = pos;
386 }
387
388 boolean atEnd() {
389 return pos_ >= upperBound_;
390 }
391 }
392 }
393