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/curl - multi.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 104 137 75.9 %
Date: 2014-08-04 Functions: 14 15 93.3 %
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             :    | Author: Sterling Hughes <sterling@php.net>                           |
      16             :    +----------------------------------------------------------------------+
      17             : */
      18             : 
      19             : /* $Id$ */
      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          16 : PHP_FUNCTION(curl_multi_init)
      55             : {
      56             :         php_curlm *mh;
      57             :         
      58          16 :         if (zend_parse_parameters_none() == FAILURE) {
      59           0 :                 return;
      60             :         }
      61             : 
      62          16 :         mh = ecalloc(1, sizeof(php_curlm));
      63          16 :         mh->multi = curl_multi_init();
      64             : 
      65          16 :         zend_llist_init(&mh->easyh, sizeof(zval), _php_curl_multi_cleanup_list, 0);
      66             : 
      67          16 :         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          24 : PHP_FUNCTION(curl_multi_add_handle)
      74             : {
      75             :         zval      *z_mh;
      76             :         zval      *z_ch;
      77             :         php_curlm *mh;
      78             :         php_curl  *ch;
      79             :         zval tmp_val;
      80             : 
      81          24 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rr", &z_mh, &z_ch) == FAILURE) {
      82           1 :                 return;
      83             :         }
      84             : 
      85          23 :         ZEND_FETCH_RESOURCE(mh, php_curlm *, &z_mh, -1, le_curl_multi_handle_name, le_curl_multi_handle);
      86          23 :         ZEND_FETCH_RESOURCE(ch, php_curl *, &z_ch, -1, le_curl_name, le_curl);
      87             : 
      88          23 :         _php_curl_cleanup_handle(ch);
      89             : 
      90             :         /* we want to create a copy of this zval that we store in the multihandle structure element "easyh" */
      91          23 :         tmp_val = *z_ch;
      92             :         zval_copy_ctor(&tmp_val);
      93             : 
      94          23 :         zend_llist_add_element(&mh->easyh, &tmp_val);
      95             : 
      96          23 :         RETURN_LONG((long) curl_multi_add_handle(mh->multi, ch->cp));     
      97             : }
      98             : /* }}} */
      99             : 
     100          23 : void _php_curl_multi_cleanup_list(void *data) /* {{{ */
     101             : {
     102          23 :         zval *z_ch = (zval *)data;
     103             :         php_curl *ch;
     104             :         TSRMLS_FETCH();
     105             : 
     106          23 :         if (!z_ch) {
     107           0 :                 return;
     108             :         }
     109             :         
     110          23 :         ch = (php_curl *) zend_fetch_resource(&z_ch TSRMLS_CC, -1, le_curl_name, NULL, 1, le_curl);
     111          23 :         if (!ch) {
     112           0 :                 return;
     113             :         }
     114             : 
     115          23 :         zend_list_delete(Z_LVAL_P(z_ch));
     116             : }
     117             : /* }}} */
     118             : 
     119             : /* Used internally as comparison routine passed to zend_list_del_element */
     120          23 : static int curl_compare_resources( zval *z1, zval **z2 ) /* {{{ */
     121             : {
     122          69 :         return (Z_TYPE_P( z1 ) == Z_TYPE_PP( z2 ) && 
     123          23 :                         Z_TYPE_P( z1 ) == IS_RESOURCE     &&
     124          23 :                         Z_LVAL_P( z1 ) == Z_LVAL_PP( z2 ) );
     125             : }
     126             : /* }}} */
     127             : 
     128             : /* {{{ proto int curl_multi_remove_handle(resource mh, resource ch)
     129             :    Remove a multi handle from a set of cURL handles */
     130          22 : PHP_FUNCTION(curl_multi_remove_handle)
     131             : {
     132             :         zval      *z_mh;
     133             :         zval      *z_ch;
     134             :         php_curlm *mh;
     135             :         php_curl  *ch;
     136             : 
     137          22 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rr", &z_mh, &z_ch) == FAILURE) {
     138           0 :                 return;
     139             :         }
     140             : 
     141          22 :         ZEND_FETCH_RESOURCE(mh, php_curlm *, &z_mh, -1, le_curl_multi_handle_name, le_curl_multi_handle);
     142          22 :         ZEND_FETCH_RESOURCE(ch, php_curl *, &z_ch, -1, le_curl_name, le_curl);
     143             : 
     144             : 
     145             : 
     146          22 :         RETVAL_LONG((long) curl_multi_remove_handle(mh->multi, ch->cp));
     147          22 :         zend_llist_del_element( &mh->easyh, &z_ch, 
     148             :                                                         (int (*)(void *, void *)) curl_compare_resources );
     149             : 
     150             : }
     151             : /* }}} */
     152             : 
     153           1 : static void _make_timeval_struct(struct timeval *to, double timeout) /* {{{ */
     154             : {
     155             :         unsigned long conv;
     156             : 
     157           1 :         conv = (unsigned long) (timeout * 1000000.0);
     158           1 :         to->tv_sec = conv / 1000000;
     159           1 :         to->tv_usec = conv % 1000000;
     160           1 : }
     161             : /* }}} */
     162             : 
     163             : /* {{{ proto int curl_multi_select(resource mh[, double timeout])
     164             :    Get all the sockets associated with the cURL extension, which can then be "selected" */
     165           1 : PHP_FUNCTION(curl_multi_select)
     166             : {
     167             :         zval           *z_mh;
     168             :         php_curlm      *mh;
     169             :         fd_set          readfds;
     170             :         fd_set          writefds;
     171             :         fd_set          exceptfds;
     172             :         int             maxfd;
     173           1 :         double          timeout = 1.0;
     174             :         struct timeval  to;
     175             : 
     176           1 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|d", &z_mh, &timeout) == FAILURE) {
     177           0 :                 return;
     178             :         }
     179             : 
     180           1 :         ZEND_FETCH_RESOURCE(mh, php_curlm *, &z_mh, -1, le_curl_multi_handle_name, le_curl_multi_handle);
     181             : 
     182           1 :         _make_timeval_struct(&to, timeout);
     183             :         
     184           1 :         FD_ZERO(&readfds);
     185           1 :         FD_ZERO(&writefds);
     186           1 :         FD_ZERO(&exceptfds);
     187             : 
     188           1 :         curl_multi_fdset(mh->multi, &readfds, &writefds, &exceptfds, &maxfd);
     189           1 :         if (maxfd == -1) {
     190           1 :                 RETURN_LONG(-1);
     191             :         }
     192           0 :         RETURN_LONG(select(maxfd + 1, &readfds, &writefds, &exceptfds, &to));
     193             : }
     194             : /* }}} */
     195             : 
     196             : /* {{{ proto int curl_multi_exec(resource mh, int &still_running) 
     197             :    Run the sub-connections of the current cURL handle */
     198          77 : PHP_FUNCTION(curl_multi_exec)
     199             : {
     200             :         zval      *z_mh;
     201             :         zval      *z_still_running;
     202             :         php_curlm *mh;
     203             :         int        still_running;
     204             :         int        result;
     205             : 
     206          77 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rz", &z_mh, &z_still_running) == FAILURE) {
     207           0 :                 return;
     208             :         }
     209             : 
     210          77 :         ZEND_FETCH_RESOURCE(mh, php_curlm *, &z_mh, -1, le_curl_multi_handle_name, le_curl_multi_handle);
     211             : 
     212             :         {
     213             :                 zend_llist_position pos;
     214             :                 php_curl *ch;
     215             :                 zval    *pz_ch;
     216             : 
     217         315 :                 for(pz_ch = (zval *)zend_llist_get_first_ex(&mh->easyh, &pos); pz_ch;
     218         161 :                         pz_ch = (zval *)zend_llist_get_next_ex(&mh->easyh, &pos)) {
     219             : 
     220         161 :                         ZEND_FETCH_RESOURCE(ch, php_curl *, &pz_ch, -1, le_curl_name, le_curl);
     221         161 :                         _php_curl_verify_handlers(ch, 1 TSRMLS_CC);
     222             :                 }
     223             :         }
     224             : 
     225          81 :         convert_to_long_ex(&z_still_running);
     226          77 :         still_running = Z_LVAL_P(z_still_running);
     227          77 :         result = curl_multi_perform(mh->multi, &still_running);
     228          77 :         ZVAL_LONG(z_still_running, still_running);
     229             : 
     230          77 :         RETURN_LONG(result);
     231             : }
     232             : /* }}} */
     233             : 
     234             : /* {{{ proto string curl_multi_getcontent(resource ch)
     235             :    Return the content of a cURL handle if CURLOPT_RETURNTRANSFER is set */
     236          13 : PHP_FUNCTION(curl_multi_getcontent)
     237             : {
     238             :         zval     *z_ch;
     239             :         php_curl *ch;
     240             : 
     241          13 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &z_ch) == FAILURE) {
     242           4 :                 return;
     243             :         }
     244             : 
     245           9 :         ZEND_FETCH_RESOURCE(ch, php_curl *, &z_ch, -1, le_curl_name, le_curl);
     246             : 
     247           9 :         if (ch->handlers->write->method == PHP_CURL_RETURN && ch->handlers->write->buf.len > 0) {
     248           9 :                 smart_str_0(&ch->handlers->write->buf);
     249           9 :                 RETURN_STRINGL(ch->handlers->write->buf.c, ch->handlers->write->buf.len, 1);
     250             :         }
     251             : 
     252           0 :         RETURN_EMPTY_STRING();
     253             : }
     254             : /* }}} */
     255             : 
     256             : /* {{{ proto array curl_multi_info_read(resource mh [, long msgs_in_queue])
     257             :    Get information about the current transfers */
     258           0 : PHP_FUNCTION(curl_multi_info_read)
     259             : {
     260             :         zval      *z_mh;
     261             :         php_curlm *mh;
     262             :         CURLMsg   *tmp_msg;
     263             :         int        queued_msgs;
     264           0 :         zval      *zmsgs_in_queue = NULL;
     265             : 
     266           0 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|z", &z_mh, &zmsgs_in_queue) == FAILURE) {
     267           0 :                 return;
     268             :         }
     269             : 
     270           0 :         ZEND_FETCH_RESOURCE(mh, php_curlm *, &z_mh, -1, le_curl_multi_handle_name, le_curl_multi_handle);
     271             : 
     272           0 :         tmp_msg = curl_multi_info_read(mh->multi, &queued_msgs);
     273           0 :         if (tmp_msg == NULL) {
     274           0 :                 RETURN_FALSE;
     275             :         }
     276           0 :         if (zmsgs_in_queue) {
     277           0 :                 zval_dtor(zmsgs_in_queue);
     278           0 :                 ZVAL_LONG(zmsgs_in_queue, queued_msgs);
     279             :         }
     280             : 
     281           0 :         array_init(return_value);
     282           0 :         add_assoc_long(return_value, "msg", tmp_msg->msg);
     283           0 :         add_assoc_long(return_value, "result", tmp_msg->data.result);
     284             : 
     285             :         /* find the original easy curl handle */
     286             :         {
     287             :                 zend_llist_position pos;
     288             :                 php_curl *ch;
     289             :                 zval    *pz_ch;
     290             : 
     291             :                 /* search the list of easy handles hanging off the multi-handle */
     292           0 :                 for(pz_ch = (zval *)zend_llist_get_first_ex(&mh->easyh, &pos); pz_ch;
     293           0 :                         pz_ch = (zval *)zend_llist_get_next_ex(&mh->easyh, &pos)) {
     294           0 :                         ZEND_FETCH_RESOURCE(ch, php_curl *, &pz_ch, -1, le_curl_name, le_curl);
     295           0 :                         if (ch->cp == tmp_msg->easy_handle) {
     296             : 
     297             :                                 /* we are adding a reference to the underlying php_curl
     298             :                                    resource, so we need to add one to the resource's refcount 
     299             :                                    in order to ensure it doesn't get destroyed when the 
     300             :                                    underlying curl easy handle goes out of scope.
     301             :                                    Normally you would call zval_copy_ctor( pz_ch ), or
     302             :                                    SEPARATE_ZVAL, but those create new zvals, which is already
     303             :                                    being done in add_assoc_resource */
     304             : 
     305           0 :                                 zend_list_addref( Z_RESVAL_P( pz_ch ) );
     306             : 
     307             :                                 /* add_assoc_resource automatically creates a new zval to 
     308             :                                    wrap the "resource" represented by the current pz_ch */
     309             : 
     310           0 :                                 add_assoc_resource(return_value, "handle", Z_RESVAL_P(pz_ch));
     311             : 
     312           0 :                                 break;
     313             :                         }
     314             :                 }
     315             :         }
     316             : }
     317             : /* }}} */
     318             : 
     319             : /* {{{ proto void curl_multi_close(resource mh)
     320             :    Close a set of cURL handles */
     321          14 : PHP_FUNCTION(curl_multi_close)
     322             : {
     323             :         zval      *z_mh;
     324             :         php_curlm *mh;
     325             : 
     326          14 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &z_mh) == FAILURE) {
     327           0 :                 return;
     328             :         }
     329             : 
     330          14 :         ZEND_FETCH_RESOURCE(mh, php_curlm *, &z_mh, -1, le_curl_multi_handle_name, le_curl_multi_handle);
     331             : 
     332          14 :         zend_list_delete(Z_LVAL_P(z_mh));
     333             : }
     334             : /* }}} */
     335             : 
     336          16 : void _php_curl_multi_close(zend_rsrc_list_entry *rsrc TSRMLS_DC) /* {{{ */
     337             : {
     338          16 :         php_curlm *mh = (php_curlm *) rsrc->ptr;
     339          16 :         if (mh) {
     340             :                 zend_llist_position pos;
     341             :                 php_curl *ch;
     342             :                 zval    *pz_ch;
     343             : 
     344          33 :                 for(pz_ch = (zval *)zend_llist_get_first_ex(&mh->easyh, &pos); pz_ch;
     345           1 :                         pz_ch = (zval *)zend_llist_get_next_ex(&mh->easyh, &pos)) {
     346             : 
     347           1 :                         ch = (php_curl *) zend_fetch_resource(&pz_ch TSRMLS_CC, -1, le_curl_name, NULL, 1, le_curl);
     348           1 :                         _php_curl_verify_handlers(ch, 0 TSRMLS_CC);
     349             :                 }
     350             : 
     351          16 :                 curl_multi_cleanup(mh->multi);
     352          16 :                 zend_llist_clean(&mh->easyh);
     353          16 :                 efree(mh);
     354          16 :                 rsrc->ptr = NULL;
     355             :         }
     356          16 : }
     357             : /* }}} */
     358             : 
     359             : #if LIBCURL_VERSION_NUM >= 0x070c00 /* Available since 7.12.0 */
     360             : /* {{{ proto bool curl_multi_strerror(int code)
     361             :          return string describing error code */
     362           2 : PHP_FUNCTION(curl_multi_strerror)
     363             : {
     364             :         long code;
     365             :         const char *str;
     366             : 
     367           2 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &code) == FAILURE) {
     368           0 :                 return;
     369             :         }
     370             : 
     371           2 :         str = curl_multi_strerror(code);
     372           2 :         if (str) {
     373           2 :                 RETURN_STRING(str, 1);
     374             :         } else {
     375           0 :                 RETURN_NULL();
     376             :         }
     377             : }
     378             : /* }}} */
     379             : #endif
     380             : 
     381             : #if LIBCURL_VERSION_NUM >= 0x070f04 /* 7.15.4 */
     382           2 : static int _php_curl_multi_setopt(php_curlm *mh, long option, zval **zvalue, zval *return_value TSRMLS_DC) /* {{{ */
     383             : { 
     384           2 :         CURLMcode error = CURLM_OK;
     385             : 
     386           2 :         switch (option) {
     387             : #if LIBCURL_VERSION_NUM >= 0x071000 /* 7.16.0 */
     388             :                 case CURLMOPT_PIPELINING:
     389             : #endif
     390             : #if LIBCURL_VERSION_NUM >= 0x071003 /* 7.16.3 */
     391             :                 case CURLMOPT_MAXCONNECTS:
     392             : #endif
     393           1 :                         convert_to_long_ex(zvalue);
     394           1 :                         error = curl_multi_setopt(mh->multi, option, Z_LVAL_PP(zvalue));
     395           1 :                         break;
     396             : 
     397             :                 default:
     398           1 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid curl multi configuration option");
     399           1 :                         error = CURLM_UNKNOWN_OPTION;
     400             :                         break;
     401             :         }
     402             : 
     403           2 :         if (error != CURLM_OK) {
     404           1 :                 return 1;
     405             :         } else {
     406           1 :                 return 0;
     407             :         }
     408             : }
     409             : /* }}} */
     410             : 
     411             : /* {{{ proto int curl_multi_setopt(resource mh, int option, mixed value)
     412             :        Set an option for the curl multi handle */
     413           2 : PHP_FUNCTION(curl_multi_setopt)
     414             : {
     415             :         zval       *z_mh, **zvalue;
     416             :         long        options;
     417             :         php_curlm *mh;
     418             : 
     419           2 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlZ", &z_mh, &options, &zvalue) == FAILURE) {
     420           0 :                 return;
     421             :         }
     422             : 
     423           2 :         ZEND_FETCH_RESOURCE(mh, php_curlm *, &z_mh, -1, le_curl_multi_handle_name, le_curl_multi_handle);
     424             : 
     425           2 :         if (!_php_curl_multi_setopt(mh, options, zvalue, return_value TSRMLS_CC)) {
     426           1 :                 RETURN_TRUE;
     427             :         } else {
     428           1 :                 RETURN_FALSE;
     429             :         }
     430             : }
     431             : /* }}} */
     432             : #endif
     433             : 
     434             : #endif
     435             : 
     436             : /*
     437             :  * Local variables:
     438             :  * tab-width: 4
     439             :  * c-basic-offset: 4
     440             :  * End:
     441             :  * vim600: noet sw=4 ts=4 fdm=marker
     442             :  * vim<600: noet sw=4 ts=4
     443             :  */

Generated by: LCOV version 1.10

Generated at Mon, 04 Aug 2014 15:49:02 +0000 (15 days ago)

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