@Override
    public int compare(final HttpCookie c1, final HttpCookie c2) {
      if (c1 == c2) {
        return 0;
      }
      if (c1 == null) {
        return -1;
      }
      if (c2 == null) {
        return 1;
      }

      // path rule only applies to the cookies with same name
      if (!c1.getName().equals(c2.getName())) {
        return 0;
      }

      // those with more specific Path attributes precede those with less
      // specific
      if (c1.getPath().startsWith(c2.getPath())) {
        return -1;
      } else if (c2.getPath().startsWith(c1.getPath())) {
        return 1;
      } else {
        return 0;
      }
    }
Exemplo n.º 2
0
    public int compare(HttpCookie c1, HttpCookie c2) {
      if (c1 == c2) return 0;
      if (c1 == null) return -1;
      if (c2 == null) return 1;

      // path rule only applies to the cookies with same name
      if (!c1.getName().equals(c2.getName())) return 0;

      // those with more specific Path attributes precede those with less specific
      if (c1.getPath().startsWith(c2.getPath())) return -1;
      else if (c2.getPath().startsWith(c1.getPath())) return 1;
      else return 0;
    }
Exemplo n.º 3
0
  @Test
  public void testRfc2109Response() throws Exception {
    CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
    CookieHandler.setDefault(cookieManager);
    MockWebServer server = new MockWebServer();
    server.play();

    server.enqueue(
        new MockResponse()
            .addHeader(
                "Set-Cookie: a=android; "
                    + "Comment=this cookie is delicious; "
                    + "Domain="
                    + server.getCookieDomain()
                    + "; "
                    + "Max-Age=60; "
                    + "Path=/path; "
                    + "Secure; "
                    + "Version=1"));
    get(server, "/path/foo");

    List<HttpCookie> cookies = cookieManager.getCookieStore().getCookies();
    assertEquals(1, cookies.size());
    HttpCookie cookie = cookies.get(0);
    assertEquals("a", cookie.getName());
    assertEquals("android", cookie.getValue());
    assertEquals("this cookie is delicious", cookie.getComment());
    assertEquals(null, cookie.getCommentURL());
    assertEquals(false, cookie.getDiscard());
    assertTrue(server.getCookieDomain().equalsIgnoreCase(cookie.getDomain()));
    assertEquals(60, cookie.getMaxAge());
    assertEquals("/path", cookie.getPath());
    assertEquals(true, cookie.getSecure());
    assertEquals(1, cookie.getVersion());
  }
Exemplo n.º 4
0
  @Test
  public void testNetscapeResponse() throws Exception {
    CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
    CookieHandler.setDefault(cookieManager);
    MockWebServer server = new MockWebServer();
    server.play();

    server.enqueue(
        new MockResponse()
            .addHeader(
                "Set-Cookie: a=android; "
                    + "expires=Fri, 31-Dec-9999 23:59:59 GMT; "
                    + "path=/path; "
                    + "domain="
                    + server.getCookieDomain()
                    + "; "
                    + "secure"));
    get(server, "/path/foo");

    List<HttpCookie> cookies = cookieManager.getCookieStore().getCookies();
    assertEquals(1, cookies.size());
    HttpCookie cookie = cookies.get(0);
    assertEquals("a", cookie.getName());
    assertEquals("android", cookie.getValue());
    assertEquals(null, cookie.getComment());
    assertEquals(null, cookie.getCommentURL());
    assertEquals(false, cookie.getDiscard());
    assertTrue(server.getCookieDomain().equalsIgnoreCase(cookie.getDomain()));
    assertTrue(cookie.getMaxAge() > 100000000000L);
    assertEquals("/path", cookie.getPath());
    assertEquals(true, cookie.getSecure());
    assertEquals(0, cookie.getVersion());
  }
Exemplo n.º 5
0
  @Test
  public void testRfc2109Response() throws Exception {
    CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
    client.setCookieJar(new JavaNetCookieJar(cookieManager));
    MockWebServer server = new MockWebServer();
    server.start();

    HttpUrl urlWithIpAddress = urlWithIpAddress(server, "/path/foo");
    server.enqueue(
        new MockResponse()
            .addHeader(
                "Set-Cookie: a=android; "
                    + "Comment=this cookie is delicious; "
                    + "Domain="
                    + urlWithIpAddress.host()
                    + "; "
                    + "Max-Age=60; "
                    + "Path=/path; "
                    + "Secure; "
                    + "Version=1"));
    get(urlWithIpAddress);

    List<HttpCookie> cookies = cookieManager.getCookieStore().getCookies();
    assertEquals(1, cookies.size());
    HttpCookie cookie = cookies.get(0);
    assertEquals("a", cookie.getName());
    assertEquals("android", cookie.getValue());
    assertEquals(null, cookie.getCommentURL());
    assertEquals(false, cookie.getDiscard());
    assertEquals(60.0, cookie.getMaxAge(), 1.0); // Converting to a fixed date can cause rounding!
    assertEquals("/path", cookie.getPath());
    assertEquals(true, cookie.getSecure());
  }
