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: 2015-01-26 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-2015 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(), "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             : 
     104          24 :         if (!z_ch) {
     105           0 :                 return;
     106             :         }
     107             : 
     108          24 :         ch = zend_fetch_resource(z_ch, -1, le_curl_name, NULL, 1, le_curl);
     109          24 :         if (!ch) {
     110           0 :                 return;
     111             :         }
     112             : 
     113          24 :         zend_list_close(Z_RES_P(z_ch));
     114             : }
     115             : /* }}} */
     116             : 
     117             : /* Used internally as comparison routine passed to zend_list_del_element */
     118          50 : static int curl_compare_resources( zval *z1, zval *z2 ) /* {{{ */
     119             : {
     120          50 :         return (Z_TYPE_P(z1) == Z_TYPE_P(z2) &&
     121             :                         Z_TYPE_P(z1) == IS_RESOURCE &&
     122           0 :                         Z_RES_P(z1) == Z_RES_P(z2));
     123             : }
     124             : /* }}} */
     125             : 
     126             : /* {{{ proto int curl_multi_remove_handle(resource mh, resource ch)
     127             :    Remove a multi handle from a set of cURL handles */
     128          23 : PHP_FUNCTION(curl_multi_remove_handle)
     129             : {
     130             :         zval      *z_mh;
     131             :         zval      *z_ch;
     132             :         php_curlm *mh;
     133             :         php_curl  *ch;
     134             : 
     135          23 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "rr", &z_mh, &z_ch) == FAILURE) {
     136           0 :                 return;
     137             :         }
     138             : 
     139          23 :         ZEND_FETCH_RESOURCE(mh, php_curlm *, z_mh, -1, le_curl_multi_handle_name, le_curl_multi_handle);
     140          23 :         ZEND_FETCH_RESOURCE(ch, php_curl *, z_ch, -1, le_curl_name, le_curl);
     141             : 
     142          23 :         RETVAL_LONG((zend_long)curl_multi_remove_handle(mh->multi, ch->cp));
     143          23 :         zend_llist_del_element(&mh->easyh, &z_ch, (int (*)(void *, void *))curl_compare_resources);
     144             : 
     145             : }
     146             : /* }}} */
     147             : 
     148           1 : static void _make_timeval_struct(struct timeval *to, double timeout) /* {{{ */
     149             : {
     150             :         unsigned long conv;
     151             : 
     152           1 :         conv = (unsigned long) (timeout * 1000000.0);
     153           1 :         to->tv_sec = conv / 1000000;
     154           1 :         to->tv_usec = conv % 1000000;
     155           1 : }
     156             : /* }}} */
     157             : 
     158             : /* {{{ proto int curl_multi_select(resource mh[, double timeout])
     159             :    Get all the sockets associated with the cURL extension, which can then be "selected" */
     160           1 : PHP_FUNCTION(curl_multi_select)
     161             : {
     162             :         zval           *z_mh;
     163             :         php_curlm      *mh;
     164             :         fd_set          readfds;
     165             :         fd_set          writefds;
     166             :         fd_set          exceptfds;
     167             :         int             maxfd;
     168           1 :         double          timeout = 1.0;
     169             :         struct timeval  to;
     170             : 
     171           1 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|d", &z_mh, &timeout) == FAILURE) {
     172           0 :                 return;
     173             :         }
     174             : 
     175           1 :         ZEND_FETCH_RESOURCE(mh, php_curlm *, z_mh, -1, le_curl_multi_handle_name, le_curl_multi_handle);
     176             : 
     177           1 :         _make_timeval_struct(&to, timeout);
     178             : 
     179           1 :         FD_ZERO(&readfds);
     180           1 :         FD_ZERO(&writefds);
     181           1 :         FD_ZERO(&exceptfds);
     182             : 
     183           1 :         curl_multi_fdset(mh->multi, &readfds, &writefds, &exceptfds, &maxfd);
     184           1 :         if (maxfd == -1) {
     185           1 :                 RETURN_LONG(-1);
     186             :         }
     187           0 :         RETURN_LONG(select(maxfd + 1, &readfds, &writefds, &exceptfds, &to));
     188             : }
     189             : /* }}} */
     190             : 
     191             : /* {{{ proto int curl_multi_exec(resource mh, int &still_running)
     192             :    Run the sub-connections of the current cURL handle */
     193          84 : PHP_FUNCTION(curl_multi_exec)
     194             : {
     195             :         zval      *z_mh;
     196             :         zval      *z_still_running;
     197             :         php_curlm *mh;
     198             :         int        still_running;
     199             :         int        result;
     200             : 
     201          84 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "rz/", &z_mh, &z_still_running) == FAILURE) {
     202           0 :                 return;
     203             :         }
     204             : 
     205          84 :         ZEND_FETCH_RESOURCE(mh, php_curlm *, z_mh, -1, le_curl_multi_handle_name, le_curl_multi_handle);
     206             : 
     207             :         {
     208             :                 zend_llist_position pos;
     209             :                 php_curl *ch;
     210             :                 zval    *pz_ch;
     211             : 
     212         336 :                 for(pz_ch = (zval *)zend_llist_get_first_ex(&mh->easyh, &pos); pz_ch;
     213         168 :                         pz_ch = (zval *)zend_llist_get_next_ex(&mh->easyh, &pos)) {
     214             : 
     215         168 :                         ZEND_FETCH_RESOURCE(ch, php_curl *, pz_ch, -1, le_curl_name, le_curl);
     216         168 :                         _php_curl_verify_handlers(ch, 1);
     217             :                 }
     218             :         }
     219             : 
     220         168 :         convert_to_long_ex(z_still_running);
     221          84 :         still_running = Z_LVAL_P(z_still_running);
     222          84 :         result = curl_multi_perform(mh->multi, &still_running);
     223          84 :         ZVAL_LONG(z_still_running, still_running);
     224             : 
     225          84 :         RETURN_LONG(result);
     226             : }
     227             : /* }}} */
     228             : 
     229             : /* {{{ proto string curl_multi_getcontent(resource ch)
     230             :    Return the content of a cURL handle if CURLOPT_RETURNTRANSFER is set */
     231          14 : PHP_FUNCTION(curl_multi_getcontent)
     232             : {
     233             :         zval     *z_ch;
     234             :         php_curl *ch;
     235             : 
     236          14 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &z_ch) == FAILURE) {
     237           4 :                 return;
     238             :         }
     239             : 
     240          10 :         ZEND_FETCH_RESOURCE(ch, php_curl *, z_ch, -1, le_curl_name, le_curl);
     241             : 
     242          10 :         if (ch->handlers->write->method == PHP_CURL_RETURN) {
     243           9 :                 if (!ch->handlers->write->buf.s) {
     244           0 :                         RETURN_EMPTY_STRING();
     245             :                 }
     246           9 :                 smart_str_0(&ch->handlers->write->buf);
     247          18 :                 RETURN_STR(zend_string_copy(ch->handlers->write->buf.s));
     248             :         }
     249             : 
     250           1 :         RETURN_NULL();
     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(), "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             : 
     293           0 :                         ZEND_FETCH_RESOURCE(ch, php_curl *, pz_ch, -1, le_curl_name, le_curl);
     294           0 :                         if (ch->cp == tmp_msg->easy_handle) {
     295             : 
     296             :                                 /* we are adding a reference to the underlying php_curl
     297             :                                    resource, so we need to add one to the resource's refcount
     298             :                                    in order to ensure it doesn't get destroyed when the
     299             :                                    underlying curl easy handle goes out of scope.
     300             :                                    Normally you would call zval_copy_ctor( pz_ch ), or
     301             :                                    SEPARATE_ZVAL, but those create new zvals, which is already
     302             :                                    being done in add_assoc_resource */
     303             :                                 Z_ADDREF_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_zval(return_value, "handle", 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          15 : PHP_FUNCTION(curl_multi_close)
     320             : {
     321             :         zval      *z_mh;
     322             :         php_curlm *mh;
     323             : 
     324          15 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &z_mh) == FAILURE) {
     325           0 :                 return;
     326             :         }
     327             : 
     328          15 :         ZEND_FETCH_RESOURCE(mh, php_curlm *, z_mh, -1, le_curl_multi_handle_name, le_curl_multi_handle);
     329             : 
     330          15 :         zend_list_close(Z_RES_P(z_mh));
     331             : }
     332             : /* }}} */
     333             : 
     334          17 : void _php_curl_multi_close(zend_resource *rsrc) /* {{{ */
     335             : {
     336          17 :         php_curlm *mh = (php_curlm *)rsrc->ptr;
     337          17 :         if (mh) {
     338             :                 zend_llist_position pos;
     339             :                 php_curl *ch;
     340             :                 zval    *pz_ch;
     341             : 
     342          58 :                 for (pz_ch = (zval *)zend_llist_get_first_ex(&mh->easyh, &pos); pz_ch;
     343          24 :                         pz_ch = (zval *)zend_llist_get_next_ex(&mh->easyh, &pos)) {
     344             : 
     345          24 :                         ch = (php_curl *) zend_fetch_resource(pz_ch, -1, le_curl_name, NULL, 1, le_curl);
     346          24 :                         _php_curl_verify_handlers(ch, 0);
     347             :                 }
     348             : 
     349          17 :                 curl_multi_cleanup(mh->multi);
     350          17 :                 zend_llist_clean(&mh->easyh);
     351          17 :                 efree(mh);
     352          17 :                 rsrc->ptr = NULL;
     353             :         }
     354          17 : }
     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           2 : PHP_FUNCTION(curl_multi_strerror)
     361             : {
     362             :         zend_long code;
     363             :         const char *str;
     364             : 
     365           2 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &code) == FAILURE) {
     366           0 :                 return;
     367             :         }
     368             : 
     369           2 :         str = curl_multi_strerror(code);
     370           2 :         if (str) {
     371           4 :                 RETURN_STRING(str);
     372             :         } else {
     373           0 :                 RETURN_NULL();
     374             :         }
     375             : }
     376             : /* }}} */
     377             : #endif
     378             : 
     379             : #if LIBCURL_VERSION_NUM >= 0x070f04 /* 7.15.4 */
     380           2 : static int _php_curl_multi_setopt(php_curlm *mh, zend_long option, zval *zvalue, zval *return_value) /* {{{ */
     381             : {
     382           2 :         CURLMcode error = CURLM_OK;
     383             : 
     384           2 :         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           1 :                         convert_to_long_ex(zvalue);
     392           1 :                         error = curl_multi_setopt(mh->multi, option, Z_LVAL_P(zvalue));
     393           1 :                         break;
     394             : 
     395             :                 default:
     396           1 :                         php_error_docref(NULL, E_WARNING, "Invalid curl multi configuration option");
     397           1 :                         error = CURLM_UNKNOWN_OPTION;
     398             :                         break;
     399             :         }
     400             : 
     401           2 :         if (error != CURLM_OK) {
     402           1 :                 return 1;
     403             :         } else {
     404           1 :                 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           2 : PHP_FUNCTION(curl_multi_setopt)
     412             : {
     413             :         zval       *z_mh, *zvalue;
     414             :         zend_long        options;
     415             :         php_curlm *mh;
     416             : 
     417           2 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "rlz", &z_mh, &options, &zvalue) == FAILURE) {
     418           0 :                 return;
     419             :         }
     420             : 
     421           2 :         ZEND_FETCH_RESOURCE(mh, php_curlm *, z_mh, -1, le_curl_multi_handle_name, le_curl_multi_handle);
     422             : 
     423           2 :         if (!_php_curl_multi_setopt(mh, options, zvalue, return_value)) {
     424           1 :                 RETURN_TRUE;
     425             :         } else {
     426           1 :                 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 Mon, 26 Jan 2015 14:46:42 +0000 (19 hours ago)

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