Skip to content

Commit

Permalink
Core: fix empty cookie values (#625).
Browse files Browse the repository at this point in the history
If the browser sends a (non-DM) cookie with an empty value a proper ClientState object is created. No exception is thrown.

Thanks, Patricia, for reporting!

Close #625
  • Loading branch information
jri committed Mar 1, 2014
1 parent c1a43a3 commit f6b8f32
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
Expand Up @@ -15,14 +15,14 @@ public class ClientState {

/**
* Converts a "Cookie" header value (String) to a map (key=String, value=String).
* E.g. "user=jri; workspace_id=123" => {"user"="jri", "workspace_id"="123"}
* E.g. "dm4_workspace_id=123; dm4_topicmap_id=234" => {"dm4_workspace_id"="123", "dm4_topicmap_id"="234"}
* <p>
* Called by JAX-RS container to create a ClientState from a "Cookie" @HeaderParam
*/
public ClientState(String cookie) {
if (cookie != null) {
for (String value : cookie.split("; ")) {
String[] val = value.split("=");
String[] val = value.split("=", 2); // Limit 2 ensures 2nd element in case of empty value
values.put(val[0], val[1]);
}
}
Expand Down
23 changes: 23 additions & 0 deletions modules/dm4-test/src/test/java/de/deepamehta/test/JavaAPITest.java
Expand Up @@ -20,6 +20,8 @@ public class JavaAPITest {

private Logger logger = Logger.getLogger(getClass().getName());

// ---

@Test
public void asListWithNullArgument() {
try {
Expand All @@ -35,4 +37,25 @@ public void asListWithNoArguments() {
List l = Arrays.asList();
assertSame(0, l.size());
}

// ---

@Test
public void split() {
String[] a = "abc=123".split("=");
assertSame(2, a.length);
}

@Test
public void splitWithTrailingEmpty() {
String[] a = "abc=".split("=");
assertSame(1, a.length);
}

@Test
public void splitWithTrailingEmptyAndLimit() {
String[] a = "abc=".split("=", 2);
assertSame(2, a.length);
assertEquals("", a[1]);
}
}
Expand Up @@ -381,7 +381,7 @@ var js = {
},

get_cookie: function(key) {
// Note: document.cookie contains all cookies as one string, e.g. "username=jri; workspace_id=83".
// Note: document.cookie contains all cookies as one string, e.g. "dm4_workspace_id=123; dm4_topicmap_id=234"
if (document.cookie.match(new RegExp("\\b" + key + "=(\\w*)"))) {
return RegExp.$1
}
Expand Down

0 comments on commit f6b8f32

Please sign in to comment.