From 0b114da2b64eac386b43933d3243100b4abf82ee Mon Sep 17 00:00:00 2001 From: Niels Provos Date: Tue, 26 Feb 2008 03:12:07 +0000 Subject: [PATCH] introduce evhttp_accept_socket() to accept from an already created socket svn:r666 --- ChangeLog | 1 + evhttp.h | 20 +++++++++++++++++++- http.c | 22 ++++++++++++++++------ 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/ChangeLog b/ChangeLog index 5fb74380..2569f08c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -48,6 +48,7 @@ Changes in current version: o simplify evbuffer by removing orig_buffer o do not insert event into list when evsel->add fails o add support for PUT/DELETE requests; from Josh Rotenberg + o introduce evhttp_accept_socket() to accept from an already created socket Changes in 1.4.0: diff --git a/evhttp.h b/evhttp.h index fb496ffb..54c504b5 100644 --- a/evhttp.h +++ b/evhttp.h @@ -40,6 +40,9 @@ extern "C" { #undef WIN32_LEAN_AND_MEAN #endif +/* For int types. */ +#include + /** @file evhttp.h * * Basic support for HTTP serving. @@ -82,10 +85,25 @@ struct evhttp *evhttp_new(struct event_base *base); * @param address a string containing the IP address to listen(2) on * @param port the port number to listen on * @return 0 on success, -1 on failure. - * @see evhttp_free() + * @see evhttp_free(), evhttp_accept_socket() */ int evhttp_bind_socket(struct evhttp *http, const char *address, u_short port); +/** + * Makes an HTTP server accept connections on the specified socket + * + * This may be useful to create a socket and then fork multiple instances + * of an http server, or when a socket has been communicated via file + * descriptor passing in situations where an http servers does not have + * permissions to bind to a low-numbered port. + * + * @param http a pointer to an evhttp object + * @param fd a socket fd that is ready for accepting connections + * @return 0 on success, -1 on failure. + * @see evhttp_free(), evhttp_bind_socket() + */ +int evhttp_accept_socket(struct evhttp *http, evutil_socket_t fd); + /** * Free the previously created HTTP server. * diff --git a/http.c b/http.c index 28a28db8..dccb373e 100644 --- a/http.c +++ b/http.c @@ -2009,8 +2009,8 @@ accept_socket(evutil_socket_t fd, short what, void *arg) int evhttp_bind_socket(struct evhttp *http, const char *address, u_short port) { - struct event *ev = &http->bind_ev; evutil_socket_t fd; + int res; if ((fd = bind_socket(address, port)) == -1) return (-1); @@ -2021,14 +2021,24 @@ evhttp_bind_socket(struct evhttp *http, const char *address, u_short port) return (-1); } + res = evhttp_accept_socket(http, fd); + + if (res != -1) + event_debug(("Bound to port %d - Awaiting connections ... ", + port)); + + return (res); +} + +int +evhttp_accept_socket(struct evhttp *http, evutil_socket_t fd) +{ + struct event *ev = &http->bind_ev; + /* Schedule the socket for accepting */ event_set(ev, fd, EV_READ | EV_PERSIST, accept_socket, http); EVHTTP_BASE_SET(http, ev); - event_add(ev, NULL); - - event_debug(("Bound to port %d - Awaiting connections ... ", port)); - - return (0); + return (event_add(ev, NULL)); } static struct evhttp*