add json content type handler if ext/json is present
authorMichael Wallner <mike@php.net>
Wed, 9 May 2012 12:30:04 +0000 (12:30 +0000)
committerMichael Wallner <mike@php.net>
Wed, 9 May 2012 12:30:04 +0000 (12:30 +0000)
config9.m4
php_http.c
php_http_api.h
php_http_client_factory.c
php_http_env.c
tests/factory.phpt

index 9f800a1ef5491dfe354b209a247dc43954883cb7..63062de8e5d048a045c584fad338e7bd3d8fd4b6 100644 (file)
@@ -383,6 +383,12 @@ dnl ----
                fi
        ])
 
+dnl ----
+dnl JSON
+dnl ----
+       HTTP_HAVE_PHP_EXT([json])
+
+
 dnl ----
 dnl ICONV
 dnl ----
@@ -436,6 +442,7 @@ dnl ----
        dnl shared extension deps
        HTTP_SHARED_DEP([hash])
        HTTP_SHARED_DEP([iconv])
+       HTTP_SHARED_DEP([json])
        
        PHP_SUBST([HTTP_SHARED_LIBADD])
 
index e3ee6ab69aced0bf96635c70eece58b780ba41a2..bba04c7b948bbb9f1f7385595a27b8707d7f682e 100644 (file)
@@ -51,6 +51,9 @@ static zend_module_dep http_module_deps[] = {
 #ifdef PHP_HTTP_HAVE_ICONV
        ZEND_MOD_REQUIRED("iconv")
 #endif
+#ifdef PHP_HTTP_HAVE_JSON
+       ZEND_MOD_REQUIRED("json")
+#endif
 #ifdef PHP_HTTP_HAVE_EVENT
        ZEND_MOD_CONFLICTS("event")
 #endif
index 2334404f31f7b1aea20caf43b67ffc1ba3b2491e..72ebf4dc49ebd8cf9113dd71b08bc08d25c7e39a 100644 (file)
@@ -50,6 +50,10 @@ typedef int STATUS;
 #      define PHP_HTTP_HAVE_HASH
 #endif
 
+#if (defined(HAVE_JSON) || defined(PHP_HTTP_HAVE_EXT_JSON)) && (PHP_HTTP_SHARED_DEPS || !defined(COMPILE_DL_JSON))
+#      define PHP_HTTP_HAVE_JSON
+#endif
+
 #ifdef PHP_WIN32
 #      define CURL_STATICLIB
 #      define PHP_HTTP_HAVE_NETDB
index a7052115b8ef6205310a043f544de87c3918d25e..1b14f481dac82a2592f3ed056b808ed33a8e8cc6 100644 (file)
@@ -174,10 +174,10 @@ PHP_METHOD(HttpClientFactory, createClient)
 PHP_METHOD(HttpClientFactory, createPool)
 {
        int argc = 0;
-       zval ***argv;
+       zval ***argv = NULL;
 
        with_error_handling(EH_THROW, php_http_exception_get_class_entry()) {
-               if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|*", &argv, &argc)) {
+               if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "*", &argv, &argc)) {
                        with_error_handling(EH_THROW, php_http_exception_get_class_entry()) {
                                int i;
                                zval *zdriver;
@@ -238,7 +238,7 @@ PHP_METHOD(HttpClientFactory, createDataShare)
        zval ***argv;
 
        with_error_handling(EH_THROW, php_http_exception_get_class_entry()) {
-               if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|*", &argv, &argc)) {
+               if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "*", &argv, &argc)) {
                        with_error_handling(EH_THROW, php_http_exception_get_class_entry()) {
                                int i;
                                zval *zdriver;
index 5df1e5bb8edb4ae29d626d8724e2826bcfc8fa79..09ee36c96366689378fe9889f61086a2653ad796 100644 (file)
@@ -917,8 +917,35 @@ PHP_METHOD(HttpEnv, cleanPersistentHandles)
        }
 }
 
+#ifdef PHP_HTTP_HAVE_JSON
+#include "ext/json/php_json.h"
+
+static SAPI_POST_HANDLER_FUNC(php_http_json_post_handler)
+{
+       if (SG(request_info).raw_post_data) {
+               php_json_decode_ex(arg, SG(request_info).raw_post_data, SG(request_info).raw_post_data_length, PHP_JSON_OBJECT_AS_ARRAY, PG(max_input_nesting_level) TSRMLS_CC);
+       }
+}
+
+#endif
+
 PHP_MINIT_FUNCTION(http_env)
 {
+#ifdef PHP_HTTP_HAVE_JSON
+       sapi_post_entry entry = {NULL, 0, NULL, NULL};
+
+       entry.post_reader = sapi_read_standard_form_data;
+       entry.post_handler = php_http_json_post_handler;
+
+       entry.content_type = "text/json";
+       entry.content_type_len = lenof("text/json");
+       sapi_register_post_entry(&entry TSRMLS_CC);
+
+       entry.content_type = "application/json";
+       entry.content_type_len = lenof("application/json");
+       sapi_register_post_entry(&entry TSRMLS_CC);
+#endif
+
        PHP_HTTP_REGISTER_CLASS(http, Env, http_env, NULL, 0);
 
        return SUCCESS;
index 4267e3650308c63fa0e5623aa97ee5e6630f7a56..6a629768102506962d894628880f0862f436d554 100644 (file)
@@ -28,8 +28,12 @@ $r = $f->createClient();
 $p = $f->createPool();
 $s = $f->createDataShare();
 
+$r->setRequest(new http\Client\Request("GET", "http://localhost/"));
+$x = $f->createPool($r);
+$y = $f->createDatashare($r);
+
 var_dump(
-       array_map("get_class", array($f,$r,$p,$s)), 
+       array_map("get_class", array($f,$r,$p,$s,$x,$y)), 
        $f->getDriver()
 );
 
@@ -45,7 +49,7 @@ echo "Done\n";
 ?>
 --EXPECTF--
 Test
-array(4) {
+array(6) {
   [0]=>
   string(9) "MyFactory"
   [1]=>
@@ -54,6 +58,10 @@ array(4) {
   string(6) "MyPool"
   [3]=>
   string(7) "MyShare"
+  [4]=>
+  string(6) "MyPool"
+  [5]=>
+  string(7) "MyShare"
 }
 string(4) "curl"
 clients are not supported by this driver