PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/standard - http.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 56 117 47.9 %
Date: 2014-04-18 Functions: 2 2 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 5                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1997-2013 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$ */
      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           6 : 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 TSRMLS_DC)
      33             : {
      34           6 :         char *key = NULL, *ekey, *newprefix, *p;
      35             :         int arg_sep_len, key_len, ekey_len, key_type, newprefix_len;
      36             :         ulong idx;
      37           6 :         zval **zdata = NULL, *copyzval;
      38             : 
      39           6 :         if (!ht) {
      40           0 :                 return FAILURE;
      41             :         }
      42             : 
      43           6 :         if (ht->nApplyCount > 0) {
      44             :                 /* Prevent recursion */
      45           0 :                 return SUCCESS;
      46             :         }
      47             : 
      48           6 :         if (!arg_sep) {
      49           5 :                 arg_sep = INI_STR("arg_separator.output");
      50           5 :                 if (!arg_sep || !strlen(arg_sep)) {
      51           0 :                         arg_sep = URL_DEFAULT_ARG_SEP;
      52             :                 }
      53             :         }
      54           6 :         arg_sep_len = strlen(arg_sep);
      55             : 
      56          30 :         for (zend_hash_internal_pointer_reset(ht);
      57          24 :                 (key_type = zend_hash_get_current_key_ex(ht, &key, &key_len, &idx, 0, NULL)) != HASH_KEY_NON_EXISTANT;
      58             :                 zend_hash_move_forward(ht)
      59          18 :         ) {
      60          18 :                 if (key_type == HASH_KEY_IS_STRING && key_len && key[key_len-1] == '\0') {
      61             :                         /* We don't want that trailing NULL */
      62          15 :                         key_len -= 1;
      63             :                 }
      64             : 
      65             :                 /* handling for private & protected object properties */
      66          18 :                 if (key && *key == '\0' && type != NULL) {
      67             :                         char *tmp;
      68             : 
      69           4 :                         zend_object *zobj = zend_objects_get_address(type TSRMLS_CC);
      70           4 :                         if (zend_check_property_access(zobj, key, key_len-1 TSRMLS_CC) != SUCCESS) {
      71             :                                 /* private or protected property access outside of the class */
      72           2 :                                 continue;
      73             :                         }
      74           2 :                         zend_unmangle_property_name(key, key_len-1, &tmp, &key);
      75           2 :                         key_len = strlen(key);          
      76             :                 }
      77             : 
      78          16 :                 if (zend_hash_get_current_data_ex(ht, (void **)&zdata, NULL) == FAILURE || !zdata || !(*zdata)) {
      79           0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error traversing form data array");
      80           0 :                         return FAILURE;
      81             :                 }
      82          16 :                 if (Z_TYPE_PP(zdata) == IS_ARRAY || Z_TYPE_PP(zdata) == IS_OBJECT) {
      83           0 :                         if (key_type == HASH_KEY_IS_STRING) {
      84           0 :                                 ekey = php_url_encode(key, key_len, &ekey_len);
      85           0 :                                 newprefix_len = key_suffix_len + ekey_len + key_prefix_len + 3 /* %5B */;
      86           0 :                                 newprefix = emalloc(newprefix_len + 1);
      87           0 :                                 p = newprefix;
      88             : 
      89           0 :                                 if (key_prefix) {
      90           0 :                                         memcpy(p, key_prefix, key_prefix_len);
      91           0 :                                         p += key_prefix_len;
      92             :                                 }
      93             : 
      94           0 :                                 memcpy(p, ekey, ekey_len);
      95           0 :                                 p += ekey_len;
      96           0 :                                 efree(ekey);
      97             : 
      98           0 :                                 if (key_suffix) {
      99           0 :                                         memcpy(p, key_suffix, key_suffix_len);
     100           0 :                                         p += key_suffix_len;
     101             :                                 }
     102           0 :                                 *(p++) = '%';
     103           0 :                                 *(p++) = '5';
     104           0 :                                 *(p++) = 'B';
     105           0 :                                 *p = '\0';
     106             :                         } else {
     107             :                                 /* Is an integer key */
     108           0 :                                 ekey_len = spprintf(&ekey, 0, "%ld", idx);
     109           0 :                                 newprefix_len = key_prefix_len + num_prefix_len + ekey_len + key_suffix_len + 3 /* %5B */;
     110           0 :                                 newprefix = emalloc(newprefix_len + 1);
     111           0 :                                 p = newprefix;
     112             : 
     113           0 :                                 if (key_prefix) {
     114           0 :                                         memcpy(p, key_prefix, key_prefix_len);
     115           0 :                                         p += key_prefix_len;
     116             :                                 }
     117             : 
     118           0 :                                 memcpy(p, num_prefix, num_prefix_len);
     119           0 :                                 p += num_prefix_len;
     120             : 
     121           0 :                                 memcpy(p, ekey, ekey_len);
     122           0 :                                 p += ekey_len;
     123           0 :                                 efree(ekey);
     124             : 
     125           0 :                                 if (key_suffix) {
     126           0 :                                         memcpy(p, key_suffix, key_suffix_len);
     127           0 :                                         p += key_suffix_len;
     128             :                                 }
     129           0 :                                 *(p++) = '%';
     130           0 :                                 *(p++) = '5';
     131           0 :                                 *(p++) = 'B';
     132           0 :                                 *p = '\0';
     133             :                         }
     134           0 :                         ht->nApplyCount++;
     135           0 :                         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 TSRMLS_CC);
     136           0 :                         ht->nApplyCount--;
     137           0 :                         efree(newprefix);
     138          16 :                 } else if (Z_TYPE_PP(zdata) == IS_NULL || Z_TYPE_PP(zdata) == IS_RESOURCE) {
     139             :                         /* Skip these types */
     140           0 :                         continue;
     141             :                 } else {
     142          16 :                         if (formstr->len) {
     143          11 :                                 smart_str_appendl(formstr, arg_sep, arg_sep_len);
     144             :                         }
     145             :                         /* Simple key=value */
     146          16 :                         smart_str_appendl(formstr, key_prefix, key_prefix_len);
     147          16 :                         if (key_type == HASH_KEY_IS_STRING) {
     148          13 :                                 ekey = php_url_encode(key, key_len, &ekey_len);
     149          13 :                                 smart_str_appendl(formstr, ekey, ekey_len);
     150          13 :                                 efree(ekey);
     151             :                         } else {
     152             :                                 /* Numeric key */
     153           3 :                                 if (num_prefix) {
     154           2 :                                         smart_str_appendl(formstr, num_prefix, num_prefix_len);
     155             :                                 }
     156           3 :                                 ekey_len = spprintf(&ekey, 0, "%ld", idx);
     157           3 :                                 smart_str_appendl(formstr, ekey, ekey_len);
     158           3 :                                 efree(ekey);
     159             :                         }
     160          16 :                         smart_str_appendl(formstr, key_suffix, key_suffix_len);
     161          16 :                         smart_str_appendl(formstr, "=", 1);
     162          16 :                         switch (Z_TYPE_PP(zdata)) {
     163             :                                 case IS_STRING:
     164          13 :                                         ekey = php_url_encode(Z_STRVAL_PP(zdata), Z_STRLEN_PP(zdata), &ekey_len);
     165          13 :                                         break;
     166             :                                 case IS_LONG:
     167             :                                 case IS_BOOL:
     168           3 :                                         ekey_len = spprintf(&ekey, 0, "%ld", Z_LVAL_PP(zdata));
     169           3 :                                         break;
     170             :                                 case IS_DOUBLE:
     171           0 :                                         ekey_len = spprintf(&ekey, 0, "%.*G", (int) EG(precision), Z_DVAL_PP(zdata));
     172           0 :                                         break;
     173             :                                 default:
     174             :                                         /* fall back on convert to string */
     175           0 :                                         MAKE_STD_ZVAL(copyzval);
     176           0 :                                         *copyzval = **zdata;
     177           0 :                                         zval_copy_ctor(copyzval);
     178           0 :                                         convert_to_string_ex(&copyzval);
     179           0 :                                         ekey = php_url_encode(Z_STRVAL_P(copyzval), Z_STRLEN_P(copyzval), &ekey_len);
     180           0 :                                         zval_ptr_dtor(&copyzval);
     181             :                         }
     182          16 :                         smart_str_appendl(formstr, ekey, ekey_len);
     183          16 :                         efree(ekey);
     184             :                 }
     185             :         }
     186             : 
     187           6 :         return SUCCESS;
     188             : }
     189             : /* }}} */
     190             : 
     191             : /* {{{ proto string http_build_query(mixed formdata [, string prefix [, string arg_separator]])
     192             :    Generates a form-encoded query string from an associative array or object. */
     193           6 : PHP_FUNCTION(http_build_query)
     194             : {
     195             :         zval *formdata;
     196           6 :         char *prefix = NULL, *arg_sep=NULL;
     197           6 :         int arg_sep_len = 0, prefix_len = 0;
     198           6 :         smart_str formstr = {0};
     199             :         
     200             : 
     201           6 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|ss", &formdata, &prefix, &prefix_len, &arg_sep, &arg_sep_len) != SUCCESS) {
     202           0 :                 RETURN_FALSE;
     203             :         }
     204             : 
     205           6 :         if (Z_TYPE_P(formdata) != IS_ARRAY && Z_TYPE_P(formdata) != IS_OBJECT) {
     206           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Parameter 1 expected to be Array or Object.  Incorrect value given");
     207           0 :                 RETURN_FALSE;
     208             :         }
     209             : 
     210           6 :         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 TSRMLS_CC) == FAILURE) {
     211           0 :                 if (formstr.c) {
     212           0 :                         efree(formstr.c);
     213             :                 }
     214           0 :                 RETURN_FALSE;
     215             :         }
     216             : 
     217           6 :         if (!formstr.c) {
     218           1 :                 RETURN_EMPTY_STRING();
     219             :         }
     220             : 
     221           5 :         smart_str_0(&formstr);
     222             :         
     223           5 :         RETURN_STRINGL(formstr.c, formstr.len, 0);
     224             : }
     225             : /* }}} */
     226             : 
     227             : /*
     228             :  * Local variables:
     229             :  * tab-width: 4
     230             :  * c-basic-offset: 4
     231             :  * End:
     232             :  * vim600: sw=4 ts=4 fdm=marker
     233             :  * vim<600: sw=4 ts=4
     234             :  */
     235             : 

Generated by: LCOV version 1.10

Generated at Fri, 18 Apr 2014 07:01:36 +0000 (6 days ago)

Copyright © 2005-2014 The PHP Group
All rights reserved.