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: 229 884 25.9 %
Date: 2014-04-18 Functions: 7 9 77.8 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Fri, 18 Apr 2014 07:01:34 +0000 (2 days ago)

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