Search in sources :

Example 11 with Cookie

use of io.undertow.server.handlers.Cookie in project undertow by undertow-io.

the class Cookies method parseCookie.

private static void parseCookie(final String cookie, final Map<String, Cookie> parsedCookies, int maxCookies, boolean allowEqualInValue) {
    int state = 0;
    String name = null;
    int start = 0;
    int cookieCount = parsedCookies.size();
    final Map<String, String> cookies = new HashMap<>();
    final Map<String, String> additional = new HashMap<>();
    for (int i = 0; i < cookie.length(); ++i) {
        char c = cookie.charAt(i);
        switch(state) {
            case 0:
                {
                    //eat leading whitespace
                    if (c == ' ' || c == '\t' || c == ';') {
                        start = i + 1;
                        break;
                    }
                    state = 1;
                //fall through
                }
            case 1:
                {
                    //extract key
                    if (c == '=') {
                        name = cookie.substring(start, i);
                        start = i + 1;
                        state = 2;
                    } else if (c == ';') {
                        if (name != null) {
                            cookieCount = createCookie(name, cookie.substring(start, i), maxCookies, cookieCount, cookies, additional);
                        } else if (UndertowLogger.REQUEST_LOGGER.isTraceEnabled()) {
                            UndertowLogger.REQUEST_LOGGER.trace("Ignoring invalid cookies in header " + cookie);
                        }
                        state = 0;
                        start = i + 1;
                    }
                    break;
                }
            case 2:
                {
                    //extract value
                    if (c == ';') {
                        cookieCount = createCookie(name, cookie.substring(start, i), maxCookies, cookieCount, cookies, additional);
                        state = 0;
                        start = i + 1;
                    } else if (c == '"' && start == i) {
                        //only process the " if it is the first character
                        state = 3;
                        start = i + 1;
                    } else if (!allowEqualInValue && c == '=') {
                        cookieCount = createCookie(name, cookie.substring(start, i), maxCookies, cookieCount, cookies, additional);
                        state = 4;
                        start = i + 1;
                    }
                    break;
                }
            case 3:
                {
                    //extract quoted value
                    if (c == '"') {
                        cookieCount = createCookie(name, cookie.substring(start, i), maxCookies, cookieCount, cookies, additional);
                        state = 0;
                        start = i + 1;
                    }
                    break;
                }
            case 4:
                {
                    //skip value portion behind '='
                    if (c == ';') {
                        state = 0;
                    }
                    start = i + 1;
                    break;
                }
        }
    }
    if (state == 2) {
        createCookie(name, cookie.substring(start), maxCookies, cookieCount, cookies, additional);
    }
    for (final Map.Entry<String, String> entry : cookies.entrySet()) {
        Cookie c = new CookieImpl(entry.getKey(), entry.getValue());
        String domain = additional.get(DOMAIN);
        if (domain != null) {
            c.setDomain(domain);
        }
        String version = additional.get(VERSION);
        if (version != null) {
            c.setVersion(Integer.parseInt(version));
        }
        String path = additional.get(PATH);
        if (path != null) {
            c.setPath(path);
        }
        parsedCookies.put(c.getName(), c);
    }
}
Also used : Cookie(io.undertow.server.handlers.Cookie) CookieImpl(io.undertow.server.handlers.CookieImpl) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map) HashMap(java.util.HashMap)

Example 12 with Cookie

use of io.undertow.server.handlers.Cookie in project indy by Commonjava.

the class HeaderDebugger method handleRequest.

