Skip to content

Commit

Permalink
Merge pull request #43 from youzan/hotfix-error
Browse files Browse the repository at this point in the history
add error output
  • Loading branch information
imaben authored Dec 6, 2017
2 parents c500656 + 4713d80 commit 957f29c
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 14 deletions.
5 changes: 4 additions & 1 deletion zan-extension/php_swoole.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Tianfeng Han <[email protected]> |
| Zan Group <[email protected]> |
| Zan Group <[email protected]> |
+----------------------------------------------------------------------+
*/

Expand Down Expand Up @@ -459,6 +459,9 @@ void php_swoole_get_recv_data(zval *zdata, swEventData *req, char *header, uint3
void php_swoole_onConnect(swServer *serv, swDataHead *);
int php_swoole_onReceive(swServer *serv, swEventData *req);
void php_swoole_onClose(swServer *, swDataHead *);
void php_swoole_onWorkerStart(swServer *, int worker_id);
void php_swoole_onWorkerStop(swServer *, int worker_id);
void php_swoole_onWorkerError(swServer *serv, int worker_id, pid_t worker_pid, int exit_code, int signo);

#define php_swoole_array_get_value(ht, str, v) (sw_zend_hash_find(ht, str, sizeof(str), (void **) &v) == SUCCESS && !ZVAL_IS_NULL(v))
#define php_swoole_array_get_ptr_value(ht, str, v) (sw_zend_hash_find(ht, str, strlen(str)+1, (void **) &v) == SUCCESS && !ZVAL_IS_NULL(v))
Expand Down
6 changes: 3 additions & 3 deletions zan-extension/swoole.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ const zend_function_entry zan_functions[] =
{
PHP_FE(swoole_version, NULL)
PHP_FE(swoole_cpu_num, NULL)

/*------nova_packet------*/
PHP_FE(nova_decode, arginfo_nova_decode)
PHP_FE(nova_encode, arginfo_nova_encode)
Expand All @@ -196,7 +196,7 @@ const zend_function_entry zan_functions[] =
PHP_FE(nova_get_sequence, NULL)
PHP_FE(nova_get_time, NULL)
PHP_FE(nova_get_ip, NULL)

/*------swoole_event-----*/
PHP_FE(swoole_event_add, arginfo_swoole_event_add)
PHP_FE(swoole_event_set, arginfo_swoole_event_set)
Expand Down Expand Up @@ -517,7 +517,7 @@ PHP_MINIT_FUNCTION(zan)
#ifdef SW_USE_REDIS
swoole_redis_init(module_number TSRMLS_CC);
#endif

swoole_aio_init(module_number TSRMLS_CC);
swoole_process_init(module_number TSRMLS_CC);
swoole_http_server_init(module_number TSRMLS_CC);
Expand Down
118 changes: 115 additions & 3 deletions zan-extension/swoole_http_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ swString *swoole_zlib_buffer = NULL;
#endif
swString *swoole_http_form_data_buffer;

static http_context *current_ctx = NULL;

enum http_global_flag
{
HTTP_GLOBAL_GET = 1u << 1,
Expand Down Expand Up @@ -86,6 +88,10 @@ zend_class_entry *swoole_http_response_class_entry_ptr;
static zend_class_entry swoole_http_request_ce;
zend_class_entry *swoole_http_request_class_entry_ptr;

static void (*old_error_handler)(int, const char *, const uint, const char*, va_list);
static void worker_error_handler(int error_num, const char *error_filename, const uint error_lineno, const char *format, va_list args);
static void http_onWorkerStart(swServer *serv, int worker_id);
static void http_onWorkerStop(swServer *serv, int worker_id);
static int http_onReceive(swServer *serv, swEventData *req);
static void http_onClose(swServer *serv, swDataHead *info);

Expand Down Expand Up @@ -114,6 +120,22 @@ static int http_trim_double_quote(zval **value, char **ptr);
#define http_strncasecmp(const_str, at, length) ((length >= sizeof(const_str)-1) &&\
(strncasecmp(at, ZEND_STRL(const_str)) == 0))

#ifdef va_copy
#define call_old_error_handler(error_num, error_filename, error_lineno, format, args) \
{ \
va_list copy; \
va_copy(copy, args); \
old_error_handler(error_num, error_filename, error_lineno, format, copy); \
va_end(copy); \
}
#else
#define call_old_error_handler(error_num, error_filename, error_lineno, format, args) \
{ \
old_error_handler(error_num, error_filename, error_lineno, format, args); \
}
#endif


//header filed format,like:Content-Type
static inline void http_header_key_format(char *key, int length)
{
Expand Down Expand Up @@ -916,6 +938,19 @@ static int http_request_message_complete(php_http_parser *parser)
return 0;
}

static void http_onWorkerStart(swServer *serv, int worker_id)
{
old_error_handler = zend_error_cb;
zend_error_cb = worker_error_handler;
php_swoole_onWorkerStart(serv, worker_id);
}

static void http_onWorkerStop(swServer *serv, int worker_id)
{
zend_error_cb = old_error_handler;
php_swoole_onWorkerStop(serv, worker_id);
}

static int http_onReceive(swServer *serv, swEventData *req)
{
if (swEventData_is_dgram(req->info.type))
Expand Down Expand Up @@ -1058,6 +1093,8 @@ static int http_onReceive(swServer *serv, swEventData *req)
}
}

current_ctx = ctx;

if (sw_call_user_function_ex(EG(function_table), NULL, zcallback, &retval, 2, args, 0, NULL TSRMLS_CC) == FAILURE)
{
swError("onRequest handler error");
Expand All @@ -1068,6 +1105,8 @@ static int http_onReceive(swServer *serv, swEventData *req)
zend_exception_error(EG(exception), E_ERROR TSRMLS_CC);
}

current_ctx = NULL;

//websocket user handshake
if (conn->websocket_status == WEBSOCKET_STATUS_HANDSHAKE)
{
Expand Down Expand Up @@ -1433,8 +1472,10 @@ static PHP_METHOD(swoole_http_server, start)
}
#endif

serv->onReceive = http_onReceive;
serv->onClose = http_onClose;
serv->onWorkerStart = http_onWorkerStart;
serv->onWorkerStop = http_onWorkerStop;
serv->onReceive = http_onReceive;
serv->onClose = http_onClose;

zval *zsetting = sw_zend_read_property(swoole_server_class_entry_ptr, getThis(), ZEND_STRL("setting"), 1 TSRMLS_CC);
int is_update = 0;
Expand Down Expand Up @@ -1464,7 +1505,7 @@ static PHP_METHOD(swoole_http_server, start)
if (serv->listen_list->open_websocket_protocol)
{
add_assoc_bool(zsetting, "open_websocket_protocol", 1);
}
}

