annotate src/org/eclipse/jetty/http/HttpFields.java @ 1025:cf0367978d8b

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