From 64722314d70dc547e477d87927c22b598aa463d0 Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Wed, 7 Nov 2012 08:52:35 +0000 Subject: [PATCH] - add http\Env::getResponseStatusForAllCodes() - merge message headers --- config9.m4 | 2 +- php_http_env.c | 22 +++++++ php_http_env.h | 1 + php_http_message.c | 116 +++++++++++++++++++----------------- php_http_strlist.c | 3 +- tests/envresponsecodes.phpt | 26 ++++++++ tests/message005.phpt | 5 +- tests/propertyproxy001.phpt | 4 +- 8 files changed, 116 insertions(+), 63 deletions(-) create mode 100644 tests/envresponsecodes.phpt diff --git a/config9.m4 b/config9.m4 index d4e211e..f9f92c6 100644 --- a/config9.m4 +++ b/config9.m4 @@ -341,7 +341,7 @@ dnl ---- EVENT_VER="`$EGREP _EVENT_VERSION $EVENT_DIR/include/event2/event.h | $AWK '{print $3}'`" AC_DEFINE([PHP_HTTP_HAVE_EVENT2], [1], [ ]) else - AC_DEFFINE([PHP_HTTP_HAVE_EVENT2], [0], [ ]) + AC_DEFINE([PHP_HTTP_HAVE_EVENT2], [0], [ ]) if test -f "$EVENT_DIR/include/evhttp.h" && test -f "$EVENT_DIR/include/evdns.h"; then if test -f "$EVENT_DIR/include/evrpc.h"; then EVENT_VER="1.4 or greater" diff --git a/php_http_env.c b/php_http_env.c index f73be20..3ca996f 100644 --- a/php_http_env.c +++ b/php_http_env.c @@ -663,6 +663,8 @@ PHP_HTTP_BEGIN_ARGS(getResponseStatusForCode, 1) PHP_HTTP_ARG_VAL(code, 0) PHP_HTTP_END_ARGS; +PHP_HTTP_EMPTY_ARGS(getResponseStatusForAllCodes); + PHP_HTTP_BEGIN_ARGS(getResponseHeader, 0) PHP_HTTP_ARG_VAL(header_name, 0) PHP_HTTP_END_ARGS; @@ -726,6 +728,7 @@ static zend_function_entry php_http_env_method_entry[] = { PHP_HTTP_ENV_ME(getRequestBody) PHP_HTTP_ENV_ME(getResponseStatusForCode) + PHP_HTTP_ENV_ME(getResponseStatusForAllCodes) PHP_HTTP_ENV_ME(getResponseHeader) PHP_HTTP_ENV_ME(getResponseCode) @@ -793,6 +796,25 @@ PHP_METHOD(HttpEnv, getResponseStatusForCode) RETURN_FALSE; } +PHP_METHOD(HttpEnv, getResponseStatusForAllCodes) +{ + const char *s; + unsigned c; + php_http_strlist_iterator_t i; + + if (SUCCESS != zend_parse_parameters_none()) { + RETURN_FALSE; + } + + array_init(return_value); + for ( php_http_strlist_iterator_init(&i, php_http_env_response_status, 100); + *(s = php_http_strlist_iterator_this(&i, &c)); + php_http_strlist_iterator_next(&i) + ) { + add_index_string(return_value, c, s, 1); + } +} + PHP_METHOD(HttpEnv, getResponseHeader) { char *header_name_str = NULL; diff --git a/php_http_env.h b/php_http_env.h index 4326054..5bd3fb9 100644 --- a/php_http_env.h +++ b/php_http_env.h @@ -75,6 +75,7 @@ zend_class_entry *php_http_env_get_class_entry(void); PHP_METHOD(HttpEnv, getRequestHeader); PHP_METHOD(HttpEnv, getRequestBody); PHP_METHOD(HttpEnv, getResponseStatusForCode); +PHP_METHOD(HttpEnv, getResponseStatusForAllCodes); PHP_METHOD(HttpEnv, getResponseHeader); PHP_METHOD(HttpEnv, getResponseCode); PHP_METHOD(HttpEnv, setResponseHeader); diff --git a/php_http_message.c b/php_http_message.c index 04e8706..a93d9f5 100644 --- a/php_http_message.c +++ b/php_http_message.c @@ -12,6 +12,9 @@ #include "php_http_api.h" +static zval *message_header_strval(zval **header TSRMLS_DC); +static void message_headers(php_http_message_t *msg, php_http_buffer_t *str); + PHP_HTTP_API zend_bool php_http_message_info_callback(php_http_message_t **message, HashTable **headers, php_http_info_t *info TSRMLS_DC) { php_http_message_t *old = *message; @@ -147,21 +150,9 @@ PHP_HTTP_API zval *php_http_message_header(php_http_message_t *msg, char *key_st if (SUCCESS == zend_symtable_find(&msg->hdrs, key, key_len + 1, (void *) &header)) { if (join && Z_TYPE_PP(header) == IS_ARRAY) { - zval *header_str, **val; - HashPosition pos; - php_http_buffer_t str; TSRMLS_FETCH_FROM_CTX(msg->ts); - php_http_buffer_init(&str); - MAKE_STD_ZVAL(header_str); - FOREACH_VAL(pos, *header, val) { - zval *strval = php_http_ztyp(IS_STRING, *val); - php_http_buffer_appendf(&str, PHP_HTTP_BUFFER_LEN(&str) ? ", %s":"%s", Z_STRVAL_P(strval)); - zval_ptr_dtor(&strval); - } - php_http_buffer_fix(&str); - ZVAL_STRINGL(header_str, PHP_HTTP_BUFFER_VAL(&str), PHP_HTTP_BUFFER_LEN(&str), 0); - ret = header_str; + ret = message_header_strval(header TSRMLS_CC); } else { Z_ADDREF_PP(header); ret = *header; @@ -307,7 +298,37 @@ PHP_HTTP_API void php_http_message_update_headers(php_http_message_t *msg) } } } -static inline void message_headers(php_http_message_t *msg, php_http_buffer_t *str) + +static zval *message_header_strval(zval **header TSRMLS_DC) +{ + zval *ret; + + if (Z_TYPE_PP(header) == IS_BOOL) { + MAKE_STD_ZVAL(ret); + ZVAL_STRING(ret, Z_BVAL_PP(header) ? "true" : "false", 1); + } else if (Z_TYPE_PP(header) == IS_ARRAY) { + zval **val; + HashPosition pos; + php_http_buffer_t str; + + php_http_buffer_init(&str); + MAKE_STD_ZVAL(ret); + FOREACH_VAL(pos, *header, val) { + zval *strval = message_header_strval(val TSRMLS_CC); + + php_http_buffer_appendf(&str, PHP_HTTP_BUFFER_LEN(&str) ? ", %s":"%s", Z_STRVAL_P(strval)); + zval_ptr_dtor(&strval); + } + php_http_buffer_fix(&str); + ZVAL_STRINGL(ret, PHP_HTTP_BUFFER_VAL(&str), PHP_HTTP_BUFFER_LEN(&str), 0); + } else { + ret = php_http_zsep(1, IS_STRING, *header); + } + + return ret; +} + +static void message_headers(php_http_message_t *msg, php_http_buffer_t *str) { php_http_array_hashkey_t key = php_http_array_hashkey_init(0); HashPosition pos1; @@ -331,48 +352,35 @@ static inline void message_headers(php_http_message_t *msg, php_http_buffer_t *s FOREACH_HASH_KEYVAL(pos1, &msg->hdrs, key, header) { if (key.type == HASH_KEY_IS_STRING) { - HashPosition pos2; - zval **single_header; - - switch (Z_TYPE_PP(header)) { - case IS_BOOL: - php_http_buffer_appendf(str, "%s: %s" PHP_HTTP_CRLF, key.str, Z_BVAL_PP(header)?"true":"false"); - break; - - case IS_LONG: - php_http_buffer_appendf(str, "%s: %ld" PHP_HTTP_CRLF, key.str, Z_LVAL_PP(header)); - break; - - case IS_DOUBLE: - php_http_buffer_appendf(str, "%s: %F" PHP_HTTP_CRLF, key.str, Z_DVAL_PP(header)); - break; - - case IS_STRING: - if (Z_STRVAL_PP(header)[Z_STRLEN_PP(header)-1] == '\r') fprintf(stderr, "DOH!\n"); - php_http_buffer_appendf(str, "%s: %s" PHP_HTTP_CRLF, key.str, Z_STRVAL_PP(header)); - break; - - case IS_ARRAY: - FOREACH_VAL(pos2, *header, single_header) { - switch (Z_TYPE_PP(single_header)) { - case IS_BOOL: - php_http_buffer_appendf(str, "%s: %s" PHP_HTTP_CRLF, key.str, Z_BVAL_PP(single_header)?"true":"false"); - break; - - case IS_LONG: - php_http_buffer_appendf(str, "%s: %ld" PHP_HTTP_CRLF, key.str, Z_LVAL_PP(single_header)); - break; - - case IS_DOUBLE: - php_http_buffer_appendf(str, "%s: %F" PHP_HTTP_CRLF, key.str, Z_DVAL_PP(single_header)); - break; - - case IS_STRING: - php_http_buffer_appendf(str, "%s: %s" PHP_HTTP_CRLF, key.str, Z_STRVAL_PP(single_header)); - break; + if (key.len == sizeof("Set-Cookie") && !strcasecmp(key.str, "Set-Cookie") && Z_TYPE_PP(header) == IS_ARRAY) { + HashPosition pos2; + zval **single_header; + + FOREACH_VAL(pos2, *header, single_header) { + if (Z_TYPE_PP(single_header) == IS_ARRAY) { + php_http_cookie_list_t *cookie = php_http_cookie_list_from_struct(NULL, *single_header TSRMLS_CC); + + if (cookie) { + char *buf; + size_t len; + + php_http_cookie_list_to_string(cookie, &buf, &len); + php_http_buffer_appendf(str, "Set-Cookie: %s" PHP_HTTP_CRLF, buf); + php_http_cookie_list_free(&cookie); + efree(buf); } + } else { + zval *strval = message_header_strval(single_header TSRMLS_CC); + + php_http_buffer_appendf(str, "Set-Cookie: %s" PHP_HTTP_CRLF, Z_STRVAL_P(strval)); + zval_ptr_dtor(&strval); } - break; + } + } else { + zval *strval = message_header_strval(header TSRMLS_CC); + + php_http_buffer_appendf(str, "%s: %s" PHP_HTTP_CRLF, key.str, Z_STRVAL_P(strval)); + zval_ptr_dtor(&strval); } } } diff --git a/php_http_strlist.c b/php_http_strlist.c index 35acf40..3a52f3b 100644 --- a/php_http_strlist.c +++ b/php_http_strlist.c @@ -28,7 +28,7 @@ PHP_HTTP_API php_http_strlist_iterator_t *php_http_strlist_iterator_init(php_htt PHP_HTTP_API const char *php_http_strlist_iterator_this(php_http_strlist_iterator_t *iter, unsigned *id) { if (id) { - *id = iter->major * iter->factor + iter->minor; + *id = (iter->major + 1) * iter->factor + iter->minor; } return iter->p; @@ -46,6 +46,7 @@ PHP_HTTP_API const char *php_http_strlist_iterator_next(php_http_strlist_iterato if (!*iter->p) { ++iter->p; ++iter->major; + iter->minor = 0; } } diff --git a/tests/envresponsecodes.phpt b/tests/envresponsecodes.phpt new file mode 100644 index 0000000..21d7bbf --- /dev/null +++ b/tests/envresponsecodes.phpt @@ -0,0 +1,26 @@ +--TEST-- +http\Env response codes +--SKIPIF-- + +--FILE-- + +Done +--EXPECT-- +Test +array(0) { +} +Done diff --git a/tests/message005.phpt b/tests/message005.phpt index 25e6243..4cf3e23 100644 --- a/tests/message005.phpt +++ b/tests/message005.phpt @@ -27,8 +27,5 @@ String: foobar === UNKNOWN / HTTP/1.1 -Numbers: 1 -Numbers: 2 -Numbers: 3 -Numbers: 4.5%d +Numbers: 1, 2, 3, 4.5 DONE diff --git a/tests/propertyproxy001.phpt b/tests/propertyproxy001.phpt index 3f9d70a..da17577 100644 --- a/tests/propertyproxy001.phpt +++ b/tests/propertyproxy001.phpt @@ -86,9 +86,7 @@ bykey: 1 by1ref: 2 by2ref: 1 byXref: 2 -bynext: 1 -bynext: 2 -bynext: 3 +bynext: 1, 2, 3 DONE -- 2.30.2