1 2 24 25 26 27 28 29 package com.lutris.http; 30 import java.text.FieldPosition ; 31 import java.text.ParsePosition ; 32 import java.text.SimpleDateFormat ; 33 import java.util.Date ; 34 import java.util.Locale ; 35 import java.util.StringTokenizer ; 36 import java.util.TimeZone ; 37 import java.util.Vector ; 38 39 import javax.servlet.http.Cookie ; 40 41 50 public class HttpCookieParser { 51 52 private static final String EXPIRES = "expires"; 53 private static final String MAXAGE = "max-age"; 54 private static final String PATH = "path"; 55 private static final String DOMAIN = "domain"; 56 private static final String SECURE = "secure"; 57 58 61 private static final SimpleDateFormat dateFormatter = 62 new SimpleDateFormat ("EEEE, dd-MMM-yyyy HH:mm:ss z", Locale.US); 63 64 67 static { 68 dateFormatter.setTimeZone(TimeZone.getTimeZone("GMT")); 69 } 70 71 81 public static Vector parseRequestHeader(String httpReqCookieHeader) { 82 Cookie cookie; 83 Vector result = new Vector (); 84 85 StringTokenizer cookieTokens = 87 new StringTokenizer (httpReqCookieHeader.trim(), ";"); 88 while (cookieTokens.hasMoreTokens()) { 89 StringTokenizer t = 91 new StringTokenizer (cookieTokens.nextToken(), "="); 92 String name = t.nextToken().trim(); 93 if (t.hasMoreTokens()) { 94 String value = t.nextToken().trim(); 96 result.addElement(new Cookie (name, value)); 97 } 98 } 99 return result; 100 } 101 102 115 public static Cookie parseRequestHeader(String httpReqCookieHeader, 116 String name) { 117 StringTokenizer cookieTokens = 119 new StringTokenizer (httpReqCookieHeader.trim(), ";"); 120 while (cookieTokens.hasMoreTokens()) { 121 StringTokenizer t = 123 new StringTokenizer (cookieTokens.nextToken(), "="); 124 String tokenName = t.nextToken().trim(); 125 if (t.hasMoreTokens() && tokenName.equals(name)) { 126 String value = t.nextToken().trim(); 128 return new Cookie (name, value); 129 } 130 } 131 return null; 132 } 133 134 135 142 public static int expiresToMaxAge(String dateStr) { 143 Date date = dateFormatter.parse(dateStr.trim(), new ParsePosition (0)); 144 Date current = new Date (); 145 return (int)(current.getTime()-date.getTime()) * 1000; 146 } 147 148 149 156 public static String maxAgeToExpires(int maxAge) { 157 long maxInMillis = (long)maxAge * 1000; 158 Date date = new Date (new Date ().getTime()+maxInMillis); 159 StringBuffer dateStr = new StringBuffer (); 160 dateFormatter.format(date, dateStr, new FieldPosition (0)); 161 return dateStr.toString(); 162 } 163 164 175 public static Vector parseResponseHeader(String httpResponseCookieHeader) { 176 Vector result = new Vector (); 177 httpResponseCookieHeader = httpResponseCookieHeader.trim(); 178 StringTokenizer cookieTokens = 180 new StringTokenizer (httpResponseCookieHeader, ","); 181 while (cookieTokens.hasMoreTokens()) { 182 StringTokenizer tokens = 184 new StringTokenizer (cookieTokens.nextToken(), ";"); 185 Cookie cookie = null; 186 while (tokens.hasMoreTokens()) { 187 StringTokenizer t = 189 new StringTokenizer (tokens.nextToken(), "="); 190 String name = t.nextToken().trim(); 191 if (t.hasMoreTokens()) { 192 String value = t.nextToken().trim(); 193 if (cookie != null) { 194 if (name.equalsIgnoreCase(EXPIRES)) { 195 cookie.setMaxAge(expiresToMaxAge(value)); 196 } else if (name.equalsIgnoreCase(DOMAIN)) { 197 cookie.setDomain(value); 198 } else if (name.equalsIgnoreCase(PATH)) { 199 cookie.setPath(value); 200 } 201 } else { cookie = new Cookie (name, value); 203 result.addElement(cookie); 204 } 205 } else if ((cookie != null) && name.equalsIgnoreCase(SECURE)) { 206 cookie.setSecure(true); 207 } 208 } 209 } 210 return result; 211 } 212 213 214 public static Cookie parseCookieString(String cookieString) { 215 cookieString = cookieString.trim(); 216 StringTokenizer tokens = new StringTokenizer (cookieString, ";"); 218 Cookie cookie = null; 219 while (tokens.hasMoreTokens()) { 220 StringTokenizer t = 222 new StringTokenizer (tokens.nextToken(), "="); 223 String name = t.nextToken().trim(); 224 if (t.hasMoreTokens()) { 225 String value = t.nextToken().trim(); 226 if (cookie != null) { 227 if (name.equalsIgnoreCase(EXPIRES)) { 228 cookie.setMaxAge(expiresToMaxAge(value)); 229 } else if (name.equalsIgnoreCase(DOMAIN)) { 230 cookie.setDomain(value); 231 } else if (name.equalsIgnoreCase(PATH)) { 232 cookie.setPath(value); 233 } else if (name.equalsIgnoreCase(MAXAGE) ) { 234 int maxAge = 0; 235 try { 236 maxAge = Integer.parseInt(value); 237 } catch (Exception e) { 238 } 239 cookie.setMaxAge(maxAge); 240 } 241 } else { cookie = new Cookie (name, value); 243 } 244 } else if ((cookie != null) && name.equalsIgnoreCase(SECURE)) { 245 cookie.setSecure(true); 246 } 247 } 248 return cookie; 249 } 250 251 252 260 private static void appendCookieArg(StringBuffer buf, String key, String value) { 264 if (buf.length() > 0) { 265 buf.append("; "); 266 } 267 buf.append(key); 268 buf.append("="); 269 buf.append(value); 270 } 271 272 283 private static void appendCookieArg(int version, StringBuffer buf, String key, String value) { 284 if (version == 0) { 287 key = key.toLowerCase(); 289 } 290 appendCookieArg(buf, key, value); 291 } 292 293 299 public static String formatResponseCookie(Cookie cookie) { 300 StringBuffer buf = new StringBuffer (); 301 int version = cookie.getVersion(); 302 303 if (version > 0) { 305 throw new Error ("HttpCookieParser: only version 0 cookies supported right now"); 306 } 307 308 appendCookieArg(buf, cookie.getName(), cookie.getValue()); 309 if ((version > 0) && (cookie.getComment() != null)) { 310 appendCookieArg(version, buf, "Comment", cookie.getComment()); 311 } 312 if (cookie.getDomain() != null) { 313 appendCookieArg(version, buf, "Domain", cookie.getDomain()); 314 } 315 if (cookie.getMaxAge() >= 0) { 316 if (version > 0) { 317 appendCookieArg(version, buf, "Max-Age", Integer.toString(cookie.getMaxAge())); 318 } else { 319 appendCookieArg(version, buf, "Expires", maxAgeToExpires(cookie.getMaxAge())); 320 } 321 } 322 if (cookie.getPath() != null) { 323 appendCookieArg(version, buf, "Path", cookie.getPath()); 324 } 325 if (cookie.getDomain() != null) { 326 appendCookieArg(version, buf, "Domain", cookie.getDomain()); 327 } 328 return buf.toString(); 329 } 330 } 331 | Popular Tags |