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

Commit

Permalink
Establish conformity through flags.
Browse files Browse the repository at this point in the history
- Each context will now contain a context-specific bitmask containing
  proper configuration and ownership information.

- Side-effects: the evhtp struct has been changed to reflect this
  new ordering. This removes the following defined variables:

    disable_100_cont;
    enable_reuseport;
    enable_nodelay;
    enable_defer_accept;

  Use the following bitmask instead:

    EVHTP_FLAG_ENABLE_100_CONT (on by defualt)
    EVHTP_FLAG_ENABLE_REUSEPORT
    EVHTP_FLAG_ENABLE_NODELAY
    EVHTP_FLAG_ENABLE_DEFER_ACCEPT

- Optimally, use the evhtp_enable_flag() and or evhtp_set_flags() for
  best future uses.
  • Loading branch information
NathanFrench committed May 22, 2017
1 parent d0347dc commit 58da6dd
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 31 deletions.
54 changes: 48 additions & 6 deletions evhtp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1602,7 +1602,8 @@ htp__request_parse_headers_(htparser * p)
return -1;
}

if (c->type == evhtp_type_server && c->htp->disable_100_cont == 0)
if (c->type == evhtp_type_server &&
c->htp->flags & EVHTP_FLAG_ENABLE_100_CONT)
{
/* only send a 100 continue response if it hasn't been disabled via
* evhtp_disable_100_continue.
Expand Down Expand Up @@ -3577,7 +3578,7 @@ evhtp_accept_socket(evhtp_t * htp, evutil_socket_t sock, int backlog)

do {
#if defined SO_REUSEPORT
if (htp->enable_reuseport)
if (htp->flags & EVHTP_FLAG_ENABLE_REUSEPORT)
{
if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, (void *)&on, sizeof(on)) == -1)
{
Expand All @@ -3587,7 +3588,7 @@ evhtp_accept_socket(evhtp_t * htp, evutil_socket_t sock, int backlog)
#endif

#if defined TCP_NODELAY
if (htp->enable_nodelay == 1)
if (htp->flags & EVHTP_FLAG_ENABLE_NODELAY)
{
if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (void *)&on, sizeof(on)) == -1)
{
Expand All @@ -3597,7 +3598,7 @@ evhtp_accept_socket(evhtp_t * htp, evutil_socket_t sock, int backlog)
#endif

#if defined TCP_DEFER_ACCEPT
if (htp->enable_defer_accept == 1)
if (htp->flags & EVHTP_FLAG_ENABLE_DEFER_ACCEPT)
{
if (setsockopt(sock, IPPROTO_TCP, TCP_DEFER_ACCEPT, (void *)&on, sizeof(on)) == -1)
{
Expand Down Expand Up @@ -4688,7 +4689,7 @@ evhtp_set_max_body_size(evhtp_t * htp, uint64_t len)
void
evhtp_disable_100_continue(evhtp_t * htp)
{
htp->disable_100_cont = 1;
htp->flags &= ~EVHTP_FLAG_ENABLE_100_CONT;
}

void
Expand All @@ -4697,6 +4698,42 @@ evhtp_set_parser_flags(evhtp_t * htp, int flags)
htp->parser_flags = flags;
}

void
evhtp_set_flags(evhtp_t * htp, uint16_t flags)
{
if (htp == NULL)
{
return;
}

/* XXX: should we attempt to do all socket ops again
* if this changes mid stream?
*/
htp->flags = flags;
}

void
evhtp_enable_flag(evhtp_t * htp, uint16_t flag)
{
if (htp == NULL)
{
return;
}

htp->flags |= flag;
}

void
evhtp_disable_flag(evhtp_t * htp, uint16_t flag)
{
if (htp == NULL)
{
return;
}

htp->flags &= ~flag;
}

