You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@apr.apache.org by mi...@apache.org on 2018/09/01 20:11:10 UTC

svn commit: r1839843 - in /apr/apr-util/branches/1.7.x: include/apr_json.h jose/apr_jose_encode.c json/apr_json.c json/apr_json_decode.c

Author: minfrin
Date: Sat Sep  1 20:11:10 2018
New Revision: 1839843

URL: http://svn.apache.org/viewvc?rev=1839843&view=rev
Log:
Backport r1839840

apr_json: Split apr_json_object_set() into apr_json_object_set() with
a simple key, and apr_json_object_seti_ex() with the key as an
apr_json_value_t, so the caller has a simpler interface to use to add
a key value pair to an object.

Modified:
    apr/apr-util/branches/1.7.x/include/apr_json.h
    apr/apr-util/branches/1.7.x/jose/apr_jose_encode.c
    apr/apr-util/branches/1.7.x/json/apr_json.c
    apr/apr-util/branches/1.7.x/json/apr_json_decode.c

Modified: apr/apr-util/branches/1.7.x/include/apr_json.h
URL: http://svn.apache.org/viewvc/apr/apr-util/branches/1.7.x/include/apr_json.h?rev=1839843&r1=1839842&r2=1839843&view=diff
==============================================================================
--- apr/apr-util/branches/1.7.x/include/apr_json.h (original)
+++ apr/apr-util/branches/1.7.x/include/apr_json.h Sat Sep  1 20:11:10 2018
@@ -271,6 +271,22 @@ APU_DECLARE(apr_json_value_t *)
 /**
  * Associate a value with a key in a JSON object.
  * @param obj The JSON object.
+ * @param key Pointer to the key string.
+ * @param klen Length of the key, or APR_JSON_VALUE_STRING if NUL
+ *   terminated.
+ * @param val Value to associate with the key.
+ * @param pool Pool to use.
+ * @return APR_SUCCESS on success, APR_EINVAL if the key is
+ *   NULL or not a string, or the object is not an APR_JSON_OBJECT.
+ * @remark If the value is NULL the key value pair is deleted.
+ */
+APU_DECLARE(apr_status_t) apr_json_object_set(apr_json_value_t *obj,
+        const char *key, apr_ssize_t klen, apr_json_value_t *val,
+        apr_pool_t *pool) __attribute__((nonnull(1, 2, 5)));
+
+/**
+ * Associate a value with a key in a JSON object, preserving whitespace.
+ * @param obj The JSON object.
  * @param key Pointer to the key string, including any whitespace
  *   required.
  * @param val Value to associate with the key.
@@ -279,9 +295,9 @@ APU_DECLARE(apr_json_value_t *)
  *   NULL or not a string, or the object is not an APR_JSON_OBJECT.
  * @remark If the value is NULL the key value pair is deleted.
  */
-APU_DECLARE(apr_status_t) apr_json_object_set(apr_json_value_t *obj,
+APU_DECLARE(apr_status_t) apr_json_object_set_ex(apr_json_value_t *obj,
         apr_json_value_t *key, apr_json_value_t *val,
-        apr_pool_t *pool) __attribute__((nonnull(1, 4)));
+        apr_pool_t *pool) __attribute__((nonnull(1, 2, 4)));
 
 /**
  * Look up the value associated with a key in a JSON object.

Modified: apr/apr-util/branches/1.7.x/jose/apr_jose_encode.c
URL: http://svn.apache.org/viewvc/apr/apr-util/branches/1.7.x/jose/apr_jose_encode.c?rev=1839843&r1=1839842&r2=1839843&view=diff
==============================================================================
--- apr/apr-util/branches/1.7.x/jose/apr_jose_encode.c (original)
+++ apr/apr-util/branches/1.7.x/jose/apr_jose_encode.c Sat Sep  1 20:11:10 2018
@@ -263,7 +263,7 @@ static apr_status_t apr_jose_encode_json
         apr_pool_t *p)
 {
 
-    apr_json_value_t *json, *key, *val;
+    apr_json_value_t *json;
     char *buf;
     const char *buf64;
     apr_size_t len;
@@ -300,10 +300,9 @@ static apr_status_t apr_jose_encode_json
 
             apr_brigade_cleanup(bb);
 
-            key = apr_json_string_create(p, "protected",
-                    APR_JSON_VALUE_STRING);
-            val = apr_json_string_create(p, buf, len);
-            apr_json_object_set(json, key, val, p);
+            apr_json_object_set(json, "protected",
+                    APR_JSON_VALUE_STRING,
+                    apr_json_string_create(p, buf, len), p);
 
         }
 
@@ -311,9 +310,8 @@ static apr_status_t apr_jose_encode_json
 
         if (e->unprotected) {
 
-            key = apr_json_string_create(p, "unprotected",
-                    APR_JSON_VALUE_STRING);
-            apr_json_object_set(json, key, e->unprotected, p);
+            apr_json_object_set(json, "unprotected",
+                    APR_JSON_VALUE_STRING, e->unprotected, p);
 
         }
 
@@ -341,9 +339,8 @@ static apr_status_t apr_jose_encode_json
 
             /* create header */
 
