diff --git a/inc/context.h b/inc/context.h new file mode 100644 index 0000000..bb348b9 --- /dev/null +++ b/inc/context.h @@ -0,0 +1,9 @@ +#include +#include +#include /* for offsetof() macro */ +#include + + +typedef struct { + uint64_t query_counter; +} rfc3161_context; diff --git a/inc/http.h b/inc/http.h index d998d73..6939b25 100644 --- a/inc/http.h +++ b/inc/http.h @@ -1 +1,3 @@ +#include "context.h" + int http_server_start(); diff --git a/src/lib/http.c b/src/lib/http.c index b579314..39779a9 100644 --- a/src/lib/http.c +++ b/src/lib/http.c @@ -11,8 +11,10 @@ #include #include #include -#include "utils.h" #include +#include "utils.h" +#include "context.h" +#include "rfc3161.h" void log_request_debug(const struct mg_request_info *request_info, int request_id) { @@ -61,8 +63,9 @@ static int begin_request_handler(struct mg_connection *conn) { int rfc3161_handler(struct mg_connection *conn, void *context) { /* Handler may access the request info using mg_get_request_info */ const struct mg_request_info *request_info = mg_get_request_info(conn); + rfc3161_context *ct = (rfc3161_context *)context; int ret; - log_request_debug(request_info, 42); + log_request_debug(request_info, ct->query_counter); bool is_tsq = 0; @@ -95,6 +98,8 @@ int rfc3161_handler(struct mg_connection *conn, void *context) { "uts-server, a simple RFC 3161 timestamp server"); } + ct->query_counter++; + return 1; } @@ -108,11 +113,12 @@ int http_server_start() { // Prepare callbacks structure. We have only one callback, the rest are // NULL. memset(&callbacks, 0, sizeof(callbacks)); - callbacks.begin_request = begin_request_handler; + //callbacks.begin_request = begin_request_handler; // Start the web server. ctx = mg_start(&callbacks, NULL, options); - mg_set_request_handler(ctx, "/", rfc3161_handler, (void *)NULL); + rfc3161_context *ct = (rfc3161_context *)calloc(1, sizeof(rfc3161_context)); + mg_set_request_handler(ctx, "/", rfc3161_handler, (void *)ct); // Wait until user hits "enter". Server is running in separate thread. // Navigating to http://localhost:8080 will invoke begin_request_handler(). @@ -120,6 +126,7 @@ int http_server_start() { // Stop the server. mg_stop(ctx); + free(ct); return 0; }