From: Michael Wallner Date: Fri, 17 Sep 2021 09:41:11 +0000 (+0200) Subject: JSON_POST_ERROR, json_post.onerror.* INI, warning X-Git-Tag: v1.1.0~5 X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-json_post;a=commitdiff_plain;h=129e8b459061f9f4c76e3ee8a6baaef68c0bba41 JSON_POST_ERROR, json_post.onerror.* INI, warning --- diff --git a/package.xml b/package.xml index 185aff9..ddc8986 100644 --- a/package.xml +++ b/package.xml @@ -36,10 +36,12 @@ This extension does not provide any constants, functions or classes. BSD-2-Clause diff --git a/php_json_post.c b/php_json_post.c index 772018d..94e0e36 100644 --- a/php_json_post.c +++ b/php_json_post.c @@ -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(); } diff --git a/php_json_post.h b/php_json_post.h index 76cb8fe..2895b70 100644 --- a/php_json_post.h +++ b/php_json_post.h @@ -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 index 0000000..bc957d7 --- /dev/null +++ b/tests/error.inc @@ -0,0 +1,11 @@ + +DONE diff --git a/tests/error001.phpt b/tests/error001.phpt index 1a43334..ea4d193 100644 --- a/tests/error001.phpt +++ b/tests/error001.phpt @@ -1,28 +1,13 @@ --TEST-- -json_post with malformed JSON (https://github.com/m6w6/ext-json_post/issues/3) ---SKIPIF-- - ---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-- - -Done +{"a +--FILE_EXTERNAL-- +error.inc --EXPECTHEADERS-- -Status: 400 Bad Request -X-JSON-Error-Code: 3 ---EXPECTF-- -array(0) { -} -int(0) -Done +--EXPECT-- +DONE diff --git a/tests/error002.phpt b/tests/error002.phpt index 8ebb2c0..c4d3511 100644 --- a/tests/error002.phpt +++ b/tests/error002.phpt @@ -1,30 +1,16 @@ --TEST-- -json_post with malformed JSON (https://github.com/m6w6/ext-json_post/issues/3) ---SKIPIF-- - +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-- - -Done +{"a +--FILE_EXTERNAL-- +error.inc --EXPECTHEADERS-- Status: 400 Bad Request -X-JSON-Error-Code: 3 ---EXPECTF-- -array(0) { -} -int(400) -Done +--EXPECT-- +DONE diff --git a/tests/error003.phpt b/tests/error003.phpt index 9147a12..93bcc9b 100644 --- a/tests/error003.phpt +++ b/tests/error003.phpt @@ -1,25 +1,16 @@ --TEST-- -json_post with malformed JSON (https://github.com/m6w6/ext-json_post/issues/3) ---SKIPIF-- - +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-- - -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 diff --git a/tests/error004.phpt b/tests/error004.phpt index 42cfa34..00fb76b 100644 --- a/tests/error004.phpt +++ b/tests/error004.phpt @@ -1,22 +1,14 @@ --TEST-- -json_post with malformed JSON (https://github.com/m6w6/ext-json_post/issues/3) ---SKIPIF-- - +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-- - -Done +{"a +--FILE_EXTERNAL-- +error.inc --EXPECTHEADERS-- --EXPECT-- diff --git a/tests/error005.phpt b/tests/error005.phpt index aeea867..97c4988 100644 --- a/tests/error005.phpt +++ b/tests/error005.phpt @@ -1,31 +1,24 @@ --TEST-- -json_post with malformed JSON (https://github.com/m6w6/ext-json_post/issues/3) ---SKIPIF-- - +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-- -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 index 0000000..aef634a --- /dev/null +++ b/tests/error006.phpt @@ -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