@@ -105,11 +105,19 @@ class HttpServer {
105105 delete socket;
106106 }
107107
108- void set_handler (std::string pattern, zend::Callable *cb) {
108+ bool set_handler (std::string pattern, zval *zfn) {
109+ auto cb = sw_callable_create (zfn);
110+ if (!cb) {
111+ return false ;
112+ }
113+ if (handlers.find (pattern) != handlers.end ()) {
114+ sw_callable_free (handlers[pattern]);
115+ }
109116 handlers[pattern] = cb;
110117 if (pattern == " /" ) {
111118 default_handler = cb;
112119 }
120+ return true ;
113121 }
114122
115123 zend::Callable *get_handler (HttpContext *ctx) {
@@ -388,13 +396,8 @@ static PHP_METHOD(swoole_http_server_coro, handle) {
388396 Z_PARAM_ZVAL (zfn)
389397 ZEND_PARSE_PARAMETERS_END ();
390398
391- auto cb = sw_callable_create (zfn);
392- if (!cb) {
393- RETURN_FALSE;
394- }
395-
396399 std::string key (pattern, pattern_len);
397- hs->set_handler (key, cb );
400+ RETURN_BOOL ( hs->set_handler (key, zfn) );
398401}
399402
400403static PHP_METHOD (swoole_http_server_coro, set) {
@@ -421,10 +424,9 @@ static PHP_METHOD(swoole_http_server_coro, start) {
421424 /* get callback fci cache */
422425 char *func_name = nullptr ;
423426 zend_fcall_info_cache fci_cache;
424- zval zcallback;
425- ZVAL_STRING (&zcallback, " onAccept" );
427+ zend::Variable zcallback (" onAccept" );
426428 if (!sw_zend_is_callable_at_frame (
427- & zcallback, ZEND_THIS, execute_data, 0 , &func_name, nullptr , &fci_cache, nullptr )) {
429+ zcallback. ptr () , ZEND_THIS, execute_data, 0 , &func_name, nullptr , &fci_cache, nullptr )) {
428430 php_swoole_fatal_error (E_CORE_ERROR, " function '%s' is not callable" , func_name);
429431 return ;
430432 }
@@ -510,7 +512,7 @@ static PHP_METHOD(swoole_http_server_coro, start) {
510512 if (conn) {
511513 zval zsocket;
512514 php_swoole_init_socket_object (&zsocket, conn);
513- long cid = PHPCoroutine::create (&fci_cache, 1 , &zsocket, & zcallback);
515+ long cid = PHPCoroutine::create (&fci_cache, 1 , &zsocket, zcallback. ptr () );
514516 zval_dtor (&zsocket);
515517 if (cid < 0 ) {
516518 goto _wait_1s;
@@ -535,8 +537,6 @@ static PHP_METHOD(swoole_http_server_coro, start) {
535537 }
536538 }
537539
538- zval_dtor (&zcallback);
539-
540540 RETURN_TRUE;
541541}
542542
0 commit comments