1 package dolda.jsvc.util;
9 private final static Map<Request, MultiMap<String, Cookie>> cache = new WeakHashMap<Request, MultiMap<String, Cookie>>();
10 public final static DateFormat datefmt;
12 datefmt = new SimpleDateFormat("EEE, dd-MMM-yyyy HH:mm:ss z", Locale.ENGLISH);
13 datefmt.setCalendar(Calendar.getInstance(TimeZone.getTimeZone("UTC")));
15 public final String name;
18 public String domain, path;
19 public boolean secure;
21 public Cookie(String name, String value, Date expires, String domain, String path, boolean secure) {
22 if(!Http.istoken(name))
23 throw(new RuntimeException("Invalid cookie name: `" + name + "'"));
26 this.expires = expires;
32 public Cookie(String name) {
33 this(name, null, null, null, null, false);
36 public Cookie(String name, String value) {
37 this(name, value, null, null, null, false);
40 public String format() {
41 StringBuilder buf = new StringBuilder();
42 buf.append(Http.tokenquote(name));
44 buf.append(Http.tokenquote(value));
46 buf.append("; Domain=" + Http.tokenquote(domain));
48 buf.append("; Path=" + Http.tokenquote(path));
50 buf.append("; Expires=" + Http.tokenquote(datefmt.format(expires)));
52 buf.append("; Secure");
53 return(buf.toString());
56 public void addto(Request req) {
57 req.outheaders().add("Set-Cookie", format());
60 public static MultiMap<String, Cookie> parse(Request req) {
61 MultiMap<String, Cookie> ret = new WrappedMultiMap<String, Cookie>(new TreeMap<String, Collection<Cookie>>());
62 for(String in : req.inheaders().values("Cookie")) {
64 StringReader r = new StringReader(in);
67 String k = Http.tokenunquote(r);
68 String v = Http.tokenunquote(r);
71 if(k.equals("$Version")) {
72 if(Integer.parseInt(v) != 1)
73 throw(new Http.EncodingException("Unknown cookie format version"));
74 } else if(k.equals("$Path")) {
77 } else if(k.equals("$Domain")) {
85 } catch(IOException e) {
92 public static MultiMap<String, Cookie> get(Request req) {
94 MultiMap<String, Cookie> ret = cache.get(req);
103 public String toString() {
104 StringBuilder buf = new StringBuilder();
105 buf.append("Cookie(");
106 buf.append(format());
108 return(buf.toString());