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

Generated by: LCOV version 1.10

Generated at Mon, 04 Aug 2014 15:49:14 +0000 (18 days ago)

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