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: 255 432 59.0 %
Date: 2016-09-27 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          97 : 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          97 :         php_stream *stream = NULL;
     117          97 :         php_url *resource = NULL;
     118             :         int use_ssl;
     119          97 :         int use_proxy = 0;
     120          97 :         zend_string *tmp = NULL;
     121          97 :         char *ua_str = NULL;
     122          97 :         zval *ua_zval = NULL, *tmpzval = NULL, ssl_proxy_peer_name;
     123          97 :         int body = 0;
     124             :         char location[HTTP_HEADER_BLOCK_SIZE];
     125             :         zval response_header;
     126          97 :         int reqok = 0;
     127          97 :         char *http_header_line = NULL;
     128             :         char tmp_line[128];
     129          97 :         size_t chunk_size = 0, file_size = 0;
     130          97 :         int eol_detect = 0;
     131             :         char *transport_string;
     132          97 :         zend_string *errstr = NULL;
     133             :         size_t transport_len;
     134          97 :         int have_header = 0;
     135          97 :         zend_bool request_fulluri = 0, ignore_errors = 0;
     136             :         struct timeval timeout;
     137          97 :         char *user_headers = NULL;
     138          97 :         int header_init = ((flags & HTTP_WRAPPER_HEADER_INIT) != 0);
     139          97 :         int redirected = ((flags & HTTP_WRAPPER_REDIRECTED) != 0);
     140          97 :         zend_bool follow_location = 1;
     141          97 :         php_stream_filter *transfer_encoding = NULL;
     142             :         int response_code;
     143             :         zend_array *symbol_table;
     144          97 :         smart_str req_buf = {0};
     145             :         zend_bool custom_request_method;
     146             : 
     147          97 :         ZVAL_UNDEF(&response_header);
     148          97 :         tmp_line[0] = '\0';
     149             : 
     150          97 :         if (redirect_max < 1) {
     151           3 :                 php_stream_wrapper_log_error(wrapper, options, "Redirection limit reached, aborting");
     152           3 :                 return NULL;
     153             :         }
     154             : 
     155          94 :         resource = php_url_parse(path);
     156          94 :         if (resource == NULL) {
     157           0 :                 return NULL;
     158             :         }
     159             : 
     160          94 :         if (strncasecmp(resource->scheme, "http", sizeof("http")) && strncasecmp(resource->scheme, "https", sizeof("https"))) {
     161           0 :                 if (!context ||
     162           0 :                         (tmpzval = php_stream_context_get_option(context, wrapper->wops->label, "proxy")) == NULL ||
     163             :                         Z_TYPE_P(tmpzval) != IS_STRING ||
     164           0 :                         Z_STRLEN_P(tmpzval) <= 0) {
     165           0 :                         php_url_free(resource);
     166           0 :                         return php_stream_open_wrapper_ex(path, mode, REPORT_ERRORS, NULL, context);
     167             :                 }
     168             :                 /* Called from a non-http wrapper with http proxying requested (i.e. ftp) */
     169           0 :                 request_fulluri = 1;
     170           0 :                 use_ssl = 0;
     171           0 :                 use_proxy = 1;
     172             : 
     173           0 :                 transport_len = Z_STRLEN_P(tmpzval);
     174           0 :                 transport_string = estrndup(Z_STRVAL_P(tmpzval), Z_STRLEN_P(tmpzval));
     175             :         } else {
     176             :                 /* Normal http request (possibly with proxy) */
     177             : 
     178          94 :                 if (strpbrk(mode, "awx+")) {
     179           0 :                         php_stream_wrapper_log_error(wrapper, options, "HTTP wrapper does not support writeable connections");
     180           0 :                         php_url_free(resource);
     181           0 :                         return NULL;
     182             :                 }
     183             : 
     184          94 :                 use_ssl = resource->scheme && (strlen(resource->scheme) > 4) && resource->scheme[4] == 's';
     185             :                 /* choose default ports */
     186          97 :                 if (use_ssl && resource->port == 0)
     187           3 :                         resource->port = 443;
     188          91 :                 else if (resource->port == 0)
     189           4 :                         resource->port = 80;
     190             : 
     191         186 :                 if (context &&
     192          92 :                         (tmpzval = php_stream_context_get_option(context, wrapper->wops->label, "proxy")) != NULL &&
     193             :                         Z_TYPE_P(tmpzval) == IS_STRING &&
     194           0 :                         Z_STRLEN_P(tmpzval) > 0) {
     195           0 :                         use_proxy = 1;
     196           0 :                         transport_len = Z_STRLEN_P(tmpzval);
     197           0 :                         transport_string = estrndup(Z_STRVAL_P(tmpzval), Z_STRLEN_P(tmpzval));
     198             :                 } else {
     199          94 :                         transport_len = spprintf(&transport_string, 0, "%s://%s:%d", use_ssl ? "ssl" : "tcp", resource->host, resource->port);
     200             :                 }
     201             :         }
     202             : 
     203          97 :         if (context && (tmpzval = php_stream_context_get_option(context, wrapper->wops->label, "timeout")) != NULL) {
     204           3 :                 double d = zval_get_double(tmpzval);
     205             : #ifndef PHP_WIN32
     206           3 :                 timeout.tv_sec = (time_t) d;
     207           3 :                 timeout.tv_usec = (size_t) ((d - timeout.tv_sec) * 1000000);
     208             : #else
     209             :                 timeout.tv_sec = (long) d;
     210             :                 timeout.tv_usec = (long) ((d - timeout.tv_sec) * 1000000);
     211             : #endif
     212             :         } else {
     213             : #ifndef PHP_WIN32
     214          91 :                 timeout.tv_sec = FG(default_socket_timeout);
     215             : #else
     216             :                 timeout.tv_sec = (long)FG(default_socket_timeout);
     217             : #endif
     218          91 :                 timeout.tv_usec = 0;
     219             :         }
     220             : 
     221          94 :         stream = php_stream_xport_create(transport_string, transport_len, options,
     222             :                         STREAM_XPORT_CLIENT | STREAM_XPORT_CONNECT,
     223             :                         NULL, &timeout, context, &errstr, NULL);
     224             : 
     225          94 :         if (stream) {
     226          65 :                 php_stream_set_option(stream, PHP_STREAM_OPTION_READ_TIMEOUT, 0, &timeout);
     227             :         }
     228             : 
     229          94 :         if (errstr) {
     230          28 :                 php_stream_wrapper_log_error(wrapper, options, "%s", ZSTR_VAL(errstr));
     231          28 :                 zend_string_release(errstr);
     232          28 :                 errstr = NULL;
     233             :         }
     234             : 
     235          94 :         efree(transport_string);
     236             : 
     237          94 :         if (stream && use_proxy && use_ssl) {
     238           0 :                 smart_str header = {0};
     239             : 
     240             :                 /* Set peer_name or name verification will try to use the proxy server name */
     241           0 :                 if (!context || (tmpzval = php_stream_context_get_option(context, "ssl", "peer_name")) == NULL) {
     242           0 :                         ZVAL_STRING(&ssl_proxy_peer_name, resource->host);
     243           0 :                         php_stream_context_set_option(PHP_STREAM_CONTEXT(stream), "ssl", "peer_name", &ssl_proxy_peer_name);
     244           0 :                         zval_ptr_dtor(&ssl_proxy_peer_name);
     245             :                 }
     246             : 
     247             :                 smart_str_appendl(&header, "CONNECT ", sizeof("CONNECT ")-1);
     248           0 :                 smart_str_appends(&header, resource->host);
     249             :                 smart_str_appendc(&header, ':');
     250           0 :                 smart_str_append_unsigned(&header, resource->port);
     251             :                 smart_str_appendl(&header, " HTTP/1.0\r\n", sizeof(" HTTP/1.0\r\n")-1);
     252             : 
     253             :             /* check if we have Proxy-Authorization header */
     254           0 :                 if (context && (tmpzval = php_stream_context_get_option(context, "http", "header")) != NULL) {
     255             :                         char *s, *p;
     256             : 
     257           0 :                         if (Z_TYPE_P(tmpzval) == IS_ARRAY) {
     258           0 :                                 zval *tmpheader = NULL;
     259             : 
     260           0 :                                 ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(tmpzval), tmpheader) {
     261           0 :                                         if (Z_TYPE_P(tmpheader) == IS_STRING) {
     262           0 :                                                 s = Z_STRVAL_P(tmpheader);
     263             :                                                 do {
     264           0 :                                                         while (*s == ' ' || *s == '\t') s++;
     265           0 :                                                         p = s;
     266           0 :                                                         while (*p != 0 && *p != ':' && *p != '\r' && *p !='\n') p++;
     267           0 :                                                         if (*p == ':') {
     268           0 :                                                                 p++;
     269           0 :                                                                 if (p - s == sizeof("Proxy-Authorization:") - 1 &&
     270             :                                                                     zend_binary_strcasecmp(s, sizeof("Proxy-Authorization:") - 1,
     271           0 :                                                                         "Proxy-Authorization:", sizeof("Proxy-Authorization:") - 1) == 0) {
     272           0 :                                                                         while (*p != 0 && *p != '\r' && *p !='\n') p++;
     273           0 :                                                                         smart_str_appendl(&header, s, p - s);
     274             :                                                                         smart_str_appendl(&header, "\r\n", sizeof("\r\n")-1);
     275           0 :                                                                         goto finish;
     276             :                                                                 } else {
     277           0 :                                                                         while (*p != 0 && *p != '\r' && *p !='\n') p++;
     278             :                                                                 }
     279             :                                                         }
     280           0 :                                                         s = p;
     281           0 :                                                         while (*s == '\r' || *s == '\n') s++;
     282           0 :                                                 } while (*s != 0);
     283             :                                         }
     284             :                                 } ZEND_HASH_FOREACH_END();
     285           0 :                         } else if (Z_TYPE_P(tmpzval) == IS_STRING && Z_STRLEN_P(tmpzval)) {
     286           0 :                                 s = Z_STRVAL_P(tmpzval);
     287             :                                 do {
     288           0 :                                         while (*s == ' ' || *s == '\t') s++;
     289           0 :                                         p = s;
     290           0 :                                         while (*p != 0 && *p != ':' && *p != '\r' && *p !='\n') p++;
     291           0 :                                         if (*p == ':') {
     292           0 :                                                 p++;
     293           0 :                                                 if (p - s == sizeof("Proxy-Authorization:") - 1 &&
     294             :                                                     zend_binary_strcasecmp(s, sizeof("Proxy-Authorization:") - 1,
     295           0 :                                                         "Proxy-Authorization:", sizeof("Proxy-Authorization:") - 1) == 0) {
     296           0 :                                                         while (*p != 0 && *p != '\r' && *p !='\n') p++;
     297           0 :                                                         smart_str_appendl(&header, s, p - s);
     298             :                                                         smart_str_appendl(&header, "\r\n", sizeof("\r\n")-1);
     299           0 :                                                         goto finish;
     300             :                                                 } else {
     301           0 :                                                         while (*p != 0 && *p != '\r' && *p !='\n') p++;
     302             :                                                 }
     303             :                                         }
     304           0 :                                         s = p;
     305           0 :                                         while (*s == '\r' || *s == '\n') s++;
     306           0 :                                 } while (*s != 0);
     307             :                         }
     308             :                 }
     309             : finish:
     310             :                 smart_str_appendl(&header, "\r\n", sizeof("\r\n")-1);
     311             : 
     312           0 :                 if (php_stream_write(stream, ZSTR_VAL(header.s), ZSTR_LEN(header.s)) != ZSTR_LEN(header.s)) {
     313           0 :                         php_stream_wrapper_log_error(wrapper, options, "Cannot connect to HTTPS server through proxy");
     314           0 :                         php_stream_close(stream);
     315           0 :                         stream = NULL;
     316             :                 }
     317             :                 smart_str_free(&header);
     318             : 
     319           0 :                 if (stream) {
     320             :                         char header_line[HTTP_HEADER_BLOCK_SIZE];
     321             : 
     322             :                         /* get response header */
     323           0 :                         while (php_stream_gets(stream, header_line, HTTP_HEADER_BLOCK_SIZE-1) != NULL) {
     324           0 :                                 if (header_line[0] == '\n' ||
     325           0 :                                     header_line[0] == '\r' ||
     326           0 :                                     header_line[0] == '\0') {
     327             :                                   break;
     328             :                                 }
     329             :                         }
     330             :                 }
     331             : 
     332             :                 /* enable SSL transport layer */
     333           0 :                 if (stream) {
     334           0 :                         if (php_stream_xport_crypto_setup(stream, STREAM_CRYPTO_METHOD_SSLv23_CLIENT, NULL) < 0 ||
     335           0 :                             php_stream_xport_crypto_enable(stream, 1) < 0) {
     336           0 :                                 php_stream_wrapper_log_error(wrapper, options, "Cannot connect to HTTPS server through proxy");
     337           0 :                                 php_stream_close(stream);
     338           0 :                                 stream = NULL;
     339             :                         }
     340             :                 }
     341             :         }
     342             : 
     343          94 :         if (stream == NULL)
     344          29 :                 goto out;
     345             : 
     346             :         /* avoid buffering issues while reading header */
     347          65 :         if (options & STREAM_WILL_CAST)
     348           0 :                 chunk_size = php_stream_set_chunk_size(stream, 1);
     349             : 
     350             :         /* avoid problems with auto-detecting when reading the headers -> the headers
     351             :          * are always in canonical \r\n format */
     352          65 :         eol_detect = stream->flags & (PHP_STREAM_FLAG_DETECT_EOL | PHP_STREAM_FLAG_EOL_MAC);
     353          65 :         stream->flags &= ~(PHP_STREAM_FLAG_DETECT_EOL | PHP_STREAM_FLAG_EOL_MAC);
     354             : 
     355          65 :         php_stream_context_set(stream, context);
     356             : 
     357          65 :         php_stream_notify_info(context, PHP_STREAM_NOTIFY_CONNECT, NULL, 0);
     358             : 
     359          65 :         if (header_init && context && (tmpzval = php_stream_context_get_option(context, "http", "max_redirects")) != NULL) {
     360           6 :                 redirect_max = (int)zval_get_long(tmpzval);
     361             :         }
     362             : 
     363          65 :         custom_request_method = 0;
     364          65 :         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 :                                 custom_request_method = 1;
     373          10 :                                 smart_str_append(&req_buf, Z_STR_P(tmpzval));
     374             :                                 smart_str_appendc(&req_buf, ' ');
     375             :                         }
     376             :                 }
     377             :         }
     378             : 
     379          65 :         if (!custom_request_method) {
     380             :                 smart_str_appends(&req_buf, "GET ");
     381             :         }
     382             : 
     383             :         /* Should we send the entire path in the request line, default to no. */
     384         128 :         if (!request_fulluri && context &&
     385          63 :                 (tmpzval = php_stream_context_get_option(context, "http", "request_fulluri")) != NULL) {
     386           0 :                 request_fulluri = zend_is_true(tmpzval);
     387             :         }
     388             : 
     389          65 :         if (request_fulluri) {
     390             :                 /* Ask for everything */
     391           0 :                 smart_str_appends(&req_buf, path);
     392             :         } else {
     393             :                 /* Send the traditional /path/to/file?query_string */
     394             : 
     395             :                 /* file */
     396         127 :                 if (resource->path && *resource->path) {
     397          62 :                         smart_str_appends(&req_buf, resource->path);
     398             :                 } else {
     399             :                         smart_str_appendc(&req_buf, '/');
     400             :                 }
     401             : 
     402             :                 /* query string */
     403          65 :                 if (resource->query) {
     404             :                         smart_str_appendc(&req_buf, '?');
     405           0 :                         smart_str_appends(&req_buf, resource->query);
     406             :                 }
     407             :         }
     408             : 
     409             :         /* protocol version we are speaking */
     410          73 :         if (context && (tmpzval = php_stream_context_get_option(context, "http", "protocol_version")) != NULL) {
     411             :                 char *protocol_version;
     412           8 :                 spprintf(&protocol_version, 0, "%.1F", zval_get_double(tmpzval));
     413             : 
     414             :                 smart_str_appends(&req_buf, " HTTP/");
     415           8 :                 smart_str_appends(&req_buf, protocol_version);
     416             :                 smart_str_appends(&req_buf, "\r\n");
     417           8 :                 efree(protocol_version);
     418             :         } else {
     419             :                 smart_str_appends(&req_buf, " HTTP/1.0\r\n");
     420             :         }
     421             : 
     422          65 :         if (context && (tmpzval = php_stream_context_get_option(context, "http", "header")) != NULL) {
     423          20 :                 tmp = NULL;
     424             : 
     425          20 :                 if (Z_TYPE_P(tmpzval) == IS_ARRAY) {
     426           1 :                         zval *tmpheader = NULL;
     427           1 :                         smart_str tmpstr = {0};
     428             : 
     429           5 :                         ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(tmpzval), tmpheader) {
     430           2 :                                 if (Z_TYPE_P(tmpheader) == IS_STRING) {
     431           2 :                                         smart_str_append(&tmpstr, Z_STR_P(tmpheader));
     432             :                                         smart_str_appendl(&tmpstr, "\r\n", sizeof("\r\n") - 1);
     433             :                                 }
     434             :                         } ZEND_HASH_FOREACH_END();
     435             :                         smart_str_0(&tmpstr);
     436             :                         /* Remove newlines and spaces from start and end. there's at least one extra \r\n at the end that needs to go. */
     437           1 :                         if (tmpstr.s) {
     438           1 :                                 tmp = php_trim(tmpstr.s, NULL, 0, 3);
     439             :                                 smart_str_free(&tmpstr);
     440             :                         }
     441          19 :                 } else if (Z_TYPE_P(tmpzval) == IS_STRING && Z_STRLEN_P(tmpzval)) {
     442             :                         /* Remove newlines and spaces from start and end php_trim will estrndup() */
     443          19 :                         tmp = php_trim(Z_STR_P(tmpzval), NULL, 0, 3);
     444             :                 }
     445          20 :                 if (tmp && ZSTR_LEN(tmp)) {
     446             :                         char *s;
     447             :                         char *t;
     448             : 
     449          20 :                         user_headers = estrndup(ZSTR_VAL(tmp), ZSTR_LEN(tmp));
     450             : 
     451          20 :                         if (ZSTR_IS_INTERNED(tmp)) {
     452          16 :                                 tmp = zend_string_init(ZSTR_VAL(tmp), ZSTR_LEN(tmp), 0);
     453          12 :                         } else if (GC_REFCOUNT(tmp) > 1) {
     454           5 :                                 GC_REFCOUNT(tmp)--;
     455          10 :                                 tmp = zend_string_init(ZSTR_VAL(tmp), ZSTR_LEN(tmp), 0);
     456             :                         }
     457             : 
     458             :                         /* Make lowercase for easy comparison against 'standard' headers */
     459          20 :                         php_strtolower(ZSTR_VAL(tmp), ZSTR_LEN(tmp));
     460          20 :                         t = ZSTR_VAL(tmp);
     461             : 
     462          20 :                         if (!header_init) {
     463             :                                 /* strip POST headers on redirect */
     464           7 :                                 strip_header(user_headers, t, "content-length:");
     465           7 :                                 strip_header(user_headers, t, "content-type:");
     466             :                         }
     467             : 
     468          20 :                         if ((s = strstr(t, "user-agent:")) &&
     469           0 :                             (s == t || *(s-1) == '\r' || *(s-1) == '\n' ||
     470           0 :                                          *(s-1) == '\t' || *(s-1) == ' ')) {
     471           0 :                                  have_header |= HTTP_HEADER_USER_AGENT;
     472             :                         }
     473          20 :                         if ((s = strstr(t, "host:")) &&
     474           0 :                             (s == t || *(s-1) == '\r' || *(s-1) == '\n' ||
     475           0 :                                          *(s-1) == '\t' || *(s-1) == ' ')) {
     476           0 :                                  have_header |= HTTP_HEADER_HOST;
     477             :                         }
     478          20 :                         if ((s = strstr(t, "from:")) &&
     479           0 :                             (s == t || *(s-1) == '\r' || *(s-1) == '\n' ||
     480           0 :                                          *(s-1) == '\t' || *(s-1) == ' ')) {
     481           0 :                                  have_header |= HTTP_HEADER_FROM;
     482             :                                 }
     483          20 :                         if ((s = strstr(t, "authorization:")) &&
     484           0 :                             (s == t || *(s-1) == '\r' || *(s-1) == '\n' ||
     485           0 :                                          *(s-1) == '\t' || *(s-1) == ' ')) {
     486           0 :                                  have_header |= HTTP_HEADER_AUTH;
     487             :                         }
     488          20 :                         if ((s = strstr(t, "content-length:")) &&
     489           0 :                             (s == t || *(s-1) == '\r' || *(s-1) == '\n' ||
     490           0 :                                          *(s-1) == '\t' || *(s-1) == ' ')) {
     491           0 :                                  have_header |= HTTP_HEADER_CONTENT_LENGTH;
     492             :                         }
     493          36 :                         if ((s = strstr(t, "content-type:")) &&
     494          16 :                             (s == t || *(s-1) == '\r' || *(s-1) == '\n' ||
     495           0 :                                          *(s-1) == '\t' || *(s-1) == ' ')) {
     496           8 :                                  have_header |= HTTP_HEADER_TYPE;
     497             :                         }
     498          20 :                         if ((s = strstr(t, "connection:")) &&
     499           0 :                             (s == t || *(s-1) == '\r' || *(s-1) == '\n' ||
     500           0 :                                          *(s-1) == '\t' || *(s-1) == ' ')) {
     501           6 :                                  have_header |= HTTP_HEADER_CONNECTION;
     502             :                         }
     503             :                         /* remove Proxy-Authorization header */
     504          20 :                         if (use_proxy && use_ssl && (s = strstr(t, "proxy-authorization:")) &&
     505           0 :                             (s == t || *(s-1) == '\r' || *(s-1) == '\n' ||
     506           0 :                                          *(s-1) == '\t' || *(s-1) == ' ')) {
     507           0 :                                 char *p = s + sizeof("proxy-authorization:") - 1;
     508             : 
     509           0 :                                 while (s > t && (*(s-1) == ' ' || *(s-1) == '\t')) s--;
     510           0 :                                 while (*p != 0 && *p != '\r' && *p != '\n') p++;
     511           0 :                                 while (*p == '\r' || *p == '\n') p++;
     512           0 :                                 if (*p == 0) {
     513           0 :                                         if (s == t) {
     514           0 :                                                 efree(user_headers);
     515           0 :                                                 user_headers = NULL;
     516             :                                         } else {
     517           0 :                                                 while (s > t && (*(s-1) == '\r' || *(s-1) == '\n')) s--;
     518           0 :                                                 user_headers[s - t] = 0;
     519             :                                         }
     520             :                                 } else {
     521           0 :                                         memmove(user_headers + (s - t), user_headers + (p - t), strlen(p) + 1);
     522             :                                 }
     523             :                         }
     524             : 
     525             :                 }
     526          20 :                 if (tmp) {
     527             :                         zend_string_release(tmp);
     528             :                 }
     529             :         }
     530             : 
     531             :         /* auth header if it was specified */
     532          65 :         if (((have_header & HTTP_HEADER_AUTH) == 0) && resource->user) {
     533             :                 /* make scratch large enough to hold the whole URL (over-estimate) */
     534           0 :                 size_t scratch_len = strlen(path) + 1;
     535           0 :                 char *scratch = emalloc(scratch_len);
     536             :                 zend_string *stmp;
     537             : 
     538             :                 /* decode the strings first */
     539           0 :                 php_url_decode(resource->user, strlen(resource->user));
     540             : 
     541           0 :                 strcpy(scratch, resource->user);
     542           0 :                 strcat(scratch, ":");
     543             : 
     544             :                 /* Note: password is optional! */
     545           0 :                 if (resource->pass) {
     546           0 :                         php_url_decode(resource->pass, strlen(resource->pass));
     547           0 :                         strcat(scratch, resource->pass);
     548             :                 }
     549             : 
     550           0 :                 stmp = php_base64_encode((unsigned char*)scratch, strlen(scratch));
     551             : 
     552             :                 smart_str_appends(&req_buf, "Authorization: Basic ");
     553           0 :                 smart_str_appends(&req_buf, ZSTR_VAL(stmp));
     554             :                 smart_str_appends(&req_buf, "\r\n");
     555             : 
     556           0 :                 php_stream_notify_info(context, PHP_STREAM_NOTIFY_AUTH_REQUIRED, NULL, 0);
     557             : 
     558             :                 zend_string_free(stmp);
     559           0 :                 efree(scratch);
     560             :         }
     561             : 
     562             :         /* if the user has configured who they are, send a From: line */
     563          65 :         if (!(have_header & HTTP_HEADER_FROM) && FG(from_address)) {
     564             :                 smart_str_appends(&req_buf, "From: ");
     565           2 :                 smart_str_appends(&req_buf, FG(from_address));
     566             :                 smart_str_appends(&req_buf, "\r\n");
     567             :         }
     568             : 
     569             :         /* Send Host: header so name-based virtual hosts work */
     570          65 :         if ((have_header & HTTP_HEADER_HOST) == 0) {
     571             :                 smart_str_appends(&req_buf, "Host: ");
     572          65 :                 smart_str_appends(&req_buf, resource->host);
     573         181 :                 if ((use_ssl && resource->port != 443 && resource->port != 0) ||
     574         116 :                         (!use_ssl && resource->port != 80 && resource->port != 0)) {
     575             :                         smart_str_appendc(&req_buf, ':');
     576          59 :                         smart_str_append_unsigned(&req_buf, resource->port);
     577             :                 }
     578             :                 smart_str_appends(&req_buf, "\r\n");
     579             :         }
     580             : 
     581             :         /* Send a Connection: close header to avoid hanging when the server
     582             :          * interprets the RFC literally and establishes a keep-alive connection,
     583             :          * unless the user specifically requests something else by specifying a
     584             :          * Connection header in the context options. Send that header even for
     585             :          * HTTP/1.0 to avoid issues when the server respond with a HTTP/1.1
     586             :          * keep-alive response, which is the preferred response type. */
     587          65 :         if ((have_header & HTTP_HEADER_CONNECTION) == 0) {
     588             :                 smart_str_appends(&req_buf, "Connection: close\r\n");
     589             :         }
     590             : 
     591         128 :         if (context &&
     592          63 :             (ua_zval = php_stream_context_get_option(context, "http", "user_agent")) != NULL &&
     593             :                 Z_TYPE_P(ua_zval) == IS_STRING) {
     594           0 :                 ua_str = Z_STRVAL_P(ua_zval);
     595          65 :         } else if (FG(user_agent)) {
     596           0 :                 ua_str = FG(user_agent);
     597             :         }
     598             : 
     599          65 :         if (((have_header & HTTP_HEADER_USER_AGENT) == 0) && ua_str) {
     600             : #define _UA_HEADER "User-Agent: %s\r\n"
     601             :                 char *ua;
     602             :                 size_t ua_len;
     603             : 
     604           0 :                 ua_len = sizeof(_UA_HEADER) + strlen(ua_str);
     605             : 
     606             :                 /* ensure the header is only sent if user_agent is not blank */
     607           0 :                 if (ua_len > sizeof(_UA_HEADER)) {
     608           0 :                         ua = emalloc(ua_len + 1);
     609           0 :                         if ((ua_len = slprintf(ua, ua_len, _UA_HEADER, ua_str)) > 0) {
     610           0 :                                 ua[ua_len] = 0;
     611             :                                 smart_str_appendl(&req_buf, ua, ua_len);
     612             :                         } else {
     613           0 :                                 php_error_docref(NULL, E_WARNING, "Cannot construct User-agent header");
     614             :                         }
     615           0 :                         efree(ua);
     616             :                 }
     617             :         }
     618             : 
     619          65 :         if (user_headers) {
     620             :                 /* A bit weird, but some servers require that Content-Length be sent prior to Content-Type for POST
     621             :                  * see bug #44603 for details. Since Content-Type maybe part of user's headers we need to do this check first.
     622             :                  */
     623          50 :                 if (
     624             :                                 header_init &&
     625             :                                 context &&
     626          13 :                                 !(have_header & HTTP_HEADER_CONTENT_LENGTH) &&
     627          13 :                                 (tmpzval = php_stream_context_get_option(context, "http", "content")) != NULL &&
     628           2 :                                 Z_TYPE_P(tmpzval) == IS_STRING && Z_STRLEN_P(tmpzval) > 0
     629             :                 ) {
     630             :                         smart_str_appends(&req_buf, "Content-Length: ");
     631           2 :                         smart_str_append_unsigned(&req_buf, Z_STRLEN_P(tmpzval));
     632             :                         smart_str_appends(&req_buf, "\r\n");
     633           2 :                         have_header |= HTTP_HEADER_CONTENT_LENGTH;
     634             :                 }
     635             : 
     636          20 :                 smart_str_appends(&req_buf, user_headers);
     637             :                 smart_str_appends(&req_buf, "\r\n");
     638          20 :                 efree(user_headers);
     639             :         }
     640             : 
     641             :         /* Request content, such as for POST requests */
     642         118 :         if (header_init && context &&
     643          47 :                 (tmpzval = php_stream_context_get_option(context, "http", "content")) != NULL &&
     644           2 :                 Z_TYPE_P(tmpzval) == IS_STRING && Z_STRLEN_P(tmpzval) > 0) {
     645           2 :                 if (!(have_header & HTTP_HEADER_CONTENT_LENGTH)) {
     646             :                         smart_str_appends(&req_buf, "Content-Length: ");
     647           0 :                         smart_str_append_unsigned(&req_buf, Z_STRLEN_P(tmpzval));
     648             :                         smart_str_appends(&req_buf, "\r\n");
     649             :                 }
     650           2 :                 if (!(have_header & HTTP_HEADER_TYPE)) {
     651             :                         smart_str_appends(&req_buf, "Content-Type: application/x-www-form-urlencoded\r\n");
     652           0 :                         php_error_docref(NULL, E_NOTICE, "Content-type not specified assuming application/x-www-form-urlencoded");
     653             :                 }
     654             :                 smart_str_appends(&req_buf, "\r\n");
     655           2 :                 smart_str_appendl(&req_buf, Z_STRVAL_P(tmpzval), Z_STRLEN_P(tmpzval));
     656             :         } else {
     657             :                 smart_str_appends(&req_buf, "\r\n");
     658             :         }
     659             : 
     660             :         /* send it */
     661          65 :         php_stream_write(stream, ZSTR_VAL(req_buf.s), ZSTR_LEN(req_buf.s));
     662             : 
     663          65 :         location[0] = '\0';
     664             : 
     665          65 :         symbol_table = zend_rebuild_symbol_table();
     666             : 
     667          65 :         if (header_init) {
     668             :                 zval ztmp;
     669          48 :                 array_init(&ztmp);
     670          48 :                 zend_set_local_var_str("http_response_header", sizeof("http_response_header")-1, &ztmp, 0);
     671             :         }
     672             : 
     673             :         {
     674          65 :                 zval *response_header_ptr = zend_hash_str_find_ind(symbol_table, "http_response_header", sizeof("http_response_header")-1);
     675         130 :                 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          64 :                         ZVAL_COPY(&response_header, response_header_ptr);
     680             :                 }
     681             :         }
     682             : 
     683          64 :         if (!php_stream_eof(stream)) {
     684             :                 size_t tmp_line_len;
     685             :                 /* get response header */
     686             : 
     687          64 :                 if (php_stream_get_line(stream, tmp_line, sizeof(tmp_line) - 1, &tmp_line_len) != NULL) {
     688             :                         zval http_response;
     689             : 
     690          64 :                         if (tmp_line_len > 9) {
     691          64 :                                 response_code = atoi(tmp_line + 9);
     692             :                         } else {
     693           0 :                                 response_code = 0;
     694             :                         }
     695          64 :                         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          64 :                         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         121 :                         if (response_code >= 200 && response_code < 400) {
     706          57 :                                 reqok = 1;
     707             :                         } else {
     708           7 :                                 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           7 :                                                 if (!tmp_line_len) {
     716           0 :                                                         tmp_line[0] = '\0';
     717             :                                                 }
     718           7 :                                                 php_stream_notify_error(context, PHP_STREAM_NOTIFY_FAILURE,
     719             :                                                                 tmp_line, response_code);
     720             :                                 }
     721             :                         }
     722          64 :                         if (tmp_line[tmp_line_len - 1] == '\n') {
     723          64 :                                 --tmp_line_len;
     724          64 :                                 if (tmp_line[tmp_line_len - 1] == '\r') {
     725          63 :                                         --tmp_line_len;
     726             :                                 }
     727             :                         }
     728         128 :                         ZVAL_STRINGL(&http_response, tmp_line, tmp_line_len);
     729          64 :                         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          64 :         http_header_line = emalloc(HTTP_HEADER_BLOCK_SIZE);
     739             : 
     740         228 :         while (!body && !php_stream_eof(stream)) {
     741             :                 size_t http_header_line_length;
     742         164 :                 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          32 :                                 if (context && (tmpzval = php_stream_context_get_option(context, "http", "follow_location")) != NULL) {
     762           8 :                                         follow_location = zval_is_true(tmpzval);
     763          16 :                                 } 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          24 :                                 strlcpy(location, http_header_line + 10, sizeof(location));
     771          76 :                         } else if (!strncasecmp(http_header_line, "Content-Type: ", 14)) {
     772           8 :                                 php_stream_notify_info(context, PHP_STREAM_NOTIFY_MIME_TYPE_IS, http_header_line + 14, 0);
     773          68 :                         } else if (!strncasecmp(http_header_line, "Content-Length: ", 16)) {
     774           8 :                                 file_size = atoi(http_header_line + 16);
     775           8 :                                 php_stream_notify_file_size(context, file_size, http_header_line, 0);
     776          60 :                         } 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          64 :         if (!reqok || (location[0] != '\0' && follow_location)) {
     810          28 :                 if (!follow_location || (((options & STREAM_ONLY_GET_HEADERS) || ignore_errors) && redirect_max <= 1)) {
     811             :                         goto out;
     812             :                 }
     813             : 
     814          25 :                 if (location[0] != '\0')
     815          20 :                         php_stream_notify_info(context, PHP_STREAM_NOTIFY_REDIRECTED, location, 0);
     816             : 
     817          25 :                 php_stream_close(stream);
     818          25 :                 stream = NULL;
     819             : 
     820          25 :                 if (location[0] != '\0') {
     821             : 
     822             :                         char new_path[HTTP_HEADER_BLOCK_SIZE];
     823             :                         char loc_path[HTTP_HEADER_BLOCK_SIZE];
     824             : 
     825          20 :                         *new_path='\0';
     826          29 :                         if (strlen(location)<8 || (strncasecmp(location, "http://", sizeof("http://")-1) &&
     827           2 :                                                         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          13 :                                 strlcpy(new_path, location, sizeof(new_path));
     862             :                         }
     863             : 
     864          20 :                         php_url_free(resource);
     865             :                         /* check for invalid redirection URLs */
     866          20 :                         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          20 :                         if (strncasecmp(new_path, "http://", sizeof("http://") - 1) || strncasecmp(new_path, "https://", sizeof("https://") - 1)) {
     888          20 :                                 CHECK_FOR_CNTRL_CHARS(resource->user)
     889          20 :                                 CHECK_FOR_CNTRL_CHARS(resource->pass)
     890          20 :                                 CHECK_FOR_CNTRL_CHARS(resource->path)
     891             :                         }
     892          20 :                         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           5 :                         php_stream_wrapper_log_error(wrapper, options, "HTTP request failed! %s", tmp_line);
     895             :                 }
     896             :         }
     897             : out:
     898             : 
     899             :         smart_str_free(&req_buf);
     900             : 
     901          94 :         if (http_header_line) {
     902          64 :                 efree(http_header_line);
     903             :         }
     904             : 
     905          94 :         if (resource) {
     906          94 :                 php_url_free(resource);
     907             :         }
     908             : 
     909          94 :         if (stream) {
     910          55 :                 if (header_init) {
     911          40 :                         ZVAL_COPY(&stream->wrapperdata, &response_header);
     912             :                 }
     913          55 :                 php_stream_notify_progress_init(context, 0, file_size);
     914             : 
     915             :                 /* Restore original chunk size now that we're done with headers */
     916          55 :                 if (options & STREAM_WILL_CAST)
     917           0 :                         php_stream_set_chunk_size(stream, (int)chunk_size);
     918             : 
     919             :                 /* restore the users auto-detect-line-endings setting */
     920          55 :                 stream->flags |= eol_detect;
     921             : 
     922             :                 /* as far as streams are concerned, we are now at the start of
     923             :                  * the stream */
     924          55 :                 stream->position = 0;
     925             : 
     926             :                 /* restore mode */
     927          55 :                 strlcpy(stream->mode, mode, sizeof(stream->mode));
     928             : 
     929          55 :                 if (transfer_encoding) {
     930           0 :                         php_stream_filter_append(&stream->readfilters, transfer_encoding);
     931             :                 }
     932             :         } else {
     933          39 :                 if (transfer_encoding) {
     934           0 :                         php_stream_filter_free(transfer_encoding);
     935             :                 }
     936             :         }
     937             : 
     938          94 :         zval_ptr_dtor(&response_header);
     939             : 
     940          94 :         return stream;
     941             : }
     942             : /* }}} */
     943             : 
     944          77 : 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) /* {{{ */
     945             : {
     946          77 :         return php_stream_url_wrap_http_ex(wrapper, path, mode, options, opened_path, context, PHP_URL_REDIRECT_MAX, HTTP_WRAPPER_HEADER_INIT STREAMS_CC);
     947             : }
     948             : /* }}} */
     949             : 
     950          15 : static int php_stream_http_stream_stat(php_stream_wrapper *wrapper, php_stream *stream, php_stream_statbuf *ssb) /* {{{ */
     951             : {
     952             :         /* one day, we could fill in the details based on Date: and Content-Length:
     953             :          * headers.  For now, we return with a failure code to prevent the underlying
     954             :          * file's details from being used instead. */
     955          15 :         return -1;
     956             : }
     957             : /* }}} */
     958             : 
     959             : static php_stream_wrapper_ops http_stream_wops = {
     960             :         php_stream_url_wrap_http,
     961             :         NULL, /* stream_close */
     962             :         php_stream_http_stream_stat,
     963             :         NULL, /* stat_url */
     964             :         NULL, /* opendir */
     965             :         "http",
     966             :         NULL, /* unlink */
     967             :         NULL, /* rename */
     968             :         NULL, /* mkdir */
     969             :         NULL, /* rmdir */
     970             :         NULL
     971             : };
     972             : 
     973             : PHPAPI php_stream_wrapper php_stream_http_wrapper = {
     974             :         &http_stream_wops,
     975             :         NULL,
     976             :         1 /* is_url */
     977             : };
     978             : 
     979             : /*
     980             :  * Local variables:
     981             :  * tab-width: 4
     982             :  * c-basic-offset: 4
     983             :  * End:
     984             :  * vim600: sw=4 ts=4 fdm=marker
     985             :  * vim<600: sw=4 ts=4
     986             :  */

Generated by: LCOV version 1.10

Generated at Tue, 27 Sep 2016 10:26:08 +0000 (8 hours ago)

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