From 0327131cf87a5be32585fea1291178b60a359cbf Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Fri, 14 Dec 2012 10:32:14 +0000 Subject: [PATCH] add http\Header::getParams() --- php_http_headers.c | 145 +++++++++++++++++++++++++---------------- php_http_headers.h | 1 + phpunit/HeaderTest.php | 23 +++++++ 3 files changed, 112 insertions(+), 57 deletions(-) diff --git a/php_http_headers.c b/php_http_headers.c index 933952c..77981b6 100644 --- a/php_http_headers.c +++ b/php_http_headers.c @@ -16,13 +16,28 @@ PHP_HTTP_API STATUS php_http_headers_parse(const char *header, size_t length, Ha { php_http_header_parser_t ctx; php_http_buffer_t buf; + php_http_header_parser_state_t rs; - php_http_buffer_from_string_ex(&buf, header, length); - php_http_header_parser_init(&ctx TSRMLS_CC); - php_http_header_parser_parse(&ctx, &buf, PHP_HTTP_HEADER_PARSER_CLEANUP, headers, callback_func, callback_data); + if (!php_http_buffer_from_string_ex(&buf, header, length)) { + php_http_error(HE_WARNING, PHP_HTTP_E_RUNTIME, "Could not allocate buffer"); + return FAILURE; + } + + if (!php_http_header_parser_init(&ctx TSRMLS_CC)) { + php_http_buffer_dtor(&buf); + php_http_error(HE_WARNING, PHP_HTTP_E_HEADER, "Could not initialize header parser"); + return FAILURE; + } + + rs = php_http_header_parser_parse(&ctx, &buf, PHP_HTTP_HEADER_PARSER_CLEANUP, headers, callback_func, callback_data); php_http_header_parser_dtor(&ctx); php_http_buffer_dtor(&buf); - /* FIXME */ + + if (rs == PHP_HTTP_HEADER_PARSER_STATE_FAILURE) { + php_http_error(HE_WARNING, PHP_HTTP_E_MALFORMED_HEADERS, "Could not parse headers"); + return FAILURE; + } + return SUCCESS; } @@ -55,6 +70,13 @@ PHP_HTTP_BEGIN_ARGS(parse, 1) PHP_HTTP_ARG_VAL(flags, 0) PHP_HTTP_END_ARGS; +PHP_HTTP_BEGIN_ARGS(getParams, 0) + PHP_HTTP_ARG_VAL(param_sep, 0) + PHP_HTTP_ARG_VAL(arg_sep, 0) + PHP_HTTP_ARG_VAL(val_sep, 0) + PHP_HTTP_ARG_VAL(flags, 0) +PHP_HTTP_END_ARGS; + static zend_class_entry *php_http_header_class_entry; zend_class_entry *php_http_header_get_class_entry(void) @@ -70,6 +92,7 @@ static zend_function_entry php_http_header_method_entry[] = { PHP_HTTP_HEADER_ME(unserialize, ZEND_ACC_PUBLIC) PHP_HTTP_HEADER_ME(match, ZEND_ACC_PUBLIC) PHP_HTTP_HEADER_ME(negotiate, ZEND_ACC_PUBLIC) + PHP_HTTP_HEADER_ME(getParams, ZEND_ACC_PUBLIC) PHP_HTTP_HEADER_ME(parse, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) EMPTY_FUNCTION_ENTRY }; @@ -203,6 +226,31 @@ PHP_METHOD(HttpHeader, negotiate) } } +PHP_METHOD(HttpHeader, getParams) +{ + zval zctor, *zparams_obj, **zargs = NULL; + + INIT_PZVAL(&zctor); + ZVAL_STRINGL(&zctor, "__construct", lenof("__construct"), 0); + + MAKE_STD_ZVAL(zparams_obj); + object_init_ex(zparams_obj, php_http_params_get_class_entry()); + + zargs = (zval **) ecalloc(ZEND_NUM_ARGS()+1, sizeof(zval *)); + zargs[0] = zend_read_property(Z_OBJCE_P(getThis()), getThis(), ZEND_STRL("value"), 0 TSRMLS_CC); + if (ZEND_NUM_ARGS()) { + zend_get_parameters_array(ZEND_NUM_ARGS(), ZEND_NUM_ARGS(), &zargs[1]); + } + + if (SUCCESS == call_user_function(NULL, &zparams_obj, &zctor, return_value, ZEND_NUM_ARGS()+1, zargs TSRMLS_CC)) { + RETVAL_ZVAL(zparams_obj, 0, 1); + } + + if (zargs) { + efree(zargs); + } +} + PHP_METHOD(HttpHeader, parse) { char *header_str; @@ -210,63 +258,46 @@ PHP_METHOD(HttpHeader, parse) zend_class_entry *ce = NULL; if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|C", &header_str, &header_len, &ce)) { - php_http_header_parser_t *parser = php_http_header_parser_init(NULL TSRMLS_CC); - php_http_buffer_t *buf = php_http_buffer_from_string(header_str, header_len); - - if (parser && buf) { - php_http_header_parser_state_t rs; - - array_init(return_value); - - rs = php_http_header_parser_parse(parser, buf, - PHP_HTTP_HEADER_PARSER_CLEANUP, Z_ARRVAL_P(return_value), NULL, NULL); - - if (rs == PHP_HTTP_HEADER_PARSER_STATE_FAILURE) { - php_http_error(HE_WARNING, PHP_HTTP_E_MALFORMED_HEADERS, "Could not parse headers"); - zval_dtor(return_value); - RETVAL_NULL(); - } else { - if (ce && instanceof_function(ce, php_http_header_class_entry TSRMLS_CC)) { - HashPosition pos; - php_http_array_hashkey_t key = php_http_array_hashkey_init(0); - zval **val; - - FOREACH_KEYVAL(pos, return_value, key, val) { - zval *zho, *zkey, *zvalue; - - Z_ADDREF_PP(val); - zvalue = *val; - - MAKE_STD_ZVAL(zkey); - if (key.type == HASH_KEY_IS_LONG) { - ZVAL_LONG(zkey, key.num); - } else { - ZVAL_STRINGL(zkey, key.str, key.len - 1, 1); - } - - MAKE_STD_ZVAL(zho); - object_init_ex(zho, ce); - zend_call_method_with_2_params(&zho, ce, NULL, "__construct", NULL, zkey, zvalue); - - if (key.type == HASH_KEY_IS_LONG) { - zend_hash_index_update(Z_ARRVAL_P(return_value), key.num, (void *) &zho, sizeof(zval *), NULL); - } else { - zend_hash_update(Z_ARRVAL_P(return_value), key.str, key.len, (void *) &zho, sizeof(zval *), NULL); - } - - zval_ptr_dtor(&zvalue); - zval_ptr_dtor(&zkey); + array_init(return_value); + + if (SUCCESS != php_http_headers_parse(header_str, header_len, Z_ARRVAL_P(return_value), NULL, NULL TSRMLS_CC)) { + php_http_error(HE_WARNING, PHP_HTTP_E_MALFORMED_HEADERS, "Could not parse headers"); + zval_dtor(return_value); + RETVAL_NULL(); + } else { + if (ce && instanceof_function(ce, php_http_header_class_entry TSRMLS_CC)) { + HashPosition pos; + php_http_array_hashkey_t key = php_http_array_hashkey_init(0); + zval **val; + + FOREACH_KEYVAL(pos, return_value, key, val) { + zval *zho, *zkey, *zvalue; + + Z_ADDREF_PP(val); + zvalue = *val; + + MAKE_STD_ZVAL(zkey); + if (key.type == HASH_KEY_IS_LONG) { + ZVAL_LONG(zkey, key.num); + } else { + ZVAL_STRINGL(zkey, key.str, key.len - 1, 1); } + + MAKE_STD_ZVAL(zho); + object_init_ex(zho, ce); + zend_call_method_with_2_params(&zho, ce, NULL, "__construct", NULL, zkey, zvalue); + + if (key.type == HASH_KEY_IS_LONG) { + zend_hash_index_update(Z_ARRVAL_P(return_value), key.num, (void *) &zho, sizeof(zval *), NULL); + } else { + zend_hash_update(Z_ARRVAL_P(return_value), key.str, key.len, (void *) &zho, sizeof(zval *), NULL); + } + + zval_ptr_dtor(&zvalue); + zval_ptr_dtor(&zkey); } } } - - if (parser) { - php_http_header_parser_free(&parser); - } - if (buf) { - php_http_buffer_free(&buf); - } } } diff --git a/php_http_headers.h b/php_http_headers.h index 8709574..238efb6 100644 --- a/php_http_headers.h +++ b/php_http_headers.h @@ -24,6 +24,7 @@ PHP_METHOD(HttpHeader, serialize); PHP_METHOD(HttpHeader, unserialize); PHP_METHOD(HttpHeader, match); PHP_METHOD(HttpHeader, negotiate); +PHP_METHOD(HttpHeader, getParams); PHP_METHOD(HttpHeader, parse); PHP_MINIT_FUNCTION(http_header); diff --git a/phpunit/HeaderTest.php b/phpunit/HeaderTest.php index b34adcb..c04a7f7 100644 --- a/phpunit/HeaderTest.php +++ b/phpunit/HeaderTest.php @@ -57,4 +57,27 @@ class HeaderTest extends PHPUnit_Framework_TestCase { $this->setExpectedException("PHPUnit_Framework_Error_Warning", "Could not parse headers"); $this->assertFalse(http\Header::parse($header)); } + + function testParams() { + $header = new http\Header("Cache-control", "public, must-revalidate, max-age=0"); + $this->assertEquals( + array( + "public" => array("value" => true, "arguments" => array()), + "must-revalidate" => array("value" => true, "arguments" => array()), + "max-age" => array("value" => 0, "arguments" => array()), + ), + $header->getParams()->params + ); + } + + function testParamsWithArgs() { + $header = new http\Header("Custom", '"foo" is "bar". "bar" is "bis" where "bis" is 3.'); + $this->assertEquals( + array( + "foo" => array("value" => "bar", "arguments" => array()), + "bar" => array("value" => "baz", "arguments" => array("baz" => "3")) + ), + $header->getParams(".", "where", "is")->params + ); + } } -- 2.30.2