src/head/wr_server.c in webroar-0.3.1 vs src/head/wr_server.c in webroar-0.4.0

- old
+ new

@@ -16,16 +16,18 @@ * You should have received a copy of the GNU General Public License * along with WebROaR. If not, see <http://www.gnu.org/licenses/>. */ #include <wr_request.h> +extern config_t *Config; + static struct ev_loop *loop; //Event loop struct ev_idle idle_watcher; //Ideal watcher /************ Private functions **************************/ /** Create Server */ -wr_svr_t* wr_svr_new(struct ev_loop* loop, wr_conf_t* conf) { +wr_svr_t* wr_svr_new(struct ev_loop* loop) { LOG_FUNCTION wr_svr_t* server = wr_malloc(wr_svr_t); if(!server) { LOG_ERROR(WARN, "Server object allocation failed. Returning ..."); @@ -33,69 +35,76 @@ } //Get ebb server object ebb_server_init(&(server->ebb_svr),loop); - if(conf->server->flag&WR_SVR_SSL_SUPPORT) { #ifdef HAVE_GNUTLS - + if(Config->Server.flag & SERVER_SSL_SUPPORT) { //Initialize ebb server for SSL support ebb_server_init(&(server->secure_ebb_svr),loop); // Add GnuTLS support - if(ebb_server_set_secure(&(server->secure_ebb_svr), conf->server->certificate.str, conf->server->key.str) < 0) { + if(ebb_server_set_secure(&(server->secure_ebb_svr), Config->Server.SSL.certificate.str, Config->Server.SSL.key.str) < 0) { ebb_server_unlisten(&(server->secure_ebb_svr)); //free(server); //return NULL; LOG_ERROR(SEVERE,"ebb_server_set_secure() failed. Server can not run on SSL."); } else { server->secure_ebb_svr.data = server; server->secure_ebb_svr.new_connection = wr_new_conn_cb; } - + } #endif - } server->ebb_svr.data = server; server->ebb_svr.new_connection = wr_new_conn_cb; //Create Server Control object server->ctl = wr_svr_ctl_new(); if(!server->ctl) { ebb_server_unlisten(&(server->ebb_svr)); +#ifdef HAVE_GNUTLS + if(Config->Server.flag & SERVER_SSL_SUPPORT) { + ebb_server_unlisten(&(server->secure_ebb_svr)); + } +#endif free(server); LOG_ERROR(WARN, "%s() control object allocation failed. Returning ...",__FUNCTION__); return NULL; } - server->conf = conf; server->apps = NULL; server->default_app = NULL; server->resolver = wr_req_resolver_new(); if(server->resolver == NULL) { ebb_server_unlisten(&(server->ebb_svr)); +#ifdef HAVE_GNUTLS + if(Config->Server.flag & SERVER_SSL_SUPPORT) { + ebb_server_unlisten(&(server->secure_ebb_svr)); + } +#endif free(server); LOG_ERROR(WARN, "Resolver object allocation failed. Returning ..."); return NULL; } return server; } /** Attach ideal watcher with event loop */ -static inline void attach_idle_watcher() { +void attach_idle_watcher() { if(!ev_is_active(&idle_watcher)) { ev_idle_start (loop, &idle_watcher); } } /** Detach Ideal watcher from event loop*/ -static inline void detach_idle_watcher() { +void detach_idle_watcher() { ev_idle_stop(loop, &idle_watcher); } /** Callback function for Ideal watcher */ -static inline void idle_cb (struct ev_loop *loop, struct ev_idle *w, int revents) { +void idle_cb (struct ev_loop *loop, struct ev_idle *w, int revents) { /*if(clients_in_use_p()) { rb_thread_schedule(); } else if(!rb_thread_alone()) {*/ /* if you have another long running thread running besides the ones used * for the webapp's requests you will run into performance problems in @@ -114,48 +123,50 @@ /***************************************************** * Server API Definition * *****************************************************/ /** Starts listening for requests */ -int wr_svr_init(wr_svr_t** server, wr_conf_t *conf) { +int wr_svr_init(wr_svr_t** server) { //TODO: attach idle watcher //ev_idle_init (&idle_watcher, idle_cb); //attach_idle_watcher(); //Create and initialize Server object loop = ev_default_loop (0); - *server = wr_svr_new(loop, conf); + *server = wr_svr_new(loop); if(*server == NULL) { LOG_ERROR(SEVERE,"Server is NULL"); return -1; } - (*server)->conf = conf; - if(conf->server->flag&WR_SVR_SSL_SUPPORT) { #ifdef HAVE_GNUTLS - LOG_DEBUG(DEBUG,"SSL port = %d", conf->server->ssl_port); - if(ebb_server_listen_on_port(&(*server)->secure_ebb_svr, conf->server->ssl_port) < 0) { - LOG_ERROR(SEVERE,"ebb_server_listen_on_port(): failed. Port number = %d",conf->server->ssl_port); - printf("Port %d is already in use.\n", conf->server->ssl_port); + if(Config->Server.flag & SERVER_SSL_SUPPORT) { + LOG_DEBUG(DEBUG,"SSL port = %d", Config->Server.SSL.port); + if(ebb_server_listen_on_port(&(*server)->secure_ebb_svr, Config->Server.SSL.port) < 0) { + LOG_ERROR(SEVERE,"ebb_server_listen_on_port(): failed. Port number = %d",Config->Server.SSL.port); + printf("Port %d is already in use.\n", Config->Server.SSL.port); + return -1; } -#endif - } - LOG_DEBUG(DEBUG,"port = %d", conf->server->port); +#endif + + LOG_DEBUG(DEBUG,"port = %d", Config->Server.port); //ebb server starts listening for request - if(ebb_server_listen_on_port(&(*server)->ebb_svr, conf->server->port) < 1) { - printf("Port %d is already in use.\n", conf->server->port); + if(ebb_server_listen_on_port(&(*server)->ebb_svr, Config->Server.port) < 1) { + printf("Port %d is already in use.\n", Config->Server.port); return -1; } (*server)->on_app_add = wr_app_add_cb; (*server)->on_app_remove = wr_app_remove_cb; (*server)->on_app_reload = wr_app_reload_cb; (*server)->on_wkr_add = wr_wkr_add_cb; + (*server)->on_wkr_add_error = wr_wkr_add_error_cb; (*server)->on_wkr_remove = wr_wkr_remove_cb; (*server)->on_wkr_ping = wr_wkr_ping_cb; + (*server)->on_wkr_conf_req = wr_app_conf_req_cb; (*server)->default_app = (*server)->static_app = NULL; return 0; } @@ -166,15 +177,15 @@ ebb_server_unlisten(&(server->ebb_svr)); #ifdef HAVE_GNUTLS //Destroy ebb server object used for SSL - ebb_server_unlisten(&(server->secure_ebb_svr)); + if(Config->Server.flag & SERVER_SSL_SUPPORT) + ebb_server_unlisten(&(server->secure_ebb_svr)); #endif - // Destroy application list if(server->apps) { wr_app_free(server->apps); server->apps = NULL; } @@ -184,10 +195,9 @@ server->apps = NULL; } //Destroy Server Control object wr_svr_ctl_free(server->ctl); - wr_conf_free(server->conf); wr_req_resolver_free(server->resolver); free(server); } /*********************************************************/