Search in sources :

Example 66 with Cookie

use of com.firenio.codec.http11.Cookie in project EhViewer by seven332.

the class CookieRepositoryTest method testRemoveByExpired.

@Test
public void testRemoveByExpired() {
    Context app = RuntimeEnvironment.application;
    HttpUrl urlEh = HttpUrl.parse("http://www.ehviewer.com/");
    Cookie cookieEh1 = new Cookie.Builder().name("level").value("999").domain("www.ehviewer.com").path("/").expiresAt(System.currentTimeMillis() + 100000).build();
    Cookie cookieEh2 = new Cookie.Builder().name("level").value("0").domain("www.ehviewer.com").path("/").expiresAt(System.currentTimeMillis() - 100000).build();
    CookieRepository repository = new CookieRepository(app, "cookie.db");
    repository.saveFromResponse(urlEh, Collections.singletonList(cookieEh1));
    repository.saveFromResponse(urlEh, Collections.singletonList(cookieEh2));
    Map<String, CookieSet> map = Reflect.on(repository).field("map").get();
    assertEquals(1, map.size());
    equals(map.get("www.ehviewer.com"), Collections.<Cookie>emptyList());
    repository.close();
    repository = new CookieRepository(app, "cookie.db");
    map = Reflect.on(repository).field("map").get();
    assertEquals(0, map.size());
    repository.close();
}
Also used : Context(android.content.Context) Cookie(okhttp3.Cookie) HttpUrl(okhttp3.HttpUrl) Test(org.junit.Test)

Example 67 with Cookie

use of com.firenio.codec.http11.Cookie in project EhViewer by seven332.

the class CookieRepositoryTest method equals.

private void equals(CookieSet cookieSet, List<Cookie> cookies) {
    assertNotNull(cookieSet);
    assertNotNull(cookies);
    Map<CookieSet.Key, Cookie> map = Reflect.on(cookieSet).field("map").get();
    assertEquals(cookies.size(), map.size());
    for (Cookie cookie : cookies) {
        assertEquals(cookie, map.get(new CookieSet.Key(cookie)));
    }
}
Also used : Cookie(okhttp3.Cookie)

Example 68 with Cookie

use of com.firenio.codec.http11.Cookie in project EhViewer by seven332.

the class CookieRepositoryTest method testRemoveByNonPersistent.

@Test
public void testRemoveByNonPersistent() {
    Context app = RuntimeEnvironment.application;
    HttpUrl urlEh = HttpUrl.parse("http://www.ehviewer.com/");
    Cookie cookieEh1 = new Cookie.Builder().name("level").value("999").domain("www.ehviewer.com").path("/").expiresAt(System.currentTimeMillis() + 100000).build();
    Cookie cookieEh2 = new Cookie.Builder().name("level").value("0").domain("www.ehviewer.com").path("/").build();
    CookieRepository repository = new CookieRepository(app, "cookie.db");
    repository.saveFromResponse(urlEh, Collections.singletonList(cookieEh1));
    repository.saveFromResponse(urlEh, Collections.singletonList(cookieEh2));
    Map<String, CookieSet> map = Reflect.on(repository).field("map").get();
    assertEquals(1, map.size());
    equals(map.get("www.ehviewer.com"), Collections.singletonList(cookieEh2));
    repository.close();
    repository = new CookieRepository(app, "cookie.db");
    map = Reflect.on(repository).field("map").get();
    assertEquals(0, map.size());
    repository.close();
}
Also used : Context(android.content.Context) Cookie(okhttp3.Cookie) HttpUrl(okhttp3.HttpUrl) Test(org.junit.Test)

Example 69 with Cookie

use of com.firenio.codec.http11.Cookie in project EhViewer by seven332.

the class IdentityCookiePreference method init.

