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/standard - http_fopen_wrapper.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 288 482 59.8 %
Date: 2014-04-16 Functions: 4 4 100.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             :    | Authors: Rasmus Lerdorf <rasmus@php.net>                             |
      16             :    |          Jim Winstead <jimw@php.net>                                 |
      17             :    |          Hartmut Holzgraefe <hholzgra@php.net>                       |
      18             :    |          Wez Furlong <wez@thebrainroom.com>                          |
      19             :    |          Sara Golemon <pollita@php.net>                              |
      20             :    +----------------------------------------------------------------------+
      21             :  */
      22             : /* $Id$ */ 
      23             : 
      24             : #include "php.h"
      25             : #include "php_globals.h"
      26             : #include "php_streams.h"
      27             : #include "php_network.h"
      28             : #include "php_ini.h"
      29             : #include "ext/standard/basic_functions.h"
      30             : #include "ext/standard/php_smart_str.h"
      31             : 
      32             : #include <stdio.h>
      33             : #include <stdlib.h>
      34             : #include <errno.h>
      35             : #include <sys/types.h>
      36             : #include <sys/stat.h>
      37             : #include <fcntl.h>
      38             : 
      39             : #ifdef PHP_WIN32
      40             : #define O_RDONLY _O_RDONLY
      41             : #include "win32/param.h"
      42             : #else
      43             : #include <sys/param.h>
      44             : #endif
      45             : 
      46             : #include "php_standard.h"
      47             : 
      48             : #include <sys/types.h>
      49             : #if HAVE_SYS_SOCKET_H
      50             : #include <sys/socket.h>
      51             : #endif
      52             : 
      53             : #ifdef PHP_WIN32
      54             : #include <winsock2.h>
      55             : #elif defined(NETWARE) && defined(USE_WINSOCK)
      56             : #include <novsock2.h>
      57             : #else
      58             : #include <netinet/in.h>
      59             : #include <netdb.h>
      60             : #if HAVE_ARPA_INET_H
      61             : #include <arpa/inet.h>
      62             : #endif
      63             : #endif
      64             : 
      65             : #if defined(PHP_WIN32) || defined(__riscos__) || defined(NETWARE)
      66             : #undef AF_UNIX
      67             : #endif
      68             : 
      69             : #if defined(AF_UNIX)
      70             : #include <sys/un.h>
      71             : #endif
      72             : 
      73             : #include "php_fopen_wrappers.h"
      74             : 
      75             : #define HTTP_HEADER_BLOCK_SIZE          1024
      76             : #define PHP_URL_REDIRECT_MAX            20
      77             : #define HTTP_HEADER_USER_AGENT          1
      78             : #define HTTP_HEADER_HOST                        2
      79             : #define HTTP_HEADER_AUTH                        4
      80             : #define HTTP_HEADER_FROM                        8
      81             : #define HTTP_HEADER_CONTENT_LENGTH      16
      82             : #define HTTP_HEADER_TYPE                        32
      83             : #define HTTP_HEADER_CONNECTION          64
      84             : 
      85             : #define HTTP_WRAPPER_HEADER_INIT    1
      86             : #define HTTP_WRAPPER_REDIRECTED     2
      87             : 
      88          12 : static inline void strip_header(char *header_bag, char *lc_header_bag,
      89             :                 const char *lc_header_name)
      90             : {
      91          12 :         char *lc_header_start = strstr(lc_header_bag, lc_header_name);
      92          12 :         char *header_start = header_bag + (lc_header_start - lc_header_bag);
      93             : 
      94          18 :         if (lc_header_start
      95           6 :         && (lc_header_start == lc_header_bag || *(lc_header_start-1) == '\n')
      96             :         ) {
      97           6 :                 char *lc_eol = strchr(lc_header_start, '\n');
      98           6 :                 char *eol = header_start + (lc_eol - lc_header_start);
      99             : 
     100           6 :                 if (lc_eol) {
     101           4 :                         size_t eollen = strlen(lc_eol);
     102             : 
     103           4 :                         memmove(lc_header_start, lc_eol+1, eollen);
     104           4 :                         memmove(header_start, eol+1, eollen);
     105             :                 } else {
     106           2 :                         *lc_header_start = '\0';
     107           2 :                         *header_start = '\0';
     108             :                 }
     109             :         }
     110          12 : }
     111             : 
     112          65 : php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper, 
     113             :                 const char *path, const char *mode, int options, char **opened_path, 
     114             :                 php_stream_context *context, int redirect_max, int flags STREAMS_DC TSRMLS_DC) /* {{{ */
     115             : {
     116          65 :         php_stream *stream = NULL;
     117          65 :         php_url *resource = NULL;
     118             :         int use_ssl;
     119          65 :         int use_proxy = 0;
     120          65 :         char *scratch = NULL;
     121          65 :         char *tmp = NULL;
     122          65 :         char *ua_str = NULL;
     123          65 :         zval **ua_zval = NULL, **tmpzval = NULL;
     124          65 :         int scratch_len = 0;
     125          65 :         int body = 0;
     126             :         char location[HTTP_HEADER_BLOCK_SIZE];
     127          65 :         zval *response_header = NULL;
     128          65 :         int reqok = 0;
     129          65 :         char *http_header_line = NULL;
     130             :         char tmp_line[128];
     131          65 :         size_t chunk_size = 0, file_size = 0;
     132          65 :         int eol_detect = 0;
     133          65 :         char *transport_string, *errstr = NULL;
     134          65 :         int transport_len, have_header = 0, request_fulluri = 0, ignore_errors = 0;
     135          65 :         char *protocol_version = NULL;
     136          65 :         int protocol_version_len = 3; /* Default: "1.0" */
     137             :         struct timeval timeout;
     138          65 :         char *user_headers = NULL;
     139          65 :         int header_init = ((flags & HTTP_WRAPPER_HEADER_INIT) != 0);
     140          65 :         int redirected = ((flags & HTTP_WRAPPER_REDIRECTED) != 0);
     141          65 :         int follow_location = 1;
     142          65 :         php_stream_filter *transfer_encoding = NULL;
     143             :         int response_code;
     144             : 
     145          65 :         tmp_line[0] = '\0';
     146             : 
     147          65 :         if (redirect_max < 1) {
     148           3 :                 php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "Redirection limit reached, aborting");
     149           3 :                 return NULL;
     150             :         }
     151             : 
     152          62 :         resource = php_url_parse(path);
     153          62 :         if (resource == NULL) {
     154           0 :                 return NULL;
     155             :         }
     156             : 
     157          62 :         if (strncasecmp(resource->scheme, "http", sizeof("http")) && strncasecmp(resource->scheme, "https", sizeof("https"))) {
     158           0 :                 if (!context || 
     159           0 :                         php_stream_context_get_option(context, wrapper->wops->label, "proxy", &tmpzval) == FAILURE ||
     160           0 :                         Z_TYPE_PP(tmpzval) != IS_STRING ||
     161           0 :                         Z_STRLEN_PP(tmpzval) <= 0) {
     162           0 :                         php_url_free(resource);
     163           0 :                         return php_stream_open_wrapper_ex(path, mode, REPORT_ERRORS, NULL, context);
     164             :                 }
     165             :                 /* Called from a non-http wrapper with http proxying requested (i.e. ftp) */
     166           0 :                 request_fulluri = 1;
     167           0 :                 use_ssl = 0;
     168           0 :                 use_proxy = 1;
     169             : 
     170           0 :                 transport_len = Z_STRLEN_PP(tmpzval);
     171           0 :                 transport_string = estrndup(Z_STRVAL_PP(tmpzval), Z_STRLEN_PP(tmpzval));
     172             :         } else {
     173             :                 /* Normal http request (possibly with proxy) */
     174             :         
     175          62 :                 if (strpbrk(mode, "awx+")) {
     176           0 :                         php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "HTTP wrapper does not support writeable connections");
     177           0 :                         php_url_free(resource);
     178           0 :                         return NULL;
     179             :                 }
     180             : 
     181          62 :                 use_ssl = resource->scheme && (strlen(resource->scheme) > 4) && resource->scheme[4] == 's';
     182             :                 /* choose default ports */
     183          68 :                 if (use_ssl && resource->port == 0)
     184           6 :                         resource->port = 443;
     185          56 :                 else if (resource->port == 0)
     186           4 :                         resource->port = 80;
     187             : 
     188         123 :                 if (context &&
     189          61 :                         php_stream_context_get_option(context, wrapper->wops->label, "proxy", &tmpzval) == SUCCESS &&
     190           0 :                         Z_TYPE_PP(tmpzval) == IS_STRING &&
     191           0 :                         Z_STRLEN_PP(tmpzval) > 0) {
     192           0 :                         use_proxy = 1;
     193           0 :                         transport_len = Z_STRLEN_PP(tmpzval);
     194           0 :                         transport_string = estrndup(Z_STRVAL_PP(tmpzval), Z_STRLEN_PP(tmpzval));
     195             :                 } else {
     196          62 :                         transport_len = spprintf(&transport_string, 0, "%s://%s:%d", use_ssl ? "ssl" : "tcp", resource->host, resource->port);
     197             :                 }
     198             :         }
     199             : 
     200          65 :         if (context && php_stream_context_get_option(context, wrapper->wops->label, "timeout", &tmpzval) == SUCCESS) {
     201           6 :                 SEPARATE_ZVAL(tmpzval);
     202          12 :                 convert_to_double_ex(tmpzval);
     203           3 :                 timeout.tv_sec = (time_t) Z_DVAL_PP(tmpzval);
     204           3 :                 timeout.tv_usec = (size_t) ((Z_DVAL_PP(tmpzval) - timeout.tv_sec) * 1000000);
     205             :         } else {
     206          59 :                 timeout.tv_sec = FG(default_socket_timeout);
     207          59 :                 timeout.tv_usec = 0;
     208             :         }
     209             : 
     210          62 :         stream = php_stream_xport_create(transport_string, transport_len, options,
     211             :                         STREAM_XPORT_CLIENT | STREAM_XPORT_CONNECT,
     212             :                         NULL, &timeout, context, &errstr, NULL);
     213             :     
     214          62 :         if (stream) {
     215          57 :                 php_stream_set_option(stream, PHP_STREAM_OPTION_READ_TIMEOUT, 0, &timeout);
     216             :         }
     217             :                         
     218          62 :         if (errstr) {
     219           4 :                 php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "%s", errstr);
     220           4 :                 efree(errstr);
     221           4 :                 errstr = NULL;
     222             :         }
     223             : 
     224          62 :         efree(transport_string);
     225             : 
     226          62 :         if (stream && use_proxy && use_ssl) {
     227           0 :                 smart_str header = {0};
     228             : 
     229           0 :                 smart_str_appendl(&header, "CONNECT ", sizeof("CONNECT ")-1);
     230           0 :                 smart_str_appends(&header, resource->host);
     231           0 :                 smart_str_appendc(&header, ':');
     232           0 :                 smart_str_append_unsigned(&header, resource->port);
     233           0 :                 smart_str_appendl(&header, " HTTP/1.0\r\n", sizeof(" HTTP/1.0\r\n")-1);
     234             : 
     235             :             /* check if we have Proxy-Authorization header */
     236           0 :                 if (context && php_stream_context_get_option(context, "http", "header", &tmpzval) == SUCCESS) {
     237             :                         char *s, *p;
     238             : 
     239           0 :                         if (Z_TYPE_PP(tmpzval) == IS_ARRAY) {
     240             :                                 HashPosition pos;
     241           0 :                                 zval **tmpheader = NULL;
     242             : 
     243           0 :                                 for (zend_hash_internal_pointer_reset_ex(Z_ARRVAL_PP(tmpzval), &pos);
     244           0 :                                         SUCCESS == zend_hash_get_current_data_ex(Z_ARRVAL_PP(tmpzval), (void *)&tmpheader, &pos);
     245           0 :                                         zend_hash_move_forward_ex(Z_ARRVAL_PP(tmpzval), &pos)) {
     246           0 :                                         if (Z_TYPE_PP(tmpheader) == IS_STRING) {
     247           0 :                                                 s = Z_STRVAL_PP(tmpheader);
     248             :                                                 do {
     249           0 :                                                         while (*s == ' ' || *s == '\t') s++;
     250           0 :                                                         p = s;
     251           0 :                                                         while (*p != 0 && *p != ':' && *p != '\r' && *p !='\n') p++;
     252           0 :                                                         if (*p == ':') {
     253           0 :                                                                 p++;
     254           0 :                                                                 if (p - s == sizeof("Proxy-Authorization:") - 1 &&
     255             :                                                                     zend_binary_strcasecmp(s, sizeof("Proxy-Authorization:") - 1,
     256           0 :                                                                         "Proxy-Authorization:", sizeof("Proxy-Authorization:") - 1) == 0) {
     257           0 :                                                                         while (*p != 0 && *p != '\r' && *p !='\n') p++;
     258           0 :                                                                         smart_str_appendl(&header, s, p - s);
     259           0 :                                                                         smart_str_appendl(&header, "\r\n", sizeof("\r\n")-1);
     260           0 :                                                                         goto finish;
     261             :                                                                 } else {
     262           0 :                                                                         while (*p != 0 && *p != '\r' && *p !='\n') p++;
     263             :                                                                 }
     264             :                                                         }
     265           0 :                                                         s = p;
     266           0 :                                                         while (*s == '\r' || *s == '\n') s++;
     267           0 :                                                 } while (*s != 0);
     268             :                                         }
     269             :                                 }
     270           0 :                         } else if (Z_TYPE_PP(tmpzval) == IS_STRING && Z_STRLEN_PP(tmpzval)) {
     271           0 :                                 s = Z_STRVAL_PP(tmpzval);
     272             :                                 do {
     273           0 :                                         while (*s == ' ' || *s == '\t') s++;
     274           0 :                                         p = s;
     275           0 :                                         while (*p != 0 && *p != ':' && *p != '\r' && *p !='\n') p++;
     276           0 :                                         if (*p == ':') {
     277           0 :                                                 p++;
     278           0 :                                                 if (p - s == sizeof("Proxy-Authorization:") - 1 &&
     279             :                                                     zend_binary_strcasecmp(s, sizeof("Proxy-Authorization:") - 1,
     280           0 :                                                         "Proxy-Authorization:", sizeof("Proxy-Authorization:") - 1) == 0) {
     281           0 :                                                         while (*p != 0 && *p != '\r' && *p !='\n') p++;
     282           0 :                                                         smart_str_appendl(&header, s, p - s);
     283           0 :                                                         smart_str_appendl(&header, "\r\n", sizeof("\r\n")-1);
     284           0 :                                                         goto finish;
     285             :                                                 } else {
     286           0 :                                                         while (*p != 0 && *p != '\r' && *p !='\n') p++;
     287             :                                                 }
     288             :                                         }
     289           0 :                                         s = p;
     290           0 :                                         while (*s == '\r' || *s == '\n') s++;
     291           0 :                                 } while (*s != 0);
     292             :                         }
     293             :                 }
     294             : finish:
     295           0 :                 smart_str_appendl(&header, "\r\n", sizeof("\r\n")-1);
     296             : 
     297           0 :                 if (php_stream_write(stream, header.c, header.len) != header.len) {
     298           0 :                         php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "Cannot connect to HTTPS server through proxy");
     299           0 :                         php_stream_close(stream);
     300           0 :                         stream = NULL;
     301             :                 }
     302           0 :                 smart_str_free(&header);
     303             : 
     304           0 :                 if (stream) {
     305             :                         char header_line[HTTP_HEADER_BLOCK_SIZE];
     306             : 
     307             :                         /* get response header */
     308           0 :                         while (php_stream_gets(stream, header_line, HTTP_HEADER_BLOCK_SIZE-1) != NULL) {
     309           0 :                                 if (header_line[0] == '\n' ||
     310           0 :                                     header_line[0] == '\r' ||
     311           0 :                                     header_line[0] == '\0') {
     312             :                                   break;
     313             :                                 }
     314             :                         }
     315             :                 }
     316             : 
     317             :                 /* enable SSL transport layer */
     318           0 :                 if (stream) {
     319           0 :                         if (php_stream_xport_crypto_setup(stream, STREAM_CRYPTO_METHOD_SSLv23_CLIENT, NULL TSRMLS_CC) < 0 ||
     320           0 :                             php_stream_xport_crypto_enable(stream, 1 TSRMLS_CC) < 0) {
     321           0 :                                 php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "Cannot connect to HTTPS server through proxy");
     322           0 :                                 php_stream_close(stream);
     323           0 :                                 stream = NULL;
     324             :                         }
     325             :                 }
     326             :         }
     327             : 
     328          62 :         if (stream == NULL)
     329           5 :                 goto out;
     330             : 
     331             :         /* avoid buffering issues while reading header */
     332          57 :         if (options & STREAM_WILL_CAST)
     333           0 :                 chunk_size = php_stream_set_chunk_size(stream, 1);
     334             :         
     335             :         /* avoid problems with auto-detecting when reading the headers -> the headers
     336             :          * are always in canonical \r\n format */
     337          57 :         eol_detect = stream->flags & (PHP_STREAM_FLAG_DETECT_EOL | PHP_STREAM_FLAG_EOL_MAC);
     338          57 :         stream->flags &= ~(PHP_STREAM_FLAG_DETECT_EOL | PHP_STREAM_FLAG_EOL_MAC);
     339             : 
     340          57 :         php_stream_context_set(stream, context TSRMLS_CC);
     341             : 
     342          57 :         php_stream_notify_info(context, PHP_STREAM_NOTIFY_CONNECT, NULL, 0);
     343             : 
     344          57 :         if (header_init && context && php_stream_context_get_option(context, "http", "max_redirects", &tmpzval) == SUCCESS) {
     345          12 :                 SEPARATE_ZVAL(tmpzval);
     346           6 :                 convert_to_long_ex(tmpzval);
     347           6 :                 redirect_max = Z_LVAL_PP(tmpzval);
     348             :         }
     349             : 
     350          57 :         if (context && php_stream_context_get_option(context, "http", "method", &tmpzval) == SUCCESS) {
     351          14 :                 if (Z_TYPE_PP(tmpzval) == IS_STRING && Z_STRLEN_PP(tmpzval) > 0) {
     352             :                         /* As per the RFC, automatically redirected requests MUST NOT use other methods than
     353             :                          * GET and HEAD unless it can be confirmed by the user */
     354          32 :                         if (!redirected
     355           6 :                                 || (Z_STRLEN_PP(tmpzval) == 3 && memcmp("GET", Z_STRVAL_PP(tmpzval), 3) == 0)
     356          12 :                                 || (Z_STRLEN_PP(tmpzval) == 4 && memcmp("HEAD",Z_STRVAL_PP(tmpzval), 4) == 0)
     357             :                         ) {
     358           8 :                                 scratch_len = strlen(path) + 29 + Z_STRLEN_PP(tmpzval);
     359           8 :                                 scratch = emalloc(scratch_len);
     360           8 :                                 strlcpy(scratch, Z_STRVAL_PP(tmpzval), Z_STRLEN_PP(tmpzval) + 1);
     361           8 :                                 strncat(scratch, " ", 1);
     362             :                         }
     363             :                 }
     364             :         }
     365             :  
     366          57 :         if (context && php_stream_context_get_option(context, "http", "protocol_version", &tmpzval) == SUCCESS) {
     367          14 :                 SEPARATE_ZVAL(tmpzval);
     368          25 :                 convert_to_double_ex(tmpzval);
     369           7 :                 protocol_version_len = spprintf(&protocol_version, 0, "%.1F", Z_DVAL_PP(tmpzval));
     370             :         }
     371             : 
     372          57 :         if (!scratch) {
     373          49 :                 scratch_len = strlen(path) + 29 + protocol_version_len;
     374          49 :                 scratch = emalloc(scratch_len);
     375          49 :                 strncpy(scratch, "GET ", scratch_len);
     376             :         }
     377             : 
     378             :         /* Should we send the entire path in the request line, default to no. */
     379         113 :         if (!request_fulluri &&
     380             :                 context &&
     381          56 :                 php_stream_context_get_option(context, "http", "request_fulluri", &tmpzval) == SUCCESS) {
     382           0 :                 zval ztmp = **tmpzval;
     383             : 
     384             :                 zval_copy_ctor(&ztmp);
     385           0 :                 convert_to_boolean(&ztmp);
     386           0 :                 request_fulluri = Z_BVAL(ztmp) ? 1 : 0;
     387             :                 zval_dtor(&ztmp);
     388             :         }
     389             : 
     390          57 :         if (request_fulluri) {
     391             :                 /* Ask for everything */
     392           0 :                 strcat(scratch, path);
     393             :         } else {
     394             :                 /* Send the traditional /path/to/file?query_string */
     395             : 
     396             :                 /* file */
     397         112 :                 if (resource->path && *resource->path) {
     398          55 :                         strlcat(scratch, resource->path, scratch_len);
     399             :                 } else {
     400           2 :                         strlcat(scratch, "/", scratch_len);
     401             :                 }
     402             : 
     403             :                 /* query string */
     404          57 :                 if (resource->query) {
     405           0 :                         strlcat(scratch, "?", scratch_len);
     406           0 :                         strlcat(scratch, resource->query, scratch_len);
     407             :                 }
     408             :         }
     409             : 
     410             :         /* protocol version we are speaking */
     411          57 :         if (protocol_version) {
     412           7 :                 strlcat(scratch, " HTTP/", scratch_len);
     413           7 :                 strlcat(scratch, protocol_version, scratch_len);
     414           7 :                 strlcat(scratch, "\r\n", scratch_len);
     415             :         } else {
     416          50 :                 strlcat(scratch, " HTTP/1.0\r\n", scratch_len);
     417             :         }
     418             : 
     419             :         /* send it */
     420          57 :         php_stream_write(stream, scratch, strlen(scratch));
     421             : 
     422          57 :         if (context && php_stream_context_get_option(context, "http", "header", &tmpzval) == SUCCESS) {
     423          20 :                 tmp = NULL;
     424             :                 
     425          20 :                 if (Z_TYPE_PP(tmpzval) == IS_ARRAY) {
     426             :                         HashPosition pos;
     427           1 :                         zval **tmpheader = NULL;
     428           1 :                         smart_str tmpstr = {0};
     429             : 
     430           4 :                         for (zend_hash_internal_pointer_reset_ex(Z_ARRVAL_PP(tmpzval), &pos);
     431           3 :                                 SUCCESS == zend_hash_get_current_data_ex(Z_ARRVAL_PP(tmpzval), (void *)&tmpheader, &pos);
     432           2 :                                 zend_hash_move_forward_ex(Z_ARRVAL_PP(tmpzval), &pos)
     433           2 :                         ) {
     434           2 :                                 if (Z_TYPE_PP(tmpheader) == IS_STRING) {
     435           2 :                                         smart_str_appendl(&tmpstr, Z_STRVAL_PP(tmpheader), Z_STRLEN_PP(tmpheader));
     436           2 :                                         smart_str_appendl(&tmpstr, "\r\n", sizeof("\r\n") - 1);
     437             :                                 }
     438             :                         }
     439           1 :                         smart_str_0(&tmpstr);
     440             :                         /* Remove newlines and spaces from start and end. there's at least one extra \r\n at the end that needs to go. */
     441           1 :                         if (tmpstr.c) {
     442           1 :                                 tmp = php_trim(tmpstr.c, strlen(tmpstr.c), NULL, 0, NULL, 3 TSRMLS_CC);
     443           1 :                                 smart_str_free(&tmpstr);
     444             :                         }
     445             :                 }
     446          20 :                 if (Z_TYPE_PP(tmpzval) == IS_STRING && Z_STRLEN_PP(tmpzval)) {
     447             :                         /* Remove newlines and spaces from start and end php_trim will estrndup() */
     448          19 :                         tmp = php_trim(Z_STRVAL_PP(tmpzval), Z_STRLEN_PP(tmpzval), NULL, 0, NULL, 3 TSRMLS_CC);
     449             :                 }
     450          20 :                 if (tmp && strlen(tmp) > 0) {
     451             :                         char *s;
     452             : 
     453          20 :                         user_headers = estrdup(tmp);
     454             : 
     455             :                         /* Make lowercase for easy comparison against 'standard' headers */
     456          20 :                         php_strtolower(tmp, strlen(tmp));
     457             : 
     458          20 :                         if (!header_init) {
     459             :                                 /* strip POST headers on redirect */
     460           6 :                                 strip_header(user_headers, tmp, "content-length:");
     461           6 :                                 strip_header(user_headers, tmp, "content-type:");
     462             :                         }
     463             : 
     464          20 :                         if ((s = strstr(tmp, "user-agent:")) && 
     465           0 :                             (s == tmp || *(s-1) == '\r' || *(s-1) == '\n' || 
     466           0 :                                          *(s-1) == '\t' || *(s-1) == ' ')) {
     467           0 :                                  have_header |= HTTP_HEADER_USER_AGENT;
     468             :                         }
     469          20 :                         if ((s = strstr(tmp, "host:")) &&
     470           0 :                             (s == tmp || *(s-1) == '\r' || *(s-1) == '\n' || 
     471           0 :                                          *(s-1) == '\t' || *(s-1) == ' ')) {
     472           1 :                                  have_header |= HTTP_HEADER_HOST;
     473             :                         }
     474          20 :                         if ((s = strstr(tmp, "from:")) &&
     475           0 :                             (s == tmp || *(s-1) == '\r' || *(s-1) == '\n' || 
     476           0 :                                          *(s-1) == '\t' || *(s-1) == ' ')) {
     477           0 :                                  have_header |= HTTP_HEADER_FROM;
     478             :                                 }
     479          20 :                         if ((s = strstr(tmp, "authorization:")) &&
     480           0 :                             (s == tmp || *(s-1) == '\r' || *(s-1) == '\n' || 
     481           0 :                                          *(s-1) == '\t' || *(s-1) == ' ')) {
     482           0 :                                  have_header |= HTTP_HEADER_AUTH;
     483             :                         }
     484          20 :                         if ((s = strstr(tmp, "content-length:")) &&
     485           0 :                             (s == tmp || *(s-1) == '\r' || *(s-1) == '\n' || 
     486           0 :                                          *(s-1) == '\t' || *(s-1) == ' ')) {
     487           0 :                                  have_header |= HTTP_HEADER_CONTENT_LENGTH;
     488             :                         }
     489          36 :                         if ((s = strstr(tmp, "content-type:")) &&
     490          16 :                             (s == tmp || *(s-1) == '\r' || *(s-1) == '\n' || 
     491           0 :                                          *(s-1) == '\t' || *(s-1) == ' ')) {
     492           8 :                                  have_header |= HTTP_HEADER_TYPE;
     493             :                         }
     494          20 :                         if ((s = strstr(tmp, "connection:")) &&
     495           0 :                             (s == tmp || *(s-1) == '\r' || *(s-1) == '\n' || 
     496           0 :                                          *(s-1) == '\t' || *(s-1) == ' ')) {
     497           5 :                                  have_header |= HTTP_HEADER_CONNECTION;
     498             :                         }
     499             :                         /* remove Proxy-Authorization header */
     500          20 :                         if (use_proxy && use_ssl && (s = strstr(tmp, "proxy-authorization:")) &&
     501           0 :                             (s == tmp || *(s-1) == '\r' || *(s-1) == '\n' || 
     502           0 :                                          *(s-1) == '\t' || *(s-1) == ' ')) {
     503           0 :                                 char *p = s + sizeof("proxy-authorization:") - 1;
     504             :                                 
     505           0 :                                 while (s > tmp && (*(s-1) == ' ' || *(s-1) == '\t')) s--;
     506           0 :                                 while (*p != 0 && *p != '\r' && *p != '\n') p++;
     507           0 :                                 while (*p == '\r' || *p == '\n') p++;
     508           0 :                                 if (*p == 0) {
     509           0 :                                         if (s == tmp) {
     510           0 :                                                 efree(user_headers);
     511           0 :                                                 user_headers = NULL;
     512             :                                         } else {
     513           0 :                                                 while (s > tmp && (*(s-1) == '\r' || *(s-1) == '\n')) s--;
     514           0 :                                                 user_headers[s - tmp] = 0;
     515             :                                         }
     516             :                                 } else {
     517           0 :                                         memmove(user_headers + (s - tmp), user_headers + (p - tmp), strlen(p) + 1);
     518             :                                 }
     519             :                         }
     520             : 
     521             :                 }
     522          20 :                 if (tmp) {
     523          20 :                         efree(tmp);
     524             :                 }
     525             :         }
     526             : 
     527             :         /* auth header if it was specified */
     528          57 :         if (((have_header & HTTP_HEADER_AUTH) == 0) && resource->user) {
     529             :                 /* decode the strings first */
     530           0 :                 php_url_decode(resource->user, strlen(resource->user));
     531             : 
     532             :                 /* scratch is large enough, since it was made large enough for the whole URL */
     533           0 :                 strcpy(scratch, resource->user);
     534           0 :                 strcat(scratch, ":");
     535             : 
     536             :                 /* Note: password is optional! */
     537           0 :                 if (resource->pass) {
     538           0 :                         php_url_decode(resource->pass, strlen(resource->pass));
     539           0 :                         strcat(scratch, resource->pass);
     540             :                 }
     541             : 
     542           0 :                 tmp = (char*)php_base64_encode((unsigned char*)scratch, strlen(scratch), NULL);
     543             :                 
     544           0 :                 if (snprintf(scratch, scratch_len, "Authorization: Basic %s\r\n", tmp) > 0) {
     545           0 :                         php_stream_write(stream, scratch, strlen(scratch));
     546           0 :                         php_stream_notify_info(context, PHP_STREAM_NOTIFY_AUTH_REQUIRED, NULL, 0);
     547             :                 }
     548             : 
     549           0 :                 efree(tmp);
     550           0 :                 tmp = NULL;
     551             :         }
     552             : 
     553             :         /* if the user has configured who they are, send a From: line */
     554          57 :         if (((have_header & HTTP_HEADER_FROM) == 0) && FG(from_address)) {
     555           2 :                 if (snprintf(scratch, scratch_len, "From: %s\r\n", FG(from_address)) > 0)
     556           2 :                         php_stream_write(stream, scratch, strlen(scratch));
     557             :         }
     558             : 
     559             :         /* Send Host: header so name-based virtual hosts work */
     560          57 :         if ((have_header & HTTP_HEADER_HOST) == 0) {
     561         198 :                 if ((use_ssl && resource->port != 443 && resource->port != 0) || 
     562          94 :                         (!use_ssl && resource->port != 80 && resource->port != 0)) {
     563          48 :                         if (snprintf(scratch, scratch_len, "Host: %s:%i\r\n", resource->host, resource->port) > 0)
     564          48 :                                 php_stream_write(stream, scratch, strlen(scratch));
     565             :                 } else {
     566           8 :                         if (snprintf(scratch, scratch_len, "Host: %s\r\n", resource->host) > 0) {
     567           8 :                                 php_stream_write(stream, scratch, strlen(scratch));
     568             :                         }
     569             :                 }
     570             :         }
     571             : 
     572             :         /* Send a Connection: close header when using HTTP 1.1 or later to avoid
     573             :          * hanging when the server interprets the RFC literally and establishes a
     574             :          * keep-alive connection, unless the user specifically requests something
     575             :          * else by specifying a Connection header in the context options. */
     576          66 :         if (protocol_version &&
     577           7 :             ((have_header & HTTP_HEADER_CONNECTION) == 0) &&
     578           2 :             (strncmp(protocol_version, "1.0", MIN(protocol_version_len, 3)) > 0)) {
     579           1 :                 php_stream_write_string(stream, "Connection: close\r\n");
     580             :         }
     581             : 
     582         113 :         if (context && 
     583          56 :             php_stream_context_get_option(context, "http", "user_agent", &ua_zval) == SUCCESS &&
     584           0 :                 Z_TYPE_PP(ua_zval) == IS_STRING) {
     585           0 :                 ua_str = Z_STRVAL_PP(ua_zval);
     586          57 :         } else if (FG(user_agent)) {
     587           0 :                 ua_str = FG(user_agent);
     588             :         }
     589             : 
     590          57 :         if (((have_header & HTTP_HEADER_USER_AGENT) == 0) && ua_str) {
     591             : #define _UA_HEADER "User-Agent: %s\r\n"
     592             :                 char *ua;
     593             :                 size_t ua_len;
     594             :                 
     595           0 :                 ua_len = sizeof(_UA_HEADER) + strlen(ua_str);
     596             :                 
     597             :                 /* ensure the header is only sent if user_agent is not blank */
     598           0 :                 if (ua_len > sizeof(_UA_HEADER)) {
     599           0 :                         ua = emalloc(ua_len + 1);
     600           0 :                         if ((ua_len = slprintf(ua, ua_len, _UA_HEADER, ua_str)) > 0) {
     601           0 :                                 ua[ua_len] = 0;
     602           0 :                                 php_stream_write(stream, ua, ua_len);
     603             :                         } else {
     604           0 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot construct User-agent header");
     605             :                         }
     606             : 
     607           0 :                         if (ua) {
     608           0 :                                 efree(ua);
     609             :                         }
     610             :                 }       
     611             :         }
     612             : 
     613          57 :         if (user_headers) {
     614             :                 /* A bit weird, but some servers require that Content-Length be sent prior to Content-Type for POST
     615             :                  * see bug #44603 for details. Since Content-Type maybe part of user's headers we need to do this check first.
     616             :                  */
     617          52 :                 if (
     618             :                                 header_init &&
     619             :                                 context &&
     620          14 :                                 !(have_header & HTTP_HEADER_CONTENT_LENGTH) &&
     621          14 :                                 php_stream_context_get_option(context, "http", "content", &tmpzval) == SUCCESS &&
     622           4 :                                 Z_TYPE_PP(tmpzval) == IS_STRING && Z_STRLEN_PP(tmpzval) > 0
     623             :                 ) {
     624           2 :                         scratch_len = slprintf(scratch, scratch_len, "Content-Length: %d\r\n", Z_STRLEN_PP(tmpzval));
     625           2 :                         php_stream_write(stream, scratch, scratch_len);
     626           2 :                         have_header |= HTTP_HEADER_CONTENT_LENGTH;
     627             :                 }
     628             : 
     629          20 :                 php_stream_write(stream, user_headers, strlen(user_headers));
     630          20 :                 php_stream_write(stream, "\r\n", sizeof("\r\n")-1);
     631          20 :                 efree(user_headers);
     632             :         }
     633             : 
     634             :         /* Request content, such as for POST requests */
     635         108 :         if (header_init && context &&
     636          45 :                 php_stream_context_get_option(context, "http", "content", &tmpzval) == SUCCESS &&
     637           4 :                 Z_TYPE_PP(tmpzval) == IS_STRING && Z_STRLEN_PP(tmpzval) > 0) {
     638           2 :                 if (!(have_header & HTTP_HEADER_CONTENT_LENGTH)) {
     639           0 :                         scratch_len = slprintf(scratch, scratch_len, "Content-Length: %d\r\n", Z_STRLEN_PP(tmpzval));
     640           0 :                         php_stream_write(stream, scratch, scratch_len);
     641             :                 }
     642           2 :                 if (!(have_header & HTTP_HEADER_TYPE)) {
     643           0 :                         php_stream_write(stream, "Content-Type: application/x-www-form-urlencoded\r\n",
     644             :                                 sizeof("Content-Type: application/x-www-form-urlencoded\r\n") - 1);
     645           0 :                         php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Content-type not specified assuming application/x-www-form-urlencoded");
     646             :                 }
     647           2 :                 php_stream_write(stream, "\r\n", sizeof("\r\n")-1);
     648           2 :                 php_stream_write(stream, Z_STRVAL_PP(tmpzval), Z_STRLEN_PP(tmpzval));
     649             :         } else {
     650          55 :                 php_stream_write(stream, "\r\n", sizeof("\r\n")-1);
     651             :         }
     652             : 
     653          57 :         location[0] = '\0';
     654             : 
     655          57 :         if (!EG(active_symbol_table)) {
     656          32 :                 zend_rebuild_symbol_table(TSRMLS_C);
     657             :         }
     658             : 
     659          57 :         if (header_init) {
     660             :                 zval *ztmp;
     661          46 :                 MAKE_STD_ZVAL(ztmp);
     662          46 :                 array_init(ztmp);
     663         144 :                 ZEND_SET_SYMBOL(EG(active_symbol_table), "http_response_header", ztmp);
     664             :         }
     665             : 
     666             :         {
     667             :                 zval **rh;
     668          57 :                 zend_hash_find(EG(active_symbol_table), "http_response_header", sizeof("http_response_header"), (void **) &rh);
     669          57 :                 response_header = *rh;
     670             :         }
     671             : 
     672          57 :         if (!php_stream_eof(stream)) {
     673             :                 size_t tmp_line_len;
     674             :                 /* get response header */
     675             : 
     676          57 :                 if (php_stream_get_line(stream, tmp_line, sizeof(tmp_line) - 1, &tmp_line_len) != NULL) {
     677             :                         zval *http_response;
     678             : 
     679          57 :                         if (tmp_line_len > 9) {
     680          57 :                                 response_code = atoi(tmp_line + 9);
     681             :                         } else {
     682           0 :                                 response_code = 0;
     683             :                         }
     684          57 :                         if (context && SUCCESS==php_stream_context_get_option(context, "http", "ignore_errors", &tmpzval)) {
     685           8 :                                 ignore_errors = zend_is_true(*tmpzval TSRMLS_CC);
     686             :                         }
     687             :                         /* when we request only the header, don't fail even on error codes */
     688          57 :                         if ((options & STREAM_ONLY_GET_HEADERS) || ignore_errors) {
     689           8 :                                 reqok = 1;
     690             :                         }
     691             :                         /* all status codes in the 2xx range are defined by the specification as successful;
     692             :                          * all status codes in the 3xx range are for redirection, and so also should never
     693             :                          * fail */
     694         108 :                         if (response_code >= 200 && response_code < 400) {
     695          51 :                                 reqok = 1;
     696             :                         } else {
     697           6 :                                 switch(response_code) {
     698             :                                         case 403:
     699           0 :                                                 php_stream_notify_error(context, PHP_STREAM_NOTIFY_AUTH_RESULT,
     700             :                                                                 tmp_line, response_code);
     701           0 :                                                 break;
     702             :                                         default:
     703             :                                                 /* safety net in the event tmp_line == NULL */
     704           6 :                                                 if (!tmp_line_len) {
     705           0 :                                                         tmp_line[0] = '\0';
     706             :                                                 }
     707           6 :                                                 php_stream_notify_error(context, PHP_STREAM_NOTIFY_FAILURE,
     708             :                                                                 tmp_line, response_code);
     709             :                                 }
     710             :                         }
     711          57 :                         if (tmp_line[tmp_line_len - 1] == '\n') {
     712          57 :                                 --tmp_line_len;
     713          57 :                                 if (tmp_line[tmp_line_len - 1] == '\r') {
     714          57 :                                         --tmp_line_len;
     715             :                                 }
     716             :                         }
     717          57 :                         MAKE_STD_ZVAL(http_response);
     718          57 :                         ZVAL_STRINGL(http_response, tmp_line, tmp_line_len, 1);
     719          57 :                         zend_hash_next_index_insert(Z_ARRVAL_P(response_header), &http_response, sizeof(zval *), NULL);
     720             :                 }
     721             :         } else {
     722           0 :                 php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "HTTP request failed, unexpected end of socket!");
     723           0 :                 goto out;
     724             :         }
     725             :         
     726             :         /* read past HTTP headers */
     727             :         
     728          57 :         http_header_line = emalloc(HTTP_HEADER_BLOCK_SIZE);
     729             : 
     730         218 :         while (!body && !php_stream_eof(stream)) {
     731             :                 size_t http_header_line_length;
     732         161 :                 if (php_stream_get_line(stream, http_header_line, HTTP_HEADER_BLOCK_SIZE, &http_header_line_length) && *http_header_line != '\n' && *http_header_line != '\r') {
     733         104 :                         char *e = http_header_line + http_header_line_length - 1;
     734         104 :                         if (*e != '\n') {
     735             :                                 do { /* partial header */
     736           0 :                                         if (php_stream_get_line(stream, http_header_line, HTTP_HEADER_BLOCK_SIZE, &http_header_line_length) == NULL) {
     737           0 :                                                 php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "Failed to read HTTP headers");
     738           0 :                                                 goto out;
     739             :                                         }
     740           0 :                                         e = http_header_line + http_header_line_length - 1;
     741           0 :                                 } while (*e != '\n');
     742           0 :                                 continue;
     743             :                         }
     744         416 :                         while (*e == '\n' || *e == '\r') {
     745         208 :                                 e--;
     746             :                         }
     747         104 :                         http_header_line_length = e - http_header_line + 1;
     748         104 :                         http_header_line[http_header_line_length] = '\0';
     749             : 
     750         104 :                         if (!strncasecmp(http_header_line, "Location: ", 10)) {
     751          23 :                                 if (context && php_stream_context_get_option(context, "http", "follow_location", &tmpzval) == SUCCESS) {
     752          12 :                                         SEPARATE_ZVAL(tmpzval);
     753          24 :                                         convert_to_long_ex(tmpzval);
     754           6 :                                         follow_location = Z_LVAL_PP(tmpzval);
     755          11 :                                 } else if (!(response_code >= 300 && response_code < 304 || 307 == response_code)) { 
     756             :                                         /* we shouldn't redirect automatically
     757             :                                         if follow_location isn't set and response_code not in (300, 301, 302, 303 and 307) 
     758             :                                         see http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.1 */
     759           0 :                                         follow_location = 0;
     760             :                                 }
     761          17 :                                 strlcpy(location, http_header_line + 10, sizeof(location));
     762          87 :                         } else if (!strncasecmp(http_header_line, "Content-Type: ", 14)) {
     763          11 :                                 php_stream_notify_info(context, PHP_STREAM_NOTIFY_MIME_TYPE_IS, http_header_line + 14, 0);
     764          76 :                         } else if (!strncasecmp(http_header_line, "Content-Length: ", 16)) {
     765           7 :                                 file_size = atoi(http_header_line + 16);
     766           7 :                                 php_stream_notify_file_size(context, file_size, http_header_line, 0);
     767          69 :                         } else if (!strncasecmp(http_header_line, "Transfer-Encoding: chunked", sizeof("Transfer-Encoding: chunked"))) {
     768             : 
     769             :                                 /* create filter to decode response body */
     770           0 :                                 if (!(options & STREAM_ONLY_GET_HEADERS)) {
     771           0 :                                         long decode = 1;
     772             : 
     773           0 :                                         if (context && php_stream_context_get_option(context, "http", "auto_decode", &tmpzval) == SUCCESS) {
     774           0 :                                                 SEPARATE_ZVAL(tmpzval);
     775           0 :                                                 convert_to_boolean(*tmpzval);
     776           0 :                                                 decode = Z_LVAL_PP(tmpzval);
     777             :                                         }
     778           0 :                                         if (decode) {
     779           0 :                                                 transfer_encoding = php_stream_filter_create("dechunk", NULL, php_stream_is_persistent(stream) TSRMLS_CC);
     780           0 :                                                 if (transfer_encoding) {
     781             :                                                         /* don't store transfer-encodeing header */
     782           0 :                                                         continue;
     783             :                                                 }
     784             :                                         }
     785             :                                 }
     786             :                         }
     787             : 
     788         104 :                         if (http_header_line[0] == '\0') {
     789           0 :                                 body = 1;
     790             :                         } else {
     791             :                                 zval *http_header;
     792             : 
     793         104 :                                 MAKE_STD_ZVAL(http_header);
     794             : 
     795         104 :                                 ZVAL_STRINGL(http_header, http_header_line, http_header_line_length, 1);
     796             :                                 
     797         104 :                                 zend_hash_next_index_insert(Z_ARRVAL_P(response_header), &http_header, sizeof(zval *), NULL);
     798             :                         }
     799             :                 } else {
     800             :                         break;
     801             :                 }
     802             :         }
     803             : 
     804          57 :         if (!reqok || (location[0] != '\0' && follow_location)) {
     805          21 :                 if (!follow_location || (((options & STREAM_ONLY_GET_HEADERS) || ignore_errors) && redirect_max <= 1)) {
     806             :                         goto out;
     807             :                 }
     808             : 
     809          18 :                 if (location[0] != '\0')
     810          14 :                         php_stream_notify_info(context, PHP_STREAM_NOTIFY_REDIRECTED, location, 0);
     811             : 
     812          18 :                 php_stream_close(stream);
     813          18 :                 stream = NULL;
     814             : 
     815          18 :                 if (location[0] != '\0') {
     816             : 
     817             :                         char new_path[HTTP_HEADER_BLOCK_SIZE];
     818             :                         char loc_path[HTTP_HEADER_BLOCK_SIZE];
     819             : 
     820          14 :                         *new_path='\0';
     821          20 :                         if (strlen(location)<8 || (strncasecmp(location, "http://", sizeof("http://")-1) && 
     822           0 :                                                         strncasecmp(location, "https://", sizeof("https://")-1) && 
     823           0 :                                                         strncasecmp(location, "ftp://", sizeof("ftp://")-1) && 
     824           0 :                                                         strncasecmp(location, "ftps://", sizeof("ftps://")-1))) 
     825             :                         {
     826           6 :                                 if (*location != '/') {
     827           0 :                                         if (*(location+1) != '\0' && resource->path) {
     828           0 :                                                 char *s = strrchr(resource->path, '/');
     829           0 :                                                 if (!s) {
     830           0 :                                                         s = resource->path;
     831           0 :                                                         if (!s[0]) {
     832           0 :                                                                 efree(s);
     833           0 :                                                                 s = resource->path = estrdup("/");
     834             :                                                         } else {
     835           0 :                                                                 *s = '/';
     836             :                                                         }
     837             :                                                 }
     838           0 :                                                 s[1] = '\0'; 
     839           0 :                                                 if (resource->path && *(resource->path) == '/' && *(resource->path + 1) == '\0') {
     840           0 :                                                         snprintf(loc_path, sizeof(loc_path) - 1, "%s%s", resource->path, location);
     841             :                                                 } else {
     842           0 :                                                         snprintf(loc_path, sizeof(loc_path) - 1, "%s/%s", resource->path, location);
     843             :                                                 }
     844             :                                         } else {
     845           0 :                                                 snprintf(loc_path, sizeof(loc_path) - 1, "/%s", location);
     846             :                                         }
     847             :                                 } else {
     848           6 :                                         strlcpy(loc_path, location, sizeof(loc_path));
     849             :                                 }
     850          12 :                                 if ((use_ssl && resource->port != 443) || (!use_ssl && resource->port != 80)) {
     851           6 :                                         snprintf(new_path, sizeof(new_path) - 1, "%s://%s:%d%s", resource->scheme, resource->host, resource->port, loc_path);
     852             :                                 } else {
     853           0 :                                         snprintf(new_path, sizeof(new_path) - 1, "%s://%s%s", resource->scheme, resource->host, loc_path);
     854             :                                 }
     855             :                         } else {
     856           8 :                                 strlcpy(new_path, location, sizeof(new_path));
     857             :                         }
     858             : 
     859          14 :                         php_url_free(resource);
     860             :                         /* check for invalid redirection URLs */
     861          14 :                         if ((resource = php_url_parse(new_path)) == NULL) {
     862           0 :                                 php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "Invalid redirect URL! %s", new_path);
     863           0 :                                 goto out;
     864             :                         }
     865             : 
     866             : #define CHECK_FOR_CNTRL_CHARS(val) { \
     867             :         if (val) { \
     868             :                 unsigned char *s, *e; \
     869             :                 int l; \
     870             :                 l = php_url_decode(val, strlen(val)); \
     871             :                 s = (unsigned char*)val; e = s + l; \
     872             :                 while (s < e) { \
     873             :                         if (iscntrl(*s)) { \
     874             :                                 php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "Invalid redirect URL! %s", new_path); \
     875             :                                 goto out; \
     876             :                         } \
     877             :                         s++; \
     878             :                 } \
     879             :         } \
     880             : }
     881             :                         /* check for control characters in login, password & path */
     882          14 :                         if (strncasecmp(new_path, "http://", sizeof("http://") - 1) || strncasecmp(new_path, "https://", sizeof("https://") - 1)) {
     883          14 :                                 CHECK_FOR_CNTRL_CHARS(resource->user)
     884          14 :                                 CHECK_FOR_CNTRL_CHARS(resource->pass)
     885          14 :                                 CHECK_FOR_CNTRL_CHARS(resource->path)
     886             :                         }
     887          14 :                         stream = php_stream_url_wrap_http_ex(wrapper, new_path, mode, options, opened_path, context, --redirect_max, HTTP_WRAPPER_REDIRECTED STREAMS_CC TSRMLS_CC);
     888             :                 } else {
     889           4 :                         php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "HTTP request failed! %s", tmp_line);
     890             :                 }
     891             :         }
     892             : out:
     893          62 :         if (protocol_version) {
     894           7 :                 efree(protocol_version);
     895             :         }
     896             : 
     897          62 :         if (http_header_line) {
     898          57 :                 efree(http_header_line);
     899             :         }
     900             : 
     901          62 :         if (scratch) {
     902          57 :                 efree(scratch);
     903             :         }
     904             : 
     905          62 :         if (resource) {
     906          62 :                 php_url_free(resource);
     907             :         }
     908             : 
     909          62 :         if (stream) {
     910          49 :                 if (header_init) {
     911          39 :                         zval_add_ref(&response_header);
     912          39 :                         stream->wrapperdata = response_header;
     913             :                 }
     914          49 :                 php_stream_notify_progress_init(context, 0, file_size);
     915             :                 
     916             :                 /* Restore original chunk size now that we're done with headers */
     917          49 :                 if (options & STREAM_WILL_CAST)
     918           0 :                         php_stream_set_chunk_size(stream, chunk_size);
     919             : 
     920             :                 /* restore the users auto-detect-line-endings setting */
     921          49 :                 stream->flags |= eol_detect;
     922             :                 
     923             :                 /* as far as streams are concerned, we are now at the start of
     924             :                  * the stream */
     925          49 :                 stream->position = 0;
     926             : 
     927             :                 /* restore mode */
     928          49 :                 strlcpy(stream->mode, mode, sizeof(stream->mode));
     929             : 
     930          49 :                 if (transfer_encoding) {
     931           0 :                         php_stream_filter_append(&stream->readfilters, transfer_encoding);
     932             :                 }
     933          13 :         } else if (transfer_encoding) {
     934           0 :                 php_stream_filter_free(transfer_encoding TSRMLS_CC);
     935             :         }
     936             : 
     937          62 :         return stream;
     938             : }
     939             : /* }}} */
     940             : 
     941          51 : php_stream *php_stream_url_wrap_http(php_stream_wrapper *wrapper, const char *path, const char *mode, int options, char **opened_path, php_stream_context *context STREAMS_DC TSRMLS_DC) /* {{{ */
     942             : {
     943          51 :         return php_stream_url_wrap_http_ex(wrapper, path, mode, options, opened_path, context, PHP_URL_REDIRECT_MAX, HTTP_WRAPPER_HEADER_INIT STREAMS_CC TSRMLS_CC);
     944             : }
     945             : /* }}} */
     946             : 
     947          12 : static int php_stream_http_stream_stat(php_stream_wrapper *wrapper, php_stream *stream, php_stream_statbuf *ssb TSRMLS_DC) /* {{{ */
     948             : {
     949             :         /* one day, we could fill in the details based on Date: and Content-Length:
     950             :          * headers.  For now, we return with a failure code to prevent the underlying
     951             :          * file's details from being used instead. */
     952          12 :         return -1;
     953             : }
     954             : /* }}} */
     955             : 
     956             : static php_stream_wrapper_ops http_stream_wops = {
     957             :         php_stream_url_wrap_http,
     958             :         NULL, /* stream_close */
     959             :         php_stream_http_stream_stat,
     960             :         NULL, /* stat_url */
     961             :         NULL, /* opendir */
     962             :         "http",
     963             :         NULL, /* unlink */
     964             :         NULL, /* rename */
     965             :         NULL, /* mkdir */
     966             :         NULL  /* rmdir */
     967             : };
     968             : 
     969             : PHPAPI php_stream_wrapper php_stream_http_wrapper = {
     970             :         &http_stream_wops,
     971             :         NULL,
     972             :         1 /* is_url */
     973             : };
     974             : 
     975             : /*
     976             :  * Local variables:
     977             :  * tab-width: 4
     978             :  * c-basic-offset: 4
     979             :  * End:
     980             :  * vim600: sw=4 ts=4 fdm=marker
     981             :  * vim<600: sw=4 ts=4
     982             :  */

Generated by: LCOV version 1.10

Generated at Wed, 16 Apr 2014 12:47:57 +0000 (6 days ago)

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