Search in sources :

Example 1 with CookieImpl

use of io.undertow.server.handlers.CookieImpl 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 2 with CookieImpl

use of io.undertow.server.handlers.CookieImpl in project core-ng-project by neowu.

the class ResponseHandler method putCookies.

private void putCookies(ResponseImpl response, HttpServerExchange exchange) {
    if (response.cookies != null) {
        Map<String, Cookie> cookies = exchange.getResponseCookies();
        response.cookies.forEach((spec, value) -> {
            CookieImpl cookie = cookie(spec, value);
            cookies.put(spec.name, cookie);
            logger.debug("[response:cookie] name={}, value={}, domain={}, path={}, secure={}, httpOnly={}, maxAge={}", spec.name, new FieldParam(spec.name, cookie.getValue()), cookie.getDomain(), cookie.getPath(), cookie.isSecure(), cookie.isHttpOnly(), cookie.getMaxAge());
        });
    }
}
Also used : Cookie(io.undertow.server.handlers.Cookie) CookieImpl(io.undertow.server.handlers.CookieImpl) FieldParam(core.framework.impl.log.filter.FieldParam)

Example 3 with CookieImpl

use of io.undertow.server.handlers.CookieImpl in project core-ng-project by neowu.

the class ResponseHandlerTest method cookie.

@Test
void cookie() {
    CookieImpl cookie = responseHandler.cookie(new CookieSpec("test").secure(), "1=2");
    assertEquals("test", cookie.getName());
    assertEquals("1%3D2", cookie.getValue());
}
Also used : CookieImpl(io.undertow.server.handlers.CookieImpl) CookieSpec(core.framework.web.CookieSpec) Test(org.junit.jupiter.api.Test)

Example 4 with CookieImpl

use of io.undertow.server.handlers.CookieImpl in project spring-framework by spring-projects.

the class UndertowServerHttpResponse method applyCookies.

@SuppressWarnings("deprecation")
@Override
protected void applyCookies() {
    for (String name : getCookies().keySet()) {
        for (ResponseCookie httpCookie : getCookies().get(name)) {
            Cookie cookie = new CookieImpl(name, httpCookie.getValue());
            if (!httpCookie.getMaxAge().isNegative()) {
                cookie.setMaxAge((int) httpCookie.getMaxAge().getSeconds());
            }
            if (httpCookie.getDomain() != null) {
                cookie.setDomain(httpCookie.getDomain());
            }
            if (httpCookie.getPath() != null) {
                cookie.setPath(httpCookie.getPath());
            }
            cookie.setSecure(httpCookie.isSecure());
            cookie.setHttpOnly(httpCookie.isHttpOnly());
            cookie.setSameSiteMode(httpCookie.getSameSite());
            // getResponseCookies() is deprecated in Undertow 2.2
            this.exchange.getResponseCookies().putIfAbsent(name, cookie);
        }
    }
}
Also used : Cookie(io.undertow.server.handlers.Cookie) ResponseCookie(org.springframework.http.ResponseCookie) CookieImpl(io.undertow.server.handlers.CookieImpl) ResponseCookie(org.springframework.http.ResponseCookie)

Example 5 with CookieImpl

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

the class SessionCookieConfig method setSessionId.

@Override
public void setSessionId(final HttpServerExchange exchange, final String sessionId) {
    Cookie cookie = new CookieImpl(cookieName, sessionId).setPath(path).setDomain(domain).setDiscard(discard).setSecure(secure).setHttpOnly(httpOnly).setComment(comment);
    if (maxAge > 0) {
        cookie.setMaxAge(maxAge);
    }
    exchange.setResponseCookie(cookie);
    UndertowLogger.SESSION_LOGGER.tracef("Setting session cookie session id %s on %s", sessionId, exchange);
}
Also used : Cookie(io.undertow.server.handlers.Cookie) CookieImpl(io.undertow.server.handlers.CookieImpl)

Aggregations

CookieImpl (io.undertow.server.handlers.CookieImpl)11 Cookie (io.undertow.server.handlers.Cookie)7 SecureRandomSessionIdGenerator (io.undertow.server.session.SecureRandomSessionIdGenerator)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 TreeMap (java.util.TreeMap)2 FieldParam (core.framework.impl.log.filter.FieldParam)1 CookieSpec (core.framework.web.CookieSpec)1 AuthenticationCallHandler (io.undertow.security.handlers.AuthenticationCallHandler)1 AuthenticationConstraintHandler (io.undertow.security.handlers.AuthenticationConstraintHandler)1 HttpHandler (io.undertow.server.HttpHandler)1 PathHandler (io.undertow.server.handlers.PathHandler)1 StatusCodes (io.undertow.util.StatusCodes)1 List (java.util.List)1 Supplier (java.util.function.Supplier)1 Collectors (java.util.stream.Collectors)1 SimpleRoutingSupport (org.jboss.as.web.session.SimpleRoutingSupport)1 SimpleSessionIdentifierCodec (org.jboss.as.web.session.SimpleSessionIdentifierCodec)1 Service (org.jboss.msc.Service)1 StartContext (org.jboss.msc.service.StartContext)1