2 Copyright (C) 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu
.java
.net
.protocol
.http
;
41 import gnu
.java
.net
.LineInputStream
;
43 import java
.io
.IOException
;
44 import java
.io
.InputStream
;
45 import java
.text
.DateFormat
;
46 import java
.text
.ParseException
;
47 import java
.util
.Collection
;
48 import java
.util
.Date
;
49 import java
.util
.Iterator
;
50 import java
.util
.LinkedHashMap
;
51 import java
.util
.LinkedHashSet
;
56 * A collection of HTTP header names and associated values.
57 * Retrieval of values is case insensitive. An iteration over the keys
58 * returns the header names in the order they were received.
60 * @author Chris Burdess (dog@gnu.org)
66 static final DateFormat dateFormat
= new HTTPDateFormat();
75 if (name
== null || name
.length() == 0)
77 throw new IllegalArgumentException(name
);
84 return name
.toLowerCase().hashCode();
87 public boolean equals(Object other
)
89 if (other
instanceof Header
)
91 return ((Header
) other
).name
.equalsIgnoreCase(name
);
96 public String
toString()
103 static class HeaderEntry
107 final Map
.Entry entry
;
109 HeaderEntry(Map
.Entry entry
)
114 public Object
getKey()
116 return ((Header
) entry
.getKey()).name
;
119 public Object
getValue()
121 return entry
.getValue();
124 public Object
setValue(Object value
)
126 return entry
.setValue(value
);
129 public int hashCode()
131 return entry
.hashCode();
134 public boolean equals(Object other
)
136 return entry
.equals(other
);
139 public String
toString()
141 return getKey().toString() + "=" + getValue();
150 public boolean containsKey(Object key
)
152 return super.containsKey(new Header((String
) key
));
155 public Object
get(Object key
)
157 return super.get(new Header((String
) key
));
161 * Returns the value of the specified header as a string.
163 public String
getValue(String header
)
165 return (String
) super.get(new Header(header
));
169 * Returns the value of the specified header as an integer,
170 * or -1 if the header is not present or not an integer.
172 public int getIntValue(String header
)
174 String val
= getValue(header
);
181 return Integer
.parseInt(val
);
183 catch (NumberFormatException e
)
190 * Returns the value of the specified header as a long, or -1 if the
191 * header is not present or cannot be parsed as a long.
193 public long getLongValue(String header
)
195 String val
= getValue(header
);
202 return Long
.parseLong(val
);
204 catch (NumberFormatException e
)
211 * Returns the value of the specified header as a date,
212 * or <code>null</code> if the header is not present or not a date.
214 public Date
getDateValue(String header
)
216 String val
= getValue(header
);
223 return dateFormat
.parse(val
);
225 catch (ParseException e
)
231 public Object
put(Object key
, Object value
)
233 return super.put(new Header((String
) key
), value
);
236 public Object
remove(Object key
)
238 return super.remove(new Header((String
) key
));
241 public void putAll(Map t
)
243 for (Iterator i
= t
.keySet().iterator(); i
.hasNext(); )
245 String key
= (String
) i
.next();
246 String value
= (String
) t
.get(key
);
253 Set keys
= super.keySet();
254 Set ret
= new LinkedHashSet();
255 for (Iterator i
= keys
.iterator(); i
.hasNext(); )
257 ret
.add(((Header
) i
.next()).name
);
262 public Set
entrySet()
264 Set entries
= super.entrySet();
265 Set ret
= new LinkedHashSet();
266 for (Iterator i
= entries
.iterator(); i
.hasNext(); )
268 Map
.Entry entry
= (Map
.Entry
) i
.next();
269 ret
.add(new HeaderEntry(entry
));
275 * Parse the specified input stream, adding headers to this collection.
277 public void parse(InputStream in
)
280 LineInputStream lin
= (in
instanceof LineInputStream
) ?
281 (LineInputStream
) in
: new LineInputStream(in
);
284 StringBuilder value
= new StringBuilder();
287 String line
= lin
.readLine();
292 addValue(name
, value
.toString());
296 int len
= line
.length();
301 addValue(name
, value
.toString());
305 char c1
= line
.charAt(0);
306 if (c1
== ' ' || c1
== '\t')
310 if (line
.charAt(last
) != '\r')
312 value
.append(line
.substring(0, last
));
318 addValue(name
, value
.toString());
321 int di
= line
.indexOf(':');
322 name
= line
.substring(0, di
);
328 while (di
< len
&& line
.charAt(di
) == ' ');
330 if (line
.charAt(last
) != '\r')
332 value
.append(line
.substring(di
, last
));
337 private void addValue(String name
, String value
)
339 Header key
= new Header(name
);
340 String old
= (String
) super.get(key
);
343 super.put(key
, value
);
347 super.put(key
, old
+ ", " + value
);