Ejemplo n.º 1
0
 /**
  * @throws CloneNotSupportedException
  * @see java.lang.Object#clone()
  */
 @Override
 public Cookie clone() throws CloneNotSupportedException {
   Cookie ret = (Cookie) super.clone();
   ret.domain = this.domain;
   ret.name = this.name;
   ret.value = this.value;
   ret.path = this.path;
   ret.max_age = this.max_age;
   ret.version = this.version;
   ret.secure = this.secure;
   return ret;
 }
Ejemplo n.º 2
0
  /**
   * Parses a "Set-Cookie" header and creates/updates/deletes cookies according to the parsed
   * values. Parsing is done according to the specification in RFC 2109
   *
   * @param s The plain value of the "Set-Cookie" HTTP header. e.g.: without the "Set-Cookie: "
   *     prefix.
   */
  public void parseCookieHeader(String s) {
    StringTokenizer t = new StringTokenizer(s, ";");
    Cookie cookie = new Cookie();
    while (t.hasMoreTokens()) {
      Matcher m = pNvPair.matcher(t.nextToken());
      if (m.matches()) {
        String n = m.group(1);
        String v = (m.groupCount() > 2 && m.group(3) != null) ? m.group(3).trim() : "";
        if (n.compareToIgnoreCase("version") == 0) {
          cookie.version = Integer.parseInt(v);
          continue;
        }
        if (n.compareToIgnoreCase("domain") == 0) {
          cookie.domain = v;
          continue;
        }
        if (n.compareToIgnoreCase("path") == 0) {
          cookie.path = v;
          continue;
        }
        if (n.compareToIgnoreCase("max-age") == 0) {
          cookie.max_age = Integer.parseInt(v);
          if (cookie.max_age < 0) cookie.max_age = 0;
          cookie.max_age *= 1000;
          cookie.max_age += System.currentTimeMillis();
          continue;
        }
        if (n.compareToIgnoreCase("expires") == 0) {
          SimpleDateFormat df = new SimpleDateFormat("EEE, dd-MMM-yy HH:mm:ss zzz");
          try {
            cookie.max_age = System.currentTimeMillis() - df.parse(v).getTime();
            if (cookie.max_age < 0) cookie.max_age = 0;
          } catch (ParseException e) {
            cookie.max_age = 0;
          }
          continue;
        }
        if (n.compareToIgnoreCase("comment") == 0) {
          // ignored
          continue;
        }
        if (n.compareToIgnoreCase("secure") == 0) {
          cookie.secure = 1;
          continue;
        }
        if (!n.startsWith("$")) {
          if (null != cookie.name) {
            if (cookie.version > 0) {
              // Strip possible quotes
              cookie.domain = stripQuotes(cookie.domain);
              cookie.path = stripQuotes(cookie.path);
              // cookie.comment = stripQuotes(cookie.comment);
              // cookie.name = stripQuotes(cookie.name);
              // cookie.value = stripQuotes(cookie.value);
            }
            if (domainMatch(cookie.domain)) {
              if (cookie.max_age > 0) {
                this.jar.put(cookie.getKey(), cookie);
                this.uploadPolicy.displayDebug(
                    "[CookieJar] Adding cookie: " + cookie.getKey() + ": " + cookie, 50);

              } else {
                this.jar.put(cookie.getKey(), null);
                this.uploadPolicy.displayDebug(
                    "[CookieJar] Ignoring cookie: " + cookie.getKey() + ": " + cookie, 50);
              }
            }
            try {
              cookie = cookie.clone();
            } catch (CloneNotSupportedException e) {
              cookie = new Cookie();
            }
          }
          cookie.name = n;
          cookie.value = v;
        }
      }
    }
  }