if (is_update) {
zend_update_property(swoole_server_class_entry_ptr, getThis(), ZEND_STRL("setting"), zsetting TSRMLS_CC);
Expand Down Expand Up @@ -2369,3 +2410,74 @@ static PHP_METHOD(swoole_http_response, __destruct)
swoole_http_context_free(context TSRMLS_CC);
}
}

static void worker_error_handler(int error_num, const char *error_filename, const uint error_lineno, const char *format, va_list args)
{
SWOOLE_FETCH_TSRMLS;

zend_bool _old_in_compilation;
zend_execute_data *_old_current_execute_data;

_old_in_compilation = CG(in_compilation);
_old_current_execute_data = EG(current_execute_data);

if (!PG(modules_activated) || !EG(objects_store).object_buckets || !current_ctx) {
call_old_error_handler(error_num, error_filename, error_lineno, format, args);
return;
}
if (error_num == E_USER_ERROR ||
error_num == E_COMPILE_ERROR ||
error_num == E_CORE_ERROR ||
error_num == E_ERROR ||
error_num == E_PARSE) {

char buffer[1024] = {0};
size_t buffer_len;

#ifdef va_copy
va_list argcopy;
va_copy(argcopy, args);
buffer_len = vslprintf(buffer, sizeof(buffer)-1, format, argcopy);
va_end(argcopy);
#else
buffer_len = vslprintf(buffer, sizeof(buffer)-1, format, args);
#endif
if (buffer_len > sizeof(buffer) - 1 || buffer_len == (size_t)-1) {
buffer_len = sizeof(buffer) - 1;
}

current_ctx->response.status = 500;
zval *zresponse = current_ctx->response.zobject;
zval *function = NULL;
SW_MAKE_STD_ZVAL(function);
SW_ZVAL_STRING(function, "end", 1);
zval *retval = NULL;
if (!current_ctx->send_header) {
zval **args[1];
zval *buff;
SW_MAKE_STD_ZVAL(buff);
SW_ZVAL_STRINGL(buff, buffer, buffer_len, 1);
args[0] = &buff;

if (sw_call_user_function_ex(EG(function_table), &zresponse, function, &retval, 1, args, 0, NULL TSRMLS_CC) == FAILURE)
{
swError("Write error msg failure");
}
sw_zval_ptr_dtor(&buff);
} else {
if (sw_call_user_function_ex(EG(function_table), &zresponse, function, &retval, 0, NULL, 0, NULL TSRMLS_CC) == FAILURE)
{
swError("Call response->end() failure");
}
}
if (retval) sw_zval_ptr_dtor(&retval);
sw_zval_ptr_dtor(&function);
}
zend_try {
call_old_error_handler(error_num, error_filename, error_lineno, format, args);
} zend_catch {
CG(in_compilation) = _old_in_compilation;
EG(current_execute_data) = _old_current_execute_data;
} zend_end_try();
}

