1 31 32 package org.apache.commons.httpclient; 33 34 import org.apache.commons.logging.Log; 35 import org.apache.commons.logging.LogFactory; 36 37 import java.util.BitSet ; 38 import java.util.NoSuchElementException ; 39 import java.util.StringTokenizer ; 40 import java.util.Vector ; 41 42 43 89 public class HeaderElement extends NameValuePair { 90 91 93 96 public HeaderElement() { 97 this(null, null, null); 98 } 99 100 105 public HeaderElement(String name, String value) { 106 this(name, value, null); 107 } 108 109 116 public HeaderElement(String name, String value, 117 NameValuePair[] parameters) { 118 super(name, value); 119 setParameters(parameters); 120 } 121 122 124 125 private static final Log LOG = LogFactory.getLog(HeaderElement.class); 126 127 132 private static final BitSet SEPARATORS = new BitSet (128); 133 134 139 private static final BitSet TOKEN_CHAR = new BitSet (128); 140 141 146 private static final BitSet UNSAFE_CHAR = new BitSet (128); 147 148 152 static { 153 SEPARATORS.set('('); 155 SEPARATORS.set(')'); 156 SEPARATORS.set('<'); 157 SEPARATORS.set('>'); 158 SEPARATORS.set('@'); 159 SEPARATORS.set(','); 160 SEPARATORS.set(';'); 161 SEPARATORS.set(':'); 162 SEPARATORS.set('\\'); 163 SEPARATORS.set('"'); 164 SEPARATORS.set('/'); 165 SEPARATORS.set('['); 166 SEPARATORS.set(']'); 167 SEPARATORS.set('?'); 168 SEPARATORS.set('='); 169 SEPARATORS.set('{'); 170 SEPARATORS.set('}'); 171 SEPARATORS.set(' '); 172 SEPARATORS.set('\t'); 173 174 for (int ch = 32; ch < 127; ch++) { 176 TOKEN_CHAR.set(ch); 177 } 178 TOKEN_CHAR.xor(SEPARATORS); 179 180 for (int ch = 0; ch < 32; ch++) { 183 UNSAFE_CHAR.set(ch); 184 } 185 UNSAFE_CHAR.set(' '); 186 UNSAFE_CHAR.set('<'); 187 UNSAFE_CHAR.set('>'); 188 UNSAFE_CHAR.set('"'); 189 UNSAFE_CHAR.set('{'); 190 UNSAFE_CHAR.set('}'); 191 UNSAFE_CHAR.set('|'); 192 UNSAFE_CHAR.set('\\'); 193 UNSAFE_CHAR.set('^'); 194 UNSAFE_CHAR.set('~'); 195 UNSAFE_CHAR.set('['); 196 UNSAFE_CHAR.set(']'); 197 UNSAFE_CHAR.set('`'); 198 UNSAFE_CHAR.set(127); 199 } 200 201 203 204 private NameValuePair[] parameters = null; 205 206 208 214 public NameValuePair[] getParameters() { 215 return this.parameters; 216 } 217 218 222 protected void setParameters(final NameValuePair[] pairs) { 223 parameters = pairs; 224 } 225 227 236 public static final HeaderElement[] parse(String headerValue) 237 throws HttpException { 238 239 LOG.trace("enter HeaderElement.parse(String)"); 240 241 if (headerValue == null) { 242 return null; 243 } 244 245 Vector elements = new Vector (); 246 StringTokenizer tokenizer = 247 new StringTokenizer (headerValue.trim(), ","); 248 249 while (tokenizer.countTokens() > 0) { 250 String nextToken = tokenizer.nextToken(); 251 252 try { 255 while (HeaderElement.hasOddNumberOfQuotationMarks(nextToken)) { 256 nextToken += "," + tokenizer.nextToken(); 257 } 258 } catch (NoSuchElementException exception) { 259 throw new HttpException( 260 "Bad header format: wrong number of quotation marks"); 261 } 262 263 try { 265 272 if (tokenizer.hasMoreTokens()) { 273 String s = nextToken.toLowerCase(); 274 if (s.endsWith("mon") 275 || s.endsWith("tue") 276 || s.endsWith("wed") 277 || s.endsWith("thu") 278 || s.endsWith("fri") 279 || s.endsWith("sat") 280 || s.endsWith("sun") 281 || s.endsWith("monday") 282 || s.endsWith("tuesday") 283 || s.endsWith("wednesday") 284 || s.endsWith("thursday") 285 || s.endsWith("friday") 286 || s.endsWith("saturday") 287 || s.endsWith("sunday")) { 288 289 nextToken += "," + tokenizer.nextToken(); 290 } 291 } 292 } catch (NoSuchElementException exception) { 293 throw new HttpException 294 ("Bad header format: parsing with wrong header elements"); 295 } 296 297 String tmp = nextToken.trim(); 298 if (!tmp.endsWith(";")) { 299 tmp += ";"; 300 } 301 char[] header = tmp.toCharArray(); 302 303 boolean inAString = false; 305 int startPos = 0; 306 HeaderElement element = new HeaderElement(); 307 Vector paramlist = new Vector (); 308 for (int i = 0 ; i < header.length ; i++) { 309 if (header[i] == ';' && !inAString) { 310 NameValuePair pair = parsePair(header, startPos, i); 311 if (pair == null) { 312 throw new HttpException( 313 "Bad header format: empty name/value pair in" 314 + nextToken); 315 316 } else if (startPos == 0) { 318 element.setName(pair.getName()); 319 element.setValue(pair.getValue()); 320 } else { 321 paramlist.addElement(pair); 322 } 323 startPos = i + 1; 324 } else if (header[i] == '"' 325 && !(inAString && i > 0 && header[i - 1] == '\\')) { 326 inAString = !inAString; 327 } 328 } 329 330 if (paramlist.size() > 0) { 332 NameValuePair[] tmp2 = new NameValuePair[paramlist.size()]; 333 paramlist.copyInto((NameValuePair[]) tmp2); 334 element.setParameters (tmp2); 335 paramlist.removeAllElements(); 336 } 337 338 elements.addElement(element); 340 } 341 342 HeaderElement[] headerElements = new HeaderElement[elements.size()]; 343 elements.copyInto((HeaderElement[]) headerElements); 344 return headerElements; 345 } 346 347 355 private static final boolean hasOddNumberOfQuotationMarks(String string) { 356 boolean odd = false; 357 int start = -1; 358 while ((start = string.indexOf('"', start + 1)) != -1) { 359 odd = !odd; 360 } 361 return odd; 362 } 363 364 372 private static final NameValuePair parsePair(char[] header, 373 int start, int end) { 374 375 LOG.trace("enter HeaderElement.parsePair(char[], int, int)"); 376 377 NameValuePair pair = null; 378 String name = new String (header, start, end - start).trim(); 379 String value = null; 380 381 int index = name.indexOf("="); 383 if (index >= 0) { 384 if ((index + 1) < name.length()) { 385 value = name.substring(index + 1).trim(); 386 if (value.startsWith("\"") && value.endsWith("\"")) { 388 value = value.substring(1, value.length() - 1); 389 } 390 } 391 name = name.substring(0, index).trim(); 392 } 393 394 pair = new NameValuePair(name, value); 395 396 return pair; 397 } 398 399 400 407 408 public NameValuePair getParameterByName(String name) { 409 if (name == null) { 410 throw new NullPointerException ("Name is null"); 411 } 412 NameValuePair found = null; 413 NameValuePair parameters[] = getParameters(); 414 if (parameters != null) { 415 for (int i = 0; i < parameters.length; i++) { 416 NameValuePair current = parameters[ i ]; 417 if (current.getName().equalsIgnoreCase(name)) { 418 found = current; 419 break; 420 } 421 } 422 } 423 return found; 424 } 425 426 427 } 428 429 | Popular Tags |