count requests
This commit is contained in:
56
server.c
56
server.c
@@ -2,14 +2,67 @@
|
|||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <strings.h>
|
#include <strings.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
#define PORT 6008
|
#define PORT 6008
|
||||||
#define BACKLOG 16
|
#define BACKLOG 16
|
||||||
#define RECV_BUF 256
|
#define RECV_BUF 256
|
||||||
|
#define DATE_BUF 11
|
||||||
|
|
||||||
|
static char current_day[DATE_BUF];
|
||||||
|
static unsigned long daily_requests;
|
||||||
|
static bool stats_initialized;
|
||||||
|
static const char *stats_file = "request_counts.txt";
|
||||||
|
|
||||||
|
static void write_day_count(const char *day, unsigned long count)
|
||||||
|
{
|
||||||
|
FILE *f = fopen(stats_file, "a");
|
||||||
|
if (!f)
|
||||||
|
return;
|
||||||
|
fprintf(f, "%s %lu\n", day, count);
|
||||||
|
fclose(f);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void flush_request_stats(void)
|
||||||
|
{
|
||||||
|
if (!stats_initialized || daily_requests == 0)
|
||||||
|
return;
|
||||||
|
write_day_count(current_day, daily_requests);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void record_request(void)
|
||||||
|
{
|
||||||
|
time_t now = time(NULL);
|
||||||
|
struct tm tm_now;
|
||||||
|
if (localtime_r(&now, &tm_now) == NULL)
|
||||||
|
return;
|
||||||
|
|
||||||
|
char today[DATE_BUF];
|
||||||
|
if (strftime(today, sizeof(today), "%Y-%m-%d", &tm_now) == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (!stats_initialized)
|
||||||
|
{
|
||||||
|
strncpy(current_day, today, sizeof(current_day));
|
||||||
|
current_day[DATE_BUF - 1] = '\0';
|
||||||
|
daily_requests = 0;
|
||||||
|
stats_initialized = true;
|
||||||
|
}
|
||||||
|
if (strcmp(today, current_day) != 0)
|
||||||
|
{
|
||||||
|
flush_request_stats();
|
||||||
|
strncpy(current_day, today, sizeof(current_day));
|
||||||
|
current_day[DATE_BUF - 1] = '\0';
|
||||||
|
daily_requests = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
daily_requests++;
|
||||||
|
}
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
@@ -27,6 +80,8 @@ int main()
|
|||||||
|
|
||||||
printf("listening port %d\nbacklog %d\nrecv buffer %d\n", PORT, BACKLOG, RECV_BUF);
|
printf("listening port %d\nbacklog %d\nrecv buffer %d\n", PORT, BACKLOG, RECV_BUF);
|
||||||
|
|
||||||
|
atexit(flush_request_stats);
|
||||||
|
|
||||||
char buf[RECV_BUF];
|
char buf[RECV_BUF];
|
||||||
const char *hdr = "X-Forwarded-For:";
|
const char *hdr = "X-Forwarded-For:";
|
||||||
const size_t hdrlen = strlen(hdr);
|
const size_t hdrlen = strlen(hdr);
|
||||||
@@ -43,6 +98,7 @@ int main()
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
buf[r] = 0;
|
buf[r] = 0;
|
||||||
|
record_request();
|
||||||
// detect optional JSON response request on the first line
|
// detect optional JSON response request on the first line
|
||||||
bool wants_json = false;
|
bool wants_json = false;
|
||||||
const char *line_end = strstr(buf, "\r\n");
|
const char *line_end = strstr(buf, "\r\n");
|
||||||
|
|||||||
Reference in New Issue
Block a user