Search in sources :

Example 6 with HttpCookie

use of java.net.HttpCookie in project druid by druid-io.

the class DruidKerberosUtil method getAuthCookie.

public static HttpCookie getAuthCookie(CookieStore cookieStore, URI uri) {
    if (cookieStore == null) {
        return null;
    }
    boolean isSSL = uri.getScheme().equals("https");
    List<HttpCookie> cookies = cookieStore.getCookies();
    for (HttpCookie c : cookies) {
        // replay will not be transmitted to the server.
        if (c.getSecure() && !isSSL) {
            continue;
        }
        if (c.getName().equals(AuthenticatedURL.AUTH_COOKIE)) {
            return c;
        }
    }
    return null;
}
Also used : HttpCookie(java.net.HttpCookie)

Example 7 with HttpCookie

use of java.net.HttpCookie in project android by cSploit.

the class RequestParser method parseRawCookie.

public static ArrayList<HttpCookie> parseRawCookie(String rawCookie) {
    String[] rawCookieParams = rawCookie.split(";");
    ArrayList<HttpCookie> cookies = new ArrayList<HttpCookie>();
    for (String rawCookieParam : rawCookieParams) {
        String[] rawCookieNameAndValue = rawCookieParam.split("=");
        if (rawCookieNameAndValue.length != 2)
            continue;
        String cookieName = rawCookieNameAndValue[0].trim();
        String cookieValue = rawCookieNameAndValue[1].trim();
        HttpCookie cookie;
        try {
            cookie = new HttpCookie(cookieName, cookieValue);
        } catch (IllegalArgumentException e) {
            Logger.error("Invalid cookie. name=" + cookieName + ":" + cookieValue);
            continue;
        }
        for (int i = 1; i < rawCookieParams.length; i++) {
            String[] rawCookieParamNameAndValue = rawCookieParams[i].trim().split("=");
            String paramName = rawCookieParamNameAndValue[0].trim();
            if (paramName.equalsIgnoreCase("secure"))
                cookie.setSecure(true);
            else {
                // attribute not a flag or missing value.
                if (rawCookieParamNameAndValue.length == 2) {
                    String paramValue = rawCookieParamNameAndValue[1].trim();
                    if (paramName.equalsIgnoreCase("max-age")) {
                        long maxAge = Long.parseLong(paramValue);
                        cookie.setMaxAge(maxAge);
                    } else if (paramName.equalsIgnoreCase("domain"))
                        cookie.setDomain(paramValue);
                    else if (paramName.equalsIgnoreCase("path"))
                        cookie.setPath(paramValue);
                    else if (paramName.equalsIgnoreCase("comment"))
                        cookie.setComment(paramValue);
                }
            }
        }
        cookies.add(cookie);
    }
    return cookies;
}
Also used : ArrayList(java.util.ArrayList) HttpCookie(java.net.HttpCookie)

Example 8 with HttpCookie

use of java.net.HttpCookie in project jetty.project by eclipse.

the class CookieTest method testViaCookieManager.

@Test
public void testViaCookieManager() throws Exception {
    // Setup client
    CookieManager cookieMgr = new CookieManager();
    client.setCookieStore(cookieMgr.getCookieStore());
    HttpCookie cookie = new HttpCookie("hello", "world");
    cookie.setPath("/");
    cookie.setVersion(0);
    cookie.setMaxAge(100000);
    cookieMgr.getCookieStore().add(server.getWsUri(), cookie);
    cookie = new HttpCookie("foo", "bar is the word");
    cookie.setPath("/");
    cookie.setMaxAge(100000);
    cookieMgr.getCookieStore().add(server.getWsUri(), cookie);
    // Client connects
    CookieTrackingSocket clientSocket = new CookieTrackingSocket();
    Future<Session> clientConnectFuture = client.connect(clientSocket, server.getWsUri());
    // Server accepts connect
    IBlockheadServerConnection serverConn = server.accept();
    // client confirms upgrade and receipt of frame
    String serverCookies = confirmClientUpgradeAndCookies(clientSocket, clientConnectFuture, serverConn);
    assertThat("Cookies seen at server side", serverCookies, containsString("hello=world"));
    assertThat("Cookies seen at server side", serverCookies, containsString("foo=bar is the word"));
}
Also used : IBlockheadServerConnection(org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection) Matchers.containsString(org.hamcrest.Matchers.containsString) HttpCookie(java.net.HttpCookie) CookieManager(java.net.CookieManager) Session(org.eclipse.jetty.websocket.api.Session) Test(org.junit.Test)

