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: 0 476 0.0 %
Date: 2014-11-25 Functions: 0 4 0.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Tue, 25 Nov 2014 09:11:04 +0000 (2 days ago)

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