diff --git a/http_server.c b/http_server.c index ea34a3d..57cf962 100644 --- a/http_server.c +++ b/http_server.c @@ -162,6 +162,11 @@ end: antd_close(client); } +void* finish_request(void* data) +{ + return NULL; +} + int rule_check(const char*k, const char* v, const char* host, const char* _url, const char* _query, char* buf) { // first perfom rule check on host, if not success, perform on url diff --git a/http_server.h b/http_server.h index 3636fd9..8bc2259 100644 --- a/http_server.h +++ b/http_server.h @@ -24,6 +24,7 @@ extern config_t server_config; void accept_request(void*); +void* finish_request(void*); void cat(void*, FILE *); void cannot_execute(void*); void error_die(const char *); diff --git a/httpd.c b/httpd.c index 7a5bc15..5777ca4 100644 --- a/httpd.c +++ b/httpd.c @@ -2,6 +2,9 @@ #include #include "http_server.h" #include "libs/ini.h" +#include "libs/scheduler.h" +#include +static antd_scheduler_t scheduler; #define MATCH(s, n) strcmp(section, s) == 0 && strcmp(name, n) == 0 int server_sock = -1; @@ -175,10 +178,12 @@ void load_config(const char* file) init_file_system(); } void stop_serve(int dummy) { + UNUSED(dummy); list_free(&(server_config.rules)); freedict(server_config.handlers); LOG("Unclosed connection: %d\n", server_config.connection); unload_all_plugin(); + antd_scheduler_destroy(&scheduler); #ifdef USE_OPENSSL SSL_CTX_free(ctx); #endif @@ -216,21 +221,19 @@ int main(int argc, char* argv[]) server_sock = startup(&port); LOG("httpd running on port %d\n", port); - - while (1) + // default to 4 workers + antd_scheduler_init(&scheduler, 4); + fcntl(server_sock, F_SETFL, O_NONBLOCK); + while (scheduler.status) { - if( server_config.connection >= server_config.maxcon ) - { - LOG("Too many unclosed connection (%d). Wait for it\n", server_config.connection); - continue; - } - antd_client_t* client = (antd_client_t*)malloc(sizeof(antd_client_t)); + antd_task_schedule(&scheduler); client_sock = accept(server_sock,(struct sockaddr *)&client_name,&client_name_len); if (client_sock == -1) { - perror("Cannot accept client request\n"); + //perror("Cannot accept client request\n"); continue; } + antd_client_t* client = (antd_client_t*)malloc(sizeof(antd_client_t)); /* get the remote IP */ @@ -273,7 +276,9 @@ int main(int argc, char* argv[]) } } #endif - if (pthread_create(&newthread , NULL,(void *(*)(void *))accept_request, (void *)client) != 0) + // create callback for the server + antd_add_task(&scheduler, antd_create_task(accept_request,(void*)client, finish_request )); + /*if (pthread_create(&newthread , NULL,(void *(*)(void *))accept_request, (void *)client) != 0) { perror("pthread_create"); antd_close(client); @@ -282,7 +287,7 @@ int main(int argc, char* argv[]) { //reclaim the stack data when thread finish pthread_detach(newthread) ; - } + }*/ //accept_request(&client); } diff --git a/libs/handle.h b/libs/handle.h index beea497..234cfeb 100644 --- a/libs/handle.h +++ b/libs/handle.h @@ -54,6 +54,11 @@ typedef struct{ char* ip; } antd_client_t; +typedef struct { + antd_client_t* client; + dictionary request; +} antd_request_t; + int response(void*, const char*); void ctype(void*,const char*); void redirect(void*,const char*);