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

LTP GCOV extension - code coverage report
Current view: directory - curl - streams.c
Test: PHP Code Coverage
Date: 2009-11-19 Instrumented lines: 201
Code covered: 80.1 % Executed lines: 161
Legend: not executed executed

       1                 : /*
       2                 :    +----------------------------------------------------------------------+
       3                 :    | PHP Version 5                                                        |
       4                 :    +----------------------------------------------------------------------+
       5                 :    | Copyright (c) 1997-2009 The PHP Group                                |
       6                 :    +----------------------------------------------------------------------+
       7                 :    | This source file is subject to version 3.01 of the PHP license,      |
       8                 :    | that is bundled with this package in the file LICENSE, and is        |
       9                 :    | available through the world-wide-web at the following url:           |
      10                 :    | http://www.php.net/license/3_01.txt                                  |
      11                 :    | If you did not receive a copy of the PHP license and are unable to   |
      12                 :    | obtain it through the world-wide-web, please send a note to          |
      13                 :    | license@php.net so we can mail you a copy immediately.               |
      14                 :    +----------------------------------------------------------------------+
      15                 :    | Author: Wez Furlong <wez@thebrainroom.com>                           |
      16                 :    +----------------------------------------------------------------------+
      17                 : */
      18                 : 
      19                 : /* $Id: streams.c 284748 2009-07-25 13:09:03Z jani $ */
      20                 : 
      21                 : /* This file implements cURL based wrappers.
      22                 :  * NOTE: If you are implementing your own streams that are intended to
      23                 :  * work independently of wrappers, this is not a good example to follow!
      24                 :  **/
      25                 : 
      26                 : #ifdef HAVE_CONFIG_H
      27                 : #include "config.h"
      28                 : #endif
      29                 : 
      30                 : #include "php.h"
      31                 : #include "php_memory_streams.h"
      32                 : 
      33                 : #if HAVE_CURL
      34                 : 
      35                 : #include <stdio.h>
      36                 : #include <string.h>
      37                 : 
      38                 : #ifdef PHP_WIN32
      39                 : #include <winsock2.h>
      40                 : #include <sys/types.h>
      41                 : #endif
      42                 : 
      43                 : #include <curl/curl.h>
      44                 : #include <curl/easy.h>
      45                 : 
      46                 : #define SMART_STR_PREALLOC 4096
      47                 : 
      48                 : #include "ext/standard/php_smart_str.h"
      49                 : #include "ext/standard/info.h"
      50                 : #include "ext/standard/file.h"
      51                 : #include "ext/standard/php_string.h"
      52                 : #include "php_curl.h"
      53                 : 
      54                 : static size_t on_data_available(char *data, size_t size, size_t nmemb, void *ctx)
      55              82 : {
      56              82 :         php_stream *stream = (php_stream *) ctx;
      57              82 :         php_curl_stream *curlstream = (php_curl_stream *) stream->abstract;
      58                 :         size_t wrote;
      59                 :         TSRMLS_FETCH();
      60                 : 
      61                 :         /* TODO: I'd like to deprecate this.
      62                 :          * This code is here because until we start getting real data, we don't know
      63                 :          * if we have had all of the headers 
      64                 :          * */
      65              82 :         if (curlstream->readbuffer.writepos == 0) {
      66                 :                 zval *sym;
      67                 : 
      68              23 :                 MAKE_STD_ZVAL(sym);
      69              23 :                 *sym = *curlstream->headers;
      70              23 :                 zval_copy_ctor(sym);
      71              23 :                 ZEND_SET_SYMBOL(EG(active_symbol_table), "http_response_header", sym);
      72                 :         }
      73                 :         
      74              82 :         php_stream_seek(curlstream->readbuffer.buf, curlstream->readbuffer.writepos, SEEK_SET);
      75              82 :         wrote = php_stream_write(curlstream->readbuffer.buf, data, size * nmemb);
      76              82 :         curlstream->readbuffer.writepos = php_stream_tell(curlstream->readbuffer.buf);
      77                 : 
      78              82 :         return wrote;
      79                 : }
      80                 : 
      81                 : /* cURL guarantees that headers are written as complete lines, with this function
      82                 :  * called once for each header */
      83                 : static size_t on_header_available(char *data, size_t size, size_t nmemb, void *ctx)
      84             233 : {
      85             233 :         size_t length = size * nmemb;
      86                 :         zval *header;
      87             233 :         php_stream *stream = (php_stream *) ctx;
      88             233 :         php_curl_stream *curlstream = (php_curl_stream *) stream->abstract;
      89                 :         TSRMLS_FETCH();
      90                 : 
      91             233 :         if (length < 2) {
      92                 :                 /* invalid header ? */
      93               0 :                 return length;
      94                 :         }
      95                 : 
      96             233 :         if (!(length == 2 && data[0] == '\r' && data[1] == '\n')) {
      97             201 :                 MAKE_STD_ZVAL(header);
      98             201 :                 Z_STRLEN_P(header) = length;
      99             201 :                 Z_STRVAL_P(header) = estrndup(data, length);
     100             201 :                 if (Z_STRVAL_P(header)[length-1] == '\n') {
     101             201 :                         Z_STRVAL_P(header)[length-1] = '\0';
     102             201 :                         Z_STRLEN_P(header)--;
     103                 :                         
     104             201 :                         if (Z_STRVAL_P(header)[length-2] == '\r') {
     105             201 :                                 Z_STRVAL_P(header)[length-2] = '\0';
     106             201 :                                 Z_STRLEN_P(header)--;
     107                 :                         }
     108                 :                 }
     109             201 :                 Z_TYPE_P(header) = IS_STRING;
     110             201 :                 zend_hash_next_index_insert(Z_ARRVAL_P(curlstream->headers), &header, sizeof(zval *), NULL);
     111                 :                 
     112                 :                 /* based on the header, we might need to trigger a notification */
     113             201 :                 if (!strncasecmp(data, "Location: ", 10)) {
     114              13 :                         php_stream_notify_info(stream->context, PHP_STREAM_NOTIFY_REDIRECTED, data + 10, 0);
     115             188 :                 } else if (!strncasecmp(data, "Content-Type: ", 14)) {
     116              12 :                         php_stream_notify_info(stream->context, PHP_STREAM_NOTIFY_MIME_TYPE_IS, data + 14, 0);
     117             176 :                 } else if (!strncasecmp(data, "Context-Length: ", 16)) {
     118               0 :                         php_stream_notify_file_size(stream->context, atoi(data + 16), data, 0);
     119               0 :                         php_stream_notify_progress_init(stream->context, 0, 0);
     120                 :                 }
     121                 :         }
     122             233 :         return length;
     123                 :         
     124                 : }
     125                 : 
     126                 : static int on_progress_avail(php_stream *stream, double dltotal, double dlnow, double ultotal, double ulnow)
     127              70 : {
     128                 :         TSRMLS_FETCH();
     129                 : 
     130                 :         /* our notification system only works in a single direction; we should detect which
     131                 :          * direction is important and use the correct values in this call */
     132              70 :         php_stream_notify_progress(stream->context, (size_t) dlnow, (size_t) dltotal);
     133              70 :         return 0;
     134                 : }
     135                 : 
     136                 : static size_t php_curl_stream_write(php_stream *stream, const char *buf, size_t count TSRMLS_DC)
     137               0 : {
     138               0 :         php_curl_stream *curlstream = (php_curl_stream *) stream->abstract;
     139                 : 
     140               0 :         if (curlstream->writebuffer.buf) {
     141               0 :                 return php_stream_write(curlstream->writebuffer.buf, buf, count);
     142                 :         }
     143                 :         
     144               0 :         return 0;
     145                 : }
     146                 : 
     147                 : static size_t php_curl_stream_read(php_stream *stream, char *buf, size_t count TSRMLS_DC)
     148             104 : {
     149             104 :         php_curl_stream *curlstream = (php_curl_stream *) stream->abstract;
     150             104 :         size_t didread = 0;
     151                 :         
     152             104 :         if (curlstream->readbuffer.readpos >= curlstream->readbuffer.writepos && curlstream->pending) {
     153                 :                 /* we need to read some more data */
     154                 :                 struct timeval tv;
     155                 : 
     156                 :                 /* fire up the connection */
     157              79 :                 if (curlstream->readbuffer.writepos == 0) {
     158              24 :                         while (CURLM_CALL_MULTI_PERFORM == curl_multi_perform(curlstream->multi, &curlstream->pending));
     159                 :                 }
     160                 :                 
     161                 :                 do {
     162                 :                         /* get the descriptors from curl */
     163          115425 :                         curl_multi_fdset(curlstream->multi, &curlstream->readfds, &curlstream->writefds, &curlstream->excfds, &curlstream->maxfd);
     164                 : 
     165                 :                         /* if we are in blocking mode, set a timeout */
     166          115425 :                         tv.tv_usec = 0;
     167          115425 :                         tv.tv_sec = 15; /* TODO: allow this to be configured from the script */
     168                 : 
     169                 :                         /* wait for data */
     170                 :                         switch ((curlstream->maxfd < 0) ? 1 : 
     171          115425 :                                         select(curlstream->maxfd + 1, &curlstream->readfds, &curlstream->writefds, &curlstream->excfds, &tv)) {
     172                 :                                 case -1:
     173                 :                                         /* error */
     174               0 :                                         return 0;
     175                 :                                 case 0:
     176                 :                                         /* no data yet: timed-out */
     177               0 :                                         return 0;
     178                 :                                 default:
     179                 :                                         /* fetch the data */
     180                 :                                         do {
     181          115490 :                                                 curlstream->mcode = curl_multi_perform(curlstream->multi, &curlstream->pending);
     182          115490 :                                         } while (curlstream->mcode == CURLM_CALL_MULTI_PERFORM);
     183                 :                         }
     184                 :                 } while (curlstream->maxfd >= 0 &&
     185          115425 :                                 curlstream->readbuffer.readpos >= curlstream->readbuffer.writepos && curlstream->pending > 0);
     186                 : 
     187                 :         }
     188                 : 
     189                 :         /* if there is data in the buffer, try and read it */
     190             104 :         if (curlstream->readbuffer.writepos > 0 && curlstream->readbuffer.readpos < curlstream->readbuffer.writepos) {
     191              76 :                 php_stream_seek(curlstream->readbuffer.buf, curlstream->readbuffer.readpos, SEEK_SET);
     192              76 :                 didread = php_stream_read(curlstream->readbuffer.buf, buf, count);
     193              76 :                 curlstream->readbuffer.readpos = php_stream_tell(curlstream->readbuffer.buf);
     194                 :         }
     195                 : 
     196             104 :         if (didread == 0) {
     197              28 :                 stream->eof = 1;
     198                 :         }
     199                 :         
     200             104 :         return didread;
     201                 : }
     202                 : 
     203                 : static int php_curl_stream_close(php_stream *stream, int close_handle TSRMLS_DC)
     204              29 : {
     205              29 :         php_curl_stream *curlstream = (php_curl_stream *) stream->abstract;
     206                 : 
     207                 :         /* TODO: respect the close_handle flag here, so that casting to a FILE* on
     208                 :          * systems without fopencookie will work properly */
     209                 :         
     210              29 :         curl_multi_remove_handle(curlstream->multi, curlstream->curl);
     211              29 :         curl_easy_cleanup(curlstream->curl); 
     212              29 :         curl_multi_cleanup(curlstream->multi);       
     213                 : 
     214                 :         /* we are not closing curlstream->readbuf here, because we export
     215                 :          * it as a zval with the wrapperdata - the engine will garbage collect it */
     216                 : 
     217              29 :         efree(curlstream->url);
     218              29 :         efree(curlstream);
     219                 :         
     220              29 :         return 0;
     221                 : }
     222                 : 
     223                 : static int php_curl_stream_flush(php_stream *stream TSRMLS_DC)
     224              29 : {
     225                 : #ifdef ilia_0
     226                 :         php_curl_stream *curlstream = (php_curl_stream *) stream->abstract;
     227                 : #endif
     228              29 :         return 0;
     229                 : }
     230                 : 
     231                 : static int php_curl_stream_stat(php_stream *stream, php_stream_statbuf *ssb TSRMLS_DC)
     232              15 : {
     233                 :         /* TODO: fill in details based on Data: and Content-Length: headers, and/or data
     234                 :          * from curl_easy_getinfo().
     235                 :          * For now, return -1 to indicate that it doesn't make sense to stat this stream */
     236              15 :         return -1;
     237                 : }
     238                 : 
     239                 : static int php_curl_stream_cast(php_stream *stream, int castas, void **ret TSRMLS_DC)
     240               0 : {
     241               0 :         php_curl_stream *curlstream = (php_curl_stream *) stream->abstract;
     242                 :         /* delegate to the readbuffer stream */
     243               0 :         return php_stream_cast(curlstream->readbuffer.buf, castas, ret, 0);
     244                 : }
     245                 : 
     246                 : php_stream_ops php_curl_stream_ops = {
     247                 :         php_curl_stream_write,
     248                 :         php_curl_stream_read,
     249                 :         php_curl_stream_close,
     250                 :         php_curl_stream_flush,
     251                 :         "cURL",
     252                 :         NULL, /* seek */
     253                 :         php_curl_stream_cast, /* cast */
     254                 :         php_curl_stream_stat  /* stat */
     255                 : };
     256                 : 
     257                 : 
     258                 : php_stream *php_curl_stream_opener(php_stream_wrapper *wrapper, char *filename, char *mode,
     259                 :                 int options, char **opened_path, php_stream_context *context STREAMS_DC TSRMLS_DC)
     260              29 : {
     261                 :         php_stream *stream;
     262                 :         php_curl_stream *curlstream;
     263              29 :         zval *tmp, **ctx_opt = NULL;
     264              29 :         struct curl_slist *slist = NULL;
     265                 : 
     266              29 :         curlstream = emalloc(sizeof(php_curl_stream));
     267              29 :         memset(curlstream, 0, sizeof(php_curl_stream));
     268                 : 
     269              29 :         stream = php_stream_alloc(&php_curl_stream_ops, curlstream, 0, mode);
     270              29 :         php_stream_context_set(stream, context);
     271                 : 
     272              29 :         curlstream->curl = curl_easy_init();
     273              29 :         curlstream->multi = curl_multi_init();
     274              29 :         curlstream->pending = 1;
     275                 : 
     276                 :         /* if opening for an include statement, ensure that the local storage will
     277                 :          * have a FILE* associated with it.
     278                 :          * Otherwise, use the "smart" memory stream that will turn itself into a file
     279                 :          * when it gets large */
     280                 : #if !HAVE_FOPENCOOKIE
     281                 :         if (options & STREAM_WILL_CAST) {
     282                 :                 curlstream->readbuffer.buf = php_stream_fopen_tmpfile();
     283                 :         } else
     284                 : #endif
     285                 :         {
     286              29 :                 curlstream->readbuffer.buf = php_stream_temp_new();
     287                 :         }
     288                 :         
     289                 :         /* curl requires the URL to be valid throughout it's operation, so dup it */
     290              29 :         curlstream->url = estrdup(filename);
     291              29 :         curl_easy_setopt(curlstream->curl, CURLOPT_URL, curlstream->url);
     292                 : 
     293                 :         /* feed curl data into our read buffer */       
     294              29 :         curl_easy_setopt(curlstream->curl, CURLOPT_WRITEFUNCTION, on_data_available);
     295              29 :         curl_easy_setopt(curlstream->curl, CURLOPT_FILE, stream);
     296                 :         
     297                 :         /* feed headers */
     298              29 :         curl_easy_setopt(curlstream->curl, CURLOPT_HEADERFUNCTION, on_header_available);
     299              29 :         curl_easy_setopt(curlstream->curl, CURLOPT_WRITEHEADER, stream);
     300                 : 
     301              29 :         curl_easy_setopt(curlstream->curl, CURLOPT_ERRORBUFFER, curlstream->errstr);
     302              29 :         curl_easy_setopt(curlstream->curl, CURLOPT_VERBOSE, 0);
     303                 : 
     304                 :         /* enable progress notification */
     305              29 :         curl_easy_setopt(curlstream->curl, CURLOPT_PROGRESSFUNCTION, on_progress_avail);
     306              29 :         curl_easy_setopt(curlstream->curl, CURLOPT_PROGRESSDATA, stream);
     307              29 :         curl_easy_setopt(curlstream->curl, CURLOPT_NOPROGRESS, 0);
     308                 : 
     309              29 :         curl_easy_setopt(curlstream->curl, CURLOPT_USERAGENT, FG(user_agent) ? FG(user_agent) : "PHP/" PHP_VERSION);
     310                 :         
     311                 :         /* TODO: read cookies and options from context */
     312              49 :         if (context && !strncasecmp(filename, "http", sizeof("http")-1)) {
     313                 :                 /* Protocol version */
     314              20 :                 if (SUCCESS == php_stream_context_get_option(context, "http", "protocol_version", &ctx_opt) && Z_TYPE_PP(ctx_opt) == IS_DOUBLE) {
     315               0 :                         if (Z_DVAL_PP(ctx_opt) == 1.1) {
     316               0 :                                 curl_easy_setopt(curlstream->curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
     317                 :                         } else {
     318               0 :                                 curl_easy_setopt(curlstream->curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
     319                 :                         }
     320                 :                 }
     321                 : 
     322              20 :                 if (SUCCESS == php_stream_context_get_option(context, "http", "curl_verify_ssl_host", &ctx_opt) && Z_TYPE_PP(ctx_opt) == IS_BOOL && Z_LVAL_PP(ctx_opt) == 1) {
     323               0 :                         curl_easy_setopt(curlstream->curl, CURLOPT_SSL_VERIFYHOST, 1);
     324                 :                 } else {
     325              20 :                         curl_easy_setopt(curlstream->curl, CURLOPT_SSL_VERIFYHOST, 0);
     326                 :                 }
     327              20 :                 if (SUCCESS == php_stream_context_get_option(context, "http", "curl_verify_ssl_peer", &ctx_opt) && Z_TYPE_PP(ctx_opt) == IS_BOOL && Z_LVAL_PP(ctx_opt) == 1) {
     328               0 :                         curl_easy_setopt(curlstream->curl, CURLOPT_SSL_VERIFYPEER, 1);
     329                 :                 } else {
     330              20 :                         curl_easy_setopt(curlstream->curl, CURLOPT_SSL_VERIFYPEER, 0);
     331                 :                 }
     332                 : 
     333                 :                 /* HTTP(S) */
     334              20 :                 if (SUCCESS == php_stream_context_get_option(context, "http", "user_agent", &ctx_opt) && Z_TYPE_PP(ctx_opt) == IS_STRING) {
     335               0 :                         curl_easy_setopt(curlstream->curl, CURLOPT_USERAGENT, Z_STRVAL_PP(ctx_opt));
     336                 :                 }
     337              20 :                 if (SUCCESS == php_stream_context_get_option(context, "http", "header", &ctx_opt)) {
     338               2 :                         if (Z_TYPE_PP(ctx_opt) == IS_ARRAY) {
     339                 :                                 HashPosition pos;
     340               1 :                                 zval **header = NULL;
     341                 :                         
     342               1 :                                 for (zend_hash_internal_pointer_reset_ex(Z_ARRVAL_PP(ctx_opt), &pos);
     343               4 :                                         SUCCESS == zend_hash_get_current_data_ex(Z_ARRVAL_PP(ctx_opt), (void *)&header, &pos);
     344                 :                                         zend_hash_move_forward_ex(Z_ARRVAL_PP(ctx_opt), &pos)
     345               2 :                                 ) {
     346               2 :                                         if (Z_TYPE_PP(header) == IS_STRING) {
     347               2 :                                                 slist = curl_slist_append(slist, Z_STRVAL_PP(header));
     348                 :                                         }
     349                 :                                 }
     350               1 :                         } else if (Z_TYPE_PP(ctx_opt) == IS_STRING && Z_STRLEN_PP(ctx_opt)) {
     351                 :                                 char *p, *token, *trimmed, *copy_ctx_opt;
     352                 : 
     353               1 :                                 copy_ctx_opt = php_trim(Z_STRVAL_PP(ctx_opt), Z_STRLEN_PP(ctx_opt), NULL, 0, NULL, 3 TSRMLS_CC);
     354               1 :                                 p = php_strtok_r(copy_ctx_opt, "\r\n", &token);
     355               4 :                                 while (p) {
     356               2 :                                         trimmed = php_trim(p, strlen(p), NULL, 0, NULL, 3 TSRMLS_CC);
     357               2 :                                         slist = curl_slist_append(slist, trimmed);
     358               2 :                                         efree(trimmed);
     359               2 :                                         p = php_strtok_r(NULL, "\r\n", &token);
     360                 :                                 }
     361               1 :                                 efree(copy_ctx_opt);
     362                 :                         }
     363               2 :                         if (slist) {
     364               2 :                                 curl_easy_setopt(curlstream->curl, CURLOPT_HTTPHEADER, slist);
     365                 :                         }
     366                 :                 }
     367              20 :                 if (SUCCESS == php_stream_context_get_option(context, "http", "method", &ctx_opt) && Z_TYPE_PP(ctx_opt) == IS_STRING) {
     368               2 :                         if (strcasecmp(Z_STRVAL_PP(ctx_opt), "get")) {
     369               2 :                                 if (!strcasecmp(Z_STRVAL_PP(ctx_opt), "head")) {
     370               0 :                                         curl_easy_setopt(curlstream->curl, CURLOPT_NOBODY, 1);
     371                 :                                 } else {
     372               2 :                                         if (!strcasecmp(Z_STRVAL_PP(ctx_opt), "post")) {
     373               2 :                                                 curl_easy_setopt(curlstream->curl, CURLOPT_POST, 1);
     374                 :                                         } else {
     375               0 :                                                 curl_easy_setopt(curlstream->curl, CURLOPT_CUSTOMREQUEST, Z_STRVAL_PP(ctx_opt));
     376                 :                                         }
     377               2 :                                         if (SUCCESS == php_stream_context_get_option(context, "http", "content", &ctx_opt) && Z_TYPE_PP(ctx_opt) == IS_STRING) {
     378               2 :                                                 curl_easy_setopt(curlstream->curl, CURLOPT_POSTFIELDS, Z_STRVAL_PP(ctx_opt));
     379               2 :                                                 curl_easy_setopt(curlstream->curl, CURLOPT_POSTFIELDSIZE, (long)Z_STRLEN_PP(ctx_opt));
     380                 :                                         }
     381                 :                                 }
     382                 :                         }
     383                 :                 }
     384              20 :                 if (SUCCESS == php_stream_context_get_option(context, "http", "proxy", &ctx_opt) && Z_TYPE_PP(ctx_opt) == IS_STRING) {
     385               0 :                         curl_easy_setopt(curlstream->curl, CURLOPT_PROXY, Z_STRVAL_PP(ctx_opt));
     386                 :                 }
     387              20 :                 if (SUCCESS == php_stream_context_get_option(context, "http", "max_redirects", &ctx_opt)) {
     388               6 :                         long mr = 20;
     389               6 :                         if (Z_TYPE_PP(ctx_opt) != IS_STRING || !is_numeric_string(Z_STRVAL_PP(ctx_opt), Z_STRLEN_PP(ctx_opt), &mr, NULL, 1)) {
     390               6 :                                 if (Z_TYPE_PP(ctx_opt) == IS_LONG) {
     391               6 :                                         mr = Z_LVAL_PP(ctx_opt);
     392                 :                                 }
     393                 :                         }
     394               6 :                         if (mr > 1) {
     395               2 :                                 if ((PG(open_basedir) && *PG(open_basedir)) || PG(safe_mode)) {
     396               0 :                                         curl_easy_setopt(curlstream->curl, CURLOPT_FOLLOWLOCATION, 0);
     397                 :                                 } else {
     398               2 :                                         curl_easy_setopt(curlstream->curl, CURLOPT_FOLLOWLOCATION, 1);
     399                 :                                 }
     400               2 :                                 curl_easy_setopt(curlstream->curl, CURLOPT_MAXREDIRS, mr);
     401                 :                         }
     402                 :                 } else {
     403              14 :                         if ((PG(open_basedir) && *PG(open_basedir)) || PG(safe_mode)) {
     404               0 :                                 curl_easy_setopt(curlstream->curl, CURLOPT_FOLLOWLOCATION, 0);
     405                 :                         } else {
     406              14 :                                 curl_easy_setopt(curlstream->curl, CURLOPT_FOLLOWLOCATION, 1);
     407                 :                         }
     408              14 :                         curl_easy_setopt(curlstream->curl, CURLOPT_MAXREDIRS, 20L);
     409                 :                 }
     410               9 :         } else if (context && !strncasecmp(filename, "ftps", sizeof("ftps")-1)) {
     411               0 :                 if (SUCCESS == php_stream_context_get_option(context, "ftp", "curl_verify_ssl_host", &ctx_opt) && Z_TYPE_PP(ctx_opt) == IS_BOOL && Z_LVAL_PP(ctx_opt) == 1) {
     412               0 :                         curl_easy_setopt(curlstream->curl, CURLOPT_SSL_VERIFYHOST, 1);
     413                 :                 } else {
     414               0 :                         curl_easy_setopt(curlstream->curl, CURLOPT_SSL_VERIFYHOST, 0);
     415                 :                 }
     416               0 :                 if (SUCCESS == php_stream_context_get_option(context, "ftp", "curl_verify_ssl_peer", &ctx_opt) && Z_TYPE_PP(ctx_opt) == IS_BOOL && Z_LVAL_PP(ctx_opt) == 1) {
     417               0 :                         curl_easy_setopt(curlstream->curl, CURLOPT_SSL_VERIFYPEER, 1);
     418                 :                 } else {
     419               0 :                         curl_easy_setopt(curlstream->curl, CURLOPT_SSL_VERIFYPEER, 0);
     420                 :                 }
     421                 :         }
     422                 : 
     423                 :         /* prepare for "pull" mode */
     424              29 :         curl_multi_add_handle(curlstream->multi, curlstream->curl);
     425                 : 
     426                 :         /* Prepare stuff for file_get_wrapper_data: the data is an array:
     427                 :          *
     428                 :          * data = array(
     429                 :          *   "headers" => array("Content-Type: text/html", "Xxx: Yyy"),
     430                 :          *   "readbuf" => resource (equivalent to curlstream->readbuffer)
     431                 :          * );
     432                 :          * */
     433              29 :         MAKE_STD_ZVAL(stream->wrapperdata);
     434              29 :         array_init(stream->wrapperdata);
     435                 :         
     436              29 :         MAKE_STD_ZVAL(curlstream->headers);
     437              29 :         array_init(curlstream->headers);
     438                 :         
     439              29 :         add_assoc_zval(stream->wrapperdata, "headers", curlstream->headers);
     440                 :         
     441              29 :         MAKE_STD_ZVAL(tmp);
     442              29 :         php_stream_to_zval(curlstream->readbuffer.buf, tmp);
     443              29 :         add_assoc_zval(stream->wrapperdata, "readbuf", tmp);
     444                 : 
     445                 : #if !HAVE_FOPENCOOKIE
     446                 :         if (options & STREAM_WILL_CAST) {
     447                 :                 /* we will need to download the whole resource now,
     448                 :                  * since we cannot get the actual FD for the download,
     449                 :                  * so we won't be able to drive curl via stdio. */
     450                 : 
     451                 : /* TODO: this needs finishing */
     452                 :                 
     453                 :                 curl_easy_perform(curlstream->curl);
     454                 :         }
     455                 :         else
     456                 : #endif
     457                 :         {
     458                 :                 /* fire up the connection; we need to detect a connection error here,
     459                 :                  * otherwise the curlstream we return ends up doing nothing useful. */
     460                 :                 CURLMcode m;
     461                 :                 CURLMsg *msg;
     462              29 :                 int msgs_left, msg_found = 0;
     463                 : 
     464             107 :                 while (CURLM_CALL_MULTI_PERFORM == (m = curl_multi_perform(curlstream->multi, &curlstream->pending))) {
     465                 :                         ; /* spin */
     466                 :                 }
     467                 : 
     468              29 :                 if (m != CURLM_OK) {
     469                 : #if HAVE_CURL_MULTI_STRERROR
     470               0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", curl_multi_strerror(m));
     471                 : #else 
     472                 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "There was an error mcode=%d", m);
     473                 : #endif
     474               0 :                         goto exit_fail;
     475                 :                 }
     476                 :                 
     477                 :                 /* we have only one curl handle here, even though we use multi syntax, 
     478                 :                  * so it's ok to fail on any error */
     479              59 :                 while ((msg = curl_multi_info_read(curlstream->multi, &msgs_left))) {
     480               1 :                         if (msg->data.result == CURLE_OK) {
     481               1 :                                 continue;
     482                 :                         } else {
     483                 : #if HAVE_CURL_EASY_STRERROR
     484               0 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", curl_easy_strerror(msg->data.result));
     485                 : #else
     486                 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "There was an error mcode=%d", msg->data.result);
     487                 : #endif
     488               0 :                                 msg_found++;
     489                 :                         }
     490                 :                 }
     491              29 :                 if (msg_found) {
     492               0 :                         goto exit_fail;
     493                 :                 }
     494                 :         }
     495                 : 
     496                 :         /* context headers are not needed anymore */
     497              29 :         if (slist) {
     498               2 :                 curl_easy_setopt(curlstream->curl, CURLOPT_HTTPHEADER, NULL);
     499               2 :                 curl_slist_free_all(slist);
     500                 :         }
     501              29 :         return stream;
     502                 : 
     503               0 : exit_fail:
     504               0 :         php_stream_close(stream);
     505               0 :         if (slist) {
     506               0 :                 curl_slist_free_all(slist);
     507                 :         }
     508               0 :         return NULL;
     509                 : }
     510                 : 
     511                 : static php_stream_wrapper_ops php_curl_wrapper_ops = {
     512                 :         php_curl_stream_opener,
     513                 :         NULL, /* stream_close: curl streams know how to clean themselves up */
     514                 :         NULL, /* stream_stat: curl streams know how to stat themselves */
     515                 :         NULL, /* stat url */
     516                 :         NULL, /* opendir */
     517                 :         "cURL", /* label */
     518                 :         NULL, /* unlink */
     519                 :         NULL, /* rename */
     520                 :         NULL, /* mkdir */
     521                 :         NULL  /* rmdir */
     522                 : };
     523                 : 
     524                 : php_stream_wrapper php_curl_wrapper = {
     525                 :         &php_curl_wrapper_ops,
     526                 :         NULL,
     527                 :         1 /* is_url */
     528                 : };
     529                 : 
     530                 : #endif
     531                 : 
     532                 : /*
     533                 :  * Local variables:
     534                 :  * tab-width: 4
     535                 :  * c-basic-offset: 4
     536                 :  * End:
     537                 :  * vim600: noet sw=4 ts=4 fdm=marker
     538                 :  * vim<600: noet sw=4 ts=4
     539                 :  */

Generated by: LTP GCOV extension version 1.5

Generated at Thu, 19 Nov 2009 08:20:05 +0000 (5 days ago)

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