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

Generated by: LCOV version 1.10

Generated at Sun, 06 Apr 2014 17:30:59 +0000 (9 days ago)

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