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: 105 137 76.6 %
Date: 2014-12-13 Functions: 14 15 93.3 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Sat, 13 Dec 2014 06:16:11 +0000 (6 days ago)

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