Esempio n. 1
0
int main() {
    const char* host;
    uint16_t port, num_threads;
    bool skip_validation;
    parse_env(&host, &port, &num_threads, &skip_validation);

    evbase_t* evbase = event_base_new();
    evhtp_t* htp = evhtp_new(evbase, NULL);
    evhtp_set_cb(htp, "/add", add_cb, NULL);
    evhtp_use_threads_wexit(htp, init_thread, exit_thread, num_threads, NULL);
    evhtp_bind_socket(htp, host, port, 1024);

    printf("READY\n");

    event_base_loop(evbase, 0);
}
Esempio n. 2
0
int
main(int argc, char ** argv) {
    struct event     * ev_sigint;
    evbase_t         * evbase = NULL;
    evhtp_t          * htp    = NULL;
    evhtp_callback_t * cb_1   = NULL;
    evhtp_callback_t * cb_2   = NULL;
    evhtp_callback_t * cb_3   = NULL;
    evhtp_callback_t * cb_4   = NULL;
    evhtp_callback_t * cb_5   = NULL;

#ifndef EVHTP_DISABLE_REGEX
    evhtp_callback_t * cb_6   = NULL;
#endif
    evhtp_callback_t * cb_7   = NULL;
#ifndef EVHTP_DISABLE_REGEX
    evhtp_callback_t * cb_8   = NULL;
#endif
    evhtp_callback_t * cb_9   = NULL;
    evhtp_callback_t * cb_10  = NULL;
    evhtp_callback_t * cb_11  = NULL;
    evhtp_callback_t * cb_12  = NULL;

    if (parse_args(argc, argv) < 0) {
        exit(1);
    }

    srand((unsigned)time(NULL));

    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);


    cb_1  = evhtp_set_cb(htp, "/ref", test_default_cb, "fjdkls");
    evhtp_assert(cb_1 != NULL);

    cb_2  = evhtp_set_cb(htp, "/foo", test_foo_cb, "bar");
    evhtp_assert(cb_2 != NULL);

    cb_3  = evhtp_set_cb(htp, "/foo/", test_foo_cb, "bar");
    evhtp_assert(cb_3 != NULL);

    cb_4  = evhtp_set_cb(htp, "/bar", test_bar_cb, "baz");
    evhtp_assert(cb_4 != NULL);

    cb_5  = evhtp_set_cb(htp, "/500", test_500_cb, "500");
    evhtp_assert(cb_5 != NULL);

#ifndef EVHTP_DISABLE_REGEX
    cb_6  = evhtp_set_regex_cb(htp, "^(/anything/).*", test_regex, NULL);
    evhtp_assert(cb_6 != NULL);
#endif
    cb_7  = evhtp_set_cb(htp, "/pause", test_pause_cb, NULL);
    evhtp_assert(cb_7 != NULL);
#ifndef EVHTP_DISABLE_REGEX
    cb_8  = evhtp_set_regex_cb(htp, "^/create/(.*)", create_callback, NULL);
    evhtp_assert(cb_8 != NULL);
#endif
    cb_9  = evhtp_set_glob_cb(htp, "*/glob/*", test_glob_cb, NULL);
    evhtp_assert(cb_9 != NULL);

    cb_10 = evhtp_set_cb(htp, "/max_body_size", test_max_body, NULL);
    evhtp_assert(cb_10 != NULL);

    /* set a callback to test out chunking API */
    cb_11 = evhtp_set_cb(htp, "/chunkme", test_chunking, NULL);
    evhtp_assert(cb_11 != NULL);

    /* set a callback which takes ownership of the underlying bufferevent and
     * just starts echoing things
     */
    cb_12 = evhtp_set_cb(htp, "/ownme", test_ownership, NULL);
    evhtp_assert(cb_12 != NULL);

    /* set a callback to pause on each header for cb_7 */
    evhtp_callback_set_hook(cb_7, evhtp_hook_on_path, pause_init_cb, NULL);

    /* set a callback to set hooks specifically for the cb_6 callback */
#ifndef EVHTP_DISABLE_REGEX
    evhtp_callback_set_hook(cb_6, evhtp_hook_on_headers, test_regex_hdrs_cb, NULL);
#endif

    evhtp_callback_set_hook(cb_10, evhtp_hook_on_headers, set_max_body, NULL);

    /* set a default request handler */
    evhtp_set_gencb(htp, test_default_cb, "foobarbaz");

    /* set a callback invoked before a connection is accepted */
    evhtp_set_pre_accept_cb(htp, test_pre_accept, &bind_port);

    /* set a callback to set per-connection hooks (via a post_accept cb) */
    evhtp_set_post_accept_cb(htp, set_my_connection_handlers, NULL);

#ifndef EVHTP_DISABLE_SSL
    if (ssl_pem != NULL) {
        evhtp_ssl_cfg_t scfg = {
            .pemfile            = ssl_pem,
            .privfile           = ssl_pem,
            .cafile             = ssl_ca,
            .capath             = ssl_capath,
            .ciphers            = "RC4+RSA:HIGH:+MEDIUM:+LOW",
            .ssl_opts           = SSL_OP_NO_SSLv2,
            .ssl_ctx_timeout    = 60 * 60 * 48,
            .verify_peer        = SSL_VERIFY_PEER,
            .verify_depth       = 42,
            .x509_verify_cb     = dummy_ssl_verify_callback,
            .x509_chk_issued_cb = dummy_check_issued_cb,
            .scache_type        = evhtp_ssl_scache_type_internal,
            .scache_size        = 1024,
            .scache_timeout     = 1024,
            .scache_init        = NULL,
            .scache_add         = NULL,
            .scache_get         = NULL,
            .scache_del         = NULL,
        };

        evhtp_ssl_init(htp, &scfg);
#ifndef EVHTP_DISABLE_EVTHR
        if (use_threads) {
            #define OPENSSL_THREAD_DEFINES
#include <openssl/opensslconf.h>
#if defined(OPENSSL_THREADS)
#else
            fprintf(stderr, "Your version of OpenSSL does not support threading!\n");
            exit(-1);
#endif
        }
#endif
    }
#endif

#ifndef EVHTP_DISABLE_EVTHR
    if (use_threads) {
        evhtp_use_threads_wexit(htp, NULL, NULL, num_threads, NULL);
    }
#endif

    if (evhtp_bind_socket(htp, bind_addr, bind_port, backlog) < 0) {
        fprintf(stderr, "Could not bind socket: %s\n", strerror(errno));
        exit(-1);
    }

    ev_sigint = evsignal_new(evbase, SIGINT, sigint, evbase);
    evsignal_add(ev_sigint, NULL);

    event_base_loop(evbase, 0);

    evhtp_safe_free(ev_sigint, event_free);
    evhtp_unbind_socket(htp);

    evhtp_safe_free(htp, evhtp_free);
    evhtp_safe_free(evbase, event_base_free);

    return 0;
} /* main */