-            key = apr_json_string_create(p, "header",
-                    APR_JSON_VALUE_STRING);
-            apr_json_object_set(json, key, recip->header, p);
+            apr_json_object_set(json, "header",
+                    APR_JSON_VALUE_STRING, recip->header, p);
 
             apr_brigade_cleanup(bb);
 
@@ -353,10 +350,9 @@ static apr_status_t apr_jose_encode_json
                     recip->ekey.len,
                     APR_ENCODE_BASE64URL, &len64);
 
-            key = apr_json_string_create(p, "encrypted_key",
-                    APR_JSON_VALUE_STRING);
-            val = apr_json_string_create(p, buf64, len64);
-            apr_json_object_set(json, key, val, p);
+            apr_json_object_set(json, "encrypted_key",
+                    APR_JSON_VALUE_STRING,
+                    apr_json_string_create(p, buf64, len64), p);
 
         }
 
@@ -368,10 +364,9 @@ static apr_status_t apr_jose_encode_json
 
             /* create recipients element */
 
-            key = apr_json_string_create(p, "recipients",
-                    APR_JSON_VALUE_STRING);
             recips = apr_json_array_create(p, jwe->recipients->nelts);
-            apr_json_object_set(json, key, recips, p);
+            apr_json_object_set(json, "recipients",
+                    APR_JSON_VALUE_STRING, recips, p);
 
             /* populate each recipient */
 
@@ -405,9 +400,8 @@ static apr_status_t apr_jose_encode_json
 
                 /* create header */
 
-                key = apr_json_string_create(p, "header",
-                        APR_JSON_VALUE_STRING);
-                apr_json_object_set(r, key, recip->header, p);
+                apr_json_object_set(r, "header",
+                        APR_JSON_VALUE_STRING, recip->header, p);
 
                 apr_brigade_cleanup(bb);
 
@@ -417,10 +411,9 @@ static apr_status_t apr_jose_encode_json
                         recip->ekey.len,
                         APR_ENCODE_BASE64URL, &len64);
 
-                key = apr_json_string_create(p, "encrypted_key",
-                        APR_JSON_VALUE_STRING);
-                val = apr_json_string_create(p, buf64, len64);
-                apr_json_object_set(r, key, val, p);
+                apr_json_object_set(r, "encrypted_key",
+                        APR_JSON_VALUE_STRING,
+                        apr_json_string_create(p, buf64, len64), p);
 
             }
             if (APR_SUCCESS != status) {
@@ -436,9 +429,8 @@ static apr_status_t apr_jose_encode_json
             buf64 = apr_pencode_base64_binary(p, e->iv.data, e->iv.len,
                     APR_ENCODE_BASE64URL, &len64);
 
-            key = apr_json_string_create(p, "iv", APR_JSON_VALUE_STRING);
-            val = apr_json_string_create(p, buf64, len64);
-            apr_json_object_set(json, key, val, p);
+            apr_json_object_set(json, "iv", APR_JSON_VALUE_STRING,
+                    apr_json_string_create(p, buf64, len64), p);
         }
 
         /* create aad */
@@ -448,9 +440,8 @@ static apr_status_t apr_jose_encode_json
             buf64 = apr_pencode_base64_binary(p, e->aad.data, e->aad.len,
                     APR_ENCODE_BASE64URL, &len64);
 
-            key = apr_json_string_create(p, "aad", APR_JSON_VALUE_STRING);
-            val = apr_json_string_create(p, buf64, len64);
-            apr_json_object_set(json, key, val, p);
+            apr_json_object_set(json, "aad", APR_JSON_VALUE_STRING,
+                    apr_json_string_create(p, buf64, len64), p);
         }
 
         /* create ciphertext */
@@ -460,9 +451,8 @@ static apr_status_t apr_jose_encode_json
             buf64 = apr_pencode_base64_binary(p, e->cipher.data, e->cipher.len,
                     APR_ENCODE_BASE64URL, &len64);
 
-            key = apr_json_string_create(p, "ciphertext", APR_JSON_VALUE_STRING);
-            val = apr_json_string_create(p, buf64, len64);
-            apr_json_object_set(json, key, val, p);
+            apr_json_object_set(json, "ciphertext", APR_JSON_VALUE_STRING,
+                    apr_json_string_create(p, buf64, len64), p);
         }
 
         /* create tag */
