aboutsummaryrefslogtreecommitdiffstats
path: root/src/libutil/http_router.h
blob: 115ee9b8a86eccfe1d6940ab31dfdd85fe756c1b (plain)
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
143
144
145
146
147
148
149
/*-
 * Copyright 2019 Vsevolod Stakhov
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#ifndef RSPAMD_HTTP_ROUTER_H
#define RSPAMD_HTTP_ROUTER_H

#include "config.h"
#include "http_connection.h"

#ifdef  __cplusplus
extern "C" {
#endif

struct rspamd_http_connection_router;
struct rspamd_http_connection_entry;

typedef int (*rspamd_http_router_handler_t) (struct rspamd_http_connection_entry
											 *conn_ent,
											 struct rspamd_http_message *msg);

typedef void (*rspamd_http_router_error_handler_t) (struct rspamd_http_connection_entry *conn_ent,
													GError *err);

typedef void (*rspamd_http_router_finish_handler_t) (struct rspamd_http_connection_entry *conn_ent);


struct rspamd_http_connection_entry {
	struct rspamd_http_connection_router *rt;
	struct rspamd_http_connection *conn;
	gpointer ud;
	gboolean is_reply;
	gboolean support_gzip;
	struct rspamd_http_connection_entry *prev, *next;
};

struct rspamd_http_connection_router {
	struct rspamd_http_connection_entry *conns;
	GHashTable *paths;
	GHashTable *response_headers;
	GPtrArray *regexps;
	ev_tstamp timeout;
	struct ev_loop *event_loop;
	struct rspamd_http_context *ctx;
	gchar *default_fs_path;
	rspamd_http_router_handler_t unknown_method_handler;
	struct rspamd_cryptobox_keypair *key;
	rspamd_http_router_error_handler_t error_handler;
	rspamd_http_router_finish_handler_t finish_handler;
};

/**
 * Create new http connection router and the associated HTTP connection
 * @param eh error handler callback
 * @param fh finish handler callback
 * @param default_fs_path if not NULL try to serve static files from
 * the specified directory
 * @return
 */
struct rspamd_http_connection_router *rspamd_http_router_new (
		rspamd_http_router_error_handler_t eh,
		rspamd_http_router_finish_handler_t fh,
		ev_tstamp timeout,
		const char *default_fs_path,
		struct rspamd_http_context *ctx);

/**
 * Set encryption key for the HTTP router
 * @param router router structure
 * @param key opaque key structure
 */
void rspamd_http_router_set_key (struct rspamd_http_connection_router *router,
								 struct rspamd_cryptobox_keypair *key);

/**
 * Add new path to the router
 */
void rspamd_http_router_add_path (struct rspamd_http_connection_router *router,
								  const gchar *path, rspamd_http_router_handler_t handler);

/**
 * Add custom header to append to router replies
 * @param router
 * @param name
 * @param value
 */
void rspamd_http_router_add_header (struct rspamd_http_connection_router *router,
									const gchar *name, const gchar *value);

/**
 * Sets method to handle unknown request methods
 * @param router
 * @param handler
 */
void rspamd_http_router_set_unknown_handler (struct rspamd_http_connection_router *router,
											 rspamd_http_router_handler_t handler);

/**
 * Inserts router headers to the outbound message
 * @param router
 * @param msg
 */
void rspamd_http_router_insert_headers (struct rspamd_http_connection_router *router,
										struct rspamd_http_message *msg);

struct rspamd_regexp_s;

/**
 * Adds new pattern to router, regexp object is refcounted by this function
 * @param router
 * @param re
 * @param handler
 */
void rspamd_http_router_add_regexp (struct rspamd_http_connection_router *router,
									struct rspamd_regexp_s *re, rspamd_http_router_handler_t handler);

/**
 * Handle new accepted socket
 * @param router router object
 * @param fd server socket
 * @param ud opaque userdata
 */
void rspamd_http_router_handle_socket (
		struct rspamd_http_connection_router *router,
		gint fd,
		gpointer ud);

/**
 * Free router and all connections associated
 * @param router
 */
void rspamd_http_router_free (struct rspamd_http_connection_router *router);

#ifdef  __cplusplus
}
#endif

#endif