private void init() {
    EhCookieStore store = EhApplication.getEhCookieStore(getContext());
    List<Cookie> eCookies = store.getCookies(HttpUrl.get(EhUrl.HOST_E));
    List<Cookie> exCookies = store.getCookies(HttpUrl.get(EhUrl.HOST_EX));
    List<Cookie> cookies = new LinkedList<>(eCookies);
    cookies.addAll(exCookies);
    String ipbMemberId = null;
    String ipbPassHash = null;
    String igneous = null;
    for (int i = 0, n = cookies.size(); i < n; i++) {
        Cookie cookie = cookies.get(i);
        switch(cookie.name()) {
            case EhCookieStore.KEY_IPD_MEMBER_ID:
                ipbMemberId = cookie.value();
                break;
            case EhCookieStore.KEY_IPD_PASS_HASH:
                ipbPassHash = cookie.value();
                break;
            case EhCookieStore.KEY_IGNEOUS:
                igneous = cookie.value();
                break;
        }
    }
    if (ipbMemberId != null || ipbPassHash != null || igneous != null) {
        message = EhCookieStore.KEY_IPD_MEMBER_ID + ": " + ipbMemberId + "<br>" + EhCookieStore.KEY_IPD_PASS_HASH + ": " + ipbPassHash + "<br>" + EhCookieStore.KEY_IGNEOUS + ": " + igneous;
        setDialogMessage(Html.fromHtml(getContext().getString(R.string.settings_eh_identity_cookies_signed, message)));
        message = message.replace("<br>", "\n");
    } else {
        setDialogMessage(getContext().getString(R.string.settings_eh_identity_cookies_tourist));
    }
}
Also used : Cookie(okhttp3.Cookie) EhCookieStore(com.hippo.ehviewer.client.EhCookieStore) LinkedList(java.util.LinkedList)

Example 70 with Cookie

use of com.firenio.codec.http11.Cookie in project EhViewer by seven332.

the class CookieRepository method getCookies.

public synchronized List<Cookie> getCookies(HttpUrl url) {
    List<Cookie> accepted = new ArrayList<>();
    List<Cookie> expired = new ArrayList<>();
    for (Map.Entry<String, CookieSet> entry : map.entrySet()) {
        String domain = entry.getKey();
        CookieSet cookieSet = entry.getValue();
        if (domainMatch(url, domain)) {
            cookieSet.get(url, accepted, expired);
        }
    }
    for (Cookie cookie : expired) {
        if (cookie.persistent()) {
            db.remove(cookie);
        }
    }
    // RFC 6265 Section-5.4 step 2, sort the cookie-list
    // Cookies with longer paths are listed before cookies with shorter paths.
    // Ignore creation-time, we don't store them.
    Collections.sort(accepted, new Comparator<Cookie>() {

        @Override
        public int compare(Cookie o1, Cookie o2) {
            return o2.path().length() - o1.path().length();
        }
    });
    return accepted;
}
Also used : Cookie(okhttp3.Cookie) ArrayList(java.util.ArrayList) Map(java.util.Map)

Aggregations

Cookie (okhttp3.Cookie)102 ArrayList (java.util.ArrayList)31 IOException (java.io.IOException)20 HttpUrl (okhttp3.HttpUrl)18 ByteArrayInputStream (java.io.ByteArrayInputStream)16 ObjectInputStream (java.io.ObjectInputStream)16 SharedPreferences (android.content.SharedPreferences)12 Context (android.content.Context)8 HashMap (java.util.HashMap)8 List (java.util.List)8 Map (java.util.Map)8 Test (org.junit.Test)8 Response (okhttp3.Response)7 SerializableCookie (com.lzy.okgo.cookie.SerializableCookie)6 Request (okhttp3.Request)6 SuppressLint (android.annotation.SuppressLint)4 CookieManager (android.webkit.CookieManager)4 EhCookieStore (com.hippo.ehviewer.client.EhCookieStore)4 OnClick (butterknife.OnClick)3 SerializableCookie (com.franmontiel.persistentcookiejar.persistence.SerializableCookie)3