From cdf52911bf6408bf3b097b7116a7b103239dff17 Mon Sep 17 00:00:00 2001 From: Mark Ellzey Date: Sat, 11 Apr 2015 22:01:58 -0400 Subject: [PATCH] Added evhtp_get_cb (see full commit msg) - evhtp_get_cb attempts to find the callback matching the exact string 'needle'. This is useful in cases where we want to get the original handle, but is not in scope. - when this is called against patterns, you must use the pattern, not what would match that pattern. Meaning: Let's say you define a wildcard callback: evhtp_set_glob_cb(htp, "/foo/bar*", ....); calling 'evhtp_get_cb(htp, "/foo/bar/baz");' results in a NULL return, since we're not executing the match, we're looking for the original pattern. the correct usage is 'evhtp_get_cb(htp, "/foo/bar*");' - also added help for test_perf.c --- evhtp.c | 13 +++++++++++++ evhtp.h | 28 ++++++++++++++++++++++++++++ examples/test_perf.c | 17 +++++++++++++---- 3 files changed, 54 insertions(+), 4 deletions(-) diff --git a/evhtp.c b/evhtp.c index dc45469..045e118 100644 --- a/evhtp.c +++ b/evhtp.c @@ -3574,6 +3574,19 @@ evhtp_set_cb(evhtp_t * htp, const char * path, evhtp_callback_cb cb, void * arg) return hcb; } +evhtp_callback_t * +evhtp_get_cb(evhtp_t * htp, const char * path) { + evhtp_callback_t * callback; + + TAILQ_FOREACH(callback, htp->callbacks, next) { + if (strcmp(callback->val.path, path) == 0) { + return callback; + } + } + + return NULL; +} + #ifndef EVHTP_DISABLE_EVTHR static void _evhtp_thread_init(evthr_t * thr, void * arg) { diff --git a/evhtp.h b/evhtp.h index a78d749..81278a5 100644 --- a/evhtp.h +++ b/evhtp.h @@ -700,6 +700,34 @@ EVHTP_EXPORT evhtp_callback_t * evhtp_set_regex_cb(evhtp_t * htp, const char * p EVHTP_EXPORT evhtp_callback_t * evhtp_set_glob_cb(evhtp_t * htp, const char * pattern, evhtp_callback_cb cb, void * arg); + +/** + * @brief attempts to find the callback matching the exact string 'needle'. This is useful + * in cases where we want to get the original handle, but is not in scope. + * + * with pattern based callbacks, this does not attempt to find a callback that would + * match the string if the pattern matcher was executed. + * + * Meaning: + * evhtp_set_glob_cb(htp, "/foo/bar*", ....); + * + * Calling + * evhtp_get_cb(htp, "/foo/bar/baz"); + * + * Will return NULL since it's not the exact pattern set + * + * Calling + * evhtp_get_cb(htp, "/foo/bar*"); + * + * Is the correct usage. + * + * @param htp + * @param needle + * + * @return NULL if callback is not not found + */ +EVHTP_EXPORT evhtp_callback_t * evhtp_get_cb(evhtp_t * htp, const char * needle); + /** * @brief sets a callback hook for either a connection or a path/regex . * diff --git a/examples/test_perf.c b/examples/test_perf.c index a03c91d..707de17 100644 --- a/examples/test_perf.c +++ b/examples/test_perf.c @@ -22,7 +22,7 @@ static size_t payload_sz = 100; static void response_cb(evhtp_request_t * r, void * a) { evbuffer_add_reference(r->buffer_out, - (const char *)a, payload_sz, NULL, NULL); + (const char *)a, payload_sz, NULL, NULL); evhtp_send_reply(r, EVHTP_RES_OK); } @@ -46,8 +46,8 @@ main(int argc, char ** argv) { case 'p': bport = atoi(optarg); break; - case 'b': - backlog = atoll(optarg); + case 'b': + backlog = atoll(optarg); case 'n': nodelay = 1; break; @@ -61,6 +61,15 @@ main(int argc, char ** argv) { payload_sz = atoll(optarg); break; default: + fprintf(stdout, "Usage: %s [flags]\n", argv[0]); + fprintf(stdout, " -t : number of worker threads [Default: %d]\n", num_threads); + fprintf(stdout, " -a : bind address [Default: %s]\n", baddr); + fprintf(stdout, " -p : bind port [Default: %d]\n", bport); + fprintf(stdout, " -b : listen backlog [Default: %d]\n", backlog); + fprintf(stdout, " -s : size of the response [Default: %zu]\n", payload_sz); + fprintf(stdout, " -n : disable nagle (nodelay) [Default: %s]\n", nodelay ? "true" : "false"); + fprintf(stdout, " -d : enable deferred accept [Default: %s]\n", defer_accept ? "true" : "false"); + fprintf(stdout, " -r : enable linux reuseport [Default: %s]\n", reuse_port ? "true" : "false"); exit(EXIT_FAILURE); } /* switch */ } @@ -82,7 +91,7 @@ main(int argc, char ** argv) { htp->enable_defer_accept = defer_accept; htp->enable_reuseport = reuse_port; - memset(payload, 0x42, payload_sz); + memset(payload, 0x42, payload_sz); evhtp_assert(evhtp_set_cb(htp, "/data", response_cb, payload));