private static List parseSetCookie0()

in api_dev/src/main/java/com/google/appengine/tools/util/ClientCookie.java [490:586]


  private static List<ClientCookie> parseSetCookie0(String setCookie0Header,
                                                    URL url)
    throws HttpHeaderParseException {
    final HttpHeaderParser parser = new HttpHeaderParser(setCookie0Header);
    final ArrayList<ClientCookie> results = new ArrayList<ClientCookie>();
    
    // read name=value
    parser.eatLWS();
    final ClientCookie cookie = new ClientCookie();
    cookie.effectiveVersion_ = 0;
    cookie.name_ = parser.eatV0CookieToken();
    parser.eatLWS();
    parser.eatChar('=');
    parser.eatLWS();
    cookie.value_ = parser.eatV0CookieValue();
    parser.eatLWS();

    // read attributes
    while (!parser.isEnd()) {
      parser.eatChar(';');
      parser.eatLWS();
      final String name = parser.eatV0CookieToken().toLowerCase();
      if (name.equals("secure")) {
        cookie.secure_ = true;
      } else if (name.equals("httponly")) {
        cookie.httponly_ = true;
      } else {
        parser.eatLWS();
        parser.eatChar('=');
        parser.eatLWS();
        if (name.equals("expires")) {
          cookie.expires_ = parser.eatV0CookieDate().getTime();
        } else {
          final String value = parser.eatV0CookieValue();
          if (name.equals("domain")) {
            cookie.domain_ = value.toLowerCase();
          } else if (name.equals("path")) {
            cookie.path_ = value;
          } else {
            logger.info("unrecognized v0 cookie attribute: " +
              name + "=" + value);
          }
        }
      }
      parser.eatLWS();
    }

    // validate the cookie -- see Netscape V0 spec
    final String requestHost = url.getHost().toLowerCase();
    final String requestPath = url.getPath();
    boolean valid = true;
    if (cookie.domain_ == null) {
      cookie.effectiveDomain_ = '.' + requestHost;
    } else {
      if (!requestHost.equals(cookie.domain_)) {
        if (!cookie.domain_.startsWith(".")) {
          cookie.effectiveDomain_ = '.' + cookie.domain_;
        } else {
          cookie.effectiveDomain_ = cookie.domain_;
        }
        if (!requestHost.endsWith(cookie.effectiveDomain_)) {
          logger.info("rejecting v0 cookie [bad domain - no match]: " +
            setCookie0Header);
          valid = false;
        } else {
          final int numPeriods =
            countOccurrences(cookie.effectiveDomain_, '.');
          boolean special = false;
          for (int i = 0; i < SPECIAL_DOMAINS.length; i++) {
            if (cookie.effectiveDomain_.endsWith(SPECIAL_DOMAINS[i])) {
              special = true;
              break;
            }
          }
          if (special ? (numPeriods < 2) : (numPeriods < 3)) {
            logger.info("rejecting v0 cookie [bad domain - no periods]: " +
              setCookie0Header);
            valid = false;
          }
        }
      } else {
        cookie.effectiveDomain_ = '.' + cookie.domain_;
      }
    }
    if (cookie.path_ == null) {
      cookie.effectivePath_ = requestPath;
    } else {
      // no path prefix check here - see the spec
      cookie.effectivePath_ = cookie.path_;
    }
    if (valid) {
      results.add(cookie);
    }

    // done
    return results;
  }