JSON_POST_ERROR, json_post.onerror.* INI, warning
authorMichael Wallner <mike@php.net>
Fri, 17 Sep 2021 09:41:11 +0000 (11:41 +0200)
committerMichael Wallner <mike@php.net>
Fri, 17 Sep 2021 09:41:11 +0000 (11:41 +0200)
package.xml
php_json_post.c
php_json_post.h
tests/error.inc [new file with mode: 0644]
tests/error001.phpt
tests/error002.phpt
tests/error003.phpt
tests/error004.phpt
tests/error005.phpt
tests/error006.phpt [new file with mode: 0644]

index 185aff9a18351bf29a33b27952057662610f3c04..ddc8986a58bb5d0c5f853d2bb9fa4262045e764a 100644 (file)
@@ -36,10 +36,12 @@ This extension does not provide any constants, functions or classes.
  <license uri="http://copyfree.org/content/standard/licenses/2bsd/license.txt">BSD-2-Clause</license>
  <notes><![CDATA[
 * Fix gh-issue #3:
-    * Add json_post.error_response INI entry, specifying whether and which
+    * Add json_post.onerror.response INI entry, specifying whether and which
       response code to send when `json_decode` fails.
-    * Add json_post.error_exit INI entry, specifying whether to exit PHP
+    * Add json_post.onerror.exit INI entry, specifying whether to exit PHP
       without running the script when `json_decode` fails.
+    * Add json_post.onerror.warning INI entry, specifying whether to raise
+                       a WARNING when `json_decode` fails.
 ]]></notes>
  <contents>
   <dir name="/">
index 772018dc540dce78297109b5168b2e1f3220282e..94e0e36eb9cc996811df4560dcb39d5232861c95 100644 (file)
@@ -26,12 +26,14 @@ ZEND_DECLARE_MODULE_GLOBALS(json_post);
 
 PHP_INI_BEGIN()
        STD_PHP_INI_ENTRY("json_post.flags", "1", PHP_INI_PERDIR, OnUpdateLong, flags, zend_json_post_globals, json_post_globals)
