-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathHttpHeaderElement.java
More file actions
203 lines (177 loc) · 4.55 KB
/
HttpHeaderElement.java
File metadata and controls
203 lines (177 loc) · 4.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
* @(#)HttpHeaderElement.java 0.3-3 06/05/2001
*
* This file is part of the HTTPClient package
* Copyright (C) 1996-2001 Ronald Tschal�r
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307, USA
*
* For questions, suggestions, bug-reports, enhancement-requests etc.
* I may be contacted at:
*
* ronald@innovation.ch
*
* The HTTPClient's home page is located at:
*
* http://www.innovation.ch/java/HTTPClient/
*
*/
package HTTPClient;
/**
* This class holds a description of an http header element. It is used
* by {@link Util#parseHeader(java.lang.String) Util.parseHeader}.
*
* @see Util#parseHeader(java.lang.String)
* @see Util#getElement(java.util.Vector, java.lang.String)
* @see Util#assembleHeader(java.util.Vector)
* @version 0.3-3 06/05/2001
* @author Ronald Tschal�r
*/
public class HttpHeaderElement
{
/** element name */
private String name;
/** element value */
private String value;
/** element parameters */
private NVPair[] parameters;
// Constructors
/**
* Construct an element with the given name. The value and parameters
* are set to null. This can be used when a dummy element is constructed
* for comparison or retrieval purposes.
*
* @param name the name of the element
*/
public HttpHeaderElement(String name)
{
this.name = name;
this.value = null;
parameters = new NVPair[0];
}
/**
* @param name the first token in the element
* @param value the value part, or null
* @param params the parameters
*/
public HttpHeaderElement(String name, String value, NVPair[] params)
{
this.name = name;
this.value = value;
if (params != null)
{
parameters = new NVPair[params.length];
System.arraycopy(params, 0, parameters, 0, params.length);
}
else
parameters = new NVPair[0];
}
// Methods
/**
* @return the name
*/
public String getName()
{
return name;
}
/**
* @return the value
*/
public String getValue()
{
return value;
}
/**
* @return the parameters
*/
public NVPair[] getParams()
{
return parameters;
}
/**
* Two elements are equal if they have the same name. The comparison is
* <em>case-insensitive</em>.
*
* @param obj the object to compare with
* @return true if <var>obj</var> is an HttpHeaderElement with the same
* name as this element.
*/
public boolean equals(Object obj)
{
if ((obj != null) && (obj instanceof HttpHeaderElement))
{
String other = ((HttpHeaderElement) obj).name;
return name.equalsIgnoreCase(other);
}
return false;
}
/**
* @return a string containing the HttpHeaderElement formatted as it
* would appear in a header
*/
public String toString()
{
StringBuffer buf = new StringBuffer();
appendTo(buf);
return buf.toString();
}
/**
* Append this header element to the given buffer. This is basically a
* more efficient version of <code>toString()</code> for assembling
* multiple elements.
*
* @param buf the StringBuffer to append this header to
* @see #toString()
*/
public void appendTo(StringBuffer buf)
{
buf.append(name);
if (value != null)
{
if (Util.needsQuoting(value))
{
buf.append("=\"");
buf.append(Util.quoteString(value, "\\\""));
buf.append('"');
}
else
{
buf.append('=');
buf.append(value);
}
}
for (int idx=0; idx<parameters.length; idx++)
{
buf.append(";");
buf.append(parameters[idx].getName());
String pval = parameters[idx].getValue();
if (pval != null)
{
if (Util.needsQuoting(pval))
{
buf.append("=\"");
buf.append(Util.quoteString(pval, "\\\""));
buf.append('"');
}
else
{
buf.append('=');
buf.append(pval);
}
}
}
}
}