Mercurial Hosting > luan
annotate src/org/eclipse/jetty/http/HttpFields.java @ 980:bd26bd9320e2
simplify HttpParser
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 16 Oct 2016 21:01:26 -0600 |
parents | fa6158f29c45 |
children | f126d30e04a4 |
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.BufferCache; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
44 import org.eclipse.jetty.io.BufferCache.CachedBuffer; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
45 import org.eclipse.jetty.io.BufferUtil; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
46 import org.eclipse.jetty.io.ByteArrayBuffer; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
47 import org.eclipse.jetty.util.LazyList; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
48 import org.eclipse.jetty.util.QuotedStringTokenizer; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
49 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
|
50 import org.slf4j.Logger; |
8e9db0bbf4f9
remove org.eclipse.jetty.util.log and upgrade slf4j
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
51 import org.slf4j.LoggerFactory; |
802
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 /* ------------------------------------------------------------ */ |
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 * 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
|
56 * |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
57 * <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
|
58 * single thread. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
59 * |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
60 * |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
61 */ |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
62 public class HttpFields |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
63 { |
831 | 64 private static final Logger LOG = LoggerFactory.getLogger(HttpFields.class); |
65 | |
66 /* ------------------------------------------------------------ */ | |
67 public static final String __COOKIE_DELIM="\"\\\n\r\t\f\b%+ ;="; | |
68 public static final TimeZone __GMT = TimeZone.getTimeZone("GMT"); | |
69 | |
70 /* ------------------------------------------------------------ */ | |
71 private static final String[] DAYS = | |
72 { "Sat", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; | |
73 private static final String[] MONTHS = | |
74 { "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
|
75 |
831 | 76 |
77 /* ------------------------------------------------------------ */ | |
78 private static class DateGenerator | |
79 { | |
80 private final StringBuilder buf = new StringBuilder(32); | |
81 private final GregorianCalendar gc = new GregorianCalendar(__GMT); | |
82 | |
83 /** | |
84 * Format HTTP date "EEE, dd MMM yyyy HH:mm:ss 'GMT'" | |
85 */ | |
86 public String formatDate(long date) | |
87 { | |
88 buf.setLength(0); | |
89 gc.setTimeInMillis(date); | |
90 | |
91 int day_of_week = gc.get(Calendar.DAY_OF_WEEK); | |
92 int day_of_month = gc.get(Calendar.DAY_OF_MONTH); | |
93 int month = gc.get(Calendar.MONTH); | |
94 int year = gc.get(Calendar.YEAR); | |
95 int century = year / 100; | |
96 year = year % 100; | |
97 | |
98 int hours = gc.get(Calendar.HOUR_OF_DAY); | |
99 int minutes = gc.get(Calendar.MINUTE); | |
100 int seconds = gc.get(Calendar.SECOND); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
101 |
831 | 102 buf.append(DAYS[day_of_week]); |
103 buf.append(','); | |
104 buf.append(' '); | |
105 StringUtil.append2digits(buf, day_of_month); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
106 |
831 | 107 buf.append(' '); |
108 buf.append(MONTHS[month]); | |
109 buf.append(' '); | |
110 StringUtil.append2digits(buf, century); | |
111 StringUtil.append2digits(buf, year); | |
112 | |
113 buf.append(' '); | |
114 StringUtil.append2digits(buf, hours); | |
115 buf.append(':'); | |
116 StringUtil.append2digits(buf, minutes); | |
117 buf.append(':'); | |
118 StringUtil.append2digits(buf, seconds); | |
119 buf.append(" GMT"); | |
120 return buf.toString(); | |
121 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
122 |
831 | 123 /* ------------------------------------------------------------ */ |
124 /** | |
125 * Format "EEE, dd-MMM-yy HH:mm:ss 'GMT'" for cookies | |
126 */ | |
127 public void formatCookieDate(StringBuilder buf, long date) | |
128 { | |
129 gc.setTimeInMillis(date); | |
130 | |
131 int day_of_week = gc.get(Calendar.DAY_OF_WEEK); | |
132 int day_of_month = gc.get(Calendar.DAY_OF_MONTH); | |
133 int month = gc.get(Calendar.MONTH); | |
134 int year = gc.get(Calendar.YEAR); | |
135 year = year % 10000; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
136 |
831 | 137 int epoch = (int) ((date / 1000) % (60 * 60 * 24)); |
138 int seconds = epoch % 60; | |
139 epoch = epoch / 60; | |
140 int minutes = epoch % 60; | |
141 int hours = epoch / 60; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
142 |
831 | 143 buf.append(DAYS[day_of_week]); |
144 buf.append(','); | |
145 buf.append(' '); | |
146 StringUtil.append2digits(buf, day_of_month); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
147 |
831 | 148 buf.append('-'); |
149 buf.append(MONTHS[month]); | |
150 buf.append('-'); | |
151 StringUtil.append2digits(buf, year/100); | |
152 StringUtil.append2digits(buf, year%100); | |
153 | |
154 buf.append(' '); | |
155 StringUtil.append2digits(buf, hours); | |
156 buf.append(':'); | |
157 StringUtil.append2digits(buf, minutes); | |
158 buf.append(':'); | |
159 StringUtil.append2digits(buf, seconds); | |
160 buf.append(" GMT"); | |
161 } | |
162 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
163 |
831 | 164 /* ------------------------------------------------------------ */ |
165 private static final ThreadLocal<DateGenerator> __dateGenerator =new ThreadLocal<DateGenerator>() | |
166 { | |
167 @Override | |
168 protected DateGenerator initialValue() | |
169 { | |
170 return new DateGenerator(); | |
171 } | |
172 }; | |
173 | |
174 /* ------------------------------------------------------------ */ | |
175 /** | |
176 * Format HTTP date "EEE, dd MMM yyyy HH:mm:ss 'GMT'" | |
177 */ | |
178 public static String formatDate(long date) | |
179 { | |
180 return __dateGenerator.get().formatDate(date); | |
181 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
182 |
831 | 183 /* ------------------------------------------------------------ */ |
184 /** | |
185 * Format "EEE, dd-MMM-yyyy HH:mm:ss 'GMT'" for cookies | |
186 */ | |
187 public static void formatCookieDate(StringBuilder buf, long date) | |
188 { | |
189 __dateGenerator.get().formatCookieDate(buf,date); | |
190 } | |
191 | |
192 /* ------------------------------------------------------------ */ | |
193 /** | |
194 * Format "EEE, dd-MMM-yyyy HH:mm:ss 'GMT'" for cookies | |
195 */ | |
196 public static String formatCookieDate(long date) | |
197 { | |
198 StringBuilder buf = new StringBuilder(28); | |
199 formatCookieDate(buf, date); | |
200 return buf.toString(); | |
201 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
202 |
831 | 203 /* ------------------------------------------------------------ */ |
204 private final static String __dateReceiveFmt[] = | |
205 { | |
206 "EEE, dd MMM yyyy HH:mm:ss zzz", | |
207 "EEE, dd-MMM-yy HH:mm:ss", | |
208 "EEE MMM dd HH:mm:ss yyyy", | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
209 |
831 | 210 "EEE, dd MMM yyyy HH:mm:ss", "EEE dd MMM yyyy HH:mm:ss zzz", |
211 "EEE dd MMM yyyy HH:mm:ss", "EEE MMM dd yyyy HH:mm:ss zzz", "EEE MMM dd yyyy HH:mm:ss", | |
212 "EEE MMM-dd-yyyy HH:mm:ss zzz", "EEE MMM-dd-yyyy HH:mm:ss", "dd MMM yyyy HH:mm:ss zzz", | |
213 "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", | |
214 "MMM dd HH:mm:ss yyyy", "EEE MMM dd HH:mm:ss yyyy zzz", | |
215 "EEE, MMM dd HH:mm:ss yyyy zzz", "EEE, MMM dd HH:mm:ss yyyy", "EEE, dd-MMM-yy HH:mm:ss zzz", | |
216 "EEE dd-MMM-yy HH:mm:ss zzz", "EEE dd-MMM-yy HH:mm:ss", | |
217 }; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
218 |
831 | 219 /* ------------------------------------------------------------ */ |
220 private static class DateParser | |
221 { | |
222 final SimpleDateFormat _dateReceive[]= new SimpleDateFormat[__dateReceiveFmt.length]; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
223 |
831 | 224 long parse(final String dateVal) |
225 { | |
226 for (int i = 0; i < _dateReceive.length; i++) | |
227 { | |
228 if (_dateReceive[i] == null) | |
229 { | |
230 _dateReceive[i] = new SimpleDateFormat(__dateReceiveFmt[i], Locale.US); | |
231 _dateReceive[i].setTimeZone(__GMT); | |
232 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
233 |
831 | 234 try |
235 { | |
236 Date date = (Date) _dateReceive[i].parseObject(dateVal); | |
237 return date.getTime(); | |
238 } | |
239 catch (java.lang.Exception e) | |
240 { | |
241 // LOG.ignore(e); | |
242 } | |
243 } | |
244 | |
245 if (dateVal.endsWith(" GMT")) | |
246 { | |
247 final String val = dateVal.substring(0, dateVal.length() - 4); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
248 |
831 | 249 for (int i = 0; i < _dateReceive.length; i++) |
250 { | |
251 try | |
252 { | |
253 Date date = (Date) _dateReceive[i].parseObject(val); | |
254 return date.getTime(); | |
255 } | |
256 catch (java.lang.Exception e) | |
257 { | |
258 // LOG.ignore(e); | |
259 } | |
260 } | |
261 } | |
262 return -1; | |
263 } | |
264 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
265 |
831 | 266 /* ------------------------------------------------------------ */ |
267 public static long parseDate(String date) | |
268 { | |
269 return __dateParser.get().parse(date); | |
270 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
271 |
831 | 272 /* ------------------------------------------------------------ */ |
273 private static final ThreadLocal<DateParser> __dateParser =new ThreadLocal<DateParser>() | |
274 { | |
275 @Override | |
276 protected DateParser initialValue() | |
277 { | |
278 return new DateParser(); | |
279 } | |
280 }; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
281 |
831 | 282 /* -------------------------------------------------------------- */ |
283 public final static String __01Jan1970=formatDate(0); | |
284 public final static Buffer __01Jan1970_BUFFER=new ByteArrayBuffer(__01Jan1970); | |
285 public final static String __01Jan1970_COOKIE = formatCookieDate(0).trim(); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
286 |
831 | 287 /* -------------------------------------------------------------- */ |
288 private final ArrayList<Field> _fields = new ArrayList<Field>(20); | |
289 private final HashMap<Buffer,Field> _names = new HashMap<Buffer,Field>(32); | |
290 | |
291 /* ------------------------------------------------------------ */ | |
292 /** | |
293 * Constructor. | |
294 */ | |
295 public HttpFields() | |
296 { | |
297 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
298 |
831 | 299 // TODO externalize this cache so it can be configurable |
300 private static ConcurrentMap<String, Buffer> __cache = new ConcurrentHashMap<String, Buffer>(); | |
301 private static int __cacheSize = Integer.getInteger("org.eclipse.jetty.http.HttpFields.CACHE",2000); | |
302 | |
303 /* -------------------------------------------------------------- */ | |
304 private Buffer convertValue(String value) | |
305 { | |
306 Buffer buffer = __cache.get(value); | |
307 if (buffer!=null) | |
308 return buffer; | |
309 | |
310 try | |
311 { | |
312 buffer = new ByteArrayBuffer(value,StringUtil.__ISO_8859_1); | |
313 | |
314 if (__cacheSize>0) | |
315 { | |
316 if (__cache.size()>__cacheSize) | |
317 __cache.clear(); | |
318 Buffer b=__cache.putIfAbsent(value,buffer); | |
319 if (b!=null) | |
320 buffer=b; | |
321 } | |
322 | |
323 return buffer; | |
324 } | |
325 catch (UnsupportedEncodingException e) | |
326 { | |
327 throw new RuntimeException(e); | |
328 } | |
329 } | |
330 | |
331 /* -------------------------------------------------------------- */ | |
332 /** | |
333 * Get Collection of header names. | |
334 */ | |
335 public Collection<String> getFieldNamesCollection() | |
336 { | |
337 final List<String> list = new ArrayList<String>(_fields.size()); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
338 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
339 for (Field f : _fields) |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
340 { |
831 | 341 if (f!=null) |
342 list.add(BufferUtil.to8859_1_String(f._name)); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
343 } |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
344 return list; |
831 | 345 } |
346 | |
347 /* -------------------------------------------------------------- */ | |
348 /** | |
349 * Get enumeration of header _names. Returns an enumeration of strings representing the header | |
350 * _names for this request. | |
351 */ | |
352 public Enumeration<String> getFieldNames() | |
353 { | |
354 final Enumeration<?> buffers = Collections.enumeration(_names.keySet()); | |
355 return new Enumeration<String>() | |
356 { | |
357 public String nextElement() | |
358 { | |
359 return buffers.nextElement().toString(); | |
360 } | |
361 | |
362 public boolean hasMoreElements() | |
363 { | |
364 return buffers.hasMoreElements(); | |
365 } | |
366 }; | |
367 } | |
368 | |
369 /* ------------------------------------------------------------ */ | |
370 public int size() | |
371 { | |
372 return _fields.size(); | |
373 } | |
374 | |
375 /* ------------------------------------------------------------ */ | |
376 /** | |
377 * Get a Field by index. | |
378 * @return A Field value or null if the Field value has not been set | |
379 * | |
380 */ | |
381 public Field getField(int i) | |
382 { | |
383 return _fields.get(i); | |
384 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
385 |
831 | 386 /* ------------------------------------------------------------ */ |
387 private Field getField(String name) | |
388 { | |
389 return _names.get(HttpHeaders.CACHE.lookup(name)); | |
390 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
391 |
831 | 392 /* ------------------------------------------------------------ */ |
393 private Field getField(Buffer name) | |
394 { | |
395 return _names.get(HttpHeaders.CACHE.lookup(name)); | |
396 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
397 |
831 | 398 /* ------------------------------------------------------------ */ |
399 public boolean containsKey(Buffer name) | |
400 { | |
401 return _names.containsKey(HttpHeaders.CACHE.lookup(name)); | |
402 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
403 |
831 | 404 /* ------------------------------------------------------------ */ |
405 public boolean containsKey(String name) | |
406 { | |
407 return _names.containsKey(HttpHeaders.CACHE.lookup(name)); | |
408 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
409 |
831 | 410 /* -------------------------------------------------------------- */ |
411 /** | |
412 * @return the value of a field, or null if not found. For multiple fields of the same name, | |
413 * only the first is returned. | |
414 * @param name the case-insensitive field name | |
415 */ | |
416 public String getStringField(String name) | |
417 { | |
418 Field field = getField(name); | |
419 return field==null?null:field.getValue(); | |
420 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
421 |
831 | 422 /* -------------------------------------------------------------- */ |
423 /** | |
424 * @return the value of a field, or null if not found. For multiple fields of the same name, | |
425 * only the first is returned. | |
426 * @param name the case-insensitive field name | |
427 */ | |
428 public String getStringField(Buffer name) | |
429 { | |
430 Field field = getField(name); | |
431 return field==null?null:field.getValue(); | |
432 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
433 |
831 | 434 /* -------------------------------------------------------------- */ |
435 /** | |
436 * @return the value of a field, or null if not found. For multiple fields of the same name, | |
437 * only the first is returned. | |
438 * @param name the case-insensitive field name | |
439 */ | |
440 public Buffer get(Buffer name) | |
441 { | |
442 Field field = getField(name); | |
443 return field==null?null:field._value; | |
444 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
445 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
446 |
831 | 447 /* -------------------------------------------------------------- */ |
448 /** | |
449 * Get multi headers | |
450 * | |
451 * @return Enumeration of the values, or null if no such header. | |
452 * @param name the case-insensitive field name | |
453 */ | |
454 public Collection<String> getValuesCollection(String name) | |
455 { | |
456 Field field = getField(name); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
457 if (field==null) |
831 | 458 return null; |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
459 |
831 | 460 final List<String> list = new ArrayList<String>(); |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
461 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
462 while(field!=null) |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
463 { |
831 | 464 list.add(field.getValue()); |
465 field=field._next; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
466 } |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
467 return list; |
831 | 468 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
469 |
831 | 470 /* -------------------------------------------------------------- */ |
471 /** | |
472 * Get multi headers | |
473 * | |
474 * @return Enumeration of the values | |
475 * @param name the case-insensitive field name | |
476 */ | |
477 public Enumeration<String> getValues(String name) | |
478 { | |
479 final Field field = getField(name); | |
480 if (field == null) | |
481 { | |
482 List<String> empty=Collections.emptyList(); | |
483 return Collections.enumeration(empty); | |
484 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
485 |
831 | 486 return new Enumeration<String>() |
487 { | |
488 Field f = field; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
489 |
831 | 490 public boolean hasMoreElements() |
491 { | |
492 return f != null; | |
493 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
494 |
831 | 495 public String nextElement() throws NoSuchElementException |
496 { | |
497 if (f == null) throw new NoSuchElementException(); | |
498 Field n = f; | |
499 f = f._next; | |
500 return n.getValue(); | |
501 } | |
502 }; | |
503 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
504 |
831 | 505 /* -------------------------------------------------------------- */ |
506 /** | |
507 * Get multi headers | |
508 * | |
509 * @return Enumeration of the value Strings | |
510 * @param name the case-insensitive field name | |
511 */ | |
512 public Enumeration<String> getValues(Buffer name) | |
513 { | |
514 final Field field = getField(name); | |
515 if (field == null) | |
516 { | |
517 List<String> empty=Collections.emptyList(); | |
518 return Collections.enumeration(empty); | |
519 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
520 |
831 | 521 return new Enumeration<String>() |
522 { | |
523 Field f = field; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
524 |
831 | 525 public boolean hasMoreElements() |
526 { | |
527 return f != null; | |
528 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
529 |
831 | 530 public String nextElement() throws NoSuchElementException |
531 { | |
532 if (f == null) throw new NoSuchElementException(); | |
533 Field n = f; | |
534 f = f._next; | |
535 return n.getValue(); | |
536 } | |
537 }; | |
538 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
539 |
831 | 540 /* -------------------------------------------------------------- */ |
541 /** | |
542 * Get multi field values with separator. The multiple values can be represented as separate | |
543 * headers of the same name, or by a single header using the separator(s), or a combination of | |
544 * both. Separators may be quoted. | |
545 * | |
546 * @param name the case-insensitive field name | |
547 * @param separators String of separators. | |
548 * @return Enumeration of the values, or null if no such header. | |
549 */ | |
550 public Enumeration<String> getValues(String name, final String separators) | |
551 { | |
552 final Enumeration<String> e = getValues(name); | |
553 if (e == null) | |
554 return null; | |
555 return new Enumeration<String>() | |
556 { | |
557 QuotedStringTokenizer tok = null; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
558 |
831 | 559 public boolean hasMoreElements() |
560 { | |
561 if (tok != null && tok.hasMoreElements()) return true; | |
562 while (e.hasMoreElements()) | |
563 { | |
564 String value = e.nextElement(); | |
565 tok = new QuotedStringTokenizer(value, separators, false, false); | |
566 if (tok.hasMoreElements()) return true; | |
567 } | |
568 tok = null; | |
569 return false; | |
570 } | |
571 | |
572 public String nextElement() throws NoSuchElementException | |
573 { | |
574 if (!hasMoreElements()) throw new NoSuchElementException(); | |
575 String next = (String) tok.nextElement(); | |
576 if (next != null) next = next.trim(); | |
577 return next; | |
578 } | |
579 }; | |
580 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
581 |
831 | 582 |
583 /* -------------------------------------------------------------- */ | |
584 /** | |
585 * Set a field. | |
586 * | |
587 * @param name the name of the field | |
588 * @param value the value of the field. If null the field is cleared. | |
589 */ | |
590 public void put(String name, String value) | |
591 { | |
592 if (value==null) | |
593 remove(name); | |
594 else | |
595 { | |
596 Buffer n = HttpHeaders.CACHE.lookup(name); | |
597 Buffer v = convertValue(value); | |
598 put(n, v); | |
599 } | |
600 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
601 |
831 | 602 /* -------------------------------------------------------------- */ |
603 /** | |
604 * Set a field. | |
605 * | |
606 * @param name the name of the field | |
607 * @param value the value of the field. If null the field is cleared. | |
608 */ | |
609 public void put(Buffer name, String value) | |
610 { | |
611 Buffer n = HttpHeaders.CACHE.lookup(name); | |
612 Buffer v = convertValue(value); | |
613 put(n, v); | |
614 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
615 |
831 | 616 /* -------------------------------------------------------------- */ |
617 /** | |
618 * Set a field. | |
619 * | |
620 * @param name the name of the field | |
621 * @param value the value of the field. If null the field is cleared. | |
622 */ | |
623 public void put(Buffer name, Buffer value) | |
624 { | |
625 remove(name); | |
626 if (value == null) | |
627 return; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
628 |
831 | 629 if (!(name instanceof BufferCache.CachedBuffer)) |
630 name = HttpHeaders.CACHE.lookup(name); | |
631 if (!(value instanceof CachedBuffer)) | |
632 value= HttpHeaderValues.CACHE.lookup(value).asImmutableBuffer(); | |
633 | |
634 // new value; | |
635 Field field = new Field(name, value); | |
636 _fields.add(field); | |
637 _names.put(name, field); | |
638 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
639 |
831 | 640 /* -------------------------------------------------------------- */ |
641 /** | |
642 * Set a field. | |
643 * | |
644 * @param name the name of the field | |
645 * @param list the List value of the field. If null the field is cleared. | |
646 */ | |
647 public void put(String name, List<?> list) | |
648 { | |
649 if (list == null || list.size() == 0) | |
650 { | |
651 remove(name); | |
652 return; | |
653 } | |
654 Buffer n = HttpHeaders.CACHE.lookup(name); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
655 |
831 | 656 Object v = list.get(0); |
657 if (v != null) | |
658 put(n, HttpHeaderValues.CACHE.lookup(v.toString())); | |
659 else | |
660 remove(n); | |
661 | |
662 if (list.size() > 1) | |
663 { | |
664 java.util.Iterator<?> iter = list.iterator(); | |
665 iter.next(); | |
666 while (iter.hasNext()) | |
667 { | |
668 v = iter.next(); | |
669 if (v != null) put(n, HttpHeaderValues.CACHE.lookup(v.toString())); | |
670 } | |
671 } | |
672 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
673 |
831 | 674 /* -------------------------------------------------------------- */ |
675 /** | |
676 * Add to or set a field. If the field is allowed to have multiple values, add will add multiple | |
677 * headers of the same name. | |
678 * | |
679 * @param name the name of the field | |
680 * @param value the value of the field. | |
681 * @exception IllegalArgumentException If the name is a single valued field and already has a | |
682 * value. | |
683 */ | |
684 public void add(String name, String value) throws IllegalArgumentException | |
685 { | |
686 if (value==null) | |
687 return; | |
688 Buffer n = HttpHeaders.CACHE.lookup(name); | |
689 Buffer v = convertValue(value); | |
690 add(n, v); | |
691 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
692 |
831 | 693 /* -------------------------------------------------------------- */ |
694 /** | |
695 * Add to or set a field. If the field is allowed to have multiple values, add will add multiple | |
696 * headers of the same name. | |
697 * | |
698 * @param name the name of the field | |
699 * @param value the value of the field. | |
700 * @exception IllegalArgumentException If the name is a single valued field and already has a | |
701 * value. | |
702 */ | |
703 public void add(Buffer name, Buffer value) throws IllegalArgumentException | |
704 { | |
705 if (value == null) throw new IllegalArgumentException("null value"); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
706 |
831 | 707 if (!(name instanceof CachedBuffer)) |
708 name = HttpHeaders.CACHE.lookup(name); | |
709 name=name.asImmutableBuffer(); | |
710 | |
711 if (!(value instanceof CachedBuffer) && HttpHeaderValues.hasKnownValues(HttpHeaders.CACHE.getOrdinal(name))) | |
712 value= HttpHeaderValues.CACHE.lookup(value); | |
713 value=value.asImmutableBuffer(); | |
714 | |
715 Field field = _names.get(name); | |
716 Field last = null; | |
717 while (field != null) | |
718 { | |
719 last = field; | |
720 field = field._next; | |
721 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
722 |
831 | 723 // create the field |
724 field = new Field(name, value); | |
725 _fields.add(field); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
726 |
831 | 727 // look for chain to add too |
728 if (last != null) | |
729 last._next = field; | |
730 else | |
731 _names.put(name, field); | |
732 } | |
733 | |
734 /* ------------------------------------------------------------ */ | |
735 /** | |
736 * Remove a field. | |
737 * | |
738 * @param name | |
739 */ | |
740 public void remove(String name) | |
741 { | |
742 remove(HttpHeaders.CACHE.lookup(name)); | |
743 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
744 |
831 | 745 /* ------------------------------------------------------------ */ |
746 /** | |
747 * Remove a field. | |
748 * | |
749 * @param name | |
750 */ | |
751 public void remove(Buffer name) | |
752 { | |
753 if (!(name instanceof BufferCache.CachedBuffer)) | |
754 name = HttpHeaders.CACHE.lookup(name); | |
755 Field field = _names.remove(name); | |
756 while (field != null) | |
757 { | |
758 _fields.remove(field); | |
759 field = field._next; | |
760 } | |
761 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
762 |
831 | 763 /* -------------------------------------------------------------- */ |
764 /** | |
765 * Get a header as an long value. Returns the value of an integer field or -1 if not found. The | |
766 * case of the field name is ignored. | |
767 * | |
768 * @param name the case-insensitive field name | |
769 * @exception NumberFormatException If bad long found | |
770 */ | |
771 public long getLongField(String name) throws NumberFormatException | |
772 { | |
773 Field field = getField(name); | |
774 return field==null?-1L:field.getLongValue(); | |
775 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
776 |
831 | 777 /* -------------------------------------------------------------- */ |
778 /** | |
779 * Get a header as an long value. Returns the value of an integer field or -1 if not found. The | |
780 * case of the field name is ignored. | |
781 * | |
782 * @param name the case-insensitive field name | |
783 * @exception NumberFormatException If bad long found | |
784 */ | |
785 public long getLongField(Buffer name) throws NumberFormatException | |
786 { | |
787 Field field = getField(name); | |
788 return field==null?-1L:field.getLongValue(); | |
789 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
790 |
831 | 791 /* -------------------------------------------------------------- */ |
792 /** | |
793 * Get a header as a date value. Returns the value of a date field, or -1 if not found. The case | |
794 * of the field name is ignored. | |
795 * | |
796 * @param name the case-insensitive field name | |
797 */ | |
798 public long getDateField(String name) | |
799 { | |
800 Field field = getField(name); | |
801 if (field == null) | |
802 return -1; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
803 |
831 | 804 String val = valueParameters(BufferUtil.to8859_1_String(field._value), null); |
805 if (val == null) | |
806 return -1; | |
807 | |
808 final long date = __dateParser.get().parse(val); | |
809 if (date==-1) | |
810 throw new IllegalArgumentException("Cannot convert date: " + val); | |
811 return date; | |
812 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
813 |
831 | 814 /* -------------------------------------------------------------- */ |
815 /** | |
816 * Sets the value of an long field. | |
817 * | |
818 * @param name the field name | |
819 * @param value the field long value | |
820 */ | |
821 public void putLongField(Buffer name, long value) | |
822 { | |
823 Buffer v = BufferUtil.toBuffer(value); | |
824 put(name, v); | |
825 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
826 |
831 | 827 /* -------------------------------------------------------------- */ |
828 /** | |
829 * Sets the value of an long field. | |
830 * | |
831 * @param name the field name | |
832 * @param value the field long value | |
833 */ | |
834 public void putLongField(String name, long value) | |
835 { | |
836 Buffer n = HttpHeaders.CACHE.lookup(name); | |
837 Buffer v = BufferUtil.toBuffer(value); | |
838 put(n, v); | |
839 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
840 |
831 | 841 /* -------------------------------------------------------------- */ |
842 /** | |
843 * Sets the value of an long field. | |
844 * | |
845 * @param name the field name | |
846 * @param value the field long value | |
847 */ | |
848 public void addLongField(String name, long value) | |
849 { | |
850 Buffer n = HttpHeaders.CACHE.lookup(name); | |
851 Buffer v = BufferUtil.toBuffer(value); | |
852 add(n, v); | |
853 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
854 |
831 | 855 /* -------------------------------------------------------------- */ |
856 /** | |
857 * Sets the value of an long field. | |
858 * | |
859 * @param name the field name | |
860 * @param value the field long value | |
861 */ | |
862 public void addLongField(Buffer name, long value) | |
863 { | |
864 Buffer v = BufferUtil.toBuffer(value); | |
865 add(name, v); | |
866 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
867 |
831 | 868 /* -------------------------------------------------------------- */ |
869 /** | |
870 * Sets the value of a date field. | |
871 * | |
872 * @param name the field name | |
873 * @param date the field date value | |
874 */ | |
875 public void putDateField(Buffer name, long date) | |
876 { | |
877 String d=formatDate(date); | |
878 Buffer v = new ByteArrayBuffer(d); | |
879 put(name, v); | |
880 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
881 |
831 | 882 /* -------------------------------------------------------------- */ |
883 /** | |
884 * Sets the value of a date field. | |
885 * | |
886 * @param name the field name | |
887 * @param date the field date value | |
888 */ | |
889 public void putDateField(String name, long date) | |
890 { | |
891 Buffer n = HttpHeaders.CACHE.lookup(name); | |
892 putDateField(n,date); | |
893 } | |
894 | |
895 /* -------------------------------------------------------------- */ | |
896 /** | |
897 * Sets the value of a date field. | |
898 * | |
899 * @param name the field name | |
900 * @param date the field date value | |
901 */ | |
902 public void addDateField(String name, long date) | |
903 { | |
904 String d=formatDate(date); | |
905 Buffer n = HttpHeaders.CACHE.lookup(name); | |
906 Buffer v = new ByteArrayBuffer(d); | |
907 add(n, v); | |
908 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
909 |
831 | 910 /* ------------------------------------------------------------ */ |
911 /** | |
912 * Format a set cookie value | |
913 * | |
914 * @param cookie The cookie. | |
915 */ | |
916 public void addSetCookie(HttpCookie cookie) | |
917 { | |
918 addSetCookie( | |
919 cookie.getName(), | |
920 cookie.getValue(), | |
921 cookie.getDomain(), | |
922 cookie.getPath(), | |
923 cookie.getMaxAge(), | |
924 cookie.getComment(), | |
925 cookie.isSecure(), | |
926 cookie.isHttpOnly(), | |
927 cookie.getVersion()); | |
928 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
929 |
831 | 930 /** |
931 * Format a set cookie value | |
932 * | |
933 * @param name the name | |
934 * @param value the value | |
935 * @param domain the domain | |
936 * @param path the path | |
937 * @param maxAge the maximum age | |
938 * @param comment the comment (only present on versions > 0) | |
939 * @param isSecure true if secure cookie | |
940 * @param isHttpOnly true if for http only | |
941 * @param version version of cookie logic to use (0 == default behavior) | |
942 */ | |
943 public void addSetCookie( | |
944 final String name, | |
945 final String value, | |
946 final String domain, | |
947 final String path, | |
948 final long maxAge, | |
949 final String comment, | |
950 final boolean isSecure, | |
951 final boolean isHttpOnly, | |
952 int version) | |
953 { | |
954 String delim=__COOKIE_DELIM; | |
955 | |
956 // Check arguments | |
957 if (name == null || name.length() == 0) | |
958 throw new IllegalArgumentException("Bad cookie name"); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
959 |
831 | 960 // Format value and params |
961 StringBuilder buf = new StringBuilder(128); | |
962 String name_value_params; | |
963 QuotedStringTokenizer.quoteIfNeeded(buf, name, delim); | |
964 buf.append('='); | |
965 String start=buf.toString(); | |
966 boolean hasDomain = false; | |
967 boolean hasPath = false; | |
968 | |
969 if (value != null && value.length() > 0) | |
970 QuotedStringTokenizer.quoteIfNeeded(buf, value, delim); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
971 |
831 | 972 if (comment != null && comment.length() > 0) |
973 { | |
974 buf.append(";Comment="); | |
975 QuotedStringTokenizer.quoteIfNeeded(buf, comment, delim); | |
976 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
977 |
831 | 978 if (path != null && path.length() > 0) |
979 { | |
980 hasPath = true; | |
981 buf.append(";Path="); | |
982 if (path.trim().startsWith("\"")) | |
983 buf.append(path); | |
984 else | |
985 QuotedStringTokenizer.quoteIfNeeded(buf,path,delim); | |
986 } | |
987 if (domain != null && domain.length() > 0) | |
988 { | |
989 hasDomain = true; | |
990 buf.append(";Domain="); | |
991 QuotedStringTokenizer.quoteIfNeeded(buf,domain.toLowerCase(Locale.ENGLISH),delim); | |
992 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
993 |
831 | 994 if (maxAge >= 0) |
995 { | |
996 // Always add the expires param as some browsers still don't handle max-age | |
997 buf.append(";Expires="); | |
998 if (maxAge == 0) | |
999 buf.append(__01Jan1970_COOKIE); | |
1000 else | |
1001 formatCookieDate(buf, System.currentTimeMillis() + 1000L * maxAge); | |
1002 | |
1003 if (version >0) | |
1004 { | |
1005 buf.append(";Max-Age="); | |
1006 buf.append(maxAge); | |
1007 } | |
1008 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1009 |
831 | 1010 if (isSecure) |
1011 buf.append(";Secure"); | |
1012 if (isHttpOnly) | |
1013 buf.append(";HttpOnly"); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1014 |
831 | 1015 name_value_params = buf.toString(); |
1016 | |
1017 // remove existing set-cookie of same name | |
1018 Field field = getField(HttpHeaders.SET_COOKIE); | |
1019 Field last=null; | |
1020 while (field!=null) | |
1021 { | |
1022 String val = (field._value == null ? null : field._value.toString()); | |
1023 if (val!=null && val.startsWith(start)) | |
1024 { | |
1025 //existing cookie has same name, does it also match domain and path? | |
1026 if (((!hasDomain && !val.contains("Domain")) || (hasDomain && val.contains("Domain="+domain))) && | |
1027 ((!hasPath && !val.contains("Path")) || (hasPath && val.contains("Path="+path)))) | |
1028 { | |
1029 _fields.remove(field); | |
1030 if (last==null) | |
1031 _names.put(HttpHeaders.SET_COOKIE_BUFFER,field._next); | |
1032 else | |
1033 last._next=field._next; | |
1034 break; | |
1035 } | |
1036 } | |
1037 last=field; | |
1038 field=field._next; | |
1039 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1040 |
831 | 1041 add(HttpHeaders.SET_COOKIE_BUFFER, new ByteArrayBuffer(name_value_params)); |
1042 | |
1043 // Expire responses with set-cookie headers so they do not get cached. | |
1044 put(HttpHeaders.EXPIRES_BUFFER, __01Jan1970_BUFFER); | |
1045 } | |
1046 | |
1047 /* -------------------------------------------------------------- */ | |
1048 public void putTo(Buffer buffer) throws IOException | |
1049 { | |
1050 for (int i = 0; i < _fields.size(); i++) | |
1051 { | |
1052 Field field = _fields.get(i); | |
1053 if (field != null) | |
1054 field.putTo(buffer); | |
1055 } | |
1056 BufferUtil.putCRLF(buffer); | |
1057 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1058 |
831 | 1059 /* -------------------------------------------------------------- */ |
1060 public String toString() | |
1061 { | |
1062 try | |
1063 { | |
1064 StringBuffer buffer = new StringBuffer(); | |
1065 for (int i = 0; i < _fields.size(); i++) | |
1066 { | |
1067 Field field = (Field) _fields.get(i); | |
1068 if (field != null) | |
1069 { | |
1070 String tmp = field.getName(); | |
1071 if (tmp != null) buffer.append(tmp); | |
1072 buffer.append(": "); | |
1073 tmp = field.getValue(); | |
1074 if (tmp != null) buffer.append(tmp); | |
1075 buffer.append("\r\n"); | |
1076 } | |
1077 } | |
1078 buffer.append("\r\n"); | |
1079 return buffer.toString(); | |
1080 } | |
1081 catch (Exception e) | |
1082 { | |
1083 LOG.warn("",e); | |
1084 return e.toString(); | |
1085 } | |
1086 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1087 |
831 | 1088 /* ------------------------------------------------------------ */ |
1089 /** | |
1090 * Clear the header. | |
1091 */ | |
1092 public void clear() | |
1093 { | |
1094 _fields.clear(); | |
1095 _names.clear(); | |
1096 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1097 |
831 | 1098 /* ------------------------------------------------------------ */ |
1099 /** | |
1100 * Add fields from another HttpFields instance. Single valued fields are replaced, while all | |
1101 * others are added. | |
1102 * | |
1103 * @param fields | |
1104 */ | |
1105 public void add(HttpFields fields) | |
1106 { | |
1107 if (fields == null) return; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1108 |
831 | 1109 Enumeration e = fields.getFieldNames(); |
1110 while (e.hasMoreElements()) | |
1111 { | |
1112 String name = (String) e.nextElement(); | |
1113 Enumeration values = fields.getValues(name); | |
1114 while (values.hasMoreElements()) | |
1115 add(name, (String) values.nextElement()); | |
1116 } | |
1117 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1118 |
831 | 1119 /* ------------------------------------------------------------ */ |
1120 /** | |
1121 * Get field value parameters. Some field values can have parameters. This method separates the | |
1122 * value from the parameters and optionally populates a map with the parameters. For example: | |
1123 * | |
1124 * <PRE> | |
1125 * | |
1126 * FieldName : Value ; param1=val1 ; param2=val2 | |
1127 * | |
1128 * </PRE> | |
1129 * | |
1130 * @param value The Field value, possibly with parameteres. | |
1131 * @param parameters A map to populate with the parameters, or null | |
1132 * @return The value. | |
1133 */ | |
1134 public static String valueParameters(String value, Map<String,String> parameters) | |
1135 { | |
1136 if (value == null) return null; | |
1137 | |
1138 int i = value.indexOf(';'); | |
1139 if (i < 0) return value; | |
1140 if (parameters == null) return value.substring(0, i).trim(); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1141 |
831 | 1142 StringTokenizer tok1 = new QuotedStringTokenizer(value.substring(i), ";", false, true); |
1143 while (tok1.hasMoreTokens()) | |
1144 { | |
1145 String token = tok1.nextToken(); | |
1146 StringTokenizer tok2 = new QuotedStringTokenizer(token, "= "); | |
1147 if (tok2.hasMoreTokens()) | |
1148 { | |
1149 String paramName = tok2.nextToken(); | |
1150 String paramVal = null; | |
1151 if (tok2.hasMoreTokens()) paramVal = tok2.nextToken(); | |
1152 parameters.put(paramName, paramVal); | |
1153 } | |
1154 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1155 |
831 | 1156 return value.substring(0, i).trim(); |
1157 } | |
1158 | |
1159 /* ------------------------------------------------------------ */ | |
1160 private static final Float __zero = Float.valueOf("0.0"); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1161 |
831 | 1162 /* ------------------------------------------------------------ */ |
1163 public static float getQuality(String value) | |
1164 { | |
1165 if (value == null) return 0f; | |
1166 | |
1167 if( value.indexOf(";") == -1 ) | |
1168 return 1f; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1169 |
831 | 1170 Map params = new HashMap(); |
1171 valueParameters(value, params); | |
1172 String qs = (String) params.get("q"); | |
1173 try { | |
1174 return Float.parseFloat(qs); | |
1175 } catch (NumberFormatException e) { | |
1176 return 1f; | |
1177 } | |
1178 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1179 |
831 | 1180 /* ------------------------------------------------------------ */ |
1181 /** | |
1182 * List values in quality order. | |
1183 * | |
1184 * @param e Enumeration of values with quality parameters | |
1185 * @return values in quality order. | |
1186 */ | |
1187 public static List qualityList(Enumeration e) | |
1188 { | |
1189 if (e == null || !e.hasMoreElements()) return Collections.EMPTY_LIST; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1190 |
831 | 1191 Object list = null; |
1192 Object qual = null; | |
1193 | |
1194 // Assume list will be well ordered and just add nonzero | |
1195 while (e.hasMoreElements()) | |
1196 { | |
1197 String v = e.nextElement().toString(); | |
1198 Float q = getQuality(v); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1199 |
831 | 1200 if (q.floatValue() >= 0.001) |
1201 { | |
1202 list = LazyList.add(list, v); | |
1203 qual = LazyList.add(qual, q); | |
1204 } | |
1205 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1206 |
831 | 1207 List vl = LazyList.getList(list, false); |
1208 if (vl.size() < 2) return vl; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1209 |
831 | 1210 List ql = LazyList.getList(qual, false); |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1211 |
831 | 1212 // sort list with swaps |
1213 Float last = __zero; | |
1214 for (int i = vl.size(); i-- > 0;) | |
1215 { | |
1216 Float q = (Float) ql.get(i); | |
1217 if (last.compareTo(q) > 0) | |
1218 { | |
1219 Object tmp = vl.get(i); | |
1220 vl.set(i, vl.get(i + 1)); | |
1221 vl.set(i + 1, tmp); | |
1222 ql.set(i, ql.get(i + 1)); | |
1223 ql.set(i + 1, q); | |
1224 last = __zero; | |
1225 i = vl.size(); | |
1226 continue; | |
1227 } | |
1228 last = q; | |
1229 } | |
1230 ql.clear(); | |
1231 return vl; | |
1232 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1233 |
831 | 1234 /* ------------------------------------------------------------ */ |
1235 /* ------------------------------------------------------------ */ | |
1236 /* ------------------------------------------------------------ */ | |
1237 public static final class Field | |
1238 { | |
1239 private Buffer _name; | |
1240 private Buffer _value; | |
1241 private Field _next; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1242 |
831 | 1243 /* ------------------------------------------------------------ */ |
1244 private Field(Buffer name, Buffer value) | |
1245 { | |
1246 _name = name; | |
1247 _value = value; | |
1248 _next = null; | |
1249 } | |
1250 | |
1251 /* ------------------------------------------------------------ */ | |
1252 public void putTo(Buffer buffer) throws IOException | |
1253 { | |
1254 int o=(_name instanceof CachedBuffer)?((CachedBuffer)_name).getOrdinal():-1; | |
1255 if (o>=0) | |
1256 buffer.put(_name); | |
1257 else | |
1258 { | |
1259 int s=_name.getIndex(); | |
1260 int e=_name.putIndex(); | |
1261 while (s<e) | |
1262 { | |
1263 byte b=_name.peek(s++); | |
1264 switch(b) | |
1265 { | |
1266 case '\r': | |
1267 case '\n': | |
1268 case ':' : | |
1269 continue; | |
1270 default: | |
1271 buffer.put(b); | |
1272 } | |
1273 } | |
1274 } | |
1275 | |
1276 buffer.put((byte) ':'); | |
1277 buffer.put((byte) ' '); | |
1278 | |
1279 o=(_value instanceof CachedBuffer)?((CachedBuffer)_value).getOrdinal():-1; | |
1280 if (o>=0) | |
1281 buffer.put(_value); | |
1282 else | |
1283 { | |
1284 int s=_value.getIndex(); | |
1285 int e=_value.putIndex(); | |
1286 while (s<e) | |
1287 { | |
1288 byte b=_value.peek(s++); | |
1289 switch(b) | |
1290 { | |
1291 case '\r': | |
1292 case '\n': | |
1293 continue; | |
1294 default: | |
1295 buffer.put(b); | |
1296 } | |
1297 } | |
1298 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1299 |
831 | 1300 BufferUtil.putCRLF(buffer); |
1301 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1302 |
831 | 1303 /* ------------------------------------------------------------ */ |
1304 public String getName() | |
1305 { | |
1306 return BufferUtil.to8859_1_String(_name); | |
1307 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1308 |
831 | 1309 /* ------------------------------------------------------------ */ |
1310 Buffer getNameBuffer() | |
1311 { | |
1312 return _name; | |
1313 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1314 |
831 | 1315 /* ------------------------------------------------------------ */ |
1316 public int getNameOrdinal() | |
1317 { | |
1318 return HttpHeaders.CACHE.getOrdinal(_name); | |
1319 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1320 |
831 | 1321 /* ------------------------------------------------------------ */ |
1322 public String getValue() | |
1323 { | |
1324 return BufferUtil.to8859_1_String(_value); | |
1325 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1326 |
831 | 1327 /* ------------------------------------------------------------ */ |
1328 public Buffer getValueBuffer() | |
1329 { | |
1330 return _value; | |
1331 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1332 |
831 | 1333 /* ------------------------------------------------------------ */ |
1334 public int getValueOrdinal() | |
1335 { | |
1336 return HttpHeaderValues.CACHE.getOrdinal(_value); | |
1337 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1338 |
831 | 1339 /* ------------------------------------------------------------ */ |
1340 public int getIntValue() | |
1341 { | |
1342 return (int) getLongValue(); | |
1343 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1344 |
831 | 1345 /* ------------------------------------------------------------ */ |
1346 public long getLongValue() | |
1347 { | |
1348 return BufferUtil.toLong(_value); | |
1349 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1350 |
831 | 1351 /* ------------------------------------------------------------ */ |
1352 public String toString() | |
1353 { | |
1354 return ("[" + getName() + "=" + _value + (_next == null ? "" : "->") + "]"); | |
1355 } | |
1356 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1357 } |