@Override public String getPath() { return nettyCookie.path(); }
@Override public int compare(Cookie c1, Cookie c2) { String path1 = c1.path(); String path2 = c2.path(); // Cookies with unspecified path default to the path of the request. We don't // know the request path here, but we assume that the length of an unspecified // path is longer than any specified path (i.e. pathless cookies come first), // because setting cookies with a path longer than the request path is of // limited use. int len1 = path1 == null ? Integer.MAX_VALUE : path1.length(); int len2 = path2 == null ? Integer.MAX_VALUE : path2.length(); int diff = len2 - len1; if (diff != 0) { return diff; } // Rely on Java's sort stability to retain creation order in cases where // cookies have same path length. return -1; } };
private org.jooby.Cookie cookie(final Cookie c) { org.jooby.Cookie.Definition cookie = new org.jooby.Cookie.Definition(c.name(), c.value()); Optional.ofNullable(c.domain()).ifPresent(cookie::domain); Optional.ofNullable(c.path()).ifPresent(cookie::path); return cookie.toCookie(); }
@Override public MultiValueMap<String, ResponseCookie> getCookies() { MultiValueMap<String, ResponseCookie> result = new LinkedMultiValueMap<>(); this.response.cookies().values().stream().flatMap(Collection::stream) .forEach(cookie -> result.add(cookie.name(), ResponseCookie.from(cookie.name(), cookie.value()) .domain(cookie.domain()) .path(cookie.path()) .maxAge(cookie.maxAge()) .secure(cookie.isSecure()) .httpOnly(cookie.isHttpOnly()) .build())); return CollectionUtils.unmodifiableMultiValueMap(result); }
/** * @param path The path * @param nettyHeaders The Netty HTTP headers * @param conversionService The conversion service */ public NettyCookies(String path, HttpHeaders nettyHeaders, ConversionService conversionService) { this.conversionService = conversionService; String value = nettyHeaders.get(HttpHeaderNames.COOKIE); if (value != null) { cookies = new LinkedHashMap<>(); Set<io.netty.handler.codec.http.cookie.Cookie> nettyCookies = ServerCookieDecoder.LAX.decode(value); for (io.netty.handler.codec.http.cookie.Cookie nettyCookie : nettyCookies) { String cookiePath = nettyCookie.path(); if (cookiePath != null) { if (path.startsWith(cookiePath)) { cookies.put(nettyCookie.name(), new NettyCookie(nettyCookie)); } } else { cookies.put(nettyCookie.name(), new NettyCookie(nettyCookie)); } } } else { cookies = Collections.emptyMap(); } }
@Override public boolean equals(Object o) { if (this == o) { return true; } if (!(o instanceof Cookie)) { return false; } Cookie that = (Cookie) o; if (!name().equals(that.name())) { return false; } if (path() == null) { if (that.path() != null) { return false; } } else if (that.path() == null) { return false; } else if (!path().equals(that.path())) { return false; } if (domain() == null) { if (that.domain() != null) { return false; } } else { return domain().equalsIgnoreCase(that.domain()); } return true; }
@Override public int compareTo(Cookie c) { int v = name().compareTo(c.name()); if (v != 0) { return v; } if (path() == null) { if (c.path() != null) { return -1; } } else if (c.path() == null) { return 1; } else { v = path().compareTo(c.path()); if (v != 0) { return v; } } if (domain() == null) { if (c.domain() != null) { return -1; } } else if (c.domain() == null) { return 1; } else { v = domain().compareToIgnoreCase(c.domain()); return v; } return 0; }
private void add(String requestDomain, String requestPath, Cookie cookie) { AbstractMap.SimpleEntry<String, Boolean> pair = cookieDomain(cookie.domain(), requestDomain); String keyDomain = pair.getKey(); boolean hostOnly = pair.getValue(); String keyPath = cookiePath(cookie.path(), requestPath); CookieKey key = new CookieKey(cookie.name().toLowerCase(), keyDomain, keyPath); if (hasCookieExpired(cookie, 0)) cookieJar.remove(key); else cookieJar.put(key, new StoredCookie(cookie, hostOnly, cookie.maxAge() != Cookie.UNDEFINED_MAX_AGE)); }
@Override public MultiValueMap<String, ResponseCookie> getCookies() { MultiValueMap<String, ResponseCookie> result = new LinkedMultiValueMap<>(); this.response.cookies().values().stream().flatMap(Collection::stream) .forEach(cookie -> result.add(cookie.name(), ResponseCookie.from(cookie.name(), cookie.value()) .domain(cookie.domain()) .path(cookie.path()) .maxAge(cookie.maxAge()) .secure(cookie.isSecure()) .httpOnly(cookie.isHttpOnly()) .build())); return CollectionUtils.unmodifiableMultiValueMap(result); }
@Override public void run() throws IOException { org.apache.http.HttpResponse response = helper.getResponse(root()); String value = response.getFirstHeader(HttpHeaders.SET_COOKIE).getValue(); Cookie decodeCookie = ClientCookieDecoder.STRICT.decode(value); assertThat(decodeCookie.path(), is("/")); } });
private MultiValueMap<String, ResponseCookie> initCookies() { final MultiValueMap<String, ResponseCookie> cookies = new LinkedMultiValueMap<>(); headers.getAll(HttpHeaderNames.SET_COOKIE) .stream() .map(ClientCookieDecoder.LAX::decode) .forEach(c -> cookies.add(c.name(), ResponseCookie.from(c.name(), c.value()) .maxAge(c.maxAge()) .domain(c.domain()) .path(c.path()) .secure(c.isSecure()) .httpOnly(c.isHttpOnly()) .build())); return cookies; }
@Test public void should_set_and_recognize_cookie_with_path() throws IOException { runWithConfiguration("cookie.json"); Cookie decodeCookie = getCookie("/cookie-with-path"); assertThat(decodeCookie.name(), is("login")); assertThat(decodeCookie.value(), is("true")); assertThat(decodeCookie.path(), is("/")); }
/** * Parse netty cookie to {@link Cookie}. * * @param nettyCookie netty raw cookie instance */ private void parseCookie(io.netty.handler.codec.http.cookie.Cookie nettyCookie) { var cookie = new Cookie(); cookie.name(nettyCookie.name()); cookie.value(nettyCookie.value()); cookie.httpOnly(nettyCookie.isHttpOnly()); cookie.path(nettyCookie.path()); cookie.domain(nettyCookie.domain()); cookie.maxAge(nettyCookie.maxAge()); this.cookies.put(cookie.name(), cookie); }
/** * Parse netty cookie to {@link Cookie}. * * @param nettyCookie netty raw cookie instance */ private void parseCookie(io.netty.handler.codec.http.cookie.Cookie nettyCookie) { var cookie = new Cookie(); cookie.name(nettyCookie.name()); cookie.value(nettyCookie.value()); cookie.httpOnly(nettyCookie.isHttpOnly()); cookie.path(nettyCookie.path()); cookie.domain(nettyCookie.domain()); cookie.maxAge(nettyCookie.maxAge()); this.cookies.put(cookie.name(), cookie); }
harCookie.setPath(cookie.path()); harCookie.setSecure(cookie.isSecure()); if (cookie.maxAge() > 0) {
@Override public String getPath() { return nettyCookie.path(); }
assertThat(setCookie.value()).isEqualTo("1"); assertThat(setCookie.domain()).isEqualTo("http://localhost"); assertThat(setCookie.path()).isEqualTo("/"); assertThat(setCookie.maxAge()).isEqualTo(Duration.ofSeconds(60).getSeconds()); assertThat(setCookie.isSecure()).isTrue();
@Override public String getPath() { return nettyCookie.path(); }
private static ResponseCookie convert(Cookie cookie) { String value = cookie.wrap() ? quote(cookie.value()) : cookie.value(); return responseCookie(cookie.name(), value) .domain(cookie.domain()) .path(cookie.path()) .maxAge(cookie.maxAge()) .httpOnly(cookie.isHttpOnly()) .secure(cookie.isSecure()) .build(); }