11 changes: 4 additions & 7 deletions zan-extension/swoole_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,6 @@ static void php_swoole_onShutdown(swServer *);

static int php_swoole_onPacket(swServer *, swEventData *);

static void php_swoole_onWorkerStart(swServer *, int worker_id);
static void php_swoole_onWorkerStop(swServer *, int worker_id);
static void php_swoole_onWorkerError(swServer *serv, int worker_id, pid_t worker_pid, int exit_code, int signo);
static void php_swoole_onUserWorkerStart(swServer *serv, swWorker *worker);

static int php_swoole_onTask(swServer *, swEventData *task);
Expand Down Expand Up @@ -728,7 +725,7 @@ static int php_swoole_onPacket(swServer *serv, swEventData *req)
return SW_OK;
}

static void php_swoole_onWorkerStart(swServer *serv, int worker_id)
void php_swoole_onWorkerStart(swServer *serv, int worker_id)
{

SWOOLE_FETCH_TSRMLS;
Expand Down Expand Up @@ -787,7 +784,7 @@ static void php_swoole_onWorkerStart(swServer *serv, int worker_id)
}
}

static void php_swoole_onWorkerStop(swServer *serv, int worker_id)
void php_swoole_onWorkerStop(swServer *serv, int worker_id)
{

SWOOLE_FETCH_TSRMLS;
Expand Down Expand Up @@ -901,7 +898,7 @@ static int php_swoole_onTask(swServer *serv, swEventData *req)
}


static void php_swoole_onWorkerError(swServer *serv, int worker_id, pid_t worker_pid, int exit_code, int signo)
void php_swoole_onWorkerError(swServer *serv, int worker_id, pid_t worker_pid, int exit_code, int signo)
{

SWOOLE_FETCH_TSRMLS;
Expand Down Expand Up @@ -1902,7 +1899,7 @@ PHP_METHOD(swoole_server, set)

sw_zend_call_method_with_1_params(&port_object, swoole_server_port_class_entry_ptr, NULL, "set", &retval, zset);
zend_update_property(swoole_server_class_entry_ptr, zobject, ZEND_STRL("setting"), zset TSRMLS_CC);

sw_zval_ptr_dtor(&zset);

RETURN_TRUE;
Expand Down

0 comments on commit 957f29c

Please sign in to comment.