-       STD_PHP_INI_ENTRY("json_post.error_response", "0", PHP_INI_PERDIR, OnUpdateLong, error_response, zend_json_post_globals, json_post_globals)
-       STD_PHP_INI_ENTRY("json_post.error_exit", "0", PHP_INI_PERDIR, OnUpdateBool, error_exit, zend_json_post_globals, json_post_globals)
+       STD_PHP_INI_ENTRY("json_post.onerror.response", "0", PHP_INI_PERDIR, OnUpdateLong, onerror.response, zend_json_post_globals, json_post_globals)
+       STD_PHP_INI_ENTRY("json_post.onerror.exit", "0", PHP_INI_PERDIR, OnUpdateBool, onerror.exit, zend_json_post_globals, json_post_globals)
+       STD_PHP_INI_ENTRY("json_post.onerror.warning", "0", PHP_INI_PERDIR, OnUpdateBool, onerror.warning, zend_json_post_globals, json_post_globals)
 PHP_INI_END()
 
 static void php_json_post_init_globals(zend_json_post_globals *json_post_globals)
 {
+       memset(json_post_globals, 0, sizeof(*json_post_globals));
 #if PHP_VERSION_ID >= 50400
        json_post_globals->flags = PHP_JSON_OBJECT_AS_ARRAY;
 #else
@@ -41,11 +43,6 @@ static void php_json_post_init_globals(zend_json_post_globals *json_post_globals
 
 #if PHP_VERSION_ID < 70000
 ZEND_EXTERN_MODULE_GLOBALS(json);
-static inline void zend_print_long_to_buf(char *p, long l) {
-       do {
-               *--p = (char) (l % 10) + '0';
-       } while (l /= 10);
-}
 #endif
 
 #ifndef TSRMLS_CC
@@ -65,6 +62,8 @@ PHP_MINFO_FUNCTION(json_post)
 
 static SAPI_POST_HANDLER_FUNC(php_json_post_handler)
 {
+       int module_number = 0;
+
 #if PHP_VERSION_ID >= 70000
        zend_string *json = NULL;
 
@@ -150,14 +149,16 @@ static SAPI_POST_HANDLER_FUNC(php_json_post_handler)
 #      endif
 #endif
 
+       REGISTER_LONG_CONSTANT("JSON_POST_ERROR", JSON_G(error_code), CONST_CS);
+
        if (JSON_G(error_code)) {
-               if (JSON_POST_G(error_response)) {
-                       char header[] = "X-JSON-Error-Code:   ";
-                       zend_print_long_to_buf(header + sizeof(header) - 1, (JSON_G(error_code) & 0xff));
-                       sapi_header_op(SAPI_HEADER_SET_STATUS, (void *) (long) JSON_POST_G(error_response) TSRMLS_CC);
-                       sapi_add_header(header, sizeof(header)-1, 1);
+               if (JSON_POST_G(onerror.response)) {
+                       sapi_header_op(SAPI_HEADER_SET_STATUS, (void *) (zend_long) JSON_POST_G(onerror.response) TSRMLS_CC);
+               }
+               if (JSON_POST_G(onerror.warning)) {
+                       zend_error(E_WARNING, "json_post: json_decode failed with error code: %d", JSON_G(error_code));
                }
-               if (JSON_POST_G(error_exit)) {
+               if (JSON_POST_G(onerror.exit)) {
                        sapi_send_headers(TSRMLS_C);
                        zend_bailout();
                }
index 76cb8fe95506fe9489cb95592ab88bed398274ee..2895b70256978f6ce3d1fde52a8578e5a991776b 100644 (file)
@@ -30,10 +30,17 @@ extern zend_module_entry json_post_module_entry;
 #      include "TSRM.h"
 #endif
 
+#if PHP_VERSION_ID < 70000
+typedef long zend_long;
+#endif
+
 ZEND_BEGIN_MODULE_GLOBALS(json_post)
-       long flags;
-       int error_response;
-       zend_bool error_exit;
+       zend_long flags;
+       struct {
+               zend_long response;
+               zend_bool warning;
+               zend_bool exit;
+       } onerror;
 ZEND_END_MODULE_GLOBALS(json_post)
 
 ZEND_EXTERN_MODULE_GLOBALS(json_post);
diff --git a/tests/error.inc b/tests/error.inc
new file mode 100644 (file)
index 0000000..bc957d7
--- /dev/null
@@ -0,0 +1,11 @@
+<?php
+if ($_POST)
+       var_dump(compact("_POST"));
+if (($json_last_error = json_last_error()))
+       var_dump(compact("json_last_error"));
+if (($JSON_POST_ERROR = JSON_POST_ERROR) !== 3)
+       var_dump(compact("JSON_POST_ERROR"));
+if (($http_response_code = http_response_code()) != ini_get("json_post.onerror.response") && ini_get("json_post.onerror.response"))
+       var_dump(compact("http_response_code"));
+?>
+DONE
index 1a43334304b264b607d029d18ae86528b5c828dc..ea4d193f870910c9c54b1ace9215dbf5c92c5fee 100644 (file)
@@ -1,28 +1,13 @@
 --TEST--
-json_post with malformed JSON (https://github.com/m6w6/ext-json_post/issues/3)
---SKIPIF--
-<?php
-extension_loaded("json_post") or die("skip need json_post support\n");
-?>
---INI--
-json_post.error_response = 400
+json_post with malformed JSON [default] (https://github.com/m6w6/ext-json_post/issues/3)
+--EXTENSIONS--
+json_post
 --POST_RAW--
 Content-Type: application/json
 
-{
-       "greeting": "Hello World
-}
---FILE--
-<?php
-var_dump($_POST);
-var_dump(json_last_error());
-?>
-Done
+{"a
+--FILE_EXTERNAL--
+error.inc
 --EXPECTHEADERS--
-Status: 400 Bad Request
-X-JSON-Error-Code: 3
---EXPECTF--
-array(0) {
-}
-int(0)
-Done
+--EXPECT--
+DONE
index 8ebb2c00aa7ee3567f26e5f32e940728879ec4c8..c4d35110b32c7899d72e3f251dd849131ad37739 100644 (file)
@@ -1,30 +1,16 @@
 --TEST--
-json_post with malformed JSON (https://github.com/m6w6/ext-json_post/issues/3)
---SKIPIF--
-<?php
-extension_loaded("json_post") or die("skip need json_post support\n");
-if (PHP_VERSION_ID < 70000) die("skip need PHP-7.0+\n");
-?>
+json_post with malformed JSON [response] (https://github.com/m6w6/ext-json_post/issues/3)
+--EXTENSIONS--
+json_post
 --INI--
-json_post.error_response = 400
-json_post.flags = 4194305
+json_post.onerror.response = 400
 --POST_RAW--
 Content-Type: application/json
 
-{
-       "greeting": "Hello World
-}
---FILE--
-<?php
-var_dump($_POST);
-var_dump(http_response_code());
-?>
-Done
+{"a
+--FILE_EXTERNAL--
+error.inc
 --EXPECTHEADERS--
 Status: 400 Bad Request
-X-JSON-Error-Code: 3
---EXPECTF--
-array(0) {
-}
-int(400)
-Done
+--EXPECT--
+DONE
index 9147a12855e619399ef56e05ac6f61c31a8e7a80..93bcc9ba6bc05653a7b56ca58cbeb31e71deb180 100644 (file)
@@ -1,25 +1,16 @@
 --TEST--
-json_post with malformed JSON (https://github.com/m6w6/ext-json_post/issues/3)
---SKIPIF--
-<?php
-extension_loaded("json_post") or die("skip need json_post support\n");
-?>
+json_post with malformed JSON [warning] (https://github.com/m6w6/ext-json_post/issues/3)
+--EXTENSIONS--
+json_post
 --INI--
-json_post.error_response = 444
-json_post.error_exit = true
+json_post.onerror.warning = on
 --POST_RAW--
 Content-Type: application/json
 
-{
-       "greeting": "Hello World
-}
---FILE--
-<?php
-var_dump($_POST);
-var_dump(http_response_code());
-?>
-Done
+{"a
+--FILE_EXTERNAL--
+error.inc
 --EXPECTHEADERS--
-Status: 444
-X-JSON-Error-Code: 3
---EXPECT--
+--EXPECTF--
+Warning: json_post: json_decode failed with error code: 3 in %s on line %s
+DONE
index 42cfa3408e24f21960e161de5ce182bf8b1d508e..00fb76b9113db79ee2acf603373da946164f5a5d 100644 (file)
@@ -1,22 +1,14 @@
 --TEST--
-json_post with malformed JSON (https://github.com/m6w6/ext-json_post/issues/3)
---SKIPIF--
-<?php
-extension_loaded("json_post") or die("skip need json_post support\n");
-?>
+json_post with malformed JSON [exit] (https://github.com/m6w6/ext-json_post/issues/3)
+--EXTENSIONS--
+json_post
 --INI--
-json_post.error_exit = true
+json_post.onerror.exit = true
 --POST_RAW--
 Content-Type: application/json
 
-{
-       "greeting": "Hello World
-}
---FILE--
-<?php
-var_dump($_POST);
-var_dump(http_response_code());
-?>
-Done
+{"a
+--FILE_EXTERNAL--
+error.inc
 --EXPECTHEADERS--
 --EXPECT--
index aeea867dea0cce62de19717ddeb90871ad7a644f..97c4988d0601a280dc808b9a4511f769f7a39bce 100644 (file)
@@ -1,31 +1,24 @@
 --TEST--
-json_post with malformed JSON (https://github.com/m6w6/ext-json_post/issues/3)
---SKIPIF--
-<?php
-extension_loaded("json_post") or die("skip need json_post support\n");
-if (PHP_VERSION_ID < 70000) die("skip need PHP-7.0+\n");
-?>
+json_post with malformed JSON [response override] (https://github.com/m6w6/ext-json_post/issues/3)
+--EXTENSIONS--
+json_post
 --INI--
-json_post.error_response = 444
-json_post.flags = 1
+json_post.onerror.response = 444
 --POST_RAW--
 Content-Type: application/json
 
-{
-       "greeting": "Hello World
-}
+{"a
 --FILE--
 <?php
-http_response_code(400);
-var_dump($_POST);
-var_dump(http_response_code());
+if (JSON_POST_ERROR)
+       http_response_code(400);
+include "./error.inc";
 ?>
-Done
 --EXPECTHEADERS--
 Status: 400 Bad Request
-X-JSON-Error-Code: 3
---EXPECTF--
-array(0) {
+--EXPECT--
+array(1) {
+  ["http_response_code"]=>
+  int(400)
 }
-int(400)
-Done
+DONE
diff --git a/tests/error006.phpt b/tests/error006.phpt
new file mode 100644 (file)
index 0000000..aef634a
--- /dev/null
@@ -0,0 +1,15 @@
+--TEST--
+json_post with malformed JSON [json_throw] (https://github.com/m6w6/ext-json_post/issues/3)
+--EXTENSIONS--
+json_post
+--INI--
+json_post.flags = 4194305
+--POST_RAW--
+Content-Type: application/json
+
+{"a
+--FILE_EXTERNAL--
+error.inc
+--EXPECTHEADERS--
+--EXPECT--
+DONE