@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
    if (logger.isTraceEnabled()) {
        logger.trace("{} {}", exchange.getRequestMethod(), exchange.getRequestPath());
        logger.trace("FROM: {}", exchange.getSourceAddress());
        final HeaderMap headerMap = exchange.getRequestHeaders();
        final Collection<HttpString> names = headerMap.getHeaderNames();
        logger.trace("HEADERS: ");
        for (final HttpString name : names) {
            final HeaderValues values = headerMap.get(name);
            for (final String value : values) {
                logger.trace("{}: {}", name, value);
            }
        }
        logger.trace("COOKIES: ");
        final Map<String, Cookie> cookies = exchange.getRequestCookies();
        for (final String key : cookies.keySet()) {
            final Cookie cookie = cookies.get(key);
            if (cookie == null) {
                continue;
            }
            logger.trace("Cookie({}):\n  Domain: {}\n  Name: {}\n  Path: {}\n  Value: {}\n  Expires: {}\n  Max-Age: {}\n  Comment: {}\n  Version: {}\n\n", key, cookie.getDomain(), cookie.getName(), cookie.getPath(), cookie.getValue(), cookie.getExpires(), cookie.getMaxAge(), cookie.getComment(), cookie.getVersion());
        }
    }
    handler.handleRequest(exchange);
}
Also used : Cookie(io.undertow.server.handlers.Cookie) HeaderMap(io.undertow.util.HeaderMap) HeaderValues(io.undertow.util.HeaderValues) HttpString(io.undertow.util.HttpString) HttpString(io.undertow.util.HttpString)

Example 13 with Cookie

use of io.undertow.server.handlers.Cookie in project undertow by undertow-io.

the class JvmRouteHandler method handleRequest.

@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
    Cookie sessionId = exchange.getRequestCookies().get(sessionCookieName);
    if (sessionId != null) {
        int part = sessionId.getValue().indexOf('.');
        if (part != -1) {
            sessionId.setValue(sessionId.getValue().substring(0, part));
        }
    }
    exchange.addResponseWrapper(wrapper);
    next.handleRequest(exchange);
}
Also used : Cookie(io.undertow.server.handlers.Cookie)

Example 14 with Cookie

use of io.undertow.server.handlers.Cookie in project undertow by undertow-io.

the class LoadBalancingProxyClient method findStickyHost.

protected Host findStickyHost(HttpServerExchange exchange) {
    Map<String, Cookie> cookies = exchange.getRequestCookies();
    for (String cookieName : sessionCookieNames) {
        Cookie sk = cookies.get(cookieName);
        if (sk != null) {
            int index = sk.getValue().indexOf('.');
            if (index == -1) {
                continue;
            }
            String route = sk.getValue().substring(index + 1);
            index = route.indexOf('.');
            if (index != -1) {
                route = route.substring(0, index);
            }
            return routes.get(route);
        }
    }
    return null;
}
Also used : Cookie(io.undertow.server.handlers.Cookie)

Example 15 with Cookie

use of io.undertow.server.handlers.Cookie in project undertow by undertow-io.

the class CookiesTestCase method testEqualsInValueNotAllowedInQuotedValue.

@Test
public void testEqualsInValueNotAllowedInQuotedValue() {
    Map<String, Cookie> cookies = Cookies.parseRequestCookies(2, false, Arrays.asList("CUSTOMER=\"WILE_E_COYOTE=THE_COYOTE\"; SHIPPING=FEDEX"));
    Assert.assertEquals(2, cookies.size());
    Cookie cookie = cookies.get("CUSTOMER");
    Assert.assertNotNull(cookie);
    Assert.assertEquals("WILE_E_COYOTE=THE_COYOTE", cookie.getValue());
    cookie = cookies.get("SHIPPING");
    Assert.assertNotNull(cookie);
    Assert.assertEquals("FEDEX", cookie.getValue());
}
Also used : Cookie(io.undertow.server.handlers.Cookie) UnitTest(io.undertow.testutils.category.UnitTest) Test(org.junit.Test)

Aggregations

Cookie (io.undertow.server.handlers.Cookie)21 UnitTest (io.undertow.testutils.category.UnitTest)12 Test (org.junit.Test)12 CookieImpl (io.undertow.server.handlers.CookieImpl)4 HttpString (io.undertow.util.HttpString)2 NotificationReceiver (io.undertow.security.api.NotificationReceiver)1 SecurityNotification (io.undertow.security.api.SecurityNotification)1 Account (io.undertow.security.idm.Account)1 Session (io.undertow.server.session.Session)1 HeaderMap (io.undertow.util.HeaderMap)1 HeaderValues (io.undertow.util.HeaderValues)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 HttpCookie (org.springframework.http.HttpCookie)1 ResponseCookie (org.springframework.http.ResponseCookie)1 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)1