diff --git a/http.c b/http.c index a6390f56..c3cb4189 100644 --- a/http.c +++ b/http.c @@ -2557,12 +2557,17 @@ evhttp_set_timeout(struct evhttp* http, int timeout_in_secs) http->timeout = timeout_in_secs; } -void +int evhttp_set_cb(struct evhttp *http, const char *uri, void (*cb)(struct evhttp_request *, void *), void *cbarg) { struct evhttp_cb *http_cb; + TAILQ_FOREACH(http_cb, &http->callbacks, next) { + if (strcmp(http_cb->what, uri) == 0) + return (-1); + } + if ((http_cb = mm_calloc(1, sizeof(struct evhttp_cb))) == NULL) event_err(1, "%s: calloc", __func__); @@ -2571,6 +2576,8 @@ evhttp_set_cb(struct evhttp *http, const char *uri, http_cb->cbarg = cbarg; TAILQ_INSERT_TAIL(&http->callbacks, http_cb, next); + + return (0); } int diff --git a/include/event2/http.h b/include/event2/http.h index dc7d7009..12ecddbf 100644 --- a/include/event2/http.h +++ b/include/event2/http.h @@ -119,9 +119,17 @@ int evhttp_accept_socket(struct evhttp *http, evutil_socket_t fd); */ void evhttp_free(struct evhttp* http); -/** Set a callback for a specified URI */ -void evhttp_set_cb(struct evhttp *, const char *, - void (*)(struct evhttp_request *, void *), void *); +/** + Set a callback for a specified URI + + @param http the http sever on which to set the callback + @param path the path for which to invoke the callback + @param cb the callback function that gets invoked on requesting path + @param cb_arg an additional context argument for the callback + @return 0 on success, -1 if the callback existed already +*/ +int evhttp_set_cb(struct evhttp *http, const char *path, + void (*cb)(struct evhttp_request *, void *), void *cb_arg); /** Removes the callback for a specified URI */ int evhttp_del_cb(struct evhttp *, const char *); diff --git a/test/regress_http.c b/test/regress_http.c index 7cbba72a..21b465fa 100644 --- a/test/regress_http.c +++ b/test/regress_http.c @@ -2076,6 +2076,7 @@ static void http_primitives(void *ptr) { char *escaped = NULL; + struct evhttp *http; escaped = evhttp_htmlescape("