void mk_cheetah_print_worker_memory_usage(pid_t pid)
{
- int last, init, n, c = 0;
- int s = 1024;
- char *buf;
- char *value;
- pid_t ppid;
- FILE *f;
-
- ppid = getpid();
- buf = mk_api->mem_alloc(s);
- sprintf(buf, MK_CHEETAH_PROC_TASK, ppid, pid);
-
- f = fopen(buf, "r");
- if(!f){
- printf("Cannot get details\n");
- return;
- }
-
- buf = fgets(buf, s, f);
- if(!buf){
- printf("Cannot format details\n");
- return;
- }
- fclose(f);
-
- last = 0;
- init = 0;
-
- printf("\n");
+ int last, init, n, c = 0;
+ int s = 1024;
+ char *buf;
+ char *value;
+ pid_t ppid;
+ FILE *f;
+
+ ppid = getpid();
+ buf = mk_api->mem_alloc(s);
+ sprintf(buf, MK_CHEETAH_PROC_TASK, ppid, pid);
+
+ f = fopen(buf, "r");
+ if (!f) {
+ printf("Cannot get details\n");
return;
+ }
- while((n = mk_string_search(buf+last, " ")) > 0){
- if(c == 23){
- value = mk_string_copy_substr(buf, init, last+n);
- printf("%s\n", value);
- mk_mem_free(buf);
- mk_mem_free(value);
- return;
- }
- init = last+n+1;
- last += n +1;
- c++;
+ buf = fgets(buf, s, f);
+ if (!buf) {
+ printf("Cannot format details\n");
+ return;
+ }
+ fclose(f);
+
+ last = 0;
+ init = 0;
+
+ printf("\n");
+ return;
+
+ while ((n = mk_string_search(buf + last, " ")) > 0) {
+ if (c == 23) {
+ value = mk_string_copy_substr(buf, init, last + n);
+ printf("%s\n", value);
+ mk_mem_free(buf);
+ mk_mem_free(value);
+ return;
}
+ init = last + n + 1;
+ last += n + 1;
+ c++;
+ }
}
void mk_cheetah_print_running_user()
{
- struct passwd pwd;
- struct passwd *result;
- char *buf;
- size_t bufsize;
- uid_t uid;
-
- bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
- if (bufsize == -1){
- bufsize = 16384;
- }
-
- buf = malloc(bufsize);
- uid = getuid();
- getpwuid_r(uid, &pwd, buf, bufsize, &result);
-
- printf("%s\n", pwd.pw_name);
- free(buf);
+ struct passwd pwd;
+ struct passwd *result;
+ char *buf;
+ size_t bufsize;
+ uid_t uid;
+
+ bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
+ if (bufsize == -1) {
+ bufsize = 16384;
+ }
+
+ buf = malloc(bufsize);
+ uid = getuid();
+ getpwuid_r(uid, &pwd, buf, bufsize, &result);
+
+ printf("%s\n", pwd.pw_name);
+ free(buf);
}
void mk_cheetah_cmd_uptime()
{
- int days; int hours; int minutes; int seconds;
- long int upmind;
- long int upminh;
- long int uptime;
-
- /* uptime in seconds */
- uptime = time(NULL) - init_time;
-
- /* days */
- days = uptime / MK_CHEETAH_ONEDAY;
- upmind = uptime - (days * MK_CHEETAH_ONEDAY);
-
- /* hours */
- hours = upmind / MK_CHEETAH_ONEHOUR;
- upminh = upmind - hours * MK_CHEETAH_ONEHOUR;
-
- /* minutes */
- minutes = upminh / MK_CHEETAH_ONEMINUTE;
- seconds = upminh - minutes * MK_CHEETAH_ONEMINUTE;
-
- printf("Server has been running: %i day%s, %i hour%s, %i minute%s and %i second%s\n",
- days, (days > 1) ? "s" : "",
- hours, (hours > 1) ? "s" : "",
- minutes, (minutes > 1) ? "s" : "",
- seconds, (seconds > 1) ? "s" : "");
+ int days;
+ int hours;
+ int minutes;
+ int seconds;
+ long int upmind;
+ long int upminh;
+ long int uptime;
+
+ /* uptime in seconds */
+ uptime = time(NULL) - init_time;
+
+ /* days */
+ days = uptime / MK_CHEETAH_ONEDAY;
+ upmind = uptime - (days * MK_CHEETAH_ONEDAY);
+
+ /* hours */
+ hours = upmind / MK_CHEETAH_ONEHOUR;
+ upminh = upmind - hours * MK_CHEETAH_ONEHOUR;
+
+ /* minutes */
+ minutes = upminh / MK_CHEETAH_ONEMINUTE;
+ seconds = upminh - minutes * MK_CHEETAH_ONEMINUTE;
+
+ printf
+ ("Server has been running: %i day%s, %i hour%s, %i minute%s and %i second%s\n",
+ days, (days > 1) ? "s" : "", hours, (hours > 1) ? "s" : "", minutes,
+ (minutes > 1) ? "s" : "", seconds, (seconds > 1) ? "s" : "");
}
void mk_cheetah_cmd_plugins_print(struct plugin *list, const char *stage)
{
- struct plugin *p;
-
- if(!list){
- return;
- }
-
- p = list;
- printf("* %s", stage);
- printf("\n Loaded plugins on this stage");
- printf("\n ----------------------------");
- while(p){
- printf("\n [%s] %s v%s on \"%s\"",
- p->shortname,
- p->name,
- p->version,
- p->path);
- p = p->next;
- }
+ struct plugin *p;
- printf("\n\n");
+ if (!list) {
+ return;
+ }
+
+ p = list;
+ printf("* %s", stage);
+ printf("\n Loaded plugins on this stage");
+ printf("\n ----------------------------");
+ while (p) {
+ printf("\n [%s] %s v%s on \"%s\"",
+ p->shortname, p->name, p->version, p->path);
+ p = p->next;
+ }
+
+ printf("\n\n");
}
void mk_cheetah_cmd_plugins()
{
- struct plugin_stages *p = mk_api->config->plugins;
-
- mk_cheetah_cmd_plugins_print(p->stage_00, "STAGE_00");
- mk_cheetah_cmd_plugins_print(p->stage_10, "STAGE_10");
- mk_cheetah_cmd_plugins_print(p->stage_20, "STAGE_20");
- mk_cheetah_cmd_plugins_print(p->stage_30, "STAGE_30");
- mk_cheetah_cmd_plugins_print(p->stage_40, "STAGE_40");
- mk_cheetah_cmd_plugins_print(p->stage_50, "STAGE_50");
- mk_cheetah_cmd_plugins_print(p->stage_60, "STAGE_60");
+ struct plugin_stages *p = mk_api->config->plugins;
+
+ mk_cheetah_cmd_plugins_print(p->stage_00, "STAGE_00");
+ mk_cheetah_cmd_plugins_print(p->stage_10, "STAGE_10");
+ mk_cheetah_cmd_plugins_print(p->stage_20, "STAGE_20");
+ mk_cheetah_cmd_plugins_print(p->stage_30, "STAGE_30");
+ mk_cheetah_cmd_plugins_print(p->stage_40, "STAGE_40");
+ mk_cheetah_cmd_plugins_print(p->stage_50, "STAGE_50");
+ mk_cheetah_cmd_plugins_print(p->stage_60, "STAGE_60");
}
void mk_cheetah_cmd_vhosts()
{
- struct host *host;
-
- host = mk_api->config->hosts;
-
- while(host){
- printf("* VHost '%s'\n", host->servername);
- printf(" - Configuration Path : %s\n",
- host->file);
- printf(" - Document Root : %s\n",
- host->documentroot.data);
- printf(" - Access Log : %s\n",
- host->access_log_path);
- printf(" - Error Log : %s\n",
- host->error_log_path);
- printf(" - List Directory Content : %s",
- (host->getdir == VAR_ON) ? "Yes" : "No");
- host = host->next;
- }
+ struct host *host;
+
+ host = mk_api->config->hosts;
+
+ while (host) {
+ printf("* VHost '%s'\n", host->servername);
+ printf(" - Configuration Path : %s\n", host->file);
+ printf(" - Document Root : %s\n",
+ host->documentroot.data);
+ printf(" - Access Log : %s\n",
+ host->access_log_path);
+ printf(" - Error Log : %s\n", host->error_log_path);
+ printf(" - List Directory Content : %s",
+ (host->getdir == VAR_ON) ? "Yes" : "No");
+ host = host->next;
+ }
}
void mk_cheetah_cmd_workers()
{
- struct sched_list_node *sl;
- sl = *mk_api->sched_list;
-
- while(sl){
- printf("* Worker %i\n", sl->idx);
- printf(" - Task ID : %i\n", sl->pid);
-
- /* Memory Usage */
- printf(" - Memory usage : ");
- mk_cheetah_print_worker_memory_usage(sl->pid);
-
- printf(" - Active Requests : %i\n",
- sl->active_requests);
- printf(" - Closed Requests : %i\n",
- sl->closed_requests);
- sl = sl->next;
- }
+ struct sched_list_node *sl;
+ sl = *mk_api->sched_list;
+
+ while (sl) {
+ printf("* Worker %i\n", sl->idx);
+ printf(" - Task ID : %i\n", sl->pid);
+
+ /* Memory Usage */
+ printf(" - Memory usage : ");
+ mk_cheetah_print_worker_memory_usage(sl->pid);
+
+ printf(" - Active Requests : %i\n", sl->active_requests);
+ printf(" - Closed Requests : %i\n", sl->closed_requests);
+ sl = sl->next;
+ }
}
void mk_cheetah_cmd_quit()
{
- printf("Cheeta says: Good Bye!\n");
- fflush(stdout);
- pthread_exit(NULL);
+ printf("Cheeta says: Good Bye!\n");
+ fflush(stdout);
+ pthread_exit(NULL);
}
void mk_cheetah_cmd_help()
{
- printf("\nList of available commands for Cheetah Shell\n");
- printf("\ncommand shortcut description");
- printf("\n----------------------------------------------------");
- printf("\nhelp (\\h) Print this help");
- printf("\nstatus (\\s) Display general web server information");
- printf("\nuptime (\\u) Display how long the web server has been running");
- printf("\nplugins (\\g) List loaded plugins and associated stages");
- printf("\nvhosts (\\v) List virtual hosts configured");
- printf("\nworkers (\\w) Show thread workers information");
- printf("\nquit (\\q) Exit Cheetah shell :_(\n");
+ printf("\nList of available commands for Cheetah Shell\n");
+ printf("\ncommand shortcut description");
+ printf("\n----------------------------------------------------");
+ printf("\nhelp (\\h) Print this help");
+ printf("\nstatus (\\s) Display general web server information");
+ printf
+ ("\nuptime (\\u) Display how long the web server has been running");
+ printf("\nplugins (\\g) List loaded plugins and associated stages");
+ printf("\nvhosts (\\v) List virtual hosts configured");
+ printf("\nworkers (\\w) Show thread workers information");
+ printf("\nquit (\\q) Exit Cheetah shell :_(\n");
}
void mk_cheetah_cmd(char *cmd)
{
- int nthreads = 0;
- struct sched_list_node *sl;
-
-
- sl = *mk_api->sched_list;
- while(sl){
- nthreads++;
- sl = sl->next;
- }
-
- if(strcmp(cmd, MK_CHEETAH_STATUS) == 0 ||
- strcmp(cmd, MK_CHEETAH_STATUS_SC) == 0){
- printf("\nMonkey Version : %s\n", VERSION);
- printf("Configutarion path : %s\n", mk_api->config->serverconf);
- printf("Process ID : %i\n", getpid());
-
- printf("Process User : ");
- mk_cheetah_print_running_user();
-
- printf("Server Port : %i\n", mk_api->config->serverport);
- printf("Worker Threads : %i (per configuration: %i)\n",
- nthreads,
- mk_api->config->workers);
- }
- else if(strcmp(cmd, MK_CHEETAH_UPTIME) == 0 ||
- strcmp(cmd, MK_CHEETAH_UPTIME_SC) == 0){
- mk_cheetah_cmd_uptime();
- }
- else if(strcmp(cmd, MK_CHEETAH_PLUGINS) == 0 ||
- strcmp(cmd, MK_CHEETAH_PLUGINS_SC) == 0){
- mk_cheetah_cmd_plugins();
- }
- else if(strcmp(cmd, MK_CHEETAH_WORKERS) == 0 ||
- strcmp(cmd, MK_CHEETAH_WORKERS_SC) == 0){
- mk_cheetah_cmd_workers();
- }
- else if(strcmp(cmd, MK_CHEETAH_VHOSTS) == 0 ||
- strcmp(cmd, MK_CHEETAH_VHOSTS_SC) == 0){
- mk_cheetah_cmd_vhosts();
- }
- else if(strcmp(cmd, MK_CHEETAH_HELP) == 0 ||
- strcmp(cmd, MK_CHEETAH_HELP_SC) == 0){
- mk_cheetah_cmd_help();
- }
- else if(strcmp(cmd, MK_CHEETAH_QUIT) == 0 ||
- strcmp(cmd, MK_CHEETAH_QUIT_SC) == 0){
- mk_cheetah_cmd_quit();
- }
- else if(strlen(cmd) == 0){
- return;
- }
- else{
- printf("Invalid command, type 'help' for a list of available commands\n");
- }
-
- printf("\n");
- fflush(stdout);
+ int nthreads = 0;
+ struct sched_list_node *sl;
+
+
+ sl = *mk_api->sched_list;
+ while (sl) {
+ nthreads++;
+ sl = sl->next;
+ }
+
+ if (strcmp(cmd, MK_CHEETAH_STATUS) == 0 ||
+ strcmp(cmd, MK_CHEETAH_STATUS_SC) == 0) {
+ printf("\nMonkey Version : %s\n", VERSION);
+ printf("Configutarion path : %s\n", mk_api->config->serverconf);
+ printf("Process ID : %i\n", getpid());
+
+ printf("Process User : ");
+ mk_cheetah_print_running_user();
+
+ printf("Server Port : %i\n", mk_api->config->serverport);
+ printf("Worker Threads : %i (per configuration: %i)\n",
+ nthreads, mk_api->config->workers);
+ }
+ else if (strcmp(cmd, MK_CHEETAH_UPTIME) == 0 ||
+ strcmp(cmd, MK_CHEETAH_UPTIME_SC) == 0) {
+ mk_cheetah_cmd_uptime();
+ }
+ else if (strcmp(cmd, MK_CHEETAH_PLUGINS) == 0 ||
+ strcmp(cmd, MK_CHEETAH_PLUGINS_SC) == 0) {
+ mk_cheetah_cmd_plugins();
+ }
+ else if (strcmp(cmd, MK_CHEETAH_WORKERS) == 0 ||
+ strcmp(cmd, MK_CHEETAH_WORKERS_SC) == 0) {
+ mk_cheetah_cmd_workers();
+ }
+ else if (strcmp(cmd, MK_CHEETAH_VHOSTS) == 0 ||
+ strcmp(cmd, MK_CHEETAH_VHOSTS_SC) == 0) {
+ mk_cheetah_cmd_vhosts();
+ }
+ else if (strcmp(cmd, MK_CHEETAH_HELP) == 0 ||
+ strcmp(cmd, MK_CHEETAH_HELP_SC) == 0) {
+ mk_cheetah_cmd_help();
+ }
+ else if (strcmp(cmd, MK_CHEETAH_QUIT) == 0 ||
+ strcmp(cmd, MK_CHEETAH_QUIT_SC) == 0) {
+ mk_cheetah_cmd_quit();
+ }
+ else if (strlen(cmd) == 0) {
+ return;
+ }
+ else {
+ printf
+ ("Invalid command, type 'help' for a list of available commands\n");
+ }
+
+ printf("\n");
+ fflush(stdout);
}
void mk_cheetah_loop()
{
- int len;
- char cmd[200];
- char line[200];
- char *rcmd;
-
- printf("\n*** Welcome to Cheetah!, the Monkey Shell :) ***\n");
- printf("\nType 'help' for a list of available commands\n\n");
- fflush(stdout);
-
- while(1){
- printf("%s", MK_CHEETAH_PROMPT);
- rcmd = fgets(line, sizeof(line), stdin);
-
- len = strlen(line);
- strncpy(cmd, line, len-1);
- cmd[len-1] = '\0';
-
- mk_cheetah_cmd(cmd);
- bzero(line, sizeof(line));
- }
+ int len;
+ char cmd[200];
+ char line[200];
+ char *rcmd;
+
+ printf("\n*** Welcome to Cheetah!, the Monkey Shell :) ***\n");
+ printf("\nType 'help' for a list of available commands\n\n");
+ fflush(stdout);
+
+ while (1) {
+ printf("%s", MK_CHEETAH_PROMPT);
+ rcmd = fgets(line, sizeof(line), stdin);
+
+ len = strlen(line);
+ strncpy(cmd, line, len - 1);
+ cmd[len - 1] = '\0';
+
+ mk_cheetah_cmd(cmd);
+ bzero(line, sizeof(line));
+ }
}
void *mk_cheetah_init(void *args)
{
- init_time = time(NULL);
- mk_cheetah_loop();
- return 0;
+ init_time = time(NULL);
+ mk_cheetah_loop();
+ return 0;
}
/* This function is called when the plugin is loaded, it must
*/
int _mk_plugin_init(void **api)
{
- mk_api = *api;
- return 0;
+ mk_api = *api;
+ return 0;
}
int _mk_plugin_stage_10(struct server_config *config)
{
- pthread_t tid;
- pthread_attr_t thread_attr;
-
- pthread_attr_init(&thread_attr);
- pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
- if(pthread_create(&tid, &thread_attr, (void *) mk_cheetah_init, config)<0)
- {
- perror("pthread_create");
- exit(1);
- }
-
- return 0;
+ pthread_t tid;
+ pthread_attr_t thread_attr;
+
+ pthread_attr_init(&thread_attr);
+ pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
+ if (pthread_create(&tid, &thread_attr, (void *) mk_cheetah_init, config) <
+ 0) {
+ perror("pthread_create");
+ exit(1);
+ }
+
+ return 0;
}
struct plugin_api *mk_api;
-char *_tags_global[] = {\
- "%_html_title_%", \
- "%_theme_path_%", \
- NULL};
-
-char *_tags_entry[] =\
- {"%_target_title_%", \
- "%_target_url_%", \
- "%_target_name_%", \
- "%_target_time_%", \
- "%_target_size_%", \
- NULL};
+char *_tags_global[] = { "%_html_title_%",
+ "%_theme_path_%",
+ NULL
+};
+
+char *_tags_entry[] = { "%_target_title_%",
+ "%_target_url_%",
+ "%_target_name_%",
+ "%_target_time_%",
+ "%_target_size_%",
+ NULL
+};
/* DIR_HTML logic:
* ---------------
/* Function wrote by Max (Felipe Astroza), thanks! */
char *mk_dirhtml_human_readable_size(off_t size)
{
- unsigned long u = 1024, i, len;
- char *buf;
- static const char *__units[] = {"b", "K", "M", "G",
- "T", "P", "E", "Z","Y", NULL};
-
- for(i = 0; __units[i] != NULL; i++) {
- if((size / u) == 0){
- break;
- }
- u *= 1024;
- }
- if(!i){
- mk_api->str_build(&buf, &len, "%u%s", size, __units[0]);
- }
- else {
- float fsize = (float)((double)size / (u/1024));
- mk_api->str_build(&buf, &len, "%.1f%s", fsize, __units[i]);
- }
-
- return buf;
+ unsigned long u = 1024, i, len;
+ char *buf;
+ static const char *__units[] = { "b", "K", "M", "G",
+ "T", "P", "E", "Z", "Y", NULL
+ };
+
+ for (i = 0; __units[i] != NULL; i++) {
+ if ((size / u) == 0) {
+ break;
+ }
+ u *= 1024;
+ }
+ if (!i) {
+ mk_api->str_build(&buf, &len, "%u%s", size, __units[0]);
+ }
+ else {
+ float fsize = (float) ((double) size / (u / 1024));
+ mk_api->str_build(&buf, &len, "%.1f%s", fsize, __units[i]);
+ }
+
+ return buf;
}
struct mk_f_list *mk_dirhtml_create_element(char *file,
- unsigned char type, char *full_path,
- unsigned long *list_len)
+ unsigned char type,
+ char *full_path,
+ unsigned long *list_len)
{
- off_t size;
- int n;
- struct tm *st_time;
- struct mk_f_list *entry;
- struct file_info *entry_info;
+ off_t size;
+ int n;
+ struct tm *st_time;
+ struct mk_f_list *entry;
+ struct file_info *entry_info;
- entry_info = mk_api->file_get_info(full_path);
+ entry_info = mk_api->file_get_info(full_path);
- entry = mk_api->mem_alloc_z(sizeof(struct mk_f_list));
- entry->name = file;
- entry->type = type;
- entry->info = entry_info;
- entry->next = NULL;
+ entry = mk_api->mem_alloc_z(sizeof(struct mk_f_list));
+ entry->name = file;
+ entry->type = type;
+ entry->info = entry_info;
+ entry->next = NULL;
- st_time = localtime((time_t *) &entry_info->last_modification);
+ st_time = localtime((time_t *) & entry_info->last_modification);
- entry->ft_modif = mk_api->mem_alloc_z(50);
- n = strftime(entry->ft_modif, 50,
- "%d-%b-%G %H:%M", st_time);
+ entry->ft_modif = mk_api->mem_alloc_z(50);
+ n = strftime(entry->ft_modif, 50, "%d-%b-%G %H:%M", st_time);
- size = entry->info->size;
+ size = entry->info->size;
- if(type != DT_DIR){
- entry->size = mk_dirhtml_human_readable_size(size);
- }
- else{
- entry->size = MK_DIRHTML_SIZE_DIR;
- }
+ if (type != DT_DIR) {
+ entry->size = mk_dirhtml_human_readable_size(size);
+ }
+ else {
+ entry->size = MK_DIRHTML_SIZE_DIR;
+ }
- *list_len = *list_len + 1;
+ *list_len = *list_len + 1;
- return entry;
+ return entry;
}
-struct mk_f_list *mk_dirhtml_create_list(DIR *dir, char *path,
+struct mk_f_list *mk_dirhtml_create_list(DIR * dir, char *path,
unsigned long *list_len)
{
- unsigned long len;
- char *full_path=0;
- struct dirent *ent;
- struct mk_f_list *list=0, *entry=0, *last=0;
-
- /* Before to send the information, we need to build
- * the list of entries, this really sucks because the user
- * always will want to have the information sorted, why we don't
- * add some spec to the HTTP protocol in order to send the information
- * in a generic way and let the client choose how to show it
- * as they does browsing a FTP server ???, we can save bandweight,
- * let the cool firefox developers create different templates and
- * we are going to have a more happy end users.
- *
- * that kind of ideas comes when you are in an airport just waiting :)
- */
+ unsigned long len;
+ char *full_path = 0;
+ struct dirent *ent;
+ struct mk_f_list *list = 0, *entry = 0, *last = 0;
- while((ent = readdir(dir)) != NULL)
- {
- if(ent->d_name[0] == '.') continue;
+ /* Before to send the information, we need to build
+ * the list of entries, this really sucks because the user
+ * always will want to have the information sorted, why we don't
+ * add some spec to the HTTP protocol in order to send the information
+ * in a generic way and let the client choose how to show it
+ * as they does browsing a FTP server ???, we can save bandweight,
+ * let the cool firefox developers create different templates and
+ * we are going to have a more happy end users.
+ *
+ * that kind of ideas comes when you are in an airport just waiting :)
+ */
- /* Look just for files and dirs */
- if(ent->d_type!=DT_REG && ent->d_type!=DT_DIR
- && ent->d_type!=DT_LNK)
- {
- continue;
- }
-
- if(!ent->d_name)
- {
- puts("mk_dirhtml :: buffer error");
- }
+ while ((ent = readdir(dir)) != NULL) {
+ if (ent->d_name[0] == '.')
+ continue;
+ /* Look just for files and dirs */
+ if (ent->d_type != DT_REG && ent->d_type != DT_DIR
+ && ent->d_type != DT_LNK) {
+ continue;
+ }
- mk_api->str_build(&full_path, &len, "%s%s", path, ent->d_name);
+ if (!ent->d_name) {
+ puts("mk_dirhtml :: buffer error");
+ }
- entry = mk_dirhtml_create_element(ent->d_name,
- ent->d_type,
- full_path,
- list_len);
- mk_api->mem_free(full_path);
+ mk_api->str_build(&full_path, &len, "%s%s", path, ent->d_name);
- if (!entry)
- {
- continue;
- }
+ entry = mk_dirhtml_create_element(ent->d_name,
+ ent->d_type, full_path, list_len);
- if(!list){
- list = entry;
- }
- else{
- last->next = entry;
- }
- last = entry;
- }
+ mk_api->mem_free(full_path);
+
+ if (!entry) {
+ continue;
+ }
- return list;
+ if (!list) {
+ list = entry;
+ }
+ else {
+ last->next = entry;
+ }
+ last = entry;
+ }
+
+ return list;
}
/* Read dirhtml config and themes */
int mk_dirhtml_conf(char *confdir)
{
- int ret = 0;
- unsigned long len;
- char *conf_file;
-
- mk_api->str_build(&conf_file,
- &len,
- "%s",
- confdir);
- ret = mk_dirhtml_read_config(conf_file);
-
- /* This function will load the default theme
- * setted in dirhtml_conf struct
- */
- ret = mk_dirhtml_theme_load();
- mk_api->pointer_set(&mk_dirhtml_default_mime, MK_DIRHTML_DEFAULT_MIME);
+ int ret = 0;
+ unsigned long len;
+ char *conf_file;
- return ret;
+ mk_api->str_build(&conf_file, &len, "%s", confdir);
+ ret = mk_dirhtml_read_config(conf_file);
+
+ /* This function will load the default theme
+ * setted in dirhtml_conf struct
+ */
+ ret = mk_dirhtml_theme_load();
+ mk_api->pointer_set(&mk_dirhtml_default_mime, MK_DIRHTML_DEFAULT_MIME);
+
+ return ret;
}
/*
*/
int mk_dirhtml_read_config(char *path)
{
- unsigned long len;
- char *default_file;
- struct mk_config *p;
+ unsigned long len;
+ char *default_file;
+ struct mk_config *p;
- mk_api->str_build(&default_file, &len, "%sdirhtml.conf", path);
- p = conf = mk_api->config_create(default_file);
+ mk_api->str_build(&default_file, &len, "%sdirhtml.conf", path);
+ p = conf = mk_api->config_create(default_file);
- /* alloc dirhtml config struct */
- dirhtml_conf = mk_api->mem_alloc(sizeof(struct dirhtml_config));
+ /* alloc dirhtml config struct */
+ dirhtml_conf = mk_api->mem_alloc(sizeof(struct dirhtml_config));
- while(p){
- if(strcasecmp(p->key, "Theme") == 0){
- dirhtml_conf->theme = mk_api->str_dup(p->val);
- mk_api->str_build(&dirhtml_conf->theme_path, &len,
- "%sthemes/%s/", path, dirhtml_conf->theme);
- }
- p = p->next;
+ while (p) {
+ if (strcasecmp(p->key, "Theme") == 0) {
+ dirhtml_conf->theme = mk_api->str_dup(p->val);
+ mk_api->str_build(&dirhtml_conf->theme_path, &len,
+ "%sthemes/%s/", path, dirhtml_conf->theme);
}
+ p = p->next;
+ }
- mk_api->mem_free(default_file);
- return 0;
+ mk_api->mem_free(default_file);
+ return 0;
}
int mk_dirhtml_theme_load()
{
- /* Data */
- char *header, *entry, *footer;
-
- /* Load theme files */
- header = mk_dirhtml_load_file(MK_DIRHTML_FILE_HEADER);
- entry = mk_dirhtml_load_file(MK_DIRHTML_FILE_ENTRY);
- footer = mk_dirhtml_load_file(MK_DIRHTML_FILE_FOOTER);
-
- if(!header || !entry || !footer)
- {
- mk_api->mem_free(header);
- mk_api->mem_free(entry);
- mk_api->mem_free(footer);
- return -1;
- }
-
- /* Parse themes */
- mk_dirhtml_tpl_header = mk_dirhtml_template_create(header);
- mk_dirhtml_tpl_entry = mk_dirhtml_template_create(entry);
- mk_dirhtml_tpl_footer = mk_dirhtml_template_create(footer);
+ /* Data */
+ char *header, *entry, *footer;
-#ifdef DEBUG_THEME
- /* Debug data */
- mk_dirhtml_theme_debug(&mk_dirhtml_tpl_header);
- mk_dirhtml_theme_debug(&mk_dirhtml_tpl_entry);
- mk_dirhtml_theme_debug(&mk_dirhtml_tpl_footer);
+ /* Load theme files */
+ header = mk_dirhtml_load_file(MK_DIRHTML_FILE_HEADER);
+ entry = mk_dirhtml_load_file(MK_DIRHTML_FILE_ENTRY);
+ footer = mk_dirhtml_load_file(MK_DIRHTML_FILE_FOOTER);
-#endif
+ if (!header || !entry || !footer) {
mk_api->mem_free(header);
mk_api->mem_free(entry);
mk_api->mem_free(footer);
+ return -1;
+ }
- return 0;
+ /* Parse themes */
+ mk_dirhtml_tpl_header = mk_dirhtml_template_create(header);
+ mk_dirhtml_tpl_entry = mk_dirhtml_template_create(entry);
+ mk_dirhtml_tpl_footer = mk_dirhtml_template_create(footer);
+
+#ifdef DEBUG_THEME
+ /* Debug data */
+ mk_dirhtml_theme_debug(&mk_dirhtml_tpl_header);
+ mk_dirhtml_theme_debug(&mk_dirhtml_tpl_entry);
+ mk_dirhtml_theme_debug(&mk_dirhtml_tpl_footer);
+
+#endif
+ mk_api->mem_free(header);
+ mk_api->mem_free(entry);
+ mk_api->mem_free(footer);
+
+ return 0;
}
#ifdef DEBUG_THEME
int mk_dirhtml_theme_debug(struct dirhtml_template **st_tpl)
{
- int i=0;
- struct dirhtml_template *aux;
+ int i = 0;
+ struct dirhtml_template *aux;
- aux = *st_tpl;
+ aux = *st_tpl;
- printf("\n** DEBUG_THEME **");
- fflush(stdout);
+ printf("\n** DEBUG_THEME **");
+ fflush(stdout);
- while(aux)
- {
- printf("\n%i) len=%i, tag_id=%i", i, aux->len, aux->tag_id);
- if(aux->tag_id >= 0){
- printf(" (%s) ",aux->tags[aux->tag_id]);
- }
- fflush(stdout);
- aux = aux->next;
- i++;
+ while (aux) {
+ printf("\n%i) len=%i, tag_id=%i", i, aux->len, aux->tag_id);
+ if (aux->tag_id >= 0) {
+ printf(" (%s) ", aux->tags[aux->tag_id]);
}
- return 0;
+ fflush(stdout);
+ aux = aux->next;
+ i++;
+ }
+ return 0;
}
#endif
*/
int mk_dirhtml_theme_match_tag(char *content, char *tpl[])
{
- int i, len, match;
-
- for(i=0; tpl[i]; i++){
- len = strlen(tpl[i]);
- match = (int) mk_api->str_search_n(content, tpl[i], len);
- if(match>=0){
- return i;
- }
+ int i, len, match;
+
+ for (i = 0; tpl[i]; i++) {
+ len = strlen(tpl[i]);
+ match = (int) mk_api->str_search_n(content, tpl[i], len);
+ if (match >= 0) {
+ return i;
}
+ }
- return -1;
+ return -1;
}
/* return the number of valid tags found in text string */
int mk_dirhtml_content_count_tags(char *content, char *tpl[])
{
- int pos=0, count=0;
- int len, tpl_idx;
- int loop=0;
-
- len = strlen(content);
- while(loop<len)
- {
- pos = (int) mk_api->str_search_n(content+loop, MK_DIRHTML_TAG_INIT, -1);
- if(pos>=0){
- tpl_idx = mk_dirhtml_theme_match_tag(content+loop, tpl);
- if(tpl_idx>=0){
- count++;
- }
- loop+=pos;
- }
- else{
- break;
- }
- loop++;
-
+ int pos = 0, count = 0;
+ int len, tpl_idx;
+ int loop = 0;
+
+ len = strlen(content);
+ while (loop < len) {
+ pos =
+ (int) mk_api->str_search_n(content + loop, MK_DIRHTML_TAG_INIT,
+ -1);
+ if (pos >= 0) {
+ tpl_idx = mk_dirhtml_theme_match_tag(content + loop, tpl);
+ if (tpl_idx >= 0) {
+ count++;
+ }
+ loop += pos;
}
- return count;
+ else {
+ break;
+ }
+ loop++;
+
+ }
+ return count;
}
struct dirhtml_template *mk_dirhtml_template_create(char *content)
{
- int i=0, cont_len;
- int pos, last=0; /* 0=search init, 1=search end */
- int n_tags=0, tpl_idx=0;
-
- char *_buf;
- int _len;
-
- /* Global keys */
- char **_tpl=0;
-
- /* Template to return */
- struct dirhtml_template *st_tpl=0;
-
- cont_len = strlen(content);
- if(cont_len<=0){
- return NULL;
- }
+ int i = 0, cont_len;
+ int pos, last = 0; /* 0=search init, 1=search end */
+ int n_tags = 0, tpl_idx = 0;
- /* Parsing content */
- while(i<cont_len)
- {
- pos = (int) mk_api->str_search_n(content+i,
- MK_DIRHTML_TAG_INIT, -1);
+ char *_buf;
+ int _len;
- if(pos<0){
- break;
- }
+ /* Global keys */
+ char **_tpl = 0;
- /* Checking global tag, if it's not found, proceed with
- * 'entry tags'
- */
- _tpl = (char **) _tags_global;
- tpl_idx = mk_dirhtml_theme_match_tag(content+i+pos, _tpl);
+ /* Template to return */
+ struct dirhtml_template *st_tpl = 0;
- /* if global template do not match, use the entry tags */
- if(tpl_idx < 0){
- _tpl = (char **) _tags_entry;
- tpl_idx = mk_dirhtml_theme_match_tag(content+i+pos, _tpl);
- }
-
- /* if tag found is known, we add them to our list */
- if(tpl_idx>=0){
-
- _buf = mk_api->str_copy_substr(content, i, i+pos);
- _len = strlen(_buf);
-
- /* Dummy if/else to create or pass a created st_tpl */
- if(!st_tpl){
- st_tpl = mk_dirhtml_template_list_add(NULL,
- _buf,
- _len,
- _tpl,
- -1);
- }
- else{
- mk_dirhtml_template_list_add(&st_tpl,
- _buf,
- _len,
- _tpl,
- -1);
- }
- i += (pos+strlen(_tpl[tpl_idx]));
-
- /* This means that a value need to be replaced */
- mk_dirhtml_template_list_add(&st_tpl, NULL, -1,
- _tpl, tpl_idx);
- n_tags++;
- }
- else{
- i++;
- }
+ cont_len = strlen(content);
+ if (cont_len <= 0) {
+ return NULL;
+ }
+
+ /* Parsing content */
+ while (i < cont_len) {
+ pos = (int) mk_api->str_search_n(content + i,
+ MK_DIRHTML_TAG_INIT, -1);
+
+ if (pos < 0) {
+ break;
}
- if(last<cont_len){
- _buf = mk_api->str_copy_substr(content, i, cont_len);
- _len = strlen(_buf);
+ /* Checking global tag, if it's not found, proceed with
+ * 'entry tags'
+ */
+ _tpl = (char **) _tags_global;
+ tpl_idx = mk_dirhtml_theme_match_tag(content + i + pos, _tpl);
- if(n_tags<=0){
- st_tpl = mk_dirhtml_template_list_add(NULL, _buf,
- _len, _tpl, -1);
- }
- else{
- mk_dirhtml_template_list_add(&st_tpl, _buf,
- _len, _tpl, -1);
- }
+ /* if global template do not match, use the entry tags */
+ if (tpl_idx < 0) {
+ _tpl = (char **) _tags_entry;
+ tpl_idx = mk_dirhtml_theme_match_tag(content + i + pos, _tpl);
}
- return st_tpl;
-}
+ /* if tag found is known, we add them to our list */
+ if (tpl_idx >= 0) {
-struct dirhtml_template
-*mk_dirhtml_template_list_add(struct dirhtml_template **header,
- char *buf, int len,
- char **tpl, int tag_id)
-{
- struct dirhtml_template *node, *aux;
+ _buf = mk_api->str_copy_substr(content, i, i + pos);
+ _len = strlen(_buf);
- node = mk_api->mem_alloc_z(sizeof(struct dirhtml_template));
- if(!node)
- {
- return NULL;
+ /* Dummy if/else to create or pass a created st_tpl */
+ if (!st_tpl) {
+ st_tpl = mk_dirhtml_template_list_add(NULL,
+ _buf, _len, _tpl, -1);
+ }
+ else {
+ mk_dirhtml_template_list_add(&st_tpl, _buf, _len, _tpl, -1);
+ }
+ i += (pos + strlen(_tpl[tpl_idx]));
+
+ /* This means that a value need to be replaced */
+ mk_dirhtml_template_list_add(&st_tpl, NULL, -1, _tpl, tpl_idx);
+ n_tags++;
+ }
+ else {
+ i++;
}
+ }
-
- node->buf = buf;
- node->len = len;
- node->tag_id = tag_id;
- node->tags = tpl;
- node->next = NULL;
+ if (last < cont_len) {
+ _buf = mk_api->str_copy_substr(content, i, cont_len);
+ _len = strlen(_buf);
- /*
- if(tag_id >= 0){
- printf("\n -> %s", node->tags[node->tag_id]);
+ if (n_tags <= 0) {
+ st_tpl = mk_dirhtml_template_list_add(NULL, _buf, _len, _tpl, -1);
}
- */
- if(!header){
- return (struct dirhtml_template *) node;
+ else {
+ mk_dirhtml_template_list_add(&st_tpl, _buf, _len, _tpl, -1);
}
+ }
- aux = *header;
- while((*aux).next!=NULL){
- aux = (*aux).next;
- }
+ return st_tpl;
+}
- (*aux).next = node;
+struct dirhtml_template
+ *mk_dirhtml_template_list_add(struct dirhtml_template **header,
+ char *buf, int len, char **tpl, int tag_id)
+{
+ struct dirhtml_template *node, *aux;
+
+ node = mk_api->mem_alloc_z(sizeof(struct dirhtml_template));
+ if (!node) {
+ return NULL;
+ }
+
+
+ node->buf = buf;
+ node->len = len;
+ node->tag_id = tag_id;
+ node->tags = tpl;
+ node->next = NULL;
+
+ /*
+ if(tag_id >= 0){
+ printf("\n -> %s", node->tags[node->tag_id]);
+ }
+ */
+ if (!header) {
return (struct dirhtml_template *) node;
+ }
+
+ aux = *header;
+ while ((*aux).next != NULL) {
+ aux = (*aux).next;
+ }
+
+ (*aux).next = node;
+ return (struct dirhtml_template *) node;
}
int mk_dirhtml_tag_get_id(char *tpl_tags[], char *tag)
{
- int i;
- for(i=0; tpl_tags[i]; i++)
- {
- if(strcmp(tpl_tags[i], tag)==0){
- return i;
- }
+ int i;
+ for (i = 0; tpl_tags[i]; i++) {
+ if (strcmp(tpl_tags[i], tag) == 0) {
+ return i;
}
+ }
- return -1;
+ return -1;
}
int mk_dirhtml_template_len(struct dirhtml_template *tpl)
{
- int len=0;
- struct dirhtml_template *aux;
+ int len = 0;
+ struct dirhtml_template *aux;
- aux = tpl;
- while(aux){
- len++;
- aux = aux->next;
- }
+ aux = tpl;
+ while (aux) {
+ len++;
+ aux = aux->next;
+ }
- return len;
+ return len;
}
struct mk_iov *mk_dirhtml_theme_compose(struct dirhtml_template *template,
- struct dirhtml_value *values)
+ struct dirhtml_value *values)
{
- /*
- * template = struct { char buf ; int len, int tag }
- * values = struct {int tag, char *value, struct *next}
- */
-
- struct mk_iov *iov;
- struct dirhtml_template *tpl = template;
- struct dirhtml_value *val = values;
-
- int tpl_len;
-
- tpl_len = mk_dirhtml_template_len(template);
-
- /* we duplicate the lenght in case we get separators */
- iov = (struct mk_iov *) mk_api->iov_create(tpl_len*2, 1);
- tpl = template;
-
- while(tpl){
- /* check for dynamic value */
- if(!tpl->buf && tpl->tag_id>=0){
- val = values;
- while(val){
- if(val->tags == tpl->tags &&
- val->tag_id == tpl->tag_id){
- mk_api->iov_add_entry(iov,
- val->value,
- val->len,
- val->sep,
- MK_IOV_NOT_FREE_BUF);
- break;
- }
- else{
- val = val->next;
- }
-
- }
- }
- /* static */
- else{
- mk_api->iov_add_entry(iov, tpl->buf,
- tpl->len,
- mk_iov_none,
- MK_IOV_NOT_FREE_BUF);
+ /*
+ * template = struct { char buf ; int len, int tag }
+ * values = struct {int tag, char *value, struct *next}
+ */
+
+ struct mk_iov *iov;
+ struct dirhtml_template *tpl = template;
+ struct dirhtml_value *val = values;
+
+ int tpl_len;
+
+ tpl_len = mk_dirhtml_template_len(template);
+
+ /* we duplicate the lenght in case we get separators */
+ iov = (struct mk_iov *) mk_api->iov_create(tpl_len * 2, 1);
+ tpl = template;
+
+ while (tpl) {
+ /* check for dynamic value */
+ if (!tpl->buf && tpl->tag_id >= 0) {
+ val = values;
+ while (val) {
+ if (val->tags == tpl->tags && val->tag_id == tpl->tag_id) {
+ mk_api->iov_add_entry(iov,
+ val->value,
+ val->len,
+ val->sep, MK_IOV_NOT_FREE_BUF);
+ break;
+ }
+ else {
+ val = val->next;
}
- tpl = tpl->next;
+
+ }
+ }
+ /* static */
+ else {
+ mk_api->iov_add_entry(iov, tpl->buf,
+ tpl->len, mk_iov_none, MK_IOV_NOT_FREE_BUF);
}
- return (struct mk_iov *) iov;
+ tpl = tpl->next;
+ }
+ return (struct mk_iov *) iov;
}
struct dirhtml_value *mk_dirhtml_tag_assign(struct dirhtml_value **values,
- int tag_id, mk_pointer sep,
+ int tag_id, mk_pointer sep,
char *value, char **tags)
{
- struct dirhtml_value *check, *aux=0;
-
- aux = mk_api->mem_alloc(sizeof(struct dirhtml_value));
- if(!aux){
- return NULL;
- }
+ struct dirhtml_value *check, *aux = 0;
- aux->tag_id = tag_id;
- aux->value = value;
- aux->sep = sep;
- aux->tags = tags;
+ aux = mk_api->mem_alloc(sizeof(struct dirhtml_value));
+ if (!aux) {
+ return NULL;
+ }
- if(value){
- aux->len = strlen(value);
- }
- else{
- aux->len = -1;
- }
+ aux->tag_id = tag_id;
+ aux->value = value;
+ aux->sep = sep;
+ aux->tags = tags;
- aux->next = NULL;
+ if (value) {
+ aux->len = strlen(value);
+ }
+ else {
+ aux->len = -1;
+ }
- if(!values){
- return (struct dirhtml_value *) aux;
- }
+ aux->next = NULL;
- check = *values;
- while((*check).next){
- check = (*check).next;
- }
+ if (!values) {
+ return (struct dirhtml_value *) aux;
+ }
- (*check).next = aux;
+ check = *values;
+ while ((*check).next) {
+ check = (*check).next;
+ }
+ (*check).next = aux;
- return (struct dirhtml_value *) aux;
+
+ return (struct dirhtml_value *) aux;
}
void mk_dirhtml_tag_free_list(struct dirhtml_value **list)
{
- struct dirhtml_value *prev, *target;
+ struct dirhtml_value *prev, *target;
- target = *list;
- while(target){
- while((*target).next){
- prev = target;
- target = (*target).next;
- }
- mk_api->mem_free(target);
+ target = *list;
+ while (target) {
+ while ((*target).next) {
+ prev = target;
+ target = (*target).next;
+ }
+ mk_api->mem_free(target);
- if(target == *list){
- break;
- }
- (*prev).next = NULL;
- target = *list;
+ if (target == *list) {
+ break;
}
- *list = NULL;
+ (*prev).next = NULL;
+ target = *list;
+ }
+ *list = NULL;
}
char *mk_dirhtml_load_file(char *filename)
{
- char *tmp=0, *data=0;
- unsigned long len;
+ char *tmp = 0, *data = 0;
+ unsigned long len;
- mk_api->str_build(&tmp, &len, "%s%s",
- dirhtml_conf->theme_path, filename);
+ mk_api->str_build(&tmp, &len, "%s%s", dirhtml_conf->theme_path, filename);
- if(!tmp)
- {
- return NULL;
- }
+ if (!tmp) {
+ return NULL;
+ }
- data = mk_api->file_to_buffer(tmp);
- mk_api->mem_free(tmp);
+ data = mk_api->file_to_buffer(tmp);
+ mk_api->mem_free(tmp);
- if(!data)
- {
- return NULL;
- }
+ if (!data) {
+ return NULL;
+ }
- return (char *) data;
+ return (char *) data;
}
int mk_dirhtml_entry_cmp(const void *a, const void *b)
{
- struct mk_f_list * const *f_a = a;
- struct mk_f_list * const *f_b = b;
+ struct mk_f_list *const *f_a = a;
+ struct mk_f_list *const *f_b = b;
- return strcmp((*f_a)->name, (*f_b)->name);
+ return strcmp((*f_a)->name, (*f_b)->name);
}
int mk_dirhtml_send(int fd, struct request *sr, struct mk_iov *data)
{
- int n;
- unsigned long len;
- char *buf=0;
-
- if(sr->protocol >= HTTP_PROTOCOL_11){
- /* Chunk header */
- mk_api->str_build(&buf, &len, "%x%s",
- data->total_len - 1, MK_CRLF);
-
- /* Add chunked information */
- mk_api->iov_set_entry(data, buf, len, MK_IOV_FREE_BUF, 0);
- }
- n = (int) mk_api->iov_send(fd, data, MK_IOV_SEND_TO_SOCKET);
- return n;
+ int n;
+ unsigned long len;
+ char *buf = 0;
+
+ if (sr->protocol >= HTTP_PROTOCOL_11) {
+ /* Chunk header */
+ mk_api->str_build(&buf, &len, "%x%s", data->total_len - 1, MK_CRLF);
+
+ /* Add chunked information */
+ mk_api->iov_set_entry(data, buf, len, MK_IOV_FREE_BUF, 0);
+ }
+ n = (int) mk_api->iov_send(fd, data, MK_IOV_SEND_TO_SOCKET);
+ return n;
}
int mk_dirhtml_send_chunked_end(int fd)
{
- char *_end = "0\r\n\r\n";
- int len = 5;
+ char *_end = "0\r\n\r\n";
+ int len = 5;
- return write(fd, _end, len);
+ return write(fd, _end, len);
}
void mk_dirhtml_free_list(struct mk_f_list **toc, unsigned long len)
{
- int i;
- struct mk_f_list *entry;
+ int i;
+ struct mk_f_list *entry;
- for(i=0; i<len; i++)
- {
- entry = toc[i];
+ for (i = 0; i < len; i++) {
+ entry = toc[i];
- if(entry->type != DT_DIR)
- {
- mk_api->mem_free(entry->size);
- }
- mk_api->mem_free(entry->ft_modif);
- mk_api->mem_free(entry->info);
- mk_api->mem_free(entry);
+ if (entry->type != DT_DIR) {
+ mk_api->mem_free(entry->size);
}
-
- mk_api->mem_free(toc);
+ mk_api->mem_free(entry->ft_modif);
+ mk_api->mem_free(entry->info);
+ mk_api->mem_free(entry);
+ }
+
+ mk_api->mem_free(toc);
}
int mk_dirhtml_init(struct client_request *cr, struct request *sr)
{
- DIR *dir;
- int i=0, n;
- mk_pointer sep;
-
- /* file info */
- unsigned long list_len=0;
-
- struct mk_f_list *file_list, *entry, **toc;
- struct mk_iov *iov_header, *iov_footer, *iov_entry;
- struct dirhtml_value *values_global = 0;
- struct dirhtml_value *values_entry = 0;
-
- if(!(dir = opendir(sr->real_path.data)))
- {
- return -1;
- }
+ DIR *dir;
+ int i = 0, n;
+ mk_pointer sep;
- file_list = mk_dirhtml_create_list(dir, sr->real_path.data, &list_len);
-
- /* Building headers */
- sr->headers->status = M_HTTP_OK;
- sr->headers->cgi = SH_CGI;
- sr->headers->breakline = MK_HEADER_BREAKLINE;
- sr->headers->content_type = mk_dirhtml_default_mime;
-
- if(sr->protocol >= HTTP_PROTOCOL_11)
- {
- sr->headers->transfer_encoding = MK_HEADER_TE_TYPE_CHUNKED;
- }
-
- /* Sending headers */
- n = (int) mk_api->header_send(cr->socket, cr, sr, sr->log);
-
- /* Creating response template */
- /* Set %_html_title_% */
- values_global = mk_dirhtml_tag_assign(NULL, 0, mk_iov_none,
- sr->uri_processed,
- (char **) _tags_global);
-
- /* Set %_theme_path_% */
- mk_dirhtml_tag_assign(&values_global, 1, mk_iov_none,
- dirhtml_conf->theme_path,
- (char **) _tags_global);
-
- /* HTML Header */
- iov_header = mk_dirhtml_theme_compose(mk_dirhtml_tpl_header,
- values_global);
-
- /* HTML Footer */
- iov_footer = mk_dirhtml_theme_compose(mk_dirhtml_tpl_footer,
- values_global);
-
- /* Creating table of contents and sorting */
- toc = mk_api->mem_alloc(sizeof(struct mk_f_list)*list_len);
- entry = file_list;
- i = 0;
- while(entry)
- {
- toc[i] = entry;
- i++;
- entry = entry->next;
- }
- qsort(toc, list_len, sizeof(*toc), mk_dirhtml_entry_cmp);
-
- n = mk_dirhtml_send(cr->socket, sr, iov_header);
-
- if(n < 0){
- closedir(dir);
-
- mk_dirhtml_tag_free_list(&values_global);
- mk_api->iov_free(iov_header);
- mk_api->iov_free(iov_footer);
- mk_dirhtml_free_list(toc, list_len);
- return 0;
- }
+ /* file info */
+ unsigned long list_len = 0;
- /* sending TOC */
- for(i=0; i<list_len; i++)
- {
- /* %_target_title_% */
- if(toc[i]->type==DT_DIR){
- sep = mk_iov_slash;
- }
- else{
- sep = mk_iov_none;
- }
+ struct mk_f_list *file_list, *entry, **toc;
+ struct mk_iov *iov_header, *iov_footer, *iov_entry;
+ struct dirhtml_value *values_global = 0;
+ struct dirhtml_value *values_entry = 0;
- /* target title */
- values_entry = mk_dirhtml_tag_assign(NULL, 0, sep,
- toc[i]->name,
- (char **) _tags_entry);
+ if (!(dir = opendir(sr->real_path.data))) {
+ return -1;
+ }
+
+ file_list = mk_dirhtml_create_list(dir, sr->real_path.data, &list_len);
+
+ /* Building headers */
+ sr->headers->status = M_HTTP_OK;
+ sr->headers->cgi = SH_CGI;
+ sr->headers->breakline = MK_HEADER_BREAKLINE;
+ sr->headers->content_type = mk_dirhtml_default_mime;
+
+ if (sr->protocol >= HTTP_PROTOCOL_11) {
+ sr->headers->transfer_encoding = MK_HEADER_TE_TYPE_CHUNKED;
+ }
+
+ /* Sending headers */
+ n = (int) mk_api->header_send(cr->socket, cr, sr, sr->log);
+
+ /* Creating response template */
+ /* Set %_html_title_% */
+ values_global = mk_dirhtml_tag_assign(NULL, 0, mk_iov_none,
+ sr->uri_processed,
+ (char **) _tags_global);
+
+ /* Set %_theme_path_% */
+ mk_dirhtml_tag_assign(&values_global, 1, mk_iov_none,
+ dirhtml_conf->theme_path, (char **) _tags_global);
+
+ /* HTML Header */
+ iov_header = mk_dirhtml_theme_compose(mk_dirhtml_tpl_header,
+ values_global);
+
+ /* HTML Footer */
+ iov_footer = mk_dirhtml_theme_compose(mk_dirhtml_tpl_footer,
+ values_global);
+
+ /* Creating table of contents and sorting */
+ toc = mk_api->mem_alloc(sizeof(struct mk_f_list) * list_len);
+ entry = file_list;
+ i = 0;
+ while (entry) {
+ toc[i] = entry;
+ i++;
+ entry = entry->next;
+ }
+ qsort(toc, list_len, sizeof(*toc), mk_dirhtml_entry_cmp);
+
+ n = mk_dirhtml_send(cr->socket, sr, iov_header);
+
+ if (n < 0) {
+ closedir(dir);
- /* target url */
- mk_dirhtml_tag_assign(&values_entry, 1, sep,
- toc[i]->name,
- (char **) _tags_entry);
+ mk_dirhtml_tag_free_list(&values_global);
+ mk_api->iov_free(iov_header);
+ mk_api->iov_free(iov_footer);
+ mk_dirhtml_free_list(toc, list_len);
+ return 0;
+ }
- /* target name */
- mk_dirhtml_tag_assign(&values_entry, 2, sep,
- toc[i]->name,
- (char **) _tags_entry);
+ /* sending TOC */
+ for (i = 0; i < list_len; i++) {
+ /* %_target_title_% */
+ if (toc[i]->type == DT_DIR) {
+ sep = mk_iov_slash;
+ }
+ else {
+ sep = mk_iov_none;
+ }
- /* target modification time */
- mk_dirhtml_tag_assign(&values_entry, 3, mk_iov_none,
- toc[i]->ft_modif,
- (char **) _tags_entry);
+ /* target title */
+ values_entry = mk_dirhtml_tag_assign(NULL, 0, sep,
+ toc[i]->name,
+ (char **) _tags_entry);
- /* target size */
- mk_dirhtml_tag_assign(&values_entry, 4, mk_iov_none,
- toc[i]->size,
- (char **) _tags_entry);
+ /* target url */
+ mk_dirhtml_tag_assign(&values_entry, 1, sep,
+ toc[i]->name, (char **) _tags_entry);
- iov_entry = mk_dirhtml_theme_compose(mk_dirhtml_tpl_entry,
- values_entry);
+ /* target name */
+ mk_dirhtml_tag_assign(&values_entry, 2, sep,
+ toc[i]->name, (char **) _tags_entry);
- /* send entry */
- n = mk_dirhtml_send(cr->socket, sr, iov_entry);
+ /* target modification time */
+ mk_dirhtml_tag_assign(&values_entry, 3, mk_iov_none,
+ toc[i]->ft_modif, (char **) _tags_entry);
- if( (i%20) == 0 && i > 0){
- mk_api->socket_cork_flag(cr->socket, TCP_CORK_OFF);
- mk_api->socket_cork_flag(cr->socket, TCP_CORK_ON);
- }
+ /* target size */
+ mk_dirhtml_tag_assign(&values_entry, 4, mk_iov_none,
+ toc[i]->size, (char **) _tags_entry);
- if(n<0){
- break;
- }
+ iov_entry = mk_dirhtml_theme_compose(mk_dirhtml_tpl_entry,
+ values_entry);
- /* free entry list */
- mk_dirhtml_tag_free_list(&values_entry);
- mk_api->iov_free(iov_entry);
- }
+ /* send entry */
+ n = mk_dirhtml_send(cr->socket, sr, iov_entry);
- n = mk_dirhtml_send(cr->socket, sr, iov_footer);
- mk_api->socket_cork_flag(cr->socket, TCP_CORK_OFF);
+ if ((i % 20) == 0 && i > 0) {
+ mk_api->socket_cork_flag(cr->socket, TCP_CORK_OFF);
+ mk_api->socket_cork_flag(cr->socket, TCP_CORK_ON);
+ }
- if(sr->protocol >= HTTP_PROTOCOL_11 && n >= 0){
- mk_dirhtml_send_chunked_end(cr->socket);
+ if (n < 0) {
+ break;
}
- closedir(dir);
+ /* free entry list */
+ mk_dirhtml_tag_free_list(&values_entry);
+ mk_api->iov_free(iov_entry);
+ }
- mk_dirhtml_tag_free_list(&values_global);
- mk_api->iov_free(iov_header);
- mk_api->iov_free(iov_footer);
- mk_dirhtml_free_list(toc, list_len);
+ n = mk_dirhtml_send(cr->socket, sr, iov_footer);
+ mk_api->socket_cork_flag(cr->socket, TCP_CORK_OFF);
- return 0;
+ if (sr->protocol >= HTTP_PROTOCOL_11 && n >= 0) {
+ mk_dirhtml_send_chunked_end(cr->socket);
+ }
+
+ closedir(dir);
+
+ mk_dirhtml_tag_free_list(&values_global);
+ mk_api->iov_free(iov_header);
+ mk_api->iov_free(iov_footer);
+ mk_dirhtml_free_list(toc, list_len);
+
+ return 0;
}
int _mk_plugin_init(void **api, char *confdir)
{
- mk_api = *api;
- mk_dirhtml_conf(confdir);
- return 0;
+ mk_api = *api;
+ mk_dirhtml_conf(confdir);
+ return 0;
}
int _mk_plugin_stage_40(struct client_request *cr, struct request *sr)
{
- /* Validate file/directory */
- if(!sr->file_info){
- return -1;
- }
+ /* Validate file/directory */
+ if (!sr->file_info) {
+ return -1;
+ }
- /* This plugin just handle directories */
- if(sr->file_info->is_directory == MK_FILE_FALSE){
- return -1;
- }
+ /* This plugin just handle directories */
+ if (sr->file_info->is_directory == MK_FILE_FALSE) {
+ return -1;
+ }
- /* check setup */
- if(sr->host_conf->getdir == VAR_OFF){
- mk_request_error(M_CLIENT_FORBIDDEN, cr, sr, 1, sr->log);
- return -1;
- }
+ /* check setup */
+ if (sr->host_conf->getdir == VAR_OFF) {
+ mk_request_error(M_CLIENT_FORBIDDEN, cr, sr, 1, sr->log);
+ return -1;
+ }
- mk_dirhtml_init(cr, sr);
- return 0;
+ mk_dirhtml_init(cr, sr);
+ return 0;
}
struct mk_f_list
{
- char *name;
- char *size;
- char *ft_modif;
- unsigned char type;
- struct file_info *info;
- struct mk_f_list *next;
+ char *name;
+ char *size;
+ char *ft_modif;
+ unsigned char type;
+ struct file_info *info;
+ struct mk_f_list *next;
};
/* Main configuration of dirhtml module */
struct dirhtml_config
{
- char *theme;
- char *theme_path;
+ char *theme;
+ char *theme_path;
};
/* Used to keep splitted content of every template */
struct dirhtml_template
{
- char *buf;
- int tag_id;
- int len;
- struct dirhtml_template *next;
- char **tags; /* array of theme tags: [%_xaa__%, %_xyz_%] */
+ char *buf;
+ int tag_id;
+ int len;
+ struct dirhtml_template *next;
+ char **tags; /* array of theme tags: [%_xaa__%, %_xyz_%] */
};
/* Templates for header, entries and footer */
struct dirhtml_value
{
- int tag_id;
- mk_pointer sep; /* separator code after value */
+ int tag_id;
+ mk_pointer sep; /* separator code after value */
- /* string data */
- int len;
- char *value;
+ /* string data */
+ int len;
+ char *value;
- /* next node */
- struct dirhtml_value *next;
+ /* next node */
+ struct dirhtml_value *next;
- char **tags; /* array of tags which values correspond */
+ char **tags; /* array of tags which values correspond */
};
struct dirhtml_value *mk_dirhtml_value_global;
/* Configuration struct */
struct mk_config *conf;
-char *check_string(char *str);
-char *read_header_footer_file(char *file_path);
+char *check_string(char *str);
+char *read_header_footer_file(char *file_path);
int mk_dirhtml_conf();
char *mk_dirhtml_load_file(char *filename);
-struct dirhtml_template
-*mk_dirhtml_template_create(char *content);
+struct dirhtml_template *mk_dirhtml_template_create(char *content);
-struct dirhtml_template
-*mk_dirhtml_template_list_add(struct dirhtml_template **header,
- char *buf, int len,
- char **tpl, int tag);
+struct dirhtml_template
+ *mk_dirhtml_template_list_add(struct dirhtml_template **header,
+ char *buf, int len, char **tpl, int tag);
int mk_dirhtml_init(struct client_request *cr, struct request *sr);
int mk_dirhtml_read_config(char *path);
int mk_dirhtml_theme_debug(struct dirhtml_template **st_tpl);
struct dirhtml_value *mk_dirhtml_tag_assign(struct dirhtml_value **values,
- int tag_id, mk_pointer sep,
+ int tag_id, mk_pointer sep,
char *value, char **tags);
struct f_list *get_dir_content(struct request *sr, char *path);
/* Read database configuration parameters */
int mk_security_conf(char *confdir)
{
- int ret = 0;
- unsigned long len;
- char *conf_path;
- struct mk_config *p;
- struct mk_security *new, *r;
-
- mk_api->str_build(&conf_path,
- &len,
- "%s/security.conf",
- confdir);
-
- p = conf = mk_api->config_create(conf_path);
-
- r = rules;
- while(p){
- /* Passing to internal struct */
- new = mk_api->mem_alloc(sizeof(struct mk_security));
- if(strcasecmp(p->key, "IP") == 0){
- new->type = MK_SECURITY_TYPE_IP;
- }
- else if(strcasecmp(p->key, "URL") == 0){
- new->type = MK_SECURITY_TYPE_URL;
- }
+ int ret = 0;
+ unsigned long len;
+ char *conf_path;
+ struct mk_config *p;
+ struct mk_security *new, *r;
+
+ mk_api->str_build(&conf_path, &len, "%s/security.conf", confdir);
+
+ p = conf = mk_api->config_create(conf_path);
+
+ r = rules;
+ while (p) {
+ /* Passing to internal struct */
+ new = mk_api->mem_alloc(sizeof(struct mk_security));
+ if (strcasecmp(p->key, "IP") == 0) {
+ new->type = MK_SECURITY_TYPE_IP;
+ }
+ else if (strcasecmp(p->key, "URL") == 0) {
+ new->type = MK_SECURITY_TYPE_URL;
+ }
- new->value = p->val;
- new->next = NULL;
+ new->value = p->val;
+ new->next = NULL;
- /* Linking node */
- if(!rules){
- rules = new;
- }
- else{
- r = rules;
- while(r->next){
- r = r->next;
- }
- r->next = new;
- }
- p = p->next;
+ /* Linking node */
+ if (!rules) {
+ rules = new;
+ }
+ else {
+ r = rules;
+ while (r->next) {
+ r = r->next;
+ }
+ r->next = new;
}
+ p = p->next;
+ }
- mk_api->mem_free(conf_path);
- return ret;
+ mk_api->mem_free(conf_path);
+ return ret;
}
int mk_security_check_ip(char *ipv4)
{
- unsigned int i=0;
- struct mk_security *p;
-
- p = rules;
- while(p){
- if(p->type == MK_SECURITY_TYPE_IP){
- for(i=0; p->value[i]; i ++) {
- if (p->value[i]=='?') {
- if (ipv4[i]=='.' || ipv4[i]=='\0')
- return -1;
- else
- continue;
- }
-
- if (p->value[i]=='*')
- return -1;
-
- if (p->value[i]!=ipv4[i])
- return 0;
- }
+ unsigned int i = 0;
+ struct mk_security *p;
+
+ p = rules;
+ while (p) {
+ if (p->type == MK_SECURITY_TYPE_IP) {
+ for (i = 0; p->value[i]; i++) {
+ if (p->value[i] == '?') {
+ if (ipv4[i] == '.' || ipv4[i] == '\0')
+ return -1;
+ else
+ continue;
}
- p = p->next;
- }
- if(ipv4[i] == '\0'){
- return -1;
- }
- else{
- return 0;
+ if (p->value[i] == '*')
+ return -1;
+
+ if (p->value[i] != ipv4[i])
+ return 0;
+ }
}
+ p = p->next;
+ }
+
+ if (ipv4[i] == '\0') {
+ return -1;
+ }
+ else {
+ return 0;
+ }
}
int mk_security_check_url(mk_pointer url)
{
- int n;
- struct mk_security *p;
-
- p = rules;
- while(p){
- if(p->type == MK_SECURITY_TYPE_URL){
- n = (int) mk_api->str_search_n(url.data,p->value,url.len);
- if(n>=0){
- return -1;
- }
- }
- p = p->next;
+ int n;
+ struct mk_security *p;
+
+ p = rules;
+ while (p) {
+ if (p->type == MK_SECURITY_TYPE_URL) {
+ n = (int) mk_api->str_search_n(url.data, p->value, url.len);
+ if (n >= 0) {
+ return -1;
+ }
}
+ p = p->next;
+ }
- return 0;
+ return 0;
}
int _mk_plugin_init(void **api, char *confdir)
{
- mk_api = *api;
- rules = 0;
+ mk_api = *api;
+ rules = 0;
- /* Read configuration */
- mk_security_conf(confdir);
- return 0;
+ /* Read configuration */
+ mk_security_conf(confdir);
+ return 0;
}
int _mk_plugin_stage_20(unsigned int socket, struct sched_connection *conx)
{
- if(mk_security_check_ip(conx->ipv4)!=0){
- return MK_PLUGIN_RET_CLOSE_CONX;
- }
+ if (mk_security_check_ip(conx->ipv4) != 0) {
+ return MK_PLUGIN_RET_CLOSE_CONX;
+ }
- return MK_PLUGIN_RET_CONTINUE;
+ return MK_PLUGIN_RET_CONTINUE;
}
int _mk_plugin_stage_30(struct client_request *cr, struct request *sr)
{
- if(mk_security_check_url(sr->uri) < 0){
- return MK_PLUGIN_RET_CLOSE_CONX;
- }
-
- return MK_PLUGIN_RET_CONTINUE;
+ if (mk_security_check_url(sr->uri) < 0) {
+ return MK_PLUGIN_RET_CLOSE_CONX;
+ }
+
+ return MK_PLUGIN_RET_CONTINUE;
}
#define MK_SECURITY_TYPE_IP 0
#define MK_SECURITY_TYPE_URL 1
-struct mk_security {
- int type;
- char *value;
- struct mk_security *next;
+struct mk_security
+{
+ int type;
+ char *value;
+ struct mk_security *next;
};
struct mk_security *rules;
/* This function is called when a thread is created */
void mk_cache_thread_init()
{
- struct request_idx *cache_request_idx;
- struct mk_iov *cache_iov_log;
- struct mk_iov *cache_iov_header;
- struct header_toc *cache_header_toc;
-
- /* client request index */
- cache_request_idx = mk_mem_malloc(sizeof(struct request_idx));
- cache_request_idx->first = NULL;
- cache_request_idx->last = NULL;
- pthread_setspecific(request_index, (void *) cache_request_idx);
-
- /* Cache iov log struct */
- cache_iov_log = mk_iov_create(15, 0);
- pthread_setspecific(mk_cache_iov_log, (void *) cache_iov_log);
-
- /* Cache iov header struct */
- cache_iov_header = mk_iov_create(45, 0);
- pthread_setspecific(mk_cache_iov_header, (void *) cache_iov_header);
-
- /* Cache header toc, monkey just search for MK_KNOWN_HEADERS
- * in request
- */
- cache_header_toc = mk_mem_malloc_z(sizeof(struct header_toc)*
- MK_KNOWN_HEADERS);
- pthread_setspecific(mk_cache_header_toc, (void *) cache_header_toc);
+ struct request_idx *cache_request_idx;
+ struct mk_iov *cache_iov_log;
+ struct mk_iov *cache_iov_header;
+ struct header_toc *cache_header_toc;
+
+ /* client request index */
+ cache_request_idx = mk_mem_malloc(sizeof(struct request_idx));
+ cache_request_idx->first = NULL;
+ cache_request_idx->last = NULL;
+ pthread_setspecific(request_index, (void *) cache_request_idx);
+
+ /* Cache iov log struct */
+ cache_iov_log = mk_iov_create(15, 0);
+ pthread_setspecific(mk_cache_iov_log, (void *) cache_iov_log);
+
+ /* Cache iov header struct */
+ cache_iov_header = mk_iov_create(45, 0);
+ pthread_setspecific(mk_cache_iov_header, (void *) cache_iov_header);
+
+ /* Cache header toc, monkey just search for MK_KNOWN_HEADERS
+ * in request
+ */
+ cache_header_toc = mk_mem_malloc_z(sizeof(struct header_toc) *
+ MK_KNOWN_HEADERS);
+ pthread_setspecific(mk_cache_header_toc, (void *) cache_header_toc);
}
/* iso_8859_15 (man iso_8859_15) */
int get_char(int code)
{
- switch(code){
- /* Perl is great :) */
- case 160: return ' ';
- case 161: return '¡';
- case 162: return '¢';
- case 163: return '£';
- case 164: return '¤';
- case 165: return '¥';
- case 166: return '¦';
- case 167: return '§';
- case 168: return '¨';
- case 169: return '©';
- case 170: return 'ª';
- case 171: return '«';
- case 172: return '¬';
- case 173: return '';
- case 174: return '®';
- case 175: return '¯';
- case 176: return '°';
- case 177: return '±';
- case 178: return '²';
- case 179: return '³';
- case 180: return '´';
- case 181: return 'µ';
- case 182: return '¶';
- case 183: return '·';
- case 184: return '¸';
- case 185: return '¹';
- case 186: return 'º';
- case 187: return '»';
- case 188: return '¼';
- case 189: return '½';
- case 190: return '¾';
- case 191: return '¿';
- case 192: return 'À';
- case 193: return 'Á';
- case 194: return 'Â';
- case 195: return 'Ã';
- case 196: return 'Ä';
- case 197: return 'Å';
- case 198: return 'Æ';
- case 199: return 'Ç';
- case 200: return 'È';
- case 201: return 'É';
- case 202: return 'Ê';
- case 203: return 'Ë';
- case 204: return 'Ì';
- case 205: return 'Í';
- case 206: return 'Î';
- case 207: return 'Ï';
- case 208: return 'Ð';
- case 209: return 'Ñ';
- case 210: return 'Ò';
- case 211: return 'Ó';
- case 212: return 'Ô';
- case 213: return 'Õ';
- case 214: return 'Ö';
- case 215: return '×';
- case 216: return 'Ø';
- case 217: return 'Ù';
- case 218: return 'Ú';
- case 219: return 'Û';
- case 220: return 'Ü';
- case 221: return 'Ý';
- case 222: return 'Þ';
- case 223: return 'ß';
- case 224: return 'à';
- case 225: return 'á';
- case 226: return 'â';
- case 227: return 'ã';
- case 228: return 'ä';
- case 229: return 'å';
- case 230: return 'æ';
- case 231: return 'ç';
- case 232: return 'è';
- case 233: return 'é';
- case 234: return 'ê';
- case 235: return 'ë';
- case 236: return 'ì';
- case 237: return 'í';
- case 238: return 'î';
- case 239: return 'ï';
- case 240: return 'ð';
- case 241: return 'ñ';
- case 242: return 'ò';
- case 243: return 'ó';
- case 244: return 'ô';
- case 245: return 'õ';
- case 246: return 'ö';
- case 247: return '÷';
- case 248: return 'ø';
- case 249: return 'ù';
- case 250: return 'ú';
- case 251: return 'û';
- case 252: return 'ü';
- case 253: return 'ý';
- case 254: return 'þ';
- case 255: return 'ÿ';
- }
- return -1;
+ switch (code) {
+ /* Perl is great :) */
+ case 160:
+ return ' ';
+ case 161:
+ return '¡';
+ case 162:
+ return '¢';
+ case 163:
+ return '£';
+ case 164:
+ return '¤';
+ case 165:
+ return '¥';
+ case 166:
+ return '¦';
+ case 167:
+ return '§';
+ case 168:
+ return '¨';
+ case 169:
+ return '©';
+ case 170:
+ return 'ª';
+ case 171:
+ return '«';
+ case 172:
+ return '¬';
+ case 173:
+ return '';
+ case 174:
+ return '®';
+ case 175:
+ return '¯';
+ case 176:
+ return '°';
+ case 177:
+ return '±';
+ case 178:
+ return '²';
+ case 179:
+ return '³';
+ case 180:
+ return '´';
+ case 181:
+ return 'µ';
+ case 182:
+ return '¶';
+ case 183:
+ return '·';
+ case 184:
+ return '¸';
+ case 185:
+ return '¹';
+ case 186:
+ return 'º';
+ case 187:
+ return '»';
+ case 188:
+ return '¼';
+ case 189:
+ return '½';
+ case 190:
+ return '¾';
+ case 191:
+ return '¿';
+ case 192:
+ return 'À';
+ case 193:
+ return 'Á';
+ case 194:
+ return 'Â';
+ case 195:
+ return 'Ã';
+ case 196:
+ return 'Ä';
+ case 197:
+ return 'Å';
+ case 198:
+ return 'Æ';
+ case 199:
+ return 'Ç';
+ case 200:
+ return 'È';
+ case 201:
+ return 'É';
+ case 202:
+ return 'Ê';
+ case 203:
+ return 'Ë';
+ case 204:
+ return 'Ì';
+ case 205:
+ return 'Í';
+ case 206:
+ return 'Î';
+ case 207:
+ return 'Ï';
+ case 208:
+ return 'Ð';
+ case 209:
+ return 'Ñ';
+ case 210:
+ return 'Ò';
+ case 211:
+ return 'Ó';
+ case 212:
+ return 'Ô';
+ case 213:
+ return 'Õ';
+ case 214:
+ return 'Ö';
+ case 215:
+ return '×';
+ case 216:
+ return 'Ø';
+ case 217:
+ return 'Ù';
+ case 218:
+ return 'Ú';
+ case 219:
+ return 'Û';
+ case 220:
+ return 'Ü';
+ case 221:
+ return 'Ý';
+ case 222:
+ return 'Þ';
+ case 223:
+ return 'ß';
+ case 224:
+ return 'à';
+ case 225:
+ return 'á';
+ case 226:
+ return 'â';
+ case 227:
+ return 'ã';
+ case 228:
+ return 'ä';
+ case 229:
+ return 'å';
+ case 230:
+ return 'æ';
+ case 231:
+ return 'ç';
+ case 232:
+ return 'è';
+ case 233:
+ return 'é';
+ case 234:
+ return 'ê';
+ case 235:
+ return 'ë';
+ case 236:
+ return 'ì';
+ case 237:
+ return 'í';
+ case 238:
+ return 'î';
+ case 239:
+ return 'ï';
+ case 240:
+ return 'ð';
+ case 241:
+ return 'ñ';
+ case 242:
+ return 'ò';
+ case 243:
+ return 'ó';
+ case 244:
+ return 'ô';
+ case 245:
+ return 'õ';
+ case 246:
+ return 'ö';
+ case 247:
+ return '÷';
+ case 248:
+ return 'ø';
+ case 249:
+ return 'ù';
+ case 250:
+ return 'ú';
+ case 251:
+ return 'û';
+ case 252:
+ return 'ü';
+ case 253:
+ return 'ý';
+ case 254:
+ return 'þ';
+ case 255:
+ return 'ÿ';
+ }
+ return -1;
}
/* Transorma numeracion Hexa a base decimal */
int hex2int(char *pChars)
{
- int Hi;
- int Lo;
- int Result;
+ int Hi;
+ int Lo;
+ int Result;
- Hi=pChars[0];
- if ('0'<=Hi&&Hi<='9') {
- Hi-='0';
- } else if ('a'<=Hi&&Hi<='f') {
- Hi-=('a'-10);
- } else if ('A'<=Hi&&Hi<='F') {
- Hi-=('A'-10);
- }
- Lo = pChars[1];
- if ('0'<=Lo&&Lo<='9') {
- Lo-='0';
- } else if ('a'<=Lo&&Lo<='f') {
- Lo-=('a'-10);
- } else if ('A'<=Lo&&Lo<='F') {
- Lo-=('A'-10);
- }
- Result=Lo+(16*Hi);
+ Hi = pChars[0];
+ if ('0' <= Hi && Hi <= '9') {
+ Hi -= '0';
+ }
+ else if ('a' <= Hi && Hi <= 'f') {
+ Hi -= ('a' - 10);
+ }
+ else if ('A' <= Hi && Hi <= 'F') {
+ Hi -= ('A' - 10);
+ }
+ Lo = pChars[1];
+ if ('0' <= Lo && Lo <= '9') {
+ Lo -= '0';
+ }
+ else if ('a' <= Lo && Lo <= 'f') {
+ Lo -= ('a' - 10);
+ }
+ else if ('A' <= Lo && Lo <= 'F') {
+ Lo -= ('A' - 10);
+ }
+ Result = Lo + (16 * Hi);
- return (Result);
+ return (Result);
}
void mk_clock_log_set_time()
{
- time_t utime;
+ time_t utime;
- if(!log_current_time.data)
- {
- log_current_time.data = mk_mem_malloc_z(30);
- log_current_time.len = 28;
- }
+ if (!log_current_time.data) {
+ log_current_time.data = mk_mem_malloc_z(30);
+ log_current_time.len = 28;
+ }
- if ((utime = time(NULL)) == -1)
- {
- return;
- }
+ if ((utime = time(NULL)) == -1) {
+ return;
+ }
- log_current_utime = utime;
- strftime(log_current_time.data, 30, "[%d/%b/%G %T %z]",
- (struct tm *)localtime((time_t *) &utime));
+ log_current_utime = utime;
+ strftime(log_current_time.data, 30, "[%d/%b/%G %T %z]",
+ (struct tm *) localtime((time_t *) & utime));
}
void mk_clock_header_set_time()
{
- int n, len = 30;
- time_t date;
- struct tm *gmt_tm;
+ int n, len = 30;
+ time_t date;
+ struct tm *gmt_tm;
- if(!header_current_time.data)
- {
- header_current_time.data = mk_mem_malloc_z(len);
- header_current_time.len = len - 1;
- }
+ if (!header_current_time.data) {
+ header_current_time.data = mk_mem_malloc_z(len);
+ header_current_time.len = len - 1;
+ }
- date = time(NULL);
- gmt_tm = (struct tm *) gmtime(&date);
- n = strftime(header_current_time.data, len, GMT_DATEFORMAT, gmt_tm);
+ date = time(NULL);
+ gmt_tm = (struct tm *) gmtime(&date);
+ n = strftime(header_current_time.data, len, GMT_DATEFORMAT, gmt_tm);
}
void *mk_clock_worker_init(void *args)
{
- while(1)
- {
- mk_clock_log_set_time();
- mk_clock_header_set_time();
- sleep(1);
- }
+ while (1) {
+ mk_clock_log_set_time();
+ mk_clock_header_set_time();
+ sleep(1);
+ }
}
struct mk_config *mk_config_create(char *path)
{
- FILE *f;
- int len;
- char buf[255];
- char *key=0, *val=0, *last=0;
- struct mk_config *cnf=0, *new, *p;
-
- if((f=fopen(path, "r")) == NULL) {
- fprintf(stderr, "\nConfig Error: I can't open %s file\n\n", path);
- exit(1);
- }
-
- /* looking for configuration directives */
- while(fgets(buf, 255, f)){
- len = strlen(buf);
- if(buf[len-1] == '\n') {
- buf[--len] = 0;
- if(len && buf[len-1] == '\r')
- buf[--len] = 0;
- }
-
- if(!buf[0] || buf[0] == '#')
- continue;
-
- key = strtok_r(buf, "\"\t ", &last);
- val = strtok_r(NULL, "\n", &last);
-
- if(!key || !val){
- continue;
- }
+ FILE *f;
+ int len;
+ char buf[255];
+ char *key = 0, *val = 0, *last = 0;
+ struct mk_config *cnf = 0, *new, *p;
+
+ if ((f = fopen(path, "r")) == NULL) {
+ fprintf(stderr, "\nConfig Error: I can't open %s file\n\n", path);
+ exit(1);
+ }
- /* Allow new entry found */
- new = mk_mem_malloc(sizeof(struct mk_config));
- new->key = mk_string_dup(key);
- new->val = mk_string_dup(val);
- new->next = NULL;
+ /* looking for configuration directives */
+ while (fgets(buf, 255, f)) {
+ len = strlen(buf);
+ if (buf[len - 1] == '\n') {
+ buf[--len] = 0;
+ if (len && buf[len - 1] == '\r')
+ buf[--len] = 0;
+ }
- /* Link to main list */
- if(!cnf){
- cnf = new;
- }
- else{
- p = cnf;
- while(p->next){
- p = p->next;
- }
- p->next = new;
- }
+ if (!buf[0] || buf[0] == '#')
+ continue;
+
+ key = strtok_r(buf, "\"\t ", &last);
+ val = strtok_r(NULL, "\n", &last);
+
+ if (!key || !val) {
+ continue;
}
- fclose(f);
- return cnf;
+ /* Allow new entry found */
+ new = mk_mem_malloc(sizeof(struct mk_config));
+ new->key = mk_string_dup(key);
+ new->val = mk_string_dup(val);
+ new->next = NULL;
+
+ /* Link to main list */
+ if (!cnf) {
+ cnf = new;
+ }
+ else {
+ p = cnf;
+ while (p->next) {
+ p = p->next;
+ }
+ p->next = new;
+ }
+ }
+
+ fclose(f);
+ return cnf;
}
void mk_config_free(struct mk_config *cnf)
{
- struct mk_config *prev=0, *target;
+ struct mk_config *prev = 0, *target;
- target = cnf;
- while(target){
- while(target->next){
- prev = target;
- target = target->next;
- }
+ target = cnf;
+ while (target) {
+ while (target->next) {
+ prev = target;
+ target = target->next;
+ }
- mk_mem_free(target);
+ mk_mem_free(target);
- if(target == cnf){
- return;
- }
- prev->next = NULL;
- target = cnf;
+ if (target == cnf) {
+ return;
}
+ prev->next = NULL;
+ target = cnf;
+ }
}
void *mk_config_getval(struct mk_config *cnf, char *key, int mode)
{
- int on, off;
- struct mk_config *p;
-
- p = cnf;
- while(p){
- if(strcasecmp(p->key, key) == 0){
- switch(mode){
- case MK_CONFIG_VAL_STR:
- return (void *) p->val;
- case MK_CONFIG_VAL_NUM:
- return (void *) atoi(p->val);
- case MK_CONFIG_VAL_BOOL:
- on = strcasecmp(p->val, VALUE_ON);
- off = strcasecmp(p->val,VALUE_OFF);
-
- if(on!=0 && off!=0){
- return (void *) -1;
- }
- else if(on>=0){
- return (void *) VAR_ON;
- }
- else{
- return (void *) VAR_OFF;
- }
- case MK_CONFIG_VAL_LIST:
- return mk_string_split_line(p->val);
- }
+ int on, off;
+ struct mk_config *p;
+
+ p = cnf;
+ while (p) {
+ if (strcasecmp(p->key, key) == 0) {
+ switch (mode) {
+ case MK_CONFIG_VAL_STR:
+ return (void *) p->val;
+ case MK_CONFIG_VAL_NUM:
+ return (void *) atoi(p->val);
+ case MK_CONFIG_VAL_BOOL:
+ on = strcasecmp(p->val, VALUE_ON);
+ off = strcasecmp(p->val, VALUE_OFF);
+
+ if (on != 0 && off != 0) {
+ return (void *) -1;
}
- else{
- p = p->next;
+ else if (on >= 0) {
+ return (void *) VAR_ON;
}
+ else {
+ return (void *) VAR_OFF;
+ }
+ case MK_CONFIG_VAL_LIST:
+ return mk_string_split_line(p->val);
+ }
}
- return NULL;
+ else {
+ p = p->next;
+ }
+ }
+ return NULL;
}
/* Read configuration files */
void mk_config_read_files(char *path_conf, char *file_conf)
{
- unsigned long len;
- char *path=0;
- struct stat checkdir;
- struct mk_config *cnf;
- struct mk_string_line *line, *line_val;
-
- config->serverconf = mk_string_dup(path_conf);
- config->workers = MK_WORKERS_DEFAULT;
-
- if(stat(config->serverconf, &checkdir)==-1){
- fprintf(stderr, "ERROR: Invalid path to configuration files.");
- exit(1);
- }
-
- m_build_buffer(&path, &len, "%s/%s", path_conf, file_conf);
-
- cnf = mk_config_create(path);
-
- /* Connection port */
- config->serverport = (int) mk_config_getval(cnf,
- "Port",
- MK_CONFIG_VAL_NUM);
- if(!config->serverport>=1 && !config->serverport<=65535){
- mk_config_print_error_msg("Port", path);
- }
-
- /* Number of thread workers */
- config->workers = (int) mk_config_getval(cnf,
- "Workers",
- MK_CONFIG_VAL_NUM);
- if(config->maxclients < 1){
- mk_config_print_error_msg("Workers", path);
- }
+ unsigned long len;
+ char *path = 0;
+ struct stat checkdir;
+ struct mk_config *cnf;
+ struct mk_string_line *line, *line_val;
- /* Timeout */
- config->timeout = (int) mk_config_getval(cnf,
- "Timeout",
- MK_CONFIG_VAL_NUM);
- if(config->timeout < 1){
- mk_config_print_error_msg("Timeout", path);
- }
-
- /* KeepAlive */
- config->keep_alive = (int) mk_config_getval(cnf,
- "KeepAlive",
- MK_CONFIG_VAL_BOOL);
- if(config->keep_alive == VAR_ERR){
- mk_config_print_error_msg("KeepAlive", path);
- }
+ config->serverconf = mk_string_dup(path_conf);
+ config->workers = MK_WORKERS_DEFAULT;
- /* MaxKeepAliveRequest */
- config->max_keep_alive_request = (int) mk_config_getval(cnf,
- "MaxKeepAliveRequest",
- MK_CONFIG_VAL_NUM);
- if(config->max_keep_alive_request == 0){
- mk_config_print_error_msg("MaxKeepAliveRequest", path);
- }
-
- /* KeepAliveTimeout */
- config->keep_alive_timeout = (int) mk_config_getval(cnf,
- "KeepAliveTimeout",
- MK_CONFIG_VAL_NUM);
- if(config->keep_alive_timeout == 0){
- mk_config_print_error_msg("KeepAliveTimeout", path);
- }
-
- /* Pid File */
- config->pid_file_path = mk_config_getval(cnf,
- "PidFile",
- MK_CONFIG_VAL_STR);
-
- /* Home user's directory /~ */
- config->user_dir = mk_config_getval(cnf, "UserDir", MK_CONFIG_VAL_STR);
-
- /* Index files */
- line_val = line = mk_config_getval(cnf, "Indexfile", MK_CONFIG_VAL_LIST);
- while(line_val!=NULL) {
- mk_config_add_index(line_val->val);
- line_val = line_val->next;
- }
+ if (stat(config->serverconf, &checkdir) == -1) {
+ fprintf(stderr, "ERROR: Invalid path to configuration files.");
+ exit(1);
+ }
- /* HideVersion Variable */
- config->hideversion = (int) mk_config_getval(cnf,
- "HideVersion",
- MK_CONFIG_VAL_BOOL);
- if(config->hideversion == VAR_ERR){
- mk_config_print_error_msg("HideVersion", path);
- }
-
- /* User Variable */
- config->user = mk_config_getval(cnf, "User", MK_CONFIG_VAL_STR);
-
- /* Resume */
- config->resume = (int) mk_config_getval(cnf,
- "Resume",
+ m_build_buffer(&path, &len, "%s/%s", path_conf, file_conf);
+
+ cnf = mk_config_create(path);
+
+ /* Connection port */
+ config->serverport = (int) mk_config_getval(cnf,
+ "Port", MK_CONFIG_VAL_NUM);
+ if (!config->serverport >= 1 && !config->serverport <= 65535) {
+ mk_config_print_error_msg("Port", path);
+ }
+
+ /* Number of thread workers */
+ config->workers = (int) mk_config_getval(cnf,
+ "Workers", MK_CONFIG_VAL_NUM);
+ if (config->maxclients < 1) {
+ mk_config_print_error_msg("Workers", path);
+ }
+
+ /* Timeout */
+ config->timeout = (int) mk_config_getval(cnf,
+ "Timeout", MK_CONFIG_VAL_NUM);
+ if (config->timeout < 1) {
+ mk_config_print_error_msg("Timeout", path);
+ }
+
+ /* KeepAlive */
+ config->keep_alive = (int) mk_config_getval(cnf,
+ "KeepAlive",
MK_CONFIG_VAL_BOOL);
- if(config->resume == VAR_ERR){
- mk_config_print_error_msg("Resume", path);
- }
-
- /* Symbolic Links */
- config->symlink = (int) mk_config_getval(cnf,
- "SymLink",
+ if (config->keep_alive == VAR_ERR) {
+ mk_config_print_error_msg("KeepAlive", path);
+ }
+
+ /* MaxKeepAliveRequest */
+ config->max_keep_alive_request = (int) mk_config_getval(cnf,
+ "MaxKeepAliveRequest",
+ MK_CONFIG_VAL_NUM);
+ if (config->max_keep_alive_request == 0) {
+ mk_config_print_error_msg("MaxKeepAliveRequest", path);
+ }
+
+ /* KeepAliveTimeout */
+ config->keep_alive_timeout = (int) mk_config_getval(cnf,
+ "KeepAliveTimeout",
+ MK_CONFIG_VAL_NUM);
+ if (config->keep_alive_timeout == 0) {
+ mk_config_print_error_msg("KeepAliveTimeout", path);
+ }
+
+ /* Pid File */
+ config->pid_file_path = mk_config_getval(cnf,
+ "PidFile", MK_CONFIG_VAL_STR);
+
+ /* Home user's directory /~ */
+ config->user_dir = mk_config_getval(cnf, "UserDir", MK_CONFIG_VAL_STR);
+
+ /* Index files */
+ line_val = line = mk_config_getval(cnf, "Indexfile", MK_CONFIG_VAL_LIST);
+ while (line_val != NULL) {
+ mk_config_add_index(line_val->val);
+ line_val = line_val->next;
+ }
+
+ /* HideVersion Variable */
+ config->hideversion = (int) mk_config_getval(cnf,
+ "HideVersion",
MK_CONFIG_VAL_BOOL);
- if(config->symlink == VAR_ERR){
- mk_config_print_error_msg("SymLink", path);
- }
-
- mk_mem_free(path);
- mk_config_free(cnf);
- mk_config_read_hosts(path_conf);
+ if (config->hideversion == VAR_ERR) {
+ mk_config_print_error_msg("HideVersion", path);
+ }
+
+ /* User Variable */
+ config->user = mk_config_getval(cnf, "User", MK_CONFIG_VAL_STR);
+
+ /* Resume */
+ config->resume = (int) mk_config_getval(cnf,
+ "Resume", MK_CONFIG_VAL_BOOL);
+ if (config->resume == VAR_ERR) {
+ mk_config_print_error_msg("Resume", path);
+ }
+
+ /* Symbolic Links */
+ config->symlink = (int) mk_config_getval(cnf,
+ "SymLink", MK_CONFIG_VAL_BOOL);
+ if (config->symlink == VAR_ERR) {
+ mk_config_print_error_msg("SymLink", path);
+ }
+
+ mk_mem_free(path);
+ mk_config_free(cnf);
+ mk_config_read_hosts(path_conf);
}
void mk_config_read_hosts(char *path)
{
DIR *dir;
unsigned long len;
- char *buf=0;
+ char *buf = 0;
char *file;
- struct host *p_host, *new_host; /* debug */
+ struct host *p_host, *new_host; /* debug */
struct dirent *ent;
m_build_buffer(&buf, &len, "%s/sites/default", path);
config->nhosts++;
mk_mem_free(buf);
- if(!config->hosts)
- {
+ if (!config->hosts) {
printf("\nError parsing main configuration file 'default'\n");
exit(1);
}
p_host = config->hosts;
/* Reading content */
- while ((ent = readdir(dir)) != NULL)
- {
- if (strcmp((char *) ent->d_name, "." ) == 0) continue;
- if (strcmp((char *) ent->d_name, ".." ) == 0) continue;
- if (strcasecmp((char *) ent->d_name, "default" ) == 0) continue;
+ while ((ent = readdir(dir)) != NULL) {
+ if (strcmp((char *) ent->d_name, ".") == 0)
+ continue;
+ if (strcmp((char *) ent->d_name, "..") == 0)
+ continue;
+ if (strcasecmp((char *) ent->d_name, "default") == 0)
+ continue;
m_build_buffer(&file, &len, "%s/sites/%s", path, ent->d_name);
new_host = (struct host *) mk_config_get_host(file);
mk_mem_free(file);
- if(!new_host)
- {
+ if (!new_host) {
continue;
}
- else{
+ else {
p_host->next = new_host;
p_host = new_host;
- config->nhosts++;
+ config->nhosts++;
}
}
closedir(dir);
/*
- h = config->hosts;
- while(h)
- {
- printf("*** HOST ***\n");
- printf(" [servername]\t\t%s\n", h->servername);
- printf(" [documentroot]\t\t%s\n", h->documentroot);
- printf(" [conf file]\t\t%s\n", h->file);
- printf(" [access log]\t\t%s\n", h->access_log_path);
- printf(" [error log]\t\t%s\n", h->error_log_path);
- printf(" [script alias]\t\t%s %s\n", h->scriptalias[0], h->scriptalias[1]);
- printf(" [get dir]\t\t%i\n", h->getdir);
- printf(" [header file]\t\t%s\n", h->header_file);
- printf(" [footer file]\t\t%s\n\n", h->footer_file);
-
- h = h->next;
- }
- fflush(stdout);
- */
+ h = config->hosts;
+ while(h)
+ {
+ printf("*** HOST ***\n");
+ printf(" [servername]\t\t%s\n", h->servername);
+ printf(" [documentroot]\t\t%s\n", h->documentroot);
+ printf(" [conf file]\t\t%s\n", h->file);
+ printf(" [access log]\t\t%s\n", h->access_log_path);
+ printf(" [error log]\t\t%s\n", h->error_log_path);
+ printf(" [script alias]\t\t%s %s\n", h->scriptalias[0], h->scriptalias[1]);
+ printf(" [get dir]\t\t%i\n", h->getdir);
+ printf(" [header file]\t\t%s\n", h->header_file);
+ printf(" [footer file]\t\t%s\n\n", h->footer_file);
+
+ h = h->next;
+ }
+ fflush(stdout);
+ */
}
struct host *mk_config_get_host(char *path)
{
- unsigned long len=0;
- struct stat checkdir;
- struct host *host;
- struct mk_config *cnf;
-
- cnf = mk_config_create(path);
-
- host = mk_mem_malloc_z(sizeof(struct host));
- host->servername = 0;
- host->file = mk_string_dup(path);
-
- host->servername = mk_config_getval(cnf, "Servername", MK_CONFIG_VAL_STR);
- host->documentroot.data = mk_config_getval(cnf,
- "DocumentRoot",
- MK_CONFIG_VAL_STR);
- host->documentroot.len = strlen(host->documentroot.data);
- if(stat(host->documentroot.data, &checkdir)==-1) {
- fprintf(stderr,
- "ERROR: Invalid path to Server_root in %s\n\n",
- path);
- exit(1);
- }
- else if(!(checkdir.st_mode & S_IFDIR)) {
- fprintf(stderr,
- "ERROR: DocumentRoot variable in %s has an invalid directory path\n\n",
- path);
- exit(1);
- }
+ unsigned long len = 0;
+ struct stat checkdir;
+ struct host *host;
+ struct mk_config *cnf;
+
+ cnf = mk_config_create(path);
+
+ host = mk_mem_malloc_z(sizeof(struct host));
+ host->servername = 0;
+ host->file = mk_string_dup(path);
+
+ host->servername = mk_config_getval(cnf, "Servername", MK_CONFIG_VAL_STR);
+ host->documentroot.data = mk_config_getval(cnf,
+ "DocumentRoot",
+ MK_CONFIG_VAL_STR);
+ host->documentroot.len = strlen(host->documentroot.data);
+ if (stat(host->documentroot.data, &checkdir) == -1) {
+ fprintf(stderr, "ERROR: Invalid path to Server_root in %s\n\n", path);
+ exit(1);
+ }
+ else if (!(checkdir.st_mode & S_IFDIR)) {
+ fprintf(stderr,
+ "ERROR: DocumentRoot variable in %s has an invalid directory path\n\n",
+ path);
+ exit(1);
+ }
- /* Access log */
- host->access_log_path = mk_config_getval(cnf,
- "AccessLog",
- MK_CONFIG_VAL_STR);
- /* Error log */
- host->error_log_path = mk_config_getval(cnf,
- "ErrorLog",
- MK_CONFIG_VAL_STR);
-
- /* Get directory */
- host->getdir = (int) mk_config_getval(cnf, "GetDir", MK_CONFIG_VAL_BOOL);
- if(host->getdir == VAR_ERR){
- mk_config_print_error_msg("GetDir", path);
- }
+ /* Access log */
+ host->access_log_path = mk_config_getval(cnf,
+ "AccessLog", MK_CONFIG_VAL_STR);
+ /* Error log */
+ host->error_log_path = mk_config_getval(cnf,
+ "ErrorLog", MK_CONFIG_VAL_STR);
+
+ /* Get directory */
+ host->getdir = (int) mk_config_getval(cnf, "GetDir", MK_CONFIG_VAL_BOOL);
+ if (host->getdir == VAR_ERR) {
+ mk_config_print_error_msg("GetDir", path);
+ }
- if(!host->servername){
- mk_config_free(cnf);
- return NULL;
- }
+ if (!host->servername) {
+ mk_config_free(cnf);
+ return NULL;
+ }
- /* Server Signature */
- if(config->hideversion==VAR_OFF){
- m_build_buffer(&host->host_signature, &len,
- "Monkey/%s Server (Host: %s, Port: %i)",
- VERSION, host->servername, config->serverport);
- }
- else{
- m_build_buffer(&host->host_signature, &len,
- "Monkey Server (Host: %s, Port: %i)",
- host->servername, config->serverport);
- }
- m_build_buffer(&host->header_host_signature.data,
- &host->header_host_signature.len,
- "Server: %s", host->host_signature);
-
- if(pipe(host->log_access)<0){
- perror("pipe");
- }
+ /* Server Signature */
+ if (config->hideversion == VAR_OFF) {
+ m_build_buffer(&host->host_signature, &len,
+ "Monkey/%s Server (Host: %s, Port: %i)",
+ VERSION, host->servername, config->serverport);
+ }
+ else {
+ m_build_buffer(&host->host_signature, &len,
+ "Monkey Server (Host: %s, Port: %i)",
+ host->servername, config->serverport);
+ }
+ m_build_buffer(&host->header_host_signature.data,
+ &host->header_host_signature.len,
+ "Server: %s", host->host_signature);
- if(pipe(host->log_error)<0){
- perror("pipe");
- }
+ if (pipe(host->log_access) < 0) {
+ perror("pipe");
+ }
- fcntl(host->log_access[1], F_SETFL, O_NONBLOCK);
- fcntl(host->log_error[1], F_SETFL, O_NONBLOCK);
+ if (pipe(host->log_error) < 0) {
+ perror("pipe");
+ }
- host->next = NULL;
- mk_config_free(cnf);
- return host;
+ fcntl(host->log_access[1], F_SETFL, O_NONBLOCK);
+ fcntl(host->log_error[1], F_SETFL, O_NONBLOCK);
+
+ host->next = NULL;
+ mk_config_free(cnf);
+ return host;
}
/* Imprime error de configuracion y cierra */
void mk_config_print_error_msg(char *variable, char *path)
{
- fprintf(stderr, "\nError: %s variable in %s has an invalid value.\n", variable, path);
- fflush(stderr);
- exit(1);
+ fprintf(stderr, "\nError: %s variable in %s has an invalid value.\n",
+ variable, path);
+ fflush(stderr);
+ exit(1);
}
/* Agrega distintos index.xxx */
void mk_config_add_index(char *indexname)
{
- struct indexfile *new_index=0, *aux_index;
-
- new_index = (struct indexfile *) malloc(sizeof(struct indexfile));
- strncpy(new_index->indexname,indexname,MAX_INDEX_NOMBRE - 1);
- new_index->indexname[MAX_INDEX_NOMBRE - 1]='\0';
- new_index->next=NULL;
-
- if(first_index==NULL) {
- first_index=new_index;
- }
- else {
- aux_index=first_index;
- while(aux_index->next!=NULL)
- aux_index=aux_index->next;
- aux_index->next=new_index;
- }
+ struct indexfile *new_index = 0, *aux_index;
+
+ new_index = (struct indexfile *) malloc(sizeof(struct indexfile));
+ strncpy(new_index->indexname, indexname, MAX_INDEX_NOMBRE - 1);
+ new_index->indexname[MAX_INDEX_NOMBRE - 1] = '\0';
+ new_index->next = NULL;
+
+ if (first_index == NULL) {
+ first_index = new_index;
+ }
+ else {
+ aux_index = first_index;
+ while (aux_index->next != NULL)
+ aux_index = aux_index->next;
+ aux_index->next = new_index;
+ }
}
void mk_config_set_init_values(void)
{
- /* Valores iniciales */
- config->timeout=15;
- config->hideversion=VAR_OFF;
- config->keep_alive=VAR_ON;
- config->keep_alive_timeout=15;
- config->max_keep_alive_request=50;
- config->maxclients=150;
- config->max_ip = 15;
- config->resume=VAR_ON;
- config->standard_port=80;
- config->serverport=2001;
- config->symlink=VAR_OFF;
- config->nhosts = 0;
- config->user = NULL;
- config->open_flags = O_RDONLY | O_NONBLOCK;
-
- /* Plugins */
- config->plugins = mk_mem_malloc_z(sizeof(struct plugin_stages));
+ /* Valores iniciales */
+ config->timeout = 15;
+ config->hideversion = VAR_OFF;
+ config->keep_alive = VAR_ON;
+ config->keep_alive_timeout = 15;
+ config->max_keep_alive_request = 50;
+ config->maxclients = 150;
+ config->max_ip = 15;
+ config->resume = VAR_ON;
+ config->standard_port = 80;
+ config->serverport = 2001;
+ config->symlink = VAR_OFF;
+ config->nhosts = 0;
+ config->user = NULL;
+ config->open_flags = O_RDONLY | O_NONBLOCK;
+
+ /* Plugins */
+ config->plugins = mk_mem_malloc_z(sizeof(struct plugin_stages));
}
/* read main configuration from monkey.conf */
void mk_config_start_configure(void)
{
- unsigned long len;
+ unsigned long len;
- mk_config_set_init_values();
- mk_config_read_files(config->file_config, M_DEFAULT_CONFIG_FILE);
+ mk_config_set_init_values();
+ mk_config_read_files(config->file_config, M_DEFAULT_CONFIG_FILE);
- /* if not index names defined, set default */
- if(first_index==NULL){
- mk_config_add_index("index.html");
- }
+ /* if not index names defined, set default */
+ if (first_index == NULL) {
+ mk_config_add_index("index.html");
+ }
- /* Load mimes */
- mk_mimetype_read_config();
-
- /* Basic server information */
- if(config->hideversion==VAR_OFF)
- {
- m_build_buffer(&config->server_software.data,
- &len, "Monkey/%s (%s)",VERSION,OS);
- config->server_software.len = len;
- }
- else
- {
- m_build_buffer(&config->server_software.data, &len,
- "Monkey Server");
- config->server_software.len = len;
- }
+ /* Load mimes */
+ mk_mimetype_read_config();
+
+ /* Basic server information */
+ if (config->hideversion == VAR_OFF) {
+ m_build_buffer(&config->server_software.data,
+ &len, "Monkey/%s (%s)", VERSION, OS);
+ config->server_software.len = len;
+ }
+ else {
+ m_build_buffer(&config->server_software.data, &len, "Monkey Server");
+ config->server_software.len = len;
+ }
}
struct host *mk_config_host_find(mk_pointer host)
{
- struct host *aux_host;
-
- aux_host = config->hosts;
-
- while(aux_host){
- if(strncasecmp(aux_host->servername, host.data, host.len)==0)
- break;
- else
- aux_host=aux_host->next;
- }
-
- return aux_host;
+ struct host *aux_host;
+
+ aux_host = config->hosts;
+
+ while (aux_host) {
+ if (strncasecmp(aux_host->servername, host.data, host.len) == 0)
+ break;
+ else
+ aux_host = aux_host->next;
+ }
+
+ return aux_host;
}
void mk_config_sanity_check()
{
- /* Check O_NOATIME for current user, flag will just be used
- * if running user is allowed to.
- */
- int fd, flags=config->open_flags;
+ /* Check O_NOATIME for current user, flag will just be used
+ * if running user is allowed to.
+ */
+ int fd, flags = config->open_flags;
- flags |= O_NOATIME;
- fd = open(config->file_config, flags);
+ flags |= O_NOATIME;
+ fd = open(config->file_config, flags);
- if(fd > -1){
- config->open_flags = flags;
- close(fd);
- }
+ if (fd > -1) {
+ config->open_flags = flags;
+ close(fd);
+ }
}
int mk_conn_read(int socket)
{
- int ret;
- struct client_request *cr;
- struct sched_list_node *sched;
-
- sched = mk_sched_get_thread_conf();
-
- cr = mk_request_client_get(socket);
- if(!cr)
- {
- /* Note: Linux don't set TCP_NODELAY socket flag by default,
- * also we set the client socket on non-blocking mode
- */
- mk_socket_set_tcp_nodelay(socket);
- mk_socket_set_nonblocking(socket);
-
- cr = mk_request_client_create(socket);
-
- /* Update requests counter */
- mk_sched_update_thread_status(NULL,
- MK_SCHEDULER_ACTIVE_UP,
- MK_SCHEDULER_CLOSED_DOWN);
- }
- else{
- /* If cr struct already exists, that could means that we
- * are facing a keepalive connection, need to verify, if it
- * applies we increase the thread status for active connections
- */
- if(cr->counter_connections > 1 && cr->body_length == 0){
- mk_sched_update_thread_status(NULL,
- MK_SCHEDULER_ACTIVE_UP,
- MK_SCHEDULER_CLOSED_NONE);
- }
+ int ret;
+ struct client_request *cr;
+ struct sched_list_node *sched;
+
+ sched = mk_sched_get_thread_conf();
+
+ cr = mk_request_client_get(socket);
+ if (!cr) {
+ /* Note: Linux don't set TCP_NODELAY socket flag by default,
+ * also we set the client socket on non-blocking mode
+ */
+ mk_socket_set_tcp_nodelay(socket);
+ mk_socket_set_nonblocking(socket);
+
+ cr = mk_request_client_create(socket);
+
+ /* Update requests counter */
+ mk_sched_update_thread_status(NULL,
+ MK_SCHEDULER_ACTIVE_UP,
+ MK_SCHEDULER_CLOSED_DOWN);
+ }
+ else {
+ /* If cr struct already exists, that could means that we
+ * are facing a keepalive connection, need to verify, if it
+ * applies we increase the thread status for active connections
+ */
+ if (cr->counter_connections > 1 && cr->body_length == 0) {
+ mk_sched_update_thread_status(NULL,
+ MK_SCHEDULER_ACTIVE_UP,
+ MK_SCHEDULER_CLOSED_NONE);
}
+ }
+
+ /* Read incomming data */
+ ret = mk_handler_read(socket, cr);
- /* Read incomming data */
- ret = mk_handler_read(socket, cr);
-
- if(ret > 0){
- if(mk_http_pending_request(cr)==0){
- mk_epoll_socket_change_mode(sched->epoll_fd,
- socket,
- MK_EPOLL_WRITE);
- }
- else if(cr->body_length+1 >= MAX_REQUEST_BODY)
- {
- /* Request is incomplete and our buffer is full,
- * close connection
- */
- mk_request_client_remove(socket);
- return -1;
- }
+ if (ret > 0) {
+ if (mk_http_pending_request(cr) == 0) {
+ mk_epoll_socket_change_mode(sched->epoll_fd,
+ socket, MK_EPOLL_WRITE);
+ }
+ else if (cr->body_length + 1 >= MAX_REQUEST_BODY) {
+ /* Request is incomplete and our buffer is full,
+ * close connection
+ */
+ mk_request_client_remove(socket);
+ return -1;
}
- return ret;
+ }
+ return ret;
}
int mk_conn_write(int socket)
{
- int ret=-1, ka;
- struct client_request *cr;
- struct sched_list_node *sched;
-
- sched = mk_sched_get_thread_conf();
- mk_sched_update_conn_status(sched, socket, MK_SCHEDULER_CONN_PROCESS);
-
- /* Get node from schedule list node which contains
- * the information regarding to the current client/socket
- */
- cr = mk_request_client_get(socket);
-
- if(!cr)
- {
- return -1;
- }
-
- ret = mk_handler_write(socket, cr);
- ka = mk_http_keepalive_check(socket, cr);
-
- /* if ret < 0, means that some error
- * happened in the writer call, in the
- * other hand, 0 means a successful request
- * processed, if ret > 0 means that some data
- * still need to be send.
- */
-
- if(ret <= 0)
- {
- mk_request_free_list(cr);
-
- /* We need to ask to http_keepalive if this
- * connection can continue working or we must
- * close it.
- */
-
- mk_sched_update_thread_status(sched,
- MK_SCHEDULER_ACTIVE_DOWN,
- MK_SCHEDULER_CLOSED_UP);
-
- if(ka<0 || ret<0)
- {
- mk_request_client_remove(socket);
- return -1;
- }
- else{
- mk_request_ka_next(cr);
- mk_epoll_socket_change_mode(sched->epoll_fd,
- socket,
- MK_EPOLL_READ);
- return 0;
- }
- }
- else if(ret > 0)
- {
- return 0;
- }
-
- /* avoid to make gcc cry :_( */
- return -1;
+ int ret = -1, ka;
+ struct client_request *cr;
+ struct sched_list_node *sched;
+
+ sched = mk_sched_get_thread_conf();
+ mk_sched_update_conn_status(sched, socket, MK_SCHEDULER_CONN_PROCESS);
+
+ /* Get node from schedule list node which contains
+ * the information regarding to the current client/socket
+ */
+ cr = mk_request_client_get(socket);
+
+ if (!cr) {
+ return -1;
+ }
+
+ ret = mk_handler_write(socket, cr);
+ ka = mk_http_keepalive_check(socket, cr);
+
+ /* if ret < 0, means that some error
+ * happened in the writer call, in the
+ * other hand, 0 means a successful request
+ * processed, if ret > 0 means that some data
+ * still need to be send.
+ */
+
+ if (ret <= 0) {
+ mk_request_free_list(cr);
+
+ /* We need to ask to http_keepalive if this
+ * connection can continue working or we must
+ * close it.
+ */
+
+ mk_sched_update_thread_status(sched,
+ MK_SCHEDULER_ACTIVE_DOWN,
+ MK_SCHEDULER_CLOSED_UP);
+
+ if (ka < 0 || ret < 0) {
+ mk_request_client_remove(socket);
+ return -1;
+ }
+ else {
+ mk_request_ka_next(cr);
+ mk_epoll_socket_change_mode(sched->epoll_fd,
+ socket, MK_EPOLL_READ);
+ return 0;
+ }
+ }
+ else if (ret > 0) {
+ return 0;
+ }
+
+ /* avoid to make gcc cry :_( */
+ return -1;
}
int mk_conn_error(int socket)
{
- struct client_request *cr;
- struct sched_list_node *sched;
-
- sched = mk_sched_get_thread_conf();
- mk_sched_remove_client(NULL, socket);
- cr = mk_request_client_get(socket);
- if(cr){
- mk_request_client_remove(socket);
- }
+ struct client_request *cr;
+ struct sched_list_node *sched;
- return 0;
+ sched = mk_sched_get_thread_conf();
+ mk_sched_remove_client(NULL, socket);
+ cr = mk_request_client_get(socket);
+ if (cr) {
+ mk_request_client_remove(socket);
+ }
+
+ return 0;
}
int mk_conn_close(int socket)
{
- struct sched_list_node *sched;
+ struct sched_list_node *sched;
- sched = mk_sched_get_thread_conf();
- mk_sched_remove_client(sched, socket);
+ sched = mk_sched_get_thread_conf();
+ mk_sched_remove_client(sched, socket);
- return 0;
+ return 0;
}
int mk_conn_timeout(int socket)
{
- struct sched_list_node *sched;
+ struct sched_list_node *sched;
- sched = mk_sched_get_thread_conf();
- mk_sched_check_timeouts(sched);
+ sched = mk_sched_get_thread_conf();
+ mk_sched_check_timeouts(sched);
- return 0;
+ return 0;
}
#define MAX_EVENTS 5000
-mk_epoll_handlers *mk_epoll_set_handlers(void (*read)(int),
- void (*write)(int),
- void (*error)(int),
- void (*close)(int),
- void (*timeout)(int))
+mk_epoll_handlers *mk_epoll_set_handlers(void (*read) (int),
+ void (*write) (int),
+ void (*error) (int),
+ void (*close) (int),
+ void (*timeout) (int))
{
- mk_epoll_handlers *handler;
+ mk_epoll_handlers *handler;
- handler = malloc(sizeof(mk_epoll_handlers));
- handler->read = (void *) read;
- handler->write = (void *) write;
- handler->error = (void *) error;
- handler->close = (void *) close;
- handler->timeout = (void *) timeout;
+ handler = malloc(sizeof(mk_epoll_handlers));
+ handler->read = (void *) read;
+ handler->write = (void *) write;
+ handler->error = (void *) error;
+ handler->close = (void *) close;
+ handler->timeout = (void *) timeout;
- return handler;
+ return handler;
}
int mk_epoll_create(int max_events)
{
- int efd;
+ int efd;
- efd = epoll_create(max_events);
- if (efd == -1) {
- perror("epoll_create");
- }
+ efd = epoll_create(max_events);
+ if (efd == -1) {
+ perror("epoll_create");
+ }
- return efd;
+ return efd;
}
-void *mk_epoll_init(int efd, mk_epoll_handlers *handler, int max_events)
+void *mk_epoll_init(int efd, mk_epoll_handlers * handler, int max_events)
{
- int i, fd, ret=-1;
- int num_fds;
- int fds_timeout;
- struct epoll_event events[max_events];
- struct sched_list_node *sched;
-
- /* Get thread conf */
- sched = mk_sched_get_thread_conf();
-
- pthread_mutex_lock(&mutex_wait_register);
- pthread_mutex_unlock(&mutex_wait_register);
-
- fds_timeout = log_current_utime + config->timeout;
-
- while(1){
- num_fds = epoll_wait(efd, events,
- max_events, MK_EPOLL_WAIT_TIMEOUT);
-
- for(i=0; i<num_fds; i++) {
- fd = events[i].data.fd;
- // Case 1: Error condition
- if (events[i].events & (EPOLLHUP | EPOLLERR)) {
- (* handler->error)(fd);
- continue;
- }
-
- if(events[i].events & EPOLLIN)
- {
- ret = (* handler->read)(fd);
- }
- else if(events[i].events & EPOLLOUT)
- {
- ret = (* handler->write)(fd);
- }
-
- if(ret<0)
- {
- (* handler->close)(fd);
- }
- }
-
- /* Check timeouts and update next one */
- if(log_current_utime >= fds_timeout){
- mk_sched_check_timeouts(sched);
- fds_timeout = log_current_utime + config->timeout;
- }
+ int i, fd, ret = -1;
+ int num_fds;
+ int fds_timeout;
+ struct epoll_event events[max_events];
+ struct sched_list_node *sched;
+
+ /* Get thread conf */
+ sched = mk_sched_get_thread_conf();
+
+ pthread_mutex_lock(&mutex_wait_register);
+ pthread_mutex_unlock(&mutex_wait_register);
+
+ fds_timeout = log_current_utime + config->timeout;
+
+ while (1) {
+ num_fds = epoll_wait(efd, events, max_events, MK_EPOLL_WAIT_TIMEOUT);
+
+ for (i = 0; i < num_fds; i++) {
+ fd = events[i].data.fd;
+ // Case 1: Error condition
+ if (events[i].events & (EPOLLHUP | EPOLLERR)) {
+ (*handler->error) (fd);
+ continue;
+ }
+
+ if (events[i].events & EPOLLIN) {
+ ret = (*handler->read) (fd);
+ }
+ else if (events[i].events & EPOLLOUT) {
+ ret = (*handler->write) (fd);
+ }
+
+ if (ret < 0) {
+ (*handler->close) (fd);
+ }
}
+
+ /* Check timeouts and update next one */
+ if (log_current_utime >= fds_timeout) {
+ mk_sched_check_timeouts(sched);
+ fds_timeout = log_current_utime + config->timeout;
+ }
+ }
}
int mk_epoll_add_client(int efd, int socket, int mode)
{
- int ret;
- struct epoll_event event;
-
+ int ret;
+ struct epoll_event event;
- event.events = EPOLLIN | EPOLLERR | EPOLLHUP;
- event.data.fd = socket;
- if(mode == MK_EPOLL_BEHAVIOR_TRIGGERED)
- {
- event.events |= EPOLLET;
- }
+ event.events = EPOLLIN | EPOLLERR | EPOLLHUP;
+ event.data.fd = socket;
+ if (mode == MK_EPOLL_BEHAVIOR_TRIGGERED) {
+ event.events |= EPOLLET;
+ }
- ret = epoll_ctl(efd, EPOLL_CTL_ADD, socket, &event);
- if(ret < 0)
- {
- perror("epoll_ctl");
- }
- return ret;
+
+ ret = epoll_ctl(efd, EPOLL_CTL_ADD, socket, &event);
+ if (ret < 0) {
+ perror("epoll_ctl");
+ }
+ return ret;
}
int mk_epoll_socket_change_mode(int efd, int socket, int mode)
{
- int ret;
- struct epoll_event event;
-
- event.events = EPOLLET | EPOLLERR | EPOLLHUP;
- event.data.fd = socket;
-
- switch(mode)
- {
- case MK_EPOLL_READ:
- event.events |= EPOLLIN;
- break;
- case MK_EPOLL_WRITE:
- event.events |= EPOLLOUT;
- break;
- case MK_EPOLL_RW:
- event.events |= EPOLLIN | EPOLLOUT;
- break;
- }
-
- ret = epoll_ctl(efd, EPOLL_CTL_MOD, socket, &event);
- if(ret < 0)
- {
- perror("\nepoll_ctl");
- }
- return ret;
+ int ret;
+ struct epoll_event event;
+
+ event.events = EPOLLET | EPOLLERR | EPOLLHUP;
+ event.data.fd = socket;
+
+ switch (mode) {
+ case MK_EPOLL_READ:
+ event.events |= EPOLLIN;
+ break;
+ case MK_EPOLL_WRITE:
+ event.events |= EPOLLOUT;
+ break;
+ case MK_EPOLL_RW:
+ event.events |= EPOLLIN | EPOLLOUT;
+ break;
+ }
+
+ ret = epoll_ctl(efd, EPOLL_CTL_MOD, socket, &event);
+ if (ret < 0) {
+ perror("\nepoll_ctl");
+ }
+ return ret;
}
-
struct file_info *mk_file_get_info(char *path)
{
- struct file_info *f_info;
- struct stat f, target;
-
- /* Stat right resource */
- if(lstat(path, &f)==-1)
- {
- return NULL;
- }
-
- f_info = mk_mem_malloc(sizeof(struct file_info));
- f_info->is_link = MK_FILE_FALSE;
- f_info->is_directory = MK_FILE_FALSE;
- f_info->exec_access = MK_FILE_FALSE;
- f_info->read_access = MK_FILE_FALSE;
-
- if(S_ISLNK(f.st_mode))
- {
- f_info->is_link = MK_FILE_TRUE;
- if(stat(path, &target)==-1)
- {
- return NULL;
- }
- }
- else{
- target = f;
- }
-
- f_info->size = target.st_size;
- f_info->last_modification = target.st_mtime;
-
- if(S_ISDIR(target.st_mode))
- {
- f_info->is_directory = MK_FILE_TRUE;
- }
-
- /* Checking read access */
- if( (target.st_mode & S_IRUSR && target.st_uid == euid) ||
- (target.st_mode & S_IRGRP && target.st_gid == egid) ||
- (target.st_mode & S_IROTH)){
- f_info->read_access = MK_FILE_TRUE;
+ struct file_info *f_info;
+ struct stat f, target;
+
+ /* Stat right resource */
+ if (lstat(path, &f) == -1) {
+ return NULL;
+ }
+
+ f_info = mk_mem_malloc(sizeof(struct file_info));
+ f_info->is_link = MK_FILE_FALSE;
+ f_info->is_directory = MK_FILE_FALSE;
+ f_info->exec_access = MK_FILE_FALSE;
+ f_info->read_access = MK_FILE_FALSE;
+
+ if (S_ISLNK(f.st_mode)) {
+ f_info->is_link = MK_FILE_TRUE;
+ if (stat(path, &target) == -1) {
+ return NULL;
}
-
- /* Checking execution access */
- if( (target.st_mode & S_IXUSR && target.st_uid == euid) ||
- (target.st_mode & S_IXGRP && target.st_gid == egid) ||
- (target.st_mode & S_IXOTH))
- {
- f_info->exec_access = MK_FILE_TRUE;
-
- }
- return f_info;
+ }
+ else {
+ target = f;
+ }
+
+ f_info->size = target.st_size;
+ f_info->last_modification = target.st_mtime;
+
+ if (S_ISDIR(target.st_mode)) {
+ f_info->is_directory = MK_FILE_TRUE;
+ }
+
+ /* Checking read access */
+ if ((target.st_mode & S_IRUSR && target.st_uid == euid) ||
+ (target.st_mode & S_IRGRP && target.st_gid == egid) ||
+ (target.st_mode & S_IROTH)) {
+ f_info->read_access = MK_FILE_TRUE;
+ }
+
+ /* Checking execution access */
+ if ((target.st_mode & S_IXUSR && target.st_uid == euid) ||
+ (target.st_mode & S_IXGRP && target.st_gid == egid) ||
+ (target.st_mode & S_IXOTH)) {
+ f_info->exec_access = MK_FILE_TRUE;
+
+ }
+ return f_info;
}
/* Read file content to a memory buffer,
*/
char *mk_file_to_buffer(char *path)
{
- FILE *fp;
- char *buffer;
- long bytes;
- struct file_info *finfo;
-
- if(!(finfo = mk_file_get_info(path)))
- {
- return NULL;
- }
-
- if(!(fp = fopen(path, "r")))
- {
- return NULL;
- }
-
- buffer = calloc(finfo->size+1, sizeof(char));
- if(!buffer)
- {
- fclose(fp);
- return NULL;
- }
-
- bytes = fread(buffer, finfo->size, 1, fp);
-
- if(bytes < 1)
- {
- mk_mem_free(buffer);
- fclose(fp);
- return NULL;
- }
-
- fclose(fp);
- return (char *) buffer;
-
-}
+ FILE *fp;
+ char *buffer;
+ long bytes;
+ struct file_info *finfo;
+
+ if (!(finfo = mk_file_get_info(path))) {
+ return NULL;
+ }
+
+ if (!(fp = fopen(path, "r"))) {
+ return NULL;
+ }
+ buffer = calloc(finfo->size + 1, sizeof(char));
+ if (!buffer) {
+ fclose(fp);
+ return NULL;
+ }
+
+ bytes = fread(buffer, finfo->size, 1, fp);
+
+ if (bytes < 1) {
+ mk_mem_free(buffer);
+ fclose(fp);
+ return NULL;
+ }
+
+ fclose(fp);
+ return (char *) buffer;
+
+}
int mk_header_iov_add_entry(struct mk_iov *mk_io, mk_pointer data,
mk_pointer sep, int free)
{
- return mk_iov_add_entry(mk_io, data.data, data.len, sep, free);
+ return mk_iov_add_entry(mk_io, data.data, data.len, sep, free);
}
struct mk_iov *mk_header_iov_get()
{
- return pthread_getspecific(mk_cache_iov_header);
+ return pthread_getspecific(mk_cache_iov_header);
}
void mk_header_iov_free(struct mk_iov *iov)
{
- mk_iov_free_marked(iov);
+ mk_iov_free_marked(iov);
}
/* Send_Header , envia las cabeceras principales */
int mk_header_send(int fd, struct client_request *cr,
- struct request *sr, struct log_info *s_log)
+ struct request *sr, struct log_info *s_log)
{
- int fd_status=0;
- unsigned long len=0;
- char *buffer=0;
- struct header_values *sh;
- struct mk_iov *iov;
-
- sh = sr->headers;
-
- iov = mk_header_iov_get();
-
- /* Status Code */
- switch(sh->status){
- case M_HTTP_OK:
- mk_header_iov_add_entry(iov, mk_hr_http_ok,
- mk_iov_none,
- MK_IOV_NOT_FREE_BUF);
- break;
-
- case M_HTTP_PARTIAL:
- mk_header_iov_add_entry(iov, mk_hr_http_partial,
- mk_iov_none,
- MK_IOV_NOT_FREE_BUF);
- break;
-
- case M_REDIR_MOVED:
- s_log->status=S_LOG_OFF;
- mk_header_iov_add_entry(iov, mk_hr_redir_moved,
- mk_iov_none,
- MK_IOV_NOT_FREE_BUF);
- break;
-
- case M_REDIR_MOVED_T:
- s_log->status=S_LOG_ON;
- mk_header_iov_add_entry(iov, mk_hr_redir_moved_t,
- mk_iov_none,
- MK_IOV_NOT_FREE_BUF);
- break;
-
- case M_NOT_MODIFIED:
- s_log->status=S_LOG_OFF;
- mk_header_iov_add_entry(iov, mk_hr_not_modified,
- mk_iov_none,
- MK_IOV_NOT_FREE_BUF);
- break;
-
- case M_CLIENT_BAD_REQUEST:
- mk_header_iov_add_entry(iov, mk_hr_client_bad_request,
- mk_iov_none,
- MK_IOV_NOT_FREE_BUF);
- break;
-
- case M_CLIENT_FORBIDDEN:
- mk_header_iov_add_entry(iov, mk_hr_client_forbidden,
- mk_iov_none,
- MK_IOV_NOT_FREE_BUF);
- break;
-
- case M_CLIENT_NOT_FOUND:
- mk_header_iov_add_entry(iov, mk_hr_client_not_found,
- mk_iov_none,
- MK_IOV_NOT_FREE_BUF);
- break;
-
- case M_CLIENT_METHOD_NOT_ALLOWED:
- mk_header_iov_add_entry(iov, mk_hr_client_method_not_allowed,
- mk_iov_none,
- MK_IOV_NOT_FREE_BUF);
- break;
-
- case M_CLIENT_REQUEST_TIMEOUT:
- mk_header_iov_add_entry(iov, mk_hr_client_request_timeout,
- mk_iov_none,
- MK_IOV_NOT_FREE_BUF);
- s_log->status=S_LOG_OFF;
- break;
-
- case M_CLIENT_LENGTH_REQUIRED:
- mk_header_iov_add_entry(iov, mk_hr_client_length_required,
- mk_iov_none,
- MK_IOV_NOT_FREE_BUF);
- break;
-
- case M_SERVER_NOT_IMPLEMENTED:
- mk_header_iov_add_entry(iov, mk_hr_server_not_implemented,
- mk_iov_none,
- MK_IOV_NOT_FREE_BUF);
- break;
-
- case M_SERVER_INTERNAL_ERROR:
- mk_header_iov_add_entry(iov, mk_hr_server_internal_error,
- mk_iov_none,
- MK_IOV_NOT_FREE_BUF);
- break;
-
- case M_SERVER_HTTP_VERSION_UNSUP:
- mk_header_iov_add_entry(iov,
- mk_hr_server_http_version_unsup,
- mk_iov_none,
- MK_IOV_NOT_FREE_BUF);
- break;
- };
-
- if(sh->status!=0){
- s_log->final_response = sh->status;
- }
-
- if(fd_status<0){
- mk_header_iov_free(iov);
- return -1;
- }
-
- /* Informacion del server */
- mk_iov_add_entry(iov, sr->host_conf->header_host_signature.data,
- sr->host_conf->header_host_signature.len,
- mk_iov_crlf, MK_IOV_NOT_FREE_BUF);
-
- /* Date */
- mk_iov_add_entry(iov,
- mk_header_short_date.data,
- mk_header_short_date.len,
- mk_iov_header_value, MK_IOV_NOT_FREE_BUF);
- mk_iov_add_entry(iov,
- header_current_time.data,
- header_current_time.len,
- mk_iov_crlf, MK_IOV_NOT_FREE_BUF);
-
- /* Connection */
- if(config->keep_alive == VAR_ON &&
- cr->request->connection.data != NULL &&
- cr->request->keep_alive == VAR_ON &&
- (cr->counter_connections < config->max_keep_alive_request)){
- m_build_buffer(
- &buffer,
- &len,
- "Keep-Alive: timeout=%i, max=%i"
- MK_CRLF,
- config->keep_alive_timeout,
- (config->max_keep_alive_request -
- cr->counter_connections)
- );
- mk_iov_add_entry(iov, buffer, len,
- mk_iov_none, MK_IOV_FREE_BUF);
- mk_iov_add_entry(iov,
- mk_header_conn_ka.data,
- mk_header_conn_ka.len,
- mk_iov_none,
- MK_IOV_NOT_FREE_BUF);
+ int fd_status = 0;
+ unsigned long len = 0;
+ char *buffer = 0;
+ struct header_values *sh;
+ struct mk_iov *iov;
+
+ sh = sr->headers;
+
+ iov = mk_header_iov_get();
+
+ /* Status Code */
+ switch (sh->status) {
+ case M_HTTP_OK:
+ mk_header_iov_add_entry(iov, mk_hr_http_ok,
+ mk_iov_none, MK_IOV_NOT_FREE_BUF);
+ break;
+
+ case M_HTTP_PARTIAL:
+ mk_header_iov_add_entry(iov, mk_hr_http_partial,
+ mk_iov_none, MK_IOV_NOT_FREE_BUF);
+ break;
+
+ case M_REDIR_MOVED:
+ s_log->status = S_LOG_OFF;
+ mk_header_iov_add_entry(iov, mk_hr_redir_moved,
+ mk_iov_none, MK_IOV_NOT_FREE_BUF);
+ break;
+
+ case M_REDIR_MOVED_T:
+ s_log->status = S_LOG_ON;
+ mk_header_iov_add_entry(iov, mk_hr_redir_moved_t,
+ mk_iov_none, MK_IOV_NOT_FREE_BUF);
+ break;
+
+ case M_NOT_MODIFIED:
+ s_log->status = S_LOG_OFF;
+ mk_header_iov_add_entry(iov, mk_hr_not_modified,
+ mk_iov_none, MK_IOV_NOT_FREE_BUF);
+ break;
+
+ case M_CLIENT_BAD_REQUEST:
+ mk_header_iov_add_entry(iov, mk_hr_client_bad_request,
+ mk_iov_none, MK_IOV_NOT_FREE_BUF);
+ break;
+
+ case M_CLIENT_FORBIDDEN:
+ mk_header_iov_add_entry(iov, mk_hr_client_forbidden,
+ mk_iov_none, MK_IOV_NOT_FREE_BUF);
+ break;
+
+ case M_CLIENT_NOT_FOUND:
+ mk_header_iov_add_entry(iov, mk_hr_client_not_found,
+ mk_iov_none, MK_IOV_NOT_FREE_BUF);
+ break;
+
+ case M_CLIENT_METHOD_NOT_ALLOWED:
+ mk_header_iov_add_entry(iov, mk_hr_client_method_not_allowed,
+ mk_iov_none, MK_IOV_NOT_FREE_BUF);
+ break;
+
+ case M_CLIENT_REQUEST_TIMEOUT:
+ mk_header_iov_add_entry(iov, mk_hr_client_request_timeout,
+ mk_iov_none, MK_IOV_NOT_FREE_BUF);
+ s_log->status = S_LOG_OFF;
+ break;
+
+ case M_CLIENT_LENGTH_REQUIRED:
+ mk_header_iov_add_entry(iov, mk_hr_client_length_required,
+ mk_iov_none, MK_IOV_NOT_FREE_BUF);
+ break;
+
+ case M_SERVER_NOT_IMPLEMENTED:
+ mk_header_iov_add_entry(iov, mk_hr_server_not_implemented,
+ mk_iov_none, MK_IOV_NOT_FREE_BUF);
+ break;
+
+ case M_SERVER_INTERNAL_ERROR:
+ mk_header_iov_add_entry(iov, mk_hr_server_internal_error,
+ mk_iov_none, MK_IOV_NOT_FREE_BUF);
+ break;
+
+ case M_SERVER_HTTP_VERSION_UNSUP:
+ mk_header_iov_add_entry(iov,
+ mk_hr_server_http_version_unsup,
+ mk_iov_none, MK_IOV_NOT_FREE_BUF);
+ break;
+ };
+
+ if (sh->status != 0) {
+ s_log->final_response = sh->status;
+ }
+
+ if (fd_status < 0) {
+ mk_header_iov_free(iov);
+ return -1;
+ }
+
+ /* Informacion del server */
+ mk_iov_add_entry(iov, sr->host_conf->header_host_signature.data,
+ sr->host_conf->header_host_signature.len,
+ mk_iov_crlf, MK_IOV_NOT_FREE_BUF);
+
+ /* Date */
+ mk_iov_add_entry(iov,
+ mk_header_short_date.data,
+ mk_header_short_date.len,
+ mk_iov_header_value, MK_IOV_NOT_FREE_BUF);
+ mk_iov_add_entry(iov,
+ header_current_time.data,
+ header_current_time.len,
+ mk_iov_crlf, MK_IOV_NOT_FREE_BUF);
+
+ /* Connection */
+ if (config->keep_alive == VAR_ON &&
+ cr->request->connection.data != NULL &&
+ cr->request->keep_alive == VAR_ON &&
+ (cr->counter_connections < config->max_keep_alive_request)) {
+ m_build_buffer(&buffer,
+ &len,
+ "Keep-Alive: timeout=%i, max=%i"
+ MK_CRLF,
+ config->keep_alive_timeout,
+ (config->max_keep_alive_request -
+ cr->counter_connections)
+ );
+ mk_iov_add_entry(iov, buffer, len, mk_iov_none, MK_IOV_FREE_BUF);
+ mk_iov_add_entry(iov,
+ mk_header_conn_ka.data,
+ mk_header_conn_ka.len,
+ mk_iov_none, MK_IOV_NOT_FREE_BUF);
+ }
+ else if (sr->protocol >= HTTP_PROTOCOL_10 || sr->content_length == 0) {
+ mk_iov_add_entry(iov,
+ mk_header_conn_close.data,
+ mk_header_conn_close.len,
+ mk_iov_none, MK_IOV_NOT_FREE_BUF);
+ }
+
+ /* Location */
+ if (sh->location != NULL) {
+ mk_iov_add_entry(iov,
+ mk_header_short_location.data,
+ mk_header_short_location.len,
+ mk_iov_header_value, MK_IOV_NOT_FREE_BUF);
+
+ mk_iov_add_entry(iov,
+ sh->location,
+ strlen(sh->location), mk_iov_crlf, MK_IOV_FREE_BUF);
+ }
+
+ /* Last-Modified */
+ if (sh->last_modified.len > 0) {
+ mk_iov_add_entry(iov, mk_header_last_modified.data,
+ mk_header_last_modified.len,
+ mk_iov_header_value, MK_IOV_NOT_FREE_BUF);
+ mk_iov_add_entry(iov, sh->last_modified.data,
+ sh->last_modified.len,
+ mk_iov_crlf, MK_IOV_NOT_FREE_BUF);
+ }
+
+ /* Content type */
+ if (sh->content_type.len > 0) {
+ mk_iov_add_entry(iov,
+ mk_header_short_ct.data,
+ mk_header_short_ct.len,
+ mk_iov_header_value, MK_IOV_NOT_FREE_BUF);
+
+ mk_iov_add_entry(iov,
+ sh->content_type.data,
+ sh->content_type.len,
+ mk_iov_crlf, MK_IOV_NOT_FREE_BUF);
+ }
+
+ /* Transfer Encoding */
+ switch (sh->transfer_encoding) {
+ case MK_HEADER_TE_TYPE_CHUNKED:
+ mk_iov_add_entry(iov,
+ mk_header_te_chunked.data,
+ mk_header_te_chunked.len,
+ mk_iov_crlf, MK_IOV_NOT_FREE_BUF);
+ break;
+ }
+
+ /* Accept ranges
+ mk_iov_add_entry(iov,
+ mk_header_accept_ranges.data,
+ mk_header_accept_ranges.len,
+ mk_iov_crlf, MK_IOV_NOT_FREE_BUF);
+ */
+ /* Tamaño total de la informacion a enviar */
+ if ((sh->content_length != 0 &&
+ (sh->ranges[0] >= 0 || sh->ranges[1] >= 0)) &&
+ config->resume == VAR_ON) {
+ long int length;
+
+ /* yyy- */
+ if (sh->ranges[0] >= 0 && sh->ranges[1] == -1) {
+ length = (unsigned int)
+ (sh->content_length - sh->ranges[0]);
+ m_build_buffer(&buffer, &len, "%s %i", RH_CONTENT_LENGTH, length);
+ mk_iov_add_entry(iov, buffer, len, mk_iov_crlf, MK_IOV_FREE_BUF);
+
+ m_build_buffer(&buffer,
+ &len,
+ "%s bytes %d-%d/%d",
+ RH_CONTENT_RANGE,
+ sh->ranges[0],
+ (sh->content_length - 1), sh->content_length);
+ mk_iov_add_entry(iov, buffer, len, mk_iov_crlf, MK_IOV_FREE_BUF);
+ }
+
+ /* yyy-xxx */
+ if (sh->ranges[0] >= 0 && sh->ranges[1] >= 0) {
+ length = (unsigned int)
+ abs(sh->ranges[1] - sh->ranges[0]) + 1;
+ m_build_buffer(&buffer, &len, "%s %d", RH_CONTENT_LENGTH, length);
+ mk_iov_add_entry(iov, buffer, len, mk_iov_crlf, MK_IOV_FREE_BUF);
+
+ m_build_buffer(&buffer,
+ &len,
+ "%s bytes %d-%d/%d",
+ RH_CONTENT_RANGE,
+ sh->ranges[0], sh->ranges[1], sh->content_length);
+
+ mk_iov_add_entry(iov, buffer, len, mk_iov_crlf, MK_IOV_FREE_BUF);
}
- else if(sr->protocol >= HTTP_PROTOCOL_10 || sr->content_length==0){
- mk_iov_add_entry(iov,
- mk_header_conn_close.data,
- mk_header_conn_close.len,
- mk_iov_none,
- MK_IOV_NOT_FREE_BUF);
+
+ /* -xxx */
+ if (sh->ranges[0] == -1 && sh->ranges[1] > 0) {
+ length = (unsigned int) sh->ranges[1];
+
+ if (length > sh->content_length) {
+ length = sh->content_length;
+ sh->ranges[1] = sh->content_length;
+ }
+
+ m_build_buffer(&buffer, &len, "%s %d", RH_CONTENT_LENGTH, length);
+ mk_iov_add_entry(iov, buffer, len, mk_iov_crlf, MK_IOV_FREE_BUF);
+
+ m_build_buffer(&buffer,
+ &len,
+ "%s bytes %d-%d/%d",
+ RH_CONTENT_RANGE,
+ (sh->content_length - sh->ranges[1]),
+ (sh->content_length - 1), sh->content_length);
+ mk_iov_add_entry(iov, buffer, len, mk_iov_crlf, MK_IOV_FREE_BUF);
}
+ }
+ else if (sh->content_length >= 0) {
+ mk_iov_add_entry(iov, mk_rh_content_length.data,
+ mk_rh_content_length.len,
+ mk_iov_space, MK_IOV_NOT_FREE_BUF);
- /* Location */
- if(sh->location!=NULL)
- {
- mk_iov_add_entry(iov,
- mk_header_short_location.data,
- mk_header_short_location.len,
- mk_iov_header_value, MK_IOV_NOT_FREE_BUF);
-
- mk_iov_add_entry(iov,
- sh->location,
- strlen(sh->location),
- mk_iov_crlf, MK_IOV_FREE_BUF);
- }
-
- /* Last-Modified */
- if(sh->last_modified.len>0)
- {
- mk_iov_add_entry(iov, mk_header_last_modified.data,
- mk_header_last_modified.len,
- mk_iov_header_value, MK_IOV_NOT_FREE_BUF);
- mk_iov_add_entry(iov, sh->last_modified.data,
- sh->last_modified.len,
- mk_iov_crlf, MK_IOV_NOT_FREE_BUF);
- }
-
- /* Content type */
- if(sh->content_type.len>0)
- {
- mk_iov_add_entry(iov,
- mk_header_short_ct.data,
- mk_header_short_ct.len,
- mk_iov_header_value, MK_IOV_NOT_FREE_BUF);
-
- mk_iov_add_entry(iov,
- sh->content_type.data,
- sh->content_type.len,
- mk_iov_crlf, MK_IOV_NOT_FREE_BUF);
- }
-
- /* Transfer Encoding */
- switch(sh->transfer_encoding)
- {
- case MK_HEADER_TE_TYPE_CHUNKED:
- mk_iov_add_entry(iov,
- mk_header_te_chunked.data,
- mk_header_te_chunked.len,
- mk_iov_crlf,
- MK_IOV_NOT_FREE_BUF);
- break;
- }
-
- /* Accept ranges
- mk_iov_add_entry(iov,
- mk_header_accept_ranges.data,
- mk_header_accept_ranges.len,
- mk_iov_crlf, MK_IOV_NOT_FREE_BUF);
- */
- /* Tamaño total de la informacion a enviar */
- if((sh->content_length!=0 &&
- (sh->ranges[0]>=0 || sh->ranges[1]>=0)) &&
- config->resume==VAR_ON)
- {
- long int length;
-
- /* yyy- */
- if(sh->ranges[0]>=0 && sh->ranges[1]==-1){
- length = (unsigned int)
- ( sh->content_length - sh->ranges[0] );
- m_build_buffer(
- &buffer,
- &len,
- "%s %i",
- RH_CONTENT_LENGTH,
- length);
- mk_iov_add_entry(iov, buffer, len,
- mk_iov_crlf, MK_IOV_FREE_BUF);
-
- m_build_buffer(
- &buffer,
- &len,
- "%s bytes %d-%d/%d",
- RH_CONTENT_RANGE,
- sh->ranges[0],
- (sh->content_length - 1),
- sh->content_length);
- mk_iov_add_entry(iov, buffer, len,
- mk_iov_crlf, MK_IOV_FREE_BUF);
- }
-
- /* yyy-xxx */
- if(sh->ranges[0]>=0 && sh->ranges[1]>=0){
- length = (unsigned int)
- abs(sh->ranges[1] - sh->ranges[0]) + 1;
- m_build_buffer(
- &buffer,
- &len,
- "%s %d",
- RH_CONTENT_LENGTH,
- length);
- mk_iov_add_entry(iov, buffer, len,
- mk_iov_crlf, MK_IOV_FREE_BUF);
-
- m_build_buffer(
- &buffer,
- &len,
- "%s bytes %d-%d/%d",
- RH_CONTENT_RANGE,
- sh->ranges[0],
- sh->ranges[1],
- sh->content_length);
-
- mk_iov_add_entry(iov, buffer, len,
- mk_iov_crlf, MK_IOV_FREE_BUF);
- }
-
- /* -xxx */
- if(sh->ranges[0]==-1 && sh->ranges[1]>0){
- length = (unsigned int)sh->ranges[1];
-
- if(length > sh->content_length){
- length = sh->content_length;
- sh->ranges[1] = sh->content_length;
- }
-
- m_build_buffer(
- &buffer,
- &len,
- "%s %d",
- RH_CONTENT_LENGTH,
- length);
- mk_iov_add_entry(iov, buffer, len,
- mk_iov_crlf, MK_IOV_FREE_BUF);
-
- m_build_buffer(
- &buffer,
- &len,
- "%s bytes %d-%d/%d",
- RH_CONTENT_RANGE,
- (sh->content_length - sh->ranges[1]),
- (sh->content_length - 1),
- sh->content_length);
- mk_iov_add_entry(iov, buffer, len,
- mk_iov_crlf, MK_IOV_FREE_BUF);
- }
- }
- else if(sh->content_length>=0)
- {
- mk_iov_add_entry(iov, mk_rh_content_length.data,
- mk_rh_content_length.len,
- mk_iov_space, MK_IOV_NOT_FREE_BUF);
-
- mk_iov_add_entry(iov, sh->content_length_p.data,
- sh->content_length_p.len,
- mk_iov_crlf, MK_IOV_NOT_FREE_BUF);
- }
-
- if(sh->cgi==SH_NOCGI || sh->breakline == MK_HEADER_BREAKLINE)
- {
- mk_iov_add_entry(iov, mk_iov_crlf.data, mk_iov_crlf.len,
- mk_iov_none, MK_IOV_NOT_FREE_BUF);
- }
-
- mk_socket_set_cork_flag(fd, TCP_CORK_ON);
- mk_iov_send(fd, iov, MK_IOV_SEND_TO_SOCKET);
+ mk_iov_add_entry(iov, sh->content_length_p.data,
+ sh->content_length_p.len,
+ mk_iov_crlf, MK_IOV_NOT_FREE_BUF);
+ }
+
+ if (sh->cgi == SH_NOCGI || sh->breakline == MK_HEADER_BREAKLINE) {
+ mk_iov_add_entry(iov, mk_iov_crlf.data, mk_iov_crlf.len,
+ mk_iov_none, MK_IOV_NOT_FREE_BUF);
+ }
+
+ mk_socket_set_cork_flag(fd, TCP_CORK_ON);
+ mk_iov_send(fd, iov, MK_IOV_SEND_TO_SOCKET);
#ifdef DEBUG_HEADERS_OUT
- mk_iov_send(0, iov, MK_IOV_SEND_TO_SOCKET);
+ mk_iov_send(0, iov, MK_IOV_SEND_TO_SOCKET);
#endif
- mk_header_iov_free(iov);
-
- return 0;
+ mk_header_iov_free(iov);
+
+ return 0;
}
char *mk_header_chunked_line(int len)
{
- char *buf;
+ char *buf;
- buf = mk_mem_malloc_z(10);
- snprintf(buf, 9, "%x%s", len, MK_CRLF);
+ buf = mk_mem_malloc_z(10);
+ snprintf(buf, 9, "%x%s", len, MK_CRLF);
- return buf;
+ return buf;
}
struct header_values *mk_header_create()
{
- struct header_values *headers;
-
- headers = (struct header_values *) mk_mem_malloc(sizeof(struct header_values));
- headers->ranges[0]=-1;
- headers->ranges[1]=-1;
- headers->content_length = -1;
- headers->transfer_encoding = -1;
- mk_pointer_reset(&headers->content_length_p);
- mk_pointer_reset(&headers->content_type);
- mk_pointer_reset(&headers->last_modified);
- headers->location = NULL;
-
- return headers;
+ struct header_values *headers;
+
+ headers =
+ (struct header_values *) mk_mem_malloc(sizeof(struct header_values));
+ headers->ranges[0] = -1;
+ headers->ranges[1] = -1;
+ headers->content_length = -1;
+ headers->transfer_encoding = -1;
+ mk_pointer_reset(&headers->content_length_p);
+ mk_pointer_reset(&headers->content_type);
+ mk_pointer_reset(&headers->last_modified);
+ headers->location = NULL;
+
+ return headers;
}
-
int mk_http_method_check(mk_pointer method)
{
- if(strncasecmp(method.data,
- HTTP_METHOD_GET_STR,
- method.len)==0)
- {
- return HTTP_METHOD_GET;
- }
-
- if(strncasecmp(method.data,
- HTTP_METHOD_POST_STR,
- method.len)==0)
- {
- return HTTP_METHOD_POST;
- }
-
- if(strncasecmp(method.data,
- HTTP_METHOD_HEAD_STR,
- method.len)==0)
- {
- return HTTP_METHOD_HEAD;
- }
+ if (strncasecmp(method.data, HTTP_METHOD_GET_STR, method.len) == 0) {
+ return HTTP_METHOD_GET;
+ }
- return METHOD_NOT_FOUND;
+ if (strncasecmp(method.data, HTTP_METHOD_POST_STR, method.len) == 0) {
+ return HTTP_METHOD_POST;
+ }
+
+ if (strncasecmp(method.data, HTTP_METHOD_HEAD_STR, method.len) == 0) {
+ return HTTP_METHOD_HEAD;
+ }
+
+ return METHOD_NOT_FOUND;
}
mk_pointer mk_http_method_check_str(int method)
{
- switch(method){
- case HTTP_METHOD_GET:
- return mk_http_method_get_p;
-
- case HTTP_METHOD_POST:
- return mk_http_method_post_p;
-
- case HTTP_METHOD_HEAD:
- return mk_http_method_head_p;
- }
- return mk_http_method_null_p;
+ switch (method) {
+ case HTTP_METHOD_GET:
+ return mk_http_method_get_p;
+
+ case HTTP_METHOD_POST:
+ return mk_http_method_post_p;
+
+ case HTTP_METHOD_HEAD:
+ return mk_http_method_head_p;
+ }
+ return mk_http_method_null_p;
}
int mk_http_method_get(char *body)
{
- int int_method, pos = 0;
- int max_len_method = 5;
- mk_pointer method;
-
- /* Max method length is 4 (POST/HEAD) */
- pos = mk_string_char_search(body, ' ', 5);
- if(pos<=2 || pos>=max_len_method){
- return METHOD_NOT_FOUND;
- }
+ int int_method, pos = 0;
+ int max_len_method = 5;
+ mk_pointer method;
+
+ /* Max method length is 4 (POST/HEAD) */
+ pos = mk_string_char_search(body, ' ', 5);
+ if (pos <= 2 || pos >= max_len_method) {
+ return METHOD_NOT_FOUND;
+ }
- method.data = body;
- method.len = (unsigned long) pos;
+ method.data = body;
+ method.len = (unsigned long) pos;
- int_method = mk_http_method_check(method);
+ int_method = mk_http_method_check(method);
- return int_method;
+ return int_method;
}
int mk_http_protocol_check(char *protocol, int len)
{
- if(strncasecmp(protocol, HTTP_PROTOCOL_11_STR, len)==0)
- {
- return HTTP_PROTOCOL_11;
- }
- if(strncasecmp(protocol, HTTP_PROTOCOL_10_STR, len)==0)
- {
- return HTTP_PROTOCOL_10;
- }
- if(strncasecmp(protocol, HTTP_PROTOCOL_09_STR, len)==0)
- {
- return HTTP_PROTOCOL_09;
- }
-
- return HTTP_PROTOCOL_UNKNOWN;
+ if (strncasecmp(protocol, HTTP_PROTOCOL_11_STR, len) == 0) {
+ return HTTP_PROTOCOL_11;
+ }
+ if (strncasecmp(protocol, HTTP_PROTOCOL_10_STR, len) == 0) {
+ return HTTP_PROTOCOL_10;
+ }
+ if (strncasecmp(protocol, HTTP_PROTOCOL_09_STR, len) == 0) {
+ return HTTP_PROTOCOL_09;
+ }
+
+ return HTTP_PROTOCOL_UNKNOWN;
}
mk_pointer mk_http_protocol_check_str(int protocol)
{
- if(protocol==HTTP_PROTOCOL_11)
- {
- return mk_http_protocol_11_p;
- }
- if(protocol==HTTP_PROTOCOL_10)
- {
- return mk_http_protocol_10_p;
- }
- if(protocol==HTTP_PROTOCOL_09)
- {
- return mk_http_protocol_09_p;
- }
-
- return mk_http_protocol_null_p;
+ if (protocol == HTTP_PROTOCOL_11) {
+ return mk_http_protocol_11_p;
+ }
+ if (protocol == HTTP_PROTOCOL_10) {
+ return mk_http_protocol_10_p;
+ }
+ if (protocol == HTTP_PROTOCOL_09) {
+ return mk_http_protocol_09_p;
+ }
+
+ return mk_http_protocol_null_p;
}
int mk_http_init(struct client_request *cr, struct request *sr)
{
- int debug_error=0, bytes=0;
- struct mimetype *mime;
- mk_pointer gmt_file_unix_time; // gmt time of server file (unix time)
-
- /* Normal request default site */
- if((strcmp(sr->uri_processed,"/"))==0)
- {
- sr->real_path.data = mk_string_dup(sr->host_conf->documentroot.data);
- sr->real_path.len = sr->host_conf->documentroot.len;
- }
+ int debug_error = 0, bytes = 0;
+ struct mimetype *mime;
+ mk_pointer gmt_file_unix_time; // gmt time of server file (unix time)
+
+ /* Normal request default site */
+ if ((strcmp(sr->uri_processed, "/")) == 0) {
+ sr->real_path.data = mk_string_dup(sr->host_conf->documentroot.data);
+ sr->real_path.len = sr->host_conf->documentroot.len;
+ }
+
+ if (sr->user_home == VAR_OFF) {
+ mk_buffer_cat(&sr->real_path, sr->host_conf->documentroot.data,
+ sr->uri_processed);
+ }
+
+ if (sr->method != HTTP_METHOD_HEAD) {
+ debug_error = 1;
+ }
+
+ if (mk_string_search_n(sr->uri.data, HTTP_DIRECTORY_BACKWARD,
+ sr->uri.len) >= 0) {
+ sr->log->final_response = M_CLIENT_FORBIDDEN;
+ mk_request_error(M_CLIENT_FORBIDDEN, cr, sr, debug_error, sr->log);
+ return -1;
+ }
- if(sr->user_home==VAR_OFF)
- {
- mk_buffer_cat(&sr->real_path, sr->host_conf->documentroot.data,
- sr->uri_processed);
- }
-
- if(sr->method!=HTTP_METHOD_HEAD){
- debug_error=1;
- }
+ /* Plugin Stage 30: look for handlers for this request */
+ if (mk_plugin_stage_run(MK_PLUGIN_STAGE_30, 0, NULL, cr, sr) ==
+ MK_PLUGIN_RET_CLOSE_CONX) {
+ sr->log->final_response = M_CLIENT_FORBIDDEN;
+ mk_request_error(M_CLIENT_FORBIDDEN, cr, sr, debug_error, sr->log);
+ return -1;
+ }
- if(mk_string_search_n(sr->uri.data, HTTP_DIRECTORY_BACKWARD,
- sr->uri.len) >= 0){
- sr->log->final_response=M_CLIENT_FORBIDDEN;
- mk_request_error(M_CLIENT_FORBIDDEN, cr, sr,
- debug_error, sr->log);
- return -1;
- }
+ sr->file_info = mk_file_get_info(sr->real_path.data);
- /* Plugin Stage 30: look for handlers for this request */
- if(mk_plugin_stage_run(MK_PLUGIN_STAGE_30, 0, NULL, cr, sr) ==
- MK_PLUGIN_RET_CLOSE_CONX){
- sr->log->final_response=M_CLIENT_FORBIDDEN;
- mk_request_error(M_CLIENT_FORBIDDEN, cr, sr,
- debug_error, sr->log);
- return -1;
+ if (!sr->file_info) {
+ /* if the resource requested doesn't exists, let's
+ * check if some plugin would like to handle it
+ */
+ if (mk_plugin_stage_run(MK_PLUGIN_STAGE_40, cr->socket, NULL, cr, sr)
+ == 0) {
+ return -1;
}
- sr->file_info = mk_file_get_info(sr->real_path.data);
-
- if(!sr->file_info){
- /* if the resource requested doesn't exists, let's
- * check if some plugin would like to handle it
- */
- if(mk_plugin_stage_run(MK_PLUGIN_STAGE_40, cr->socket, NULL, cr, sr) == 0){
- return -1;
- }
-
- mk_request_error(M_CLIENT_NOT_FOUND, cr, sr,
- debug_error, sr->log);
- return -1;
- }
+ mk_request_error(M_CLIENT_NOT_FOUND, cr, sr, debug_error, sr->log);
+ return -1;
+ }
+
+ /* Check symbolic link file */
+ if (sr->file_info->is_link == MK_FILE_TRUE) {
+ if (config->symlink == VAR_OFF) {
+ sr->log->final_response = M_CLIENT_FORBIDDEN;
+ mk_request_error(M_CLIENT_FORBIDDEN, cr, sr,
+ debug_error, sr->log);
+ return -1;
+ }
+ else {
+ int n;
+ char linked_file[MAX_PATH];
+ n = readlink(sr->real_path.data, linked_file, MAX_PATH);
+ /*
+ if(Deny_Check(linked_file)==-1) {
+ sr->log->final_response=M_CLIENT_FORBIDDEN;
+ mk_request_error(M_CLIENT_FORBIDDEN, cr, sr, debug_error, sr->log);
+ return -1;
+ }
+ */
+
+ }
+ }
+
+ /* is it a valid directory ? */
+ if (sr->file_info->is_directory == MK_FILE_TRUE) {
+ /* Send redirect header if end slash is not found */
+ if (mk_http_directory_redirect_check(cr, sr) == -1) {
+ /* Redirect has been sent */
+ return -1;
+ }
+
+ /* looking for a index file */
+ mk_pointer index_file;
+ index_file = mk_request_index(sr->real_path.data);
+
+ if (index_file.data) {
+ mk_mem_free(sr->file_info);
+ mk_pointer_free(&sr->real_path);
+
+ sr->real_path = index_file;
+ sr->file_info = mk_file_get_info(sr->real_path.data);
+ }
+ }
+
+ /* read permissions and check file */
+ if (sr->file_info->read_access == MK_FILE_FALSE) {
+ mk_request_error(M_CLIENT_FORBIDDEN, cr, sr, 1, sr->log);
+ return -1;
+ }
- /* Check symbolic link file */
- if(sr->file_info->is_link == MK_FILE_TRUE){
- if(config->symlink==VAR_OFF){
- sr->log->final_response=M_CLIENT_FORBIDDEN;
- mk_request_error(M_CLIENT_FORBIDDEN, cr, sr,
- debug_error, sr->log);
- return -1;
- }
- else{
- int n;
- char linked_file[MAX_PATH];
- n = readlink(sr->real_path.data, linked_file, MAX_PATH);
- /*
- if(Deny_Check(linked_file)==-1) {
- sr->log->final_response=M_CLIENT_FORBIDDEN;
- mk_request_error(M_CLIENT_FORBIDDEN, cr, sr, debug_error, sr->log);
- return -1;
- }
- */
-
- }
- }
+ /* Plugin Stage 40: look for handlers for this request */
+ if (mk_plugin_stage_run(MK_PLUGIN_STAGE_40, cr->socket, NULL, cr, sr) ==
+ 0) {
+ return -1;
+ }
- /* is it a valid directory ? */
- if(sr->file_info->is_directory == MK_FILE_TRUE) {
- /* Send redirect header if end slash is not found */
- if(mk_http_directory_redirect_check(cr, sr) == -1){
- /* Redirect has been sent */
- return -1;
- }
- /* looking for a index file */
- mk_pointer index_file;
- index_file = mk_request_index(sr->real_path.data);
+ /* Matching MimeType */
+ mime = mk_mimetype_find(&sr->real_path);
+ if (!mime) {
+ mime = mimetype_default;
+ }
- if(index_file.data) {
- mk_mem_free(sr->file_info);
- mk_pointer_free(&sr->real_path);
-
- sr->real_path = index_file;
- sr->file_info = mk_file_get_info(sr->real_path.data);
- }
- }
+ if (sr->file_info->is_directory == MK_FILE_TRUE) {
+ mk_request_error(M_CLIENT_FORBIDDEN, cr, sr, 1, sr->log);
+ return -1;
+ }
- /* read permissions and check file */
- if(sr->file_info->read_access == MK_FILE_FALSE){
- mk_request_error(M_CLIENT_FORBIDDEN, cr, sr, 1, sr->log);
- return -1;
- }
-
- /* Plugin Stage 40: look for handlers for this request */
- if(mk_plugin_stage_run(MK_PLUGIN_STAGE_40, cr->socket, NULL, cr, sr) == 0){
+ /* get file size */
+ if (sr->file_info->size < 0) {
+ mk_request_error(M_CLIENT_NOT_FOUND, cr, sr, 1, sr->log);
+ return -1;
+ }
+
+ /* counter connections */
+ sr->headers->pconnections_left = (int)
+ (config->max_keep_alive_request - cr->counter_connections);
+
+
+ gmt_file_unix_time =
+ PutDate_string((time_t) sr->file_info->last_modification);
+
+ if (sr->if_modified_since.data && sr->method == HTTP_METHOD_GET) {
+ time_t date_client; // Date send by client
+ time_t date_file_server; // Date server file
+
+ date_client = PutDate_unix(sr->if_modified_since.data);
+ date_file_server = sr->file_info->last_modification;
+
+ if ((date_file_server <= date_client) && (date_client > 0)) {
+ sr->headers->status = M_NOT_MODIFIED;
+ mk_header_send(cr->socket, cr, sr, sr->log);
+ mk_pointer_free(&gmt_file_unix_time);
+ return 0;
+ }
+ }
+ sr->headers->status = M_HTTP_OK;
+ sr->headers->cgi = SH_NOCGI;
+ sr->headers->last_modified = gmt_file_unix_time;
+ sr->headers->location = NULL;
+
+ /* Object size for log and response headers */
+ sr->log->size = sr->headers->content_length = sr->file_info->size;
+ sr->log->size_p = sr->headers->content_length_p =
+ mk_utils_int2mkp(sr->file_info->size);
+
+ if (sr->method == HTTP_METHOD_GET || sr->method == HTTP_METHOD_POST) {
+ sr->headers->content_type = mime->type;
+ /* Range */
+ if (sr->range.data != NULL && config->resume == VAR_ON) {
+ if (mk_http_range_parse(sr) < 0) {
+ mk_request_error(M_CLIENT_BAD_REQUEST, cr, sr, 1, sr->log);
+ mk_pointer_free(&gmt_file_unix_time);
return -1;
+ }
+ if (sr->headers->ranges[0] >= 0 || sr->headers->ranges[1] >= 0)
+ sr->headers->status = M_HTTP_PARTIAL;
}
+ }
+ else { /* without content-type */
+ mk_pointer_reset(&sr->headers->content_type);
+ }
-
- /* Matching MimeType */
- mime = mk_mimetype_find(&sr->real_path);
- if(!mime)
- {
- mime = mimetype_default;
- }
-
- if(sr->file_info->is_directory == MK_FILE_TRUE){
- mk_request_error(M_CLIENT_FORBIDDEN, cr, sr, 1, sr->log);
- return -1;
- }
+ mk_header_send(cr->socket, cr, sr, sr->log);
- /* get file size */
- if(sr->file_info->size < 0) {
- mk_request_error(M_CLIENT_NOT_FOUND, cr, sr, 1, sr->log);
- return -1;
- }
-
- /* counter connections */
- sr->headers->pconnections_left = (int)
- (config->max_keep_alive_request - cr->counter_connections);
-
-
- gmt_file_unix_time =
- PutDate_string((time_t) sr->file_info->last_modification);
-
- if(sr->if_modified_since.data && sr->method==HTTP_METHOD_GET){
- time_t date_client; // Date send by client
- time_t date_file_server; // Date server file
-
- date_client = PutDate_unix(sr->if_modified_since.data);
- date_file_server = sr->file_info->last_modification;
-
- if( (date_file_server <= date_client) && (date_client > 0) )
- {
- sr->headers->status = M_NOT_MODIFIED;
- mk_header_send(cr->socket, cr, sr, sr->log);
- mk_pointer_free(&gmt_file_unix_time);
- return 0;
- }
- }
- sr->headers->status = M_HTTP_OK;
- sr->headers->cgi = SH_NOCGI;
- sr->headers->last_modified = gmt_file_unix_time;
- sr->headers->location = NULL;
-
- /* Object size for log and response headers */
- sr->log->size = sr->headers->content_length = sr->file_info->size;
- sr->log->size_p = sr->headers->content_length_p =
- mk_utils_int2mkp(sr->file_info->size);
-
- if(sr->method==HTTP_METHOD_GET || sr->method==HTTP_METHOD_POST)
- {
- sr->headers->content_type = mime->type;
- /* Range */
- if(sr->range.data!=NULL && config->resume==VAR_ON){
- if(mk_http_range_parse(sr)<0)
- {
- mk_request_error(M_CLIENT_BAD_REQUEST, cr,
- sr, 1, sr->log);
- mk_pointer_free(&gmt_file_unix_time);
- return -1;
- }
- if(sr->headers->ranges[0]>=0 || sr->headers->ranges[1]>=0)
- sr->headers->status = M_HTTP_PARTIAL;
- }
- }
- else{ /* without content-type */
- mk_pointer_reset(&sr->headers->content_type);
- }
+ if (sr->headers->content_length == 0) {
+ return 0;
+ }
- mk_header_send(cr->socket, cr, sr, sr->log);
+ /* Sending file */
+ if ((sr->method == HTTP_METHOD_GET || sr->method == HTTP_METHOD_POST)
+ && sr->file_info->size > 0) {
+ sr->fd_file = open(sr->real_path.data, config->open_flags);
- if(sr->headers->content_length==0){
- return 0;
+ if (sr->fd_file == -1) {
+ perror("open");
+ return -1;
}
- /* Sending file */
- if((sr->method==HTTP_METHOD_GET || sr->method==HTTP_METHOD_POST)
- && sr->file_info->size>0)
- {
- sr->fd_file = open(sr->real_path.data, config->open_flags);
-
- if(sr->fd_file == -1){
- perror("open");
- return -1;
- }
-
- /* Calc bytes to send & offset */
- if(mk_http_range_set(sr, sr->file_info->size)!=0)
- {
- mk_request_error(M_CLIENT_BAD_REQUEST, cr,
- sr, 1, sr->log);
- return -1;
- }
- bytes = SendFile(cr->socket, cr, sr);
+ /* Calc bytes to send & offset */
+ if (mk_http_range_set(sr, sr->file_info->size) != 0) {
+ mk_request_error(M_CLIENT_BAD_REQUEST, cr, sr, 1, sr->log);
+ return -1;
}
+ bytes = SendFile(cr->socket, cr, sr);
+ }
- return bytes;
+ return bytes;
}
int mk_http_directory_redirect_check(struct client_request *cr,
struct request *sr)
{
- char *host;
- char *location=0;
- char *real_location=0;
- unsigned long len;
-
- /*
- * We have to check if exist an slash to the end of
- * this string, if doesn't exist we send a redirection header
- */
- if(sr->uri_processed[strlen(sr->uri_processed) - 1] == '/') {
- return 0;
- }
-
- host = mk_pointer_to_buf(sr->host);
-
- m_build_buffer(&location, &len, "%s/", sr->uri_processed);
- if(config->serverport == config->standard_port)
- {
- m_build_buffer(&real_location, &len, "http://%s%s",
- host, location);
- }
- else{
- m_build_buffer(&real_location, &len, "http://%s:%i%s",
- host, config->serverport,
- location);
- }
-
- mk_mem_free(host);
-
- sr->headers->status = M_REDIR_MOVED;
- sr->headers->content_length = -1;
- mk_pointer_reset(&sr->headers->content_type);
- sr->headers->location = real_location;
- sr->headers->cgi = SH_NOCGI;
- sr->headers->pconnections_left =
- (config->max_keep_alive_request -
- cr->counter_connections);
-
- mk_header_send(cr->socket, cr, sr, sr->log);
- mk_socket_set_cork_flag(cr->socket, TCP_CORK_OFF);
-
- /*
- * we do not free() real_location
- * as it's freed by iov
- */
- mk_mem_free(location);
- sr->headers->location=NULL;
- return -1;
+ char *host;
+ char *location = 0;
+ char *real_location = 0;
+ unsigned long len;
+
+ /*
+ * We have to check if exist an slash to the end of
+ * this string, if doesn't exist we send a redirection header
+ */
+ if (sr->uri_processed[strlen(sr->uri_processed) - 1] == '/') {
+ return 0;
+ }
+
+ host = mk_pointer_to_buf(sr->host);
+
+ m_build_buffer(&location, &len, "%s/", sr->uri_processed);
+ if (config->serverport == config->standard_port) {
+ m_build_buffer(&real_location, &len, "http://%s%s", host, location);
+ }
+ else {
+ m_build_buffer(&real_location, &len, "http://%s:%i%s",
+ host, config->serverport, location);
+ }
+
+ mk_mem_free(host);
+
+ sr->headers->status = M_REDIR_MOVED;
+ sr->headers->content_length = -1;
+ mk_pointer_reset(&sr->headers->content_type);
+ sr->headers->location = real_location;
+ sr->headers->cgi = SH_NOCGI;
+ sr->headers->pconnections_left =
+ (config->max_keep_alive_request - cr->counter_connections);
+
+ mk_header_send(cr->socket, cr, sr, sr->log);
+ mk_socket_set_cork_flag(cr->socket, TCP_CORK_OFF);
+
+ /*
+ * we do not free() real_location
+ * as it's freed by iov
+ */
+ mk_mem_free(location);
+ sr->headers->location = NULL;
+ return -1;
}
/*
*/
int mk_http_keepalive_check(int socket, struct client_request *cr)
{
- if(!cr->request)
- {
- return -1;
- }
+ if (!cr->request) {
+ return -1;
+ }
- if(config->keep_alive==VAR_OFF || cr->request->keep_alive==VAR_OFF)
- {
- return -1;
- }
+ if (config->keep_alive == VAR_OFF || cr->request->keep_alive == VAR_OFF) {
+ return -1;
+ }
- if(cr->counter_connections>=config->max_keep_alive_request)
- {
- return -1;
- }
+ if (cr->counter_connections >= config->max_keep_alive_request) {
+ return -1;
+ }
- return 0;
+ return 0;
}
int mk_http_range_set(struct request *sr, long file_size)
{
- struct header_values *sh = sr->headers;
-
- sr->bytes_to_send = file_size;
- sr->bytes_offset = 0;
-
- if(config->resume==VAR_ON && sr->range.data){
- /* yyy- */
- if(sh->ranges[0]>=0 && sh->ranges[1]==-1){
- sr->bytes_offset = sh->ranges[0];
- sr->bytes_to_send = file_size - sr->bytes_offset;
- }
+ struct header_values *sh = sr->headers;
- /* yyy-xxx */
- if(sh->ranges[0]>=0 && sh->ranges[1]>=0){
- sr->bytes_offset = sh->ranges[0];
- sr->bytes_to_send = labs(sh->ranges[1]-sh->ranges[0])+1;
- }
+ sr->bytes_to_send = file_size;
+ sr->bytes_offset = 0;
- /* -xxx */
- if(sh->ranges[0]==-1 && sh->ranges[1]>0){
- sr->bytes_to_send = sh->ranges[1];
- sr->bytes_offset = file_size - sh->ranges[1];
- }
+ if (config->resume == VAR_ON && sr->range.data) {
+ /* yyy- */
+ if (sh->ranges[0] >= 0 && sh->ranges[1] == -1) {
+ sr->bytes_offset = sh->ranges[0];
+ sr->bytes_to_send = file_size - sr->bytes_offset;
+ }
- if(sr->bytes_offset>file_size || sr->bytes_to_send>file_size)
- {
- return -1;
- }
+ /* yyy-xxx */
+ if (sh->ranges[0] >= 0 && sh->ranges[1] >= 0) {
+ sr->bytes_offset = sh->ranges[0];
+ sr->bytes_to_send = labs(sh->ranges[1] - sh->ranges[0]) + 1;
+ }
- lseek(sr->fd_file, sr->bytes_offset, SEEK_SET);
+ /* -xxx */
+ if (sh->ranges[0] == -1 && sh->ranges[1] > 0) {
+ sr->bytes_to_send = sh->ranges[1];
+ sr->bytes_offset = file_size - sh->ranges[1];
}
- return 0;
+
+ if (sr->bytes_offset > file_size || sr->bytes_to_send > file_size) {
+ return -1;
+ }
+
+ lseek(sr->fd_file, sr->bytes_offset, SEEK_SET);
+ }
+ return 0;
}
int mk_http_range_parse(struct request *sr)
{
- int eq_pos, sep_pos, len;
- char *buffer=0;
+ int eq_pos, sep_pos, len;
+ char *buffer = 0;
- if(!sr->range.data)
- return -1;
+ if (!sr->range.data)
+ return -1;
- if((eq_pos = mk_string_search_n(sr->range.data, "=",
- sr->range.len))<0)
- return -1;
+ if ((eq_pos = mk_string_search_n(sr->range.data, "=", sr->range.len)) < 0)
+ return -1;
- if(strncasecmp(sr->range.data, "Bytes", eq_pos)!=0)
- return -1;
-
- if((sep_pos = mk_string_search_n(sr->range.data, "-",
- sr->range.len))<0)
- return -1;
-
- len = sr->range.len;
+ if (strncasecmp(sr->range.data, "Bytes", eq_pos) != 0)
+ return -1;
- /* =-xxx */
- if(eq_pos+1 == sep_pos){
- sr->headers->ranges[0] = -1;
- sr->headers->ranges[1] = (unsigned long) atol(sr->range.data + sep_pos + 1);
+ if ((sep_pos = mk_string_search_n(sr->range.data, "-",
+ sr->range.len)) < 0)
+ return -1;
- if(sr->headers->ranges[1]<=0)
- {
- return -1;
- }
- return 0;
- }
+ len = sr->range.len;
- /* =yyy-xxx */
- if( (eq_pos+1 != sep_pos) && (len > sep_pos + 1))
- {
- buffer = mk_string_copy_substr(sr->range.data, eq_pos+1, sep_pos);
- sr->headers->ranges[0] = (unsigned long) atol(buffer);
- mk_mem_free(buffer);
-
- buffer = mk_string_copy_substr(sr->range.data, sep_pos+1, len);
- sr->headers->ranges[1] = (unsigned long) atol(buffer);
- mk_mem_free(buffer);
-
- if(sr->headers->ranges[1]<=0 ||
- sr->headers->ranges[0]>sr->headers->ranges[1])
- {
- return -1;
- }
+ /* =-xxx */
+ if (eq_pos + 1 == sep_pos) {
+ sr->headers->ranges[0] = -1;
+ sr->headers->ranges[1] =
+ (unsigned long) atol(sr->range.data + sep_pos + 1);
- return 0;
+ if (sr->headers->ranges[1] <= 0) {
+ return -1;
}
- /* =yyy- */
- if( (eq_pos+1 != sep_pos) && (len == sep_pos + 1))
- {
- buffer = mk_string_copy_substr(sr->range.data, eq_pos+1, len);
- sr->headers->ranges[0] = (unsigned long) atol(buffer);
- mk_mem_free(buffer);
- return 0;
+ return 0;
+ }
+
+ /* =yyy-xxx */
+ if ((eq_pos + 1 != sep_pos) && (len > sep_pos + 1)) {
+ buffer = mk_string_copy_substr(sr->range.data, eq_pos + 1, sep_pos);
+ sr->headers->ranges[0] = (unsigned long) atol(buffer);
+ mk_mem_free(buffer);
+
+ buffer = mk_string_copy_substr(sr->range.data, sep_pos + 1, len);
+ sr->headers->ranges[1] = (unsigned long) atol(buffer);
+ mk_mem_free(buffer);
+
+ if (sr->headers->ranges[1] <= 0 ||
+ sr->headers->ranges[0] > sr->headers->ranges[1]) {
+ return -1;
}
-
- return -1;
+
+ return 0;
+ }
+ /* =yyy- */
+ if ((eq_pos + 1 != sep_pos) && (len == sep_pos + 1)) {
+ buffer = mk_string_copy_substr(sr->range.data, eq_pos + 1, len);
+ sr->headers->ranges[0] = (unsigned long) atol(buffer);
+ mk_mem_free(buffer);
+ return 0;
+ }
+
+ return -1;
}
/*
*/
int mk_http_pending_request(struct client_request *cr)
{
- int n,len;
- char *str;
-
- len = cr->body_length;
-
- /* try to match CRLF end */
- if(strcmp(cr->body+len-mk_endblock.len, mk_endblock.data) == 0){
- n = len-mk_endblock.len;
- }
- else{
- n = mk_string_search(cr->body, mk_endblock.data);
- }
-
- if(n<=0)
- {
- return -1;
- }
+ int n, len;
+ char *str;
- if(cr->first_block_end<0)
- {
- cr->first_block_end = n;
- }
-
- str = cr->body + n + mk_endblock.len;
+ len = cr->body_length;
- if(cr->first_method == HTTP_METHOD_UNKNOWN){
- cr->first_method = mk_http_method_get(cr->body);
- }
+ /* try to match CRLF end */
+ if (strcmp(cr->body + len - mk_endblock.len, mk_endblock.data) == 0) {
+ n = len - mk_endblock.len;
+ }
+ else {
+ n = mk_string_search(cr->body, mk_endblock.data);
+ }
- if(cr->first_method == HTTP_METHOD_POST)
- {
- if(cr->first_block_end > 0){
- /* if first block has ended, we need to verify if exists
- * a previous block end, that will means that the POST
- * method has sent the whole information.
- * just for ref: pipelining is not allowed with POST
- */
- if(cr->first_block_end == cr->body_length-mk_endblock.len){
- /* Content-length is required, if is it not found,
- * we pass as successfull in order to raise the error
- * later
- */
- if(mk_method_post_content_length(cr->body) < 0){
- cr->status = MK_REQUEST_STATUS_COMPLETED;
- return 0;
- }
- }
- else{
- cr->status = MK_REQUEST_STATUS_COMPLETED;
- return 0;
- }
- }
- else{
- return -1;
+ if (n <= 0) {
+ return -1;
+ }
+
+ if (cr->first_block_end < 0) {
+ cr->first_block_end = n;
+ }
+
+ str = cr->body + n + mk_endblock.len;
+
+ if (cr->first_method == HTTP_METHOD_UNKNOWN) {
+ cr->first_method = mk_http_method_get(cr->body);
+ }
+
+ if (cr->first_method == HTTP_METHOD_POST) {
+ if (cr->first_block_end > 0) {
+ /* if first block has ended, we need to verify if exists
+ * a previous block end, that will means that the POST
+ * method has sent the whole information.
+ * just for ref: pipelining is not allowed with POST
+ */
+ if (cr->first_block_end == cr->body_length - mk_endblock.len) {
+ /* Content-length is required, if is it not found,
+ * we pass as successfull in order to raise the error
+ * later
+ */
+ if (mk_method_post_content_length(cr->body) < 0) {
+ cr->status = MK_REQUEST_STATUS_COMPLETED;
+ return 0;
}
+ }
+ else {
+ cr->status = MK_REQUEST_STATUS_COMPLETED;
+ return 0;
+ }
}
+ else {
+ return -1;
+ }
+ }
- cr->status = MK_REQUEST_STATUS_COMPLETED;
- return 0;
+ cr->status = MK_REQUEST_STATUS_COMPLETED;
+ return 0;
}
mk_pointer *mk_http_status_get(short int code)
{
- mk_list_sint_t *l;
-
- l = mk_http_status_list;
- while(l)
- {
- if(l->index == code)
- {
- return &l->value;
- }
- else {
- l = l->next;
- }
+ mk_list_sint_t *l;
+
+ l = mk_http_status_list;
+ while (l) {
+ if (l->index == code) {
+ return &l->value;
+ }
+ else {
+ l = l->next;
}
+ }
- return NULL;
+ return NULL;
}
void mk_http_status_add(short int val[2])
{
- short i, len=6;
- char *str_val;
- mk_list_sint_t *list, *new;
-
- for(i=val[0];i<=val[1]; i++)
- {
-
- new = mk_mem_malloc(sizeof(mk_list_sint_t));
- new->index = i;
- new->next = NULL;
-
- str_val = mk_mem_malloc(6);
- snprintf(str_val, len-1, "%i", i);
-
- new->value.data = str_val;
- new->value.len = 3;
-
- if(!mk_http_status_list)
- {
- mk_http_status_list = new;
- }
- else{
- list = mk_http_status_list;
- while(list->next)
- list = list->next;
+ short i, len = 6;
+ char *str_val;
+ mk_list_sint_t *list, *new;
- list->next = new;
- list = new;
- }
+ for (i = val[0]; i <= val[1]; i++) {
+
+ new = mk_mem_malloc(sizeof(mk_list_sint_t));
+ new->index = i;
+ new->next = NULL;
+
+ str_val = mk_mem_malloc(6);
+ snprintf(str_val, len - 1, "%i", i);
+
+ new->value.data = str_val;
+ new->value.len = 3;
+
+ if (!mk_http_status_list) {
+ mk_http_status_list = new;
+ }
+ else {
+ list = mk_http_status_list;
+ while (list->next)
+ list = list->next;
+
+ list->next = new;
+ list = new;
}
+ }
}
void mk_http_status_list_init()
{
- /* Status type */
- short int success[2] = {200, 206};
- short int redirections[2] = {300, 305};
- short int client_errors[2] = {400, 415};
- short int server_errors[2] = {500, 505};
-
- mk_http_status_add(success);
- mk_http_status_add(redirections);
- mk_http_status_add(client_errors);
- mk_http_status_add(server_errors);
+ /* Status type */
+ short int success[2] = { 200, 206 };
+ short int redirections[2] = { 300, 305 };
+ short int client_errors[2] = { 400, 415 };
+ short int server_errors[2] = { 500, 505 };
+
+ mk_http_status_add(success);
+ mk_http_status_add(redirections);
+ mk_http_status_add(client_errors);
+ mk_http_status_add(server_errors);
}
#ifndef MK_CACHE_H
#define MK_CACHE_H
-#define MK_KNOWN_HEADERS 11 /* Number of different headers that
- * Monkey knows about
- */
+#define MK_KNOWN_HEADERS 11 /* Number of different headers that
+ * Monkey knows about
+ */
pthread_key_t mk_cache_iov_log;
pthread_key_t mk_cache_iov_header;
pthread_key_t mk_cache_header_toc;
-struct mk_cache_date_t {
- time_t unix_time;
- time_t expire;
- time_t last_access;
- mk_pointer date;
+struct mk_cache_date_t
+{
+ time_t unix_time;
+ time_t expire;
+ time_t last_access;
+ mk_pointer date;
};
struct mk_cache_date_t *mk_cache_file_date;
void mk_clock_set_time();
#endif
-
#define MK_CONFIG_VAL_BOOL 2
#define MK_CONFIG_VAL_LIST 3
-struct mk_config {
- char *key;
- char *val;
- struct mk_config *next;
+struct mk_config
+{
+ char *key;
+ char *val;
+ struct mk_config *next;
};
/* Base struct of server */
-struct server_config {
- mk_pointer port;
-
- char *serverconf; /* path to configuration files */
-
- mk_pointer server_addr;
- mk_pointer server_software;
-
- char *user;
- char *user_dir;
- char *pid_file_path; /* pid of server */
- char *file_config;
- char **request_headers_allowed;
-
- int workers; /* number of worker threads */
- int worker_capacity; /* how many clients per thread... */
-
- int symlink; /* symbolic links */
- int serverport; /* port */
- int timeout; /* max time to wait for a new connection */
- int maxclients; /* max clients (max threads) */
- int hideversion; /* hide version of server to clients ? */
- int standard_port; /* common port used in web servers (80) */
- int pid_status;
- int resume; /* Resume (on/off) */
-
- /* keep alive */
- int keep_alive; /* it's a persisten connection ? */
- int max_keep_alive_request; /* max persistent connections to allow */
- int keep_alive_timeout; /* persistent connection timeout */
-
- /* counter of threads working */
- int thread_counter;
- /* real user */
- uid_t egid;
- gid_t euid;
-
- /* max ip */
- int max_ip;
-
- struct dir_html_theme *dir_theme;
-
- /* configured host quantity */
- int nhosts;
- struct host *hosts;
-
- mode_t open_flags;
- struct plugin_stages *plugins;
+struct server_config
+{
+ mk_pointer port;
+
+ char *serverconf; /* path to configuration files */
+
+ mk_pointer server_addr;
+ mk_pointer server_software;
+
+ char *user;
+ char *user_dir;
+ char *pid_file_path; /* pid of server */
+ char *file_config;
+ char **request_headers_allowed;
+
+ int workers; /* number of worker threads */
+ int worker_capacity; /* how many clients per thread... */
+
+ int symlink; /* symbolic links */
+ int serverport; /* port */
+ int timeout; /* max time to wait for a new connection */
+ int maxclients; /* max clients (max threads) */
+ int hideversion; /* hide version of server to clients ? */
+ int standard_port; /* common port used in web servers (80) */
+ int pid_status;
+ int resume; /* Resume (on/off) */
+
+ /* keep alive */
+ int keep_alive; /* it's a persisten connection ? */
+ int max_keep_alive_request; /* max persistent connections to allow */
+ int keep_alive_timeout; /* persistent connection timeout */
+
+ /* counter of threads working */
+ int thread_counter;
+ /* real user */
+ uid_t egid;
+ gid_t euid;
+
+ /* max ip */
+ int max_ip;
+
+ struct dir_html_theme *dir_theme;
+
+ /* configured host quantity */
+ int nhosts;
+ struct host *hosts;
+
+ mode_t open_flags;
+ struct plugin_stages *plugins;
};
struct server_config *config;
-struct host {
- char *file; /* configuration file */
- char *servername; /* host name */
+struct host
+{
+ char *file; /* configuration file */
+ char *servername; /* host name */
mk_pointer documentroot;
- char *access_log_path; /* access log file */
- char *error_log_path; /* error log file */
- int getdir; /* allow show directory info ? */
+ char *access_log_path; /* access log file */
+ char *error_log_path; /* error log file */
+ int getdir; /* allow show directory info ? */
char *cgi_alias;
char *cgi_path;
int mk_conn_error(int socket);
int mk_conn_close(int socket);
int mk_conn_timeout(int socket);
-
#define MK_EPOLL_WAIT_TIMEOUT 3000
-#define MK_EPOLL_BEHAVIOR_DEFAULT 2
+#define MK_EPOLL_BEHAVIOR_DEFAULT 2
#define MK_EPOLL_BEHAVIOR_TRIGGERED 3
-typedef struct {
- int (*read)(int);
- int (*write)(int);
- int (*error)(int);
- int (*close)(int);
- int (*timeout)(int);
+typedef struct
+{
+ int (*read) (int);
+ int (*write) (int);
+ int (*error) (int);
+ int (*close) (int);
+ int (*timeout) (int);
} mk_epoll_handlers;
int mk_epoll_create(int max_events);
-void *mk_epoll_init(int efd, mk_epoll_handlers *handler, int max_events);
+void *mk_epoll_init(int efd, mk_epoll_handlers * handler, int max_events);
-mk_epoll_handlers *mk_epoll_set_handlers(void (*read)(int),
- void (*write)(int),
- void (*error)(int),
- void (*close)(int),
- void (*timeout)(int));
+mk_epoll_handlers *mk_epoll_set_handlers(void (*read) (int),
+ void (*write) (int),
+ void (*error) (int),
+ void (*close) (int),
+ void (*timeout) (int));
int mk_epoll_add_client(int efd, int socket, int mode);
int mk_epoll_socket_change_mode(int efd, int socket, int mode);
-
struct file_info
{
- off_t size;
- short int is_link;
- short int is_directory;
- short int exec_access;
- short int read_access;
- time_t last_modification;
+ off_t size;
+ short int is_link;
+ short int is_directory;
+ short int exec_access;
+ short int read_access;
+ time_t last_modification;
};
struct file_info *mk_file_get_info(char *path);
char *mk_file_to_buffer(char *path);
-
mk_pointer mk_header_last_modified;
int mk_header_send(int fd, struct client_request *cr,
- struct request *sr, struct log_info *s_log);
+ struct request *sr, struct log_info *s_log);
struct header_values *mk_header_create();
#endif
-
#define M_CLIENT_BAD_REQUEST 400
#define M_CLIENT_UNAUTH 401
-#define M_CLIENT_PAYMENT_NEEDED 402 /* Wtf?! :-) */
+#define M_CLIENT_PAYMENT_NEEDED 402 /* Wtf?! :-) */
#define M_CLIENT_FORBIDDEN 403
#define M_CLIENT_NOT_FOUND 404
#define M_CLIENT_METHOD_NOT_ALLOWED 405
mk_pointer mk_iov_none;
mk_pointer mk_iov_equal;
-struct mk_iov
+struct mk_iov
{
- struct iovec *io;
- char **buf_to_free;
- int iov_idx;
- int buf_idx;
- int size;
- unsigned long total_len;
+ struct iovec *io;
+ char **buf_to_free;
+ int iov_idx;
+ int buf_idx;
+ int size;
+ unsigned long total_len;
};
struct mk_iov *mk_iov_create(int n, int offset);
-int mk_iov_add_entry(struct mk_iov *mk_io, char *buf,
+int mk_iov_add_entry(struct mk_iov *mk_io, char *buf,
int len, mk_pointer sep, int free);
int mk_iov_add_separator(struct mk_iov *mk_io, mk_pointer sep);
void mk_iov_free(struct mk_iov *mk_io);
-int _mk_iov_add(struct mk_iov *mk_io, char *buf, int len,
+int _mk_iov_add(struct mk_iov *mk_io, char *buf, int len,
mk_pointer sep, int free, int idx);
void _mk_iov_set_free(struct mk_iov *mk_io, char *buf);
-int mk_iov_set_entry(struct mk_iov *mk_io, char *buf, int len,
+int mk_iov_set_entry(struct mk_iov *mk_io, char *buf, int len,
int free, int idx);
void mk_iov_separators_init();
void mk_iov_print(struct mk_iov *mk_io);
#endif
-
/* logfile.c */
pthread_key_t timer;
-struct log_target {
- int fd;
- char *target;
- struct log_target *next;
+struct log_target
+{
+ int fd;
+ char *target;
+ struct log_target *next;
};
struct log_target *lt;
-struct log_info {
- int method;
- int protocol;
+struct log_info
+{
+ int method;
+ int protocol;
- mk_pointer uri;
- mk_pointer ip;
+ mk_pointer uri;
+ mk_pointer ip;
- int final_response; /* Ok: 200, Not Found 400, etc... */
- int size;
- mk_pointer size_p;
- int status; /* on/off : 301. */
- mk_pointer error_msg;
-
- struct host *host_conf;
+ int final_response; /* Ok: 200, Not Found 400, etc... */
+ int size;
+ mk_pointer size_p;
+ int status; /* on/off : 301. */
+ mk_pointer error_msg;
+
+ struct host *host_conf;
};
-int mk_logger_write_log(struct client_request *cr, struct log_info *log, struct host *h);
+int mk_logger_write_log(struct client_request *cr, struct log_info *log,
+ struct host *h);
int mk_logger_register_pid();
int mk_logger_remove_pid();
struct log_target *mk_logger_match(int fd);
#endif
-
typedef struct
{
- char *data;
- unsigned long len;
+ char *data;
+ unsigned long len;
} mk_pointer;
struct list_sint
{
- unsigned short int index;
- mk_pointer value;
- struct list_sint *next;
+ unsigned short int index;
+ mk_pointer value;
+ struct list_sint *next;
};
typedef struct list_sint mk_list_sint_t;
void *mk_mem_malloc(size_t size);
extern void *mk_mem_malloc_z(size_t size);
-void *mk_mem_realloc(void* ptr, size_t size);
+void *mk_mem_realloc(void *ptr, size_t size);
void mk_mem_free(void *ptr);
void mk_mem_pointers_init();
/* mk_pointer_* */
mk_pointer mk_pointer_create(char *buf, long init, long end);
-void mk_pointer_free(mk_pointer *p);
-void mk_pointer_reset(mk_pointer *p);
+void mk_pointer_free(mk_pointer * p);
+void mk_pointer_reset(mk_pointer * p);
void mk_pointer_print(mk_pointer p);
char *mk_pointer_to_buf(mk_pointer p);
-void mk_pointer_set(mk_pointer *p, char *data);
+void mk_pointer_set(mk_pointer * p, char *data);
#endif
-
int mk_method_post(struct client_request *cr, struct request *sr);
mk_pointer mk_method_post_get_vars(char *body, int index);
long int mk_method_post_content_length(char *body);
-
-
#define MAX_MIMETYPES_TIPO 55
#define MAX_SCRIPT_BIN_PATH 255
-struct mimetype {
- char *name;
- mk_pointer type;
- char *script_bin_path;
- struct mimetype *next;
-} *first_mime;
+struct mimetype
+{
+ char *name;
+ mk_pointer type;
+ char *script_bin_path;
+ struct mimetype *next;
+} *first_mime;
struct mimetype *mimetype_default;
int mk_mimetype_free(char **arr);
int mk_mimetype_add(char *name, char *type, char *bin_path);
-struct mimetype *mk_mimetype_find(mk_pointer *filename);
+struct mimetype *mk_mimetype_find(mk_pointer * filename);
struct mimetype *mk_mimetype_cmp(char *name);
-
-
int server_fd;
/* Max buf length sent by client */
-#define MAX_REQUEST_BODY 1024
+#define MAX_REQUEST_BODY 1024
/* Max Path lenth */
-#define MAX_PATH 1024
+#define MAX_PATH 1024
/* Send_Header(...,int cgi) */
#define SH_NOCGI 0
int thread_counter;
/* Thread mutexes */
-pthread_mutex_t mutex_thread_list;
-pthread_mutex_t mutex_thread_counter;
-pthread_mutex_t mutex_cgi_child;
-pthread_mutex_t mutex_logfile;
-pthread_mutex_t mutex_wait_register;
+pthread_mutex_t mutex_thread_list;
+pthread_mutex_t mutex_thread_counter;
+pthread_mutex_t mutex_cgi_child;
+pthread_mutex_t mutex_logfile;
+pthread_mutex_t mutex_wait_register;
mk_pointer mk_monkey_protocol;
mk_pointer mk_monkey_port;
*/
#ifndef MK_PLUGIN_H
-#define MK_PLUGIN_H
+#define MK_PLUGIN_H
#include "request.h"
#include "memory.h"
#define MK_PLUGIN_LOAD "plugins.load"
-#define MK_PLUGIN_ERROR -1 /* plugin execution error */
+#define MK_PLUGIN_ERROR -1 /* plugin execution error */
#define MK_PLUGIN_
-#define MK_PLUGIN_STAGE_00 ((__uint32_t) 0) /* Dummy plugin */
-#define MK_PLUGIN_STAGE_10 ((__uint32_t) 1) /* Before server's loop */
-#define MK_PLUGIN_STAGE_20 ((__uint32_t) 2) /* Accepted connection */
-#define MK_PLUGIN_STAGE_30 ((__uint32_t) 4) /* Connection assigned */
-#define MK_PLUGIN_STAGE_40 ((__uint32_t) 8) /* Object Handler */
-#define MK_PLUGIN_STAGE_50 ((__uint32_t) 16) /* Request ended */
-#define MK_PLUGIN_STAGE_60 ((__uint32_t) 32) /* Connection closed */
+#define MK_PLUGIN_STAGE_00 ((__uint32_t) 0) /* Dummy plugin */
+#define MK_PLUGIN_STAGE_10 ((__uint32_t) 1) /* Before server's loop */
+#define MK_PLUGIN_STAGE_20 ((__uint32_t) 2) /* Accepted connection */
+#define MK_PLUGIN_STAGE_30 ((__uint32_t) 4) /* Connection assigned */
+#define MK_PLUGIN_STAGE_40 ((__uint32_t) 8) /* Object Handler */
+#define MK_PLUGIN_STAGE_50 ((__uint32_t) 16) /* Request ended */
+#define MK_PLUGIN_STAGE_60 ((__uint32_t) 32) /* Connection closed */
#define MK_PLUGIN_RET_NOT_ME -1
#define MK_PLUGIN_RET_OWNER 100
#define MK_PLUGIN_RET_CLOSE_CONX 200
#define MK_PLUGIN_RET_CONTINUE 300
-struct plugin_stages {
- struct plugin *stage_00;
- struct plugin *stage_10;
- struct plugin *stage_20;
- struct plugin *stage_30;
- struct plugin *stage_40;
- struct plugin *stage_50;
- struct plugin *stage_60;
+struct plugin_stages
+{
+ struct plugin *stage_00;
+ struct plugin *stage_10;
+ struct plugin *stage_20;
+ struct plugin *stage_30;
+ struct plugin *stage_40;
+ struct plugin *stage_50;
+ struct plugin *stage_60;
};
-struct plugin_list {
- struct plugin *p;
- struct plugin_list *next;
+struct plugin_list
+{
+ struct plugin *p;
+ struct plugin_list *next;
};
struct plugin_list *plg_list;
-struct plugin {
- char *shortname;
- char *name;
- char *version;
- char *path;
- void *handler;
- __uint32_t *stages;
-
- /* Plugin external functions */
- int (*call_init)(void *api, char *confdir);
- int (*call_worker_init)();
- int (*call_stage_10)();
- int (*call_stage_20)(unsigned int,
- struct sched_connection *,
- struct client_request *);
- int (*call_stage_30)(struct client_request *, struct request *);
- int (*call_stage_40)(struct client_request *, struct request *);
-
- struct plugin *next;
+struct plugin
+{
+ char *shortname;
+ char *name;
+ char *version;
+ char *path;
+ void *handler;
+ __uint32_t *stages;
+
+ /* Plugin external functions */
+ int (*call_init) (void *api, char *confdir);
+ int (*call_worker_init) ();
+ int (*call_stage_10) ();
+ int (*call_stage_20) (unsigned int,
+ struct sched_connection *, struct client_request *);
+ int (*call_stage_30) (struct client_request *, struct request *);
+ int (*call_stage_40) (struct client_request *, struct request *);
+
+ struct plugin *next;
};
-struct plugin_api {
- struct server_config *config;
- struct plugin_list *plugins;
- struct sched_list_node **sched_list;
-
- /* Exporting Functions */
- void *(*mem_alloc)(int);
- void *(*mem_alloc_z)(int);
- void *(*mem_free)(void *);
- void *(*str_build)(char **, unsigned long *, const char *, ...);
- void *(*str_dup)(const char *);
- void *(*str_search)(char *, char *);
- void *(*str_search_n)(char *, char *, int);
- void *(*str_copy_substr)(const char *, int, int);
- void *(*str_split_line)(const char *);
- void *(*file_to_buffer)(char *);
- void *(*file_get_info)(char *);
- void *(*header_send)(int,
- struct client_request *,
- struct request *,
- struct log_info *);
- void *(*iov_create)(int, int);
- void *(*iov_free)(struct mk_iov *);
- void *(*iov_add_entry)(struct mk_iov *,
- char *,
- int,
- mk_pointer,
- int);
- void *(*iov_set_entry)(struct mk_iov*,
- char *,
- int,
- int,
- int);
- void *(*iov_send)(int, struct mk_iov *, int);
- void *(*iov_print)(struct mk_iov *);
- void *(*pointer_set)(mk_pointer *, char *);
- void *(*pointer_print)(mk_pointer);
- void *(*plugin_load_symbol)(void *, char *);
- void *(*socket_cork_flag)(int, int);
- void *(*socket_set_tcp_nodelay)(int);
- void *(*socket_connect)(int, char *, int);
- void *(*socket_create)();
- void *(*config_create)(char *);
- void *(*config_free)(struct mk_config *);
- void *(*config_getval)(struct mk_config *, char *, int);
- void *(*sched_get_connection)(struct sched_list_node *, int);
+struct plugin_api
+{
+ struct server_config *config;
+ struct plugin_list *plugins;
+ struct sched_list_node **sched_list;
+
+ /* Exporting Functions */
+ void *(*mem_alloc) (int);
+ void *(*mem_alloc_z) (int);
+ void *(*mem_free) (void *);
+ void *(*str_build) (char **, unsigned long *, const char *, ...);
+ void *(*str_dup) (const char *);
+ void *(*str_search) (char *, char *);
+ void *(*str_search_n) (char *, char *, int);
+ void *(*str_copy_substr) (const char *, int, int);
+ void *(*str_split_line) (const char *);
+ void *(*file_to_buffer) (char *);
+ void *(*file_get_info) (char *);
+ void *(*header_send) (int,
+ struct client_request *,
+ struct request *, struct log_info *);
+ void *(*iov_create) (int, int);
+ void *(*iov_free) (struct mk_iov *);
+ void *(*iov_add_entry) (struct mk_iov *, char *, int, mk_pointer, int);
+ void *(*iov_set_entry) (struct mk_iov *, char *, int, int, int);
+ void *(*iov_send) (int, struct mk_iov *, int);
+ void *(*iov_print) (struct mk_iov *);
+ void *(*pointer_set) (mk_pointer *, char *);
+ void *(*pointer_print) (mk_pointer);
+ void *(*plugin_load_symbol) (void *, char *);
+ void *(*socket_cork_flag) (int, int);
+ void *(*socket_set_tcp_nodelay) (int);
+ void *(*socket_connect) (int, char *, int);
+ void *(*socket_create) ();
+ void *(*config_create) (char *);
+ void *(*config_free) (struct mk_config *);
+ void *(*config_getval) (struct mk_config *, char *, int);
+ void *(*sched_get_connection) (struct sched_list_node *, int);
};
typedef char mk_plugin_data_t[];
int mk_plugin_stage_run(mk_plugin_stage_t stage,
unsigned int socket,
struct sched_connection *conx,
- struct client_request *cr,
- struct request *sr);
+ struct client_request *cr, struct request *sr);
void mk_plugin_worker_startup();
#endif
#define P_BROKEN_PIPE 1
/* Struct to register info about the thread childs (clients) */
-struct process {
- pthread_t thread_pid;
- int socket;
- char *ip_client;
- struct client_request *cr;
- struct process *next;
-} *first_process;
+struct process
+{
+ pthread_t thread_pid;
+ int socket;
+ char *ip_client;
+ struct client_request *cr;
+ struct process *next;
+} *first_process;
struct process *RegProc(pthread_t thread, int socket);
-int FreeThread(pthread_t thread);
+int FreeThread(pthread_t thread);
/* Handle index file names: index.* */
#define MAX_INDEX_NOMBRE 50
-struct indexfile {
- char indexname[MAX_INDEX_NOMBRE];
- struct indexfile *next;
-} *first_index;
+struct indexfile
+{
+ char indexname[MAX_INDEX_NOMBRE];
+ struct indexfile *next;
+} *first_index;
#define MK_CRLF "\r\n"
#define MK_ENDBLOCK "\r\n\r\n"
#define EXIT_PCONNECTION 24
/* Request error messages for log file */
-#define ERROR_MSG_400 "[error 400] Bad Request"
+#define ERROR_MSG_400 "[error 400] Bad Request"
#define ERROR_MSG_403 "[error 403] Forbidden"
#define ERROR_MSG_404 "[error 404] Not Found"
#define ERROR_MSG_405 "[error 405] Method Not Allowed"
struct request_idx
{
- struct client_request *first;
- struct client_request *last;
+ struct client_request *first;
+ struct client_request *last;
};
struct client_request
{
- int pipelined; /* Pipelined request */
- int socket;
- int counter_connections; /* Count persistent connections */
- int status; /* Request status */
- char *body; /* Original request sent */
-
- char *ipv4;
-
- int body_length;
-
- int first_block_end;
- int first_method;
-
- time_t init_time;
- struct request *request; /* Parsed request */
- struct client_request *next;
+ int pipelined; /* Pipelined request */
+ int socket;
+ int counter_connections; /* Count persistent connections */
+ int status; /* Request status */
+ char *body; /* Original request sent */
+
+ char *ipv4;
+
+ int body_length;
+
+ int first_block_end;
+ int first_method;
+
+ time_t init_time;
+ struct request *request; /* Parsed request */
+ struct client_request *next;
};
pthread_key_t request_index;
-struct header_toc {
- char *init;
- char *end;
- int status; /* 0: not found, 1: found = skip! */
- struct header_toc *next;
+struct header_toc
+{
+ char *init;
+ char *end;
+ int status; /* 0: not found, 1: found = skip! */
+ struct header_toc *next;
};
/* Request plugin Handler, each request can be handled by
* several plugins, we handle list in a simple list */
-struct handler {
- struct plugin *p;
- struct handler *next;
+struct handler
+{
+ struct plugin *p;
+ struct handler *next;
};
-struct request {
- int status;
- int pipelined; /* Pipelined request */
- mk_pointer body;
-
- /*----First header of client request--*/
- int method;
- mk_pointer method_p;
- mk_pointer uri; /* original request */
- char *uri_processed; /* processed request */
- int uri_twin;
-
- int protocol;
+struct request
+{
+ int status;
+ int pipelined; /* Pipelined request */
+ mk_pointer body;
+
+ /*----First header of client request--*/
+ int method;
+ mk_pointer method_p;
+ mk_pointer uri; /* original request */
+ char *uri_processed; /* processed request */
+ int uri_twin;
+
+ int protocol;
/*------------------*/
- /*---Request headers--*/
- int content_length;
- mk_pointer accept;
- mk_pointer accept_language;
- mk_pointer accept_encoding;
- mk_pointer accept_charset;
- mk_pointer content_type;
- mk_pointer connection;
- mk_pointer cookies;
- mk_pointer host;
- mk_pointer if_modified_since;
- mk_pointer last_modified_since;
- mk_pointer range;
- mk_pointer referer;
- mk_pointer resume;
- mk_pointer user_agent;
- /*---------------------*/
-
- /* POST */
- mk_pointer post_variables;
- /*-----------------*/
-
- /*-Internal-*/
- mk_pointer real_path; /* Absolute real path */
- char *user_uri; /* ~user/...path */
- mk_pointer query_string; /* ?... */
-
- char *virtual_user; /* Virtualhost user */
- char *script_filename;
- int keep_alive;
- int user_home; /* user_home request(VAR_ON/VAR_OFF) */
-
- /*-Connection-*/
- int port;
- /*------------*/
-
- int make_log;
- int cgi_pipe[2];
-
- /* file descriptors */
- int fd_file;
-
- struct file_info *file_info;
- struct host *host_conf;
- struct log_info *log; /* Request Log */
- struct header_values *headers; /* headers response */
- struct request *next;
-
- long loop;
- long bytes_to_send;
- off_t bytes_offset;
-
- /* Plugin handlers */
- struct handler *handled_by;
+ /*---Request headers--*/
+ int content_length;
+ mk_pointer accept;
+ mk_pointer accept_language;
+ mk_pointer accept_encoding;
+ mk_pointer accept_charset;
+ mk_pointer content_type;
+ mk_pointer connection;
+ mk_pointer cookies;
+ mk_pointer host;
+ mk_pointer if_modified_since;
+ mk_pointer last_modified_since;
+ mk_pointer range;
+ mk_pointer referer;
+ mk_pointer resume;
+ mk_pointer user_agent;
+ /*---------------------*/
+
+ /* POST */
+ mk_pointer post_variables;
+ /*-----------------*/
+
+ /*-Internal-*/
+ mk_pointer real_path; /* Absolute real path */
+ char *user_uri; /* ~user/...path */
+ mk_pointer query_string; /* ?... */
+
+ char *virtual_user; /* Virtualhost user */
+ char *script_filename;
+ int keep_alive;
+ int user_home; /* user_home request(VAR_ON/VAR_OFF) */
+
+ /*-Connection-*/
+ int port;
+ /*------------*/
+
+ int make_log;
+ int cgi_pipe[2];
+
+ /* file descriptors */
+ int fd_file;
+
+ struct file_info *file_info;
+ struct host *host_conf;
+ struct log_info *log; /* Request Log */
+ struct header_values *headers; /* headers response */
+ struct request *next;
+
+ long loop;
+ long bytes_to_send;
+ off_t bytes_offset;
+
+ /* Plugin handlers */
+ struct handler *handled_by;
};
-struct header_values {
- int status;
+struct header_values
+{
+ int status;
- int content_length;
- mk_pointer content_length_p;
+ int content_length;
+ mk_pointer content_length_p;
- int cgi;
- int pconnections_left;
- int ranges[2];
- int transfer_encoding;
- int breakline;
+ int cgi;
+ int pconnections_left;
+ int ranges[2];
+ int transfer_encoding;
+ int breakline;
- mk_pointer content_type;
- mk_pointer last_modified;
- char *location;
+ mk_pointer content_type;
+ mk_pointer last_modified;
+ char *location;
};
/* Custom HTML Page for errors */
-mk_pointer *mk_request_set_default_page(char *title, mk_pointer message, char *signature);
+mk_pointer *mk_request_set_default_page(char *title, mk_pointer message,
+ char *signature);
int mk_request_header_process(struct request *sr);
mk_pointer mk_request_header_find(struct header_toc *toc, int toc_len,
char *request_body, mk_pointer header);
-void mk_request_error(int num_error, struct client_request *cr,
- struct request *s_request, int debug,
- struct log_info *s_log);
+void mk_request_error(int num_error, struct client_request *cr,
+ struct request *s_request, int debug,
+ struct log_info *s_log);
struct request *mk_request_alloc();
void mk_request_free_list(struct client_request *cr);
#define MK_SCHEDULER_CONN_PROCESS 1
-struct sched_connection {
- int socket;
- int status;
- char ipv4[16];
- time_t arrive_time;
+struct sched_connection
+{
+ int socket;
+ int status;
+ char ipv4[16];
+ time_t arrive_time;
};
/* Global struct */
-struct sched_list_node{
- short int idx;
- pthread_t tid;
- pid_t pid;
- int epoll_fd;
- int active_requests;
- int closed_requests;
- struct sched_connection *queue;
- struct client_request *request_handler;
- struct sched_list_node *next;
+struct sched_list_node
+{
+ short int idx;
+ pthread_t tid;
+ pid_t pid;
+ int epoll_fd;
+ int active_requests;
+ int closed_requests;
+ struct sched_connection *queue;
+ struct client_request *request_handler;
+ struct sched_list_node *next;
};
struct sched_list_node *sched_list;
/* Struct under thread context */
-typedef struct {
- int epoll_fd;
- int max_events;
+typedef struct
+{
+ int epoll_fd;
+ int max_events;
} sched_thread_conf;
pthread_key_t epoll_fd;
int mk_sched_check_timeouts(struct sched_list_node *sched);
int mk_sched_add_client(struct sched_list_node *sched, int remote_fd);
int mk_sched_remove_client(struct sched_list_node *sched, int remote_fd);
-struct sched_connection *mk_sched_get_connection(struct sched_list_node *sched,
- int remote_fd);
-int mk_sched_update_conn_status(struct sched_list_node *sched,
- int remote_fd, int status);
+struct sched_connection *mk_sched_get_connection(struct sched_list_node
+ *sched, int remote_fd);
+int mk_sched_update_conn_status(struct sched_list_node *sched, int remote_fd,
+ int status);
#endif
*/
/* signals.c */
-void mk_signal_handler(int signo);
-void mk_signal_init();
-void mk_signal_term();
+void mk_signal_handler(int signo);
+void mk_signal_init();
+void mk_signal_term();
void mk_signal_thread_sigpipe_safe();
-
-
int mk_socket_get_ip(int socket, char *ipv4);
int mk_socket_close(int socket);
-int mk_socket_timeout(int s, char *buf, int len,
- int timeout, int recv_send);
+int mk_socket_timeout(int s, char *buf, int len, int timeout, int recv_send);
int mk_socket_create();
int mk_socket_connect(int sockfd, char *server, int port);
int mk_socket_server(int port);
#endif
-
#ifndef MK_STR_H
#define MK_STR_H
-struct mk_string_line {
- char *val;
- int len;
- struct mk_string_line *next;
+struct mk_string_line
+{
+ char *val;
+ int len;
+ struct mk_string_line *next;
};
char *mk_string_copy_substr(const char *string, int pos_init, int pos_end);
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
-#define MONKEY_HTTP_PROTOCOL HTTP_PROTOCOL_11 /* Protocolo oficial */
-
-
+#define MONKEY_HTTP_PROTOCOL HTTP_PROTOCOL_11 /* Protocolo oficial */
/* utils.c */
int SendFile(int socket, struct client_request *cr, struct request *request);
int AccessFile(struct stat file);
-int ExecFile(char *pathfile);
-int hex2int(char *pChars);
+int ExecFile(char *pathfile);
+int hex2int(char *pChars);
char *strstr2(char *s, char *t);
mk_pointer PutDate_string(time_t date);
char *get_real_string(mk_pointer req_uri);
-char *get_name_protocol(int remote_protocol);
+char *get_name_protocol(int remote_protocol);
-char *m_build_buffer(char **buffer, unsigned long *len, const char *format, ...);
+char *m_build_buffer(char **buffer, unsigned long *len, const char *format,
+ ...);
-int mk_buffer_cat(mk_pointer *p, char *buf1, char *buf2);
+int mk_buffer_cat(mk_pointer * p, char *buf1, char *buf2);
#define SYML_NOT -1
#define SYML_OK 0
mk_pointer mk_utils_int2mkp(int n);
#endif
-
#include <pthread.h>
-pthread_t mk_worker_spawn(void (*func)(void *));
+pthread_t mk_worker_spawn(void (*func) (void *));
#endif
struct mk_iov *mk_iov_create(int n, int offset)
{
- struct mk_iov *iov;
+ struct mk_iov *iov;
- iov = mk_mem_malloc(sizeof(struct mk_iov));
- iov->iov_idx = offset;
- iov->io = mk_mem_malloc_z(n*sizeof(struct iovec));
- iov->buf_to_free = mk_mem_malloc(n*sizeof(char *));
- iov->buf_idx = 0;
- iov->total_len = 0;
- iov->size = n;
+ iov = mk_mem_malloc(sizeof(struct mk_iov));
+ iov->iov_idx = offset;
+ iov->io = mk_mem_malloc_z(n * sizeof(struct iovec));
+ iov->buf_to_free = mk_mem_malloc(n * sizeof(char *));
+ iov->buf_idx = 0;
+ iov->total_len = 0;
+ iov->size = n;
- return iov;
+ return iov;
}
int mk_iov_add_entry(struct mk_iov *mk_io, char *buf, int len,
- mk_pointer sep, int free)
+ mk_pointer sep, int free)
{
- if(buf){
- mk_io->io[mk_io->iov_idx].iov_base = (unsigned char *) buf;
- mk_io->io[mk_io->iov_idx].iov_len = len;
- mk_io->iov_idx++;
- mk_io->total_len += len;
- }
+ if (buf) {
+ mk_io->io[mk_io->iov_idx].iov_base = (unsigned char *) buf;
+ mk_io->io[mk_io->iov_idx].iov_len = len;
+ mk_io->iov_idx++;
+ mk_io->total_len += len;
+ }
#ifdef DEBUG_IOV
- if(mk_io->iov_idx > mk_io->size){
- printf("\nDEBUG IOV :: ERROR, Broke array size in:");
- printf("\n '''%s'''", buf);
- fflush(stdout);
- }
+ if (mk_io->iov_idx > mk_io->size) {
+ printf("\nDEBUG IOV :: ERROR, Broke array size in:");
+ printf("\n '''%s'''", buf);
+ fflush(stdout);
+ }
#endif
- /* Add separator */
- if(sep.len > 0)
- {
- mk_io->io[mk_io->iov_idx].iov_base = sep.data;
- mk_io->io[mk_io->iov_idx].iov_len = sep.len;
- mk_io->iov_idx++;
- mk_io->total_len += sep.len;
- }
-
- if(free == MK_IOV_FREE_BUF)
- {
- _mk_iov_set_free(mk_io, buf);
- }
+ /* Add separator */
+ if (sep.len > 0) {
+ mk_io->io[mk_io->iov_idx].iov_base = sep.data;
+ mk_io->io[mk_io->iov_idx].iov_len = sep.len;
+ mk_io->iov_idx++;
+ mk_io->total_len += sep.len;
+ }
- return mk_io->iov_idx;
+ if (free == MK_IOV_FREE_BUF) {
+ _mk_iov_set_free(mk_io, buf);
+ }
+
+ return mk_io->iov_idx;
}
-int mk_iov_set_entry(struct mk_iov *mk_io, char *buf, int len,
+int mk_iov_set_entry(struct mk_iov *mk_io, char *buf, int len,
int free, int idx)
{
- mk_io->io[idx].iov_base = buf;
- mk_io->io[idx].iov_len = len;
- mk_io->total_len += len;
+ mk_io->io[idx].iov_base = buf;
+ mk_io->io[idx].iov_len = len;
+ mk_io->total_len += len;
- if(free == MK_IOV_FREE_BUF)
- {
- _mk_iov_set_free(mk_io, buf);
- }
+ if (free == MK_IOV_FREE_BUF) {
+ _mk_iov_set_free(mk_io, buf);
+ }
- return 0;
+ return 0;
}
void _mk_iov_set_free(struct mk_iov *mk_io, char *buf)
{
- mk_io->buf_to_free[mk_io->buf_idx] = (char *)buf;
- mk_io->buf_idx++;
+ mk_io->buf_to_free[mk_io->buf_idx] = (char *) buf;
+ mk_io->buf_idx++;
}
ssize_t mk_iov_send(int fd, struct mk_iov *mk_io, int to)
{
- ssize_t n=-1;
-
- if(to==MK_IOV_SEND_TO_SOCKET){
- n = writev(fd, mk_io->io, mk_io->iov_idx);
- if(n<0){
- /*
- perror("writev");
- switch(errno){
- case EBADF:
- printf("\nEBADF");
- break;
- case EFAULT:
- printf("\nEFAULT");
- break;
- case EFBIG:
- printf("\nEFBIG");
- break;
- case EINTR:
- printf("\nEINTR");
- break;
- case EINVAL:
- printf("\nEINVAL");
- break;
- case EIO:
- printf("\nEIO");
- break;
- case ENOMEM:
- printf("\nENOMEM");
- break;
- case ENOSPC:
- printf("\nENOSPC");
- break;
- case ENXIO:
- printf("\nENXIO");
- break;
-
- }
- fflush(stdout);
- */
- return n;
- }
+ ssize_t n = -1;
+
+ if (to == MK_IOV_SEND_TO_SOCKET) {
+ n = writev(fd, mk_io->io, mk_io->iov_idx);
+ if (n < 0) {
+ /*
+ perror("writev");
+ switch(errno){
+ case EBADF:
+ printf("\nEBADF");
+ break;
+ case EFAULT:
+ printf("\nEFAULT");
+ break;
+ case EFBIG:
+ printf("\nEFBIG");
+ break;
+ case EINTR:
+ printf("\nEINTR");
+ break;
+ case EINVAL:
+ printf("\nEINVAL");
+ break;
+ case EIO:
+ printf("\nEIO");
+ break;
+ case ENOMEM:
+ printf("\nENOMEM");
+ break;
+ case ENOSPC:
+ printf("\nENOSPC");
+ break;
+ case ENXIO:
+ printf("\nENXIO");
+ break;
+
+ }
+ fflush(stdout);
+ */
+ return n;
}
- else if(to==MK_IOV_SEND_TO_PIPE){
- /* for some reason, vmsplice is not working as expected for us,
- * maybe we need to fix something here, at the moment
- * we will keep using writev to push the iovec struct to the pipe
- *
- *
- * n = vmsplice(fd,
- * (const struct iovec *) mk_io->io,
- * mk_io->iov_idx,
- * SPLICE_F_GIFT);
- * return n;
- */
-
- n = writev(fd, mk_io->io, mk_io->iov_idx);
-
- if(n<0){
- perror("writev");
- }
+ }
+ else if (to == MK_IOV_SEND_TO_PIPE) {
+ /* for some reason, vmsplice is not working as expected for us,
+ * maybe we need to fix something here, at the moment
+ * we will keep using writev to push the iovec struct to the pipe
+ *
+ *
+ * n = vmsplice(fd,
+ * (const struct iovec *) mk_io->io,
+ * mk_io->iov_idx,
+ * SPLICE_F_GIFT);
+ * return n;
+ */
+
+ n = writev(fd, mk_io->io, mk_io->iov_idx);
+
+ if (n < 0) {
+ perror("writev");
}
+ }
- return n;
+ return n;
}
void mk_iov_free(struct mk_iov *mk_io)
{
- mk_iov_free_marked(mk_io);
- mk_mem_free(mk_io->buf_to_free);
- mk_mem_free(mk_io->io);
- mk_mem_free(mk_io);
+ mk_iov_free_marked(mk_io);
+ mk_mem_free(mk_io->buf_to_free);
+ mk_mem_free(mk_io->io);
+ mk_mem_free(mk_io);
}
+
void mk_iov_free_marked(struct mk_iov *mk_io)
{
- int i, limit=0;
+ int i, limit = 0;
- limit = mk_io->buf_idx;
+ limit = mk_io->buf_idx;
- for(i=0; i<limit; i++)
- {
+ for (i = 0; i < limit; i++) {
#ifdef DEBUG_IOV
- printf("\nDEBUG IOV :: going free (idx: %i/%i): %s",i,
- limit, mk_io->buf_to_free[i]);
- fflush(stdout);
+ printf("\nDEBUG IOV :: going free (idx: %i/%i): %s", i,
+ limit, mk_io->buf_to_free[i]);
+ fflush(stdout);
#endif
- mk_mem_free(mk_io->buf_to_free[i]);
- }
+ mk_mem_free(mk_io->buf_to_free[i]);
+ }
- mk_io->iov_idx = 0;
- mk_io->buf_idx = 0;
+ mk_io->iov_idx = 0;
+ mk_io->buf_idx = 0;
}
void mk_iov_print(struct mk_iov *mk_io)
{
- int i;
+ int i;
- for(i=0; i<mk_io->iov_idx; i++){
- printf("\n%i len=%i)\n'%s'", i, mk_io->io[i].iov_len,
- (char *) mk_io->io[i].iov_base);
- fflush(stdout);
- }
+ for (i = 0; i < mk_io->iov_idx; i++) {
+ printf("\n%i len=%i)\n'%s'", i, mk_io->io[i].iov_len,
+ (char *) mk_io->io[i].iov_base);
+ fflush(stdout);
+ }
}
void mk_iov_separators_init()
{
- mk_pointer_set(&mk_iov_crlf, MK_IOV_CRLF);
- mk_pointer_set(&mk_iov_lf, MK_IOV_LF);
- mk_pointer_set(&mk_iov_space, MK_IOV_SPACE);
- mk_pointer_set(&mk_iov_header_value, MK_IOV_HEADER_VALUE);
- mk_pointer_set(&mk_iov_slash, MK_IOV_SLASH);
- mk_pointer_set(&mk_iov_none, MK_IOV_NONE);
- mk_pointer_set(&mk_iov_equal, MK_IOV_EQUAL);
+ mk_pointer_set(&mk_iov_crlf, MK_IOV_CRLF);
+ mk_pointer_set(&mk_iov_lf, MK_IOV_LF);
+ mk_pointer_set(&mk_iov_space, MK_IOV_SPACE);
+ mk_pointer_set(&mk_iov_header_value, MK_IOV_HEADER_VALUE);
+ mk_pointer_set(&mk_iov_slash, MK_IOV_SLASH);
+ mk_pointer_set(&mk_iov_none, MK_IOV_NONE);
+ mk_pointer_set(&mk_iov_equal, MK_IOV_EQUAL);
}
void mk_logger_target_add(int fd, char *target)
{
- struct log_target *new, *aux;
+ struct log_target *new, *aux;
- new = mk_mem_malloc(sizeof(struct log_target));
- new->fd = fd;
- new->target = target;
- new->next = NULL;
+ new = mk_mem_malloc(sizeof(struct log_target));
+ new->fd = fd;
+ new->target = target;
+ new->next = NULL;
- if(!lt)
- {
- lt = new;
- return;
- }
+ if (!lt) {
+ lt = new;
+ return;
+ }
- aux = lt;
- while(aux->next)
- aux = aux->next;
+ aux = lt;
+ while (aux->next)
+ aux = aux->next;
- aux->next = new;
+ aux->next = new;
}
struct log_target *mk_logger_match(int fd)
{
- struct log_target *aux;
+ struct log_target *aux;
- aux = lt;
+ aux = lt;
- while(aux)
- {
- if(aux->fd == fd)
- {
- return aux;
- }
- aux = aux->next;
+ while (aux) {
+ if (aux->fd == fd) {
+ return aux;
}
+ aux = aux->next;
+ }
- return NULL;
+ return NULL;
}
void *mk_logger_worker_init(void *args)
{
- int efd, max_events=config->nhosts;
- int i, bytes, err;
- struct log_target *target=0;
- struct host *h=0;
- int flog;
- long slen;
- int timeout;
- int clk;
-
- /* pipe_size:
- * ----------
- * Linux set a pipe size usingto the PAGE_SIZE,
- * check linux/include/pipe_fs_i.h for details:
- *
- * #define PIPE_SIZE PAGE_SIZE
- *
- * In the same header file we can found that every
- * pipe has 16 pages, so our real memory allocation
- * is: (PAGE_SIZE*PIPE_BUFFERS)
- */
- long pipe_size;
-
- /* buffer_limit:
- * -------------
- * it means the maximum data that a monkey log pipe can contain.
- */
- long buffer_limit;
-
- /* Monkey allow just 75% of a pipe capacity */
- pipe_size = sysconf(_SC_PAGESIZE)*16;
- buffer_limit = (pipe_size*0.75);
-
- /* Creating poll */
- efd = mk_epoll_create(max_events);
-
- h = config->hosts;
- while(h)
- {
- /* Add access log file */
- mk_epoll_add_client(efd, h->log_access[0],
- MK_EPOLL_BEHAVIOR_DEFAULT);
- mk_logger_target_add(h->log_access[0], h->access_log_path);
-
- /* Add error log file */
- mk_epoll_add_client(efd, h->log_error[0],
- MK_EPOLL_BEHAVIOR_DEFAULT);
- mk_logger_target_add(h->log_error[0], h->error_log_path);
-
- h = h->next;
+ int efd, max_events = config->nhosts;
+ int i, bytes, err;
+ struct log_target *target = 0;
+ struct host *h = 0;
+ int flog;
+ long slen;
+ int timeout;
+ int clk;
+
+ /* pipe_size:
+ * ----------
+ * Linux set a pipe size usingto the PAGE_SIZE,
+ * check linux/include/pipe_fs_i.h for details:
+ *
+ * #define PIPE_SIZE PAGE_SIZE
+ *
+ * In the same header file we can found that every
+ * pipe has 16 pages, so our real memory allocation
+ * is: (PAGE_SIZE*PIPE_BUFFERS)
+ */
+ long pipe_size;
+
+ /* buffer_limit:
+ * -------------
+ * it means the maximum data that a monkey log pipe can contain.
+ */
+ long buffer_limit;
+
+ /* Monkey allow just 75% of a pipe capacity */
+ pipe_size = sysconf(_SC_PAGESIZE) * 16;
+ buffer_limit = (pipe_size * 0.75);
+
+ /* Creating poll */
+ efd = mk_epoll_create(max_events);
+
+ h = config->hosts;
+ while (h) {
+ /* Add access log file */
+ mk_epoll_add_client(efd, h->log_access[0], MK_EPOLL_BEHAVIOR_DEFAULT);
+ mk_logger_target_add(h->log_access[0], h->access_log_path);
+
+ /* Add error log file */
+ mk_epoll_add_client(efd, h->log_error[0], MK_EPOLL_BEHAVIOR_DEFAULT);
+ mk_logger_target_add(h->log_error[0], h->error_log_path);
+
+ h = h->next;
+ }
+
+ timeout = time(NULL) + 3;
+
+ /* Reading pipe buffer */
+ while (1) {
+ usleep(1200);
+
+ struct epoll_event events[max_events];
+ int num_fds = epoll_wait(efd, events, max_events, -1);
+
+ clk = log_current_utime;
+ if (!h) {
+ h = config->hosts;
}
- timeout = time(NULL) + 3;
-
- /* Reading pipe buffer */
- while(1)
- {
- usleep(1200);
+ for (i = 0; i < num_fds; i++) {
+ target = mk_logger_match(events[i].data.fd);
+
+ if (!target) {
+ printf("\nERROR matching host/epoll_fd");
+ fflush(stdout);
+ continue;
+ }
+
+ err = ioctl(target->fd, FIONREAD, &bytes);
+ if (err == -1) {
+ perror("err");
+ }
- struct epoll_event events[max_events];
- int num_fds = epoll_wait(efd, events, max_events, -1);
- clk = log_current_utime;
- if(!h)
- {
- h = config->hosts;
+ if (bytes < buffer_limit && clk <= timeout) {
+ break;
+ }
+ else {
+ timeout = clk + 3;
+ flog = open(target->target, O_WRONLY | O_CREAT, 0644);
+
+ if (flog == -1) {
+ printf("\n* error: check your logfile file permission");
+ perror("open");
+ continue;
}
- for(i=0; i< num_fds; i++)
- {
- target = mk_logger_match(events[i].data.fd);
-
- if(!target)
- {
- printf("\nERROR matching host/epoll_fd");
- fflush(stdout);
- continue;
- }
-
- err = ioctl(target->fd, FIONREAD, &bytes);
- if(err == -1)
- {
- perror("err");
- }
-
-
- if(bytes < buffer_limit && clk<=timeout)
- {
- break;
- }
- else
- {
- timeout = clk+3;
- flog = open(target->target,
- O_WRONLY | O_CREAT , 0644);
-
- if(flog==-1)
- {
- printf("\n* error: check your logfile file permission");
- perror("open");
- continue;
- }
-
- lseek(flog, 0, SEEK_END);
- slen = splice(events[i].data.fd, NULL, flog,
- NULL, bytes, SPLICE_F_MOVE);
- if(slen==-1)
- {
- perror("splice");
- }
- close(flog);
- }
+ lseek(flog, 0, SEEK_END);
+ slen = splice(events[i].data.fd, NULL, flog,
+ NULL, bytes, SPLICE_F_MOVE);
+ if (slen == -1) {
+ perror("splice");
}
+ close(flog);
+ }
}
+ }
}
struct mk_iov *mk_logger_iov_get()
{
- return pthread_getspecific(mk_cache_iov_log);
+ return pthread_getspecific(mk_cache_iov_log);
}
void mk_logger_iov_free(struct mk_iov *iov)
{
- mk_iov_free_marked(iov);
+ mk_iov_free_marked(iov);
}
/* Registra en archivos de logs: accesos
y errores */
-int mk_logger_write_log(struct client_request *cr, struct log_info *log, struct host *h)
+int mk_logger_write_log(struct client_request *cr, struct log_info *log,
+ struct host *h)
{
- struct mk_iov *iov;
- mk_pointer *status, method, protocol;
- struct sched_list_node *sched;
- struct sched_connection *conx;
-
- if(log->status!=S_LOG_ON)
- {
- return 0;
- }
-
- iov = mk_logger_iov_get();
-
- sched = mk_sched_get_thread_conf();
- conx = mk_sched_get_connection(sched, cr->socket);
-
- /* client IP address */
- mk_iov_add_entry(iov, conx->ipv4, 16,
- mk_logfile_iov_dash, MK_IOV_NOT_FREE_BUF);
-
- /* Date/time when object was requested */
- mk_iov_add_entry(iov, log_current_time.data, log_current_time.len,
- mk_iov_space,
+ struct mk_iov *iov;
+ mk_pointer *status, method, protocol;
+ struct sched_list_node *sched;
+ struct sched_connection *conx;
+
+ if (log->status != S_LOG_ON) {
+ return 0;
+ }
+
+ iov = mk_logger_iov_get();
+
+ sched = mk_sched_get_thread_conf();
+ conx = mk_sched_get_connection(sched, cr->socket);
+
+ /* client IP address */
+ mk_iov_add_entry(iov, conx->ipv4, 16,
+ mk_logfile_iov_dash, MK_IOV_NOT_FREE_BUF);
+
+ /* Date/time when object was requested */
+ mk_iov_add_entry(iov, log_current_time.data, log_current_time.len,
+ mk_iov_space, MK_IOV_NOT_FREE_BUF);
+
+ /* Register a successfull request */
+ if (log->final_response == M_HTTP_OK
+ || log->final_response == M_REDIR_MOVED_T) {
+ /* HTTP method required */
+ method = mk_http_method_check_str(log->method);
+ mk_iov_add_entry(iov, method.data, method.len, mk_iov_space,
MK_IOV_NOT_FREE_BUF);
- /* Register a successfull request */
- if(log->final_response==M_HTTP_OK || log->final_response==M_REDIR_MOVED_T)
- {
- /* HTTP method required */
- method = mk_http_method_check_str(log->method);
- mk_iov_add_entry(iov, method.data, method.len, mk_iov_space,
- MK_IOV_NOT_FREE_BUF);
-
- /* HTTP URI required */
- mk_iov_add_entry(iov, log->uri.data, log->uri.len,
- mk_iov_space, MK_IOV_NOT_FREE_BUF);
-
-
- if(log->protocol)
- {
- protocol = mk_http_protocol_check_str(log->protocol);
- mk_iov_add_entry(iov, protocol.data, protocol.len,
- mk_iov_space,
- MK_IOV_NOT_FREE_BUF);
- }
+ /* HTTP URI required */
+ mk_iov_add_entry(iov, log->uri.data, log->uri.len,
+ mk_iov_space, MK_IOV_NOT_FREE_BUF);
- /* HTTP status code */
- status = (mk_pointer *)
- mk_http_status_get(log->final_response);
- mk_iov_add_entry(iov, status->data, status->len,
- mk_iov_space,
- MK_IOV_NOT_FREE_BUF);
-
- /* object size */
- mk_iov_add_entry(iov,
- log->size_p.data,
- log->size_p.len,
- mk_iov_lf,
- MK_IOV_NOT_FREE_BUF);
-
- /* Send info to pipe */
- mk_iov_send(h->log_access[1], iov, MK_IOV_SEND_TO_PIPE);
- }
- else{ /* Register some error */
- mk_iov_add_entry(iov,
- log->error_msg.data,
- log->error_msg.len,
- mk_iov_lf,
- MK_IOV_NOT_FREE_BUF);
- mk_iov_send(h->log_error[1], iov, MK_IOV_SEND_TO_PIPE);
+ if (log->protocol) {
+ protocol = mk_http_protocol_check_str(log->protocol);
+ mk_iov_add_entry(iov, protocol.data, protocol.len,
+ mk_iov_space, MK_IOV_NOT_FREE_BUF);
}
- mk_logger_iov_free(iov);
- return 0;
+
+ /* HTTP status code */
+ status = (mk_pointer *)
+ mk_http_status_get(log->final_response);
+ mk_iov_add_entry(iov, status->data, status->len,
+ mk_iov_space, MK_IOV_NOT_FREE_BUF);
+
+ /* object size */
+ mk_iov_add_entry(iov,
+ log->size_p.data,
+ log->size_p.len, mk_iov_lf, MK_IOV_NOT_FREE_BUF);
+
+ /* Send info to pipe */
+ mk_iov_send(h->log_access[1], iov, MK_IOV_SEND_TO_PIPE);
+ }
+ else { /* Register some error */
+ mk_iov_add_entry(iov,
+ log->error_msg.data,
+ log->error_msg.len, mk_iov_lf, MK_IOV_NOT_FREE_BUF);
+ mk_iov_send(h->log_error[1], iov, MK_IOV_SEND_TO_PIPE);
+
+ }
+ mk_logger_iov_free(iov);
+ return 0;
}
/* Write Monkey's PID */
int mk_logger_register_pid()
{
- FILE *pid_file;
-
- remove(config->pid_file_path);
- config->pid_status=VAR_OFF;
- if((pid_file=fopen(config->pid_file_path,"w"))==NULL){
- puts("Error: I can't log pid of monkey");
- exit(1);
- }
- fprintf(pid_file,"%i", getpid());
- fclose(pid_file);
- config->pid_status=VAR_ON;
-
- return 0;
+ FILE *pid_file;
+
+ remove(config->pid_file_path);
+ config->pid_status = VAR_OFF;
+ if ((pid_file = fopen(config->pid_file_path, "w")) == NULL) {
+ puts("Error: I can't log pid of monkey");
+ exit(1);
+ }
+ fprintf(pid_file, "%i", getpid());
+ fclose(pid_file);
+ config->pid_status = VAR_ON;
+
+ return 0;
}
/* Elimina log del PID */
int mk_logger_remove_pid()
{
- mk_user_undo_uidgid();
- return remove(config->pid_file_path);
+ mk_user_undo_uidgid();
+ return remove(config->pid_file_path);
}
void *mk_mem_malloc(size_t size)
{
- void *aux=0;
+ void *aux = 0;
- if((aux=malloc(size))==NULL){
- perror("malloc");
- return NULL;
- }
+ if ((aux = malloc(size)) == NULL) {
+ perror("malloc");
+ return NULL;
+ }
- return aux;
+ return aux;
}
extern void *mk_mem_malloc_z(size_t size)
{
- char *buf=0;
+ char *buf = 0;
- buf = mk_mem_malloc(size);
- if(!buf)
- {
- return NULL;
- }
+ buf = mk_mem_malloc(size);
+ if (!buf) {
+ return NULL;
+ }
- bzero(buf, size);
-
- return buf;
+ bzero(buf, size);
+
+ return buf;
}
-void *mk_mem_realloc(void* ptr, size_t size)
+void *mk_mem_realloc(void *ptr, size_t size)
{
- char *aux=0;
+ char *aux = 0;
- if((aux=realloc(ptr, size))==NULL){
- perror("realloc");
- return NULL;
- }
+ if ((aux = realloc(ptr, size)) == NULL) {
+ perror("realloc");
+ return NULL;
+ }
- return (void *) aux;
+ return (void *) aux;
}
-
+
void mk_mem_free(void *ptr)
{
- free(ptr);
+ free(ptr);
}
mk_pointer mk_pointer_create(char *buf, long init, long end)
{
- mk_pointer p;
+ mk_pointer p;
- mk_pointer_reset(&p);
- p.data = buf+init;
+ mk_pointer_reset(&p);
+ p.data = buf + init;
- if(init!=end)
- {
- p.len = (end - init);
- }
- else{
- p.len = 1;
- }
+ if (init != end) {
+ p.len = (end - init);
+ }
+ else {
+ p.len = 1;
+ }
- return p;
+ return p;
}
-void mk_pointer_reset(mk_pointer *p)
+void mk_pointer_reset(mk_pointer * p)
{
- p->data = NULL;
- p->len = 0;
+ p->data = NULL;
+ p->len = 0;
}
-void mk_pointer_free(mk_pointer *p)
+void mk_pointer_free(mk_pointer * p)
{
- mk_mem_free(p->data);
- p->len = 0;
+ mk_mem_free(p->data);
+ p->len = 0;
}
char *mk_pointer_to_buf(mk_pointer p)
{
- char *buf;
+ char *buf;
- buf = strndup(p.data, p.len);
- return (char *) buf;
+ buf = strndup(p.data, p.len);
+ return (char *) buf;
}
void mk_pointer_print(mk_pointer p)
{
- int i;
-
- printf("\nDEBUG MK_POINTER: '");
- for(i=0; i<p.len; i++){
- printf("%c", p.data[i]);
- }
- printf("'");
- fflush(stdout);
+ int i;
+
+ printf("\nDEBUG MK_POINTER: '");
+ for (i = 0; i < p.len; i++) {
+ printf("%c", p.data[i]);
+ }
+ printf("'");
+ fflush(stdout);
}
-void mk_pointer_set(mk_pointer *p, char *data)
+void mk_pointer_set(mk_pointer * p, char *data)
{
- p->data = data;
- p->len = strlen(data);
+ p->data = data;
+ p->len = strlen(data);
}
void mk_mem_pointers_init()
{
- /* Error messages */
- mk_pointer_set(&request_error_msg_400, ERROR_MSG_400);
- mk_pointer_set(&request_error_msg_403, ERROR_MSG_403);
- mk_pointer_set(&request_error_msg_404, ERROR_MSG_404);
- mk_pointer_set(&request_error_msg_405, ERROR_MSG_405);
- mk_pointer_set(&request_error_msg_408, ERROR_MSG_408);
- mk_pointer_set(&request_error_msg_411, ERROR_MSG_411);
- mk_pointer_set(&request_error_msg_500, ERROR_MSG_500);
- mk_pointer_set(&request_error_msg_501, ERROR_MSG_501);
- mk_pointer_set(&request_error_msg_505, ERROR_MSG_505);
-
- /* Short server response headers */
- mk_pointer_set(&mk_header_short_date, MK_HEADER_SHORT_DATE);
- mk_pointer_set(&mk_header_short_location, MK_HEADER_SHORT_LOCATION);
- mk_pointer_set(&mk_header_short_ct, MK_HEADER_SHORT_CT);
-
- /* Request vars */
- mk_pointer_set(&mk_crlf, MK_CRLF);
- mk_pointer_set(&mk_endblock, MK_ENDBLOCK);
-
- /* Client headers */
- mk_pointer_set(&mk_rh_accept, RH_ACCEPT);
- mk_pointer_set(&mk_rh_accept_charset, RH_ACCEPT_CHARSET);
- mk_pointer_set(&mk_rh_accept_encoding, RH_ACCEPT_ENCODING);
- mk_pointer_set(&mk_rh_accept_language, RH_ACCEPT_LANGUAGE);
- mk_pointer_set(&mk_rh_connection, RH_CONNECTION);
- mk_pointer_set(&mk_rh_cookie, RH_COOKIE);
- mk_pointer_set(&mk_rh_content_length, RH_CONTENT_LENGTH);
- mk_pointer_set(&mk_rh_content_range, RH_CONTENT_RANGE);
- mk_pointer_set(&mk_rh_content_type, RH_CONTENT_TYPE);
- mk_pointer_set(&mk_rh_if_modified_since, RH_IF_MODIFIED_SINCE);
- mk_pointer_set(&mk_rh_host, RH_HOST);
- mk_pointer_set(&mk_rh_last_modified, RH_LAST_MODIFIED);
- mk_pointer_set(&mk_rh_last_modified_since, RH_LAST_MODIFIED_SINCE);
- mk_pointer_set(&mk_rh_referer, RH_REFERER);
- mk_pointer_set(&mk_rh_range, RH_RANGE);
- mk_pointer_set(&mk_rh_user_agent, RH_USER_AGENT);
-
- /* Server response first header */
- mk_pointer_set(&mk_hr_http_ok, MK_HR_HTTP_OK);
- mk_pointer_set(&mk_hr_http_partial, MK_HR_HTTP_PARTIAL);
- mk_pointer_set(&mk_hr_redir_moved, MK_HR_REDIR_MOVED);
- mk_pointer_set(&mk_hr_redir_moved_t, MK_HR_REDIR_MOVED_T);
- mk_pointer_set(&mk_hr_not_modified, MK_HR_NOT_MODIFIED);
- mk_pointer_set(&mk_hr_client_bad_request, MK_HR_CLIENT_BAD_REQUEST);
- mk_pointer_set(&mk_hr_client_forbidden, MK_HR_CLIENT_FORBIDDEN);
- mk_pointer_set(&mk_hr_client_not_found, MK_HR_CLIENT_NOT_FOUND);
- mk_pointer_set(&mk_hr_client_method_not_allowed,
- MK_HR_CLIENT_METHOD_NOT_ALLOWED);
- mk_pointer_set(&mk_hr_client_request_timeout,
- MK_HR_CLIENT_REQUEST_TIMEOUT);
- mk_pointer_set(&mk_hr_client_length_required,
- MK_HR_CLIENT_LENGTH_REQUIRED);
- mk_pointer_set(&mk_hr_server_internal_error,
- MK_HR_SERVER_INTERNAL_ERROR);
- mk_pointer_set(&mk_hr_server_not_implemented,
- MK_HR_SERVER_NOT_IMPLEMENTED);
- mk_pointer_set(&mk_hr_server_http_version_unsup,
- MK_HR_SERVER_HTTP_VERSION_UNSUP);
-
- /* Server response normal headers */
- mk_pointer_set(&mk_header_conn_ka, MK_HEADER_CONN_KA);
- mk_pointer_set(&mk_header_conn_close, MK_HEADER_CONN_CLOSE);
- mk_pointer_set(&mk_header_accept_ranges, MK_HEADER_ACCEPT_RANGES);
- mk_pointer_set(&mk_header_te_chunked, MK_HEADER_TE_CHUNKED);
- mk_pointer_set(&mk_header_last_modified, MK_HEADER_LAST_MODIFIED);
-
- mk_http_status_list_init();
- mk_iov_separators_init();
+ /* Error messages */
+ mk_pointer_set(&request_error_msg_400, ERROR_MSG_400);
+ mk_pointer_set(&request_error_msg_403, ERROR_MSG_403);
+ mk_pointer_set(&request_error_msg_404, ERROR_MSG_404);
+ mk_pointer_set(&request_error_msg_405, ERROR_MSG_405);
+ mk_pointer_set(&request_error_msg_408, ERROR_MSG_408);
+ mk_pointer_set(&request_error_msg_411, ERROR_MSG_411);
+ mk_pointer_set(&request_error_msg_500, ERROR_MSG_500);
+ mk_pointer_set(&request_error_msg_501, ERROR_MSG_501);
+ mk_pointer_set(&request_error_msg_505, ERROR_MSG_505);
+
+ /* Short server response headers */
+ mk_pointer_set(&mk_header_short_date, MK_HEADER_SHORT_DATE);
+ mk_pointer_set(&mk_header_short_location, MK_HEADER_SHORT_LOCATION);
+ mk_pointer_set(&mk_header_short_ct, MK_HEADER_SHORT_CT);
+
+ /* Request vars */
+ mk_pointer_set(&mk_crlf, MK_CRLF);
+ mk_pointer_set(&mk_endblock, MK_ENDBLOCK);
+
+ /* Client headers */
+ mk_pointer_set(&mk_rh_accept, RH_ACCEPT);
+ mk_pointer_set(&mk_rh_accept_charset, RH_ACCEPT_CHARSET);
+ mk_pointer_set(&mk_rh_accept_encoding, RH_ACCEPT_ENCODING);
+ mk_pointer_set(&mk_rh_accept_language, RH_ACCEPT_LANGUAGE);
+ mk_pointer_set(&mk_rh_connection, RH_CONNECTION);
+ mk_pointer_set(&mk_rh_cookie, RH_COOKIE);
+ mk_pointer_set(&mk_rh_content_length, RH_CONTENT_LENGTH);
+ mk_pointer_set(&mk_rh_content_range, RH_CONTENT_RANGE);
+ mk_pointer_set(&mk_rh_content_type, RH_CONTENT_TYPE);
+ mk_pointer_set(&mk_rh_if_modified_since, RH_IF_MODIFIED_SINCE);
+ mk_pointer_set(&mk_rh_host, RH_HOST);
+ mk_pointer_set(&mk_rh_last_modified, RH_LAST_MODIFIED);
+ mk_pointer_set(&mk_rh_last_modified_since, RH_LAST_MODIFIED_SINCE);
+ mk_pointer_set(&mk_rh_referer, RH_REFERER);
+ mk_pointer_set(&mk_rh_range, RH_RANGE);
+ mk_pointer_set(&mk_rh_user_agent, RH_USER_AGENT);
+
+ /* Server response first header */
+ mk_pointer_set(&mk_hr_http_ok, MK_HR_HTTP_OK);
+ mk_pointer_set(&mk_hr_http_partial, MK_HR_HTTP_PARTIAL);
+ mk_pointer_set(&mk_hr_redir_moved, MK_HR_REDIR_MOVED);
+ mk_pointer_set(&mk_hr_redir_moved_t, MK_HR_REDIR_MOVED_T);
+ mk_pointer_set(&mk_hr_not_modified, MK_HR_NOT_MODIFIED);
+ mk_pointer_set(&mk_hr_client_bad_request, MK_HR_CLIENT_BAD_REQUEST);
+ mk_pointer_set(&mk_hr_client_forbidden, MK_HR_CLIENT_FORBIDDEN);
+ mk_pointer_set(&mk_hr_client_not_found, MK_HR_CLIENT_NOT_FOUND);
+ mk_pointer_set(&mk_hr_client_method_not_allowed,
+ MK_HR_CLIENT_METHOD_NOT_ALLOWED);
+ mk_pointer_set(&mk_hr_client_request_timeout,
+ MK_HR_CLIENT_REQUEST_TIMEOUT);
+ mk_pointer_set(&mk_hr_client_length_required,
+ MK_HR_CLIENT_LENGTH_REQUIRED);
+ mk_pointer_set(&mk_hr_server_internal_error, MK_HR_SERVER_INTERNAL_ERROR);
+ mk_pointer_set(&mk_hr_server_not_implemented,
+ MK_HR_SERVER_NOT_IMPLEMENTED);
+ mk_pointer_set(&mk_hr_server_http_version_unsup,
+ MK_HR_SERVER_HTTP_VERSION_UNSUP);
+
+ /* Server response normal headers */
+ mk_pointer_set(&mk_header_conn_ka, MK_HEADER_CONN_KA);
+ mk_pointer_set(&mk_header_conn_close, MK_HEADER_CONN_CLOSE);
+ mk_pointer_set(&mk_header_accept_ranges, MK_HEADER_ACCEPT_RANGES);
+ mk_pointer_set(&mk_header_te_chunked, MK_HEADER_TE_CHUNKED);
+ mk_pointer_set(&mk_header_last_modified, MK_HEADER_LAST_MODIFIED);
+
+ mk_http_status_list_init();
+ mk_iov_separators_init();
// mk_palm_set_env();
- /* Server */
- mk_pointer_set(&mk_monkey_protocol, HTTP_PROTOCOL_11_STR);
- // mk_pointer_set(&mk_monkey_port, config->port);
+ /* Server */
+ mk_pointer_set(&mk_monkey_protocol, HTTP_PROTOCOL_11_STR);
+ // mk_pointer_set(&mk_monkey_port, config->port);
- /* Logfile */
- mk_pointer_set(&mk_logfile_iov_dash, MK_LOGFILE_IOV_DASH);
+ /* Logfile */
+ mk_pointer_set(&mk_logfile_iov_dash, MK_LOGFILE_IOV_DASH);
- /* HTTP */
- mk_pointer_set(&mk_http_method_get_p, HTTP_METHOD_GET_STR);
- mk_pointer_set(&mk_http_method_post_p, HTTP_METHOD_POST_STR);
- mk_pointer_set(&mk_http_method_head_p, HTTP_METHOD_HEAD_STR);
- mk_pointer_reset(&mk_http_method_null_p);
+ /* HTTP */
+ mk_pointer_set(&mk_http_method_get_p, HTTP_METHOD_GET_STR);
+ mk_pointer_set(&mk_http_method_post_p, HTTP_METHOD_POST_STR);
+ mk_pointer_set(&mk_http_method_head_p, HTTP_METHOD_HEAD_STR);
+ mk_pointer_reset(&mk_http_method_null_p);
- mk_pointer_set(&mk_http_protocol_09_p, HTTP_PROTOCOL_09_STR);
- mk_pointer_set(&mk_http_protocol_10_p, HTTP_PROTOCOL_10_STR);
- mk_pointer_set(&mk_http_protocol_11_p, HTTP_PROTOCOL_11_STR);
- mk_pointer_reset(&mk_http_protocol_null_p);
+ mk_pointer_set(&mk_http_protocol_09_p, HTTP_PROTOCOL_09_STR);
+ mk_pointer_set(&mk_http_protocol_10_p, HTTP_PROTOCOL_10_STR);
+ mk_pointer_set(&mk_http_protocol_11_p, HTTP_PROTOCOL_11_STR);
+ mk_pointer_reset(&mk_http_protocol_null_p);
- /* User Home */
- mk_pointer_set(&mk_user_home, MK_USER_HOME);
+ /* User Home */
+ mk_pointer_set(&mk_user_home, MK_USER_HOME);
}
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
-
+
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
long int mk_method_post_content_length(char *body)
{
- struct header_toc* toc = NULL;
- long int len;
- mk_pointer tmp;
-
- /* obs: Table of Content (toc) is created when the full
- * request has arrived, this function cannot be used from
- * mk_http_pending_request().
- */
- toc = pthread_getspecific(mk_cache_header_toc);
- tmp = mk_request_header_find(toc, MK_KNOWN_HEADERS, body,
- mk_rh_content_length);
-
- if(!tmp.data)
- {
- return -1;
- }
-
- len = atoi(tmp.data);
-
- return len;
+ struct header_toc *toc = NULL;
+ long int len;
+ mk_pointer tmp;
+
+ /* obs: Table of Content (toc) is created when the full
+ * request has arrived, this function cannot be used from
+ * mk_http_pending_request().
+ */
+ toc = pthread_getspecific(mk_cache_header_toc);
+ tmp = mk_request_header_find(toc, MK_KNOWN_HEADERS, body,
+ mk_rh_content_length);
+
+ if (!tmp.data) {
+ return -1;
+ }
+
+ len = atoi(tmp.data);
+
+ return len;
}
/* POST METHOD */
int mk_method_post(struct client_request *cr, struct request *sr)
{
- struct header_toc* toc = NULL;
- mk_pointer tmp;
- char buffer[MAX_REQUEST_BODY];
- long content_length_post=0;
-
- content_length_post = mk_method_post_content_length(cr->body);
-
- if(content_length_post == -1)
- {
- mk_request_error(M_CLIENT_LENGTH_REQUIRED,
- cr, sr, 0, sr->log);
- return -1;
- }
-
- if(content_length_post<=0 || content_length_post >=MAX_REQUEST_BODY){
- mk_request_error(M_CLIENT_BAD_REQUEST,
- cr, sr, 0, sr->log);
- return -1;
- }
-
- toc = pthread_getspecific(mk_cache_header_toc);
- tmp = mk_request_header_find(toc, MK_KNOWN_HEADERS, sr->body.data,
- mk_rh_content_type);
-
- if(!tmp.data){
- mk_request_error(M_CLIENT_BAD_REQUEST,
- cr, sr, 0, sr->log);
- return -1;
- }
- sr->content_type = tmp;
-
- if(sr->post_variables.len < content_length_post){
- content_length_post=strlen(buffer);
- }
-
- sr->content_length=content_length_post;
-
- return 0;
-
+ struct header_toc *toc = NULL;
+ mk_pointer tmp;
+ char buffer[MAX_REQUEST_BODY];
+ long content_length_post = 0;
+
+ content_length_post = mk_method_post_content_length(cr->body);
+
+ if (content_length_post == -1) {
+ mk_request_error(M_CLIENT_LENGTH_REQUIRED, cr, sr, 0, sr->log);
+ return -1;
+ }
+
+ if (content_length_post <= 0 || content_length_post >= MAX_REQUEST_BODY) {
+ mk_request_error(M_CLIENT_BAD_REQUEST, cr, sr, 0, sr->log);
+ return -1;
+ }
+
+ toc = pthread_getspecific(mk_cache_header_toc);
+ tmp = mk_request_header_find(toc, MK_KNOWN_HEADERS, sr->body.data,
+ mk_rh_content_type);
+
+ if (!tmp.data) {
+ mk_request_error(M_CLIENT_BAD_REQUEST, cr, sr, 0, sr->log);
+ return -1;
+ }
+ sr->content_type = tmp;
+
+ if (sr->post_variables.len < content_length_post) {
+ content_length_post = strlen(buffer);
+ }
+
+ sr->content_length = content_length_post;
+
+ return 0;
+
}
/* Return POST variables sent in request */
mk_pointer mk_method_post_get_vars(char *body, int index)
{
- long len=1;
- char *str=0;
- mk_pointer p;
+ long len = 1;
+ char *str = 0;
+ mk_pointer p;
- str = mk_string_copy_substr(body, index, strlen(body));
- if(str)
- {
- len = strlen(str);
- }
+ str = mk_string_copy_substr(body, index, strlen(body));
+ if (str) {
+ len = strlen(str);
+ }
- p.data = str;
- p.len = len;
+ p.data = str;
+ p.len = len;
- return p;
+ return p;
}
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
-
+
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
/* Carga en estructura los mimetypes */
void mk_mimetype_read_config()
{
- char buffer[255], path[MAX_PATH];
- char *name=0,*type=0, *last=0;
- FILE *mime_file;
-
- snprintf(path,MAX_PATH,"%s/monkey.mime", config->serverconf);
-
- if((mime_file=fopen(path,"r"))==NULL ) {
- puts("Error: I can't open monkey.mime file");
- exit(1);
- }
-
- /* Rutina que carga en memoria los mime types */
- while( fgets(buffer,255,mime_file) ) {
- int len;
- len = strlen(buffer);
- if(buffer[len-1] == '\n') {
- buffer[--len] = 0;
- if(len && buffer[len-1] == '\r')
- buffer[--len] = 0;
- }
-
- name = strtok_r(buffer, "\"\t ", &last);
- type = strtok_r(NULL, "\"\t ", &last);
-
- if (!name || !type) continue;
- if (buffer[0] == '#') continue;
-
- if(mk_mimetype_add(name,type,NULL)!=0)
- puts("Error loading Mime Types");
- }
- fclose(mime_file);
-
- /* Set default mime type */
- mimetype_default = mk_mem_malloc_z(sizeof(struct mimetype));
- mimetype_default->name = MIMETYPE_DEFAULT_NAME;
- mk_pointer_set(&mimetype_default->type, MIMETYPE_DEFAULT_TYPE);
- mimetype_default->script_bin_path = NULL;
- mimetype_default->next = NULL;
+ char buffer[255], path[MAX_PATH];
+ char *name = 0, *type = 0, *last = 0;
+ FILE *mime_file;
+
+ snprintf(path, MAX_PATH, "%s/monkey.mime", config->serverconf);
+
+ if ((mime_file = fopen(path, "r")) == NULL) {
+ puts("Error: I can't open monkey.mime file");
+ exit(1);
+ }
+
+ /* Rutina que carga en memoria los mime types */
+ while (fgets(buffer, 255, mime_file)) {
+ int len;
+ len = strlen(buffer);
+ if (buffer[len - 1] == '\n') {
+ buffer[--len] = 0;
+ if (len && buffer[len - 1] == '\r')
+ buffer[--len] = 0;
+ }
+
+ name = strtok_r(buffer, "\"\t ", &last);
+ type = strtok_r(NULL, "\"\t ", &last);
+
+ if (!name || !type)
+ continue;
+ if (buffer[0] == '#')
+ continue;
+
+ if (mk_mimetype_add(name, type, NULL) != 0)
+ puts("Error loading Mime Types");
+ }
+ fclose(mime_file);
+
+ /* Set default mime type */
+ mimetype_default = mk_mem_malloc_z(sizeof(struct mimetype));
+ mimetype_default->name = MIMETYPE_DEFAULT_NAME;
+ mk_pointer_set(&mimetype_default->type, MIMETYPE_DEFAULT_TYPE);
+ mimetype_default->script_bin_path = NULL;
+ mimetype_default->next = NULL;
}
int mk_mimetype_add(char *name, char *type, char *bin_path)
{
-
- struct mimetype *new_mime, *aux_mime;
-
- new_mime = mk_mem_malloc_z(sizeof(struct mimetype));
-
- new_mime->name = mk_string_dup(name);
- mk_pointer_set(&new_mime->type, mk_string_dup(type));
- new_mime->script_bin_path = mk_string_dup(bin_path);
-
- new_mime->next=NULL;
-
- if(first_mime==NULL)
- first_mime=new_mime;
- else {
- aux_mime=first_mime;
- while(aux_mime->next!=NULL)
- aux_mime=aux_mime->next;
- aux_mime->next = new_mime;
- }
- return 0;
+
+ struct mimetype *new_mime, *aux_mime;
+
+ new_mime = mk_mem_malloc_z(sizeof(struct mimetype));
+
+ new_mime->name = mk_string_dup(name);
+ mk_pointer_set(&new_mime->type, mk_string_dup(type));
+ new_mime->script_bin_path = mk_string_dup(bin_path);
+
+ new_mime->next = NULL;
+
+ if (first_mime == NULL)
+ first_mime = new_mime;
+ else {
+ aux_mime = first_mime;
+ while (aux_mime->next != NULL)
+ aux_mime = aux_mime->next;
+ aux_mime->next = new_mime;
+ }
+ return 0;
}
-struct mimetype *mk_mimetype_find(mk_pointer *filename)
+struct mimetype *mk_mimetype_find(mk_pointer * filename)
{
- int j, len;
+ int j, len;
- j = len = filename->len;
+ j = len = filename->len;
- /* looking for extension */
- while(filename->data[j]!='.' && j>=0)
- j--;
+ /* looking for extension */
+ while (filename->data[j] != '.' && j >= 0)
+ j--;
- if(j==0){
- return NULL;
- }
+ if (j == 0) {
+ return NULL;
+ }
- return mk_mimetype_cmp(filename->data+j+1);
+ return mk_mimetype_cmp(filename->data + j + 1);
}
/* Busca mime type segun Request */
struct mimetype *mk_mimetype_cmp(char *name)
{
- struct mimetype *aux_mime;
-
- aux_mime=first_mime;
- while(aux_mime!=NULL) {
- if(strcasecmp(aux_mime->name,name)==0) {
- return aux_mime;
- }
- else
- aux_mime=aux_mime->next;
- }
+ struct mimetype *aux_mime;
- return NULL;
+ aux_mime = first_mime;
+ while (aux_mime != NULL) {
+ if (strcasecmp(aux_mime->name, name) == 0) {
+ return aux_mime;
+ }
+ else
+ aux_mime = aux_mime->next;
+ }
+
+ return NULL;
}
int mk_mimetype_free(char **arr)
{
- mk_mem_free(arr);
- return 0;
+ mk_mem_free(arr);
+ return 0;
}
-
#include <string.h>
#include "monkey.h"
-
-#define TAG_CLOSE 0
+
+#define TAG_CLOSE 0
#define TAG_OPEN 1
void Mod_Error()
{
- puts("Configuration Error in modules.conf");
- exit(1);
+ puts("Configuration Error in modules.conf");
+ exit(1);
}
char **MOD_Read_Config(char *mod_name)
{
- int i, flag=TAG_CLOSE; /* 0: YES , 1: NO */
- int vars_counter=0;
- char *tag_module_open, *tag_module_close;
- char *path=0, buffer[255];
- char *variable=0, *value=0, *last=0;
- char **arg=0, **ptr=0;
- FILE *configfile;
-
- ptr = arg = (char **) M_malloc(sizeof(char *) * 10);
-
- path=m_build_buffer("%s/modules.conf", config->serverconf);
-
- if( (configfile=fopen(path,"r"))==NULL ) {
- puts("Error: I can't open modules.conf file.");
- exit(1);
- }
-
- tag_module_open = (char *) m_build_buffer("<%s>", mod_name);
- tag_module_close = (char *) m_build_buffer("</%s>", mod_name);
-
- while(!feof(configfile)) {
- fgets(buffer,255,configfile);
-
- if(buffer[0] == '#' || buffer[0] == '\n' || buffer[0] == '\r')
- continue;
-
- for(i=0; i<255 && buffer[i]!='\0'; i++)
- if(buffer[i] == '\n' || buffer[i] == '\r')
- buffer[i]='\0';
-
- variable = strtok_r(buffer, "\"\t ", &last);
- value = strtok_r(NULL, "\"\t ", &last);
-
- if(!variable || (!value && (strcasecmp(variable, tag_module_open)!=0) && (strcasecmp(variable, tag_module_close)!=0)))
- {
- continue;
- }
-
- if(strcasecmp(variable, tag_module_open)==0 && flag==TAG_CLOSE) {
- flag=TAG_OPEN;
- }
- else {
- if(strcasecmp(variable, tag_module_open)==0 && flag==TAG_OPEN) {
- Mod_Error();
- }
- }
-
- if(variable && vars_counter<10 && flag==TAG_OPEN){
- *ptr++ = M_CGI_env_add_var(variable, value);
- }
- }
-
- fclose(configfile);
- M_free(path);
- *ptr++ = '\0';
-
- return (char **) arg;
+ int i, flag = TAG_CLOSE; /* 0: YES , 1: NO */
+ int vars_counter = 0;
+ char *tag_module_open, *tag_module_close;
+ char *path = 0, buffer[255];
+ char *variable = 0, *value = 0, *last = 0;
+ char **arg = 0, **ptr = 0;
+ FILE *configfile;
+
+ ptr = arg = (char **) M_malloc(sizeof(char *) * 10);
+
+ path = m_build_buffer("%s/modules.conf", config->serverconf);
+
+ if ((configfile = fopen(path, "r")) == NULL) {
+ puts("Error: I can't open modules.conf file.");
+ exit(1);
+ }
+
+ tag_module_open = (char *) m_build_buffer("<%s>", mod_name);
+ tag_module_close = (char *) m_build_buffer("</%s>", mod_name);
+
+ while (!feof(configfile)) {
+ fgets(buffer, 255, configfile);
+
+ if (buffer[0] == '#' || buffer[0] == '\n' || buffer[0] == '\r')
+ continue;
+
+ for (i = 0; i < 255 && buffer[i] != '\0'; i++)
+ if (buffer[i] == '\n' || buffer[i] == '\r')
+ buffer[i] = '\0';
+
+ variable = strtok_r(buffer, "\"\t ", &last);
+ value = strtok_r(NULL, "\"\t ", &last);
+
+ if (!variable
+ || (!value && (strcasecmp(variable, tag_module_open) != 0)
+ && (strcasecmp(variable, tag_module_close) != 0))) {
+ continue;
+ }
+
+ if (strcasecmp(variable, tag_module_open) == 0 && flag == TAG_CLOSE) {
+ flag = TAG_OPEN;
+ }
+ else {
+ if (strcasecmp(variable, tag_module_open) == 0
+ && flag == TAG_OPEN) {
+ Mod_Error();
+ }
+ }
+
+ if (variable && vars_counter < 10 && flag == TAG_OPEN) {
+ *ptr++ = M_CGI_env_add_var(variable, value);
+ }
+ }
+
+ fclose(configfile);
+ M_free(path);
+ *ptr++ = '\0';
+
+ return (char **) arg;
}
char *MOD_get_ptr_value(char **ptr, char *var)
{
- int i=0, length, pos;
- char *buffer=0, *value=0;
-
- while(ptr[i]){
- buffer=m_build_buffer("%s", ptr[i]);
- pos = str_search( buffer, "=", 1);
- if(strncasecmp(var, buffer, pos)==0){
- length=strlen(buffer);
- value=malloc(length+1);
- strncpy(value, buffer + pos + 1, length - pos );
- value[length-pos]='\0';
- M_free(buffer);
- return (char *) value;
- }
- i++;
- M_free(buffer);
- M_free(value);
- }
- return (char *) "";
+ int i = 0, length, pos;
+ char *buffer = 0, *value = 0;
+
+ while (ptr[i]) {
+ buffer = m_build_buffer("%s", ptr[i]);
+ pos = str_search(buffer, "=", 1);
+ if (strncasecmp(var, buffer, pos) == 0) {
+ length = strlen(buffer);
+ value = malloc(length + 1);
+ strncpy(value, buffer + pos + 1, length - pos);
+ value[length - pos] = '\0';
+ M_free(buffer);
+ return (char *) value;
+ }
+ i++;
+ M_free(buffer);
+ M_free(value);
+ }
+ return (char *) "";
}
#include "plugin.h"
#if defined(__DATE__) && defined(__TIME__)
- static const char MONKEY_BUILT[] = __DATE__ " " __TIME__;
+static const char MONKEY_BUILT[] = __DATE__ " " __TIME__;
#else
- static const char MONKEY_BUILT[] = "Unknown";
+static const char MONKEY_BUILT[] = "Unknown";
#endif
void mk_details()
{
- printf("* Process ID is %i", getpid());
- printf("\n* Server socket listening on Port %i", config->serverport);
- printf("\n* %i threads, %i client connections per thread, total %i\n",
- config->workers, config->worker_capacity,
- config->workers*config->worker_capacity);
- fflush(stdout);
+ printf("* Process ID is %i", getpid());
+ printf("\n* Server socket listening on Port %i", config->serverport);
+ printf("\n* %i threads, %i client connections per thread, total %i\n",
+ config->workers, config->worker_capacity,
+ config->workers * config->worker_capacity);
+ fflush(stdout);
}
void mk_version()
-{
- printf("Monkey HTTP Daemon %s\n",VERSION);
- printf("Built : %s\n", MONKEY_BUILT);
- printf("Home : http://www.monkey-project.com\n");
- fflush(stdout);
+{
+ printf("Monkey HTTP Daemon %s\n", VERSION);
+ printf("Built : %s\n", MONKEY_BUILT);
+ printf("Home : http://www.monkey-project.com\n");
+ fflush(stdout);
}
void mk_help()
{
- printf("Usage : monkey [-c directory] [-D] [-v] [-h]\n\n");
- printf("Available options:\n");
- printf(" -b\t\trun Monkey in benchmark mode, limits are disabled\n");
- printf(" -c directory\tspecify directory from configuration files\n");
- printf(" -D\t\trun Monkey as daemon\n");
- printf(" -v\t\tshow version number\n");
- printf(" -h\t\tthis help\n\n");
- exit(0);
+ printf("Usage : monkey [-c directory] [-D] [-v] [-h]\n\n");
+ printf("Available options:\n");
+ printf(" -b\t\trun Monkey in benchmark mode, limits are disabled\n");
+ printf(" -c directory\tspecify directory from configuration files\n");
+ printf(" -D\t\trun Monkey as daemon\n");
+ printf(" -v\t\tshow version number\n");
+ printf(" -h\t\tthis help\n\n");
+ exit(0);
}
/* MAIN */
int main(int argc, char **argv)
{
- int opt;
- int daemon = 0;
-
- config = mk_mem_malloc(sizeof(struct server_config));
- config->file_config=0;
-
- opterr = 0;
- while ((opt = getopt(argc, argv, "DSvhc:")) != -1)
- {
- switch (opt) {
- case 'v':
- mk_version() ;
- exit(0);
- break;
- case 'h':
- mk_help();
- break;
- case 'D':
- daemon = 1;
- break;
- case 'c':
- if (strlen(optarg) != 0) {
- config->file_config=optarg;
- break;
- }
- case '?':
- printf("Monkey: Invalid option or option needs an argument.\n");
- mk_help();
- break;
- }
- }
-
- if(!config->file_config){
- config->file_config=MONKEY_PATH_CONF;
+ int opt;
+ int daemon = 0;
+
+ config = mk_mem_malloc(sizeof(struct server_config));
+ config->file_config = 0;
+
+ opterr = 0;
+ while ((opt = getopt(argc, argv, "DSvhc:")) != -1) {
+ switch (opt) {
+ case 'v':
+ mk_version();
+ exit(0);
+ break;
+ case 'h':
+ mk_help();
+ break;
+ case 'D':
+ daemon = 1;
+ break;
+ case 'c':
+ if (strlen(optarg) != 0) {
+ config->file_config = optarg;
+ break;
+ }
+ case '?':
+ printf("Monkey: Invalid option or option needs an argument.\n");
+ mk_help();
+ break;
}
+ }
- mk_version();
- mk_signal_init();
- mk_config_start_configure();
- mk_plugin_init();
+ if (!config->file_config) {
+ config->file_config = MONKEY_PATH_CONF;
+ }
- server_fd = mk_socket_server(config->serverport);
+ mk_version();
+ mk_signal_init();
+ mk_config_start_configure();
+ mk_plugin_init();
- /* Workers: logger and clock */
- mk_worker_spawn((void *)mk_logger_worker_init);
- mk_worker_spawn((void *)mk_clock_worker_init);
+ server_fd = mk_socket_server(config->serverport);
- /* Running Monkey as daemon */
- if(daemon)
- {
- mk_utils_set_daemon();
- }
+ /* Workers: logger and clock */
+ mk_worker_spawn((void *) mk_logger_worker_init);
+ mk_worker_spawn((void *) mk_clock_worker_init);
- /* Register PID of Monkey */
- mk_logger_register_pid();
+ /* Running Monkey as daemon */
+ if (daemon) {
+ mk_utils_set_daemon();
+ }
-
- mk_mem_pointers_init();
+ /* Register PID of Monkey */
+ mk_logger_register_pid();
- /* Create thread keys */
- pthread_key_create(&request_index, NULL);
- pthread_key_create(&epoll_fd, NULL);
- pthread_key_create(&timer, NULL);
- pthread_key_create(&mk_cache_iov_log, NULL);
- pthread_key_create(&mk_cache_iov_header, NULL);
- pthread_key_create(&mk_cache_header_toc, NULL);
- /* Change process owner */
- mk_user_set_uidgid();
+ mk_mem_pointers_init();
- mk_config_sanity_check();
+ /* Create thread keys */
+ pthread_key_create(&request_index, NULL);
+ pthread_key_create(&epoll_fd, NULL);
+ pthread_key_create(&timer, NULL);
+ pthread_key_create(&mk_cache_iov_log, NULL);
+ pthread_key_create(&mk_cache_iov_header, NULL);
+ pthread_key_create(&mk_cache_header_toc, NULL);
- /* Launch monkey http workers */
- mk_server_launch_workers();
+ /* Change process owner */
+ mk_user_set_uidgid();
- /* Print server details */
- mk_details();
+ mk_config_sanity_check();
- /* Plugins Stage 10 */
- mk_plugin_stage_run(MK_PLUGIN_STAGE_10, 0, NULL, NULL, NULL);
+ /* Launch monkey http workers */
+ mk_server_launch_workers();
- /* Server loop, let's listen for incomming clients */
- mk_server_loop(server_fd);
+ /* Print server details */
+ mk_details();
- return 0;
-}
+ /* Plugins Stage 10 */
+ mk_plugin_stage_run(MK_PLUGIN_STAGE_10, 0, NULL, NULL, NULL);
+ /* Server loop, let's listen for incomming clients */
+ mk_server_loop(server_fd);
+
+ return 0;
+}
-/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
+/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* Monkey HTTP Daemon
* ------------------
void *mk_plugin_load(char *path)
{
- void *handle;
-
- handle = dlopen(path, RTLD_LAZY);
- if(!handle){
- fprintf(stderr, "Error during dlopen(): %s\n", dlerror());
- exit(1);
- }
- return handle;
+ void *handle;
+
+ handle = dlopen(path, RTLD_LAZY);
+ if (!handle) {
+ fprintf(stderr, "Error during dlopen(): %s\n", dlerror());
+ exit(1);
+ }
+ return handle;
}
void *mk_plugin_load_symbol(void *handler, const char *symbol)
{
- char *err;
- void *s;
+ char *err;
+ void *s;
- dlerror();
- s = dlsym(handler, symbol);
- if((err = dlerror()) != NULL){
- return NULL;
- }
+ dlerror();
+ s = dlsym(handler, symbol);
+ if ((err = dlerror()) != NULL) {
+ return NULL;
+ }
- return s;
+ return s;
}
void mk_plugin_register_add_to_stage(struct plugin **st, struct plugin *p)
{
- struct plugin *list;
+ struct plugin *list;
- if(!*st){
- *st = p;
- return;
- }
+ if (!*st) {
+ *st = p;
+ return;
+ }
- list = *st;
+ list = *st;
- while(list->next){
- list = list->next;
- }
+ while (list->next) {
+ list = list->next;
+ }
- list->next = p;
+ list->next = p;
}
void mk_plugin_register_stages(struct plugin *p)
{
- struct plugin_list *new, *list;
-
- /* Main plugin list */
- new = mk_mem_malloc(sizeof(struct plugin_list));
- new->p = p;
- new->next = NULL;
-
- if(!plg_list){
- plg_list = new;
- }
- else{
- list = plg_list;
- while(list->next){
- list = list->next;
- }
- list->next = new;
+ struct plugin_list *new, *list;
+
+ /* Main plugin list */
+ new = mk_mem_malloc(sizeof(struct plugin_list));
+ new->p = p;
+ new->next = NULL;
+
+ if (!plg_list) {
+ plg_list = new;
+ }
+ else {
+ list = plg_list;
+ while (list->next) {
+ list = list->next;
}
+ list->next = new;
+ }
- /* Assign plugin to stages */
- if(*p->stages & MK_PLUGIN_STAGE_00){
- mk_plugin_register_add_to_stage(&config->plugins->stage_00, p);
- }
+ /* Assign plugin to stages */
+ if (*p->stages & MK_PLUGIN_STAGE_00) {
+ mk_plugin_register_add_to_stage(&config->plugins->stage_00, p);
+ }
- if(*p->stages & MK_PLUGIN_STAGE_10){
- mk_plugin_register_add_to_stage(&config->plugins->stage_10, p);
- }
+ if (*p->stages & MK_PLUGIN_STAGE_10) {
+ mk_plugin_register_add_to_stage(&config->plugins->stage_10, p);
+ }
- if(*p->stages & MK_PLUGIN_STAGE_20){
- mk_plugin_register_add_to_stage(&config->plugins->stage_20, p);
- }
+ if (*p->stages & MK_PLUGIN_STAGE_20) {
+ mk_plugin_register_add_to_stage(&config->plugins->stage_20, p);
+ }
- if(*p->stages & MK_PLUGIN_STAGE_30){
- mk_plugin_register_add_to_stage(&config->plugins->stage_30, p);
- }
+ if (*p->stages & MK_PLUGIN_STAGE_30) {
+ mk_plugin_register_add_to_stage(&config->plugins->stage_30, p);
+ }
- if(*p->stages & MK_PLUGIN_STAGE_40){
- mk_plugin_register_add_to_stage(&config->plugins->stage_40, p);
- }
+ if (*p->stages & MK_PLUGIN_STAGE_40) {
+ mk_plugin_register_add_to_stage(&config->plugins->stage_40, p);
+ }
- if(*p->stages & MK_PLUGIN_STAGE_50){
- mk_plugin_register_add_to_stage(&config->plugins->stage_50, p);
- }
+ if (*p->stages & MK_PLUGIN_STAGE_50) {
+ mk_plugin_register_add_to_stage(&config->plugins->stage_50, p);
+ }
- if(*p->stages & MK_PLUGIN_STAGE_60){
- mk_plugin_register_add_to_stage(&config->plugins->stage_60, p);
- }
+ if (*p->stages & MK_PLUGIN_STAGE_60) {
+ mk_plugin_register_add_to_stage(&config->plugins->stage_60, p);
+ }
}
void *mk_plugin_register(void *handler, char *path)
{
- struct plugin *p;
+ struct plugin *p;
- p = mk_mem_malloc_z(sizeof(struct plugin));
- p->shortname = mk_plugin_load_symbol(handler, "_shortname");
- p->name = mk_plugin_load_symbol(handler, "_name");
- p->version = mk_plugin_load_symbol(handler, "_version");
- p->path = mk_string_dup(path);
- p->handler = handler;
- p->stages = (mk_plugin_stage_t *) mk_plugin_load_symbol(handler, "_stages");
+ p = mk_mem_malloc_z(sizeof(struct plugin));
+ p->shortname = mk_plugin_load_symbol(handler, "_shortname");
+ p->name = mk_plugin_load_symbol(handler, "_name");
+ p->version = mk_plugin_load_symbol(handler, "_version");
+ p->path = mk_string_dup(path);
+ p->handler = handler;
+ p->stages =
+ (mk_plugin_stage_t *) mk_plugin_load_symbol(handler, "_stages");
- /* Plugin external function */
- p->call_init = (int (*)()) mk_plugin_load_symbol(handler,
- "_mk_plugin_init");
+ /* Plugin external function */
+ p->call_init = (int (*)()) mk_plugin_load_symbol(handler,
+ "_mk_plugin_init");
- p->call_worker_init = (int (*)()) mk_plugin_load_symbol(handler,
- "_mk_plugin_worker_init");
+ p->call_worker_init = (int (*)()) mk_plugin_load_symbol(handler,
+ "_mk_plugin_worker_init");
- p->call_stage_10 = (int (*)())
- mk_plugin_load_symbol(handler, "_mk_plugin_stage_00");
+ p->call_stage_10 = (int (*)())
+ mk_plugin_load_symbol(handler, "_mk_plugin_stage_00");
- p->call_stage_10 = (int (*)())
- mk_plugin_load_symbol(handler, "_mk_plugin_stage_10");
+ p->call_stage_10 = (int (*)())
+ mk_plugin_load_symbol(handler, "_mk_plugin_stage_10");
- p->call_stage_20 = (int (*)())
- mk_plugin_load_symbol(handler, "_mk_plugin_stage_20");
+ p->call_stage_20 = (int (*)())
+ mk_plugin_load_symbol(handler, "_mk_plugin_stage_20");
- p->call_stage_30 = (int (*)())
- mk_plugin_load_symbol(handler, "_mk_plugin_stage_30");
+ p->call_stage_30 = (int (*)())
+ mk_plugin_load_symbol(handler, "_mk_plugin_stage_30");
- p->call_stage_40 = (int (*)())
- mk_plugin_load_symbol(handler, "_mk_plugin_stage_40");
+ p->call_stage_40 = (int (*)())
+ mk_plugin_load_symbol(handler, "_mk_plugin_stage_40");
- p->next = NULL;
+ p->next = NULL;
- if(!p->name || !p->version || !p->stages){
- mk_mem_free(p);
- return NULL;
- }
+ if (!p->name || !p->version || !p->stages) {
+ mk_mem_free(p);
+ return NULL;
+ }
- mk_plugin_register_stages(p);
- return p;
+ mk_plugin_register_stages(p);
+ return p;
}
void mk_plugin_init()
{
- char *path;
- void *handle;
- struct plugin *p;
- struct plugin_api *api;
- struct plugin_list *plist;
- struct mk_config *cnf;
-
- api = mk_mem_malloc_z(sizeof(struct plugin_api));
-
- /* Setup and connections list */
- api->config = config;
- api->sched_list = &sched_list;
-
- /* API plugins funcions */
- api->mem_alloc = (void *) mk_mem_malloc;
- api->mem_alloc_z = (void *) mk_mem_malloc_z;
- api->mem_free = (void *) mk_mem_free;
- api->str_build = (void *) m_build_buffer;
- api->str_dup = (void *) mk_string_dup;
- api->str_search = (void *) mk_string_search;
- api->str_search_n = (void *) mk_string_search_n;
- api->str_copy_substr = (void *) mk_string_copy_substr;
- api->str_split_line = (void *) mk_string_split_line;
- api->file_to_buffer = (void *) mk_file_to_buffer;
- api->file_get_info = (void *) mk_file_get_info;
- api->header_send = (void *) mk_header_send;
- api->iov_create = (void *) mk_iov_create;
- api->iov_free = (void *) mk_iov_free;
- api->iov_add_entry = (void *) mk_iov_add_entry;
- api->iov_set_entry = (void *) mk_iov_set_entry;
- api->iov_send = (void *) mk_iov_send;
- api->iov_print = (void *) mk_iov_print;
- api->pointer_set = (void *) mk_pointer_set;
- api->pointer_print = (void *) mk_pointer_print;
- api->plugin_load_symbol = (void *) mk_plugin_load_symbol;
- api->socket_cork_flag = (void *) mk_socket_set_cork_flag;
- api->socket_connect = (void *) mk_socket_connect;
- api->socket_set_tcp_nodelay = (void *) mk_socket_set_tcp_nodelay;
- api->socket_create = (void *) mk_socket_create;
- api->config_create = (void *) mk_config_create;
- api->config_free = (void *) mk_config_free;
- api->config_getval = (void *) mk_config_getval;
- api->sched_get_connection = (void *) mk_sched_get_connection;
-
- path = mk_mem_malloc_z(1024);
- snprintf(path, 1024, "%s/%s", config->serverconf, MK_PLUGIN_LOAD);
-
- /* Read configuration file */
- cnf = mk_config_create(path);
-
- while(cnf) {
- if(strcasecmp(cnf->key, "LoadPlugin") == 0){
- handle = mk_plugin_load(cnf->val);
- p = mk_plugin_register(handle, cnf->val);
- if(!p){
- fprintf(stderr, "Plugin error: %s", cnf->val);
- dlclose(handle);
- }
- else{
- char *plugin_confdir = 0;
- unsigned long len;
-
- m_build_buffer(&plugin_confdir,
- &len,
- "%s/plugins/%s/",
- config->serverconf,
- p->shortname);
-
- p->call_init(&api, plugin_confdir);
- }
- }
- cnf = cnf->next;
+ char *path;
+ void *handle;
+ struct plugin *p;
+ struct plugin_api *api;
+ struct plugin_list *plist;
+ struct mk_config *cnf;
+
+ api = mk_mem_malloc_z(sizeof(struct plugin_api));
+
+ /* Setup and connections list */
+ api->config = config;
+ api->sched_list = &sched_list;
+
+ /* API plugins funcions */
+ api->mem_alloc = (void *) mk_mem_malloc;
+ api->mem_alloc_z = (void *) mk_mem_malloc_z;
+ api->mem_free = (void *) mk_mem_free;
+ api->str_build = (void *) m_build_buffer;
+ api->str_dup = (void *) mk_string_dup;
+ api->str_search = (void *) mk_string_search;
+ api->str_search_n = (void *) mk_string_search_n;
+ api->str_copy_substr = (void *) mk_string_copy_substr;
+ api->str_split_line = (void *) mk_string_split_line;
+ api->file_to_buffer = (void *) mk_file_to_buffer;
+ api->file_get_info = (void *) mk_file_get_info;
+ api->header_send = (void *) mk_header_send;
+ api->iov_create = (void *) mk_iov_create;
+ api->iov_free = (void *) mk_iov_free;
+ api->iov_add_entry = (void *) mk_iov_add_entry;
+ api->iov_set_entry = (void *) mk_iov_set_entry;
+ api->iov_send = (void *) mk_iov_send;
+ api->iov_print = (void *) mk_iov_print;
+ api->pointer_set = (void *) mk_pointer_set;
+ api->pointer_print = (void *) mk_pointer_print;
+ api->plugin_load_symbol = (void *) mk_plugin_load_symbol;
+ api->socket_cork_flag = (void *) mk_socket_set_cork_flag;
+ api->socket_connect = (void *) mk_socket_connect;
+ api->socket_set_tcp_nodelay = (void *) mk_socket_set_tcp_nodelay;
+ api->socket_create = (void *) mk_socket_create;
+ api->config_create = (void *) mk_config_create;
+ api->config_free = (void *) mk_config_free;
+ api->config_getval = (void *) mk_config_getval;
+ api->sched_get_connection = (void *) mk_sched_get_connection;
+
+ path = mk_mem_malloc_z(1024);
+ snprintf(path, 1024, "%s/%s", config->serverconf, MK_PLUGIN_LOAD);
+
+ /* Read configuration file */
+ cnf = mk_config_create(path);
+
+ while (cnf) {
+ if (strcasecmp(cnf->key, "LoadPlugin") == 0) {
+ handle = mk_plugin_load(cnf->val);
+ p = mk_plugin_register(handle, cnf->val);
+ if (!p) {
+ fprintf(stderr, "Plugin error: %s", cnf->val);
+ dlclose(handle);
+ }
+ else {
+ char *plugin_confdir = 0;
+ unsigned long len;
+
+ m_build_buffer(&plugin_confdir,
+ &len,
+ "%s/plugins/%s/",
+ config->serverconf, p->shortname);
+
+ p->call_init(&api, plugin_confdir);
+ }
}
+ cnf = cnf->next;
+ }
- api->plugins = plg_list;
- plist = plg_list;
- mk_mem_free(path);
+ api->plugins = plg_list;
+ plist = plg_list;
+ mk_mem_free(path);
}
int mk_plugin_stage_run(mk_plugin_stage_t stage,
unsigned int socket,
struct sched_connection *conx,
- struct client_request *cr,
- struct request *sr)
+ struct client_request *cr, struct request *sr)
{
- int ret;
- struct plugin *p;
-
- if(stage & MK_PLUGIN_STAGE_10){
- p = config->plugins->stage_10;
- while(p){
- p->call_stage_10();
- p = p->next;
- }
+ int ret;
+ struct plugin *p;
+
+ if (stage & MK_PLUGIN_STAGE_10) {
+ p = config->plugins->stage_10;
+ while (p) {
+ p->call_stage_10();
+ p = p->next;
}
- if(stage & MK_PLUGIN_STAGE_20){
- p = config->plugins->stage_20;
- while(p){
- ret = p->call_stage_20(socket, conx, cr);
- switch(ret){
- case MK_PLUGIN_RET_CLOSE_CONX:
- return MK_PLUGIN_RET_CLOSE_CONX;
- }
-
- p = p->next;
- }
+ }
+ if (stage & MK_PLUGIN_STAGE_20) {
+ p = config->plugins->stage_20;
+ while (p) {
+ ret = p->call_stage_20(socket, conx, cr);
+ switch (ret) {
+ case MK_PLUGIN_RET_CLOSE_CONX:
+ return MK_PLUGIN_RET_CLOSE_CONX;
+ }
+
+ p = p->next;
}
-
- if(stage & MK_PLUGIN_STAGE_30){
- p = config->plugins->stage_30;
- while(p){
- ret = p->call_stage_30(cr, sr);
- switch(ret){
- case MK_PLUGIN_RET_CLOSE_CONX:
- return MK_PLUGIN_RET_CLOSE_CONX;
- }
-
- p = p->next;
- }
+ }
+
+ if (stage & MK_PLUGIN_STAGE_30) {
+ p = config->plugins->stage_30;
+ while (p) {
+ ret = p->call_stage_30(cr, sr);
+ switch (ret) {
+ case MK_PLUGIN_RET_CLOSE_CONX:
+ return MK_PLUGIN_RET_CLOSE_CONX;
+ }
+
+ p = p->next;
}
-
- if(stage & MK_PLUGIN_STAGE_40){
- p = config->plugins->stage_40;
- while(p){
- ret = p->call_stage_40(cr, sr);
- switch(ret){
- case MK_PLUGIN_RET_NOT_ME:
- break;
- case MK_PLUGIN_RET_END:
- break;
- case MK_PLUGIN_RET_CONTINUE:
- /* Register plugin for next loops */
- mk_request_handler_register(sr, p);
- break;
- }
- p = p->next;
- }
+ }
+
+ if (stage & MK_PLUGIN_STAGE_40) {
+ p = config->plugins->stage_40;
+ while (p) {
+ ret = p->call_stage_40(cr, sr);
+ switch (ret) {
+ case MK_PLUGIN_RET_NOT_ME:
+ break;
+ case MK_PLUGIN_RET_END:
+ break;
+ case MK_PLUGIN_RET_CONTINUE:
+ /* Register plugin for next loops */
+ mk_request_handler_register(sr, p);
+ break;
+ }
+ p = p->next;
}
+ }
- return -1;
+ return -1;
}
/* This function is called by every created worker
*/
void mk_plugin_worker_startup()
{
- struct plugin_list *plg;
-
- plg = plg_list;
-
- while(plg){
- if(plg->p->call_worker_init){
- plg->p->call_worker_init();
- }
- plg = plg->next;
+ struct plugin_list *plg;
+
+ plg = plg_list;
+
+ while (plg) {
+ if (plg->p->call_worker_init) {
+ plg->p->call_worker_init();
}
+ plg = plg->next;
+ }
}
#include <string.h>
-#include <arpa/inet.h>
+#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/types.h>
struct request *mk_request_parse(struct client_request *cr)
{
- int i, n, init_block=0, n_blocks=0;
- int pipelined=FALSE;
- struct request *cr_buf=0, *cr_search=0;
-
- init_block = 0;
-
- for(i=cr->first_block_end; i<=cr->body_length-mk_endblock.len; i++)
- {
- /* Allocating request block */
- cr_buf = mk_request_alloc();
-
- /* mk_pointer */
- cr_buf->body.data = cr->body+init_block;
- cr_buf->body.len = i-init_block;
-
- if(i==cr->first_block_end){
- cr_buf->method = cr->first_method;
- }
- else{
- cr_buf->method = mk_http_method_get(cr_buf->body.data);
- }
+ int i, n, init_block = 0, n_blocks = 0;
+ int pipelined = FALSE;
+ struct request *cr_buf = 0, *cr_search = 0;
+
+ init_block = 0;
+
+ for (i = cr->first_block_end; i <= cr->body_length - mk_endblock.len; i++) {
+ /* Allocating request block */
+ cr_buf = mk_request_alloc();
- cr_buf->next = NULL;
-
- i = init_block = i + mk_endblock.len;
-
- /* Looking for POST data */
- if(cr_buf->method == HTTP_METHOD_POST)
- {
- cr_buf->post_variables =
- mk_method_post_get_vars(cr->body, i);
-
- if(cr_buf->post_variables.len >= 0)
- {
- i = init_block = i+cr_buf->post_variables.len;
- }
- }
-
- if(!cr->request)
- {
- cr->request = cr_buf;
- }
- else{
- cr_search = cr->request;
- while(cr_search)
- {
- if(cr_search->next==NULL)
- {
- cr_search->next = cr_buf;
- break;
- }
- else
- {
- cr_search = cr_search->next;
- }
- }
- }
- n_blocks++;
- n = mk_string_search(cr->body+i, mk_endblock.data);
- if(n<=0)
- {
- break;
+ /* mk_pointer */
+ cr_buf->body.data = cr->body + init_block;
+ cr_buf->body.len = i - init_block;
+
+ if (i == cr->first_block_end) {
+ cr_buf->method = cr->first_method;
+ }
+ else {
+ cr_buf->method = mk_http_method_get(cr_buf->body.data);
+ }
+
+ cr_buf->next = NULL;
+
+ i = init_block = i + mk_endblock.len;
+
+ /* Looking for POST data */
+ if (cr_buf->method == HTTP_METHOD_POST) {
+ cr_buf->post_variables = mk_method_post_get_vars(cr->body, i);
+
+ if (cr_buf->post_variables.len >= 0) {
+ i = init_block = i + cr_buf->post_variables.len;
+ }
+ }
+
+ if (!cr->request) {
+ cr->request = cr_buf;
+ }
+ else {
+ cr_search = cr->request;
+ while (cr_search) {
+ if (cr_search->next == NULL) {
+ cr_search->next = cr_buf;
+ break;
}
- else{
- i = i + n;
+ else {
+ cr_search = cr_search->next;
}
- }
-
- /* Checking pipelining connection */
- cr_search = cr->request;
- if(n_blocks>1)
- {
- pipelined = TRUE;
-
- while(cr_search){
- if(cr_search->method!=HTTP_METHOD_GET &&
- cr_search->method!=HTTP_METHOD_HEAD)
- {
- pipelined = FALSE;
- break;
- }
- cr_search = cr_search->next;
- }
-
- if(pipelined == FALSE){
- /* All pipelined requests must use GET method */
- return NULL;
- }
- else{
- cr->pipelined = TRUE;
- }
- }
-
- /* DEBUG BLOCKS
- printf("*****************************************");
- fflush(stdout);
- cr_search = cr->request;
- while(cr_search){
- printf("\n---BLOCK---:\n%s---END BLOCK---\n\n", cr_search->body);
- fflush(stdout);
- cr_search = cr_search->next;
- }
- */
-
- return cr->request;
+ }
+ }
+ n_blocks++;
+ n = mk_string_search(cr->body + i, mk_endblock.data);
+ if (n <= 0) {
+ break;
+ }
+ else {
+ i = i + n;
+ }
+ }
+
+ /* Checking pipelining connection */
+ cr_search = cr->request;
+ if (n_blocks > 1) {
+ pipelined = TRUE;
+
+ while (cr_search) {
+ if (cr_search->method != HTTP_METHOD_GET &&
+ cr_search->method != HTTP_METHOD_HEAD) {
+ pipelined = FALSE;
+ break;
+ }
+ cr_search = cr_search->next;
+ }
+
+ if (pipelined == FALSE) {
+ /* All pipelined requests must use GET method */
+ return NULL;
+ }
+ else {
+ cr->pipelined = TRUE;
+ }
+ }
+
+ /* DEBUG BLOCKS
+ printf("*****************************************");
+ fflush(stdout);
+ cr_search = cr->request;
+ while(cr_search){
+ printf("\n---BLOCK---:\n%s---END BLOCK---\n\n", cr_search->body);
+ fflush(stdout);
+ cr_search = cr_search->next;
+ }
+ */
+
+ return cr->request;
}
int mk_handler_read(int socket, struct client_request *cr)
{
- int bytes;
-
- bytes = read(socket, cr->body+cr->body_length,
- MAX_REQUEST_BODY-cr->body_length);
-
- if (bytes < 0) {
- if (errno == EAGAIN) {
- return 1;
- }
- else{
- mk_request_client_remove(socket);
- return -1;
- }
- }
- if (bytes == 0){
- mk_request_client_remove(socket);
- return -1;
- }
-
- if(bytes > 0)
- {
- cr->body_length+=bytes;
- cr->body[cr->body_length] = '\0';
- }
-
- return bytes;
+ int bytes;
+
+ bytes = read(socket, cr->body + cr->body_length,
+ MAX_REQUEST_BODY - cr->body_length);
+
+ if (bytes < 0) {
+ if (errno == EAGAIN) {
+ return 1;
+ }
+ else {
+ mk_request_client_remove(socket);
+ return -1;
+ }
+ }
+ if (bytes == 0) {
+ mk_request_client_remove(socket);
+ return -1;
+ }
+
+ if (bytes > 0) {
+ cr->body_length += bytes;
+ cr->body[cr->body_length] = '\0';
+ }
+
+ return bytes;
}
int mk_handler_write(int socket, struct client_request *cr)
{
- int bytes, final_status=0;
- struct request *p_request;
-
- /*
- * Get node from schedule list node which contains
- * the information regarding to the current thread
- */
- if(!cr)
- {
- return -1;
- }
-
- if(!cr->request)
- {
- if(!mk_request_parse(cr))
- {
- return -1;
- }
- }
-
- p_request = cr->request;
-
- while(p_request)
- {
- /* Request not processed */
- if(p_request->bytes_to_send < 0)
- {
- final_status = mk_request_process(cr, p_request);
- }
- /* Request with data to send */
- else if(p_request->bytes_to_send>0)
- {
- bytes = SendFile(socket, cr, p_request);
- final_status = bytes;
- }
- /*
- * If we got an error, we don't want to parse
- * and send information for another pipelined request
- */
- if(final_status > 0)
- {
- return final_status;
- }
- else if(final_status <= 0)
- {
- mk_logger_write_log(cr, p_request->log, p_request->host_conf);
- }
- p_request = p_request->next;
- }
-
- /* If we are here, is because all pipelined request were
- * processed successfully, let's return 0;
- */
- return 0;
+ int bytes, final_status = 0;
+ struct request *p_request;
+
+ /*
+ * Get node from schedule list node which contains
+ * the information regarding to the current thread
+ */
+ if (!cr) {
+ return -1;
+ }
+
+ if (!cr->request) {
+ if (!mk_request_parse(cr)) {
+ return -1;
+ }
+ }
+
+ p_request = cr->request;
+
+ while (p_request) {
+ /* Request not processed */
+ if (p_request->bytes_to_send < 0) {
+ final_status = mk_request_process(cr, p_request);
+ }
+ /* Request with data to send */
+ else if (p_request->bytes_to_send > 0) {
+ bytes = SendFile(socket, cr, p_request);
+ final_status = bytes;
+ }
+ /*
+ * If we got an error, we don't want to parse
+ * and send information for another pipelined request
+ */
+ if (final_status > 0) {
+ return final_status;
+ }
+ else if (final_status <= 0) {
+ mk_logger_write_log(cr, p_request->log, p_request->host_conf);
+ }
+ p_request = p_request->next;
+ }
+
+ /* If we are here, is because all pipelined request were
+ * processed successfully, let's return 0;
+ */
+ return 0;
}
int mk_request_process(struct client_request *cr, struct request *s_request)
{
- int status=0;
- struct host *host;
-
- status = mk_request_header_process(s_request);
-
- if(status<0)
- {
- return EXIT_NORMAL;
- }
-
- switch(s_request->method)
- {
- case METHOD_NOT_ALLOWED:
- mk_request_error(M_CLIENT_METHOD_NOT_ALLOWED, cr,
- s_request, 1, s_request->log);
- return EXIT_NORMAL;
- case METHOD_NOT_FOUND:
- mk_request_error(M_SERVER_NOT_IMPLEMENTED, cr,
- s_request, 1, s_request->log);
- return EXIT_NORMAL;
+ int status = 0;
+ struct host *host;
+
+ status = mk_request_header_process(s_request);
+
+ if (status < 0) {
+ return EXIT_NORMAL;
+ }
+
+ switch (s_request->method) {
+ case METHOD_NOT_ALLOWED:
+ mk_request_error(M_CLIENT_METHOD_NOT_ALLOWED, cr,
+ s_request, 1, s_request->log);
+ return EXIT_NORMAL;
+ case METHOD_NOT_FOUND:
+ mk_request_error(M_SERVER_NOT_IMPLEMENTED, cr,
+ s_request, 1, s_request->log);
+ return EXIT_NORMAL;
+ }
+
+ s_request->user_home = VAR_OFF;
+ s_request->log->method = s_request->method;
+
+ /* Valid request URI? */
+ if (s_request->uri_processed == NULL) {
+ mk_request_error(M_CLIENT_BAD_REQUEST, cr, s_request, 1,
+ s_request->log);
+ return EXIT_NORMAL;
+ }
+
+ /* HTTP/1.1 needs Host header */
+ if (!s_request->host.data && s_request->protocol == HTTP_PROTOCOL_11) {
+ s_request->log->final_response = M_CLIENT_BAD_REQUEST;
+ mk_request_error(M_CLIENT_BAD_REQUEST, cr, s_request, 1,
+ s_request->log);
+ return EXIT_NORMAL;
+ }
+
+ /* Method not allowed ? */
+ if (s_request->method == METHOD_NOT_ALLOWED) {
+ s_request->log->final_response = M_CLIENT_METHOD_NOT_ALLOWED;
+ mk_request_error(M_CLIENT_METHOD_NOT_ALLOWED, cr, s_request, 1,
+ s_request->log);
+ return EXIT_NORMAL;
+ }
+
+ /* Validating protocol version */
+ if (s_request->protocol == HTTP_PROTOCOL_UNKNOWN) {
+
+ s_request->log->final_response = M_SERVER_HTTP_VERSION_UNSUP;
+ mk_request_error(M_SERVER_HTTP_VERSION_UNSUP, cr, s_request, 1,
+ s_request->log);
+ return EXIT_NORMAL;
+ }
+
+ if (s_request->host.data) {
+ host = mk_config_host_find(s_request->host);
+ if (host) {
+ s_request->host_conf = host;
+ }
+ else {
+ s_request->host_conf = config->hosts;
+ }
+ }
+ else {
+ s_request->host_conf = config->hosts;
+ }
+ s_request->log->host_conf = s_request->host_conf;
+
+ /* is requesting an user home directory ? */
+ if (config->user_dir) {
+ if (strncmp(s_request->uri_processed,
+ mk_user_home.data, mk_user_home.len) == 0) {
+ if (mk_user_init(cr, s_request) != 0) {
+ return EXIT_NORMAL;
+ }
}
+ }
- s_request->user_home=VAR_OFF;
- s_request->log->method = s_request->method;
-
- /* Valid request URI? */
- if(s_request->uri_processed==NULL){
- mk_request_error(M_CLIENT_BAD_REQUEST, cr, s_request, 1,
- s_request->log);
- return EXIT_NORMAL;
- }
-
- /* HTTP/1.1 needs Host header */
- if(!s_request->host.data && s_request->protocol==HTTP_PROTOCOL_11){
- s_request->log->final_response=M_CLIENT_BAD_REQUEST;
- mk_request_error(M_CLIENT_BAD_REQUEST, cr, s_request,1,
- s_request->log);
- return EXIT_NORMAL;
- }
-
- /* Method not allowed ? */
- if(s_request->method==METHOD_NOT_ALLOWED){
- s_request->log->final_response=M_CLIENT_METHOD_NOT_ALLOWED;
- mk_request_error(M_CLIENT_METHOD_NOT_ALLOWED, cr, s_request, 1,
- s_request->log);
- return EXIT_NORMAL;
- }
-
- /* Validating protocol version */
- if(s_request->protocol == HTTP_PROTOCOL_UNKNOWN)
- {
-
- s_request->log->final_response=M_SERVER_HTTP_VERSION_UNSUP;
- mk_request_error(M_SERVER_HTTP_VERSION_UNSUP, cr, s_request, 1,
- s_request->log);
- return EXIT_NORMAL;
- }
-
- if(s_request->host.data)
- {
- host=mk_config_host_find(s_request->host);
- if(host)
- {
- s_request->host_conf = host;
- }
- else{
- s_request->host_conf = config->hosts;
- }
- }
- else{
- s_request->host_conf = config->hosts;
- }
- s_request->log->host_conf = s_request->host_conf;
-
- /* is requesting an user home directory ? */
- if(config->user_dir){
- if(strncmp(s_request->uri_processed,
- mk_user_home.data,
- mk_user_home.len)==0){
- if(mk_user_init(cr, s_request)!=0){
- return EXIT_NORMAL;
- }
- }
+ /* Handling method requested */
+ if (s_request->method == HTTP_METHOD_POST) {
+ if ((status = mk_method_post(cr, s_request)) == -1) {
+ return status;
}
-
- /* Handling method requested */
- if(s_request->method==HTTP_METHOD_POST)
- {
- if((status=mk_method_post(cr, s_request))==-1){
- return status;
- }
- }
-
- status = mk_http_init(cr, s_request);
-
- return status;
+ }
+
+ status = mk_http_init(cr, s_request);
+
+ return status;
}
/* Return a struct with method, URI , protocol version
and all static headers defined here sent in request */
int mk_request_header_process(struct request *sr)
{
- int uri_init=0, uri_end=0;
- char *query_init=0;
- int prot_init=0, prot_end=0, pos_sep=0;
- int fh_limit;
- char *port=0;
- char *headers;
- mk_pointer host;
-
- /* If verification fails it will return always
- * a bad request status
- */
- sr->log->final_response = M_CLIENT_BAD_REQUEST;
+ int uri_init = 0, uri_end = 0;
+ char *query_init = 0;
+ int prot_init = 0, prot_end = 0, pos_sep = 0;
+ int fh_limit;
+ char *port = 0;
+ char *headers;
+ mk_pointer host;
+
+ /* If verification fails it will return always
+ * a bad request status
+ */
+ sr->log->final_response = M_CLIENT_BAD_REQUEST;
+
+ /* Method */
+ sr->method_p = mk_http_method_check_str(sr->method);
+
+ /* Request URI */
+ uri_init = (index(sr->body.data, ' ') - sr->body.data) + 1;
+ fh_limit = (index(sr->body.data, '\n') - sr->body.data);
+
+ uri_end = mk_string_search_r(sr->body.data, ' ', fh_limit) - 1;
+
+ if (uri_end <= 0) {
+ return -1;
+ }
+
+ prot_init = uri_end + 2;
- /* Method */
- sr->method_p = mk_http_method_check_str(sr->method);
+ if (uri_end < uri_init) {
+ return -1;
+ }
- /* Request URI */
- uri_init = (index(sr->body.data, ' ') - sr->body.data) + 1;
- fh_limit = (index(sr->body.data, '\n') - sr->body.data);
+ /* Query String */
+ query_init = index(sr->body.data + uri_init, '?');
+ if (query_init) {
+ int init, end;
- uri_end = mk_string_search_r(sr->body.data, ' ', fh_limit) - 1;
+ init = (int) (query_init - (sr->body.data + uri_init)) + uri_init;
+ if (init <= uri_end) {
+ end = uri_end;
+ uri_end = init - 1;
- if(uri_end <= 0)
- {
- return -1;
+ sr->query_string = mk_pointer_create(sr->body.data,
+ init + 1, end + 1);
}
+ }
- prot_init = uri_end + 2;
-
- if(uri_end < uri_init)
- {
- return -1;
- }
-
- /* Query String */
- query_init = index(sr->body.data+uri_init, '?');
- if(query_init)
- {
- int init, end;
-
- init = (int) (query_init-(sr->body.data+uri_init)) + uri_init;
- if(init <= uri_end)
- {
- end = uri_end;
- uri_end = init - 1;
-
- sr->query_string = mk_pointer_create(sr->body.data,
- init+1, end+1);
- }
- }
-
- /* Request URI Part 2 */
- sr->uri = sr->log->uri = mk_pointer_create(sr->body.data,
- uri_init, uri_end+1);
-
- if(sr->uri.len<1)
- {
- return -1;
- }
-
-
- /* HTTP Version */
- prot_end = fh_limit-1;
- if(prot_end!=prot_init && prot_end>0){
- sr->protocol = sr->log->protocol =
- mk_http_protocol_check(sr->body.data+prot_init, prot_end-prot_init);
- }
-
- headers = sr->body.data+prot_end+mk_crlf.len;
-
- /* URI processed */
- sr->uri_processed = get_real_string(sr->uri);
-
- if(!sr->uri_processed)
- {
- sr->uri_processed = mk_pointer_to_buf(sr->uri);
- sr->uri_twin = VAR_ON;
- }
-
- /* Creating table of content (index) for request headers */
- int toc_len = MK_KNOWN_HEADERS;
- struct header_toc *toc = mk_request_header_toc_create(toc_len);
- mk_request_header_toc_parse(toc, headers, toc_len);
-
- /* Host */
- host = mk_request_header_find(toc, toc_len, headers, mk_rh_host);
-
- if(host.data)
- {
- if((pos_sep = mk_string_char_search(host.data, ':', host.len))>=0)
- {
- sr->host.data = host.data;
- sr->host.len = pos_sep;
-
- port = mk_string_copy_substr(host.data, pos_sep+1, host.len);
- sr->port = atoi(port);
- mk_mem_free(port);
- }
- else{
- sr->host=host; /* maybe null */
- sr->port=config->standard_port;
- }
- }
- else{
- sr->host.data=NULL;
- }
-
- /* Looking for headers */
- sr->accept = mk_request_header_find(toc, toc_len, headers, mk_rh_accept);
- sr->accept_charset = mk_request_header_find(toc, toc_len, headers,
- mk_rh_accept_charset);
- sr->accept_encoding = mk_request_header_find(toc, toc_len, headers,
- mk_rh_accept_encoding);
-
-
- sr->accept_language = mk_request_header_find(toc, toc_len, headers,
- mk_rh_accept_language);
- sr->cookies = mk_request_header_find(toc, toc_len, headers, mk_rh_cookie);
- sr->connection = mk_request_header_find(toc, toc_len, headers,
- mk_rh_connection);
- sr->referer = mk_request_header_find(toc, toc_len, headers,
- mk_rh_referer);
- sr->user_agent = mk_request_header_find(toc, toc_len, headers,
- mk_rh_user_agent);
- sr->range = mk_request_header_find(toc, toc_len, headers, mk_rh_range);
- sr->if_modified_since = mk_request_header_find(toc, toc_len, headers,
- mk_rh_if_modified_since);
-
- /* Default Keepalive is off */
- sr->keep_alive = VAR_OFF;
- if(sr->connection.data){
- if(mk_string_casestr(sr->connection.data, "Keep-Alive")){
- sr->keep_alive = VAR_ON;
- }
+ /* Request URI Part 2 */
+ sr->uri = sr->log->uri = mk_pointer_create(sr->body.data,
+ uri_init, uri_end + 1);
+
+ if (sr->uri.len < 1) {
+ return -1;
+ }
+
+
+ /* HTTP Version */
+ prot_end = fh_limit - 1;
+ if (prot_end != prot_init && prot_end > 0) {
+ sr->protocol = sr->log->protocol =
+ mk_http_protocol_check(sr->body.data + prot_init,
+ prot_end - prot_init);
+ }
+
+ headers = sr->body.data + prot_end + mk_crlf.len;
+
+ /* URI processed */
+ sr->uri_processed = get_real_string(sr->uri);
+
+ if (!sr->uri_processed) {
+ sr->uri_processed = mk_pointer_to_buf(sr->uri);
+ sr->uri_twin = VAR_ON;
+ }
+
+ /* Creating table of content (index) for request headers */
+ int toc_len = MK_KNOWN_HEADERS;
+ struct header_toc *toc = mk_request_header_toc_create(toc_len);
+ mk_request_header_toc_parse(toc, headers, toc_len);
+
+ /* Host */
+ host = mk_request_header_find(toc, toc_len, headers, mk_rh_host);
+
+ if (host.data) {
+ if ((pos_sep = mk_string_char_search(host.data, ':', host.len)) >= 0) {
+ sr->host.data = host.data;
+ sr->host.len = pos_sep;
+
+ port = mk_string_copy_substr(host.data, pos_sep + 1, host.len);
+ sr->port = atoi(port);
+ mk_mem_free(port);
}
- else{
- /* Default value for HTTP/1.1 */
- if(sr->protocol==HTTP_PROTOCOL_11){
- /* Assume keep-alive connection */
- sr->keep_alive = VAR_ON;
- }
+ else {
+ sr->host = host; /* maybe null */
+ sr->port = config->standard_port;
+ }
+ }
+ else {
+ sr->host.data = NULL;
+ }
+
+ /* Looking for headers */
+ sr->accept = mk_request_header_find(toc, toc_len, headers, mk_rh_accept);
+ sr->accept_charset = mk_request_header_find(toc, toc_len, headers,
+ mk_rh_accept_charset);
+ sr->accept_encoding = mk_request_header_find(toc, toc_len, headers,
+ mk_rh_accept_encoding);
+
+
+ sr->accept_language = mk_request_header_find(toc, toc_len, headers,
+ mk_rh_accept_language);
+ sr->cookies = mk_request_header_find(toc, toc_len, headers, mk_rh_cookie);
+ sr->connection = mk_request_header_find(toc, toc_len, headers,
+ mk_rh_connection);
+ sr->referer = mk_request_header_find(toc, toc_len, headers,
+ mk_rh_referer);
+ sr->user_agent = mk_request_header_find(toc, toc_len, headers,
+ mk_rh_user_agent);
+ sr->range = mk_request_header_find(toc, toc_len, headers, mk_rh_range);
+ sr->if_modified_since = mk_request_header_find(toc, toc_len, headers,
+ mk_rh_if_modified_since);
+
+ /* Default Keepalive is off */
+ sr->keep_alive = VAR_OFF;
+ if (sr->connection.data) {
+ if (mk_string_casestr(sr->connection.data, "Keep-Alive")) {
+ sr->keep_alive = VAR_ON;
}
- sr->log->final_response = M_HTTP_OK;
+ }
+ else {
+ /* Default value for HTTP/1.1 */
+ if (sr->protocol == HTTP_PROTOCOL_11) {
+ /* Assume keep-alive connection */
+ sr->keep_alive = VAR_ON;
+ }
+ }
+ sr->log->final_response = M_HTTP_OK;
- return 0;
+ return 0;
}
/* Return value of some variable sent in request */
-mk_pointer mk_request_header_find(struct header_toc *toc, int toc_len,
- char *request_body, mk_pointer header)
+mk_pointer mk_request_header_find(struct header_toc * toc, int toc_len,
+ char *request_body, mk_pointer header)
{
- int i;
- mk_pointer var;
-
- var.data = NULL;
- var.len = 0;
-
- /* new code */
- if(toc)
- {
- for(i=0; i<toc_len; i++)
- {
- /* status = 1 means that the toc entry was already
- * checked by monkey
- */
- if(toc[i].status == 1)
- {
- continue;
- }
-
- if(!toc[i].init)
- break;
-
- if(strncasecmp(toc[i].init, header.data, header.len)==0)
- {
- var.data = toc[i].init + header.len + 1;
- var.len = toc[i].end - var.data;
- toc[i].status = 1;
- return var;
- }
- }
+ int i;
+ mk_pointer var;
+
+ var.data = NULL;
+ var.len = 0;
+
+ /* new code */
+ if (toc) {
+ for (i = 0; i < toc_len; i++) {
+ /* status = 1 means that the toc entry was already
+ * checked by monkey
+ */
+ if (toc[i].status == 1) {
+ continue;
+ }
+
+ if (!toc[i].init)
+ break;
+
+ if (strncasecmp(toc[i].init, header.data, header.len) == 0) {
+ var.data = toc[i].init + header.len + 1;
+ var.len = toc[i].end - var.data;
+ toc[i].status = 1;
+ return var;
+ }
}
+ }
- return var;
+ return var;
}
/* FIXME: IMPROVE access */
/* Look for some index.xxx in pathfile */
mk_pointer mk_request_index(char *pathfile)
{
- unsigned long len;
- char *file_aux=0;
- mk_pointer f;
- struct indexfile *aux_index;
-
- mk_pointer_reset(&f);
-
- aux_index=first_index;
-
- while(aux_index) {
- m_build_buffer(&file_aux, &len, "%s%s",
- pathfile, aux_index->indexname);
-
- if(access(file_aux,F_OK)==0)
- {
- f.data = file_aux;
- f.len = len;
- return f;
- }
- mk_mem_free(file_aux);
- aux_index=aux_index->next;
- }
-
- return f;
+ unsigned long len;
+ char *file_aux = 0;
+ mk_pointer f;
+ struct indexfile *aux_index;
+
+ mk_pointer_reset(&f);
+
+ aux_index = first_index;
+
+ while (aux_index) {
+ m_build_buffer(&file_aux, &len, "%s%s",
+ pathfile, aux_index->indexname);
+
+ if (access(file_aux, F_OK) == 0) {
+ f.data = file_aux;
+ f.len = len;
+ return f;
+ }
+ mk_mem_free(file_aux);
+ aux_index = aux_index->next;
+ }
+
+ return f;
}
/* Send error responses */
-void mk_request_error(int num_error, struct client_request *cr,
- struct request *s_request, int debug, struct log_info *s_log)
+void mk_request_error(int num_error, struct client_request *cr,
+ struct request *s_request, int debug,
+ struct log_info *s_log)
{
- char *aux_message=0;
- mk_pointer message, *page=0;
- long n;
-
- switch(num_error) {
- case M_CLIENT_BAD_REQUEST:
- page = mk_request_set_default_page("Bad Request",
- s_request->uri,
- s_request->host_conf->host_signature);
- s_log->error_msg = request_error_msg_400;
- break;
-
- case M_CLIENT_FORBIDDEN:
- page = mk_request_set_default_page("Forbidden",
- s_request->uri,
- s_request->host_conf->host_signature);
- s_log->error_msg = request_error_msg_403;
- // req s_request->uri;
- break;
-
- case M_CLIENT_NOT_FOUND:
- m_build_buffer(&message.data, &message.len,
- "The requested URL was not found on this server.");
- page = mk_request_set_default_page("Not Found",
- message,
- s_request->host_conf->host_signature);
- s_log->error_msg = request_error_msg_404;
- // req uri;
- mk_pointer_free(&message);
- break;
-
- case M_CLIENT_METHOD_NOT_ALLOWED:
- page = mk_request_set_default_page("Method Not Allowed",
- s_request->uri,
- s_request->host_conf->host_signature);
-
- s_log->final_response=M_CLIENT_METHOD_NOT_ALLOWED;
- s_log->error_msg = request_error_msg_405;
- break;
-
- case M_CLIENT_REQUEST_TIMEOUT:
- s_log->status=S_LOG_OFF;
- s_log->error_msg = request_error_msg_408;
- break;
-
- case M_CLIENT_LENGTH_REQUIRED:
- s_log->error_msg = request_error_msg_411;
- break;
-
- case M_SERVER_NOT_IMPLEMENTED:
- page = mk_request_set_default_page("Method Not Implemented",
- s_request->uri,
- s_request->host_conf->host_signature);
- s_log->final_response=M_SERVER_NOT_IMPLEMENTED;
- s_log->error_msg = request_error_msg_501;
- break;
-
- case M_SERVER_INTERNAL_ERROR:
- m_build_buffer(&message.data, &message.len,
- "Problems found running %s ",
- s_request->uri);
- page = mk_request_set_default_page("Internal Server Error",
- message, s_request->host_conf->host_signature);
- s_log->error_msg = request_error_msg_500;
-
- mk_pointer_free(&message);
- break;
-
- case M_SERVER_HTTP_VERSION_UNSUP:
- mk_pointer_reset(&message);
- page = mk_request_set_default_page("HTTP Version Not Supported",
- message,
- s_request->host_conf->host_signature);
- s_log->error_msg = request_error_msg_505;
- break;
- }
-
- s_log->final_response=num_error;
-
- s_request->headers->status = num_error;
- if(page){
- s_request->headers->content_length = page->len;
- s_request->headers->content_length_p = mk_utils_int2mkp(page->len);
- }
-
- s_request->headers->location = NULL;
- s_request->headers->cgi = SH_NOCGI;
- s_request->headers->pconnections_left = 0;
- mk_pointer_reset(&s_request->headers->last_modified);
-
- if(aux_message) mk_mem_free(aux_message);
-
- if(!page)
- {
- mk_pointer_reset(&s_request->headers->content_type);
- }
- else
- {
- mk_pointer_set(&s_request->headers->content_type, "text/html");
- }
-
- mk_header_send(cr->socket, cr, s_request, s_log);
-
- if(debug==1){
- n = write(cr->socket, page->data, page->len);
- mk_pointer_free(page);
- mk_mem_free(page);
- }
+ char *aux_message = 0;
+ mk_pointer message, *page = 0;
+ long n;
+
+ switch (num_error) {
+ case M_CLIENT_BAD_REQUEST:
+ page = mk_request_set_default_page("Bad Request",
+ s_request->uri,
+ s_request->host_conf->
+ host_signature);
+ s_log->error_msg = request_error_msg_400;
+ break;
+
+ case M_CLIENT_FORBIDDEN:
+ page = mk_request_set_default_page("Forbidden",
+ s_request->uri,
+ s_request->host_conf->
+ host_signature);
+ s_log->error_msg = request_error_msg_403;
+ // req s_request->uri;
+ break;
+
+ case M_CLIENT_NOT_FOUND:
+ m_build_buffer(&message.data, &message.len,
+ "The requested URL was not found on this server.");
+ page = mk_request_set_default_page("Not Found",
+ message,
+ s_request->host_conf->
+ host_signature);
+ s_log->error_msg = request_error_msg_404;
+ // req uri;
+ mk_pointer_free(&message);
+ break;
+
+ case M_CLIENT_METHOD_NOT_ALLOWED:
+ page = mk_request_set_default_page("Method Not Allowed",
+ s_request->uri,
+ s_request->host_conf->
+ host_signature);
+
+ s_log->final_response = M_CLIENT_METHOD_NOT_ALLOWED;
+ s_log->error_msg = request_error_msg_405;
+ break;
+
+ case M_CLIENT_REQUEST_TIMEOUT:
+ s_log->status = S_LOG_OFF;
+ s_log->error_msg = request_error_msg_408;
+ break;
+
+ case M_CLIENT_LENGTH_REQUIRED:
+ s_log->error_msg = request_error_msg_411;
+ break;
+
+ case M_SERVER_NOT_IMPLEMENTED:
+ page = mk_request_set_default_page("Method Not Implemented",
+ s_request->uri,
+ s_request->host_conf->
+ host_signature);
+ s_log->final_response = M_SERVER_NOT_IMPLEMENTED;
+ s_log->error_msg = request_error_msg_501;
+ break;
+
+ case M_SERVER_INTERNAL_ERROR:
+ m_build_buffer(&message.data, &message.len,
+ "Problems found running %s ", s_request->uri);
+ page = mk_request_set_default_page("Internal Server Error",
+ message,
+ s_request->host_conf->
+ host_signature);
+ s_log->error_msg = request_error_msg_500;
+
+ mk_pointer_free(&message);
+ break;
+
+ case M_SERVER_HTTP_VERSION_UNSUP:
+ mk_pointer_reset(&message);
+ page = mk_request_set_default_page("HTTP Version Not Supported",
+ message,
+ s_request->host_conf->
+ host_signature);
+ s_log->error_msg = request_error_msg_505;
+ break;
+ }
+
+ s_log->final_response = num_error;
+
+ s_request->headers->status = num_error;
+ if (page) {
+ s_request->headers->content_length = page->len;
+ s_request->headers->content_length_p = mk_utils_int2mkp(page->len);
+ }
+
+ s_request->headers->location = NULL;
+ s_request->headers->cgi = SH_NOCGI;
+ s_request->headers->pconnections_left = 0;
+ mk_pointer_reset(&s_request->headers->last_modified);
+
+ if (aux_message)
+ mk_mem_free(aux_message);
+
+ if (!page) {
+ mk_pointer_reset(&s_request->headers->content_type);
+ }
+ else {
+ mk_pointer_set(&s_request->headers->content_type, "text/html");
+ }
+
+ mk_header_send(cr->socket, cr, s_request, s_log);
+
+ if (debug == 1) {
+ n = write(cr->socket, page->data, page->len);
+ mk_pointer_free(page);
+ mk_mem_free(page);
+ }
}
/* Build error page */
-mk_pointer *mk_request_set_default_page(char *title, mk_pointer message, char *signature)
+mk_pointer *mk_request_set_default_page(char *title, mk_pointer message,
+ char *signature)
{
- char *temp;
- mk_pointer *p;
+ char *temp;
+ mk_pointer *p;
- p = mk_mem_malloc(sizeof(mk_pointer));
+ p = mk_mem_malloc(sizeof(mk_pointer));
- temp = mk_pointer_to_buf(message);
- m_build_buffer(&p->data, &p->len,
- MK_REQUEST_DEFAULT_PAGE,
- title, temp, signature);
- mk_mem_free(temp);
+ temp = mk_pointer_to_buf(message);
+ m_build_buffer(&p->data, &p->len,
+ MK_REQUEST_DEFAULT_PAGE, title, temp, signature);
+ mk_mem_free(temp);
- return p;
+ return p;
}
/* Create a memory allocation in order to handle the request data */
struct request *mk_request_alloc()
{
- struct request *request=0;
-
- request = mk_mem_malloc(sizeof(struct request));
- request->log = mk_mem_malloc(sizeof(struct log_info));
-
- request->status=VAR_OFF; /* Request not processed yet */
- request->make_log=VAR_ON; /* build log file of this request ? */
-
- mk_pointer_reset(&request->body);
-
- request->log->final_response=M_HTTP_OK;
- request->log->status=S_LOG_ON;
- mk_pointer_reset(&request->log->size_p);
- mk_pointer_reset(&request->log->error_msg);
-
- request->status=VAR_ON;
- request->method=METHOD_NOT_FOUND;
-
- mk_pointer_reset(&request->uri);
- request->uri_processed = NULL;
- request->uri_twin = VAR_OFF;
-
- request->accept.data = NULL;
- request->accept_language.data = NULL;
- request->accept_encoding.data = NULL;
- request->accept_charset.data = NULL;
- request->content_length = 0;
- request->content_type.data = NULL;
- request->connection.data = NULL;
- request->cookies.data = NULL;
- request->host.data = NULL;
- request->if_modified_since.data = NULL;
- request->last_modified_since.data = NULL;
- request->range.data = NULL;
- request->referer.data = NULL;
- request->resume.data = NULL;
- request->user_agent.data = NULL;
-
- request->post_variables.data = NULL;
-
- request->user_uri = NULL;
- mk_pointer_reset(&request->query_string);
-
- request->file_info = NULL;
- request->virtual_user = NULL;
- request->script_filename = NULL;
- mk_pointer_reset(&request->real_path);
- request->host_conf = config->hosts;
-
- request->loop = 0;
- request->bytes_to_send = -1;
- request->bytes_offset = 0;
- request->fd_file = -1;
-
- /* Response Headers */
- request->headers = mk_header_create();
-
- request->handled_by = NULL;
- return request;
+ struct request *request = 0;
+
+ request = mk_mem_malloc(sizeof(struct request));
+ request->log = mk_mem_malloc(sizeof(struct log_info));
+
+ request->status = VAR_OFF; /* Request not processed yet */
+ request->make_log = VAR_ON; /* build log file of this request ? */
+
+ mk_pointer_reset(&request->body);
+
+ request->log->final_response = M_HTTP_OK;
+ request->log->status = S_LOG_ON;
+ mk_pointer_reset(&request->log->size_p);
+ mk_pointer_reset(&request->log->error_msg);
+
+ request->status = VAR_ON;
+ request->method = METHOD_NOT_FOUND;
+
+ mk_pointer_reset(&request->uri);
+ request->uri_processed = NULL;
+ request->uri_twin = VAR_OFF;
+
+ request->accept.data = NULL;
+ request->accept_language.data = NULL;
+ request->accept_encoding.data = NULL;
+ request->accept_charset.data = NULL;
+ request->content_length = 0;
+ request->content_type.data = NULL;
+ request->connection.data = NULL;
+ request->cookies.data = NULL;
+ request->host.data = NULL;
+ request->if_modified_since.data = NULL;
+ request->last_modified_since.data = NULL;
+ request->range.data = NULL;
+ request->referer.data = NULL;
+ request->resume.data = NULL;
+ request->user_agent.data = NULL;
+
+ request->post_variables.data = NULL;
+
+ request->user_uri = NULL;
+ mk_pointer_reset(&request->query_string);
+
+ request->file_info = NULL;
+ request->virtual_user = NULL;
+ request->script_filename = NULL;
+ mk_pointer_reset(&request->real_path);
+ request->host_conf = config->hosts;
+
+ request->loop = 0;
+ request->bytes_to_send = -1;
+ request->bytes_offset = 0;
+ request->fd_file = -1;
+
+ /* Response Headers */
+ request->headers = mk_header_create();
+
+ request->handled_by = NULL;
+ return request;
}
void mk_request_free_list(struct client_request *cr)
{
- struct request *sr=0, *before=0;
-
+ struct request *sr = 0, *before = 0;
+
/* sr = last node */
- while(cr->request)
- {
+ while (cr->request) {
sr = before = cr->request;
- while(sr->next)
- {
+ while (sr->next) {
sr = sr->next;
}
- if(sr!=cr->request){
- while(before->next!=sr){
+ if (sr != cr->request) {
+ while (before->next != sr) {
before = before->next;
}
before->next = NULL;
}
- else{
+ else {
cr->request = NULL;
}
mk_request_free(sr);
void mk_request_free(struct request *sr)
{
- /* I hate it, but I don't know another light way :( */
- if(sr->fd_file>0)
- {
- close(sr->fd_file);
- }
- if(sr->headers){
- mk_mem_free(sr->headers->location);
- mk_pointer_free(&sr->headers->content_length_p);
- mk_pointer_free(&sr->headers->last_modified);
- /*
- mk_mem_free(sr->headers->content_type);
- headers->content_type never it's allocated
- with malloc or something, so we don't need
- to free it, the value has been freed before
- in M_METHOD_Get_and_Head(struct request *sr)
-
- this BUG was reported by gentoo team.. thanks guys XD
- */
-
- mk_mem_free(sr->headers);
- }
+ /* I hate it, but I don't know another light way :( */
+ if (sr->fd_file > 0) {
+ close(sr->fd_file);
+ }
+ if (sr->headers) {
+ mk_mem_free(sr->headers->location);
+ mk_pointer_free(&sr->headers->content_length_p);
+ mk_pointer_free(&sr->headers->last_modified);
+ /*
+ mk_mem_free(sr->headers->content_type);
+ headers->content_type never it's allocated
+ with malloc or something, so we don't need
+ to free it, the value has been freed before
+ in M_METHOD_Get_and_Head(struct request *sr)
+
+ this BUG was reported by gentoo team.. thanks guys XD
+ */
-
- if(sr->log){
- /*
- * We do not free log->size_p, as if it was
- * used due to an error, it points to the
- * same memory block than header->content_length_p
- * points to, we just reset it.
- */
- mk_pointer_reset(&sr->log->size_p);
-
- /*
- * sr->log->error_msg just point to
- * local data on request.c, no
- * dynamic allocation is made
- */
-
- mk_mem_free(sr->log);
- }
+ mk_mem_free(sr->headers);
+ }
- mk_pointer_reset(&sr->body);
- mk_pointer_reset(&sr->uri);
- if(sr->uri_twin==VAR_ON)
- {
- mk_mem_free(sr->uri_processed);
- }
+ if (sr->log) {
+ /*
+ * We do not free log->size_p, as if it was
+ * used due to an error, it points to the
+ * same memory block than header->content_length_p
+ * points to, we just reset it.
+ */
+ mk_pointer_reset(&sr->log->size_p);
+
+ /*
+ * sr->log->error_msg just point to
+ * local data on request.c, no
+ * dynamic allocation is made
+ */
+
+ mk_mem_free(sr->log);
+ }
+
+ mk_pointer_reset(&sr->body);
+ mk_pointer_reset(&sr->uri);
- mk_pointer_free(&sr->post_variables);
- mk_mem_free(sr->user_uri);
- mk_pointer_reset(&sr->query_string);
+ if (sr->uri_twin == VAR_ON) {
+ mk_mem_free(sr->uri_processed);
+ }
+
+ mk_pointer_free(&sr->post_variables);
+ mk_mem_free(sr->user_uri);
+ mk_pointer_reset(&sr->query_string);
- mk_mem_free(sr->file_info);
- mk_mem_free(sr->virtual_user);
- mk_mem_free(sr->script_filename);
- mk_pointer_free(&sr->real_path);
- mk_mem_free(sr);
+ mk_mem_free(sr->file_info);
+ mk_mem_free(sr->virtual_user);
+ mk_mem_free(sr->script_filename);
+ mk_pointer_free(&sr->real_path);
+ mk_mem_free(sr);
}
/* Create a client request struct and put it on the
*/
struct client_request *mk_request_client_create(int socket)
{
- struct request_idx *request_index;
- struct client_request *cr;
- struct sched_connection *sc;
-
- sc = mk_sched_get_connection(NULL, socket);
- cr = mk_mem_malloc(sizeof(struct client_request));
-
- /* IPv4 Address */
- cr->ipv4 = (char *) sc->ipv4;
-
- cr->pipelined = FALSE;
- cr->counter_connections = 0;
- cr->socket = socket;
- cr->status = MK_REQUEST_STATUS_INCOMPLETE;
- cr->request = NULL;
-
- /* creation time in unix time */
- cr->init_time = sc->arrive_time;
-
- cr->next = NULL;
- cr->body = mk_mem_malloc(MAX_REQUEST_BODY);
- cr->body_length = 0;
- cr->first_block_end = -1;
- cr->first_method = HTTP_METHOD_UNKNOWN;
-
- request_index = mk_sched_get_request_index();
- if(!request_index->first)
- {
- request_index->first = request_index->last = cr;
- }
- else{
- request_index->last->next = cr;
- request_index->last = cr;
- }
- mk_sched_set_request_index(request_index);
+ struct request_idx *request_index;
+ struct client_request *cr;
+ struct sched_connection *sc;
+ sc = mk_sched_get_connection(NULL, socket);
+ cr = mk_mem_malloc(sizeof(struct client_request));
- mk_sched_update_thread_status(NULL,
- MK_SCHEDULER_ACTIVE_UP,
- MK_SCHEDULER_CLOSED_NONE);
-
- return cr;
+ /* IPv4 Address */
+ cr->ipv4 = (char *) sc->ipv4;
+
+ cr->pipelined = FALSE;
+ cr->counter_connections = 0;
+ cr->socket = socket;
+ cr->status = MK_REQUEST_STATUS_INCOMPLETE;
+ cr->request = NULL;
+
+ /* creation time in unix time */
+ cr->init_time = sc->arrive_time;
+
+ cr->next = NULL;
+ cr->body = mk_mem_malloc(MAX_REQUEST_BODY);
+ cr->body_length = 0;
+ cr->first_block_end = -1;
+ cr->first_method = HTTP_METHOD_UNKNOWN;
+
+ request_index = mk_sched_get_request_index();
+ if (!request_index->first) {
+ request_index->first = request_index->last = cr;
+ }
+ else {
+ request_index->last->next = cr;
+ request_index->last = cr;
+ }
+ mk_sched_set_request_index(request_index);
+
+
+ mk_sched_update_thread_status(NULL,
+ MK_SCHEDULER_ACTIVE_UP,
+ MK_SCHEDULER_CLOSED_NONE);
+
+ return cr;
}
struct client_request *mk_request_client_get(int socket)
{
- struct request_idx *request_index;
- struct client_request *cr=NULL;
-
- request_index = mk_sched_get_request_index();
- cr = request_index->first;
- while(cr!=NULL)
- {
- if(cr->socket == socket)
- {
- break;
- }
- cr = cr->next;
- }
-
- return cr;
+ struct request_idx *request_index;
+ struct client_request *cr = NULL;
+
+ request_index = mk_sched_get_request_index();
+ cr = request_index->first;
+ while (cr != NULL) {
+ if (cr->socket == socket) {
+ break;
+ }
+ cr = cr->next;
+ }
+
+ return cr;
}
/*
*/
struct client_request *mk_request_client_remove(int socket)
{
- struct request_idx *request_index;
- struct client_request *cr, *aux;
-
- request_index = mk_sched_get_request_index();
- cr = request_index->first;
-
- while(cr)
- {
- if(cr->socket == socket)
- {
- if(cr==request_index->first)
- {
- request_index->first = cr->next;
- }
- else
- {
- aux = request_index->first;
- while(aux->next!=cr)
- {
- aux = aux->next;
- }
- aux->next = cr->next;
- if(!aux->next)
- {
- request_index->last = aux;
- }
- }
- break;
- }
- cr = cr->next;
- }
-
- /* No keep alive connection */
- if(cr->counter_connections == 0){
- mk_sched_update_thread_status(NULL,
- MK_SCHEDULER_ACTIVE_DOWN,
- MK_SCHEDULER_CLOSED_UP);
- }
- //mk_pointer_free(&cr->ip);
- mk_mem_free(cr->body);
- mk_mem_free(cr);
-
- return NULL;
+ struct request_idx *request_index;
+ struct client_request *cr, *aux;
+
+ request_index = mk_sched_get_request_index();
+ cr = request_index->first;
+
+ while (cr) {
+ if (cr->socket == socket) {
+ if (cr == request_index->first) {
+ request_index->first = cr->next;
+ }
+ else {
+ aux = request_index->first;
+ while (aux->next != cr) {
+ aux = aux->next;
+ }
+ aux->next = cr->next;
+ if (!aux->next) {
+ request_index->last = aux;
+ }
+ }
+ break;
+ }
+ cr = cr->next;
+ }
+
+ /* No keep alive connection */
+ if (cr->counter_connections == 0) {
+ mk_sched_update_thread_status(NULL,
+ MK_SCHEDULER_ACTIVE_DOWN,
+ MK_SCHEDULER_CLOSED_UP);
+ }
+ //mk_pointer_free(&cr->ip);
+ mk_mem_free(cr->body);
+ mk_mem_free(cr);
+
+ return NULL;
}
struct header_toc *mk_request_header_toc_create(int len)
{
- int i;
- struct header_toc *p;
+ int i;
+ struct header_toc *p;
- p = (struct header_toc *) pthread_getspecific(mk_cache_header_toc);
+ p = (struct header_toc *) pthread_getspecific(mk_cache_header_toc);
- for(i=0; i<len; i++)
- {
- p[i].init = NULL;
- p[i].end = NULL;
- p[i].status = 0;
- }
- return p;
+ for (i = 0; i < len; i++) {
+ p[i].init = NULL;
+ p[i].end = NULL;
+ p[i].status = 0;
+ }
+ return p;
}
void mk_request_header_toc_parse(struct header_toc *toc, char *data, int len)
{
- char *p, *l;
- int i;
-
- p = data;
- for(i=0; i<len && p; i++)
- {
- l = strstr(p, MK_CRLF);
- if(l)
- {
- toc[i].init = p;
- toc[i].end = l;
- p = l + mk_crlf.len;
- }
- else
- {
- break;
- }
+ char *p, *l;
+ int i;
+
+ p = data;
+ for (i = 0; i < len && p; i++) {
+ l = strstr(p, MK_CRLF);
+ if (l) {
+ toc[i].init = p;
+ toc[i].end = l;
+ p = l + mk_crlf.len;
+ }
+ else {
+ break;
}
+ }
}
void mk_request_ka_next(struct client_request *cr)
{
- bzero(cr->body, sizeof(cr->body));
- cr->first_method = -1;
- cr->first_block_end = -1;
- cr->body_length = 0;
- cr->counter_connections++;
+ bzero(cr->body, sizeof(cr->body));
+ cr->first_method = -1;
+ cr->first_block_end = -1;
+ cr->body_length = 0;
+ cr->counter_connections++;
}
void mk_request_handler_register(struct request *sr, struct plugin *p)
{
- struct handler *new, *aux;
+ struct handler *new, *aux;
- new = mk_mem_malloc(sizeof(struct handler));
- new->p = p;
- new->next = NULL;
+ new = mk_mem_malloc(sizeof(struct handler));
+ new->p = p;
+ new->next = NULL;
- if(!sr->handled_by){
- sr->handled_by = new;
- return;
- }
+ if (!sr->handled_by) {
+ sr->handled_by = new;
+ return;
+ }
- aux = sr->handled_by;
- while(aux){
- if(!aux->next){
- aux->next = new;
- return;
- }
- aux = aux->next;
+ aux = sr->handled_by;
+ while (aux) {
+ if (!aux->next) {
+ aux->next = new;
+ return;
}
-
- printf("\nMK_REQUEST_REGISTER_HANDLER: NEVER ASSIGNED");
- fflush(stdout);
+ aux = aux->next;
+ }
+
+ printf("\nMK_REQUEST_REGISTER_HANDLER: NEVER ASSIGNED");
+ fflush(stdout);
}
void mk_request_handler_clear(struct request *sr)
{
- struct handler *prev=0, *aux;
+ struct handler *prev = 0, *aux;
- if(!sr->handled_by){
- return;
- }
+ if (!sr->handled_by) {
+ return;
+ }
- while(sr->handled_by){
- aux = sr->handled_by;
- while(aux->next){
- prev = aux;
- aux = aux->next;
- }
- mk_mem_free(aux);
- prev->next = NULL;
+ while (sr->handled_by) {
+ aux = sr->handled_by;
+ while (aux->next) {
+ prev = aux;
+ aux = aux->next;
}
+ mk_mem_free(aux);
+ prev->next = NULL;
+ }
- return;
+ return;
}
/* Register thread information */
int mk_sched_register_thread(pthread_t tid, int efd)
{
- int i;
- struct sched_list_node *sr, *aux;
-
- sr = mk_mem_malloc_z(sizeof(struct sched_list_node));
- sr->tid = tid;
- sr->pid = -1;
- sr->epoll_fd = efd;
- sr->queue = mk_mem_malloc_z(sizeof(struct sched_connection)*
- config->worker_capacity);
- sr->request_handler = NULL;
- sr->next = NULL;
-
- for(i=0; i<config->worker_capacity; i++){
- sr->queue[i].status = MK_SCHEDULER_CONN_AVAILABLE;
- }
+ int i;
+ struct sched_list_node *sr, *aux;
+
+ sr = mk_mem_malloc_z(sizeof(struct sched_list_node));
+ sr->tid = tid;
+ sr->pid = -1;
+ sr->epoll_fd = efd;
+ sr->queue = mk_mem_malloc_z(sizeof(struct sched_connection) *
+ config->worker_capacity);
+ sr->request_handler = NULL;
+ sr->next = NULL;
+
+ for (i = 0; i < config->worker_capacity; i++) {
+ sr->queue[i].status = MK_SCHEDULER_CONN_AVAILABLE;
+ }
+
+ if (!sched_list) {
+ sr->idx = 1;
+ sched_list = sr;
+ return 0;
+ }
- if(!sched_list)
- {
- sr->idx = 1;
- sched_list = sr;
- return 0;
- }
-
- aux = sched_list;
- while(aux->next)
- {
- aux = aux->next;
- }
- sr->idx = aux->idx + 1;
- aux->next = sr;
-
- return 0;
+ aux = sched_list;
+ while (aux->next) {
+ aux = aux->next;
+ }
+ sr->idx = aux->idx + 1;
+ aux->next = sr;
+
+ return 0;
}
/*
*/
int mk_sched_launch_thread(int max_events)
{
- int efd;
- pthread_t tid;
- pthread_attr_t attr;
- sched_thread_conf *thconf;
- pthread_mutex_t mutex_wait_register;
-
- /* Creating epoll file descriptor */
- efd = mk_epoll_create(max_events);
- if(efd < 1)
- {
- return -1;
- }
-
- /* Thread stuff */
- pthread_mutex_init(&mutex_wait_register,(pthread_mutexattr_t *) NULL);
- pthread_mutex_lock(&mutex_wait_register);
-
- thconf = mk_mem_malloc(sizeof(sched_thread_conf));
- thconf->epoll_fd = efd;
- thconf->max_events = max_events;
-
- pthread_attr_init(&attr);
- pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
- if(pthread_create(&tid, &attr, mk_sched_launch_epoll_loop,
- (void *) thconf)!=0)
- {
- perror("pthread_create");
- return -1;
- }
-
- /* Register working thread */
- mk_sched_register_thread(tid, efd);
- pthread_mutex_unlock(&mutex_wait_register);
-
- return 0;
+ int efd;
+ pthread_t tid;
+ pthread_attr_t attr;
+ sched_thread_conf *thconf;
+ pthread_mutex_t mutex_wait_register;
+
+ /* Creating epoll file descriptor */
+ efd = mk_epoll_create(max_events);
+ if (efd < 1) {
+ return -1;
+ }
+
+ /* Thread stuff */
+ pthread_mutex_init(&mutex_wait_register, (pthread_mutexattr_t *) NULL);
+ pthread_mutex_lock(&mutex_wait_register);
+
+ thconf = mk_mem_malloc(sizeof(sched_thread_conf));
+ thconf->epoll_fd = efd;
+ thconf->max_events = max_events;
+
+ pthread_attr_init(&attr);
+ pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
+ if (pthread_create(&tid, &attr, mk_sched_launch_epoll_loop,
+ (void *) thconf) != 0) {
+ perror("pthread_create");
+ return -1;
+ }
+
+ /* Register working thread */
+ mk_sched_register_thread(tid, efd);
+ pthread_mutex_unlock(&mutex_wait_register);
+
+ return 0;
}
/* created thread, all this calls are in the thread context */
void *mk_sched_launch_epoll_loop(void *thread_conf)
{
- sched_thread_conf *thconf;
- struct sched_list_node *thinfo;
+ sched_thread_conf *thconf;
+ struct sched_list_node *thinfo;
- /* Avoid SIGPIPE signals */
- mk_signal_thread_sigpipe_safe();
+ /* Avoid SIGPIPE signals */
+ mk_signal_thread_sigpipe_safe();
- thconf = thread_conf;
+ thconf = thread_conf;
- /* Init specific thread cache */
- mk_cache_thread_init();
- mk_plugin_worker_startup();
+ /* Init specific thread cache */
+ mk_cache_thread_init();
+ mk_plugin_worker_startup();
- mk_epoll_handlers *handler;
- handler = mk_epoll_set_handlers((void *) mk_conn_read,
- (void *) mk_conn_write,
- (void *) mk_conn_error,
- (void *) mk_conn_close,
- (void *) mk_conn_timeout);
+ mk_epoll_handlers *handler;
+ handler = mk_epoll_set_handlers((void *) mk_conn_read,
+ (void *) mk_conn_write,
+ (void *) mk_conn_error,
+ (void *) mk_conn_close,
+ (void *) mk_conn_timeout);
- /* Nasty way to export task id */
- usleep(1000);
+ /* Nasty way to export task id */
+ usleep(1000);
+ thinfo = mk_sched_get_thread_conf();
+ while (!thinfo) {
thinfo = mk_sched_get_thread_conf();
- while(!thinfo){
- thinfo = mk_sched_get_thread_conf();
- }
-
- /* Glibc doesn't export to user space the gettid() syscall */
- thinfo->pid = syscall(__NR_gettid);
+ }
- mk_sched_set_thread_poll(thconf->epoll_fd);
- mk_epoll_init(thconf->epoll_fd, handler, thconf->max_events);
+ /* Glibc doesn't export to user space the gettid() syscall */
+ thinfo->pid = syscall(__NR_gettid);
- return 0;
+ mk_sched_set_thread_poll(thconf->epoll_fd);
+ mk_epoll_init(thconf->epoll_fd, handler, thconf->max_events);
+
+ return 0;
}
struct request_idx *mk_sched_get_request_index()
{
- return pthread_getspecific(request_index);
+ return pthread_getspecific(request_index);
}
void mk_sched_set_request_index(struct request_idx *ri)
{
- pthread_setspecific(request_index, (void *)ri);
+ pthread_setspecific(request_index, (void *) ri);
}
void mk_sched_set_thread_poll(int epoll)
{
- pthread_setspecific(epoll_fd, (void *) epoll);
+ pthread_setspecific(epoll_fd, (void *) epoll);
}
int mk_sched_get_thread_poll()
{
- return (int) pthread_getspecific(epoll_fd);
+ return (int) pthread_getspecific(epoll_fd);
}
struct sched_list_node *mk_sched_get_thread_conf()
{
- struct sched_list_node *node;
- pthread_t current;
-
- current = pthread_self();
- node = sched_list;
- while(node){
- if(pthread_equal(node->tid, current) != 0){
- return node;
- }
- node = node->next;
+ struct sched_list_node *node;
+ pthread_t current;
+
+ current = pthread_self();
+ node = sched_list;
+ while (node) {
+ if (pthread_equal(node->tid, current) != 0) {
+ return node;
}
+ node = node->next;
+ }
- return NULL;
+ return NULL;
}
void mk_sched_update_thread_status(struct sched_list_node *sched,
int active, int closed)
{
- if(!sched){
- sched = mk_sched_get_thread_conf();
- }
-
- switch(active){
- case MK_SCHEDULER_ACTIVE_UP:
- sched->active_requests++;
- break;
- case MK_SCHEDULER_ACTIVE_DOWN:
- sched->active_requests--;
- break;
- }
-
- switch(closed){
- case MK_SCHEDULER_CLOSED_UP:
- sched->closed_requests++;
- break;
- case MK_SCHEDULER_CLOSED_DOWN:
- sched->closed_requests--;
- break;
- }
+ if (!sched) {
+ sched = mk_sched_get_thread_conf();
+ }
+
+ switch (active) {
+ case MK_SCHEDULER_ACTIVE_UP:
+ sched->active_requests++;
+ break;
+ case MK_SCHEDULER_ACTIVE_DOWN:
+ sched->active_requests--;
+ break;
+ }
+
+ switch (closed) {
+ case MK_SCHEDULER_CLOSED_UP:
+ sched->closed_requests++;
+ break;
+ case MK_SCHEDULER_CLOSED_DOWN:
+ sched->closed_requests--;
+ break;
+ }
}
int mk_sched_add_client(struct sched_list_node *sched, int remote_fd)
{
- unsigned int i, ret;
-
- /* Look for an available slot */
- for(i=0; i<config->worker_capacity; i++){
- if(sched->queue[i].status == MK_SCHEDULER_CONN_AVAILABLE){
- /* Set IP */
- bzero(sched->queue[i].ipv4, 17);
- mk_socket_get_ip(remote_fd, sched->queue[i].ipv4);
-
- /* Before to continue, we need run plugin stage 20 */
- ret = mk_plugin_stage_run(MK_PLUGIN_STAGE_20,
- remote_fd,
- &sched->queue[i],
- NULL, NULL);
-
- /* Close connection, otherwise continue */
- if(ret == MK_PLUGIN_RET_CLOSE_CONX){
- mk_conn_close(remote_fd);
- return MK_PLUGIN_RET_CLOSE_CONX;
- }
-
- /* Socket and status */
- sched->queue[i].socket = remote_fd;
- sched->queue[i].status = MK_SCHEDULER_CONN_PENDING;
- sched->queue[i].arrive_time = log_current_utime;
-
- mk_epoll_add_client(sched->epoll_fd, remote_fd,
- MK_EPOLL_BEHAVIOR_TRIGGERED);
- return 0;
- }
+ unsigned int i, ret;
+
+ /* Look for an available slot */
+ for (i = 0; i < config->worker_capacity; i++) {
+ if (sched->queue[i].status == MK_SCHEDULER_CONN_AVAILABLE) {
+ /* Set IP */
+ bzero(sched->queue[i].ipv4, 17);
+ mk_socket_get_ip(remote_fd, sched->queue[i].ipv4);
+
+ /* Before to continue, we need run plugin stage 20 */
+ ret = mk_plugin_stage_run(MK_PLUGIN_STAGE_20,
+ remote_fd,
+ &sched->queue[i], NULL, NULL);
+
+ /* Close connection, otherwise continue */
+ if (ret == MK_PLUGIN_RET_CLOSE_CONX) {
+ mk_conn_close(remote_fd);
+ return MK_PLUGIN_RET_CLOSE_CONX;
+ }
+
+ /* Socket and status */
+ sched->queue[i].socket = remote_fd;
+ sched->queue[i].status = MK_SCHEDULER_CONN_PENDING;
+ sched->queue[i].arrive_time = log_current_utime;
+
+ mk_epoll_add_client(sched->epoll_fd, remote_fd,
+ MK_EPOLL_BEHAVIOR_TRIGGERED);
+ return 0;
}
+ }
- return -1;
+ return -1;
}
int mk_sched_remove_client(struct sched_list_node *sched, int remote_fd)
{
- struct sched_connection *sc;
-
- sc = mk_sched_get_connection(sched, remote_fd);
- if(sc){
- close(remote_fd);
- sc->status = MK_SCHEDULER_CONN_AVAILABLE;
- return 0;
- }
- return -1;
+ struct sched_connection *sc;
+
+ sc = mk_sched_get_connection(sched, remote_fd);
+ if (sc) {
+ close(remote_fd);
+ sc->status = MK_SCHEDULER_CONN_AVAILABLE;
+ return 0;
+ }
+ return -1;
}
-struct sched_connection *mk_sched_get_connection(struct sched_list_node *sched,
- int remote_fd)
+struct sched_connection *mk_sched_get_connection(struct sched_list_node
+ *sched, int remote_fd)
{
- int i;
-
- if(!sched){
- sched = mk_sched_get_thread_conf();
- if(!sched){
- close(remote_fd);
- return NULL;
- }
+ int i;
+
+ if (!sched) {
+ sched = mk_sched_get_thread_conf();
+ if (!sched) {
+ close(remote_fd);
+ return NULL;
}
+ }
- for(i=0; i<config->worker_capacity; i++){
- if(sched->queue[i].socket == remote_fd){
- return &sched->queue[i];
- }
+ for (i = 0; i < config->worker_capacity; i++) {
+ if (sched->queue[i].socket == remote_fd) {
+ return &sched->queue[i];
}
+ }
- return NULL;
+ return NULL;
}
int mk_sched_check_timeouts(struct sched_list_node *sched)
{
- int i;
- struct request_idx *req_idx;
- struct client_request *req_cl;
-
- /* PENDING CONN TIMEOUT */
- for(i=0; i<config->worker_capacity; i++){
- if(sched->queue[i].status == MK_SCHEDULER_CONN_PENDING){
- if(sched->queue[i].arrive_time + config->timeout <=
- log_current_utime){
- mk_sched_remove_client(sched, sched->queue[i].socket);
- }
- }
+ int i;
+ struct request_idx *req_idx;
+ struct client_request *req_cl;
+
+ /* PENDING CONN TIMEOUT */
+ for (i = 0; i < config->worker_capacity; i++) {
+ if (sched->queue[i].status == MK_SCHEDULER_CONN_PENDING) {
+ if (sched->queue[i].arrive_time + config->timeout <=
+ log_current_utime) {
+ mk_sched_remove_client(sched, sched->queue[i].socket);
+ }
}
+ }
- /* PROCESSING CONN TIMEOUT */
- req_idx = mk_sched_get_request_index();
- req_cl = req_idx->first;
-
- while(req_cl){
- if(req_cl->status == MK_REQUEST_STATUS_INCOMPLETE){
- if((req_cl->init_time + config->timeout) >=
- log_current_utime){
- close(req_cl->socket);
- }
- }
- req_cl = req_cl->next;
+ /* PROCESSING CONN TIMEOUT */
+ req_idx = mk_sched_get_request_index();
+ req_cl = req_idx->first;
+
+ while (req_cl) {
+ if (req_cl->status == MK_REQUEST_STATUS_INCOMPLETE) {
+ if ((req_cl->init_time + config->timeout) >= log_current_utime) {
+ close(req_cl->socket);
+ }
}
+ req_cl = req_cl->next;
+ }
- return 0;
+ return 0;
}
int mk_sched_update_conn_status(struct sched_list_node *sched,
int remote_fd, int status)
{
- int i;
+ int i;
- if(!sched){
- return -1;
- }
+ if (!sched) {
+ return -1;
+ }
- for(i=0; i<config->workers; i++){
- if(sched->queue[i].socket == remote_fd){
- sched->queue[i].status = status;
- return 0;
- }
+ for (i = 0; i < config->workers; i++) {
+ if (sched->queue[i].socket == remote_fd) {
+ sched->queue[i].status = status;
+ return 0;
}
- return 0;
+ }
+ return 0;
}
*/
int mk_server_worker_capacity(int nworkers)
{
- int max, avl;
- struct rlimit lim;
-
- /* Limit by system */
- getrlimit(RLIMIT_NOFILE, &lim);
- max = lim.rlim_cur;
-
- /* Minimum of fds needed by Monkey:
- * --------------------------------
- * 3 fds: stdin, stdout, stderr
- * 1 fd for main socket server
- * 1 fd for epoll array (per thread)
- * 1 fd for worker logger when writing to FS
- * 2 fd for worker logger pipe
- */
-
- avl = max - (3 + 1 + nworkers + 1 + 2);
- return ((avl/2)/nworkers);
+ int max, avl;
+ struct rlimit lim;
+
+ /* Limit by system */
+ getrlimit(RLIMIT_NOFILE, &lim);
+ max = lim.rlim_cur;
+
+ /* Minimum of fds needed by Monkey:
+ * --------------------------------
+ * 3 fds: stdin, stdout, stderr
+ * 1 fd for main socket server
+ * 1 fd for epoll array (per thread)
+ * 1 fd for worker logger when writing to FS
+ * 2 fd for worker logger pipe
+ */
+
+ avl = max - (3 + 1 + nworkers + 1 + 2);
+ return ((avl / 2) / nworkers);
}
/* Here we launch the worker threads to attend clients */
void mk_server_launch_workers()
{
- int i;
+ int i;
- config->worker_capacity = mk_server_worker_capacity(config->workers);
-
- for(i=0; i<config->workers; i++)
- {
- mk_sched_launch_thread(config->worker_capacity);
- }
+ config->worker_capacity = mk_server_worker_capacity(config->workers);
+
+ for (i = 0; i < config->workers; i++) {
+ mk_sched_launch_thread(config->worker_capacity);
+ }
}
void mk_server_loop(int server_fd)
{
- int remote_fd;
- struct sockaddr_in sockaddr;
- struct sched_list_node *sched = sched_list;
- socklen_t socket_size = sizeof(struct sockaddr_in);
-
- while(1){
- remote_fd = accept(server_fd, (struct sockaddr *)&sockaddr,
- &socket_size);
-
- if(remote_fd == -1){
- continue;
- }
-
- /* Assign socket to worker thread */
- mk_sched_add_client(sched, remote_fd);
-
- if(sched->next){
- sched = sched->next;
- }
- else{
- sched = sched_list;
- }
- }
+ int remote_fd;
+ struct sockaddr_in sockaddr;
+ struct sched_list_node *sched = sched_list;
+ socklen_t socket_size = sizeof(struct sockaddr_in);
+
+ while (1) {
+ remote_fd = accept(server_fd, (struct sockaddr *) &sockaddr,
+ &socket_size);
+
+ if (remote_fd == -1) {
+ continue;
+ }
+
+ /* Assign socket to worker thread */
+ mk_sched_add_client(sched, remote_fd);
+
+ if (sched->next) {
+ sched = sched->next;
+ }
+ else {
+ sched = sched_list;
+ }
+ }
}
void mk_signal_handler(int signo)
{
-switch( signo ) {
- case SIGUSR2:
- printf("%s => Monkey reconfiguration \n",
- log_current_time.data); /* Not ready */
- break;
+ switch (signo) {
+ case SIGUSR2:
+ printf("%s => Monkey reconfiguration \n", log_current_time.data); /* Not ready */
+ break;
- case SIGINT:
- mk_logger_remove_pid();
- printf("\n\n%s => Interrupt from keyboard\n\n",
- log_current_time.data);
- exit(0);
- case SIGHUP:
- printf("%s => Hangup\n",
- log_current_time.data);
- mk_signal_term();
- break;
+ case SIGINT:
+ mk_logger_remove_pid();
+ printf("\n\n%s => Interrupt from keyboard\n\n",
+ log_current_time.data);
+ exit(0);
+ case SIGHUP:
+ printf("%s => Hangup\n", log_current_time.data);
+ mk_signal_term();
+ break;
- case SIGBUS:
- printf("%s => Invalid memory reference\n",
- log_current_time.data);
- abort();
- break;
-
- case SIGPIPE:
- printf("\n sigpipe");
- fflush(stdout);
- break;
-
- case SIGSEGV:
- printf("%s => Invalid memory reference\n",
- log_current_time.data);
- break;
-
- case SIGTERM:
- printf("%s => Termination signal\n",
- log_current_time.data);
- mk_signal_term();
- break;
- }
+ case SIGBUS:
+ printf("%s => Invalid memory reference\n", log_current_time.data);
+ abort();
+ break;
- pthread_exit(NULL);
+ case SIGPIPE:
+ printf("\n sigpipe");
+ fflush(stdout);
+ break;
+
+ case SIGSEGV:
+ printf("%s => Invalid memory reference\n", log_current_time.data);
+ break;
+
+ case SIGTERM:
+ printf("%s => Termination signal\n", log_current_time.data);
+ mk_signal_term();
+ break;
+ }
+
+ pthread_exit(NULL);
}
void mk_signal_init()
{
- signal(SIGHUP , (void *) mk_signal_handler);
- signal(SIGINT , (void *) mk_signal_handler);
- signal(SIGPIPE, (void *) mk_signal_handler);
- signal(SIGBUS, (void *) mk_signal_handler);
- signal(SIGSEGV, (void *) mk_signal_handler);
- signal(SIGTERM, (void *) mk_signal_handler);
- signal(SIGUSR2, (void *) mk_signal_handler);
+ signal(SIGHUP, (void *) mk_signal_handler);
+ signal(SIGINT, (void *) mk_signal_handler);
+ signal(SIGPIPE, (void *) mk_signal_handler);
+ signal(SIGBUS, (void *) mk_signal_handler);
+ signal(SIGSEGV, (void *) mk_signal_handler);
+ signal(SIGTERM, (void *) mk_signal_handler);
+ signal(SIGUSR2, (void *) mk_signal_handler);
}
void mk_signal_term()
{
- signal(SIGHUP , (void *) SIG_DFL);
- signal(SIGINT , (void *) SIG_DFL);
- signal(SIGPIPE, (void *) SIG_DFL);
- signal(SIGBUS, (void *) SIG_DFL);
- signal(SIGSEGV, (void *) SIG_DFL);
- signal(SIGTERM, (void *) SIG_DFL);
- signal(SIGUSR2, (void *) SIG_DFL);
+ signal(SIGHUP, (void *) SIG_DFL);
+ signal(SIGINT, (void *) SIG_DFL);
+ signal(SIGPIPE, (void *) SIG_DFL);
+ signal(SIGBUS, (void *) SIG_DFL);
+ signal(SIGSEGV, (void *) SIG_DFL);
+ signal(SIGTERM, (void *) SIG_DFL);
+ signal(SIGUSR2, (void *) SIG_DFL);
}
void mk_signal_thread_sigpipe_safe()
{
- sigset_t set, old;
+ sigset_t set, old;
- sigemptyset(&set);
- sigaddset(&set, SIGPIPE);
- pthread_sigmask(SIG_BLOCK, &set, &old);
+ sigemptyset(&set);
+ sigaddset(&set, SIGPIPE);
+ pthread_sigmask(SIG_BLOCK, &set, &old);
}
*/
int mk_socket_set_cork_flag(int fd, int state)
{
- return setsockopt(fd, SOL_TCP, TCP_CORK, &state, sizeof(state));
+ return setsockopt(fd, SOL_TCP, TCP_CORK, &state, sizeof(state));
}
int mk_socket_set_nonblocking(int sockfd)
{
- if (fcntl(sockfd, F_SETFL, fcntl(sockfd, F_GETFD, 0)|O_NONBLOCK) == -1) {
- perror("fcntl");
- return -1;
- }
- return 0;
+ if (fcntl(sockfd, F_SETFL, fcntl(sockfd, F_GETFD, 0) | O_NONBLOCK) == -1) {
+ perror("fcntl");
+ return -1;
+ }
+ return 0;
}
int mk_socket_set_tcp_nodelay(int sockfd)
{
- int on=1;
- return setsockopt(sockfd, SOL_TCP, TCP_NODELAY, &on, sizeof(on));
+ int on = 1;
+ return setsockopt(sockfd, SOL_TCP, TCP_NODELAY, &on, sizeof(on));
}
int mk_socket_get_ip(int socket, char *ipv4)
{
- int ipv4_len = 16;
- socklen_t len;
- struct sockaddr_in m_addr;
+ int ipv4_len = 16;
+ socklen_t len;
+ struct sockaddr_in m_addr;
- len = sizeof(m_addr);
- getpeername(socket, (struct sockaddr*)&m_addr, &len);
- inet_ntop(PF_INET, &m_addr.sin_addr, ipv4, ipv4_len);
+ len = sizeof(m_addr);
+ getpeername(socket, (struct sockaddr *) &m_addr, &len);
+ inet_ntop(PF_INET, &m_addr.sin_addr, ipv4, ipv4_len);
- return 0;
+ return 0;
}
int mk_socket_close(int socket)
{
- return close(socket);
+ return close(socket);
}
-int mk_socket_timeout(int s, char *buf, int len,
- int timeout, int recv_send)
+int mk_socket_timeout(int s, char *buf, int len, int timeout, int recv_send)
{
- fd_set fds;
- time_t init_time, max_time;
- int n=0, status;
- struct timeval tv;
-
- init_time=time(NULL);
- max_time = init_time + timeout;
-
- FD_ZERO(&fds);
- FD_SET(s,&fds);
-
- tv.tv_sec=timeout;
- tv.tv_usec=0;
-
- if(recv_send==ST_RECV)
- n=select(s+1,&fds,NULL,NULL,&tv); // recv
- else{
- n=select(s+1,NULL,&fds,NULL,&tv); // send
- }
-
- switch(n){
- case 0:
- return -2;
- break;
- case -1:
- //pthread_kill(pthread_self(), SIGPIPE);
- return -1;
- }
-
- if(recv_send==ST_RECV){
- status=recv(s,buf,len, 0);
- }
- else{
- status=send(s,buf,len, 0);
- }
-
- if( status < 0 ){
- if(time(NULL) >= max_time){
- //pthread_kill(pthread_self(), SIGPIPE);
- }
- }
-
- return status;
+ fd_set fds;
+ time_t init_time, max_time;
+ int n = 0, status;
+ struct timeval tv;
+
+ init_time = time(NULL);
+ max_time = init_time + timeout;
+
+ FD_ZERO(&fds);
+ FD_SET(s, &fds);
+
+ tv.tv_sec = timeout;
+ tv.tv_usec = 0;
+
+ if (recv_send == ST_RECV)
+ n = select(s + 1, &fds, NULL, NULL, &tv); // recv
+ else {
+ n = select(s + 1, NULL, &fds, NULL, &tv); // send
+ }
+
+ switch (n) {
+ case 0:
+ return -2;
+ break;
+ case -1:
+ //pthread_kill(pthread_self(), SIGPIPE);
+ return -1;
+ }
+
+ if (recv_send == ST_RECV) {
+ status = recv(s, buf, len, 0);
+ }
+ else {
+ status = send(s, buf, len, 0);
+ }
+
+ if (status < 0) {
+ if (time(NULL) >= max_time) {
+ //pthread_kill(pthread_self(), SIGPIPE);
+ }
+ }
+
+ return status;
}
int mk_socket_create()
{
- int sockfd;
+ int sockfd;
- if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
- perror("client: socket");
- return -1;
- }
+ if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
+ perror("client: socket");
+ return -1;
+ }
- return sockfd;
+ return sockfd;
}
int mk_socket_connect(int sockfd, char *server, int port)
{
- int res;
- struct sockaddr_in *remote;
-
- remote = (struct sockaddr_in *)
- mk_mem_malloc_z(sizeof(struct sockaddr_in));
- remote->sin_family = AF_INET;
+ int res;
+ struct sockaddr_in *remote;
- res = inet_pton(AF_INET, server, (void *)(&(remote->sin_addr.s_addr)));
+ remote = (struct sockaddr_in *)
+ mk_mem_malloc_z(sizeof(struct sockaddr_in));
+ remote->sin_family = AF_INET;
- if(res < 0)
- {
- perror("Can't set remote->sin_addr.s_addr");
- mk_mem_free(remote);
- return -1;
- }
- else if(res == 0){
- perror("Invalid IP address\n");
- mk_mem_free(remote);
- return -1;
- }
+ res = inet_pton(AF_INET, server, (void *) (&(remote->sin_addr.s_addr)));
- remote->sin_port = htons(port);
- if (connect(sockfd,
- (struct sockaddr *)remote,
- sizeof(struct sockaddr)) == -1)
- {
- close(sockfd);
- perror("connect");
- return -1;
- }
+ if (res < 0) {
+ perror("Can't set remote->sin_addr.s_addr");
mk_mem_free(remote);
- return 0;
+ return -1;
+ }
+ else if (res == 0) {
+ perror("Invalid IP address\n");
+ mk_mem_free(remote);
+ return -1;
+ }
+
+ remote->sin_port = htons(port);
+ if (connect(sockfd,
+ (struct sockaddr *) remote, sizeof(struct sockaddr)) == -1) {
+ close(sockfd);
+ perror("connect");
+ return -1;
+ }
+ mk_mem_free(remote);
+ return 0;
}
void mk_socket_reset(int socket)
{
- int status=1;
-
- if(setsockopt(socket,SOL_SOCKET,SO_REUSEADDR,&status,sizeof(int))==-1) {
- perror("setsockopt");
- exit(1);
- }
+ int status = 1;
+
+ if (setsockopt(socket, SOL_SOCKET, SO_REUSEADDR, &status, sizeof(int)) ==
+ -1) {
+ perror("setsockopt");
+ exit(1);
+ }
}
/* Just IPv4 for now... */
int mk_socket_server(int port)
{
- int fd;
- struct sockaddr_in local_sockaddr_in;
-
- /* Create server socket */
- fd=socket(PF_INET,SOCK_STREAM,0);
- mk_socket_set_tcp_nodelay(fd);
-
- local_sockaddr_in.sin_family=AF_INET;
- local_sockaddr_in.sin_port=htons(port);
- local_sockaddr_in.sin_addr.s_addr=INADDR_ANY;
- memset(&(local_sockaddr_in.sin_zero),'\0',8);
-
- /* Avoid bind issues, reset socket */
- mk_socket_reset(fd);
-
- if(bind(fd,(struct sockaddr *)&local_sockaddr_in,
- sizeof(struct sockaddr)) != 0)
- {
- perror("bind");
- printf("Error: Port %i cannot be used\n", port);
- exit(1);
- }
-
- /* Listen queue:
- * The queue limit is given by /proc/sys/net/core/somaxconn
- * we need to add a dynamic function to get that value on fly
- */
- if((listen(fd, 1024))!=0) {
- perror("listen");
- exit(1);
- }
-
- return fd;
+ int fd;
+ struct sockaddr_in local_sockaddr_in;
+
+ /* Create server socket */
+ fd = socket(PF_INET, SOCK_STREAM, 0);
+ mk_socket_set_tcp_nodelay(fd);
+
+ local_sockaddr_in.sin_family = AF_INET;
+ local_sockaddr_in.sin_port = htons(port);
+ local_sockaddr_in.sin_addr.s_addr = INADDR_ANY;
+ memset(&(local_sockaddr_in.sin_zero), '\0', 8);
+
+ /* Avoid bind issues, reset socket */
+ mk_socket_reset(fd);
+
+ if (bind(fd, (struct sockaddr *) &local_sockaddr_in,
+ sizeof(struct sockaddr)) != 0) {
+ perror("bind");
+ printf("Error: Port %i cannot be used\n", port);
+ exit(1);
+ }
+
+ /* Listen queue:
+ * The queue limit is given by /proc/sys/net/core/somaxconn
+ * we need to add a dynamic function to get that value on fly
+ */
+ if ((listen(fd, 1024)) != 0) {
+ perror("listen");
+ exit(1);
+ }
+
+ return fd;
}
/* Return a buffer with a new string from string */
char *mk_string_copy_substr(const char *string, int pos_init, int pos_end)
{
- unsigned int size, bytes;
- char *buffer=0;
-
- size = (unsigned int) (pos_end - pos_init ) + 1;
- if(size<=2) size=4;
-
- buffer = malloc(size);
-
- if(!buffer){
- return NULL;
- }
-
- if(pos_init > pos_end)
- {
- mk_mem_free(buffer);
- return NULL;
- }
-
- bytes = pos_end - pos_init;
- memcpy(buffer, string+pos_init, bytes);
- buffer[bytes]='\0';
-
- return (char *) buffer;
+ unsigned int size, bytes;
+ char *buffer = 0;
+
+ size = (unsigned int) (pos_end - pos_init) + 1;
+ if (size <= 2)
+ size = 4;
+
+ buffer = malloc(size);
+
+ if (!buffer) {
+ return NULL;
+ }
+
+ if (pos_init > pos_end) {
+ mk_mem_free(buffer);
+ return NULL;
+ }
+
+ bytes = pos_end - pos_init;
+ memcpy(buffer, string + pos_init, bytes);
+ buffer[bytes] = '\0';
+
+ return (char *) buffer;
}
int mk_string_char_search(char *string, int c, int n)
{
- int i;
+ int i;
- if(n<0)
- {
- n = strlen(string);
- }
+ if (n < 0) {
+ n = strlen(string);
+ }
- for(i=0; i<n; i++)
- {
- if(string[i]==c)
- return i;
- }
+ for (i = 0; i < n; i++) {
+ if (string[i] == c)
+ return i;
+ }
- return -1;
+ return -1;
}
+
/* Get position of a substring.
* Original version taken from google, modified in order
* to send the position instead the substring.
int _mk_string_search(char *string, char *search, int n)
{
- char *np;
- int res;
+ char *np;
+ int res;
- np = strcasestr(string, search);
- if(!np)
- {
- return -1;
- }
+ np = strcasestr(string, search);
+ if (!np) {
+ return -1;
+ }
- res = np-string;
- if(res>n && n>=0)
- {
- return -1;
- }
- return (np-string);
+ res = np - string;
+ if (res > n && n >= 0) {
+ return -1;
+ }
+ return (np - string);
}
int mk_string_search(char *string, char *search)
{
- return _mk_string_search(string, search, -1);
+ return _mk_string_search(string, search, -1);
}
/* lookup char in reverse order */
int mk_string_search_r(char *string, char search, int n)
{
- int i,j;
+ int i, j;
- if(n>=0){
- j = n;
- }
- else{
- j = strlen(string);
- }
+ if (n >= 0) {
+ j = n;
+ }
+ else {
+ j = strlen(string);
+ }
- for(i=j;i>=0;i--)
- {
- if(string[i]==search){
- return i;
- }
+ for (i = j; i >= 0; i--) {
+ if (string[i] == search) {
+ return i;
}
+ }
- return -1;
+ return -1;
}
int mk_string_search_n(char *string, char *search, int n)
{
- return _mk_string_search(string, search, n);
+ return _mk_string_search(string, search, n);
}
char *mk_string_remove_space(char *buf)
{
size_t bufsize;
- int new_i=0, i, len, spaces=0;
- char *new_buf=0;
+ int new_i = 0, i, len, spaces = 0;
+ char *new_buf = 0;
len = strlen(buf);
- for(i=0; i<len; i++)
- {
- if(buf[i] == ' '){
+ for (i = 0; i < len; i++) {
+ if (buf[i] == ' ') {
spaces++;
}
}
- bufsize = len+1-spaces;
- if(bufsize <= 1){
+ bufsize = len + 1 - spaces;
+ if (bufsize <= 1) {
return NULL;
}
new_buf = mk_mem_malloc(bufsize);
- for(i=0; i<len; i++)
- {
- if(buf[i] != ' '){
+ for (i = 0; i < len; i++) {
+ if (buf[i] != ' ') {
new_buf[new_i] = buf[i];
new_i++;
}
char *mk_string_casestr(char *heystack, char *needle)
{
- if(!heystack || !needle)
- {
- return NULL;
- }
+ if (!heystack || !needle) {
+ return NULL;
+ }
- return strcasestr(heystack, needle);
+ return strcasestr(heystack, needle);
}
char *mk_string_dup(const char *s)
{
- if(!s)
- return NULL;
+ if (!s)
+ return NULL;
- return strdup(s);
+ return strdup(s);
}
int mk_string_array_count(char *arr[])
{
- int i=0;
+ int i = 0;
- for(i=0; arr[i]; i++){}
- return i;
+ for (i = 0; arr[i]; i++) {
+ }
+ return i;
}
struct mk_string_line *mk_string_split_line(char *line)
{
- unsigned int i=0, len, val_len;
- int end;
- char *val;
- struct mk_string_line *sl=0, *new, *p;
+ unsigned int i = 0, len, val_len;
+ int end;
+ char *val;
+ struct mk_string_line *sl = 0, *new, *p;
+
+ if (!line) {
+ return NULL;
+ }
+
+ len = strlen(line);
- if(!line){
- return NULL;
+ while (i < len) {
+ end = mk_string_char_search(line + i, ' ', len - i);
+
+ if (end >= 0 && end + i < len) {
+ end += i;
+ val = mk_string_copy_substr(line, i, end);
+ val_len = end - i;
}
-
- len = strlen(line);
-
- while(i<len){
- end = mk_string_char_search(line+i, ' ', len-i);
-
- if(end>=0 && end+i<len){
- end+=i;
- val = mk_string_copy_substr(line, i , end);
- val_len = end - i;
- }
- else{
- val = mk_string_copy_substr(line, i, len);
- val_len = len - i;
- end = len;
-
- }
-
- /* Alloc node */
- new = mk_mem_malloc(sizeof(struct mk_string_line));
- new->val = val;
- new->len = val_len;
- new->next = NULL;
-
- /* Link node */
- if(!sl){
- sl = new;
- }
- else{
- p = sl;
- while(p->next){
- p = p->next;
- }
-
- p->next = new;
- }
- i = end+1;
+ else {
+ val = mk_string_copy_substr(line, i, len);
+ val_len = len - i;
+ end = len;
+
}
- return sl;
+ /* Alloc node */
+ new = mk_mem_malloc(sizeof(struct mk_string_line));
+ new->val = val;
+ new->len = val_len;
+ new->next = NULL;
+
+ /* Link node */
+ if (!sl) {
+ sl = new;
+ }
+ else {
+ p = sl;
+ while (p->next) {
+ p = p->next;
+ }
+
+ p->next = new;
+ }
+ i = end + 1;
+ }
+
+ return sl;
}
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
-
+
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int mk_user_init(struct client_request *cr, struct request *sr)
{
- int limit;
- int offset = mk_user_home.len;
- char *user=0, *user_server_root=0;
- struct passwd *s_user;
- unsigned long len;
-
- sr->user_home=VAR_ON;
-
- user = mk_mem_malloc(strlen(sr->uri_processed) + 1);
- limit=mk_string_search(sr->uri_processed+offset, "/");
-
- if(limit==-1)
- limit=strlen(sr->uri_processed) - offset ;
-
- strncpy(user, sr->uri_processed+offset, limit);
- user[limit]='\0';
-
- if(sr->uri.data[offset+limit]=='/')
- {
- m_build_buffer(&sr->uri.data, &sr->uri.len,
- "%s", sr->uri_processed+offset+limit);
-
- /* Extract URI portion after /~user */
- sr->user_uri = (char*)mk_mem_malloc_z(sr->uri.len + 1);
- char* src = sr->uri.data;
- char* dst = sr->user_uri;
-
- while (*src != ' ' && src < (sr->uri.data + sr->uri.len)){
- *dst++ = *src++;
- }
- }
-
- if((s_user=getpwnam(user))==NULL){
- mk_mem_free(user);
- mk_request_error(M_CLIENT_NOT_FOUND, cr, sr,1,sr->log);
- return -1;
- }
- mk_mem_free(user);
-
- m_build_buffer(&user_server_root, &len, "%s/%s",s_user->pw_dir, config->user_dir);
-
- if(sr->user_uri!=NULL)
- {
- m_build_buffer(&sr->real_path.data, &sr->real_path.len, "%s%s",
- user_server_root, sr->user_uri);
- }
- else
- {
- m_build_buffer(&sr->real_path.data, &sr->real_path.len, "%s",
- user_server_root);
- }
- mk_mem_free(user_server_root);
- return 0;
+ int limit;
+ int offset = mk_user_home.len;
+ char *user = 0, *user_server_root = 0;
+ struct passwd *s_user;
+ unsigned long len;
+
+ sr->user_home = VAR_ON;
+
+ user = mk_mem_malloc(strlen(sr->uri_processed) + 1);
+ limit = mk_string_search(sr->uri_processed + offset, "/");
+
+ if (limit == -1)
+ limit = strlen(sr->uri_processed) - offset;
+
+ strncpy(user, sr->uri_processed + offset, limit);
+ user[limit] = '\0';
+
+ if (sr->uri.data[offset + limit] == '/') {
+ m_build_buffer(&sr->uri.data, &sr->uri.len,
+ "%s", sr->uri_processed + offset + limit);
+
+ /* Extract URI portion after /~user */
+ sr->user_uri = (char *) mk_mem_malloc_z(sr->uri.len + 1);
+ char *src = sr->uri.data;
+ char *dst = sr->user_uri;
+
+ while (*src != ' ' && src < (sr->uri.data + sr->uri.len)) {
+ *dst++ = *src++;
+ }
+ }
+
+ if ((s_user = getpwnam(user)) == NULL) {
+ mk_mem_free(user);
+ mk_request_error(M_CLIENT_NOT_FOUND, cr, sr, 1, sr->log);
+ return -1;
+ }
+ mk_mem_free(user);
+
+ m_build_buffer(&user_server_root, &len, "%s/%s", s_user->pw_dir,
+ config->user_dir);
+
+ if (sr->user_uri != NULL) {
+ m_build_buffer(&sr->real_path.data, &sr->real_path.len, "%s%s",
+ user_server_root, sr->user_uri);
+ }
+ else {
+ m_build_buffer(&sr->real_path.data, &sr->real_path.len, "%s",
+ user_server_root);
+ }
+ mk_mem_free(user_server_root);
+ return 0;
}
/* Cambia el usuario del proceso */
int mk_user_set_uidgid()
{
- struct passwd *usr;
-
- EGID=(gid_t) getegid();
- EUID=(gid_t) geteuid();
-
- if(geteuid()==0 && config->user) { /* Lanzado por root ?? */
- struct rlimit rl;
-
- /* Just if i'm superuser */
- rl.rlim_max= (256 * config->maxclients);
- rl.rlim_cur = rl.rlim_max;
- setrlimit( RLIMIT_NOFILE, &rl );
-
- /* Chequear si existe el usuario USER ... */
- if ((usr = getpwnam( config->user )) == NULL) {
- printf("Error: Invalid user '%s'\n", config->user);
- exit(1);
- }
-
-
- if (initgroups(config->user, usr->pw_gid) != 0) {
- exit(1);
- }
-
- /* Cambiar el UID y el GID del proceso */
- if(setgid(usr->pw_gid)==-1) {
- printf("I can't change the GID to %u\n", usr->pw_gid);
- exit(1);
- }
-
-
- if(setuid(usr->pw_uid)==-1) {
- printf("I can't change the UID to %u\n", usr->pw_uid);
- exit(1);
- }
-
- egid = geteuid();
- euid = getegid();
- }
- return 0;
+ struct passwd *usr;
+
+ EGID = (gid_t) getegid();
+ EUID = (gid_t) geteuid();
+
+ if (geteuid() == 0 && config->user) { /* Lanzado por root ?? */
+ struct rlimit rl;
+
+ /* Just if i'm superuser */
+ rl.rlim_max = (256 * config->maxclients);
+ rl.rlim_cur = rl.rlim_max;
+ setrlimit(RLIMIT_NOFILE, &rl);
+
+ /* Chequear si existe el usuario USER ... */
+ if ((usr = getpwnam(config->user)) == NULL) {
+ printf("Error: Invalid user '%s'\n", config->user);
+ exit(1);
+ }
+
+
+ if (initgroups(config->user, usr->pw_gid) != 0) {
+ exit(1);
+ }
+
+ /* Cambiar el UID y el GID del proceso */
+ if (setgid(usr->pw_gid) == -1) {
+ printf("I can't change the GID to %u\n", usr->pw_gid);
+ exit(1);
+ }
+
+
+ if (setuid(usr->pw_uid) == -1) {
+ printf("I can't change the UID to %u\n", usr->pw_uid);
+ exit(1);
+ }
+
+ egid = geteuid();
+ euid = getegid();
+ }
+ return 0;
}
/* Vuelve el proceso a su usuario original */
int mk_user_undo_uidgid()
{
- if(EUID==0){
- setegid(EGID);
- seteuid(EUID);
- }
- return 0;
+ if (EUID == 0) {
+ setegid(EGID);
+ seteuid(EUID);
+ }
+ return 0;
}
int SendFile(int socket, struct client_request *cr, struct request *sr)
{
- long int nbytes=0;
+ long int nbytes = 0;
- nbytes = sendfile(socket, sr->fd_file, &sr->bytes_offset,
- sr->bytes_to_send);
+ nbytes = sendfile(socket, sr->fd_file, &sr->bytes_offset,
+ sr->bytes_to_send);
- if(nbytes > 0 && sr->loop == 0){
- mk_socket_set_cork_flag(socket, TCP_CORK_OFF);
- }
+ if (nbytes > 0 && sr->loop == 0) {
+ mk_socket_set_cork_flag(socket, TCP_CORK_OFF);
+ }
+
+ if (nbytes == -1) {
+ fprintf(stderr, "error from sendfile: %s\n", strerror(errno));
+ return -1;
+ }
+ else {
+ sr->bytes_to_send -= nbytes;
+ }
- if (nbytes == -1) {
- fprintf(stderr, "error from sendfile: %s\n", strerror(errno));
- return -1;
- }
- else
- {
- sr->bytes_to_send-=nbytes;
- }
-
- sr->loop++;
- return sr->bytes_to_send;
+ sr->loop++;
+ return sr->bytes_to_send;
}
/* Devuelve la fecha para enviarla
en el header */
mk_pointer PutDate_string(time_t date)
{
- int n, size=50;
- mk_pointer date_gmt;
- struct tm *gmt_tm;
-
- mk_pointer_reset(&date_gmt);
-
- if(date==0){
- if ( (date = time(NULL)) == -1 ){
- return date_gmt;
- }
- }
-
- date_gmt.data = mk_mem_malloc(size);
- gmt_tm = (struct tm *) gmtime(&date);
- n = strftime(date_gmt.data, size-1, GMT_DATEFORMAT, gmt_tm);
- date_gmt.data[n] = '\0';
- date_gmt.len = n;
-
- return date_gmt;
+ int n, size = 50;
+ mk_pointer date_gmt;
+ struct tm *gmt_tm;
+
+ mk_pointer_reset(&date_gmt);
+
+ if (date == 0) {
+ if ((date = time(NULL)) == -1) {
+ return date_gmt;
+ }
+ }
+
+ date_gmt.data = mk_mem_malloc(size);
+ gmt_tm = (struct tm *) gmtime(&date);
+ n = strftime(date_gmt.data, size - 1, GMT_DATEFORMAT, gmt_tm);
+ date_gmt.data[n] = '\0';
+ date_gmt.len = n;
+
+ return date_gmt;
}
time_t PutDate_unix(char *date)
{
- time_t new_unix_time;
- struct tm t_data;
-
- if(!strptime(date, GMT_DATEFORMAT, (struct tm *) &t_data)){
- return -1;
- }
+ time_t new_unix_time;
+ struct tm t_data;
+
+ if (!strptime(date, GMT_DATEFORMAT, (struct tm *) &t_data)) {
+ return -1;
+ }
- new_unix_time = mktime((struct tm *) &t_data);
+ new_unix_time = mktime((struct tm *) &t_data);
- return (new_unix_time);
+ return (new_unix_time);
}
-int mk_buffer_cat(mk_pointer *p, char *buf1, char *buf2){
+int mk_buffer_cat(mk_pointer * p, char *buf1, char *buf2)
+{
- int len1, len2;
+ int len1, len2;
- len1 = strlen(buf1);
- len2 = strlen(buf2);
+ len1 = strlen(buf1);
+ len2 = strlen(buf2);
- /* alloc space */
- p->data = (char *) mk_mem_malloc(len1+len2+1);
+ /* alloc space */
+ p->data = (char *) mk_mem_malloc(len1 + len2 + 1);
- /* copy data */
- memcpy(p->data, buf1, len1);
- memcpy(p->data+len1, buf2, len2);
- p->data[len1+len2]='\0';
+ /* copy data */
+ memcpy(p->data, buf1, len1);
+ memcpy(p->data + len1, buf2, len2);
+ p->data[len1 + len2] = '\0';
- /* assign len */
- p->len = len1+len2;
+ /* assign len */
+ p->len = len1 + len2;
- return 0;
+ return 0;
}
-char *m_build_buffer(char **buffer, unsigned long *len, const char *format, ...)
+char *m_build_buffer(char **buffer, unsigned long *len, const char *format,
+ ...)
{
- va_list ap;
- int length;
- char *ptr;
- static size_t _mem_alloc = 64;
- size_t alloc = 0;
-
- /* *buffer *must* be an empty/NULL buffer */
-
- *buffer = (char *) mk_mem_malloc(_mem_alloc);
- if(!*buffer)
- {
- return NULL;
- }
- alloc = _mem_alloc;
-
- va_start(ap, format);
- length = vsnprintf(*buffer, alloc, format, ap);
-
- if(length >= alloc) {
- ptr = realloc(*buffer, length + 1);
- if(!ptr) {
- va_end(ap);
- return NULL;
- }
- *buffer = ptr;
- alloc = length + 1;
- length = vsnprintf(*buffer, alloc, format, ap);
- }
- va_end(ap);
-
- if(length<0){
- return NULL;
- }
-
- ptr = *buffer;
- ptr[length] = '\0';
- *len = length;
-
- return *buffer;
+ va_list ap;
+ int length;
+ char *ptr;
+ static size_t _mem_alloc = 64;
+ size_t alloc = 0;
+
+ /* *buffer *must* be an empty/NULL buffer */
+
+ *buffer = (char *) mk_mem_malloc(_mem_alloc);
+ if (!*buffer) {
+ return NULL;
+ }
+ alloc = _mem_alloc;
+
+ va_start(ap, format);
+ length = vsnprintf(*buffer, alloc, format, ap);
+
+ if (length >= alloc) {
+ ptr = realloc(*buffer, length + 1);
+ if (!ptr) {
+ va_end(ap);
+ return NULL;
+ }
+ *buffer = ptr;
+ alloc = length + 1;
+ length = vsnprintf(*buffer, alloc, format, ap);
+ }
+ va_end(ap);
+
+ if (length < 0) {
+ return NULL;
+ }
+
+ ptr = *buffer;
+ ptr[length] = '\0';
+ *len = length;
+
+ return *buffer;
}
/* Run current process in background mode (daemon, evil Monkey >:) */
int mk_utils_set_daemon()
{
- switch (fork())
- {
- case 0 : break;
- case -1: exit(1); break; /* Error */
- default: exit(0); /* Success */
- };
-
- setsid(); /* Create new session */
- fclose(stdin); /* close screen outputs */
- fclose(stderr);
- fclose(stdout);
-
- return 0;
+ switch (fork()) {
+ case 0:
+ break;
+ case -1:
+ exit(1);
+ break; /* Error */
+ default:
+ exit(0); /* Success */
+ };
+
+ setsid(); /* Create new session */
+ fclose(stdin); /* close screen outputs */
+ fclose(stderr);
+ fclose(stdout);
+
+ return 0;
}
-char *get_real_string(mk_pointer uri){
+char *get_real_string(mk_pointer uri)
+{
- int i, hex_result, aux_char;
- int buf_idx=0;
- char *buf;
- char hex[3];
+ int i, hex_result, aux_char;
+ int buf_idx = 0;
+ char *buf;
+ char hex[3];
- if((i = mk_string_char_search(uri.data, '%', uri.len))<0)
- {
- return NULL;
- }
+ if ((i = mk_string_char_search(uri.data, '%', uri.len)) < 0) {
+ return NULL;
+ }
- buf = mk_mem_malloc_z(uri.len);
+ buf = mk_mem_malloc_z(uri.len);
- if(i>0){
- strncpy(buf, uri.data, i);
- buf_idx = i;
- }
+ if (i > 0) {
+ strncpy(buf, uri.data, i);
+ buf_idx = i;
+ }
+
+ while (i < uri.len) {
+ if (uri.data[i] == '%' && i + 2 < uri.len) {
+ memset(hex, '\0', sizeof(hex));
+ strncpy(hex, uri.data + i + 1, 2);
+ hex[2] = '\0';
- while(i<uri.len)
- {
- if(uri.data[i]=='%' && i+2<uri.len){
- memset(hex, '\0', sizeof(hex));
- strncpy(hex, uri.data+i+1,2);
- hex[2]='\0';
-
- if((hex_result=hex2int(hex))<=127){
- buf[buf_idx]=toascii(hex_result);
- }
- else {
- if((aux_char=get_char(hex_result))!=-1){
- buf[buf_idx]=aux_char;
- }
- else{
- mk_mem_free(buf);
- return NULL;
- }
- }
- i+=2;
+ if ((hex_result = hex2int(hex)) <= 127) {
+ buf[buf_idx] = toascii(hex_result);
+ }
+ else {
+ if ((aux_char = get_char(hex_result)) != -1) {
+ buf[buf_idx] = aux_char;
}
- else{
- buf[buf_idx] = uri.data[i];
+ else {
+ mk_mem_free(buf);
+ return NULL;
}
- i++;
- buf_idx++;
- }
- buf[buf_idx]='\0';
+ }
+ i += 2;
+ }
+ else {
+ buf[buf_idx] = uri.data[i];
+ }
+ i++;
+ buf_idx++;
+ }
+ buf[buf_idx] = '\0';
- return buf;
+ return buf;
}
void mk_utils_toupper(char *string)
{
- int i, len;
-
- len = strlen(string);
- for(i=0; i<len; i++)
- {
- string[i] = toupper(string[i]);
- }
+ int i, len;
+
+ len = strlen(string);
+ for (i = 0; i < len; i++) {
+ string[i] = toupper(string[i]);
+ }
}
mk_pointer mk_utils_int2mkp(int n)
{
- mk_pointer p;
- char *buf;
- unsigned long len;
- int size = 32;
+ mk_pointer p;
+ char *buf;
+ unsigned long len;
+ int size = 32;
- buf = mk_mem_malloc(size);
- len = snprintf(buf, 32, "%i", n);
+ buf = mk_mem_malloc(size);
+ len = snprintf(buf, 32, "%i", n);
- p.data = buf;
- p.len = len;
+ p.data = buf;
+ p.len = len;
- return p;
+ return p;
}
-
#include <stdlib.h>
#include <unistd.h>
-pthread_t mk_worker_spawn(void (*func)(void *))
+pthread_t mk_worker_spawn(void (*func) (void *))
{
- pthread_t tid;
- pthread_attr_t thread_attr;
+ pthread_t tid;
+ pthread_attr_t thread_attr;
- pthread_attr_init(&thread_attr);
- pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
- if(pthread_create(&tid, &thread_attr, (void *) func, NULL)<0)
- {
- perror("pthread_create");
- exit(1);
- }
+ pthread_attr_init(&thread_attr);
+ pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
+ if (pthread_create(&tid, &thread_attr, (void *) func, NULL) < 0) {
+ perror("pthread_create");
+ exit(1);
+ }
- return tid;
+ return tid;
}