LCOV - code coverage report
Current view: top level - http - php_http_misc.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 96 106 90.6 %
Date: 2014-11-03 12:21:11 Functions: 10 10 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :     +--------------------------------------------------------------------+
       3             :     | PECL :: http                                                       |
       4             :     +--------------------------------------------------------------------+
       5             :     | Redistribution and use in source and binary forms, with or without |
       6             :     | modification, are permitted provided that the conditions mentioned |
       7             :     | in the accompanying LICENSE file are met.                          |
       8             :     +--------------------------------------------------------------------+
       9             :     | Copyright (c) 2004-2014, Michael Wallner <mike@php.net>            |
      10             :     +--------------------------------------------------------------------+
      11             : */
      12             : 
      13             : #include "php_http_api.h"
      14             : 
      15             : #include <ext/standard/php_lcg.h>
      16             : #include <zend_exceptions.h>
      17             : 
      18             : /* SLEEP */
      19             : 
      20        1858 : void php_http_sleep(double s)
      21             : {
      22             : #if defined(PHP_WIN32)
      23             :         Sleep((DWORD) PHP_HTTP_MSEC(s));
      24             : #elif defined(HAVE_USLEEP)
      25        1858 :         usleep(PHP_HTTP_USEC(s));
      26             : #elif defined(HAVE_NANOSLEEP)
      27             :         struct timespec req, rem;
      28             : 
      29             :         req.tv_sec = (time_t) s;
      30             :         req.tv_nsec = PHP_HTTP_NSEC(s) % PHP_HTTP_NANOSEC;
      31             : 
      32             :         while (nanosleep(&req, &rem) && (errno == EINTR) && (PHP_HTTP_NSEC(rem.tv_sec) + rem.tv_nsec) > PHP_HTTP_NSEC(PHP_HTTP_DIFFSEC))) {
      33             :                 req.tv_sec = rem.tv_sec;
      34             :                 req.tv_nsec = rem.tv_nsec;
      35             :         }
      36             : #else
      37             :         struct timeval timeout;
      38             : 
      39             :         timeout.tv_sec = (time_t) s;
      40             :         timeout.tv_usec = PHP_HTTP_USEC(s) % PHP_HTTP_MCROSEC;
      41             : 
      42             :         select(0, NULL, NULL, NULL, &timeout);
      43             : #endif
      44        1858 : }
      45             : 
      46             : 
      47             : /* STRING UTILITIES */
      48             : 
      49          14 : int php_http_match(const char *haystack_str, const char *needle_str, int flags)
      50             : {
      51          14 :         int result = 0;
      52             : 
      53          14 :         if (!haystack_str || !needle_str) {
      54           0 :                 return result;
      55             :         }
      56             : 
      57          14 :         if (flags & PHP_HTTP_MATCH_FULL) {
      58           3 :                 if (flags & PHP_HTTP_MATCH_CASE) {
      59           2 :                         result = !strcmp(haystack_str, needle_str);
      60             :                 } else {
      61           1 :                         result = !strcasecmp(haystack_str, needle_str);
      62             :                 }
      63             :         } else {
      64             :                 const char *found;
      65          11 :                 char *haystack = estrdup(haystack_str), *needle = estrdup(needle_str);
      66             : 
      67          11 :                 if (flags & PHP_HTTP_MATCH_CASE) {
      68           2 :                         found = zend_memnstr(haystack, needle, strlen(needle), haystack+strlen(haystack));
      69             :                 } else {
      70           9 :                         found = php_stristr(haystack, needle, strlen(haystack), strlen(needle));
      71             :                 }
      72             : 
      73          11 :                 if (found) {
      74           9 :                         if (!(flags & PHP_HTTP_MATCH_WORD)
      75           9 :                         ||      (       (found == haystack || !PHP_HTTP_IS_CTYPE(alnum, *(found - 1)))
      76           8 :                                 &&      (!*(found + strlen(needle)) || !PHP_HTTP_IS_CTYPE(alnum, *(found + strlen(needle))))
      77             :                                 )
      78             :                         ) {
      79           8 :                                 result = 1;
      80             :                         }
      81             :                 }
      82             : 
      83          11 :                 STR_FREE(haystack);
      84          11 :                 STR_FREE(needle);
      85             :         }
      86             : 
      87          14 :         return result;
      88             : }
      89             : 
      90        1108 : char *php_http_pretty_key(register char *key, size_t key_len, zend_bool uctitle, zend_bool xhyphen)
      91             : {
      92        1108 :         size_t i = 1;
      93             :         int wasalpha;
      94             : 
      95        1108 :         if (key && key_len) {
      96        1108 :                 if ((wasalpha = PHP_HTTP_IS_CTYPE(alpha, key[0]))) {
      97        1105 :                         key[0] = (char) (uctitle ? PHP_HTTP_TO_CTYPE(upper, key[0]) : PHP_HTTP_TO_CTYPE(lower, key[0]));
      98             :                 }
      99        1108 :                 PHP_HTTP_DUFF(key_len,
     100             :                         if (PHP_HTTP_IS_CTYPE(alpha, key[i])) {
     101             :                                 key[i] = (char) (((!wasalpha) && uctitle) ? PHP_HTTP_TO_CTYPE(upper, key[i]) : PHP_HTTP_TO_CTYPE(lower, key[i]));
     102             :                                 wasalpha = 1;
     103             :                         } else {
     104             :                                 if (xhyphen && (key[i] == '_')) {
     105             :                                         key[i] = '-';
     106             :                                 }
     107             :                                 wasalpha = 0;
     108             :                         }
     109             :                         ++i;
     110             :                 );
     111             :         }
     112        1108 :         return key;
     113             : }
     114             : 
     115             : 
     116           1 : size_t php_http_boundary(char *buf, size_t buf_len TSRMLS_DC)
     117             : {
     118           1 :         return snprintf(buf, buf_len, "%15.15F", sapi_get_request_time(TSRMLS_C) * php_combined_lcg(TSRMLS_C));
     119             : }
     120             : 
     121          55 : int php_http_select_str(const char *cmp, int argc, ...)
     122             : {
     123             :         va_list argv;
     124          55 :         int match = -1;
     125             : 
     126          55 :         if (cmp && argc > 0) {
     127             :                 int i;
     128             : 
     129          55 :                 va_start(argv, argc);
     130          82 :                 for (i = 0; i < argc; ++i) {
     131          81 :                         const char *test = va_arg(argv, const char *);
     132             : 
     133          81 :                         if (!strcasecmp(cmp, test)) {
     134          54 :                                 match = i;
     135          54 :                                 break;
     136             :                         }
     137             :                 }
     138          55 :                 va_end(argv);
     139             :         }
     140             : 
     141          55 :         return match;
     142             : }
     143             : 
     144             : 
     145             : /* ARRAYS */
     146             : 
     147          17 : unsigned php_http_array_list(HashTable *ht TSRMLS_DC, unsigned argc, ...)
     148             : {
     149             :         HashPosition pos;
     150          17 :         unsigned argl = 0;
     151             :         va_list argv;
     152             : 
     153          17 :         va_start(argv, argc);
     154          62 :         for (   zend_hash_internal_pointer_reset_ex(ht, &pos);
     155          73 :                         SUCCESS == zend_hash_has_more_elements_ex(ht, &pos) && (argl < argc);
     156          28 :                         zend_hash_move_forward_ex(ht, &pos))
     157             :         {
     158          28 :                 zval **data, ***argp = (zval ***) va_arg(argv, zval ***);
     159             : 
     160          28 :                 if (SUCCESS == zend_hash_get_current_data_ex(ht, (void *) &data, &pos)) {
     161          28 :                         *argp = data;
     162          28 :                         ++argl;
     163             :                 }
     164             :         }
     165          17 :         va_end(argv);
     166             : 
     167          17 :         return argl;
     168             : }
     169             : 
     170           2 : void php_http_array_copy_strings(void *zpp)
     171             : {
     172           2 :         zval **zvpp = ((zval **) zpp);
     173             : 
     174           2 :         *zvpp = php_http_zsep(1, IS_STRING, *zvpp);
     175           2 : }
     176             : 
     177           4 : int php_http_array_apply_append_func(void *pDest TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key)
     178             : {
     179             :         int flags;
     180           4 :         char *key = NULL;
     181             :         HashTable *dst;
     182           4 :         zval **data = NULL, *value = *((zval **) pDest);
     183             : 
     184           4 :         dst = va_arg(args, HashTable *);
     185           4 :         flags = va_arg(args, int);
     186             : 
     187           4 :         if ((!(flags & ARRAY_JOIN_STRONLY)) || hash_key->nKeyLength) {
     188           4 :                 if ((flags & ARRAY_JOIN_PRETTIFY) && hash_key->nKeyLength) {
     189           0 :                         key = php_http_pretty_key(estrndup(hash_key->arKey, hash_key->nKeyLength - 1), hash_key->nKeyLength - 1, 1, 1);
     190           0 :                         zend_hash_find(dst, key, hash_key->nKeyLength, (void *) &data);
     191             :                 } else {
     192           4 :                         zend_hash_quick_find(dst, hash_key->arKey, hash_key->nKeyLength, hash_key->h, (void *) &data);
     193             :                 }
     194             : 
     195           4 :                 if (flags & ARRAY_JOIN_STRINGIFY) {
     196           4 :                         value = php_http_zsep(1, IS_STRING, value);
     197             :                 } else {
     198           0 :                         Z_ADDREF_P(value);
     199             :                 }
     200             : 
     201           4 :                 if (data) {
     202           0 :                         if (Z_TYPE_PP(data) != IS_ARRAY) {
     203           0 :                                 convert_to_array(*data);
     204             :                         }
     205           0 :                         add_next_index_zval(*data, value);
     206           4 :                 } else if (key) {
     207           0 :                         zend_symtable_update(dst, key, hash_key->nKeyLength, &value, sizeof(zval *), NULL);
     208             :                 } else {
     209           4 :                         zend_hash_quick_add(dst, hash_key->arKey, hash_key->nKeyLength, hash_key->h, &value, sizeof(zval *), NULL);
     210             :                 }
     211             : 
     212           4 :                 if (key) {
     213           0 :                         efree(key);
     214             :                 }
     215             :         }
     216             : 
     217           4 :         return ZEND_HASH_APPLY_KEEP;
     218             : }
     219             : 
     220          12 : int php_http_array_apply_merge_func(void *pDest TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key)
     221             : {
     222             :         int flags;
     223          12 :         char *key = NULL;
     224             :         HashTable *dst;
     225          12 :         zval *value = *((zval **) pDest);
     226             : 
     227          12 :         dst = va_arg(args, HashTable *);
     228          12 :         flags = va_arg(args, int);
     229             : 
     230          12 :         if ((!(flags & ARRAY_JOIN_STRONLY)) || hash_key->nKeyLength) {
     231          12 :                 if (flags & ARRAY_JOIN_STRINGIFY) {
     232           0 :                         value = php_http_zsep(1, IS_STRING, value);
     233             :                 } else {
     234          12 :                         Z_ADDREF_P(value);
     235             :                 }
     236             : 
     237          12 :                 if ((flags & ARRAY_JOIN_PRETTIFY) && hash_key->nKeyLength) {
     238           5 :                         key = php_http_pretty_key(estrndup(hash_key->arKey, hash_key->nKeyLength - 1), hash_key->nKeyLength - 1, 1, 1);
     239           5 :                         zend_hash_update(dst, key, hash_key->nKeyLength, (void *) &value, sizeof(zval *), NULL);
     240           5 :                         efree(key);
     241             :                 } else {
     242           7 :                         zend_hash_quick_update(dst, hash_key->arKey, hash_key->nKeyLength, hash_key->h, (void *) &value, sizeof(zval *), NULL);
     243             :                 }
     244             :         }
     245             : 
     246          12 :         return ZEND_HASH_APPLY_KEEP;
     247             : }
     248             : 
     249             : /* PASS CALLBACK */
     250             : 
     251           4 : size_t php_http_pass_fcall_callback(void *cb_arg, const char *str, size_t len)
     252             : {
     253           4 :         php_http_pass_fcall_arg_t *fcd = cb_arg;
     254             :         zval *zdata;
     255             :         TSRMLS_FETCH_FROM_CTX(fcd->ts);
     256             : 
     257           4 :         MAKE_STD_ZVAL(zdata);
     258           4 :         ZVAL_STRINGL(zdata, str, len, 1);
     259           4 :         if (SUCCESS == zend_fcall_info_argn(&fcd->fci TSRMLS_CC, 2, &fcd->fcz, &zdata)) {
     260           4 :                 zend_fcall_info_call(&fcd->fci, &fcd->fcc, NULL, NULL TSRMLS_CC);
     261           4 :                 zend_fcall_info_args_clear(&fcd->fci, 0);
     262             :         }
     263           4 :         zval_ptr_dtor(&zdata);
     264           4 :         return len;
     265             : }
     266             : 
     267             : 
     268             : /* ZEND */
     269             : 
     270             : /*
     271             :  * Local variables:
     272             :  * tab-width: 4
     273             :  * c-basic-offset: 4
     274             :  * End:
     275             :  * vim600: noet sw=4 ts=4 fdm=marker
     276             :  * vim<600: noet sw=4 ts=4
     277             :  */

Generated by: LCOV version 1.11