summaryrefslogtreecommitdiffstats
path: root/src/lib-http/http-server-resource.c
blob: dc602e4048b97374c6b0b14a865a3f68138fead2 (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/* Copyright (c) 2013-2018 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "array.h"
#include "bsearch-insert-pos.h"
#include "str-sanitize.h"

#include "http-url.h"
#include "http-server-private.h"

static struct event_category event_category_http_server_resource = {
	.name = "http-server-resource"
};

/*
 * Location
 */

static int
http_server_location_cmp(struct http_server_location *const *loc1,
			 struct http_server_location *const *loc2)
{
	return strcmp((*loc1)->path, (*loc2)->path);
}

static struct http_server_location *
http_server_location_add(struct http_server *server, pool_t pool,
			 const char *path)
{
	struct http_server_location qloc, *loc;
	unsigned int insert_idx;

	i_zero(&qloc);
	qloc.path = path;
	loc = &qloc;

	if (array_bsearch_insert_pos(&server->locations, &loc,
				     http_server_location_cmp, &insert_idx))
		return array_idx_elem(&server->locations, insert_idx);

	loc = p_new(pool, struct http_server_location, 1);
	loc->path = p_strdup(pool, path);
	array_insert(&server->locations, insert_idx, &loc, 1);
	return loc;
}

static int
http_server_location_find(struct http_server *server, const char *path,
			  struct http_server_location **loc_r,
			  const char **sub_path_r)
{
	struct http_server_location qloc, *loc;
	size_t loc_len;
	unsigned int insert_idx;

	*sub_path_r = NULL;
	*loc_r = NULL;

	i_zero(&qloc);
	qloc.path = path;
	loc = &qloc;

	if (array_bsearch_insert_pos(&server->locations, &loc,
				     http_server_location_cmp, &insert_idx)) {
		/* Exact match */
		*loc_r = array_idx_elem(&server->locations, insert_idx);
		*sub_path_r = "";
		return 1;
	}
	if (insert_idx == 0) {
		/* Not found at all */
		return -1;
	}
	loc = array_idx_elem(&server->locations, insert_idx-1);

	loc_len = strlen(loc->path);
	if (!str_begins(path, loc->path)) {
		/* Location isn't a prefix of path */
		return -1;
	} else if (path[loc_len] != '/') {
		/* Match doesn't end at '/' */
		return -1;
	}

	*sub_path_r = &path[loc_len + 1];
	*loc_r = loc;
	return 0;
}

static void
http_server_location_remove(struct http_server *server,
			    struct http_server_location *loc)
{
	struct http_server_location *const *locp;

	array_foreach(&server->locations, locp) {
		if (*locp == loc) {
			array_delete(
				&server->locations,
				array_foreach_idx(&server->locations, locp), 1);
			return;
		}
	}
}

/*
 * Resource
 */

static void http_server_resource_update_event(struct http_server_resource *res)
{
	struct http_server_location *const *locs;
	unsigned int locs_count;

	locs = array_get(&res->locations, &locs_count);
	if (locs_count == 0) {
		event_set_append_log_prefix(res->event, "resource: ");
		return;
	}

	event_add_str(res->event, "path", locs[0]->path);
	event_set_append_log_prefix(
		res->event, t_strdup_printf("resource %s: ",
			str_sanitize(locs[0]->path, 128)));
}

#undef http_server_resource_create
struct http_server_resource *
http_server_resource_create(struct http_server *server, pool_t pool,
			    http_server_resource_callback_t *callback,
			    void *context)
{
	struct http_server_resource *res;

	pool_ref(pool);

	res = p_new(pool, struct http_server_resource, 1);
	res->pool = pool;
	res->server = server;

	res->callback = callback;
	res->context = context;

	p_array_init(&res->locations, pool, 4);

	res->event = event_create(server->event);
	event_add_category(res->event, &event_category_http_server_resource);
	http_server_resource_update_event(res);

	array_append(&server->resources, &res, 1);

	return res;
}

void http_server_resource_free(struct http_server_resource **_res)
{
	struct http_server_resource *res = *_res;
	struct http_server_location *loc;

	if (res == NULL)
		return;

	*_res = NULL;

	e_debug(res->event, "Free");

	if (res->destroy_callback != NULL) {
		res->destroy_callback(res->destroy_context);
		res->destroy_callback = NULL;
	}

	array_foreach_elem(&res->locations, loc)
		http_server_location_remove(res->server, loc);

	event_unref(&res->event);
	pool_unref(&res->pool);
}

pool_t http_server_resource_get_pool(struct http_server_resource *res)
{
	return res->pool;
}

const char *http_server_resource_get_path(struct http_server_resource *res)
{
	struct http_server_location *const *locs;
	unsigned int locs_count;

	locs = array_get(&res->locations, &locs_count);
	i_assert(locs_count > 0);

	return locs[0]->path;
}

struct event *http_server_resource_get_event(struct http_server_resource *res)
{
	return res->event;
}

void http_server_resource_add_location(struct http_server_resource *res,
				       const char *path)
{
	struct http_server_location *loc;

	i_assert(*path == '\0' || *path == '/');

	loc = http_server_location_add(res->server, res->pool, path);
	i_assert(loc->resource == NULL);

	loc->resource = res;
	array_append(&res->locations, &loc, 1);

	if (array_count(&res->locations) == 1)
		http_server_resource_update_event(res);
}

int http_server_resource_find(struct http_server *server, const char *path,
			      struct http_server_resource **res_r,
			      const char **sub_path_r)
{
	struct http_server_location *loc;
	int ret;

	if (path == NULL)
		return -1;

	*res_r = NULL;
	*sub_path_r = NULL;

	ret = http_server_location_find(server, path, &loc, sub_path_r);
	if (ret < 0)
		return -1;

	i_assert(loc->resource != NULL);
	*res_r = loc->resource;
	return ret;
}

bool http_server_resource_callback(struct http_server_request *req)
{
	struct http_server *server = req->server;
	struct http_server_resource *res;
	const char *sub_path;

	switch (req->req.target.format) {
	case HTTP_REQUEST_TARGET_FORMAT_ORIGIN:
		/* According to RFC 7240, Section 5.3.1 only the origin form is
		   applicable to local resources on an origin server.
		*/
		break;
	case HTTP_REQUEST_TARGET_FORMAT_ABSOLUTE:
	case HTTP_REQUEST_TARGET_FORMAT_AUTHORITY:
	case HTTP_REQUEST_TARGET_FORMAT_ASTERISK:
		/* Not applicable for a local resource. */
		return FALSE;
	}

	if (http_server_resource_find(server, req->req.target.url->path,
				      &res, &sub_path) < 0)
		return FALSE;

	e_debug(res->event, "Got request: %s", http_server_request_label(req));

	i_assert(res->callback != NULL);
	res->callback(res->context, req, sub_path);
	return TRUE;
}

#undef http_server_resource_set_destroy_callback
void http_server_resource_set_destroy_callback(struct http_server_resource *res,
					       void (*callback)(void *),
					       void *context)
{
	res->destroy_callback = callback;
	res->destroy_context = context;
}