@@ -472,9 +462,8 @@ static apr_status_t apr_jose_encode_json
             buf64 = apr_pencode_base64_binary(p, e->tag.data, e->tag.len,
                     APR_ENCODE_BASE64URL, &len64);
 
-            key = apr_json_string_create(p, "tag", APR_JSON_VALUE_STRING);
-            val = apr_json_string_create(p, buf64, len64);
-            apr_json_object_set(json, key, val, p);
+            apr_json_object_set(json, "tag", APR_JSON_VALUE_STRING,
+                    apr_json_string_create(p, buf64, len64), p);
         }
 
     }
@@ -493,7 +482,7 @@ static apr_status_t apr_jose_encode_json
         apr_brigade_flush flush, void *ctx, apr_jose_t *jose, apr_jose_cb_t *cb,
         apr_pool_t *p)
 {
-    apr_json_value_t *json, *key, *val;
+    apr_json_value_t *json;
     char *buf;
     const char *buf64;
     apr_size_t len;
@@ -530,9 +519,8 @@ static apr_status_t apr_jose_encode_json
 
     /* add the payload to our json */
 
-    key = apr_json_string_create(p, "payload", APR_JSON_VALUE_STRING);
-    val = apr_json_string_create(p, buf64, len64);
-    apr_json_object_set(json, key, val, p);
+    apr_json_object_set(json, "payload", APR_JSON_VALUE_STRING,
+            apr_json_string_create(p, buf64, len64), p);
 
     /* calculate the flattened signature */
 
@@ -551,9 +539,8 @@ static apr_status_t apr_jose_encode_json
             return status;
         }
 
-        key = apr_json_string_create(p, "protected", APR_JSON_VALUE_STRING);
-        val = apr_json_string_create(p, buf, len);
-        apr_json_object_set(json, key, val, p);
+        apr_json_object_set(json, "protected", APR_JSON_VALUE_STRING,
+                apr_json_string_create(p, buf, len), p);
 
         status = apr_brigade_write(bb, flush, ctx, ".", 1);
         if (APR_SUCCESS != status) {
@@ -576,8 +563,8 @@ static apr_status_t apr_jose_encode_json
 
         /* create header */
 
-        key = apr_json_string_create(p, "header", APR_JSON_VALUE_STRING);
-        apr_json_object_set(json, key, jws->signature->header, p);
+        apr_json_object_set(json, "header", APR_JSON_VALUE_STRING,
+                jws->signature->header, p);
 
         apr_brigade_cleanup(bb);
 
@@ -587,9 +574,8 @@ static apr_status_t apr_jose_encode_json
                 jws->signature->sig.data, jws->signature->sig.len,
                 APR_ENCODE_BASE64URL, &len64);
 
-        key = apr_json_string_create(p, "signature", APR_JSON_VALUE_STRING);
-        val = apr_json_string_create(p, buf64, len64);
-        apr_json_object_set(json, key, val, p);
+        apr_json_object_set(json, "signature", APR_JSON_VALUE_STRING,
+                apr_json_string_create(p, buf64, len64), p);
 
     }
 
@@ -602,9 +588,9 @@ static apr_status_t apr_jose_encode_json
 
         /* create signatures element */
 
-        key = apr_json_string_create(p, "signatures", APR_JSON_VALUE_STRING);
         sigs = apr_json_array_create(p, jws->signatures->nelts);
-        apr_json_object_set(json, key, sigs, p);
+        apr_json_object_set(json, "signatures", APR_JSON_VALUE_STRING,
+                sigs, p);
 
         /* populate each signature */
 
@@ -630,9 +616,8 @@ static apr_status_t apr_jose_encode_json
 
             /* add protected header to array */
 
-            key = apr_json_string_create(p, "protected", APR_JSON_VALUE_STRING);
-            val = apr_json_string_create(p, buf, len);
-            apr_json_object_set(s, key, val, p);
+            apr_json_object_set(s, "protected", APR_JSON_VALUE_STRING,
+                    apr_json_string_create(p, buf, len), p);
 
             status = apr_brigade_write(bb, flush, ctx, ".", 1);
             if (APR_SUCCESS != status) {
@@ -655,8 +640,8 @@ static apr_status_t apr_jose_encode_json
 
             /* create header */
 
-            key = apr_json_string_create(p, "header", APR_JSON_VALUE_STRING);
-            apr_json_object_set(s, key, sig->header, p);
+            apr_json_object_set(s, "header", APR_JSON_VALUE_STRING,
+                    sig->header, p);
 
             apr_brigade_cleanup(bb);
 
@@ -666,9 +651,8 @@ static apr_status_t apr_jose_encode_json
                     sig->sig.len,
                     APR_ENCODE_BASE64URL, &len64);
 
