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

Generated by: LCOV version 1.10

Generated at Wed, 22 Oct 2014 07:24:58 +0000 (3 days ago)

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