Skip to content

Commit 6029dec

Browse files
author
John J. Aylward
committed
ensure key names are consistent when parsing the cookie string since
cookie-keys are not case sensitive, but json-keys are.
1 parent d334b58 commit 6029dec

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

src/main/java/org/json/Cookie.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.json;
22

3+
import java.util.Locale;
4+
35
/*
46
Copyright (c) 2002 JSON.org
57
@@ -74,7 +76,9 @@ public static String escape(String string) {
7476
* The name will be stored under the key "name", and the value will be
7577
* stored under the key "value". This method does not do checking or
7678
* validation of the parameters. It only converts the cookie string into
77-
* a JSONObject.
79+
* a JSONObject. All attribute names are converted to lower case keys in the
80+
* JSONObject (HttpOnly => httponly). If an attribute is specified more than
81+
* once, only the value found closer to the end of the cookie-string is kept.
7882
* @param string The cookie specification string.
7983
* @return A JSONObject containing "name", "value", and possibly other
8084
* members.
@@ -104,7 +108,7 @@ public static JSONObject toJSONObject(String string) {
104108
x.next();
105109
// parse the remaining cookie attributes
106110
while (x.more()) {
107-
name = unescape(x.nextTo("=;")).trim();
111+
name = unescape(x.nextTo("=;")).trim().toLowerCase(Locale.ROOT);
108112
// don't allow a cookies attributes to overwrite it's name or value.
109113
if("name".equalsIgnoreCase(name)) {
110114
throw new JSONException("Illegal attribute name: 'name'");

src/test/java/org/json/junit/CookieTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public void booleanAttribute() {
8484
JSONObject jo = Cookie.toJSONObject(cookieStr);
8585
assertTrue("has key 'name'", jo.has("name"));
8686
assertTrue("has key 'value'", jo.has("value"));
87-
assertTrue("has key 'myAttribute'", jo.has("myAttribute"));
87+
assertTrue("has key 'myAttribute'", jo.has("myattribute"));
8888
}
8989

9090
/**
@@ -177,7 +177,7 @@ public void convertCookieToString() {
177177
"thisWont=beIncluded;"+
178178
"secure";
179179
String expectedCookieStr =
180-
"{\"thisWont\":\"beIncluded\","+
180+
"{\"thiswont\":\"beIncluded\","+
181181
"\"path\":\"/\","+
182182
"\"expires\":\"Wed, 19-Mar-2014 17:53:53 GMT\","+
183183
"\"___domain\":\".yahoo.com\","+

0 commit comments

Comments
 (0)