Example #1
0
  // Private version of parse() that will store the original header used to
  // create the cookie, in the cookie itself. This can be useful for filtering
  // Set-Cookie[2] headers, using the internal parsing logic defined in this
  // class.
  private static List<HttpCookie> parse(String header, boolean retainHeader) {

    int version = guessCookieVersion(header);

    // if header start with set-cookie or set-cookie2, strip it off
    if (startsWithIgnoreCase(header, SET_COOKIE2)) {
      header = header.substring(SET_COOKIE2.length());
    } else if (startsWithIgnoreCase(header, SET_COOKIE)) {
      header = header.substring(SET_COOKIE.length());
    }

    List<HttpCookie> cookies = new java.util.ArrayList<HttpCookie>();
    // The Netscape cookie may have a comma in its expires attribute,
    // while the comma is the delimiter in rfc 2965/2109 cookie header string.
    // so the parse logic is slightly different
    if (version == 0) {
      // Netscape draft cookie
      HttpCookie cookie = parseInternal(header, retainHeader);
      cookie.setVersion(0);
      cookies.add(cookie);
    } else {
      // rfc2965/2109 cookie
      // if header string contains more than one cookie,
      // it'll separate them with comma
      List<String> cookieStrings = splitMultiCookies(header);
      for (String cookieStr : cookieStrings) {
        HttpCookie cookie = parseInternal(cookieStr, retainHeader);
        cookie.setVersion(1);
        cookies.add(cookie);
      }
    }

    return cookies;
  }
 public HttpCookie toHttpCookie() {
   HttpCookie cookie = new HttpCookie(name, value);
   cookie.setComment(comment);
   cookie.setCommentURL(commentURL);
   cookie.setDiscard(discard);
   cookie.setDomain(domain);
   cookie.setMaxAge((expiry - System.currentTimeMillis()) / 1000L);
   cookie.setPath(path);
   cookie.setPortlist(portList);
   cookie.setSecure(secure);
   cookie.setVersion(version);
   return cookie;
 }
Example #3
0
  public static HttpCookie fromNewCookie(NewCookie c) {

    HttpCookie cookie = new HttpCookie(c.getName(), c.getValue());

    cookie.setComment(c.getComment());
    cookie.setCommentURL("not available");
    cookie.setDiscard(false);
    cookie.setDomain(c.getDomain());
    cookie.setMaxAge(c.getMaxAge());
    cookie.setPath(c.getPath());
    cookie.setPortlist("not available");
    cookie.setSecure(c.isSecure());
    cookie.setVersion(c.getVersion());
    cookie.setHttpOnly(c.isHttpOnly());

    return cookie;
  }
Example #4
0
  public HttpCookie toHttpCookie() {
    HttpCookie cookie = null;

    long expiration = 0;
    try {
      expiration = getExpiration();
    } catch (Exception e) {
    }

    String domain = getDomain();
    String path = getPath();

    int secureFlag = 0;
    try {
      secureFlag = getSecure();
    } catch (Exception e) {
    }

    // Currently no use or need to use the HTTP-only flag in cookies for now
    cookie = new HttpCookie(getName(), getValue());
    cookie.setVersion(0);

    long currentTimeInSeconds = Calendar.getInstance().getTimeInMillis() / 1000;
    if (currentTimeInSeconds < expiration) {
      long maxAge = expiration - currentTimeInSeconds;
      cookie.setMaxAge(maxAge);
    }

    if (secureFlag != 0) {
      cookie.setSecure(true);
    }

    if (domain != null && domain.length() > 0) {
      cookie.setDomain(domain);
      if (path != null && path.length() > 0) {
        cookie.setPath(path);
      } else {
        cookie.setPath("/");
      }
      return cookie;
    }
    return null;
  }
Example #5
0
  public void loadState(Activity a) {
    SharedPreferences prefs = a.getSharedPreferences("cookies", 0);
    String cookie = prefs.getString("RevTK", "no value");

    Log.v(DEBUG_TAG, "loaded RevTK cookie: " + cookie);

    HttpCookie revTKCookie = new HttpCookie("RevTK", cookie);
    revTKCookie.setVersion(0);
    revTKCookie.setDomain("kanji.koohii.com");

    try {
      if (!(cookie.equals("no value") || revTKCookie.hasExpired())) {
        cm.getCookieStore().add(new URI("http://kanji.koohii.com/"), revTKCookie);
        loggedIn = true;
      }
    } catch (URISyntaxException e) { // should not happen
      e.printStackTrace();
    }
  }