Code cleaning
parent
7c7d9aaaf6
commit
db3abd3904
1290
php_stomp.c
1290
php_stomp.c
File diff suppressed because it is too large
Load Diff
22
php_stomp.h
22
php_stomp.h
|
@ -24,8 +24,8 @@
|
|||
#include "stomp.h"
|
||||
|
||||
typedef struct _stomp_object {
|
||||
zend_object std;
|
||||
stomp_t *stomp;
|
||||
zend_object std;
|
||||
stomp_t *stomp;
|
||||
} stomp_object_t;
|
||||
|
||||
#define PHP_STOMP_EXTNAME "Stomp"
|
||||
|
@ -88,16 +88,16 @@ PHP_FUNCTION(stomp_get_read_timeout);
|
|||
PHP_METHOD(stompframe, __construct);
|
||||
|
||||
ZEND_BEGIN_MODULE_GLOBALS(stomp)
|
||||
/* INI */
|
||||
char *default_broker;
|
||||
long read_timeout_sec;
|
||||
long read_timeout_usec;
|
||||
long connection_timeout_sec;
|
||||
long connection_timeout_usec;
|
||||
/* INI */
|
||||
char *default_broker;
|
||||
long read_timeout_sec;
|
||||
long read_timeout_usec;
|
||||
long connection_timeout_sec;
|
||||
long connection_timeout_usec;
|
||||
|
||||
/* Others */
|
||||
long error_no;
|
||||
char *error_msg;
|
||||
/* Others */
|
||||
long error_no;
|
||||
char *error_msg;
|
||||
ZEND_END_MODULE_GLOBALS(stomp)
|
||||
|
||||
#ifdef ZTS
|
||||
|
|
650
stomp.c
650
stomp.c
|
@ -37,29 +37,29 @@ extern stomp_ce_exception;
|
|||
*/
|
||||
stomp_t *stomp_new(const char *host, unsigned short port, long read_timeout_sec, long read_timeout_usec TSRMLS_DC)
|
||||
{
|
||||
/* Memory allocation for the stomp */
|
||||
stomp_t *stomp = (stomp_t *) emalloc(sizeof(stomp_t));
|
||||
memset(stomp, 0, sizeof(*stomp));
|
||||
/* Memory allocation for the stomp */
|
||||
stomp_t *stomp = (stomp_t *) emalloc(sizeof(stomp_t));
|
||||
memset(stomp, 0, sizeof(*stomp));
|
||||
|
||||
/* Define all values */
|
||||
stomp->host = (char *) emalloc(strlen(host) + 1);
|
||||
memcpy(stomp->host, host, strlen(host));
|
||||
stomp->host[strlen(host)] = '\0';
|
||||
/* Define all values */
|
||||
stomp->host = (char *) emalloc(strlen(host) + 1);
|
||||
memcpy(stomp->host, host, strlen(host));
|
||||
stomp->host[strlen(host)] = '\0';
|
||||
|
||||
stomp->port = port;
|
||||
stomp->status = 0;
|
||||
stomp->error = NULL;
|
||||
stomp->errnum = 0;
|
||||
stomp->read_timeout_sec = read_timeout_sec;
|
||||
stomp->read_timeout_usec = read_timeout_usec;
|
||||
stomp->session = NULL;
|
||||
stomp->port = port;
|
||||
stomp->status = 0;
|
||||
stomp->error = NULL;
|
||||
stomp->errnum = 0;
|
||||
stomp->read_timeout_sec = read_timeout_sec;
|
||||
stomp->read_timeout_usec = read_timeout_usec;
|
||||
stomp->session = NULL;
|
||||
|
||||
#if HAVE_STOMP_SSL
|
||||
stomp->ssl_handle = NULL;
|
||||
stomp->use_ssl = 0;
|
||||
stomp->ssl_handle = NULL;
|
||||
stomp->use_ssl = 0;
|
||||
#endif
|
||||
|
||||
return stomp;
|
||||
return stomp;
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
|
@ -67,13 +67,13 @@ stomp_t *stomp_new(const char *host, unsigned short port, long read_timeout_sec,
|
|||
*/
|
||||
void stomp_set_error(stomp_t *stomp, const char *error, int errnum)
|
||||
{
|
||||
if (error != NULL) {
|
||||
if (stomp->error != NULL) {
|
||||
efree(stomp->error);
|
||||
}
|
||||
stomp->error = estrdup(error);
|
||||
stomp->errnum = errnum;
|
||||
}
|
||||
if (error != NULL) {
|
||||
if (stomp->error != NULL) {
|
||||
efree(stomp->error);
|
||||
}
|
||||
stomp->error = estrdup(error);
|
||||
stomp->errnum = errnum;
|
||||
}
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
|
@ -81,68 +81,68 @@ void stomp_set_error(stomp_t *stomp, const char *error, int errnum)
|
|||
*/
|
||||
int stomp_connect(stomp_t *stomp TSRMLS_DC)
|
||||
{
|
||||
char error[1024];
|
||||
socklen_t size;
|
||||
struct timeval tv;
|
||||
fd_set rfds;
|
||||
char error[1024];
|
||||
socklen_t size;
|
||||
struct timeval tv;
|
||||
fd_set rfds;
|
||||
|
||||
tv.tv_sec = STOMP_G(connection_timeout_sec);
|
||||
tv.tv_usec = STOMP_G(connection_timeout_usec);
|
||||
tv.tv_sec = STOMP_G(connection_timeout_sec);
|
||||
tv.tv_usec = STOMP_G(connection_timeout_usec);
|
||||
|
||||
stomp->fd = php_network_connect_socket_to_host(stomp->host, stomp->port, SOCK_STREAM, 0, &tv, NULL, NULL, NULL, 0 TSRMLS_CC);
|
||||
if (stomp->fd == -1) {
|
||||
snprintf(error, sizeof(error), "Unable to connect to %s:%ld", stomp->host, stomp->port);
|
||||
stomp_set_error(stomp, error, errno);
|
||||
return 0;
|
||||
}
|
||||
stomp->fd = php_network_connect_socket_to_host(stomp->host, stomp->port, SOCK_STREAM, 0, &tv, NULL, NULL, NULL, 0 TSRMLS_CC);
|
||||
if (stomp->fd == -1) {
|
||||
snprintf(error, sizeof(error), "Unable to connect to %s:%ld", stomp->host, stomp->port);
|
||||
stomp_set_error(stomp, error, errno);
|
||||
return 0;
|
||||
}
|
||||
|
||||
size = sizeof(stomp->localaddr);
|
||||
memset(&stomp->localaddr, 0, size);
|
||||
if (getsockname(stomp->fd, (struct sockaddr*) &stomp->localaddr, &size) == -1) {
|
||||
snprintf(error, sizeof(error), "getsockname failed: %s (%d)", strerror(errno), errno);
|
||||
stomp_set_error(stomp, error, errno);
|
||||
return 0;
|
||||
}
|
||||
size = sizeof(stomp->localaddr);
|
||||
memset(&stomp->localaddr, 0, size);
|
||||
if (getsockname(stomp->fd, (struct sockaddr*) &stomp->localaddr, &size) == -1) {
|
||||
snprintf(error, sizeof(error), "getsockname failed: %s (%d)", strerror(errno), errno);
|
||||
stomp_set_error(stomp, error, errno);
|
||||
return 0;
|
||||
}
|
||||
|
||||
tv.tv_sec = 0;
|
||||
tv.tv_usec = 0;
|
||||
tv.tv_sec = 0;
|
||||
tv.tv_usec = 0;
|
||||
|
||||
FD_ZERO(&rfds);
|
||||
FD_SET(stomp->fd, &rfds);
|
||||
FD_ZERO(&rfds);
|
||||
FD_SET(stomp->fd, &rfds);
|
||||
|
||||
if (select(stomp->fd + 1, NULL, &rfds, NULL, &tv) > 0) {
|
||||
if (select(stomp->fd + 1, NULL, &rfds, NULL, &tv) > 0) {
|
||||
#if HAVE_STOMP_SSL
|
||||
if (stomp->use_ssl) {
|
||||
SSL_CTX *ctx = SSL_CTX_new(SSLv23_client_method());
|
||||
if (NULL == ctx) {
|
||||
stomp_set_error(stomp, "failed to create the SSL context", 0);
|
||||
return 0;
|
||||
}
|
||||
if (stomp->use_ssl) {
|
||||
SSL_CTX *ctx = SSL_CTX_new(SSLv23_client_method());
|
||||
if (NULL == ctx) {
|
||||
stomp_set_error(stomp, "failed to create the SSL context", 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
SSL_CTX_set_options(ctx, SSL_OP_ALL);
|
||||
SSL_CTX_set_options(ctx, SSL_OP_ALL);
|
||||
|
||||
stomp->ssl_handle = SSL_new(ctx);
|
||||
if (stomp->ssl_handle == NULL) {
|
||||
stomp_set_error(stomp, "failed to create the SSL handle", 0);
|
||||
SSL_CTX_free(ctx);
|
||||
return 0;
|
||||
}
|
||||
|
||||
SSL_set_fd(stomp->ssl_handle, stomp->fd);
|
||||
stomp->ssl_handle = SSL_new(ctx);
|
||||
if (stomp->ssl_handle == NULL) {
|
||||
stomp_set_error(stomp, "failed to create the SSL handle", 0);
|
||||
SSL_CTX_free(ctx);
|
||||
return 0;
|
||||
}
|
||||
|
||||
SSL_set_fd(stomp->ssl_handle, stomp->fd);
|
||||
|
||||
if (SSL_connect(stomp->ssl_handle) <= 0) {
|
||||
stomp_set_error(stomp, "SSL/TLS handshake failed", 0);
|
||||
SSL_shutdown(stomp->ssl_handle);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
if (SSL_connect(stomp->ssl_handle) <= 0) {
|
||||
stomp_set_error(stomp, "SSL/TLS handshake failed", 0);
|
||||
SSL_shutdown(stomp->ssl_handle);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
return 1;
|
||||
} else {
|
||||
snprintf(error, sizeof(error), "Unable to connect to %s:%ld", stomp->host, stomp->port);
|
||||
stomp_set_error(stomp, error, errno);
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
} else {
|
||||
snprintf(error, sizeof(error), "Unable to connect to %s:%ld", stomp->host, stomp->port);
|
||||
stomp_set_error(stomp, error, errno);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
|
@ -150,29 +150,29 @@ int stomp_connect(stomp_t *stomp TSRMLS_DC)
|
|||
*/
|
||||
void stomp_close(stomp_t *stomp TSRMLS_DC)
|
||||
{
|
||||
if (NULL == stomp) {
|
||||
return;
|
||||
}
|
||||
if (NULL == stomp) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (stomp->fd != -1) {
|
||||
if (stomp->fd != -1) {
|
||||
#if HAVE_STOMP_SSL
|
||||
if(stomp->ssl_handle) {
|
||||
SSL_shutdown(stomp->ssl_handle);
|
||||
}
|
||||
if(stomp->ssl_handle) {
|
||||
SSL_shutdown(stomp->ssl_handle);
|
||||
}
|
||||
#endif
|
||||
closesocket(stomp->fd);
|
||||
}
|
||||
if (stomp->host) {
|
||||
efree(stomp->host);
|
||||
}
|
||||
if (stomp->session) {
|
||||
efree(stomp->session);
|
||||
}
|
||||
if (stomp->error) {
|
||||
efree(stomp->error);
|
||||
}
|
||||
closesocket(stomp->fd);
|
||||
}
|
||||
if (stomp->host) {
|
||||
efree(stomp->host);
|
||||
}
|
||||
if (stomp->session) {
|
||||
efree(stomp->session);
|
||||
}
|
||||
if (stomp->error) {
|
||||
efree(stomp->error);
|
||||
}
|
||||
|
||||
efree(stomp);
|
||||
efree(stomp);
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
|
@ -180,70 +180,70 @@ void stomp_close(stomp_t *stomp TSRMLS_DC)
|
|||
*/
|
||||
int stomp_send(stomp_t *stomp, stomp_frame_t *frame TSRMLS_DC)
|
||||
{
|
||||
smart_str buf = {0};
|
||||
smart_str buf = {0};
|
||||
|
||||
/* Command */
|
||||
smart_str_appends(&buf, frame->command);
|
||||
smart_str_appendc(&buf, '\n');
|
||||
/* Command */
|
||||
smart_str_appends(&buf, frame->command);
|
||||
smart_str_appendc(&buf, '\n');
|
||||
|
||||
/* Headers */
|
||||
if (frame->headers) {
|
||||
/* Headers */
|
||||
if (frame->headers) {
|
||||
|
||||
char *key;
|
||||
ulong pos;
|
||||
zend_hash_internal_pointer_reset(frame->headers);
|
||||
char *key;
|
||||
ulong pos;
|
||||
zend_hash_internal_pointer_reset(frame->headers);
|
||||
|
||||
while (zend_hash_get_current_key(frame->headers, &key, &pos, 0) == HASH_KEY_IS_STRING) {
|
||||
char *value = NULL;
|
||||
while (zend_hash_get_current_key(frame->headers, &key, &pos, 0) == HASH_KEY_IS_STRING) {
|
||||
char *value = NULL;
|
||||
|
||||
smart_str_appends(&buf, key);
|
||||
smart_str_appendc(&buf, ':');
|
||||
smart_str_appends(&buf, key);
|
||||
smart_str_appendc(&buf, ':');
|
||||
|
||||
if (zend_hash_get_current_data(frame->headers, (void **)&value) == SUCCESS) {
|
||||
smart_str_appends(&buf, value);
|
||||
}
|
||||
if (zend_hash_get_current_data(frame->headers, (void **)&value) == SUCCESS) {
|
||||
smart_str_appends(&buf, value);
|
||||
}
|
||||
|
||||
smart_str_appendc(&buf, '\n');
|
||||
smart_str_appendc(&buf, '\n');
|
||||
|
||||
zend_hash_move_forward(frame->headers);
|
||||
}
|
||||
}
|
||||
zend_hash_move_forward(frame->headers);
|
||||
}
|
||||
}
|
||||
|
||||
if (frame->body_length > 0) {
|
||||
smart_str_appends(&buf, "content-length: ");
|
||||
smart_str_append_long(&buf, frame->body_length);
|
||||
smart_str_appendc(&buf, '\n');
|
||||
}
|
||||
if (frame->body_length > 0) {
|
||||
smart_str_appends(&buf, "content-length: ");
|
||||
smart_str_append_long(&buf, frame->body_length);
|
||||
smart_str_appendc(&buf, '\n');
|
||||
}
|
||||
|
||||
smart_str_appendc(&buf, '\n');
|
||||
smart_str_appendc(&buf, '\n');
|
||||
|
||||
if (frame->body > 0) {
|
||||
smart_str_appends(&buf, frame->body);
|
||||
}
|
||||
if (frame->body > 0) {
|
||||
smart_str_appends(&buf, frame->body);
|
||||
}
|
||||
|
||||
#ifdef HAVE_STOMP_SSL
|
||||
if (stomp->use_ssl) {
|
||||
if (-1 == SSL_write(stomp->ssl_handle, buf.c, buf.len) || -1 == SSL_write(stomp->ssl_handle, "\0\n", 2)) {
|
||||
char error[1024];
|
||||
snprintf(error, sizeof(error), "Unable to send data");
|
||||
stomp_set_error(stomp, error, errno);
|
||||
return 0;
|
||||
}
|
||||
} else {
|
||||
if (stomp->use_ssl) {
|
||||
if (-1 == SSL_write(stomp->ssl_handle, buf.c, buf.len) || -1 == SSL_write(stomp->ssl_handle, "\0\n", 2)) {
|
||||
char error[1024];
|
||||
snprintf(error, sizeof(error), "Unable to send data");
|
||||
stomp_set_error(stomp, error, errno);
|
||||
return 0;
|
||||
}
|
||||
} else {
|
||||
#endif
|
||||
if (-1 == send(stomp->fd, buf.c, buf.len, 0) || -1 == send(stomp->fd, "\0\n", 2, 0)) {
|
||||
char error[1024];
|
||||
snprintf(error, sizeof(error), "Unable to send data");
|
||||
stomp_set_error(stomp, error, errno);
|
||||
return 0;
|
||||
}
|
||||
if (-1 == send(stomp->fd, buf.c, buf.len, 0) || -1 == send(stomp->fd, "\0\n", 2, 0)) {
|
||||
char error[1024];
|
||||
snprintf(error, sizeof(error), "Unable to send data");
|
||||
stomp_set_error(stomp, error, errno);
|
||||
return 0;
|
||||
}
|
||||
#ifdef HAVE_STOMP_SSL
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
smart_str_free(&buf);
|
||||
smart_str_free(&buf);
|
||||
|
||||
return 1;
|
||||
return 1;
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
|
@ -253,21 +253,21 @@ int stomp_recv(stomp_t *stomp, char *msg, size_t length)
|
|||
{
|
||||
int len;
|
||||
#if HAVE_STOMP_SSL
|
||||
if(stomp->use_ssl) {
|
||||
len = SSL_read(stomp->ssl_handle, msg, length);
|
||||
} else {
|
||||
if(stomp->use_ssl) {
|
||||
len = SSL_read(stomp->ssl_handle, msg, length);
|
||||
} else {
|
||||
#endif
|
||||
len = recv(stomp->fd, msg, length, 0);
|
||||
len = recv(stomp->fd, msg, length, 0);
|
||||
#if HAVE_STOMP_SSL
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
if (len == 0) {
|
||||
if (len == 0) {
|
||||
TSRMLS_FETCH();
|
||||
zend_throw_exception_ex(stomp_ce_exception, errno TSRMLS_CC, "Unexpected EOF while reading from socket");
|
||||
stomp->status = -1;
|
||||
}
|
||||
return len;
|
||||
stomp->status = -1;
|
||||
}
|
||||
return len;
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
|
@ -275,53 +275,53 @@ int stomp_recv(stomp_t *stomp, char *msg, size_t length)
|
|||
*/
|
||||
static int stomp_read_buffer(stomp_t *stomp, char **data)
|
||||
{
|
||||
int rc = 0;
|
||||
size_t i = 0;
|
||||
size_t bufsize = STOMP_BUFSIZE + 1;
|
||||
char *buffer = (char *) emalloc(STOMP_BUFSIZE + 1);
|
||||
int rc = 0;
|
||||
size_t i = 0;
|
||||
size_t bufsize = STOMP_BUFSIZE + 1;
|
||||
char *buffer = (char *) emalloc(STOMP_BUFSIZE + 1);
|
||||
|
||||
while (1) {
|
||||
while (1) {
|
||||
|
||||
size_t length = 1;
|
||||
rc = stomp_recv(stomp, buffer + i, length);
|
||||
if (rc < 1) {
|
||||
efree(buffer);
|
||||
return -1;
|
||||
}
|
||||
size_t length = 1;
|
||||
rc = stomp_recv(stomp, buffer + i, length);
|
||||
if (rc < 1) {
|
||||
efree(buffer);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (1 == length) {
|
||||
i++;
|
||||
if (1 == length) {
|
||||
i++;
|
||||
|
||||
if (buffer[i-1] == 0) {
|
||||
char endline[1];
|
||||
if (1 != stomp_recv(stomp, endline, 1) && '\n' != endline[0]) {
|
||||
efree(buffer);
|
||||
return 0;
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (buffer[i-1] == 0) {
|
||||
char endline[1];
|
||||
if (1 != stomp_recv(stomp, endline, 1) && '\n' != endline[0]) {
|
||||
efree(buffer);
|
||||
return 0;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (i >= bufsize) {
|
||||
buffer = (char *) erealloc(buffer, bufsize + STOMP_BUFSIZE);
|
||||
bufsize += STOMP_BUFSIZE;
|
||||
}
|
||||
if (i >= bufsize) {
|
||||
buffer = (char *) erealloc(buffer, bufsize + STOMP_BUFSIZE);
|
||||
bufsize += STOMP_BUFSIZE;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (i > 1) {
|
||||
*data = (char *) emalloc(i);
|
||||
if (NULL == *data) {
|
||||
efree(buffer);
|
||||
return -1;
|
||||
}
|
||||
if (i > 1) {
|
||||
*data = (char *) emalloc(i);
|
||||
if (NULL == *data) {
|
||||
efree(buffer);
|
||||
return -1;
|
||||
}
|
||||
|
||||
memcpy(*data, buffer, i);
|
||||
}
|
||||
memcpy(*data, buffer, i);
|
||||
}
|
||||
|
||||
efree(buffer);
|
||||
efree(buffer);
|
||||
|
||||
return i-1;
|
||||
return i-1;
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
|
@ -329,52 +329,52 @@ static int stomp_read_buffer(stomp_t *stomp, char **data)
|
|||
*/
|
||||
static int stomp_read_line(stomp_t *stomp, char **data)
|
||||
{
|
||||
int rc = 0;
|
||||
size_t i = 0;
|
||||
size_t bufsize = STOMP_BUFSIZE + 1;
|
||||
char *buffer = (char *) emalloc(STOMP_BUFSIZE + 1);
|
||||
int rc = 0;
|
||||
size_t i = 0;
|
||||
size_t bufsize = STOMP_BUFSIZE + 1;
|
||||
char *buffer = (char *) emalloc(STOMP_BUFSIZE + 1);
|
||||
|
||||
while (1) {
|
||||
while (1) {
|
||||
|
||||
size_t length = 1;
|
||||
rc = stomp_recv(stomp, buffer + i, length);
|
||||
if (rc < 1) {
|
||||
efree(buffer);
|
||||
return -1;
|
||||
}
|
||||
size_t length = 1;
|
||||
rc = stomp_recv(stomp, buffer + i, length);
|
||||
if (rc < 1) {
|
||||
efree(buffer);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (1 == length) {
|
||||
i++;
|
||||
if (1 == length) {
|
||||
i++;
|
||||
|
||||
if (buffer[i-1] == '\n') {
|
||||
buffer[i-1] = 0;
|
||||
break;
|
||||
} else if (buffer[i-1] == 0) {
|
||||
efree(buffer);
|
||||
return 0;
|
||||
}
|
||||
if (buffer[i-1] == '\n') {
|
||||
buffer[i-1] = 0;
|
||||
break;
|
||||
} else if (buffer[i-1] == 0) {
|
||||
efree(buffer);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (i >= bufsize) {
|
||||
buffer = (char *) erealloc(buffer, bufsize + STOMP_BUFSIZE);
|
||||
bufsize += STOMP_BUFSIZE;
|
||||
}
|
||||
}
|
||||
if (i >= bufsize) {
|
||||
buffer = (char *) erealloc(buffer, bufsize + STOMP_BUFSIZE);
|
||||
bufsize += STOMP_BUFSIZE;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if (i > 1) {
|
||||
*data = (char *) emalloc(i);
|
||||
if (NULL == *data) {
|
||||
efree(buffer);
|
||||
return -1;
|
||||
}
|
||||
if (i > 1) {
|
||||
*data = (char *) emalloc(i);
|
||||
if (NULL == *data) {
|
||||
efree(buffer);
|
||||
return -1;
|
||||
}
|
||||
|
||||
memcpy(*data, buffer, i);
|
||||
}
|
||||
memcpy(*data, buffer, i);
|
||||
}
|
||||
|
||||
efree(buffer);
|
||||
efree(buffer);
|
||||
|
||||
return i-1;
|
||||
return i-1;
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
|
@ -382,19 +382,19 @@ static int stomp_read_line(stomp_t *stomp, char **data)
|
|||
*/
|
||||
void frame_destroy(stomp_frame_t *frame)
|
||||
{
|
||||
if (frame) {
|
||||
if (frame->command) {
|
||||
efree(frame->command);
|
||||
}
|
||||
if (frame->body) {
|
||||
efree(frame->body);
|
||||
}
|
||||
if (frame->headers) {
|
||||
zend_hash_destroy(frame->headers);
|
||||
efree(frame->headers);
|
||||
}
|
||||
efree(frame);
|
||||
}
|
||||
if (frame) {
|
||||
if (frame->command) {
|
||||
efree(frame->command);
|
||||
}
|
||||
if (frame->body) {
|
||||
efree(frame->body);
|
||||
}
|
||||
if (frame->headers) {
|
||||
zend_hash_destroy(frame->headers);
|
||||
efree(frame->headers);
|
||||
}
|
||||
efree(frame);
|
||||
}
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
|
@ -402,110 +402,110 @@ void frame_destroy(stomp_frame_t *frame)
|
|||
*/
|
||||
stomp_frame_t *stomp_read_frame(stomp_t *stomp)
|
||||
{
|
||||
stomp_frame_t *f = NULL;
|
||||
char *cmd = NULL, *length_str = NULL;
|
||||
int length = 0;
|
||||
stomp_frame_t *f = NULL;
|
||||
char *cmd = NULL, *length_str = NULL;
|
||||
int length = 0;
|
||||
|
||||
INIT_STOMP_FRAME(f);
|
||||
INIT_STOMP_FRAME(f);
|
||||
|
||||
if (NULL == f) {
|
||||
return NULL;
|
||||
}
|
||||
if (NULL == f) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Parse the command */
|
||||
length = stomp_read_line(stomp, &cmd);
|
||||
if (length < 1) {
|
||||
RETURN_READ_FRAME_FAIL;
|
||||
}
|
||||
/* Parse the command */
|
||||
length = stomp_read_line(stomp, &cmd);
|
||||
if (length < 1) {
|
||||
RETURN_READ_FRAME_FAIL;
|
||||
}
|
||||
|
||||
f->command = cmd;
|
||||
f->command_length = length;
|
||||
f->command = cmd;
|
||||
f->command_length = length;
|
||||
|
||||
/* Parse the header */
|
||||
while (1) {
|
||||
char *p = NULL;
|
||||
length = stomp_read_line(stomp, &p);
|
||||
|
||||
if (length < 0) {
|
||||
RETURN_READ_FRAME_FAIL;
|
||||
}
|
||||
/* Parse the header */
|
||||
while (1) {
|
||||
char *p = NULL;
|
||||
length = stomp_read_line(stomp, &p);
|
||||
|
||||
if (length < 0) {
|
||||
RETURN_READ_FRAME_FAIL;
|
||||
}
|
||||
|
||||
if (0 == length) {
|
||||
break;
|
||||
} else {
|
||||
char *p2 = NULL;
|
||||
char *key;
|
||||
char *value;
|
||||
if (0 == length) {
|
||||
break;
|
||||
} else {
|
||||
char *p2 = NULL;
|
||||
char *key;
|
||||
char *value;
|
||||
|
||||
p2 = strstr(p,":");
|
||||
|
||||
if (p2 == NULL) {
|
||||
efree(p);
|
||||
RETURN_READ_FRAME_FAIL;
|
||||
}
|
||||
p2 = strstr(p,":");
|
||||
|
||||
if (p2 == NULL) {
|
||||
efree(p);
|
||||
RETURN_READ_FRAME_FAIL;
|
||||
}
|
||||
|
||||
/* Null terminate the key */
|
||||
*p2=0;
|
||||
key = p;
|
||||
/* Null terminate the key */
|
||||
*p2=0;
|
||||
key = p;
|
||||
|
||||
/* The rest if the value. */
|
||||
value = p2+1;
|
||||
/* The rest if the value. */
|
||||
value = p2+1;
|
||||
|
||||
/* Insert key/value into hash table. */
|
||||
zend_hash_add(f->headers, key, strlen(key) + 1, value, strlen(value) + 1, NULL);
|
||||
efree(p);
|
||||
}
|
||||
}
|
||||
/* Insert key/value into hash table. */
|
||||
zend_hash_add(f->headers, key, strlen(key) + 1, value, strlen(value) + 1, NULL);
|
||||
efree(p);
|
||||
}
|
||||
}
|
||||
|
||||
/* Check for the content length */
|
||||
if (zend_hash_find(f->headers, "content-length", strlen("content-length"), (void **)&length_str) == SUCCESS) {
|
||||
char endbuffer[2];
|
||||
length = 2;
|
||||
/* Check for the content length */
|
||||
if (zend_hash_find(f->headers, "content-length", strlen("content-length"), (void **)&length_str) == SUCCESS) {
|
||||
char endbuffer[2];
|
||||
length = 2;
|
||||
|
||||
f->body_length = atoi(length_str);
|
||||
f->body = (char *) emalloc(f->body_length);
|
||||
f->body_length = atoi(length_str);
|
||||
f->body = (char *) emalloc(f->body_length);
|
||||
|
||||
if (-1 == stomp_recv(stomp, f->body, f->body_length)) {
|
||||
RETURN_READ_FRAME_FAIL;
|
||||
}
|
||||
if (-1 == stomp_recv(stomp, f->body, f->body_length)) {
|
||||
RETURN_READ_FRAME_FAIL;
|
||||
}
|
||||
|
||||
if (length != stomp_recv(stomp, endbuffer, length) || endbuffer[0] != '\0' || endbuffer[1] != '\n') {
|
||||
RETURN_READ_FRAME_FAIL;
|
||||
}
|
||||
} else {
|
||||
f->body_length = stomp_read_buffer(stomp, &f->body);
|
||||
}
|
||||
if (length != stomp_recv(stomp, endbuffer, length) || endbuffer[0] != '\0' || endbuffer[1] != '\n') {
|
||||
RETURN_READ_FRAME_FAIL;
|
||||
}
|
||||
} else {
|
||||
f->body_length = stomp_read_buffer(stomp, &f->body);
|
||||
}
|
||||
|
||||
return f;
|
||||
return f;
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
/* {{{ stomp_valid_receipt
|
||||
*/
|
||||
int stomp_valid_receipt(stomp_t *stomp, stomp_frame_t *frame) {
|
||||
int success = 1;
|
||||
char *receipt = NULL;
|
||||
if (zend_hash_find(frame->headers, "receipt", sizeof("receipt"), (void **)&receipt) == SUCCESS) {
|
||||
stomp_frame_t *res = stomp_read_frame(stomp);
|
||||
success = 0;
|
||||
if (res) {
|
||||
if (0 == strncmp("RECEIPT", res->command, sizeof("RECEIPT") - 1)) {
|
||||
char *receipt_id = NULL;
|
||||
if (zend_hash_find(res->headers, "receipt-id", sizeof("receipt-id"), (void **)&receipt_id) == SUCCESS
|
||||
&& strlen(receipt) == strlen(receipt_id)
|
||||
&& !strcmp(receipt, receipt_id)) {
|
||||
success = 1;
|
||||
}
|
||||
} else if (0 == strncmp("ERROR", res->command, sizeof("ERROR") - 1)) {
|
||||
char *error_msg = NULL;
|
||||
if (zend_hash_find(res->headers, "message", sizeof("message"), (void **)&error_msg) == SUCCESS) {
|
||||
stomp_set_error(stomp, error_msg, 0);
|
||||
}
|
||||
}
|
||||
frame_destroy(res);
|
||||
}
|
||||
}
|
||||
return success;
|
||||
int success = 1;
|
||||
char *receipt = NULL;
|
||||
if (zend_hash_find(frame->headers, "receipt", sizeof("receipt"), (void **)&receipt) == SUCCESS) {
|
||||
stomp_frame_t *res = stomp_read_frame(stomp);
|
||||
success = 0;
|
||||
if (res) {
|
||||
if (0 == strncmp("RECEIPT", res->command, sizeof("RECEIPT") - 1)) {
|
||||
char *receipt_id = NULL;
|
||||
if (zend_hash_find(res->headers, "receipt-id", sizeof("receipt-id"), (void **)&receipt_id) == SUCCESS
|
||||
&& strlen(receipt) == strlen(receipt_id)
|
||||
&& !strcmp(receipt, receipt_id)) {
|
||||
success = 1;
|
||||
}
|
||||
} else if (0 == strncmp("ERROR", res->command, sizeof("ERROR") - 1)) {
|
||||
char *error_msg = NULL;
|
||||
if (zend_hash_find(res->headers, "message", sizeof("message"), (void **)&error_msg) == SUCCESS) {
|
||||
stomp_set_error(stomp, error_msg, 0);
|
||||
}
|
||||
}
|
||||
frame_destroy(res);
|
||||
}
|
||||
}
|
||||
return success;
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
|
@ -513,15 +513,15 @@ int stomp_valid_receipt(stomp_t *stomp, stomp_frame_t *frame) {
|
|||
*/
|
||||
int stomp_select(stomp_t *stomp)
|
||||
{
|
||||
struct timeval tv;
|
||||
fd_set rfds;
|
||||
struct timeval tv;
|
||||
fd_set rfds;
|
||||
|
||||
tv.tv_sec = stomp->read_timeout_sec;
|
||||
tv.tv_usec = stomp->read_timeout_usec;
|
||||
tv.tv_sec = stomp->read_timeout_sec;
|
||||
tv.tv_usec = stomp->read_timeout_usec;
|
||||
|
||||
FD_ZERO(&rfds);
|
||||
FD_SET(stomp->fd, &rfds);
|
||||
FD_ZERO(&rfds);
|
||||
FD_SET(stomp->fd, &rfds);
|
||||
|
||||
return select(stomp->fd + 1, &rfds, NULL, NULL, &tv);
|
||||
return select(stomp->fd + 1, &rfds, NULL, NULL, &tv);
|
||||
}
|
||||
/* }}} */
|
||||
|
|
42
stomp.h
42
stomp.h
|
@ -30,34 +30,34 @@
|
|||
#define STOMP_BUFSIZE 4096
|
||||
|
||||
#define INIT_STOMP_FRAME(f) \
|
||||
f = (stomp_frame_t *) emalloc(sizeof(stomp_frame_t)); \
|
||||
f->command = NULL; f->body = NULL; \
|
||||
ALLOC_HASHTABLE(f->headers); \
|
||||
zend_hash_init(f->headers, 0, NULL, NULL, 0);
|
||||
f = (stomp_frame_t *) emalloc(sizeof(stomp_frame_t)); \
|
||||
f->command = NULL; f->body = NULL; \
|
||||
ALLOC_HASHTABLE(f->headers); \
|
||||
zend_hash_init(f->headers, 0, NULL, NULL, 0);
|
||||
|
||||
typedef struct _stomp {
|
||||
php_socket_t fd;
|
||||
php_sockaddr_storage localaddr;
|
||||
char *host;
|
||||
unsigned short port;
|
||||
int status;
|
||||
char *error;
|
||||
int errnum;
|
||||
long read_timeout_sec;
|
||||
long read_timeout_usec;
|
||||
char *session;
|
||||
php_socket_t fd;
|
||||
php_sockaddr_storage localaddr;
|
||||
char *host;
|
||||
unsigned short port;
|
||||
int status;
|
||||
char *error;
|
||||
int errnum;
|
||||
long read_timeout_sec;
|
||||
long read_timeout_usec;
|
||||
char *session;
|
||||
#if HAVE_STOMP_SSL
|
||||
SSL *ssl_handle;
|
||||
int use_ssl;
|
||||
SSL *ssl_handle;
|
||||
int use_ssl;
|
||||
#endif
|
||||
} stomp_t;
|
||||
|
||||
typedef struct _stomp_frame {
|
||||
char *command;
|
||||
int command_length;
|
||||
HashTable *headers;
|
||||
char *body;
|
||||
int body_length;
|
||||
char *command;
|
||||
int command_length;
|
||||
HashTable *headers;
|
||||
char *body;
|
||||
int body_length;
|
||||
} stomp_frame_t;
|
||||
|
||||
stomp_t *stomp_new(const char *host, unsigned short port, long read_timeout_sec, long read_timeout_usec TSRMLS_DC);
|
||||
|
|
Loading…
Reference in New Issue