From a09eb4976decfcd2a9a66b9b460d018c2227afe5 Mon Sep 17 00:00:00 2001 From: Jehiah Czebotar Date: Tue, 13 Mar 2012 21:52:08 -0400 Subject: [PATCH 1/2] simplehttp changes for libevent2 --- simplehttp/async_simplehttp.c | 1 + simplehttp/log.c | 1 + simplehttp/simplehttp.c | 28 ++++++++++++++++++---------- simplehttp/simplehttp.h | 8 ++++++-- 4 files changed, 26 insertions(+), 12 deletions(-) diff --git a/simplehttp/async_simplehttp.c b/simplehttp/async_simplehttp.c index 8c0591d..ccb6d68 100644 --- a/simplehttp/async_simplehttp.c +++ b/simplehttp/async_simplehttp.c @@ -4,6 +4,7 @@ #include #include #include "async_simplehttp.h" +#include // this is set as a parameter to init_async_connection_pool() static int request_logging = 0; diff --git a/simplehttp/log.c b/simplehttp/log.c index 063a30a..5539569 100644 --- a/simplehttp/log.c +++ b/simplehttp/log.c @@ -4,6 +4,7 @@ #include #include #include "simplehttp.h" +#include const char *simplehttp_method(struct evhttp_request *req) { diff --git a/simplehttp/simplehttp.c b/simplehttp/simplehttp.c index 9000888..0be7dbc 100644 --- a/simplehttp/simplehttp.c +++ b/simplehttp/simplehttp.c @@ -12,11 +12,13 @@ #include #include #include + #include "queue.h" #include "simplehttp.h" #include "stat.h" #include "request.h" #include "options.h" +#include typedef struct cb_entry { char *path; @@ -30,8 +32,8 @@ int simplehttp_logging = 0; int callback_count = 0; uint64_t request_count = 0; struct evhttp *httpd; -struct event pipe_ev; -extern struct event_base *current_base; +struct event *pipe_ev; +struct event_base *current_event_base = NULL; int help_cb(int *value); @@ -41,7 +43,7 @@ static void ignore_cb(int sig, short what, void *arg) void termination_handler(int signum) { - event_loopbreak(); + simplehttp_loopbreak(); } int get_uid(char *user) @@ -149,8 +151,8 @@ void generic_request_handler(struct evhttp_request *req, void *arg) void simplehttp_init() { - if (!current_base) { - event_init(); + if (!current_event_base) { + current_event_base = event_base_new(); } TAILQ_INIT(&callbacks); TAILQ_INIT(&simplehttp_reqs); @@ -165,7 +167,9 @@ void simplehttp_free() free(entry->path); free(entry); } + event_free(pipe_ev); evhttp_free(httpd); + event_base_free(current_event_base); simplehttp_stats_destruct(); } @@ -278,13 +282,13 @@ int simplehttp_listen() signal(SIGQUIT, termination_handler); signal(SIGTERM, termination_handler); - signal_set(&pipe_ev, SIGPIPE, ignore_cb, NULL); - signal_add(&pipe_ev, NULL); + pipe_ev = event_new(current_event_base, SIGPIPE, EV_SIGNAL|EV_PERSIST, ignore_cb, NULL); + event_add(pipe_ev, NULL); simplehttp_stats_init(); - httpd = evhttp_start(address, port); - if (!httpd) { + httpd = evhttp_new(current_event_base); + if (evhttp_bind_socket(httpd, address, port) == -1) { printf("could not bind to %s:%d\n", address, port); return 0; } @@ -295,9 +299,13 @@ int simplehttp_listen() return 1; } +void simplehttp_loopbreak() +{ + event_base_loopbreak(current_event_base); +} void simplehttp_run() { - event_dispatch(); + event_base_dispatch(current_event_base); } int simplehttp_main() diff --git a/simplehttp/simplehttp.h b/simplehttp/simplehttp.h index b706c48..fffa038 100644 --- a/simplehttp/simplehttp.h +++ b/simplehttp/simplehttp.h @@ -2,9 +2,12 @@ #define _SIMPLEHTTP_H #include "queue.h" +#include +#include +#include +#include +#include #include "options.h" -#include -#include #define SIMPLEHTTP_VERSION "0.1.2" #ifndef DUPE_N_TERMINATE @@ -43,6 +46,7 @@ struct simplehttp_stats { void simplehttp_init(); int simplehttp_main(); int simplehttp_listen(); +void simplehttp_loopbreak(); void simplehttp_run(); void simplehttp_free(); void simplehttp_set_cb(char *path, void (*cb)(struct evhttp_request *, struct evbuffer *, void *), void *ctx); From 6dc7498ffb3669df6cddf4bd832f2e52fdaeca70 Mon Sep 17 00:00:00 2001 From: Jehiah Czebotar Date: Tue, 13 Mar 2012 22:05:16 -0400 Subject: [PATCH 2/2] more changes for libevent2; expose simplehttp_current_base --- simplehttp/async_simplehttp.c | 2 +- simplehttp/log.c | 1 + simplehttp/simplehttp.c | 22 ++++++++++++---------- simplehttp/simplehttp.h | 4 ++-- 4 files changed, 16 insertions(+), 13 deletions(-) diff --git a/simplehttp/async_simplehttp.c b/simplehttp/async_simplehttp.c index ccb6d68..0ba33b1 100644 --- a/simplehttp/async_simplehttp.c +++ b/simplehttp/async_simplehttp.c @@ -66,7 +66,7 @@ struct evhttp_connection *get_connection(char *address, int port, struct Connect conn->port = port; conn->next_evcon = 0; for (i = 0; i < ASYNC_PER_HOST_CONNECTION_LIMIT; i++) { - conn->evcon[i] = evhttp_connection_new(address, port); + conn->evcon[i] = evhttp_connection_base_new(simplehttp_event_base, NULL, address, port); evhttp_connection_set_retries(conn->evcon[i], 0); } TAILQ_INSERT_TAIL(&connection_pool, conn, next); diff --git a/simplehttp/log.c b/simplehttp/log.c index 5539569..6c72f96 100644 --- a/simplehttp/log.c +++ b/simplehttp/log.c @@ -5,6 +5,7 @@ #include #include "simplehttp.h" #include +#include const char *simplehttp_method(struct evhttp_request *req) { diff --git a/simplehttp/simplehttp.c b/simplehttp/simplehttp.c index 0be7dbc..4398dcb 100644 --- a/simplehttp/simplehttp.c +++ b/simplehttp/simplehttp.c @@ -19,6 +19,8 @@ #include "request.h" #include "options.h" #include +#include +#include typedef struct cb_entry { char *path; @@ -33,7 +35,7 @@ int callback_count = 0; uint64_t request_count = 0; struct evhttp *httpd; struct event *pipe_ev; -struct event_base *current_event_base = NULL; +struct event_base *simplehttp_event_base = NULL; int help_cb(int *value); @@ -151,8 +153,8 @@ void generic_request_handler(struct evhttp_request *req, void *arg) void simplehttp_init() { - if (!current_event_base) { - current_event_base = event_base_new(); + if (!simplehttp_event_base) { + simplehttp_event_base = event_base_new(); } TAILQ_INIT(&callbacks); TAILQ_INIT(&simplehttp_reqs); @@ -169,7 +171,7 @@ void simplehttp_free() } event_free(pipe_ev); evhttp_free(httpd); - event_base_free(current_event_base); + event_base_free(simplehttp_event_base); simplehttp_stats_destruct(); } @@ -253,10 +255,10 @@ int simplehttp_listen() if (root != NULL) { if (chroot(root) != 0) { - err(1, strerror(errno)); + err(1, "%s", strerror(errno)); } if (chdir("/") != 0) { - err(1, strerror(errno)); + err(1, "%s", strerror(errno)); } } @@ -282,12 +284,12 @@ int simplehttp_listen() signal(SIGQUIT, termination_handler); signal(SIGTERM, termination_handler); - pipe_ev = event_new(current_event_base, SIGPIPE, EV_SIGNAL|EV_PERSIST, ignore_cb, NULL); + pipe_ev = event_new(simplehttp_event_base, SIGPIPE, EV_SIGNAL|EV_PERSIST, ignore_cb, NULL); event_add(pipe_ev, NULL); simplehttp_stats_init(); - httpd = evhttp_new(current_event_base); + httpd = evhttp_new(simplehttp_event_base); if (evhttp_bind_socket(httpd, address, port) == -1) { printf("could not bind to %s:%d\n", address, port); return 0; @@ -301,11 +303,11 @@ int simplehttp_listen() void simplehttp_loopbreak() { - event_base_loopbreak(current_event_base); + event_base_loopbreak(simplehttp_event_base); } void simplehttp_run() { - event_base_dispatch(current_event_base); + event_base_dispatch(simplehttp_event_base); } int simplehttp_main() diff --git a/simplehttp/simplehttp.h b/simplehttp/simplehttp.h index fffa038..607f829 100644 --- a/simplehttp/simplehttp.h +++ b/simplehttp/simplehttp.h @@ -5,8 +5,6 @@ #include #include #include -#include -#include #include "options.h" #define SIMPLEHTTP_VERSION "0.1.2" @@ -43,6 +41,8 @@ struct simplehttp_stats { int callback_count; }; +extern struct event_base *simplehttp_event_base; + void simplehttp_init(); int simplehttp_main(); int simplehttp_listen();