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