Skip to content
This repository has been archived by the owner on Jun 30, 2021. It is now read-only.

Commit

Permalink
[#129] Added -DEVHTP_SORT_CALLBACKS option for sorting callbacks
Browse files Browse the repository at this point in the history
Added a compile-time flag -DEVHTP_SORT_CALLBACKS which will auto-sort
globs and non-regex callbacks to search from longest to shortest.

This is optional since we do not want to potentially mess up any code
currently running in production that does this sort of thing manually
(ie ordering code).
  • Loading branch information
NathanFrench committed Mar 13, 2019
1 parent 00ccb5c commit cd92003
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ if(EVHTP_THR_SHARED_PIPE)
target_compile_definitions(evhtp PUBLIC EVTHR_SHARED_PIPE)
endif()

if (EVHTP_SORT_CALLBACKS)
target_compile_definitions(evhtp PUBLIC EVHTP_SORT_CALLBACKS)
endif()

if(has_c99)
target_compile_definitions(evhtp PUBLIC EVHTP_HAS_C99)
endif()
Expand Down
3 changes: 3 additions & 0 deletions cmake/options.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ option (EVHTP_DISABLE_REGEX "Disable regex support" OFF)
# -DEVHTP_DEBUG=ON
option (EVHTP_DEBUG "Enable verbose debug logging" OFF)

# -DEVHTP_SORT_CALLBACKS=ON
option (EVHTP_SORT_CALLBACKS "Sort callback strings by length" OFF)

# can be overwritten by new set_alloc functions
set(EVHTP_ALLOCATOR CACHE STRING "Allocator library")
set_property(CACHE EVHTP_ALLOCATOR PROPERTY STRINGS "jemalloc;tcmalloc")
Expand Down
50 changes: 49 additions & 1 deletion evhtp.c
Original file line number Diff line number Diff line change
Expand Up @@ -4290,10 +4290,58 @@ evhtp_callback_free(evhtp_callback_t * callback)
int
evhtp_callbacks_add_callback(evhtp_callbacks_t * cbs, evhtp_callback_t * cb)
{
#ifdef EVHTP_SORT_CALLBACKS
evhtp_callback_t * callback;
size_t cblen;

if (TAILQ_FIRST(cbs) == NULL) {
TAILQ_INSERT_TAIL(cbs, cb, next);

return 0;
}

switch (cb->type) {
case evhtp_callback_type_hash:
cblen = cb->len;
break;
case evhtp_callback_type_glob:
cblen = strlen(cb->val.glob);
break;
case evhtp_callback_type_regex:
cblen = 0;
break;
default:
cblen = 0;
break;
}

TAILQ_FOREACH(callback, cbs, next) {
size_t len = 0;

switch (callback->type) {
case evhtp_callback_type_hash:
len = callback->len;
break;
case evhtp_callback_type_regex:
len = 0;
break;
case evhtp_callback_type_glob:
len = strlen(callback->val.glob);
break;
}

if (cblen >= len) {
TAILQ_INSERT_BEFORE(callback, cb, next);

return 0;
}
}
#endif

TAILQ_INSERT_TAIL(cbs, cb, next);

return 0;
}
} /* evhtp_callbacks_add_callback */

static int
htp__set_hook_(evhtp_hooks_t ** hooks, evhtp_hook_type type, evhtp_hook cb, void * arg)
Expand Down

0 comments on commit cd92003

Please sign in to comment.