From d334b58f45541d6435a7816d0136c0427af539cc Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Tue, 26 May 2020 08:30:25 -0400 Subject: [PATCH] Made more corrections to Cookie.ToString. 1. Made Cookie Name and Value properties case insensitive 2. Throws exception on illegal Cookie Name 3. Doesn't emit "false" flag values 4. Properly escape key-value attributes. --- src/main/java/org/json/Cookie.java | 43 ++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/json/Cookie.java b/src/main/java/org/json/Cookie.java index ddd1c69..3ce3d74 100644 --- a/src/main/java/org/json/Cookie.java +++ b/src/main/java/org/json/Cookie.java @@ -130,21 +130,42 @@ public class Cookie { /** * Convert a JSONObject into a cookie specification string. The JSONObject - * must contain "name" and "value" members. + * must contain "name" and "value" members (case insensitive). * If the JSONObject contains other members, they will be appended to the cookie * specification string. User-Agents are instructed to ignore unknown attributes, * so ensure your JSONObject is using only known attributes. * See also: https://tools.ietf.org/html/rfc6265 * @param jo A JSONObject * @return A cookie specification string - * @throws JSONException if a called function fails + * @throws JSONException thrown if the cookie has no name. */ public static String toString(JSONObject jo) throws JSONException { StringBuilder sb = new StringBuilder(); - - sb.append(escape(jo.getString("name"))); + + String name = null; + Object value = null; + for(String key : jo.keySet()){ + if("name".equalsIgnoreCase(key)) { + name = jo.getString(key).trim(); + } + if("value".equalsIgnoreCase(key)) { + value=jo.getString(key).trim(); + } + if(name != null && value != null) { + break; + } + } + + if(name == null || "".equals(name.trim())) { + throw new JSONException("Cookie does not have a name"); + } + if(value == null) { + value = ""; + } + + sb.append(escape(name)); sb.append("="); - sb.append(escape(jo.getString("value"))); + sb.append(escape((String)value)); for(String key : jo.keySet()){ if("name".equalsIgnoreCase(key) @@ -152,11 +173,17 @@ public class Cookie { // already processed above continue; } - Object value = jo.opt(key); + value = jo.opt(key); if(value instanceof Boolean) { - sb.append(';').append(key); + if(Boolean.TRUE.equals(value)) { + sb.append(';').append(escape(key)); + } + // don't emit false values } else { - sb.append(';').append(key).append('=').append(escape(value.toString())); + sb.append(';') + .append(escape(key)) + .append('=') + .append(escape(value.toString())); } }