1 28 29 package HTTPClient; 30 31 import java.io.File ; 32 import java.net.ProtocolException ; 33 import java.util.Date ; 34 import java.util.Hashtable ; 35 36 37 46 47 public class Cookie implements java.io.Serializable 48 { 49 protected String name; 50 protected String value; 51 protected Date expires; 52 protected String domain; 53 protected String path; 54 protected boolean secure; 55 56 57 72 public Cookie(String name, String value, String domain, String path, 73 Date expires, boolean secure) 74 { 75 if (name == null) throw new NullPointerException ("missing name"); 76 if (value == null) throw new NullPointerException ("missing value"); 77 if (domain == null) throw new NullPointerException ("missing domain"); 78 if (path == null) throw new NullPointerException ("missing path"); 79 80 this.name = name; 81 this.value = value; 82 this.domain = domain.toLowerCase(); 83 this.path = path; 84 this.expires = expires; 85 this.secure = secure; 86 87 if (this.domain.indexOf('.') == -1) this.domain += ".local"; 88 } 89 90 91 96 protected Cookie(RoRequest req) 97 { 98 name = null; 99 value = null; 100 expires = null; 101 domain = req.getConnection().getHost(); 102 if (domain.indexOf('.') == -1) domain += ".local"; 103 path = Util.getPath(req.getRequestURI()); 104 105 String prot = req.getConnection().getProtocol(); 106 if (prot.equals("https") || prot.equals("shttp")) 107 secure = true; 108 else 109 secure = false; 110 } 111 112 113 121 protected static Cookie[] parse(String set_cookie, RoRequest req) 122 throws ProtocolException 123 { 124 int beg = 0, 125 end = 0, 126 start = 0; 127 char[] buf = set_cookie.toCharArray(); 128 int len = buf.length; 129 130 Cookie cookie_arr[] = new Cookie[0], curr; 131 132 133 cookies: while (true) { 135 beg = Util.skipSpace(buf, beg); 136 if (beg >= len) break; if (buf[beg] == ',') { 139 beg++; 140 continue; 141 } 142 143 curr = new Cookie(req); 144 start = beg; 145 146 boolean legal = true; 147 148 parts: while (true) { 150 if (beg >= len || buf[beg] == ',') break; 151 152 if (buf[beg] == ';') 154 { 155 beg = Util.skipSpace(buf, beg+1); 156 continue; 157 } 158 159 if ((beg+6 <= len) && 161 set_cookie.regionMatches(true, beg, "secure", 0, 6)) 162 { 163 curr.secure = true; 164 beg += 6; 165 166 beg = Util.skipSpace(buf, beg); 167 if (beg < len && buf[beg] == ';') beg = Util.skipSpace(buf, beg+1); 169 else if (beg < len && buf[beg] != ',') 170 throw new ProtocolException ("Bad Set-Cookie header: " + 171 set_cookie + "\nExpected " + 172 "';' or ',' at position " + 173 beg); 174 175 continue; 176 } 177 178 end = set_cookie.indexOf('=', beg); 180 if (end == -1) 181 throw new ProtocolException ("Bad Set-Cookie header: " + 182 set_cookie + "\nNo '=' found " + 183 "for token starting at " + 184 "position " + beg); 185 186 String name = set_cookie.substring(beg, end).trim(); 187 beg = Util.skipSpace(buf, end+1); 188 189 if (name.equalsIgnoreCase("expires")) 190 { 191 196 int pos = beg; 197 while (buf[pos] >= 'a' && buf[pos] <= 'z' || 198 buf[pos] >= 'A' && buf[pos] <= 'Z') 199 pos++; 200 pos = Util.skipSpace(buf, pos); 201 if (buf[pos] == ',') 202 beg = pos+1; 203 } 204 205 int comma = set_cookie.indexOf(',', beg); 206 int semic = set_cookie.indexOf(';', beg); 207 if (comma == -1 && semic == -1) end = len; 208 else if (comma == -1) end = semic; 209 else if (semic == -1) end = comma; 210 else end = Math.min(comma, semic); 211 212 String value = set_cookie.substring(beg, end).trim(); 213 214 if (name.equalsIgnoreCase("expires")) 215 { 216 try 217 { curr.expires = new Date (value); } 218 catch (IllegalArgumentException iae) 219 { 220 226 } 227 } 228 else if (name.equalsIgnoreCase("domain")) 229 { 230 value = value.toLowerCase(); 232 233 if (value.charAt(0) != '.' && !value.equals(curr.domain)) 235 value = '.' + value; 236 237 if (!curr.domain.endsWith(value)) 239 legal = false; 240 241 242 253 254 if (!value.equals(".local") && value.indexOf('.', 1) == -1) 257 legal = false; 258 259 String top = null; 262 if (value.length() > 3 ) 263 top = value.substring(value.length()-4); 264 if (top == null || !( 265 top.equalsIgnoreCase(".com") || 266 top.equalsIgnoreCase(".edu") || 267 top.equalsIgnoreCase(".net") || 268 top.equalsIgnoreCase(".org") || 269 top.equalsIgnoreCase(".gov") || 270 top.equalsIgnoreCase(".mil") || 271 top.equalsIgnoreCase(".int"))) 272 { 273 int dl = curr.domain.length(), vl = value.length(); 274 if (dl > vl && 275 curr.domain.substring(0, dl-vl).indexOf('.') != -1) 276 legal = false; 277 } 278 279 curr.domain = value; 280 } 281 else if (name.equalsIgnoreCase("path")) 282 curr.path = value; 283 else 284 { 285 curr.name = name; 286 curr.value = value; 287 } 288 289 beg = end; 290 if (beg < len && buf[beg] == ';') beg = Util.skipSpace(buf, beg+1); 292 } 293 294 if (curr.name == null || curr.value == null) 295 throw new ProtocolException ("Bad Set-Cookie header: " + 296 set_cookie + "\nNo Name=Value found" 297 + " for cookie starting at " + 298 "posibition " + start); 299 300 if (legal) 301 { 302 cookie_arr = Util.resizeArray(cookie_arr, cookie_arr.length+1); 303 cookie_arr[cookie_arr.length-1] = curr; 304 } 305 } 306 307 return cookie_arr; 308 } 309 310 311 314 public String getName() 315 { 316 return name; 317 } 318 319 320 323 public String getValue() 324 { 325 return value; 326 } 327 328 329 332 public Date expires() 333 { 334 return expires; 335 } 336 337 338 342 public boolean discard() 343 { 344 return (expires == null); 345 } 346 347 348 351 public String getDomain() 352 { 353 return domain; 354 } 355 356 357 360 public String getPath() 361 { 362 return path; 363 } 364 365 366 369 public boolean isSecure() 370 { 371 return secure; 372 } 373 374 375 378 public boolean hasExpired() 379 { 380 return (expires != null && expires.getTime() <= System.currentTimeMillis()); 381 } 382 383 384 388 protected boolean sendWith(RoRequest req) 389 { 390 HTTPConnection con = req.getConnection(); 391 String eff_host = con.getHost(); 392 if (eff_host.indexOf('.') == -1) eff_host += ".local"; 393 394 return ((domain.charAt(0) == '.' && eff_host.endsWith(domain) || 395 domain.charAt(0) != '.' && eff_host.equals(domain)) && 396 Util.getPath(req.getRequestURI()).startsWith(path) && 397 (!secure || con.getProtocol().equals("https") || 398 con.getProtocol().equals("shttp"))); 399 } 400 401 402 405 public int hashCode() 406 { 407 return (name.hashCode() + path.hashCode() + domain.hashCode()); 408 } 409 410 411 414 public boolean equals(Object obj) 415 { 416 if ((obj != null) && (obj instanceof Cookie)) 417 { 418 Cookie other = (Cookie) obj; 419 return (this.name.equals(other.name) && 420 this.path.equals(other.path) && 421 this.domain.equals(other.domain)); 422 } 423 return false; 424 } 425 426 427 430 protected String toExternalForm() 431 { 432 return name + "=" + value; 433 } 434 435 436 440 public String toString() 441 { 442 String string = name + "=" + value; 443 if (expires != null) string += "; expires=" + expires; 444 if (path != null) string += "; path=" + path; 445 if (domain != null) string += "; domain=" + domain; 446 if (secure) string += "; secure"; 447 return string; 448 } 449 } 450 451 | Popular Tags |