Exemplo n.º 6
0
  @Test
  public void testNetscapeResponse() throws Exception {
    CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
    client.setCookieJar(new JavaNetCookieJar(cookieManager));
    MockWebServer server = new MockWebServer();
    server.start();

    HttpUrl urlWithIpAddress = urlWithIpAddress(server, "/path/foo");
    server.enqueue(
        new MockResponse()
            .addHeader(
                "Set-Cookie: a=android; "
                    + "expires=Fri, 31-Dec-9999 23:59:59 GMT; "
                    + "path=/path; "
                    + "domain="
                    + urlWithIpAddress.host()
                    + "; "
                    + "secure"));
    get(urlWithIpAddress);

    List<HttpCookie> cookies = cookieManager.getCookieStore().getCookies();
    assertEquals(1, cookies.size());
    HttpCookie cookie = cookies.get(0);
    assertEquals("a", cookie.getName());
    assertEquals("android", cookie.getValue());
    assertEquals(null, cookie.getComment());
    assertEquals(null, cookie.getCommentURL());
    assertEquals(false, cookie.getDiscard());
    assertTrue(cookie.getMaxAge() > 100000000000L);
    assertEquals("/path", cookie.getPath());
    assertEquals(true, cookie.getSecure());
    assertEquals(0, cookie.getVersion());
  }
 private void removeFromPersistence(URI uri, List<HttpCookie> cookiesToRemove) {
   SharedPreferences.Editor editor = sharedPreferences.edit();
   for (HttpCookie cookieToRemove : cookiesToRemove) {
     editor.remove(uri.toString() + SP_KEY_DELIMITER + cookieToRemove.getName());
   }
   editor.apply();
 }
  private void saveToPersistence(URI uri, HttpCookie cookie) {
    SharedPreferences.Editor editor = sharedPreferences.edit();

    editor.putString(
        uri.toString() + SP_KEY_DELIMITER + cookie.getName(),
        new SerializableHttpCookie().encode(cookie));

    editor.apply();
  }
Exemplo n.º 9
0
 @Override
 public void beforeRequest(Map<String, List<String>> headers) {
   CookieStore cookieStore = getCookieStore();
   List<HttpCookie> cookies = cookieStore.get(URI.create(getURL()));
   if (!cookies.isEmpty()) {
     List<String> cookieHeader = headers.get("Cookie");
     if (cookieHeader == null) cookieHeader = headers.get("cookie");
     if (cookieHeader == null) headers.put("Cookie", cookieHeader = new ArrayList<>());
     for (HttpCookie cookie : cookies)
       cookieHeader.add(cookie.getName() + "=" + cookie.getValue());
   }
 }
Exemplo n.º 10
0
  public void saveState(Activity a) {
    Log.v(DEBUG_TAG, "saveState() requested");

    if (!loggedIn) {
      return;
    }

    Log.v(DEBUG_TAG, "saving state");
    SharedPreferences prefs = a.getSharedPreferences("cookies", 0);
    SharedPreferences.Editor editor = prefs.edit();

    for (HttpCookie cookie : cm.getCookieStore().getCookies()) {
      if (cookie.getName().equals("RevTK")) {
        Log.v(DEBUG_TAG, "put RevTK cookie");
        editor.putString(cookie.getName(), cookie.getValue());
        break;
      }
    }

    editor.apply();
  }
Exemplo n.º 11
0
 @Override
 public NettyHttpResponse cookie(HttpCookie httpCookie) {
   Cookie nettyCookie = new DefaultCookie(httpCookie.getName(), httpCookie.getValue());
   nettyCookie.setDomain(httpCookie.getDomain());
   nettyCookie.setPath(httpCookie.getPath());
   nettyCookie.setSecure(httpCookie.getSecure());
   if (0 <= (int) httpCookie.getMaxAge()) nettyCookie.setMaxAge((int) httpCookie.getMaxAge());
   nettyCookie.setVersion(httpCookie.getVersion());
   nettyCookie.setDiscard(httpCookie.getDiscard());
   nettyCookie.setHttpOnly(true);
   CookieEncoder encoder = new CookieEncoder(true);
   encoder.addCookie(nettyCookie);
   return header(HttpHeaders.Names.SET_COOKIE, encoder.encode());
 }
