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/soap - php_http.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 232 914 25.4 %
Date: 2014-08-04 Functions: 7 9 77.8 %
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: Brad Lafountain <rodif_bl@yahoo.com>                        |
      16             :   |          Shane Caraveo <shane@caraveo.com>                           |
      17             :   |          Dmitry Stogov <dmitry@zend.com>                             |
      18             :   +----------------------------------------------------------------------+
      19             : */
      20             : /* $Id$ */
      21             : 
      22             : #include "php_soap.h"
      23             : #include "ext/standard/base64.h"
      24             : #include "ext/standard/md5.h"
      25             : #include "ext/standard/php_rand.h"
      26             : 
      27             : static char *get_http_header_value(char *headers, char *type);
      28             : static int get_http_body(php_stream *socketd, int close, char *headers,  char **response, int *out_size TSRMLS_DC);
      29             : static int get_http_headers(php_stream *socketd,char **response, int *out_size TSRMLS_DC);
      30             : 
      31             : #define smart_str_append_const(str, const) \
      32             :         smart_str_appendl(str,const,sizeof(const)-1)
      33             : 
      34             : /* Proxy HTTP Authentication */
      35           0 : int proxy_authentication(zval* this_ptr, smart_str* soap_headers TSRMLS_DC)
      36             : {
      37             :         zval **login, **password;
      38             : 
      39           0 :         if (zend_hash_find(Z_OBJPROP_P(this_ptr), "_proxy_login", sizeof("_proxy_login"), (void **)&login) == SUCCESS) {
      40             :                 unsigned char* buf;
      41             :                 int len;
      42           0 :                 smart_str auth = {0};
      43             : 
      44           0 :                 smart_str_appendl(&auth, Z_STRVAL_PP(login), Z_STRLEN_PP(login));
      45           0 :                 smart_str_appendc(&auth, ':');
      46           0 :                 if (zend_hash_find(Z_OBJPROP_P(this_ptr), "_proxy_password", sizeof("_proxy_password"), (void **)&password) == SUCCESS) {
      47           0 :                         smart_str_appendl(&auth, Z_STRVAL_PP(password), Z_STRLEN_PP(password));
      48             :                 }
      49           0 :                 smart_str_0(&auth);
      50           0 :                 buf = php_base64_encode((unsigned char*)auth.c, auth.len, &len);
      51           0 :                 smart_str_append_const(soap_headers, "Proxy-Authorization: Basic ");
      52           0 :                 smart_str_appendl(soap_headers, (char*)buf, len);
      53           0 :                 smart_str_append_const(soap_headers, "\r\n");
      54           0 :                 efree(buf);
      55           0 :                 smart_str_free(&auth);
      56           0 :                 return 1;
      57             :         }
      58           0 :         return 0;
      59             : }
      60             : 
      61             : /* HTTP Authentication */
      62         708 : int basic_authentication(zval* this_ptr, smart_str* soap_headers TSRMLS_DC)
      63             : {
      64             :         zval **login, **password;
      65             : 
      66         708 :         if (zend_hash_find(Z_OBJPROP_P(this_ptr), "_login", sizeof("_login"), (void **)&login) == SUCCESS &&
      67           0 :                         !zend_hash_exists(Z_OBJPROP_P(this_ptr), "_digest", sizeof("_digest"))) {
      68             :                 unsigned char* buf;
      69             :                 int len;
      70           0 :                 smart_str auth = {0};
      71             : 
      72           0 :                 smart_str_appendl(&auth, Z_STRVAL_PP(login), Z_STRLEN_PP(login));
      73           0 :                 smart_str_appendc(&auth, ':');
      74           0 :                 if (zend_hash_find(Z_OBJPROP_P(this_ptr), "_password", sizeof("_password"), (void **)&password) == SUCCESS) {
      75           0 :                         smart_str_appendl(&auth, Z_STRVAL_PP(password), Z_STRLEN_PP(password));
      76             :                 }
      77           0 :                 smart_str_0(&auth);
      78           0 :                 buf = php_base64_encode((unsigned char*)auth.c, auth.len, &len);
      79           0 :                 smart_str_append_const(soap_headers, "Authorization: Basic ");
      80           0 :                 smart_str_appendl(soap_headers, (char*)buf, len);
      81           0 :                 smart_str_append_const(soap_headers, "\r\n");
      82           0 :                 efree(buf);
      83           0 :                 smart_str_free(&auth);
      84           0 :                 return 1;
      85             :         }
      86         708 :         return 0;
      87             : }
      88             : 
      89             : /* Additional HTTP headers */
      90         709 : void http_context_headers(php_stream_context* context,
      91             :                           zend_bool has_authorization,
      92             :                           zend_bool has_proxy_authorization,
      93             :                           zend_bool has_cookies,
      94             :                           smart_str* soap_headers TSRMLS_DC)
      95             : {
      96             :         zval **tmp;
      97             : 
      98        1417 :         if (context &&
      99         708 :                 php_stream_context_get_option(context, "http", "header", &tmp) == SUCCESS &&
     100           0 :                 Z_TYPE_PP(tmp) == IS_STRING && Z_STRLEN_PP(tmp)) {
     101           0 :                 char *s = Z_STRVAL_PP(tmp);
     102             :                 char *p;
     103             :                 int name_len;
     104             : 
     105           0 :                 while (*s) {
     106             :                         /* skip leading newlines and spaces */
     107           0 :                         while (*s == ' ' || *s == '\t' || *s == '\r' || *s == '\n') {
     108           0 :                                 s++;
     109             :                         }
     110             :                         /* extract header name */
     111           0 :                         p = s;
     112           0 :                         name_len = -1;
     113           0 :                         while (*p) {
     114           0 :                                 if (*p == ':') {
     115           0 :                                         if (name_len < 0) name_len = p - s;
     116           0 :                                         break;
     117           0 :                                 } else if (*p == ' ' || *p == '\t') {
     118           0 :                                         if (name_len < 0) name_len = p - s;
     119           0 :                                 } else if (*p == '\r' || *p == '\n') {
     120             :                                         break;
     121             :                                 }
     122           0 :                                 p++;
     123             :                         }
     124           0 :                         if (*p == ':') {
     125             :                                 /* extract header value */
     126           0 :                                 while (*p && *p != '\r' && *p != '\n') {
     127           0 :                                         p++;
     128             :                                 }
     129             :                                 /* skip some predefined headers */
     130           0 :                                 if ((name_len != sizeof("host")-1 ||
     131           0 :                                      strncasecmp(s, "host", sizeof("host")-1) != 0) &&
     132             :                                     (name_len != sizeof("connection")-1 ||
     133           0 :                                      strncasecmp(s, "connection", sizeof("connection")-1) != 0) &&
     134             :                                     (name_len != sizeof("user-agent")-1 ||
     135           0 :                                      strncasecmp(s, "user-agent", sizeof("user-agent")-1) != 0) &&
     136             :                                     (name_len != sizeof("content-length")-1 ||
     137           0 :                                      strncasecmp(s, "content-length", sizeof("content-length")-1) != 0) &&
     138             :                                     (name_len != sizeof("content-type")-1 ||
     139           0 :                                      strncasecmp(s, "content-type", sizeof("content-type")-1) != 0) &&
     140             :                                     (!has_cookies ||
     141             :                                      name_len != sizeof("cookie")-1 ||
     142           0 :                                      strncasecmp(s, "cookie", sizeof("cookie")-1) != 0) &&
     143             :                                     (!has_authorization ||
     144             :                                      name_len != sizeof("authorization")-1 ||
     145           0 :                                      strncasecmp(s, "authorization", sizeof("authorization")-1) != 0) &&
     146             :                                     (!has_proxy_authorization ||
     147             :                                      name_len != sizeof("proxy-authorization")-1 ||
     148           0 :                                      strncasecmp(s, "proxy-authorization", sizeof("proxy-authorization")-1) != 0)) {
     149             :                                     /* add header */
     150           0 :                                         smart_str_appendl(soap_headers, s, p-s);
     151           0 :                                         smart_str_append_const(soap_headers, "\r\n");
     152             :                                 }
     153             :                         }
     154           0 :                         s = (*p) ? (p + 1) : p;
     155             :                 }
     156             :         }
     157         709 : }
     158             : 
     159           1 : static php_stream* http_connect(zval* this_ptr, php_url *phpurl, int use_ssl, php_stream_context *context, int *use_proxy TSRMLS_DC)
     160             : {
     161             :         php_stream *stream;
     162             :         zval **proxy_host, **proxy_port, **tmp;
     163             :         char *host;
     164             :         char *name;
     165             :         char *protocol;
     166             :         long namelen;
     167             :         int port;
     168             :         int old_error_reporting;
     169             :         struct timeval tv;
     170           1 :         struct timeval *timeout = NULL;
     171             : 
     172           1 :         if (zend_hash_find(Z_OBJPROP_P(this_ptr), "_proxy_host", sizeof("_proxy_host"), (void **) &proxy_host) == SUCCESS &&
     173           0 :             Z_TYPE_PP(proxy_host) == IS_STRING &&
     174           0 :             zend_hash_find(Z_OBJPROP_P(this_ptr), "_proxy_port", sizeof("_proxy_port"), (void **) &proxy_port) == SUCCESS &&
     175           0 :             Z_TYPE_PP(proxy_port) == IS_LONG) {
     176           0 :                 host = Z_STRVAL_PP(proxy_host);
     177           0 :                 port = Z_LVAL_PP(proxy_port);
     178           0 :                 *use_proxy = 1;
     179             :         } else {
     180           1 :                 host = phpurl->host;
     181           1 :                 port = phpurl->port;
     182             :         }
     183           1 :         if (zend_hash_find(Z_OBJPROP_P(this_ptr), "_connection_timeout", sizeof("_connection_timeout"), (void **) &tmp) == SUCCESS &&
     184           0 :             Z_TYPE_PP(tmp) == IS_LONG && Z_LVAL_PP(tmp) > 0) {
     185           0 :           tv.tv_sec = Z_LVAL_PP(tmp);
     186           0 :           tv.tv_usec = 0;
     187           0 :                 timeout = &tv;
     188             :         }
     189             : 
     190           1 :         old_error_reporting = EG(error_reporting);
     191           1 :         EG(error_reporting) &= ~(E_WARNING|E_NOTICE|E_USER_WARNING|E_USER_NOTICE);
     192             : 
     193             :         /* Changed ternary operator to an if/else so that additional comparisons can be done on the ssl_method property */
     194           1 :         if (use_ssl && !*use_proxy) {
     195           0 :                 if (zend_hash_find(Z_OBJPROP_P(this_ptr), "_ssl_method", sizeof("_ssl_method"), (void **) &tmp) == SUCCESS &&
     196           0 :                         Z_TYPE_PP(tmp) == IS_LONG) {
     197             :                         /* uses constants declared in soap.c to determine ssl uri protocol */
     198           0 :                         switch (Z_LVAL_PP(tmp)) {
     199             :                                 case SOAP_SSL_METHOD_TLS:
     200           0 :                                         protocol = "tls";
     201           0 :                                         break;
     202             : 
     203             :                                 case SOAP_SSL_METHOD_SSLv2:
     204           0 :                                         protocol = "sslv2";
     205           0 :                                         break;
     206             : 
     207             :                                 case SOAP_SSL_METHOD_SSLv3:
     208           0 :                                         protocol = "sslv3";
     209           0 :                                         break;
     210             : 
     211             :                                 case SOAP_SSL_METHOD_SSLv23:
     212           0 :                                         protocol = "ssl";
     213           0 :                                         break;
     214             : 
     215             :                                 default:
     216           0 :                                         protocol = "ssl";
     217             :                                         break;
     218             : 
     219             :                         }
     220             :                 } else {
     221           0 :                         protocol = "ssl";
     222             :                 }
     223             :         } else {
     224           1 :                 protocol = "tcp";
     225             :         }
     226             : 
     227           1 :         namelen = spprintf(&name, 0, "%s://%s:%d", protocol, host, port);
     228             : 
     229           1 :         stream = php_stream_xport_create(name, namelen,
     230             :                 REPORT_ERRORS,
     231             :                 STREAM_XPORT_CLIENT | STREAM_XPORT_CONNECT,
     232             :                 NULL /*persistent_id*/,
     233             :                 timeout,
     234             :                 context,
     235             :                 NULL, NULL);
     236           1 :         efree(name);
     237             : 
     238             :         /* SSL & proxy */
     239           1 :         if (stream && *use_proxy && use_ssl) {
     240           0 :                 smart_str soap_headers = {0};
     241             :                 char *http_headers;
     242             :                 int http_header_size;
     243             : 
     244           0 :                 smart_str_append_const(&soap_headers, "CONNECT ");
     245           0 :                 smart_str_appends(&soap_headers, phpurl->host);
     246           0 :                 smart_str_appendc(&soap_headers, ':');
     247           0 :                 smart_str_append_unsigned(&soap_headers, phpurl->port);
     248           0 :                 smart_str_append_const(&soap_headers, " HTTP/1.1\r\n");
     249           0 :                 smart_str_append_const(&soap_headers, "Host: ");
     250           0 :                 smart_str_appends(&soap_headers, phpurl->host);
     251           0 :                 if (phpurl->port != 80) {
     252           0 :                         smart_str_appendc(&soap_headers, ':');
     253           0 :                         smart_str_append_unsigned(&soap_headers, phpurl->port);
     254             :                 }
     255           0 :                 smart_str_append_const(&soap_headers, "\r\n");
     256           0 :                 proxy_authentication(this_ptr, &soap_headers TSRMLS_CC);
     257           0 :                 smart_str_append_const(&soap_headers, "\r\n");
     258           0 :                 if (php_stream_write(stream, soap_headers.c, soap_headers.len) != soap_headers.len) {
     259           0 :                         php_stream_close(stream);
     260           0 :                         stream = NULL;
     261             :                 }
     262           0 :                 smart_str_free(&soap_headers);
     263             : 
     264           0 :                 if (stream) {
     265           0 :                         if (!get_http_headers(stream, &http_headers, &http_header_size TSRMLS_CC) || http_headers == NULL) {
     266           0 :                                 php_stream_close(stream);
     267           0 :                                 stream = NULL;
     268             :                         }
     269           0 :                         if (http_headers) {
     270           0 :                                 efree(http_headers);
     271             :                         }
     272             :                 }
     273             :                 /* enable SSL transport layer */
     274           0 :                 if (stream) {
     275             :                         /* if a stream is created without encryption, check to see if SSL method parameter is specified and use
     276             :                            proper encrypyion method based on constants defined in soap.c */
     277           0 :                         int crypto_method = STREAM_CRYPTO_METHOD_SSLv23_CLIENT;
     278           0 :                         if (zend_hash_find(Z_OBJPROP_P(this_ptr), "_ssl_method", sizeof("_ssl_method"), (void **) &tmp) == SUCCESS &&
     279           0 :                                 Z_TYPE_PP(tmp) == IS_LONG) {
     280           0 :                                 switch (Z_LVAL_PP(tmp)) {
     281             :                                         case SOAP_SSL_METHOD_TLS:
     282           0 :                                                 crypto_method = STREAM_CRYPTO_METHOD_TLS_CLIENT;
     283           0 :                                                 break;
     284             : 
     285             :                                         case SOAP_SSL_METHOD_SSLv2:
     286           0 :                                                 crypto_method = STREAM_CRYPTO_METHOD_SSLv2_CLIENT;
     287           0 :                                                 break;
     288             : 
     289             :                                         case SOAP_SSL_METHOD_SSLv3:
     290           0 :                                                 crypto_method = STREAM_CRYPTO_METHOD_SSLv3_CLIENT;
     291           0 :                                                 break;
     292             : 
     293             :                                         case SOAP_SSL_METHOD_SSLv23:
     294           0 :                                                 crypto_method = STREAM_CRYPTO_METHOD_SSLv23_CLIENT;
     295           0 :                                                 break;
     296             : 
     297             :                                         default:
     298           0 :                                                 crypto_method = STREAM_CRYPTO_METHOD_TLS_CLIENT;
     299             :                                                 break;
     300             :                                 }
     301             :                         }
     302           0 :                         if (php_stream_xport_crypto_setup(stream, crypto_method, NULL TSRMLS_CC) < 0 ||
     303           0 :                             php_stream_xport_crypto_enable(stream, 1 TSRMLS_CC) < 0) {
     304           0 :                                 php_stream_close(stream);
     305           0 :                                 stream = NULL;
     306             :                         }
     307             :                 }
     308             :         }
     309             : 
     310           1 :         EG(error_reporting) = old_error_reporting;
     311           1 :         return stream;
     312             : }
     313             : 
     314           0 : static int in_domain(const char *host, const char *domain)
     315             : {
     316           0 :   if (domain[0] == '.') {
     317           0 :     int l1 = strlen(host);
     318           0 :     int l2 = strlen(domain);
     319           0 :     if (l1 > l2) {
     320           0 :         return strcmp(host+l1-l2,domain) == 0;
     321             :     } else {
     322           0 :       return 0;
     323             :     }
     324             :   } else {
     325           0 :     return strcmp(host,domain) == 0;
     326             :   }
     327             : }
     328             : 
     329         317 : int make_http_soap_request(zval  *this_ptr,
     330             :                            char  *buf,
     331             :                            int    buf_size,
     332             :                            char  *location,
     333             :                            char  *soapaction,
     334             :                            int    soap_version,
     335             :                            char **buffer,
     336             :                            int   *buffer_len TSRMLS_DC)
     337             : {
     338             :         char *request;
     339         317 :         smart_str soap_headers = {0};
     340         317 :         smart_str soap_headers_z = {0};
     341             :         int request_size, err;
     342         317 :         php_url *phpurl = NULL;
     343             :         php_stream *stream;
     344             :         zval **trace, **tmp;
     345         317 :         int use_proxy = 0;
     346             :         int use_ssl;
     347             :         char *http_headers, *http_body, *content_type, *http_version, *cookie_itt;
     348             :         int http_header_size, http_body_size, http_close;
     349             :         char *connection;
     350             :         int http_1_1;
     351             :         int http_status;
     352         317 :         int content_type_xml = 0;
     353         317 :         long redirect_max = 20;
     354             :         char *content_encoding;
     355         317 :         char *http_msg = NULL;
     356             :         zend_bool old_allow_url_fopen;
     357         317 :         php_stream_context *context = NULL;
     358         317 :         zend_bool has_authorization = 0;
     359         317 :         zend_bool has_proxy_authorization = 0;
     360         317 :         zend_bool has_cookies = 0;
     361             : 
     362         317 :         if (this_ptr == NULL || Z_TYPE_P(this_ptr) != IS_OBJECT) {
     363           0 :                 return FALSE;
     364             :         }
     365             : 
     366         317 :   request = buf;
     367         317 :   request_size = buf_size;
     368             :         /* Compress request */
     369         317 :         if (zend_hash_find(Z_OBJPROP_P(this_ptr), "compression", sizeof("compression"), (void **)&tmp) == SUCCESS && Z_TYPE_PP(tmp) == IS_LONG) {
     370           0 :                 int level = Z_LVAL_PP(tmp) & 0x0f;
     371           0 :                 int kind  = Z_LVAL_PP(tmp) & SOAP_COMPRESSION_DEFLATE;
     372             : 
     373           0 :                 if (level > 9) {level = 9;}
     374             :                 
     375           0 :           if ((Z_LVAL_PP(tmp) & SOAP_COMPRESSION_ACCEPT) != 0) {
     376           0 :                         smart_str_append_const(&soap_headers_z,"Accept-Encoding: gzip, deflate\r\n");
     377             :           }
     378           0 :           if (level > 0) {
     379             :                         zval func;
     380             :                         zval retval;
     381             :                         zval param1, param2, param3;
     382             :                         zval *params[3];
     383             :                         int n;
     384             : 
     385           0 :                         params[0] = &param1;
     386           0 :                         INIT_PZVAL(params[0]);
     387           0 :                         params[1] = &param2;
     388           0 :                         INIT_PZVAL(params[1]);
     389           0 :                         params[2] = &param3;
     390           0 :                         INIT_PZVAL(params[2]);
     391           0 :                         ZVAL_STRINGL(params[0], buf, buf_size, 0);
     392           0 :                         ZVAL_LONG(params[1], level);
     393           0 :             if (kind == SOAP_COMPRESSION_DEFLATE) {
     394           0 :                 n = 2;
     395           0 :                                 ZVAL_STRING(&func, "gzcompress", 0);
     396           0 :                                 smart_str_append_const(&soap_headers_z,"Content-Encoding: deflate\r\n");
     397             :             } else {
     398           0 :               n = 3;
     399           0 :                                 ZVAL_STRING(&func, "gzencode", 0);
     400           0 :                                 smart_str_append_const(&soap_headers_z,"Content-Encoding: gzip\r\n");
     401           0 :                                 ZVAL_LONG(params[2], 0x1f);
     402             :             }
     403           0 :                         if (call_user_function(CG(function_table), (zval**)NULL, &func, &retval, n, params TSRMLS_CC) == SUCCESS &&
     404           0 :                             Z_TYPE(retval) == IS_STRING) {
     405           0 :                                 request = Z_STRVAL(retval);
     406           0 :                                 request_size = Z_STRLEN(retval);
     407             :                         } else {
     408           0 :                                 if (request != buf) {efree(request);}
     409           0 :                                 smart_str_free(&soap_headers_z);
     410           0 :                                 return FALSE;
     411             :                         }
     412             :           }
     413             :         }
     414             : 
     415         317 :         if (zend_hash_find(Z_OBJPROP_P(this_ptr), "httpsocket", sizeof("httpsocket"), (void **)&tmp) == SUCCESS) {
     416           0 :                 php_stream_from_zval_no_verify(stream,tmp);
     417           0 :                 if (zend_hash_find(Z_OBJPROP_P(this_ptr), "_use_proxy", sizeof("_use_proxy"), (void **)&tmp) == SUCCESS && Z_TYPE_PP(tmp) == IS_LONG) {
     418           0 :                         use_proxy = Z_LVAL_PP(tmp);
     419             :                 }
     420             :         } else {
     421         317 :                 stream = NULL;
     422             :         }
     423             : 
     424         317 :         if (location != NULL && location[0] != '\000') {
     425         317 :                 phpurl = php_url_parse(location);
     426             :         }
     427             : 
     428         317 :         if (SUCCESS == zend_hash_find(Z_OBJPROP_P(this_ptr),
     429             :                         "_stream_context", sizeof("_stream_context"), (void**)&tmp)) {
     430           1 :                 context = php_stream_context_from_zval(*tmp, 0);
     431             :         }
     432             : 
     433         318 :         if (context && 
     434           1 :                 php_stream_context_get_option(context, "http", "max_redirects", &tmp) == SUCCESS) {
     435           0 :                 if (Z_TYPE_PP(tmp) != IS_STRING || !is_numeric_string(Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp), &redirect_max, NULL, 1)) {
     436           0 :                         if (Z_TYPE_PP(tmp) == IS_LONG)
     437           0 :                                 redirect_max = Z_LVAL_PP(tmp);
     438             :                 }
     439             :         }
     440             : 
     441             : try_again:
     442         317 :         if (phpurl == NULL || phpurl->host == NULL) {
     443         316 :           if (phpurl != NULL) {php_url_free(phpurl);}
     444         316 :                 if (request != buf) {efree(request);}
     445         316 :                 add_soap_fault(this_ptr, "HTTP", "Unable to parse URL", NULL, NULL TSRMLS_CC);
     446         316 :                 smart_str_free(&soap_headers_z);
     447         316 :                 return FALSE;
     448             :         }
     449             : 
     450           1 :         use_ssl = 0;
     451           1 :         if (phpurl->scheme != NULL && strcmp(phpurl->scheme, "https") == 0) {
     452           0 :                 use_ssl = 1;
     453           1 :         } else if (phpurl->scheme == NULL || strcmp(phpurl->scheme, "http") != 0) {
     454           0 :                 php_url_free(phpurl);
     455           0 :                 if (request != buf) {efree(request);}
     456           0 :                 add_soap_fault(this_ptr, "HTTP", "Unknown protocol. Only http and https are allowed.", NULL, NULL TSRMLS_CC);
     457           0 :                 smart_str_free(&soap_headers_z);
     458           0 :                 return FALSE;
     459             :         }
     460             : 
     461           1 :         old_allow_url_fopen = PG(allow_url_fopen);
     462           1 :         PG(allow_url_fopen) = 1;
     463           1 :         if (use_ssl && php_stream_locate_url_wrapper("https://", NULL, STREAM_LOCATE_WRAPPERS_ONLY TSRMLS_CC) == NULL) {
     464           0 :                 php_url_free(phpurl);
     465           0 :                 if (request != buf) {efree(request);}
     466           0 :                 add_soap_fault(this_ptr, "HTTP", "SSL support is not available in this build", NULL, NULL TSRMLS_CC);
     467           0 :                 PG(allow_url_fopen) = old_allow_url_fopen;
     468           0 :                 smart_str_free(&soap_headers_z);
     469           0 :                 return FALSE;
     470             :         }
     471             : 
     472           1 :         if (phpurl->port == 0) {
     473           1 :                 phpurl->port = use_ssl ? 443 : 80;
     474             :         }
     475             : 
     476             :         /* Check if request to the same host */
     477           1 :         if (stream != NULL) {
     478             :           php_url *orig;
     479           0 :                 if (zend_hash_find(Z_OBJPROP_P(this_ptr), "httpurl", sizeof("httpurl"), (void **)&tmp) == SUCCESS &&
     480           0 :                     (orig = (php_url *) zend_fetch_resource(tmp TSRMLS_CC, -1, "httpurl", NULL, 1, le_url)) != NULL &&
     481           0 :                     ((use_proxy && !use_ssl) ||
     482           0 :                      (((use_ssl && orig->scheme != NULL && strcmp(orig->scheme, "https") == 0) ||
     483           0 :                       (!use_ssl && orig->scheme == NULL) ||
     484           0 :                       (!use_ssl && strcmp(orig->scheme, "https") != 0)) &&
     485           0 :                      strcmp(orig->host, phpurl->host) == 0 &&
     486           0 :                      orig->port == phpurl->port))) {
     487             :     } else {
     488           0 :                         php_stream_close(stream);
     489           0 :                         zend_hash_del(Z_OBJPROP_P(this_ptr), "httpurl", sizeof("httpurl"));
     490           0 :                         zend_hash_del(Z_OBJPROP_P(this_ptr), "httpsocket", sizeof("httpsocket"));
     491           0 :                         zend_hash_del(Z_OBJPROP_P(this_ptr), "_use_proxy", sizeof("_use_proxy"));
     492           0 :                         stream = NULL;
     493           0 :                         use_proxy = 0;
     494             :     }
     495             :         }
     496             : 
     497             :         /* Check if keep-alive connection is still opened */
     498           1 :         if (stream != NULL && php_stream_eof(stream)) {
     499           0 :                 php_stream_close(stream);
     500           0 :                 zend_hash_del(Z_OBJPROP_P(this_ptr), "httpurl", sizeof("httpurl"));
     501           0 :                 zend_hash_del(Z_OBJPROP_P(this_ptr), "httpsocket", sizeof("httpsocket"));
     502           0 :                 zend_hash_del(Z_OBJPROP_P(this_ptr), "_use_proxy", sizeof("_use_proxy"));
     503           0 :                 stream = NULL;
     504           0 :                 use_proxy = 0;
     505             :         }
     506             : 
     507           1 :         if (!stream) {
     508           1 :                 stream = http_connect(this_ptr, phpurl, use_ssl, context, &use_proxy TSRMLS_CC);
     509           1 :                 if (stream) {
     510             :                         php_stream_auto_cleanup(stream);
     511           1 :                         add_property_resource(this_ptr, "httpsocket", php_stream_get_resource_id(stream));
     512           1 :                         add_property_long(this_ptr, "_use_proxy", use_proxy);
     513             :                 } else {
     514           0 :                         php_url_free(phpurl);
     515           0 :                         if (request != buf) {efree(request);}
     516           0 :                         add_soap_fault(this_ptr, "HTTP", "Could not connect to host", NULL, NULL TSRMLS_CC);
     517           0 :                         PG(allow_url_fopen) = old_allow_url_fopen;
     518           0 :                         smart_str_free(&soap_headers_z);
     519           0 :                         return FALSE;
     520             :                 }
     521             :         }
     522           1 :         PG(allow_url_fopen) = old_allow_url_fopen;
     523             : 
     524           1 :         if (stream) {
     525             :                 zval **cookies, **login, **password;
     526           1 :           int ret = zend_list_insert(phpurl, le_url TSRMLS_CC);
     527             : 
     528           1 :                 add_property_resource(this_ptr, "httpurl", ret);
     529             :                 /*zend_list_addref(ret);*/
     530             : 
     531           1 :                 if (context && 
     532           0 :                     php_stream_context_get_option(context, "http", "protocol_version", &tmp) == SUCCESS &&
     533           0 :                     Z_TYPE_PP(tmp) == IS_DOUBLE &&
     534           0 :                     Z_DVAL_PP(tmp) == 1.0) {
     535           0 :                         http_1_1 = 0;
     536             :                 } else {
     537           1 :                         http_1_1 = 1;
     538             :                 }
     539             : 
     540           1 :                 smart_str_append_const(&soap_headers, "POST ");
     541           1 :                 if (use_proxy && !use_ssl) {
     542           0 :                         smart_str_appends(&soap_headers, phpurl->scheme);
     543           0 :                         smart_str_append_const(&soap_headers, "://");
     544           0 :                         smart_str_appends(&soap_headers, phpurl->host);
     545           0 :                         smart_str_appendc(&soap_headers, ':');
     546           0 :                         smart_str_append_unsigned(&soap_headers, phpurl->port);
     547             :                 }
     548           1 :                 if (phpurl->path) {
     549           0 :                         smart_str_appends(&soap_headers, phpurl->path);
     550             :                 } else {
     551           1 :                         smart_str_appendc(&soap_headers, '/');
     552             :                 }
     553           1 :                 if (phpurl->query) {
     554           0 :                         smart_str_appendc(&soap_headers, '?');
     555           0 :                         smart_str_appends(&soap_headers, phpurl->query);
     556             :                 }
     557           1 :                 if (phpurl->fragment) {
     558           0 :                         smart_str_appendc(&soap_headers, '#');
     559           0 :                         smart_str_appends(&soap_headers, phpurl->fragment);
     560             :                 }
     561           1 :                 if (http_1_1) {
     562           1 :                         smart_str_append_const(&soap_headers, " HTTP/1.1\r\n");
     563             :                 } else {
     564           0 :                         smart_str_append_const(&soap_headers, " HTTP/1.0\r\n");
     565             :                 }
     566           1 :                 smart_str_append_const(&soap_headers, "Host: ");
     567           1 :                 smart_str_appends(&soap_headers, phpurl->host);
     568           1 :                 if (phpurl->port != (use_ssl?443:80)) {
     569           0 :                         smart_str_appendc(&soap_headers, ':');
     570           0 :                         smart_str_append_unsigned(&soap_headers, phpurl->port);
     571             :                 }
     572           2 :                 if (!http_1_1 ||
     573           1 :                         (zend_hash_find(Z_OBJPROP_P(this_ptr), "_keep_alive", sizeof("_keep_alive"), (void **)&tmp) == SUCCESS &&
     574           0 :                          Z_LVAL_PP(tmp) == 0)) {
     575           0 :                         smart_str_append_const(&soap_headers, "\r\n"
     576             :                                 "Connection: close\r\n");
     577             :                 } else {
     578           1 :                         smart_str_append_const(&soap_headers, "\r\n"
     579             :                                 "Connection: Keep-Alive\r\n");
     580             :                 }
     581           1 :                 if (zend_hash_find(Z_OBJPROP_P(this_ptr), "_user_agent", sizeof("_user_agent"), (void **)&tmp) == SUCCESS &&
     582           0 :                     Z_TYPE_PP(tmp) == IS_STRING) {
     583           0 :                         if (Z_STRLEN_PP(tmp) > 0) {
     584           0 :                                 smart_str_append_const(&soap_headers, "User-Agent: ");
     585           0 :                                 smart_str_appendl(&soap_headers, Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp));
     586           0 :                                 smart_str_append_const(&soap_headers, "\r\n");
     587             :                         }
     588           1 :                 } else if (context && 
     589           0 :                            php_stream_context_get_option(context, "http", "user_agent", &tmp) == SUCCESS &&
     590           0 :                            Z_TYPE_PP(tmp) == IS_STRING) {
     591           0 :                         if (Z_STRLEN_PP(tmp) > 0) {
     592           0 :                                 smart_str_append_const(&soap_headers, "User-Agent: ");
     593           0 :                                 smart_str_appendl(&soap_headers, Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp));
     594           0 :                                 smart_str_append_const(&soap_headers, "\r\n");
     595             :                         }
     596           1 :                 } else if (FG(user_agent)) {
     597           0 :                         smart_str_append_const(&soap_headers, "User-Agent: ");
     598           0 :                         smart_str_appends(&soap_headers, FG(user_agent));
     599           0 :                         smart_str_append_const(&soap_headers, "\r\n");
     600             :                 } else {
     601           1 :                         smart_str_append_const(&soap_headers, "User-Agent: PHP-SOAP/"PHP_VERSION"\r\n");
     602             :                 }
     603             : 
     604           1 :                 smart_str_append(&soap_headers, &soap_headers_z);
     605             : 
     606           1 :                 if (soap_version == SOAP_1_2) {
     607           0 :                         smart_str_append_const(&soap_headers,"Content-Type: application/soap+xml; charset=utf-8");
     608           0 :                         if (soapaction) {
     609           0 :                                 smart_str_append_const(&soap_headers,"; action=\"");
     610           0 :                                 smart_str_appends(&soap_headers, soapaction);
     611           0 :                                 smart_str_append_const(&soap_headers,"\"");
     612             :                         }
     613           0 :                         smart_str_append_const(&soap_headers,"\r\n");
     614             :                 } else {
     615           1 :                         smart_str_append_const(&soap_headers,"Content-Type: text/xml; charset=utf-8\r\n");
     616           1 :                         if (soapaction) {
     617           1 :                                 smart_str_append_const(&soap_headers, "SOAPAction: \"");
     618           1 :                                 smart_str_appends(&soap_headers, soapaction);
     619           1 :                                 smart_str_append_const(&soap_headers, "\"\r\n");
     620             :                         }
     621             :                 }
     622           1 :                 smart_str_append_const(&soap_headers,"Content-Length: ");
     623           1 :                 smart_str_append_long(&soap_headers, request_size);
     624           1 :                 smart_str_append_const(&soap_headers, "\r\n");
     625             : 
     626             :                 /* HTTP Authentication */
     627           1 :                 if (zend_hash_find(Z_OBJPROP_P(this_ptr), "_login", sizeof("_login"), (void **)&login) == SUCCESS &&
     628           0 :                     Z_TYPE_PP(login) == IS_STRING) {
     629             :                         zval **digest;
     630             : 
     631           0 :                         has_authorization = 1;
     632           0 :                         if (zend_hash_find(Z_OBJPROP_P(this_ptr), "_digest", sizeof("_digest"), (void **)&digest) == SUCCESS) {
     633           0 :                                 if (Z_TYPE_PP(digest) == IS_ARRAY) {
     634             :                                         char          HA1[33], HA2[33], response[33], cnonce[33], nc[9];
     635             :                                         PHP_MD5_CTX   md5ctx;
     636             :                                         unsigned char hash[16];
     637             : 
     638           0 :                                         PHP_MD5Init(&md5ctx);
     639           0 :                                         snprintf(cnonce, sizeof(cnonce), "%ld", php_rand(TSRMLS_C));
     640           0 :                                         PHP_MD5Update(&md5ctx, (unsigned char*)cnonce, strlen(cnonce));
     641           0 :                                         PHP_MD5Final(hash, &md5ctx);
     642           0 :                                         make_digest(cnonce, hash);
     643             : 
     644           0 :                                         if (zend_hash_find(Z_ARRVAL_PP(digest), "nc", sizeof("nc"), (void **)&tmp) == SUCCESS &&
     645           0 :                                             Z_TYPE_PP(tmp) == IS_LONG) {
     646           0 :                                                 Z_LVAL_PP(tmp)++;
     647           0 :                                                 snprintf(nc, sizeof(nc), "%08ld", Z_LVAL_PP(tmp));
     648             :                                         } else {
     649           0 :                                                 add_assoc_long(*digest, "nc", 1);
     650           0 :                                                 strcpy(nc, "00000001");
     651             :                                         }
     652             : 
     653           0 :                                         PHP_MD5Init(&md5ctx);
     654           0 :                                         PHP_MD5Update(&md5ctx, (unsigned char*)Z_STRVAL_PP(login), Z_STRLEN_PP(login));
     655           0 :                                         PHP_MD5Update(&md5ctx, (unsigned char*)":", 1);
     656           0 :                                         if (zend_hash_find(Z_ARRVAL_PP(digest), "realm", sizeof("realm"), (void **)&tmp) == SUCCESS &&
     657           0 :                                             Z_TYPE_PP(tmp) == IS_STRING) {
     658           0 :                                                 PHP_MD5Update(&md5ctx, (unsigned char*)Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp));
     659             :                                         }
     660           0 :                                         PHP_MD5Update(&md5ctx, (unsigned char*)":", 1);
     661           0 :                                         if (zend_hash_find(Z_OBJPROP_P(this_ptr), "_password", sizeof("_password"), (void **)&password) == SUCCESS &&
     662           0 :                                             Z_TYPE_PP(password) == IS_STRING) {
     663           0 :                                                 PHP_MD5Update(&md5ctx, (unsigned char*)Z_STRVAL_PP(password), Z_STRLEN_PP(password));
     664             :                                         }
     665           0 :                                         PHP_MD5Final(hash, &md5ctx);
     666           0 :                                         make_digest(HA1, hash);
     667           0 :                                         if (zend_hash_find(Z_ARRVAL_PP(digest), "algorithm", sizeof("algorithm"), (void **)&tmp) == SUCCESS &&
     668           0 :                                             Z_TYPE_PP(tmp) == IS_STRING &&
     669           0 :                                             Z_STRLEN_PP(tmp) == sizeof("md5-sess")-1 &&
     670           0 :                                             stricmp(Z_STRVAL_PP(tmp), "md5-sess") == 0) {
     671           0 :                                                 PHP_MD5Init(&md5ctx);
     672           0 :                                                 PHP_MD5Update(&md5ctx, (unsigned char*)HA1, 32);
     673           0 :                                                 PHP_MD5Update(&md5ctx, (unsigned char*)":", 1);
     674           0 :                                                 if (zend_hash_find(Z_ARRVAL_PP(digest), "nonce", sizeof("nonce"), (void **)&tmp) == SUCCESS &&
     675           0 :                                                     Z_TYPE_PP(tmp) == IS_STRING) {
     676           0 :                                                         PHP_MD5Update(&md5ctx, (unsigned char*)Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp));
     677             :                                                 }
     678           0 :                                                 PHP_MD5Update(&md5ctx, (unsigned char*)":", 1);
     679           0 :                                                 PHP_MD5Update(&md5ctx, (unsigned char*)cnonce, 8);
     680           0 :                                                 PHP_MD5Final(hash, &md5ctx);
     681           0 :                                                 make_digest(HA1, hash);
     682             :                                         }
     683             : 
     684           0 :                                         PHP_MD5Init(&md5ctx);
     685           0 :                                         PHP_MD5Update(&md5ctx, (unsigned char*)"POST:", sizeof("POST:")-1);
     686           0 :                                         if (phpurl->path) {
     687           0 :                                                 PHP_MD5Update(&md5ctx, (unsigned char*)phpurl->path, strlen(phpurl->path));
     688             :                                         } else {
     689           0 :                                                 PHP_MD5Update(&md5ctx, (unsigned char*)"/", 1);
     690             :                                         }
     691           0 :                                         if (phpurl->query) {
     692           0 :                                                 PHP_MD5Update(&md5ctx, (unsigned char*)"?", 1);
     693           0 :                                                 PHP_MD5Update(&md5ctx, (unsigned char*)phpurl->query, strlen(phpurl->query));
     694             :                                         }
     695             : 
     696             :                                         /* TODO: Support for qop="auth-int" */
     697             : /*
     698             :                                         if (zend_hash_find(Z_ARRVAL_PP(digest), "qop", sizeof("qop"), (void **)&tmp) == SUCCESS &&
     699             :                                             Z_TYPE_PP(tmp) == IS_STRING &&
     700             :                                             Z_STRLEN_PP(tmp) == sizeof("auth-int")-1 &&
     701             :                                             stricmp(Z_STRVAL_PP(tmp), "auth-int") == 0) {
     702             :                                                 PHP_MD5Update(&md5ctx, ":", 1);
     703             :                                                 PHP_MD5Update(&md5ctx, HEntity, HASHHEXLEN);
     704             :                                         }
     705             : */
     706           0 :                                         PHP_MD5Final(hash, &md5ctx);
     707           0 :                                         make_digest(HA2, hash);
     708             : 
     709           0 :                                         PHP_MD5Init(&md5ctx);
     710           0 :                                         PHP_MD5Update(&md5ctx, (unsigned char*)HA1, 32);
     711           0 :                                         PHP_MD5Update(&md5ctx, (unsigned char*)":", 1);
     712           0 :                                         if (zend_hash_find(Z_ARRVAL_PP(digest), "nonce", sizeof("nonce"), (void **)&tmp) == SUCCESS &&
     713           0 :                                             Z_TYPE_PP(tmp) == IS_STRING) {
     714           0 :                                                 PHP_MD5Update(&md5ctx, (unsigned char*)Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp));
     715             :                                         }
     716           0 :                                         PHP_MD5Update(&md5ctx, (unsigned char*)":", 1);
     717           0 :                                         if (zend_hash_find(Z_ARRVAL_PP(digest), "qop", sizeof("qop"), (void **)&tmp) == SUCCESS &&
     718           0 :                                             Z_TYPE_PP(tmp) == IS_STRING) {
     719           0 :                                                 PHP_MD5Update(&md5ctx, (unsigned char*)nc, 8);
     720           0 :                                                 PHP_MD5Update(&md5ctx, (unsigned char*)":", 1);
     721           0 :                                                 PHP_MD5Update(&md5ctx, (unsigned char*)cnonce, 8);
     722           0 :                                                 PHP_MD5Update(&md5ctx, (unsigned char*)":", 1);
     723             :                                                 /* TODO: Support for qop="auth-int" */
     724           0 :                                                 PHP_MD5Update(&md5ctx, (unsigned char*)"auth", sizeof("auth")-1);
     725           0 :                                                 PHP_MD5Update(&md5ctx, (unsigned char*)":", 1);
     726             :                                         }
     727           0 :                                         PHP_MD5Update(&md5ctx, (unsigned char*)HA2, 32);
     728           0 :                                         PHP_MD5Final(hash, &md5ctx);
     729           0 :                                         make_digest(response, hash);
     730             :         
     731           0 :                                         smart_str_append_const(&soap_headers, "Authorization: Digest username=\"");
     732           0 :                                         smart_str_appendl(&soap_headers, Z_STRVAL_PP(login), Z_STRLEN_PP(login));
     733           0 :                                         if (zend_hash_find(Z_ARRVAL_PP(digest), "realm", sizeof("realm"), (void **)&tmp) == SUCCESS &&
     734           0 :                                             Z_TYPE_PP(tmp) == IS_STRING) {
     735           0 :                                                 smart_str_append_const(&soap_headers, "\", realm=\"");
     736           0 :                                                 smart_str_appendl(&soap_headers, Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp));
     737             :                                         }
     738           0 :                                         if (zend_hash_find(Z_ARRVAL_PP(digest), "nonce", sizeof("nonce"), (void **)&tmp) == SUCCESS &&
     739           0 :                                             Z_TYPE_PP(tmp) == IS_STRING) {
     740           0 :                                                 smart_str_append_const(&soap_headers, "\", nonce=\"");
     741           0 :                                                 smart_str_appendl(&soap_headers, Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp));
     742             :                                         }
     743           0 :                                         smart_str_append_const(&soap_headers, "\", uri=\"");
     744           0 :                                         if (phpurl->path) {
     745           0 :                                                 smart_str_appends(&soap_headers, phpurl->path);
     746             :                                         } else {
     747           0 :                                                 smart_str_appendc(&soap_headers, '/');
     748             :                                         } 
     749           0 :                                         if (phpurl->query) {
     750           0 :                                                 smart_str_appendc(&soap_headers, '?');
     751           0 :                                                 smart_str_appends(&soap_headers, phpurl->query);
     752             :                                         }
     753           0 :                                         if (phpurl->fragment) {
     754           0 :                                                 smart_str_appendc(&soap_headers, '#');
     755           0 :                                                 smart_str_appends(&soap_headers, phpurl->fragment);
     756             :                                         }
     757           0 :                                         if (zend_hash_find(Z_ARRVAL_PP(digest), "qop", sizeof("qop"), (void **)&tmp) == SUCCESS &&
     758           0 :                                             Z_TYPE_PP(tmp) == IS_STRING) {
     759             :                                         /* TODO: Support for qop="auth-int" */
     760           0 :                                                 smart_str_append_const(&soap_headers, "\", qop=\"auth");
     761           0 :                                                 smart_str_append_const(&soap_headers, "\", nc=\"");
     762           0 :                                                 smart_str_appendl(&soap_headers, nc, 8);
     763           0 :                                                 smart_str_append_const(&soap_headers, "\", cnonce=\"");
     764           0 :                                                 smart_str_appendl(&soap_headers, cnonce, 8);
     765             :                                         }
     766           0 :                                         smart_str_append_const(&soap_headers, "\", response=\"");
     767           0 :                                         smart_str_appendl(&soap_headers, response, 32);
     768           0 :                                         if (zend_hash_find(Z_ARRVAL_PP(digest), "opaque", sizeof("opaque"), (void **)&tmp) == SUCCESS &&
     769           0 :                                             Z_TYPE_PP(tmp) == IS_STRING) {
     770           0 :                                                 smart_str_append_const(&soap_headers, "\", opaque=\"");
     771           0 :                                                 smart_str_appendl(&soap_headers, Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp));
     772             :                                         }
     773           0 :                                         if (zend_hash_find(Z_ARRVAL_PP(digest), "algorithm", sizeof("algorithm"), (void **)&tmp) == SUCCESS &&
     774           0 :                                                 Z_TYPE_PP(tmp) == IS_STRING) {
     775           0 :                                                 smart_str_append_const(&soap_headers, "\", algorithm=\"");
     776           0 :                                                 smart_str_appendl(&soap_headers, Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp));
     777             :                                         }
     778           0 :                                         smart_str_append_const(&soap_headers, "\"\r\n");
     779             :                                 }
     780             :                         } else {
     781             :                                 unsigned char* buf;
     782             :                                 int len;
     783             : 
     784           0 :                                 smart_str auth = {0};
     785           0 :                                 smart_str_appendl(&auth, Z_STRVAL_PP(login), Z_STRLEN_PP(login));
     786           0 :                                 smart_str_appendc(&auth, ':');
     787           0 :                                 if (zend_hash_find(Z_OBJPROP_P(this_ptr), "_password", sizeof("_password"), (void **)&password) == SUCCESS &&
     788           0 :                                     Z_TYPE_PP(password) == IS_STRING) {
     789           0 :                                         smart_str_appendl(&auth, Z_STRVAL_PP(password), Z_STRLEN_PP(password));
     790             :                                 }
     791           0 :                                 smart_str_0(&auth);
     792           0 :                                 buf = php_base64_encode((unsigned char*)auth.c, auth.len, &len);
     793           0 :                                 smart_str_append_const(&soap_headers, "Authorization: Basic ");
     794           0 :                                 smart_str_appendl(&soap_headers, (char*)buf, len);
     795           0 :                                 smart_str_append_const(&soap_headers, "\r\n");
     796           0 :                                 efree(buf);
     797           0 :                                 smart_str_free(&auth);
     798             :                         }
     799             :                 }
     800             : 
     801             :                 /* Proxy HTTP Authentication */
     802           1 :                 if (use_proxy && !use_ssl) {
     803           0 :                         has_proxy_authorization = proxy_authentication(this_ptr, &soap_headers TSRMLS_CC);
     804             :                 }
     805             : 
     806             :                 /* Send cookies along with request */
     807           1 :                 if (zend_hash_find(Z_OBJPROP_P(this_ptr), "_cookies", sizeof("_cookies"), (void **)&cookies) == SUCCESS) {
     808             :                         zval **data;
     809             :                         char *key;
     810             :                         uint key_len;
     811             :                         int i, n;
     812             : 
     813           0 :                         has_cookies = 1;
     814           0 :                         n = zend_hash_num_elements(Z_ARRVAL_PP(cookies));
     815           0 :                         if (n > 0) {
     816           0 :                                 zend_hash_internal_pointer_reset(Z_ARRVAL_PP(cookies));
     817           0 :                                 smart_str_append_const(&soap_headers, "Cookie: ");
     818           0 :                                 for (i = 0; i < n; i++) {
     819           0 :                                         zend_hash_get_current_data(Z_ARRVAL_PP(cookies), (void **)&data);
     820           0 :                                         zend_hash_get_current_key_ex(Z_ARRVAL_PP(cookies), &key, &key_len, NULL, 0, NULL);
     821             : 
     822           0 :                                         if (Z_TYPE_PP(data) == IS_ARRAY) {
     823             :                                           zval** value;
     824             : 
     825           0 :                                                 if (zend_hash_index_find(Z_ARRVAL_PP(data), 0, (void**)&value) == SUCCESS &&
     826           0 :                                                     Z_TYPE_PP(value) == IS_STRING) {
     827             :                                                   zval **tmp;
     828           0 :                                                   if ((zend_hash_index_find(Z_ARRVAL_PP(data), 1, (void**)&tmp) == FAILURE ||
     829           0 :                                                        strncmp(phpurl->path?phpurl->path:"/",Z_STRVAL_PP(tmp),Z_STRLEN_PP(tmp)) == 0) &&
     830           0 :                                                       (zend_hash_index_find(Z_ARRVAL_PP(data), 2, (void**)&tmp) == FAILURE ||
     831           0 :                                                        in_domain(phpurl->host,Z_STRVAL_PP(tmp))) &&
     832           0 :                                                       (use_ssl || zend_hash_index_find(Z_ARRVAL_PP(data), 3, (void**)&tmp) == FAILURE)) {
     833           0 :                                                                 smart_str_appendl(&soap_headers, key, key_len);
     834           0 :                                                                 smart_str_appendc(&soap_headers, '=');
     835           0 :                                                                 smart_str_appendl(&soap_headers, Z_STRVAL_PP(value), Z_STRLEN_PP(value));
     836           0 :                                                                 smart_str_appendc(&soap_headers, ';');
     837             :                                                         }
     838             :                                                 }
     839             :                                         }
     840           0 :                                         zend_hash_move_forward(Z_ARRVAL_PP(cookies));
     841             :                                 }
     842           0 :                                 smart_str_append_const(&soap_headers, "\r\n");
     843             :                         }
     844             :                 }
     845             : 
     846           1 :                 http_context_headers(context, has_authorization, has_proxy_authorization, has_cookies, &soap_headers TSRMLS_CC);
     847             : 
     848           1 :                 smart_str_append_const(&soap_headers, "\r\n");
     849           1 :                 smart_str_0(&soap_headers);
     850           2 :                 if (zend_hash_find(Z_OBJPROP_P(this_ptr), "trace", sizeof("trace"), (void **) &trace) == SUCCESS &&
     851           1 :                     Z_LVAL_PP(trace) > 0) {
     852           1 :                         add_property_stringl(this_ptr, "__last_request_headers", soap_headers.c, soap_headers.len, 1);
     853             :                 }
     854           1 :                 smart_str_appendl(&soap_headers, request, request_size);
     855           1 :                 smart_str_0(&soap_headers);
     856             : 
     857           1 :                 err = php_stream_write(stream, soap_headers.c, soap_headers.len);
     858           1 :                 if (err != soap_headers.len) {
     859           0 :                         if (request != buf) {efree(request);}
     860           0 :                         php_stream_close(stream);
     861           0 :                         zend_hash_del(Z_OBJPROP_P(this_ptr), "httpurl", sizeof("httpurl"));
     862           0 :                         zend_hash_del(Z_OBJPROP_P(this_ptr), "httpsocket", sizeof("httpsocket"));
     863           0 :                         zend_hash_del(Z_OBJPROP_P(this_ptr), "_use_proxy", sizeof("_use_proxy"));
     864           0 :                         add_soap_fault(this_ptr, "HTTP", "Failed Sending HTTP SOAP request", NULL, NULL TSRMLS_CC);
     865           0 :                         smart_str_free(&soap_headers_z);
     866           0 :                         return FALSE;
     867             :                 }
     868           1 :                 smart_str_free(&soap_headers);
     869             :         } else {
     870           0 :                 add_soap_fault(this_ptr, "HTTP", "Failed to create stream??", NULL, NULL TSRMLS_CC);
     871           0 :                 smart_str_free(&soap_headers_z);
     872           0 :                 return FALSE;
     873             :         }
     874             : 
     875           1 :         if (!buffer) {
     876           0 :                 php_stream_close(stream);
     877           0 :                 zend_hash_del(Z_OBJPROP_P(this_ptr), "httpsocket", sizeof("httpsocket"));
     878           0 :                 zend_hash_del(Z_OBJPROP_P(this_ptr), "_use_proxy", sizeof("_use_proxy"));
     879           0 :                 smart_str_free(&soap_headers_z);
     880           0 :                 return TRUE;
     881             :         }
     882             : 
     883             :         do {
     884           1 :                 if (!get_http_headers(stream, &http_headers, &http_header_size TSRMLS_CC)) {
     885           0 :                         if (http_headers) {efree(http_headers);}
     886           0 :                         if (request != buf) {efree(request);}
     887           0 :                         php_stream_close(stream);
     888           0 :                         zend_hash_del(Z_OBJPROP_P(this_ptr), "httpsocket", sizeof("httpsocket"));
     889           0 :                         zend_hash_del(Z_OBJPROP_P(this_ptr), "_use_proxy", sizeof("_use_proxy"));
     890           0 :                         add_soap_fault(this_ptr, "HTTP", "Error Fetching http headers", NULL, NULL TSRMLS_CC);
     891           0 :                         smart_str_free(&soap_headers_z);
     892           0 :                         return FALSE;
     893             :                 }
     894             : 
     895           2 :                 if (zend_hash_find(Z_OBJPROP_P(this_ptr), "trace", sizeof("trace"), (void **) &trace) == SUCCESS &&
     896           1 :                     Z_LVAL_PP(trace) > 0) {
     897           1 :                         add_property_stringl(this_ptr, "__last_response_headers", http_headers, http_header_size, 1);
     898             :                 }
     899             : 
     900             :                 /* Check to see what HTTP status was sent */
     901           1 :                 http_1_1 = 0;
     902           1 :                 http_status = 0;
     903           1 :                 http_version = get_http_header_value(http_headers,"HTTP/");
     904           1 :                 if (http_version) {
     905             :                         char *tmp;
     906             : 
     907           1 :                         if (!strncmp(http_version,"1.1", 3)) {
     908           1 :                                 http_1_1 = 1;
     909             :                         }
     910             : 
     911           1 :                         tmp = strstr(http_version," ");
     912           1 :                         if (tmp != NULL) {
     913           1 :                                 tmp++;
     914           1 :                                 http_status = atoi(tmp);
     915             :                         }
     916           1 :                         tmp = strstr(tmp," ");
     917           1 :                         if (tmp != NULL) {
     918           1 :                                 tmp++;
     919           1 :                                 if (http_msg) {
     920           0 :                                         efree(http_msg);
     921             :                                 }
     922           1 :                                 http_msg = estrdup(tmp);
     923             :                         }
     924           1 :                         efree(http_version);
     925             : 
     926             :                         /* Try and get headers again */
     927           1 :                         if (http_status == 100) {
     928           0 :                                 efree(http_headers);
     929             :                         }
     930             :                 }
     931           1 :         } while (http_status == 100);
     932             : 
     933             :         /* Grab and send back every cookie */
     934             : 
     935             :         /* Not going to worry about Path: because
     936             :            we shouldn't be changing urls so path dont
     937             :            matter too much
     938             :         */
     939           1 :         cookie_itt = strstr(http_headers,"Set-Cookie: ");
     940           2 :         while (cookie_itt) {
     941             :                 char *end_pos, *cookie;
     942             :                 char *eqpos, *sempos;
     943             :                 zval **cookies;
     944             : 
     945           0 :                 if (zend_hash_find(Z_OBJPROP_P(this_ptr), "_cookies", sizeof("_cookies"), (void **)&cookies) == FAILURE) {
     946             :                         zval *tmp_cookies;
     947           0 :                         MAKE_STD_ZVAL(tmp_cookies);
     948           0 :                         array_init(tmp_cookies);
     949           0 :                         zend_hash_update(Z_OBJPROP_P(this_ptr), "_cookies", sizeof("_cookies"), &tmp_cookies, sizeof(zval *), (void **)&cookies);
     950             :                 }
     951             : 
     952           0 :                 end_pos = strstr(cookie_itt,"\r\n");
     953           0 :                 cookie = get_http_header_value(cookie_itt,"Set-Cookie: ");
     954             : 
     955           0 :                 eqpos = strstr(cookie, "=");
     956           0 :                 sempos = strstr(cookie, ";");
     957           0 :                 if (eqpos != NULL && (sempos == NULL || sempos > eqpos)) {
     958           0 :                         smart_str name = {0};
     959             :                         int cookie_len;
     960             :                         zval *zcookie;
     961             : 
     962           0 :                         if (sempos != NULL) {
     963           0 :                                 cookie_len = sempos-(eqpos+1);
     964             :                         } else {
     965           0 :                                 cookie_len = strlen(cookie)-(eqpos-cookie)-1;
     966             :                         }
     967             : 
     968           0 :                         smart_str_appendl(&name, cookie, eqpos - cookie);
     969           0 :                         smart_str_0(&name);
     970             : 
     971           0 :                         ALLOC_INIT_ZVAL(zcookie);
     972           0 :                         array_init(zcookie);
     973           0 :                         add_index_stringl(zcookie, 0, eqpos + 1, cookie_len, 1);
     974             : 
     975           0 :                         if (sempos != NULL) {
     976           0 :                                 char *options = cookie + cookie_len+1;
     977           0 :                                 while (*options) {
     978           0 :                                         while (*options == ' ') {options++;}
     979           0 :                                         sempos = strstr(options, ";");
     980           0 :                                         if (strstr(options,"path=") == options) {
     981           0 :                                                 eqpos = options + sizeof("path=")-1;
     982           0 :                                                 add_index_stringl(zcookie, 1, eqpos, sempos?(sempos-eqpos):strlen(eqpos), 1);
     983           0 :                                         } else if (strstr(options,"domain=") == options) {
     984           0 :                                                 eqpos = options + sizeof("domain=")-1;
     985           0 :                                                 add_index_stringl(zcookie, 2, eqpos, sempos?(sempos-eqpos):strlen(eqpos), 1);
     986           0 :                                         } else if (strstr(options,"secure") == options) {
     987           0 :                                                 add_index_bool(zcookie, 3, 1);
     988             :                                         }
     989           0 :                                         if (sempos != NULL) {
     990           0 :                                                 options = sempos+1;
     991             :                                         } else {
     992           0 :                                           break;
     993             :                                         }
     994             :                                 }
     995             :                         }
     996           0 :                         if (!zend_hash_index_exists(Z_ARRVAL_P(zcookie), 1)) {
     997           0 :                                 char *t = phpurl->path?phpurl->path:"/";
     998           0 :                                 char *c = strrchr(t, '/');
     999           0 :                                 if (c) {
    1000           0 :                                         add_index_stringl(zcookie, 1, t, c-t, 1);
    1001             :                                 }
    1002             :                         }
    1003           0 :                         if (!zend_hash_index_exists(Z_ARRVAL_P(zcookie), 2)) {
    1004           0 :                                 add_index_string(zcookie, 2, phpurl->host, 1);
    1005             :                         }
    1006             : 
    1007           0 :                         add_assoc_zval_ex(*cookies, name.c, name.len+1, zcookie);
    1008           0 :                         smart_str_free(&name);
    1009             :                 }
    1010             : 
    1011           0 :                 cookie_itt = strstr(cookie_itt + sizeof("Set-Cookie: "), "Set-Cookie: ");
    1012           0 :                 efree(cookie);
    1013             :         }
    1014             : 
    1015             :         /* See if the server requested a close */
    1016           1 :         if (http_1_1) {
    1017           1 :                 http_close = FALSE;
    1018           1 :                 if (use_proxy && !use_ssl) {
    1019           0 :                         connection = get_http_header_value(http_headers,"Proxy-Connection: ");
    1020           0 :                         if (connection) {
    1021           0 :                                 if (strncasecmp(connection, "close", sizeof("close")-1) == 0) {
    1022           0 :                                         http_close = TRUE;
    1023             :                                 }
    1024           0 :                                 efree(connection);
    1025             :                         }
    1026             :                 }
    1027           1 :                 if (http_close == FALSE) {
    1028           1 :                         connection = get_http_header_value(http_headers,"Connection: ");
    1029           1 :                         if (connection) {
    1030           1 :                                 if (strncasecmp(connection, "close", sizeof("close")-1) == 0) {
    1031           1 :                                         http_close = TRUE;
    1032             :                                 }
    1033           1 :                                 efree(connection);
    1034             :                         }
    1035             :                 }
    1036             :         } else {
    1037           0 :                 http_close = TRUE;
    1038           0 :                 if (use_proxy && !use_ssl) {
    1039           0 :                         connection = get_http_header_value(http_headers,"Proxy-Connection: ");
    1040           0 :                         if (connection) {
    1041           0 :                                 if (strncasecmp(connection, "Keep-Alive", sizeof("Keep-Alive")-1) == 0) {
    1042           0 :                                         http_close = FALSE;
    1043             :                                 }
    1044           0 :                                 efree(connection);
    1045             :                         }
    1046             :                 }
    1047           0 :                 if (http_close == TRUE) {
    1048           0 :                         connection = get_http_header_value(http_headers,"Connection: ");
    1049           0 :                         if (connection) {
    1050           0 :                                 if (strncasecmp(connection, "Keep-Alive", sizeof("Keep-Alive")-1) == 0) {
    1051           0 :                                         http_close = FALSE;
    1052             :                                 }
    1053           0 :                                 efree(connection);
    1054             :                         }
    1055             :                 }
    1056             :         }       
    1057             : 
    1058           1 :         if (!get_http_body(stream, http_close, http_headers, &http_body, &http_body_size TSRMLS_CC)) {
    1059           0 :                 if (request != buf) {efree(request);}
    1060           0 :                 php_stream_close(stream);
    1061           0 :                 efree(http_headers);
    1062           0 :                 zend_hash_del(Z_OBJPROP_P(this_ptr), "httpsocket", sizeof("httpsocket"));
    1063           0 :                 zend_hash_del(Z_OBJPROP_P(this_ptr), "_use_proxy", sizeof("_use_proxy"));
    1064           0 :                 add_soap_fault(this_ptr, "HTTP", "Error Fetching http body, No Content-Length, connection closed or chunked data", NULL, NULL TSRMLS_CC);
    1065           0 :                 if (http_msg) {
    1066           0 :                         efree(http_msg);
    1067             :                 }
    1068           0 :                 smart_str_free(&soap_headers_z);
    1069           0 :                 return FALSE;
    1070             :         }
    1071             : 
    1072           1 :         if (request != buf) {efree(request);}
    1073             : 
    1074           1 :         if (http_close) {
    1075           1 :                 php_stream_close(stream);
    1076           1 :                 zend_hash_del(Z_OBJPROP_P(this_ptr), "httpsocket", sizeof("httpsocket"));
    1077           1 :                 zend_hash_del(Z_OBJPROP_P(this_ptr), "_use_proxy", sizeof("_use_proxy"));
    1078           1 :                 stream = NULL;
    1079             :         }
    1080             : 
    1081             :         /* Process HTTP status codes */
    1082           1 :         if (http_status >= 300 && http_status < 400) {
    1083             :                 char *loc;
    1084             : 
    1085           0 :                 if ((loc = get_http_header_value(http_headers,"Location: ")) != NULL) {
    1086           0 :                         php_url *new_url  = php_url_parse(loc);
    1087             : 
    1088           0 :                         if (new_url != NULL) {
    1089           0 :                                 efree(http_headers);
    1090           0 :                                 efree(http_body);
    1091           0 :                                 efree(loc);
    1092           0 :                                 if (new_url->scheme == NULL && new_url->path != NULL) {
    1093           0 :                                         new_url->scheme = phpurl->scheme ? estrdup(phpurl->scheme) : NULL;
    1094           0 :                                         new_url->host = phpurl->host ? estrdup(phpurl->host) : NULL;
    1095           0 :                                         new_url->port = phpurl->port;
    1096           0 :                                         if (new_url->path && new_url->path[0] != '/') {
    1097           0 :                                                 if (phpurl->path) {
    1098           0 :                                                         char *t = phpurl->path;
    1099           0 :                                                         char *p = strrchr(t, '/');
    1100           0 :                                                         if (p) {
    1101           0 :                                                                 char *s = emalloc((p - t) + strlen(new_url->path) + 2);
    1102           0 :                                                                 strncpy(s, t, (p - t) + 1);
    1103           0 :                                                                 s[(p - t) + 1] = 0;
    1104           0 :                                                                 strcat(s, new_url->path);
    1105           0 :                                                                 efree(new_url->path);
    1106           0 :                                                                 new_url->path = s;
    1107             :                                                         } 
    1108             :                                                 } else {
    1109           0 :                                                         char *s = emalloc(strlen(new_url->path) + 2);
    1110           0 :                                                         s[0] = '/'; s[1] = 0;
    1111           0 :                                                         strcat(s, new_url->path);
    1112           0 :                                                         efree(new_url->path);
    1113           0 :                                                         new_url->path = s;
    1114             :                                                 }
    1115             :                                         }
    1116             :                                 }
    1117           0 :                                 phpurl = new_url;
    1118             : 
    1119           0 :                                 if (--redirect_max < 1) {
    1120           0 :                                         add_soap_fault(this_ptr, "HTTP", "Redirection limit reached, aborting", NULL, NULL TSRMLS_CC);
    1121           0 :                                         smart_str_free(&soap_headers_z);
    1122           0 :                                         return FALSE;
    1123             :                                 }
    1124             : 
    1125           0 :                                 goto try_again;
    1126             :                         }
    1127             :                 }
    1128           1 :         } else if (http_status == 401) {
    1129             :                 /* Digest authentication */
    1130             :                 zval **digest, **login, **password;
    1131           0 :                 char *auth = get_http_header_value(http_headers, "WWW-Authenticate: ");
    1132             : 
    1133           0 :                 if (auth &&
    1134           0 :                                 strstr(auth, "Digest") == auth &&
    1135           0 :                     (zend_hash_find(Z_OBJPROP_P(this_ptr), "_digest", sizeof("_digest"), (void **)&digest) == FAILURE ||
    1136           0 :                      Z_TYPE_PP(digest) != IS_ARRAY) &&
    1137           0 :                     zend_hash_find(Z_OBJPROP_P(this_ptr), "_login", sizeof("_login"), (void **)&login) == SUCCESS &&
    1138           0 :                     Z_TYPE_PP(login) == IS_STRING &&
    1139           0 :                     zend_hash_find(Z_OBJPROP_P(this_ptr), "_password", sizeof("_password"), (void **)&password) == SUCCESS &&
    1140           0 :                     Z_TYPE_PP(password) == IS_STRING) {
    1141             :                         char *s;
    1142           0 :                         zval *digest = NULL;
    1143             : 
    1144           0 :                         s = auth + sizeof("Digest")-1;
    1145           0 :                         while (*s != '\0') {
    1146             :                                 char *name, *val;
    1147           0 :                                 while (*s == ' ') ++s;
    1148           0 :                                 name = s;
    1149           0 :                                 while (*s != '\0' && *s != '=') ++s;
    1150           0 :                                 if (*s == '=') {
    1151           0 :                                         *s = '\0';
    1152           0 :                                         ++s;
    1153           0 :                                         if (*s == '"') {
    1154           0 :                                                 ++s;
    1155           0 :                                                 val = s;
    1156           0 :                                                 while (*s != '\0' && *s != '"') ++s;
    1157             :                                         } else {
    1158           0 :                                                 val = s;
    1159           0 :                                                 while (*s != '\0' && *s != ' ' && *s != ',') ++s;
    1160             :                                         }
    1161           0 :                                         if (*s != '\0') {
    1162           0 :                                                 if (*s != ',') {
    1163           0 :                                                         *s = '\0';
    1164           0 :                                                         ++s;
    1165           0 :                                                         while (*s != '\0' && *s != ',') ++s;
    1166           0 :                                                         if (*s != '\0') ++s;
    1167             :                                                 } else {
    1168           0 :                                                         *s = '\0';
    1169           0 :                                                         ++s;
    1170             :                                                 }
    1171             :                                         }
    1172           0 :                                         if (digest == NULL) {
    1173           0 :                                                 ALLOC_INIT_ZVAL(digest);
    1174           0 :                                                 array_init(digest);
    1175             :                                         }
    1176           0 :                                         add_assoc_string(digest, name, val ,1);
    1177             :                                 }
    1178             :                         }
    1179             : 
    1180           0 :                         if (digest != NULL) {
    1181           0 :                                 php_url *new_url  = emalloc(sizeof(php_url));
    1182             : 
    1183             :                                 Z_DELREF_P(digest);
    1184           0 :                                 add_property_zval_ex(this_ptr, "_digest", sizeof("_digest"), digest TSRMLS_CC);
    1185             : 
    1186           0 :                                 *new_url = *phpurl;
    1187           0 :                                 if (phpurl->scheme) phpurl->scheme = estrdup(phpurl->scheme);
    1188           0 :                                 if (phpurl->user) phpurl->user = estrdup(phpurl->user);
    1189           0 :                                 if (phpurl->pass) phpurl->pass = estrdup(phpurl->pass);
    1190           0 :                                 if (phpurl->host) phpurl->host = estrdup(phpurl->host);
    1191           0 :                                 if (phpurl->path) phpurl->path = estrdup(phpurl->path);
    1192           0 :                                 if (phpurl->query) phpurl->query = estrdup(phpurl->query);
    1193           0 :                                 if (phpurl->fragment) phpurl->fragment = estrdup(phpurl->fragment);
    1194           0 :                                 phpurl = new_url;
    1195             : 
    1196           0 :                                 efree(auth);
    1197           0 :                                 efree(http_headers);
    1198           0 :                                 efree(http_body);
    1199             : 
    1200           0 :                                 goto try_again;
    1201             :                         }
    1202             :                 }
    1203           0 :                 if (auth) efree(auth);
    1204             :         }
    1205           1 :         smart_str_free(&soap_headers_z);
    1206             : 
    1207             :         /* Check and see if the server even sent a xml document */
    1208           1 :         content_type = get_http_header_value(http_headers,"Content-Type: ");
    1209           1 :         if (content_type) {
    1210           1 :                 char *pos = NULL;
    1211             :                 int cmplen;
    1212           1 :                 pos = strstr(content_type,";");
    1213           1 :                 if (pos != NULL) {
    1214           1 :                         cmplen = pos - content_type;
    1215             :                 } else {
    1216           0 :                         cmplen = strlen(content_type);
    1217             :                 }
    1218           2 :                 if (strncmp(content_type, "text/xml", cmplen) == 0 ||
    1219           1 :                     strncmp(content_type, "application/soap+xml", cmplen) == 0) {
    1220           0 :                         content_type_xml = 1;
    1221             : /*
    1222             :                         if (strncmp(http_body, "<?xml", 5)) {
    1223             :                                 zval *err;
    1224             :                                 MAKE_STD_ZVAL(err);
    1225             :                                 ZVAL_STRINGL(err, http_body, http_body_size, 1);
    1226             :                                 add_soap_fault(this_ptr, "HTTP", "Didn't receive an xml document", NULL, err TSRMLS_CC);
    1227             :                                 efree(content_type);
    1228             :                                 efree(http_headers);
    1229             :                                 efree(http_body);
    1230             :                                 return FALSE;
    1231             :                         }
    1232             : */
    1233             :                 }
    1234           1 :                 efree(content_type);
    1235             :         }
    1236             : 
    1237             :         /* Decompress response */
    1238           1 :         content_encoding = get_http_header_value(http_headers,"Content-Encoding: ");
    1239           1 :         if (content_encoding) {
    1240             :                 zval func;
    1241             :                 zval retval;
    1242             :           zval param;
    1243             :                 zval *params[1];
    1244             : 
    1245           0 :                 if ((strcmp(content_encoding,"gzip") == 0 ||
    1246           0 :                      strcmp(content_encoding,"x-gzip") == 0) &&
    1247           0 :                      zend_hash_exists(EG(function_table), "gzinflate", sizeof("gzinflate"))) {
    1248           0 :                         ZVAL_STRING(&func, "gzinflate", 0);
    1249           0 :                         params[0] = &param;
    1250           0 :                         ZVAL_STRINGL(params[0], http_body+10, http_body_size-10, 0);
    1251           0 :                         INIT_PZVAL(params[0]);
    1252           0 :                 } else if (strcmp(content_encoding,"deflate") == 0 &&
    1253           0 :                            zend_hash_exists(EG(function_table), "gzuncompress", sizeof("gzuncompress"))) {
    1254           0 :                         ZVAL_STRING(&func, "gzuncompress", 0);
    1255           0 :                         params[0] = &param;
    1256           0 :                         ZVAL_STRINGL(params[0], http_body, http_body_size, 0);
    1257           0 :                         INIT_PZVAL(params[0]);
    1258             :                 } else {
    1259           0 :                         efree(content_encoding);
    1260           0 :                         efree(http_headers);
    1261           0 :                         efree(http_body);
    1262           0 :                         if (http_msg) {
    1263           0 :                                 efree(http_msg);
    1264             :                         }
    1265           0 :                         add_soap_fault(this_ptr, "HTTP", "Unknown Content-Encoding", NULL, NULL TSRMLS_CC);
    1266           0 :                         return FALSE;
    1267             :                 }
    1268           0 :                 if (call_user_function(CG(function_table), (zval**)NULL, &func, &retval, 1, params TSRMLS_CC) == SUCCESS &&
    1269           0 :                     Z_TYPE(retval) == IS_STRING) {
    1270           0 :                         efree(http_body);
    1271           0 :                         *buffer = Z_STRVAL(retval);
    1272           0 :                         *buffer_len = Z_STRLEN(retval);
    1273             :                 } else {
    1274           0 :                         efree(content_encoding);
    1275           0 :                         efree(http_headers);
    1276           0 :                         efree(http_body);
    1277           0 :                         add_soap_fault(this_ptr, "HTTP", "Can't uncompress compressed response", NULL, NULL TSRMLS_CC);
    1278           0 :                         if (http_msg) {
    1279           0 :                                 efree(http_msg);
    1280             :                         }
    1281           0 :                         return FALSE;
    1282             :                 }
    1283           0 :                 efree(content_encoding);
    1284             :         } else {
    1285           1 :                 *buffer = http_body;
    1286           1 :                 *buffer_len = http_body_size;
    1287             :         }
    1288             : 
    1289           1 :         efree(http_headers);
    1290             : 
    1291           1 :         if (http_status >= 400) {
    1292           0 :                 int error = 0;
    1293             : 
    1294           0 :                 if (*buffer_len == 0) {
    1295           0 :                         error = 1;
    1296           0 :                 } else if (*buffer_len > 0) {
    1297           0 :                         if (!content_type_xml) {
    1298           0 :                                 char *s = *buffer;
    1299             : 
    1300           0 :                                 while (*s != '\0' && *s < ' ') {
    1301           0 :                                         s++;
    1302             :                                 }
    1303           0 :                                 if (strncmp(s, "<?xml", 5)) {
    1304           0 :                                         error = 1;
    1305             :                                 }
    1306             :                         }
    1307             :                 }
    1308             : 
    1309           0 :                 if (error) {
    1310           0 :                         efree(*buffer);
    1311           0 :                         add_soap_fault(this_ptr, "HTTP", http_msg, NULL, NULL TSRMLS_CC);
    1312           0 :                         efree(http_msg);
    1313           0 :                         return FALSE;
    1314             :                 }
    1315             :         }
    1316             : 
    1317           1 :         if (http_msg) {
    1318           1 :                 efree(http_msg);
    1319             :         }
    1320             : 
    1321           1 :         return TRUE;
    1322             : }
    1323             : 
    1324           6 : static char *get_http_header_value(char *headers, char *type)
    1325             : {
    1326           6 :         char *pos, *tmp = NULL;
    1327             :         int typelen, headerslen;
    1328             : 
    1329           6 :         typelen = strlen(type);
    1330           6 :         headerslen = strlen(headers);
    1331             : 
    1332             :         /* header `titles' can be lower case, or any case combination, according
    1333             :          * to the various RFC's. */
    1334           6 :         pos = headers;
    1335             :         do {
    1336             :                 /* start of buffer or start of line */
    1337          30 :                 if (strncasecmp(pos, type, typelen) == 0) {
    1338             :                         char *eol;
    1339             : 
    1340             :                         /* match */
    1341           4 :                         tmp = pos + typelen;
    1342           4 :                         eol = strchr(tmp, '\n');
    1343           4 :                         if (eol == NULL) {
    1344           0 :                                 eol = headers + headerslen;
    1345           4 :                         } else if (eol > tmp && *(eol-1) == '\r') {
    1346           4 :                                 eol--;
    1347             :                         }
    1348           4 :                         return estrndup(tmp, eol - tmp);
    1349             :                 }
    1350             : 
    1351             :                 /* find next line */
    1352          26 :                 pos = strchr(pos, '\n');
    1353          26 :                 if (pos) {
    1354          24 :                         pos++;
    1355             :                 }
    1356             : 
    1357          26 :         } while (pos);
    1358             : 
    1359           2 :         return NULL;
    1360             : }
    1361             : 
    1362           1 : static int get_http_body(php_stream *stream, int close, char *headers,  char **response, int *out_size TSRMLS_DC)
    1363             : {
    1364           1 :         char *header, *http_buf = NULL;
    1365           1 :         int header_close = close, header_chunked = 0, header_length = 0, http_buf_size = 0;
    1366             : 
    1367           1 :         if (!close) {
    1368           0 :                 header = get_http_header_value(headers, "Connection: ");
    1369           0 :                 if (header) {
    1370           0 :                         if(!strncasecmp(header, "close", sizeof("close")-1)) header_close = 1;
    1371           0 :                         efree(header);
    1372             :                 }
    1373             :         }
    1374           1 :         header = get_http_header_value(headers, "Transfer-Encoding: ");
    1375           1 :         if (header) {
    1376           0 :                 if(!strncasecmp(header, "chunked", sizeof("chunked")-1)) header_chunked = 1;
    1377           0 :                 efree(header);
    1378             :         }
    1379           1 :         header = get_http_header_value(headers, "Content-Length: ");
    1380           1 :         if (header) {
    1381           1 :                 header_length = atoi(header);
    1382           1 :                 efree(header);
    1383           1 :                 if (!header_length && !header_chunked) {
    1384             :                         /* Empty response */
    1385           0 :                         http_buf = emalloc(1);
    1386           0 :                         http_buf[0] = '\0';
    1387           0 :                         (*response) = http_buf;
    1388           0 :                         (*out_size) = 0;
    1389           0 :                         return TRUE;
    1390             :                 }
    1391             :         }
    1392             : 
    1393           1 :         if (header_chunked) {
    1394             :                 char ch, done, headerbuf[8192];
    1395             : 
    1396           0 :                 done = FALSE;
    1397             : 
    1398           0 :                 while (!done) {
    1399           0 :                         int buf_size = 0;
    1400             : 
    1401           0 :                         php_stream_gets(stream, headerbuf, sizeof(headerbuf));
    1402           0 :                         if (sscanf(headerbuf, "%x", &buf_size) > 0 ) {
    1403           0 :                                 if (buf_size > 0) {
    1404           0 :                                         int len_size = 0;
    1405             : 
    1406           0 :                                         if (http_buf_size + buf_size + 1 < 0) {
    1407           0 :                                                 efree(http_buf);
    1408           0 :                                                 return FALSE;
    1409             :                                         }
    1410           0 :                                         http_buf = erealloc(http_buf, http_buf_size + buf_size + 1);
    1411             : 
    1412           0 :                                         while (len_size < buf_size) {
    1413           0 :                                                 int len_read = php_stream_read(stream, http_buf + http_buf_size, buf_size - len_size);
    1414           0 :                                                 if (len_read <= 0) {
    1415             :                                                         /* Error or EOF */
    1416           0 :                                                         done = TRUE;
    1417           0 :                                                   break;
    1418             :                                                 }
    1419           0 :                                                 len_size += len_read;
    1420           0 :                                                 http_buf_size += len_read;
    1421             :                                         }
    1422             : 
    1423             :                                         /* Eat up '\r' '\n' */
    1424           0 :                                         ch = php_stream_getc(stream);
    1425           0 :                                         if (ch == '\r') {
    1426           0 :                                                 ch = php_stream_getc(stream);
    1427             :                                         }
    1428           0 :                                         if (ch != '\n') {
    1429             :                                                 /* Somthing wrong in chunked encoding */
    1430           0 :                                                 if (http_buf) {
    1431           0 :                                                         efree(http_buf);
    1432             :                                                 }
    1433           0 :                                                 return FALSE;
    1434             :                                         }
    1435             :                                 }
    1436             :                         } else {
    1437             :                                 /* Somthing wrong in chunked encoding */
    1438           0 :                                 if (http_buf) {
    1439           0 :                                         efree(http_buf);
    1440             :                                 }
    1441           0 :                                 return FALSE;
    1442             :                         }
    1443           0 :                         if (buf_size == 0) {
    1444           0 :                                 done = TRUE;
    1445             :                         }
    1446             :                 }
    1447             : 
    1448             :                 /* Ignore trailer headers */
    1449             :                 while (1) {
    1450           0 :                         if (!php_stream_gets(stream, headerbuf, sizeof(headerbuf))) {
    1451           0 :                                 break;
    1452             :                         }
    1453             : 
    1454           0 :                         if ((headerbuf[0] == '\r' && headerbuf[1] == '\n') ||
    1455           0 :                             (headerbuf[0] == '\n')) {
    1456             :                                 /* empty line marks end of headers */
    1457             :                                 break;
    1458             :                         }
    1459           0 :                 }
    1460             : 
    1461           0 :                 if (http_buf == NULL) {
    1462           0 :                         http_buf = emalloc(1);
    1463             :                 }
    1464             : 
    1465           1 :         } else if (header_length) {
    1466           1 :                 if (header_length < 0 || header_length >= INT_MAX) {
    1467           0 :                         return FALSE;
    1468             :                 }
    1469           1 :                 http_buf = safe_emalloc(1, header_length, 1);
    1470           3 :                 while (http_buf_size < header_length) {
    1471           1 :                         int len_read = php_stream_read(stream, http_buf + http_buf_size, header_length - http_buf_size);
    1472           1 :                         if (len_read <= 0) {
    1473           0 :                                 break;
    1474             :                         }
    1475           1 :                         http_buf_size += len_read;
    1476             :                 }
    1477           0 :         } else if (header_close) {
    1478             :                 do {
    1479             :                         int len_read;
    1480           0 :                         http_buf = erealloc(http_buf, http_buf_size + 4096 + 1);
    1481           0 :                         len_read = php_stream_read(stream, http_buf + http_buf_size, 4096);
    1482           0 :                         if (len_read > 0) {
    1483           0 :                                 http_buf_size += len_read;
    1484             :                         }
    1485           0 :                 } while(!php_stream_eof(stream));
    1486             :         } else {
    1487           0 :                 return FALSE;
    1488             :         }
    1489             : 
    1490           1 :         http_buf[http_buf_size] = '\0';
    1491           1 :         (*response) = http_buf;
    1492           1 :         (*out_size) = http_buf_size;
    1493           1 :         return TRUE;
    1494             : }
    1495             : 
    1496           1 : static int get_http_headers(php_stream *stream, char **response, int *out_size TSRMLS_DC)
    1497             : {
    1498           1 :         int done = FALSE;
    1499           1 :         smart_str tmp_response = {0};
    1500             :         char headerbuf[8192];
    1501             : 
    1502           8 :         while (!done) {
    1503           7 :                 if (!php_stream_gets(stream, headerbuf, sizeof(headerbuf))) {
    1504           0 :                         break;
    1505             :                 }
    1506             : 
    1507          13 :                 if ((headerbuf[0] == '\r' && headerbuf[1] == '\n') ||
    1508           6 :                     (headerbuf[0] == '\n')) {
    1509             :                         /* empty line marks end of headers */
    1510           1 :                         done = TRUE;
    1511           1 :                         break;
    1512             :                 }
    1513             : 
    1514             :                 /* add header to collection */
    1515           6 :                 smart_str_appends(&tmp_response, headerbuf);
    1516             :         }
    1517           1 :         smart_str_0(&tmp_response);
    1518           1 :         (*response) = tmp_response.c;
    1519           1 :         (*out_size) = tmp_response.len;
    1520           1 :         return done;
    1521             : }
    1522             : /*
    1523             :  * Local variables:
    1524             :  * tab-width: 4
    1525             :  * c-basic-offset: 4
    1526             :  * End:
    1527             :  * vim600: sw=4 ts=4 fdm=marker
    1528             :  * vim<600: sw=4 ts=4
    1529             :  */

Generated by: LCOV version 1.10

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

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