adding a context which will be passed from query to query

This commit is contained in:
kakwa 2016-08-24 22:37:05 +02:00
parent b27d676148
commit c982c6b405
3 changed files with 22 additions and 4 deletions

9
inc/context.h Normal file
View File

@ -0,0 +1,9 @@
#include <stdlib.h>
#include <stdint.h>
#include <stddef.h> /* for offsetof() macro */
#include <string.h>
typedef struct {
uint64_t query_counter;
} rfc3161_context;

View File

@ -1 +1,3 @@
#include "context.h"
int http_server_start();

View File

@ -11,8 +11,10 @@
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "utils.h"
#include <sys/syslog.h>
#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;
}