Exemplo n.º 12
0
  /**
   * Test the equality of two http cookies.
   *
   * <p>The result is <tt>true</tt> only if two cookies come from same domain (case-insensitive),
   * have same name (case-insensitive), and have same path (case-sensitive).
   *
   * @return <tt>true</tt> if 2 http cookies equal to each other; otherwise, <tt>false</tt>
   */
  @Override
  public boolean equals(Object obj) {
    if (obj == this) return true;
    if (!(obj instanceof HttpCookie)) return false;
    HttpCookie other = (HttpCookie) obj;

    // One http cookie equals to another cookie (RFC 2965 sec. 3.3.3) if:
    //   1. they come from same domain (case-insensitive),
    //   2. have same name (case-insensitive),
    //   3. and have same path (case-sensitive).
    return equalsIgnoreCase(getName(), other.getName())
        && equalsIgnoreCase(getDomain(), other.getDomain())
        && Objects.equals(getPath(), other.getPath());
  }
Exemplo n.º 13
0
 /**
  * Convert a request header to OkHttp's cookies via {@link HttpCookie}. That extra step handles
  * multiple cookies in a single request header, which {@link Cookie#parse} doesn't support.
  */
 private List<Cookie> decodeHeaderAsJavaNetCookies(HttpUrl url, String header) {
   List<HttpCookie> javaNetCookies;
   try {
     javaNetCookies = HttpCookie.parse(header);
   } catch (IllegalArgumentException e) {
     // Unfortunately sometimes java.net gives a Cookie like "$Version=1" which it can't parse!
     Internal.logger.log(WARNING, "Parsing request cookie failed for " + url.resolve("/..."), e);
     return Collections.emptyList();
   }
   List<Cookie> result = new ArrayList<>();
   for (HttpCookie javaNetCookie : javaNetCookies) {
     result.add(
         new Cookie.Builder()
             .name(javaNetCookie.getName())
             .value(javaNetCookie.getValue())
             .domain(url.host())
             .build());
   }
   return result;
 }
Exemplo n.º 14
0
  @Test
  public void testQuotedAttributeValues() throws Exception {
    CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
    CookieHandler.setDefault(cookieManager);
    MockWebServer server = new MockWebServer();
    server.play();

    server.enqueue(
        new MockResponse()
            .addHeader(
                "Set-Cookie2: a=\"android\"; "
                    + "Comment=\"this cookie is delicious\"; "
                    + "CommentURL=\"http://google.com/\"; "
                    + "Discard; "
                    + "Domain=\""
                    + server.getCookieDomain()
                    + "\"; "
                    + "Max-Age=\"60\"; "
                    + "Path=\"/path\"; "
                    + "Port=\"80,443,"
                    + server.getPort()
                    + "\"; "
                    + "Secure; "
                    + "Version=\"1\""));
    get(server, "/path/foo");

    List<HttpCookie> cookies = cookieManager.getCookieStore().getCookies();
    assertEquals(1, cookies.size());
    HttpCookie cookie = cookies.get(0);
    assertEquals("a", cookie.getName());
    assertEquals("android", cookie.getValue());
    assertEquals("this cookie is delicious", cookie.getComment());
    assertEquals("http://google.com/", cookie.getCommentURL());
    assertEquals(true, cookie.getDiscard());
    assertTrue(server.getCookieDomain().equalsIgnoreCase(cookie.getDomain()));
    assertEquals(60, cookie.getMaxAge());
    assertEquals("/path", cookie.getPath());
    assertEquals("80,443," + server.getPort(), cookie.getPortlist());
    assertEquals(true, cookie.getSecure());
    assertEquals(1, cookie.getVersion());
  }
Exemplo n.º 15
0
 /**
  * @param uri cookie corresponding uri.
  * @param cookie cookie.
  */
 public CookieEntity(URI uri, HttpCookie cookie) {
   this.uri = uri == null ? null : uri.toString();
   this.name = cookie.getName();
   this.value = cookie.getValue();
   this.comment = cookie.getComment();
   this.commentURL = cookie.getCommentURL();
   this.discard = cookie.getDiscard();
   this.domain = cookie.getDomain();
   long maxAge = cookie.getMaxAge();
   if (maxAge > 0L) { // session, temp cookie
     this.expiry = (maxAge * 1000L) + System.currentTimeMillis();
     // 溢出
     if (this.expiry < 0) this.expiry = HttpDateTime.getMaxExpiryMillis();
   } else {
     this.expiry = -1L;
   }
   this.path = cookie.getPath();
   if (!TextUtils.isEmpty(path) && path.length() > 1 && path.endsWith("/")) {
     this.path = path.substring(0, path.length() - 1);
   }
   this.portList = cookie.getPortlist();
   this.secure = cookie.getSecure();
   this.version = cookie.getVersion();
 }
