summaryrefslogtreecommitdiff
path: root/http.c
diff options
context:
space:
mode:
authorLeon M. George <leon@georgemail.eu>2022-08-01 10:16:18 +0200
committerAzat Khuzhin <azat@libevent.org>2022-08-13 20:12:18 +0200
commit1bdc91350ee30d3aa51fe809fd719c2c3183b445 (patch)
tree7f0c51badd9293c101f206a874e1f437815011dc /http.c
parenta4cdc3c5e864c3ee4b836c78f92e07229be39c0f (diff)
downloadlibevent-1bdc91350ee30d3aa51fe809fd719c2c3183b445.tar.gz
http: allow setting bevcb per socket
Co-authored-by: Azat Khuzhin <azat@libevent.org> v2: remove handling of HTTP_BIND_IPV6
Diffstat (limited to 'http.c')
-rw-r--r--http.c37
1 files changed, 27 insertions, 10 deletions
diff --git a/http.c b/http.c
index d578735d..9072fcec 100644
--- a/http.c
+++ b/http.c
@@ -197,7 +197,7 @@ static void evhttp_read_header(struct evhttp_connection *evcon,
static int evhttp_add_header_internal(struct evkeyvalq *headers,
const char *key, const char *value);
static const char *evhttp_response_phrase_internal(int code);
-static void evhttp_get_request(struct evhttp *, evutil_socket_t, struct sockaddr *, ev_socklen_t);
+static void evhttp_get_request(struct evhttp *, evutil_socket_t, struct sockaddr *, ev_socklen_t, struct bufferevent *bev);
static void evhttp_write_buffer(struct evhttp_connection *,
void (*)(struct evhttp_connection *, void *), void *);
static void evhttp_make_header(struct evhttp_connection *, struct evhttp_request *);
@@ -3795,9 +3795,15 @@ evhttp_handle_request(struct evhttp_request *req, void *arg)
static void
accept_socket_cb(struct evconnlistener *listener, evutil_socket_t nfd, struct sockaddr *peer_sa, int peer_socklen, void *arg)
{
- struct evhttp *http = arg;
+ struct evhttp_bound_socket *bound = arg;
+
+ struct evhttp *http = bound->http;
- evhttp_get_request(http, nfd, peer_sa, peer_socklen);
+ struct bufferevent *bev = NULL;
+ if (bound->bevcb)
+ bev = bound->bevcb(http->base, bound->bevcbarg);
+
+ evhttp_get_request(http, nfd, peer_sa, peer_socklen, bev);
}
int
@@ -3893,9 +3899,11 @@ evhttp_bind_listener(struct evhttp *http, struct evconnlistener *listener)
return (NULL);
bound->listener = listener;
+ bound->bevcb = NULL;
+ bound->http = http;
TAILQ_INSERT_TAIL(&http->sockets, bound, next);
- evconnlistener_set_cb(listener, accept_socket_cb, http);
+ evconnlistener_set_cb(listener, accept_socket_cb, bound);
return bound;
}
@@ -3912,6 +3920,14 @@ evhttp_bound_socket_get_listener(struct evhttp_bound_socket *bound)
}
void
+evhttp_bound_set_bevcb(struct evhttp_bound_socket *bound,
+ struct bufferevent* (*cb)(struct event_base *, void *), void *cbarg)
+{
+ bound->bevcb = cb;
+ bound->bevcbarg = cbarg;
+}
+
+void
evhttp_del_accept_socket(struct evhttp *http, struct evhttp_bound_socket *bound)
{
TAILQ_REMOVE(&http->sockets, bound, next);
@@ -4517,10 +4533,10 @@ struct evbuffer *evhttp_request_get_output_buffer(struct evhttp_request *req)
static struct evhttp_connection*
evhttp_get_request_connection(
struct evhttp* http,
- evutil_socket_t fd, struct sockaddr *sa, ev_socklen_t salen)
+ evutil_socket_t fd, struct sockaddr *sa, ev_socklen_t salen,
+ struct bufferevent* bev)
{
struct evhttp_connection *evcon;
- struct bufferevent* bev = NULL;
#ifdef EVENT__HAVE_STRUCT_SOCKADDR_UN
if (sa->sa_family == AF_UNIX) {
@@ -4537,7 +4553,7 @@ evhttp_get_request_connection(
EV_SOCK_FMT"\n", __func__, EV_SOCK_ARG(fd)));
/* we need a connection object to put the http request on */
- if (http->bevcb != NULL) {
+ if (!bev && http->bevcb != NULL) {
bev = (*http->bevcb)(http->base, http->bevcbarg);
}
@@ -4560,7 +4576,7 @@ evhttp_get_request_connection(
__func__, hostname, portname, EV_SOCK_ARG(fd)));
/* we need a connection object to put the http request on */
- if (http->bevcb != NULL) {
+ if (!bev && http->bevcb != NULL) {
bev = (*http->bevcb)(http->base, http->bevcbarg);
}
evcon = evhttp_connection_base_bufferevent_new(
@@ -4636,11 +4652,12 @@ evhttp_associate_new_request_with_connection(struct evhttp_connection *evcon)
static void
evhttp_get_request(struct evhttp *http, evutil_socket_t fd,
- struct sockaddr *sa, ev_socklen_t salen)
+ struct sockaddr *sa, ev_socklen_t salen,
+ struct bufferevent *bev)
{
struct evhttp_connection *evcon;
- evcon = evhttp_get_request_connection(http, fd, sa, salen);
+ evcon = evhttp_get_request_connection(http, fd, sa, salen, bev);
if (evcon == NULL) {
event_sock_warn(fd, "%s: cannot get connection on "EV_SOCK_FMT,
__func__, EV_SOCK_ARG(fd));