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

Generated by: LCOV version 1.10

Generated at Mon, 27 Jul 2015 02:32:26 +0000 (27 hours ago)

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