2002-07-08 07:03:43 +08:00
|
|
|
#include "php_soap.h"
|
2004-01-08 17:56:49 +08:00
|
|
|
#include "ext/standard/base64.h"
|
2002-07-08 07:03:43 +08:00
|
|
|
|
2004-01-06 00:44:01 +08:00
|
|
|
static char *get_http_header_value(char *headers, char *type);
|
|
|
|
static int get_http_body(php_stream *socketd, char *headers, char **response, int *out_size TSRMLS_DC);
|
|
|
|
static int get_http_headers(php_stream *socketd,char **response, int *out_size TSRMLS_DC);
|
|
|
|
|
2004-01-07 01:12:52 +08:00
|
|
|
#define smart_str_append_const(str, const) \
|
|
|
|
smart_str_appendl(str,const,sizeof(const)-1)
|
|
|
|
|
2004-01-15 16:38:14 +08:00
|
|
|
int send_http_soap_request(zval *this_ptr, xmlDoc *doc, char *location, char *soapaction, int soap_version TSRMLS_DC)
|
2002-07-08 07:03:43 +08:00
|
|
|
{
|
|
|
|
xmlChar *buf;
|
2004-01-07 01:12:52 +08:00
|
|
|
smart_str soap_headers = {0};
|
2004-01-13 21:50:09 +08:00
|
|
|
int buf_size,err;
|
2002-07-08 07:03:43 +08:00
|
|
|
php_url *phpurl = NULL;
|
2003-03-05 22:23:56 +08:00
|
|
|
php_stream *stream;
|
2004-01-13 21:50:09 +08:00
|
|
|
zval **trace, **tmp;
|
2002-07-08 07:03:43 +08:00
|
|
|
|
2004-01-13 21:50:09 +08:00
|
|
|
if (this_ptr == NULL || Z_TYPE_P(this_ptr) != IS_OBJECT) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (zend_hash_find(Z_OBJPROP_P(this_ptr), "httpsocket", sizeof("httpsocket"), (void **)&tmp) == SUCCESS) {
|
|
|
|
php_stream_from_zval_no_verify(stream,tmp);
|
|
|
|
} else {
|
|
|
|
stream = NULL;
|
|
|
|
}
|
2002-07-08 07:03:43 +08:00
|
|
|
|
|
|
|
xmlDocDumpMemory(doc, &buf, &buf_size);
|
2004-01-10 02:22:03 +08:00
|
|
|
if (!buf) {
|
2004-01-15 18:59:24 +08:00
|
|
|
add_soap_fault(this_ptr, "Client", "Error build soap request", NULL, NULL TSRMLS_CC);
|
2004-01-06 00:44:01 +08:00
|
|
|
return FALSE;
|
|
|
|
}
|
2004-01-10 02:22:03 +08:00
|
|
|
if (zend_hash_find(Z_OBJPROP_P(this_ptr), "trace", sizeof("trace"), (void **) &trace) == SUCCESS &&
|
|
|
|
Z_LVAL_PP(trace) > 0) {
|
2002-08-07 11:03:09 +08:00
|
|
|
add_property_stringl(this_ptr, "__last_request", buf, buf_size, 1);
|
2004-01-07 01:12:52 +08:00
|
|
|
}
|
2002-07-08 07:03:43 +08:00
|
|
|
|
2004-01-09 22:11:34 +08:00
|
|
|
/* Check if keep-alive connection is still opened */
|
|
|
|
if (stream != NULL) {
|
|
|
|
struct timeval tv;
|
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_usec = 1;
|
|
|
|
php_stream_set_option(stream, PHP_STREAM_OPTION_READ_TIMEOUT, 0, &tv);
|
2004-01-10 02:22:03 +08:00
|
|
|
if (php_stream_set_option(stream, PHP_STREAM_OPTION_CHECK_LIVENESS, 0, NULL) != PHP_STREAM_OPTION_RETURN_OK) {
|
|
|
|
php_stream_close(stream);
|
2004-01-09 22:11:34 +08:00
|
|
|
zend_hash_del(Z_OBJPROP_P(this_ptr), "httpsocket", sizeof("httpsocket"));
|
2004-01-10 02:22:03 +08:00
|
|
|
stream = NULL;
|
|
|
|
} else {
|
2004-01-09 22:11:34 +08:00
|
|
|
tv.tv_sec = FG(default_socket_timeout);;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
php_stream_set_option(stream, PHP_STREAM_OPTION_READ_TIMEOUT, 0, &tv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-01-13 21:50:09 +08:00
|
|
|
if (location != NULL && location[0] != '\000') {
|
|
|
|
phpurl = php_url_parse(location);
|
|
|
|
}
|
|
|
|
if (phpurl == NULL) {
|
|
|
|
xmlFree(buf);
|
2004-01-15 18:59:24 +08:00
|
|
|
add_soap_fault(this_ptr, "Client", "Unable to parse URL", NULL, NULL TSRMLS_CC);
|
2004-01-13 21:50:09 +08:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2004-01-10 02:22:03 +08:00
|
|
|
if (!stream) {
|
2003-03-05 22:23:56 +08:00
|
|
|
int use_ssl;
|
|
|
|
use_ssl = strcmp(phpurl->scheme, "https") == 0;
|
2004-01-07 01:12:52 +08:00
|
|
|
if (use_ssl && php_stream_locate_url_wrapper("https://", NULL, STREAM_LOCATE_WRAPPERS_ONLY TSRMLS_CC) == NULL) {
|
|
|
|
xmlFree(buf);
|
|
|
|
php_url_free(phpurl);
|
2004-01-15 18:59:24 +08:00
|
|
|
add_soap_fault(this_ptr, "Client", "SSL support not available in this build", NULL, NULL TSRMLS_CC);
|
2004-01-06 00:44:01 +08:00
|
|
|
return FALSE;
|
2003-03-05 22:23:56 +08:00
|
|
|
}
|
2004-01-06 00:44:01 +08:00
|
|
|
|
2003-03-05 07:01:24 +08:00
|
|
|
if (phpurl->port == 0) {
|
2003-03-05 22:23:56 +08:00
|
|
|
phpurl->port = use_ssl ? 443 : 80;
|
2003-03-05 07:01:24 +08:00
|
|
|
}
|
2004-01-06 00:44:01 +08:00
|
|
|
|
2004-01-07 01:12:52 +08:00
|
|
|
#ifdef ZEND_ENGINE_2
|
2002-07-08 07:03:43 +08:00
|
|
|
{
|
2004-01-07 01:12:52 +08:00
|
|
|
char *res;
|
|
|
|
long reslen;
|
2003-03-05 22:23:56 +08:00
|
|
|
|
2004-01-07 01:12:52 +08:00
|
|
|
reslen = spprintf(&res, 0, "%s://%s:%d", use_ssl ? "ssl" : "tcp", phpurl->host, phpurl->port);
|
|
|
|
|
2004-01-10 02:22:03 +08:00
|
|
|
stream = php_stream_xport_create(res, reslen,
|
2004-01-07 01:12:52 +08:00
|
|
|
ENFORCE_SAFE_MODE | REPORT_ERRORS,
|
2004-01-10 02:22:03 +08:00
|
|
|
STREAM_XPORT_CLIENT | STREAM_XPORT_CONNECT,
|
|
|
|
NULL /*persistent_id*/,
|
|
|
|
NULL /*timeout*/,
|
2004-01-07 01:12:52 +08:00
|
|
|
NULL, NULL, NULL);
|
|
|
|
|
|
|
|
efree(res);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
stream = php_stream_sock_open_host(phpurl->host, (unsigned short)phpurl->port, SOCK_STREAM, NULL, NULL);
|
|
|
|
if (use_ssl) {
|
|
|
|
if (FAILURE == php_stream_sock_ssl_activate(stream, 1)) {
|
|
|
|
xmlFree(buf);
|
|
|
|
php_url_free(phpurl);
|
2004-01-15 18:59:24 +08:00
|
|
|
add_soap_fault(this_ptr, "Client", "SSL Connection attempt failed", NULL, NULL TSRMLS_CC);
|
2004-01-07 01:12:52 +08:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
2003-03-05 22:23:56 +08:00
|
|
|
#endif
|
2004-01-06 00:44:01 +08:00
|
|
|
|
2004-01-10 02:22:03 +08:00
|
|
|
if (stream) {
|
2004-01-09 22:11:34 +08:00
|
|
|
php_stream_auto_cleanup(stream);
|
2003-03-05 22:23:56 +08:00
|
|
|
add_property_resource(this_ptr, "httpsocket", php_stream_get_resource_id(stream));
|
2003-03-05 07:01:24 +08:00
|
|
|
} else {
|
2004-01-07 01:12:52 +08:00
|
|
|
xmlFree(buf);
|
|
|
|
php_url_free(phpurl);
|
2004-01-15 18:59:24 +08:00
|
|
|
add_soap_fault(this_ptr, "Client", "Could not connect to host", NULL, NULL TSRMLS_CC);
|
2004-01-06 00:44:01 +08:00
|
|
|
return FALSE;
|
2003-03-05 07:01:24 +08:00
|
|
|
}
|
2002-07-08 07:03:43 +08:00
|
|
|
}
|
|
|
|
|
2003-03-05 22:23:56 +08:00
|
|
|
if (stream) {
|
2004-01-08 17:56:49 +08:00
|
|
|
zval **cookies, **login, **password;
|
2003-03-05 22:23:56 +08:00
|
|
|
|
2004-01-07 01:12:52 +08:00
|
|
|
smart_str_append_const(&soap_headers, "POST ");
|
|
|
|
smart_str_appends(&soap_headers, phpurl->path);
|
|
|
|
smart_str_append_const(&soap_headers, " HTTP/1.1\r\n"
|
|
|
|
"Host: ");
|
|
|
|
smart_str_appends(&soap_headers, phpurl->host);
|
|
|
|
smart_str_append_const(&soap_headers, "\r\n"
|
2004-01-09 22:11:34 +08:00
|
|
|
"Connection: Keep-Alive\r\n"
|
2004-01-10 02:22:03 +08:00
|
|
|
/*
|
|
|
|
"Connection: close\r\n"
|
|
|
|
"Accept: text/html; text/xml; text/plain\r\n"
|
|
|
|
*/
|
2004-01-15 16:38:14 +08:00
|
|
|
"User-Agent: PHP SOAP 0.1\r\n");
|
|
|
|
if (soap_version == SOAP_1_2) {
|
2004-01-27 14:54:14 +08:00
|
|
|
smart_str_append_const(&soap_headers,"Content-Type: application/soap+xml; charset=\"utf-8");
|
|
|
|
if (soapaction) {
|
|
|
|
smart_str_append_const(&soap_headers,"\"; action=\"");
|
|
|
|
smart_str_appends(&soap_headers, soapaction);
|
|
|
|
}
|
|
|
|
smart_str_append_const(&soap_headers,"\"\r\n");
|
2004-01-15 16:38:14 +08:00
|
|
|
} else {
|
|
|
|
smart_str_append_const(&soap_headers,"Content-Type: text/xml; charset=\"utf-8\"\r\n");
|
2004-01-27 14:54:14 +08:00
|
|
|
smart_str_append_const(&soap_headers, "SOAPAction: \"");
|
|
|
|
smart_str_appends(&soap_headers, soapaction);
|
|
|
|
smart_str_append_const(&soap_headers, "\"\r\n");
|
2004-01-15 16:38:14 +08:00
|
|
|
}
|
|
|
|
smart_str_append_const(&soap_headers,"Content-Length: ");
|
2004-01-07 01:12:52 +08:00
|
|
|
smart_str_append_long(&soap_headers, buf_size);
|
2004-01-27 14:54:14 +08:00
|
|
|
smart_str_append_const(&soap_headers, "\r\n");
|
2004-01-06 00:44:01 +08:00
|
|
|
|
2004-01-08 17:56:49 +08:00
|
|
|
/* HTTP Authentication */
|
2004-01-10 02:22:03 +08:00
|
|
|
if (zend_hash_find(Z_OBJPROP_P(this_ptr), "_login", sizeof("_login"), (void **)&login) == SUCCESS) {
|
2004-01-08 17:56:49 +08:00
|
|
|
char* buf;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
smart_str auth = {0};
|
|
|
|
smart_str_appendl(&auth, Z_STRVAL_PP(login), Z_STRLEN_PP(login));
|
|
|
|
smart_str_appendc(&auth, ':');
|
2004-01-10 02:22:03 +08:00
|
|
|
if (zend_hash_find(Z_OBJPROP_P(this_ptr), "_password", sizeof("_password"), (void **)&password) == SUCCESS) {
|
2004-01-08 17:56:49 +08:00
|
|
|
smart_str_appendl(&auth, Z_STRVAL_PP(password), Z_STRLEN_PP(password));
|
|
|
|
}
|
2004-01-10 00:35:04 +08:00
|
|
|
smart_str_0(&auth);
|
2004-01-08 17:56:49 +08:00
|
|
|
buf = php_base64_encode(auth.c, auth.len, &len);
|
|
|
|
smart_str_append_const(&soap_headers, "Authorization: Basic ");
|
|
|
|
smart_str_appendl(&soap_headers, buf, len);
|
|
|
|
smart_str_append_const(&soap_headers, "\r\n");
|
|
|
|
efree(buf);
|
|
|
|
smart_str_free(&auth);
|
|
|
|
}
|
2002-07-08 07:03:43 +08:00
|
|
|
|
2003-01-15 04:24:40 +08:00
|
|
|
/* Send cookies along with request */
|
2004-01-10 02:22:03 +08:00
|
|
|
if (zend_hash_find(Z_OBJPROP_P(this_ptr), "_cookies", sizeof("_cookies"), (void **)&cookies) == SUCCESS) {
|
2002-07-08 07:03:43 +08:00
|
|
|
zval **data;
|
|
|
|
char *key;
|
2004-01-13 15:57:29 +08:00
|
|
|
int i, n;
|
|
|
|
|
|
|
|
n = zend_hash_num_elements(Z_ARRVAL_PP(cookies));
|
|
|
|
if (n > 0) {
|
|
|
|
zend_hash_internal_pointer_reset(Z_ARRVAL_PP(cookies));
|
|
|
|
smart_str_append_const(&soap_headers, "Cookie: ");
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
zend_hash_get_current_data(Z_ARRVAL_PP(cookies), (void **)&data);
|
|
|
|
zend_hash_get_current_key(Z_ARRVAL_PP(cookies), &key, NULL, FALSE);
|
|
|
|
|
|
|
|
smart_str_appendl(&soap_headers, key, strlen(key));
|
|
|
|
smart_str_appendc(&soap_headers, '=');
|
|
|
|
smart_str_appendl(&soap_headers, Z_STRVAL_PP(data), Z_STRLEN_PP(data));
|
|
|
|
smart_str_append_const(&soap_headers, ";");
|
|
|
|
zend_hash_move_forward(Z_ARRVAL_PP(cookies));
|
|
|
|
}
|
|
|
|
smart_str_append_const(&soap_headers, "\r\n");
|
2002-07-08 07:03:43 +08:00
|
|
|
}
|
|
|
|
}
|
2004-01-07 01:12:52 +08:00
|
|
|
smart_str_append_const(&soap_headers, "\r\n");
|
2004-01-09 22:11:34 +08:00
|
|
|
smart_str_appendl(&soap_headers, buf, buf_size);
|
2004-01-10 00:35:04 +08:00
|
|
|
smart_str_0(&soap_headers);
|
2003-03-05 22:23:56 +08:00
|
|
|
|
2004-01-09 22:11:34 +08:00
|
|
|
err = php_stream_write(stream, soap_headers.c, soap_headers.len);
|
2004-01-10 02:22:03 +08:00
|
|
|
if (err != soap_headers.len) {
|
2004-01-13 21:50:09 +08:00
|
|
|
php_url_free(phpurl);
|
2004-01-07 01:12:52 +08:00
|
|
|
xmlFree(buf);
|
|
|
|
smart_str_free(&soap_headers);
|
2004-01-06 00:44:01 +08:00
|
|
|
php_stream_close(stream);
|
|
|
|
zend_hash_del(Z_OBJPROP_P(this_ptr), "httpsocket", sizeof("httpsocket"));
|
2004-01-15 18:59:24 +08:00
|
|
|
add_soap_fault(this_ptr, "Client", "Failed Sending HTTP SOAP request", NULL, NULL TSRMLS_CC);
|
2004-01-06 00:44:01 +08:00
|
|
|
return FALSE;
|
|
|
|
}
|
2004-01-07 01:12:52 +08:00
|
|
|
smart_str_free(&soap_headers);
|
2002-07-08 07:03:43 +08:00
|
|
|
|
|
|
|
}
|
2004-01-13 21:50:09 +08:00
|
|
|
php_url_free(phpurl);
|
2002-07-08 07:03:43 +08:00
|
|
|
xmlFree(buf);
|
2004-01-06 00:44:01 +08:00
|
|
|
return TRUE;
|
2002-07-08 07:03:43 +08:00
|
|
|
}
|
|
|
|
|
2004-01-06 00:44:01 +08:00
|
|
|
int get_http_soap_response(zval *this_ptr, char **buffer, int *buffer_len TSRMLS_DC)
|
2002-07-08 07:03:43 +08:00
|
|
|
{
|
2004-01-27 23:49:34 +08:00
|
|
|
char *http_headers, *http_body, *content_type, *http_version, *cookie_itt;
|
2002-07-08 07:03:43 +08:00
|
|
|
int http_header_size, http_body_size, http_close;
|
2003-03-05 22:23:56 +08:00
|
|
|
php_stream *stream;
|
2004-01-13 21:50:09 +08:00
|
|
|
zval **trace, **tmp;
|
2004-01-09 22:11:34 +08:00
|
|
|
char* connection;
|
|
|
|
int http_1_1 = 0;
|
2004-01-27 23:49:34 +08:00
|
|
|
int http_status = 0;
|
2002-07-08 07:03:43 +08:00
|
|
|
|
2004-01-13 21:50:09 +08:00
|
|
|
if (zend_hash_find(Z_OBJPROP_P(this_ptr), "httpsocket", sizeof("httpsocket"), (void **)&tmp) == SUCCESS) {
|
|
|
|
php_stream_from_zval_no_verify(stream,tmp);
|
|
|
|
} else {
|
|
|
|
stream = NULL;
|
|
|
|
}
|
|
|
|
if (stream == NULL) {
|
|
|
|
return FALSE;
|
2002-07-08 07:03:43 +08:00
|
|
|
}
|
|
|
|
|
2004-01-10 02:22:03 +08:00
|
|
|
if (!get_http_headers(stream, &http_headers, &http_header_size TSRMLS_CC)) {
|
2004-01-06 00:44:01 +08:00
|
|
|
php_stream_close(stream);
|
|
|
|
zend_hash_del(Z_OBJPROP_P(this_ptr), "httpsocket", sizeof("httpsocket"));
|
2004-01-15 18:59:24 +08:00
|
|
|
add_soap_fault(this_ptr, "Client", "Error Fetching http headers", NULL, NULL TSRMLS_CC);
|
2004-01-06 00:44:01 +08:00
|
|
|
return FALSE;
|
|
|
|
}
|
2002-07-08 07:03:43 +08:00
|
|
|
|
2003-01-15 04:24:40 +08:00
|
|
|
/* Check to see what HTTP status was sent */
|
2002-07-08 07:03:43 +08:00
|
|
|
http_version = get_http_header_value(http_headers,"HTTP/");
|
2004-01-10 02:22:03 +08:00
|
|
|
if (http_version) {
|
2002-07-08 07:03:43 +08:00
|
|
|
char *tmp;
|
|
|
|
|
|
|
|
tmp = strstr(http_version," ");
|
|
|
|
|
2004-01-10 02:22:03 +08:00
|
|
|
if (tmp != NULL) {
|
2002-07-08 07:03:43 +08:00
|
|
|
tmp++;
|
2004-01-27 23:49:34 +08:00
|
|
|
http_status = atoi(tmp);
|
2002-07-08 07:03:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Try and process any respsone that is xml might contain fault code
|
|
|
|
|
|
|
|
Maybe try and test for some of the 300's 400's specfics but not
|
|
|
|
right now.
|
|
|
|
|
2004-01-27 23:49:34 +08:00
|
|
|
if (http_status >= 200 && http_status < 300) {
|
|
|
|
} else if (http_status >= 300 && http_status < 400) {
|
|
|
|
add_soap_fault(this_ptr, "Client", "HTTTP redirection is not supported", NULL, NULL TSRMLS_CC);
|
|
|
|
} else if (http_status == 400) {
|
|
|
|
add_soap_fault(this_ptr, "Client", "Bad Request", NULL, NULL TSRMLS_CC);
|
|
|
|
} else if (http_status == 401) {
|
|
|
|
add_soap_fault(this_ptr, "Client", "Unauthorized Request", NULL, NULL TSRMLS_CC);
|
|
|
|
} else if (http_status == 405) {
|
|
|
|
add_soap_fault(this_ptr, "Client", "Method not allowed", NULL, NULL TSRMLS_CC);
|
|
|
|
} else if (http_status == 415) {
|
|
|
|
add_soap_fault(this_ptr, "Client", "Unsupported Media Type", NULL, NULL TSRMLS_CC);
|
|
|
|
} else if (http_status >= 400 && http_status < 500) {
|
|
|
|
add_soap_fault(this_ptr, "Client", "Client Error", NULL, NULL TSRMLS_CC);
|
|
|
|
} else if (http_status == 500) {
|
|
|
|
add_soap_fault(this_ptr, "Server", "Internal Server Error", NULL, NULL TSRMLS_CC);
|
|
|
|
} else if (http_status >= 500 && http_status < 600) {
|
|
|
|
add_soap_fault(this_ptr, "Server", "Server Error", NULL, NULL TSRMLS_CC);
|
|
|
|
} else {
|
|
|
|
add_soap_fault(this_ptr, "Client", "Unsupported HTTP status code", NULL, NULL TSRMLS_CC);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2004-01-28 00:07:58 +08:00
|
|
|
/* Try and get headers again */
|
|
|
|
if (http_status == 100) {
|
2004-01-14 01:43:09 +08:00
|
|
|
efree(http_headers);
|
2004-01-10 02:22:03 +08:00
|
|
|
if (!get_http_headers(stream, &http_headers, &http_header_size TSRMLS_CC)) {
|
2004-01-06 00:44:01 +08:00
|
|
|
php_stream_close(stream);
|
|
|
|
zend_hash_del(Z_OBJPROP_P(this_ptr), "httpsocket", sizeof("httpsocket"));
|
2004-01-15 18:59:24 +08:00
|
|
|
add_soap_fault(this_ptr, "Client", "Error Fetching http headers", NULL, NULL TSRMLS_CC);
|
2004-01-06 00:44:01 +08:00
|
|
|
return FALSE;
|
|
|
|
}
|
2002-07-08 07:03:43 +08:00
|
|
|
}
|
2004-01-10 02:22:03 +08:00
|
|
|
|
2004-01-09 22:11:34 +08:00
|
|
|
if (strncmp(http_version,"1.1", 3)) {
|
|
|
|
http_1_1 = 1;
|
|
|
|
}
|
2002-07-08 07:03:43 +08:00
|
|
|
efree(http_version);
|
|
|
|
}
|
2004-01-06 00:44:01 +08:00
|
|
|
|
2004-01-10 02:22:03 +08:00
|
|
|
if (!get_http_body(stream, http_headers, &http_body, &http_body_size TSRMLS_CC)) {
|
2004-01-06 00:44:01 +08:00
|
|
|
php_stream_close(stream);
|
|
|
|
zend_hash_del(Z_OBJPROP_P(this_ptr), "httpsocket", sizeof("httpsocket"));
|
2004-01-15 18:59:24 +08:00
|
|
|
add_soap_fault(this_ptr, "Client", "Error Fetching http body", NULL, NULL TSRMLS_CC);
|
2004-01-06 00:44:01 +08:00
|
|
|
return FALSE;
|
|
|
|
}
|
2002-07-08 07:03:43 +08:00
|
|
|
|
2004-01-10 02:22:03 +08:00
|
|
|
if (zend_hash_find(Z_OBJPROP_P(this_ptr), "trace", sizeof("trace"), (void **) &trace) == SUCCESS &&
|
|
|
|
Z_LVAL_PP(trace) > 0) {
|
2002-08-07 11:03:09 +08:00
|
|
|
add_property_stringl(this_ptr, "__last_response", http_body, http_body_size, 1);
|
2004-01-07 01:12:52 +08:00
|
|
|
}
|
2002-07-08 07:03:43 +08:00
|
|
|
|
2003-01-15 04:24:40 +08:00
|
|
|
/* See if the server requested a close */
|
2002-07-08 07:03:43 +08:00
|
|
|
http_close = TRUE;
|
|
|
|
connection = get_http_header_value(http_headers,"Connection: ");
|
2004-01-10 02:22:03 +08:00
|
|
|
if (connection) {
|
2004-01-13 17:31:50 +08:00
|
|
|
if (strncasecmp(connection, "Keep-Alive", sizeof("Keep-Alive")-1) == 0) {
|
2002-07-08 07:03:43 +08:00
|
|
|
http_close = FALSE;
|
2004-01-09 22:11:34 +08:00
|
|
|
}
|
2002-07-08 07:03:43 +08:00
|
|
|
efree(connection);
|
2004-01-28 00:19:22 +08:00
|
|
|
/*
|
2004-01-09 22:11:34 +08:00
|
|
|
} else if (http_1_1) {
|
|
|
|
http_close = FALSE;
|
2004-01-28 00:19:22 +08:00
|
|
|
*/
|
2002-07-08 07:03:43 +08:00
|
|
|
}
|
|
|
|
|
2003-03-05 22:23:56 +08:00
|
|
|
if (http_close) {
|
2002-07-08 07:03:43 +08:00
|
|
|
php_stream_close(stream);
|
2003-03-05 22:23:56 +08:00
|
|
|
zend_hash_del(Z_OBJPROP_P(this_ptr), "httpsocket", sizeof("httpsocket"));
|
2002-07-08 07:03:43 +08:00
|
|
|
}
|
|
|
|
|
2003-01-15 04:24:40 +08:00
|
|
|
/* Check and see if the server even sent a xml document */
|
2002-07-08 07:03:43 +08:00
|
|
|
content_type = get_http_header_value(http_headers,"Content-Type: ");
|
2004-01-10 02:22:03 +08:00
|
|
|
if (content_type) {
|
2002-07-08 07:03:43 +08:00
|
|
|
char *pos = NULL;
|
|
|
|
int cmplen;
|
|
|
|
pos = strstr(content_type,";");
|
2004-01-10 02:22:03 +08:00
|
|
|
if (pos != NULL) {
|
2002-07-08 07:03:43 +08:00
|
|
|
cmplen = pos - content_type;
|
2004-01-07 01:12:52 +08:00
|
|
|
} else {
|
2002-07-08 07:03:43 +08:00
|
|
|
cmplen = strlen(content_type);
|
2004-01-07 01:12:52 +08:00
|
|
|
}
|
2004-01-13 17:31:50 +08:00
|
|
|
if (strncmp(content_type, "text/xml", cmplen) == 0 ||
|
|
|
|
strncmp(content_type, "application/soap+xml", cmplen == 0)) {
|
2004-01-13 18:27:13 +08:00
|
|
|
/*
|
2004-01-10 02:22:03 +08:00
|
|
|
if (strncmp(http_body, "<?xml", 5)) {
|
2002-07-08 07:03:43 +08:00
|
|
|
zval *err;
|
|
|
|
MAKE_STD_ZVAL(err);
|
|
|
|
ZVAL_STRINGL(err, http_body, http_body_size, 1);
|
2004-01-15 18:59:24 +08:00
|
|
|
add_soap_fault(this_ptr, "Client", "Didn't recieve an xml document", NULL, err TSRMLS_CC);
|
2002-07-08 07:03:43 +08:00
|
|
|
efree(content_type);
|
2004-01-07 01:12:52 +08:00
|
|
|
efree(http_headers);
|
|
|
|
efree(http_body);
|
|
|
|
return FALSE;
|
2002-07-08 07:03:43 +08:00
|
|
|
}
|
2004-01-13 18:27:13 +08:00
|
|
|
*/
|
2002-07-08 07:03:43 +08:00
|
|
|
}
|
|
|
|
efree(content_type);
|
|
|
|
}
|
|
|
|
|
2003-01-15 04:24:40 +08:00
|
|
|
/* Grab and send back every cookie */
|
2004-01-06 00:44:01 +08:00
|
|
|
|
2003-01-15 04:24:40 +08:00
|
|
|
/* Not going to worry about Path: because
|
|
|
|
we shouldn't be changing urls so path dont
|
|
|
|
matter too much
|
|
|
|
*/
|
2002-07-08 07:03:43 +08:00
|
|
|
cookie_itt = strstr(http_headers,"Set-Cookie: ");
|
2004-01-10 02:22:03 +08:00
|
|
|
while (cookie_itt) {
|
2002-07-08 07:03:43 +08:00
|
|
|
char *end_pos, *cookie;
|
|
|
|
char *eqpos, *sempos;
|
|
|
|
smart_str name = {0}, value = {0};
|
|
|
|
zval **cookies, *z_cookie;
|
|
|
|
|
2004-01-10 02:22:03 +08:00
|
|
|
if (zend_hash_find(Z_OBJPROP_P(this_ptr), "_cookies", sizeof("_cookies"), (void **)&cookies) == FAILURE) {
|
2002-07-08 07:03:43 +08:00
|
|
|
zval *tmp_cookies;
|
|
|
|
MAKE_STD_ZVAL(tmp_cookies);
|
|
|
|
array_init(tmp_cookies);
|
|
|
|
zend_hash_update(Z_OBJPROP_P(this_ptr), "_cookies", sizeof("_cookies"), &tmp_cookies, sizeof(zval *), (void **)&cookies);
|
|
|
|
}
|
|
|
|
|
|
|
|
end_pos = strstr(cookie_itt,"\r\n");
|
|
|
|
cookie = get_http_header_value(cookie_itt,"Set-Cookie: ");
|
|
|
|
|
|
|
|
eqpos = strstr(cookie, "=");
|
|
|
|
sempos = strstr(cookie, ";");
|
2004-01-13 15:57:29 +08:00
|
|
|
if (eqpos != NULL && (sempos == NULL || sempos > eqpos)) {
|
|
|
|
int cookie_len;
|
2002-07-08 07:03:43 +08:00
|
|
|
|
2004-01-13 15:57:29 +08:00
|
|
|
if (sempos != NULL) {
|
|
|
|
cookie_len = sempos-(eqpos+1);
|
2004-01-28 21:31:28 +08:00
|
|
|
} else {
|
2004-01-13 15:57:29 +08:00
|
|
|
cookie_len = strlen(cookie)-(eqpos-cookie)-1;
|
|
|
|
}
|
2002-07-08 07:03:43 +08:00
|
|
|
|
2004-01-13 15:57:29 +08:00
|
|
|
smart_str_appendl(&name, cookie, eqpos - cookie);
|
|
|
|
smart_str_0(&name);
|
2002-07-08 07:03:43 +08:00
|
|
|
|
2004-01-13 15:57:29 +08:00
|
|
|
smart_str_appendl(&value, eqpos + 1, cookie_len);
|
|
|
|
smart_str_0(&value);
|
2002-07-08 07:03:43 +08:00
|
|
|
|
2004-01-13 15:57:29 +08:00
|
|
|
MAKE_STD_ZVAL(z_cookie);
|
|
|
|
ZVAL_STRINGL(z_cookie, value.c, value.len, 1);
|
|
|
|
|
|
|
|
zend_hash_update(Z_ARRVAL_PP(cookies), name.c, name.len + 1, &z_cookie, sizeof(zval *), NULL);
|
|
|
|
}
|
2002-07-08 07:03:43 +08:00
|
|
|
|
|
|
|
cookie_itt = strstr(cookie_itt + sizeof("Set-Cookie: "), "Set-Cookie: ");
|
|
|
|
|
|
|
|
smart_str_free(&value);
|
|
|
|
smart_str_free(&name);
|
|
|
|
efree(cookie);
|
|
|
|
}
|
|
|
|
|
|
|
|
*buffer = http_body;
|
|
|
|
*buffer_len = http_body_size;
|
|
|
|
efree(http_headers);
|
2004-01-06 00:44:01 +08:00
|
|
|
return TRUE;
|
2002-07-08 07:03:43 +08:00
|
|
|
}
|
|
|
|
|
2004-01-06 00:44:01 +08:00
|
|
|
static char *get_http_header_value(char *headers, char *type)
|
2002-07-08 07:03:43 +08:00
|
|
|
{
|
2003-03-05 22:23:56 +08:00
|
|
|
char *pos, *tmp = NULL;
|
|
|
|
int typelen, headerslen;
|
|
|
|
|
|
|
|
typelen = strlen(type);
|
|
|
|
headerslen = strlen(headers);
|
|
|
|
|
|
|
|
/* header `titles' can be lower case, or any case combination, according
|
|
|
|
* to the various RFC's. */
|
|
|
|
pos = headers;
|
|
|
|
do {
|
|
|
|
/* start of buffer or start of line */
|
|
|
|
if (strncasecmp(pos, type, typelen) == 0) {
|
|
|
|
char *eol;
|
2004-01-06 00:44:01 +08:00
|
|
|
|
2003-03-05 22:23:56 +08:00
|
|
|
/* match */
|
|
|
|
tmp = pos + typelen;
|
|
|
|
eol = strstr(tmp, "\r\n");
|
|
|
|
if (eol == NULL) {
|
|
|
|
eol = headers + headerslen;
|
|
|
|
}
|
|
|
|
return estrndup(tmp, eol - tmp);
|
|
|
|
}
|
2004-01-06 00:44:01 +08:00
|
|
|
|
2003-03-05 22:23:56 +08:00
|
|
|
/* find next line */
|
|
|
|
pos = strstr(pos, "\r\n");
|
2004-01-10 02:22:03 +08:00
|
|
|
if (pos) {
|
2003-03-05 22:23:56 +08:00
|
|
|
pos += 2;
|
2004-01-10 02:22:03 +08:00
|
|
|
}
|
2002-07-08 07:03:43 +08:00
|
|
|
|
2003-03-05 22:23:56 +08:00
|
|
|
} while (pos);
|
|
|
|
|
|
|
|
return NULL;
|
2002-07-08 07:03:43 +08:00
|
|
|
}
|
|
|
|
|
2004-01-06 00:44:01 +08:00
|
|
|
static int get_http_body(php_stream *stream, char *headers, char **response, int *out_size TSRMLS_DC)
|
2002-07-08 07:03:43 +08:00
|
|
|
{
|
2003-03-05 22:23:56 +08:00
|
|
|
char *trans_enc, *content_length, *http_buf = NULL;
|
2002-07-08 07:03:43 +08:00
|
|
|
int http_buf_size = 0;
|
|
|
|
|
|
|
|
trans_enc = get_http_header_value(headers, "Transfer-Encoding: ");
|
|
|
|
content_length = get_http_header_value(headers, "Content-Length: ");
|
|
|
|
|
2003-03-05 22:23:56 +08:00
|
|
|
if (trans_enc && !strcmp(trans_enc, "chunked")) {
|
2004-01-06 00:44:01 +08:00
|
|
|
int buf_size = 0, len_size;
|
2002-07-08 07:03:43 +08:00
|
|
|
char done, chunk_size[10];
|
|
|
|
|
|
|
|
done = FALSE;
|
2003-03-05 22:23:56 +08:00
|
|
|
http_buf = NULL;
|
|
|
|
|
|
|
|
while (!done) {
|
|
|
|
php_stream_gets(stream, chunk_size, sizeof(chunk_size));
|
|
|
|
|
|
|
|
if (sscanf(chunk_size, "%x", &buf_size) != -1) {
|
|
|
|
http_buf = erealloc(http_buf, http_buf_size + buf_size + 1);
|
2002-07-08 07:03:43 +08:00
|
|
|
len_size = 0;
|
2004-01-06 00:44:01 +08:00
|
|
|
|
2003-03-05 22:23:56 +08:00
|
|
|
while (http_buf_size < buf_size) {
|
|
|
|
len_size += php_stream_read(stream, http_buf + http_buf_size, buf_size - len_size);
|
2002-07-08 07:03:43 +08:00
|
|
|
http_buf_size += len_size;
|
|
|
|
}
|
2004-01-06 00:44:01 +08:00
|
|
|
|
2003-01-15 04:24:40 +08:00
|
|
|
/* Eat up '\r' '\n' */
|
2003-03-05 22:23:56 +08:00
|
|
|
php_stream_getc(stream);php_stream_getc(stream);
|
2002-07-08 07:03:43 +08:00
|
|
|
}
|
2003-03-05 22:23:56 +08:00
|
|
|
if (buf_size == 0) {
|
2002-07-08 07:03:43 +08:00
|
|
|
done = TRUE;
|
2003-03-05 22:23:56 +08:00
|
|
|
}
|
2002-07-08 07:03:43 +08:00
|
|
|
}
|
|
|
|
efree(trans_enc);
|
2003-03-05 22:23:56 +08:00
|
|
|
|
|
|
|
if (http_buf == NULL) {
|
|
|
|
http_buf = estrndup("", 1);
|
|
|
|
http_buf_size = 1;
|
|
|
|
} else {
|
|
|
|
http_buf[http_buf_size] = '\0';
|
|
|
|
}
|
2004-01-06 00:44:01 +08:00
|
|
|
|
2003-03-05 22:23:56 +08:00
|
|
|
} else if (content_length) {
|
2002-07-08 07:03:43 +08:00
|
|
|
int size;
|
|
|
|
size = atoi(content_length);
|
|
|
|
http_buf = emalloc(size + 1);
|
|
|
|
|
2004-01-10 02:22:03 +08:00
|
|
|
while (http_buf_size < size) {
|
2003-03-05 22:23:56 +08:00
|
|
|
http_buf_size += php_stream_read(stream, http_buf + http_buf_size, size - http_buf_size);
|
|
|
|
}
|
2002-07-08 07:03:43 +08:00
|
|
|
http_buf[size] = '\0';
|
|
|
|
efree(content_length);
|
2003-03-05 22:23:56 +08:00
|
|
|
} else {
|
2004-01-09 22:11:34 +08:00
|
|
|
php_error(E_ERROR, "Don't know how to read http body, No Content-Length or chunked data");
|
2004-01-06 00:44:01 +08:00
|
|
|
return FALSE;
|
2002-07-08 07:03:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
(*response) = http_buf;
|
|
|
|
(*out_size) = http_buf_size;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2004-01-06 00:44:01 +08:00
|
|
|
static int get_http_headers(php_stream *stream, char **response, int *out_size TSRMLS_DC)
|
2002-07-08 07:03:43 +08:00
|
|
|
{
|
2003-03-05 22:23:56 +08:00
|
|
|
int done = FALSE;
|
2002-07-08 07:03:43 +08:00
|
|
|
smart_str tmp_response = {0};
|
2003-03-05 22:23:56 +08:00
|
|
|
char headerbuf[8192];
|
2002-07-08 07:03:43 +08:00
|
|
|
|
2004-01-10 02:22:03 +08:00
|
|
|
while (!done) {
|
2003-03-05 22:23:56 +08:00
|
|
|
if (!php_stream_gets(stream, headerbuf, sizeof(headerbuf))) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(headerbuf, "\r\n") == 0) {
|
|
|
|
/* empty line marks end of headers */
|
|
|
|
done = TRUE;
|
|
|
|
break;
|
2003-03-05 07:01:24 +08:00
|
|
|
}
|
2003-03-05 22:23:56 +08:00
|
|
|
|
|
|
|
/* add header to collection */
|
|
|
|
smart_str_appends(&tmp_response, headerbuf);
|
2002-07-08 07:03:43 +08:00
|
|
|
}
|
2003-03-05 22:23:56 +08:00
|
|
|
smart_str_0(&tmp_response);
|
2002-07-08 07:03:43 +08:00
|
|
|
(*response) = tmp_response.c;
|
|
|
|
(*out_size) = tmp_response.len;
|
2003-03-05 22:23:56 +08:00
|
|
|
return done;
|
2002-07-08 07:03:43 +08:00
|
|
|
}
|