line.append(cookie.getDomain()); line.append(tab); line.append(cookie.getPath() != null ? cookie.getPath() : "/"); line.append(tab); line.append(cookie.isSecure() ? "TRUE" : "FALSE"); line.append(tab); line.append(cookie.getExpiryDate() != null ? cookie.getExpiryDate().getTime() / 1000 : -1); line.append(tab); line.append(cookie.getName()); line.append(tab); line.append(cookie.getValue() != null ? cookie.getValue() : ""); line.append("\n"); out.write(line.toString().getBytes());
private List<Header> doFormatManyHeaders(final List<Cookie> cookies) { List<Header> headers = new ArrayList<Header>(cookies.size()); for (Cookie cookie : cookies) { int version = cookie.getVersion(); CharArrayBuffer buffer = new CharArrayBuffer(40); buffer.append("Cookie: "); buffer.append("$Version="); buffer.append(Integer.toString(version)); buffer.append("; "); formatCookieAsVer(buffer, cookie, version); headers.add(new BufferedHeader(buffer)); } return headers; }
/** * Removes all of {@link Cookie cookies} in this HTTP state * that have expired by the specified {@link java.util.Date date}. * * @return true if any cookies were purged. * * @see Cookie#isExpired(Date) */ public synchronized boolean clearExpired(final Date date) { if (date == null) { return false; } boolean removed = false; for (Iterator<Cookie> it = cookies.iterator(); it.hasNext();) { if (it.next().isExpired(date)) { it.remove(); removed = true; } } return removed; }
/** * Don't log the cookie's value; that's potentially sensitive information. */ private String cookieToString(Cookie cookie) { return cookie.getClass().getSimpleName() + "[version=" + cookie.getVersion() + ",name=" + cookie.getName() + ",domain=" + cookie.getDomain() + ",path=" + cookie.getPath() + ",expiry=" + cookie.getExpiryDate() + "]"; } // END android-added
public int compare(final Cookie c1, final Cookie c2) { int res = c1.getName().compareTo(c2.getName()); if (res == 0) { // do not differentiate empty and null domains String d1 = c1.getDomain(); if (d1 == null) { d1 = ""; } String d2 = c2.getDomain(); if (d2 == null) { d2 = ""; } res = d1.compareToIgnoreCase(d2); } return res; }
public void addCookie(Cookie cookie) { if (isCookieCountMaxedForDomain(cookie.getDomain())) { logger.log( Level.FINEST, "Maximum number of cookies reached for domain " + cookie.getDomain() + ". Will not add new cookie " + cookie.getName() + " with value " + cookie.getValue()); return; } addCookieImpl(cookie); }
private void writeObject(ObjectOutputStream out) throws IOException { out.writeObject(cookie.getName()); out.writeObject(cookie.getValue()); out.writeObject(cookie.getComment()); out.writeObject(cookie.getDomain()); out.writeObject(cookie.getExpiryDate()); out.writeObject(cookie.getPath()); out.writeInt(cookie.getVersion()); out.writeBoolean(cookie.isSecure()); }
/** * Return a string suitable for sending in a <tt>"Cookie"</tt> header * as defined in RFC 2109 for backward compatibility with cookie version 0 * @param buffer The char array buffer to use for output * @param cookie The {@link Cookie} to be formatted as string * @param version The version to use. */ protected void formatCookieAsVer(final CharArrayBuffer buffer, final Cookie cookie, int version) { formatParamAsVer(buffer, cookie.getName(), cookie.getValue(), version); if (cookie.getPath() != null) { if (cookie instanceof ClientCookie && ((ClientCookie) cookie).containsAttribute(ClientCookie.PATH_ATTR)) { buffer.append("; "); formatParamAsVer(buffer, "$Path", cookie.getPath(), version); } } if (cookie.getDomain() != null) { if (cookie instanceof ClientCookie && ((ClientCookie) cookie).containsAttribute(ClientCookie.DOMAIN_ATTR)) { buffer.append("; "); formatParamAsVer(buffer, "$Domain", cookie.getDomain(), version); } } }
public List<Header> formatCookies(final List<Cookie> cookies) { if (cookies == null) { throw new IllegalArgumentException("List of cookies may not be null"); } if (cookies.isEmpty()) { throw new IllegalArgumentException("List of cookies may not be empty"); } CharArrayBuffer buffer = new CharArrayBuffer(20 * cookies.size()); buffer.append(SM.COOKIE); buffer.append(": "); for (int i = 0; i < cookies.size(); i++) { Cookie cookie = cookies.get(i); if (i > 0) { buffer.append("; "); } buffer.append(cookie.getName()); String s = cookie.getValue(); if (s != null) { buffer.append("="); buffer.append(s); } } List<Header> headers = new ArrayList<Header>(1); headers.add(new BufferedHeader(buffer)); return headers; }
@Override public boolean match(final Cookie cookie, final CookieOrigin origin) { Args.notNull(cookie, "Cookie"); Args.notNull(origin, "Cookie origin"); final String host = origin.getHost(); final String domain = cookie.getDomain(); if (domain == null) { return false; } return host.endsWith(domain); }
public synchronized void addCookie(final org.apache.http.cookie.Cookie cookie) { if (cookie != null) { log.debug("Add cookie : name={}, path={}, value={},", cookie.getName(), cookie.getPath(), cookie.getValue()); // first remove any old cookie that is equivalent cookies.remove(cookie); if (!cookie.isExpired(new Date())) { cookies.add(cookie); } } }
/** * Returns a string that uniquely identifies the cookie, The format The * format of the key is {@code "normalizedDomain;name;path"}. Adapted from * {@link CookieIdentityComparator#compare(Cookie, Cookie)}. */ protected String sortableKey(Cookie cookie) { String normalizedDomain = normalizeHost(cookie.getDomain()); // use ";" as delimiter since it is the delimiter in the cookie header, // so presumably can't appear in any of these values StringBuilder buf = new StringBuilder(normalizedDomain); buf.append(";").append(cookie.getName()); buf.append(";").append(cookie.getPath() != null ? cookie.getPath() : "/"); return buf.toString(); }
/** * Adds an {@link Cookie HTTP cookie}, replacing any existing equivalent cookies. If the given * cookie has already expired it will not be added, but existing values will still be removed. * * @param cookie the {@link Cookie cookie} to be added * * @see #addCookies(HashMap) * */ @Override public void addCookie(final Cookie cookie) { if (cookie != null) { // first remove any old cookie that is equivalent cookies.remove(cookie.getDomain() + cookie.getName() + cookie.getPath()); if (!cookie.isExpired(new Date())) { cookies.put(cookie.getDomain() + cookie.getName() + cookie.getPath(), cookie); } } }
@Override public boolean match(Cookie cookie, CookieOrigin origin) { if (cookie == null) { throw new IllegalArgumentException("Cookie may not be null"); } if (origin == null) { throw new IllegalArgumentException("Cookie origin may not be null"); } String host = origin.getHost(); String domain = cookie.getDomain(); if (domain == null) { return false; } return host.endsWith(domain); }
@Override public void addCookie(Cookie cookie) { if (omitNonPersistentCookies && !cookie.isPersistent()) return; String name = cookie.getName() + cookie.getDomain(); // Save cookie into local store, or remove if expired if (!cookie.isExpired(new Date())) { cookies.put(name, cookie); } else { cookies.remove(name); } // Save cookie into persistent store SharedPreferences.Editor prefsWriter = cookiePrefs.edit(); prefsWriter.putString(COOKIE_NAME_STORE, TextUtils.join(",", cookies.keySet())); prefsWriter.putString(COOKIE_NAME_PREFIX + name, encodeCookie(new SerializableCookie(cookie))); prefsWriter.commit(); }
@Override public void addCookie(Cookie cookie) { String name = cookie.getName(); // Save cookie into local store, or remove if expired if(!cookie.isExpired(new Date())) { cookies.put(name, cookie); } else { cookies.remove(name); } // Save cookie into persistent store SharedPreferences.Editor prefsWriter = cookiePrefs.edit(); prefsWriter.putString(COOKIE_NAME_STORE, TextUtils.join(",", cookies.keySet())); prefsWriter.putString(COOKIE_NAME_PREFIX + name, encodeCookie(new SerializableCookie(cookie))); prefsWriter.commit(); }
/** * The function iterates through the list of cookies in the cookiestore and tries to * match them with the cookieName. If there is a match, the cookieStore already * has a valid cookie and the client need not send Credentials for validation purpose. * @param cookieStore The cookie Store * @param cookieName Name of the cookie which needs to be validated * @param isSSL Whether this is a http/https connection * @return true or false based on whether the client needs to send the credentials or * not to the server. */ static boolean needToSendCredentials(CookieStore cookieStore, String cookieName, boolean isSSL) { if (cookieName == null || cookieStore == null) { return true; } List<Cookie> cookies = cookieStore.getCookies(); for (Cookie c : cookies) { // If this is a secured cookie and the current connection is non-secured, // then, skip this cookie. We need to skip this cookie because, the cookie // replay will not be transmitted to the server. if (c.isSecure() && !isSSL) { continue; } if (c.getName().equals(cookieName)) { return false; } } return true; }