-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathrouter.h
86 lines (72 loc) · 2.63 KB
/
router.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2018-2020 SCANOSS LTD
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/**
* router.h
*
* Public structures, constants and declarations for WAYUU Router.
*/
#include <openssl/ssl.h>
#include <stdbool.h>
#ifndef __WAYUU_ROUTER_H
#define __WAYUU_ROUTER_H
#include "http_utils.h"
/**
* MAX_ROUTES: The maximum amount of route definitions in the routing table.
*/
#define MAX_ROUTES 128
#define MAX_ROUTE_PATH 256
#define MAX_PARAM_LENGTH 64
typedef struct route_matcher {
int type;
char prefix[MAX_ROUTE_PATH];
char param[MAX_PARAM_LENGTH];
} route_matcher;
/**
* request_handler: function that handles the request.
*/
typedef void (*request_handler)(api_request *req);
/**
* request_filter: filter to apply to the request. It returns true if successful and false otherwise.
* When filter returns false the router will assume that the request has completed and will not invoke handler.
* The filter must handle error responses.
*/
typedef bool (*request_filter)(api_request *req);
/**
* router_init: Initialises routing table. This method should be called once per instance.
*/
void router_init();
/**
* router_handle_request: Main routing method, searches for request handler in routing table and if found
* delegates request to it, otherwise it returns a default response.
*
*/
void router_handle_request(api_request *request);
/**
* router_add_route: Adds a route to the global routing table.
*
* - Matcher syntax: METHOD:PATH
* - METHOD: The HTTP Method, for now only GET, POST, DELETE are supported
* - PATH: The HTTP Request path, relative to the API mount point (/api). Example: /user/list
* - Path parameters are supported, as well as query parameters.
*/
void router_add_route(char *matcher, request_handler handler, request_filter filter);
void reject_routing_request(api_request *req);
void free_routing_table();
void router_resolve_route(const char *template, route_matcher *matcher);
char *router_extract_param(const char *path, route_matcher *matcher);
#endif