View Javadoc
1   /*
2    * Copyright (c) 2002-2026 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
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   * Http related utils.
36   *
37   * @author Ronald Brill
38   */
39  public final class HttpUtils {
40  
41      /**
42       * Safe characters for x-www-form-urlencoded data;
43       * i.e. alphanumeric plus {@code "-", "_", ".", "*"}
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       * Date format pattern used to parse HTTP date headers in RFC 1123 format.
66       */
67      private static final String PATTERN_RFC1123 = "EEE, dd MMM yyyy HH:mm:ss zzz";
68  
69      /** RFC 1123 date formatter. */
70      private static final DateTimeFormatter FORMATTER_RFC1123 = new DateTimeFormatterBuilder()
71              .parseLenient()
72              .parseCaseInsensitive()
73              .appendPattern(PATTERN_RFC1123)
74              .toFormatter(Locale.ENGLISH);
75  
76      /**
77       * Date format pattern used to parse HTTP date headers in RFC 1036 format.
78       */
79      public static final String PATTERN_RFC1036 = "EEE, dd-MMM-yy HH:mm:ss zzz";
80  
81      /** RFC 1036 date formatter. */
82      private static final DateTimeFormatter FORMATTER_RFC1036 = new DateTimeFormatterBuilder()
83              .parseLenient()
84              .parseCaseInsensitive()
85              .appendPattern(PATTERN_RFC1036)
86              .toFormatter(Locale.ENGLISH);
87  
88      /**
89       * Date format pattern used to parse HTTP date headers in ANSI C
90       * {@code asctime()} format.
91       */
92      private static final String PATTERN_ASCTIME = "EEE MMM d HH:mm:ss yyyy";
93  
94      /** ASCII time date formatter. */
95      private static final DateTimeFormatter FORMATTER_ASCTIME = new DateTimeFormatterBuilder()
96              .parseLenient()
97              .parseCaseInsensitive()
98              .appendPattern(PATTERN_ASCTIME)
99              .toFormatter(Locale.ENGLISH);
100 
101     /**
102      * Standard date formatters: {@link #FORMATTER_RFC1123}, {@link #FORMATTER_RFC1036}, {@link #FORMATTER_ASCTIME}.
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      * Parses a date value.  The formats used for parsing the date value are retrieved from
114      * the default http params.
115      *
116      * @param dateValue the date value to parse
117      *
118      * @return the parsed date or null if input could not be parsed
119      */
120     public static Date parseDate(final String dateValue) {
121         if (dateValue == null) {
122             return null;
123         }
124 
125         String v = dateValue;
126         // trim single quotes around date if present
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                 // ignore
137             }
138         }
139         return null;
140     }
141 
142     /**
143      * Formats the given date according to the RFC 1123 pattern.
144      *
145      * @param date The date to format.
146      * @return An RFC 1123 formatted date string.
147      *
148      * @see #PATTERN_RFC1123
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      * Returns a list of {@link NameValuePair}s URI query parameters.
157      * By convention, {@code '&'} and {@code ';'} are accepted as parameter separators.
158      *
159      * @param s URI query component.
160      * @param charset charset to use when decoding the parameters.
161      * @return list of query parameters.
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      * Converts the specified form parameters to a URL-encoded query string.
234      *
235      * @param parameters the form parameters
236      * @param charset the character set to use for encoding
237      * @return the URL-encoded query string
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         // util class
282     }
283 
284     /**
285      * Extracts from the sequence of chars a token terminated with any of the given delimiters
286      * discarding semantically insignificant whitespace characters.
287      *
288      * @param buf buffer with the sequence of chars to be parsed
289      * @param range defines the bounds and current position of the buffer
290      * @param delimiters set of delimiting characters. Can be {@code null} if the token
291      *        is not delimited by any character.
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      * Skips semantically insignificant whitespace characters and moves the cursor to the closest
318      * non-whitespace character.
319      *
320      * @param buf buffer with the sequence of chars to be parsed
321      * @param range defines the bounds and current position of the buffer
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      * Transfers content into the destination buffer until a whitespace character or any of
338      * the given delimiters is encountered.
339      *
340      * @param buf buffer with the sequence of chars to be parsed
341      * @param range defines the bounds and current position of the buffer
342      * @param delimiters set of delimiting characters. Can be {@code null} if the value
343      *        is delimited by a whitespace only.
344      * @param dst destination buffer
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