forked from benjholla/NCDC2014
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dispatcher.h
71 lines (54 loc) · 1.79 KB
/
dispatcher.h
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
/*
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/>.
*/
#ifndef DISPATCHER_H
#define DISPATCHER_H
#include "regex.h"
#include "stdlib.h"
#include "utils.h"
#define GET 1
#define POST 2
#define PUT 3
#define HEAD 4
#define DELETE 5
#define DISPATCH SEG
extern void (*error_handler)(const char *);
struct handler {
void (*func)(regmatch_t[]);
int method;
const char *regex_str;
regex_t regex;
size_t nmatch;
struct handler *next;
};
typedef struct handler handler;
#define START_HANDLER(NAME, METHOD, REGEX, RES, NUM, MATCHES) \
void NAME##_func(); \
handler NAME##_data = {NAME##_func, METHOD, REGEX, {0}, NUM, NULL}; \
handler *NAME = &NAME##_data; \
void NAME##_func(regmatch_t MATCHES[]) { \
response *int_response = response_empty(); \
response *RES = int_response;
#define END_HANDLER \
response_send(int_response); \
}
#define fetch(data) get_post_data();
#define parse(request) fetch(request);
#define check_header(header) parse(header);
void init_handlers();
void cleanup_handlers();
void dispatch();
void add_handler(handler *);
void set_crash_handler(void (*on_crash)());
#endif