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.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.http.annotation.Contract;
39  import org.apache.http.annotation.ThreadingBehavior;
40  import org.apache.http.cookie.ClientCookie;
41  import org.apache.http.cookie.CommonCookieAttributeHandler;
42  import org.apache.http.cookie.MalformedCookieException;
43  import org.apache.http.cookie.SetCookie;
44  import org.apache.http.message.ParserCursor;
45  import org.apache.http.util.Args;
46  
47  /**
48   *
49   * @since 4.4
50   */
51  @Contract(threading = ThreadingBehavior.IMMUTABLE)
52  public class LaxExpiresHandler extends AbstractCookieAttributeHandler implements CommonCookieAttributeHandler {
53  
54      static final TimeZone UTC = TimeZone.getTimeZone("UTC");
55  
56      private static final BitSet DELIMS;
57      static {
58          final BitSet bitSet = new BitSet();
59          bitSet.set(0x9);
60          for (int b = 0x20; b <= 0x2f; b++) {
61              bitSet.set(b);
62          }
63          for (int b = 0x3b; b <= 0x40; b++) {
64              bitSet.set(b);
65          }
66          for (int b = 0x5b; b <= 0x60; b++) {
67              bitSet.set(b);
68          }
69          for (int b = 0x7b; b <= 0x7e; b++) {
70              bitSet.set(b);
71          }
72          DELIMS = bitSet;
73      }
74      private static final Map<String, Integer> MONTHS;
75      static {
76          final ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<String, Integer>(12);
77          map.put("jan", Calendar.JANUARY);
78          map.put("feb", Calendar.FEBRUARY);
79          map.put("mar", Calendar.MARCH);
80          map.put("apr", Calendar.APRIL);
81          map.put("may", Calendar.MAY);
82          map.put("jun", Calendar.JUNE);
83          map.put("jul", Calendar.JULY);
84          map.put("aug", Calendar.AUGUST);
85          map.put("sep", Calendar.SEPTEMBER);
86          map.put("oct", Calendar.OCTOBER);
87          map.put("nov", Calendar.NOVEMBER);
88          map.put("dec", Calendar.DECEMBER);
89          MONTHS = map;
90      }
91  
92      private final static Pattern TIME_PATTERN = Pattern.compile(
93              "^([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})([^0-9].*)?$");
94      private final static Pattern DAY_OF_MONTH_PATTERN = Pattern.compile(
95              "^([0-9]{1,2})([^0-9].*)?$");
96      private final static Pattern MONTH_PATTERN = Pattern.compile(
97              "^(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)(.*)?$", Pattern.CASE_INSENSITIVE);
98      private final static Pattern YEAR_PATTERN = Pattern.compile(
99              "^([0-9]{2,4})([^0-9].*)?$");
100 
101     public LaxExpiresHandler() {
102         super();
103     }
104 
105     @Override
106     public void parse(final SetCookie cookie, final String value) throws MalformedCookieException {
107         Args.notNull(cookie, "Cookie");
108         final ParserCursor cursor = new ParserCursor(0, value.length());
109         final StringBuilder content = new StringBuilder();
110 
111         int second = 0, minute = 0, hour = 0, day = 0, month = 0, year = 0;
112         boolean foundTime = false, foundDayOfMonth = false, foundMonth = false, foundYear = false;
113         try {
114             while (!cursor.atEnd()) {
115                 skipDelims(value, cursor);
116                 content.setLength(0);
117                 copyContent(value, cursor, content);
118 
119                 if (content.length() == 0) {
120                     break;
121                 }
122                 if (!foundTime) {
123                     final Matcher matcher = TIME_PATTERN.matcher(content);
124                     if (matcher.matches()) {
125                         foundTime = true;
126                         hour = Integer.parseInt(matcher.group(1));
127                         minute = Integer.parseInt(matcher.group(2));
128                         second =Integer.parseInt(matcher.group(3));
129                         continue;
130                     }
131                 }
132                 if (!foundDayOfMonth) {
133                     final Matcher matcher = DAY_OF_MONTH_PATTERN.matcher(content);
134                     if (matcher.matches()) {
135                         foundDayOfMonth = true;
136                         day = Integer.parseInt(matcher.group(1));
137                         continue;
138                     }
139                 }
140                 if (!foundMonth) {
141                     final Matcher matcher = MONTH_PATTERN.matcher(content);
142                     if (matcher.matches()) {
143                         foundMonth = true;
144                         month = MONTHS.get(matcher.group(1).toLowerCase(Locale.ROOT));
145                         continue;
146                     }
147                 }
148                 if (!foundYear) {
149                     final Matcher matcher = YEAR_PATTERN.matcher(content);
150                     if (matcher.matches()) {
151                         foundYear = true;
152                         year = Integer.parseInt(matcher.group(1));
153                         continue;
154                     }
155                 }
156             }
157         } catch (final NumberFormatException ignore) {
158             throw new MalformedCookieException("Invalid 'expires' attribute: " + value);
159         }
160         if (!foundTime || !foundDayOfMonth || !foundMonth || !foundYear) {
161             throw new MalformedCookieException("Invalid 'expires' attribute: " + value);
162         }
163         if (year >= 70 && year <= 99) {
164             year = 1900 + year;
165         }
166         if (year >= 0 && year <= 69) {
167             year = 2000 + year;
168         }
169         if (day < 1 || day > 31 || year < 1601 || hour > 23 || minute > 59 || second > 59) {
170             throw new MalformedCookieException("Invalid 'expires' attribute: " + value);
171         }
172 
173         final Calendar c = Calendar.getInstance();
174         c.setTimeZone(UTC);
175         c.setTimeInMillis(0L);
176         c.set(Calendar.SECOND, second);
177         c.set(Calendar.MINUTE, minute);
178         c.set(Calendar.HOUR_OF_DAY, hour);
179         c.set(Calendar.DAY_OF_MONTH, day);
180         c.set(Calendar.MONTH, month);
181         c.set(Calendar.YEAR, year);
182         cookie.setExpiryDate(c.getTime());
183     }
184 
185     private void skipDelims(final CharSequence buf, final ParserCursor cursor) {
186         int pos = cursor.getPos();
187         final int indexFrom = cursor.getPos();
188         final int indexTo = cursor.getUpperBound();
189         for (int i = indexFrom; i < indexTo; i++) {
190             final char current = buf.charAt(i);
191             if (DELIMS.get(current)) {
192                 pos++;
193             } else {
194                 break;
195             }
196         }
197         cursor.updatePos(pos);
198     }
199 
200     private void copyContent(final CharSequence buf, final ParserCursor cursor, final StringBuilder dst) {
201         int pos = cursor.getPos();
202         final int indexFrom = cursor.getPos();
203         final int indexTo = cursor.getUpperBound();
204         for (int i = indexFrom; i < indexTo; i++) {
205             final char current = buf.charAt(i);
206             if (DELIMS.get(current)) {
207                 break;
208             } else {
209                 pos++;
210                 dst.append(current);
211             }
212         }
213         cursor.updatePos(pos);
214     }
215 
216     @Override
217     public String getAttributeName() {
218         return ClientCookie.EXPIRES_ATTR;
219     }
220 
221 }