-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.c
65 lines (57 loc) · 1.8 KB
/
filter.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <stdio.h>
#include <string.h>
#include "log.h"
#include "http.h"
#include "actions.h"
#include "filter.h"
void filter_request(int client_socket, HTTP_REQUEST req, HTTP_RESPONSE *res)
{
DEBUG_LOG("------------ Filtering -------------\n");
for (int i = 0; i < NUM_FILTERS; i++)
{
// Correct method?
if (filters[i].method != req.method)
continue;
// Correct path?
if (filters[i].path != NULL && req.path != NULL &&
strcmp(filters[i].path, req.path) != 0)
continue;
// Correct host?
// if (filters[i].host != NULL ||
// strcmp(filters[i].host, req.host) != 0)
// {
// return;
// }
// Match headers.
int match = filters[i].num_headers;
// Iterate over required filter headers.
for (int j = 0; j < filters[i].num_headers; j++)
{
// Iterate over the request itself.
for (int k = 0; k < req.header_number; k++)
{
// Match the key.
if (strcmp(filters[i].headers[j].key, req.headers[k].key) == 0)
{
// Match the value.
if (strcmp(filters[i].headers[j].value,
req.headers[k].value) == 0)
match--;
break;
}
}
}
if (match == 0)
{
// Execute the action and exit the function.
if (filters[i].action(client_socket, req)) {
return;
}
}
}
// No match, we return a generic 200.
DEBUG_LOG("No matches, proceeding with standard 404\n");
not_found_action(client_socket, req);
DEBUG_LOG("------------------------------------!\n");
return;
}