From 3964b8f0cd83ef3ba5de9d0b2e5e395f95404a8c Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Thu, 5 Mar 2015 07:51:15 +0100 Subject: [PATCH] split off pecl_http --- .gitignore | 36 +++++++++++++ CREDITS | 2 + LICENSE | 22 ++++++++ config.m4 | 7 +++ config.w32 | 5 ++ package.xml | 71 +++++++++++++++++++++++++ php_json_post.c | 138 ++++++++++++++++++++++++++++++++++++++++++++++++ php_json_post.h | 54 +++++++++++++++++++ tests/001.phpt | 65 +++++++++++++++++++++++ tests/002.phpt | 67 +++++++++++++++++++++++ 10 files changed, 467 insertions(+) create mode 100644 .gitignore create mode 100644 CREDITS create mode 100644 LICENSE create mode 100644 config.m4 create mode 100644 config.w32 create mode 100644 package.xml create mode 100644 php_json_post.c create mode 100644 php_json_post.h create mode 100644 tests/001.phpt create mode 100644 tests/002.phpt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..811bba1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,36 @@ +.deps +*.lo +*.la +.libs +acinclude.m4 +aclocal.m4 +autom4te.cache +build +config.guess +config.h +config.h.in +config.log +config.nice +config.status +config.sub +configure +configure.in +include +install-sh +libtool +ltmain.sh +Makefile +Makefile.fragments +Makefile.global +Makefile.objects +missing +mkinstalldirs +modules +run-tests.php +tests/*/*.diff +tests/*/*.out +tests/*/*.php +tests/*/*.exp +tests/*/*.log +tests/*/*.sh +php_json_post.o diff --git a/CREDITS b/CREDITS new file mode 100644 index 0000000..aee6ece --- /dev/null +++ b/CREDITS @@ -0,0 +1,2 @@ +json_post +Michael Wallner \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..b49d8ed --- /dev/null +++ b/LICENSE @@ -0,0 +1,22 @@ +Copyright (c) 2015, Michael Wallner . +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/config.m4 b/config.m4 new file mode 100644 index 0000000..025155c --- /dev/null +++ b/config.m4 @@ -0,0 +1,7 @@ +PHP_ARG_ENABLE(json-post, whether to enable json-post support, +[ --enable-json-post Enable json-post support]) + +if test "$PHP_JSON_POST" != "no"; then + PHP_NEW_EXTENSION(json_post, php_json_post.c, $ext_shared) + PHP_ADD_EXTENSION_DEP(json_post, json, true) +fi diff --git a/config.w32 b/config.w32 new file mode 100644 index 0000000..8dd5152 --- /dev/null +++ b/config.w32 @@ -0,0 +1,5 @@ +ARG_ENABLE("json-post", "enable json_post support", "no"); + +if (PHP_JSON_POST != "no") { + EXTENSION("json_post", "php_json_post.c"); +} diff --git a/package.xml b/package.xml new file mode 100644 index 0000000..36e12d7 --- /dev/null +++ b/package.xml @@ -0,0 +1,71 @@ + + + json_post + pecl.php.net + JSON POST handler + + + Michael Wallner + mike + mike@php.net + yes + + 2015-03-01 + + 1.0.0 + 1.0.0 + + + stable + stable + + BSD, revised + + + + + + + + + + + + + + + + + + + + + 5.3.0 + + + 1.4.1 + + + json + + + + json_post + + + diff --git a/php_json_post.c b/php_json_post.c new file mode 100644 index 0000000..6bc8c33 --- /dev/null +++ b/php_json_post.c @@ -0,0 +1,138 @@ +/* + +--------------------------------------------------------------------+ + | PECL :: json_post | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted provided that the conditions mentioned | + | in the accompanying LICENSE file are met. | + +--------------------------------------------------------------------+ + | Copyright (c) 2015, Michael Wallner | + +--------------------------------------------------------------------+ +*/ + + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#include "php.h" +#include "php_ini.h" +#include "ext/standard/info.h" +#include "ext/json/php_json.h" +#include "php_json_post.h" +#include "SAPI.h" + +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) +PHP_INI_END() + +static void php_json_post_init_globals(zend_json_post_globals *json_post_globals) +{ + json_post_globals->flags = PHP_JSON_OBJECT_AS_ARRAY; +} + +PHP_MINFO_FUNCTION(json_post) +{ + php_info_print_table_start(); + php_info_print_table_header(2, "json_post support", "enabled"); + php_info_print_table_end(); + + DISPLAY_INI_ENTRIES(); +} + +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 >= 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); + } +#else + json_str = SG(request_info).raw_post_data; + json_len = SG(request_info).raw_post_data_length; +#endif + + if (json_len) { + zval zjson; + + INIT_ZVAL(zjson); + 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)); + } + } +#if PHP_VERSION_ID >= 50600 + if (json_str) { + efree(json_str); + } +#endif +} + +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; + + entry.content_type = "text/json"; + entry.content_type_len = sizeof("text/json")-1; + sapi_register_post_entry(&entry TSRMLS_CC); + + ZEND_INIT_MODULE_GLOBALS(json_post, php_json_post_init_globals, NULL); + REGISTER_INI_ENTRIES(); + return SUCCESS; +} + +PHP_MSHUTDOWN_FUNCTION(json_post) +{ + UNREGISTER_INI_ENTRIES(); + + return SUCCESS; +} + +zend_function_entry json_post_functions[] = { + {0} +}; + +static zend_module_dep json_post_module_deps[] = { + ZEND_MOD_REQUIRED("json") + {NULL, NULL, NULL, 0} +}; + +zend_module_entry json_post_module_entry = { + STANDARD_MODULE_HEADER_EX, + NULL, + json_post_module_deps, + "json_post", + json_post_functions, + PHP_MINIT(json_post), + PHP_MSHUTDOWN(json_post), + NULL, + NULL, + PHP_MINFO(json_post), + PHP_JSON_POST_VERSION, + STANDARD_MODULE_PROPERTIES +}; +/* }}} */ + +#ifdef COMPILE_DL_JSON_POST +ZEND_GET_MODULE(json_post) +#endif + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim600: noet sw=4 ts=4 fdm=marker + * vim<600: noet sw=4 ts=4 + */ diff --git a/php_json_post.h b/php_json_post.h new file mode 100644 index 0000000..e797385 --- /dev/null +++ b/php_json_post.h @@ -0,0 +1,54 @@ +/* + +--------------------------------------------------------------------+ + | PECL :: json_post | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted provided that the conditions mentioned | + | in the accompanying LICENSE file are met. | + +--------------------------------------------------------------------+ + | Copyright (c) 2015, Michael Wallner | + +--------------------------------------------------------------------+ +*/ + +#ifndef PHP_JSON_POST_H +#define PHP_JSON_POST_H + +extern zend_module_entry json_post_module_entry; +#define phpext_json_post_ptr &json_post_module_entry + +#define PHP_JSON_POST_VERSION "1.0.0" + +#ifdef PHP_WIN32 +# define PHP_JSON_POST_API __declspec(dllexport) +#elif defined(__GNUC__) && __GNUC__ >= 4 +# define PHP_JSON_POST_API extern __attribute__ ((visibility("default"))) +#else +# define PHP_JSON_POST_API extern +#endif + +#ifdef ZTS +# include "TSRM.h" +#endif + +ZEND_BEGIN_MODULE_GLOBALS(json_post) + long flags; +ZEND_END_MODULE_GLOBALS(json_post) + +ZEND_EXTERN_MODULE_GLOBALS(json_post); + +#ifdef ZTS +# define JSON_POST_G(v) TSRMG(json_post_globals_id, zend_json_post_globals *, v) +#else +# define JSON_POST_G(v) (json_post_globals.v) +#endif + +#endif /* PHP_JSON_POST_H */ + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim600: noet sw=4 ts=4 fdm=marker + * vim<600: noet sw=4 ts=4 + */ diff --git a/tests/001.phpt b/tests/001.phpt new file mode 100644 index 0000000..b3e5775 --- /dev/null +++ b/tests/001.phpt @@ -0,0 +1,65 @@ +--TEST-- +json_post +--SKIPIF-- + +--POST_RAW-- +Content-type: text/json + +{ + "null": null, + "bool": true, + "int": 123, + "bigint": 36893488147419103232, + "float": 123.123, + "string": "Hello World", + "array": [1,2,3], + "object": { + "array": [1,2,3], + "unicode": "\u00D6sterreich" + } +} +--FILE-- + +--EXPECTF-- +array(8) { + ["null"]=> + NULL + ["bool"]=> + bool(true) + ["int"]=> + int(123) + ["bigint"]=> + float(3.689%dE+19) + ["float"]=> + float(123.123) + ["string"]=> + string(11) "Hello World" + ["array"]=> + array(3) { + [0]=> + int(1) + [1]=> + int(2) + [2]=> + int(3) + } + ["object"]=> + array(2) { + ["array"]=> + array(3) { + [0]=> + int(1) + [1]=> + int(2) + [2]=> + int(3) + } + ["unicode"]=> + string(11) "Österreich" + } +} +string(8) "No error" \ No newline at end of file diff --git a/tests/002.phpt b/tests/002.phpt new file mode 100644 index 0000000..643f560 --- /dev/null +++ b/tests/002.phpt @@ -0,0 +1,67 @@ +--TEST-- +json_post +--SKIPIF-- + +--INI-- +json_post.flags=2;JSON_BIGINT_AS_STRING +--POST_RAW-- +Content-type: text/json + +{ + "null": null, + "bool": true, + "int": 123, + "bigint": 36893488147419103232, + "float": 123.123, + "string": "Hello World", + "array": [1,2,3], + "object": { + "array": [1,2,3], + "unicode": "\u00D6sterreich" + } +} +--FILE-- + +--EXPECTF-- +object(stdClass)#%d (8) { + ["null"]=> + NULL + ["bool"]=> + bool(true) + ["int"]=> + int(123) + ["bigint"]=> + string(20) "36893488147419103232" + ["float"]=> + float(123.123) + ["string"]=> + string(11) "Hello World" + ["array"]=> + array(3) { + [0]=> + int(1) + [1]=> + int(2) + [2]=> + int(3) + } + ["object"]=> + object(stdClass)#%d (2) { + ["array"]=> + array(3) { + [0]=> + int(1) + [1]=> + int(2) + [2]=> + int(3) + } + ["unicode"]=> + string(11) "Österreich" + } +} +string(8) "No error" \ No newline at end of file -- 2.30.2