introduce evhttp_accept_socket() to accept from an already created socket

svn:r666
This commit is contained in:
Niels Provos 2008-02-26 03:12:07 +00:00
parent b14cd655d1
commit 0b114da2b6
3 changed files with 36 additions and 7 deletions

View File

@ -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:

View File

@ -40,6 +40,9 @@ extern "C" {
#undef WIN32_LEAN_AND_MEAN
#endif
/* For int types. */
#include <evutil.h>
/** @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.
*

22
http.c
View File

@ -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*