Skip to content
This repository has been archived by the owner on Aug 31, 2021. It is now read-only.

Libevent2 #35

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion simplehttp/async_simplehttp.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <inttypes.h>
#include <time.h>
#include "async_simplehttp.h"
#include <event2/http_struct.h>

// this is set as a parameter to init_async_connection_pool()
static int request_logging = 0;
Expand Down Expand Up @@ -65,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);
Expand Down
2 changes: 2 additions & 0 deletions simplehttp/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <string.h>
#include <inttypes.h>
#include "simplehttp.h"
#include <event2/http_struct.h>
#include <event2/buffer_compat.h>

const char *simplehttp_method(struct evhttp_request *req)
{
Expand Down
34 changes: 22 additions & 12 deletions simplehttp/simplehttp.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@
#include <string.h>
#include <stdio.h>
#include <fnmatch.h>

#include "queue.h"
#include "simplehttp.h"
#include "stat.h"
#include "request.h"
#include "options.h"
#include <event2/http_struct.h>
#include <event2/http_compat.h>
#include <event2/event_struct.h>

typedef struct cb_entry {
char *path;
Expand All @@ -30,8 +34,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 *simplehttp_event_base = NULL;

int help_cb(int *value);

Expand All @@ -41,7 +45,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)
Expand Down Expand Up @@ -149,8 +153,8 @@ void generic_request_handler(struct evhttp_request *req, void *arg)

void simplehttp_init()
{
if (!current_base) {
event_init();
if (!simplehttp_event_base) {
simplehttp_event_base = event_base_new();
}
TAILQ_INIT(&callbacks);
TAILQ_INIT(&simplehttp_reqs);
Expand All @@ -165,7 +169,9 @@ void simplehttp_free()
free(entry->path);
free(entry);
}
event_free(pipe_ev);
evhttp_free(httpd);
event_base_free(simplehttp_event_base);
simplehttp_stats_destruct();
}

Expand Down Expand Up @@ -249,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));
}
}

Expand All @@ -278,13 +284,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(simplehttp_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(simplehttp_event_base);
if (evhttp_bind_socket(httpd, address, port) == -1) {
printf("could not bind to %s:%d\n", address, port);
return 0;
}
Expand All @@ -295,9 +301,13 @@ int simplehttp_listen()
return 1;
}

void simplehttp_loopbreak()
{
event_base_loopbreak(simplehttp_event_base);
}
void simplehttp_run()
{
event_dispatch();
event_base_dispatch(simplehttp_event_base);
}

int simplehttp_main()
Expand Down
8 changes: 6 additions & 2 deletions simplehttp/simplehttp.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
#define _SIMPLEHTTP_H

#include "queue.h"
#include <event2/event.h>
#include <event2/buffer.h>
#include <event2/http.h>
#include "options.h"
#include <event.h>
#include <evhttp.h>

#define SIMPLEHTTP_VERSION "0.1.2"
#ifndef DUPE_N_TERMINATE
Expand Down Expand Up @@ -40,9 +41,12 @@ struct simplehttp_stats {
int callback_count;
};

extern struct event_base *simplehttp_event_base;

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);
Expand Down