From c0d96fe2c0d156412bcb22bf5b9f5e9ed0046c9c Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Wed, 9 May 2012 12:30:04 +0000 Subject: [PATCH] add json content type handler if ext/json is present --- config9.m4 | 7 +++++++ php_http.c | 3 +++ php_http_api.h | 4 ++++ php_http_client_factory.c | 6 +++--- php_http_env.c | 27 +++++++++++++++++++++++++++ tests/factory.phpt | 12 ++++++++++-- 6 files changed, 54 insertions(+), 5 deletions(-) diff --git a/config9.m4 b/config9.m4 index 9f800a1..63062de 100644 --- a/config9.m4 +++ b/config9.m4 @@ -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]) diff --git a/php_http.c b/php_http.c index e3ee6ab..bba04c7 100644 --- a/php_http.c +++ b/php_http.c @@ -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 diff --git a/php_http_api.h b/php_http_api.h index 2334404..72ebf4d 100644 --- a/php_http_api.h +++ b/php_http_api.h @@ -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 diff --git a/php_http_client_factory.c b/php_http_client_factory.c index a705211..1b14f48 100644 --- a/php_http_client_factory.c +++ b/php_http_client_factory.c @@ -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; diff --git a/php_http_env.c b/php_http_env.c index 5df1e5b..09ee36c 100644 --- a/php_http_env.c +++ b/php_http_env.c @@ -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; diff --git a/tests/factory.phpt b/tests/factory.phpt index 4267e36..6a62976 100644 --- a/tests/factory.phpt +++ b/tests/factory.phpt @@ -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 -- 2.30.2