split off pecl_http
authorMichael Wallner <mike@php.net>
Thu, 5 Mar 2015 06:51:15 +0000 (07:51 +0100)
committerMichael Wallner <mike@php.net>
Thu, 5 Mar 2015 06:53:24 +0000 (07:53 +0100)
.gitignore [new file with mode: 0644]
CREDITS [new file with mode: 0644]
LICENSE [new file with mode: 0644]
config.m4 [new file with mode: 0644]
config.w32 [new file with mode: 0644]
package.xml [new file with mode: 0644]
php_json_post.c [new file with mode: 0644]
php_json_post.h [new file with mode: 0644]
tests/001.phpt [new file with mode: 0644]
tests/002.phpt [new file with mode: 0644]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..811bba1
--- /dev/null
@@ -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 (file)
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 (file)
index 0000000..b49d8ed
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,22 @@
+Copyright (c) 2015, Michael Wallner <mike@iworks.at>.
+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 (file)
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 (file)
index 0000000..8dd5152
--- /dev/null
@@ -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 (file)
index 0000000..36e12d7
--- /dev/null
@@ -0,0 +1,71 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<package version="2.0" xmlns="http://pear.php.net/dtd/package-2.0"
+ xmlns:tasks="http://pear.php.net/dtd/tasks-1.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="
+  http://pear.php.net/dtd/tasks-1.0
+  http://pear.php.net/dtd/tasks-1.0.xsd
+  http://pear.php.net/dtd/package-2.0
+  http://pear.php.net/dtd/package-2.0.xsd">
+ <name>json_post</name>
+ <channel>pecl.php.net</channel>
+ <summary>JSON POST handler</summary>
+ <description><![CDATA[
+This extension provides a PHP content type handler for "text/json" to PHP's
+form data parser. If the `Content-Type` of an incoming request is `text/json`,
+the JSON contents of the request body will by parsed into `$_POST`.
+
+This extension does not provide any constants, functions or classes.
+]]></description>
+ <lead>
+  <name>Michael Wallner</name>
+  <user>mike</user>
+  <email>mike@php.net</email>
+  <active>yes</active>
+ </lead>
+ <date>2015-03-01</date>
+ <version>
+  <release>1.0.0</release>
+  <api>1.0.0</api>
+ </version>
+ <stability>
+  <release>stable</release>
+  <api>stable</api>
+ </stability>
+ <license>BSD, revised</license>
+ <notes><![CDATA[
+* Split off pecl_http
+]]></notes>
+ <contents>
+  <dir name="/">
+   <file role="doc" name="LICENSE"/>
+   <file role="doc" name="CREDITS"/>
+   <file role="src" name="config.m4"/>
+   <file role="src" name="config.w32"/>
+
+   <file role="src" name="php_json_post.h"/>
+   <file role="src" name="php_json_post.c"/>
+
+   <dir name="tests">
+     <file role="test" name="001.phpt"/>
+     <file role="test" name="002.phpt"/>
+   </dir>
+  </dir>
+ </contents>
+ <dependencies>
+  <required>
+   <php>
+    <min>5.3.0</min>
+   </php>
+   <pearinstaller>
+    <min>1.4.1</min>
+   </pearinstaller>
+   <extension>
+    <name>json</name>
+   </extension>
+  </required>
+ </dependencies>
+ <providesextension>json_post</providesextension>
+ <extsrcrelease/>
+ <changelog />
+</package>
diff --git a/php_json_post.c b/php_json_post.c
new file mode 100644 (file)
index 0000000..6bc8c33
--- /dev/null
@@ -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 <mike@php.net>                 |
+    +--------------------------------------------------------------------+
+*/
+
+
+#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 (file)
index 0000000..e797385
--- /dev/null
@@ -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 <mike@php.net>                 |
+    +--------------------------------------------------------------------+
+*/
+
+#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 (file)
index 0000000..b3e5775
--- /dev/null
@@ -0,0 +1,65 @@
+--TEST--
+json_post
+--SKIPIF--
+<?php
+extension_loaded("json_post")) or die("skip need json_post support\n");
+?>
+--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--
+<?php
+var_dump($_POST, json_last_error_msg());
+?>
+--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 (file)
index 0000000..643f560
--- /dev/null
@@ -0,0 +1,67 @@
+--TEST--
+json_post
+--SKIPIF--
+<?php
+extension_loaded("json_post")) or die("skip need json_post support\n");
+?>
+--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--
+<?php
+var_dump($_POST, json_last_error_msg());
+?>
+--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