use of com.firenio.codec.http11.Cookie in project EhViewer by seven332.
the class UConfigActivity method onDestroy.
@Override
protected void onDestroy() {
super.onDestroy();
webView.destroy();
// Put cookies back to okhttp cookie store
CookieManager cookieManager = CookieManager.getInstance();
String cookiesString = cookieManager.getCookie(url);
if (cookiesString != null && !cookiesString.isEmpty()) {
EhCookieStore store = EhApplication.getEhCookieStore(this);
HttpUrl eUrl = HttpUrl.parse(EhUrl.HOST_E);
HttpUrl exUrl = HttpUrl.parse(EhUrl.HOST_EX);
// The cookies saved in the uconfig page should be shared between e and ex
for (String header : cookiesString.split(";")) {
Cookie eCookie = Cookie.parse(eUrl, header);
if (eCookie != null) {
store.addCookie(longLive(eCookie));
}
Cookie exCookie = Cookie.parse(exUrl, header);
if (exCookie != null) {
store.addCookie(longLive(exCookie));
}
}
}
}
use of com.firenio.codec.http11.Cookie in project EhViewer by seven332.
the class CookieRepository method getCookieHeader.
public String getCookieHeader(HttpUrl url) {
List<Cookie> cookies = getCookies(url);
StringBuilder cookieHeader = new StringBuilder();
for (int i = 0, size = cookies.size(); i < size; i++) {
if (i > 0) {
cookieHeader.append("; ");
}
Cookie cookie = cookies.get(i);
cookieHeader.append(cookie.name()).append('=').append(cookie.value());
}
return cookieHeader.toString();
}
use of com.firenio.codec.http11.Cookie in project EhViewer by seven332.
the class CookieSet method get.
/**
* Get cookies for the url. Fill {@code accepted} and {@code expired}.
*/
public void get(HttpUrl url, List<Cookie> accepted, List<Cookie> expired) {
long now = System.currentTimeMillis();
Iterator<Map.Entry<Key, Cookie>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Cookie cookie = iterator.next().getValue();
if (cookie.expiresAt() <= now) {
iterator.remove();
expired.add(cookie);
} else if (cookie.matches(url)) {
accepted.add(cookie);
}
}
}
use of com.firenio.codec.http11.Cookie in project EhViewer by seven332.
the class CookieRepositoryTest method equals.
private void equals(List<Cookie> cookies1, List<Cookie> cookies2) {
assertNotNull(cookies1);
assertNotNull(cookies2);
assertEquals(cookies1.size(), cookies2.size());
for (Cookie cookie : cookies1) {
assertTrue(cookies2.contains(cookie));
}
}
use of com.firenio.codec.http11.Cookie in project EhViewer by seven332.
the class CookieRepositoryTest method testPersistent.
@Test
public void testPersistent() {
Context app = RuntimeEnvironment.application;
HttpUrl urlEh = HttpUrl.parse("http://www.ehviewer.com/");
Cookie cookieEh1 = new Cookie.Builder().name("user").value("1234567890").domain("ehviewer.com").path("/").build();
Cookie cookieEh2 = new Cookie.Builder().name("level").value("999").domain("www.ehviewer.com").path("/").expiresAt(System.currentTimeMillis() + 100000).build();
Cookie cookieEh3 = new Cookie.Builder().name("speed").value("10").domain("www.ehviewer.com").path("/").expiresAt(System.currentTimeMillis() + 100000).build();
HttpUrl urlNMB = HttpUrl.parse("http://h.nimingban.com/");
Cookie cookieNMB = new Cookie.Builder().name("hash").value("0987654321").domain("nimingban.com").expiresAt(System.currentTimeMillis() + 100000).path("/").build();
CookieRepository repository = new CookieRepository(app, "cookie.db");
repository.saveFromResponse(urlEh, Arrays.asList(cookieEh1, cookieEh2, cookieEh3));
repository.saveFromResponse(urlNMB, Collections.singletonList(cookieNMB));
Map<String, CookieSet> map = Reflect.on(repository).field("map").get();
assertEquals(3, map.size());
equals(map.get("ehviewer.com"), Collections.singletonList(cookieEh1));
equals(map.get("www.ehviewer.com"), Arrays.asList(cookieEh2, cookieEh3));
equals(map.get("nimingban.com"), Collections.singletonList(cookieNMB));
repository.close();
repository = new CookieRepository(app, "cookie.db");
map = Reflect.on(repository).field("map").get();
assertEquals(2, map.size());
equals(map.get("www.ehviewer.com"), Arrays.asList(cookieEh2, cookieEh3));
equals(map.get("nimingban.com"), Collections.singletonList(cookieNMB));
repository.close();
}
Aggregations