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

Generated by: LCOV version 1.10

Generated at Fri, 31 Jul 2015 08:58:58 +0000 (2 days ago)

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