From: Michael Wallner Date: Fri, 25 Feb 2005 10:20:22 +0000 (+0000) Subject: * moved defines into php_http_std_defs.h X-Git-Tag: RELEASE_0_6_0~12 X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-http;a=commitdiff_plain;h=2811a2111f519ee55e05c4084903a34dc0c3b818 * moved defines into php_http_std_defs.h * added http_urlencode_hash_ex() * added CREDITS file * integrated http_build_query()/php_url_encode_hash_ex() into std source files --- diff --git a/CREDITS b/CREDITS new file mode 100644 index 0000000..d1002cf --- /dev/null +++ b/CREDITS @@ -0,0 +1,5 @@ +CREDITS +======= +Michael Wallner, +Sara Golemon, php_url_encode_hash(), http_build_query() +Daniel Stenberg, , et al. http_parse_date() diff --git a/http.c b/http.c index 03ac2df..fdeb84f 100644 --- a/http.c +++ b/http.c @@ -35,6 +35,7 @@ #include "php_http.h" #include "php_http_api.h" #include "php_http_curl_api.h" +#include "php_http_std_defs.h" #ifdef ZEND_ENGINE_2 # include "ext/standard/php_http.h" diff --git a/http_api.c b/http_api.c index 5840eda..b08a51a 100644 --- a/http_api.c +++ b/http_api.c @@ -39,12 +39,11 @@ #ifdef ZEND_ENGINE_2 # include "ext/standard/php_http.h" -#else - #include "http_build_query.c" #endif #include "php_http.h" #include "php_http_api.h" +#include "php_http_std_defs.h" ZEND_DECLARE_MODULE_GLOBALS(http) @@ -128,7 +127,6 @@ static int check_day(char *day, size_t len); static int check_month(char *month); static int check_tzone(char *tzone); - static int http_ob_stack_get(php_ob_buffer *, php_ob_buffer **); /* {{{ static int http_sort_q(const void *, const void *) */ @@ -167,10 +165,10 @@ static STATUS _http_send_chunk(const void *data, const size_t begin, if (php_stream_seek(s, begin, SEEK_SET)) { return FAILURE; } - buf = (char *) ecalloc(1, HTTP_BUF_SIZE); + buf = (char *) ecalloc(1, HTTP_SENDBUF_SIZE); /* read into buf and write out */ - while ((len -= HTTP_BUF_SIZE) >= 0) { - if (!(read = php_stream_read(s, buf, HTTP_BUF_SIZE))) { + while ((len -= HTTP_SENDBUF_SIZE) >= 0) { + if (!(read = php_stream_read(s, buf, HTTP_SENDBUF_SIZE))) { efree(buf); return FAILURE; } @@ -182,7 +180,7 @@ static STATUS _http_send_chunk(const void *data, const size_t begin, /* read & write left over */ if (len) { - if (read = php_stream_read(s, buf, HTTP_BUF_SIZE + len)) { + if (read = php_stream_read(s, buf, HTTP_SENDBUF_SIZE + len)) { if (read - php_body_write(buf, read TSRMLS_CC)) { efree(buf); return FAILURE; @@ -483,8 +481,8 @@ PHP_HTTP_API time_t _http_parse_date(const char *date) } /* }}} */ -/* {{{ inline char *http_etag(void *, size_t, http_send_mode) */ -PHP_HTTP_API inline char *_http_etag(const void *data_ptr, const size_t data_len, +/* {{{ char *http_etag(void *, size_t, http_send_mode) */ +PHP_HTTP_API char *_http_etag(const void *data_ptr, const size_t data_len, const http_send_mode data_mode TSRMLS_DC) { char ssb_buf[128] = {0}; @@ -527,8 +525,8 @@ PHP_HTTP_API inline char *_http_etag(const void *data_ptr, const size_t data_len } /* }}} */ -/* {{{ inline http_lmod(void *, http_send_mode) */ -PHP_HTTP_API inline time_t _http_lmod(const void *data_ptr, const http_send_mode data_mode TSRMLS_DC) +/* {{{ time_t http_lmod(void *, http_send_mode) */ +PHP_HTTP_API time_t _http_lmod(const void *data_ptr, const http_send_mode data_mode TSRMLS_DC) { switch (data_mode) { @@ -560,39 +558,39 @@ PHP_HTTP_API inline time_t _http_lmod(const void *data_ptr, const http_send_mode } /* }}} */ -/* {{{inline int http_is_range_request(void) */ -PHP_HTTP_API inline int _http_is_range_request(TSRMLS_D) +/* {{{ int http_is_range_request(void) */ +PHP_HTTP_API int _http_is_range_request(TSRMLS_D) { return zend_hash_exists(Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_SERVER]), "HTTP_RANGE", sizeof("HTTP_RANGE")); } /* }}} */ -/* {{{ inline STATUS http_send_status(int) */ -PHP_HTTP_API inline STATUS _http_send_status(const int status TSRMLS_DC) +/* {{{ STATUS http_send_status(int) */ +PHP_HTTP_API STATUS _http_send_status(const int status TSRMLS_DC) { int s = status; return sapi_header_op(SAPI_HEADER_SET_STATUS, (void *) s TSRMLS_CC); } /* }}} */ -/* {{{ inline STATUS http_send_header(char *) */ -PHP_HTTP_API inline STATUS _http_send_header(const char *header TSRMLS_DC) +/* {{{ STATUS http_send_header(char *) */ +PHP_HTTP_API STATUS _http_send_header(const char *header TSRMLS_DC) { return http_send_status_header(0, header); } /* }}} */ -/* {{{ inline STATUS http_send_status_header(int, char *) */ -PHP_HTTP_API inline STATUS _http_send_status_header(const int status, const char *header TSRMLS_DC) +/* {{{ STATUS http_send_status_header(int, char *) */ +PHP_HTTP_API STATUS _http_send_status_header(const int status, const char *header TSRMLS_DC) { sapi_header_line h = {(char *) header, strlen(header), status}; return sapi_header_op(SAPI_HEADER_REPLACE, &h TSRMLS_CC); } /* }}} */ -/* {{{ inline zval *http_get_server_var(char *) */ -PHP_HTTP_API inline zval *_http_get_server_var(const char *key TSRMLS_DC) +/* {{{ zval *http_get_server_var(char *) */ +PHP_HTTP_API zval *_http_get_server_var(const char *key TSRMLS_DC) { zval **var; if (SUCCESS == zend_hash_find( @@ -995,10 +993,7 @@ PHP_HTTP_API char *_http_negotiate_q(const char *entry, const zval *supported, } /* walk through the supported array */ - for ( zend_hash_internal_pointer_reset(Z_ARRVAL_P(supported)); - SUCCESS == zend_hash_get_current_data( - Z_ARRVAL_P(supported), (void **) &zsupp); - zend_hash_move_forward(Z_ARRVAL_P(supported))) { + FOREACH_VAL(supported, zsupp) { if (!strcasecmp(Z_STRVAL_PP(zsupp), Z_STRVAL_PP(zentry))) { add_assoc_double(zentries, Z_STRVAL_PP(zsupp), qual); break; @@ -1520,12 +1515,11 @@ PHP_HTTP_API STATUS _http_parse_headers(char *header, int header_len, zval *arra /* {{{ void http_get_request_headers(zval *) */ PHP_HTTP_API void _http_get_request_headers(zval *array TSRMLS_DC) { - char *key; + char *key = NULL; + long idx = 0; - for ( zend_hash_internal_pointer_reset(HTTP_SERVER_VARS); - zend_hash_get_current_key(HTTP_SERVER_VARS, &key, NULL, 0) != HASH_KEY_NON_EXISTANT; - zend_hash_move_forward(HTTP_SERVER_VARS)) { - if (!strncmp(key, "HTTP_", 5)) { + FOREACH_HASH_KEY(HTTP_SERVER_VARS, key, idx) { + if (key && !strncmp(key, "HTTP_", 5)) { zval **header; zend_hash_get_current_data(HTTP_SERVER_VARS, (void **) &header); add_assoc_stringl(array, pretty_key(key + 5, strlen(key) - 5, 1, 1), Z_STRVAL_PP(header), Z_STRLEN_PP(header), 1); @@ -1534,6 +1528,47 @@ PHP_HTTP_API void _http_get_request_headers(zval *array TSRMLS_DC) } /* }}} */ +/* {{{ STATUS http_urlencode_hash_ex(HashTable *, int, char **, size_t *) */ +PHP_HTTP_API STATUS _http_urlencode_hash_ex(HashTable *hash, int override_argsep, + char *pre_encoded_data, size_t pre_encoded_len, + char **encoded_data, size_t *encoded_len TSRMLS_DC) +{ + smart_str qstr = {0}; + + if (override_argsep) { + HTTP_URL_ARGSEP_OVERRIDE; + } + + if (pre_encoded_len && pre_encoded_data) { + smart_str_appendl(&qstr, pre_encoded_data, pre_encoded_len); + } + + if (SUCCESS != php_url_encode_hash_ex(hash, &qstr, NULL, 0, NULL, 0, NULL, 0, NULL TSRMLS_CC)) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Couldn't encode query data"); + if (qstr.c) { + efree(qstr.c); + } + if (override_argsep) { + HTTP_URL_ARGSEP_RESTORE; + } + return FAILURE; + } + + if (override_argsep) { + HTTP_URL_ARGSEP_RESTORE; + } + + smart_str_0(&qstr); + + *encoded_data = qstr.c; + if (encoded_len) { + *encoded_len = qstr.len; + } + + return SUCCESS; +} +/* }}} */ + /* {{{ STATUS http_auth_header(char *, char*) */ PHP_HTTP_API STATUS _http_auth_header(const char *type, const char *realm TSRMLS_DC) { @@ -1578,6 +1613,170 @@ PHP_HTTP_API STATUS _http_auth_credentials(char **user, char **pass TSRMLS_DC) } /* }}} */ +#ifndef ZEND_ENGINE_2 +/* {{{ php_url_encode_hash + Author: Sarah Golemon */ +PHP_HTTP_API int php_url_encode_hash_ex(HashTable *ht, smart_str *formstr, + const char *num_prefix, int num_prefix_len, + const char *key_prefix, int key_prefix_len, + const char *key_suffix, int key_suffix_len, + zval *type TSRMLS_DC) +{ + char *arg_sep = NULL, *key = NULL, *ekey, *newprefix, *p; + int arg_sep_len, key_len, ekey_len, key_type, newprefix_len; + ulong idx; + zval **zdata = NULL, *copyzval; + + if (!ht) { + return FAILURE; + } + + if (ht->nApplyCount > 0) { + /* Prevent recursion */ + return SUCCESS; + } + + arg_sep = INI_STR("arg_separator.output"); + if (!arg_sep || !strlen(arg_sep)) { + arg_sep = HTTP_URL_ARGSEP_DEFAULT; + } + arg_sep_len = strlen(arg_sep); + + for (zend_hash_internal_pointer_reset(ht); + (key_type = zend_hash_get_current_key_ex(ht, &key, &key_len, &idx, 0, NULL)) != HASH_KEY_NON_EXISTANT; + zend_hash_move_forward(ht) + ) { + if (key_type == HASH_KEY_IS_STRING && key_len && key[key_len-1] == '\0') { + /* We don't want that trailing NULL */ + key_len -= 1; + } + +#ifdef ZEND_ENGINE_2 + /* handling for private & protected object properties */ + if (key && *key == '\0' && type != NULL) { + char *tmp; + + zend_object *zobj = zend_objects_get_address(type TSRMLS_CC); + if (zend_check_property_access(zobj, key TSRMLS_CC) != SUCCESS) { + /* private or protected property access outside of the class */ + continue; + } + zend_unmangle_property_name(key, &tmp, &key); + key_len = strlen(key); + } +#endif + + if (zend_hash_get_current_data_ex(ht, (void **)&zdata, NULL) == FAILURE || !zdata || !(*zdata)) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error traversing form data array."); + return FAILURE; + } + if (Z_TYPE_PP(zdata) == IS_ARRAY || Z_TYPE_PP(zdata) == IS_OBJECT) { + if (key_type == HASH_KEY_IS_STRING) { + ekey = php_url_encode(key, key_len, &ekey_len); + newprefix_len = key_suffix_len + ekey_len + key_prefix_len + 1; + newprefix = emalloc(newprefix_len + 1); + p = newprefix; + + if (key_prefix) { + memcpy(p, key_prefix, key_prefix_len); + p += key_prefix_len; + } + + memcpy(p, ekey, ekey_len); + p += ekey_len; + efree(ekey); + + if (key_suffix) { + memcpy(p, key_suffix, key_suffix_len); + p += key_suffix_len; + } + + *(p++) = '['; + *p = '\0'; + } else { + /* Is an integer key */ + ekey_len = spprintf(&ekey, 12, "%ld", idx); + newprefix_len = key_prefix_len + num_prefix_len + ekey_len + key_suffix_len + 1; + newprefix = emalloc(newprefix_len + 1); + p = newprefix; + + if (key_prefix) { + memcpy(p, key_prefix, key_prefix_len); + p += key_prefix_len; + } + + memcpy(p, num_prefix, num_prefix_len); + p += num_prefix_len; + + memcpy(p, ekey, ekey_len); + p += ekey_len; + efree(ekey); + + if (key_suffix) { + memcpy(p, key_suffix, key_suffix_len); + p += key_suffix_len; + } + *(p++) = '['; + *p = '\0'; + } + ht->nApplyCount++; + php_url_encode_hash_ex(HASH_OF(*zdata), formstr, NULL, 0, newprefix, newprefix_len, "]", 1, (Z_TYPE_PP(zdata) == IS_OBJECT ? *zdata : NULL) TSRMLS_CC); + ht->nApplyCount--; + efree(newprefix); + } else if (Z_TYPE_PP(zdata) == IS_NULL || Z_TYPE_PP(zdata) == IS_RESOURCE) { + /* Skip these types */ + continue; + } else { + if (formstr->len) { + smart_str_appendl(formstr, arg_sep, arg_sep_len); + } + /* Simple key=value */ + smart_str_appendl(formstr, key_prefix, key_prefix_len); + if (key_type == HASH_KEY_IS_STRING) { + ekey = php_url_encode(key, key_len, &ekey_len); + smart_str_appendl(formstr, ekey, ekey_len); + efree(ekey); + } else { + /* Numeric key */ + if (num_prefix) { + smart_str_appendl(formstr, num_prefix, num_prefix_len); + } + ekey_len = spprintf(&ekey, 12, "%ld", idx); + smart_str_appendl(formstr, ekey, ekey_len); + efree(ekey); + } + smart_str_appendl(formstr, key_suffix, key_suffix_len); + smart_str_appendl(formstr, "=", 1); + switch (Z_TYPE_PP(zdata)) { + case IS_STRING: + ekey = php_url_encode(Z_STRVAL_PP(zdata), Z_STRLEN_PP(zdata), &ekey_len); + break; + case IS_LONG: + case IS_BOOL: + ekey_len = spprintf(&ekey, 12, "%ld", Z_LVAL_PP(zdata)); + break; + case IS_DOUBLE: + ekey_len = spprintf(&ekey, 48, "%.*G", (int) EG(precision), Z_DVAL_PP(zdata)); + break; + default: + /* fall back on convert to string */ + MAKE_STD_ZVAL(copyzval); + *copyzval = **zdata; + zval_copy_ctor(copyzval); + convert_to_string_ex(©zval); + ekey = php_url_encode(Z_STRVAL_P(copyzval), Z_STRLEN_P(copyzval), &ekey_len); + zval_ptr_dtor(©zval); + } + smart_str_appendl(formstr, ekey, ekey_len); + efree(ekey); + } + } + + return SUCCESS; +} +/* }}} */ +#endif /* !ZEND_ENDGINE_2 */ + /* }}} public API */ /* diff --git a/http_build_query.c b/http_build_query.c deleted file mode 100644 index ecad1d0..0000000 --- a/http_build_query.c +++ /dev/null @@ -1,232 +0,0 @@ -/* - +----------------------------------------------------------------------+ - | PHP Version 5 | - +----------------------------------------------------------------------+ - | Copyright (c) 1997-2004 The PHP Group | - +----------------------------------------------------------------------+ - | This source file is subject to version 3.0 of the PHP license, | - | that is bundled with this package in the file LICENSE, and is | - | available through the world-wide-web at the following url: | - | http://www.php.net/license/3_0.txt. | - | If you did not receive a copy of the PHP license and are unable to | - | obtain it through the world-wide-web, please send a note to | - | license@php.net so we can mail you a copy immediately. | - +----------------------------------------------------------------------+ - | Authors: Sara Golemon | - +----------------------------------------------------------------------+ -*/ - -/* $Id$ */ - -#include "php_http.h" -#include "php_http_api.h" -#include "php_ini.h" -#include "ext/standard/url.h" - -#define URL_DEFAULT_ARG_SEP "&" - -/* {{{ php_url_encode_hash */ -PHP_HTTP_API int php_url_encode_hash_ex(HashTable *ht, smart_str *formstr, - const char *num_prefix, int num_prefix_len, - const char *key_prefix, int key_prefix_len, - const char *key_suffix, int key_suffix_len, - zval *type TSRMLS_DC) -{ - char *arg_sep = NULL, *key = NULL, *ekey, *newprefix, *p; - int arg_sep_len, key_len, ekey_len, key_type, newprefix_len; - ulong idx; - zval **zdata = NULL, *copyzval; - - if (!ht) { - return FAILURE; - } - - if (ht->nApplyCount > 0) { - /* Prevent recursion */ - return SUCCESS; - } - - arg_sep = INI_STR("arg_separator.output"); - if (!arg_sep || !strlen(arg_sep)) { - arg_sep = URL_DEFAULT_ARG_SEP; - } - arg_sep_len = strlen(arg_sep); - - for (zend_hash_internal_pointer_reset(ht); - (key_type = zend_hash_get_current_key_ex(ht, &key, &key_len, &idx, 0, NULL)) != HASH_KEY_NON_EXISTANT; - zend_hash_move_forward(ht) - ) { - if (key_type == HASH_KEY_IS_STRING && key_len && key[key_len-1] == '\0') { - /* We don't want that trailing NULL */ - key_len -= 1; - } - -#ifdef ZEND_ENGINE_2 - /* handling for private & protected object properties */ - if (key && *key == '\0' && type != NULL) { - char *tmp; - - zend_object *zobj = zend_objects_get_address(type TSRMLS_CC); - if (zend_check_property_access(zobj, key TSRMLS_CC) != SUCCESS) { - /* private or protected property access outside of the class */ - continue; - } - zend_unmangle_property_name(key, &tmp, &key); - key_len = strlen(key); - } -#endif - - if (zend_hash_get_current_data_ex(ht, (void **)&zdata, NULL) == FAILURE || !zdata || !(*zdata)) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error traversing form data array."); - return FAILURE; - } - if (Z_TYPE_PP(zdata) == IS_ARRAY || Z_TYPE_PP(zdata) == IS_OBJECT) { - if (key_type == HASH_KEY_IS_STRING) { - ekey = php_url_encode(key, key_len, &ekey_len); - newprefix_len = key_suffix_len + ekey_len + key_prefix_len + 1; - newprefix = emalloc(newprefix_len + 1); - p = newprefix; - - if (key_prefix) { - memcpy(p, key_prefix, key_prefix_len); - p += key_prefix_len; - } - - memcpy(p, ekey, ekey_len); - p += ekey_len; - efree(ekey); - - if (key_suffix) { - memcpy(p, key_suffix, key_suffix_len); - p += key_suffix_len; - } - - *(p++) = '['; - *p = '\0'; - } else { - /* Is an integer key */ - ekey_len = spprintf(&ekey, 12, "%ld", idx); - newprefix_len = key_prefix_len + num_prefix_len + ekey_len + key_suffix_len + 1; - newprefix = emalloc(newprefix_len + 1); - p = newprefix; - - if (key_prefix) { - memcpy(p, key_prefix, key_prefix_len); - p += key_prefix_len; - } - - memcpy(p, num_prefix, num_prefix_len); - p += num_prefix_len; - - memcpy(p, ekey, ekey_len); - p += ekey_len; - efree(ekey); - - if (key_suffix) { - memcpy(p, key_suffix, key_suffix_len); - p += key_suffix_len; - } - *(p++) = '['; - *p = '\0'; - } - ht->nApplyCount++; - php_url_encode_hash_ex(HASH_OF(*zdata), formstr, NULL, 0, newprefix, newprefix_len, "]", 1, (Z_TYPE_PP(zdata) == IS_OBJECT ? *zdata : NULL) TSRMLS_CC); - ht->nApplyCount--; - efree(newprefix); - } else if (Z_TYPE_PP(zdata) == IS_NULL || Z_TYPE_PP(zdata) == IS_RESOURCE) { - /* Skip these types */ - continue; - } else { - if (formstr->len) { - smart_str_appendl(formstr, arg_sep, arg_sep_len); - } - /* Simple key=value */ - smart_str_appendl(formstr, key_prefix, key_prefix_len); - if (key_type == HASH_KEY_IS_STRING) { - ekey = php_url_encode(key, key_len, &ekey_len); - smart_str_appendl(formstr, ekey, ekey_len); - efree(ekey); - } else { - /* Numeric key */ - if (num_prefix) { - smart_str_appendl(formstr, num_prefix, num_prefix_len); - } - ekey_len = spprintf(&ekey, 12, "%ld", idx); - smart_str_appendl(formstr, ekey, ekey_len); - efree(ekey); - } - smart_str_appendl(formstr, key_suffix, key_suffix_len); - smart_str_appendl(formstr, "=", 1); - switch (Z_TYPE_PP(zdata)) { - case IS_STRING: - ekey = php_url_encode(Z_STRVAL_PP(zdata), Z_STRLEN_PP(zdata), &ekey_len); - break; - case IS_LONG: - case IS_BOOL: - ekey_len = spprintf(&ekey, 12, "%ld", Z_LVAL_PP(zdata)); - break; - case IS_DOUBLE: - ekey_len = spprintf(&ekey, 48, "%.*G", (int) EG(precision), Z_DVAL_PP(zdata)); - break; - default: - /* fall back on convert to string */ - MAKE_STD_ZVAL(copyzval); - *copyzval = **zdata; - zval_copy_ctor(copyzval); - convert_to_string_ex(©zval); - ekey = php_url_encode(Z_STRVAL_P(copyzval), Z_STRLEN_P(copyzval), &ekey_len); - zval_ptr_dtor(©zval); - } - smart_str_appendl(formstr, ekey, ekey_len); - efree(ekey); - } - } - - return SUCCESS; -} -/* }}} */ - -/* {{{ proto string http_build_query(mixed formdata [, string prefix]) - Generates a form-encoded query string from an associative array or object. */ -PHP_FUNCTION(http_build_query) -{ - zval *formdata; - char *prefix = NULL; - int prefix_len = 0; - smart_str formstr = {0}; - - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|s", &formdata, &prefix, &prefix_len) != SUCCESS) { - RETURN_FALSE; - } - - if (Z_TYPE_P(formdata) != IS_ARRAY && Z_TYPE_P(formdata) != IS_OBJECT) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Parameter 1 expected to be Array or Object. Incorrect value given."); - RETURN_FALSE; - } - - if (php_url_encode_hash_ex(HASH_OF(formdata), &formstr, prefix, prefix_len, NULL, 0, NULL, 0, (Z_TYPE_P(formdata) == IS_OBJECT ? formdata : NULL) TSRMLS_CC) == FAILURE) { - if (formstr.c) { - efree(formstr.c); - } - RETURN_FALSE; - } - - if (!formstr.c) { - RETURN_NULL(); - } - - smart_str_0(&formstr); - - RETURN_STRINGL(formstr.c, formstr.len, 0); -} -/* }}} */ - -/* - * Local variables: - * tab-width: 4 - * c-basic-offset: 4 - * End: - * vim600: sw=4 ts=4 fdm=marker - * vim<600: sw=4 ts=4 - */ - diff --git a/http_curl_api.c b/http_curl_api.c index dbd2440..9bf3857 100644 --- a/http_curl_api.c +++ b/http_curl_api.c @@ -33,6 +33,7 @@ #include "php_http.h" #include "php_http_api.h" #include "php_http_curl_api.h" +#include "php_http_std_defs.h" #include "ext/standard/php_smart_str.h" diff --git a/http_functions.c b/http_functions.c index 6e7f236..5190d5b 100644 --- a/http_functions.c +++ b/http_functions.c @@ -35,6 +35,7 @@ #include "php_http.h" #include "php_http_api.h" #include "php_http_curl_api.h" +#include "php_http_std_defs.h" ZEND_DECLARE_MODULE_GLOBALS(http) @@ -429,10 +430,12 @@ PHP_FUNCTION(ob_httpetaghandler) PHP_FUNCTION(http_redirect) { int url_len; + size_t query_len = 0; zend_bool session = 0, permanent = 0; zval *params = NULL; - smart_str qstr = {0}; - char *url, *URI, LOC[HTTP_URI_MAXLEN + 9], RED[HTTP_URI_MAXLEN * 2 + 34]; + char *query, *url, *URI, + LOC[HTTP_URI_MAXLEN + sizeof("Location: ")], + RED[HTTP_URI_MAXLEN * 2 + sizeof("Redirecting to %s?%s.\n")]; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sa!/bb", &url, &url_len, ¶ms, &session, &permanent) != SUCCESS) { RETURN_FALSE; @@ -451,23 +454,18 @@ PHP_FUNCTION(http_redirect) /* treat params array with http_build_query() */ if (params) { - if (php_url_encode_hash_ex(Z_ARRVAL_P(params), &qstr, NULL,0,NULL,0,NULL,0,NULL TSRMLS_CC) != SUCCESS) { - if (qstr.c) { - efree(qstr.c); - } - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not encode query parameters"); + if (SUCCESS != http_urlencode_hash_ex(Z_ARRVAL_P(params), 0, NULL, 0, &query, &query_len)) { RETURN_FALSE; } - smart_str_0(&qstr); } URI = http_absolute_uri(url, NULL); - if (qstr.c) { - snprintf(LOC, HTTP_URI_MAXLEN + strlen("Location: "), "Location: %s?%s", URI, qstr.c); - sprintf(RED, "Redirecting to %s?%s.\n", URI, qstr.c, URI, qstr.c); - efree(qstr.c); + if (query_len) { + snprintf(LOC, HTTP_URI_MAXLEN + sizeof("Location: "), "Location: %s?%s", URI, query); + sprintf(RED, "Redirecting to %s?%s.\n", URI, query, URI, query); + efree(query); } else { - snprintf(LOC, HTTP_URI_MAXLEN + strlen("Location: "), "Location: %s", URI); + snprintf(LOC, HTTP_URI_MAXLEN + sizeof("Location: "), "Location: %s", URI); sprintf(RED, "Redirecting to %s.\n", URI, URI); } efree(URI); @@ -935,6 +933,45 @@ PHP_FUNCTION(http_auth_basic_cb) } /* }}}*/ +/* {{{ Sara Golemons http_build_query() */ +#ifndef ZEND_ENGINE_2 + +/* {{{ proto string http_build_query(mixed formdata [, string prefix]) + Generates a form-encoded query string from an associative array or object. */ +PHP_FUNCTION(http_build_query) +{ + zval *formdata; + char *prefix = NULL; + int prefix_len = 0; + smart_str formstr = {0}; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|s", &formdata, &prefix, &prefix_len) != SUCCESS) { + RETURN_FALSE; + } + + if (Z_TYPE_P(formdata) != IS_ARRAY && Z_TYPE_P(formdata) != IS_OBJECT) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Parameter 1 expected to be Array or Object. Incorrect value given."); + RETURN_FALSE; + } + + if (php_url_encode_hash_ex(HASH_OF(formdata), &formstr, prefix, prefix_len, NULL, 0, NULL, 0, (Z_TYPE_P(formdata) == IS_OBJECT ? formdata : NULL) TSRMLS_CC) == FAILURE) { + if (formstr.c) { + efree(formstr.c); + } + RETURN_FALSE; + } + + if (!formstr.c) { + RETURN_NULL(); + } + + smart_str_0(&formstr); + + RETURN_STRINGL(formstr.c, formstr.len, 0); +} +/* }}} */ +#endif /* !ZEND_ENGINE_2 */ +/* }}} */ /* * Local variables: diff --git a/http_methods.c b/http_methods.c index 6f3ba2c..5e9e0d3 100644 --- a/http_methods.c +++ b/http_methods.c @@ -26,6 +26,8 @@ #include "php_http.h" #include "php_http_api.h" #include "php_http_curl_api.h" +#include "php_http_std_defs.h" + #include "ext/standard/php_smart_str.h" #ifdef ZEND_ENGINE_2 @@ -514,6 +516,8 @@ PHP_METHOD(HTTPi_Request, __destruct) */ PHP_METHOD(HTTPi_Request, setOptions) { + char *key = NULL; + long idx = 0; zval *opts, *old_opts, **opt; getObject(httpi_request_object, obj); @@ -523,13 +527,9 @@ PHP_METHOD(HTTPi_Request, setOptions) old_opts = GET_PROP(obj, options); - /* headers and cookies need extra attention -- thus cannot use zend_hash_merge() or php_array_merge() directly */ - for ( zend_hash_internal_pointer_reset(Z_ARRVAL_P(opts)); - zend_hash_get_current_data(Z_ARRVAL_P(opts), (void **) &opt) == SUCCESS; - zend_hash_move_forward(Z_ARRVAL_P(opts))) { - char *key; - long idx; - if (HASH_KEY_IS_STRING == zend_hash_get_current_key(Z_ARRVAL_P(opts), &key, &idx, 0)) { + /* headers and cookies need extra attention -- thus cannot use array_merge() directly */ + FOREACH_KEYVAL(opts, key, idx, opt) { + if (key) { if (!strcmp(key, "headers")) { zval **headers; if (SUCCESS == zend_hash_find(Z_ARRVAL_P(old_opts), "headers", sizeof("headers"), (void **) &headers)) { @@ -545,8 +545,12 @@ PHP_METHOD(HTTPi_Request, setOptions) } zval_add_ref(opt); add_assoc_zval(old_opts, key, *opt); + + /* reset */ + key = NULL; } } + RETURN_TRUE; } /* }}} */ @@ -678,6 +682,7 @@ PHP_METHOD(HTTPi_Request, getContentType) PHP_METHOD(HTTPi_Request, setQueryData) { zval *qdata; + char *query_data = NULL; getObject(httpi_request_object, obj); if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &qdata)) { @@ -685,20 +690,11 @@ PHP_METHOD(HTTPi_Request, setQueryData) } if ((Z_TYPE_P(qdata) == IS_ARRAY) || (Z_TYPE_P(qdata) == IS_OBJECT)) { - smart_str qstr = {0}; - HTTP_URL_ARGSEP_OVERRIDE; - if (SUCCESS != php_url_encode_hash_ex(HASH_OF(qdata), &qstr, NULL, 0, NULL, 0, NULL, 0, NULL TSRMLS_CC)) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Couldn't encode query data"); - if (qstr.c) { - efree(qstr.c); - } - HTTP_URL_ARGSEP_RESTORE; + if (SUCCESS != http_urlencode_hash(HASH_OF(qdata), &query_data)) { RETURN_FALSE; } - HTTP_URL_ARGSEP_RESTORE; - smart_str_0(&qstr); - UPD_PROP(obj, string, queryData, qstr.c); - efree(qstr.c); + UPD_PROP(obj, string, queryData, query_data); + efree(query_data); RETURN_TRUE; } @@ -729,8 +725,7 @@ PHP_METHOD(HTTPi_Request, getQueryData) PHP_METHOD(HTTPi_Request, addQueryData) { zval *qdata, *old_qdata; - smart_str qstr = {0}; - char *separator; + char *query_data = NULL; getObject(httpi_request_object, obj); if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &qdata)) { @@ -738,25 +733,14 @@ PHP_METHOD(HTTPi_Request, addQueryData) } old_qdata = GET_PROP(obj, queryData); - if (Z_STRLEN_P(old_qdata)) { - smart_str_appendl(&qstr, Z_STRVAL_P(old_qdata), Z_STRLEN_P(old_qdata)); - } - - HTTP_URL_ARGSEP_OVERRIDE; - if (SUCCESS != php_url_encode_hash_ex(HASH_OF(qdata), &qstr, NULL, 0, NULL, 0, NULL, 0, NULL TSRMLS_CC)) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Couldn't encode query data"); - if (qstr.c) { - efree(qstr.c); - } - HTTP_URL_ARGSEP_RESTORE; + + if (SUCCESS != http_urlencode_hash_ex(HASH_OF(qdata), 1, Z_STRVAL_P(old_qdata), Z_STRLEN_P(old_qdata), &query_data, NULL)) { RETURN_FALSE; } - HTTP_URL_ARGSEP_RESTORE; - smart_str_0(&qstr); - - UPD_PROP(obj, string, queryData, qstr.c); - efree(qstr.c); + UPD_PROP(obj, string, queryData, query_data); + efree(query_data); + RETURN_TRUE; } /* }}} */ @@ -1032,7 +1016,7 @@ PHP_METHOD(HTTPi_Request, send) case HTTP_POST: { - zval *post_files, *post_data, **data; + zval *post_files, *post_data; post_files = GET_PROP(obj, postFiles); post_data = GET_PROP(obj, postData); @@ -1047,17 +1031,14 @@ PHP_METHOD(HTTPi_Request, send) /* * multipart post */ + char *key = NULL; + long idx; + zval **data; struct curl_httppost *http_post_data[2] = {NULL, NULL}; /* normal data */ - for ( zend_hash_internal_pointer_reset(Z_ARRVAL_P(post_data)); - zend_hash_get_current_data(Z_ARRVAL_P(post_data), (void **) &data) == SUCCESS; - zend_hash_move_forward(Z_ARRVAL_P(post_data))) { - - char *key; - long idx; - - if (HASH_KEY_IS_STRING == zend_hash_get_current_key(Z_ARRVAL_P(post_data), &key, &idx, 0)) { + FOREACH_KEYVAL(post_data, key, idx, data) { + if (key) { convert_to_string_ex(data); curl_formadd(&http_post_data[0], &http_post_data[1], CURLFORM_COPYNAME, key, @@ -1065,21 +1046,19 @@ PHP_METHOD(HTTPi_Request, send) CURLFORM_CONTENTSLENGTH, Z_STRLEN_PP(data), CURLFORM_END ); + + /* reset */ + key = NULL; } } - + /* file data */ - for ( zend_hash_internal_pointer_reset(Z_ARRVAL_P(post_files)); - zend_hash_get_current_data(Z_ARRVAL_P(post_files), (void **) &data) == SUCCESS; - zend_hash_move_forward(Z_ARRVAL_P(post_files))) { - + FOREACH_VAL(post_files, data) { zval **file, **type, **name; - if ( - SUCCESS == zend_hash_find(Z_ARRVAL_PP(data), "name", sizeof("name"), (void **) &name) && - SUCCESS == zend_hash_find(Z_ARRVAL_PP(data), "type", sizeof("type"), (void **) &type) && - SUCCESS == zend_hash_find(Z_ARRVAL_PP(data), "file", sizeof("file"), (void **) &file) - ) { + if ( SUCCESS == zend_hash_find(Z_ARRVAL_PP(data), "name", sizeof("name"), (void **) &name) && + SUCCESS == zend_hash_find(Z_ARRVAL_PP(data), "type", sizeof("type"), (void **) &type) && + SUCCESS == zend_hash_find(Z_ARRVAL_PP(data), "file", sizeof("file"), (void **) &file)) { curl_formadd(&http_post_data[0], &http_post_data[1], CURLFORM_COPYNAME, Z_STRVAL_PP(name), diff --git a/package.xml b/package.xml index 68f8379..1b19cd7 100644 --- a/package.xml +++ b/package.xml @@ -41,6 +41,7 @@ + CREDITS EXPERIMENTAL docs/functions.html @@ -50,6 +51,7 @@ php_http.h php_http_api.h php_http_curl_api.h + php_http_std_defs.h http.c http_functions.c @@ -57,8 +59,6 @@ http_api.c http_curl_api.c - http_build_query.c - php_http_build_query.h diff --git a/php_http.h b/php_http.h index b7274e9..55b5989 100644 --- a/php_http.h +++ b/php_http.h @@ -34,9 +34,7 @@ extern zend_module_entry http_module_entry; # define HTTP_G(v) (http_globals.v) #endif -#ifndef ZEND_ENGINE_2 -# include "php_http_build_query.h" -#else +#ifdef ZEND_ENGINE_2 typedef struct { zend_object zo; @@ -170,7 +168,9 @@ PHP_FUNCTION(http_post_array); #endif PHP_FUNCTION(http_auth_basic); PHP_FUNCTION(http_auth_basic_cb); - +#ifndef ZEND_ENGINE_2 +PHP_FUNCTION(http_build_query); +#endif PHP_FUNCTION(ob_httpetaghandler); PHP_MINIT_FUNCTION(http); diff --git a/php_http_api.h b/php_http_api.h index fc4faac..94f17d7 100644 --- a/php_http_api.h +++ b/php_http_api.h @@ -24,69 +24,6 @@ # define PHP_HTTP_API #endif -#ifndef ZEND_ENGINE_2 -# include "php_http_build_query.h" -#endif - -#define RETURN_SUCCESS(v) RETURN_BOOL(SUCCESS == (v)) -#define HASH_ORNULL(z) ((z) ? Z_ARRVAL_P(z) : NULL) -#define NO_ARGS if (ZEND_NUM_ARGS()) WRONG_PARAM_COUNT - -#define array_copy(src, dst) zend_hash_copy(Z_ARRVAL_P(dst), Z_ARRVAL_P(src), (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *)) -#define array_merge(src, dst) zend_hash_merge(Z_ARRVAL_P(dst), Z_ARRVAL_P(src), (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *), 1) - -#ifdef ZEND_ENGINE_2 - -# define HTTP_REGISTER_CLASS_EX(classname, name, parent, flags) \ - { \ - zend_class_entry ce; \ - INIT_CLASS_ENTRY(ce, #classname, name## _class_methods); \ - ce.create_object = name## _new_object; \ - name## _ce = zend_register_internal_class_ex(&ce, parent, NULL TSRMLS_CC); \ - name## _ce->ce_flags |= flags; \ - memcpy(& name## _object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); \ - name## _object_handlers.clone_obj = NULL; \ - name## _declare_default_properties(name## _ce); \ - } - -# define HTTP_REGISTER_CLASS(classname, name, parent, flags) \ - { \ - zend_class_entry ce; \ - INIT_CLASS_ENTRY(ce, #classname, name## _class_methods); \ - ce.create_object = NULL; \ - name## _ce = zend_register_internal_class_ex(&ce, parent, NULL TSRMLS_CC); \ - name## _ce->ce_flags |= flags; \ - } - -# define getObject(t, o) t * o = ((t *) zend_object_store_get_object(getThis() TSRMLS_CC)) -# define OBJ_PROP(o) o->zo.properties -# define DCL_PROP(a, t, n, v) zend_declare_property_ ##t(ce, (#n), sizeof(#n), (v), (ZEND_ACC_ ##a) TSRMLS_CC) -# define DCL_PROP_Z(a, n, v) zend_declare_property(ce, (#n), sizeof(#n), (v), (ZEND_ACC_ ##a) TSRMLS_CC) -# define DCL_PROP_N(a, n) zend_declare_property_null(ce, (#n), sizeof(#n), (ZEND_ACC_ ##a) TSRMLS_CC) -# define UPD_PROP(o, t, n, v) zend_update_property_ ##t(o->zo.ce, getThis(), (#n), sizeof(#n), (v) TSRMLS_CC) -# define SET_PROP(o, n, z) zend_update_property(o->zo.ce, getThis(), (#n), sizeof(#n), (z) TSRMLS_CC) -# define GET_PROP(o, n) zend_read_property(o->zo.ce, getThis(), (#n), sizeof(#n), 0 TSRMLS_CC) - -# define INIT_PARR(o, n) \ - { \ - zval *__tmp; \ - MAKE_STD_ZVAL(__tmp); \ - array_init(__tmp); \ - SET_PROP(o, n, __tmp); \ - } - -# define FREE_PARR(o, p) \ - { \ - zval *__tmp = NULL; \ - if (__tmp = GET_PROP(o, p)) { \ - zval_dtor(__tmp); \ - FREE_ZVAL(__tmp); \ - __tmp = NULL; \ - } \ - } - -#endif /* ZEND_ENGINE_2 */ - /* make functions that return SUCCESS|FAILURE more obvious */ typedef int STATUS; @@ -105,36 +42,6 @@ typedef enum { } http_send_mode; /* }}} */ -/* CR LF */ -#define HTTP_CRLF "\r\n" - -/* default cache control */ -#define HTTP_DEFAULT_CACHECONTROL "private, must-revalidate, max-age=0" - -/* max URI length */ -#define HTTP_URI_MAXLEN 2048 - -/* buffer size */ -#define HTTP_BUF_SIZE 2097152 - -/* server vars shorthand */ -#define HTTP_SERVER_VARS Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_SERVER]) - - -/* {{{ HTTP_GSC(var, name, ret) */ -#define HTTP_GSC(var, name, ret) HTTP_GSP(var, name, return ret) -/* }}} */ - -/* {{{ HTTP_GSP(var, name, ret) */ -#define HTTP_GSP(var, name, ret) \ - if (!(var = http_get_server_var(name))) { \ - ret; \ - } \ - if (!Z_STRLEN_P(var)) { \ - ret; \ - } -/* }}} */ - char *pretty_key(char *key, int key_len, int uctitle, int xhyphen); /* {{{ public API */ @@ -227,6 +134,10 @@ PHP_HTTP_API STATUS _http_send_file(const zval *zfile TSRMLS_DC); #define http_chunked_decode(e, el, d, dl) _http_chunked_decode((e), (el), (d), (dl) TSRMLS_CC) PHP_HTTP_API STATUS _http_chunked_decode(const char *encoded, const size_t encoded_len, char **decoded, size_t *decoded_len TSRMLS_DC); +#define http_urlencode_hash(h, q) _http_urlencode_hash_ex((h), 1, NULL, 0, (q), NULL TSRMLS_CC) +#define http_urlencode_hash_ex(h, o, p, pl, q, ql) _http_urlencode_hash_ex((h), (o), (p), (pl), (q), (ql) TSRMLS_CC) +PHP_HTTP_API STATUS _http_urlencode_hash_ex(HashTable *hash, int override_argsep, char *pre_encoded_data, size_t pre_encoded_len, char **encoded_data, size_t *encoded_len TSRMLS_DC); + #define http_split_response(r, h, b) _http_split_response_ex(Z_STRVAL_P(r), Z_STRLEN_P(r), (h), (b) TSRMLS_CC) #define http_split_response_ex(r, l, h, b) _http_split_response_ex((r), (l), (h), (b) TSRMLS_CC) PHP_HTTP_API STATUS _http_split_response_ex(char *response, size_t repsonse_len, zval *zheaders, zval *zbody TSRMLS_DC); @@ -243,6 +154,15 @@ PHP_HTTP_API STATUS _http_auth_credentials(char **user, char **pass TSRMLS_DC); #define http_auth_header(t, r) _http_auth_header((t), (r) TSRMLS_CC) PHP_HTTP_API STATUS _http_auth_header(const char *type, const char *realm TSRMLS_DC); +#ifndef ZEND_ENGINE_2 +#define php_url_encode_hash(ht, formstr) php_url_encode_hash_ex((ht), (formstr), NULL, 0, NULL, 0, NULL, 0, NULL TSRMLS_CC) +PHPAPI int php_url_encode_hash_ex(HashTable *ht, smart_str *formstr, + const char *num_prefix, int num_prefix_len, + const char *key_prefix, int key_prefix_len, + const char *key_suffix, int key_suffix_len, + zval *type TSRMLS_DC); +#endif + /* }}} */ #endif diff --git a/php_http_build_query.h b/php_http_build_query.h deleted file mode 100644 index bd87970..0000000 --- a/php_http_build_query.h +++ /dev/null @@ -1,45 +0,0 @@ -/* - +----------------------------------------------------------------------+ - | PHP Version 5 | - +----------------------------------------------------------------------+ - | Copyright (c) 1997-2004 The PHP Group | - +----------------------------------------------------------------------+ - | This source file is subject to version 3.0 of the PHP license, | - | that is bundled with this package in the file LICENSE, and is | - | available through the world-wide-web at the following url: | - | http://www.php.net/license/3_0.txt. | - | If you did not receive a copy of the PHP license and are unable to | - | obtain it through the world-wide-web, please send a note to | - | license@php.net so we can mail you a copy immediately. | - +----------------------------------------------------------------------+ - | Authors: Sara Golemon | - +----------------------------------------------------------------------+ -*/ - -/* $Id$ */ - -#ifndef PHP_HTTP_H -#define PHP_HTTP_H - -#include "php.h" -#include "ext/standard/php_smart_str.h" - -PHPAPI int php_url_encode_hash_ex(HashTable *ht, smart_str *formstr, - const char *num_prefix, int num_prefix_len, - const char *key_prefix, int key_prefix_len, - const char *key_suffix, int key_suffix_len, - zval *type TSRMLS_DC); -#define php_url_encode_hash(ht, formstr) php_url_encode_hash_ex((ht), (formstr), NULL, 0, NULL, 0, NULL, 0, NULL TSRMLS_CC) - -PHP_FUNCTION(http_build_query); - -#endif - -/* - * Local variables: - * tab-width: 4 - * c-basic-offset: 4 - * End: - * vim600: sw=4 ts=4 fdm=marker - * vim<600: sw=4 ts=4 - */ diff --git a/php_http_curl_api.h b/php_http_curl_api.h index 085509e..3b55c28 100644 --- a/php_http_curl_api.h +++ b/php_http_curl_api.h @@ -19,14 +19,6 @@ #define PHP_HTTP_CURL_API_H #include -#include "zend_ini.h" - -/* override arg_separator.output to "&" for data used in outgoing requests */ -#define HTTP_URL_ARGSEP_OVERRIDE zend_alter_ini_entry("arg_separator.output", sizeof("arg_separator.output") - 1, "&", 1, ZEND_INI_ALL, ZEND_INI_STAGE_RUNTIME) -#define HTTP_URL_ARGSEP_RESTORE zend_restore_ini_entry("arg_separator.output", sizeof("arg_separator.output") - 1, ZEND_INI_STAGE_RUNTIME) - -/* CURL buffer size */ -#define HTTP_CURLBUF_SIZE 16384 #define http_get(u, o, i, d, l) _http_get_ex(NULL, (u), (o), (i), (d), (l) TSRMLS_CC) #define http_get_ex(c, u, o, i, d, l) _http_get_ex((c), (u), (o), (i), (d), (l) TSRMLS_CC) diff --git a/php_http_std_defs.h b/php_http_std_defs.h new file mode 100644 index 0000000..7335be3 --- /dev/null +++ b/php_http_std_defs.h @@ -0,0 +1,152 @@ +/* + +----------------------------------------------------------------------+ + | PECL :: http | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.0 of the PHP license, that | + | is bundled with this package in the file LICENSE, and is available | + | through the world-wide-web at http://www.php.net/license/3_0.txt. | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Copyright (c) 2004-2005 Michael Wallner | + +----------------------------------------------------------------------+ +*/ + +/* $Id$ */ + +#ifndef PHP_HTTP_STD_DEFS_H +#define PHP_HTTP_STD_DEFS_H + +#define RETURN_SUCCESS(v) RETURN_BOOL(SUCCESS == (v)) +#define HASH_ORNULL(z) ((z) ? Z_ARRVAL_P(z) : NULL) +#define NO_ARGS if (ZEND_NUM_ARGS()) WRONG_PARAM_COUNT + +/* CR LF */ +#define HTTP_CRLF "\r\n" + +/* default cache control */ +#define HTTP_DEFAULT_CACHECONTROL "private, must-revalidate, max-age=0" + +/* max URI length */ +#define HTTP_URI_MAXLEN 2048 + +/* send buffer size */ +#define HTTP_SENDBUF_SIZE 2097152 + +/* CURL buffer size */ +#define HTTP_CURLBUF_SIZE 16384 + +/* server vars shorthand */ +#define HTTP_SERVER_VARS Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_SERVER]) + + +/* {{{ HTTP_GSC(var, name, ret) */ +#define HTTP_GSC(var, name, ret) HTTP_GSP(var, name, return ret) +/* }}} */ + +/* {{{ HTTP_GSP(var, name, ret) */ +#define HTTP_GSP(var, name, ret) \ + if (!(var = http_get_server_var(name))) { \ + ret; \ + } \ + if (!Z_STRLEN_P(var)) { \ + ret; \ + } +/* }}} */ + +/* override arg_separator.output to "&" for data used in outgoing requests */ +#include "zend_ini.h" +#define HTTP_URL_ARGSEP_DEFAULT "&" +#define HTTP_URL_ARGSEP_OVERRIDE zend_alter_ini_entry("arg_separator.output", sizeof("arg_separator.output") - 1, HTTP_URL_ARGSEP_DEFAULT, sizeof(HTTP_URL_ARGSEP_DEFAULT) - 1, ZEND_INI_ALL, ZEND_INI_STAGE_RUNTIME) +#define HTTP_URL_ARGSEP_RESTORE zend_restore_ini_entry("arg_separator.output", sizeof("arg_separator.output") - 1, ZEND_INI_STAGE_RUNTIME) + +/* {{{ arrays */ +#define FOREACH_VAL(array, val) FOREACH_HASH_VAL(Z_ARRVAL_P(array), val) +#define FOREACH_HASH_VAL(hash, val) \ + for ( zend_hash_internal_pointer_reset(hash); \ + zend_hash_get_current_data(hash, (void **) &val) == SUCCESS; \ + zend_hash_move_forward(hash)) + +#define FOREACH_KEY(array, strkey, numkey) FOREACH_HASH_KEY(Z_ARRVAL_P(array), strkey, numkey) +#define FOREACH_HASH_KEY(hash, strkey, numkey) \ + for ( zend_hash_internal_pointer_reset(hash); \ + zend_hash_get_current_key(hash, &strkey, &numkey, 0) != HASH_KEY_NON_EXISTANT; \ + zend_hash_move_forward(hash)) \ + +#define FOREACH_KEYVAL(array, strkey, numkey, val) FOREACH_HASH_KEYVAL(Z_ARRVAL_P(array), strkey, numkey, val) +#define FOREACH_HASH_KEYVAL(hash, strkey, numkey, val) \ + for ( zend_hash_internal_pointer_reset(hash); \ + zend_hash_get_current_key(hash, &strkey, &numkey, 0) != HASH_KEY_NON_EXISTANT && \ + zend_hash_get_current_data(hash, (void **) &val) == SUCCESS; \ + zend_hash_move_forward(hash)) \ + +#define array_copy(src, dst) zend_hash_copy(Z_ARRVAL_P(dst), Z_ARRVAL_P(src), (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *)) +#define array_merge(src, dst) zend_hash_merge(Z_ARRVAL_P(dst), Z_ARRVAL_P(src), (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *), 1) +/* }}} */ + +/* {{{ objects & properties */ +#ifdef ZEND_ENGINE_2 + +# define HTTP_REGISTER_CLASS_EX(classname, name, parent, flags) \ + { \ + zend_class_entry ce; \ + INIT_CLASS_ENTRY(ce, #classname, name## _class_methods); \ + ce.create_object = name## _new_object; \ + name## _ce = zend_register_internal_class_ex(&ce, parent, NULL TSRMLS_CC); \ + name## _ce->ce_flags |= flags; \ + memcpy(& name## _object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); \ + name## _object_handlers.clone_obj = NULL; \ + name## _declare_default_properties(name## _ce); \ + } + +# define HTTP_REGISTER_CLASS(classname, name, parent, flags) \ + { \ + zend_class_entry ce; \ + INIT_CLASS_ENTRY(ce, #classname, name## _class_methods); \ + ce.create_object = NULL; \ + name## _ce = zend_register_internal_class_ex(&ce, parent, NULL TSRMLS_CC); \ + name## _ce->ce_flags |= flags; \ + } + +# define getObject(t, o) t * o = ((t *) zend_object_store_get_object(getThis() TSRMLS_CC)) +# define OBJ_PROP(o) o->zo.properties +# define DCL_PROP(a, t, n, v) zend_declare_property_ ##t(ce, (#n), sizeof(#n), (v), (ZEND_ACC_ ##a) TSRMLS_CC) +# define DCL_PROP_Z(a, n, v) zend_declare_property(ce, (#n), sizeof(#n), (v), (ZEND_ACC_ ##a) TSRMLS_CC) +# define DCL_PROP_N(a, n) zend_declare_property_null(ce, (#n), sizeof(#n), (ZEND_ACC_ ##a) TSRMLS_CC) +# define UPD_PROP(o, t, n, v) zend_update_property_ ##t(o->zo.ce, getThis(), (#n), sizeof(#n), (v) TSRMLS_CC) +# define SET_PROP(o, n, z) zend_update_property(o->zo.ce, getThis(), (#n), sizeof(#n), (z) TSRMLS_CC) +# define GET_PROP(o, n) zend_read_property(o->zo.ce, getThis(), (#n), sizeof(#n), 0 TSRMLS_CC) + +# define INIT_PARR(o, n) \ + { \ + zval *__tmp; \ + MAKE_STD_ZVAL(__tmp); \ + array_init(__tmp); \ + SET_PROP(o, n, __tmp); \ + } + +# define FREE_PARR(o, p) \ + { \ + zval *__tmp = NULL; \ + if (__tmp = GET_PROP(o, p)) { \ + zval_dtor(__tmp); \ + FREE_ZVAL(__tmp); \ + __tmp = NULL; \ + } \ + } + +#endif /* ZEND_ENGINE_2 */ +/* }}} */ + + +#endif /* PHP_HTTP_STD_DEFS_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 + */ \ No newline at end of file