-            key = apr_json_string_create(p, "signature", APR_JSON_VALUE_STRING);
-            val = apr_json_string_create(p, buf64, len64);
-            apr_json_object_set(s, key, val, p);
+            apr_json_object_set(s, "signature", APR_JSON_VALUE_STRING,
+                    apr_json_string_create(p, buf64, len64), p);
 
         }
         if (APR_SUCCESS != status) {

Modified: apr/apr-util/branches/1.7.x/json/apr_json.c
URL: http://svn.apache.org/viewvc/apr/apr-util/branches/1.7.x/json/apr_json.c?rev=1839843&r1=1839842&r2=1839843&view=diff
==============================================================================
--- apr/apr-util/branches/1.7.x/json/apr_json.c (original)
+++ apr/apr-util/branches/1.7.x/json/apr_json.c Sat Sep  1 20:11:10 2018
@@ -129,13 +129,55 @@ apr_json_value_t *apr_json_null_create(a
     return json;
 }
 
-apr_status_t apr_json_object_set(apr_json_value_t *object, apr_json_value_t *key,
-        apr_json_value_t *val, apr_pool_t *pool)
+apr_status_t apr_json_object_set(apr_json_value_t *object,
+        const char *key, apr_ssize_t klen, apr_json_value_t *val,
+        apr_pool_t *pool)
 {
     apr_json_kv_t *kv;
     apr_hash_t *hash;
 
-    if (object->type != APR_JSON_OBJECT || !key
+    if (object->type != APR_JSON_OBJECT) {
+        return APR_EINVAL;
+    }
+
+    if (klen == APR_JSON_VALUE_STRING) {
+        klen = strlen(key);
+    }
+
+    hash = object->value.object->hash;
+
+    kv = apr_hash_get(hash, key, klen);
+
+    if (!val) {
+        if (kv) {
+            apr_hash_set(hash, key, klen, NULL);
+            APR_RING_REMOVE((kv), link);
+        }
+        return APR_SUCCESS;
+    }
+
+    if (!kv) {
+        kv = apr_palloc(pool, sizeof(apr_json_kv_t));
+        APR_RING_ELEM_INIT(kv, link);
+        APR_JSON_OBJECT_INSERT_TAIL(object->value.object, kv);
+        apr_hash_set(hash, key, klen,
+                kv);
+    }
+
+    kv->k = apr_json_string_create(pool, key, klen);
+    kv->v = val;
+
+    return APR_SUCCESS;
+}
+
+apr_status_t apr_json_object_set_ex(apr_json_value_t *object,
+        apr_json_value_t *key, apr_json_value_t *val,
+        apr_pool_t *pool)
+{
+    apr_json_kv_t *kv;
+    apr_hash_t *hash;
+
+    if (object->type != APR_JSON_OBJECT
             || key->type != APR_JSON_STRING) {
         return APR_EINVAL;
     }
@@ -316,7 +358,7 @@ apr_json_value_t *apr_json_overlay(apr_p
         if (!apr_hash_get(overlay->value.object->hash, kv->k->value.string.p,
                 kv->k->value.string.len)) {
 
-            apr_json_object_set(res, kv->k, kv->v, p);
+            apr_json_object_set_ex(res, kv->k, kv->v, p);
 
         }
         else if (APR_JSON_FLAGS_STRICT & flags) {
@@ -329,7 +371,7 @@ apr_json_value_t *apr_json_overlay(apr_p
          kv != APR_RING_SENTINEL(&(overlay->value.object)->list, apr_json_kv_t, link);
          kv = APR_RING_NEXT((kv), link)) {
 
-        apr_json_object_set(res, kv->k, kv->v, p);
+        apr_json_object_set_ex(res, kv->k, kv->v, p);
 
     }
 

Modified: apr/apr-util/branches/1.7.x/json/apr_json_decode.c
URL: http://svn.apache.org/viewvc/apr/apr-util/branches/1.7.x/json/apr_json_decode.c?rev=1839843&r1=1839842&r2=1839843&view=diff
==============================================================================
--- apr/apr-util/branches/1.7.x/json/apr_json_decode.c (original)
+++ apr/apr-util/branches/1.7.x/json/apr_json_decode.c Sat Sep  1 20:11:10 2018
@@ -501,7 +501,7 @@ static apr_status_t apr_json_decode_obje
         if ((status = apr_json_decode_value(self, &value)))
             goto out;
 
-        apr_json_object_set(json, key, value, self->pool);
+        apr_json_object_set_ex(json, key, value, self->pool);
 
         if (self->p == self->e) {
             status = APR_EOF;