File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / php / ext / standard / http.c
Revision 1.1.1.4 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Sun Jun 15 20:03:57 2014 UTC (10 years, 1 month ago) by misho
Branches: php, MAIN
CVS tags: v5_4_29, HEAD
php 5.4.29

    1: /*
    2:    +----------------------------------------------------------------------+
    3:    | PHP Version 5                                                        |
    4:    +----------------------------------------------------------------------+
    5:    | Copyright (c) 1997-2014 The PHP Group                                |
    6:    +----------------------------------------------------------------------+
    7:    | This source file is subject to version 3.01 of the PHP license,      |
    8:    | that is bundled with this package in the file LICENSE, and is        |
    9:    | available through the world-wide-web at the following url:           |
   10:    | http://www.php.net/license/3_01.txt                                  |
   11:    | If you did not receive a copy of the PHP license and are unable to   |
   12:    | obtain it through the world-wide-web, please send a note to          |
   13:    | license@php.net so we can mail you a copy immediately.               |
   14:    +----------------------------------------------------------------------+
   15:    | Authors: Sara Golemon <pollita@php.net>                              |
   16:    +----------------------------------------------------------------------+
   17: */
   18: 
   19: /* $Id: http.c,v 1.1.1.4 2014/06/15 20:03:57 misho Exp $ */
   20: 
   21: #include "php_http.h"
   22: #include "php_ini.h"
   23: #include "url.h"
   24: 
   25: #define URL_DEFAULT_ARG_SEP "&"
   26: 
   27: /* {{{ php_url_encode_hash */
   28: PHPAPI int php_url_encode_hash_ex(HashTable *ht, smart_str *formstr,
   29: 				const char *num_prefix, int num_prefix_len,
   30: 				const char *key_prefix, int key_prefix_len,
   31: 				const char *key_suffix, int key_suffix_len,
   32: 			  zval *type, char *arg_sep, int enc_type TSRMLS_DC)
   33: {
   34: 	char *key = NULL;
   35: 	char *ekey, *newprefix, *p;
   36: 	int arg_sep_len, ekey_len, key_type, newprefix_len;
   37: 	uint key_len;
   38: 	ulong idx;
   39: 	zval **zdata = NULL, *copyzval;
   40: 
   41: 	if (!ht) {
   42: 		return FAILURE;
   43: 	}
   44: 
   45: 	if (ht->nApplyCount > 0) {
   46: 		/* Prevent recursion */
   47: 		return SUCCESS;
   48: 	}
   49: 
   50: 	if (!arg_sep) {
   51: 		arg_sep = INI_STR("arg_separator.output");
   52: 		if (!arg_sep || !strlen(arg_sep)) {
   53: 			arg_sep = URL_DEFAULT_ARG_SEP;
   54: 		}
   55: 	}
   56: 	arg_sep_len = strlen(arg_sep);
   57: 
   58: 	for (zend_hash_internal_pointer_reset(ht);
   59: 		(key_type = zend_hash_get_current_key_ex(ht, &key, &key_len, &idx, 0, NULL)) != HASH_KEY_NON_EXISTANT;
   60: 		zend_hash_move_forward(ht)
   61: 	) {
   62: 		if (key_type == HASH_KEY_IS_STRING && key_len && key[key_len-1] == '\0') {
   63: 			/* We don't want that trailing NULL */
   64: 			key_len -= 1;
   65: 		}
   66: 
   67: 		/* handling for private & protected object properties */
   68: 		if (key && *key == '\0' && type != NULL) {
   69: 			const char *tmp;
   70: 
   71: 			zend_object *zobj = zend_objects_get_address(type TSRMLS_CC);
   72: 			if (zend_check_property_access(zobj, key, key_len-1 TSRMLS_CC) != SUCCESS) {
   73: 				/* private or protected property access outside of the class */
   74: 				continue;
   75: 			}
   76: 			zend_unmangle_property_name(key, key_len-1, &tmp, (const char**)&key);
   77: 			key_len = strlen(key);		
   78: 		}
   79: 
   80: 		if (zend_hash_get_current_data_ex(ht, (void **)&zdata, NULL) == FAILURE || !zdata || !(*zdata)) {
   81: 			php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error traversing form data array");
   82: 			return FAILURE;
   83: 		}
   84: 		if (Z_TYPE_PP(zdata) == IS_ARRAY || Z_TYPE_PP(zdata) == IS_OBJECT) {
   85: 			if (key_type == HASH_KEY_IS_STRING) {
   86: 				if (enc_type == PHP_QUERY_RFC3986) {
   87: 					ekey = php_raw_url_encode(key, key_len, &ekey_len);
   88: 				} else {
   89: 					ekey = php_url_encode(key, key_len, &ekey_len);
   90: 				}
   91: 				newprefix_len = key_suffix_len + ekey_len + key_prefix_len + 3 /* %5B */;
   92: 				newprefix = emalloc(newprefix_len + 1);
   93: 				p = newprefix;
   94: 
   95: 				if (key_prefix) {
   96: 					memcpy(p, key_prefix, key_prefix_len);
   97: 					p += key_prefix_len;
   98: 				}
   99: 
  100: 				memcpy(p, ekey, ekey_len);
  101: 				p += ekey_len;
  102: 				efree(ekey);
  103: 
  104: 				if (key_suffix) {
  105: 					memcpy(p, key_suffix, key_suffix_len);
  106: 					p += key_suffix_len;
  107: 				}
  108: 				*(p++) = '%';
  109: 				*(p++) = '5';
  110: 				*(p++) = 'B';
  111: 				*p = '\0';
  112: 			} else {
  113: 				/* Is an integer key */
  114: 				ekey_len = spprintf(&ekey, 0, "%ld", idx);
  115: 				newprefix_len = key_prefix_len + num_prefix_len + ekey_len + key_suffix_len + 3 /* %5B */;
  116: 				newprefix = emalloc(newprefix_len + 1);
  117: 				p = newprefix;
  118: 
  119: 				if (key_prefix) {
  120: 					memcpy(p, key_prefix, key_prefix_len);
  121: 					p += key_prefix_len;
  122: 				}
  123: 
  124: 				memcpy(p, num_prefix, num_prefix_len);
  125: 				p += num_prefix_len;
  126: 
  127: 				memcpy(p, ekey, ekey_len);
  128: 				p += ekey_len;
  129: 				efree(ekey);
  130: 
  131: 				if (key_suffix) {
  132: 					memcpy(p, key_suffix, key_suffix_len);
  133: 					p += key_suffix_len;
  134: 				}
  135: 				*(p++) = '%';
  136: 				*(p++) = '5';
  137: 				*(p++) = 'B';
  138: 				*p = '\0';
  139: 			}
  140: 			ht->nApplyCount++;
  141: 			php_url_encode_hash_ex(HASH_OF(*zdata), formstr, NULL, 0, newprefix, newprefix_len, "%5D", 3, (Z_TYPE_PP(zdata) == IS_OBJECT ? *zdata : NULL), arg_sep, enc_type TSRMLS_CC);
  142: 			ht->nApplyCount--;
  143: 			efree(newprefix);
  144: 		} else if (Z_TYPE_PP(zdata) == IS_NULL || Z_TYPE_PP(zdata) == IS_RESOURCE) {
  145: 			/* Skip these types */
  146: 			continue;
  147: 		} else {
  148: 			if (formstr->len) {
  149: 				smart_str_appendl(formstr, arg_sep, arg_sep_len);
  150: 			}
  151: 			/* Simple key=value */
  152: 			smart_str_appendl(formstr, key_prefix, key_prefix_len);
  153: 			if (key_type == HASH_KEY_IS_STRING) {
  154: 				if (enc_type == PHP_QUERY_RFC3986) {
  155: 					ekey = php_raw_url_encode(key, key_len, &ekey_len);
  156: 				} else {
  157: 					ekey = php_url_encode(key, key_len, &ekey_len);
  158: 				}
  159: 				smart_str_appendl(formstr, ekey, ekey_len);
  160: 				efree(ekey);
  161: 			} else {
  162: 				/* Numeric key */
  163: 				if (num_prefix) {
  164: 					smart_str_appendl(formstr, num_prefix, num_prefix_len);
  165: 				}
  166: 				ekey_len = spprintf(&ekey, 0, "%ld", idx);
  167: 				smart_str_appendl(formstr, ekey, ekey_len);
  168: 				efree(ekey);
  169: 			}
  170: 			smart_str_appendl(formstr, key_suffix, key_suffix_len);
  171: 			smart_str_appendl(formstr, "=", 1);
  172: 			switch (Z_TYPE_PP(zdata)) {
  173: 				case IS_STRING:
  174: 					if (enc_type == PHP_QUERY_RFC3986) {
  175: 						ekey = php_raw_url_encode(Z_STRVAL_PP(zdata), Z_STRLEN_PP(zdata), &ekey_len);
  176: 					} else {
  177: 						ekey = php_url_encode(Z_STRVAL_PP(zdata), Z_STRLEN_PP(zdata), &ekey_len);						
  178: 					}
  179: 					break;
  180: 				case IS_LONG:
  181: 				case IS_BOOL:
  182: 					ekey_len = spprintf(&ekey, 0, "%ld", Z_LVAL_PP(zdata));
  183: 					break;
  184: 				case IS_DOUBLE:
  185: 					ekey_len = spprintf(&ekey, 0, "%.*G", (int) EG(precision), Z_DVAL_PP(zdata));
  186: 					break;
  187: 				default:
  188: 					/* fall back on convert to string */
  189: 					MAKE_STD_ZVAL(copyzval);
  190: 					*copyzval = **zdata;
  191: 					zval_copy_ctor(copyzval);
  192: 					convert_to_string_ex(&copyzval);
  193: 					if (enc_type == PHP_QUERY_RFC3986) {
  194: 						ekey = php_raw_url_encode(Z_STRVAL_P(copyzval), Z_STRLEN_P(copyzval), &ekey_len);
  195: 					} else {
  196: 						ekey = php_url_encode(Z_STRVAL_P(copyzval), Z_STRLEN_P(copyzval), &ekey_len);
  197: 					}
  198: 					zval_ptr_dtor(&copyzval);
  199: 			}
  200: 			smart_str_appendl(formstr, ekey, ekey_len);
  201: 			efree(ekey);
  202: 		}
  203: 	}
  204: 
  205: 	return SUCCESS;
  206: }
  207: /* }}} */
  208: 
  209: /* {{{ proto string http_build_query(mixed formdata [, string prefix [, string arg_separator [, int enc_type]]])
  210:    Generates a form-encoded query string from an associative array or object. */
  211: PHP_FUNCTION(http_build_query)
  212: {
  213: 	zval *formdata;
  214: 	char *prefix = NULL, *arg_sep=NULL;
  215: 	int arg_sep_len = 0, prefix_len = 0;
  216: 	smart_str formstr = {0};
  217: 	long enc_type = PHP_QUERY_RFC1738;
  218: 
  219: 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|ssl", &formdata, &prefix, &prefix_len, &arg_sep, &arg_sep_len, &enc_type) != SUCCESS) {
  220: 		RETURN_FALSE;
  221: 	}
  222: 
  223: 	if (Z_TYPE_P(formdata) != IS_ARRAY && Z_TYPE_P(formdata) != IS_OBJECT) {
  224: 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Parameter 1 expected to be Array or Object.  Incorrect value given");
  225: 		RETURN_FALSE;
  226: 	}
  227: 
  228: 	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), arg_sep, enc_type TSRMLS_CC) == FAILURE) {
  229: 		if (formstr.c) {
  230: 			efree(formstr.c);
  231: 		}
  232: 		RETURN_FALSE;
  233: 	}
  234: 
  235: 	if (!formstr.c) {
  236: 		RETURN_EMPTY_STRING();
  237: 	}
  238: 
  239: 	smart_str_0(&formstr);
  240: 	
  241: 	RETURN_STRINGL(formstr.c, formstr.len, 0);
  242: }
  243: /* }}} */
  244: 
  245: /*
  246:  * Local variables:
  247:  * tab-width: 4
  248:  * c-basic-offset: 4
  249:  * End:
  250:  * vim600: sw=4 ts=4 fdm=marker
  251:  * vim<600: sw=4 ts=4
  252:  */
  253: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>