View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.hc.client5.http.impl.cookie;
28  
29  import java.util.BitSet;
30  import java.util.Calendar;
31  import java.util.Locale;
32  import java.util.Map;
33  import java.util.TimeZone;
34  import java.util.concurrent.ConcurrentHashMap;
35  import java.util.regex.Matcher;
36  import java.util.regex.Pattern;
37  
38  import org.apache.hc.client5.http.cookie.CommonCookieAttributeHandler;
39  import org.apache.hc.client5.http.cookie.Cookie;
40  import org.apache.hc.client5.http.cookie.MalformedCookieException;
41  import org.apache.hc.client5.http.cookie.SetCookie;
42  import org.apache.hc.core5.annotation.Contract;
43  import org.apache.hc.core5.annotation.ThreadingBehavior;
44  import org.apache.hc.core5.util.Args;
45  import org.apache.hc.core5.util.TextUtils;
46  import org.apache.hc.core5.util.Tokenizer;
47  
48  /**
49   * Cookie {@code expires} attribute handler conformant to the more relaxed interpretation
50   * of HTTP state management.
51   *
52   * @since 4.4
53   */
54  @Contract(threading = ThreadingBehavior.STATELESS)
55  public class LaxExpiresHandler extends AbstractCookieAttributeHandler implements CommonCookieAttributeHandler {
56  
57      static final TimeZone UTC = TimeZone.getTimeZone("UTC");
58  
59      private static final BitSet DELIMS;
60      static {
61          final BitSet bitSet = new BitSet();
62          bitSet.set(0x9);
63          for (int b = 0x20; b <= 0x2f; b++) {
64              bitSet.set(b);
65          }
66          for (int b = 0x3b; b <= 0x40; b++) {
67              bitSet.set(b);
68          }
69          for (int b = 0x5b; b <= 0x60; b++) {
70              bitSet.set(b);
71          }
72          for (int b = 0x7b; b <= 0x7e; b++) {
73              bitSet.set(b);
74          }
75          DELIMS = bitSet;
76      }
77      private static final Map<String, Integer> MONTHS;
78      static {
79          final ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>(12);
80          map.put("jan", Calendar.JANUARY);
81          map.put("feb", Calendar.FEBRUARY);
82          map.put("mar", Calendar.MARCH);
83          map.put("apr", Calendar.APRIL);
84          map.put("may", Calendar.MAY);
85          map.put("jun", Calendar.JUNE);
86          map.put("jul", Calendar.JULY);
87          map.put("aug", Calendar.AUGUST);
88          map.put("sep", Calendar.SEPTEMBER);
89          map.put("oct", Calendar.OCTOBER);
90          map.put("nov", Calendar.NOVEMBER);
91          map.put("dec", Calendar.DECEMBER);
92          MONTHS = map;
93      }
94  
95      private final static Pattern TIME_PATTERN = Pattern.compile(
96              "^([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})([^0-9].*)?$");
97      private final static Pattern DAY_OF_MONTH_PATTERN = Pattern.compile(
98              "^([0-9]{1,2})([^0-9].*)?$");
99      private final static Pattern MONTH_PATTERN = Pattern.compile(
100             "^(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)(.*)?$", Pattern.CASE_INSENSITIVE);
101     private final static Pattern YEAR_PATTERN = Pattern.compile(
102             "^([0-9]{2,4})([^0-9].*)?$");
103 
104     public LaxExpiresHandler() {
105         super();
106     }
107 
108     @Override
109     public void parse(final SetCookie cookie, final String value) throws MalformedCookieException {
110         Args.notNull(cookie, "Cookie");
111         if (TextUtils.isBlank(value)) {
112             return;
113         }
114         final Tokenizer.Cursor cursor = new Tokenizer.Cursor(0, value.length());
115         final StringBuilder content = new StringBuilder();
116 
117         int second = 0, minute = 0, hour = 0, day = 0, month = 0, year = 0;
118         boolean foundTime = false, foundDayOfMonth = false, foundMonth = false, foundYear = false;
119         try {
120             while (!cursor.atEnd()) {
121                 skipDelims(value, cursor);
122                 content.setLength(0);
123                 copyContent(value, cursor, content);
124 
125                 if (content.length() == 0) {
126                     break;
127                 }
128                 if (!foundTime) {
129                     final Matcher matcher = TIME_PATTERN.matcher(content);
130                     if (matcher.matches()) {
131                         foundTime = true;
132                         hour = Integer.parseInt(matcher.group(1));
133                         minute = Integer.parseInt(matcher.group(2));
134                         second =Integer.parseInt(matcher.group(3));
135                         continue;
136                     }
137                 }
138                 if (!foundDayOfMonth) {
139                     final Matcher matcher = DAY_OF_MONTH_PATTERN.matcher(content);
140                     if (matcher.matches()) {
141                         foundDayOfMonth = true;
142                         day = Integer.parseInt(matcher.group(1));
143                         continue;
144                     }
145                 }
146                 if (!foundMonth) {
147                     final Matcher matcher = MONTH_PATTERN.matcher(content);
148                     if (matcher.matches()) {
149                         foundMonth = true;
150                         month = MONTHS.get(matcher.group(1).toLowerCase(Locale.ROOT));
151                         continue;
152                     }
153                 }
154                 if (!foundYear) {
155                     final Matcher matcher = YEAR_PATTERN.matcher(content);
156                     if (matcher.matches()) {
157                         foundYear = true;
158                         year = Integer.parseInt(matcher.group(1));
159                         continue;
160                     }
161                 }
162             }
163         } catch (final NumberFormatException ignore) {
164             throw new MalformedCookieException("Invalid 'expires' attribute: " + value);
165         }
166         if (!foundTime || !foundDayOfMonth || !foundMonth || !foundYear) {
167             throw new MalformedCookieException("Invalid 'expires' attribute: " + value);
168         }
169         if (year >= 70 && year <= 99) {
170             year = 1900 + year;
171         }
172         if (year >= 0 && year <= 69) {
173             year = 2000 + year;
174         }
175         if (day < 1 || day > 31 || year < 1601 || hour > 23 || minute > 59 || second > 59) {
176             throw new MalformedCookieException("Invalid 'expires' attribute: " + value);
177         }
178 
179         final Calendar c = Calendar.getInstance();
180         c.setTimeZone(UTC);
181         c.setTimeInMillis(0L);
182         c.set(Calendar.SECOND, second);
183         c.set(Calendar.MINUTE, minute);
184         c.set(Calendar.HOUR_OF_DAY, hour);
185         c.set(Calendar.DAY_OF_MONTH, day);
186         c.set(Calendar.MONTH, month);
187         c.set(Calendar.YEAR, year);
188         cookie.setExpiryDate(c.getTime());
189     }
190 
191     private void skipDelims(final CharSequence buf, final Tokenizer.Cursor cursor) {
192         int pos = cursor.getPos();
193         final int indexFrom = cursor.getPos();
194         final int indexTo = cursor.getUpperBound();
195         for (int i = indexFrom; i < indexTo; i++) {
196             final char current = buf.charAt(i);
197             if (DELIMS.get(current)) {
198                 pos++;
199             } else {
200                 break;
201             }
202         }
203         cursor.updatePos(pos);
204     }
205 
206     private void copyContent(final CharSequence buf, final Tokenizer.Cursor cursor, final StringBuilder dst) {
207         int pos = cursor.getPos();
208         final int indexFrom = cursor.getPos();
209         final int indexTo = cursor.getUpperBound();
210         for (int i = indexFrom; i < indexTo; i++) {
211             final char current = buf.charAt(i);
212             if (DELIMS.get(current)) {
213                 break;
214             }
215             pos++;
216             dst.append(current);
217         }
218         cursor.updatePos(pos);
219     }
220 
221     @Override
222     public String getAttributeName() {
223         return Cookie.EXPIRES_ATTR;
224     }
225 
226 }