Skip to content

Commit

Permalink
Fix NewCookie parsing
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas NESMON (NicoNes) <nicolas.nesmon@gmail.com>
  • Loading branch information
NicoNes authored and jamezp committed Jun 9, 2021
1 parent 46a94c5 commit 12b23bb
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 14 deletions.
Expand Up @@ -10,6 +10,7 @@
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;

Expand All @@ -21,6 +22,7 @@ public class NewCookieHeaderDelegate implements RuntimeDelegate.HeaderDelegate<N
public static final NewCookieHeaderDelegate INSTANCE = new NewCookieHeaderDelegate();
private static final String OLD_COOKIE_PATTERN = "EEE, dd-MMM-yyyy HH:mm:ss z";

@Override
public NewCookie fromString(String newCookie) throws IllegalArgumentException {
if (newCookie == null) throw new IllegalArgumentException(Messages.MESSAGES.newCookieValueNull());
String cookieName = null;
Expand All @@ -35,12 +37,16 @@ public NewCookie fromString(String newCookie) throws IllegalArgumentException {
Date expiry = null;

ParameterParser parser = new ParameterParser();
Map<String, String> map = parser.parse(newCookie, ';');
LinkedHashMap<String, String> map = parser.parse(newCookie, ';');

for (Map.Entry<String, String> entry : map.entrySet()) {
String name = entry.getKey();
String value = entry.getValue();
if (name.equalsIgnoreCase("Comment"))
// Cookie name is always the first attribute (https://datatracker.ietf.org/doc/html/rfc6265#section-4.1.1).
if (cookieName == null) {
cookieName = name;
cookieValue = value;
} else if (name.equalsIgnoreCase("Comment"))
comment = value;
else if (name.equalsIgnoreCase("Domain"))
domain = value;
Expand All @@ -62,9 +68,6 @@ else if (name.equalsIgnoreCase("Expires")) {
catch (ParseException e)
{
}
} else {
cookieName = name;
cookieValue = value;
}

}
Expand Down
@@ -1,7 +1,6 @@
package org.jboss.resteasy.util;

import java.util.HashMap;
import java.util.Map;
import java.util.LinkedHashMap;

/**
* A simple parser intended to parse sequences of name/value pairs.
Expand Down Expand Up @@ -223,11 +222,11 @@ public void setLowerCaseNames(boolean b)
* @param separator the name/value pairs separator
* @return a map of name/value pairs
*/
public Map<String, String> parse(final String str, char separator)
public LinkedHashMap<String, String> parse(final String str, char separator)
{
if (str == null)
{
return new HashMap<String, String>();
return new LinkedHashMap<>();
}
return parse(str.toCharArray(), separator);
}
Expand All @@ -241,11 +240,11 @@ public Map<String, String> parse(final String str, char separator)
* @param separator the name/value pairs separator
* @return a map of name/value pairs
*/
public Map<String, String> parse(final char[] chars, char separator)
public LinkedHashMap<String, String> parse(final char[] chars, char separator)
{
if (chars == null)
{
return new HashMap<String, String>();
return new LinkedHashMap<>();
}
return parse(chars, 0, chars.length, separator);
}
Expand All @@ -261,7 +260,7 @@ public Map<String, String> parse(final char[] chars, char separator)
* @param separator the name/value pairs separator
* @return a map of name/value pairs
*/
public Map<String, String> parse(
public LinkedHashMap<String, String> parse(
final char[] chars,
int offset,
int length,
Expand All @@ -270,9 +269,9 @@ public Map<String, String> parse(

if (chars == null)
{
return new HashMap<String, String>();
return new LinkedHashMap<>();
}
HashMap<String, String> params = new HashMap<String, String>();
LinkedHashMap<String, String> params = new LinkedHashMap<>();
this.chars = chars;
this.pos = offset;
this.len = length;
Expand Down
@@ -0,0 +1,34 @@
package org.jboss.resteasy.test.cookie;

import static org.junit.Assert.assertEquals;

import javax.ws.rs.core.NewCookie;

import org.jboss.resteasy.plugins.delegates.NewCookieHeaderDelegate;
import org.junit.Before;
import org.junit.Test;

public class NewCookieHeaderDelegateTest
{

private NewCookieHeaderDelegate delegate;

@Before
public void setUp() throws Exception
{
delegate = new NewCookieHeaderDelegate();
}

@Test
public void testParseIgnoresUnknownCookieAttributes()
{

String expectedCookieName = "JSESSIONID";
String expectedCookieValue = "1fn1creezbh0117ej8n463jjwm";
NewCookie newCookie = delegate.fromString(expectedCookieName + "=" + expectedCookieValue + ";Path=/path;UnknownAttribute=AnyValue");

assertEquals(expectedCookieName, newCookie.getName());
assertEquals(expectedCookieValue, newCookie.getValue());
}

}

0 comments on commit 12b23bb

Please sign in to comment.