Example 9 with HttpCookie

use of java.net.HttpCookie in project jetty.project by eclipse.

the class ServletUpgradeRequest method getCookies.

@Override
public List<HttpCookie> getCookies() {
    if (cookies == null) {
        Cookie[] requestCookies = request.getCookies();
        if (requestCookies != null) {
            cookies = new ArrayList<>();
            for (Cookie requestCookie : requestCookies) {
                HttpCookie cookie = new HttpCookie(requestCookie.getName(), requestCookie.getValue());
                // No point handling domain/path/expires/secure/httponly on client request cookies
                cookies.add(cookie);
            }
        }
    }
    return cookies;
}
Also used : HttpCookie(java.net.HttpCookie) Cookie(javax.servlet.http.Cookie) HttpCookie(java.net.HttpCookie)

Example 10 with HttpCookie

use of java.net.HttpCookie in project jetty.project by eclipse.

the class HttpClientTest method testStoppingClosesConnections.

@Test
public void testStoppingClosesConnections() throws Exception {
    start(new EmptyServerHandler());
    String host = "localhost";
    int port = connector.getLocalPort();
    String path = "/";
    Response response = client.GET(scheme + "://" + host + ":" + port + path);
    Assert.assertEquals(200, response.getStatus());
    HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP) client.getDestination(scheme, host, port);
    DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
    long start = System.nanoTime();
    HttpConnectionOverHTTP connection = null;
    while (connection == null && TimeUnit.NANOSECONDS.toSeconds(System.nanoTime() - start) < 5) {
        connection = (HttpConnectionOverHTTP) connectionPool.getIdleConnections().peek();
        TimeUnit.MILLISECONDS.sleep(10);
    }
    Assert.assertNotNull(connection);
    String uri = destination.getScheme() + "://" + destination.getHost() + ":" + destination.getPort();
    client.getCookieStore().add(URI.create(uri), new HttpCookie("foo", "bar"));
    client.stop();
    Assert.assertEquals(0, client.getDestinations().size());
    Assert.assertEquals(0, connectionPool.getIdleConnectionCount());
    Assert.assertEquals(0, connectionPool.getActiveConnectionCount());
    Assert.assertFalse(connection.getEndPoint().isOpen());
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Response(org.eclipse.jetty.client.api.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) HttpCookie(java.net.HttpCookie) EndPoint(org.eclipse.jetty.io.EndPoint) HttpConnectionOverHTTP(org.eclipse.jetty.client.http.HttpConnectionOverHTTP) Test(org.junit.Test)

Aggregations

HttpCookie (java.net.HttpCookie)148 CookieManager (java.net.CookieManager)49 CookieStore (java.net.CookieStore)33 URI (java.net.URI)31 Test (org.junit.Test)31 IOException (java.io.IOException)14 Test (org.testng.annotations.Test)13 MockResponse (com.google.mockwebserver.MockResponse)11 MockWebServer (com.google.mockwebserver.MockWebServer)11 ArrayList (java.util.ArrayList)11 HashMap (java.util.HashMap)6 Map (java.util.Map)6 Project (com.kickstarter.models.Project)5 RestResponse (com.linkedin.r2.message.rest.RestResponse)5 URL (java.net.URL)5 Cookie (javax.servlet.http.Cookie)5 HttpServletResponse (javax.servlet.http.HttpServletResponse)5 MockResponse (okhttp3.mockwebserver.MockResponse)5 MockWebServer (okhttp3.mockwebserver.MockWebServer)5 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)5