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

Generated by: LCOV version 1.10

Generated at Thu, 16 Oct 2014 05:27:12 +0000 (6 days ago)

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