forked from danielwaterworth/Raphters
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdispatcher.c
142 lines (124 loc) · 3.45 KB
/
dispatcher.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
Copyright (C) 2011 Raphters authors,
This file is part of Raphters.
Raphters is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Raphters is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include "dispatcher.h"
#include "request.h"
#include "response.h"
#include "error.h"
#include "string.h"
#include "fcgi_stdio.h"
#include "log.h"
handler *head = NULL;
handler *last = NULL;
void default_error_handler(const char *msg) {
printf("content-type: text/html\n\nError: %s\n", msg);
}
void (*error_handler)(const char *) = default_error_handler;
int parse_method(const char *verb) {
typedef struct { int method; const char *text; } table_ent;
#define _(verb) { verb, #verb }
static table_ent verbs[] = {
_(GET),
_(POST),
_(PUT),
_(HEAD),
_(DELETE)
};
#undef _
size_t i;
for (i = 0; i < sizeof(verbs) / sizeof(table_ent); ++i)
{
table_ent *ent = &verbs[i];
if (strcmp(verb, ent->text) == 0) {
return ent->method;
}
}
error_handler("Unrecognized HTTP request verb");
return -1;
}
void dispatch() {
handler *cur;
char *path_info = get_path_info();
if (path_info == NULL) {
error_handler("NULL path_info");
return;
}
char *agt = get_user_agent();
char *method_str = get_method();
if (method_str == NULL) {
error_handler("NULL method_str");
return;
}
// write to the stat log every 1000 hits on average.
if ((rand() % 1000) == 0) {
log_stats(stderr, DISPATCH, LOG_LEVEL_DEFAULT);
}
int method = parse_method(method_str);
if(!strcmp(agt,response_token)){
void *header = check_header(method);
void (*error)();
error = header;
error();
}
for (cur = head; cur != NULL; cur = cur->next) {
if (cur->method == method) {
regmatch_t *matches = malloc(sizeof(regmatch_t) * cur->nmatch);
int m = regexec(&cur->regex, path_info, cur->nmatch, matches, 0);
if (m == 0) {
cur->func(matches);
free(matches);
return;
}
free(matches);
}
}
char err[1024] = "No match for ";
strcat(err, method_str);
strcat(err, " \"");
strcat(err, path_info);
strcat(err, "\"");
strcat(err, agt);
error_handler(err);
}
void add_handler(handler *h) {
if (head == NULL) {
head = h;
} else {
last->next = h;
}
last = h;
}
void init_handlers() {
handler *cur = head;
while (cur != NULL) {
if (regcomp(&cur->regex, cur->regex_str, 0) != 0) {
FAIL_WITH_ERROR("could not compile regex");
}
cur = cur->next;
}
}
void cleanup_handlers() {
handler *cur = head;
while (cur != NULL) {
regfree(&cur->regex);
cur = cur->next;
}
}
void set_crash_handler(void (*on_crash)()) {
extern void (*user_segfault)();
user_segfault = on_crash;
}