Exemplo n.º 16
0
  @Override
  public void send(final TransportListener listener, final Message.Mutable... messages) {
    String url = getURL();
    final URI uri = URI.create(url);
    if (_appendMessageType && messages.length == 1 && messages[0].isMeta()) {
      String type = messages[0].getChannel().substring(Channel.META.length());
      if (url.endsWith("/")) url = url.substring(0, url.length() - 1);
      url += type;
    }

    final Request request = _httpClient.newRequest(url).method(HttpMethod.POST);
    request.header(HttpHeader.CONTENT_TYPE.asString(), "application/json;charset=UTF-8");

    StringBuilder builder = new StringBuilder();
    for (HttpCookie cookie : getCookieStore().get(uri)) {
      builder.setLength(0);
      builder.append(cookie.getName()).append("=").append(cookie.getValue());
      request.header(HttpHeader.COOKIE.asString(), builder.toString());
    }

    request.content(new StringContentProvider(generateJSON(messages)));

    customize(request);

    synchronized (this) {
      if (_aborted) throw new IllegalStateException("Aborted");
      _requests.add(request);
    }

    request.listener(
        new Request.Listener.Empty() {
          @Override
          public void onHeaders(Request request) {
            listener.onSending(messages);
          }
        });

    long maxNetworkDelay = _maxNetworkDelay;
    if (messages.length == 1 && Channel.META_CONNECT.equals(messages[0].getChannel())) {
      Map<String, Object> advice = messages[0].getAdvice();
      if (advice == null) advice = _advice;
      if (advice != null) {
        Object timeout = advice.get("timeout");
        if (timeout instanceof Number) maxNetworkDelay += ((Number) timeout).longValue();
        else if (timeout != null) maxNetworkDelay += Long.parseLong(timeout.toString());
      }
    }
    // Set the idle timeout for this request larger than the total timeout
    // so there are no races between the two timeouts
    request.idleTimeout(maxNetworkDelay * 2, TimeUnit.MILLISECONDS);
    request.timeout(maxNetworkDelay, TimeUnit.MILLISECONDS);
    request.send(
        new BufferingResponseListener() {
          @Override
          public boolean onHeader(Response response, HttpField field) {
            // We do not allow cookies to be handled by HttpClient, since one
            // HttpClient instance is shared by multiple BayeuxClient instances.
            // Instead, we store the cookies in the BayeuxClient instance.
            switch (field.getHeader()) {
              case SET_COOKIE:
              case SET_COOKIE2:
                Map<String, List<String>> cookies = new HashMap<>(1);
                cookies.put(field.getName(), Collections.singletonList(field.getValue()));
                storeCookies(uri, cookies);
                return false;
              default:
                return true;
            }
          }

          private void storeCookies(URI uri, Map<String, List<String>> cookies) {
            try {
              _cookieManager.put(uri, cookies);
            } catch (IOException x) {
              logger.debug("", x);
            }
          }

          @Override
          public void onComplete(Result result) {
            synchronized (LongPollingTransport.this) {
              _requests.remove(result.getRequest());
            }

            if (result.isFailed()) {
              listener.onFailure(result.getFailure(), messages);
              return;
            }

            Response response = result.getResponse();
            int status = response.getStatus();
            if (status == HttpStatus.OK_200) {
              String content = getContentAsString();
              if (content != null && content.length() > 0) {
                try {
                  List<Message.Mutable> messages = parseMessages(content);
                  debug("Received messages {}", messages);
                  for (Message.Mutable message : messages) {
                    if (message.isSuccessful()
                        && Channel.META_CONNECT.equals(message.getChannel())) {
                      Map<String, Object> advice = message.getAdvice();
                      if (advice != null && advice.get("timeout") != null) _advice = advice;
                    }
                  }
                  listener.onMessages(messages);
                } catch (ParseException x) {
                  listener.onFailure(x, messages);
                }
              } else {
                Map<String, Object> failure = new HashMap<>(2);
                // Convert the 200 into 204 (no content)
                failure.put("httpCode", 204);
                TransportException x = new TransportException(failure);
                listener.onFailure(x, messages);
              }
            } else {
              Map<String, Object> failure = new HashMap<>(2);
              failure.put("httpCode", status);
              TransportException x = new TransportException(failure);
              listener.onFailure(x, messages);
            }
          }
        });
  }