annotate src/org/eclipse/jetty/http/HttpFields.java @ 837:3ff59e08a1b7

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