LTP GCOV extension - code coverage report
Current view: directory - ext/standard - http_fopen_wrapper.c
Test: PHP Code Coverage
Date: 2007-04-10 Instrumented lines: 328
Code covered: 0.0 % Executed lines: 0
Legend: not executed executed

       1                 : /*
       2                 :    +----------------------------------------------------------------------+
       3                 :    | PHP Version 5                                                        |
       4                 :    +----------------------------------------------------------------------+
       5                 :    | Copyright (c) 1997-2007 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: http_fopen_wrapper.c,v 1.99.2.12.2.8 2007/02/27 03:28:16 iliaa Exp $ */ 
      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                 : 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 header_init STREAMS_DC TSRMLS_DC)
      85               0 : {
      86               0 :         php_stream *stream = NULL;
      87               0 :         php_url *resource = NULL;
      88                 :         int use_ssl;
      89               0 :         int use_proxy = 0;
      90               0 :         char *scratch = NULL;
      91               0 :         char *tmp = NULL;
      92               0 :         char *ua_str = NULL;
      93               0 :         zval **ua_zval = NULL, **tmpzval = NULL;
      94               0 :         int scratch_len = 0;
      95               0 :         int body = 0;
      96                 :         char location[HTTP_HEADER_BLOCK_SIZE];
      97               0 :         zval *response_header = NULL;
      98               0 :         int reqok = 0;
      99               0 :         char *http_header_line = NULL;
     100                 :         char tmp_line[128];
     101               0 :         size_t chunk_size = 0, file_size = 0;
     102               0 :         int eol_detect = 0;
     103               0 :         char *transport_string, *errstr = NULL;
     104               0 :         int transport_len, have_header = 0, request_fulluri = 0;
     105               0 :         char *protocol_version = NULL;
     106               0 :         int protocol_version_len = 3; /* Default: "1.0" */
     107                 :         struct timeval timeout;
     108                 : 
     109               0 :         tmp_line[0] = '\0';
     110                 : 
     111               0 :         if (redirect_max < 1) {
     112               0 :                 php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "Redirection limit reached, aborting");
     113               0 :                 return NULL;
     114                 :         }
     115                 : 
     116               0 :         resource = php_url_parse(path);
     117               0 :         if (resource == NULL) {
     118               0 :                 return NULL;
     119                 :         }
     120                 : 
     121               0 :         if (strncasecmp(resource->scheme, "http", sizeof("http")) && strncasecmp(resource->scheme, "https", sizeof("https"))) {
     122               0 :                 if (!context || 
     123                 :                         php_stream_context_get_option(context, wrapper->wops->label, "proxy", &tmpzval) == FAILURE ||
     124                 :                         Z_TYPE_PP(tmpzval) != IS_STRING ||
     125                 :                         Z_STRLEN_PP(tmpzval) <= 0) {
     126               0 :                         php_url_free(resource);
     127               0 :                         return php_stream_open_wrapper_ex(path, mode, ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL, context);
     128                 :                 }
     129                 :                 /* Called from a non-http wrapper with http proxying requested (i.e. ftp) */
     130               0 :                 request_fulluri = 1;
     131               0 :                 use_ssl = 0;
     132               0 :                 use_proxy = 1;
     133                 : 
     134               0 :                 transport_len = Z_STRLEN_PP(tmpzval);
     135               0 :                 transport_string = estrndup(Z_STRVAL_PP(tmpzval), Z_STRLEN_PP(tmpzval));
     136                 :         } else {
     137                 :                 /* Normal http request (possibly with proxy) */
     138                 :         
     139               0 :                 if (strpbrk(mode, "awx+")) {
     140               0 :                         php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "HTTP wrapper does not support writeable connections.");
     141               0 :                         return NULL;
     142                 :                 }
     143                 : 
     144               0 :                 use_ssl = resource->scheme && (strlen(resource->scheme) > 4) && resource->scheme[4] == 's';
     145                 :                 /* choose default ports */
     146               0 :                 if (use_ssl && resource->port == 0)
     147               0 :                         resource->port = 443;
     148               0 :                 else if (resource->port == 0)
     149               0 :                         resource->port = 80;
     150                 : 
     151               0 :                 if (context &&
     152                 :                         php_stream_context_get_option(context, wrapper->wops->label, "proxy", &tmpzval) == SUCCESS &&
     153                 :                         Z_TYPE_PP(tmpzval) == IS_STRING &&
     154                 :                         Z_STRLEN_PP(tmpzval) > 0) {
     155               0 :                         use_proxy = 1;
     156               0 :                         transport_len = Z_STRLEN_PP(tmpzval);
     157               0 :                         transport_string = estrndup(Z_STRVAL_PP(tmpzval), Z_STRLEN_PP(tmpzval));
     158                 :                 } else {
     159               0 :                         transport_len = spprintf(&transport_string, 0, "%s://%s:%d", use_ssl ? "ssl" : "tcp", resource->host, resource->port);
     160                 :                 }
     161                 :         }
     162                 : 
     163               0 :         if (context && php_stream_context_get_option(context, wrapper->wops->label, "timeout", &tmpzval) == SUCCESS) {
     164               0 :                 SEPARATE_ZVAL(tmpzval);
     165               0 :                 convert_to_double_ex(tmpzval);
     166               0 :                 timeout.tv_sec = (time_t) Z_DVAL_PP(tmpzval);
     167               0 :                 timeout.tv_usec = (size_t) ((Z_DVAL_PP(tmpzval) - timeout.tv_sec) * 1000000);
     168                 :         } else {
     169               0 :                 timeout.tv_sec = FG(default_socket_timeout);
     170               0 :                 timeout.tv_usec = 0;
     171                 :         }
     172                 : 
     173               0 :         stream = php_stream_xport_create(transport_string, transport_len, options,
     174                 :                         STREAM_XPORT_CLIENT | STREAM_XPORT_CONNECT,
     175                 :                         NULL, &timeout, context, &errstr, NULL);
     176                 :     
     177               0 :         if (stream) {
     178               0 :                 php_stream_set_option(stream, PHP_STREAM_OPTION_READ_TIMEOUT, 0, &timeout);
     179                 :         }
     180                 :                         
     181               0 :         if (errstr) {
     182               0 :                 php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "%s", errstr);
     183               0 :                 efree(errstr);
     184               0 :                 errstr = NULL;
     185                 :         }
     186                 : 
     187               0 :         efree(transport_string);
     188                 : 
     189               0 :         if (stream && use_proxy && use_ssl) {
     190               0 :                 smart_str header = {0};
     191                 : 
     192               0 :                 smart_str_appendl(&header, "CONNECT ", sizeof("CONNECT ")-1);
     193               0 :                 smart_str_appends(&header, resource->host);
     194               0 :                 smart_str_appendc(&header, ':');
     195               0 :                 smart_str_append_unsigned(&header, resource->port);
     196               0 :                 smart_str_appendl(&header, " HTTP/1.0\r\n\r\n", sizeof(" HTTP/1.0\r\n\r\n")-1);
     197               0 :                 if (php_stream_write(stream, header.c, header.len) != header.len) {
     198               0 :                         php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "Cannot connect to HTTPS server through proxy");
     199               0 :                         php_stream_close(stream);
     200               0 :                         stream = NULL;
     201                 :                 }
     202               0 :                 smart_str_free(&header);
     203                 : 
     204               0 :                 if (stream) {
     205                 :                         char header_line[HTTP_HEADER_BLOCK_SIZE];
     206                 : 
     207                 :                         /* get response header */
     208               0 :                         while (php_stream_gets(stream, header_line, HTTP_HEADER_BLOCK_SIZE-1) != NULL)  {
     209               0 :                                 if (header_line[0] == '\n' ||
     210                 :                                     header_line[0] == '\r' ||
     211                 :                                     header_line[0] == '\0') {
     212                 :                                   break;
     213                 :                                 }
     214                 :                         }
     215                 :                 }
     216                 : 
     217                 :                 /* enable SSL transport layer */
     218               0 :                 if (stream) {
     219               0 :                         if (php_stream_xport_crypto_setup(stream, STREAM_CRYPTO_METHOD_SSLv23_CLIENT, NULL TSRMLS_CC) < 0 ||
     220                 :                             php_stream_xport_crypto_enable(stream, 1 TSRMLS_CC) < 0) {
     221               0 :                                 php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "Cannot connect to HTTPS server through proxy");
     222               0 :                                 php_stream_close(stream);
     223               0 :                                 stream = NULL;
     224                 :                         }
     225                 :                 }
     226                 :         }
     227                 : 
     228               0 :         if (stream == NULL)     
     229               0 :                 goto out;
     230                 : 
     231                 :         /* avoid buffering issues while reading header */
     232               0 :         if (options & STREAM_WILL_CAST)
     233               0 :                 chunk_size = php_stream_set_chunk_size(stream, 1);
     234                 :         
     235                 :         /* avoid problems with auto-detecting when reading the headers -> the headers
     236                 :          * are always in canonical \r\n format */
     237               0 :         eol_detect = stream->flags & (PHP_STREAM_FLAG_DETECT_EOL | PHP_STREAM_FLAG_EOL_MAC);
     238               0 :         stream->flags &= ~(PHP_STREAM_FLAG_DETECT_EOL | PHP_STREAM_FLAG_EOL_MAC);
     239                 : 
     240               0 :         php_stream_context_set(stream, context);
     241                 : 
     242               0 :         php_stream_notify_info(context, PHP_STREAM_NOTIFY_CONNECT, NULL, 0);
     243                 : 
     244               0 :         if (header_init && context && php_stream_context_get_option(context, "http", "max_redirects", &tmpzval) == SUCCESS) {
     245               0 :                 SEPARATE_ZVAL(tmpzval);
     246               0 :                 convert_to_long_ex(tmpzval);
     247               0 :                 redirect_max = Z_LVAL_PP(tmpzval);
     248                 :         }
     249                 : 
     250               0 :         if (context && php_stream_context_get_option(context, "http", "method", &tmpzval) == SUCCESS) {
     251               0 :                 if (Z_TYPE_PP(tmpzval) == IS_STRING && Z_STRLEN_PP(tmpzval) > 0) {
     252               0 :                         scratch_len = strlen(path) + 29 + Z_STRLEN_PP(tmpzval);
     253               0 :                         scratch = emalloc(scratch_len);
     254               0 :                         strlcpy(scratch, Z_STRVAL_PP(tmpzval), Z_STRLEN_PP(tmpzval) + 1);
     255               0 :                         strcat(scratch, " ");
     256                 :                 }
     257                 :         }
     258                 :  
     259               0 :         if (context && php_stream_context_get_option(context, "http", "protocol_version", &tmpzval) == SUCCESS) {
     260               0 :                 SEPARATE_ZVAL(tmpzval);
     261               0 :                 convert_to_double_ex(tmpzval);
     262               0 :                 protocol_version_len = spprintf(&protocol_version, 0, "%.1F", Z_DVAL_PP(tmpzval));
     263                 :         }
     264                 : 
     265               0 :         if (!scratch) {
     266               0 :                 scratch_len = strlen(path) + 29 + protocol_version_len;
     267               0 :                 scratch = emalloc(scratch_len);
     268               0 :                 strcpy(scratch, "GET ");
     269                 :         }
     270                 : 
     271                 :         /* Should we send the entire path in the request line, default to no. */
     272               0 :         if (!request_fulluri &&
     273                 :                 context &&
     274                 :                 php_stream_context_get_option(context, "http", "request_fulluri", &tmpzval) == SUCCESS) {
     275               0 :                 zval tmp = **tmpzval;
     276                 : 
     277               0 :                 zval_copy_ctor(&tmp);
     278               0 :                 convert_to_boolean(&tmp);
     279               0 :                 request_fulluri = Z_BVAL(tmp) ? 1 : 0;
     280               0 :                 zval_dtor(&tmp);
     281                 :         }
     282                 : 
     283               0 :         if (request_fulluri) {
     284                 :                 /* Ask for everything */
     285               0 :                 strcat(scratch, path);
     286                 :         } else {
     287                 :                 /* Send the traditional /path/to/file?query_string */
     288                 : 
     289                 :                 /* file */
     290               0 :                 if (resource->path && *resource->path) {
     291               0 :                         strlcat(scratch, resource->path, scratch_len);
     292                 :                 } else {
     293               0 :                         strlcat(scratch, "/", scratch_len);
     294                 :                 }
     295                 : 
     296                 :                 /* query string */
     297               0 :                 if (resource->query) {
     298               0 :                         strlcat(scratch, "?", scratch_len);
     299               0 :                         strlcat(scratch, resource->query, scratch_len);
     300                 :                 }
     301                 :         }
     302                 : 
     303                 :         /* protocol version we are speaking */
     304               0 :         if (protocol_version) {
     305               0 :                 strlcat(scratch, " HTTP/", scratch_len);
     306               0 :                 strlcat(scratch, protocol_version, scratch_len);
     307               0 :                 strlcat(scratch, "\r\n", scratch_len);
     308               0 :                 efree(protocol_version);
     309               0 :                 protocol_version = NULL;
     310                 :         } else {
     311               0 :                 strlcat(scratch, " HTTP/1.0\r\n", scratch_len);
     312                 :         }
     313                 : 
     314                 : 
     315                 :         /* send it */
     316               0 :         php_stream_write(stream, scratch, strlen(scratch));
     317                 : 
     318               0 :         if (context &&
     319                 :                 php_stream_context_get_option(context, "http", "header", &tmpzval) == SUCCESS &&
     320                 :                 Z_TYPE_PP(tmpzval) == IS_STRING && Z_STRLEN_PP(tmpzval)) {
     321                 :                 /* Remove newlines and spaces from start and end,
     322                 :                    php_trim will estrndup() */
     323               0 :                 tmp = php_trim(Z_STRVAL_PP(tmpzval), Z_STRLEN_PP(tmpzval), NULL, 0, NULL, 3 TSRMLS_CC);
     324               0 :                 if (strlen(tmp) > 0) {
     325               0 :                         if (!header_init) { /* Remove post headers for redirects */
     326               0 :                                 int l = strlen(tmp);
     327               0 :                                 char *s, *s2, *tmp_c = estrdup(tmp);
     328                 :                                 
     329               0 :                                 php_strtolower(tmp_c, l);
     330               0 :                                 if ((s = strstr(tmp_c, "content-length:"))) {
     331               0 :                                         if ((s2 = memchr(s, '\n', tmp_c + l - s))) {
     332               0 :                                                 int b = tmp_c + l - 1 - s2;
     333               0 :                                                 memmove(tmp, tmp + (s2 + 1 - tmp_c), b);
     334               0 :                                                 memmove(tmp_c, s2 + 1, b);
     335                 :                                                 
     336                 :                                         } else {
     337               0 :                                                 tmp[s - tmp_c] = *s = '\0';
     338                 :                                         }
     339               0 :                                         l = strlen(tmp_c);
     340                 :                                 }
     341               0 :                                 if ((s = strstr(tmp_c, "content-type:"))) {
     342               0 :                                         if ((s2 = memchr(s, '\n', tmp_c + l - s))) {
     343               0 :                                                 memmove(tmp, tmp + (s2 + 1 - tmp_c), tmp_c + l - 1 - s2);
     344                 :                                         } else {
     345               0 :                                                 tmp[s - tmp_c] = '\0';
     346                 :                                         }
     347                 :                                 }
     348               0 :                                 efree(tmp_c);
     349               0 :                                 tmp_c = php_trim(tmp, strlen(tmp), NULL, 0, NULL, 3 TSRMLS_CC);
     350               0 :                                 efree(tmp);
     351               0 :                                 tmp = tmp_c;
     352                 :                         }
     353                 :                 
     354                 :                         /* Output trimmed headers with \r\n at the end */
     355               0 :                         php_stream_write(stream, tmp, strlen(tmp));
     356               0 :                         php_stream_write(stream, "\r\n", sizeof("\r\n") - 1);
     357                 : 
     358                 :                         /* Make lowercase for easy comparison against 'standard' headers */
     359               0 :                         php_strtolower(tmp, strlen(tmp));
     360               0 :                         if (strstr(tmp, "user-agent:")) {
     361               0 :                                  have_header |= HTTP_HEADER_USER_AGENT;
     362                 :                         }
     363               0 :                         if (strstr(tmp, "host:")) {
     364               0 :                                  have_header |= HTTP_HEADER_HOST;
     365                 :                         }
     366               0 :                         if (strstr(tmp, "from:")) {
     367               0 :                                  have_header |= HTTP_HEADER_FROM;
     368                 :                                 }
     369               0 :                         if (strstr(tmp, "authorization:")) {
     370               0 :                                  have_header |= HTTP_HEADER_AUTH;
     371                 :                         }
     372               0 :                         if (strstr(tmp, "content-length:")) {
     373               0 :                                  have_header |= HTTP_HEADER_CONTENT_LENGTH;
     374                 :                         }
     375               0 :                         if (strstr(tmp, "content-type:")) {
     376               0 :                                  have_header |= HTTP_HEADER_TYPE;
     377                 :                         }
     378                 :                 }
     379               0 :                 efree(tmp);
     380                 :         }
     381                 : 
     382                 :         /* auth header if it was specified */
     383               0 :         if (((have_header & HTTP_HEADER_AUTH) == 0) && resource->user && resource->pass)      {
     384                 :                 /* decode the strings first */
     385               0 :                 php_url_decode(resource->user, strlen(resource->user));
     386               0 :                 php_url_decode(resource->pass, strlen(resource->pass));
     387                 : 
     388                 :                 /* scratch is large enough, since it was made large enough for the whole URL */
     389               0 :                 strcpy(scratch, resource->user);
     390               0 :                 strcat(scratch, ":");
     391               0 :                 strcat(scratch, resource->pass);
     392                 : 
     393               0 :                 tmp = php_base64_encode((unsigned char*)scratch, strlen(scratch), NULL);
     394                 :                 
     395               0 :                 if (snprintf(scratch, scratch_len, "Authorization: Basic %s\r\n", tmp) > 0) {
     396               0 :                         php_stream_write(stream, scratch, strlen(scratch));
     397               0 :                         php_stream_notify_info(context, PHP_STREAM_NOTIFY_AUTH_REQUIRED, NULL, 0);
     398                 :                 }
     399                 : 
     400               0 :                 efree(tmp);
     401               0 :                 tmp = NULL;
     402                 :         }
     403                 : 
     404                 :         /* if the user has configured who they are, send a From: line */
     405               0 :         if (((have_header & HTTP_HEADER_FROM) == 0) && cfg_get_string("from", &tmp) == SUCCESS)       {
     406               0 :                 if (snprintf(scratch, scratch_len, "From: %s\r\n", tmp) > 0)
     407               0 :                         php_stream_write(stream, scratch, strlen(scratch));
     408                 :         }
     409                 : 
     410                 :         /* Send Host: header so name-based virtual hosts work */
     411               0 :         if ((have_header & HTTP_HEADER_HOST) == 0) {
     412               0 :                 if ((use_ssl && resource->port != 443 && resource->port != 0) || 
     413                 :                         (!use_ssl && resource->port != 80 && resource->port != 0))        {
     414               0 :                         if (snprintf(scratch, scratch_len, "Host: %s:%i\r\n", resource->host, resource->port) > 0)
     415               0 :                                 php_stream_write(stream, scratch, strlen(scratch));
     416                 :                 } else {
     417               0 :                         if (snprintf(scratch, scratch_len, "Host: %s\r\n", resource->host) > 0) {
     418               0 :                                 php_stream_write(stream, scratch, strlen(scratch));
     419                 :                         }
     420                 :                 }
     421                 :         }
     422                 : 
     423               0 :         if (context && 
     424                 :             php_stream_context_get_option(context, "http", "user_agent", &ua_zval) == SUCCESS &&
     425                 :                 Z_TYPE_PP(ua_zval) == IS_STRING) {
     426               0 :                 ua_str = Z_STRVAL_PP(ua_zval);
     427               0 :         } else if (FG(user_agent)) {
     428               0 :                 ua_str = FG(user_agent);
     429                 :         }
     430                 : 
     431               0 :         if (((have_header & HTTP_HEADER_USER_AGENT) == 0) && ua_str) {
     432                 : #define _UA_HEADER "User-Agent: %s\r\n"
     433                 :                 char *ua;
     434                 :                 size_t ua_len;
     435                 :                 
     436               0 :                 ua_len = sizeof(_UA_HEADER) + strlen(ua_str);
     437                 :                 
     438                 :                 /* ensure the header is only sent if user_agent is not blank */
     439               0 :                 if (ua_len > sizeof(_UA_HEADER)) {
     440               0 :                         ua = emalloc(ua_len + 1);
     441               0 :                         if ((ua_len = slprintf(ua, ua_len, _UA_HEADER, ua_str)) > 0) {
     442               0 :                                 ua[ua_len] = 0;
     443               0 :                                 php_stream_write(stream, ua, ua_len);
     444                 :                         } else {
     445               0 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot construct User-agent header");
     446                 :                         }
     447                 : 
     448               0 :                         if (ua) {
     449               0 :                                 efree(ua);
     450                 :                         }
     451                 :                 }       
     452                 :         }
     453                 : 
     454                 :         /* Request content, such as for POST requests */
     455               0 :         if (header_init && context &&
     456                 :                 php_stream_context_get_option(context, "http", "content", &tmpzval) == SUCCESS &&
     457                 :                 Z_TYPE_PP(tmpzval) == IS_STRING && Z_STRLEN_PP(tmpzval) > 0) {
     458               0 :                 if (!(have_header & HTTP_HEADER_CONTENT_LENGTH)) {
     459               0 :                         scratch_len = slprintf(scratch, scratch_len, "Content-Length: %d\r\n", Z_STRLEN_PP(tmpzval));
     460               0 :                         php_stream_write(stream, scratch, scratch_len);
     461                 :                 }
     462               0 :                 if (!(have_header & HTTP_HEADER_TYPE)) {
     463               0 :                         php_stream_write(stream, "Content-Type: application/x-www-form-urlencoded\r\n",
     464                 :                                 sizeof("Content-Type: application/x-www-form-urlencoded\r\n") - 1);
     465               0 :                         php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Content-type not specified assuming application/x-www-form-urlencoded");
     466                 :                 }
     467               0 :                 php_stream_write(stream, "\r\n", sizeof("\r\n")-1);
     468               0 :                 php_stream_write(stream, Z_STRVAL_PP(tmpzval), Z_STRLEN_PP(tmpzval));
     469               0 :                 php_stream_write(stream, "\r\n\r\n", sizeof("\r\n\r\n")-1);
     470                 :         } else {
     471               0 :                 php_stream_write(stream, "\r\n", sizeof("\r\n")-1);
     472                 :         }
     473                 : 
     474               0 :         location[0] = '\0';
     475                 : 
     476               0 :         if (header_init) {
     477                 :                 zval *tmp;
     478               0 :                 MAKE_STD_ZVAL(tmp);
     479               0 :                 array_init(tmp);
     480               0 :                 ZEND_SET_SYMBOL(EG(active_symbol_table), "http_response_header", tmp);
     481                 :         }
     482                 : 
     483                 :         {
     484                 :                 zval **rh;
     485               0 :                 zend_hash_find(EG(active_symbol_table), "http_response_header", sizeof("http_response_header"), (void **) &rh);
     486               0 :                 response_header = *rh;
     487                 :         }
     488                 : 
     489               0 :         if (!php_stream_eof(stream)) {
     490                 :                 size_t tmp_line_len;
     491                 :                 /* get response header */
     492                 : 
     493               0 :                 if (php_stream_get_line(stream, tmp_line, sizeof(tmp_line) - 1, &tmp_line_len) != NULL) {
     494                 :                         zval *http_response;
     495                 :                         int response_code;
     496                 : 
     497               0 :                         if (tmp_line_len > 9) {
     498               0 :                                 response_code = atoi(tmp_line + 9);
     499                 :                         } else {
     500               0 :                                 response_code = 0;
     501                 :                         }
     502                 :                         /* when we request only the header, don't fail even on error codes */
     503               0 :                         if (options & STREAM_ONLY_GET_HEADERS) {
     504               0 :                                 reqok = 1;
     505                 :                         }
     506               0 :                         switch(response_code) {
     507                 :                                 case 200:
     508                 :                                 case 206: /* partial content */
     509                 :                                 case 302:
     510                 :                                 case 303:
     511                 :                                 case 301:
     512               0 :                                         reqok = 1;
     513               0 :                                         break;
     514                 :                                 case 403:
     515               0 :                                         php_stream_notify_error(context, PHP_STREAM_NOTIFY_AUTH_RESULT,
     516                 :                                                         tmp_line, response_code);
     517               0 :                                         break;
     518                 :                                 default:
     519                 :                                         /* safety net in the event tmp_line == NULL */
     520               0 :                                         if (!tmp_line_len) {
     521               0 :                                                 tmp_line[0] = '\0';
     522                 :                                         }
     523               0 :                                         php_stream_notify_error(context, PHP_STREAM_NOTIFY_FAILURE,
     524                 :                                                         tmp_line, response_code);
     525                 :                         }
     526               0 :                         if (tmp_line[tmp_line_len - 1] == '\n') {
     527               0 :                                 --tmp_line_len;
     528               0 :                                 if (tmp_line[tmp_line_len - 1] == '\r') {
     529               0 :                                         --tmp_line_len;
     530                 :                                 }
     531                 :                         }
     532               0 :                         MAKE_STD_ZVAL(http_response);
     533               0 :                         ZVAL_STRINGL(http_response, tmp_line, tmp_line_len, 1);
     534               0 :                         zend_hash_next_index_insert(Z_ARRVAL_P(response_header), &http_response, sizeof(zval *), NULL);
     535                 :                 }
     536                 :         } else {
     537               0 :                 php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "HTTP request failed, unexpected end of socket!");
     538               0 :                 goto out;
     539                 :         }
     540                 :         
     541                 :         /* read past HTTP headers */
     542                 :         
     543               0 :         http_header_line = emalloc(HTTP_HEADER_BLOCK_SIZE);
     544                 : 
     545               0 :         while (!body && !php_stream_eof(stream)) {
     546                 :                 size_t http_header_line_length;
     547               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') {
     548               0 :                         char *e = http_header_line + http_header_line_length - 1;
     549               0 :                         while (*e == '\n' || *e == '\r') {
     550               0 :                                 e--;
     551                 :                         }
     552               0 :                         http_header_line_length = e - http_header_line + 1;
     553               0 :                         http_header_line[http_header_line_length] = '\0';
     554                 : 
     555               0 :                         if (!strncasecmp(http_header_line, "Location: ", 10)) {
     556               0 :                                 strlcpy(location, http_header_line + 10, sizeof(location));
     557               0 :                         } else if (!strncasecmp(http_header_line, "Content-Type: ", 14)) {
     558               0 :                                 php_stream_notify_info(context, PHP_STREAM_NOTIFY_MIME_TYPE_IS, http_header_line + 14, 0);
     559               0 :                         } else if (!strncasecmp(http_header_line, "Content-Length: ", 16)) {
     560               0 :                                 file_size = atoi(http_header_line + 16);
     561               0 :                                 php_stream_notify_file_size(context, file_size, http_header_line, 0);
     562                 :                         }
     563                 : 
     564               0 :                         if (http_header_line[0] == '\0') {
     565               0 :                                 body = 1;
     566                 :                         } else {
     567                 :                                 zval *http_header;
     568                 : 
     569               0 :                                 MAKE_STD_ZVAL(http_header);
     570                 : 
     571               0 :                                 ZVAL_STRINGL(http_header, http_header_line, http_header_line_length, 1);
     572                 :                                 
     573               0 :                                 zend_hash_next_index_insert(Z_ARRVAL_P(response_header), &http_header, sizeof(zval *), NULL);
     574                 :                         }
     575                 :                 } else {
     576                 :                         break;
     577                 :                 }
     578                 :         }
     579                 :         
     580               0 :         if (!reqok || location[0] != '\0') {
     581               0 :                 if (options & STREAM_ONLY_GET_HEADERS && redirect_max <= 1) {
     582               0 :                         goto out;
     583                 :                 }
     584                 : 
     585               0 :                 if (location[0] != '\0')
     586               0 :                         php_stream_notify_info(context, PHP_STREAM_NOTIFY_REDIRECTED, location, 0);
     587                 : 
     588               0 :                 php_stream_close(stream);
     589               0 :                 stream = NULL;
     590                 : 
     591               0 :                 if (location[0] != '\0')        {
     592                 : 
     593                 :                         char new_path[HTTP_HEADER_BLOCK_SIZE];
     594                 :                         char loc_path[HTTP_HEADER_BLOCK_SIZE];
     595                 : 
     596               0 :                         *new_path='\0';
     597               0 :                         if (strlen(location)<8 || (strncasecmp(location, "http://", sizeof("http://")-1) && 
     598                 :                                                         strncasecmp(location, "https://", sizeof("https://")-1) && 
     599                 :                                                         strncasecmp(location, "ftp://", sizeof("ftp://")-1) && 
     600                 :                                                         strncasecmp(location, "ftps://", sizeof("ftps://")-1))) 
     601                 :                         {
     602               0 :                                 if (*location != '/') {
     603               0 :                                         if (*(location+1) != '\0' && resource->path) {               
     604               0 :                                                 char *s = strrchr(resource->path, '/');
     605               0 :                                                 if (!s) {
     606               0 :                                                         s = resource->path;
     607               0 :                                                         if (!s[0]) {
     608               0 :                                                                 efree(s);
     609               0 :                                                                 s = resource->path = estrdup("/");
     610                 :                                                         } else {
     611               0 :                                                                 *s = '/';
     612                 :                                                         }
     613                 :                                                 }
     614               0 :                                                 s[1] = '\0'; 
     615               0 :                                                 if (resource->path && *(resource->path) == '/' && *(resource->path + 1) == '\0') {
     616               0 :                                                         snprintf(loc_path, sizeof(loc_path) - 1, "%s%s", resource->path, location);
     617                 :                                                 } else {
     618               0 :                                                         snprintf(loc_path, sizeof(loc_path) - 1, "%s/%s", resource->path, location);
     619                 :                                                 }
     620                 :                                         } else {
     621               0 :                                                 snprintf(loc_path, sizeof(loc_path) - 1, "/%s", location);
     622                 :                                         }
     623                 :                                 } else {
     624               0 :                                         strlcpy(loc_path, location, sizeof(loc_path));
     625                 :                                 }
     626               0 :                                 if ((use_ssl && resource->port != 443) || (!use_ssl && resource->port != 80)) {
     627               0 :                                         snprintf(new_path, sizeof(new_path) - 1, "%s://%s:%d%s", resource->scheme, resource->host, resource->port, loc_path);
     628                 :                                 } else {
     629               0 :                                         snprintf(new_path, sizeof(new_path) - 1, "%s://%s%s", resource->scheme, resource->host, loc_path);
     630                 :                                 }
     631                 :                         } else {
     632               0 :                                 strlcpy(new_path, location, sizeof(new_path));
     633                 :                         }
     634                 : 
     635               0 :                         php_url_free(resource);
     636                 :                         /* check for invalid redirection URLs */
     637               0 :                         if ((resource = php_url_parse(new_path)) == NULL) {
     638               0 :                                 php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "Invalid redirect URL! %s", new_path);
     639               0 :                                 goto out;
     640                 :                         }
     641                 : 
     642                 : #define CHECK_FOR_CNTRL_CHARS(val) {    \
     643                 :         if (val) {      \
     644                 :                 unsigned char *s, *e;   \
     645                 :                 int l;  \
     646                 :                 l = php_url_decode(val, strlen(val));   \
     647                 :                 s = val; e = s + l;     \
     648                 :                 while (s < e) {      \
     649                 :                         if (iscntrl(*s)) {      \
     650                 :                                 php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "Invalid redirect URL! %s", new_path);       \
     651                 :                                 goto out;       \
     652                 :                         }       \
     653                 :                         s++;    \
     654                 :                 }       \
     655                 :         }       \
     656                 : }       \
     657                 :                         /* check for control characters in login, password & path */
     658               0 :                         if (strncasecmp(new_path, "http://", sizeof("http://") - 1) || strncasecmp(new_path, "https://", sizeof("https://") - 1)) {
     659               0 :                                 CHECK_FOR_CNTRL_CHARS(resource->user)
     660               0 :                                 CHECK_FOR_CNTRL_CHARS(resource->pass)
     661               0 :                                 CHECK_FOR_CNTRL_CHARS(resource->path)
     662                 :                         }
     663               0 :                         stream = php_stream_url_wrap_http_ex(wrapper, new_path, mode, options, opened_path, context, --redirect_max, 0 STREAMS_CC TSRMLS_CC);
     664                 :                 } else {
     665               0 :                         php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "HTTP request failed! %s", tmp_line);
     666                 :                 }
     667                 :         }
     668               0 : out:
     669               0 :         if (protocol_version) {
     670               0 :                 efree(protocol_version);
     671                 :         }
     672                 : 
     673               0 :         if (http_header_line) {
     674               0 :                 efree(http_header_line);
     675                 :         }
     676                 : 
     677               0 :         if (scratch) {
     678               0 :                 efree(scratch);
     679                 :         }
     680                 : 
     681               0 :         if (resource) {
     682               0 :                 php_url_free(resource);
     683                 :         }
     684                 : 
     685               0 :         if (stream) {
     686               0 :                 if (header_init) {
     687               0 :                         zval_add_ref(&response_header);
     688               0 :                         stream->wrapperdata = response_header;
     689                 :                 }
     690               0 :                 php_stream_notify_progress_init(context, 0, file_size);
     691                 :                 /* Restore original chunk size now that we're done with headers */
     692               0 :                 if (options & STREAM_WILL_CAST)
     693               0 :                         php_stream_set_chunk_size(stream, chunk_size);
     694                 : 
     695                 :                 /* restore the users auto-detect-line-endings setting */
     696               0 :                 stream->flags |= eol_detect;
     697                 :                 
     698                 :                 /* as far as streams are concerned, we are now at the start of
     699                 :                  * the stream */
     700               0 :                 stream->position = 0;
     701                 : 
     702                 :         }
     703                 : 
     704               0 :         return stream;
     705                 : }
     706                 : 
     707                 : 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)
     708               0 : {
     709               0 :         return php_stream_url_wrap_http_ex(wrapper, path, mode, options, opened_path, context, PHP_URL_REDIRECT_MAX, 1 STREAMS_CC TSRMLS_CC);
     710                 : }
     711                 : 
     712                 : static int php_stream_http_stream_stat(php_stream_wrapper *wrapper,
     713                 :                 php_stream *stream,
     714                 :                 php_stream_statbuf *ssb
     715                 :                 TSRMLS_DC)
     716               0 : {
     717                 :         /* one day, we could fill in the details based on Date: and Content-Length:
     718                 :          * headers.  For now, we return with a failure code to prevent the underlying
     719                 :          * file's details from being used instead. */
     720               0 :         return -1;
     721                 : }
     722                 : 
     723                 : static php_stream_wrapper_ops http_stream_wops = {
     724                 :         php_stream_url_wrap_http,
     725                 :         NULL, /* stream_close */
     726                 :         php_stream_http_stream_stat,
     727                 :         NULL, /* stat_url */
     728                 :         NULL, /* opendir */
     729                 :         "http",
     730                 :         NULL, /* unlink */
     731                 :         NULL, /* rename */
     732                 :         NULL, /* mkdir */
     733                 :         NULL  /* rmdir */
     734                 : };
     735                 : 
     736                 : PHPAPI php_stream_wrapper php_stream_http_wrapper =     {
     737                 :         &http_stream_wops,
     738                 :         NULL,
     739                 :         1 /* is_url */
     740                 : };
     741                 : 
     742                 : /*
     743                 :  * Local variables:
     744                 :  * tab-width: 4
     745                 :  * c-basic-offset: 4
     746                 :  * End:
     747                 :  * vim600: sw=4 ts=4 fdm=marker
     748                 :  * vim<600: sw=4 ts=4
     749                 :  */

Generated by: LTP GCOV extension version 1.5