Mercurial Hosting > luan
annotate src/org/eclipse/jetty/http/HttpFields.java @ 1026:6647dbc8be71
remove Buffer.CaseInsensitve
| author | Franklin Schmidt <fschmidt@gmail.com> |
|---|---|
| date | Wed, 02 Nov 2016 23:52:33 -0600 |
| parents | 3718afd99988 |
| children | eca26899c4bc |
| rev | line source |
|---|---|
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1 // |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
2 // ======================================================================== |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
3 // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd. |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
4 // ------------------------------------------------------------------------ |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
5 // All rights reserved. This program and the accompanying materials |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
6 // are made available under the terms of the Eclipse Public License v1.0 |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
7 // and Apache License v2.0 which accompanies this distribution. |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
8 // |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
9 // The Eclipse Public License is available at |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
10 // http://www.eclipse.org/legal/epl-v10.html |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
11 // |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
12 // The Apache License v2.0 is available at |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
13 // http://www.opensource.org/licenses/apache2.0.php |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
14 // |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
15 // You may elect to redistribute this code under either of these licenses. |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
16 // ======================================================================== |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
17 // |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
18 |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
19 package org.eclipse.jetty.http; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
20 |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
21 import java.io.IOException; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
22 import java.io.UnsupportedEncodingException; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
23 import java.text.SimpleDateFormat; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
24 import java.util.ArrayList; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
25 import java.util.Calendar; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
26 import java.util.Collections; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
27 import java.util.Collection; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
28 import java.util.Date; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
29 import java.util.Enumeration; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
30 import java.util.GregorianCalendar; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
31 import java.util.HashMap; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
32 import java.util.Iterator; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
33 import java.util.List; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
34 import java.util.Locale; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
35 import java.util.Map; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
36 import java.util.NoSuchElementException; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
37 import java.util.StringTokenizer; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
38 import java.util.TimeZone; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
39 import java.util.concurrent.ConcurrentHashMap; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
40 import java.util.concurrent.ConcurrentMap; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
41 |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
42 import org.eclipse.jetty.io.Buffer; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
43 import org.eclipse.jetty.io.BufferUtil; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
44 import org.eclipse.jetty.io.ByteArrayBuffer; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
45 import org.eclipse.jetty.util.LazyList; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
46 import org.eclipse.jetty.util.QuotedStringTokenizer; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
47 import org.eclipse.jetty.util.StringUtil; |
|
820
8e9db0bbf4f9
remove org.eclipse.jetty.util.log and upgrade slf4j
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
48 import org.slf4j.Logger; |
|
8e9db0bbf4f9
remove org.eclipse.jetty.util.log and upgrade slf4j
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
49 import org.slf4j.LoggerFactory; |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
50 |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
51 /* ------------------------------------------------------------ */ |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
52 /** |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
53 * HTTP Fields. A collection of HTTP header and or Trailer fields. |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
54 * |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
55 * <p>This class is not synchronized as it is expected that modifications will only be performed by a |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
56 * single thread. |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
57 * |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
58 * |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
59 */ |
|
1019
f126d30e04a4
start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
855
diff
changeset
|
60 public final class HttpFields |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
61 { |
| 831 | 62 private static final Logger LOG = LoggerFactory.getLogger(HttpFields.class); |
| 63 | |
|
1019
f126d30e04a4
start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
855
diff
changeset
|
64 private static final String __COOKIE_DELIM="\"\\\n\r\t\f\b%+ ;="; |
|
f126d30e04a4
start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
855
diff
changeset
|
65 private static final TimeZone __GMT = TimeZone.getTimeZone("GMT"); |
| 831 | 66 |
| 67 private static final String[] DAYS = | |
| 68 { "Sat", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; | |
| 69 private static final String[] MONTHS = | |
| 70 { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "Jan"}; | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
71 |
| 831 | 72 |
| 73 private static class DateGenerator | |
| 74 { | |
| 75 private final StringBuilder buf = new StringBuilder(32); | |
| 76 private final GregorianCalendar gc = new GregorianCalendar(__GMT); | |
| 77 | |
| 78 /** | |
| 79 * Format HTTP date "EEE, dd MMM yyyy HH:mm:ss 'GMT'" | |
| 80 */ | |
| 81 public String formatDate(long date) | |
| 82 { | |
| 83 buf.setLength(0); | |
| 84 gc.setTimeInMillis(date); | |
| 85 | |
| 86 int day_of_week = gc.get(Calendar.DAY_OF_WEEK); | |
| 87 int day_of_month = gc.get(Calendar.DAY_OF_MONTH); | |
| 88 int month = gc.get(Calendar.MONTH); | |
| 89 int year = gc.get(Calendar.YEAR); | |
| 90 int century = year / 100; | |
| 91 year = year % 100; | |
| 92 | |
| 93 int hours = gc.get(Calendar.HOUR_OF_DAY); | |
| 94 int minutes = gc.get(Calendar.MINUTE); | |
| 95 int seconds = gc.get(Calendar.SECOND); | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
96 |
| 831 | 97 buf.append(DAYS[day_of_week]); |
| 98 buf.append(','); | |
| 99 buf.append(' '); | |
| 100 StringUtil.append2digits(buf, day_of_month); | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
101 |
| 831 | 102 buf.append(' '); |
| 103 buf.append(MONTHS[month]); | |
| 104 buf.append(' '); | |
| 105 StringUtil.append2digits(buf, century); | |
| 106 StringUtil.append2digits(buf, year); | |
| 107 | |
| 108 buf.append(' '); | |
| 109 StringUtil.append2digits(buf, hours); | |
| 110 buf.append(':'); | |
| 111 StringUtil.append2digits(buf, minutes); | |
| 112 buf.append(':'); | |
| 113 StringUtil.append2digits(buf, seconds); | |
| 114 buf.append(" GMT"); | |
| 115 return buf.toString(); | |
| 116 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
117 |
| 831 | 118 /* ------------------------------------------------------------ */ |
| 119 /** | |
| 120 * Format "EEE, dd-MMM-yy HH:mm:ss 'GMT'" for cookies | |
| 121 */ | |
| 122 public void formatCookieDate(StringBuilder buf, long date) | |
| 123 { | |
| 124 gc.setTimeInMillis(date); | |
| 125 | |
| 126 int day_of_week = gc.get(Calendar.DAY_OF_WEEK); | |
| 127 int day_of_month = gc.get(Calendar.DAY_OF_MONTH); | |
| 128 int month = gc.get(Calendar.MONTH); | |
| 129 int year = gc.get(Calendar.YEAR); | |
| 130 year = year % 10000; | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
131 |
| 831 | 132 int epoch = (int) ((date / 1000) % (60 * 60 * 24)); |
| 133 int seconds = epoch % 60; | |
| 134 epoch = epoch / 60; | |
| 135 int minutes = epoch % 60; | |
| 136 int hours = epoch / 60; | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
137 |
| 831 | 138 buf.append(DAYS[day_of_week]); |
| 139 buf.append(','); | |
| 140 buf.append(' '); | |
| 141 StringUtil.append2digits(buf, day_of_month); | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
142 |
| 831 | 143 buf.append('-'); |
| 144 buf.append(MONTHS[month]); | |
| 145 buf.append('-'); | |
| 146 StringUtil.append2digits(buf, year/100); | |
| 147 StringUtil.append2digits(buf, year%100); | |
| 148 | |
| 149 buf.append(' '); | |
| 150 StringUtil.append2digits(buf, hours); | |
| 151 buf.append(':'); | |
| 152 StringUtil.append2digits(buf, minutes); | |
| 153 buf.append(':'); | |
| 154 StringUtil.append2digits(buf, seconds); | |
| 155 buf.append(" GMT"); | |
| 156 } | |
| 157 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
158 |
|
1019
f126d30e04a4
start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
855
diff
changeset
|
159 private static final ThreadLocal<DateGenerator> __dateGenerator = new ThreadLocal<DateGenerator>() |
| 831 | 160 { |
| 161 @Override | |
| 162 protected DateGenerator initialValue() | |
| 163 { | |
| 164 return new DateGenerator(); | |
| 165 } | |
| 166 }; | |
| 167 | |
| 168 /* ------------------------------------------------------------ */ | |
| 169 /** | |
| 170 * Format HTTP date "EEE, dd MMM yyyy HH:mm:ss 'GMT'" | |
| 171 */ | |
|
1019
f126d30e04a4
start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
855
diff
changeset
|
172 private static String formatDate(long date) |
| 831 | 173 { |
| 174 return __dateGenerator.get().formatDate(date); | |
| 175 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
176 |
| 831 | 177 /* ------------------------------------------------------------ */ |
| 178 /** | |
| 179 * Format "EEE, dd-MMM-yyyy HH:mm:ss 'GMT'" for cookies | |
| 180 */ | |
|
1019
f126d30e04a4
start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
855
diff
changeset
|
181 private static void formatCookieDate(StringBuilder buf, long date) |
| 831 | 182 { |
| 183 __dateGenerator.get().formatCookieDate(buf,date); | |
| 184 } | |
| 185 | |
| 186 /* ------------------------------------------------------------ */ | |
| 187 /** | |
| 188 * Format "EEE, dd-MMM-yyyy HH:mm:ss 'GMT'" for cookies | |
| 189 */ | |
|
1019
f126d30e04a4
start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
855
diff
changeset
|
190 private static String formatCookieDate(long date) |
| 831 | 191 { |
| 192 StringBuilder buf = new StringBuilder(28); | |
| 193 formatCookieDate(buf, date); | |
| 194 return buf.toString(); | |
| 195 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
196 |
| 831 | 197 private final static String __dateReceiveFmt[] = |
| 198 { | |
| 199 "EEE, dd MMM yyyy HH:mm:ss zzz", | |
| 200 "EEE, dd-MMM-yy HH:mm:ss", | |
| 201 "EEE MMM dd HH:mm:ss yyyy", | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
202 |
| 831 | 203 "EEE, dd MMM yyyy HH:mm:ss", "EEE dd MMM yyyy HH:mm:ss zzz", |
| 204 "EEE dd MMM yyyy HH:mm:ss", "EEE MMM dd yyyy HH:mm:ss zzz", "EEE MMM dd yyyy HH:mm:ss", | |
| 205 "EEE MMM-dd-yyyy HH:mm:ss zzz", "EEE MMM-dd-yyyy HH:mm:ss", "dd MMM yyyy HH:mm:ss zzz", | |
| 206 "dd MMM yyyy HH:mm:ss", "dd-MMM-yy HH:mm:ss zzz", "dd-MMM-yy HH:mm:ss", "MMM dd HH:mm:ss yyyy zzz", | |
| 207 "MMM dd HH:mm:ss yyyy", "EEE MMM dd HH:mm:ss yyyy zzz", | |
| 208 "EEE, MMM dd HH:mm:ss yyyy zzz", "EEE, MMM dd HH:mm:ss yyyy", "EEE, dd-MMM-yy HH:mm:ss zzz", | |
| 209 "EEE dd-MMM-yy HH:mm:ss zzz", "EEE dd-MMM-yy HH:mm:ss", | |
| 210 }; | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
211 |
| 831 | 212 private static class DateParser |
| 213 { | |
|
1019
f126d30e04a4
start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
855
diff
changeset
|
214 final SimpleDateFormat _dateReceive[] = new SimpleDateFormat[__dateReceiveFmt.length]; |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
215 |
| 831 | 216 long parse(final String dateVal) |
| 217 { | |
| 218 for (int i = 0; i < _dateReceive.length; i++) | |
| 219 { | |
| 220 if (_dateReceive[i] == null) | |
| 221 { | |
| 222 _dateReceive[i] = new SimpleDateFormat(__dateReceiveFmt[i], Locale.US); | |
| 223 _dateReceive[i].setTimeZone(__GMT); | |
| 224 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
225 |
| 831 | 226 try |
| 227 { | |
| 228 Date date = (Date) _dateReceive[i].parseObject(dateVal); | |
| 229 return date.getTime(); | |
| 230 } | |
| 231 catch (java.lang.Exception e) | |
| 232 { | |
| 233 // LOG.ignore(e); | |
| 234 } | |
| 235 } | |
| 236 | |
| 237 if (dateVal.endsWith(" GMT")) | |
| 238 { | |
| 239 final String val = dateVal.substring(0, dateVal.length() - 4); | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
240 |
| 831 | 241 for (int i = 0; i < _dateReceive.length; i++) |
| 242 { | |
| 243 try | |
| 244 { | |
| 245 Date date = (Date) _dateReceive[i].parseObject(val); | |
| 246 return date.getTime(); | |
| 247 } | |
| 248 catch (java.lang.Exception e) | |
| 249 { | |
| 250 // LOG.ignore(e); | |
| 251 } | |
| 252 } | |
| 253 } | |
| 254 return -1; | |
| 255 } | |
| 256 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
257 |
| 831 | 258 private static final ThreadLocal<DateParser> __dateParser =new ThreadLocal<DateParser>() |
| 259 { | |
| 260 @Override | |
| 261 protected DateParser initialValue() | |
| 262 { | |
| 263 return new DateParser(); | |
| 264 } | |
| 265 }; | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
266 |
|
1019
f126d30e04a4
start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
855
diff
changeset
|
267 private final static String __01Jan1970 = formatDate(0); |
|
f126d30e04a4
start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
855
diff
changeset
|
268 private final static String __01Jan1970_COOKIE = formatCookieDate(0).trim(); |
|
f126d30e04a4
start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
855
diff
changeset
|
269 |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
270 |
|
1019
f126d30e04a4
start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
855
diff
changeset
|
271 |
|
f126d30e04a4
start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
855
diff
changeset
|
272 |
|
f126d30e04a4
start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
855
diff
changeset
|
273 |
|
f126d30e04a4
start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
855
diff
changeset
|
274 |
|
1022
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1021
diff
changeset
|
275 private final ArrayList<Field> _fields = new ArrayList<Field>(); |
|
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1021
diff
changeset
|
276 private final HashMap<String,Field> _names = new HashMap<String,Field>(); |
|
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1021
diff
changeset
|
277 |
| 831 | 278 |
| 279 /* -------------------------------------------------------------- */ | |
| 280 /** | |
| 281 * Get Collection of header names. | |
| 282 */ | |
| 283 public Collection<String> getFieldNamesCollection() | |
| 284 { | |
| 285 final List<String> list = new ArrayList<String>(_fields.size()); | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
286 |
|
1019
f126d30e04a4
start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
855
diff
changeset
|
287 for (Field f : _fields) |
|
f126d30e04a4
start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
855
diff
changeset
|
288 { |
|
f126d30e04a4
start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
855
diff
changeset
|
289 if (f!=null) |
| 1021 | 290 list.add(f._name.toString()); |
|
1019
f126d30e04a4
start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
855
diff
changeset
|
291 } |
|
f126d30e04a4
start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
855
diff
changeset
|
292 return list; |
| 831 | 293 } |
| 294 | |
| 295 /* -------------------------------------------------------------- */ | |
| 296 /** | |
| 297 * Get enumeration of header _names. Returns an enumeration of strings representing the header | |
| 298 * _names for this request. | |
| 299 */ | |
| 300 public Enumeration<String> getFieldNames() | |
| 301 { | |
| 302 final Enumeration<?> buffers = Collections.enumeration(_names.keySet()); | |
| 303 return new Enumeration<String>() | |
| 304 { | |
| 305 public String nextElement() | |
| 306 { | |
| 307 return buffers.nextElement().toString(); | |
| 308 } | |
| 309 | |
| 310 public boolean hasMoreElements() | |
| 311 { | |
| 312 return buffers.hasMoreElements(); | |
| 313 } | |
| 314 }; | |
| 315 } | |
| 316 | |
| 317 /* ------------------------------------------------------------ */ | |
| 318 public int size() | |
| 319 { | |
| 320 return _fields.size(); | |
| 321 } | |
| 322 | |
| 323 /* ------------------------------------------------------------ */ | |
| 324 /** | |
| 325 * Get a Field by index. | |
| 326 * @return A Field value or null if the Field value has not been set | |
| 327 * | |
| 328 */ | |
| 329 public Field getField(int i) | |
| 330 { | |
| 331 return _fields.get(i); | |
| 332 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
333 |
| 831 | 334 private Field getField(String name) |
| 335 { | |
|
1022
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1021
diff
changeset
|
336 return _names.get(name.toLowerCase()); |
| 831 | 337 } |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
338 |
| 831 | 339 public boolean containsKey(String name) |
| 340 { | |
|
1022
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1021
diff
changeset
|
341 return _names.containsKey(name.toLowerCase()); |
| 831 | 342 } |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
343 |
| 831 | 344 /* -------------------------------------------------------------- */ |
| 345 /** | |
| 346 * @return the value of a field, or null if not found. For multiple fields of the same name, | |
| 347 * only the first is returned. | |
| 348 * @param name the case-insensitive field name | |
| 349 */ | |
| 350 public String getStringField(String name) | |
| 351 { | |
| 352 Field field = getField(name); | |
|
1022
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1021
diff
changeset
|
353 return field==null ? null : field.getValue(); |
| 831 | 354 } |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
355 |
| 831 | 356 /* -------------------------------------------------------------- */ |
| 357 /** | |
| 358 * Get multi headers | |
| 359 * | |
| 360 * @return Enumeration of the values, or null if no such header. | |
| 361 * @param name the case-insensitive field name | |
| 362 */ | |
| 363 public Collection<String> getValuesCollection(String name) | |
| 364 { | |
| 365 Field field = getField(name); | |
|
1020
6be43ef1eb96
HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1019
diff
changeset
|
366 if (field==null) |
|
6be43ef1eb96
HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1019
diff
changeset
|
367 return null; |
|
6be43ef1eb96
HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1019
diff
changeset
|
368 |
|
6be43ef1eb96
HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1019
diff
changeset
|
369 final List<String> list = new ArrayList<String>(); |
|
6be43ef1eb96
HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1019
diff
changeset
|
370 |
|
6be43ef1eb96
HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1019
diff
changeset
|
371 while(field!=null) |
|
6be43ef1eb96
HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1019
diff
changeset
|
372 { |
|
6be43ef1eb96
HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1019
diff
changeset
|
373 list.add(field.getValue()); |
|
6be43ef1eb96
HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1019
diff
changeset
|
374 field=field._next; |
|
6be43ef1eb96
HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1019
diff
changeset
|
375 } |
|
6be43ef1eb96
HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1019
diff
changeset
|
376 return list; |
| 831 | 377 } |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
378 |
| 831 | 379 /* -------------------------------------------------------------- */ |
| 380 /** | |
| 381 * Get multi headers | |
| 382 * | |
| 383 * @return Enumeration of the values | |
| 384 * @param name the case-insensitive field name | |
| 385 */ | |
| 386 public Enumeration<String> getValues(String name) | |
| 387 { | |
| 388 final Field field = getField(name); | |
| 389 if (field == null) | |
| 390 { | |
| 391 List<String> empty=Collections.emptyList(); | |
| 392 return Collections.enumeration(empty); | |
| 393 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
394 |
| 831 | 395 return new Enumeration<String>() |
| 396 { | |
| 397 Field f = field; | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
398 |
| 831 | 399 public boolean hasMoreElements() |
| 400 { | |
| 401 return f != null; | |
| 402 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
403 |
| 831 | 404 public String nextElement() throws NoSuchElementException |
| 405 { | |
| 406 if (f == null) throw new NoSuchElementException(); | |
| 407 Field n = f; | |
| 408 f = f._next; | |
| 409 return n.getValue(); | |
| 410 } | |
| 411 }; | |
| 412 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
413 |
| 831 | 414 /* -------------------------------------------------------------- */ |
| 415 /** | |
| 416 * Get multi field values with separator. The multiple values can be represented as separate | |
| 417 * headers of the same name, or by a single header using the separator(s), or a combination of | |
| 418 * both. Separators may be quoted. | |
| 419 * | |
| 420 * @param name the case-insensitive field name | |
| 421 * @param separators String of separators. | |
| 422 * @return Enumeration of the values, or null if no such header. | |
| 423 */ | |
| 424 public Enumeration<String> getValues(String name, final String separators) | |
| 425 { | |
| 426 final Enumeration<String> e = getValues(name); | |
| 427 if (e == null) | |
| 428 return null; | |
| 429 return new Enumeration<String>() | |
| 430 { | |
| 431 QuotedStringTokenizer tok = null; | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
432 |
| 831 | 433 public boolean hasMoreElements() |
| 434 { | |
| 435 if (tok != null && tok.hasMoreElements()) return true; | |
| 436 while (e.hasMoreElements()) | |
| 437 { | |
| 438 String value = e.nextElement(); | |
| 439 tok = new QuotedStringTokenizer(value, separators, false, false); | |
| 440 if (tok.hasMoreElements()) return true; | |
| 441 } | |
| 442 tok = null; | |
| 443 return false; | |
| 444 } | |
| 445 | |
| 446 public String nextElement() throws NoSuchElementException | |
| 447 { | |
| 448 if (!hasMoreElements()) throw new NoSuchElementException(); | |
| 449 String next = (String) tok.nextElement(); | |
| 450 if (next != null) next = next.trim(); | |
| 451 return next; | |
| 452 } | |
| 453 }; | |
| 454 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
455 |
| 831 | 456 |
| 457 /* -------------------------------------------------------------- */ | |
| 458 /** | |
| 459 * Set a field. | |
| 460 * | |
| 461 * @param name the name of the field | |
| 462 * @param value the value of the field. If null the field is cleared. | |
| 463 */ | |
| 464 public void put(String name, String value) | |
| 465 { | |
|
1022
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1021
diff
changeset
|
466 String nameLower = name.toLowerCase(); |
|
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1021
diff
changeset
|
467 removeLower(nameLower); |
|
1020
6be43ef1eb96
HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1019
diff
changeset
|
468 if (value == null) |
|
6be43ef1eb96
HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1019
diff
changeset
|
469 return; |
|
6be43ef1eb96
HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1019
diff
changeset
|
470 |
| 831 | 471 // new value; |
| 472 Field field = new Field(name, value); | |
| 473 _fields.add(field); | |
|
1022
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1021
diff
changeset
|
474 _names.put(nameLower, field); |
| 831 | 475 } |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
476 |
| 831 | 477 /* -------------------------------------------------------------- */ |
| 478 /** | |
| 479 * Add to or set a field. If the field is allowed to have multiple values, add will add multiple | |
| 480 * headers of the same name. | |
| 481 * | |
| 482 * @param name the name of the field | |
| 483 * @param value the value of the field. | |
| 484 * @exception IllegalArgumentException If the name is a single valued field and already has a | |
| 485 * value. | |
| 486 */ | |
| 487 public void add(String name, String value) throws IllegalArgumentException | |
| 488 { | |
| 489 if (value == null) throw new IllegalArgumentException("null value"); | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
490 |
|
1022
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1021
diff
changeset
|
491 String nameLower = name.toLowerCase(); |
|
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1021
diff
changeset
|
492 |
|
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1021
diff
changeset
|
493 Field field = _names.get(nameLower); |
| 831 | 494 Field last = null; |
| 495 while (field != null) | |
| 496 { | |
| 497 last = field; | |
| 498 field = field._next; | |
| 499 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
500 |
| 831 | 501 // create the field |
|
1022
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1021
diff
changeset
|
502 field = new Field(name, value); |
| 831 | 503 _fields.add(field); |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
504 |
| 831 | 505 // look for chain to add too |
| 506 if (last != null) | |
| 507 last._next = field; | |
| 508 else | |
|
1022
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1021
diff
changeset
|
509 _names.put(nameLower, field); |
| 831 | 510 } |
| 511 | |
| 512 /* ------------------------------------------------------------ */ | |
| 513 /** | |
| 514 * Remove a field. | |
| 515 * | |
| 516 * @param name | |
| 517 */ | |
| 518 public void remove(String name) | |
| 519 { | |
|
1022
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1021
diff
changeset
|
520 removeLower(name.toLowerCase()); |
| 831 | 521 } |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
522 |
|
1022
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1021
diff
changeset
|
523 private void removeLower(String nameLower) { |
|
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1021
diff
changeset
|
524 Field field = _names.remove(nameLower); |
| 831 | 525 while (field != null) |
| 526 { | |
| 527 _fields.remove(field); | |
| 528 field = field._next; | |
| 529 } | |
| 530 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
531 |
| 831 | 532 /* -------------------------------------------------------------- */ |
| 533 /** | |
| 534 * Get a header as an long value. Returns the value of an integer field or -1 if not found. The | |
| 535 * case of the field name is ignored. | |
| 536 * | |
| 537 * @param name the case-insensitive field name | |
| 538 * @exception NumberFormatException If bad long found | |
| 539 */ | |
| 540 public long getLongField(String name) throws NumberFormatException | |
| 541 { | |
| 542 Field field = getField(name); | |
| 543 return field==null?-1L:field.getLongValue(); | |
| 544 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
545 |
| 831 | 546 /* -------------------------------------------------------------- */ |
| 547 /** | |
| 548 * Get a header as a date value. Returns the value of a date field, or -1 if not found. The case | |
| 549 * of the field name is ignored. | |
| 550 * | |
| 551 * @param name the case-insensitive field name | |
| 552 */ | |
| 553 public long getDateField(String name) | |
| 554 { | |
| 555 Field field = getField(name); | |
| 556 if (field == null) | |
| 557 return -1; | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
558 |
|
1020
6be43ef1eb96
HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1019
diff
changeset
|
559 String val = valueParameters(field._value, null); |
| 831 | 560 if (val == null) |
| 561 return -1; | |
| 562 | |
| 563 final long date = __dateParser.get().parse(val); | |
| 564 if (date==-1) | |
| 565 throw new IllegalArgumentException("Cannot convert date: " + val); | |
| 566 return date; | |
| 567 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
568 |
| 831 | 569 /* -------------------------------------------------------------- */ |
| 570 /** | |
| 571 * Sets the value of an long field. | |
| 572 * | |
| 573 * @param name the field name | |
| 574 * @param value the field long value | |
| 575 */ | |
|
1022
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1021
diff
changeset
|
576 public void putLongField(String name, long value) |
| 831 | 577 { |
|
1020
6be43ef1eb96
HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1019
diff
changeset
|
578 String v = Long.toString(value); |
| 831 | 579 put(name, v); |
| 580 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
581 |
| 831 | 582 /* -------------------------------------------------------------- */ |
| 583 /** | |
| 584 * Sets the value of an long field. | |
| 585 * | |
| 586 * @param name the field name | |
| 587 * @param value the field long value | |
| 588 */ | |
| 589 public void addLongField(String name, long value) | |
| 590 { | |
|
1020
6be43ef1eb96
HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1019
diff
changeset
|
591 String v = Long.toString(value); |
| 831 | 592 add(name, v); |
| 593 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
594 |
| 831 | 595 /* -------------------------------------------------------------- */ |
| 596 /** | |
| 597 * Sets the value of a date field. | |
| 598 * | |
| 599 * @param name the field name | |
| 600 * @param date the field date value | |
| 601 */ | |
| 602 public void putDateField(String name, long date) | |
| 603 { | |
|
1022
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1021
diff
changeset
|
604 String d = formatDate(date); |
|
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1021
diff
changeset
|
605 put(name, d); |
| 831 | 606 } |
| 607 | |
| 608 /* -------------------------------------------------------------- */ | |
| 609 /** | |
| 610 * Sets the value of a date field. | |
| 611 * | |
| 612 * @param name the field name | |
| 613 * @param date the field date value | |
| 614 */ | |
| 615 public void addDateField(String name, long date) | |
| 616 { | |
|
1022
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1021
diff
changeset
|
617 String d = formatDate(date); |
|
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1021
diff
changeset
|
618 add(name, d); |
| 831 | 619 } |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
620 |
| 831 | 621 /* ------------------------------------------------------------ */ |
| 622 /** | |
| 623 * Format a set cookie value | |
| 624 * | |
| 625 * @param cookie The cookie. | |
| 626 */ | |
| 627 public void addSetCookie(HttpCookie cookie) | |
| 628 { | |
| 629 addSetCookie( | |
| 630 cookie.getName(), | |
| 631 cookie.getValue(), | |
| 632 cookie.getDomain(), | |
| 633 cookie.getPath(), | |
| 634 cookie.getMaxAge(), | |
| 635 cookie.getComment(), | |
| 636 cookie.isSecure(), | |
| 637 cookie.isHttpOnly(), | |
| 638 cookie.getVersion()); | |
| 639 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
640 |
| 831 | 641 /** |
| 642 * Format a set cookie value | |
| 643 * | |
| 644 * @param name the name | |
| 645 * @param value the value | |
| 646 * @param domain the domain | |
| 647 * @param path the path | |
| 648 * @param maxAge the maximum age | |
| 649 * @param comment the comment (only present on versions > 0) | |
| 650 * @param isSecure true if secure cookie | |
| 651 * @param isHttpOnly true if for http only | |
| 652 * @param version version of cookie logic to use (0 == default behavior) | |
| 653 */ | |
| 654 public void addSetCookie( | |
| 655 final String name, | |
| 656 final String value, | |
| 657 final String domain, | |
| 658 final String path, | |
| 659 final long maxAge, | |
| 660 final String comment, | |
| 661 final boolean isSecure, | |
| 662 final boolean isHttpOnly, | |
| 663 int version) | |
| 664 { | |
| 665 String delim=__COOKIE_DELIM; | |
| 666 | |
| 667 // Check arguments | |
| 668 if (name == null || name.length() == 0) | |
| 669 throw new IllegalArgumentException("Bad cookie name"); | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
670 |
| 831 | 671 // Format value and params |
| 672 StringBuilder buf = new StringBuilder(128); | |
| 673 String name_value_params; | |
| 674 QuotedStringTokenizer.quoteIfNeeded(buf, name, delim); | |
| 675 buf.append('='); | |
| 676 String start=buf.toString(); | |
| 677 boolean hasDomain = false; | |
| 678 boolean hasPath = false; | |
| 679 | |
| 680 if (value != null && value.length() > 0) | |
| 681 QuotedStringTokenizer.quoteIfNeeded(buf, value, delim); | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
682 |
| 831 | 683 if (comment != null && comment.length() > 0) |
| 684 { | |
| 685 buf.append(";Comment="); | |
| 686 QuotedStringTokenizer.quoteIfNeeded(buf, comment, delim); | |
| 687 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
688 |
| 831 | 689 if (path != null && path.length() > 0) |
| 690 { | |
| 691 hasPath = true; | |
| 692 buf.append(";Path="); | |
| 693 if (path.trim().startsWith("\"")) | |
| 694 buf.append(path); | |
| 695 else | |
| 696 QuotedStringTokenizer.quoteIfNeeded(buf,path,delim); | |
| 697 } | |
| 698 if (domain != null && domain.length() > 0) | |
| 699 { | |
| 700 hasDomain = true; | |
| 701 buf.append(";Domain="); | |
| 702 QuotedStringTokenizer.quoteIfNeeded(buf,domain.toLowerCase(Locale.ENGLISH),delim); | |
| 703 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
704 |
| 831 | 705 if (maxAge >= 0) |
| 706 { | |
| 707 // Always add the expires param as some browsers still don't handle max-age | |
| 708 buf.append(";Expires="); | |
| 709 if (maxAge == 0) | |
| 710 buf.append(__01Jan1970_COOKIE); | |
| 711 else | |
| 712 formatCookieDate(buf, System.currentTimeMillis() + 1000L * maxAge); | |
| 713 | |
| 714 if (version >0) | |
| 715 { | |
| 716 buf.append(";Max-Age="); | |
| 717 buf.append(maxAge); | |
| 718 } | |
| 719 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
720 |
| 831 | 721 if (isSecure) |
| 722 buf.append(";Secure"); | |
| 723 if (isHttpOnly) | |
| 724 buf.append(";HttpOnly"); | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
725 |
| 831 | 726 name_value_params = buf.toString(); |
| 727 | |
| 728 // remove existing set-cookie of same name | |
| 729 Field field = getField(HttpHeaders.SET_COOKIE); | |
| 730 Field last=null; | |
| 731 while (field!=null) | |
| 732 { | |
| 733 String val = (field._value == null ? null : field._value.toString()); | |
| 734 if (val!=null && val.startsWith(start)) | |
| 735 { | |
| 736 //existing cookie has same name, does it also match domain and path? | |
| 737 if (((!hasDomain && !val.contains("Domain")) || (hasDomain && val.contains("Domain="+domain))) && | |
| 738 ((!hasPath && !val.contains("Path")) || (hasPath && val.contains("Path="+path)))) | |
| 739 { | |
| 740 _fields.remove(field); | |
| 741 if (last==null) | |
|
1022
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1021
diff
changeset
|
742 _names.put(HttpHeaders.SET_COOKIE,field._next); |
| 831 | 743 else |
| 744 last._next=field._next; | |
| 745 break; | |
| 746 } | |
| 747 } | |
| 748 last=field; | |
| 749 field=field._next; | |
| 750 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
751 |
|
1022
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1021
diff
changeset
|
752 add(HttpHeaders.SET_COOKIE, name_value_params); |
| 831 | 753 |
| 754 // Expire responses with set-cookie headers so they do not get cached. | |
|
1022
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1021
diff
changeset
|
755 put(HttpHeaders.EXPIRES, __01Jan1970); |
| 831 | 756 } |
| 757 | |
|
1020
6be43ef1eb96
HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1019
diff
changeset
|
758 @Override |
| 831 | 759 public String toString() |
| 760 { | |
| 761 try | |
| 762 { | |
| 763 StringBuffer buffer = new StringBuffer(); | |
| 764 for (int i = 0; i < _fields.size(); i++) | |
| 765 { | |
| 766 Field field = (Field) _fields.get(i); | |
| 767 if (field != null) | |
| 768 { | |
| 769 String tmp = field.getName(); | |
| 770 if (tmp != null) buffer.append(tmp); | |
| 771 buffer.append(": "); | |
| 772 tmp = field.getValue(); | |
| 773 if (tmp != null) buffer.append(tmp); | |
| 774 buffer.append("\r\n"); | |
| 775 } | |
| 776 } | |
| 777 buffer.append("\r\n"); | |
| 778 return buffer.toString(); | |
| 779 } | |
| 780 catch (Exception e) | |
| 781 { | |
| 782 LOG.warn("",e); | |
| 783 return e.toString(); | |
| 784 } | |
| 785 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
786 |
| 831 | 787 /* ------------------------------------------------------------ */ |
| 788 /** | |
| 789 * Clear the header. | |
| 790 */ | |
| 791 public void clear() | |
| 792 { | |
| 793 _fields.clear(); | |
| 794 _names.clear(); | |
| 795 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
796 |
| 831 | 797 /* ------------------------------------------------------------ */ |
| 798 /** | |
| 799 * Add fields from another HttpFields instance. Single valued fields are replaced, while all | |
| 800 * others are added. | |
| 801 * | |
| 802 * @param fields | |
| 803 */ | |
| 804 public void add(HttpFields fields) | |
| 805 { | |
| 806 if (fields == null) return; | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
807 |
| 831 | 808 Enumeration e = fields.getFieldNames(); |
| 809 while (e.hasMoreElements()) | |
| 810 { | |
| 811 String name = (String) e.nextElement(); | |
| 812 Enumeration values = fields.getValues(name); | |
| 813 while (values.hasMoreElements()) | |
| 814 add(name, (String) values.nextElement()); | |
| 815 } | |
| 816 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
817 |
| 831 | 818 /* ------------------------------------------------------------ */ |
| 819 /** | |
| 820 * Get field value parameters. Some field values can have parameters. This method separates the | |
| 821 * value from the parameters and optionally populates a map with the parameters. For example: | |
| 822 * | |
| 823 * <PRE> | |
| 824 * | |
| 825 * FieldName : Value ; param1=val1 ; param2=val2 | |
| 826 * | |
| 827 * </PRE> | |
| 828 * | |
| 829 * @param value The Field value, possibly with parameteres. | |
| 830 * @param parameters A map to populate with the parameters, or null | |
| 831 * @return The value. | |
| 832 */ | |
| 833 public static String valueParameters(String value, Map<String,String> parameters) | |
| 834 { | |
| 835 if (value == null) return null; | |
| 836 | |
| 837 int i = value.indexOf(';'); | |
| 838 if (i < 0) return value; | |
| 839 if (parameters == null) return value.substring(0, i).trim(); | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
840 |
| 831 | 841 StringTokenizer tok1 = new QuotedStringTokenizer(value.substring(i), ";", false, true); |
| 842 while (tok1.hasMoreTokens()) | |
| 843 { | |
| 844 String token = tok1.nextToken(); | |
| 845 StringTokenizer tok2 = new QuotedStringTokenizer(token, "= "); | |
| 846 if (tok2.hasMoreTokens()) | |
| 847 { | |
| 848 String paramName = tok2.nextToken(); | |
| 849 String paramVal = null; | |
| 850 if (tok2.hasMoreTokens()) paramVal = tok2.nextToken(); | |
| 851 parameters.put(paramName, paramVal); | |
| 852 } | |
| 853 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
854 |
| 831 | 855 return value.substring(0, i).trim(); |
| 856 } | |
| 857 | |
| 858 /* ------------------------------------------------------------ */ | |
| 859 private static final Float __zero = Float.valueOf("0.0"); | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
860 |
| 831 | 861 /* ------------------------------------------------------------ */ |
| 862 public static float getQuality(String value) | |
| 863 { | |
| 864 if (value == null) return 0f; | |
| 865 | |
| 866 if( value.indexOf(";") == -1 ) | |
| 867 return 1f; | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
868 |
| 831 | 869 Map params = new HashMap(); |
| 870 valueParameters(value, params); | |
| 871 String qs = (String) params.get("q"); | |
| 872 try { | |
| 873 return Float.parseFloat(qs); | |
| 874 } catch (NumberFormatException e) { | |
| 875 return 1f; | |
| 876 } | |
| 877 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
878 |
| 831 | 879 /* ------------------------------------------------------------ */ |
| 880 /** | |
| 881 * List values in quality order. | |
| 882 * | |
| 883 * @param e Enumeration of values with quality parameters | |
| 884 * @return values in quality order. | |
| 885 */ | |
| 886 public static List qualityList(Enumeration e) | |
| 887 { | |
| 888 if (e == null || !e.hasMoreElements()) return Collections.EMPTY_LIST; | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
889 |
| 831 | 890 Object list = null; |
| 891 Object qual = null; | |
| 892 | |
| 893 // Assume list will be well ordered and just add nonzero | |
| 894 while (e.hasMoreElements()) | |
| 895 { | |
| 896 String v = e.nextElement().toString(); | |
| 897 Float q = getQuality(v); | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
898 |
| 831 | 899 if (q.floatValue() >= 0.001) |
| 900 { | |
| 901 list = LazyList.add(list, v); | |
| 902 qual = LazyList.add(qual, q); | |
| 903 } | |
| 904 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
905 |
| 831 | 906 List vl = LazyList.getList(list, false); |
| 907 if (vl.size() < 2) return vl; | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
908 |
| 831 | 909 List ql = LazyList.getList(qual, false); |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
910 |
| 831 | 911 // sort list with swaps |
| 912 Float last = __zero; | |
| 913 for (int i = vl.size(); i-- > 0;) | |
| 914 { | |
| 915 Float q = (Float) ql.get(i); | |
| 916 if (last.compareTo(q) > 0) | |
| 917 { | |
| 918 Object tmp = vl.get(i); | |
| 919 vl.set(i, vl.get(i + 1)); | |
| 920 vl.set(i + 1, tmp); | |
| 921 ql.set(i, ql.get(i + 1)); | |
| 922 ql.set(i + 1, q); | |
| 923 last = __zero; | |
| 924 i = vl.size(); | |
| 925 continue; | |
| 926 } | |
| 927 last = q; | |
| 928 } | |
| 929 ql.clear(); | |
| 930 return vl; | |
| 931 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
932 |
|
1020
6be43ef1eb96
HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1019
diff
changeset
|
933 |
| 831 | 934 public static final class Field |
| 935 { | |
|
1022
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1021
diff
changeset
|
936 private final String _name; |
|
1020
6be43ef1eb96
HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1019
diff
changeset
|
937 private final String _value; |
| 831 | 938 private Field _next; |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
939 |
|
1022
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1021
diff
changeset
|
940 private Field(String name, String value) |
| 831 | 941 { |
| 942 _name = name; | |
| 943 _value = value; | |
| 944 _next = null; | |
| 945 } | |
|
1022
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1021
diff
changeset
|
946 |
| 831 | 947 public void putTo(Buffer buffer) throws IOException |
| 948 { | |
|
1022
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1021
diff
changeset
|
949 byte[] nameBytes = StringUtil.getBytes(_name); |
|
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1021
diff
changeset
|
950 if (getNameOrdinal() >=0 ) |
|
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1021
diff
changeset
|
951 buffer.put(nameBytes); |
| 831 | 952 else |
| 953 { | |
|
1022
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1021
diff
changeset
|
954 for( byte b : nameBytes ) { |
| 831 | 955 switch(b) |
| 956 { | |
| 957 case '\r': | |
| 958 case '\n': | |
| 959 case ':' : | |
| 960 continue; | |
| 961 default: | |
| 962 buffer.put(b); | |
| 963 } | |
| 964 } | |
| 965 } | |
| 966 | |
| 967 buffer.put((byte) ':'); | |
| 968 buffer.put((byte) ' '); | |
| 969 | |
|
1020
6be43ef1eb96
HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1019
diff
changeset
|
970 byte[] valueBytes = StringUtil.getBytes(_value); |
|
1022
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1021
diff
changeset
|
971 if (getValueOrdinal() >= 0) |
|
1020
6be43ef1eb96
HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1019
diff
changeset
|
972 buffer.put(valueBytes); |
| 831 | 973 else |
| 974 { | |
|
1020
6be43ef1eb96
HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1019
diff
changeset
|
975 for( byte b : valueBytes ) { |
| 831 | 976 switch(b) |
| 977 { | |
| 978 case '\r': | |
| 979 case '\n': | |
| 980 continue; | |
| 981 default: | |
| 982 buffer.put(b); | |
| 983 } | |
| 984 } | |
| 985 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
986 |
| 831 | 987 BufferUtil.putCRLF(buffer); |
| 988 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
989 |
|
1020
6be43ef1eb96
HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1019
diff
changeset
|
990 private String getName() |
| 831 | 991 { |
| 1021 | 992 return _name.toString(); |
| 831 | 993 } |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
994 |
| 831 | 995 public int getNameOrdinal() |
| 996 { | |
| 997 return HttpHeaders.CACHE.getOrdinal(_name); | |
| 998 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
999 |
| 831 | 1000 public String getValue() |
| 1001 { | |
| 1002 return _value; | |
| 1003 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1004 |
| 831 | 1005 public int getValueOrdinal() |
| 1006 { | |
|
1022
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1021
diff
changeset
|
1007 return HttpHeaderValues.CACHE.getOrdinal(_value); |
| 831 | 1008 } |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1009 |
| 831 | 1010 public int getIntValue() |
| 1011 { | |
| 1012 return (int) getLongValue(); | |
| 1013 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1014 |
| 831 | 1015 public long getLongValue() |
| 1016 { | |
| 1017 return BufferUtil.toLong(_value); | |
| 1018 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1019 |
|
1020
6be43ef1eb96
HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1019
diff
changeset
|
1020 @Override |
| 831 | 1021 public String toString() |
| 1022 { | |
| 1023 return ("[" + getName() + "=" + _value + (_next == null ? "" : "->") + "]"); | |
| 1024 } | |
| 1025 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1026 } |
