This repository has been archived by the owner on Jun 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Additions to make life easier fetching status codes + examples
- added `evhtp_request_get_status_code(<request>)` which returns the last status code set for a <request> - added `evhtp_request_get_status_code_str(<request>)` which returns the string of the last status code set for a <request> - added a quick example of how to use request_fini hooks (resolves #75)
- Loading branch information
1 parent
676b885
commit f1e015d
Showing
5 changed files
with
104 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* Example of how to use a hook_request_fini callback. | ||
* any hook defined as `evhtp_hook_on_request_fini` will invoke | ||
* a user-defined function just prior to being free()'d. | ||
* | ||
* Here is just a quick example. | ||
*/ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
#include "../log.h" | ||
#include "./eutils.h" | ||
#include "internal.h" | ||
#include "evhtp/evhtp.h" | ||
|
||
static evhtp_res | ||
request__callback_fini_(evhtp_request_t * req, void * arg) { | ||
log_info("req=%p, statusCode=%d statusCodeString=%s for path=%s", req, | ||
evhtp_request_get_status_code(req), | ||
evhtp_request_get_status_code_str(req), | ||
req->uri->path->full); | ||
|
||
return EVHTP_RES_OK; | ||
} | ||
|
||
static void | ||
request__callback_(evhtp_request_t * req, void * arg) { | ||
evbuffer_add_printf(req->buffer_out, "Hello, world\n"); | ||
evhtp_send_reply(req, EVHTP_RES_200); | ||
} | ||
|
||
int | ||
main(int argc, char ** argv) { | ||
struct event_base * evbase; | ||
evhtp_callback_t * req_callback; | ||
evhtp_t * htp; | ||
|
||
evbase = event_base_new(); | ||
evhtp_alloc_assert(evbase); | ||
|
||
htp = evhtp_new(evbase, NULL); | ||
evhtp_alloc_assert(htp); | ||
|
||
req_callback = evhtp_set_cb(htp, "/", request__callback_, NULL); | ||
evhtp_alloc_assert(req_callback); | ||
|
||
/* Here we are going to make a on_request_fini for a specific | ||
* callback, in this case "/" (which will match /anything/really). | ||
*/ | ||
evhtp_callback_set_hook(req_callback, | ||
evhtp_hook_on_request_fini, | ||
request__callback_fini_, NULL); | ||
|
||
srand(time(NULL)); | ||
|
||
#define GENCHAR() ((char)('a' + rand() % 26)) | ||
|
||
log_info("Simple usage of using request_fini hooks, run: " | ||
"curl http://127.0.0.1:%d/%c/%c/%c", | ||
bind__sock_port0_(htp), | ||
GENCHAR(), GENCHAR(), GENCHAR()); | ||
|
||
|
||
return event_base_loop(evbase, 0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters