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: 208 769 27.0 %
Date: 2014-10-22 Functions: 7 9 77.8 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Wed, 22 Oct 2014 07:24:57 +0000 (2 hours ago)

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