|
| 1 | +package express.http; |
| 2 | + |
| 3 | +import java.time.Instant; |
| 4 | +import java.util.HashMap; |
| 5 | + |
| 6 | +public class CookieFactory { |
| 7 | + |
| 8 | + /** |
| 9 | + * Parse an cookie string. |
| 10 | + * |
| 11 | + * @param cookieString The cookie as string. |
| 12 | + * @return The parsed cookie as object. |
| 13 | + */ |
| 14 | + public static Cookie fromString(String cookieString) { |
| 15 | + Cookie cookie = null; |
| 16 | + char[] chars = cookieString.toCharArray(); |
| 17 | + |
| 18 | + StringBuilder key = new StringBuilder(); |
| 19 | + StringBuilder val = new StringBuilder(); |
| 20 | + boolean swap = false; |
| 21 | + |
| 22 | + for (char c : chars) { |
| 23 | + |
| 24 | + if (c == '=') { |
| 25 | + swap = true; |
| 26 | + } else if (c == ';') { |
| 27 | + |
| 28 | + cookie = addField(cookie, key.toString(), val.toString()); |
| 29 | + |
| 30 | + key.setLength(0); |
| 31 | + val.setLength(0); |
| 32 | + swap = false; |
| 33 | + } else if (swap) { |
| 34 | + val.append(c); |
| 35 | + } else { |
| 36 | + key.append(c); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + cookie = addField(cookie, key.toString(), val.toString()); |
| 41 | + return cookie; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Parse an list of strings which represents an cookie. |
| 46 | + * |
| 47 | + * @param stringCookies The list with string cookies. |
| 48 | + * @return A list with the parsed cookies. |
| 49 | + */ |
| 50 | + public static HashMap<String, Cookie> fromStrings(String[] stringCookies) { |
| 51 | + HashMap<String, Cookie> cookies = new HashMap<>(); |
| 52 | + |
| 53 | + if (stringCookies == null || stringCookies.length == 0) |
| 54 | + return cookies; |
| 55 | + |
| 56 | + for (String s : stringCookies) { |
| 57 | + Cookie cookie = fromString(s); |
| 58 | + if (cookie != null) |
| 59 | + cookies.put(cookie.getName(), cookie); |
| 60 | + } |
| 61 | + |
| 62 | + return cookies; |
| 63 | + } |
| 64 | + |
| 65 | + private static Cookie addField(Cookie cookie, String key, String value) { |
| 66 | + key = key.trim(); |
| 67 | + |
| 68 | + if (cookie == null) { |
| 69 | + |
| 70 | + cookie = new Cookie(key, value); |
| 71 | + } else if (key.equalsIgnoreCase("Path")) { |
| 72 | + |
| 73 | + cookie.setPath(value); |
| 74 | + } else if (key.equalsIgnoreCase("Domain")) { |
| 75 | + |
| 76 | + cookie.setDomain(value); |
| 77 | + } else if (key.equalsIgnoreCase("Expire")) { |
| 78 | + |
| 79 | + cookie.setExpire(Instant.parse(value)); |
| 80 | + } else if (key.equalsIgnoreCase("Max-Age")) { |
| 81 | + |
| 82 | + if (isInteger(value)) { |
| 83 | + cookie.setMaxAge(Long.parseLong(value)); |
| 84 | + } |
| 85 | + |
| 86 | + } else if (key.equalsIgnoreCase("SameSite")) { |
| 87 | + |
| 88 | + SameSite sameSite = value.equalsIgnoreCase("LAX") ? SameSite.LAX : SameSite.STRICT; |
| 89 | + cookie.setSameSite(sameSite); |
| 90 | + |
| 91 | + } else if (key.equalsIgnoreCase("Secure")) { |
| 92 | + |
| 93 | + cookie.setSecure(true); |
| 94 | + } else if (key.equalsIgnoreCase("HttpOnly")) { |
| 95 | + |
| 96 | + cookie.setHttpOnly(true); |
| 97 | + } |
| 98 | + |
| 99 | + |
| 100 | + return cookie; |
| 101 | + } |
| 102 | + |
| 103 | + private static boolean isInteger(String str) { |
| 104 | + char[] chars = str.toCharArray(); |
| 105 | + |
| 106 | + for (char c : chars) |
| 107 | + if (c < 48 || c > 57) return false; |
| 108 | + |
| 109 | + return true; |
| 110 | + } |
| 111 | +} |
0 commit comments