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

Generated by: LCOV version 1.10

Generated at Sat, 13 Dec 2014 06:16:22 +0000 (9 days ago)

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