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

Generated by: LCOV version 1.10

Generated at Sat, 30 May 2015 05:52:57 +0000 (16 hours ago)

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