reset json's error code
[m6w6/ext-json_post] / php_json_post.c
index ae7e3a6c0efc00850095499f393867e1df20d136..fd7f972f15fd3a58935b0f1beb91539c3d86e870 100644 (file)
@@ -26,6 +26,8 @@ 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)
 PHP_INI_END()
 
 static void php_json_post_init_globals(zend_json_post_globals *json_post_globals)
@@ -37,6 +39,21 @@ static void php_json_post_init_globals(zend_json_post_globals *json_post_globals
 #endif
 }
 
+#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
+#      define TSRMLS_C
+#      define TSRMLS_CC
+#endif
+
+
 PHP_MINFO_FUNCTION(json_post)
 {
        php_info_print_table_start();
@@ -48,56 +65,116 @@ PHP_MINFO_FUNCTION(json_post)
 
 static SAPI_POST_HANDLER_FUNC(php_json_post_handler)
 {
-       zval *zarg = arg;
-       char *json_str = NULL;
-       size_t json_len = 0;
+#if PHP_VERSION_ID >= 70000
+       zend_string *json = NULL;
 
-#if PHP_VERSION_ID >= 50600
        if (SG(request_info).request_body) {
                /* FG(stream_wrappers) not initialized yet, so we cannot use php://input */
                php_stream_rewind(SG(request_info).request_body);
-               json_len = php_stream_copy_to_mem(SG(request_info).request_body, &json_str, PHP_STREAM_COPY_ALL, 0);
+               json = php_stream_copy_to_mem(SG(request_info).request_body, PHP_STREAM_COPY_ALL, 0);
        }
+
+       if (json) {
+               if (json->len) {
+                       zval tmp;
+                       long flags = JSON_POST_G(flags);
+
+#ifdef PHP_JSON_THROW_ON_ERROR
+                       /* there's no execute data, so we must ensure json_decode() is not throwing */
+                       flags &= ~PHP_JSON_THROW_ON_ERROR;
+#endif
+
+                       ZVAL_NULL(&tmp);
+                       php_json_decode_ex(&tmp, json->val, json->len, flags, PG(max_input_nesting_level));
+
+                       switch (Z_TYPE(tmp)) {
+                       case IS_OBJECT:
+                       case IS_ARRAY:
+                               if (zend_hash_num_elements(HASH_OF(&tmp))) {
+                                       zval_dtor(arg);
+                                       ZVAL_COPY_VALUE(&PG(http_globals)[TRACK_VARS_POST], &tmp);
+                               } else {
+                                       /* PHP-7.4 optimizes empty array */
+                                       zval_ptr_dtor(&tmp);
+                               }
+                               break;
+                       default:
+                               break;
+                       }
+               }
+               zend_string_release(json);
+       }
+
 #else
+
+       zval *zarg = arg;
+       char *json_str = NULL;
+       size_t json_len = 0;
+
+#      if PHP_VERSION_ID >= 50600
+       if (!SG(request_info).request_body) {
+               return;
+       }
+
+       /* FG(stream_wrappers) not initialized yet, so we cannot use php://input */
+       php_stream_rewind(SG(request_info).request_body);
+       json_len = php_stream_copy_to_mem(SG(request_info).request_body, &json_str, PHP_STREAM_COPY_ALL, 0);
+#      else
        json_str = SG(request_info).raw_post_data;
        json_len = SG(request_info).raw_post_data_length;
-#endif
+#      endif
 
        if (json_len) {
                zval zjson;
 
                INIT_ZVAL(zjson);
-#if PHP_VERSION_ID >= 50400
+
+#      if PHP_VERSION_ID >= 50400
                php_json_decode_ex(&zjson, json_str, json_len, JSON_POST_G(flags), PG(max_input_nesting_level) TSRMLS_CC);
                if (Z_TYPE(zjson) != IS_NULL) {
                        zval_dtor(zarg);
                        ZVAL_COPY_VALUE(zarg, (&zjson));
-#else
+#      else
                php_json_decode(&zjson, json_str, json_len, (zend_bool)(JSON_POST_G(flags)&1), PG(max_input_nesting_level) TSRMLS_CC);
                if (Z_TYPE(zjson) != IS_NULL) {
                        zval_dtor(zarg);
                        zarg->value = zjson.value;
                        Z_TYPE_P(zarg) = Z_TYPE(zjson);
-#endif
+#      endif
                }
        }
-#if PHP_VERSION_ID >= 50600
+#      if PHP_VERSION_ID >= 50600
        if (json_str) {
                efree(json_str);
        }
+#      endif
 #endif
+
+       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(error_exit)) {
+                       sapi_send_headers(TSRMLS_C);
+                       zend_bailout();
+               }
+               /* ext/json in PHP-7 fails to reset error_code in RINIT */
+               JSON_G(error_code) = 0;
+       }
 }
 
 PHP_MINIT_FUNCTION(json_post)
 {
-       sapi_post_entry entry = {NULL, 0, NULL, NULL};
-
-       entry.post_reader = sapi_read_standard_form_data;
-       entry.post_handler = php_json_post_handler;
+       sapi_post_entry json_post_entries[] = {
+               { "text/json", sizeof("text/json")-1, sapi_read_standard_form_data, php_json_post_handler },
+               { "application/json", sizeof("application/json")-1, sapi_read_standard_form_data, php_json_post_handler },
+               { NULL, 0, NULL, NULL }
+       };
 
-       entry.content_type = "text/json";
-       entry.content_type_len = sizeof("text/json")-1;
-       sapi_register_post_entry(&entry TSRMLS_CC);
+       sapi_register_post_entries(json_post_entries TSRMLS_CC);
 
        ZEND_INIT_MODULE_GLOBALS(json_post, php_json_post_init_globals, NULL);
        REGISTER_INI_ENTRIES();