int
evhtp_add_alias(evhtp_t * evhtp, const char * name)
{
Expand Down Expand Up @@ -4797,15 +4834,20 @@ evhtp__new_(evhtp_t ** out, struct event_base * evbase, void * arg)

htp->arg = arg;
htp->evbase = evbase;
htp->flags = EVHTP_FLAG_DEFAULTS;
htp->bev_flags = BEV_OPT_CLOSE_ON_FREE;

/* default to lenient argument parsing */
htp->parser_flags = EVHTP_PARSE_QUERY_FLAG_LENIENT;
htp->parser_flags = EVHTP_PARSE_QUERY_FLAG_DEFAULT;


TAILQ_INIT(&htp->vhosts);
TAILQ_INIT(&htp->aliases);

/* note that we pass the htp context to the callback,
* not the user supplied arguments. That is stored
* within the context itself.
*/
evhtp_set_gencb(htp, htp__default_request_cb_, (void *)htp);

*out = htp;
Expand Down
33 changes: 19 additions & 14 deletions evhtp.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ extern "C" {

#else
#define htp_debug_strlen(x) 0
#define htp_log_debug(fmt, ...) do {} while (0)
#define htp_log_debug(fmt, ...) do { \
} while (0)
#endif


Expand Down Expand Up @@ -298,22 +299,23 @@ struct evhtp_s {
int bev_flags; /**< bufferevent flags to use on bufferevent_*_socket_new() */
uint64_t max_body_size;
uint64_t max_keepalive_requests;
uint8_t disable_100_cont : 1, /**< if set, evhtp will not respond to Expect: 100-continue */
enable_reuseport : 1,
enable_nodelay : 1,
enable_defer_accept : 1,
pad : 4;

int parser_flags; /**< default query flags to alter 'strictness' (see EVHTP_PARSE_QUERY_FLAG_*) */
#define EVHTP_FLAG_ENABLE_100_CONT (1 << 1)
#define EVHTP_FLAG_ENABLE_REUSEPORT (1 << 2)
#define EVHTP_FLAG_ENABLE_NODELAY (1 << 3)
#define EVHTP_FLAG_ENABLE_DEFER_ACCEPT (1 << 4)
#define EVHTP_FLAG_DEFAULTS EVHTP_FLAG_ENABLE_100_CONT
uint16_t flags; /**< the base flags set for this context, see: EVHTP_FLAG_* */
uint16_t parser_flags; /**< default query flags to alter 'strictness' (see EVHTP_PARSE_QUERY_FLAG_*) */

#ifndef EVHTP_DISABLE_SSL
evhtp_ssl_ctx_t * ssl_ctx; /**< if ssl enabled, this is the servers CTX */
evhtp_ssl_ctx_t * ssl_ctx; /**< if ssl enabled, this is the servers CTX */
evhtp_ssl_cfg_t * ssl_cfg;
#endif

#ifndef EVHTP_DISABLE_EVTHR
evthr_pool_t * thr_pool; /**< connection threadpool */
pthread_mutex_t * lock; /**< parent lock for add/del cbs in threads */
evthr_pool_t * thr_pool; /**< connection threadpool */
pthread_mutex_t * lock; /**< parent lock for add/del cbs in threads */

evhtp_thread_init_cb thread_init_cb;
evhtp_thread_exit_cb thread_exit_cb;
Expand Down Expand Up @@ -559,7 +561,7 @@ struct evhtp_ssl_cfg_s {
#endif

/**
* @defgroup evhtp_core Core evhtp functions
* @defgroup evhtp_core Core evhtp functions
* @{
*/

Expand All @@ -579,9 +581,9 @@ EVHTP_EXPORT evhtp_t * evhtp_new(evbase_t * evbase, void * arg);
*
* @param evhtp
*
* @return
* @return
*/
EVHTP_EXPORT void evhtp_free(evhtp_t * evhtp);
EVHTP_EXPORT void evhtp_free(evhtp_t * evhtp);

/** @} */

Expand Down Expand Up @@ -634,6 +636,7 @@ EVHTP_EXPORT int evhtp_ssl_init(evhtp_t * htp, evhtp_ssl_cfg_t * ssl_cfg);
* @param htp
*/
EVHTP_EXPORT void evhtp_disable_100_continue(evhtp_t * htp);
DEPRECATED("evhtp_disable_100 will soon be deprecated, use htp->flags instead");

/**
* @brief creates a lock around callbacks and hooks, allowing for threaded
Expand Down Expand Up @@ -885,7 +888,7 @@ EVHTP_EXPORT int evhtp_bind_sockaddr(evhtp_t * htp, struct sockaddr *,
* @return
*/
EVHTP_EXPORT int evhtp_use_threads(evhtp_t *, evhtp_thread_init_cb, int nthreads, void *)
DEPRECATED("will take on the syntax of evhtp_use_threads_wexit");
DEPRECATED("will take on the syntax of evhtp_use_threads_wexit");

/**
* @brief Temporary function which will be renamed evhtp_use_threads in the
Expand Down Expand Up @@ -1121,6 +1124,8 @@ EVHTP_EXPORT int evhtp_kvs_for_each(evhtp_kvs_t * kvs, evhtp_kvs_iterator cb, vo
| EVHTP_PARSE_QUERY_FLAG_ALLOW_NULL_VALS \
| EVHTP_PARSE_QUERY_FLAG_TREAT_SEMICOLON_AS_SEP

#define EVHTP_PARSE_QUERY_FLAG_DEFAULT EVHTP_PARSE_QUERY_FLAG_LENIENT

/**
* @brief Parses the query portion of the uri into a set of key/values
*
Expand Down
4 changes: 1 addition & 3 deletions examples/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -537,12 +537,10 @@ main(int argc, char ** argv) {
evbase = event_base_new();
htp = evhtp_new(evbase, NULL);

evhtp_enable_flag(htp, EVHTP_FLAG_ENABLE_REUSEPORT);
evhtp_set_parser_flags(htp, EVHTP_PARSE_QUERY_FLAG_LENIENT);
evhtp_set_max_keepalive_requests(htp, max_keepalives);

/* htp->enable_nodelay = 1; */
/* htp->enable_defer_accept = 1; */
htp->enable_reuseport = 1;

cb_1 = evhtp_set_cb(htp, "/ref", test_default_cb, "fjdkls");
evhtp_assert(cb_1 != NULL);
Expand Down
30 changes: 22 additions & 8 deletions examples/test_perf.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,25 @@ static int reuse_port = 0;
static size_t payload_sz = 100;

static void
response_cb(evhtp_request_t * r, void * a) {
response_cb(evhtp_request_t * r, void * a)
{
evbuffer_add_reference(r->buffer_out,
(const char *)a, payload_sz, NULL, NULL);

evhtp_send_reply(r, EVHTP_RES_OK);
}

int
main(int argc, char ** argv) {
main(int argc, char ** argv)
{
extern char * optarg;
extern int optind;
extern int opterr;
extern int optopt;
int c;

while ((c = getopt(argc, argv, "t:a:p:b:ndrs:")) != -1) {
while ((c = getopt(argc, argv, "t:a:p:b:ndrs:")) != -1)
{
switch (c) {
case 't':
num_threads = atoi(optarg);
Expand Down Expand Up @@ -87,16 +90,28 @@ main(int argc, char ** argv) {

evhtp_set_parser_flags(htp, EVHTP_PARSE_QUERY_FLAG_LENIENT);

htp->enable_nodelay = nodelay;
htp->enable_defer_accept = defer_accept;
htp->enable_reuseport = reuse_port;
if (nodelay)
{
evhtp_enable_flag(htp, EVHTP_FLAG_ENABLE_NODELAY);
}

if (defer_accept)
{
evhtp_enable_flag(htp, EVHTP_FLAG_ENABLE_DEFER_ACCEPT);
}

if (reuse_port)
{
evhtp_enable_flag(htp, EVHTP_FLAG_ENABLE_REUSEPORT);
}

memset(payload, 0x42, payload_sz);

evhtp_assert(evhtp_set_cb(htp, "/data", response_cb, payload));

#ifndef EVHTP_DISABLE_EVTHR
if (num_threads > 0) {
if (num_threads > 0)
{
evhtp_assert(evhtp_use_threads(htp, NULL, num_threads, NULL) != -1);
}
#endif
Expand All @@ -108,4 +123,3 @@ main(int argc, char ** argv) {

return 0;
} /* main */

0 comments on commit 58da6dd

Please sign in to comment.