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: 278 458 60.7 %
Date: 2016-05-28 Functions: 4 4 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Sun, 29 May 2016 00:48:34 +0000 (45 hours ago)

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