<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="/">
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
#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
static SAPI_POST_HANDLER_FUNC(php_json_post_handler)
{
+ int module_number = 0;
+
#if PHP_VERSION_ID >= 70000
zend_string *json = NULL;
# 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();
}
# 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);
--- /dev/null
+<?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
--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
--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
--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
--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--
--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
--- /dev/null
+--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