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

LTP GCOV extension - code coverage report
Current view: directory - curl - multi.c
Test: PHP Code Coverage
Date: 2009-11-21 Instrumented lines: 104
Code covered: 73.1 % Executed lines: 76
Legend: not executed executed

       1                 : /*
       2                 :    +----------------------------------------------------------------------+
       3                 :    | PHP Version 5                                                        |
       4                 :    +----------------------------------------------------------------------+
       5                 :    | Copyright (c) 1997-2009 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                 :    | Author: Sterling Hughes <sterling@php.net>                           |
      16                 :    +----------------------------------------------------------------------+
      17                 : */
      18                 : 
      19                 : /* $Id: multi.c 272370 2008-12-31 11:15:49Z sebastian $ */
      20                 : 
      21                 : #define ZEND_INCLUDE_FULL_WINDOWS_HEADERS
      22                 : 
      23                 : #ifdef HAVE_CONFIG_H
      24                 : #include "config.h"
      25                 : #endif
      26                 : 
      27                 : #include "php.h"
      28                 : 
      29                 : #if HAVE_CURL
      30                 : 
      31                 : #include "php_curl.h"
      32                 : 
      33                 : #include <curl/curl.h>
      34                 : #include <curl/multi.h>
      35                 : 
      36                 : #ifdef HAVE_SYS_SELECT_H
      37                 : #include <sys/select.h>
      38                 : #endif
      39                 : 
      40                 : #ifdef HAVE_SYS_TIME_H
      41                 : #include <sys/time.h>
      42                 : #endif
      43                 : 
      44                 : #ifdef HAVE_SYS_TYPES_H
      45                 : #include <sys/types.h>
      46                 : #endif
      47                 : 
      48                 : #ifdef HAVE_UNISTD_H
      49                 : #include <unistd.h>
      50                 : #endif
      51                 : 
      52                 : /* {{{ proto resource curl_multi_init(void)
      53                 :    Returns a new cURL multi handle */
      54                 : PHP_FUNCTION(curl_multi_init)
      55              11 : {
      56                 :         php_curlm *mh;
      57                 :         
      58              11 :         if (zend_parse_parameters_none() == FAILURE) {
      59               0 :                 return;
      60                 :         }
      61                 : 
      62              11 :         mh = ecalloc(1, sizeof(php_curlm));
      63              11 :         mh->multi = curl_multi_init();
      64                 : 
      65              11 :         zend_llist_init(&mh->easyh, sizeof(zval), _php_curl_multi_cleanup_list, 0);
      66                 : 
      67              11 :         ZEND_REGISTER_RESOURCE(return_value, mh, le_curl_multi_handle);
      68                 : }
      69                 : /* }}} */
      70                 : 
      71                 : /* {{{ proto int curl_multi_add_handle(resource mh, resource ch)
      72                 :    Add a normal cURL handle to a cURL multi handle */
      73                 : PHP_FUNCTION(curl_multi_add_handle)
      74              16 : {
      75                 :         zval      *z_mh;
      76                 :         zval      *z_ch;
      77                 :         php_curlm *mh;
      78                 :         php_curl  *ch;
      79                 :         zval tmp_val;
      80                 : 
      81              16 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rr", &z_mh, &z_ch) == FAILURE) {
      82               1 :                 return;
      83                 :         }
      84                 : 
      85              15 :         ZEND_FETCH_RESOURCE(mh, php_curlm *, &z_mh, -1, le_curl_multi_handle_name, le_curl_multi_handle);
      86              15 :         ZEND_FETCH_RESOURCE(ch, php_curl *, &z_ch, -1, le_curl_name, le_curl);
      87                 : 
      88              15 :         _php_curl_cleanup_handle(ch);
      89              15 :         ch->uses++;
      90                 : 
      91                 :         /* we want to create a copy of this zval that we store in the multihandle structure element "easyh" */
      92              15 :         tmp_val = *z_ch;
      93              15 :         zval_copy_ctor(&tmp_val);
      94                 : 
      95              15 :         zend_llist_add_element(&mh->easyh, &tmp_val);
      96                 : 
      97              15 :         RETURN_LONG((long) curl_multi_add_handle(mh->multi, ch->cp));     
      98                 : }
      99                 : /* }}} */
     100                 : 
     101                 : void _php_curl_multi_cleanup_list(void *data) /* {{{ */
     102              15 : {
     103              15 :         zval *z_ch = (zval *)data;
     104                 :         php_curl *ch;
     105                 :         TSRMLS_FETCH();
     106                 : 
     107              15 :         if (!z_ch) {
     108               0 :                 return;
     109                 :         }
     110                 :         
     111              15 :         ch = (php_curl *) zend_fetch_resource(&z_ch TSRMLS_CC, -1, le_curl_name, NULL, 1, le_curl);
     112              15 :         if (!ch) {
     113               0 :                 return;
     114                 :         }
     115                 : 
     116              15 :         if (ch->uses) {      
     117               1 :                 ch->uses--;
     118                 :         } else {
     119              14 :                 zend_list_delete(Z_LVAL_P(z_ch));
     120                 :         }
     121                 : }
     122                 : /* }}} */
     123                 : 
     124                 : /* Used internally as comparison routine passed to zend_list_del_element */
     125                 : static int curl_compare_resources( zval *z1, zval **z2 ) /* {{{ */
     126              15 : {
     127              15 :         return (Z_TYPE_P( z1 ) == Z_TYPE_PP( z2 ) && 
     128                 :             Z_TYPE_P( z1 ) == IS_RESOURCE     &&
     129                 :             Z_LVAL_P( z1 ) == Z_LVAL_PP( z2 ) );
     130                 : }
     131                 : /* }}} */
     132                 : 
     133                 : /* {{{ proto int curl_multi_remove_handle(resource mh, resource ch)
     134                 :    Remove a multi handle from a set of cURL handles */
     135                 : PHP_FUNCTION(curl_multi_remove_handle)
     136              14 : {
     137                 :         zval      *z_mh;
     138                 :         zval      *z_ch;
     139                 :         php_curlm *mh;
     140                 :         php_curl  *ch;
     141                 : 
     142              14 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rr", &z_mh, &z_ch) == FAILURE) {
     143               0 :                 return;
     144                 :         }
     145                 : 
     146              14 :         ZEND_FETCH_RESOURCE(mh, php_curlm *, &z_mh, -1, le_curl_multi_handle_name, le_curl_multi_handle);
     147              14 :         ZEND_FETCH_RESOURCE(ch, php_curl *, &z_ch, -1, le_curl_name, le_curl);
     148                 : 
     149              14 :         --ch->uses;
     150                 : 
     151              14 :         zend_llist_del_element( &mh->easyh, &z_ch, 
     152                 :                                                         (int (*)(void *, void *)) curl_compare_resources );
     153                 :         
     154              14 :         RETURN_LONG((long) curl_multi_remove_handle(mh->multi, ch->cp));
     155                 : }
     156                 : /* }}} */
     157                 : 
     158                 : static void _make_timeval_struct(struct timeval *to, double timeout) /* {{{ */
     159               1 : {
     160                 :         unsigned long conv;
     161                 : 
     162               1 :         conv = (unsigned long) (timeout * 1000000.0);
     163               1 :         to->tv_sec = conv / 1000000;
     164               1 :         to->tv_usec = conv % 1000000;
     165               1 : }
     166                 : /* }}} */
     167                 : 
     168                 : /* {{{ proto int curl_multi_select(resource mh[, double timeout])
     169                 :    Get all the sockets associated with the cURL extension, which can then be "selected" */
     170                 : PHP_FUNCTION(curl_multi_select)
     171               1 : {
     172                 :         zval           *z_mh;
     173                 :         php_curlm      *mh;
     174                 :         fd_set          readfds;
     175                 :         fd_set          writefds;
     176                 :         fd_set          exceptfds;
     177                 :         int             maxfd;
     178               1 :         double          timeout = 1.0;
     179                 :         struct timeval  to;
     180                 : 
     181               1 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|d", &z_mh, &timeout) == FAILURE) {
     182               0 :                 return;
     183                 :         }
     184                 : 
     185               1 :         ZEND_FETCH_RESOURCE(mh, php_curlm *, &z_mh, -1, le_curl_multi_handle_name, le_curl_multi_handle);
     186                 : 
     187               1 :         _make_timeval_struct(&to, timeout);
     188                 :         
     189               1 :         FD_ZERO(&readfds);
     190               1 :         FD_ZERO(&writefds);
     191               1 :         FD_ZERO(&exceptfds);
     192                 : 
     193               1 :         curl_multi_fdset(mh->multi, &readfds, &writefds, &exceptfds, &maxfd);
     194               1 :         RETURN_LONG(select(maxfd + 1, &readfds, &writefds, &exceptfds, &to));
     195                 : }
     196                 : /* }}} */
     197                 : 
     198                 : /* {{{ proto int curl_multi_exec(resource mh, int &still_running) 
     199                 :    Run the sub-connections of the current cURL handle */
     200                 : PHP_FUNCTION(curl_multi_exec)
     201             411 : {
     202                 :         zval      *z_mh;
     203                 :         zval      *z_still_running;
     204                 :         php_curlm *mh;
     205                 :         int        still_running;
     206                 :         int        result;
     207                 : 
     208             411 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rz", &z_mh, &z_still_running) == FAILURE) {
     209               0 :                 return;
     210                 :         }
     211                 : 
     212             411 :         ZEND_FETCH_RESOURCE(mh, php_curlm *, &z_mh, -1, le_curl_multi_handle_name, le_curl_multi_handle);
     213                 : 
     214             411 :         convert_to_long_ex(&z_still_running);
     215             411 :         still_running = Z_LVAL_P(z_still_running);
     216             411 :         result = curl_multi_perform(mh->multi, &still_running);
     217             411 :         ZVAL_LONG(z_still_running, still_running);
     218                 : 
     219             411 :         RETURN_LONG(result);
     220                 : }
     221                 : /* }}} */
     222                 : 
     223                 : /* {{{ proto string curl_multi_getcontent(resource ch)
     224                 :    Return the content of a cURL handle if CURLOPT_RETURNTRANSFER is set */
     225                 : PHP_FUNCTION(curl_multi_getcontent)
     226              13 : {
     227                 :         zval     *z_ch;
     228                 :         php_curl *ch;
     229                 : 
     230              13 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &z_ch) == FAILURE) {
     231               4 :                 return;
     232                 :         }
     233                 : 
     234               9 :         ZEND_FETCH_RESOURCE(ch, php_curl *, &z_ch, -1, le_curl_name, le_curl);
     235                 : 
     236               9 :         if (ch->handlers->write->method == PHP_CURL_RETURN && ch->handlers->write->buf.len > 0) {
     237               9 :                 smart_str_0(&ch->handlers->write->buf);
     238               9 :                 RETURN_STRINGL(ch->handlers->write->buf.c, ch->handlers->write->buf.len, 1);
     239                 :         }
     240                 : }
     241                 : /* }}} */
     242                 : 
     243                 : /* {{{ proto array curl_multi_info_read(resource mh [, long msgs_in_queue])
     244                 :    Get information about the current transfers */
     245                 : PHP_FUNCTION(curl_multi_info_read)
     246               0 : {
     247                 :         zval      *z_mh;
     248                 :         php_curlm *mh;
     249                 :         CURLMsg   *tmp_msg;
     250                 :         int        queued_msgs;
     251               0 :         zval      *zmsgs_in_queue = NULL;
     252                 : 
     253               0 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|z", &z_mh, &zmsgs_in_queue) == FAILURE) {
     254               0 :                 return;
     255                 :         }
     256                 : 
     257               0 :         ZEND_FETCH_RESOURCE(mh, php_curlm *, &z_mh, -1, le_curl_multi_handle_name, le_curl_multi_handle);
     258                 : 
     259               0 :         tmp_msg = curl_multi_info_read(mh->multi, &queued_msgs);
     260               0 :         if (tmp_msg == NULL) {
     261               0 :                 RETURN_FALSE;
     262                 :         }
     263               0 :         if (zmsgs_in_queue) {
     264               0 :                 zval_dtor(zmsgs_in_queue);
     265               0 :                 ZVAL_LONG(zmsgs_in_queue, queued_msgs);
     266                 :         }
     267                 : 
     268               0 :         array_init(return_value);
     269               0 :         add_assoc_long(return_value, "msg", tmp_msg->msg);
     270               0 :         add_assoc_long(return_value, "result", tmp_msg->data.result);
     271                 : 
     272                 :         /* find the original easy curl handle */
     273                 :         {
     274                 :                 zend_llist_position pos;
     275                 :                 php_curl *ch;
     276                 :                 zval    *pz_ch;
     277                 : 
     278                 :                 /* search the list of easy handles hanging off the multi-handle */
     279               0 :                 for(pz_ch = (zval *)zend_llist_get_first_ex(&mh->easyh, &pos); pz_ch;
     280               0 :                         pz_ch = (zval *)zend_llist_get_next_ex(&mh->easyh, &pos)) {
     281               0 :                         ZEND_FETCH_RESOURCE(ch, php_curl *, &pz_ch, -1, le_curl_name, le_curl);
     282               0 :                         if (ch->cp == tmp_msg->easy_handle) {
     283                 : 
     284                 :                                 /* we are adding a reference to the underlying php_curl
     285                 :                                    resource, so we need to add one to the resource's refcount 
     286                 :                                    in order to ensure it doesn't get destroyed when the 
     287                 :                                    underlying curl easy handle goes out of scope.
     288                 :                                    Normally you would call zval_copy_ctor( pz_ch ), or
     289                 :                                    SEPARATE_ZVAL, but those create new zvals, which is already
     290                 :                                    being done in add_assoc_resource */
     291                 : 
     292               0 :                                 zend_list_addref( Z_RESVAL_P( pz_ch ) );
     293                 : 
     294                 :                                 /* add_assoc_resource automatically creates a new zval to 
     295                 :                                    wrap the "resource" represented by the current pz_ch */
     296                 : 
     297               0 :                                 add_assoc_resource(return_value, "handle", Z_RESVAL_P(pz_ch));
     298                 : 
     299               0 :                                 break;
     300                 :                         }
     301                 :                 }
     302                 :         }
     303                 : }
     304                 : /* }}} */
     305                 : 
     306                 : /* {{{ proto void curl_multi_close(resource mh)
     307                 :    Close a set of cURL handles */
     308                 : PHP_FUNCTION(curl_multi_close)
     309              10 : {
     310                 :         zval      *z_mh;
     311                 :         php_curlm *mh;
     312                 : 
     313              10 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &z_mh) == FAILURE) {
     314               0 :                 return;
     315                 :         }
     316                 : 
     317              10 :         ZEND_FETCH_RESOURCE(mh, php_curlm *, &z_mh, -1, le_curl_multi_handle_name, le_curl_multi_handle);
     318                 : 
     319              10 :         zend_list_delete(Z_LVAL_P(z_mh));
     320                 : }
     321                 : /* }}} */
     322                 : 
     323                 : void _php_curl_multi_close(zend_rsrc_list_entry *rsrc TSRMLS_DC) /* {{{ */
     324              11 : {
     325              11 :         php_curlm *mh = (php_curlm *) rsrc->ptr;
     326              11 :         if (mh) {
     327              11 :                 curl_multi_cleanup(mh->multi);
     328              11 :                 zend_llist_clean(&mh->easyh);
     329              11 :                 efree(mh);
     330              11 :                 rsrc->ptr = NULL;
     331                 :         }
     332              11 : }
     333                 : /* }}} */
     334                 : 
     335                 : #endif
     336                 : 
     337                 : /*
     338                 :  * Local variables:
     339                 :  * tab-width: 4
     340                 :  * c-basic-offset: 4
     341                 :  * End:
     342                 :  * vim600: noet sw=4 ts=4 fdm=marker
     343                 :  * vim<600: noet sw=4 ts=4
     344                 :  */

Generated by: LTP GCOV extension version 1.5

Generated at Sat, 21 Nov 2009 12:26:58 +0000 (3 days ago)

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