Make stomp_set_error() to be printf like (mi+php at aldan dot algebra dot com)

v1.1
Pierrick Charron 2014-12-07 18:49:04 -05:00
parent d37e34917d
commit 059566498b
2 changed files with 73 additions and 51 deletions

74
stomp.c
View File

@ -23,7 +23,6 @@
#endif
#include "php.h"
#include "zend_exceptions.h"
#include "ext/standard/php_smart_str.h"
#include "stomp.h"
#include "php_stomp.h"
@ -68,7 +67,7 @@ stomp_t *stomp_init()
/* {{{ stomp_frame_stack_push
*/
void stomp_frame_stack_push(stomp_frame_stack_t **stack, stomp_frame_t *frame)
static void stomp_frame_stack_push(stomp_frame_stack_t **stack, stomp_frame_t *frame)
{
stomp_frame_stack_t *cell = (stomp_frame_stack_t *) emalloc(sizeof(stomp_frame_stack_t));
cell->frame = frame;
@ -86,7 +85,7 @@ void stomp_frame_stack_push(stomp_frame_stack_t **stack, stomp_frame_t *frame)
/* {{{ stomp_frame_stack_shift
*/
stomp_frame_t *stomp_frame_stack_shift(stomp_frame_stack_t **stack) {
static stomp_frame_t *stomp_frame_stack_shift(stomp_frame_stack_t **stack) {
stomp_frame_t *frame = NULL;
if (*stack) {
stomp_frame_stack_t *cell = *stack;
@ -100,7 +99,7 @@ stomp_frame_t *stomp_frame_stack_shift(stomp_frame_stack_t **stack) {
/* {{{ stomp_frame_stack_clear
*/
void stomp_frame_stack_clear(stomp_frame_stack_t **stack) {
static void stomp_frame_stack_clear(stomp_frame_stack_t **stack) {
stomp_frame_t *frame = NULL;
while ((frame = stomp_frame_stack_shift(stack))) efree(frame);
}
@ -108,8 +107,11 @@ void stomp_frame_stack_clear(stomp_frame_stack_t **stack) {
/* {{{ stomp_set_error
*/
void stomp_set_error(stomp_t *stomp, const char *error, int errnum, const char *details)
void stomp_set_error(stomp_t *stomp, const char *error, int errnum, const char *fmt, ...)
{
va_list ap;
int len;
if (stomp->error != NULL) {
efree(stomp->error);
stomp->error = NULL;
@ -122,8 +124,21 @@ void stomp_set_error(stomp_t *stomp, const char *error, int errnum, const char *
if (error != NULL) {
stomp->error = estrdup(error);
}
if (details != NULL) {
stomp->error_details = estrdup(details);
if (fmt != NULL) {
stomp->error_details = emalloc(STOMP_BUFSIZE);
if (stomp->error_details == NULL) {
return; /* Nothing else can be done */
}
va_start(ap, fmt);
/*
* Would've been better to call vasprintf(), but that
* function is missing on some platforms...
*/
len = vsnprintf(stomp->error_details, STOMP_BUFSIZE, fmt, ap);
va_end(ap);
if (len < STOMP_BUFSIZE) {
stomp->error_details = erealloc(stomp->error_details, len+1);
}
}
}
/* }}} */
@ -172,7 +187,7 @@ int stomp_connect(stomp_t *stomp, const char *host, unsigned short port TSRMLS_D
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, NULL);
stomp_set_error(stomp, error, errno, "%s", strerror(errno));
return 0;
}
int flag = 1;
@ -190,6 +205,8 @@ int stomp_connect(stomp_t *stomp, const char *host, unsigned short port TSRMLS_D
#if HAVE_STOMP_SSL
if (stomp->options.use_ssl) {
SSL_CTX *ctx = SSL_CTX_new(SSLv23_client_method());
int ret;
if (NULL == ctx) {
stomp_set_error(stomp, "failed to create the SSL context", 0, NULL);
return 0;
@ -206,8 +223,8 @@ int stomp_connect(stomp_t *stomp, const char *host, unsigned short port TSRMLS_D
SSL_set_fd(stomp->ssl_handle, stomp->fd);
if (SSL_connect(stomp->ssl_handle) <= 0) {
stomp_set_error(stomp, "SSL/TLS handshake failed", 0, NULL);
if ((ret = SSL_connect(stomp->ssl_handle)) <= 0) {
stomp_set_error(stomp, "SSL/TLS handshake failed", 0, "SSL error %d", SSL_get_error(stomp->ssl_handle, ret));
SSL_shutdown(stomp->ssl_handle);
return 0;
}
@ -216,7 +233,7 @@ int stomp_connect(stomp_t *stomp, const char *host, unsigned short port TSRMLS_D
return 1;
} else {
snprintf(error, sizeof(error), "Unable to connect to %s:%ld", stomp->host, stomp->port);
stomp_set_error(stomp, error, errno, NULL);
stomp_set_error(stomp, error, errno, "%s", strerror(errno));
return 0;
}
}
@ -303,29 +320,24 @@ int stomp_send(stomp_t *stomp, stomp_frame_t *frame TSRMLS_DC)
smart_str_appendl(&buf, "\0", sizeof("\0")-1);
if (!stomp_writable(stomp)) {
char error[1024];
snprintf(error, sizeof(error), "Unable to send data");
stomp_set_error(stomp, error, errno, NULL);
smart_str_free(&buf);
stomp_set_error(stomp, "Unable to send data", errno, "%s", strerror(errno));
return 0;
}
#ifdef HAVE_STOMP_SSL
if (stomp->options.use_ssl) {
if (-1 == SSL_write(stomp->ssl_handle, buf.c, buf.len)) {
char error[1024];
snprintf(error, sizeof(error), "Unable to send data");
stomp_set_error(stomp, error, errno, NULL);
int ret;
if (-1 == (ret = SSL_write(stomp->ssl_handle, buf.c, buf.len))) {
smart_str_free(&buf);
stomp_set_error(stomp, "Unable to send data", errno, "SSL error %d", SSL_get_error(stomp->ssl_handle, ret));
return 0;
}
} else {
#endif
if (-1 == send(stomp->fd, buf.c, buf.len, 0)) {
char error[1024];
snprintf(error, sizeof(error), "Unable to send data");
stomp_set_error(stomp, error, errno, NULL);
smart_str_free(&buf);
stomp_set_error(stomp, "Unable to send data", errno, "%s", strerror(errno));
return 0;
}
#ifdef HAVE_STOMP_SSL
@ -357,10 +369,21 @@ static int _stomp_recv(stomp_t *stomp, char *msg, const size_t length)
#endif
if (len == 0) {
TSRMLS_FETCH();
zend_throw_exception_ex(stomp_ce_exception, errno TSRMLS_CC, "Unexpected EOF while reading from socket");
#if HAVE_STOMP_SSL
if (stomp->options.use_ssl) {
stomp_set_error(stomp, "Error reading from socket", errno, "%s. (SSL in use)", strerror(errno));
} else {
#endif
stomp_set_error(stomp, "Error reading from socket", errno, "%s. (SSL not in use)", strerror(errno));
#if HAVE_STOMP_SSL
}
#endif
stomp->status = -1;
} else if (len == -1) {
stomp_set_error(stomp, "Sender closed connection unexpectedly", 0, NULL);
stomp->status = -1;
}
return len;
}
@ -638,8 +661,7 @@ int stomp_valid_receipt(stomp_t *stomp, stomp_frame_t *frame) {
&& !strcmp(receipt, receipt_id)) {
success = 1;
} else {
snprintf(error, sizeof(error), "Unexpected receipt id : %s", receipt_id);
stomp_set_error(stomp, error, 0, NULL);
stomp_set_error(stomp, error, 0, "%s", receipt_id);
}
stomp_free_frame(res);
stomp->frame_stack = stack;
@ -647,7 +669,7 @@ int stomp_valid_receipt(stomp_t *stomp, stomp_frame_t *frame) {
} 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, res->body);
stomp_set_error(stomp, error_msg, 0, "%s", res->body);
}
stomp_free_frame(res);
stomp->frame_stack = stack;

View File

@ -87,7 +87,7 @@ int stomp_send(stomp_t *connection, stomp_frame_t *frame TSRMLS_DC);
stomp_frame_t *stomp_read_frame(stomp_t *connection);
int stomp_valid_receipt(stomp_t *connection, stomp_frame_t *frame);
int stomp_select_ex(stomp_t *connection, const long int sec, const long int usec);
void stomp_set_error(stomp_t *stomp, const char *error, int errnum, const char *details);
void stomp_set_error(stomp_t *stomp, const char *error, int errnum, const char *fmt, ...) ZEND_ATTRIBUTE_PTR_FORMAT(printf, 4, 0);
void stomp_free_frame(stomp_frame_t *frame);
#define stomp_select(s) stomp_select_ex(s, s->options.read_timeout_sec, s->options.read_timeout_sec)