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: 89 113 78.8 %
Date: 2014-11-22 Functions: 2 2 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 7                                                        |
       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, size_t num_prefix_len,
      30             :                                 const char *key_prefix, size_t key_prefix_len,
      31             :                                 const char *key_suffix, size_t key_suffix_len,
      32             :                           zval *type, char *arg_sep, int enc_type TSRMLS_DC)
      33             : {
      34          20 :         zend_string *key = NULL;
      35             :         char *newprefix, *p;
      36             :         const char *prop_name;
      37             :         size_t arg_sep_len, newprefix_len, prop_len;
      38             :         zend_ulong idx;
      39          20 :         zval *zdata = NULL, copyzval;
      40             : 
      41          20 :         if (!ht) {
      42           0 :                 return FAILURE;
      43             :         }
      44             : 
      45          20 :         if (ht->u.v.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         203 :         ZEND_HASH_FOREACH_KEY_VAL_IND(ht, idx, key, zdata) {
      59             :                 /* handling for private & protected object properties */
      60          61 :                 if (key) {
      61          44 :                         if (key->val[0] == '\0' && type != NULL) {
      62             :                                 const char *tmp;
      63             : 
      64           6 :                                 zend_object *zobj = Z_OBJ_P(type);
      65           6 :                                 if (zend_check_property_access(zobj, key TSRMLS_CC) != SUCCESS) {
      66             :                                         /* private or protected property access outside of the class */
      67           4 :                                         continue;
      68             :                                 }
      69           2 :                                 zend_unmangle_property_name_ex(key, &tmp, &prop_name, &prop_len);
      70             :                         } else {
      71          36 :                                 prop_name = key->val;
      72          36 :                                 prop_len = key->len;
      73             :                         }
      74             :                 } else {
      75          19 :                         prop_name = NULL;
      76          19 :                         prop_len = 0;
      77             :                 }
      78             : 
      79         114 :                 if (Z_TYPE_P(zdata) == IS_ARRAY || Z_TYPE_P(zdata) == IS_OBJECT) {
      80           8 :                         if (key) {
      81             :                                 zend_string *ekey;
      82           6 :                                 if (enc_type == PHP_QUERY_RFC3986) {
      83           0 :                                         ekey = php_raw_url_encode(prop_name, prop_len);
      84             :                                 } else {
      85           6 :                                         ekey = php_url_encode(prop_name, prop_len);
      86             :                                 }
      87           6 :                                 newprefix_len = key_suffix_len + ekey->len + key_prefix_len + 3 /* %5B */;
      88           6 :                                 newprefix = emalloc(newprefix_len + 1);
      89           6 :                                 p = newprefix;
      90             : 
      91           6 :                                 if (key_prefix) {
      92           4 :                                         memcpy(p, key_prefix, key_prefix_len);
      93           4 :                                         p += key_prefix_len;
      94             :                                 }
      95             : 
      96           6 :                                 memcpy(p, ekey->val, ekey->len);
      97           6 :                                 p += ekey->len;
      98             :                                 zend_string_free(ekey);
      99             : 
     100           6 :                                 if (key_suffix) {
     101           4 :                                         memcpy(p, key_suffix, key_suffix_len);
     102           4 :                                         p += key_suffix_len;
     103             :                                 }
     104           6 :                                 *(p++) = '%';
     105           6 :                                 *(p++) = '5';
     106           6 :                                 *(p++) = 'B';
     107           6 :                                 *p = '\0';
     108             :                         } else {
     109             :                                 char *ekey;
     110             :                                 size_t ekey_len;
     111             :                                 /* Is an integer key */
     112           2 :                                 ekey_len = spprintf(&ekey, 0, "%pd", idx);
     113           2 :                                 newprefix_len = key_prefix_len + num_prefix_len + ekey_len + key_suffix_len + 3 /* %5B */;
     114           2 :                                 newprefix = emalloc(newprefix_len + 1);
     115           2 :                                 p = newprefix;
     116             : 
     117           2 :                                 if (key_prefix) {
     118           0 :                                         memcpy(p, key_prefix, key_prefix_len);
     119           0 :                                         p += key_prefix_len;
     120             :                                 }
     121             : 
     122           2 :                                 memcpy(p, num_prefix, num_prefix_len);
     123           2 :                                 p += num_prefix_len;
     124             : 
     125           2 :                                 memcpy(p, ekey, ekey_len);
     126           2 :                                 p += ekey_len;
     127           2 :                                 efree(ekey);
     128             : 
     129           2 :                                 if (key_suffix) {
     130           0 :                                         memcpy(p, key_suffix, key_suffix_len);
     131           0 :                                         p += key_suffix_len;
     132             :                                 }
     133           2 :                                 *(p++) = '%';
     134           2 :                                 *(p++) = '5';
     135           2 :                                 *(p++) = 'B';
     136           2 :                                 *p = '\0';
     137             :                         }
     138           8 :                         if (ZEND_HASH_APPLY_PROTECTION(ht)) {
     139           8 :                                 ht->u.v.nApplyCount++;
     140             :                         }
     141          16 :                         php_url_encode_hash_ex(HASH_OF(zdata), formstr, NULL, 0, newprefix, newprefix_len, "%5D", 3, (Z_TYPE_P(zdata) == IS_OBJECT ? zdata : NULL), arg_sep, enc_type TSRMLS_CC);
     142           8 :                         if (ZEND_HASH_APPLY_PROTECTION(ht)) {
     143           8 :                                 ht->u.v.nApplyCount--;
     144             :                         }
     145           8 :                         efree(newprefix);
     146          98 :                 } else if (Z_TYPE_P(zdata) == IS_NULL || Z_TYPE_P(zdata) == IS_RESOURCE) {
     147             :                         /* Skip these types */
     148           0 :                         continue;
     149             :                 } else {
     150          49 :                         if (formstr->s) {
     151             :                                 smart_str_appendl(formstr, arg_sep, arg_sep_len);
     152             :                         }
     153             :                         /* Simple key=value */
     154             :                         smart_str_appendl(formstr, key_prefix, key_prefix_len);
     155          49 :                         if (key) {
     156             :                                 zend_string *ekey;
     157          32 :                                 if (enc_type == PHP_QUERY_RFC3986) {
     158           3 :                                         ekey = php_raw_url_encode(prop_name, prop_len);
     159             :                                 } else {
     160          29 :                                         ekey = php_url_encode(prop_name, prop_len);
     161             :                                 }
     162             :                                 smart_str_append(formstr, ekey);
     163             :                                 zend_string_free(ekey);
     164             :                         } else {
     165             :                                 /* Numeric key */
     166          17 :                                 if (num_prefix) {
     167             :                                         smart_str_appendl(formstr, num_prefix, num_prefix_len);
     168             :                                 }
     169          17 :                                 smart_str_append_long(formstr, idx);
     170             :                         }
     171             :                         smart_str_appendl(formstr, key_suffix, key_suffix_len);
     172             :                         smart_str_appendl(formstr, "=", 1);
     173          49 :                         switch (Z_TYPE_P(zdata)) {
     174             :                                 case IS_STRING: {
     175             :                                                 zend_string *ekey;
     176          38 :                                                 if (enc_type == PHP_QUERY_RFC3986) {
     177           3 :                                                         ekey = php_raw_url_encode(Z_STRVAL_P(zdata), Z_STRLEN_P(zdata));
     178             :                                                 } else {
     179          35 :                                                         ekey = php_url_encode(Z_STRVAL_P(zdata), Z_STRLEN_P(zdata));                                            
     180             :                                                 }
     181             :                                                 smart_str_append(formstr, ekey);
     182             :                                                 zend_string_free(ekey);
     183             :                                         }
     184          38 :                                         break;
     185             :                                 case IS_LONG:
     186          11 :                                         smart_str_append_long(formstr, Z_LVAL_P(zdata));
     187          11 :                                         break;
     188             :                                 case IS_FALSE:
     189             :                                         smart_str_appendl(formstr, "0", sizeof("0")-1);
     190           0 :                                         break;
     191             :                                 case IS_TRUE:
     192             :                                         smart_str_appendl(formstr, "1", sizeof("1")-1);
     193           0 :                                         break;
     194             :                                 case IS_DOUBLE:
     195             :                                         {
     196             :                                                 char *ekey;
     197             :                                                 size_t ekey_len;
     198           0 :                                                 ekey_len = spprintf(&ekey, 0, "%.*G", (int) EG(precision), Z_DVAL_P(zdata));
     199           0 :                                                 smart_str_appendl(formstr, ekey, ekey_len);
     200           0 :                                                 efree(ekey);
     201             :                                         }
     202           0 :                                         break;
     203             :                                 default:
     204             :                                         {
     205             :                                                 zend_string *ekey;
     206             :                                                 /* fall back on convert to string */
     207           0 :                                                 ZVAL_DUP(&copyzval, zdata);
     208           0 :                                                 convert_to_string_ex(&copyzval);
     209           0 :                                                 if (enc_type == PHP_QUERY_RFC3986) {
     210           0 :                                                         ekey = php_raw_url_encode(Z_STRVAL(copyzval), Z_STRLEN(copyzval));
     211             :                                                 } else {
     212           0 :                                                         ekey = php_url_encode(Z_STRVAL(copyzval), Z_STRLEN(copyzval));
     213             :                                                 }
     214             :                                                 smart_str_append(formstr, ekey);
     215           0 :                                                 zval_ptr_dtor(&copyzval);
     216             :                                                 zend_string_free(ekey);
     217             :                                         }
     218             :                         }
     219             :                 }
     220             :         } ZEND_HASH_FOREACH_END();
     221             : 
     222          20 :         return SUCCESS;
     223             : }
     224             : /* }}} */
     225             : 
     226             : /* {{{ proto string http_build_query(mixed formdata [, string prefix [, string arg_separator [, int enc_type]]])
     227             :    Generates a form-encoded query string from an associative array or object. */
     228          13 : PHP_FUNCTION(http_build_query)
     229             : {
     230             :         zval *formdata;
     231          13 :         char *prefix = NULL, *arg_sep=NULL;
     232          13 :         size_t arg_sep_len = 0, prefix_len = 0;
     233          13 :         smart_str formstr = {0};
     234          13 :         zend_long enc_type = PHP_QUERY_RFC1738;
     235             : 
     236          13 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|ssl", &formdata, &prefix, &prefix_len, &arg_sep, &arg_sep_len, &enc_type) != SUCCESS) {
     237           0 :                 RETURN_FALSE;
     238             :         }
     239             : 
     240          31 :         if (Z_TYPE_P(formdata) != IS_ARRAY && Z_TYPE_P(formdata) != IS_OBJECT) {
     241           1 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Parameter 1 expected to be Array or Object.  Incorrect value given");
     242           1 :                 RETURN_FALSE;
     243             :         }
     244             : 
     245          40 :         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, (int)enc_type TSRMLS_CC) == FAILURE) {
     246           0 :                 if (formstr.s) {
     247             :                         smart_str_free(&formstr);
     248             :                 }
     249           0 :                 RETURN_FALSE;
     250             :         }
     251             : 
     252          12 :         if (!formstr.s) {
     253           1 :                 RETURN_EMPTY_STRING();
     254             :         }
     255             : 
     256             :         smart_str_0(&formstr);
     257             :         
     258          11 :         RETURN_STR(formstr.s);
     259             : }
     260             : /* }}} */
     261             : 
     262             : /*
     263             :  * Local variables:
     264             :  * tab-width: 4
     265             :  * c-basic-offset: 4
     266             :  * End:
     267             :  * vim600: sw=4 ts=4 fdm=marker
     268             :  * vim<600: sw=4 ts=4
     269             :  */
     270             : 

Generated by: LCOV version 1.10

Generated at Sat, 22 Nov 2014 23:01:25 +0000 (6 hours ago)

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